Skip to content

Commit 9985bc1

Browse files
authored
Add test coverage for SubzeroUtils.derivePublicKey function (#712)
A few extra tests to increase coverage for this method.
1 parent 8741a97 commit 9985bc1

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

java/shared/src/test/java/com/squareup/subzero/shared/SubzeroUtilsTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import org.bitcoinj.core.Sha256Hash;
2525
import org.bitcoinj.core.VerificationException;
2626
import org.bitcoinj.crypto.DeterministicKey;
27+
import org.bitcoinj.crypto.HDKeyDerivation;
2728
import org.bitcoinj.params.TestNet3Params;
2829
import org.bouncycastle.util.encoders.Hex;
2930
import org.junit.Before;
3031
import org.junit.Test;
3132

3233
import static org.junit.Assert.assertArrayEquals;
3334
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNotNull;
3436
import static org.junit.Assert.assertThrows;
3537
import static org.junit.Assert.assertTrue;
3638
import static com.squareup.subzero.shared.SubzeroUtils.ERROR_ENCRYPTED_MASTER_SEED_SIZE;
@@ -109,7 +111,7 @@ public void setUp() {
109111
assertEquals("2MuAdStu2xZtRSyA5B6wRtj7SmaLjDyfm1H", address.toString());
110112
}
111113

112-
@Test public void derivePublicKey() {
114+
@Test public void testDerivePublicKeyRandom() {
113115
// Using https://iancoleman.io/bip39/, I created a random key. The derivation we use for cold
114116
// wallet is m/coin_type'/change/address_index. The coin_type' is taken care of for us,
115117
// so we only care about change/address_index.
@@ -139,6 +141,38 @@ public void setUp() {
139141
assertArrayEquals(expected, childKey.getPubKey());
140142
}
141143

144+
@Test public void testDerivePublicKeyNegativeIndex() {
145+
DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]);
146+
Path invalidPath = Path.newBuilder().setIsChange(false).setIndex(-1).build();
147+
148+
Exception exception = assertThrows(IllegalStateException.class, () -> {
149+
SubzeroUtils.derivePublicKey(rootKey, invalidPath);
150+
});
151+
152+
assertEquals("index should be between 0 and 2^31-1", exception.getMessage());
153+
}
154+
155+
@Test public void testDerivePublicKeyMaxIndex() {
156+
DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]);
157+
// Integer.MAX_VALUE is 2^31-1.
158+
Path path = Path.newBuilder().setIsChange(false).setIndex(Integer.MAX_VALUE).build();
159+
160+
DeterministicKey derivedKey = SubzeroUtils.derivePublicKey(rootKey, path);
161+
assertEquals(33, derivedKey.getPubKey().length);
162+
}
163+
164+
@Test public void testDerivePublicKeyReproducible() {
165+
DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]);
166+
Path path = Path.newBuilder().setIsChange(false).setIndex(37).build();
167+
168+
DeterministicKey key1 = SubzeroUtils.derivePublicKey(rootKey, path);
169+
DeterministicKey key2 = SubzeroUtils.derivePublicKey(rootKey, path);
170+
171+
assertNotNull(key1);
172+
assertNotNull(key2);
173+
assertEquals(key1, key2);
174+
}
175+
142176
@Test
143177
public void testValidateFees() {
144178
String feeExceedsLimitString = "Transaction fee is greater than 1BTC and 10% of the total "

0 commit comments

Comments
 (0)