-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPersistTest.java
130 lines (93 loc) · 4.77 KB
/
PersistTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2018 Nuvolect LLC.
* This software is offered for free under conditions of the GPLv3 open source software license.
* Contact Nuvolect LLC for a less restrictive commercial license if you would like to use the software
* without the GPLv3 restrictions.
*/
package com.nuvolect.securesuite.util;
import android.content.Context;
import com.nuvolect.securesuite.main.CConst;
import org.junit.Test;
import java.util.Arrays;
import static com.nuvolect.securesuite.main.App.getContext;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
public class PersistTest {
String testKey = "not_collision_key_dksasoiduu";
String testValue = "we value many things in life";
@Test
public void persistKeyCreateDelete(){
Context ctx = getContext();
// Clean up any failed test
if( Persist.keyExists(ctx, testKey))
Persist.deleteKey(ctx, testKey);
Persist.put(ctx, testKey, testValue);
assertThat( Persist.keyExists(ctx, testKey), is(true));
boolean deleted = Persist.deleteKey(ctx, testKey);
assertThat( deleted, is(true));
assertThat( Persist.keyExists(ctx, testKey), is(false));
}
@Test
public void encryptTest(){
try {
Context ctx = getContext();
KeystoreUtil.init( ctx);
// Create test data and convert it to bytes, no encoding required yet
String testString = "the Quick Brown fox jumped over the lazy dog 0123456789";
byte[] clearBytes = CrypUtil.getBytes( testString);
// Encrypt the byte array, creating a new byte array
byte[] encryptedBytes = CrypUtil.encrypt(clearBytes);
assertThat( encryptedBytes != null, is( true));
assertThat( encryptedBytes.length > 0 , is( true));
// Prepare for storage by converting the byte array to a Base64 encoded string
String encryptedEncodedString = CrypUtil.encodeToB64( encryptedBytes );
assertThat( encryptedEncodedString != null, is( true));
assertThat( encryptedEncodedString.length() > 0 , is( true));
// Persist the string, and get it back again
boolean success = Persist.put(ctx, testKey, encryptedEncodedString);
assertThat( success, is( true));
String encryptedEncodedString2 = Persist.get(ctx, testKey);
assertThat( encryptedEncodedString2 != null, is( true));
assertThat( encryptedEncodedString2.length() > 0 , is( true));
// Decode the string back into a byte array using Base64 decode
byte[] encryptedBytes2 = CrypUtil.decodeFromB64( encryptedEncodedString2);
assertThat( Arrays.equals( encryptedBytes, encryptedBytes2), is( true));
// Decrypt the byte array, creating a new byte array
byte[] clearBytes2 = CrypUtil.decrypt( encryptedBytes2);
assertThat( Arrays.equals( clearBytes, clearBytes2), is( true));
// Decode the byte array creating a new String using UTF-8 encoding
String testString2 = CrypUtil.toStringUTF8( clearBytes2);
assertThat( testString.contentEquals( testString2), is( true));
// Cleanup
boolean deleted = Persist.deleteKey(ctx, testKey);
assertThat( deleted, is(true));
assertThat( Persist.keyExists(ctx, testKey), is(false));
} catch (Exception e) {
LogUtil.logException(PersistTest.class, e);
}
}
@Test
public void selfSignedKeystoreStorageTest(){
Context ctx = getContext();
boolean ksKeyExists = Persist.keyExists( ctx, Persist.SELFSIGNED_KS_KEY);
String selfsignedKsKey = null;
// Save it, if it exists, so it can be restored
if( ksKeyExists){
selfsignedKsKey = Persist.get(ctx, Persist.SELFSIGNED_KS_KEY);
Persist.deleteKey(ctx, Persist.SELFSIGNED_KS_KEY);
assertThat( Persist.keyExists( ctx, Persist.SELFSIGNED_KS_KEY), is(false));
}
char[] clearChars= CrypUtil.toChar(CConst.STRING32.getBytes());
Persist.putSelfsignedKsKey(ctx, clearChars);
assertThat( Persist.keyExists( ctx, Persist.SELFSIGNED_KS_KEY), is(true));
char[] clearChars2 = Persist.getSelfsignedKsKey(ctx);
assertThat(Arrays.equals( clearChars, clearChars2), is( true));
boolean keyDeleted = Persist.deleteKey(ctx, Persist.SELFSIGNED_KS_KEY);
assertThat( keyDeleted, is( true ));
assertThat( Persist.keyExists( ctx, Persist.SELFSIGNED_KS_KEY), is(false));
if( ksKeyExists){
Persist.put( ctx, Persist.SELFSIGNED_KS_KEY, selfsignedKsKey);
assertThat( Persist.keyExists( ctx, Persist.SELFSIGNED_KS_KEY), is(true));
}
}
}