-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprotocol_fuzz_test.go
More file actions
46 lines (39 loc) · 1.1 KB
/
protocol_fuzz_test.go
File metadata and controls
46 lines (39 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// CGo binding for Avahi
//
// Copyright (C) 2025 by Prashant Andoriya
// See LICENSE for license terms and conditions
//
// Fuzz tests for Protocol string conversion
//
//go:build linux || freebsd
package avahi
import (
"strings"
"testing"
)
// FuzzProtocolString fuzzes the Protocol.String method using valid, invalid, and extreme integer values
func FuzzProtocolString(f *testing.F) {
// Valid protocol values
f.Add(int(ProtocolIP4))
f.Add(int(ProtocolIP6))
f.Add(int(ProtocolUnspec))
// Invalid / adversarial values
f.Add(0)
f.Add(-1)
f.Add(1 << 29)
f.Add(0xffffffff)
f.Fuzz(func(t *testing.T, v int) {
proto := Protocol(v)
s := proto.String()
// Must never panic and must always return a non empty string
if s == "" {
t.Fatalf("Protocol.String() returned empty string for value %d", v)
}
// For unknown values, String() should clearly indicate that the protocol is unsupported
if v != int(ProtocolIP4) && v != int(ProtocolIP6) && v != int(ProtocolUnspec) {
if !strings.HasPrefix(s, "UNKNOWN") {
t.Fatalf("unexpected string for unknown protocol %d: %q", v, s)
}
}
})
}