From d61a75e3d8dba363737c06575f31cb4c905a6312 Mon Sep 17 00:00:00 2001 From: Yaroslav Lastivka Date: Thu, 18 Jan 2024 14:47:13 +0200 Subject: [PATCH] Create Unit Test for EncryptService's Failed Decryption 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 --- .../impl/AAAEncryptServiceImplTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/aaa-encrypt-service/impl/src/test/java/org/opendaylight/aaa/encrypt/impl/AAAEncryptServiceImplTest.java b/aaa-encrypt-service/impl/src/test/java/org/opendaylight/aaa/encrypt/impl/AAAEncryptServiceImplTest.java index 32ba3eba8..5eac2f18e 100644 --- a/aaa-encrypt-service/impl/src/test/java/org/opendaylight/aaa/encrypt/impl/AAAEncryptServiceImplTest.java +++ b/aaa-encrypt-service/impl/src/test/java/org/opendaylight/aaa/encrypt/impl/AAAEncryptServiceImplTest.java @@ -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()))); + } } -- 2.36.6