| 1 |
|
package com.atlassian.security.auth.trustedapps; |
| 2 |
|
|
| 3 |
|
import com.mockobjects.servlet.MockHttpServletRequest; |
| 4 |
|
|
| 5 |
|
import java.util.ArrayList; |
| 6 |
|
import java.util.List; |
| 7 |
|
|
| 8 |
|
import junit.framework.TestCase; |
| 9 |
|
|
|
|
|
| 94.8% |
Uncovered Elements: 5 (97) |
Complexity: 21 |
Complexity Density: 0.26 |
|
| 10 |
|
public class TestDefaultRequestMatcher extends TestCase |
| 11 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 12 |
1
|
public void testAllGood() throws Exception... |
| 13 |
|
{ |
| 14 |
1
|
RequestValidator matcher = new DefaultRequestValidator(new IPMatcher() |
| 15 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 16 |
1
|
public boolean match(String ipAddress)... |
| 17 |
|
{ |
| 18 |
1
|
assertEquals("123.12.0.89", ipAddress); |
| 19 |
1
|
return true; |
| 20 |
|
} |
| 21 |
|
}, new URLMatcher() |
| 22 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 23 |
1
|
public boolean match(String urlPath)... |
| 24 |
|
{ |
| 25 |
1
|
assertEquals("/some/request/url", urlPath); |
| 26 |
1
|
return true; |
| 27 |
|
} |
| 28 |
|
}); |
| 29 |
1
|
final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 30 |
1
|
request.setupGetRemoteAddr("123.12.0.89"); |
| 31 |
1
|
request.setupGetRequestURI("/some/request/url"); |
| 32 |
1
|
request.setupGetContextPath(""); |
| 33 |
1
|
request.setupAddHeader("X-Forwarded-For", null); |
| 34 |
1
|
matcher.validate(request); |
| 35 |
|
} |
| 36 |
|
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
1
PASS
|
|
| 37 |
1
|
public void testBadIpAddress() throws Exception... |
| 38 |
|
{ |
| 39 |
1
|
RequestValidator matcher = new DefaultRequestValidator(new MockIPMatcher(false) |
| 40 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 41 |
1
|
public boolean match(String ipAddress)... |
| 42 |
|
{ |
| 43 |
1
|
assertEquals("123.45.67.89", ipAddress); |
| 44 |
1
|
return super.match(ipAddress); |
| 45 |
|
} |
| 46 |
|
}, new MockURLMatcher(true)); |
| 47 |
|
|
| 48 |
1
|
try |
| 49 |
|
{ |
| 50 |
1
|
final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 51 |
1
|
request.setupGetRemoteAddr("123.45.67.89"); |
| 52 |
1
|
matcher.validate(request); |
| 53 |
0
|
fail("should have thrown invalid ip"); |
| 54 |
|
} |
| 55 |
|
catch (InvalidRemoteAddressException e) |
| 56 |
|
{ |
| 57 |
|
|
| 58 |
|
} |
| 59 |
|
} |
| 60 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1
PASS
|
|
| 61 |
1
|
public void testIpAddressInXForwardedForChecked() throws Exception... |
| 62 |
|
{ |
| 63 |
1
|
final List checkIps = new ArrayList(); |
| 64 |
1
|
RequestValidator matcher = new DefaultRequestValidator(new IPMatcher() |
| 65 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 66 |
2
|
public boolean match(String ipAddress)... |
| 67 |
|
{ |
| 68 |
2
|
checkIps.add(ipAddress); |
| 69 |
2
|
return true; |
| 70 |
|
} |
| 71 |
|
}, new MockURLMatcher(true)); |
| 72 |
|
|
| 73 |
1
|
final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 74 |
1
|
request.setupGetRemoteAddr("123.45.67.89"); |
| 75 |
1
|
request.setupAddHeader("X-Forwarded-For", "192.68.0.123"); |
| 76 |
1
|
request.setupGetRequestURI("/some/request/url"); |
| 77 |
1
|
request.setupGetContextPath(""); |
| 78 |
1
|
matcher.validate(request); |
| 79 |
|
|
| 80 |
1
|
assertEquals(2, checkIps.size()); |
| 81 |
1
|
assertTrue(checkIps.contains("123.45.67.89")); |
| 82 |
1
|
assertTrue(checkIps.contains("192.68.0.123")); |
| 83 |
|
} |
| 84 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1
PASS
|
|
| 85 |
1
|
public void testMultipleIpAddressesInXForwardedForChecked() throws Exception... |
| 86 |
|
{ |
| 87 |
1
|
final List checkIps = new ArrayList(); |
| 88 |
1
|
RequestValidator matcher = new DefaultRequestValidator(new IPMatcher() |
| 89 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 90 |
4
|
public boolean match(String ipAddress)... |
| 91 |
|
{ |
| 92 |
4
|
checkIps.add(ipAddress); |
| 93 |
4
|
return true; |
| 94 |
|
} |
| 95 |
|
}, new MockURLMatcher(true)); |
| 96 |
|
|
| 97 |
1
|
final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 98 |
1
|
request.setupGetRemoteAddr("123.45.67.89"); |
| 99 |
1
|
request.setupAddHeader("X-Forwarded-For", "192.68.0.123, 192.1.2.3, 192.4.5.6"); |
| 100 |
1
|
request.setupGetRequestURI("/some/request/url"); |
| 101 |
1
|
request.setupGetContextPath(""); |
| 102 |
1
|
matcher.validate(request); |
| 103 |
|
|
| 104 |
1
|
assertEquals(4, checkIps.size()); |
| 105 |
1
|
assertTrue(checkIps.contains("123.45.67.89")); |
| 106 |
1
|
assertTrue(checkIps.contains("192.68.0.123")); |
| 107 |
1
|
assertTrue(checkIps.contains("192.1.2.3")); |
| 108 |
1
|
assertTrue(checkIps.contains("192.4.5.6")); |
| 109 |
|
} |
| 110 |
|
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1
PASS
|
|
| 111 |
1
|
public void testBadIpAddressInXForwardedFor() throws Exception... |
| 112 |
|
{ |
| 113 |
1
|
RequestValidator matcher = new DefaultRequestValidator(new IPMatcher() |
| 114 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 115 |
2
|
public boolean match(String ipAddress)... |
| 116 |
|
{ |
| 117 |
2
|
return "123.45.67.89".equals(ipAddress); |
| 118 |
|
} |
| 119 |
|
}, new MockURLMatcher(true)); |
| 120 |
|
|
| 121 |
1
|
final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 122 |
1
|
request.setupGetRemoteAddr("123.45.67.89"); |
| 123 |
1
|
request.setupGetRequestURI("/some/request/url"); |
| 124 |
1
|
request.setupGetContextPath(""); |
| 125 |
1
|
request.setupAddHeader("X-Forwarded-For", "192.68.0.123"); |
| 126 |
1
|
try |
| 127 |
|
{ |
| 128 |
1
|
matcher.validate(request); |
| 129 |
0
|
fail("Should have thrown illegal xforwarded ex"); |
| 130 |
|
} |
| 131 |
|
catch (InvalidXForwardedForAddressException e) |
| 132 |
|
{ |
| 133 |
|
|
| 134 |
|
} |
| 135 |
|
} |
| 136 |
|
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1
PASS
|
|
| 137 |
1
|
public void testBadUrl() throws Exception... |
| 138 |
|
{ |
| 139 |
1
|
RequestValidator matcher = new DefaultRequestValidator(new MockIPMatcher(true), new MockURLMatcher(false)); |
| 140 |
1
|
try |
| 141 |
|
{ |
| 142 |
1
|
final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 143 |
1
|
request.setupGetRemoteAddr("123.12.0.89"); |
| 144 |
1
|
request.setupGetRequestURI("/some/request/url"); |
| 145 |
1
|
request.setupGetContextPath(""); |
| 146 |
1
|
request.setupAddHeader("X-Forwarded-For", null); |
| 147 |
1
|
matcher.validate(request); |
| 148 |
0
|
fail("should have thrown invalid url"); |
| 149 |
|
} |
| 150 |
|
catch (InvalidRequestUrlException e) |
| 151 |
|
{ |
| 152 |
|
|
| 153 |
|
} |
| 154 |
|
} |
| 155 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 156 |
1
|
public void testContextPathRemoval() throws Exception... |
| 157 |
|
{ |
| 158 |
1
|
RequestValidator matcher = new DefaultRequestValidator(new MockIPMatcher(true), new URLMatcher() |
| 159 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 160 |
1
|
public boolean match(String urlPath)... |
| 161 |
|
{ |
| 162 |
1
|
assertEquals("/some/request/url", urlPath); |
| 163 |
1
|
return true; |
| 164 |
|
} |
| 165 |
|
}); |
| 166 |
|
|
| 167 |
1
|
final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 168 |
1
|
request.setupGetRemoteAddr("123.12.0.89"); |
| 169 |
1
|
request.setupGetRequestURI("/context/some/request/url"); |
| 170 |
1
|
request.setupGetContextPath("/context"); |
| 171 |
1
|
request.setupAddHeader("X-Forwarded-For", null); |
| 172 |
1
|
matcher.validate(request); |
| 173 |
|
} |
| 174 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
1
PASS
|
|
| 175 |
1
|
public void testNullIpMatcher() throws Exception... |
| 176 |
|
{ |
| 177 |
1
|
try |
| 178 |
|
{ |
| 179 |
1
|
new DefaultRequestValidator(null, new MockURLMatcher(true)); |
| 180 |
0
|
fail("should have thrown NPE"); |
| 181 |
|
} |
| 182 |
|
catch (IllegalArgumentException e) |
| 183 |
|
{ |
| 184 |
|
|
| 185 |
|
} |
| 186 |
|
} |
| 187 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
1
PASS
|
|
| 188 |
1
|
public void testNullUrlMatcher() throws Exception... |
| 189 |
|
{ |
| 190 |
1
|
try |
| 191 |
|
{ |
| 192 |
1
|
new DefaultRequestValidator(new MockIPMatcher(true), null); |
| 193 |
0
|
fail("should have thrown NPE"); |
| 194 |
|
} |
| 195 |
|
catch (IllegalArgumentException e) |
| 196 |
|
{ |
| 197 |
|
|
| 198 |
|
} |
| 199 |
|
} |
| 200 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 201 |
|
static class MockIPMatcher implements IPMatcher |
| 202 |
|
{ |
| 203 |
|
final boolean result; |
| 204 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 205 |
4
|
public MockIPMatcher(boolean result)... |
| 206 |
|
{ |
| 207 |
4
|
this.result = result; |
| 208 |
|
} |
| 209 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 210 |
3
|
public boolean match(String ipAddress)... |
| 211 |
|
{ |
| 212 |
3
|
return result; |
| 213 |
|
} |
| 214 |
|
} |
| 215 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 216 |
|
static class MockURLMatcher implements URLMatcher |
| 217 |
|
{ |
| 218 |
|
final boolean result; |
| 219 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 220 |
6
|
public MockURLMatcher(boolean result)... |
| 221 |
|
{ |
| 222 |
6
|
this.result = result; |
| 223 |
|
} |
| 224 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 225 |
3
|
public boolean match(String urlPath)... |
| 226 |
|
{ |
| 227 |
3
|
return result; |
| 228 |
|
} |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
} |