From 599756feb8052eda9ac8c50b0e35e441a6ef56c0 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 2 Apr 2020 22:31:53 +0100 Subject: [PATCH] utils: Add test for IPDesc.Equal --- utils/ip_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 utils/ip_test.go diff --git a/utils/ip_test.go b/utils/ip_test.go new file mode 100644 index 0000000..a9ab956 --- /dev/null +++ b/utils/ip_test.go @@ -0,0 +1,64 @@ +// (c) 2020, Alex Willmer. All rights reserved. +// See the file LICENSE for licensing terms. + +package utils + +import ( + "fmt" + "net" + "testing" +) + +func TestIPDescEqual(t *testing.T) { + tests := []struct { + ipDesc1 IPDesc + ipDesc2 IPDesc + result bool + }{ + // Expected equal + { + IPDesc{net.ParseIP("127.0.0.1"), 0}, + IPDesc{net.ParseIP("127.0.0.1"), 0}, + true, + }, { + IPDesc{net.ParseIP("::1"), 0}, + IPDesc{net.ParseIP("::1"), 0}, + true, + }, { + IPDesc{net.ParseIP("127.0.0.1"), 0}, + IPDesc{net.ParseIP("::ffff:127.0.0.1"), 0}, + true, + }, + + // Expected unequal + { + IPDesc{net.ParseIP("127.0.0.1"), 0}, + IPDesc{net.ParseIP("1.2.3.4"), 0}, + false, + }, { + IPDesc{net.ParseIP("::1"), 0}, + IPDesc{net.ParseIP("2001::1"), 0}, + false, + }, { + IPDesc{net.ParseIP("127.0.0.1"), 0}, + IPDesc{net.ParseIP("127.0.0.1"), 1}, + false, + }, + } + for i, tt := range tests { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + if tt.ipDesc1.IP == nil { + t.Error("ipDesc1 nil") + } else if tt.ipDesc2.IP == nil { + t.Error("ipDesc2 nil") + } + result := tt.ipDesc1.Equal(tt.ipDesc2) + if result && result != tt.result { + t.Error("Expected IPDesc to be equal, but they were not") + } + if !result && result != tt.result { + t.Error("Expected IPDesc to be unequal, but they were equal") + } + }) + } +}