summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-04-16 16:11:42 +0300
committerPaul Buetow <paul@buetow.org>2025-04-16 16:11:42 +0300
commit8373683c8fb16b7d26a9b4056eab4461f12c3e66 (patch)
tree30889732b0f49d96d69c63e0e10ba2b581c73ed6 /internal
parent9745923f0acc994181f643fb02b612f10426f41b (diff)
fix
Diffstat (limited to 'internal')
-rw-r--r--internal/eventloop_test.go85
-rw-r--r--internal/file/file.go8
-rw-r--r--internal/file/file_test.go2
3 files changed, 66 insertions, 29 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
}
diff --git a/internal/file/file.go b/internal/file/file.go
index 6c29961..aff9b8b 100644
--- a/internal/file/file.go
+++ b/internal/file/file.go
@@ -25,7 +25,7 @@ type FdFile struct {
func NewFd(fd int32, name []byte, flags int32) FdFile {
f := FdFile{
fd: fd,
- name: stringValue(name),
+ name: StringValue(name),
flags: Flags(flags),
}
if f.flags == -1 {
@@ -106,7 +106,7 @@ type oldnameNewnameFile struct {
}
func NewOldnameNewname(oldname, newname []byte) oldnameNewnameFile {
- return oldnameNewnameFile{stringValue(oldname), stringValue(newname)}
+ return oldnameNewnameFile{StringValue(oldname), StringValue(newname)}
}
func (f oldnameNewnameFile) Name() string {
@@ -136,7 +136,7 @@ type pathnameFile struct {
}
func NewPathname(pathname []byte) pathnameFile {
- return pathnameFile{stringValue(pathname)}
+ return pathnameFile{StringValue(pathname)}
}
func (f pathnameFile) Name() string {
@@ -160,6 +160,6 @@ func (f pathnameFile) String() string {
}
// As data comes in from arrays, converted to slices, there will be null-bytes at the end..
-func stringValue(byteStr []byte) string {
+func StringValue(byteStr []byte) string {
return string(byteStr[:bytes.IndexByte(byteStr, 0)])
}
diff --git a/internal/file/file_test.go b/internal/file/file_test.go
index dc59738..a8b6fb2 100644
--- a/internal/file/file_test.go
+++ b/internal/file/file_test.go
@@ -8,7 +8,7 @@ func TestStringValue(t *testing.T) {
var array [128]byte
copy(array[:], "test string")
- if str := stringValue(array[:]); str != "test string" {
+ if str := StringValue(array[:]); str != "test string" {
t.Errorf("epxected 'test string' but got '%s' with bytes '%v'", str, []byte(str))
}
}