forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the comment of encryption and decryption util and add the tests
Signed-off-by: Ryan Liang <[email protected]>
- Loading branch information
Showing
2 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |