| 1 |
|
package com.atlassian.security.auth.trustedapps; |
| 2 |
|
|
| 3 |
|
import java.util.Iterator; |
| 4 |
|
import java.util.LinkedList; |
| 5 |
|
import java.util.List; |
| 6 |
|
import java.util.Set; |
| 7 |
|
import java.util.StringTokenizer; |
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (56) |
Complexity: 15 |
Complexity Density: 0.44 |
|
| 12 |
|
public class DefaultIPMatcher implements IPMatcher |
| 13 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 5 |
Complexity Density: 0.42 |
|
| 14 |
|
private static class AddressMask |
| 15 |
|
{ |
| 16 |
|
private final int address; |
| 17 |
|
private final int mask; |
| 18 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 19 |
5
|
public AddressMask(int address, int mask)... |
| 20 |
|
{ |
| 21 |
5
|
this.address = address; |
| 22 |
5
|
this.mask = mask; |
| 23 |
|
} |
| 24 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 25 |
56
|
public boolean matches(int otherAddress)... |
| 26 |
|
{ |
| 27 |
56
|
return address == (otherAddress & mask); |
| 28 |
|
} |
| 29 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 30 |
5
|
static AddressMask create(int[] pattern)... |
| 31 |
|
{ |
| 32 |
5
|
int address = 0; |
| 33 |
5
|
int mask = 0; |
| 34 |
|
|
| 35 |
25
|
for (int i = 0; i < pattern.length; i++) |
| 36 |
|
{ |
| 37 |
20
|
address = address << 8; |
| 38 |
20
|
mask = mask << 8; |
| 39 |
|
|
| 40 |
20
|
if (pattern[i] != -1) |
| 41 |
|
{ |
| 42 |
15
|
address = address | pattern[i]; |
| 43 |
15
|
mask = mask | 0xFF; |
| 44 |
|
} |
| 45 |
|
} |
| 46 |
|
|
| 47 |
5
|
return new AddressMask(address, mask); |
| 48 |
|
} |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
private static final String WILDCARD = "*"; |
| 52 |
|
|
| 53 |
|
private final List addressMasks; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
@param |
| 59 |
|
@throws |
| 60 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 61 |
18
|
public DefaultIPMatcher(Set patterns) throws IPAddressFormatException... |
| 62 |
|
{ |
| 63 |
18
|
this.addressMasks = new LinkedList(); |
| 64 |
23
|
for (Iterator iterator = patterns.iterator(); iterator.hasNext();) |
| 65 |
|
{ |
| 66 |
21
|
String patternStr = (String) iterator.next(); |
| 67 |
21
|
int[] pattern = parsePatternString(patternStr); |
| 68 |
5
|
addressMasks.add(AddressMask.create(pattern)); |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 7 |
Complexity Density: 0.47 |
|
| 72 |
36
|
private int[] parsePatternString(String patternStr)... |
| 73 |
|
{ |
| 74 |
36
|
int[] pattern = new int[4]; |
| 75 |
36
|
StringTokenizer st = new StringTokenizer(patternStr, "."); |
| 76 |
36
|
if (st.countTokens() != 4) |
| 77 |
|
{ |
| 78 |
2
|
throw new IPAddressFormatException(patternStr); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
128
|
for (int i = 0; i < 4; i++) |
| 82 |
|
{ |
| 83 |
109
|
final String token = st.nextToken().trim(); |
| 84 |
109
|
if (WILDCARD.equals(token)) |
| 85 |
|
{ |
| 86 |
5
|
pattern[i] = -1; |
| 87 |
|
} |
| 88 |
|
else |
| 89 |
|
{ |
| 90 |
104
|
try |
| 91 |
|
{ |
| 92 |
104
|
int value = Integer.valueOf(token).intValue(); |
| 93 |
|
|
| 94 |
99
|
if ((value < 0) || value > 255) |
| 95 |
|
{ |
| 96 |
10
|
throw new IPAddressFormatException(patternStr); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
89
|
pattern[i] = value; |
| 100 |
|
} |
| 101 |
|
catch (NumberFormatException e) |
| 102 |
|
{ |
| 103 |
5
|
throw new IPAddressFormatException(patternStr); |
| 104 |
|
} |
| 105 |
|
} |
| 106 |
|
} |
| 107 |
19
|
return pattern; |
| 108 |
|
} |
| 109 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 110 |
16
|
public boolean match(String ipAddress)... |
| 111 |
|
{ |
| 112 |
16
|
if (addressMasks.isEmpty()) |
| 113 |
|
{ |
| 114 |
1
|
return true; |
| 115 |
|
} |
| 116 |
|
|
| 117 |
15
|
int address = toAddress(ipAddress); |
| 118 |
|
|
| 119 |
62
|
for (Iterator iterator = addressMasks.iterator(); iterator.hasNext();) |
| 120 |
|
{ |
| 121 |
56
|
AddressMask addressMask = (AddressMask) iterator.next(); |
| 122 |
56
|
if (addressMask.matches(address)) |
| 123 |
|
{ |
| 124 |
8
|
return true; |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
6
|
return false; |
| 128 |
|
} |
| 129 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 130 |
15
|
private int toAddress(String ipAddress)... |
| 131 |
|
{ |
| 132 |
15
|
int address = 0; |
| 133 |
15
|
int[] parsedIPAddr = parsePatternString(ipAddress); |
| 134 |
70
|
for (int i = 0; i < parsedIPAddr.length; i++) |
| 135 |
|
{ |
| 136 |
56
|
address = address << 8; |
| 137 |
56
|
address = address | parsedIPAddr[i]; |
| 138 |
|
} |
| 139 |
14
|
return address; |
| 140 |
|
} |
| 141 |
|
} |