2 * Copyright (c) 2016, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.aaa.encrypt.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Base64;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.config.aaa.authn.encrypt.service.config.rev160915.AaaEncryptServiceConfigBuilder;
20 * @author - Sharon Aicler (saichler@gmail.com)
23 public class AAAEncryptServiceImplTest {
24 private AAAEncryptionServiceImpl impl;
28 impl = new AAAEncryptionServiceImpl(new EncryptServiceConfigImpl(
29 OSGiEncryptionServiceConfigurator.generateConfig(new AaaEncryptServiceConfigBuilder()
30 .setCipherTransforms("AES/CBC/PKCS5Padding")
31 .setEncryptIterationCount(32768)
33 .setEncryptKeyLength(128)
34 .setEncryptMethod("PBKDF2WithHmacSHA1")
36 .setEncryptType("AES")
37 .setPasswordLength(12)
42 public void testShortString() {
43 String before = "shortone";
44 String encrypt = impl.encrypt(before);
45 assertNotEquals(before, encrypt);
46 String after = impl.decrypt(encrypt);
47 assertEquals(before, after);
51 public void testLongString() {
52 String before = "This is a very long string to encrypt for testing 1...2...3";
53 String encrypt = impl.encrypt(before);
54 assertNotEquals(before, encrypt);
55 String after = impl.decrypt(encrypt);
56 assertEquals(before, after);
60 public void testNetconfEncodedPasswordWithoutPadding() {
62 String password = "bmV0Y29uZgo=";
63 String unencrypted = impl.decrypt(password);
64 assertEquals(password, unencrypted);
68 public void testNetconfEncodedPasswordWithPadding() {
69 String password = "bmV0Y29uZgo=";
70 String unencrypted = impl.decrypt(password);
71 assertEquals(password, unencrypted);
75 public void testNetconfPasswordWithoutPadding() {
77 String password = "netconf";
78 String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
79 String unencrypted = impl.decrypt(encodedPassword);
80 assertEquals(encodedPassword, unencrypted);
84 public void testNetconfPasswordWithPadding() {
85 String password = "netconf";
86 String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
87 String unencrypted = impl.decrypt(encodedPassword);
88 assertEquals(encodedPassword, unencrypted);
92 public void testAdminEncodedPasswordWithoutPadding() {
94 String password = "YWRtaW4K";
95 String unencrypted = impl.decrypt(password);
96 assertEquals(password, unencrypted);
100 public void testAdminEncodedPasswordWithPadding() {
101 String password = "YWRtaW4K";
102 String unencrypted = impl.decrypt(password);
103 assertEquals(password, unencrypted);
107 public void testAdminPasswordWithoutPadding() {
109 String password = "admin";
110 String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
111 String unencrypted = impl.decrypt(encodedPassword);
112 assertEquals(encodedPassword, unencrypted);
116 public void testAdminPasswordWithPadding() {
117 String password = "admin";
118 String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
119 String unencrypted = impl.decrypt(encodedPassword);
120 assertEquals(encodedPassword, unencrypted);
123 private void changePadding() {
124 impl = new AAAEncryptionServiceImpl(new EncryptServiceConfigImpl(
125 OSGiEncryptionServiceConfigurator.generateConfig(new AaaEncryptServiceConfigBuilder()
126 .setCipherTransforms("AES/CBC/NoPadding")
127 .setEncryptIterationCount(32768)
129 .setEncryptKeyLength(128)
130 .setEncryptMethod("PBKDF2WithHmacSHA1")
132 .setEncryptType("AES")
133 .setPasswordLength(12)