33import org .junit .Assert ;
44import org .junit .Test ;
55import org .tron .common .overlay .discover .node .Node ;
6+ import org .tron .common .utils .ByteArray ;
67
78public class NodeEntryTest {
89
@@ -23,4 +24,46 @@ public void test() throws InterruptedException {
2324 Assert .assertTrue (isDif );
2425 }
2526
27+ @ Test
28+ public void testDistance () {
29+ byte [] randomId = Node .getNodeId ();
30+ String hexRandomIdStr = ByteArray .toHexString (randomId );
31+ Assert .assertEquals (128 , hexRandomIdStr .length ());
32+
33+ byte [] nodeId1 = ByteArray .fromHexString (
34+ "0000000000000000000000000000000000000000000000000000000000000000"
35+ + "0000000000000000000000000000000000000000000000000000000000000000" );
36+ byte [] nodeId2 = ByteArray .fromHexString (
37+ "a000000000000000000000000000000000000000000000000000000000000000"
38+ + "0000000000000000000000000000000000000000000000000000000000000000" );
39+ Assert .assertEquals (256 , NodeEntry .distance (nodeId1 , nodeId2 ));
40+
41+ byte [] nodeId3 = ByteArray .fromHexString (
42+ "0000000000000000000000000000000000000000000000000000000000000001"
43+ + "0000000000000000000000000000000000000000000000000000000000000000" );
44+ Assert .assertEquals (1 , NodeEntry .distance (nodeId1 , nodeId3 ));
45+
46+ byte [] nodeId4 = ByteArray .fromHexString (
47+ "0000000000000000000000000000000000000000000000000000000000000000"
48+ + "8000000000000000000000000000000000000000000000000000000000000000" );
49+ Assert .assertEquals (0 , NodeEntry .distance (nodeId1 , nodeId4 )); // => 0
50+
51+ byte [] nodeId5 = ByteArray .fromHexString (
52+ "0000000000000000000000000000000000000000000000000000000000000000"
53+ + "4000000000000000000000000000000000000000000000000000000000000000" );
54+ Assert .assertEquals (-1 , NodeEntry .distance (nodeId1 , nodeId5 )); // => 0
55+
56+ byte [] nodeId6 = ByteArray .fromHexString (
57+ "0000000000000000000000000000000000000000000000000000000000000000"
58+ + "2000000000000000000000000000000000000000000000000000000000000000" );
59+ Assert .assertEquals (-2 , NodeEntry .distance (nodeId1 , nodeId6 )); // => 0
60+
61+ byte [] nodeId7 = ByteArray .fromHexString (
62+ "0000000000000000000000000000000000000000000000000000000000000000"
63+ + "0000000000000000000000000000000000000000000000000000000000000001" );
64+ Assert .assertEquals (-255 , NodeEntry .distance (nodeId1 , nodeId7 )); // => 0
65+
66+ Assert .assertEquals (-256 , NodeEntry .distance (nodeId1 , nodeId1 )); // => 0
67+ }
68+
2669}
0 commit comments