From ee79b49334d176e8f039cb8c64e77853b5028393 Mon Sep 17 00:00:00 2001 From: Ryan Liang Date: Thu, 17 Aug 2023 11:55:11 -0700 Subject: [PATCH] Add unit tests for EncryptionDecryptionUtil Signed-off-by: Ryan Liang --- .../jwt/EncryptionDecryptionUtilTest.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilTest.java diff --git a/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilTest.java b/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilTest.java new file mode 100644 index 0000000000..165116f757 --- /dev/null +++ b/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilTest.java @@ -0,0 +1,86 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.security.authtoken.jwt; + +import org.junit.Assert; +import org.junit.Test; +import java.util.Base64; + +public class EncryptionDecryptionUtilTest { + + @Test + public void testEncryptDecrypt() { + String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes()); + String data = "Hello, OpenSearch!"; + + String encryptedString = EncryptionDecryptionUtil.encrypt(secret, data); + String decryptedString = EncryptionDecryptionUtil.decrypt(secret, encryptedString); + + Assert.assertEquals(data, decryptedString); + } + + @Test + public void testDecryptingWithWrongKey() { + String secret1 = Base64.getEncoder().encodeToString("correctKey12345".getBytes()); + String secret2 = Base64.getEncoder().encodeToString("wrongKey1234567".getBytes()); + String data = "Hello, OpenSearch!"; + + String encryptedString = EncryptionDecryptionUtil.encrypt(secret1, data); + + try { + EncryptionDecryptionUtil.decrypt(secret2, encryptedString); + Assert.fail("Should have thrown an exception when decrypting with a wrong key"); + } catch (RuntimeException e) { + // Expected exception + } + } + + @Test + public void testDecryptingCorruptedData() { + String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes()); + String corruptedEncryptedString = "corruptedData"; + + try { + EncryptionDecryptionUtil.decrypt(secret, corruptedEncryptedString); + Assert.fail("Should have thrown an exception when trying to decrypt corrupted data"); + } catch (RuntimeException e) { + // Expected exception + } + } + + @Test + public void testEncryptDecryptEmptyString() { + String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes()); + String data = ""; + + String encryptedString = EncryptionDecryptionUtil.encrypt(secret, data); + String decryptedString = EncryptionDecryptionUtil.decrypt(secret, encryptedString); + + Assert.assertEquals(data, decryptedString); + } + + @Test(expected = NullPointerException.class) + public void testEncryptDecryptNullValue() { + String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes()); + String data = null; + + EncryptionDecryptionUtil.encrypt(secret, data); + } + + @Test(expected = NullPointerException.class) + public void testDecryptNullValue() { + String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes()); + String data = null; + + EncryptionDecryptionUtil.decrypt(secret, data); + } +}