blob: 402803479f291c5cb5412ef9d49f5b98bf0e6973 [file] [log] [blame]
Tomasz Golabekc8fcbbc2019-07-09 08:42:59 +02001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
Yuli Shlosberga4eeb112019-01-07 16:23:36 +020021import org.junit.Test;
Yuli Shlosberga4eeb112019-01-07 16:23:36 +020022
23import javax.servlet.http.Cookie;
24
25import java.io.IOException;
Tomasz Golabeke558a642019-08-21 10:40:45 +020026import org.openecomp.sdc.securityutil.AuthenticationCookie;
27import org.openecomp.sdc.securityutil.AuthenticationCookieUtils;
28import org.openecomp.sdc.securityutil.CipherUtilException;
29import org.openecomp.sdc.securityutil.ISessionValidationFilterConfiguration;
30import org.openecomp.sdc.securityutil.filters.SampleFilter;
Yuli Shlosberga4eeb112019-01-07 16:23:36 +020031
32import static org.junit.Assert.*;
33
34public class AuthenticationCookieUtilsTest {
35
36 private SampleFilter sessionValidationFilter = new SampleFilter();
37 private ISessionValidationFilterConfiguration filterCfg = sessionValidationFilter.getFilterConfiguration();
38
39 @Test
40 public void vaildateThatCookieCurrentSessionTimeIncreased() throws IOException, CipherUtilException {
41 // original cookie, pojo and servlet cookie
42 AuthenticationCookie authenticationCookieOriginal = new AuthenticationCookie("kuku");
Tomasz Golabeke558a642019-08-21 10:40:45 +020043 Cookie cookieWithOriginalTime = new Cookie(filterCfg.getCookieName(), AuthenticationCookieUtils
44 .getEncryptedCookie(authenticationCookieOriginal,filterCfg ));
Yuli Shlosberga4eeb112019-01-07 16:23:36 +020045 // cookie with increased time, pojo and servlet cookie
46 Cookie cookieWithIncreasedTime = AuthenticationCookieUtils.updateSessionTime(cookieWithOriginalTime, filterCfg);
47 AuthenticationCookie authenticationCookieIncreasedTime = AuthenticationCookieUtils.getAuthenticationCookie(cookieWithIncreasedTime, filterCfg);
48 // validation
49 long currentSessionTimeOriginal = authenticationCookieOriginal.getCurrentSessionTime();
50 long currentSessionTimeIncreased = authenticationCookieIncreasedTime.getCurrentSessionTime();
51 assertTrue(currentSessionTimeOriginal < currentSessionTimeIncreased);
52 }
53
54 @Test
55 public void validateSerializationEncriptionDeserializationDecryption() throws IOException, CipherUtilException {
56 // original cookie, pojo and servlet cookie
57 AuthenticationCookie authenticationCookieOriginal = new AuthenticationCookie("kuku");
58 Cookie cookieWithOriginalTime = new Cookie(filterCfg.getCookieName(), AuthenticationCookieUtils.getEncryptedCookie(authenticationCookieOriginal,filterCfg ));
59 // cookie with increased time, pojo and servlet cookie
60 AuthenticationCookie decriptedAndDeserializedAuthenticationCookie = AuthenticationCookieUtils.getAuthenticationCookie(cookieWithOriginalTime,filterCfg);
61 assertTrue(authenticationCookieOriginal.equals(decriptedAndDeserializedAuthenticationCookie));
62 }
63
Tomasz Golabekc8fcbbc2019-07-09 08:42:59 +020064}