Rename static NetworkParameters constants in test cases.

This commit is contained in:
Andreas Schildbach
2018-02-27 11:34:19 +01:00
parent 31a06d7095
commit ebd402eb4b
2 changed files with 33 additions and 36 deletions

View File

@@ -35,19 +35,19 @@ import static org.bitcoinj.core.Utils.HEX;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class AddressTest { public class AddressTest {
static final NetworkParameters testParams = TestNet3Params.get(); private static final NetworkParameters TESTNET = TestNet3Params.get();
static final NetworkParameters mainParams = MainNetParams.get(); private static final NetworkParameters MAINNET = MainNetParams.get();
@Test @Test
public void testJavaSerialization() throws Exception { public void testJavaSerialization() throws Exception {
Address testAddress = Address.fromBase58(testParams, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv"); Address testAddress = Address.fromBase58(TESTNET, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
new ObjectOutputStream(os).writeObject(testAddress); new ObjectOutputStream(os).writeObject(testAddress);
Address testAddressCopy = (Address) new ObjectInputStream(new ByteArrayInputStream(os.toByteArray())) Address testAddressCopy = (Address) new ObjectInputStream(new ByteArrayInputStream(os.toByteArray()))
.readObject(); .readObject();
assertEquals(testAddress, testAddressCopy); assertEquals(testAddress, testAddressCopy);
Address mainAddress = Address.fromBase58(mainParams, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL"); Address mainAddress = Address.fromBase58(MAINNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
os = new ByteArrayOutputStream(); os = new ByteArrayOutputStream();
new ObjectOutputStream(os).writeObject(mainAddress); new ObjectOutputStream(os).writeObject(mainAddress);
Address mainAddressCopy = (Address) new ObjectInputStream(new ByteArrayInputStream(os.toByteArray())) Address mainAddressCopy = (Address) new ObjectInputStream(new ByteArrayInputStream(os.toByteArray()))
@@ -58,21 +58,21 @@ public class AddressTest {
@Test @Test
public void stringification() throws Exception { public void stringification() throws Exception {
// Test a testnet address. // Test a testnet address.
Address a = new Address(testParams, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); Address a = new Address(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString()); assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
assertFalse(a.isP2SHAddress()); assertFalse(a.isP2SHAddress());
Address b = new Address(mainParams, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a")); Address b = new Address(MAINNET, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString()); assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
assertFalse(b.isP2SHAddress()); assertFalse(b.isP2SHAddress());
} }
@Test @Test
public void decoding() throws Exception { public void decoding() throws Exception {
Address a = Address.fromBase58(testParams, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv"); Address a = Address.fromBase58(TESTNET, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
assertEquals("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc", Utils.HEX.encode(a.getHash160())); assertEquals("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc", Utils.HEX.encode(a.getHash160()));
Address b = Address.fromBase58(mainParams, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL"); Address b = Address.fromBase58(MAINNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
assertEquals("4a22c3c4cbb31e4d03b15550636762bda0baf85a", Utils.HEX.encode(b.getHash160())); assertEquals("4a22c3c4cbb31e4d03b15550636762bda0baf85a", Utils.HEX.encode(b.getHash160()));
} }
@@ -80,7 +80,7 @@ public class AddressTest {
public void errorPaths() { public void errorPaths() {
// Check what happens if we try and decode garbage. // Check what happens if we try and decode garbage.
try { try {
Address.fromBase58(testParams, "this is not a valid address!"); Address.fromBase58(TESTNET, "this is not a valid address!");
fail(); fail();
} catch (WrongNetworkException e) { } catch (WrongNetworkException e) {
fail(); fail();
@@ -90,7 +90,7 @@ public class AddressTest {
// Check the empty case. // Check the empty case.
try { try {
Address.fromBase58(testParams, ""); Address.fromBase58(TESTNET, "");
fail(); fail();
} catch (WrongNetworkException e) { } catch (WrongNetworkException e) {
fail(); fail();
@@ -100,7 +100,7 @@ public class AddressTest {
// Check the case of a mismatched network. // Check the case of a mismatched network.
try { try {
Address.fromBase58(testParams, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL"); Address.fromBase58(TESTNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
fail(); fail();
} catch (WrongNetworkException e) { } catch (WrongNetworkException e) {
// Success. // Success.
@@ -164,33 +164,33 @@ public class AddressTest {
// Test that we can convert them from hashes // Test that we can convert them from hashes
byte[] hex = HEX.decode("2ac4b0b501117cc8119c5797b519538d4942e90e"); byte[] hex = HEX.decode("2ac4b0b501117cc8119c5797b519538d4942e90e");
Address a = Address.fromP2SHHash(mainParams, hex); Address a = Address.fromP2SHHash(MAINNET, hex);
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString()); assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString());
Address b = Address.fromP2SHHash(testParams, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722")); Address b = Address.fromP2SHHash(TESTNET, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString()); assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString());
Address c = Address.fromP2SHScript(mainParams, ScriptBuilder.createP2SHOutputScript(hex)); Address c = Address.fromP2SHScript(MAINNET, ScriptBuilder.createP2SHOutputScript(hex));
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString()); assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString());
} }
@Test @Test
public void p2shAddressCreationFromKeys() throws Exception { public void p2shAddressCreationFromKeys() throws Exception {
// import some keys from this example: https://gist.github.com/gavinandresen/3966071 // import some keys from this example: https://gist.github.com/gavinandresen/3966071
ECKey key1 = DumpedPrivateKey.fromBase58(mainParams, "5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU").getKey(); ECKey key1 = DumpedPrivateKey.fromBase58(MAINNET, "5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU").getKey();
key1 = ECKey.fromPrivate(key1.getPrivKeyBytes()); key1 = ECKey.fromPrivate(key1.getPrivKeyBytes());
ECKey key2 = DumpedPrivateKey.fromBase58(mainParams, "5Jb7fCeh1Wtm4yBBg3q3XbT6B525i17kVhy3vMC9AqfR6FH2qGk").getKey(); ECKey key2 = DumpedPrivateKey.fromBase58(MAINNET, "5Jb7fCeh1Wtm4yBBg3q3XbT6B525i17kVhy3vMC9AqfR6FH2qGk").getKey();
key2 = ECKey.fromPrivate(key2.getPrivKeyBytes()); key2 = ECKey.fromPrivate(key2.getPrivKeyBytes());
ECKey key3 = DumpedPrivateKey.fromBase58(mainParams, "5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw").getKey(); ECKey key3 = DumpedPrivateKey.fromBase58(MAINNET, "5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw").getKey();
key3 = ECKey.fromPrivate(key3.getPrivKeyBytes()); key3 = ECKey.fromPrivate(key3.getPrivKeyBytes());
List<ECKey> keys = Arrays.asList(key1, key2, key3); List<ECKey> keys = Arrays.asList(key1, key2, key3);
Script p2shScript = ScriptBuilder.createP2SHOutputScript(2, keys); Script p2shScript = ScriptBuilder.createP2SHOutputScript(2, keys);
Address address = Address.fromP2SHScript(mainParams, p2shScript); Address address = Address.fromP2SHScript(MAINNET, p2shScript);
assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString()); assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString());
} }
@Test @Test
public void cloning() throws Exception { public void cloning() throws Exception {
Address a = new Address(testParams, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); Address a = new Address(TESTNET, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
Address b = a.clone(); Address b = a.clone();
assertEquals(a, b); assertEquals(a, b);
@@ -205,7 +205,7 @@ public class AddressTest {
@Test @Test
public void comparisonCloneEqualTo() throws Exception { public void comparisonCloneEqualTo() throws Exception {
Address a = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX"); Address a = Address.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
Address b = a.clone(); Address b = a.clone();
int result = a.compareTo(b); int result = a.compareTo(b);
@@ -214,7 +214,7 @@ public class AddressTest {
@Test @Test
public void comparisonEqualTo() throws Exception { public void comparisonEqualTo() throws Exception {
Address a = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX"); Address a = Address.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
Address b = a.clone(); Address b = a.clone();
int result = a.compareTo(b); int result = a.compareTo(b);
@@ -223,8 +223,8 @@ public class AddressTest {
@Test @Test
public void comparisonLessThan() throws Exception { public void comparisonLessThan() throws Exception {
Address a = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX"); Address a = Address.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
Address b = Address.fromBase58(mainParams, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P"); Address b = Address.fromBase58(MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
int result = a.compareTo(b); int result = a.compareTo(b);
assertTrue(result < 0); assertTrue(result < 0);
@@ -232,8 +232,8 @@ public class AddressTest {
@Test @Test
public void comparisonGreaterThan() throws Exception { public void comparisonGreaterThan() throws Exception {
Address a = Address.fromBase58(mainParams, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P"); Address a = Address.fromBase58(MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
Address b = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX"); Address b = Address.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
int result = a.compareTo(b); int result = a.compareTo(b);
assertTrue(result > 0); assertTrue(result > 0);
@@ -242,8 +242,8 @@ public class AddressTest {
@Test @Test
public void comparisonBytesVsString() throws Exception { public void comparisonBytesVsString() throws Exception {
// TODO: To properly test this we need a much larger data set // TODO: To properly test this we need a much larger data set
Address a = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX"); Address a = Address.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
Address b = Address.fromBase58(mainParams, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P"); Address b = Address.fromBase58(MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
int resultBytes = a.compareTo(b); int resultBytes = a.compareTo(b);
int resultsString = a.toString().compareTo(b.toString()); int resultsString = a.toString().compareTo(b.toString());

View File

@@ -25,26 +25,23 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/**
*
*/
public class VersionedChecksummedBytesTest { public class VersionedChecksummedBytesTest {
static final NetworkParameters testParams = TestNet3Params.get(); private static final NetworkParameters TESTNET = TestNet3Params.get();
static final NetworkParameters mainParams = MainNetParams.get(); private static final NetworkParameters MAINNET = MainNetParams.get();
@Test @Test
public void stringification() throws Exception { public void stringification() throws Exception {
// Test a testnet address. // Test a testnet address.
VersionedChecksummedBytes a = new VersionedChecksummedBytes(testParams.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); VersionedChecksummedBytes a = new VersionedChecksummedBytes(TESTNET.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString()); assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
VersionedChecksummedBytes b = new VersionedChecksummedBytes(mainParams.getAddressHeader(), HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a")); VersionedChecksummedBytes b = new VersionedChecksummedBytes(MAINNET.getAddressHeader(), HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString()); assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
} }
@Test @Test
public void cloning() throws Exception { public void cloning() throws Exception {
VersionedChecksummedBytes a = new VersionedChecksummedBytes(testParams.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); VersionedChecksummedBytes a = new VersionedChecksummedBytes(TESTNET.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
VersionedChecksummedBytes b = a.clone(); VersionedChecksummedBytes b = a.clone();
assertEquals(a, b); assertEquals(a, b);
@@ -53,7 +50,7 @@ public class VersionedChecksummedBytesTest {
@Test @Test
public void comparisonCloneEqualTo() throws Exception { public void comparisonCloneEqualTo() throws Exception {
VersionedChecksummedBytes a = new VersionedChecksummedBytes(testParams.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc")); VersionedChecksummedBytes a = new VersionedChecksummedBytes(TESTNET.getAddressHeader(), HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
VersionedChecksummedBytes b = a.clone(); VersionedChecksummedBytes b = a.clone();
assertTrue(a.compareTo(b) == 0); assertTrue(a.compareTo(b) == 0);