    1   /*
    2    * Copyright 2004-2009 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  package org.seasar.cubby.internal.util;
    17
    18  import static org.junit.Assert.assertEquals;
    19  import static org.junit.Assert.assertFalse;
    20  import static org.junit.Assert.assertNotNull;
    21  import static org.junit.Assert.assertTrue;
    22
    23  import java.util.HashSet;
    24  import java.util.Map;
    25  import java.util.Set;
    26
    27  import javax.servlet.http.HttpSession;
    28
    29  import org.junit.Test;
    30  import org.seasar.cubby.CubbyConstants;
    31  import org.seasar.cubby.internal.util.LruHashMap;
    32  import org.seasar.cubby.internal.util.TokenHelper;
    33
    34  import static org.easymock.EasyMock.*;
    35
    36  public class TokenHelperTest {
    37
    38  	@Test
    39  	public void generateGUID() throws Exception {
    40  		final int count = 50000;
    41  		Set<String> generatedGuids = new HashSet<String>();
    42  		for (int i = 0; i < count; i++) {
    43  			String guid = TokenHelper.generateGUID();
    44  			// assertEquals("GUIDは32文字[" + guid + "]", 32, guid.length());
    45  			assertNotNull(guid);
    46  			assertTrue(guid.length() > 0);
    47  			assertFalse("GUIDが重複した:" + guid, generatedGuids.contains(guid));
    48  			generatedGuids.add(guid);
    49  		}
    50  		assertEquals(count, generatedGuids.size());
    51  	}
    52
    53  	@Test
    54  	public void setToken() throws Exception {
    55  		String token = "tokenstring";
    56
    57  		HttpSession session = createMock(HttpSession.class);
    58  		Map<String, String> tokenMap = new LruHashMap<String, String>(32);
    59  		expect(session.getAttribute(CubbyConstants.ATTR_TOKEN)).andReturn(
    60  				tokenMap).anyTimes();
    61  		replay(session);
    62
    63  		TokenHelper.setToken(session, token);
    64
    65  		assertTrue("トークン文字列がトークン用のMapのキーとして追加", tokenMap
    66  				.containsKey("tokenstring"));
    67  	}
    68
    69  	@Test
    70  	public void validateToken() throws Exception {
    71  		String token = "tokenstring";
    72
    73  		HttpSession session = createMock(HttpSession.class);
    74  		Map<String, String> tokenMap = new LruHashMap<String, String>(32);
    75  		expect(session.getAttribute(CubbyConstants.ATTR_TOKEN)).andReturn(
    76  				tokenMap).anyTimes();
    77  		replay(session);
    78
    79  		TokenHelper.setToken(session, token);
    80
    81  		assertFalse("セッション中に存在しないトークン文字列", TokenHelper.validateToken(session,
    82  				"dummytoken"));
    83  		assertTrue("セッション中に存在するトークン文字列", TokenHelper.validateToken(session,
    84  				"tokenstring"));
    85  		assertFalse("1度validateTokenした後は指定したトークン文字列は除去されている", TokenHelper
    86  				.validateToken(session, "tokenstring"));
    87  	}
    88  }