summaryrefslogtreecommitdiff
path: root/internal/eventloop_test.go
blob: 2749c8047877c6b34dc10edd6454a58ccdc3a382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package internal

import (
	"context"
	"fmt"
	"ior/internal/event"
	"ior/internal/types"
	"syscall"
	"testing"
)

type validateFunc func(t *testing.T, ev *event.Pair)

func TestEventloop(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	inCh := make(chan []byte)
	outCh := make(chan *event.Pair)
	validateCh := make(chan validateFunc)

	go func() {
		defer close(inCh)
		defer close(validateCh)

		enterEv, exitEv, validate := openFileTestdata(t)
		inCh <- enterEv
		inCh <- exitEv
		validateCh <- validate
	}()

	go func() {
		for ev := range outCh {
			t.Run(ev.EnterEv.String(), func(t *testing.T) {
				t.Log("Received", ev)
				validate := <-validateCh
				validate(t, ev)
			})
		}
	}()

	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{
		EventType: types.ENTER_OPEN_EVENT,
		TraceId:   types.SYS_ENTER_OPENAT,
		Time:      123456789,
		Pid:       10,
		Tid:       11,
		Flags:     syscall.O_RDWR,
		Filename:  [types.MAX_FILENAME_LENGTH]byte{},
		Comm:      [types.MAX_PROGNAME_LENGTH]byte{},
	}
	copy(enterEv.Filename[:], "testfile.txt")
	copy(enterEv.Comm[:], "testcomm")

	var err error

	enterEvBytes, err = enterEv.Bytes()
	if err != nil {
		t.Error(err)
	}

	exitEv := types.RetEvent{
		EventType: types.EXIT_OPEN_EVENT,
		TraceId:   types.SYS_EXIT_OPENAT,
		Time:      123456789,
		Ret:       42,
		Pid:       10,
		Tid:       11,
	}
	exitEvBytes, err = exitEv.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
}