Clover Coverage Report - Atlassian Trusted Apps(Aggregated)
Coverage timestamp: Tue Jun 9 2009 19:34:44 CDT
85   231   25   4.25
0   199   0.29   6.67
20     1.25  
3    
 
 
  TestDefaultRequestMatcher       Line # 10 81 21 94.8% 0.9484536
  TestDefaultRequestMatcher.MockIPMatcher       Line # 201 2 2 100% 1.0
  TestDefaultRequestMatcher.MockURLMatcher       Line # 216 2 2 100% 1.0
 
  (9)
 
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   
 
10    public class TestDefaultRequestMatcher extends TestCase
11    {
 
12  1 toggle public void testAllGood() throws Exception
13    {
14  1 RequestValidator matcher = new DefaultRequestValidator(new IPMatcher()
15    {
 
16  1 toggle public boolean match(String ipAddress)
17    {
18  1 assertEquals("123.12.0.89", ipAddress);
19  1 return true;
20    }
21    }, new URLMatcher()
22    {
 
23  1 toggle 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   
 
37  1 toggle public void testBadIpAddress() throws Exception
38    {
39  1 RequestValidator matcher = new DefaultRequestValidator(new MockIPMatcher(false)
40    {
 
41  1 toggle 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    // expected
58    }
59    }
60   
 
61  1 toggle public void testIpAddressInXForwardedForChecked() throws Exception
62    {
63  1 final List checkIps = new ArrayList();
64  1 RequestValidator matcher = new DefaultRequestValidator(new IPMatcher()
65    {
 
66  2 toggle 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   
 
85  1 toggle public void testMultipleIpAddressesInXForwardedForChecked() throws Exception
86    {
87  1 final List checkIps = new ArrayList();
88  1 RequestValidator matcher = new DefaultRequestValidator(new IPMatcher()
89    {
 
90  4 toggle 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   
 
111  1 toggle public void testBadIpAddressInXForwardedFor() throws Exception
112    {
113  1 RequestValidator matcher = new DefaultRequestValidator(new IPMatcher()
114    {
 
115  2 toggle 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    // expected
134    }
135    }
136   
 
137  1 toggle 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    // expected
153    }
154    }
155   
 
156  1 toggle public void testContextPathRemoval() throws Exception
157    {
158  1 RequestValidator matcher = new DefaultRequestValidator(new MockIPMatcher(true), new URLMatcher()
159    {
 
160  1 toggle 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   
 
175  1 toggle 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    // expected
185    }
186    }
187   
 
188  1 toggle 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    // expected
198    }
199    }
200   
 
201    static class MockIPMatcher implements IPMatcher
202    {
203    final boolean result;
204   
 
205  4 toggle public MockIPMatcher(boolean result)
206    {
207  4 this.result = result;
208    }
209   
 
210  3 toggle public boolean match(String ipAddress)
211    {
212  3 return result;
213    }
214    }
215   
 
216    static class MockURLMatcher implements URLMatcher
217    {
218    final boolean result;
219   
 
220  6 toggle public MockURLMatcher(boolean result)
221    {
222  6 this.result = result;
223    }
224   
 
225  3 toggle public boolean match(String urlPath)
226    {
227  3 return result;
228    }
229    }
230   
231    }