Skip to content

Commit

Permalink
fix(tests): possible out of range in integration
Browse files Browse the repository at this point in the history
ExpectAllInOrderSequentially might try to access an index out of range
depending on the number of events that are being checked.

More about in issue aquasecurity#4255.
  • Loading branch information
geyslan committed Sep 25, 2024
1 parent 604c391 commit 3f8c90d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions tests/integration/event_filters_test.go
Original file line number Diff line number Diff line change
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 3f8c90d

Please sign in to comment.