1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
22  */
23 package opengrok.auth.plugin.decoders;
24 
25 import static opengrok.auth.plugin.decoders.OSSOHeaderDecoder.OSSO_COOKIE_TIMESTAMP_HEADER;
26 import static opengrok.auth.plugin.decoders.OSSOHeaderDecoder.OSSO_SUBSCRIBER_DN_HEADER;
27 import static opengrok.auth.plugin.decoders.OSSOHeaderDecoder.OSSO_SUBSCRIBER_HEADER;
28 import static opengrok.auth.plugin.decoders.OSSOHeaderDecoder.OSSO_TIMEOUT_EXCEEDED_HEADER;
29 import static opengrok.auth.plugin.decoders.OSSOHeaderDecoder.OSSO_USER_DN_HEADER;
30 import static opengrok.auth.plugin.decoders.OSSOHeaderDecoder.OSSO_USER_GUID_HEADER;
31 import opengrok.auth.plugin.entity.User;
32 import opengrok.auth.plugin.util.DummyHttpServletRequestUser;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 
37 /**
38  * Test OSSO header decoder.
39  *
40  * @author Krystof Tulinger
41  */
42 public class OSSODecoderTest {
43 
44     DummyHttpServletRequestUser dummyRequest;
45     OSSOHeaderDecoder decoder = new OSSOHeaderDecoder();
46 
47     @Before
setUp()48     public void setUp() {
49         dummyRequest = new DummyHttpServletRequestUser();
50         dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, "5761172f");
51         dummyRequest.setHeader(OSSO_TIMEOUT_EXCEEDED_HEADER, "");
52         dummyRequest.setHeader(OSSO_SUBSCRIBER_DN_HEADER, "");
53         dummyRequest.setHeader(OSSO_SUBSCRIBER_HEADER, "");
54         dummyRequest.setHeader(OSSO_USER_DN_HEADER, "007");
55         dummyRequest.setHeader(OSSO_USER_GUID_HEADER, "123456");
56     }
57 
58     /**
59      * Test of fromRequest method, of class User.
60      */
testAll()61     public void testAll() {
62         dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, "5761172f");
63         dummyRequest.setHeader(OSSO_TIMEOUT_EXCEEDED_HEADER, "false");
64         dummyRequest.setHeader(OSSO_SUBSCRIBER_DN_HEADER, "dn=example.com");
65         dummyRequest.setHeader(OSSO_SUBSCRIBER_HEADER, "example.com");
66         dummyRequest.setHeader(OSSO_USER_DN_HEADER, "dn=specific.dn");
67         dummyRequest.setHeader(OSSO_USER_GUID_HEADER, "123456");
68 
69         User result = decoder.fromRequest(dummyRequest);
70 
71         Assert.assertNotNull(result);
72         Assert.assertEquals("dn=specific.dn", result.getUsername());
73         Assert.assertEquals("123456", result.getId());
74         Assert.assertFalse(result.getTimeouted());
75         Assert.assertEquals(Long.parseLong("1465980719000"), result.getCookieTimestamp().getTime());
76         Assert.assertFalse(result.isTimeouted());
77     }
78 
79     /**
80      * Test of getUserId method, of class User.
81      */
82     @Test
testGetUserId()83     public void testGetUserId() {
84         String[] tests = {
85             "123456",
86             "sd45gfgf5sd4g5ffd54g",
87             "ě5 1g56ew1tč6516re5g1g65d1g65d"
88         };
89 
90         for (int i = 0; i < tests.length; i++) {
91             dummyRequest.setHeader(OSSO_USER_GUID_HEADER, tests[i]);
92             User result = decoder.fromRequest(dummyRequest);
93             Assert.assertNotNull(result);
94             Assert.assertEquals(tests[i], result.getId());
95         }
96     }
97 
98     /**
99      * Test of getUserDn method, of class User.
100      */
101     @Test
testGetUserDn()102     public void testGetUserDn() {
103         String[] tests = {
104             "123456",
105             "sd45gfgf5sd4g5ffd54g",
106             "ě5 1g56ew1tč6516re5g1g65d1g65d"
107         };
108 
109         for (int i = 0; i < tests.length; i++) {
110             dummyRequest.setHeader(OSSO_USER_DN_HEADER, tests[i]);
111             User result = decoder.fromRequest(dummyRequest);
112             Assert.assertNotNull(result);
113             Assert.assertEquals(tests[i], result.getUsername());
114         }
115     }
116 
117     /**
118      * Test of getCookieTimestamp method, of class User.
119      */
120     @Test
testGetCookieTimestamp()121     public void testGetCookieTimestamp() {
122         String[] tests = {
123             "123456",
124             "5761172f",
125             "58d137be",};
126 
127         long expected[] = {
128             1193046000L,
129             1465980719000L,
130             1490106302000L
131         };
132 
133         for (int i = 0; i < tests.length; i++) {
134             dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, tests[i]);
135             User result = decoder.fromRequest(dummyRequest);
136             Assert.assertNotNull(result);
137             Assert.assertEquals(expected[i], result.getCookieTimestamp().getTime());
138         }
139     }
140 
141     /**
142      * Test of getCookieTimestamp method, of class User.
143      */
144     @Test
testInvalidGetCookieTimestamp()145     public void testInvalidGetCookieTimestamp() {
146         User u;
147         String[] tests = {
148             "sd45gfgf5sd4g5ffd54g",
149             "ě5 1g56ew1tč6516re5g1g65d1g65d",
150             "",
151             "ffffx" // not a hex number
152         };
153 
154         for (int i = 0; i < tests.length; i++) {
155             dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, tests[i]);
156             Assert.assertNotNull(u = decoder.fromRequest(dummyRequest));
157             Assert.assertNull(u.getCookieTimestamp());
158         }
159     }
160 
161     /**
162      * Test of getTimeoutExceeded method, of class User.
163      */
164     @Test
testGetTimeouted()165     public void testGetTimeouted() {
166         String[] tests = {
167             "false",
168             "true",
169             "FALSE",
170             "TRUE",
171             "abcd"
172         };
173 
174         boolean[] expected = {
175             false, true, false, true, false
176         };
177 
178         for (int i = 0; i < tests.length; i++) {
179             dummyRequest.setHeader(OSSO_TIMEOUT_EXCEEDED_HEADER, tests[i]);
180             User result = decoder.fromRequest(dummyRequest);
181             if (expected[i]) {
182                 Assert.assertNull(result);
183             } else {
184                 Assert.assertNotNull(result);
185             }
186         }
187     }
188 }
189