1+ package com .thealgorithms .ciphers ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .Arrays ;
6+
7+ import static org .junit .jupiter .api .Assertions .*;
8+
9+ class OneTimePadCipherTest {
10+ @ Test
11+ void testGenerateKeyLength () {
12+ int length = 16 ;
13+ byte [] key = OneTimePadCipher .generateKey (length );
14+ assertNotNull (key , "Key should not be null" );
15+ assertEquals (length , key .length , "Key length should match requested length" );
16+ }
17+
18+ @ Test
19+ void testEncryptDecrypt () {
20+ String plaintext = "Hello, OneTimePad!" ;
21+ byte [] input = plaintext .getBytes ();
22+
23+ byte [] key = OneTimePadCipher .generateKey (input .length );
24+
25+ byte [] ciphertext = OneTimePadCipher .applyOTP (input , key );
26+ assertNotNull (ciphertext );
27+ assertNotEquals (plaintext , new String (ciphertext ), "Ciphertext should differ from plaintext" );
28+
29+ byte [] decrypted = OneTimePadCipher .applyOTP (ciphertext , key );
30+ assertEquals (plaintext , new String (decrypted ), "Decrypted text should match original plaintext" );
31+ }
32+
33+ @ Test
34+ void testInvalidKeyLength () {
35+ byte [] input = "Hello, OneTimePad!" .getBytes ();
36+ byte [] wrongKey = OneTimePadCipher .generateKey (input .length + 1 );
37+
38+ assertThrows (IllegalArgumentException .class , () -> {
39+ OneTimePadCipher .applyOTP (input , wrongKey );
40+ });
41+ }
42+
43+ @ Test
44+ void testEmptyInputAndKey () {
45+ byte [] input = new byte [0 ];
46+ byte [] key = new byte [0 ];
47+ byte [] result = OneTimePadCipher .applyOTP (input , key );
48+ assertEquals (0 , result .length , "Empty input should return empty output" );
49+ }
50+
51+ @ Test
52+ void testDeterministicXorProperty () {
53+ byte [] input = "XORTest" .getBytes ();
54+ byte [] key = OneTimePadCipher .generateKey (input .length );
55+
56+ byte [] once = OneTimePadCipher .applyOTP (input , key );
57+ byte [] twice = OneTimePadCipher .applyOTP (once , key );
58+
59+ assertArrayEquals (input , twice , "Applying OTP twice with same key should return original input" );
60+ }
61+
62+ @ Test
63+ void testRandomKeyUniqueness () {
64+ byte [] key1 = OneTimePadCipher .generateKey (32 );
65+ byte [] key2 = OneTimePadCipher .generateKey (32 );
66+
67+ assertFalse (Arrays .equals (key1 , key2 ), "Two generated keys should not be identical" );
68+ }
69+
70+ @ Test
71+ void testUnicodeCharacters () {
72+ String plaintext = "Hello, OneTimePad!" ;
73+ byte [] input = plaintext .getBytes ();
74+
75+ byte [] key = OneTimePadCipher .generateKey (input .length );
76+ byte [] ciphertext = OneTimePadCipher .applyOTP (input , key );
77+ byte [] decrypted = OneTimePadCipher .applyOTP (ciphertext , key );
78+
79+ assertEquals (plaintext , new String (decrypted ), "Decrypted Unicode text should match original" );
80+ }
81+
82+ @ Test
83+ void testNullInputs () {
84+ byte [] validKey = OneTimePadCipher .generateKey (4 );
85+ byte [] validInput = "Test" .getBytes ();
86+
87+ assertThrows (NullPointerException .class , () -> OneTimePadCipher .applyOTP (null , validKey ));
88+ assertThrows (NullPointerException .class , () -> OneTimePadCipher .applyOTP (validInput , null ));
89+ }
90+
91+ @ Test
92+ void testNegativeKeyLength () {
93+ assertThrows (NegativeArraySizeException .class , () -> OneTimePadCipher .generateKey (-5 ));
94+ }
95+ }
0 commit comments