-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecureEntropyGeneratorTest.java
More file actions
62 lines (48 loc) · 2.11 KB
/
Copy pathSecureEntropyGeneratorTest.java
File metadata and controls
62 lines (48 loc) · 2.11 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
package com.example.bip39.entropy;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.example.bip39.api.DefaultBip39Service;
import com.example.bip39.error.Bip39ErrorCode;
import com.example.bip39.error.Bip39Exception;
import com.example.bip39.util.Bip39Constants;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class SecureEntropyGeneratorTest {
private static final String ZERO_ENTROPY_MNEMONIC =
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"
+ " about";
@Test
void rejectsUnsupportedEntropyLengths() {
SecureEntropyGenerator generator =
new SecureEntropyGenerator(bytes -> Arrays.fill(bytes, (byte) 0));
Bip39Exception exception =
assertThrows(Bip39Exception.class, () -> generator.generateEntropy(12));
assertEquals(Bip39ErrorCode.ERR_ENTROPY_LENGTH, exception.getErrorCode());
}
@Test
void generatesAllAllowedEntropyLengths() {
SecureEntropyGenerator generator =
new SecureEntropyGenerator(bytes -> Arrays.fill(bytes, (byte) 0));
for (int entropyLength : Bip39Constants.ALLOWED_ENTROPY_LENGTHS_BYTES) {
assertEquals(entropyLength, generator.generateEntropy(entropyLength).length);
}
}
@Test
void generatesDeterministicEntropyWhenSourceIsInjected() {
byte[] expectedEntropy = new byte[16];
for (int index = 0; index < expectedEntropy.length; index++) {
expectedEntropy[index] = (byte) (index + 1);
}
SecureEntropyGenerator generator =
new SecureEntropyGenerator(
bytes -> System.arraycopy(expectedEntropy, 0, bytes, 0, bytes.length));
assertArrayEquals(expectedEntropy, generator.generateEntropy(16));
}
@Test
void convertsGeneratedEntropyThroughBip39ServiceUsingThinAdapter() {
SecureEntropyGenerator generator =
new SecureEntropyGenerator(bytes -> Arrays.fill(bytes, (byte) 0));
assertEquals(ZERO_ENTROPY_MNEMONIC, generator.generateMnemonic(16, new DefaultBip39Service()));
}
}