1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.internal.util;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.servlet.http.HttpSession;
29  
30  import org.junit.Test;
31  import org.seasar.cubby.CubbyConstants;
32  import org.seasar.cubby.internal.util.LruHashMap;
33  import org.seasar.cubby.internal.util.TokenHelper;
34  
35  import static org.easymock.EasyMock.*;
36  
37  public class TokenHelperTest {
38  
39  	@Test
40  	public void generateGUID() throws Exception {
41  		final int count = 50000;
42  		Set<String> generatedGuids = new HashSet<String>();
43  		for (int i = 0; i < count; i++) {
44  			String guid = TokenHelper.generateGUID();
45  			// assertEquals("GUIDは32文字[" + guid + "]", 32, guid.length());
46  			assertNotNull(guid);
47  			assertTrue(guid.length() > 0);
48  			assertFalse("GUIDが重複した:" + guid, generatedGuids.contains(guid));
49  			generatedGuids.add(guid);
50  		}
51  		assertEquals(count, generatedGuids.size());
52  	}
53  
54  	@Test
55  	public void setToken() throws Exception {
56  		String token = "tokenstring";
57  
58  		HttpSession session = createMock(HttpSession.class);
59  		Map<String, String> tokenMap = new LruHashMap<String, String>(32);
60  		expect(session.getAttribute(CubbyConstants.ATTR_TOKEN)).andReturn(
61  				tokenMap).anyTimes();
62  		replay(session);
63  
64  		TokenHelper.setToken(session, token);
65  
66  		assertTrue("トークン文字列がトークン用のMapのキーとして追加", tokenMap
67  				.containsKey("tokenstring"));
68  	}
69  
70  	@Test
71  	public void validateToken() throws Exception {
72  		String token = "tokenstring";
73  
74  		HttpSession session = createMock(HttpSession.class);
75  		Map<String, String> tokenMap = new LruHashMap<String, String>(32);
76  		expect(session.getAttribute(CubbyConstants.ATTR_TOKEN)).andReturn(
77  				tokenMap).anyTimes();
78  		replay(session);
79  
80  		TokenHelper.setToken(session, token);
81  
82  		assertFalse("セッション中に存在しないトークン文字列", TokenHelper.validateToken(session,
83  				"dummytoken"));
84  		assertTrue("セッション中に存在するトークン文字列", TokenHelper.validateToken(session,
85  				"tokenstring"));
86  		assertFalse("1度validateTokenした後は指定したトークン文字列は除去されている", TokenHelper
87  				.validateToken(session, "tokenstring"));
88  	}
89  }