mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-13 02:35:52 +00:00
PeerAddressTest: Add roundtrip tests for all combinations of relevant protocol versions and IP address type.
This commit is contained in:
parent
7c270c23e6
commit
3975a89f6a
@ -17,14 +17,15 @@
|
|||||||
|
|
||||||
package org.bitcoinj.core;
|
package org.bitcoinj.core;
|
||||||
|
|
||||||
import org.bitcoinj.params.MainNetParams;
|
import static org.bitcoinj.core.Utils.HEX;
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
|
||||||
import static org.bitcoinj.core.Utils.HEX;
|
import org.bitcoinj.params.MainNetParams;
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class PeerAddressTest {
|
public class PeerAddressTest {
|
||||||
private static final NetworkParameters MAINNET = MainNetParams.get();
|
private static final NetworkParameters MAINNET = MainNetParams.get();
|
||||||
@ -33,16 +34,65 @@ public class PeerAddressTest {
|
|||||||
public void testPeerAddressRoundtrip() throws Exception {
|
public void testPeerAddressRoundtrip() throws Exception {
|
||||||
// copied verbatim from https://en.bitcoin.it/wiki/Protocol_specification#Network_address
|
// copied verbatim from https://en.bitcoin.it/wiki/Protocol_specification#Network_address
|
||||||
String fromSpec = "010000000000000000000000000000000000ffff0a000001208d";
|
String fromSpec = "010000000000000000000000000000000000ffff0a000001208d";
|
||||||
PeerAddress pa = new PeerAddress(MAINNET,
|
PeerAddress pa = new PeerAddress(MAINNET, HEX.decode(fromSpec), 0, 0);
|
||||||
HEX.decode(fromSpec), 0, 0);
|
|
||||||
String reserialized = Utils.HEX.encode(pa.unsafeBitcoinSerialize());
|
String reserialized = Utils.HEX.encode(pa.unsafeBitcoinSerialize());
|
||||||
assertEquals(reserialized,fromSpec );
|
assertEquals(reserialized, fromSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void roundtrip_ipv4_currentProtocolVersion() throws Exception {
|
||||||
|
long time = Utils.currentTimeSeconds();
|
||||||
|
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("1.2.3.4"), 1234,
|
||||||
|
NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion(), BigInteger.ZERO);
|
||||||
|
byte[] serialized = pa.bitcoinSerialize();
|
||||||
|
PeerAddress pa2 = new PeerAddress(MAINNET, serialized, 0,
|
||||||
|
NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion());
|
||||||
|
assertEquals("1.2.3.4", pa2.getAddr().getHostAddress());
|
||||||
|
assertEquals(1234, pa2.getPort());
|
||||||
|
assertEquals(BigInteger.ZERO, pa2.getServices());
|
||||||
|
assertTrue(pa2.getTime() >= time && pa2.getTime() < time + 5); // potentially racy
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void roundtrip_ipv4_ancientProtocolVersion() throws Exception {
|
||||||
|
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("1.2.3.4"), 1234, 0, BigInteger.ZERO);
|
||||||
|
byte[] serialized = pa.bitcoinSerialize();
|
||||||
|
PeerAddress pa2 = new PeerAddress(MAINNET, serialized, 0, 0);
|
||||||
|
assertEquals("1.2.3.4", pa2.getAddr().getHostAddress());
|
||||||
|
assertEquals(1234, pa2.getPort());
|
||||||
|
assertEquals(BigInteger.ZERO, pa2.getServices());
|
||||||
|
assertEquals(-1, pa2.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void roundtrip_ipv6_currentProtocolVersion() throws Exception {
|
||||||
|
long time = Utils.currentTimeSeconds();
|
||||||
|
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7334"), 1234,
|
||||||
|
NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion(), BigInteger.ZERO);
|
||||||
|
byte[] serialized = pa.bitcoinSerialize();
|
||||||
|
PeerAddress pa2 = new PeerAddress(MAINNET, serialized, 0,
|
||||||
|
NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion());
|
||||||
|
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", pa2.getAddr().getHostAddress());
|
||||||
|
assertEquals(1234, pa2.getPort());
|
||||||
|
assertEquals(BigInteger.ZERO, pa2.getServices());
|
||||||
|
assertTrue(pa2.getTime() >= time && pa2.getTime() < time + 5); // potentially racy
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void roundtrip_ipv6_ancientProtocolVersion() throws Exception {
|
||||||
|
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName("2001:db8:85a3:0:0:8a2e:370:7334"), 1234, 0,
|
||||||
|
BigInteger.ZERO);
|
||||||
|
byte[] serialized = pa.bitcoinSerialize();
|
||||||
|
PeerAddress pa2 = new PeerAddress(MAINNET, serialized, 0, 0);
|
||||||
|
assertEquals("2001:db8:85a3:0:0:8a2e:370:7334", pa2.getAddr().getHostAddress());
|
||||||
|
assertEquals(1234, pa2.getPort());
|
||||||
|
assertEquals(BigInteger.ZERO, pa2.getServices());
|
||||||
|
assertEquals(-1, pa2.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBitcoinSerialize() throws Exception {
|
public void testBitcoinSerialize() throws Exception {
|
||||||
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName(null), 8333, 0, BigInteger.ZERO);
|
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName(null), 8333, 0, BigInteger.ZERO);
|
||||||
assertEquals("000000000000000000000000000000000000ffff7f000001208d",
|
assertEquals("000000000000000000000000000000000000ffff7f000001208d", Utils.HEX.encode(pa.bitcoinSerialize()));
|
||||||
Utils.HEX.encode(pa.bitcoinSerialize()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user