File: whitelist_net_test.go

package info (click to toggle)
golang-github-cloudflare-cfssl 1.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,220 kB
  • sloc: asm: 1,936; javascript: 652; makefile: 94; sql: 89; sh: 64; python: 11
file content (92 lines) | stat: -rw-r--r-- 1,810 bytes parent folder | download | duplicates (3)
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
package whitelist

import (
	"encoding/json"
	"net"
	"testing"
)

func TestMarshalNet(t *testing.T) {
	tv := map[string]*BasicNet{
		"test-a": NewBasicNet(),
		"test-b": NewBasicNet(),
	}

	_, n, err := net.ParseCIDR("192.168.3.0/24")
	if err != nil {
		t.Fatalf("%v", err)
	}
	tv["test-a"].Add(n)

	_, n, err = net.ParseCIDR("192.168.7.0/24")
	if err != nil {
		t.Fatalf("%v", err)
	}
	tv["test-a"].Add(n)

	out, err := json.Marshal(tv)
	if err != nil {
		t.Fatalf("%v", err)
	}

	var tvPrime map[string]*BasicNet
	err = json.Unmarshal(out, &tvPrime)
	if err != nil {
		t.Fatalf("%v", err)
	}
}

func TestMarshalNetFail(t *testing.T) {
	wl := NewBasicNet()
	badInput := `192.168.3.1/24,127.0.0.1/32`
	if err := wl.UnmarshalJSON([]byte(badInput)); err == nil {
		t.Fatal("Expected failure unmarshaling bad JSON input.")
	}

	badInput = `"192.168.3.1,127.0.0.256"`
	if err := wl.UnmarshalJSON([]byte(badInput)); err == nil {
		t.Fatal("Expected failure unmarshaling bad JSON input.")
	}
}

var testNet *BasicNet

func testAddNet(wl NetACL, ns string, t *testing.T) {
	_, n, err := net.ParseCIDR(ns)
	if err != nil {
		t.Fatalf("%v", err)
	}

	wl.Add(n)
}

func testDelNet(wl NetACL, ns string, t *testing.T) {
	_, n, err := net.ParseCIDR(ns)
	if err != nil {
		t.Fatalf("%v", err)
	}

	wl.Remove(n)
}

func TestAdd(t *testing.T) {
	// call this to make sure it doesn't panic, and to make sure
	// these code paths are executed.
	testNet = NewBasicNet()
	testNet.Add(nil)

	testAddNet(testNet, "192.168.3.0/24", t)
}

func TestRemove(t *testing.T) {
	testNet.Remove(nil)
	testDelNet(testNet, "192.168.1.1/32", t)
	testDelNet(testNet, "192.168.3.0/24", t)
}

func TestFailPermitted(t *testing.T) {
	var ip = []byte{0, 0}
	if testNet.Permitted(ip) {
		t.Fatal("Expected failure checking invalid IP address.")
	}
}