-
Notifications
You must be signed in to change notification settings - Fork 36
/
runbook_test.go
77 lines (67 loc) · 1.49 KB
/
runbook_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
package main
import (
"encoding/json"
"net"
"testing"
)
var allowedNetworksSuccessScript = `
{
"scripts": [
{
"command": "echo"
}
],
"allowedNetworks": [
"127.0.0.1/32",
"10.0.0.0/8"
]
}`
var allowedNetworksFailureScript = `
{
"scripts": [
{
"command": "echo"
}
],
"allowedNetworks": [
"127.0.0.1/32",
"10.0"
]
}`
func TestNetworkUnmarshalling(t *testing.T) {
r := runBook{}
err := json.Unmarshal([]byte(allowedNetworksSuccessScript), &r)
if err != nil {
t.Errorf("JSON unmarshalling of allowed sources failed: %v", err)
}
if len(r.AllowedNetworks.Networks) != 2 {
t.Errorf("JSON unmarshalling didn't produce the correct result: %v", r)
}
r = runBook{}
err = json.Unmarshal([]byte(allowedNetworksFailureScript), &r)
if err == nil {
t.Errorf("JSON unmarshalling of allowed sources unexpectedly succeeded: %v", r)
}
}
func TestAddrIsAllowed(t *testing.T) {
testIPs := []struct {
ip net.IP
result bool
}{
{net.ParseIP("127.0.0.1"), true},
{net.ParseIP("172.16.0.1"), false},
{net.ParseIP("10.0.0.1"), true},
{net.ParseIP("10.0.1.1"), false},
}
nets := make([]net.IPNet, 2)
for i, cidr := range []string{"127.0.0.1/32", "10.0.0.0/24"} {
_, ipnet, _ := net.ParseCIDR(cidr)
nets[i] = *ipnet
}
r := runBook{AllowedNetworks: Networks{Networks: nets}}
for _, testIP := range testIPs {
if r.AddrIsAllowed(testIP.ip) != testIP.result {
t.Errorf("AddrIsAllowed %v expected %v", testIP.ip, testIP.result)
}
}
}