diff options
| author | Paul Buetow <paul@buetow.org> | 2025-04-16 16:11:42 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-04-16 16:11:42 +0300 |
| commit | 8373683c8fb16b7d26a9b4056eab4461f12c3e66 (patch) | |
| tree | 30889732b0f49d96d69c63e0e10ba2b581c73ed6 /internal/eventloop_test.go | |
| parent | 9745923f0acc994181f643fb02b612f10426f41b (diff) | |
fix
Diffstat (limited to 'internal/eventloop_test.go')
| -rw-r--r-- | internal/eventloop_test.go | 85 |
1 files changed, 61 insertions, 24 deletions
diff --git a/internal/eventloop_test.go b/internal/eventloop_test.go index 2749c80..c1d57c8 100644 --- a/internal/eventloop_test.go +++ b/internal/eventloop_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "ior/internal/event" + "ior/internal/file" "ior/internal/types" "syscall" "testing" @@ -23,10 +24,7 @@ func TestEventloop(t *testing.T) { defer close(inCh) defer close(validateCh) - enterEv, exitEv, validate := openFileTestdata(t) - inCh <- enterEv - inCh <- exitEv - validateCh <- validate + addTests(t, inCh, validateCh) }() go func() { @@ -41,14 +39,60 @@ func TestEventloop(t *testing.T) { ev := newEventLoop() ev.printCb = func(ev *event.Pair) { - t.Log("printCb", ev) outCh <- ev } ev.run(ctx, inCh) } -func openFileTestdata(t *testing.T) (enterEvBytes, exitEvBytes []byte, validate validateFunc) { - enterEv := types.OpenEvent{ +func addTests(t *testing.T, inCh chan []byte, validateCh chan validateFunc) { + addOpenFileTest1(t, inCh, validateCh) + addOpenFileTest2(t, inCh, validateCh) +} + +func addOpenFileTest1(t *testing.T, inCh chan<- []byte, validateCh chan<- validateFunc) { + enterEv, enterEvBytes := makeEnterOpenEvent(t) + inCh <- enterEvBytes // Should be discarded by the event loop automatically + inCh <- enterEvBytes + _, exitEvBytes := makeExitOpenEvent(t) + inCh <- exitEvBytes + inCh <- exitEvBytes // Should be discarded by the event loop automatically + + // Define the validation function and send it to the validateCh channel + validate := func(t *testing.T, ev *event.Pair) { + if ev.EnterEv.GetTraceId() != enterEv.TraceId { + t.Errorf("Expected TraceId '%v' but got '%v'", enterEv.TraceId, ev.EnterEv.GetTraceId()) + } + t.Log(fmt.Sprintf("Event pair '%v' appears fine", ev)) + } + validateCh <- validate +} + +func addOpenFileTest2(t *testing.T, inCh chan<- []byte, validateCh chan<- validateFunc) { + enterEv, enterEvBytes := makeEnterOpenEvent(t) + _, exitEvBytes := makeExitOpenEvent(t) + inCh <- enterEvBytes + inCh <- enterEvBytes + inCh <- exitEvBytes + + // Define the validation function and send it to the validateCh channel + validate := func(t *testing.T, ev *event.Pair) { + if ev.EnterEv.GetTraceId() != enterEv.TraceId { + t.Errorf("Expected TraceId '%v' but got '%v'", enterEv.TraceId, ev.EnterEv.GetTraceId()) + return + } + filenameA := ev.FileName() + filenameB := file.StringValue(enterEv.Filename[:]) + if filenameA != filenameB { + t.Errorf("Expected file name '%v' but got '%v'", filenameB, filenameA) + return + } + t.Log(fmt.Sprintf("Event pair '%v' appears fine", ev)) + } + validateCh <- validate +} + +func makeEnterOpenEvent(t *testing.T) (types.OpenEvent, []byte) { + ev := types.OpenEvent{ EventType: types.ENTER_OPEN_EVENT, TraceId: types.SYS_ENTER_OPENAT, Time: 123456789, @@ -58,17 +102,18 @@ func openFileTestdata(t *testing.T) (enterEvBytes, exitEvBytes []byte, validate Filename: [types.MAX_FILENAME_LENGTH]byte{}, Comm: [types.MAX_PROGNAME_LENGTH]byte{}, } - copy(enterEv.Filename[:], "testfile.txt") - copy(enterEv.Comm[:], "testcomm") + copy(ev.Filename[:], "testfile.txt") + copy(ev.Comm[:], "testcomm") - var err error - - enterEvBytes, err = enterEv.Bytes() + bytes, err := ev.Bytes() if err != nil { t.Error(err) } + return ev, bytes +} - exitEv := types.RetEvent{ +func makeExitOpenEvent(t *testing.T) (types.RetEvent, []byte) { + ev := types.RetEvent{ EventType: types.EXIT_OPEN_EVENT, TraceId: types.SYS_EXIT_OPENAT, Time: 123456789, @@ -76,18 +121,10 @@ func openFileTestdata(t *testing.T) (enterEvBytes, exitEvBytes []byte, validate Pid: 10, Tid: 11, } - exitEvBytes, err = exitEv.Bytes() + + bytes, err := ev.Bytes() if err != nil { t.Error(err) - } - - validate = func(t *testing.T, ev *event.Pair) { - if ev.EnterEv.GetTraceId() != enterEv.TraceId { - t.Errorf("Expected TraceId '%v' but got '%v'", enterEv.TraceId, ev.EnterEv.GetTraceId()) - } - t.Log(fmt.Sprintf("Event pair '%v' appears fine", ev)) - } - - return + return ev, bytes } |
