aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/swarm_test.go
blob: a005f456a2ec26941edd71c9f0f696a8bc2c4f64 (plain) (blame)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package swarm

import (
    "context"
    "encoding/hex"
    "io/ioutil"
    "math/rand"
    "os"
    "path"
    "runtime"
    "strings"
    "testing"
    "time"

    "github.com/tangerine-network/go-tangerine/common"
    "github.com/tangerine-network/go-tangerine/crypto"
    "github.com/tangerine-network/go-tangerine/rpc"
    "github.com/tangerine-network/go-tangerine/swarm/api"
)

// TestNewSwarm validates Swarm fields in repsect to the provided configuration.
func TestNewSwarm(t *testing.T) {
    dir, err := ioutil.TempDir("", "swarm")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)

    // a simple rpc endpoint for testing dialing
    ipcEndpoint := path.Join(dir, "TestSwarm.ipc")

    // windows namedpipes are not on filesystem but on NPFS
    if runtime.GOOS == "windows" {
        b := make([]byte, 8)
        rand.Read(b)
        ipcEndpoint = `\\.\pipe\TestSwarm-` + hex.EncodeToString(b)
    }

    _, server, err := rpc.StartIPCEndpoint(ipcEndpoint, nil)
    if err != nil {
        t.Error(err)
    }
    defer server.Stop()

    for _, tc := range []struct {
        name      string
        configure func(*api.Config)
        check     func(*testing.T, *Swarm, *api.Config)
    }{
        {
            name:      "defaults",
            configure: nil,
            check: func(t *testing.T, s *Swarm, config *api.Config) {
                if s.config != config {
                    t.Error("config is not the same object")
                }
                if s.backend != nil {
                    t.Error("backend is not nil")
                }
                if s.privateKey == nil {
                    t.Error("private key is not set")
                }
                if !s.config.HiveParams.Discovery {
                    t.Error("config.HiveParams.Discovery is false, must be true regardless the configuration")
                }
                if s.dns != nil {
                    t.Error("dns initialized, but it should not be")
                }
                if s.netStore == nil {
                    t.Error("netStore not initialized")
                }
                if s.streamer == nil {
                    t.Error("streamer not initialized")
                }
                if s.fileStore == nil {
                    t.Error("fileStore not initialized")
                }
                if s.bzz == nil {
                    t.Error("bzz not initialized")
                }
                if s.ps == nil {
                    t.Error("pss not initialized")
                }
                if s.api == nil {
                    t.Error("api not initialized")
                }
                if s.sfs == nil {
                    t.Error("swarm filesystem not initialized")
                }
            },
        },
        {
            name: "with swap",
            configure: func(config *api.Config) {
                config.SwapAPI = ipcEndpoint
                config.SwapEnabled = true
            },
            check: func(t *testing.T, s *Swarm, _ *api.Config) {
                if s.backend == nil {
                    t.Error("backend is nil")
                }
            },
        },
        {
            name: "with swap disabled",
            configure: func(config *api.Config) {
                config.SwapAPI = ipcEndpoint
                config.SwapEnabled = false
            },
            check: func(t *testing.T, s *Swarm, _ *api.Config) {
                if s.backend != nil {
                    t.Error("backend is not nil")
                }
            },
        },
        {
            name: "with swap enabled and api endpoint blank",
            configure: func(config *api.Config) {
                config.SwapAPI = ""
                config.SwapEnabled = true
            },
            check: func(t *testing.T, s *Swarm, _ *api.Config) {
                if s.backend != nil {
                    t.Error("backend is not nil")
                }
            },
        },
        {
            name: "ens",
            configure: func(config *api.Config) {
                config.EnsAPIs = []string{
                    "http://127.0.0.1:8888",
                }
            },
            check: func(t *testing.T, s *Swarm, _ *api.Config) {
                if s.dns == nil {
                    t.Error("dns is not initialized")
                }
            },
        },
    } {
        t.Run(tc.name, func(t *testing.T) {
            config := api.NewConfig()

            dir, err := ioutil.TempDir("", "swarm")
            if err != nil {
                t.Fatal(err)
            }
            defer os.RemoveAll(dir)

            config.Path = dir

            privkey, err := crypto.GenerateKey()
            if err != nil {
                t.Fatal(err)
            }

            config.Init(privkey)

            if tc.configure != nil {
                tc.configure(config)
            }

            s, err := NewSwarm(config, nil)
            if err != nil {
                t.Fatal(err)
            }

            if tc.check != nil {
                tc.check(t, s, config)
            }
        })
    }
}

func TestParseEnsAPIAddress(t *testing.T) {
    for _, x := range []struct {
        description string
        value       string
        tld         string
        endpoint    string
        addr        common.Address
    }{
        {
            description: "IPC endpoint",
            value:       "/data/testnet/geth.ipc",
            endpoint:    "/data/testnet/geth.ipc",
        },
        {
            description: "HTTP endpoint",
            value:       "http://127.0.0.1:1234",
            endpoint:    "http://127.0.0.1:1234",
        },
        {
            description: "WS endpoint",
            value:       "ws://127.0.0.1:1234",
            endpoint:    "ws://127.0.0.1:1234",
        },
        {
            description: "IPC Endpoint and TLD",
            value:       "test:/data/testnet/geth.ipc",
            endpoint:    "/data/testnet/geth.ipc",
            tld:         "test",
        },
        {
            description: "HTTP endpoint and TLD",
            value:       "test:http://127.0.0.1:1234",
            endpoint:    "http://127.0.0.1:1234",
            tld:         "test",
        },
        {
            description: "WS endpoint and TLD",
            value:       "test:ws://127.0.0.1:1234",
            endpoint:    "ws://127.0.0.1:1234",
            tld:         "test",
        },
        {
            description: "IPC Endpoint and contract address",
            value:       "314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
            endpoint:    "/data/testnet/geth.ipc",
            addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
        },
        {
            description: "HTTP endpoint and contract address",
            value:       "314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
            endpoint:    "http://127.0.0.1:1234",
            addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
        },
        {
            description: "WS endpoint and contract address",
            value:       "314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234",
            endpoint:    "ws://127.0.0.1:1234",
            addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
        },
        {
            description: "IPC Endpoint, TLD and contract address",
            value:       "test:314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
            endpoint:    "/data/testnet/geth.ipc",
            addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
            tld:         "test",
        },
        {
            description: "HTTP endpoint, TLD and contract address",
            value:       "eth:314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
            endpoint:    "http://127.0.0.1:1234",
            addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
            tld:         "eth",
        },
        {
            description: "WS endpoint, TLD and contract address",
            value:       "eth:314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234",
            endpoint:    "ws://127.0.0.1:1234",
            addr:        common.HexToAddress("314159265dD8dbb310642f98f50C066173C1259b"),
            tld:         "eth",
        },
    } {
        t.Run(x.description, func(t *testing.T) {
            tld, endpoint, addr := parseEnsAPIAddress(x.value)
            if endpoint != x.endpoint {
                t.Errorf("expected Endpoint %q, got %q", x.endpoint, endpoint)
            }
            if addr != x.addr {
                t.Errorf("expected ContractAddress %q, got %q", x.addr.String(), addr.String())
            }
            if tld != x.tld {
                t.Errorf("expected TLD %q, got %q", x.tld, tld)
            }
        })
    }
}

// TestLocalStoreAndRetrieve runs multiple tests where different size files are uploaded
// to a single Swarm instance using API Store and checked against the content returned
// by API Retrieve function.
//
// This test is intended to validate functionality of chunker store and join functions
// and their intergartion into Swarm, without comparing results with ones produced by
// another chunker implementation, as it is done in swarm/storage tests.
func TestLocalStoreAndRetrieve(t *testing.T) {
    config := api.NewConfig()

    dir, err := ioutil.TempDir("", "node")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)

    config.Path = dir

    privkey, err := crypto.GenerateKey()
    if err != nil {
        t.Fatal(err)
    }

    config.Init(privkey)

    swarm, err := NewSwarm(config, nil)
    if err != nil {
        t.Fatal(err)
    }

    // by default, test only the lonely chunk cases
    sizes := []int{1, 60, 4097, 524288 + 1, 7*524288 + 1}

    if *longrunning {
        // test broader set of cases if -longruning flag is set
        sizes = append(sizes, 83, 179, 253, 1024, 4095, 4096, 8191, 8192, 8193, 12287, 12288, 12289, 123456, 2345678, 67298391, 524288, 524288+4096, 524288+4097, 7*524288, 7*524288+4096, 7*524288+4097, 128*524288+1, 128*524288, 128*524288+4096, 128*524288+4097, 816778334)
    }
    for _, n := range sizes {
        testLocalStoreAndRetrieve(t, swarm, n, true)
        testLocalStoreAndRetrieve(t, swarm, n, false)
    }
}

// testLocalStoreAndRetrieve is using a single Swarm instance, to upload
// a file of length n with optional random data using API Store function,
// and checks the output of API Retrieve function on the same instance.
// This is a regression test for issue
// https://github.com/ethersphere/go-ethereum/issues/639
// where pyramid chunker did not split correctly files with lengths that
// are edge cases for chunk and tree parameters, depending whether there
// is a tree chunk with only one data chunk and how the compress functionality
// changed the tree.
func testLocalStoreAndRetrieve(t *testing.T, swarm *Swarm, n int, randomData bool) {
    slice := make([]byte, n)
    if randomData {
        rand.Seed(time.Now().UnixNano())
        rand.Read(slice)
    }
    dataPut := string(slice)

    ctx := context.TODO()
    k, wait, err := swarm.api.Store(ctx, strings.NewReader(dataPut), int64(len(dataPut)), false)
    if err != nil {
        t.Fatal(err)
    }
    if wait != nil {
        err = wait(ctx)
        if err != nil {
            t.Fatal(err)
        }
    }

    r, _ := swarm.api.Retrieve(context.TODO(), k)

    d, err := ioutil.ReadAll(r)
    if err != nil {
        t.Fatal(err)
    }
    dataGet := string(d)

    if len(dataPut) != len(dataGet) {
        t.Fatalf("data not matched: length expected %v, got %v", len(dataPut), len(dataGet))
    } else {
        if dataPut != dataGet {
            t.Fatal("data not matched")
        }
    }
}