MCPcopy Create free account
hub / github.com/google/nftables / TestComment

Function TestComment

xt/comment_test.go:9–62  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

7)
8
9func TestComment(t *testing.T) {
10 t.Parallel()
11 payload := Comment("The quick brown fox jumps over the lazy dog.")
12 oversized := Comment(strings.Repeat("foobar", 100))
13 tests := []struct {
14 name string
15 info InfoAny
16 errmsg string
17 }{
18 {
19 name: "un/marshal Comment round-trip",
20 info: &payload,
21 },
22 {
23 name: "marshal oversized Comment",
24 info: &oversized,
25 errmsg: "comment must be less than 256 bytes, got 600 bytes",
26 },
27 }
28
29 for _, tt := range tests {
30 t.Run(tt.name, func(t *testing.T) {
31 data, err := tt.info.marshal(0, 0)
32 if err != nil {
33 if tt.errmsg != "" && err.Error() == tt.errmsg {
34 return
35 }
36 t.Fatalf("marshal error: %+v", err)
37
38 }
39 if len(data) != CommentSize {
40 t.Fatalf("marshal error: invalid size %d", len(data))
41 }
42 if data[len(data)-1] != 0 {
43 t.Fatalf("marshal error: invalid termination")
44 }
45 var comment Comment
46 var recoveredInfo InfoAny = &comment
47 err = recoveredInfo.unmarshal(0, 0, data)
48 if err != nil {
49 t.Fatalf("unmarshal error: %+v", err)
50 }
51 if !reflect.DeepEqual(tt.info, recoveredInfo) {
52 t.Fatalf("original %+v and recovered %+v are different", tt.info, recoveredInfo)
53 }
54 })
55 }
56
57 oversizeddata := []byte(oversized)
58 var comment Comment
59 if err := (&comment).unmarshal(0, 0, oversizeddata); err == nil {
60 t.Fatalf("unmarshal: expected error, but got nil")
61 }
62}

Callers

nothing calls this directly

Calls 3

unmarshalMethod · 0.95
CommentTypeAlias · 0.85
marshalMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…