-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSecureCharBufferTests.java
More file actions
173 lines (153 loc) · 5.32 KB
/
SecureCharBufferTests.java
File metadata and controls
173 lines (153 loc) · 5.32 KB
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
/*
* SecureString library, Obfuscated/clearable in memory string management
*
* Copyright (C) 2017-2022 Alan Evans, NovaCrypto
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Original source: https://github.com/NovaCrypto/SecureString
* You can contact the authors via github issues.
*/
package io.github.novacrypto;
import org.junit.Test;
import static io.github.novacrypto.TestHelpers.readWholeBufferAsString;
import static org.junit.Assert.assertEquals;
public final class SecureCharBufferTests {
@Test
public void initialLengthIsZero() {
SecureCharBuffer buffer = new SecureCharBuffer();
assertEquals(0, buffer.length());
}
@Test
public void defaultCapacity() {
SecureCharBuffer buffer = new SecureCharBuffer();
assertEquals(512, buffer.capacity());
}
@Test
public void canSpecifyCapacity() {
SecureCharBuffer buffer = SecureCharBuffer.withCapacity(10);
assertEquals(10, buffer.capacity());
}
@Test
public void capacityIsNumberOfChars() {
SecureCharBuffer buffer = SecureCharBuffer.withCapacity(1);
buffer.append('a');
}
@Test
public void appendIncreasesLength() {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append('a');
assertEquals(1, buffer.length());
}
@Test
public void twoAppendsIncreaseLength() {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append('a');
buffer.append('b');
assertEquals(2, buffer.length());
}
@Test
public void afterAppendCanReadBufferContents() {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append('a');
assertEquals(1, buffer.length());
final int i = 0;
assertEquals('a', readCharAt(buffer, i));
}
@Test
public void afterAppendCanReadBufferContentsZ() {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append('z');
assertEquals(1, buffer.length());
assertEquals('z', readCharAt(buffer, 0));
}
@Test
public void canAppendCharSequences() {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append("abc");
final CharSequence def = "def";
buffer.append(def);
assertEquals("abcdef", readWholeBufferAsString(buffer));
}
@Test
public void canReadManyChars() {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append("abc");
assertEquals("abc", readWholeBufferAsString(buffer));
}
@Test
public void dataIsZeroedOutAfterClose() {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append("abc");
buffer.close();
final String read = readWholeBufferAsString(buffer);
assertEquals(512, buffer.length());
assertEquals(512, read.length());
for (int i = 0; i < read.length(); i++) {
assertEquals('\0', read.charAt(i));
}
}
@Test
public void readAndWriteAllBasicASCII() {
SecureCharBuffer buffer = new SecureCharBuffer();
for (int i = 0; i < 128; i++) {
buffer.append((char) i);
}
assertEquals(128, buffer.length());
for (int i = 0; i < 128; i++) {
assertEquals((char) i, readCharAt(buffer, i));
}
}
@Test
public void readAndWriteAllExtendedAscii() {
SecureCharBuffer buffer = new SecureCharBuffer();
for (int i = 128; i < 256; i++) {
buffer.append((char) i);
}
assertEquals(128, buffer.length());
for (int i = 128; i < 256; i++) {
assertEquals((char) i, readCharAt(buffer, i - 128));
}
}
@Test
public void readAndWriteAllLargeChars() {
SecureCharBuffer buffer = SecureCharBuffer.withCapacity(255 * 256);
for (int i = 256; i < 256 * 256; i++) {
buffer.append((char) i);
}
assertEquals(255 * 256, buffer.length());
for (int i = 256; i < 257; i++) {
assertEquals((char) i, readCharAt(buffer, i - 256));
}
}
@Test
public void testTimeout() throws InterruptedException {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append("abc");
buffer.timout(1000);
Thread.sleep(1500);
final String read = readWholeBufferAsString(buffer);
assertEquals(512, buffer.length());
assertEquals(512, read.length());
for (int i = 0; i < read.length(); i++) {
assertEquals('\0', read.charAt(i));
}
}
private static char readCharAt(SecureCharBuffer buffer, int index) {
final char c1 = buffer.get(index);
final char c2 = buffer.charAt(index);
assertEquals(c1, c2);
return c1;
}
}