Skip to content

Commit 9fdd4c8

Browse files
authored
Merge pull request #2914 from alexandear/procnettcp-parse-address-test
Add unit test for procnettcp.ParseAddress
2 parents 9c35258 + 2768259 commit 9fdd4c8

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

pkg/guestagent/procnettcp/procnettcp_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,65 @@ func TestParseUDP(t *testing.T) {
7272
assert.Equal(t, uint16(53), entries[0].Port)
7373
assert.Equal(t, UDPEstablished, entries[0].State)
7474
}
75+
76+
func TestParseAddress(t *testing.T) {
77+
tests := []struct {
78+
input string
79+
expectedIP net.IP
80+
expectedPort uint16
81+
expectedErrSubstr string
82+
}{
83+
{
84+
input: "0100007F:0050",
85+
expectedIP: net.IPv4(127, 0, 0, 1),
86+
expectedPort: 80,
87+
},
88+
{
89+
input: "000080FE00000000FF57A6705DC771FE:0050",
90+
expectedIP: net.ParseIP("fe80::70a6:57ff:fe71:c75d"),
91+
expectedPort: 80,
92+
},
93+
{
94+
input: "00000000000000000000000000000000:0050",
95+
expectedIP: net.IPv6zero,
96+
expectedPort: 80,
97+
},
98+
{
99+
input: "0100007F",
100+
expectedErrSubstr: `unparsable address "0100007F"`,
101+
},
102+
{
103+
input: "invalid:address",
104+
expectedErrSubstr: `unparsable address "invalid:address", expected length of`,
105+
},
106+
{
107+
input: "0100007F:0050:00",
108+
expectedErrSubstr: `unparsable address "0100007F:0050:00"`,
109+
},
110+
{
111+
input: "0100007G:0050", // Invalid hex character 'G'
112+
expectedErrSubstr: `unparsable address "0100007G:0050": unparsable quartet "0100007G"`,
113+
},
114+
{
115+
input: "0100007F:",
116+
expectedErrSubstr: `unparsable address "0100007F:": unparsable port ""`,
117+
},
118+
{
119+
input: "0100007F:invalid",
120+
expectedErrSubstr: `unparsable address "0100007F:invalid": unparsable port "invalid"`,
121+
},
122+
}
123+
124+
for _, test := range tests {
125+
t.Run(test.input, func(t *testing.T) {
126+
ip, port, err := ParseAddress(test.input)
127+
if test.expectedErrSubstr != "" {
128+
assert.ErrorContains(t, err, test.expectedErrSubstr)
129+
} else {
130+
assert.NilError(t, err)
131+
assert.DeepEqual(t, test.expectedIP, ip)
132+
assert.Equal(t, test.expectedPort, port)
133+
}
134+
})
135+
}
136+
}

0 commit comments

Comments
 (0)