-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrypUtilTest.java
174 lines (131 loc) · 5.55 KB
/
CrypUtilTest.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* 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 org.junit.Test;
import java.io.UnsupportedEncodingException;
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;
/**
* {@link CrypUtil} utility tests.
*/
public class CrypUtilTest {
private String testKeyAlias = "testKeyAlias";
private byte[] clearTextToEncrypt;
{
try {
clearTextToEncrypt = "clear text to encrypt".getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Test
public void encodeTest(){
try {
String testString = "the Quick Brown fox jumped over the lazy dog 0123456789";
byte[] clearEncodedBytes = CrypUtil.encodeToB64( testString);
String clearString = CrypUtil.decodeFromB64( clearEncodedBytes);
assertThat( clearString.contentEquals( testString), is( true));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void encryptTest(){
try {
String testString = "the Quick Brown fox jumped over the lazy dog 0123456789";
byte[] clearBytes = CrypUtil.getBytes( testString);
byte[] encryptedBytes = CrypUtil.encrypt(clearBytes);
String encryptedEncodedString = CrypUtil.encodeToB64( encryptedBytes );
// Persist put and get normally goes here
String encryptedEncodedString2 = new String( encryptedEncodedString);
byte[] encryptedBytes2 = CrypUtil.decodeFromB64( encryptedEncodedString2);
assertThat( Arrays.equals( encryptedBytes, encryptedBytes2),
is( true));
byte[] clearBytes2 = CrypUtil.decrypt( encryptedBytes2);
assertThat( Arrays.equals( clearBytes, clearBytes2),
is( true));
String testString2 = CrypUtil.toStringUTF8( clearBytes2);
assertThat( testString.contentEquals( testString2), is( true));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void encrypt() throws Exception {
Context ctx = getContext();
boolean keyCreated = KeystoreUtil.createKeyNotExists( ctx, this.testKeyAlias);
byte[] crypBytes = KeystoreUtil.encrypt( testKeyAlias, clearTextToEncrypt);
boolean noMatch = Arrays.equals( crypBytes, clearTextToEncrypt);
assertThat( noMatch, is( false ));
assertThat( crypBytes.length > 0, is( true ));
KeystoreUtil.deleteKey( getContext(), this.testKeyAlias, true);
}
@Test
public void decrypt() throws Exception {
LogUtil.log( CrypUtilTest.class, CrypUtilTest.class.getCanonicalName()+" test starting");
Context ctx = getContext();
KeystoreUtil.createKeyNotExists( ctx, this.testKeyAlias);
byte[] crypBytes = new byte[0];
byte[] clearBytes = new byte[0];
try {
crypBytes = KeystoreUtil.encrypt( testKeyAlias, clearTextToEncrypt);
} catch (Exception e) {
LogUtil.logException(CrypUtilTest.class, e);
}
try {
clearBytes = KeystoreUtil.decrypt( testKeyAlias, crypBytes);
} catch (Exception e) {
LogUtil.logException(CrypUtilTest.class, e);
}
assertThat( Arrays.equals( clearBytes, clearTextToEncrypt), is(true));
KeystoreUtil.deleteKey( getContext(), this.testKeyAlias, true);
LogUtil.log( CrypUtilTest.class, CrypUtilTest.class.getCanonicalName()+" test ending");
}
@Test
public void testInt() throws Exception {
Context ctx = getContext();
KeystoreUtil.createKeyNotExists( ctx, this.testKeyAlias);
int testInt = 123;
byte[] cryptInt = CrypUtil.encryptInt( testInt);
assertThat( cryptInt.length > 0, is( true));
int resultInt = CrypUtil.decryptInt( cryptInt);
assertThat(resultInt == testInt, is( true ));
KeystoreUtil.deleteKey( getContext(), this.testKeyAlias, true);
}
@Test
public void stringToBytesToCharToBytesToString(){
String stringToTest = "string to test";
byte[] bytesToTest = new byte[0];
try {
bytesToTest = CrypUtil.toBytesUTF8( stringToTest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
char[] chars = CrypUtil.toChar(bytesToTest);
assertThat( chars.length > 0, is(true));
byte[] resultBytes = CrypUtil.toBytesUTF8( chars);
assertThat( Arrays.equals( resultBytes, bytesToTest), is(true));
String resultString = CrypUtil.toStringUTF8( resultBytes);
assertThat( stringToTest.contentEquals( resultString), is(true));
}
@Test
public void testCleanArray(){
String stringToTest = "string to test";
byte[] bytesToTest = new byte[0];
try {
bytesToTest = CrypUtil.toBytesUTF8( stringToTest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
assertThat( bytesToTest.length > 0, is(true));
bytesToTest = CrypUtil.cleanArray( bytesToTest);
assertThat( bytesToTest.length == 0, is(true));
}
}