aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/swarm/global-store/global_store_test.go
blob: 85f361ed4fdcc969ac19cad442b03dd8ee476797 (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
// Copyright 2019 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "context"
    "io/ioutil"
    "net"
    "net/http"
    "os"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/rpc"
    mockRPC "github.com/ethereum/go-ethereum/swarm/storage/mock/rpc"
)

// TestHTTP_InMemory tests in-memory global store that exposes
// HTTP server.
func TestHTTP_InMemory(t *testing.T) {
    testHTTP(t, true)
}

// TestHTTP_Database tests global store with persisted database
// that exposes HTTP server.
func TestHTTP_Database(t *testing.T) {
    dir, err := ioutil.TempDir("", "swarm-global-store-")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)

    // create a fresh global store
    testHTTP(t, true, "--dir", dir)

    // check if data saved by the previous global store instance
    testHTTP(t, false, "--dir", dir)
}

// testWebsocket starts global store binary with HTTP server
// and validates that it can store and retrieve data.
// If put is false, no data will be stored, only retrieved,
// giving the possibility to check if data is present in the
// storage directory.
func testHTTP(t *testing.T, put bool, args ...string) {
    addr := findFreeTCPAddress(t)
    testCmd := runGlobalStore(t, append([]string{"http", "--addr", addr}, args...)...)
    defer testCmd.Interrupt()

    client, err := rpc.DialHTTP("http://" + addr)
    if err != nil {
        t.Fatal(err)
    }

    // wait until global store process is started as
    // rpc.DialHTTP is actually not connecting
    for i := 0; i < 1000; i++ {
        _, err = http.DefaultClient.Get("http://" + addr)
        if err == nil {
            break
        }
        time.Sleep(10 * time.Millisecond)
    }
    if err != nil {
        t.Fatal(err)
    }

    store := mockRPC.NewGlobalStore(client)
    defer store.Close()

    node := store.NewNodeStore(common.HexToAddress("123abc"))

    wantKey := "key"
    wantValue := "value"

    if put {
        err = node.Put([]byte(wantKey), []byte(wantValue))
        if err != nil {
            t.Fatal(err)
        }
    }

    gotValue, err := node.Get([]byte(wantKey))
    if err != nil {
        t.Fatal(err)
    }

    if string(gotValue) != wantValue {
        t.Errorf("got value %s for key %s, want %s", string(gotValue), wantKey, wantValue)
    }
}

// TestWebsocket_InMemory tests in-memory global store that exposes
// WebSocket server.
func TestWebsocket_InMemory(t *testing.T) {
    testWebsocket(t, true)
}

// TestWebsocket_Database tests global store with persisted database
// that exposes HTTP server.
func TestWebsocket_Database(t *testing.T) {
    dir, err := ioutil.TempDir("", "swarm-global-store-")
    if err != nil {
        t.Fatal(err)
    }
    defer os.RemoveAll(dir)

    // create a fresh global store
    testWebsocket(t, true, "--dir", dir)

    // check if data saved by the previous global store instance
    testWebsocket(t, false, "--dir", dir)
}

// testWebsocket starts global store binary with WebSocket server
// and validates that it can store and retrieve data.
// If put is false, no data will be stored, only retrieved,
// giving the possibility to check if data is present in the
// storage directory.
func testWebsocket(t *testing.T, put bool, args ...string) {
    addr := findFreeTCPAddress(t)
    testCmd := runGlobalStore(t, append([]string{"ws", "--addr", addr}, args...)...)
    defer testCmd.Interrupt()

    var client *rpc.Client
    var err error
    // wait until global store process is started
    for i := 0; i < 1000; i++ {
        client, err = rpc.DialWebsocket(context.Background(), "ws://"+addr, "")
        if err == nil {
            break
        }
        time.Sleep(10 * time.Millisecond)
    }
    if err != nil {
        t.Fatal(err)
    }

    store := mockRPC.NewGlobalStore(client)
    defer store.Close()

    node := store.NewNodeStore(common.HexToAddress("123abc"))

    wantKey := "key"
    wantValue := "value"

    if put {
        err = node.Put([]byte(wantKey), []byte(wantValue))
        if err != nil {
            t.Fatal(err)
        }
    }

    gotValue, err := node.Get([]byte(wantKey))
    if err != nil {
        t.Fatal(err)
    }

    if string(gotValue) != wantValue {
        t.Errorf("got value %s for key %s, want %s", string(gotValue), wantKey, wantValue)
    }
}

// findFreeTCPAddress returns a local address (IP:Port) to which
// global store can listen on.
func findFreeTCPAddress(t *testing.T) (addr string) {
    t.Helper()

    listener, err := net.Listen("tcp", "")
    if err != nil {
        t.Fatal(err)
    }
    defer listener.Close()

    return listener.Addr().String()
}