diff --git a/net/ip_test.go b/net/ip_test.go index e286ff88f56e1b324fcfd6236c85527b5f760fef..cd2b5954085989fa80e0b95741628a8523fc7712 100644 --- a/net/ip_test.go +++ b/net/ip_test.go @@ -497,14 +497,23 @@ func TestBitAtPosition(t *testing.T) { position: 1, expected: true, }, + { + name: "IPv4: invalid position", + input: IPv4(0), + position: 33, + expected: false, + }, + { + name: "IPv6: invalid position", + input: IPv6(0, 0), + position: 129, + expected: false, + }, } for _, test := range tests { b := test.input.BitAtPosition(test.position) - if b != test.expected { - t.Errorf("%s: Unexpected failure: Bit %d of %v is %v. Expected %v", - test.name, test.position, test.input, b, test.expected) - } + assert.Equal(t, test.expected, b, test.name) } } @@ -550,3 +559,26 @@ func TestIPFromString(t *testing.T) { }) } } + +func TestSizeBytes(t *testing.T) { + tests := []struct { + name string + input IP + expected uint8 + }{ + { + name: "IPv4", + input: IPv4(0), + expected: 4, + }, + { + name: "IPv6", + input: IPv6(0, 0), + expected: 16, + }, + } + + for _, test := range tests { + assert.Equal(t, test.expected, test.input.SizeBytes(), test.name) + } +} diff --git a/net/prefix_test.go b/net/prefix_test.go index ddd07a6db499cc597d7cc58a9136af9ca21325e2..247c6da0e5e7d1abf4dc2f46402f08f5743b4c3b 100644 --- a/net/prefix_test.go +++ b/net/prefix_test.go @@ -300,13 +300,11 @@ func TestGetSupernet(t *testing.T) { func TestContains(t *testing.T) { tests := []struct { - name string a Prefix b Prefix expected bool }{ { - name: "Test 1", a: Prefix{ addr: IPv4(0), pfxlen: 0, @@ -318,7 +316,6 @@ func TestContains(t *testing.T) { expected: true, }, { - name: "Test 2", a: Prefix{ addr: IPv4(100), pfxlen: 24, @@ -330,7 +327,6 @@ func TestContains(t *testing.T) { expected: false, }, { - name: "Test 3", a: Prefix{ addr: IPv4(167772160), pfxlen: 8, @@ -342,7 +338,6 @@ func TestContains(t *testing.T) { expected: true, }, { - name: "Test 4", a: Prefix{ addr: IPv4(167772160), pfxlen: 8, @@ -354,7 +349,6 @@ func TestContains(t *testing.T) { expected: true, }, { - name: "Test 5", a: Prefix{ addr: IPv4(167772160), pfxlen: 8, @@ -366,7 +360,6 @@ func TestContains(t *testing.T) { expected: false, }, { - name: "Test 6", a: Prefix{ addr: IPv4(167772160), pfxlen: 8, @@ -378,7 +371,6 @@ func TestContains(t *testing.T) { expected: false, }, { - name: "Test 7", a: Prefix{ addr: IPv4FromOctets(169, 0, 0, 0), pfxlen: 25, @@ -390,7 +382,6 @@ func TestContains(t *testing.T) { expected: false, }, { - name: "IPv6: 2001:678:1e0:100::/56 is subnet of 2001:678:1e0::/48", a: Prefix{ addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0), pfxlen: 48, @@ -402,7 +393,6 @@ func TestContains(t *testing.T) { }, }, { - name: "IPv6: 2001:678:1e0:100::/56 is subnet of 2001:678:1e0::/48", a: Prefix{ addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x200, 0, 0, 0, 0), pfxlen: 56, @@ -413,11 +403,22 @@ func TestContains(t *testing.T) { }, expected: false, }, + { + a: Prefix{ + addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x200, 0, 0, 0, 0), + pfxlen: 65, + }, + b: Prefix{ + addr: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0x100, 0, 0, 0, 0), + pfxlen: 64, + }, + expected: false, + }, } for _, test := range tests { res := test.a.Contains(test.b) - assert.Equal(t, res, test.expected, "Unexpected result %v for test %s: %s contains %s", res, test.name, test.a, test.b) + assert.Equal(t, res, test.expected, "Subnet %s contains %s", test.a, test.b) } } diff --git a/route/bgp_path_test.go b/route/bgp_path_test.go index abbd8fb8ff71c29ca1cd18aa3d3a00f1ba7c0c60..16c5b90e5f8fa48faabd1d09cb5bec3e9ecdafac 100644 --- a/route/bgp_path_test.go +++ b/route/bgp_path_test.go @@ -311,9 +311,34 @@ func TestLength(t *testing.T) { for _, test := range tests { calcLen := test.path.Length() + assert.Equal(t, test.expected, calcLen, test.name) + } +} +func TestBGPPathString(t *testing.T) { + tests := []struct { + input BGPPath + expectedPrint string + expectedString string + }{ + { + input: BGPPath{}, + expectedString: "Local Pref: 0, Origin: Incomplete, AS Path: , BGP type: internal, NEXT HOP: 0:0:0:0:0:0:0:0, MED: 0, Path ID: 0, Source: 0:0:0:0:0:0:0:0, Communities: [], LargeCommunities: [], ", + expectedPrint: ` Local Pref: 0 + Origin: Incomplete + AS Path: + BGP type: internal + NEXT HOP: 0:0:0:0:0:0:0:0 + MED: 0 + Path ID: 0 + Source: 0:0:0:0:0:0:0:0 + Communities: [] + LargeCommunities: [] +`, + }, + } - if calcLen != test.expected { - t.Errorf("Unexpected result for test %q: Expected: %d Got: %d", test.name, test.expected, calcLen) - } + for _, test := range tests { + assert.Equal(t, test.expectedString, test.input.String()) + assert.Equal(t, test.expectedPrint, test.input.Print()) } }