Skip to content

Commit

Permalink
cmd/swarm, swarm/api/http, swarm/bmt, swarm/fuse, swarm/network/strea…
Browse files Browse the repository at this point in the history
…m, swarm/storage, swarm/storage/encryption, swarm/testutil: use pseudo-random instead of crypto-random for test files content generation (ethereum#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 ethereum#17807 and comments to swarm/network_test.go.Shuffle)
  • Loading branch information
nizsheanez authored and zelig committed Nov 14, 2018
1 parent cff9711 commit eb8fa3c
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 362 deletions.
3 changes: 2 additions & 1 deletion cmd/swarm/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
36 changes: 6 additions & 30 deletions cmd/swarm/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ package main
import (
"bytes"
"crypto/md5"
"crypto/rand"
"io"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"testing"

"github.com/ethereum/go-ethereum/swarm"
"github.com/ethereum/go-ethereum/swarm/testutil"
)

// TestCLISwarmExportImport perform the following test:
Expand All @@ -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]
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
37 changes: 11 additions & 26 deletions cmd/swarm/feeds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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(),
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/swarm/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions cmd/swarm/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down
23 changes: 6 additions & 17 deletions cmd/swarm/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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'"))
Expand Down Expand 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)
Expand Down Expand Up @@ -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")
Expand Down
17 changes: 8 additions & 9 deletions swarm/api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ import (
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
"github.com/ethereum/go-ethereum/swarm/multihash"
"github.com/ethereum/go-ethereum/swarm/storage/feed"
"github.com/ethereum/go-ethereum/swarm/testutil"
)

func serverFunc(api *api.API) testutil.TestServer {
func serverFunc(api *api.API) swarmhttp.TestServer {
return swarmhttp.NewServer(api, "")
}

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

func testClientUploadDownloadRaw(toEncrypt bool, t *testing.T) {
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()

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

func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) {
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()

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

dir := newTestDirectory(t)
Expand Down Expand Up @@ -254,7 +253,7 @@ func TestClientFileListEncrypted(t *testing.T) {
}

func testClientFileList(toEncrypt bool, t *testing.T) {
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()

dir := newTestDirectory(t)
Expand Down Expand Up @@ -312,7 +311,7 @@ func testClientFileList(toEncrypt bool, t *testing.T) {
// TestClientMultipartUpload tests uploading files to swarm using a multipart
// upload
func TestClientMultipartUpload(t *testing.T) {
srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
defer srv.Close()

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

signer, _ := newTestSigner()

srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
client := NewClient(srv.URL)
defer srv.Close()

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

signer, _ := newTestSigner()

srv := testutil.NewTestSwarmServer(t, serverFunc, nil)
srv := swarmhttp.NewTestSwarmServer(t, serverFunc, nil)
client := NewClient(srv.URL)
defer srv.Close()

Expand Down
Loading

0 comments on commit eb8fa3c

Please sign in to comment.