/
CryptoUtilTest.java
131 lines (120 loc) · 4.87 KB
/
CryptoUtilTest.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
/*******************************************************************************
* Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
* Red Hat Inc
*******************************************************************************/
package org.eclipse.kapua.commons.util;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.lang.reflect.Constructor;
import java.util.Random;
@Category(JUnitTests.class)
public class CryptoUtilTest {
private static final Random RANDOM = RandomUtils.getInstance();
@Test
public void testConstructor() throws Exception {
Constructor<CryptoUtil> crypto = CryptoUtil.class.getDeclaredConstructor();
crypto.setAccessible(true);
crypto.newInstance();
}
@Test
public void testSha1Hash() throws Exception {
StringBuilder longTest = new StringBuilder();
String permittedSymbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
for (int i = 0; i < 500; i++) {
longTest.append(permittedSymbols.charAt(RANDOM.nextInt(permittedSymbols.length())));
}
String longName = longTest.toString();
String[] permittedStrings = new String[]{"a", "ab", "abc", "123", longName};
int sizeOfPermittedStrings = permittedStrings.length;
String[] falseStrings = new String[]{null};
int sizeOfFalseStrings = falseStrings.length;
// Negative tests
for (int i = 0; i < sizeOfFalseStrings; i++) {
try {
CryptoUtil.sha1Hash(falseStrings[i]);
Assert.fail("Exception expected for: " + falseStrings[i]);
} catch (Exception ex) {
// Expected
}
}
// Positive tests
for (int i = 0; i < sizeOfPermittedStrings; i++) {
try {
CryptoUtil.sha1Hash(permittedStrings[i]);
} catch (Exception ex) {
Assert.fail("No exception expected for 'test string'");
}
}
}
@Test
public void testEncodeBase64() {
StringBuilder longTest = new StringBuilder();
String permittedSymbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
for (int i = 0; i < 500; i++) {
longTest.append(permittedSymbols.charAt(RANDOM.nextInt(permittedSymbols.length())));
}
String longName = longTest.toString();
String[] permittedStrings = new String[]{"a", "ab", "abc", "123", longName};
int sizeOfPermittedStrings = permittedStrings.length;
String[] falseStrings = new String[]{null};
int sizeOfFalseStrings = falseStrings.length;
// Negative tests
for (int i = 0; i < sizeOfFalseStrings; i++) {
try {
CryptoUtil.encodeBase64(falseStrings[i]);
Assert.fail("Exception expected for: " + falseStrings[i]);
} catch (Exception ex) {
// Expected
}
}
// Positive tests
for (int i = 0; i < sizeOfPermittedStrings; i++) {
try {
CryptoUtil.encodeBase64(permittedStrings[i]);
} catch (Exception ex) {
Assert.fail("No exception expected for 'test string'");
}
}
}
@Test
public void testDecodeBase64() {
StringBuilder longTest = new StringBuilder();
String permittedSymbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
for (int i = 0; i < 500; i++) {
longTest.append(permittedSymbols.charAt(RANDOM.nextInt(permittedSymbols.length())));
}
String longName = longTest.toString();
String[] permittedStrings = new String[]{"a", "ab", "abc", "123", longName};
int sizeOfPermittedStrings = permittedStrings.length;
String[] falseStrings = new String[]{null};
int sizeOfFalseStrings = falseStrings.length;
// Negative tests
for (int i = 0; i < sizeOfFalseStrings; i++) {
try {
CryptoUtil.decodeBase64(falseStrings[i]);
Assert.fail("Exception expected for: " + falseStrings[i]);
} catch (Exception ex) {
// Expected
}
}
// Positive tests
for (int i = 0; i < sizeOfPermittedStrings; i++) {
try {
CryptoUtil.decodeBase64(permittedStrings[i]);
} catch (Exception ex) {
Assert.fail("No exception expected for 'test string'");
}
}
}
}