summaryrefslogtreecommitdiff
path: root/internal/eventloop_test.go
blob: 045e8f066b66bef29e336773b768ef5f4287ee97 (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
package internal

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

// TODO: Finish this test
func TestEventloop(t *testing.T) {
	T = t

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	inCh := make(chan []byte)
	outCh := make(chan *event.Pair, 2)

	go func() {
		defer close(inCh)
		sendOpenFileTracepoints(t, inCh)
	}()

	go func() {
		for ev := range outCh {
			t.Log("Received", ev)
		}
	}()

	ev := newEventLoop()
	ev.printCb = func(ev *event.Pair) {
		t.Log("printCb", ev)
		outCh <- ev
	}
	ev.run(ctx, inCh)
}

func sendOpenFileTracepoints(t *testing.T, ch chan<- []byte) {
	enterOpenEvent := types.OpenEvent{
		EventType: types.ENTER_OPEN_EVENT,
		TraceId:   types.SYS_ENTER_OPENAT,
		Time:      123456789,
		Pid:       10,
		Tid:       10,
		Flags:     syscall.O_RDWR,
		Filename:  [types.MAX_FILENAME_LENGTH]byte{},
		Comm:      [types.MAX_PROGNAME_LENGTH]byte{},
	}
	copy(enterOpenEvent.Filename[:], "testfile.txt")
	copy(enterOpenEvent.Comm[:], "testcomm")

	bytes, err := enterOpenEvent.Bytes()
	if err != nil {
		t.Error(err)
	}
	t.Log("Sending", enterOpenEvent, bytes)
	ch <- bytes

	exitOpenEvent := types.RetEvent{
		EventType: types.EXIT_OPEN_EVENT,
		TraceId:   types.SYS_EXIT_OPENAT,
		Time:      123456789,
		Ret:       42,
		Pid:       10,
		Tid:       10,
	}
	bytes, err = exitOpenEvent.Bytes()
	if err != nil {
		t.Error(err)
	}
	t.Log("Sending", exitOpenEvent, bytes)
	ch <- bytes
}