github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/model/unmarshallers_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the Apache License Version 2.0. 3 // This product includes software developed at Datadog (https://www.datadoghq.com/). 4 // Copyright 2016-present Datadog, Inc. 5 6 //go:build linux 7 8 // Package model holds model related files 9 package model 10 11 import ( 12 "testing" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 const syscallsEventByteCount = 64 18 19 func syscallListEqual(t *testing.T, a, b []Syscall) bool { 20 mainLoop: 21 for _, elemA := range a { 22 for _, elemB := range b { 23 if elemA == elemB { 24 continue mainLoop 25 } 26 } 27 t.Logf("syscall %s is missing", elemA) 28 return false 29 } 30 return true 31 } 32 33 func allSyscallsTest() syscallsEventTest { 34 all := syscallsEventTest{ 35 name: "all_syscalls", 36 args: make([]byte, syscallsEventByteCount), 37 } 38 39 for i := 0; i < syscallsEventByteCount*8; i++ { 40 all.want = append(all.want, Syscall(i)) 41 42 // should be tested in eBPF... 43 index := i / 8 44 bit := byte(1 << (i % 8)) 45 all.args[index] |= bit 46 } 47 48 return all 49 } 50 51 func oneSyscallTest(s Syscall) syscallsEventTest { 52 one := syscallsEventTest{ 53 name: s.String(), 54 args: make([]byte, syscallsEventByteCount), 55 } 56 57 // should be tested in eBPF ... 58 index := s / 8 59 bit := byte(1 << (s % 8)) 60 one.args[index] |= bit 61 62 one.want = []Syscall{s} 63 return one 64 } 65 66 type syscallsEventTest struct { 67 name string 68 args []byte 69 want []Syscall 70 wantErr error 71 } 72 73 func TestSyscallsEvent_UnmarshalBinary(t *testing.T) { 74 tests := []syscallsEventTest{ 75 { 76 name: "nil_array", 77 wantErr: ErrNotEnoughData, 78 }, 79 { 80 name: "no_syscall", 81 args: make([]byte, 64), 82 }, 83 allSyscallsTest(), 84 } 85 86 // add single syscall tests 87 for i := 0; i < syscallsEventByteCount*8; i++ { 88 tests = append(tests, oneSyscallTest(Syscall(i))) 89 } 90 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 e := &SyscallsEvent{} 94 _, err := e.UnmarshalBinary(tt.args) 95 if err != nil { 96 if err == tt.wantErr { 97 return 98 } 99 assert.Equal(t, nil, err, "expected normal unmarshalling") 100 } 101 assert.Equal(t, true, syscallListEqual(t, tt.want, e.Syscalls), "invalid list of syscalls: %s, expected: %s", e.Syscalls, tt.want) 102 }) 103 } 104 }