internal/testrand: new package for random data (#2282)

This commit is contained in:
Egon Elbre 2019-06-26 13:38:51 +03:00 committed by GitHub
parent 6b23380d6b
commit b6ad3e9c9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
65 changed files with 432 additions and 628 deletions

View File

@ -5,30 +5,30 @@ package cmd
import (
"io/ioutil"
"math/rand"
"os"
"testing"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
"storj.io/storj/uplink"
)
func TestLoadEncryptionKeyIntoEncryptionAccess(t *testing.T) {
t.Run("ok", func(t *testing.T) {
passphrase := make([]byte, rand.Intn(100)+1)
_, err := rand.Read(passphrase)
require.NoError(t, err)
ctx := testcontext.New(t)
defer ctx.Cleanup()
passphrase := testrand.BytesInt(testrand.Intn(100) + 1)
expectedKey, err := storj.NewKey(passphrase)
require.NoError(t, err)
ctx := testcontext.New(t)
filename := ctx.File("encryption.key")
err = ioutil.WriteFile(filename, expectedKey[:], os.FileMode(0400))
require.NoError(t, err)
defer ctx.Cleanup()
access, err := loadEncryptionAccess(filename)
require.NoError(t, err)
@ -38,6 +38,7 @@ func TestLoadEncryptionKeyIntoEncryptionAccess(t *testing.T) {
t.Run("error", func(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
filename := ctx.File("encryption.key")
_, err := loadEncryptionAccess(filename)
@ -46,17 +47,11 @@ func TestLoadEncryptionKeyIntoEncryptionAccess(t *testing.T) {
}
func TestSaveLoadEncryptionKey(t *testing.T) {
var inputKey string
{
randKey := make([]byte, rand.Intn(storj.KeySize)*3+1)
_, err := rand.Read(randKey)
require.NoError(t, err)
inputKey = string(randKey)
}
ctx := testcontext.New(t)
defer ctx.Cleanup()
inputKey := string(testrand.BytesInt(testrand.Intn(storj.KeySize)*3 + 1))
filename := ctx.File("storj-test-cmd-uplink", "encryption.key")
err := uplink.SaveEncryptionKey(inputKey, filename)
require.NoError(t, err)

View File

@ -7,12 +7,12 @@ import (
"encoding/hex"
"errors"
"log"
"math/rand"
"os"
"testing"
"storj.io/storj/internal/memory"
"storj.io/storj/internal/s3client"
"storj.io/storj/internal/testrand"
)
var benchmarkCases = []struct {
@ -33,12 +33,7 @@ var testObjects = createObjects()
func createObjects() map[string][]byte {
objects := make(map[string][]byte)
for _, bm := range benchmarkCases {
data := make([]byte, bm.objectsize)
_, err := rand.Read(data)
if err != nil {
log.Fatalf("failed to read random bytes: %+v\n", err)
}
objects[bm.name] = data
objects[bm.name] = testrand.Bytes(bm.objectsize)
}
return objects
}
@ -155,8 +150,7 @@ func benchmarkUpload(b *testing.B, client s3client.Client, bucket string, upload
b.ResetTimer()
for i := 0; i < b.N; i++ {
// make some random bytes so the objectPath is unique
randomBytes := make([]byte, 16)
rand.Read(randomBytes)
randomBytes := testrand.Bytes(16)
uniquePathPart := hex.EncodeToString(randomBytes)
objectPath := "folder/data" + uniquePathPart + "_" + benchmark.name
err := client.Upload(bucket, objectPath, testObjects[benchmark.name])

View File

@ -5,7 +5,6 @@ package sync2_test
import (
"context"
"crypto/rand"
"io"
"io/ioutil"
"testing"
@ -14,6 +13,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/sync2"
"storj.io/storj/internal/testrand"
)
func TestCopy(t *testing.T) {
@ -22,7 +22,7 @@ func TestCopy(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := io.LimitReader(rand.Reader, 32*memory.KiB.Int64())
r := io.LimitReader(testrand.Reader(), 32*memory.KiB.Int64())
n, err := sync2.Copy(ctx, ioutil.Discard, r)
@ -36,7 +36,7 @@ func TestCopy_Cancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
r := io.LimitReader(rand.Reader, 32*memory.KiB.Int64())
r := io.LimitReader(testrand.Reader(), 32*memory.KiB.Int64())
n, err := sync2.Copy(ctx, ioutil.Discard, r)

View File

@ -7,7 +7,6 @@ import (
"bytes"
"context"
"fmt"
"math/rand"
"path/filepath"
"testing"
"time"
@ -19,6 +18,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/peertls/extensions"
"storj.io/storj/pkg/peertls/tlsopts"
@ -45,15 +45,9 @@ func TestUplinksParallel(t *testing.T) {
for p := 0; p < parallelCount; p++ {
suffix := fmt.Sprintf("-%d-%d", i, p)
group.Go(func() error {
random := rand.New(rand.NewSource(rand.Int63()))
data := testrand.Bytes(memory.Size(100+testrand.Intn(500)) * memory.KiB)
data := make([]byte, 100*memory.KiB.Int()+random.Intn(500)*memory.KiB.Int())
_, err := random.Read(data)
if err != nil {
return err
}
err = uplink.Upload(ctx, satellite, "testbucket"+suffix, "test/path"+suffix, data)
err := uplink.Upload(ctx, satellite, "testbucket"+suffix, "test/path"+suffix, data)
if err != nil {
return err
}
@ -87,11 +81,9 @@ func TestDownloadWithSomeNodesOffline(t *testing.T) {
// stop discovery service so that we do not get a race condition when we delete nodes from overlay cache
satellite.Discovery.Service.Discovery.Stop()
testData := make([]byte, 1*memory.MiB)
_, err := rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(memory.MiB)
err = ul.UploadWithConfig(ctx, satellite, &uplink.RSConfig{
err := ul.UploadWithConfig(ctx, satellite, &uplink.RSConfig{
MinThreshold: 2,
RepairThreshold: 3,
SuccessThreshold: 4,
@ -173,12 +165,9 @@ func TestDownloadFromUnresponsiveNode(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 5, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
expectedData := testrand.Bytes(memory.MiB)
expectedData := make([]byte, 1*memory.MiB)
_, err := rand.Read(expectedData)
assert.NoError(t, err)
err = planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &uplink.RSConfig{
err := planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &uplink.RSConfig{
MinThreshold: 2,
RepairThreshold: 3,
SuccessThreshold: 4,

101
internal/testrand/rand.go Normal file
View File

@ -0,0 +1,101 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
// Package testrand implements generating random base types for testing.
package testrand
import (
"io"
"math/rand"
"github.com/skyrings/skyring-common/tools/uuid"
"storj.io/storj/internal/memory"
"storj.io/storj/pkg/storj"
)
// Intn returns, as an int, a non-negative pseudo-random number in [0,n)
// from the default Source.
// It panics if n <= 0.
func Intn(n int) int { return rand.Intn(n) }
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n)
// from the default Source.
// It panics if n <= 0.
func Int63n(n int64) int64 {
return rand.Int63n(n)
}
// Read reads pseudo-random data into data.
func Read(data []byte) {
const newSourceThreshold = 64
if len(data) < newSourceThreshold {
_, _ = rand.Read(data)
return
}
src := rand.NewSource(rand.Int63())
r := rand.New(src)
_, _ = r.Read(data)
}
// Bytes generates size amount of random data.
func Bytes(size memory.Size) []byte {
data := make([]byte, size.Int())
Read(data)
return data
}
// BytesInt generates size amount of random data.
func BytesInt(size int) []byte {
return Bytes(memory.Size(size))
}
// Reader creates a new random data reader.
func Reader() io.Reader {
return rand.New(rand.NewSource(rand.Int63()))
}
// NodeID creates a random node id.
func NodeID() storj.NodeID {
var id storj.NodeID
Read(id[:])
// set version to 0
id[len(id)-1] = 0
return id
}
// PieceID creates a random piece id.
func PieceID() storj.PieceID {
var id storj.PieceID
Read(id[:])
return id
}
// Key creates a random test key.
func Key() storj.Key {
var key storj.Key
Read(key[:])
return key
}
// Nonce creates a random test nonce.
func Nonce() storj.Nonce {
var nonce storj.Nonce
Read(nonce[:])
return nonce
}
// SerialNumber creates a random serial number.
func SerialNumber() storj.SerialNumber {
var serial storj.SerialNumber
Read(serial[:])
return serial
}
// UUID creates a random uuid.
func UUID() uuid.UUID {
var uuid uuid.UUID
Read(uuid[:])
return uuid
}

View File

@ -7,10 +7,10 @@ import (
"testing"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/accounting"
"storj.io/storj/satellite"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
@ -27,10 +27,7 @@ func TestBucketUsage(t *testing.T) {
t.Fail()
}
bucketID, err := uuid.New()
if err != nil {
t.Fail()
}
bucketID := testrand.UUID()
compareRollups := func(t *testing.T, expected *accounting.BucketRollup, actual *accounting.BucketRollup) {
assert.Equal(t, expected.BucketID, actual.BucketID)
@ -50,7 +47,7 @@ func TestBucketUsage(t *testing.T) {
t.Run("add rollup", func(t *testing.T) {
var err error
data := accounting.BucketRollup{
BucketID: *bucketID,
BucketID: bucketID,
RollupEndTime: now,
RemoteStoredData: 5,
InlineStoredData: 6,
@ -84,7 +81,7 @@ func TestBucketUsage(t *testing.T) {
t.Run("add rollups", func(t *testing.T) {
for i := 0; i < count; i++ {
data := accounting.BucketRollup{
BucketID: *bucketID,
BucketID: bucketID,
RollupEndTime: now.Add(time.Hour * time.Duration(i+1)),
RemoteStoredData: uint64(i),
InlineStoredData: uint64(i + 1),
@ -108,7 +105,7 @@ func TestBucketUsage(t *testing.T) {
t.Run("retrieve rollup", func(t *testing.T) {
t.Run("first 30 backward", func(t *testing.T) {
cursor := &accounting.BucketRollupCursor{
BucketID: *bucketID,
BucketID: bucketID,
Before: now.Add(time.Hour * 30),
Order: accounting.Desc,
PageSize: 10,
@ -138,7 +135,7 @@ func TestBucketUsage(t *testing.T) {
t.Run("last 30 forward", func(t *testing.T) {
cursor := &accounting.BucketRollupCursor{
BucketID: *bucketID,
BucketID: bucketID,
After: now.Add(time.Hour * 20),
Before: now.Add(time.Hour * time.Duration(count+1)),
Order: accounting.Asc,

View File

@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/accounting"
"storj.io/storj/pkg/storj"
"storj.io/storj/satellite"
@ -24,9 +25,9 @@ func TestSaveBucketTallies(t *testing.T) {
defer ctx.Cleanup()
// Setup: create bucket storage tallies
projectID, err := uuid.New()
require.NoError(t, err)
bucketTallies, expectedTallies, err := createBucketStorageTallies(*projectID)
projectID := testrand.UUID()
bucketTallies, expectedTallies, err := createBucketStorageTallies(projectID)
require.NoError(t, err)
// Execute test: retrieve the save tallies and confirm they contains the expected data

View File

@ -5,7 +5,6 @@ package live
import (
"context"
"encoding/binary"
"math/rand"
"testing"
@ -14,6 +13,8 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"storj.io/storj/internal/testrand"
)
func TestPlainMemoryLiveAccounting(t *testing.T) {
@ -43,9 +44,7 @@ func TestPlainMemoryLiveAccounting(t *testing.T) {
// make up some project IDs
projectIDs := make([]uuid.UUID, numProjects)
for i := range projectIDs {
var u uuid.UUID
binary.BigEndian.PutUint64(u[len(u)-8:], uint64(i))
projectIDs[i] = u
projectIDs[i] = testrand.UUID()
}
// send lots of space used updates for all of these projects to the live
@ -92,8 +91,8 @@ func TestResetTotals(t *testing.T) {
require.True(t, ok)
ctx := context.Background()
projID, err := uuid.New()
require.NoError(t, err)
err = service.AddProjectStorageUsage(ctx, *projID, 0, -20)
projectID := testrand.UUID()
err = service.AddProjectStorageUsage(ctx, projectID, 0, -20)
require.NoError(t, err)
}

View File

@ -4,7 +4,6 @@
package accounting_test
import (
"crypto/rand"
"fmt"
"testing"
"time"
@ -16,6 +15,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/accounting"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
@ -64,9 +64,7 @@ func TestProjectUsageStorage(t *testing.T) {
require.Equal(t, testCase.expectedExceeded, actualExceeded)
// Setup: create some bytes for the uplink to upload
expectedData := make([]byte, 50*memory.KiB)
_, err = rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(50 * memory.KiB)
// Execute test: check that the uplink gets an error when they have exceeded storage limits and try to upload a file
actualErr := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
@ -117,9 +115,7 @@ func TestProjectUsageBandwidth(t *testing.T) {
}
// Setup: create some bytes for the uplink to upload to test the download later
expectedData := make([]byte, 50*memory.KiB)
_, err = rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(50 * memory.KiB)
filePath := "test/path"
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], bucketName, filePath, expectedData)
@ -219,16 +215,15 @@ func TestProjectBandwidthTotal(t *testing.T) {
defer ctx.Cleanup()
pdb := db.ProjectAccounting()
projectID, err := uuid.New()
require.NoError(t, err)
projectID := testrand.UUID()
// Setup: create bucket bandwidth rollup records
expectedTotal, err := createBucketBandwidthRollups(ctx, db, *projectID)
expectedTotal, err := createBucketBandwidthRollups(ctx, db, projectID)
require.NoError(t, err)
// Execute test: get project bandwidth total
from := time.Now().AddDate(0, 0, -accounting.AverageDaysInMonth) // past 30 days
actualBandwidthTotal, err := pdb.GetAllocatedBandwidthTotal(ctx, *projectID, from)
actualBandwidthTotal, err := pdb.GetAllocatedBandwidthTotal(ctx, projectID, from)
require.NoError(t, err)
require.Equal(t, actualBandwidthTotal, expectedTotal)
})
@ -283,9 +278,7 @@ func TestProjectUsageCustomLimit(t *testing.T) {
require.Equal(t, project.UsageLimit, limit.Int64())
// Setup: create some bytes for the uplink to upload
expectedData := make([]byte, 50*memory.KiB)
_, err = rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(50 * memory.KiB)
// Execute test: check that the uplink gets an error when they have exceeded storage limits and try to upload a file
actualErr := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)

View File

@ -5,7 +5,6 @@ package tally_test
import (
"fmt"
"math/rand"
"testing"
"time"
@ -16,6 +15,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/internal/teststorj"
"storj.io/storj/pkg/accounting"
"storj.io/storj/pkg/encryption"
@ -72,13 +72,10 @@ func TestOnlyInline(t *testing.T) {
if err1 != nil {
assert.NoError(t, err1)
}
projectID, err := uuid.Parse(projects[0].ID.String())
require.NoError(t, err)
projectID := projects[0].ID
// Setup: create data for the uplink to upload
expectedData := make([]byte, 1*memory.KiB)
_, err = rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(1 * memory.KiB)
// Setup: get the expected size of the data that will be stored in pointer
// Since the data is small enough to be stored inline, when it is encrypted, we only
@ -104,7 +101,7 @@ func TestOnlyInline(t *testing.T) {
assert.Equal(t, 16, len(projectID[:]))
// Execute test: upload a file, then calculate at rest data
err = uplink.Upload(ctx, planet.Satellites[0], expectedBucketName, "test/path", expectedData)
err := uplink.Upload(ctx, planet.Satellites[0], expectedBucketName, "test/path", expectedData)
assert.NoError(t, err)
// Run calculate twice to test unique constraint issue
@ -134,9 +131,7 @@ func TestCalculateNodeAtRestData(t *testing.T) {
uplink := planet.Uplinks[0]
// Setup: create 50KiB of data for the uplink to upload
expectedData := make([]byte, 50*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(50 * memory.KiB)
// Setup: get the expected size of the data that will be stored in pointer
uplinkConfig := uplink.GetConfig(planet.Satellites[0])

View File

@ -4,13 +4,13 @@
package audit_test
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/audit"
"storj.io/storj/pkg/pkcrypto"
"storj.io/storj/pkg/storj"
@ -20,22 +20,16 @@ func TestContainIncrementAndGet(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 4,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
randBytes := make([]byte, 10)
_, err := rand.Read(randBytes)
require.NoError(t, err)
someHash := pkcrypto.SHA256Hash(randBytes)
input := &audit.PendingAudit{
NodeID: planet.StorageNodes[0].ID(),
PieceID: storj.PieceID{},
StripeIndex: 0,
ShareSize: 0,
ExpectedShareHash: someHash,
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
ReverifyCount: 0,
}
err = planet.Satellites[0].DB.Containment().IncrementPending(ctx, input)
err := planet.Satellites[0].DB.Containment().IncrementPending(ctx, input)
require.NoError(t, err)
output, err := planet.Satellites[0].DB.Containment().Get(ctx, input.NodeID)
@ -59,35 +53,24 @@ func TestContainIncrementPendingEntryExists(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 4,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
randBytes := make([]byte, 10)
_, err := rand.Read(randBytes)
require.NoError(t, err)
hash1 := pkcrypto.SHA256Hash(randBytes)
info1 := &audit.PendingAudit{
NodeID: planet.StorageNodes[0].ID(),
PieceID: storj.PieceID{},
StripeIndex: 0,
ShareSize: 0,
ExpectedShareHash: hash1,
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
ReverifyCount: 0,
}
err = planet.Satellites[0].DB.Containment().IncrementPending(ctx, info1)
err := planet.Satellites[0].DB.Containment().IncrementPending(ctx, info1)
require.NoError(t, err)
randBytes = make([]byte, 10)
_, err = rand.Read(randBytes)
require.NoError(t, err)
hash2 := pkcrypto.SHA256Hash(randBytes)
info2 := &audit.PendingAudit{
NodeID: info1.NodeID,
PieceID: storj.PieceID{},
StripeIndex: 1,
ShareSize: 1,
ExpectedShareHash: hash2,
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
ReverifyCount: 0,
}
@ -114,22 +97,16 @@ func TestContainDelete(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 4,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
randBytes := make([]byte, 10)
_, err := rand.Read(randBytes)
require.NoError(t, err)
hash1 := pkcrypto.SHA256Hash(randBytes)
info1 := &audit.PendingAudit{
NodeID: planet.StorageNodes[0].ID(),
PieceID: storj.PieceID{},
StripeIndex: 0,
ShareSize: 0,
ExpectedShareHash: hash1,
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
ReverifyCount: 0,
}
err = planet.Satellites[0].DB.Containment().IncrementPending(ctx, info1)
err := planet.Satellites[0].DB.Containment().IncrementPending(ctx, info1)
require.NoError(t, err)
// check contained flag set to true

View File

@ -5,9 +5,7 @@ package audit_test
import (
"context"
"crypto/rand"
"math"
"math/big"
"reflect"
"testing"
"time"
@ -17,6 +15,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/internal/teststorj"
"storj.io/storj/pkg/audit"
"storj.io/storj/pkg/pb"
@ -66,11 +65,7 @@ func TestAuditSegment(t *testing.T) {
// get a list of 100 paths generated from random
for i := 0; i < 100; i++ {
randomNum, err := rand.Int(rand.Reader, big.NewInt(int64(len(list))))
if err != nil {
t.Error("num error: failed to get num")
}
pointerItem := list[randomNum.Int64()]
pointerItem := list[testrand.Int63n(int64(len(list)))]
path := pointerItem.Path
val := pathCount{path: path, count: 1}
pathCounter = append(pathCounter, val)

View File

@ -4,7 +4,6 @@
package audit_test
import (
"math/rand"
"testing"
"time"
@ -15,6 +14,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/audit"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/storage/streams"
@ -110,10 +110,9 @@ func calcReputation(dossier *overlay.NodeDossier) float64 {
}
func TestDisqualifiedNodesGetNoDownload(t *testing.T) {
// - uploads random data
// - mark a node as disqualified
// - check we don't get it when we require order limit
// Uploads random data.
// Mark a node as disqualified.
// Check we don't get it when we require order limit.
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
@ -124,9 +123,7 @@ func TestDisqualifiedNodesGetNoDownload(t *testing.T) {
err := satellite.Audit.Service.Close()
require.NoError(t, err)
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = upl.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)

View File

@ -4,7 +4,6 @@
package audit_test
import (
"crypto/rand"
"testing"
"time"
@ -15,6 +14,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/audit"
"storj.io/storj/pkg/peertls/tlsopts"
"storj.io/storj/pkg/pkcrypto"
@ -39,9 +39,7 @@ func TestReverifySuccess(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 1*memory.MiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(memory.MiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -122,9 +120,7 @@ func TestReverifyFailMissingShare(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 1*memory.MiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(memory.MiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -211,9 +207,7 @@ func TestReverifyFailBadData(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 1*memory.MiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(memory.MiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -243,16 +237,12 @@ func TestReverifyFailBadData(t *testing.T) {
rootPieceID := stripe.Segment.GetRemote().RootPieceId
redundancy := stripe.Segment.GetRemote().GetRedundancy()
randBytes := make([]byte, 10)
_, err = rand.Read(randBytes)
require.NoError(t, err)
pending := &audit.PendingAudit{
NodeID: pieces[0].NodeId,
PieceID: rootPieceID,
StripeIndex: stripe.Index,
ShareSize: redundancy.ErasureShareSize,
ExpectedShareHash: pkcrypto.SHA256Hash(randBytes),
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
ReverifyCount: 0,
}
@ -286,9 +276,7 @@ func TestReverifyOffline(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 1*memory.MiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(memory.MiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -317,16 +305,12 @@ func TestReverifyOffline(t *testing.T) {
rootPieceID := stripe.Segment.GetRemote().RootPieceId
redundancy := stripe.Segment.GetRemote().GetRedundancy()
randBytes := make([]byte, 10)
_, err = rand.Read(randBytes)
require.NoError(t, err)
pending := &audit.PendingAudit{
NodeID: pieces[0].NodeId,
PieceID: rootPieceID,
StripeIndex: stripe.Index,
ShareSize: redundancy.ErasureShareSize,
ExpectedShareHash: pkcrypto.SHA256Hash(randBytes),
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
ReverifyCount: 0,
}
@ -363,9 +347,7 @@ func TestReverifyOfflineDialTimeout(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 1*memory.MiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(memory.MiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -413,16 +395,12 @@ func TestReverifyOfflineDialTimeout(t *testing.T) {
rootPieceID := stripe.Segment.GetRemote().RootPieceId
redundancy := stripe.Segment.GetRemote().GetRedundancy()
randBytes := make([]byte, 10)
_, err = rand.Read(randBytes)
require.NoError(t, err)
pending := &audit.PendingAudit{
NodeID: pieces[0].NodeId,
PieceID: rootPieceID,
StripeIndex: stripe.Index,
ShareSize: redundancy.ErasureShareSize,
ExpectedShareHash: pkcrypto.SHA256Hash(randBytes),
ExpectedShareHash: pkcrypto.SHA256Hash(testrand.Bytes(10)),
ReverifyCount: 0,
}
@ -456,9 +434,7 @@ func TestReverifyDeletedSegment(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -521,9 +497,7 @@ func TestReverifyModifiedSegment(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)

View File

@ -5,7 +5,6 @@ package audit_test
import (
"context"
"math/rand"
"testing"
"time"
@ -19,6 +18,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/audit"
"storj.io/storj/pkg/peertls/tlsopts"
"storj.io/storj/pkg/storj"
@ -37,9 +37,7 @@ func TestDownloadSharesHappyPath(t *testing.T) {
require.NoError(t, err)
uplink := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = uplink.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -93,9 +91,7 @@ func TestDownloadSharesOfflineNode(t *testing.T) {
require.NoError(t, err)
uplink := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = uplink.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -157,9 +153,7 @@ func TestDownloadSharesMissingPiece(t *testing.T) {
require.NoError(t, err)
uplink := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = uplink.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -217,9 +211,7 @@ func TestDownloadSharesDialTimeout(t *testing.T) {
require.NoError(t, err)
upl := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = upl.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -293,9 +285,7 @@ func TestDownloadSharesDownloadTimeout(t *testing.T) {
require.NoError(t, err)
upl := planet.Uplinks[0]
testData := make([]byte, 32*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(32 * memory.KiB)
// Upload with larger erasure share size to simulate longer download over slow transport client
err = upl.UploadWithConfig(ctx, planet.Satellites[0], &uplink.RSConfig{
@ -364,9 +354,7 @@ func TestVerifierHappyPath(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -404,9 +392,7 @@ func TestVerifierOfflineNode(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -448,9 +434,7 @@ func TestVerifierMissingPiece(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -494,9 +478,7 @@ func TestVerifierDialTimeout(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -553,9 +535,7 @@ func TestVerifierDeletedSegment(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)
@ -592,9 +572,7 @@ func TestVerifierModifiedSegment(t *testing.T) {
require.NoError(t, err)
ul := planet.Uplinks[0]
testData := make([]byte, 8*memory.KiB)
_, err = rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(8 * memory.KiB)
err = ul.Upload(ctx, planet.Satellites[0], "testbucket", "test/path", testData)
require.NoError(t, err)

View File

@ -5,13 +5,13 @@ package audit
import (
"context"
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vivint/infectious"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/pkcrypto"
"storj.io/storj/pkg/storj"
@ -121,9 +121,7 @@ func TestCreatePendingAudits(t *testing.T) {
err = f.Encode([]byte("hello, world! __"), output)
require.NoError(t, err)
var testNodeID storj.NodeID
_, err = rand.Read(testNodeID[:])
require.NoError(t, err)
testNodeID := testrand.NodeID()
ctx := context.Background()
contained := make(map[int]storj.NodeID)

View File

@ -5,13 +5,13 @@ package bloomfilter_test
import (
"flag"
"math/rand"
"sort"
"testing"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/bloomfilter"
"storj.io/storj/pkg/storj"
)
@ -36,7 +36,7 @@ func TestBytes(t *testing.T) {
for _, count := range []int{0, 100, 1000, 10000} {
filter := bloomfilter.NewOptimal(count, 0.1)
for i := 0; i < count; i++ {
id := newTestPieceID()
id := testrand.PieceID()
filter.Add(id)
}
@ -66,18 +66,11 @@ func TestBytes_Failing(t *testing.T) {
func generateTestIDs(n int) []storj.PieceID {
ids := make([]storj.PieceID, n)
for i := range ids {
ids[i] = newTestPieceID()
ids[i] = testrand.PieceID()
}
return ids
}
func newTestPieceID() storj.PieceID {
var id storj.PieceID
// using math/rand, for less overhead
_, _ = rand.Read(id[:])
return id
}
func BenchmarkFilterAdd(b *testing.B) {
ids := generateTestIDs(100000)
filter := bloomfilter.NewOptimal(len(ids), 0.1)
@ -112,12 +105,12 @@ func TestApproximateFalsePositives(t *testing.T) {
for k := 0; k < measurements; k++ {
filter := bloomfilter.NewOptimal(n, p)
for i := 0; i < n; i++ {
filter.Add(newTestPieceID())
filter.Add(testrand.PieceID())
}
positive := 0
for k := 0; k < validation; k++ {
if filter.Contains(newTestPieceID()) {
if filter.Contains(testrand.PieceID()) {
positive++
}
}

View File

@ -4,7 +4,6 @@
package datarepair_test
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
@ -13,6 +12,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
"storj.io/storj/uplink"
@ -36,11 +36,9 @@ func TestDataRepair(t *testing.T) {
satellite.Repair.Checker.Loop.Pause()
satellite.Repair.Repairer.Loop.Pause()
testData := make([]byte, 1*memory.MiB)
_, err := rand.Read(testData)
assert.NoError(t, err)
testData := testrand.Bytes(1 * memory.MiB)
err = ul.UploadWithConfig(ctx, satellite, &uplink.RSConfig{
err := ul.UploadWithConfig(ctx, satellite, &uplink.RSConfig{
MinThreshold: 3,
RepairThreshold: 5,
SuccessThreshold: 7,

View File

@ -23,23 +23,15 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/readcloser"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/encryption"
"storj.io/storj/pkg/ranger"
"storj.io/storj/pkg/storj"
)
func randData(amount int) []byte {
buf := make([]byte, amount)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
return buf
}
func TestRS(t *testing.T) {
ctx := context.Background()
data := randData(32 * 1024)
data := testrand.Bytes(32 * 1024)
fc, err := infectious.NewFEC(2, 4)
if err != nil {
t.Fatal(err)
@ -70,7 +62,7 @@ func TestRS(t *testing.T) {
// if DecodeReaders return less data than expected.
func TestRSUnexpectedEOF(t *testing.T) {
ctx := context.Background()
data := randData(32 * 1024)
data := testrand.Bytes(32 * 1024)
fc, err := infectious.NewFEC(2, 4)
if err != nil {
t.Fatal(err)
@ -98,7 +90,7 @@ func TestRSUnexpectedEOF(t *testing.T) {
func TestRSRanger(t *testing.T) {
ctx := context.Background()
data := randData(32 * 1024)
data := testrand.Bytes(32 * 1024)
fc, err := infectious.NewFEC(2, 4)
if err != nil {
t.Fatal(err)
@ -311,7 +303,7 @@ func TestRSLateEOF(t *testing.T) {
} {
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
// extend the input with random number of random bytes
random := randData(1 + rand.Intn(10000))
random := testrand.BytesInt(1 + testrand.Intn(10000))
extended := append(in, random...)
return ioutil.NopCloser(bytes.NewReader(extended))
})
@ -342,7 +334,7 @@ func TestRSRandomData(t *testing.T) {
} {
testRSProblematic(t, tt, i, func(in []byte) io.ReadCloser {
// return random data instead of expected one
return ioutil.NopCloser(bytes.NewReader(randData(len(in))))
return ioutil.NopCloser(bytes.NewReader(testrand.BytesInt(len(in))))
})
}
}
@ -384,7 +376,7 @@ type problematicReadCloser func([]byte) io.ReadCloser
func testRSProblematic(t *testing.T, tt testCase, i int, fn problematicReadCloser) {
errTag := fmt.Sprintf("Test case #%d", i)
ctx := context.Background()
data := randData(tt.dataSize)
data := testrand.BytesInt(tt.dataSize)
fc, err := infectious.NewFEC(tt.required, tt.total)
if !assert.NoError(t, err, errTag) {
return
@ -460,7 +452,7 @@ func (s *slowReader) Read(p []byte) (n int, err error) {
func TestEncoderStalledReaders(t *testing.T) {
ctx := context.Background()
data := randData(120 * 1024)
data := testrand.Bytes(120 * 1024)
fc, err := infectious.NewFEC(30, 60)
if err != nil {
t.Fatal(err)
@ -506,7 +498,7 @@ func readAllStalled(readers []io.ReadCloser, stalled int) ([][]byte, error) {
func TestDecoderErrorWithStalledReaders(t *testing.T) {
ctx := context.Background()
data := randData(10 * 1024)
data := testrand.Bytes(10 * 1024)
fc, err := infectious.NewFEC(10, 20)
if err != nil {
t.Fatal(err)
@ -554,7 +546,7 @@ func TestDecoderErrorWithStalledReaders(t *testing.T) {
}
func BenchmarkReedSolomonErasureScheme(b *testing.B) {
data := randData(8 << 20)
data := testrand.Bytes(8 << 20)
output := make([]byte, 8<<20)
confs := []struct{ required, total int }{
@ -670,7 +662,7 @@ func TestCalcPieceSize(t *testing.T) {
calculatedSize := CalcPieceSize(dataSize, es)
randReader := ioutil.NopCloser(io.LimitReader(rand.New(rand.NewSource(rand.Int63())), dataSize))
randReader := ioutil.NopCloser(io.LimitReader(testrand.Reader(), dataSize))
readers, err := EncodeReader(ctx, PadReader(randReader, es.StripeSize()), rs)
require.NoError(t, err, errTag)

View File

@ -8,21 +8,21 @@ import (
"io/ioutil"
"testing"
"storj.io/storj/pkg/storj"
"storj.io/storj/internal/testrand"
)
func TestAesGcm(t *testing.T) {
var key storj.Key
copy(key[:], randData(storj.KeySize))
key := testrand.Key()
var firstNonce AESGCMNonce
copy(firstNonce[:], randData(AESGCMNonceSize))
testrand.Read(firstNonce[:])
encrypter, err := NewAESGCMEncrypter(&key, &firstNonce, 4*1024)
if err != nil {
t.Fatal(err)
}
data := randData(encrypter.InBlockSize() * 10)
encrypted := TransformReader(
ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
data := testrand.BytesInt(encrypter.InBlockSize() * 10)
encrypted := TransformReader(ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
decrypter, err := NewAESGCMDecrypter(&key, &firstNonce, 4*1024)
if err != nil {
t.Fatal(err)
@ -32,6 +32,7 @@ func TestAesGcm(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, data2) {
t.Fatalf("encryption/decryption failed")
}

View File

@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
@ -15,6 +14,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/eestream"
"storj.io/storj/pkg/encryption"
"storj.io/storj/pkg/storj"
@ -48,7 +48,7 @@ func TestCalcEncryptedSize(t *testing.T) {
encrypter, err := encryption.NewEncrypter(scheme.Cipher, new(storj.Key), new(storj.Nonce), int(scheme.BlockSize))
require.NoError(t, err, errTag)
randReader := ioutil.NopCloser(io.LimitReader(rand.New(rand.NewSource(rand.Int63())), dataSize))
randReader := ioutil.NopCloser(io.LimitReader(testrand.Reader(), dataSize))
reader := encryption.TransformReader(eestream.PadReader(randReader, encrypter.InBlockSize()), encrypter, 0)
cipherData, err := ioutil.ReadAll(reader)

View File

@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/paths"
"storj.io/storj/pkg/storj"
)
@ -35,9 +36,7 @@ func TestStoreEncryption(t *testing.T) {
} {
errTag := fmt.Sprintf("test:%d path:%q cipher:%v", i, rawPath, cipher)
var key storj.Key
copy(key[:], randData(storj.KeySize))
store := newStore(key)
store := newStore(testrand.Key())
path := paths.NewUnencrypted(rawPath)
encPath, err := StoreEncryptPath("bucket", path, cipher, store)

View File

@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
)
@ -26,15 +27,14 @@ func TestEncryption(t *testing.T) {
} {
errTag := fmt.Sprintf("%d. %+v", i, path)
key := new(storj.Key)
copy(key[:], randData(storj.KeySize))
key := testrand.Key()
encrypted, err := EncryptPath(path, cipher, key)
encrypted, err := EncryptPath(path, cipher, &key)
if !assert.NoError(t, err, errTag) {
continue
}
decrypted, err := DecryptPath(encrypted, cipher, key)
decrypted, err := DecryptPath(encrypted, cipher, &key)
if !assert.NoError(t, err, errTag) {
continue
}
@ -61,15 +61,14 @@ func TestDeriveKey(t *testing.T) {
} {
errTag := fmt.Sprintf("%d. %+v", i, tt)
key := new(storj.Key)
copy(key[:], randData(storj.KeySize))
key := testrand.Key()
encrypted, err := EncryptPath(tt.path, cipher, key)
encrypted, err := EncryptPath(tt.path, cipher, &key)
if !assert.NoError(t, err, errTag) {
continue
}
derivedKey, err := DerivePathKey(tt.path, key, tt.depth)
derivedKey, err := DerivePathKey(tt.path, &key, tt.depth)
if tt.errString != "" {
assert.EqualError(t, err, tt.errString, errTag)
continue

View File

@ -5,39 +5,31 @@ package encryption
import (
"bytes"
"crypto/rand"
"io/ioutil"
"testing"
"storj.io/storj/pkg/storj"
"storj.io/storj/internal/testrand"
)
func randData(amount int) []byte {
buf := make([]byte, amount)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
return buf
}
func TestSecretbox(t *testing.T) {
var key storj.Key
copy(key[:], randData(storj.KeySize))
var firstNonce storj.Nonce
copy(firstNonce[:], randData(storj.NonceSize))
key := testrand.Key()
firstNonce := testrand.Nonce()
encrypter, err := NewSecretboxEncrypter(&key, &firstNonce, 4*1024)
if err != nil {
t.Fatal(err)
}
data := randData(encrypter.InBlockSize() * 10)
encrypted := TransformReader(
ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
data := testrand.BytesInt(encrypter.InBlockSize() * 10)
encrypted := TransformReader(ioutil.NopCloser(bytes.NewReader(data)), encrypter, 0)
decrypter, err := NewSecretboxDecrypter(&key, &firstNonce, 4*1024)
if err != nil {
t.Fatal(err)
}
decrypted := TransformReader(encrypted, decrypter, 0)
data2, err := ioutil.ReadAll(decrypted)
if err != nil {
t.Fatal(err)

View File

@ -11,6 +11,8 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testrand"
)
func TestCalcEncompassingBlocks(t *testing.T) {
@ -87,9 +89,11 @@ func (t *nopTransformer) Transform(out, in []byte, blockNum int64) (
func TestTransformer(t *testing.T) {
transformer := NopTransformer(4 * 1024)
data := randData(transformer.InBlockSize() * 10)
data := testrand.BytesInt(transformer.InBlockSize() * 10)
transformed := TransformReader(
ioutil.NopCloser(bytes.NewReader(data)), transformer, 0)
ioutil.NopCloser(bytes.NewReader(data)),
transformer, 0)
data2, err := ioutil.ReadAll(transformed)
if assert.NoError(t, err) {
assert.Equal(t, data, data2)
@ -112,7 +116,7 @@ func TestTransformerSize(t *testing.T) {
} {
errTag := fmt.Sprintf("Test case #%d", i)
transformer := NopTransformer(tt.blockSize)
data := randData(transformer.InBlockSize() * tt.blocks)
data := testrand.BytesInt(transformer.InBlockSize() * tt.blocks)
transformed := TransformReaderSize(
ioutil.NopCloser(bytes.NewReader(data)),
transformer, 0, tt.expectedSize)

View File

@ -8,7 +8,6 @@ import (
"crypto/x509/pkix"
"encoding/asn1"
"fmt"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
@ -16,6 +15,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testidentity"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/peertls/extensions"
"storj.io/storj/pkg/peertls/tlsopts"
@ -162,8 +162,7 @@ func TestFullCertificateAuthority_AddExtension(t *testing.T) {
oldCert := ca.Cert
assert.Len(t, ca.Cert.ExtraExtensions, 0)
randBytes := make([]byte, 10)
rand.Read(randBytes)
randBytes := testrand.Bytes(10)
randExt := pkix.Extension{
Id: asn1.ObjectIdentifier{2, 999, int(randBytes[0])},
Value: randBytes,

View File

@ -7,7 +7,6 @@ import (
"bytes"
"context"
"crypto"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
@ -22,6 +21,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testidentity"
"storj.io/storj/internal/testpeertls"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/peertls"
"storj.io/storj/pkg/peertls/extensions"
@ -245,9 +245,7 @@ func TestManageablePeerIdentity_AddExtension(t *testing.T) {
oldLeaf := manageablePeerIdentity.Leaf
assert.Len(t, manageablePeerIdentity.CA.Cert.ExtraExtensions, 0)
randBytes := make([]byte, 10)
_, err = rand.Read(randBytes)
require.NoError(t, err)
randBytes := testrand.Bytes(10)
randExt := pkix.Extension{
Id: asn1.ObjectIdentifier{2, 999, int(randBytes[0])},
Value: randBytes,

View File

@ -6,7 +6,6 @@ package kademlia
import (
"bytes"
"context"
"math/rand"
"net"
"strconv"
"sync/atomic"
@ -21,6 +20,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testidentity"
"storj.io/storj/internal/testrand"
"storj.io/storj/internal/teststorj"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/overlay"
@ -357,8 +357,8 @@ func TestRandomIds(t *testing.T) {
for x := 0; x < 1000; x++ {
var start, end bucketID
// many valid options
rand.Read(start[:])
rand.Read(end[:])
start = testrand.NodeID()
end = testrand.NodeID()
if bytes.Compare(start[:], end[:]) > 0 {
start, end = end, start
}

View File

@ -7,7 +7,6 @@ import (
"bytes"
"context"
"fmt"
"math/rand"
"sort"
"testing"
"time"
@ -16,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/internal/teststorj"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
@ -94,7 +94,7 @@ func TestGetBucket(t *testing.T) {
func RandomNode() pb.Node {
node := pb.Node{}
rand.Read(node.Id[:])
node.Id = testrand.NodeID()
return node
}
func TestKademliaFindNear(t *testing.T) {

View File

@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
)
@ -25,15 +26,9 @@ func TestSortByXOR(t *testing.T) {
}
func BenchmarkSortByXOR(b *testing.B) {
newNodeID := func() storj.NodeID {
var id storj.NodeID
rand.Read(id[:])
return id
}
nodes := []storj.NodeID{}
for k := 0; k < 1000; k++ {
nodes = append(nodes, newNodeID())
nodes = append(nodes, testrand.NodeID())
}
b.ResetTimer()
@ -41,7 +36,7 @@ func BenchmarkSortByXOR(b *testing.B) {
rand.Shuffle(len(nodes), func(i, k int) {
nodes[i], nodes[k] = nodes[k], nodes[i]
})
sortByXOR(nodes, newNodeID())
sortByXOR(nodes, testrand.NodeID())
}
}

View File

@ -5,7 +5,6 @@ package kvmetainfo_test
import (
"context"
"crypto/rand"
"fmt"
"io"
"testing"
@ -15,6 +14,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/metainfo/kvmetainfo"
"storj.io/storj/pkg/storage/streams"
"storj.io/storj/pkg/storj"
@ -115,9 +115,7 @@ func TestGetObject(t *testing.T) {
func TestGetObjectStream(t *testing.T) {
runTest(t, func(t *testing.T, ctx context.Context, planet *testplanet.Planet, db *kvmetainfo.DB, streams streams.Store) {
data := make([]byte, 32*memory.KiB)
_, err := rand.Read(data)
require.NoError(t, err)
data := testrand.Bytes(32 * memory.KiB)
bucket, err := db.CreateBucket(ctx, TestBucket, nil)
require.NoError(t, err)

View File

@ -8,7 +8,6 @@ import (
"errors"
"flag"
"io/ioutil"
"math/rand"
"os"
"testing"
"time"
@ -25,6 +24,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testidentity"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
libuplink "storj.io/storj/lib/uplink"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/identity"
@ -87,9 +87,7 @@ func TestUploadDownload(t *testing.T) {
uplinkCfg.Client.APIKey = "apiKey"
// Encryption key
passphrase := make([]byte, rand.Intn(100)+1)
_, err = rand.Read(passphrase)
require.NoError(t, err)
passphrase := testrand.BytesInt(testrand.Intn(100) + 1)
encryptionKey, err := storj.NewKey(passphrase)
require.NoError(t, err)

View File

@ -5,13 +5,13 @@ package overlay_test
import (
"context"
"math/rand"
"testing"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
@ -33,8 +33,7 @@ func BenchmarkOverlay(b *testing.B) {
var all []storj.NodeID
var check []storj.NodeID
for i := 0; i < TotalNodeCount; i++ {
var id storj.NodeID
_, _ = rand.Read(id[:]) // math/rand never returns error
id := testrand.NodeID()
all = append(all, id)
if i < OnlineCount {
check = append(check, id)
@ -48,9 +47,7 @@ func BenchmarkOverlay(b *testing.B) {
// create random offline node ids to check
for i := 0; i < OfflineCount; i++ {
var id storj.NodeID
_, _ = rand.Read(id[:]) // math/rand never returns error
check = append(check, id)
check = append(check, testrand.NodeID())
}
b.Run("KnownUnreliableOrOffline", func(b *testing.B) {

View File

@ -5,7 +5,6 @@ package overlay_test
import (
"context"
"math/rand"
"testing"
"time"
@ -16,6 +15,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
@ -61,15 +61,11 @@ func testNodeSelectionConfig(auditCount int64, newNodePercentage float64, distin
}
func testCache(ctx context.Context, t *testing.T, store overlay.DB) {
valid1ID := storj.NodeID{}
valid2ID := storj.NodeID{}
missingID := storj.NodeID{}
valid1ID := testrand.NodeID()
valid2ID := testrand.NodeID()
missingID := testrand.NodeID()
address := &pb.NodeAddress{Address: "127.0.0.1:0"}
_, _ = rand.Read(valid1ID[:])
_, _ = rand.Read(valid2ID[:])
_, _ = rand.Read(missingID[:])
nodeSelectionConfig := testNodeSelectionConfig(0, 0, false)
cache := overlay.NewCache(zaptest.NewLogger(t), store, nodeSelectionConfig)
@ -203,8 +199,8 @@ func TestRandomizedSelection(t *testing.T) {
// put nodes in cache
for i := 0; i < totalNodes; i++ {
newID := storj.NodeID{}
_, _ = rand.Read(newID[:])
newID := testrand.NodeID()
err := cache.UpdateAddress(ctx, &pb.Node{Id: newID}, defaults)
require.NoError(t, err)
_, err = cache.UpdateNodeInfo(ctx, newID, &pb.InfoResponse{

View File

@ -5,7 +5,6 @@ package peertls_test
import (
"bytes"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
@ -18,6 +17,7 @@ import (
"github.com/zeebo/errs"
"storj.io/storj/internal/testpeertls"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/peertls"
"storj.io/storj/pkg/peertls/extensions"
"storj.io/storj/pkg/pkcrypto"
@ -185,13 +185,7 @@ func TestAddExtraExtension(t *testing.T) {
cert := chain[0]
extLen := len(cert.Extensions)
randBytes := make([]byte, 10)
_, err = rand.Read(randBytes)
require.NoError(t, err)
if !assert.NoError(t, err) {
t.FailNow()
}
randBytes := testrand.Bytes(10)
ext := pkix.Extension{
Id: asn1.ObjectIdentifier{2, 999, int(randBytes[0])},
Value: randBytes,

View File

@ -6,7 +6,6 @@ package ecclient_test
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
@ -14,7 +13,6 @@ import (
"time"
"github.com/golang/protobuf/ptypes"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vivint/infectious"
@ -22,6 +20,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/auth/signing"
"storj.io/storj/pkg/eestream"
"storj.io/storj/pkg/pb"
@ -58,7 +57,7 @@ func TestECClient(t *testing.T) {
rs, err := eestream.NewRedundancyStrategy(es, 0, 0)
require.NoError(t, err)
data, err := ioutil.ReadAll(io.LimitReader(rand.Reader, dataSize.Int64()))
data, err := ioutil.ReadAll(io.LimitReader(testrand.Reader(), dataSize.Int64()))
require.NoError(t, err)
// Erasure encode some random data and upload the pieces
@ -149,16 +148,14 @@ func testDelete(ctx context.Context, t *testing.T, planet *testplanet.Planet, ec
func newAddressedOrderLimit(ctx context.Context, action pb.PieceAction, satellite *satellite.Peer, uplink *testplanet.Uplink, storageNode *storagenode.Peer, pieceID storj.PieceID) (*pb.AddressedOrderLimit, error) {
// TODO refactor to avoid OrderLimit duplication
serialNumber, err := uuid.New()
if err != nil {
return nil, err
}
serialNumber := testrand.SerialNumber()
orderExpiration, err := ptypes.TimestampProto(time.Now().Add(24 * time.Hour))
if err != nil {
return nil, err
}
limit := &pb.OrderLimit2{
SerialNumber: storj.SerialNumber(*serialNumber),
SerialNumber: serialNumber,
SatelliteId: satellite.ID(),
UplinkId: uplink.ID(),
StorageNodeId: storageNode.ID(),

View File

@ -4,7 +4,6 @@
package segments_test
import (
"math/rand"
"testing"
"time"
@ -14,6 +13,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/pb"
ecclient "storj.io/storj/pkg/storage/ec"
"storj.io/storj/pkg/storage/segments"
@ -34,11 +34,9 @@ func TestSegmentStoreRepair(t *testing.T) {
satellite.Discovery.Service.Discovery.Stop()
satellite.Discovery.Service.Refresh.Stop()
testData := make([]byte, 1*memory.MiB)
_, err := rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(1 * memory.MiB)
err = ul.UploadWithConfig(ctx, satellite, &uplink.RSConfig{
err := ul.UploadWithConfig(ctx, satellite, &uplink.RSConfig{
MinThreshold: 2,
RepairThreshold: 3,
SuccessThreshold: 4,

View File

@ -6,9 +6,7 @@ package segments_test
import (
"bytes"
"context"
"crypto/rand"
"fmt"
io "io"
"io/ioutil"
"strconv"
"testing"
@ -21,6 +19,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/eestream"
"storj.io/storj/pkg/macaroon"
"storj.io/storj/pkg/pb"
@ -85,8 +84,8 @@ func TestSegmentStorePutGet(t *testing.T) {
expiration time.Time
content []byte
}{
{"test inline put/get", "l/path/1", []byte("metadata-intline"), time.Time{}, createTestData(t, 2*memory.KiB.Int64())},
{"test remote put/get", "s0/test-bucket/mypath/1", []byte("metadata-remote"), time.Time{}, createTestData(t, 100*memory.KiB.Int64())},
{"test inline put/get", "l/path/1", []byte("metadata-intline"), time.Time{}, testrand.Bytes(2 * memory.KiB)},
{"test remote put/get", "s0/test-bucket/mypath/1", []byte("metadata-remote"), time.Time{}, testrand.Bytes(100 * memory.KiB)},
} {
test := tt
runTest(t, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, segmentStore segments.Store) {
@ -119,8 +118,8 @@ func TestSegmentStoreDelete(t *testing.T) {
expiration time.Time
content []byte
}{
{"test inline delete", "l/path/1", []byte("metadata"), time.Time{}, createTestData(t, 2*memory.KiB.Int64())},
{"test remote delete", "s0/test-bucket/mypath/1", []byte("metadata"), time.Time{}, createTestData(t, 100*memory.KiB.Int64())},
{"test inline delete", "l/path/1", []byte("metadata"), time.Time{}, testrand.Bytes(2 * memory.KiB)},
{"test remote delete", "s0/test-bucket/mypath/1", []byte("metadata"), time.Time{}, testrand.Bytes(100 * memory.KiB)},
} {
test := tt
runTest(t, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, segmentStore segments.Store) {
@ -235,12 +234,6 @@ func TestCalcNeededNodes(t *testing.T) {
}
}
func createTestData(t *testing.T, size int64) []byte {
data, err := ioutil.ReadAll(io.LimitReader(rand.Reader, size))
require.NoError(t, err)
return data
}
func runTest(t *testing.T, test func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, segmentStore segments.Store)) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,

View File

@ -4,12 +4,12 @@
package storj_test
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
)
@ -33,9 +33,7 @@ func TestNewKey(t *testing.T) {
t.Run("humanReadableKey is of KeySize length", func(t *testing.T) {
t.Parallel()
humanReadableKey := make([]byte, storj.KeySize)
_, err := rand.Read(humanReadableKey)
require.NoError(t, err)
humanReadableKey := testrand.Bytes(storj.KeySize)
key, err := storj.NewKey(humanReadableKey)
require.NoError(t, err)
@ -45,9 +43,7 @@ func TestNewKey(t *testing.T) {
t.Run("humanReadableKey is shorter than KeySize", func(t *testing.T) {
t.Parallel()
humanReadableKey := make([]byte, rand.Intn(storj.KeySize))
_, err := rand.Read(humanReadableKey)
require.NoError(t, err)
humanReadableKey := testrand.BytesInt(testrand.Intn(storj.KeySize))
key, err := storj.NewKey(humanReadableKey)
require.NoError(t, err)
@ -57,9 +53,7 @@ func TestNewKey(t *testing.T) {
t.Run("humanReadableKey is larger than KeySize", func(t *testing.T) {
t.Parallel()
humanReadableKey := make([]byte, rand.Intn(10)+storj.KeySize+1)
_, err := rand.Read(humanReadableKey)
require.NoError(t, err)
humanReadableKey := testrand.BytesInt(testrand.Intn(10) + storj.KeySize + 1)
key, err := storj.NewKey(humanReadableKey)
require.NoError(t, err)
@ -69,9 +63,7 @@ func TestNewKey(t *testing.T) {
t.Run("same human readable key produce the same key", func(t *testing.T) {
t.Parallel()
humanReadableKey := make([]byte, rand.Intn(storj.KeySize)+10)
_, err := rand.Read(humanReadableKey)
require.NoError(t, err)
humanReadableKey := testrand.BytesInt(testrand.Intn(10) + storj.KeySize + 1)
key1, err := storj.NewKey(humanReadableKey)
require.NoError(t, err)

View File

@ -4,7 +4,6 @@
package storj_test
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"testing"
@ -13,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
)
@ -159,10 +159,7 @@ func TestNodeID_UnmarshalJSON(t *testing.T) {
}
func TestNewVersionedID(t *testing.T) {
nodeID := storj.NodeID{}
_, err := rand.Read(nodeID[:])
require.NoError(t, err)
nodeID[storj.NodeIDSize-1] = 0
nodeID := testrand.NodeID()
assert.Equal(t, storj.V0, nodeID.Version().Number)

View File

@ -4,11 +4,11 @@
package storj_test
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
)
@ -20,8 +20,7 @@ func TestSerialNumber_Encode(t *testing.T) {
assert.Error(t, err)
for i := 0; i < 10; i++ {
var serialNumber storj.SerialNumber
_, _ = rand.Read(serialNumber[:])
serialNumber := testrand.SerialNumber()
fromString, err := storj.SerialNumberFromString(serialNumber.String())
assert.NoError(t, err)

View File

@ -13,6 +13,7 @@ import (
"github.com/zeebo/errs"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/accounting"
"storj.io/storj/pkg/pb"
"storj.io/storj/satellite"
@ -67,14 +68,8 @@ func TestDB(t *testing.T) {
attributionDB := db.Attribution()
newUUID := func() uuid.UUID {
v, err := uuid.New()
require.NoError(t, err)
return *v
}
project1, project2 := newUUID(), newUUID()
partner1, partner2 := newUUID(), newUUID()
project1, project2 := testrand.UUID(), testrand.UUID()
partner1, partner2 := testrand.UUID(), testrand.UUID()
infos := []*attribution.Info{
{project1, []byte("alpha"), partner1, time.Time{}},
@ -104,22 +99,16 @@ func TestQueryAttribution(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
newUUID := func() uuid.UUID {
v, err := uuid.New()
require.NoError(t, err)
return *v
}
now := time.Now().UTC()
projectID := newUUID()
partnerID := newUUID()
projectID := testrand.UUID()
partnerID := testrand.UUID()
alphaBucket := []byte("alpha")
betaBucket := []byte("beta")
testData := []AttributionTestData{
{
name: "new partnerID, projectID, alpha",
partnerID: newUUID(),
partnerID: testrand.UUID(),
projectID: projectID,
bucketName: alphaBucket,
@ -134,7 +123,7 @@ func TestQueryAttribution(t *testing.T) {
{
name: "partnerID, new projectID, alpha",
partnerID: partnerID,
projectID: newUUID(),
projectID: testrand.UUID(),
bucketName: alphaBucket,
remoteSize: remoteSize / 2,
@ -147,7 +136,7 @@ func TestQueryAttribution(t *testing.T) {
},
{
name: "new partnerID, projectID, beta",
partnerID: newUUID(),
partnerID: testrand.UUID(),
projectID: projectID,
bucketName: betaBucket,
@ -162,7 +151,7 @@ func TestQueryAttribution(t *testing.T) {
{
name: "partnerID, new projectID, beta",
partnerID: partnerID,
projectID: newUUID(),
projectID: testrand.UUID(),
bucketName: betaBucket,
remoteSize: remoteSize / 4,

View File

@ -7,18 +7,16 @@ import (
"testing"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testrand"
)
func TestClaims(t *testing.T) {
id, err := uuid.New()
assert.Nil(t, err)
assert.NoError(t, err)
assert.NotNil(t, id)
id := testrand.UUID()
claims := Claims{
ID: *id,
ID: id,
Email: "alice@mail.test",
Expiration: time.Now(),
}

View File

@ -4,7 +4,6 @@
package console_test
import (
"crypto/rand"
"testing"
"time"
@ -12,6 +11,7 @@ import (
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
@ -25,9 +25,7 @@ func TestProjectInvoiceStamps(t *testing.T) {
startDate := time.Now().UTC()
endDate := startDate.Add(time.Hour * 24)
var invoiceID [8]byte
_, err := rand.Read(invoiceID[:])
require.NoError(t, err)
invoiceID := testrand.Bytes(8)
//create project
proj, err := consoleDB.Projects().Insert(ctx, &console.Project{
@ -38,14 +36,14 @@ func TestProjectInvoiceStamps(t *testing.T) {
t.Run("create project invoice stamp", func(t *testing.T) {
stamp, err := consoleDB.ProjectInvoiceStamps().Create(ctx, console.ProjectInvoiceStamp{
ProjectID: proj.ID,
InvoiceID: invoiceID[:],
InvoiceID: invoiceID,
StartDate: startDate,
EndDate: endDate,
})
assert.NoError(t, err)
assert.Equal(t, proj.ID, stamp.ProjectID)
assert.Equal(t, invoiceID[:], stamp.InvoiceID)
assert.Equal(t, invoiceID, stamp.InvoiceID)
assert.Equal(t, startDate.Unix(), stamp.StartDate.Unix())
assert.Equal(t, endDate.Unix(), stamp.EndDate.Unix())
})
@ -55,7 +53,7 @@ func TestProjectInvoiceStamps(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, proj.ID, stamp.ProjectID)
assert.Equal(t, invoiceID[:], stamp.InvoiceID)
assert.Equal(t, invoiceID, stamp.InvoiceID)
assert.Equal(t, startDate.Unix(), stamp.StartDate.Unix())
assert.Equal(t, endDate.Unix(), stamp.EndDate.Unix())
})

View File

@ -7,10 +7,10 @@ import (
"context"
"testing"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
@ -29,19 +29,17 @@ func TestProjectMembersRepository(t *testing.T) {
createdUsers, createdProjects := prepareUsersAndProjects(ctx, t, users, projects)
t.Run("Can't insert projectMember without memberID", func(t *testing.T) {
unexistingUserID, err := uuid.New()
assert.NoError(t, err)
missingUserID := testrand.UUID()
projMember, err := projectMembers.Insert(ctx, *unexistingUserID, createdProjects[0].ID)
projMember, err := projectMembers.Insert(ctx, missingUserID, createdProjects[0].ID)
assert.Nil(t, projMember)
assert.Error(t, err)
})
t.Run("Can't insert projectMember without projectID", func(t *testing.T) {
unexistingProjectID, err := uuid.New()
assert.NoError(t, err)
missingProjectID := testrand.UUID()
projMember, err := projectMembers.Insert(ctx, createdUsers[0].ID, *unexistingProjectID)
projMember, err := projectMembers.Insert(ctx, createdUsers[0].ID, missingProjectID)
assert.Nil(t, projMember)
assert.Error(t, err)
})

View File

@ -4,13 +4,13 @@
package console_test
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
@ -21,23 +21,15 @@ func TestProjectPaymentInfos(t *testing.T) {
ctx := testcontext.New(t)
consoleDB := db.Console()
var customerID [8]byte
_, err := rand.Read(customerID[:])
require.NoError(t, err)
var paymentMethodID [8]byte
_, err = rand.Read(paymentMethodID[:])
require.NoError(t, err)
var passHash [8]byte
_, err = rand.Read(passHash[:])
require.NoError(t, err)
customerID := testrand.Bytes(8)
paymentMethodID := testrand.Bytes(8)
passHash := testrand.Bytes(8)
// create user
user, err := consoleDB.Users().Insert(ctx, &console.User{
FullName: "John Doe",
Email: "john@mail.test",
PasswordHash: passHash[:],
PasswordHash: passHash,
Status: console.Active,
})
require.NoError(t, err)
@ -45,7 +37,7 @@ func TestProjectPaymentInfos(t *testing.T) {
// create user payment info
userPmInfo, err := consoleDB.UserPayments().Create(ctx, console.UserPayment{
UserID: user.ID,
CustomerID: customerID[:],
CustomerID: customerID,
})
require.NoError(t, err)
@ -59,13 +51,13 @@ func TestProjectPaymentInfos(t *testing.T) {
info, err := consoleDB.ProjectPayments().Create(ctx, console.ProjectPayment{
ProjectID: proj.ID,
PayerID: userPmInfo.UserID,
PaymentMethodID: paymentMethodID[:],
PaymentMethodID: paymentMethodID,
})
assert.NoError(t, err)
assert.Equal(t, proj.ID, info.ProjectID)
assert.Equal(t, userPmInfo.UserID, info.PayerID)
assert.Equal(t, paymentMethodID[:], info.PaymentMethodID)
assert.Equal(t, paymentMethodID, info.PaymentMethodID)
})
t.Run("get by project id", func(t *testing.T) {
@ -74,7 +66,7 @@ func TestProjectPaymentInfos(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, proj.ID, info.ProjectID)
assert.Equal(t, userPmInfo.UserID, info.PayerID)
assert.Equal(t, paymentMethodID[:], info.PaymentMethodID)
assert.Equal(t, paymentMethodID, info.PaymentMethodID)
})
t.Run("get by payer id", func(t *testing.T) {
@ -83,7 +75,7 @@ func TestProjectPaymentInfos(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, proj.ID, info.ProjectID)
assert.Equal(t, userPmInfo.UserID, info.PayerID)
assert.Equal(t, paymentMethodID[:], info.PaymentMethodID)
assert.Equal(t, paymentMethodID, info.PaymentMethodID)
})
})
}

View File

@ -9,10 +9,10 @@ import (
"testing"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/accounting"
"storj.io/storj/pkg/pb"
"storj.io/storj/satellite"
@ -35,15 +35,8 @@ func TestUsageRollups(t *testing.T) {
now := time.Now()
start := now.Add(tallyInterval * time.Duration(-tallyIntervals))
project1, err := uuid.New()
if err != nil {
t.Fatal(err)
}
project2, err := uuid.New()
if err != nil {
t.Fatal(err)
}
project1 := testrand.UUID()
project2 := testrand.UUID()
p1base := binary.BigEndian.Uint64(project1[:8]) >> 48
p2base := binary.BigEndian.Uint64(project2[:8]) >> 48
@ -69,7 +62,7 @@ func TestUsageRollups(t *testing.T) {
value := getValue(0, i, p1base)
err := db.Orders().UpdateBucketBandwidthAllocation(ctx,
*project1,
project1,
[]byte(bucketName),
action,
value*6,
@ -80,7 +73,7 @@ func TestUsageRollups(t *testing.T) {
}
err = db.Orders().UpdateBucketBandwidthSettle(ctx,
*project1,
project1,
[]byte(bucketName),
action,
value*3,
@ -91,7 +84,7 @@ func TestUsageRollups(t *testing.T) {
}
err = db.Orders().UpdateBucketBandwidthInline(ctx,
*project1,
project1,
[]byte(bucketName),
action,
value,
@ -107,7 +100,7 @@ func TestUsageRollups(t *testing.T) {
value := getValue(1, i, p2base)
err := db.Orders().UpdateBucketBandwidthAllocation(ctx,
*project2,
project2,
[]byte(bucketName),
action,
value*6,
@ -118,7 +111,7 @@ func TestUsageRollups(t *testing.T) {
}
err = db.Orders().UpdateBucketBandwidthSettle(ctx,
*project2,
project2,
[]byte(bucketName),
action,
value*3,
@ -129,7 +122,7 @@ func TestUsageRollups(t *testing.T) {
}
err = db.Orders().UpdateBucketBandwidthInline(ctx,
*project2,
project2,
[]byte(bucketName),
action,
value,
@ -199,21 +192,21 @@ func TestUsageRollups(t *testing.T) {
usageRollups := db.Console().UsageRollups()
t.Run("test project total", func(t *testing.T) {
projTotal1, err := usageRollups.GetProjectTotal(ctx, *project1, start, now)
projTotal1, err := usageRollups.GetProjectTotal(ctx, project1, start, now)
assert.NoError(t, err)
assert.NotNil(t, projTotal1)
projTotal2, err := usageRollups.GetProjectTotal(ctx, *project2, start, now)
projTotal2, err := usageRollups.GetProjectTotal(ctx, project2, start, now)
assert.NoError(t, err)
assert.NotNil(t, projTotal2)
})
t.Run("test bucket usage rollups", func(t *testing.T) {
rollups1, err := usageRollups.GetBucketUsageRollups(ctx, *project1, start, now)
rollups1, err := usageRollups.GetBucketUsageRollups(ctx, project1, start, now)
assert.NoError(t, err)
assert.NotNil(t, rollups1)
rollups2, err := usageRollups.GetBucketUsageRollups(ctx, *project2, start, now)
rollups2, err := usageRollups.GetBucketUsageRollups(ctx, project2, start, now)
assert.NoError(t, err)
assert.NotNil(t, rollups2)
})
@ -224,11 +217,11 @@ func TestUsageRollups(t *testing.T) {
Page: 1,
}
totals1, err := usageRollups.GetBucketTotals(ctx, *project1, cursor, start, now)
totals1, err := usageRollups.GetBucketTotals(ctx, project1, cursor, start, now)
assert.NoError(t, err)
assert.NotNil(t, totals1)
totals2, err := usageRollups.GetBucketTotals(ctx, *project2, cursor, start, now)
totals2, err := usageRollups.GetBucketTotals(ctx, project2, cursor, start, now)
assert.NoError(t, err)
assert.NotNil(t, totals2)
})

View File

@ -4,13 +4,13 @@
package console_test
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
@ -21,19 +21,14 @@ func TestUserPaymentInfos(t *testing.T) {
ctx := testcontext.New(t)
consoleDB := db.Console()
var customerID [8]byte
_, err := rand.Read(customerID[:])
require.NoError(t, err)
var passHash [8]byte
_, err = rand.Read(passHash[:])
require.NoError(t, err)
customerID := testrand.Bytes(8)
passHash := testrand.Bytes(8)
// create user
user, err := consoleDB.Users().Insert(ctx, &console.User{
FullName: "John Doe",
Email: "john@mail.test",
PasswordHash: passHash[:],
PasswordHash: passHash,
Status: console.Active,
})
require.NoError(t, err)
@ -41,12 +36,12 @@ func TestUserPaymentInfos(t *testing.T) {
t.Run("create user payment info", func(t *testing.T) {
info, err := consoleDB.UserPayments().Create(ctx, console.UserPayment{
UserID: user.ID,
CustomerID: customerID[:],
CustomerID: customerID,
})
assert.NoError(t, err)
assert.Equal(t, user.ID, info.UserID)
assert.Equal(t, customerID[:], info.CustomerID)
assert.Equal(t, customerID, info.CustomerID)
})
t.Run("get user payment info", func(t *testing.T) {
@ -54,7 +49,7 @@ func TestUserPaymentInfos(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, user.ID, info.UserID)
assert.Equal(t, customerID[:], info.CustomerID)
assert.Equal(t, customerID, info.CustomerID)
})
})
}

View File

@ -7,10 +7,10 @@ import (
"testing"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/satellitedb/satellitedbtest"
@ -36,11 +36,8 @@ func TestUserRepository(t *testing.T) {
repository := db.Console().Users()
t.Run("User insertion success", func(t *testing.T) {
id, err := uuid.New()
assert.NoError(t, err)
user := &console.User{
ID: *id,
ID: testrand.UUID(),
FullName: name,
ShortName: lastName,
Email: email,

View File

@ -6,7 +6,6 @@ package inspector_test
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"strings"
"testing"
@ -16,6 +15,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/eestream"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
@ -27,13 +27,11 @@ func TestInspectorStats(t *testing.T) {
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
uplink := planet.Uplinks[0]
testData := make([]byte, 1*memory.MiB)
_, err := rand.Read(testData)
require.NoError(t, err)
testData := testrand.Bytes(1 * memory.MiB)
bucket := "testbucket"
err = uplink.Upload(ctx, planet.Satellites[0], bucket, "test/path", testData)
err := uplink.Upload(ctx, planet.Satellites[0], bucket, "test/path", testData)
require.NoError(t, err)
healthEndpoint := planet.Satellites[0].Inspector.Endpoint

View File

@ -10,7 +10,6 @@ import (
"time"
"github.com/golang/protobuf/ptypes"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zeebo/errs"
@ -21,6 +20,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/macaroon"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
@ -569,16 +569,14 @@ func TestSetAttribution(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(metainfoClient.Close)
partnerID, err := uuid.New()
require.NoError(t, err)
partnerID := testrand.UUID()
{
// bucket with no items
err = metainfoClient.SetAttribution(ctx, "alpha", *partnerID)
err = metainfoClient.SetAttribution(ctx, "alpha", partnerID)
require.NoError(t, err)
// no bucket exists
err = metainfoClient.SetAttribution(ctx, "beta", *partnerID)
err = metainfoClient.SetAttribution(ctx, "beta", partnerID)
require.NoError(t, err)
}
{
@ -586,7 +584,7 @@ func TestSetAttribution(t *testing.T) {
assert.NoError(t, err)
// bucket with items
err = metainfoClient.SetAttribution(ctx, "alpha", *partnerID)
err = metainfoClient.SetAttribution(ctx, "alpha", partnerID)
require.Error(t, err)
}
})

View File

@ -4,7 +4,6 @@
package orders_test
import (
"crypto/rand"
"testing"
"time"
@ -15,6 +14,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
"storj.io/storj/satellite/orders"
"storj.io/storj/uplink"
@ -30,12 +30,10 @@ func TestSendingReceivingOrders(t *testing.T) {
storageNode.Storage2.Sender.Loop.Pause()
}
expectedData := make([]byte, 50*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(50 * memory.KiB)
redundancy := noLongTailRedundancy(planet)
err = planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &redundancy, "testbucket", "test/path", expectedData)
err := planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &redundancy, "testbucket", "test/path", expectedData)
require.NoError(t, err)
sumBeforeSend := 0
@ -76,12 +74,10 @@ func TestUnableToSendOrders(t *testing.T) {
storageNode.Storage2.Sender.Loop.Pause()
}
expectedData := make([]byte, 50*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(50 * memory.KiB)
redundancy := noLongTailRedundancy(planet)
err = planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &redundancy, "testbucket", "test/path", expectedData)
err := planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &redundancy, "testbucket", "test/path", expectedData)
require.NoError(t, err)
sumBeforeSend := 0
@ -125,12 +121,10 @@ func TestUploadDownloadBandwidth(t *testing.T) {
storageNode.Storage2.Sender.Loop.Pause()
}
expectedData := make([]byte, 50*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(50 * memory.KiB)
redundancy := noLongTailRedundancy(planet)
err = planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &redundancy, "testbucket", "test/path", expectedData)
err := planet.Uplinks[0].UploadWithConfig(ctx, planet.Satellites[0], &redundancy, "testbucket", "test/path", expectedData)
require.NoError(t, err)
data, err := planet.Uplinks[0].Download(ctx, planet.Satellites[0], "testbucket", "test/path")

View File

@ -5,14 +5,13 @@ package satellitedb_test
import (
"context"
"crypto/rand"
"testing"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/rewards"
@ -29,13 +28,12 @@ func TestUsercredits(t *testing.T) {
consoleDB := db.Console()
user, referrer, offer := setupData(ctx, t, db)
randomID, err := uuid.New()
require.NoError(t, err)
randomID := testrand.UUID()
// test foreign key constraint for inserting a new user credit entry with randomID
var invalidUserCredits = []console.UserCredit{
{
UserID: *randomID,
UserID: randomID,
OfferID: offer.ID,
ReferredBy: referrer.ID,
CreditsEarnedInCents: 100,
@ -51,7 +49,7 @@ func TestUsercredits(t *testing.T) {
{
UserID: user.ID,
OfferID: offer.ID,
ReferredBy: *randomID,
ReferredBy: randomID,
CreditsEarnedInCents: 100,
ExpiresAt: time.Now().UTC().AddDate(0, 1, 0),
},
@ -135,7 +133,7 @@ func TestUsercredits(t *testing.T) {
}
for i, vc := range validUserCredits {
_, err = consoleDB.UserCredits().Create(ctx, vc.userCredit)
_, err := consoleDB.UserCredits().Create(ctx, vc.userCredit)
require.NoError(t, err)
{
@ -166,20 +164,18 @@ func TestUsercredits(t *testing.T) {
func setupData(ctx context.Context, t *testing.T, db satellite.DB) (user *console.User, referrer *console.User, offer *rewards.Offer) {
consoleDB := db.Console()
offersDB := db.Rewards()
// create user
var userPassHash [8]byte
_, err := rand.Read(userPassHash[:])
require.NoError(t, err)
var referrerPassHash [8]byte
_, err = rand.Read(referrerPassHash[:])
require.NoError(t, err)
// create user
userPassHash := testrand.Bytes(8)
referrerPassHash := testrand.Bytes(8)
var err error
// create an user
user, err = consoleDB.Users().Insert(ctx, &console.User{
FullName: "John Doe",
Email: "john@mail.test",
PasswordHash: userPassHash[:],
PasswordHash: userPassHash,
Status: console.Active,
})
require.NoError(t, err)
@ -188,7 +184,7 @@ func setupData(ctx context.Context, t *testing.T, db satellite.DB) (user *consol
referrer, err = consoleDB.Users().Insert(ctx, &console.User{
FullName: "referrer",
Email: "referrer@mail.test",
PasswordHash: referrerPassHash[:],
PasswordHash: referrerPassHash,
Status: console.Active,
})
require.NoError(t, err)

View File

@ -4,14 +4,13 @@
package satellitedb
import (
"crypto/rand"
"testing"
"github.com/lib/pq"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
)
@ -27,19 +26,17 @@ func TestBytesToUUID(t *testing.T) {
})
t.Run("Valid input", func(t *testing.T) {
id, err := uuid.New()
assert.NoError(t, err)
id := testrand.UUID()
result, err := bytesToUUID(id[:])
assert.NoError(t, err)
assert.Equal(t, result, *id)
assert.Equal(t, result, id)
})
}
func TestPostgresNodeIDsArray(t *testing.T) {
ids := make(storj.NodeIDList, 10)
for i := range ids {
_, _ = rand.Read(ids[i][:])
ids[i] = testrand.NodeID()
}
got, err := postgresNodeIDList(ids).Value() // returns a []byte

View File

@ -7,7 +7,6 @@ import (
"errors"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
@ -15,16 +14,11 @@ import (
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/storage"
"storj.io/storj/storage/filestore"
)
func randomValue() []byte {
var id [32]byte
_, _ = rand.Read(id[:])
return id[:]
}
func TestStoreLoad(t *testing.T) {
const blobSize = 8 << 10
const repeatCount = 16
@ -35,18 +29,18 @@ func TestStoreLoad(t *testing.T) {
store, err := filestore.NewAt(ctx.Dir("store"))
require.NoError(t, err)
data := make([]byte, blobSize)
data := testrand.Bytes(blobSize)
temp := make([]byte, len(data))
_, _ = rand.Read(data)
refs := []storage.BlobRef{}
namespace := randomValue()
namespace := testrand.Bytes(32)
// store without size
for i := 0; i < repeatCount; i++ {
ref := storage.BlobRef{
Namespace: namespace,
Key: randomValue(),
Key: testrand.Bytes(32),
}
refs = append(refs, ref)
@ -64,12 +58,12 @@ func TestStoreLoad(t *testing.T) {
require.Error(t, writer.Commit(ctx))
}
namespace = randomValue()
namespace = testrand.Bytes(32)
// store with size
for i := 0; i < repeatCount; i++ {
ref := storage.BlobRef{
Namespace: namespace,
Key: randomValue(),
Key: testrand.Bytes(32),
}
refs = append(refs, ref)
@ -83,12 +77,12 @@ func TestStoreLoad(t *testing.T) {
require.NoError(t, writer.Commit(ctx))
}
namespace = randomValue()
namespace = testrand.Bytes(32)
// store with larger size
{
ref := storage.BlobRef{
Namespace: namespace,
Key: randomValue(),
Key: testrand.Bytes(32),
}
refs = append(refs, ref)
@ -102,12 +96,12 @@ func TestStoreLoad(t *testing.T) {
require.NoError(t, writer.Commit(ctx))
}
namespace = randomValue()
namespace = testrand.Bytes(32)
// store with error
{
ref := storage.BlobRef{
Namespace: namespace,
Key: randomValue(),
Key: testrand.Bytes(32),
}
writer, err := store.Create(ctx, ref, -1)
@ -164,8 +158,7 @@ func TestDeleteWhileReading(t *testing.T) {
store, err := filestore.NewAt(ctx.Dir("store"))
require.NoError(t, err)
data := make([]byte, blobSize)
_, _ = rand.Read(data)
data := testrand.Bytes(blobSize)
ref := storage.BlobRef{
Namespace: []byte{0},

View File

@ -4,7 +4,6 @@
package collector_test
import (
"crypto/rand"
"testing"
"time"
@ -13,6 +12,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
"storj.io/storj/uplink"
)
@ -28,12 +28,10 @@ func TestCollector(t *testing.T) {
storageNode.Storage2.Sender.Loop.Pause()
}
expectedData := make([]byte, 100*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(100 * memory.KiB)
// upload some data to exactly 2 nodes that expires in 8 days
err = planet.Uplinks[0].UploadWithExpirationAndConfig(ctx,
err := planet.Uplinks[0].UploadWithExpirationAndConfig(ctx,
planet.Satellites[0],
&uplink.RSConfig{
MinThreshold: 1,

View File

@ -4,7 +4,6 @@
package inspector_test
import (
"math/rand"
"testing"
"time"
@ -15,6 +14,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/pb"
"storj.io/storj/uplink"
)
@ -49,9 +49,7 @@ func TestInspectorStats(t *testing.T) {
availableSpace = response.AvailableSpace
}
expectedData := make([]byte, 100*memory.KiB)
_, err = rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(100 * memory.KiB)
rs := &uplink.RSConfig{
MinThreshold: 2,
@ -109,11 +107,9 @@ func TestInspectorDashboard(t *testing.T) {
assert.NotNil(t, response.Stats)
}
expectedData := make([]byte, 100*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(100 * memory.KiB)
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
err := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
require.NoError(t, err)
for _, storageNode := range planet.StorageNodes {

View File

@ -4,7 +4,6 @@
package monitor_test
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
@ -13,6 +12,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/pb"
)
@ -31,11 +31,9 @@ func TestMonitor(t *testing.T) {
freeBandwidth = info.Capacity.FreeBandwidth
}
expectedData := make([]byte, 100*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(100 * memory.KiB)
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
err := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
require.NoError(t, err)
nodeAssertions := 0

View File

@ -4,7 +4,6 @@
package orders_test
import (
"crypto/rand"
"testing"
"github.com/golang/protobuf/ptypes"
@ -13,6 +12,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testidentity"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/auth/signing"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
@ -35,7 +35,7 @@ func TestOrders(t *testing.T) {
uplink := testidentity.MustPregeneratedSignedIdentity(3, storj.LatestIDVersion())
piece := storj.NewPieceID()
serialNumber := newRandomSerial()
serialNumber := testrand.SerialNumber()
// basic test
emptyUnsent, err := ordersdb.ListUnsent(ctx, 100)
@ -127,10 +127,3 @@ func TestOrders(t *testing.T) {
})
}
// TODO: move somewhere better
func newRandomSerial() storj.SerialNumber {
var serial storj.SerialNumber
_, _ = rand.Read(serial[:])
return serial
}

View File

@ -6,7 +6,6 @@ package pieces_test
import (
"bytes"
"io"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
@ -15,6 +14,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testidentity"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/pkcrypto"
"storj.io/storj/pkg/storj"
"storj.io/storj/storage/filestore"
@ -36,8 +36,7 @@ func TestPieces(t *testing.T) {
satelliteID := testidentity.MustPregeneratedSignedIdentity(0, storj.LatestIDVersion()).ID
pieceID := storj.NewPieceID()
source := make([]byte, 8000)
_, _ = rand.Read(source)
source := testrand.Bytes(8000)
{ // write data
writer, err := store.Writer(ctx, satelliteID, pieceID)

View File

@ -5,7 +5,6 @@ package piecestore_test
import (
"io"
"math/rand"
"strings"
"testing"
"time"
@ -17,6 +16,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/auth/signing"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/pkcrypto"
@ -29,11 +29,9 @@ func TestUploadAndPartialDownload(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
expectedData := make([]byte, 100*memory.KiB)
_, err := rand.Read(expectedData)
require.NoError(t, err)
expectedData := testrand.Bytes(100 * memory.KiB)
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
err := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "test/path", expectedData)
assert.NoError(t, err)
var totalDownload int64
@ -126,13 +124,9 @@ func TestUpload(t *testing.T) {
err: "expected put or put repair action got GET",
},
} {
data := make([]byte, tt.contentLength.Int64())
_, _ = rand.Read(data)
data := testrand.Bytes(tt.contentLength)
expectedHash := pkcrypto.SHA256Hash(data)
var serialNumber storj.SerialNumber
_, _ = rand.Read(serialNumber[:])
serialNumber := testrand.SerialNumber()
orderLimit := GenerateOrderLimit(
t,
@ -186,11 +180,8 @@ func TestDownload(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(client.Close)
var serialNumber storj.SerialNumber
_, _ = rand.Read(serialNumber[:])
expectedData := make([]byte, 10*memory.KiB)
_, _ = rand.Read(expectedData)
expectedData := testrand.Bytes(10 * memory.KiB)
serialNumber := testrand.SerialNumber()
orderLimit := GenerateOrderLimit(
t,
@ -237,8 +228,7 @@ func TestDownload(t *testing.T) {
errs: []string{"expected get or get repair or audit action got PUT"},
},
} {
var serialNumber storj.SerialNumber
_, _ = rand.Read(serialNumber[:])
serialNumber := testrand.SerialNumber()
orderLimit := GenerateOrderLimit(
t,
@ -293,11 +283,8 @@ func TestDelete(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(client.Close)
var serialNumber storj.SerialNumber
_, _ = rand.Read(serialNumber[:])
expectedData := make([]byte, 10*memory.KiB)
_, _ = rand.Read(expectedData)
expectedData := testrand.Bytes(10 * memory.KiB)
serialNumber := testrand.SerialNumber()
orderLimit := GenerateOrderLimit(
t,
@ -345,8 +332,7 @@ func TestDelete(t *testing.T) {
err: "expected delete action got GET",
},
} {
var serialNumber storj.SerialNumber
_, _ = rand.Read(serialNumber[:])
serialNumber := testrand.SerialNumber()
orderLimit := GenerateOrderLimit(
t,

View File

@ -4,7 +4,6 @@
package piecestore_test
import (
"math/rand"
"testing"
"time"
@ -14,6 +13,7 @@ import (
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testidentity"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
"storj.io/storj/storagenode"
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
@ -29,9 +29,9 @@ func TestUsedSerials(t *testing.T) {
node0 := testidentity.MustPregeneratedIdentity(0, storj.LatestIDVersion())
node1 := testidentity.MustPregeneratedIdentity(1, storj.LatestIDVersion())
serial1 := newRandomSerial()
serial2 := newRandomSerial()
serial3 := newRandomSerial()
serial1 := testrand.SerialNumber()
serial2 := testrand.SerialNumber()
serial3 := testrand.SerialNumber()
now := time.Now()
@ -66,7 +66,7 @@ func TestUsedSerials(t *testing.T) {
// duplicate adds should fail
for _, serial := range serialNumbers {
expirationDelta := time.Duration(rand.Intn(10)-5) * time.Hour
expirationDelta := time.Duration(testrand.Intn(10)-5) * time.Hour
err = usedSerials.Add(ctx, serial.SatelliteID, serial.SerialNumber, serial.Expiration.Add(expirationDelta))
assert.Error(t, err)
}
@ -98,10 +98,3 @@ func TestUsedSerials(t *testing.T) {
}, listedAfterDelete))
})
}
// TODO: move somewhere better
func newRandomSerial() storj.SerialNumber {
var serial storj.SerialNumber
_, _ = rand.Read(serial[:])
return serial
}

View File

@ -6,7 +6,6 @@ package piecestore_test
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
@ -16,6 +15,7 @@ import (
"storj.io/storj/internal/memory"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testplanet"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/auth/signing"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/pb"
@ -162,7 +162,7 @@ func TestOrderLimitPutValidation(t *testing.T) {
var writeErr error
buffer := make([]byte, memory.KiB)
for i := 0; i < 10; i++ {
_, _ = rand.Read(buffer)
testrand.Read(buffer)
_, writeErr = uploader.Write(buffer)
if writeErr != nil {
break
@ -224,8 +224,7 @@ func TestOrderLimitGetValidation(t *testing.T) {
uploader, err := client.Upload(ctx, orderLimit)
require.NoError(t, err)
data := make([]byte, defaultPieceSize)
_, _ = rand.Read(data)
data := testrand.Bytes(defaultPieceSize)
_, err = uploader.Write(data)
require.NoError(t, err)

View File

@ -5,13 +5,13 @@ package uplink_test
import (
"io/ioutil"
"math/rand"
"os"
"testing"
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
"storj.io/storj/uplink"
)
@ -29,9 +29,7 @@ func TestLoadEncryptionKey(t *testing.T) {
}
t.Run("ok: reading from file", func(t *testing.T) {
passphrase := make([]byte, rand.Intn(100)+1)
_, err := rand.Read(passphrase)
require.NoError(t, err)
passphrase := testrand.BytesInt(1 + testrand.Intn(100))
expectedKey, err := storj.NewKey(passphrase)
require.NoError(t, err)

View File

@ -5,7 +5,6 @@ package uplink_test
import (
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
@ -14,17 +13,14 @@ import (
"github.com/stretchr/testify/require"
"storj.io/storj/internal/testcontext"
"storj.io/storj/internal/testrand"
"storj.io/storj/pkg/storj"
"storj.io/storj/uplink"
)
func TestSaveEncryptionKey(t *testing.T) {
generateInputKey := func() string {
inputKey := make([]byte, rand.Intn(storj.KeySize*3)+1)
_, err := rand.Read(inputKey)
require.NoError(t, err)
return string(inputKey)
return string(testrand.BytesInt(testrand.Intn(storj.KeySize*3) + 1))
}
t.Run("ok", func(t *testing.T) {