Create Unit Test for EncryptService's Failed Decryption 34/109834/3
authorYaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
Thu, 18 Jan 2024 12:47:13 +0000 (14:47 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 22 Jan 2024 15:40:07 +0000 (15:40 +0000)
Added a unit test demonstrating that the decrypt method returns
the same password due to a failed decryption process,
resulting in an IllegalBlockSizeException.

JIRA: NETCONF-1216
Change-Id: I658a03d6dc81844c5e7f419c17dc13ca0602c85b
Signed-off-by: Yaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
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())));
+    }
 }