Debian Codegraph @

ipparse_test.go

raw
1 package utils
2
3 import (
4 "net/netip"
5 "strings"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9 )
10
11 func parseIPSamples() []string {
12 return []string{
13 // IPv4
14 "0.0.0.0",
15 "127.0.0.1",
16 "203.0.113.195",
17 "255.255.255.255",
18 "1.2.3.4",
19 "256.1.1.1",
20 "1.2.3.4.5",
21 "1.2.3",
22 "01.2.3.4",
23 "1.02.3.4",
24 "1.2.3.04",
25 "1.2.3.4 ",
26 " 1.2.3.4",
27 "1..3.4",
28 "1.2.3.",
29 ".1.2.3",
30 "1.2.3.4567",
31 "a.b.c.d",
32 "1.2.3.-4",
33 "",
34 // IPv6
35 "::",
36 "::1",
37 "1::",
38 "2001:db8::8a2e:370:7334",
39 "2001:0db8:0000:0000:0000:ff00:0042:8329",
40 "1:2:3:4:5:6:7:8",
41 "1:2:3:4:5:6:7:8:9",
42 "1:2:3:4:5:6:7::",
43 "1::2:3:4:5:6:7:8",
44 "1:::2",
45 "1::2::3",
46 ":1:2:3",
47 "1:2:3:",
48 ":",
49 ":::",
50 "fe80::1%eth0",
51 "fe80::1%",
52 "%eth0",
53 "::%25eth0",
54 "::ffff:192.168.0.1",
55 "::ffff:1.2.3.400",
56 "1:2:3:4:5:6:1.2.3.4",
57 "1:2:3:4:5:6:7:1.2.3.4",
58 "::1.2.3.4",
59 "1.2.3.4::",
60 "12345::",
61 // Over-long fields whose value still fits in 0xFFFF.
62 "00001::2",
63 "::00000",
64 "00000::",
65 "1:2:3:4:5:6:7:00008",
66 "cec::7dcE:0D568",
67 // Exactly four digits, leading zeros included: valid.
68 "0001:2:3:4:5:6:7:8",
69 "0000::0000",
70 "g::1",
71 "FE80::A:b",
72 "0:0:0:0:0:0:0:0",
73 "1:2::10.0.0.1",
74 "::.1.2.3",
75 }
76 }
77
78 func Test_ParseIPv4(t *testing.T) {
79 t.Parallel()
80 for _, in := range parseIPSamples() {
81 want, wantErr := netip.ParseAddr(in)
82 wantOK := wantErr == nil && !strings.Contains(in, ":")
83 got, ok := ParseIPv4(in)
84 gotBytes, okBytes := ParseIPv4([]byte(in))
85 require.Equal(t, wantOK, ok, "input %q", in)
86 require.Equal(t, wantOK, okBytes, "input %q", in)
87 if wantOK {
88 require.Equal(t, want, got, "input %q", in)
89 require.Equal(t, want, gotBytes, "input %q", in)
90 } else {
91 require.Equal(t, netip.Addr{}, got, "input %q", in)
92 }
93 }
94 }
95
96 func Test_ParseIPv6(t *testing.T) {
97 t.Parallel()
98 for _, in := range parseIPSamples() {
99 want, wantErr := netip.ParseAddr(in)
100 wantOK := wantErr == nil && strings.Contains(in, ":")
101 got, ok := ParseIPv6(in)
102 gotBytes, okBytes := ParseIPv6([]byte(in))
103 require.Equal(t, wantOK, ok, "input %q", in)
104 require.Equal(t, wantOK, okBytes, "input %q", in)
105 if wantOK {
106 require.Equal(t, want, got, "input %q", in)
107 require.Equal(t, want, gotBytes, "input %q", in)
108 } else {
109 require.Equal(t, netip.Addr{}, got, "input %q", in)
110 }
111 }
112 }
113
114 func Benchmark_ParseIPv4(b *testing.B) {
115 input := "203.0.113.195"
116 b.Run("fiber", func(b *testing.B) {
117 b.ReportAllocs()
118 for b.Loop() {
119 if _, ok := ParseIPv4(input); !ok {
120 b.Fatal("failed to parse")
121 }
122 }
123 })
124 b.Run("default", func(b *testing.B) {
125 b.ReportAllocs()
126 for b.Loop() {
127 if _, err := netip.ParseAddr(input); err != nil {
128 b.Fatal(err)
129 }
130 }
131 })
132 }
133
134 func Benchmark_ParseIPv6(b *testing.B) {
135 inputs := []struct {
136 name string
137 value string
138 }{
139 {"compressed", "2001:db8::8a2e:370:7334"},
140 {"full", "2001:0db8:0000:0000:0000:ff00:0042:8329"},
141 }
142 for _, input := range inputs {
143 b.Run(input.name+"/fiber", func(b *testing.B) {
144 b.ReportAllocs()
145 for b.Loop() {
146 if _, ok := ParseIPv6(input.value); !ok {
147 b.Fatal("failed to parse")
148 }
149 }
150 })
151 b.Run(input.name+"/default", func(b *testing.B) {
152 b.ReportAllocs()
153 for b.Loop() {
154 if _, err := netip.ParseAddr(input.value); err != nil {
155 b.Fatal(err)
156 }
157 }
158 })
159 }
160 }