-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
312 lines (247 loc) · 6.93 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"fmt"
"os"
"testing"
"github.com/fatih/structs"
"gx/ipfs/QmQa2wf1sLFKkjHCVEbna8y5qhdMjL8vtTJSAc48vZGTer/go-ipfs/core/coreunix"
"gx/ipfs/QmQa2wf1sLFKkjHCVEbna8y5qhdMjL8vtTJSAc48vZGTer/go-ipfs/repo/config"
)
var (
testNode1 *Node
testUser1 *User
testNode2 *Node
testUser2 *User
)
func init() {
fmt.Println("------------------------------------------------------------")
fmt.Println("Initialize tests")
fmt.Println("------------------------------------------------------------")
// Init paths
basePath := ExecutionPath + "/data/"
testNode1Path := basePath + "TestNode1/"
testNode2Path := basePath + "TestNode2/"
// Remove testNodes they exists
err := RemoveContents(testNode1Path)
if err != nil {
Warning.Println(err)
}
err = RemoveContents(testNode2Path)
if err != nil {
Warning.Println(err)
}
err = os.MkdirAll(basePath, 0755)
if err != nil {
Warning.Println(err)
}
// Initialize Curation module
MyCurator.Init()
// Create testNode1
err = NewNodeRepo(testNode1Path, nil)
if err != nil {
panic(err)
}
testNode1, err = NewNode(testNode1Path)
if err != nil {
panic(err)
}
// This ensures that internals like GetIPFSObj uses the test
// node to retrieve objects
MyNode = testNode1
testUser1, err = NewUser("tester1")
if err != nil {
panic(err)
}
// Create testNode2
// Need to change Addresses in order to avoid clashes
addr := &config.Addresses{
Swarm: []string{
"/ip4/0.0.0.0/tcp/4003",
"/ip6/::/tcp/4003",
},
API: "/ip4/127.0.0.1/tcp/5003",
Gateway: "/ip4/127.0.0.1/tcp/8082",
}
err = NewNodeRepo(testNode2Path, addr)
if err != nil {
panic(err)
}
testNode2, err = NewNode(testNode2Path)
if err != nil {
panic(err)
}
testUser2, err = NewUser("tester2")
if err != nil {
panic(err)
}
// Start PeerAPIs
StartPeerAPI(testNode2)
// Might need to give some time for peerAPI info to propagate
// through IPFS network
// fmt.Println("Wait 5 sec to seed node information to network...")
// time.Sleep(5 * time.Second)
fmt.Println("\n------------------------------------------------------------")
fmt.Println("Start tests")
fmt.Println("------------------------------------------------------------")
}
func TestBlacklistThroughCuration(t *testing.T) {
// Disable optimistically unchoke, otherwise test will fail
DisableOptimisticallyUnchoke = true
// Prepare fake post and fake peer
peerID := "RANDOMPEERID"
postID := "RANDOMPOSTID"
err := testNode1.AddPeer(peerID)
if err != nil {
t.Error("Should be able to add new Peer")
}
err = testNode1.AddHostingNode(postID, peerID)
if err != nil {
t.Error("Should be able to add Hosting Node")
}
// Remove from blacklist just in case it's in there already
err = testNode1.RemoveBlacklist(peerID)
if err != nil {
fmt.Println(err)
}
// Report the same spam 20 times (should only report it as one)
for i := 0; i < 30; i++ {
testNode1.onSpam(peerID, postID)
}
isBlacklist, err := testNode1.IsBlacklisted(peerID)
if err != nil {
t.Error("Should be able to check Blacklist")
}
if isBlacklist {
t.Error("Peer should not be blacklisted, because only one posts has been reported ( but repeatedly)")
}
// Add unique spam elements, which should be still under the
// blacklist threshold
for i := 0; i < 3; i++ {
testNode1.onSpam(peerID, string(i))
}
isBlacklist, err = testNode1.IsBlacklisted(peerID)
if err != nil {
t.Error("Should be able to check Blacklist")
}
if isBlacklist {
t.Error("Peer should not be blacklisted, because only one posts has been reported ( but repeatedly)")
}
// Add more, now it should be blacklisted
for i := 0; i < 55; i++ {
testNode1.onSpam(peerID, string(i+10))
}
isBlacklist, err = testNode1.IsBlacklisted(peerID)
if err != nil {
t.Error("Should be able to check Blacklist")
}
if !isBlacklist {
t.Error("Peer should be blacklisted by now")
}
// Check if peer was removed by knownHosts
peers, err := testNode1.GetPeers()
if err != nil {
t.Error("GetPeers should not error")
}
if StringInSlice(peerID, peers) {
t.Error("Should be removed")
}
// Check if peer connection gets rejected
_, err = Client{testNode1}.CheckHealth(peerID)
if err != ErrSkipBlacklisted {
t.Error("Should skip this request, since node is blacklisted")
}
}
func TestPostCommentCreationAndRetrival(t *testing.T) {
fmt.Println("\n=== Try NewPost and GetPost")
fmt.Println("Create new Post")
postContent := "PostContent"
postTitle := "PostTitle"
obj, err := testNode2.NewPost(testUser1, postTitle, postContent)
if err != nil {
panic(err)
}
fmt.Println("Retrieve Post", obj.Hash)
post, err := testNode2.GetPost(obj.Hash)
if err != nil {
panic(err)
}
fmt.Println("")
PrettyPrint(post)
if post.Content != postContent || post.Title != postTitle {
t.Errorf(`Expected posted post and retrieved post to be the same`)
}
fmt.Println("\n=== Try NewComment and GetComment")
fmt.Println("Create new Comment")
commentContent := "CommentContent"
obj, err = testNode2.NewComment(testUser1, post.Hash, post.Hash, commentContent)
if err != nil {
panic(err)
}
fmt.Println("Retrieve Comment", obj.Hash)
comment, err := testNode2.GetComment(obj.Hash)
if err != nil {
panic(err)
}
fmt.Println("")
PrettyPrint(comment)
if comment.Content != commentContent {
t.Errorf(`Expected posted comment and retrieved comment to be the same`)
}
if comment.Parent != post.Hash || comment.Post != post.Hash {
t.Errorf(`Expected posted comment parent and post to be %s`, post.Hash)
}
fmt.Println("\n=== Try /comments")
comments, err := Client{testNode1}.GetComments(testNode2.ID, post.Hash)
if err != nil {
panic(err)
}
if !StringInSlice(comment.Hash, comments) {
t.Errorf(`Expected to retrieve comment %s via /comments`, comment.Hash)
}
fmt.Println("\n/comments resp: ", comments)
}
func TestSignatureVerification(t *testing.T) {
fmt.Println("\n=== Try Post rigged content")
var err error
data := Post{
Alias: testUser1.Alias,
Title: "TrueTitle",
Content: "TrueContent",
Timestamp: Now(),
}
// Inserted tempered data
obj := &IPFSObj{Key: testUser1.PubKeyRaw}
obj.Data = structs.New(data).Map()
obj.Signature, err = Sign(testUser1, obj.Data)
if err != nil {
panic(err)
}
// Temper with data
obj.Data["Content"] = "riggedContent"
// Add to IPFS Node Repository
hash, err := coreunix.Add(testNode2.IpfsNode, ToJSONReader(obj))
if err != nil {
panic(err)
}
obj, err = GetIPFSObj(hash)
if err != RiggedError {
t.Errorf("Expected to detect tampered data and throw RiggedError")
}
}
func TestGetPostsAPI(t *testing.T) {
fmt.Println("\n=== Try pullPost")
testNode2.pullPostFrom(testNode1.ID)
params := make(map[string]interface{})
fmt.Println("Curation suggested comments:")
fmt.Println(MyCurator.GetContent(params))
}
func TestHealthAPI(t *testing.T) {
fmt.Println("\n=== Try /health")
isHealthy, err := Client{testNode1}.CheckHealth(testNode2.ID)
if err != nil {
panic(err)
}
if !isHealthy {
t.Errorf(`Expected Health Status to be true`)
}
}