-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
112 lines (99 loc) · 2.54 KB
/
main_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
package main
import (
"os"
"reflect"
"testing"
)
func TestGetPorts(t *testing.T) {
cases := []struct {
inFile string
ports []int
}{
{"testdata/top500.xml", []int{21, 22, 139, 445}},
{"testdata/allports.xml", []int{21, 22, 139, 445, 3632}},
{"testdata/local.xml", []int{25, 111, 139, 445, 2049, 35111, 40495, 49577, 57135}},
}
for _, c := range cases {
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
fileBytes := readerToBytes(in)
ports := getPorts(fileBytes)
if !reflect.DeepEqual(ports, c.ports) {
t.Logf("want: %v", c.ports)
t.Logf("have: %v", ports)
t.Errorf("failed getting ports from %s", c.inFile)
}
}
}
func TestPortsToNmap(t *testing.T) {
cases := []struct {
inFile string
out string
}{
{"testdata/top500.xml", "-p21,22,139,445"},
{"testdata/allports.xml", "-p21,22,139,445,3632"},
{"testdata/local.xml", "-p25,111,139,445,2049,35111,40495,49577,57135"},
}
for _, c := range cases {
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
fileBytes := readerToBytes(in)
ports := getPorts(fileBytes)
portsString := portsToNmap(ports)
if portsString != c.out {
t.Logf("want: %v", c.out)
t.Logf("have: %v", portsString)
t.Errorf("failed getting ports' string for nmap from %s", c.inFile)
}
}
}
func TestGetAddress(t *testing.T) {
cases := []struct {
inFile string
address string
}{
{"testdata/top500.xml", "10.10.10.3"},
{"testdata/allports.xml", "10.10.10.3"},
{"testdata/local.xml", "127.0.0.1"},
}
for _, c := range cases {
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
fileBytes := readerToBytes(in)
address := getAddress(fileBytes)
if address != c.address {
t.Logf("want: %v", c.address)
t.Logf("have: %v", address)
t.Errorf("failed getting address from %s", c.inFile)
}
}
}
func TestGetHostnames(t *testing.T) {
cases := []struct {
inFile string
hostnames []string
}{
{"testdata/top500.xml", []string{"lame.htb"}},
{"testdata/allports.xml", []string{"lame.htb"}},
{"testdata/local.xml", []string{"localhost"}},
}
for _, c := range cases {
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
fileBytes := readerToBytes(in)
hostnames := getHostnames(fileBytes)
if !reflect.DeepEqual(hostnames, c.hostnames) {
t.Logf("want: %v", c.hostnames)
t.Logf("have: %v", hostnames)
t.Errorf("failed getting hostnames from %s", c.inFile)
}
}
}