forked from cyberark/secretless-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_plugin_test.go
More file actions
105 lines (87 loc) · 2.15 KB
/
example_plugin_test.go
File metadata and controls
105 lines (87 loc) · 2.15 KB
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
package main
import (
"bufio"
"fmt"
"net"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const testIODeadline = 2 * time.Second
// readNLines reads n lines from net.Conn using testIODeadline as the read
// deadline to avoid tests that hang forever.
func readNLines(conn net.Conn, n int) ([]string, error) {
defer func() {
_ = conn.SetReadDeadline(time.Time{})
}()
var lines []string
lineReader := bufio.NewReader(conn)
for linesRead := 0; linesRead < n; linesRead++ {
_ = conn.SetReadDeadline(time.Now().Add(testIODeadline))
line, _, err := lineReader.ReadLine()
if err != nil {
err = fmt.Errorf(
"failed reading line %d: %s",
linesRead+1,
err)
return nil, err
}
lines = append(lines, string(line))
}
return lines, nil
}
// writeLine writes a line to net.Conn using testIODeadline as the write
// deadline to avoid tests that hang forever.
func writeLine(conn net.Conn, b []byte) error {
defer func() {
_ = conn.SetWriteDeadline(time.Time{})
}()
_ = conn.SetWriteDeadline(time.Now().Add(testIODeadline))
_, err := conn.Write(append(b, '\n'))
return err
}
func TestTCPPlugin(t *testing.T) {
host := os.Getenv("SECRETLESS_HOST")
if host == "" {
host = "localhost"
}
address := net.JoinHostPort(host, "6175")
// establish connection to secretless
connection, err := net.Dial("tcp", address)
if !assert.NoError(t, err) {
return
}
defer func() {
_ = connection.Close()
}()
t.Run("can inject information from credentials", func(t *testing.T) {
// initial write
err = writeLine(connection, []byte("hello"))
if !assert.NoError(t, err) {
return
}
// initial read
lines, err := readNLines(connection, 2)
if !assert.NoError(t, err) {
return
}
if assert.Equal(t, []string{
"credential injection: some secret credentials",
"initial message from client: hello",
}, lines) {
return
}
})
t.Run("proxies connection to target service", func(t *testing.T) {
err = writeLine(connection, []byte("ping"))
if !assert.NoError(t, err) {
return
}
lines, err := readNLines(connection, 1)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, []string{"ping"}, lines)
})
}