1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.shared.interceptors;
16
17 import org.apache.log4j.Logger;
18 import org.junit.Before;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.springframework.mock.web.MockHttpServletRequest;
24 import org.springframework.mock.web.MockHttpServletResponse;
25 import org.springframework.mock.web.MockServletContext;
26
27 import javax.servlet.http.Cookie;
28
29 import java.util.Properties;
30
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Mockito.when;
35
36
37
38
39 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
40 public class NativeCookieInterceptorTest {
41 public static final Logger LOG = Logger.getLogger(NativeCookieInterceptorTest.class);
42
43 private static MockServletContext servletContext;
44 private static final String PHONEGAP = "phonegap";
45 private static final String PLATFORM = "platform";
46 private static final String USER_AGENT = "User-Agent";
47 private static final String PLATFORM_VALUES[] = {"iPhone","iPad","iPod","Macintosh","Android","Blackberry","BlackBerry","MSIE","GARBAGE"};
48 private static final String RIM = "RIM-Widget";
49 private static final String RIM_VALUE = "KME-Blackberry-Application";
50 private static final String NATIVE_PARAM = "isNative";
51 private static final String NATIVE_COOKIE = "native";
52 private static final String YES = "yes";
53 private static final String NO = "no";
54 private static final String EMPTY = "";
55 private static final String VERSION = "2.2";
56
57 private NativeCookieInterceptor interceptor;
58
59 @Mock
60 private java.util.Properties kmeProperties;
61
62 @BeforeClass
63 public static void init() {
64 servletContext = new MockServletContext();
65 }
66
67 @Before
68 public void preTest() {
69 this.setInterceptor(new NativeCookieInterceptor());
70 this.getInterceptor().setKmeProperties(this.getKmeProperties());
71 when(getInterceptor().getKmeProperties().getProperty("cookie.max.age","3600")).thenReturn("3600");
72 }
73
74 @Test
75 public void testCheckPhoneGap() throws Exception {
76 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
77 MockHttpServletResponse response = new MockHttpServletResponse();
78
79 request.addHeader(USER_AGENT,PLATFORM_VALUES[0]);
80
81 request.addParameter(PHONEGAP,EMPTY);
82
83 when(getInterceptor().getKmeProperties().getProperty("kme.secure.cookie","false")).thenReturn("false");
84
85 boolean bogus = getInterceptor().preHandle(request,response,null);
86 assertTrue("This test will never fail.",bogus);
87
88 request.removeAllParameters();
89 request.addParameter(PHONEGAP,VERSION);
90 bogus = getInterceptor().preHandle(request,response,null);
91 assertTrue("This test will never fail.", bogus);
92
93 request.removeAllParameters();
94 Cookie cookieMonster = new Cookie(PHONEGAP,EMPTY);
95 request.setCookies(cookieMonster);
96 bogus = getInterceptor().preHandle(request,response,null);
97 assertTrue("This test will never fail.", bogus);
98
99 when(getInterceptor().getKmeProperties().getProperty("kme.secure.cookie","false")).thenReturn("true");
100
101 request.removeAllParameters();
102 request.addParameter(PHONEGAP, EMPTY);
103 cookieMonster = new Cookie(PHONEGAP,VERSION);
104 request.setCookies(cookieMonster);
105 bogus = getInterceptor().preHandle(request,response,null);
106 assertTrue("This test will never fail.",bogus);
107 }
108
109 @Test
110 public void testCheckPlatform() throws Exception {
111 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
112 MockHttpServletResponse response = new MockHttpServletResponse();
113
114 when(getInterceptor().getKmeProperties().getProperty("kme.secure.cookie","false")).thenReturn("false");
115
116 request.addHeader(USER_AGENT,PLATFORM_VALUES[0]);
117
118 request.addParameter(PHONEGAP,VERSION);
119 request.addParameter(PLATFORM,EMPTY);
120 request.addHeader(RIM,EMPTY);
121 boolean bogus = getInterceptor().preHandle(request,response,null);
122 assertTrue("This test will never fail.",bogus);
123
124 request.removeAllParameters();
125
126 request.addParameter(PHONEGAP,EMPTY);
127 request.addParameter(PLATFORM,EMPTY);
128 bogus = getInterceptor().preHandle(request,response,null);
129 assertTrue("This test will never fail.",bogus);
130
131 request.removeAllParameters();
132
133 request.addParameter(PHONEGAP,VERSION);
134 request.addParameter(PLATFORM,PLATFORM_VALUES[0]);
135 bogus = getInterceptor().preHandle(request,response,null);
136 assertTrue("This test will never fail.",bogus);
137
138 request.removeAllParameters();
139
140 Cookie[] cookieMonster = new Cookie[2];
141 cookieMonster[0] = new Cookie(PHONEGAP,EMPTY);
142 cookieMonster[1] = new Cookie(PLATFORM,EMPTY);
143 request.setCookies(cookieMonster);
144 bogus = getInterceptor().preHandle(request,response,null);
145 assertTrue("This test will never fail.",bogus);
146
147 when(getInterceptor().getKmeProperties().getProperty("kme.secure.cookie","false")).thenReturn("true");
148
149 request.removeAllParameters();
150
151 request.addParameter(PHONEGAP,VERSION);
152 cookieMonster = new Cookie[2];
153 cookieMonster[0] = new Cookie(PHONEGAP,EMPTY);
154 cookieMonster[1] = new Cookie(PLATFORM,EMPTY);
155 request.setCookies(cookieMonster);
156 bogus = getInterceptor().preHandle(request,response,null);
157 assertTrue("This test will never fail.",bogus);
158
159 request.removeAllParameters();
160
161 request.addParameter(PHONEGAP,VERSION);
162 cookieMonster = new Cookie[2];
163 cookieMonster[0] = new Cookie(PHONEGAP,EMPTY);
164 cookieMonster[1] = new Cookie(PLATFORM,PLATFORM_VALUES[0]);
165 request.setCookies(cookieMonster);
166 bogus = getInterceptor().preHandle(request,response,null);
167 assertTrue("This test will never fail.",bogus);
168 }
169
170 @Test
171 public void testUserAgents() throws Exception {
172 for( String s: PLATFORM_VALUES ) {
173 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
174 MockHttpServletResponse response = new MockHttpServletResponse();
175 request.removeAllParameters();
176 request.addParameter(PHONEGAP,EMPTY);
177 request.addHeader(USER_AGENT,s);
178 boolean bogus = getInterceptor().preHandle(request,response,null);
179 assertTrue("This test will never fail.",bogus);
180 }
181
182 for( String s: PLATFORM_VALUES ) {
183 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
184 MockHttpServletResponse response = new MockHttpServletResponse();
185 request.removeAllParameters();
186 request.addParameter(PHONEGAP,VERSION);
187 request.addHeader(USER_AGENT,s);
188 boolean bogus = getInterceptor().preHandle(request,response,null);
189 assertTrue("This test will never fail.",bogus);
190 }
191
192 for( String s: PLATFORM_VALUES ) {
193 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
194 MockHttpServletResponse response = new MockHttpServletResponse();
195 request.removeAllParameters();
196 Cookie[] cookieMonster = new Cookie[2];
197 cookieMonster[0] = new Cookie(PHONEGAP,EMPTY);
198 cookieMonster[1] = new Cookie(PLATFORM,EMPTY);
199 request.setCookies(cookieMonster);
200 request.addHeader(USER_AGENT,s);
201 boolean bogus = getInterceptor().preHandle(request,response,null);
202 assertTrue("This test will never fail.",bogus);
203 }
204
205 for( String s: PLATFORM_VALUES ) {
206 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
207 MockHttpServletResponse response = new MockHttpServletResponse();
208 request.removeAllParameters();
209 Cookie cookieMonster = new Cookie(PHONEGAP,VERSION);
210 request.setCookies(cookieMonster);
211 request.addHeader(USER_AGENT,s);
212 boolean bogus = getInterceptor().preHandle(request,response,null);
213 assertTrue("This test will never fail.",bogus);
214 }
215
216 for( String s: PLATFORM_VALUES ) {
217 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
218 MockHttpServletResponse response = new MockHttpServletResponse();
219 request.removeAllParameters();
220 request.addParameter(PLATFORM,EMPTY);
221 Cookie cookieMonster = new Cookie(PHONEGAP,VERSION);
222 request.setCookies(cookieMonster);
223 request.addHeader(USER_AGENT,s);
224 boolean bogus = getInterceptor().preHandle(request,response,null);
225 assertTrue("This test will never fail.",bogus);
226 }
227 }
228
229 @Test
230 public void testRIMHeader() {
231 try {
232 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
233 MockHttpServletResponse response = new MockHttpServletResponse();
234 Cookie cookieMonster = new Cookie(PHONEGAP,VERSION);
235 request.setCookies(cookieMonster);
236 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
237 request.addHeader(RIM,EMPTY);
238 boolean bogus = getInterceptor().preHandle(request,response,null);
239 assertTrue("This test will never fail.",bogus);
240 } catch(Exception e ) {
241 LOG.error(e.getLocalizedMessage(),e);
242 fail("Exception thrown in code being tested.");
243 }
244 try {
245 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
246 MockHttpServletResponse response = new MockHttpServletResponse();
247 Cookie cookieMonster = new Cookie(PHONEGAP,VERSION);
248 request.setCookies(cookieMonster);
249 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
250 request.addHeader(RIM,RIM_VALUE);
251 boolean bogus = getInterceptor().preHandle(request,response,null);
252 assertTrue("This test will never fail.",bogus);
253 } catch(Exception e ) {
254 LOG.error(e.getLocalizedMessage(),e);
255 fail("Exception thrown in code being tested.");
256 }
257 try {
258 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
259 MockHttpServletResponse response = new MockHttpServletResponse();
260 Cookie cookieMonster = new Cookie(PHONEGAP,VERSION);
261 request.setCookies(cookieMonster);
262 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
263 request.addHeader(RIM,NO);
264 boolean bogus = getInterceptor().preHandle(request,response,null);
265 assertTrue("This test will never fail.",bogus);
266 } catch(Exception e ) {
267 LOG.error(e.getLocalizedMessage(),e);
268 fail("Exception thrown in code being tested.");
269 }
270 }
271
272 @Test
273 public void testNative() {
274 try {
275 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
276 MockHttpServletResponse response = new MockHttpServletResponse();
277 request.addParameter(NATIVE_PARAM,EMPTY);
278 Cookie[] cookieMonster = new Cookie[2];
279 cookieMonster[0] = new Cookie(PHONEGAP,VERSION);
280 cookieMonster[1] = new Cookie(PLATFORM,VERSION);
281 request.setCookies(cookieMonster);
282 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
283 request.addHeader(RIM,NO);
284 boolean bogus = getInterceptor().preHandle(request,response,null);
285 assertTrue("This test will never fail.",bogus);
286 } catch(Exception e ) {
287 LOG.error(e.getLocalizedMessage(),e);
288 fail("Exception thrown in code being tested.");
289 }
290 try {
291 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
292 MockHttpServletResponse response = new MockHttpServletResponse();
293 request.addParameter(NATIVE_PARAM,YES);
294 Cookie[] cookieMonster = new Cookie[2];
295 cookieMonster[0] = new Cookie(PHONEGAP,VERSION);
296 cookieMonster[1] = new Cookie(PLATFORM,VERSION);
297 request.setCookies(cookieMonster);
298 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
299 request.addHeader(RIM,NO);
300 boolean bogus = getInterceptor().preHandle(request,response,null);
301 assertTrue("This test will never fail.",bogus);
302 } catch(Exception e ) {
303 LOG.error(e.getLocalizedMessage(),e);
304 fail("Exception thrown in code being tested.");
305 }
306 try {
307 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
308 MockHttpServletResponse response = new MockHttpServletResponse();
309 request.addParameter(NATIVE_PARAM,NO);
310 Cookie[] cookieMonster = new Cookie[2];
311 cookieMonster[0] = new Cookie(PHONEGAP,VERSION);
312 cookieMonster[1] = new Cookie(PLATFORM,VERSION);
313 request.setCookies(cookieMonster);
314 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
315 request.addHeader(RIM,NO);
316 boolean bogus = getInterceptor().preHandle(request,response,null);
317 assertTrue("This test will never fail.",bogus);
318 } catch(Exception e ) {
319 LOG.error(e.getLocalizedMessage(),e);
320 fail("Exception thrown in code being tested.");
321 }
322 try {
323 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
324 MockHttpServletResponse response = new MockHttpServletResponse();
325 Cookie[] cookieMonster = new Cookie[3];
326 cookieMonster[0] = new Cookie(PHONEGAP,VERSION);
327 cookieMonster[1] = new Cookie(PLATFORM,VERSION);
328 cookieMonster[2] = new Cookie(NATIVE_COOKIE,EMPTY);
329 request.setCookies(cookieMonster);
330 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
331 request.addHeader(RIM,NO);
332 boolean bogus = getInterceptor().preHandle(request,response,null);
333 assertTrue("This test will never fail.",bogus);
334 } catch(Exception e ) {
335 LOG.error(e.getLocalizedMessage(),e);
336 fail("Exception thrown in code being tested.");
337 }
338 try {
339 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
340 MockHttpServletResponse response = new MockHttpServletResponse();
341 Cookie[] cookieMonster = new Cookie[3];
342 cookieMonster[0] = new Cookie(PHONEGAP,VERSION);
343 cookieMonster[1] = new Cookie(PLATFORM,VERSION);
344 cookieMonster[2] = new Cookie(NATIVE_COOKIE,YES);
345 request.setCookies(cookieMonster);
346 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
347 request.addHeader(RIM,NO);
348 boolean bogus = getInterceptor().preHandle(request,response,null);
349 assertTrue("This test will never fail.",bogus);
350 } catch(Exception e ) {
351 LOG.error(e.getLocalizedMessage(),e);
352 fail("Exception thrown in code being tested.");
353 }
354 try {
355 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
356 MockHttpServletResponse response = new MockHttpServletResponse();
357 Cookie[] cookieMonster = new Cookie[3];
358 cookieMonster[0] = new Cookie(PHONEGAP,VERSION);
359 cookieMonster[1] = new Cookie(PLATFORM,VERSION);
360 cookieMonster[2] = new Cookie(NATIVE_COOKIE,NO);
361 request.setCookies(cookieMonster);
362 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
363 request.addHeader(RIM,NO);
364 boolean bogus = getInterceptor().preHandle(request,response,null);
365 assertTrue("This test will never fail.",bogus);
366 } catch(Exception e ) {
367 LOG.error(e.getLocalizedMessage(),e);
368 fail("Exception thrown in code being tested.");
369 }
370 try {
371 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
372 MockHttpServletResponse response = new MockHttpServletResponse();
373 Cookie[] cookieMonster = new Cookie[3];
374 cookieMonster[0] = new Cookie(PHONEGAP,EMPTY);
375 cookieMonster[1] = new Cookie(PLATFORM,VERSION);
376 cookieMonster[2] = new Cookie(NATIVE_COOKIE,YES);
377 request.setCookies(cookieMonster);
378 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
379 request.addHeader(RIM,NO);
380 boolean bogus = getInterceptor().preHandle(request,response,null);
381 assertTrue("This test will never fail.",bogus);
382 } catch(Exception e ) {
383 LOG.error(e.getLocalizedMessage(),e);
384 fail("Exception thrown in code being tested.");
385 }
386 }
387
388 @Test
389 public void testZeroLengthCookieArray() {
390 try {
391 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
392 MockHttpServletResponse response = new MockHttpServletResponse();
393 Cookie[] cookieMonster = new Cookie[0];
394 request.setCookies(cookieMonster);
395 request.addHeader(USER_AGENT,PLATFORM_VALUES[1]);
396 request.addHeader(RIM,NO);
397 boolean bogus = getInterceptor().preHandle(request,response,null);
398 assertTrue("This test will never fail.",bogus);
399 } catch(Exception e ) {
400 LOG.error(e.getLocalizedMessage(),e);
401 fail("Exception thrown in code being tested.");
402 }
403 }
404
405 public static MockServletContext getServletContext() {
406 return servletContext;
407 }
408
409 public static void setServletContext(MockServletContext servletContext) {
410 NativeCookieInterceptorTest.servletContext = servletContext;
411 }
412
413 public NativeCookieInterceptor getInterceptor() {
414 return interceptor;
415 }
416
417 public void setInterceptor(NativeCookieInterceptor interceptor) {
418 this.interceptor = interceptor;
419 }
420
421 public Properties getKmeProperties() {
422 return kmeProperties;
423 }
424
425 public void setKmeProperties(Properties kmeProperties) {
426 this.kmeProperties = kmeProperties;
427 }
428 }