Skip to content

Commit

Permalink
fix(tests): possible out of range in integration (#4305)
Browse files Browse the repository at this point in the history
* fix(tests): possible out of range in integration

ExpectAllInOrderSequentially might try to access an index out of range
depending on the number of events that are being checked.

More about in issue #4255.

* fix(tests): use ExpectAtLeastOneForEach

Two test cases were using ExpectAllInOrderSequentially helper function
and passing by luck, since they emit more events than expected only
beyond the expected events boundary.

For that cases we should use ExpectAtLeastOneForEach helper function
instead.
  • Loading branch information
geyslan authored Dec 3, 2024
1 parent bae1532 commit 8f63209
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions tests/integration/event_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ func Test_EventFilters(t *testing.T) {
},
useSyscaller: false,
coolDown: 0,
test: ExpectAllInOrderSequentially,
test: ExpectAtLeastOneForEach,
},
{
name: "pid: trace new (should be empty)",
Expand Down Expand Up @@ -1504,7 +1504,7 @@ func Test_EventFilters(t *testing.T) {
},
useSyscaller: true,
coolDown: 0,
test: ExpectAllInOrderSequentially,
test: ExpectAtLeastOneForEach, // syscaller might emit its own events, so we expect at least one of each
},
{
name: "event: trace execve event set in a specific policy from fakeprog1 command",
Expand Down Expand Up @@ -2460,7 +2460,8 @@ func ExpectAllEvtsEqualToOne(t *testing.T, cmdEvents []cmdEvents, actual *eventB
}

// ExpectAllInOrderSequentially validates that the actual events match the
// expected events for each command, with events appearing in the same order.
// expected events for each command, with events appearing in the same order of the
// expected events.
func ExpectAllInOrderSequentially(t *testing.T, cmdEvents []cmdEvents, actual *eventBuffer, useSyscaller bool) error {
// first stage: run commands
actual.clear()
Expand All @@ -2474,17 +2475,22 @@ func ExpectAllInOrderSequentially(t *testing.T, cmdEvents []cmdEvents, actual *e

actEvtsCopy := actual.getCopy()

actEvtIdx := 0
// second stage: check events
for cmdIdx, cmd := range cmdEvents {
for _, cmd := range cmdEvents {
syscallsInSets := []string{}
checkSets := len(cmd.sets) > 0
if checkSets {
syscallsInSets = getAllSyscallsInSets(cmd.sets)
}

// compare the expected events with the actual events in the same order
for evtIdx, expEvt := range cmd.expectedEvents {
actEvt := actEvtsCopy[cmdIdx*len(cmd.expectedEvents)+evtIdx]
for _, expEvt := range cmd.expectedEvents {
if actEvtIdx >= len(actEvtsCopy) {
return fmt.Errorf("Event %+v:\nnot found or in wrong order in actual output:\n%+v", expEvt, actEvtsCopy)
}
actEvt := actEvtsCopy[actEvtIdx]
actEvtIdx++

if checkSets && !isInSets(actEvt.EventName, syscallsInSets) {
return fmt.Errorf("Event %s not found in sets %v", actEvt.EventName, cmd.sets)
Expand Down

0 comments on commit 8f63209

Please sign in to comment.