Skip to content

Commit 819e8f0

Browse files
committed
test: add unit tests for pkg/common/utils #1310
What type of PR is this? test Check the PR title. This PR title match the format: <type>(optional scope): <description> The description of this PR title is user-oriented and clear enough for others to understand. (Optional) Translate the PR title into Chinese. 测试:为pkg/common/utils添加测试 (Optional) Which issue(s) this PR fixes:
1 parent e41240c commit 819e8f0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

pkg/common/utils/network_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,52 @@ func TestLocalIP(t *testing.T) {
5252
assert.DeepEqual(t, got, expectedIP)
5353
}
5454
}
55+
56+
// TestGetLocalIp tests the getLocalIp function with different network interface scenarios
57+
func TestGetLocalIp(t *testing.T) {
58+
// Since getLocalIp is not exported, we can test it indirectly through LocalIP
59+
// The actual value will depend on the test environment, but we can at least
60+
// verify that it returns a non-empty value that is not the unknown marker
61+
ip := LocalIP()
62+
assert.NotEqual(t, "", ip)
63+
64+
// We can also verify that the returned IP is either a valid IP or the unknown marker
65+
if ip != UNKNOWN_IP_ADDR {
66+
parsedIP := net.ParseIP(ip)
67+
assert.NotNil(t, parsedIP, "LocalIP should return a valid IP address")
68+
}
69+
}
70+
71+
// TestTLSRecordHeaderLooksLikeHTTPWithEdgeCases tests additional edge cases for TLS header detection
72+
func TestTLSRecordHeaderLooksLikeHTTPWithEdgeCases(t *testing.T) {
73+
// Test with partial HTTP method matches
74+
partialMatches := [][5]byte{
75+
{'G', 'E', 'T', 'x', '/'}, // Almost "GET /"
76+
{'H', 'E', 'A', 'x', ' '}, // Almost "HEAD "
77+
{'P', 'O', 'S', 'x', ' '}, // Almost "POST "
78+
{'P', 'U', 'T', 'x', '/'}, // Almost "PUT /"
79+
{'O', 'P', 'T', 'x', 'O'}, // Almost "OPTIO"
80+
}
81+
82+
for _, header := range partialMatches {
83+
assert.False(t, TLSRecordHeaderLooksLikeHTTP(header),
84+
"Partial HTTP method match should not be detected as HTTP")
85+
}
86+
87+
// Test with empty header
88+
emptyHeader := [5]byte{}
89+
assert.False(t, TLSRecordHeaderLooksLikeHTTP(emptyHeader),
90+
"Empty header should not be detected as HTTP")
91+
92+
// Test with other common HTTP methods that are not in the list
93+
otherMethods := [][5]byte{
94+
{'D', 'E', 'L', 'E', 'T'}, // DELETE
95+
{'P', 'A', 'T', 'C', 'H'}, // PATCH
96+
{'T', 'R', 'A', 'C', 'E'}, // TRACE
97+
}
98+
99+
for _, header := range otherMethods {
100+
assert.False(t, TLSRecordHeaderLooksLikeHTTP(header),
101+
"Other HTTP methods should not be detected as HTTP by this function")
102+
}
103+
}

0 commit comments

Comments
 (0)