SessionAdapterTest.java revision b334b83b7ac2d9a8c60d935cad0365506f13333b
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright 2013 ForgeRock, AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms of the Common Development and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Distribution License (the License). You may not use this file except in compliance with the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * specific language governing permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Software, include this CDDL Header Notice in each file and include
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header, with the fields enclosed by brackets [] replaced by your own identifying
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * information: "Portions copyright [year] [name of copyright owner]".
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.sm.ldap.adapters;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.dpro.session.service.InternalSession;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.CoreTokenConfig;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.TokenTestUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.api.TokenType;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.api.fields.SessionTokenField;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.api.tokens.Token;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.api.tokens.TokenIdFactory;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.exceptions.CoreTokenException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.utils.JSONSerialisation;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.utils.LDAPDataConversion;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ldap.utils.blob.TokenBlobUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.testng.annotations.BeforeMethod;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.testng.annotations.Test;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.UnsupportedEncodingException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Calendar;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport static org.mockito.BDDMockito.given;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport static org.mockito.BDDMockito.mock;
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshottimport static org.mockito.Matchers.*;
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshottimport static org.mockito.Mockito.times;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport static org.mockito.Mockito.verify;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport static org.testng.Assert.assertEquals;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport static org.testng.Assert.assertFalse;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport static org.testng.Assert.assertTrue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @author robert.wapshott@forgerock.com
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class SessionAdapterTest {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private SessionAdapter adapter;
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott private TokenIdFactory tokenIdFactory;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private CoreTokenConfig coreTokenConfig;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private JSONSerialisation jsonSerialisation;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private LDAPDataConversion ldapDataConversion;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private TokenBlobUtils blobUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster @BeforeMethod
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setup() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tokenIdFactory = mock(TokenIdFactory.class);
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott coreTokenConfig = mock(CoreTokenConfig.class);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster jsonSerialisation = mock(JSONSerialisation.class);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ldapDataConversion = mock(LDAPDataConversion.class);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster blobUtils = new TokenBlobUtils();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster adapter = new SessionAdapter(tokenIdFactory, coreTokenConfig, jsonSerialisation, ldapDataConversion, blobUtils);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster @Test
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void shouldSerialiseAndDeserialiseToken() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Given
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Sessions can only measure time to the closest second.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Calendar now = Calendar.getInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster now.set(Calendar.MILLISECOND, 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String userId = "ferret";
String sessionId = "badger";
byte[] mockByteData = {};
LDAPDataConversion dataConversion = new LDAPDataConversion();
InternalSession session = mock(InternalSession.class);
// Ensure Session ID is badger
given(tokenIdFactory.toSessionTokenId(any(InternalSession.class))).willReturn(sessionId);
// Ensure Session User is ferret
given(coreTokenConfig .getUserId(any(InternalSession.class))).willReturn(userId);
// Ensure expiry date is now based on the epoched time in seconds format.
given(session.getExpirationTime()).willReturn(dataConversion.toEpochedSeconds(now));
// Avoid serialisation when using mock InternalSessions
given(jsonSerialisation.deserialise(anyString(), eq(InternalSession.class))).willReturn(session);
given(jsonSerialisation.serialise(any())).willReturn(new String(mockByteData));
adapter = new SessionAdapter(
tokenIdFactory,
coreTokenConfig ,
jsonSerialisation,
dataConversion, blobUtils);
Token token = new Token(sessionId, TokenType.SESSION);
token.setUserId(userId);
token.setExpiryTimestamp(now);
token.setBlob(mockByteData);
// When
Token result = adapter.toToken(adapter.fromToken(token));
// Then
TokenTestUtils.compareTokens(result, token);
}
@Test
public void shouldRestoreLatestAccessTimeFromAttribute() {
// Given
String latestAccessTime = "12345";
Token token = new Token("badger", TokenType.SESSION);
token.setAttribute(SessionTokenField.LATEST_ACCESS_TIME.getField(), latestAccessTime);
// blob contents are missing the latestAccessTime value
token.setBlob("{\"clientDomain\":null,\"creationTime\":1376307674,\"isISStored\":true,\"maxCachingTime\":3}".getBytes());
// need a real JSONSerialisation for this test
JSONSerialisation serialisation = new JSONSerialisation(mock(Debug.class));
adapter = new SessionAdapter(tokenIdFactory, coreTokenConfig, serialisation, ldapDataConversion, blobUtils);
// When
InternalSession session = adapter.fromToken(token);
// Then
// if latestAccessTime was zero, this would fail
long epochedSeconds = System.currentTimeMillis() / 1000;
long idleTime = session.getIdleTime();
assertTrue(idleTime < epochedSeconds);
}
@Test
public void shouldAssignAttributeFromSessionLatestAccessTime() {
// Given
String latestAccessTime = "12345";
// start by making an IntenalSession that has latestAccessTime set.
JSONSerialisation serialisation = new JSONSerialisation(mock(Debug.class));
String serialisedSession = "{\"latestAccessTime\":" + latestAccessTime + "}";
InternalSession session = serialisation.deserialise(serialisedSession, InternalSession.class);
// some additional required mocking
given(tokenIdFactory.toSessionTokenId(eq(session))).willReturn("badger");
given(jsonSerialisation.serialise(any())).willReturn(serialisedSession);
// When
Token token = adapter.toToken(session);
// Then
String value = token.getValue(SessionTokenField.LATEST_ACCESS_TIME.getField());
assertEquals(value, latestAccessTime);
}
@Test
public void shouldFilterLatestAccessTime() throws CoreTokenException {
// Given
Token token = new Token("badger", TokenType.SESSION);
String someJSONLikeText = "{\"clientDomain\":null,\"creationTime\":1376307674,\"isISStored\":true,\"latestAccessTime\":1376308558,\"maxCachingTime\":3}";
token.setBlob(someJSONLikeText.getBytes());
TokenBlobUtils utils = new TokenBlobUtils();
// When
adapter.filterLatestAccessTime(token);
// Then
String contents = utils.getBlobAsString(token);
assertFalse(contents.contains(SessionTokenField.LATEST_ACCESS_TIME.getInternalSessionFieldName()));
}
@Test
public void shouldHandleMissingCommaInBlob() {
// Given
String latestAccessTime = "1376308558";
Token token = new Token("badger", TokenType.SESSION);
String someJSONLikeText = "{\"latestAccessTime\":" + latestAccessTime + "}";
token.setBlob(someJSONLikeText.getBytes());
// When
String result = adapter.filterLatestAccessTime(token);
// Then
assertEquals(result, latestAccessTime);
}
@Test
public void shouldDoNothingIfLatestAccessTimeNotFound() throws UnsupportedEncodingException {
// Given
Token mockToken = mock(Token.class);
given(mockToken.getBlob()).willReturn("badger".getBytes(TokenBlobUtils.ENCODING));
// When
adapter.filterLatestAccessTime(mockToken);
// Then
verify(mockToken, times(0)).setBlob(any(byte[].class));
}
@Test
public void shouldLocateValidFieldInJSON() {
String json = "{\"clientDomain\":null,\"creationTime\":1376307674,\"isISStored\":true,\"latestAccessTime\":1376308558,\"maxCachingTime\":3}";
assertEquals(1, adapter.findIndexOfValidField(json));
}
@Test
public void shouldIndicateNoValidFieldsInJSON() {
assertEquals(-1, adapter.findIndexOfValidField(""));
}
}