xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/util/TimestampTest.java (revision 2f7dccc7cd05ce1957006b093948d5359068ae4f)
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.util;
24 
25 import org.junit.jupiter.api.Test;
26 
27 import java.util.Date;
28 
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertThrows;
31 
32 /**
33  *
34  * @author ktulinge
35  */
36 public class TimestampTest {
37 
38     /**
39      * Test of valid timestamp cookies and their decoded values.
40      */
41     @Test
testDecodeTimestamp()42     public void testDecodeTimestamp() {
43         String[] tests = {
44             "123456",
45             "5761172f",
46             "abcdef09",
47             "58cfe588"
48         };
49 
50         long[] expected = {
51             1193046000L,
52             1465980719000L,
53             2882400009000L,
54             1490019720000L
55         };
56 
57         for (int i = 0; i < tests.length; i++) {
58             assertEquals(expected[i], Timestamp.decodeTimeCookie(tests[i]).getTime());
59         }
60     }
61 
62     /**
63      * Test of invalid timestamp cookies.
64      */
65     @Test
testInvalidDecodeTimestamp()66     public void testInvalidDecodeTimestamp() {
67         String[] tests = {
68             "sd45gfgf5sd4g5ffd54g",
69             "ě5 1g56ew1tč6516re5g1g65d1g65d",
70             "abcegkjkjsdlkjg",
71             ""
72         };
73 
74         for (String test : tests) {
75             assertThrows(Exception.class, () -> Timestamp.decodeTimeCookie(test).getTime(),
76                     "Decoding should throw an exception - invalid format");
77         }
78     }
79 
80     /**
81      * Test of encoded cookies.
82      */
83     @Test
testEncodeTimestamp()84     public void testEncodeTimestamp() {
85         Date[] tests = {
86             new Date(Long.parseLong("1193046000")),
87             new Date(Long.parseLong("1465980719000")),
88             new Date(Long.parseLong("2882400009000")),
89             new Date(1490019720000L), // 2017-03-20 14:22:00
90         };
91 
92         String[] expected = {
93             "123456",
94             "5761172f",
95             "abcdef09",
96             "58cfe588"
97         };
98 
99         for (int i = 0; i < tests.length; i++) {
100             assertEquals(expected[i], Timestamp.encodeTimeCookie(tests[i]));
101         }
102     }
103 }
104