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, 2021, 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 static org.junit.jupiter.api.Assertions.assertEquals; 32 import static org.junit.jupiter.api.Assertions.assertFalse; 33 import static org.junit.jupiter.api.Assertions.assertNotNull; 34 import static org.junit.jupiter.api.Assertions.assertNull; 35 36 import opengrok.auth.plugin.entity.User; 37 import opengrok.auth.plugin.util.DummyHttpServletRequestUser; 38 import org.junit.jupiter.api.BeforeEach; 39 import org.junit.jupiter.api.Test; 40 41 /** 42 * Test OSSO header decoder. 43 * @author Krystof Tulinger 44 */ 45 public class OSSODecoderTest { 46 47 DummyHttpServletRequestUser dummyRequest; 48 OSSOHeaderDecoder decoder = new OSSOHeaderDecoder(); 49 50 @BeforeEach setUp()51 public void setUp() { 52 dummyRequest = new DummyHttpServletRequestUser(); 53 dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, "5761172f"); 54 dummyRequest.setHeader(OSSO_TIMEOUT_EXCEEDED_HEADER, ""); 55 dummyRequest.setHeader(OSSO_SUBSCRIBER_DN_HEADER, ""); 56 dummyRequest.setHeader(OSSO_SUBSCRIBER_HEADER, ""); 57 dummyRequest.setHeader(OSSO_USER_DN_HEADER, "007"); 58 dummyRequest.setHeader(OSSO_USER_GUID_HEADER, "123456"); 59 } 60 61 /** 62 * Test of fromRequest method, of class User. 63 */ testAll()64 public void testAll() { 65 dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, "5761172f"); 66 dummyRequest.setHeader(OSSO_TIMEOUT_EXCEEDED_HEADER, "false"); 67 dummyRequest.setHeader(OSSO_SUBSCRIBER_DN_HEADER, "dn=example.com"); 68 dummyRequest.setHeader(OSSO_SUBSCRIBER_HEADER, "example.com"); 69 dummyRequest.setHeader(OSSO_USER_DN_HEADER, "dn=specific.dn"); 70 dummyRequest.setHeader(OSSO_USER_GUID_HEADER, "123456"); 71 72 User result = decoder.fromRequest(dummyRequest); 73 74 assertNotNull(result); 75 assertEquals("dn=specific.dn", result.getUsername()); 76 assertEquals("123456", result.getId()); 77 assertFalse(result.getTimeouted()); 78 assertEquals(Long.parseLong("1465980719000"), result.getCookieTimestamp().getTime()); 79 assertFalse(result.isTimeouted()); 80 } 81 82 /** 83 * Test of getUserId method, of class User. 84 */ 85 @Test testGetUserId()86 public void testGetUserId() { 87 String[] tests = { 88 "123456", 89 "sd45gfgf5sd4g5ffd54g", 90 "ě5 1g56ew1tč6516re5g1g65d1g65d" 91 }; 92 93 for (String test : tests) { 94 dummyRequest.setHeader(OSSO_USER_GUID_HEADER, test); 95 User result = decoder.fromRequest(dummyRequest); 96 assertNotNull(result); 97 assertEquals(test, result.getId()); 98 } 99 } 100 101 /** 102 * Test of getUserDn method, of class User. 103 */ 104 @Test testGetUserDn()105 public void testGetUserDn() { 106 String[] tests = { 107 "123456", 108 "sd45gfgf5sd4g5ffd54g", 109 "ě5 1g56ew1tč6516re5g1g65d1g65d" 110 }; 111 112 for (String test : tests) { 113 dummyRequest.setHeader(OSSO_USER_DN_HEADER, test); 114 User result = decoder.fromRequest(dummyRequest); 115 assertNotNull(result); 116 assertEquals(test, result.getUsername()); 117 } 118 } 119 120 /** 121 * Test of getCookieTimestamp method, of class User. 122 */ 123 @Test testGetCookieTimestamp()124 public void testGetCookieTimestamp() { 125 String[] tests = {"123456", "5761172f", "58d137be"}; 126 long[] expected = {1193046000L, 1465980719000L, 1490106302000L}; 127 128 for (int i = 0; i < tests.length; i++) { 129 dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, tests[i]); 130 User result = decoder.fromRequest(dummyRequest); 131 assertNotNull(result); 132 assertEquals(expected[i], result.getCookieTimestamp().getTime()); 133 } 134 } 135 136 /** 137 * Test of getCookieTimestamp method, of class User. 138 */ 139 @Test testInvalidGetCookieTimestamp()140 public void testInvalidGetCookieTimestamp() { 141 String[] tests = { 142 "sd45gfgf5sd4g5ffd54g", 143 "ě5 1g56ew1tč6516re5g1g65d1g65d", 144 "", 145 "ffffx" // not a hex number 146 }; 147 148 for (String test : tests) { 149 User u; 150 dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, test); 151 assertNotNull(u = decoder.fromRequest(dummyRequest)); 152 assertNull(u.getCookieTimestamp()); 153 } 154 } 155 156 /** 157 * Test of getTimeoutExceeded method, of class User. 158 */ 159 @Test testGetTimeouted()160 public void testGetTimeouted() { 161 String[] tests = {"false", "true", "FALSE", "TRUE", "abcd"}; 162 boolean[] expected = {false, true, false, true, false}; 163 164 for (int i = 0; i < tests.length; i++) { 165 dummyRequest.setHeader(OSSO_TIMEOUT_EXCEEDED_HEADER, tests[i]); 166 User result = decoder.fromRequest(dummyRequest); 167 if (expected[i]) { 168 assertNull(result); 169 } else { 170 assertNotNull(result); 171 } 172 } 173 } 174 } 175