-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider_test.go
151 lines (131 loc) · 3.41 KB
/
provider_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package kraaler_test
import (
"compress/gzip"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/aau-network-security/kraaler"
)
func TestDomainFileProvider(t *testing.T) {
tt := []struct {
name string
startServer bool
domains []string
expectedAmount int
}{
{
name: "present server",
startServer: true,
expectedAmount: 1,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
conf := kraaler.DomainFileProviderConfig{}
if tc.startServer {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
}))
defer ts.Close()
u, _ := url.Parse(ts.URL)
_, port, err := net.SplitHostPort(u.Host)
if err != nil {
t.Fatalf("unable to split host and port: %s", err)
}
portInt, err := strconv.Atoi(port)
if err != nil {
t.Fatalf("unable to get port: %s", err)
}
tc.domains = append(tc.domains, "127.0.0.1")
conf.Targets = map[int]func(string) string{
portInt: func(s string) string { return fmt.Sprintf("http://%s", s) },
}
}
tmpfile, err := ioutil.TempFile("", "kraaler-test-file-provider")
if err != nil {
t.Fatalf("unable to create temp file: %s", err)
}
defer os.Remove(tmpfile.Name())
_, err = tmpfile.Write([]byte(strings.Join(tc.domains, "\n")))
if err != nil {
t.Fatalf("unable to write to temp file: %s", err)
}
dfp, err := kraaler.NewDomainFileProvider(tmpfile.Name(), &conf)
if err != nil {
t.Fatalf("unable to create file provider: %s", err)
}
defer dfp.Close()
var urls []*url.URL
for u := range dfp.UrlsC() {
urls = append(urls, u)
}
if tc.expectedAmount != len(urls) {
t.Fatalf("unexpected amount %d, expected: %d", len(urls), tc.expectedAmount)
}
})
}
}
func TestPhishTankReader(t *testing.T) {
tt := []struct {
name string
servOut string
expectedAmount int
}{
{
name: "one url",
servOut: `[{"phish_id":"1","url":"http://test.com","submission_time":"2019-05-12T21:45:13+00:00","verified":"yes","verification_time":"2019-05-12T21:47:54+00:00","online":"yes"}]`,
expectedAmount: 1,
},
{
name: "two urls",
servOut: `[{"phish_id":"2","url":"http://test2.com"},{"phish_id":"1","url":"http://test.com"}]`,
expectedAmount: 2,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var HeadRequestsAmount int
var GetRequestsAmount int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodHead:
w.Header().Add("Etag", fmt.Sprintf("%d", HeadRequestsAmount))
HeadRequestsAmount += 1
case http.MethodGet:
writer := gzip.NewWriter(w)
writer.Write([]byte(tc.servOut))
writer.Flush()
GetRequestsAmount += 1
}
}))
defer ts.Close()
ptr := kraaler.NewPhishTankProviderWithConfig(
kraaler.PhishTankProviderConfig{
Endpoint: ts.URL,
},
)
defer ptr.Close()
var urls []*url.URL
loop:
for {
select {
case u := <-ptr.UrlsC():
urls = append(urls, u)
case <-time.After(300 * time.Millisecond):
break loop
}
}
if tc.expectedAmount != len(urls) {
t.Fatalf("unexpected amount %d, expected: %d", len(urls), tc.expectedAmount)
}
})
}
}