github.com/cilium/cilium@v1.16.2/pkg/monitor/datapath_drop_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package monitor 5 6 import ( 7 "bytes" 8 "encoding/binary" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/cilium/cilium/pkg/byteorder" 14 ) 15 16 func TestDecodeDropNotify(t *testing.T) { 17 // This check on the struct length constant is there to ensure that this 18 // test is updated when the struct changes. 19 require.Equal(t, 36, DropNotifyLen) 20 21 input := DropNotify{ 22 Type: 0x00, 23 SubType: 0x01, 24 Source: 0x02_03, 25 Hash: 0x04_05_06_07, 26 OrigLen: 0x08_09_0a_0b, 27 CapLen: 0x0c_0d_0e_10, 28 SrcLabel: 0x11_12_13_14, 29 DstLabel: 0x15_16_17_18, 30 DstID: 0x19_1a_1b_1c, 31 Line: 0x1d_1e, 32 File: 0x20, 33 ExtError: 0x21, 34 Ifindex: 0x22_23_24_25, 35 } 36 buf := bytes.NewBuffer(nil) 37 err := binary.Write(buf, byteorder.Native, input) 38 require.Nil(t, err) 39 40 output := &DropNotify{} 41 err = DecodeDropNotify(buf.Bytes(), output) 42 require.Nil(t, err) 43 44 require.Equal(t, input.Type, output.Type) 45 require.Equal(t, input.SubType, output.SubType) 46 require.Equal(t, input.Source, output.Source) 47 require.Equal(t, input.Hash, output.Hash) 48 require.Equal(t, input.OrigLen, output.OrigLen) 49 require.Equal(t, input.CapLen, output.CapLen) 50 require.Equal(t, input.SrcLabel, output.SrcLabel) 51 require.Equal(t, input.DstLabel, output.DstLabel) 52 require.Equal(t, input.DstID, output.DstID) 53 require.Equal(t, input.Line, output.Line) 54 require.Equal(t, input.File, output.File) 55 require.Equal(t, input.ExtError, output.ExtError) 56 require.Equal(t, input.Ifindex, output.Ifindex) 57 } 58 59 func BenchmarkNewDecodeDropNotify(b *testing.B) { 60 input := DropNotify{} 61 buf := bytes.NewBuffer(nil) 62 63 if err := binary.Write(buf, byteorder.Native, input); err != nil { 64 b.Fatal(err) 65 } 66 67 b.ReportAllocs() 68 b.ResetTimer() 69 70 for i := 0; i < b.N; i++ { 71 dn := &DropNotify{} 72 if err := DecodeDropNotify(buf.Bytes(), dn); err != nil { 73 b.Fatal(err) 74 } 75 } 76 } 77 78 func BenchmarkOldDecodeDropNotify(b *testing.B) { 79 input := DropNotify{} 80 buf := bytes.NewBuffer(nil) 81 82 if err := binary.Write(buf, byteorder.Native, input); err != nil { 83 b.Fatal(err) 84 } 85 86 b.ReportAllocs() 87 b.ResetTimer() 88 89 for i := 0; i < b.N; i++ { 90 dn := &DropNotify{} 91 if err := binary.Read(bytes.NewReader(buf.Bytes()), byteorder.Native, dn); err != nil { 92 b.Fatal(err) 93 } 94 } 95 }