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)
This commit is contained in:
committed by
Viktor Trón
parent
cff97119a7
commit
eb8fa3cc89
@ -19,13 +19,13 @@ package storage
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/crypto/sha3"
|
||||
"github.com/ethereum/go-ethereum/swarm/testutil"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -47,7 +47,7 @@ func newTestHasherStore(store ChunkStore, hash string) *hasherStore {
|
||||
}
|
||||
|
||||
func testRandomBrokenData(n int, tester *chunkerTester) {
|
||||
data := io.LimitReader(rand.Reader, int64(n))
|
||||
data := testutil.RandomReader(1, n)
|
||||
brokendata := brokenLimitReader(data, n, n/2)
|
||||
|
||||
buf := make([]byte, n)
|
||||
@ -56,7 +56,7 @@ func testRandomBrokenData(n int, tester *chunkerTester) {
|
||||
tester.t.Fatalf("Broken reader is not broken, hence broken. Returns: %v", err)
|
||||
}
|
||||
|
||||
data = io.LimitReader(rand.Reader, int64(n))
|
||||
data = testutil.RandomReader(2, n)
|
||||
brokendata = brokenLimitReader(data, n, n/2)
|
||||
|
||||
putGetter := newTestHasherStore(NewMapChunkStore(), SHA3Hash)
|
||||
@ -77,7 +77,8 @@ func testRandomData(usePyramid bool, hash string, n int, tester *chunkerTester)
|
||||
input, found := tester.inputs[uint64(n)]
|
||||
var data io.Reader
|
||||
if !found {
|
||||
data, input = GenerateRandomData(n)
|
||||
input = testutil.RandomBytes(1, n)
|
||||
data = bytes.NewReader(input)
|
||||
tester.inputs[uint64(n)] = input
|
||||
} else {
|
||||
data = io.LimitReader(bytes.NewReader(input), int64(n))
|
||||
@ -118,14 +119,13 @@ func testRandomData(usePyramid bool, hash string, n int, tester *chunkerTester)
|
||||
// testing partial read
|
||||
for i := 1; i < n; i += 10000 {
|
||||
readableLength := n - i
|
||||
output := make([]byte, readableLength)
|
||||
r, err := reader.ReadAt(output, int64(i))
|
||||
if r != readableLength || err != io.EOF {
|
||||
tester.t.Fatalf("readAt error with offset %v read: %v n = %v err = %v\n", i, r, readableLength, err)
|
||||
}
|
||||
if input != nil {
|
||||
if !bytes.Equal(output, input[i:]) {
|
||||
tester.t.Fatalf("input and output mismatch\n IN: %v\nOUT: %v\n", input[i:], output)
|
||||
if !bytes.Equal(output[:readableLength], input[i:]) {
|
||||
tester.t.Fatalf("input and output mismatch\n IN: %v\nOUT: %v\n", input[i:], output[:readableLength])
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -177,7 +177,8 @@ func TestDataAppend(t *testing.T) {
|
||||
input, found := tester.inputs[uint64(n)]
|
||||
var data io.Reader
|
||||
if !found {
|
||||
data, input = GenerateRandomData(n)
|
||||
input = testutil.RandomBytes(i, n)
|
||||
data = bytes.NewReader(input)
|
||||
tester.inputs[uint64(n)] = input
|
||||
} else {
|
||||
data = io.LimitReader(bytes.NewReader(input), int64(n))
|
||||
@ -199,7 +200,8 @@ func TestDataAppend(t *testing.T) {
|
||||
appendInput, found := tester.inputs[uint64(m)]
|
||||
var appendData io.Reader
|
||||
if !found {
|
||||
appendData, appendInput = GenerateRandomData(m)
|
||||
appendInput = testutil.RandomBytes(i, m)
|
||||
appendData = bytes.NewReader(appendInput)
|
||||
tester.inputs[uint64(m)] = appendInput
|
||||
} else {
|
||||
appendData = io.LimitReader(bytes.NewReader(appendInput), int64(m))
|
||||
@ -272,7 +274,7 @@ func benchReadAll(reader LazySectionReader) {
|
||||
func benchmarkSplitJoin(n int, t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
for i := 0; i < t.N; i++ {
|
||||
data := testDataReader(n)
|
||||
data := testutil.RandomReader(i, n)
|
||||
|
||||
putGetter := newTestHasherStore(NewMapChunkStore(), SHA3Hash)
|
||||
ctx := context.TODO()
|
||||
@ -292,7 +294,7 @@ func benchmarkSplitJoin(n int, t *testing.B) {
|
||||
func benchmarkSplitTreeSHA3(n int, t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
for i := 0; i < t.N; i++ {
|
||||
data := testDataReader(n)
|
||||
data := testutil.RandomReader(i, n)
|
||||
putGetter := newTestHasherStore(&FakeChunkStore{}, SHA3Hash)
|
||||
|
||||
ctx := context.Background()
|
||||
@ -311,7 +313,7 @@ func benchmarkSplitTreeSHA3(n int, t *testing.B) {
|
||||
func benchmarkSplitTreeBMT(n int, t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
for i := 0; i < t.N; i++ {
|
||||
data := testDataReader(n)
|
||||
data := testutil.RandomReader(i, n)
|
||||
putGetter := newTestHasherStore(&FakeChunkStore{}, BMTHash)
|
||||
|
||||
ctx := context.Background()
|
||||
@ -329,7 +331,7 @@ func benchmarkSplitTreeBMT(n int, t *testing.B) {
|
||||
func benchmarkSplitPyramidBMT(n int, t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
for i := 0; i < t.N; i++ {
|
||||
data := testDataReader(n)
|
||||
data := testutil.RandomReader(i, n)
|
||||
putGetter := newTestHasherStore(&FakeChunkStore{}, BMTHash)
|
||||
|
||||
ctx := context.Background()
|
||||
@ -347,7 +349,7 @@ func benchmarkSplitPyramidBMT(n int, t *testing.B) {
|
||||
func benchmarkSplitPyramidSHA3(n int, t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
for i := 0; i < t.N; i++ {
|
||||
data := testDataReader(n)
|
||||
data := testutil.RandomReader(i, n)
|
||||
putGetter := newTestHasherStore(&FakeChunkStore{}, SHA3Hash)
|
||||
|
||||
ctx := context.Background()
|
||||
@ -365,8 +367,8 @@ func benchmarkSplitPyramidSHA3(n int, t *testing.B) {
|
||||
func benchmarkSplitAppendPyramid(n, m int, t *testing.B) {
|
||||
t.ReportAllocs()
|
||||
for i := 0; i < t.N; i++ {
|
||||
data := testDataReader(n)
|
||||
data1 := testDataReader(m)
|
||||
data := testutil.RandomReader(i, n)
|
||||
data1 := testutil.RandomReader(t.N+i, m)
|
||||
|
||||
store := NewMapChunkStore()
|
||||
putGetter := newTestHasherStore(store, SHA3Hash)
|
||||
|
@ -19,7 +19,6 @@ package storage
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -31,7 +30,7 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
ch "github.com/ethereum/go-ethereum/swarm/chunk"
|
||||
colorable "github.com/mattn/go-colorable"
|
||||
"github.com/mattn/go-colorable"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -151,10 +150,6 @@ func mget(store ChunkStore, hs []Address, f func(h Address, chunk Chunk) error)
|
||||
return err
|
||||
}
|
||||
|
||||
func testDataReader(l int) (r io.Reader) {
|
||||
return io.LimitReader(rand.Reader, int64(l))
|
||||
}
|
||||
|
||||
func (r *brokenLimitedReader) Read(buf []byte) (int, error) {
|
||||
if r.off+len(buf) > r.errAt {
|
||||
return 0, fmt.Errorf("Broken reader")
|
||||
|
File diff suppressed because one or more lines are too long
@ -23,6 +23,8 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/swarm/testutil"
|
||||
)
|
||||
|
||||
const testDataSize = 0x0001000
|
||||
@ -49,9 +51,9 @@ func testFileStoreRandom(toEncrypt bool, t *testing.T) {
|
||||
fileStore := NewFileStore(localStore, NewFileStoreParams())
|
||||
defer os.RemoveAll("/tmp/bzz")
|
||||
|
||||
reader, slice := GenerateRandomData(testDataSize)
|
||||
slice := testutil.RandomBytes(1, testDataSize)
|
||||
ctx := context.TODO()
|
||||
key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
|
||||
key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
|
||||
if err != nil {
|
||||
t.Fatalf("Store error: %v", err)
|
||||
}
|
||||
@ -63,13 +65,13 @@ func testFileStoreRandom(toEncrypt bool, t *testing.T) {
|
||||
if isEncrypted != toEncrypt {
|
||||
t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
|
||||
}
|
||||
resultSlice := make([]byte, len(slice))
|
||||
resultSlice := make([]byte, testDataSize)
|
||||
n, err := resultReader.ReadAt(resultSlice, 0)
|
||||
if err != io.EOF {
|
||||
t.Fatalf("Retrieve error: %v", err)
|
||||
}
|
||||
if n != len(slice) {
|
||||
t.Fatalf("Slice size error got %d, expected %d.", n, len(slice))
|
||||
if n != testDataSize {
|
||||
t.Fatalf("Slice size error got %d, expected %d.", n, testDataSize)
|
||||
}
|
||||
if !bytes.Equal(slice, resultSlice) {
|
||||
t.Fatalf("Comparison error.")
|
||||
@ -114,9 +116,9 @@ func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
|
||||
DbStore: db,
|
||||
}
|
||||
fileStore := NewFileStore(localStore, NewFileStoreParams())
|
||||
reader, slice := GenerateRandomData(testDataSize)
|
||||
slice := testutil.RandomBytes(1, testDataSize)
|
||||
ctx := context.TODO()
|
||||
key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
|
||||
key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
|
||||
if err != nil {
|
||||
t.Errorf("Store error: %v", err)
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ import (
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto/sha3"
|
||||
@ -251,16 +250,6 @@ func GenerateRandomChunks(dataSize int64, count int) (chunks []Chunk) {
|
||||
return chunks
|
||||
}
|
||||
|
||||
func GenerateRandomData(l int) (r io.Reader, slice []byte) {
|
||||
slice, err := ioutil.ReadAll(io.LimitReader(rand.Reader, int64(l)))
|
||||
if err != nil {
|
||||
panic("rand error")
|
||||
}
|
||||
// log.Warn("generate random data", "len", len(slice), "data", common.Bytes2Hex(slice))
|
||||
r = io.LimitReader(bytes.NewReader(slice), int64(l))
|
||||
return r, slice
|
||||
}
|
||||
|
||||
// Size, Seek, Read, ReadAt
|
||||
type LazySectionReader interface {
|
||||
Context() context.Context
|
||||
|
Reference in New Issue
Block a user