Tomasz Golabek | c8fcbbc | 2019-07-09 08:42:59 +0200 | [diff] [blame] | 1 | /*- |
| 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 Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 21 | import org.junit.Test; |
Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 22 | |
| 23 | import javax.servlet.http.Cookie; |
| 24 | |
| 25 | import java.io.IOException; |
Tomasz Golabek | e558a64 | 2019-08-21 10:40:45 +0200 | [diff] [blame] | 26 | import org.openecomp.sdc.securityutil.AuthenticationCookie; |
| 27 | import org.openecomp.sdc.securityutil.AuthenticationCookieUtils; |
| 28 | import org.openecomp.sdc.securityutil.CipherUtilException; |
| 29 | import org.openecomp.sdc.securityutil.ISessionValidationFilterConfiguration; |
| 30 | import org.openecomp.sdc.securityutil.filters.SampleFilter; |
Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 31 | |
| 32 | import static org.junit.Assert.*; |
| 33 | |
| 34 | public 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 Golabek | e558a64 | 2019-08-21 10:40:45 +0200 | [diff] [blame] | 43 | Cookie cookieWithOriginalTime = new Cookie(filterCfg.getCookieName(), AuthenticationCookieUtils |
| 44 | .getEncryptedCookie(authenticationCookieOriginal,filterCfg )); |
Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 45 | // 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 Golabek | c8fcbbc | 2019-07-09 08:42:59 +0200 | [diff] [blame] | 64 | } |