Debian Codegraph @

contribs/jersey-oauth/oauth-server/src/test/java/com/sun/jersey/oauth/server/NonceManagerTest.java

raw
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12 * or packager/legal/LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package com.sun.jersey.oauth.server;
42
43 import org.junit.Test;
44 import static org.junit.Assert.*;
45
46 /**
47 * @author Thomas Meire
48 */
49 public class NonceManagerTest {
50
51 private String stamp() {
52 return stamp(0);
53 }
54
55 private String stamp(int offset) {
56 return Long.toString((System.currentTimeMillis() - offset) / 1000);
57 }
58
59 @Test
60 public void testExpiredNonce() {
61 NonceManager nonces = new NonceManager(1000, 50);
62
63 boolean accepted = nonces.verify("old-nonce-key", stamp(2000), "old-nonce");
64 assertFalse(accepted);
65
66 long size = nonces.size();
67 assertEquals(0, size);
68 }
69
70 @Test
71 public void testValidNonce() {
72 NonceManager nonces = new NonceManager(1000, 50);
73
74 boolean accepted = nonces.verify("nonce-key", stamp(), "nonce");
75 assertTrue(accepted);
76
77 long size = nonces.size();
78 assertEquals(1, size);
79 }
80
81 @Test
82 public void testDuplicateNonce() {
83 NonceManager nonces = new NonceManager(1000, 50);
84
85 String stamp = stamp();
86
87 boolean accepted;
88 accepted = nonces.verify("nonce-key", stamp, "nonce");
89 assertTrue(accepted);
90
91 accepted = nonces.verify("nonce-key", stamp, "nonce");
92 assertFalse(accepted);
93 }
94
95 @Test
96 public void testAutoGC() {
97 NonceManager nonces = new NonceManager(1000, 10);
98
99 // verify nine
100 for (int i = 0; i < 9; i++) {
101 assertTrue(nonces.verify("testing-" + i, stamp(), Integer.toString(i)));
102 }
103 assertEquals(9, nonces.size());
104
105 // invalid nonces don't trigger gc's
106 assertFalse(nonces.verify("testing-9", stamp(2000), "9"));
107 assertEquals(9, nonces.size());
108
109 try {
110 Thread.sleep(1000);
111 } catch (Exception e) {
112 fail("Can't guarantee we slept long enough...");
113 }
114 // 10th valid nonce triggers a gc on old tokens
115 assertTrue(nonces.verify("testing-10", stamp(), "10"));
116 assertEquals(1, nonces.size());
117 }
118
119 @Test
120 public void testManualGC() {
121 NonceManager nonces = new NonceManager(1000, 5000);
122
123 // insert 100 valid nonces
124 for (int i = 0; i < 100; i++) {
125 nonces.verify("testing-" + i, stamp(), Integer.toString(i));
126 }
127 assertEquals(100, nonces.size());
128
129 // make sure the gc doesn't clean valid nonces
130 nonces.gc(System.currentTimeMillis());
131 assertEquals(100, nonces.size());
132
133 // sleep a while to invalidate the nonces
134 try {
135 Thread.sleep(1100);
136 } catch (Exception e) {
137 fail("Can't guarantee we slept long enough...");
138 }
139
140 // gc should remove all the nonces
141 nonces.gc(System.currentTimeMillis());
142 assertEquals(0, nonces.size());
143 }
144 }