From 79e84a73cfa7398fa892f0dee76dea41d7b08db5 Mon Sep 17 00:00:00 2001
From: Ryan Liang <jiallian@amazon.com>
Date: Fri, 29 Sep 2023 14:41:08 -0700
Subject: [PATCH] Remove the comment of encryption and decryption util and add
 the tests

Signed-off-by: Ryan Liang <jiallian@amazon.com>
---
 .../jwt/EncryptionDecryptionUtil.java         |  3 -
 .../jwt/EncryptionDecryptionUtilsTest.java    | 88 +++++++++++++++++++
 2 files changed, 88 insertions(+), 3 deletions(-)
 create mode 100644 src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java

diff --git a/src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java b/src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java
index 77696fd743..2e11fed64a 100644
--- a/src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java
+++ b/src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java
@@ -69,7 +69,4 @@ private CipherMode(final int opmode) {
             this.opmode = opmode;
         }
     }
-
-    // TODO: RYAN PUT YOUR LOGIC HERE TO CONNECT WITH
-    // https://raw.githubusercontent.com/cwperks/security/bf22d4a4e7e716f818e4ce48767590a9f0e53f38/src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java
 }
diff --git a/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java b/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java
new file mode 100644
index 0000000000..4890f380f9
--- /dev/null
+++ b/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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 EncryptionDecryptionUtilsTest {
+
+    @Test
+    public void testEncryptDecrypt() {
+        String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes());
+        String data = "Hello, OpenSearch!";
+
+        EncryptionDecryptionUtil util = new EncryptionDecryptionUtil(secret);
+
+        String encryptedString = util.encrypt(data);
+        String decryptedString = util.decrypt(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!";
+
+        EncryptionDecryptionUtil util1 = new EncryptionDecryptionUtil(secret1);
+        String encryptedString = util1.encrypt(data);
+
+        EncryptionDecryptionUtil util2 = new EncryptionDecryptionUtil(secret2);
+        RuntimeException ex = Assert.assertThrows(RuntimeException.class, () -> util2.decrypt(encryptedString));
+
+        Assert.assertEquals("Error processing data with cipher", ex.getMessage());
+    }
+
+    @Test
+    public void testDecryptingCorruptedData() {
+        String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes());
+        String corruptedEncryptedString = "corruptedData";
+
+        EncryptionDecryptionUtil util = new EncryptionDecryptionUtil(secret);
+        RuntimeException ex = Assert.assertThrows(RuntimeException.class, () -> util.decrypt(corruptedEncryptedString));
+
+        Assert.assertEquals("Last unit does not have enough valid bits", ex.getMessage());
+    }
+
+    @Test
+    public void testEncryptDecryptEmptyString() {
+        String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes());
+        String data = "";
+
+        EncryptionDecryptionUtil util = new EncryptionDecryptionUtil(secret);
+        String encryptedString = util.encrypt(data);
+        String decryptedString = util.decrypt(encryptedString);
+
+        Assert.assertEquals(data, decryptedString);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testEncryptNullValue() {
+        String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes());
+        String data = null;
+
+        EncryptionDecryptionUtil util = new EncryptionDecryptionUtil(secret);
+        util.encrypt(data);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testDecryptNullValue() {
+        String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes());
+        String data = null;
+
+        EncryptionDecryptionUtil util = new EncryptionDecryptionUtil(secret);
+        util.decrypt(data);
+    }
+}