-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
71 lines (52 loc) · 1.8 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
package main
import (
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetPublicIP(t *testing.T) {
const (
ip4Address string = "192.168.1.1"
ip6Address string = "2001:0db8:85a3:0000:0000:8a2e:0370:7334" // Sample IPv6 address found on Wikipedia
)
// Create a new Serv Mux so that we can attach multiple routes to the httptest server
mux := http.NewServeMux()
mux.HandleFunc("/ipv4", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, ip4Address)
})
mux.HandleFunc("/ipv6", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, ip6Address)
})
ts := httptest.NewServer(mux)
defer ts.Close()
// Use stub server to test function
ans1 := getPublicIP(ts.URL + "/ipv4")
if ans1 != ip4Address {
t.Errorf("Did not receive expected IPv4")
}
// Use stub server to test function
ans2 := getPublicIP(ts.URL + "/ipv6")
if ans2 != ip6Address {
t.Errorf("Did not receive expected IPv6")
}
ts.Close()
}
func TestCheckIPsMatch(t *testing.T) {
publicIPv4, domainIPv4 := "192.168.1.1", "192.168.1.1"
publicIPv6, domainIPv6 := "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
resultIPv4 := checkIPsMatch(publicIPv4, domainIPv4)
resultIPv6 := checkIPsMatch(publicIPv6, domainIPv6)
if resultIPv4 != true && resultIPv6 != true {
t.Errorf("Failed to detect that public and domain IPs match")
}
}
func TestCheckIPsMatchFalse(t *testing.T) {
publicIPv4, domainIPv4 := "192.168.1.1", "192.168.10.10"
publicIPv6, domainIPv6 := "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "0000:0db8:85a3:0000:0000:8a2e:0370:0000"
resultIPv4 := checkIPsMatch(publicIPv4, domainIPv4)
resultIPv6 := checkIPsMatch(publicIPv6, domainIPv6)
if resultIPv4 != false && resultIPv6 != false {
t.Errorf("Failed to detect that public and domain IPs don't match")
}
}