From dc9e0a581603f42ffd8e2cb8b180d3b21513ce41 Mon Sep 17 00:00:00 2001 From: dongjiang1989 Date: Sun, 31 Mar 2024 19:51:28 +0800 Subject: [PATCH 1/3] dongjiang, fix ipv6 shortener and expander equal Signed-off-by: dongjiang1989 --- endpoint/endpoint.go | 32 +++++++++++++++++++++++++++++++- endpoint/endpoint_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index a45f61ac42..cee79910e0 100644 --- a/endpoint/endpoint.go +++ b/endpoint/endpoint.go @@ -75,7 +75,17 @@ func (t Targets) Len() int { } func (t Targets) Less(i, j int) bool { - return t[i] < t[j] + ipi, err := netip.ParseAddr(t[i]) + if err != nil { + return t[i] < t[j] + } + + ipj, err := netip.ParseAddr(t[j]) + if err != nil { + return t[i] < t[j] + } + + return ipi.String() < ipj.String() } func (t Targets) Swap(i, j int) { @@ -92,6 +102,26 @@ func (t Targets) Same(o Targets) bool { for i, e := range t { if !strings.EqualFold(e, o[i]) { + ipA, err := netip.ParseAddr(e) + if err != nil { + log.WithFields(log.Fields{ + "targets": t, + "comparisonTargets": o, + }).Debugf("Couldn't parse %s as an IP address: %v", e, err) + } + + ipB, err := netip.ParseAddr(o[i]) + if err != nil { + log.WithFields(log.Fields{ + "targets": t, + "comparisonTargets": o, + }).Debugf("Couldn't parse %s as an IP address: %v", e, err) + } + + // IPv6 Address Shortener == IPv6 Address Expander + if ipA.IsValid() && ipB.IsValid() { + return ipA.String() == ipB.String() + } return false } } diff --git a/endpoint/endpoint_test.go b/endpoint/endpoint_test.go index 47c23e2f4f..51234bdb0f 100644 --- a/endpoint/endpoint_test.go +++ b/endpoint/endpoint_test.go @@ -41,6 +41,7 @@ func TestTargetsSame(t *testing.T) { {""}, {"1.2.3.4"}, {"8.8.8.8", "8.8.4.4"}, + {"dd:dd::01", "::1", "::0001"}, {"example.org", "EXAMPLE.ORG"}, } @@ -51,6 +52,33 @@ func TestTargetsSame(t *testing.T) { } } +func TestSameSuccess(t *testing.T) { + tests := []struct { + a Targets + b Targets + }{ + { + []string{"::1"}, + []string{"::0001"}, + }, + { + []string{"::1", "dd:dd::01"}, + []string{"dd:00dd::0001", "::0001"}, + }, + + { + []string{"::1", "dd:dd::01"}, + []string{"00dd:dd::0001", "::0001"}, + }, + } + + for _, d := range tests { + if d.a.Same(d.b) == false { + t.Errorf("%#v should equal %#v", d.a, d.b) + } + } +} + func TestSameFailures(t *testing.T) { tests := []struct { a Targets From b5ef2fc9135d272ea648f5a43080634ce5084433 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Sun, 7 Apr 2024 09:44:08 +0800 Subject: [PATCH 2/3] Update endpoint/endpoint.go Co-authored-by: Michel Loiseleur <97035654+mloiseleur@users.noreply.github.com> --- endpoint/endpoint.go | 1 + 1 file changed, 1 insertion(+) diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index cee79910e0..f7af843f6e 100644 --- a/endpoint/endpoint.go +++ b/endpoint/endpoint.go @@ -102,6 +102,7 @@ func (t Targets) Same(o Targets) bool { for i, e := range t { if !strings.EqualFold(e, o[i]) { + // IPv6 can be shortened, so it should be parsed for equality checking ipA, err := netip.ParseAddr(e) if err != nil { log.WithFields(log.Fields{ From f3c1e32930c9c83a412fc6cb02e45535e3ecd88b Mon Sep 17 00:00:00 2001 From: dongjiang1989 Date: Tue, 9 Apr 2024 09:34:58 +0800 Subject: [PATCH 3/3] add unittest casae Signed-off-by: dongjiang1989 --- endpoint/endpoint_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/endpoint/endpoint_test.go b/endpoint/endpoint_test.go index 51234bdb0f..a3b3399cf4 100644 --- a/endpoint/endpoint_test.go +++ b/endpoint/endpoint_test.go @@ -70,6 +70,10 @@ func TestSameSuccess(t *testing.T) { []string{"::1", "dd:dd::01"}, []string{"00dd:dd::0001", "::0001"}, }, + { + []string{"::1", "1.1.1.1", "2600.com", "3.3.3.3"}, + []string{"2600.com", "::0001", "3.3.3.3", "1.1.1.1"}, + }, } for _, d := range tests { @@ -97,6 +101,10 @@ func TestSameFailures(t *testing.T) { []string{"1.2.3.4", "4.3.2.1"}, []string{"8.8.8.8", "8.8.4.4"}, }, + { + []string{"::1", "2600.com", "3.3.3.3"}, + []string{"2600.com", "3.3.3.3", "1.1.1.1"}, + }, } for _, d := range tests {