-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSyncServer_test.go
More file actions
141 lines (126 loc) · 3.51 KB
/
SyncServer_test.go
File metadata and controls
141 lines (126 loc) · 3.51 KB
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
package main
import (
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"testing"
)
func TestSyncWithServerPrepareJSON(t *testing.T) {
oldConfig := *config
oldCurrentTimestamp := currentTimestamp
defer func() {
tmpConf := oldConfig
config = &tmpConf
currentTimestamp = oldCurrentTimestamp
}()
tmpConf := oldConfig
config = &tmpConf
config.SyncServerToken = "token-a"
currentTimestamp = 123456
torrentMapInput := map[string]TorrentInfoStruct{
"hash-a": {
Size: 100,
Peers: map[string]PeerInfoStruct{
"1.2.3.4": {Uploaded: 2048},
},
},
}
ok, payload := SyncWithServer_PrepareJSON(torrentMapInput)
if !ok {
t.Fatal("SyncWithServer_PrepareJSON should succeed")
}
var body SyncServer_SubmitStruct
if err := json.Unmarshal([]byte(payload), &body); err != nil {
t.Fatalf("failed to decode payload: %v", err)
}
if body.Version != 1 {
t.Fatalf("Version=%d, want 1", body.Version)
}
if body.Timestamp != 123456 {
t.Fatalf("Timestamp=%d, want 123456", body.Timestamp)
}
if body.Token != "token-a" {
t.Fatalf("Token=%q, want token-a", body.Token)
}
if _, ok := body.TorrentMap["hash-a"]; !ok {
t.Fatal("torrent payload should include hash-a")
}
}
func TestSyncWithServerSubmitCompilesCIDRs(t *testing.T) {
oldClientExternal := httpClientExternal
oldConfig := *config
oldSyncConfig := syncServer_syncConfig
oldRules := syncServer_CompiledRules
defer func() {
httpClientExternal = oldClientExternal
tmpConf := oldConfig
config = &tmpConf
syncServer_syncConfig = oldSyncConfig
syncServer_CompiledRules = oldRules
}()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
"interval": 90,
"status": "",
"blockIPRule": {
"bad-peer": ["1.2.3.4", "2001:db8::/64", "bad-cidr"]
}
}`))
}))
defer server.Close()
httpClientExternal = *server.Client()
tmpConf := oldConfig
config = &tmpConf
config.SyncServerURL = server.URL
syncServer_syncConfig = &SyncServer_ConfigStruct{
Interval: 60,
Status: "",
BlockIPRule: map[string][]string{},
}
syncServer_CompiledRules = []SyncServer_RuleStruct{}
if !SyncWithServer_Submit(`{"dummy":true}`) {
t.Fatal("SyncWithServer_Submit should succeed")
}
if syncServer_syncConfig.Interval != 90 {
t.Fatalf("Interval=%d, want 90", syncServer_syncConfig.Interval)
}
if len(syncServer_CompiledRules) != 2 {
t.Fatalf("compiled Rule count=%d, want 2", len(syncServer_CompiledRules))
}
foundIPv4 := false
foundIPv6 := false
for _, rule := range syncServer_CompiledRules {
if rule.Net.String() == "1.2.3.4/32" {
foundIPv4 = true
}
if rule.Net.String() == "2001:db8::/64" {
foundIPv6 = true
}
}
if !foundIPv4 {
t.Fatal("compiled Rules should contain IPv4 entry")
}
if !foundIPv6 {
t.Fatal("compiled Rules should contain IPv6 entry")
}
}
func TestSyncServerCheckPeer(t *testing.T) {
oldRules := syncServer_CompiledRules
defer func() {
syncServer_CompiledRules = oldRules
}()
_, subnet, _ := net.ParseCIDR("10.0.0.0/24")
syncServer_CompiledRules = []SyncServer_RuleStruct{
{Net: subnet, Reason: "local-network"},
}
ok, reason := SyncServer_CheckPeer(net.ParseIP("10.0.0.5"))
if !ok || reason != "Bad-IP_FromSyncServer (local-network)" {
t.Fatalf("SyncServer_CheckPeer(10.0.0.5) = (%v, %q), want (true, \"Bad-IP_FromSyncServer (local-network)\")", ok, reason)
}
ok, _ = SyncServer_CheckPeer(net.ParseIP("10.0.1.5"))
if ok {
t.Fatal("SyncServer_CheckPeer(10.0.1.5) should be false")
}
}