Skip to content

Commit eb8fa3c

Browse files
nizsheanezzelig
authored andcommitted
cmd/swarm, swarm/api/http, swarm/bmt, swarm/fuse, swarm/network/stream, swarm/storage, swarm/storage/encryption, swarm/testutil: use pseudo-random instead of crypto-random for test files content generation (#18083)
- Replace "crypto/rand" to "math/rand" for files content generation - Remove swarm/network_test.go.Shuffle and swarm/btm/btm_test.go.Shuffle - because go1.9 support dropped (see #17807 and comments to swarm/network_test.go.Shuffle)
1 parent cff9711 commit eb8fa3c

24 files changed

+202
-362
lines changed

cmd/swarm/access_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/ethereum/go-ethereum/log"
3939
"github.com/ethereum/go-ethereum/swarm/api"
4040
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
41+
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
4142
"github.com/ethereum/go-ethereum/swarm/testutil"
4243
)
4344

@@ -54,7 +55,7 @@ var DefaultCurve = crypto.S256()
5455
// is then fetched through 2nd node. since the tested code is not key-aware - we can just
5556
// fetch from the 2nd node using HTTP BasicAuth
5657
func TestAccessPassword(t *testing.T) {
57-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
58+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
5859
defer srv.Close()
5960

6061
dataFilename := testutil.TempFileWithContent(t, data)

cmd/swarm/export_test.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ package main
1919
import (
2020
"bytes"
2121
"crypto/md5"
22-
"crypto/rand"
2322
"io"
24-
"io/ioutil"
2523
"net/http"
2624
"os"
2725
"runtime"
2826
"strings"
2927
"testing"
3028

3129
"github.com/ethereum/go-ethereum/swarm"
30+
"github.com/ethereum/go-ethereum/swarm/testutil"
3231
)
3332

3433
// TestCLISwarmExportImport perform the following test:
@@ -45,11 +44,12 @@ func TestCLISwarmExportImport(t *testing.T) {
4544
cluster := newTestCluster(t, 1)
4645

4746
// generate random 10mb file
48-
f, cleanup := generateRandomFile(t, 10000000)
49-
defer cleanup()
47+
content := testutil.RandomBytes(1, 10000000)
48+
fileName := testutil.TempFileWithContent(t, string(content))
49+
defer os.Remove(fileName)
5050

5151
// upload the file with 'swarm up' and expect a hash
52-
up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", f.Name())
52+
up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", fileName)
5353
_, matches := up.ExpectRegexp(`[a-f\d]{64}`)
5454
up.ExpectExit()
5555
hash := matches[0]
@@ -96,7 +96,7 @@ func TestCLISwarmExportImport(t *testing.T) {
9696
}
9797

9898
// compare downloaded file with the generated random file
99-
mustEqualFiles(t, f, res.Body)
99+
mustEqualFiles(t, bytes.NewReader(content), res.Body)
100100
}
101101

102102
func mustEqualFiles(t *testing.T, up io.Reader, down io.Reader) {
@@ -117,27 +117,3 @@ func mustEqualFiles(t *testing.T, up io.Reader, down io.Reader) {
117117
t.Fatalf("downloaded imported file md5=%x (length %v) is not the same as the generated one mp5=%x (length %v)", downHash, downLen, upHash, upLen)
118118
}
119119
}
120-
121-
func generateRandomFile(t *testing.T, size int) (f *os.File, teardown func()) {
122-
// create a tmp file
123-
tmp, err := ioutil.TempFile("", "swarm-test")
124-
if err != nil {
125-
t.Fatal(err)
126-
}
127-
128-
// callback for tmp file cleanup
129-
teardown = func() {
130-
tmp.Close()
131-
os.Remove(tmp.Name())
132-
}
133-
134-
// write 10mb random data to file
135-
buf := make([]byte, 10000000)
136-
_, err = rand.Read(buf)
137-
if err != nil {
138-
t.Fatal(err)
139-
}
140-
ioutil.WriteFile(tmp.Name(), buf, 0755)
141-
142-
return tmp, teardown
143-
}

cmd/swarm/feeds_test.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,37 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23-
"io"
2423
"io/ioutil"
2524
"os"
2625
"testing"
2726

28-
"github.com/ethereum/go-ethereum/swarm/api"
29-
"github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
30-
"github.com/ethereum/go-ethereum/swarm/testutil"
31-
32-
"github.com/ethereum/go-ethereum/crypto"
33-
"github.com/ethereum/go-ethereum/swarm/storage/feed"
34-
3527
"github.com/ethereum/go-ethereum/common/hexutil"
28+
"github.com/ethereum/go-ethereum/crypto"
3629
"github.com/ethereum/go-ethereum/log"
30+
"github.com/ethereum/go-ethereum/swarm/api"
3731
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
3832
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
33+
"github.com/ethereum/go-ethereum/swarm/storage/feed"
34+
"github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
35+
"github.com/ethereum/go-ethereum/swarm/testutil"
3936
)
4037

4138
func TestCLIFeedUpdate(t *testing.T) {
4239

43-
srv := testutil.NewTestSwarmServer(t, func(api *api.API) testutil.TestServer {
40+
srv := swarmhttp.NewTestSwarmServer(t, func(api *api.API) swarmhttp.TestServer {
4441
return swarmhttp.NewServer(api, "")
4542
}, nil)
4643
log.Info("starting a test swarm server")
4744
defer srv.Close()
4845

4946
// create a private key file for signing
50-
pkfile, err := ioutil.TempFile("", "swarm-test")
51-
if err != nil {
52-
t.Fatal(err)
53-
}
54-
defer pkfile.Close()
55-
defer os.Remove(pkfile.Name())
5647

5748
privkeyHex := "0000000000000000000000000000000000000000000000000000000000001979"
5849
privKey, _ := crypto.HexToECDSA(privkeyHex)
5950
address := crypto.PubkeyToAddress(privKey.PublicKey)
6051

61-
// save the private key to a file
62-
_, err = io.WriteString(pkfile, privkeyHex)
63-
if err != nil {
64-
t.Fatal(err)
65-
}
52+
pkFileName := testutil.TempFileWithContent(t, privkeyHex)
53+
defer os.Remove(pkFileName)
6654

6755
// compose a topic. We'll be doing quotes about Miguel de Cervantes
6856
var topic feed.Topic
@@ -76,7 +64,7 @@ func TestCLIFeedUpdate(t *testing.T) {
7664

7765
flags := []string{
7866
"--bzzapi", srv.URL,
79-
"--bzzaccount", pkfile.Name(),
67+
"--bzzaccount", pkFileName,
8068
"feed", "update",
8169
"--topic", topic.Hex(),
8270
"--name", name,
@@ -89,13 +77,10 @@ func TestCLIFeedUpdate(t *testing.T) {
8977

9078
// now try to get the update using the client
9179
client := swarm.NewClient(srv.URL)
92-
if err != nil {
93-
t.Fatal(err)
94-
}
9580

9681
// build the same topic as before, this time
9782
// we use NewTopic to create a topic automatically.
98-
topic, err = feed.NewTopic(name, subject)
83+
topic, err := feed.NewTopic(name, subject)
9984
if err != nil {
10085
t.Fatal(err)
10186
}
@@ -153,7 +138,7 @@ func TestCLIFeedUpdate(t *testing.T) {
153138
// test publishing a manifest
154139
flags = []string{
155140
"--bzzapi", srv.URL,
156-
"--bzzaccount", pkfile.Name(),
141+
"--bzzaccount", pkFileName,
157142
"feed", "create",
158143
"--topic", topic.Hex(),
159144
}

cmd/swarm/manifest_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
"github.com/ethereum/go-ethereum/swarm/api"
2828
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
29-
"github.com/ethereum/go-ethereum/swarm/testutil"
29+
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
3030
)
3131

3232
// TestManifestChange tests manifest add, update and remove
@@ -58,7 +58,7 @@ func TestManifestChangeEncrypted(t *testing.T) {
5858
// Argument encrypt controls whether to use encryption or not.
5959
func testManifestChange(t *testing.T, encrypt bool) {
6060
t.Parallel()
61-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
61+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
6262
defer srv.Close()
6363

6464
tmp, err := ioutil.TempDir("", "swarm-manifest-test")
@@ -430,7 +430,7 @@ func TestNestedDefaultEntryUpdateEncrypted(t *testing.T) {
430430

431431
func testNestedDefaultEntryUpdate(t *testing.T, encrypt bool) {
432432
t.Parallel()
433-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
433+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
434434
defer srv.Close()
435435

436436
tmp, err := ioutil.TempDir("", "swarm-manifest-test")

cmd/swarm/run_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
"github.com/ethereum/go-ethereum/swarm"
4343
"github.com/ethereum/go-ethereum/swarm/api"
4444
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
45-
"github.com/ethereum/go-ethereum/swarm/testutil"
4645
)
4746

4847
var loglevel = flag.Int("loglevel", 3, "verbosity of logs")
@@ -58,7 +57,7 @@ func init() {
5857
})
5958
}
6059

61-
func serverFunc(api *api.API) testutil.TestServer {
60+
func serverFunc(api *api.API) swarmhttp.TestServer {
6261
return swarmhttp.NewServer(api, "")
6362
}
6463
func TestMain(m *testing.M) {

cmd/swarm/upload_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/ethereum/go-ethereum/log"
3434
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
35+
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
3536
"github.com/ethereum/go-ethereum/swarm/testutil"
3637
"github.com/mattn/go-colorable"
3738
)
@@ -77,33 +78,22 @@ func testCLISwarmUp(toEncrypt bool, t *testing.T) {
7778
cluster := newTestCluster(t, 3)
7879
defer cluster.Shutdown()
7980

80-
// create a tmp file
81-
tmp, err := ioutil.TempFile("", "swarm-test")
82-
if err != nil {
83-
t.Fatal(err)
84-
}
85-
defer tmp.Close()
86-
defer os.Remove(tmp.Name())
81+
tmpFileName := testutil.TempFileWithContent(t, data)
82+
defer os.Remove(tmpFileName)
8783

8884
// write data to file
89-
data := "notsorandomdata"
90-
_, err = io.WriteString(tmp, data)
91-
if err != nil {
92-
t.Fatal(err)
93-
}
94-
9585
hashRegexp := `[a-f\d]{64}`
9686
flags := []string{
9787
"--bzzapi", cluster.Nodes[0].URL,
9888
"up",
99-
tmp.Name()}
89+
tmpFileName}
10090
if toEncrypt {
10191
hashRegexp = `[a-f\d]{128}`
10292
flags = []string{
10393
"--bzzapi", cluster.Nodes[0].URL,
10494
"up",
10595
"--encrypt",
106-
tmp.Name()}
96+
tmpFileName}
10797
}
10898
// upload the file with 'swarm up' and expect a hash
10999
log.Info(fmt.Sprintf("uploading file with 'swarm up'"))
@@ -203,7 +193,6 @@ func testCLISwarmUpRecursive(toEncrypt bool, t *testing.T) {
203193
}
204194
defer os.RemoveAll(tmpUploadDir)
205195
// create tmp files
206-
data := "notsorandomdata"
207196
for _, path := range []string{"tmp1", "tmp2"} {
208197
if err := ioutil.WriteFile(filepath.Join(tmpUploadDir, path), bytes.NewBufferString(data).Bytes(), 0644); err != nil {
209198
t.Fatal(err)
@@ -298,7 +287,7 @@ func TestCLISwarmUpDefaultPath(t *testing.T) {
298287
}
299288

300289
func testCLISwarmUpDefaultPath(toEncrypt bool, absDefaultPath bool, t *testing.T) {
301-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
290+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
302291
defer srv.Close()
303292

304293
tmp, err := ioutil.TempDir("", "swarm-defaultpath-test")

swarm/api/client/client_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ import (
3333
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
3434
"github.com/ethereum/go-ethereum/swarm/multihash"
3535
"github.com/ethereum/go-ethereum/swarm/storage/feed"
36-
"github.com/ethereum/go-ethereum/swarm/testutil"
3736
)
3837

39-
func serverFunc(api *api.API) testutil.TestServer {
38+
func serverFunc(api *api.API) swarmhttp.TestServer {
4039
return swarmhttp.NewServer(api, "")
4140
}
4241

@@ -49,7 +48,7 @@ func TestClientUploadDownloadRawEncrypted(t *testing.T) {
4948
}
5049

5150
func testClientUploadDownloadRaw(toEncrypt bool, t *testing.T) {
52-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
51+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
5352
defer srv.Close()
5453

5554
client := NewClient(srv.URL)
@@ -90,7 +89,7 @@ func TestClientUploadDownloadFilesEncrypted(t *testing.T) {
9089
}
9190

9291
func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) {
93-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
92+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
9493
defer srv.Close()
9594

9695
client := NewClient(srv.URL)
@@ -188,7 +187,7 @@ func newTestDirectory(t *testing.T) string {
188187
// TestClientUploadDownloadDirectory tests uploading and downloading a
189188
// directory of files to a swarm manifest
190189
func TestClientUploadDownloadDirectory(t *testing.T) {
191-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
190+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
192191
defer srv.Close()
193192

194193
dir := newTestDirectory(t)
@@ -254,7 +253,7 @@ func TestClientFileListEncrypted(t *testing.T) {
254253
}
255254

256255
func testClientFileList(toEncrypt bool, t *testing.T) {
257-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
256+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
258257
defer srv.Close()
259258

260259
dir := newTestDirectory(t)
@@ -312,7 +311,7 @@ func testClientFileList(toEncrypt bool, t *testing.T) {
312311
// TestClientMultipartUpload tests uploading files to swarm using a multipart
313312
// upload
314313
func TestClientMultipartUpload(t *testing.T) {
315-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
314+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
316315
defer srv.Close()
317316

318317
// define an uploader which uploads testDirFiles with some data
@@ -378,7 +377,7 @@ func TestClientCreateFeedMultihash(t *testing.T) {
378377

379378
signer, _ := newTestSigner()
380379

381-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
380+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
382381
client := NewClient(srv.URL)
383382
defer srv.Close()
384383

@@ -440,7 +439,7 @@ func TestClientCreateUpdateFeed(t *testing.T) {
440439

441440
signer, _ := newTestSigner()
442441

443-
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
442+
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
444443
client := NewClient(srv.URL)
445444
defer srv.Close()
446445

0 commit comments

Comments
 (0)