Create Unit Test for EncryptService's Failed Decryption
[aaa.git] / aaa-encrypt-service / impl / src / test / java / org / opendaylight / aaa / encrypt / impl / AAAEncryptServiceImplTest.java
index 32ba3eba89d1c64983b0512480cf0037fb1ce2c7..5eac2f18eb5dbf92e1ada5c1cee003778fb9e765 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.aaa.encrypt.impl;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.yang.gen.v1.config.aaa.authn.encrypt.service.config.rev160915.AaaEncryptServiceConfigBuilder;
@@ -53,4 +55,82 @@ public class AAAEncryptServiceImplTest {
         String after = impl.decrypt(encrypt);
         assertEquals(before, after);
     }
+
+    @Test
+    public void testNetconfEncodedPasswordWithoutPadding() {
+        changePadding();
+        String password = "bmV0Y29uZgo=";
+        String unencrypted = impl.decrypt(password);
+        assertEquals(password, unencrypted);
+    }
+
+    @Test
+    public void testNetconfEncodedPasswordWithPadding() {
+        String password = "bmV0Y29uZgo=";
+        String unencrypted = impl.decrypt(password);
+        assertEquals(password, unencrypted);
+    }
+
+    @Test
+    public void testNetconfPasswordWithoutPadding() {
+        changePadding();
+        String password = "netconf";
+        String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
+        String unencrypted = impl.decrypt(encodedPassword);
+        assertEquals(encodedPassword, unencrypted);
+    }
+
+    @Test
+    public void testNetconfPasswordWithPadding() {
+        String password = "netconf";
+        String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
+        String unencrypted = impl.decrypt(encodedPassword);
+        assertEquals(encodedPassword, unencrypted);
+    }
+
+    @Test
+    public void testAdminEncodedPasswordWithoutPadding() {
+        changePadding();
+        String password = "YWRtaW4K";
+        String unencrypted = impl.decrypt(password);
+        assertEquals(password, unencrypted);
+    }
+
+    @Test
+    public void testAdminEncodedPasswordWithPadding() {
+        String password = "YWRtaW4K";
+        String unencrypted = impl.decrypt(password);
+        assertEquals(password, unencrypted);
+    }
+
+    @Test
+    public void testAdminPasswordWithoutPadding() {
+        changePadding();
+        String password = "admin";
+        String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
+        String unencrypted = impl.decrypt(encodedPassword);
+        assertEquals(encodedPassword, unencrypted);
+    }
+
+    @Test
+    public void testAdminPasswordWithPadding() {
+        String password = "admin";
+        String encodedPassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
+        String unencrypted = impl.decrypt(encodedPassword);
+        assertEquals(encodedPassword, unencrypted);
+    }
+
+    private void changePadding() {
+        impl = new AAAEncryptionServiceImpl(new EncryptServiceConfigImpl(
+            OSGiEncryptionServiceConfigurator.generateConfig(new AaaEncryptServiceConfigBuilder()
+                .setCipherTransforms("AES/CBC/NoPadding")
+                .setEncryptIterationCount(32768)
+                .setEncryptKey("")
+                .setEncryptKeyLength(128)
+                .setEncryptMethod("PBKDF2WithHmacSHA1")
+                .setEncryptSalt("")
+                .setEncryptType("AES")
+                .setPasswordLength(12)
+                .build())));
+    }
 }