Skip to content

Commit

Permalink
Add unit tests for EncryptionDecryptionUtil
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 committed Aug 22, 2023
1 parent 3ebff2b commit ee79b49
Showing 1 changed file with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit ee79b49

Please sign in to comment.