1+ package com .bencodez .simpleapi .tests ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
5+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6+
7+ import java .io .File ;
8+ import java .io .IOException ;
9+ import java .security .NoSuchAlgorithmException ;
10+
11+ import org .junit .jupiter .api .AfterEach ;
12+ import org .junit .jupiter .api .BeforeEach ;
13+ import org .junit .jupiter .api .Test ;
14+
15+ import com .bencodez .simpleapi .encryption .EncryptionHandler ;
16+
17+ public class EncryptionHandlerTest {
18+
19+ private File tempKeyFile ;
20+ private EncryptionHandler encryptionHandler ;
21+
22+ @ BeforeEach
23+ public void setUp () throws IOException {
24+ // Create a temporary file for key storage
25+ tempKeyFile = new File (System .getProperty ("java.io.tmpdir" ), "encryption_test_key" );
26+
27+ // Initialize EncryptionHandler
28+ encryptionHandler = new EncryptionHandler ("Test" , tempKeyFile );
29+ }
30+
31+ @ AfterEach
32+ public void tearDown () {
33+ // Clean up the temporary file
34+ if (tempKeyFile .exists ()) {
35+ tempKeyFile .delete ();
36+ }
37+ }
38+
39+ @ Test
40+ public void testEncryptionDecryption () {
41+ String originalMessage = "Test encryption and decryption" ;
42+
43+ String encryptedMessage = encryptionHandler .encrypt (originalMessage );
44+ assertNotNull (encryptedMessage , "Encrypted message should not be null" );
45+
46+ String decryptedMessage = encryptionHandler .decrypt (encryptedMessage );
47+ assertNotNull (decryptedMessage , "Decrypted message should not be null" );
48+
49+ assertEquals (originalMessage , decryptedMessage , "Decrypted message should be equal to the original" );
50+ }
51+
52+ @ Test
53+ public void testKeyFileCreationAndLoading () throws IOException , NoSuchAlgorithmException {
54+ // Ensure the key file is created
55+ assertTrue (tempKeyFile .exists (), "Key file should be created on initialization" );
56+
57+ // Create a new handler to check if it loads the existing key
58+ EncryptionHandler newHandler = new EncryptionHandler ("Test" , tempKeyFile );
59+
60+ String originalMessage = "Another encryption test" ;
61+ String encryptedMessage = encryptionHandler .encrypt (originalMessage );
62+ String decryptedMessage = newHandler .decrypt (encryptedMessage );
63+
64+ assertEquals (originalMessage , decryptedMessage , "Decrypted message with loaded key should match original" );
65+ }
66+
67+ @ Test
68+ public void testSaveAndLoadKey () throws IOException {
69+ // Save the existing key
70+ encryptionHandler .save (tempKeyFile );
71+
72+ // Load the key and ensure encryption still works
73+ EncryptionHandler newHandler = new EncryptionHandler ("Test" , tempKeyFile );
74+
75+ String originalMessage = "Persistence test" ;
76+ String encryptedMessage = encryptionHandler .encrypt (originalMessage );
77+ String decryptedMessage = newHandler .decrypt (encryptedMessage );
78+
79+ assertEquals (originalMessage , decryptedMessage ,
80+ "Decrypted message with saved and loaded key should match original" );
81+ }
82+ }
0 commit comments