|
3 | 3 |
|
4 | 4 | import static org.junit.Assert.assertArrayEquals; |
5 | 5 | import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertFalse; |
6 | 7 | import static org.junit.Assert.assertNotEquals; |
7 | 8 | import static org.junit.Assert.fail; |
8 | 9 |
|
9 | 10 | import org.junit.Test; |
10 | 11 |
|
11 | 12 | import java.nio.charset.StandardCharsets; |
12 | 13 | import java.security.SecureRandom; |
| 14 | +import java.util.ArrayList; |
13 | 15 | import java.util.Arrays; |
| 16 | +import java.util.Collections; |
14 | 17 | import java.util.HashMap; |
| 18 | +import java.util.List; |
15 | 19 | import java.util.Map; |
16 | 20 |
|
17 | 21 | public class ShamirCoreUnitTest { |
18 | 22 |
|
| 23 | + @Test |
| 24 | + public void integration() throws SimpleException { |
| 25 | + final SecureRandom random = new SecureRandom(); |
| 26 | + for (int i = 0; i < 100; i++) { |
| 27 | + // generate a random secret |
| 28 | + int length = random.nextInt(9001) + 1000; // 1000..10000 |
| 29 | + byte[] secretBytes = new byte[length]; |
| 30 | + random.nextBytes(secretBytes); |
| 31 | + // choose totalShards ∈ [2..10] and threshold ∈ [2..totalShards] |
| 32 | + short totalShards = (short) (random.nextInt(9) + 2); |
| 33 | + short threshold = (short) (random.nextInt(totalShards - 1) + 2); |
| 34 | + // split into shards |
| 35 | + Map<Short, byte[]> shards = ShamirCore.split(secretBytes, totalShards, threshold, null); |
| 36 | + |
| 37 | + for (int j = 0; j < 10; j++) { |
| 38 | + // incorrect restore check |
| 39 | + if (threshold > 2) { |
| 40 | + // pick a random subset of shards not enough to restore (size < threshold) |
| 41 | + int notEnoughToRestoreCount = random.nextInt(threshold - 2) + 2; |
| 42 | + List<Short> keys = new ArrayList<>(shards.keySet()); |
| 43 | + Collections.shuffle(keys); |
| 44 | + List<Short> notEnoughKeys = keys.subList(0, notEnoughToRestoreCount); |
| 45 | + Map<Short, byte[]> shardsNotEnough = new HashMap<>(); |
| 46 | + for (Short k : notEnoughKeys) { |
| 47 | + shardsNotEnough.put(k, shards.get(k)); |
| 48 | + } |
| 49 | + // restore and verify |
| 50 | + byte[] badRestored = ShamirCore.restore(shardsNotEnough, null); |
| 51 | + assertFalse("Incorrectly restored data matched original", Arrays.equals(secretBytes, badRestored)); |
| 52 | + } |
| 53 | + |
| 54 | + // correct restore check |
| 55 | + // pick a random subset of shards to restore (size ∈ [threshold..totalShards]) |
| 56 | + int enoughToRestoreCount = random.nextInt(totalShards - threshold + 1) + threshold; |
| 57 | + List<Short> keysForCorrect = new ArrayList<>(shards.keySet()); |
| 58 | + Collections.shuffle(keysForCorrect); |
| 59 | + List<Short> enoughKeys = keysForCorrect.subList(0, enoughToRestoreCount); |
| 60 | + Map<Short, byte[]> shardsEnough = new HashMap<>(); |
| 61 | + for (Short k : enoughKeys) { |
| 62 | + shardsEnough.put(k, shards.get(k)); |
| 63 | + } |
| 64 | + // restore and verify |
| 65 | + byte[] goodRestored = ShamirCore.restore(shardsEnough, null); |
| 66 | + assertArrayEquals("Correctly restored data does not match original", secretBytes, goodRestored); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
19 | 71 | @Test |
20 | 72 | public void wrongShards() { |
21 | 73 |
|
|
0 commit comments