Skip to content

Commit 2273086

Browse files
committed
Import common_test.go
1 parent 8b09527 commit 2273086

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

dnscrypt-proxy/common_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"net"
5+
"testing"
6+
)
7+
8+
func TestExtractClientIPStr(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
pluginsState *PluginsState
12+
wantIP string
13+
wantOK bool
14+
}{
15+
{
16+
name: "nil clientAddr should return empty",
17+
pluginsState: &PluginsState{
18+
clientProto: "tcp",
19+
clientAddr: nil,
20+
},
21+
wantIP: "",
22+
wantOK: false,
23+
},
24+
{
25+
name: "valid UDP address",
26+
pluginsState: &PluginsState{
27+
clientProto: "udp",
28+
clientAddr: func() *net.Addr {
29+
addr := net.Addr(
30+
&net.UDPAddr{
31+
IP: net.ParseIP("192.168.1.1"),
32+
Port: 53,
33+
},
34+
)
35+
return &addr
36+
}(),
37+
},
38+
wantIP: "192.168.1.1",
39+
wantOK: true,
40+
},
41+
{
42+
name: "valid TCP address",
43+
pluginsState: &PluginsState{
44+
clientProto: "tcp",
45+
clientAddr: func() *net.Addr {
46+
addr := net.Addr(
47+
&net.TCPAddr{
48+
IP: net.ParseIP("10.0.0.1"),
49+
Port: 53,
50+
},
51+
)
52+
return &addr
53+
}(),
54+
},
55+
wantIP: "10.0.0.1",
56+
wantOK: true,
57+
},
58+
{
59+
name: "unknown protocol",
60+
pluginsState: &PluginsState{
61+
clientProto: "unknown",
62+
clientAddr: func() *net.Addr {
63+
addr := net.Addr(
64+
&net.TCPAddr{
65+
IP: net.ParseIP("10.0.0.1"),
66+
Port: 53,
67+
},
68+
)
69+
return &addr
70+
}(),
71+
},
72+
wantIP: "",
73+
wantOK: false,
74+
},
75+
}
76+
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
gotIP, gotOK := ExtractClientIPStr(tt.pluginsState)
80+
if gotIP != tt.wantIP {
81+
t.Errorf("ExtractClientIPStr() IP = %v, want %v", gotIP, tt.wantIP)
82+
}
83+
if gotOK != tt.wantOK {
84+
t.Errorf("ExtractClientIPStr() OK = %v, want %v", gotOK, tt.wantOK)
85+
}
86+
})
87+
}
88+
}

0 commit comments

Comments
 (0)