aboutsummaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
authorAlexey Sharov <www.pismeco@gmail.com>2018-11-14 16:21:14 +0800
committerViktor TrĂ³n <viktor.tron@gmail.com>2018-11-14 16:21:14 +0800
commiteb8fa3cc89ae3a3247c649486839b1c250554d2d (patch)
treec61b8ebebc739db0e5413cd214fdd0d3da2fe61e /cmd
parentcff97119a713a6f790893aaf1c172a397a48bf33 (diff)
downloaddexon-eb8fa3cc89ae3a3247c649486839b1c250554d2d.tar
dexon-eb8fa3cc89ae3a3247c649486839b1c250554d2d.tar.gz
dexon-eb8fa3cc89ae3a3247c649486839b1c250554d2d.tar.bz2
dexon-eb8fa3cc89ae3a3247c649486839b1c250554d2d.tar.lz
dexon-eb8fa3cc89ae3a3247c649486839b1c250554d2d.tar.xz
dexon-eb8fa3cc89ae3a3247c649486839b1c250554d2d.tar.zst
dexon-eb8fa3cc89ae3a3247c649486839b1c250554d2d.zip
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 https://github.com/ethereum/go-ethereum/pull/17807 and comments to swarm/network_test.go.Shuffle)
Diffstat (limited to 'cmd')
-rw-r--r--cmd/swarm/access_test.go3
-rw-r--r--cmd/swarm/export_test.go36
-rw-r--r--cmd/swarm/feeds_test.go37
-rw-r--r--cmd/swarm/manifest_test.go6
-rw-r--r--cmd/swarm/run_test.go3
-rw-r--r--cmd/swarm/upload_test.go23
6 files changed, 29 insertions, 79 deletions
diff --git a/cmd/swarm/access_test.go b/cmd/swarm/access_test.go
index b4d2e1dbc..e812cd8fd 100644
--- a/cmd/swarm/access_test.go
+++ b/cmd/swarm/access_test.go
@@ -38,6 +38,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/swarm/api"
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
+ swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
"github.com/ethereum/go-ethereum/swarm/testutil"
)
@@ -54,7 +55,7 @@ var DefaultCurve = crypto.S256()
// is then fetched through 2nd node. since the tested code is not key-aware - we can just
// fetch from the 2nd node using HTTP BasicAuth
func TestAccessPassword(t *testing.T) {
- srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
+ srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()
dataFilename := testutil.TempFileWithContent(t, data)
diff --git a/cmd/swarm/export_test.go b/cmd/swarm/export_test.go
index c533511af..f1bc2f265 100644
--- a/cmd/swarm/export_test.go
+++ b/cmd/swarm/export_test.go
@@ -19,9 +19,7 @@ package main
import (
"bytes"
"crypto/md5"
- "crypto/rand"
"io"
- "io/ioutil"
"net/http"
"os"
"runtime"
@@ -29,6 +27,7 @@ import (
"testing"
"github.com/ethereum/go-ethereum/swarm"
+ "github.com/ethereum/go-ethereum/swarm/testutil"
)
// TestCLISwarmExportImport perform the following test:
@@ -45,11 +44,12 @@ func TestCLISwarmExportImport(t *testing.T) {
cluster := newTestCluster(t, 1)
// generate random 10mb file
- f, cleanup := generateRandomFile(t, 10000000)
- defer cleanup()
+ content := testutil.RandomBytes(1, 10000000)
+ fileName := testutil.TempFileWithContent(t, string(content))
+ defer os.Remove(fileName)
// upload the file with 'swarm up' and expect a hash
- up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", f.Name())
+ up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", fileName)
_, matches := up.ExpectRegexp(`[a-f\d]{64}`)
up.ExpectExit()
hash := matches[0]
@@ -96,7 +96,7 @@ func TestCLISwarmExportImport(t *testing.T) {
}
// compare downloaded file with the generated random file
- mustEqualFiles(t, f, res.Body)
+ mustEqualFiles(t, bytes.NewReader(content), res.Body)
}
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) {
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)
}
}
-
-func generateRandomFile(t *testing.T, size int) (f *os.File, teardown func()) {
- // create a tmp file
- tmp, err := ioutil.TempFile("", "swarm-test")
- if err != nil {
- t.Fatal(err)
- }
-
- // callback for tmp file cleanup
- teardown = func() {
- tmp.Close()
- os.Remove(tmp.Name())
- }
-
- // write 10mb random data to file
- buf := make([]byte, 10000000)
- _, err = rand.Read(buf)
- if err != nil {
- t.Fatal(err)
- }
- ioutil.WriteFile(tmp.Name(), buf, 0755)
-
- return tmp, teardown
-}
diff --git a/cmd/swarm/feeds_test.go b/cmd/swarm/feeds_test.go
index 46727c21d..fc3f72ab1 100644
--- a/cmd/swarm/feeds_test.go
+++ b/cmd/swarm/feeds_test.go
@@ -20,49 +20,37 @@ import (
"bytes"
"encoding/json"
"fmt"
- "io"
"io/ioutil"
"os"
"testing"
- "github.com/ethereum/go-ethereum/swarm/api"
- "github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
- "github.com/ethereum/go-ethereum/swarm/testutil"
-
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/swarm/storage/feed"
-
"github.com/ethereum/go-ethereum/common/hexutil"
+ "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/swarm/api"
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
+ "github.com/ethereum/go-ethereum/swarm/storage/feed"
+ "github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
+ "github.com/ethereum/go-ethereum/swarm/testutil"
)
func TestCLIFeedUpdate(t *testing.T) {
- srv := testutil.NewTestSwarmServer(t, func(api *api.API) testutil.TestServer {
+ srv := swarmhttp.NewTestSwarmServer(t, func(api *api.API) swarmhttp.TestServer {
return swarmhttp.NewServer(api, "")
}, nil)
log.Info("starting a test swarm server")
defer srv.Close()
// create a private key file for signing
- pkfile, err := ioutil.TempFile("", "swarm-test")
- if err != nil {
- t.Fatal(err)
- }
- defer pkfile.Close()
- defer os.Remove(pkfile.Name())
privkeyHex := "0000000000000000000000000000000000000000000000000000000000001979"
privKey, _ := crypto.HexToECDSA(privkeyHex)
address := crypto.PubkeyToAddress(privKey.PublicKey)
- // save the private key to a file
- _, err = io.WriteString(pkfile, privkeyHex)
- if err != nil {
- t.Fatal(err)
- }
+ pkFileName := testutil.TempFileWithContent(t, privkeyHex)
+ defer os.Remove(pkFileName)
// compose a topic. We'll be doing quotes about Miguel de Cervantes
var topic feed.Topic
@@ -76,7 +64,7 @@ func TestCLIFeedUpdate(t *testing.T) {
flags := []string{
"--bzzapi", srv.URL,
- "--bzzaccount", pkfile.Name(),
+ "--bzzaccount", pkFileName,
"feed", "update",
"--topic", topic.Hex(),
"--name", name,
@@ -89,13 +77,10 @@ func TestCLIFeedUpdate(t *testing.T) {
// now try to get the update using the client
client := swarm.NewClient(srv.URL)
- if err != nil {
- t.Fatal(err)
- }
// build the same topic as before, this time
// we use NewTopic to create a topic automatically.
- topic, err = feed.NewTopic(name, subject)
+ topic, err := feed.NewTopic(name, subject)
if err != nil {
t.Fatal(err)
}
@@ -153,7 +138,7 @@ func TestCLIFeedUpdate(t *testing.T) {
// test publishing a manifest
flags = []string{
"--bzzapi", srv.URL,
- "--bzzaccount", pkfile.Name(),
+ "--bzzaccount", pkFileName,
"feed", "create",
"--topic", topic.Hex(),
}
diff --git a/cmd/swarm/manifest_test.go b/cmd/swarm/manifest_test.go
index 7ea4e0c45..01d982aa7 100644
--- a/cmd/swarm/manifest_test.go
+++ b/cmd/swarm/manifest_test.go
@@ -26,7 +26,7 @@ import (
"github.com/ethereum/go-ethereum/swarm/api"
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
- "github.com/ethereum/go-ethereum/swarm/testutil"
+ swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
)
// TestManifestChange tests manifest add, update and remove
@@ -58,7 +58,7 @@ func TestManifestChangeEncrypted(t *testing.T) {
// Argument encrypt controls whether to use encryption or not.
func testManifestChange(t *testing.T, encrypt bool) {
t.Parallel()
- srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
+ srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()
tmp, err := ioutil.TempDir("", "swarm-manifest-test")
@@ -430,7 +430,7 @@ func TestNestedDefaultEntryUpdateEncrypted(t *testing.T) {
func testNestedDefaultEntryUpdate(t *testing.T, encrypt bool) {
t.Parallel()
- srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
+ srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()
tmp, err := ioutil.TempDir("", "swarm-manifest-test")
diff --git a/cmd/swarm/run_test.go b/cmd/swarm/run_test.go
index 55199e955..416fa7a50 100644
--- a/cmd/swarm/run_test.go
+++ b/cmd/swarm/run_test.go
@@ -42,7 +42,6 @@ import (
"github.com/ethereum/go-ethereum/swarm"
"github.com/ethereum/go-ethereum/swarm/api"
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
- "github.com/ethereum/go-ethereum/swarm/testutil"
)
var loglevel = flag.Int("loglevel", 3, "verbosity of logs")
@@ -58,7 +57,7 @@ func init() {
})
}
-func serverFunc(api *api.API) testutil.TestServer {
+func serverFunc(api *api.API) swarmhttp.TestServer {
return swarmhttp.NewServer(api, "")
}
func TestMain(m *testing.M) {
diff --git a/cmd/swarm/upload_test.go b/cmd/swarm/upload_test.go
index ba4463e8b..5f9844950 100644
--- a/cmd/swarm/upload_test.go
+++ b/cmd/swarm/upload_test.go
@@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/log"
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
+ swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
"github.com/ethereum/go-ethereum/swarm/testutil"
"github.com/mattn/go-colorable"
)
@@ -77,33 +78,22 @@ func testCLISwarmUp(toEncrypt bool, t *testing.T) {
cluster := newTestCluster(t, 3)
defer cluster.Shutdown()
- // create a tmp file
- tmp, err := ioutil.TempFile("", "swarm-test")
- if err != nil {
- t.Fatal(err)
- }
- defer tmp.Close()
- defer os.Remove(tmp.Name())
+ tmpFileName := testutil.TempFileWithContent(t, data)
+ defer os.Remove(tmpFileName)
// write data to file
- data := "notsorandomdata"
- _, err = io.WriteString(tmp, data)
- if err != nil {
- t.Fatal(err)
- }
-
hashRegexp := `[a-f\d]{64}`
flags := []string{
"--bzzapi", cluster.Nodes[0].URL,
"up",
- tmp.Name()}
+ tmpFileName}
if toEncrypt {
hashRegexp = `[a-f\d]{128}`
flags = []string{
"--bzzapi", cluster.Nodes[0].URL,
"up",
"--encrypt",
- tmp.Name()}
+ tmpFileName}
}
// upload the file with 'swarm up' and expect a hash
log.Info(fmt.Sprintf("uploading file with 'swarm up'"))
@@ -203,7 +193,6 @@ func testCLISwarmUpRecursive(toEncrypt bool, t *testing.T) {
}
defer os.RemoveAll(tmpUploadDir)
// create tmp files
- data := "notsorandomdata"
for _, path := range []string{"tmp1", "tmp2"} {
if err := ioutil.WriteFile(filepath.Join(tmpUploadDir, path), bytes.NewBufferString(data).Bytes(), 0644); err != nil {
t.Fatal(err)
@@ -298,7 +287,7 @@ func TestCLISwarmUpDefaultPath(t *testing.T) {
}
func testCLISwarmUpDefaultPath(toEncrypt bool, absDefaultPath bool, t *testing.T) {
- srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
+ srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()
tmp, err := ioutil.TempDir("", "swarm-defaultpath-test")