1+ package com .iterable .iterableapi ;
2+
3+ import android .content .SharedPreferences ;
4+
5+ import org .junit .Before ;
6+ import org .junit .Test ;
7+ import org .mockito .Mock ;
8+ import org .mockito .MockitoAnnotations ;
9+
10+ import static org .junit .Assert .assertEquals ;
11+ import static org .junit .Assert .assertNull ;
12+ import static org .junit .Assert .assertNotNull ;
13+ import static org .junit .Assert .assertNotEquals ;
14+ import static org .junit .Assert .assertTrue ;
15+ import static org .junit .Assert .fail ;
16+
17+ import java .security .KeyStore ;
18+
19+ public class IterableDataEncryptorTest extends BaseTest {
20+
21+ private IterableDataEncryptor encryptor ;
22+
23+ @ Mock
24+ private SharedPreferences sharedPreferences ;
25+
26+ @ Before
27+ public void setUp () {
28+ MockitoAnnotations .initMocks (this );
29+ encryptor = new IterableDataEncryptor ();
30+ }
31+
32+ @ Test
33+ public void testConstructor () {
34+ // Simply creating a new instance should not throw any exceptions
35+ IterableDataEncryptor encryptor = new IterableDataEncryptor ();
36+ assertNotNull ("Encryptor should be created successfully" , encryptor );
37+ }
38+
39+ @ Test
40+ public void testEncryptDecryptSuccess () {
41+ String originalText = "test data to encrypt" ;
42+ String encrypted = encryptor .encrypt (originalText );
43+ String decrypted = encryptor .decrypt (encrypted );
44+
45+ assertNotNull ("Encrypted text should not be null" , encrypted );
46+ assertNotEquals ("Encrypted text should not match original" , originalText , encrypted );
47+ assertEquals ("Decrypted text should match original" , originalText , decrypted );
48+ }
49+
50+ @ Test
51+ public void testEncryptNullInput () {
52+ String encrypted = encryptor .encrypt (null );
53+ assertNull ("Encrypting null should return null" , encrypted );
54+ }
55+
56+ @ Test
57+ public void testDecryptNullInput () {
58+ String decrypted = encryptor .decrypt (null );
59+ assertNull ("Decrypting null should return null" , decrypted );
60+ }
61+
62+ @ Test
63+ public void testEncryptEmptyString () {
64+ String encrypted = encryptor .encrypt ("" );
65+ String decrypted = encryptor .decrypt (encrypted );
66+
67+ assertNotNull ("Encrypted text should not be null" , encrypted );
68+ assertEquals ("Decrypted text should be empty string" , "" , decrypted );
69+ }
70+
71+ @ Test
72+ public void testEncryptLongString () {
73+ StringBuilder longString = new StringBuilder ();
74+ for (int i = 0 ; i < 1000 ; i ++) {
75+ longString .append ("test" );
76+ }
77+ String originalText = longString .toString ();
78+
79+ String encrypted = encryptor .encrypt (originalText );
80+ String decrypted = encryptor .decrypt (encrypted );
81+
82+ assertNotNull ("Encrypted text should not be null" , encrypted );
83+ assertEquals ("Decrypted long text should match original" , originalText , decrypted );
84+ }
85+
86+ @ Test
87+ public void testMultipleEncryptions () {
88+ String text1 = "first text" ;
89+ String text2 = "second text" ;
90+
91+ String encrypted1 = encryptor .encrypt (text1 );
92+ String encrypted2 = encryptor .encrypt (text2 );
93+
94+ assertNotEquals ("Different texts should have different encryptions" , encrypted1 , encrypted2 );
95+ assertEquals ("First text should decrypt correctly" , text1 , encryptor .decrypt (encrypted1 ));
96+ assertEquals ("Second text should decrypt correctly" , text2 , encryptor .decrypt (encrypted2 ));
97+ }
98+
99+ @ Test (expected = IterableDataEncryptor .DecryptionException .class )
100+ public void testDecryptInvalidData () {
101+ encryptor .decrypt ("invalid encrypted data" );
102+ }
103+
104+ @ Test
105+ public void testClearKeyAndData () {
106+ String originalText = "test data" ;
107+ String encrypted = encryptor .encrypt (originalText );
108+
109+ // Clear the key
110+ encryptor .clearKeyAndData (sharedPreferences );
111+
112+ // Try to decrypt the data encrypted with the old key
113+ try {
114+ encryptor .decrypt (encrypted );
115+ fail ("Should not be able to decrypt data with cleared key" );
116+ } catch (Exception e ) {
117+ // Expected behavior - old encrypted data should not be decryptable
118+ assertNotNull (e );
119+ }
120+
121+ // Verify new encryption/decryption works after clearing
122+ String newEncrypted = encryptor .encrypt (originalText );
123+ String newDecrypted = encryptor .decrypt (newEncrypted );
124+ assertEquals ("New encryption/decryption should work after clearing" , originalText , newDecrypted );
125+ }
126+
127+ @ Test
128+ public void testKeyGeneration () throws Exception {
129+ // Create new encryptor which should trigger key generation
130+ IterableDataEncryptor encryptor = new IterableDataEncryptor ();
131+
132+ // Get the keystore from the encryptor (we'll need to add a method to expose this)
133+ KeyStore keyStore = encryptor .getKeyStore ();
134+
135+ // Verify the key exists in keystore
136+ assertTrue ("Key should exist in keystore" , keyStore .containsAlias ("iterable_encryption_key" ));
137+
138+ // Rest of the test remains the same
139+ String testData = "test data" ;
140+ String encrypted = encryptor .encrypt (testData );
141+ String decrypted = encryptor .decrypt (encrypted );
142+ assertEquals ("Data should be correctly encrypted and decrypted" , testData , decrypted );
143+ }
144+
145+ @ Test
146+ public void testKeyRegeneration () throws Exception {
147+ // Create first encryptor and encrypt data
148+ IterableDataEncryptor encryptor1 = new IterableDataEncryptor ();
149+ KeyStore keyStore = encryptor1 .getKeyStore ();
150+
151+ String testData = "test data" ;
152+ String encrypted1 = encryptor1 .encrypt (testData );
153+
154+ // Delete the key
155+ encryptor1 .clearKeyAndData (sharedPreferences );
156+
157+ // Create second encryptor which should generate a new key
158+ IterableDataEncryptor encryptor2 = new IterableDataEncryptor ();
159+
160+ // Rest of the test remains the same
161+ assertTrue ("Key should be regenerated" , keyStore .containsAlias ("iterable_encryption_key" ));
162+
163+ // Verify old encrypted data can't be decrypted with new key
164+ try {
165+ encryptor2 .decrypt (encrypted1 );
166+ fail ("Should not be able to decrypt data encrypted with old key" );
167+ } catch (Exception e ) {
168+ // Expected
169+ }
170+
171+ // Verify new encryption/decryption works
172+ String encrypted2 = encryptor2 .encrypt (testData );
173+ String decrypted2 = encryptor2 .decrypt (encrypted2 );
174+ assertEquals ("New key should work for encryption/decryption" , testData , decrypted2 );
175+ }
176+
177+ @ Test
178+ public void testMultipleEncryptorInstances () throws Exception {
179+ // Create two encryptor instances
180+ IterableDataEncryptor encryptor1 = new IterableDataEncryptor ();
181+ IterableDataEncryptor encryptor2 = new IterableDataEncryptor ();
182+
183+ // Test that they can decrypt each other's encrypted data
184+ String testData = "test data" ;
185+ String encrypted1 = encryptor1 .encrypt (testData );
186+ String encrypted2 = encryptor2 .encrypt (testData );
187+
188+ assertEquals ("Encryptor 2 should decrypt Encryptor 1's data" , testData , encryptor2 .decrypt (encrypted1 ));
189+ assertEquals ("Encryptor 1 should decrypt Encryptor 2's data" , testData , encryptor1 .decrypt (encrypted2 ));
190+ }
191+ }
0 commit comments