Debian Codegraph @

ipparse_test.go

raw symbols SCIP
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 "g::1",
62 "FE80::A:b",
63 "0:0:0:0:0:0:0:0",
64 "1:2::10.0.0.1",
65 "::.1.2.3",
66 }
67 }
68
69 func Test_ParseIPv4(t *testing.T) {
70 t.Parallel()
71 for _, in := range parseIPSamples() {
72 want, wantErr := netip.ParseAddr(in)
73 wantOK := wantErr == nil && !strings.Contains(in, ":")
74 got, ok := ParseIPv4(in)
75 gotBytes, okBytes := ParseIPv4([]byte(in))
76 require.Equal(t, wantOK, ok, "input %q", in)
77 require.Equal(t, wantOK, okBytes, "input %q", in)
78 if wantOK {
79 require.Equal(t, want, got, "input %q", in)
80 require.Equal(t, want, gotBytes, "input %q", in)
81 } else {
82 require.Equal(t, netip.Addr{}, got, "input %q", in)
83 }
84 }
85 }
86
87 func Test_ParseIPv6(t *testing.T) {
88 t.Parallel()
89 for _, in := range parseIPSamples() {
90 want, wantErr := netip.ParseAddr(in)
91 wantOK := wantErr == nil && strings.Contains(in, ":")
92 got, ok := ParseIPv6(in)
93 gotBytes, okBytes := ParseIPv6([]byte(in))
94 require.Equal(t, wantOK, ok, "input %q", in)
95 require.Equal(t, wantOK, okBytes, "input %q", in)
96 if wantOK {
97 require.Equal(t, want, got, "input %q", in)
98 require.Equal(t, want, gotBytes, "input %q", in)
99 } else {
100 require.Equal(t, netip.Addr{}, got, "input %q", in)
101 }
102 }
103 }
104
105 func Benchmark_ParseIPv4(b *testing.B) {
106 input := "203.0.113.195"
107 b.Run("fiber", func(b *testing.B) {
108 b.ReportAllocs()
109 for b.Loop() {
110 if _, ok := ParseIPv4(input); !ok {
111 b.Fatal("failed to parse")
112 }
113 }
114 })
115 b.Run("default", func(b *testing.B) {
116 b.ReportAllocs()
117 for b.Loop() {
118 if _, err := netip.ParseAddr(input); err != nil {
119 b.Fatal(err)
120 }
121 }
122 })
123 }
124
125 func Benchmark_ParseIPv6(b *testing.B) {
126 inputs := []struct {
127 name string
128 value string
129 }{
130 {"compressed", "2001:db8::8a2e:370:7334"},
131 {"full", "2001:0db8:0000:0000:0000:ff00:0042:8329"},
132 }
133 for _, input := range inputs {
134 b.Run(input.name+"/fiber", func(b *testing.B) {
135 b.ReportAllocs()
136 for b.Loop() {
137 if _, ok := ParseIPv6(input.value); !ok {
138 b.Fatal("failed to parse")
139 }
140 }
141 })
142 b.Run(input.name+"/default", func(b *testing.B) {
143 b.ReportAllocs()
144 for b.Loop() {
145 if _, err := netip.ParseAddr(input.value); err != nil {
146 b.Fatal(err)
147 }
148 }
149 })
150 }
151 }