5eac2f18eb5dbf92e1ada5c1cee003778fb9e765
[aaa.git] / aaa-encrypt-service / impl / src / test / java / org / opendaylight / aaa / encrypt / impl / AAAEncryptServiceImplTest.java
1 /*
2  * Copyright (c) 2016, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.aaa.encrypt.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12
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;
18
19 /*
20  *  @author - Sharon Aicler (saichler@gmail.com)
21  */
22 @Deprecated
23 public class AAAEncryptServiceImplTest {
24     private AAAEncryptionServiceImpl impl;
25
26     @Before
27     public void setup() {
28         impl = new AAAEncryptionServiceImpl(new EncryptServiceConfigImpl(
29             OSGiEncryptionServiceConfigurator.generateConfig(new AaaEncryptServiceConfigBuilder()
30                 .setCipherTransforms("AES/CBC/PKCS5Padding")
31                 .setEncryptIterationCount(32768)
32                 .setEncryptKey("")
33                 .setEncryptKeyLength(128)
34                 .setEncryptMethod("PBKDF2WithHmacSHA1")
35                 .setEncryptSalt("")
36                 .setEncryptType("AES")
37                 .setPasswordLength(12)
38                 .build())));
39     }
40
41     @Test
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);
48     }
49
50     @Test
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);
57     }
58
59     @Test
60     public void testNetconfEncodedPasswordWithoutPadding() {
61         changePadding();
62         String password = "bmV0Y29uZgo=";
63         String unencrypted = impl.decrypt(password);
64         assertEquals(password, unencrypted);
65     }
66
67     @Test
68     public void testNetconfEncodedPasswordWithPadding() {
69         String password = "bmV0Y29uZgo=";
70         String unencrypted = impl.decrypt(password);
71         assertEquals(password, unencrypted);
72     }
73
74     @Test
75     public void testNetconfPasswordWithoutPadding() {
76         changePadding();
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);
81     }
82
83     @Test
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);
89     }
90
91     @Test
92     public void testAdminEncodedPasswordWithoutPadding() {
93         changePadding();
94         String password = "YWRtaW4K";
95         String unencrypted = impl.decrypt(password);
96         assertEquals(password, unencrypted);
97     }
98
99     @Test
100     public void testAdminEncodedPasswordWithPadding() {
101         String password = "YWRtaW4K";
102         String unencrypted = impl.decrypt(password);
103         assertEquals(password, unencrypted);
104     }
105
106     @Test
107     public void testAdminPasswordWithoutPadding() {
108         changePadding();
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);
113     }
114
115     @Test
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);
121     }
122
123     private void changePadding() {
124         impl = new AAAEncryptionServiceImpl(new EncryptServiceConfigImpl(
125             OSGiEncryptionServiceConfigurator.generateConfig(new AaaEncryptServiceConfigBuilder()
126                 .setCipherTransforms("AES/CBC/NoPadding")
127                 .setEncryptIterationCount(32768)
128                 .setEncryptKey("")
129                 .setEncryptKeyLength(128)
130                 .setEncryptMethod("PBKDF2WithHmacSHA1")
131                 .setEncryptSalt("")
132                 .setEncryptType("AES")
133                 .setPasswordLength(12)
134                 .build())));
135     }
136 }