Skip to content

Add Junit tests for AESEncryption.java #5597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@
* ciphers
* a5
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
* [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java)
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
* [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java)
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.thealgorithms.ciphers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.crypto.SecretKey;
import org.junit.jupiter.api.Test;

public class AESEncryptionTest {

@Test
public void testGetSecretEncryptionKey() throws Exception {
SecretKey key = AESEncryption.getSecretEncryptionKey();
assertNotNull(key, "Secret key should not be null");
assertEquals(128, key.getEncoded().length * 8, "Key size should be 128 bits");
}

@Test
public void testEncryptText() throws Exception {
String plainText = "Hello World";
SecretKey secKey = AESEncryption.getSecretEncryptionKey();
byte[] cipherText = AESEncryption.encryptText(plainText, secKey);

assertNotNull(cipherText, "Ciphertext should not be null");
assertTrue(cipherText.length > 0, "Ciphertext should not be empty");
}

@Test
public void testDecryptText() throws Exception {
String plainText = "Hello World";
SecretKey secKey = AESEncryption.getSecretEncryptionKey();
byte[] cipherText = AESEncryption.encryptText(plainText, secKey);

// Decrypt the ciphertext
String decryptedText = AESEncryption.decryptText(cipherText, secKey);

assertNotNull(decryptedText, "Decrypted text should not be null");
assertEquals(plainText, decryptedText, "Decrypted text should match the original plain text");
}

@Test
public void testEncryptDecrypt() throws Exception {
String plainText = "Hello AES!";
SecretKey secKey = AESEncryption.getSecretEncryptionKey();

// Encrypt the plaintext
byte[] cipherText = AESEncryption.encryptText(plainText, secKey);

// Decrypt the ciphertext
String decryptedText = AESEncryption.decryptText(cipherText, secKey);

assertEquals(plainText, decryptedText, "Decrypted text should match the original plain text");
}

@Test
public void testBytesToHex() {
byte[] bytes = new byte[] {0, 1, 15, 16, (byte) 255}; // Test with diverse byte values
String hex = AESEncryption.bytesToHex(bytes);
assertEquals("00010F10FF", hex, "Hex representation should match the expected value");
}
}