forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
135 lines (110 loc) · 3.83 KB
/
helpers_test.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
//go:build !privileged_tests
package main
// These helpers must be defined in the main package so that the exported shared library functions
// can be called, as the C types used in the prototypes are only available from within the main
// package.
//
// These can not be defined in '_test.go' files, as Go test is not compatible with Cgo.
import (
"testing"
. "github.com/cilium/cilium/proxylib/proxylib"
)
func numConnections() int {
mutex.Lock()
defer mutex.Unlock()
return len(connections)
}
func checkConnectionCount(t *testing.T, expConns int) {
t.Helper()
nConns := numConnections()
if nConns != expConns {
t.Errorf("Number of connections does not match (have %d, but should be %d)", nConns, expConns)
}
}
func checkConnections(t *testing.T, res, expected FilterResult, expConns int) {
t.Helper()
if res != expected {
t.Errorf("OnNewConnection(): Invalid result, have %s, expected %s", res.Error(), expected.Error())
}
checkConnectionCount(t, expConns)
}
func CheckOnNewConnection(t *testing.T, instanceId uint64, proto string, connectionId uint64, ingress bool, srcId, dstId uint32, srcAddr, dstAddr, policyName string, bufSize int, expResult FilterResult, expNumConnections int) *byte {
t.Helper()
origBuf := make([]byte, 0, bufSize)
replyBuf := make([]byte, 1, bufSize)
replyBufAddr := &replyBuf[0]
replyBuf = replyBuf[:0] // make the buffer empty again
res := FilterResult(OnNewConnection(instanceId, proto, connectionId, ingress, srcId, dstId, srcAddr, dstAddr, policyName, &origBuf, &replyBuf))
checkConnections(t, res, expResult, expNumConnections)
return replyBufAddr
}
func CheckClose(t *testing.T, connectionId uint64, replyBufAddr *byte, n int) {
t.Helper()
checkConnectionCount(t, n)
// Find the connection
mutex.Lock()
connection, ok := connections[connectionId]
mutex.Unlock()
if !ok {
t.Errorf("OnData(): Connection %d not found!", connectionId)
} else if replyBufAddr != nil && len(*connection.ReplyBuf) > 0 && replyBufAddr != &(*connection.ReplyBuf)[0] {
t.Error("OnData(): Reply injection buffer reallocated while it must not be!")
}
Close(connectionId)
checkConnectionCount(t, n-1)
}
type ExpFilterOp struct {
op OpType
n_bytes int
}
func checkOps(ops [][2]int64, exp []ExpFilterOp) bool {
if len(ops) != len(exp) {
return false
} else {
for i, op := range ops {
if op[0] != int64(exp[i].op) || op[1] != int64(exp[i].n_bytes) {
return false
}
}
}
return true
}
func checkBuf(t *testing.T, buf InjectBuf, expected string) {
t.Helper()
if len(*buf) < len(expected) {
t.Log("Inject buffer too small, data truncated")
expected = expected[:len(*buf)] // truncate to buffer length
}
if string(*buf) != expected {
t.Errorf("OnData(): Expected inject buffer to be %s, buf have: %s", expected, *buf)
}
}
func checkOnData(t *testing.T, res, expected FilterResult, ops [][2]int64, expOps []ExpFilterOp) {
t.Helper()
if res != expected {
t.Errorf("OnData(): Invalid result, have %s, expected %s", res.Error(), expected.Error())
}
if !checkOps(ops, expOps) {
t.Errorf("OnData(): Unexpected filter operations: %v, expected %v", ops, expOps)
}
}
func CheckOnData(t *testing.T, connectionId uint64, reply, endStream bool, data *[][]byte, expOps []ExpFilterOp, expResult FilterResult, expReplyBuf string) {
t.Helper()
// Find the connection
mutex.Lock()
connection, ok := connections[connectionId]
mutex.Unlock()
if !ok && expResult != UNKNOWN_CONNECTION {
t.Errorf("OnData(): Connection %d not found!", connectionId)
}
ops := make([][2]int64, 0, 1+len(expOps)*2)
res := FilterResult(OnData(connectionId, reply, endStream, data, &ops))
checkOnData(t, res, expResult, ops, expOps)
if ok {
replyBuf := connection.ReplyBuf
checkBuf(t, replyBuf, expReplyBuf)
*replyBuf = (*replyBuf)[:0] // make empty again
}
}