2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-09 23:05:42 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
package audit_test
|
2019-01-18 13:54:08 +00:00
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-03-23 10:52:51 +00:00
|
|
|
"time"
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-03-23 10:52:51 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2019-01-15 16:08:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-01-15 16:08:45 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/internal/testplanet"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2019-01-23 19:58:44 +00:00
|
|
|
"storj.io/storj/pkg/audit"
|
2018-10-05 14:34:15 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-04-25 09:46:32 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2018-10-05 14:34:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuditSegment(t *testing.T) {
|
2019-02-05 15:45:09 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
type pathCount struct {
|
|
|
|
path storj.Path
|
|
|
|
count int
|
|
|
|
}
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-02-05 15:45:09 +00:00
|
|
|
// note: to simulate better,
|
|
|
|
// change limit in library to 5 in
|
|
|
|
// list api call, default is 0 == 1000 listing
|
2019-04-25 16:43:26 +01:00
|
|
|
//populate metainfo with 10 non-expired pointers of test data
|
|
|
|
tests, cursor, metainfo := populateTestData(t, planet, ×tamp.Timestamp{Seconds: time.Now().Unix() + 3000})
|
2019-02-05 15:45:09 +00:00
|
|
|
|
|
|
|
t.Run("NextStripe", func(t *testing.T) {
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.bm, func(t *testing.T) {
|
|
|
|
stripe, err := cursor.NextStripe(ctx)
|
|
|
|
if err != nil {
|
2019-04-01 22:27:07 +01:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, stripe)
|
2019-04-25 16:43:26 +01:00
|
|
|
} else {
|
|
|
|
require.NotNil(t, stripe)
|
2019-02-05 15:45:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-02-05 15:45:09 +00:00
|
|
|
// test to see how random paths are
|
|
|
|
t.Run("probabilisticTest", func(t *testing.T) {
|
2019-04-25 16:43:26 +01:00
|
|
|
list, _, err := metainfo.List("", "", "", true, 10, meta.None)
|
2019-02-05 15:45:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, list, 10)
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-02-05 15:45:09 +00:00
|
|
|
// get count of items picked at random
|
|
|
|
uniquePathCounted := []pathCount{}
|
|
|
|
pathCounter := []pathCount{}
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-02-05 15:45:09 +00:00
|
|
|
// 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))))
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
2019-02-05 15:45:09 +00:00
|
|
|
t.Error("num error: failed to get num")
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
2019-02-05 15:45:09 +00:00
|
|
|
pointerItem := list[randomNum.Int64()]
|
|
|
|
path := pointerItem.Path
|
|
|
|
val := pathCount{path: path, count: 1}
|
|
|
|
pathCounter = append(pathCounter, val)
|
|
|
|
}
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-02-05 15:45:09 +00:00
|
|
|
// get a count for paths in list
|
|
|
|
for _, pc := range pathCounter {
|
|
|
|
skip := false
|
|
|
|
for i, up := range uniquePathCounted {
|
|
|
|
if reflect.DeepEqual(pc.path, up.path) {
|
|
|
|
up.count = up.count + 1
|
|
|
|
uniquePathCounted[i] = up
|
|
|
|
skip = true
|
|
|
|
break
|
|
|
|
}
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
2019-02-05 15:45:09 +00:00
|
|
|
if !skip {
|
|
|
|
uniquePathCounted = append(uniquePathCounted, pc)
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 15:45:09 +00:00
|
|
|
// Section: binomial test for randomness
|
|
|
|
n := float64(100) // events
|
|
|
|
p := float64(.10) // theoretical probability of getting 1/10 paths
|
|
|
|
m := n * p
|
|
|
|
s := math.Sqrt(m * (1 - p)) // binomial distribution
|
|
|
|
|
|
|
|
// if values fall outside of the critical values of test statistics (ie Z value)
|
|
|
|
// in a 2-tail test
|
|
|
|
// we can assume, 95% confidence, it's not sampling according to a 10% probability
|
|
|
|
for _, v := range uniquePathCounted {
|
|
|
|
z := (float64(v.count) - m) / s
|
|
|
|
if z <= -1.96 || z >= 1.96 {
|
|
|
|
t.Log(false)
|
|
|
|
} else {
|
|
|
|
t.Log(true)
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 15:45:09 +00:00
|
|
|
})
|
2018-10-05 14:34:15 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-23 10:52:51 +00:00
|
|
|
func TestDeleteExpired(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 4, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-04-25 09:46:32 +01:00
|
|
|
//populate metainfo with 10 expired pointers of test data
|
2019-04-25 16:43:26 +01:00
|
|
|
_, cursor, metainfo := populateTestData(t, planet, ×tamp.Timestamp{})
|
2019-03-23 10:52:51 +00:00
|
|
|
//make sure it they're in there
|
2019-04-25 09:46:32 +01:00
|
|
|
list, _, err := metainfo.List("", "", "", true, 10, meta.None)
|
2019-03-23 10:52:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, list, 10)
|
2019-04-25 16:43:26 +01:00
|
|
|
// make sure an error and no pointer is returned
|
2019-03-23 10:52:51 +00:00
|
|
|
t.Run("NextStripe", func(t *testing.T) {
|
2019-04-25 16:43:26 +01:00
|
|
|
stripe, err := cursor.NextStripe(ctx)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, stripe)
|
2019-03-23 10:52:51 +00:00
|
|
|
})
|
|
|
|
//make sure it they're not in there anymore
|
2019-04-25 09:46:32 +01:00
|
|
|
list, _, err = metainfo.List("", "", "", true, 10, meta.None)
|
2019-03-23 10:52:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, list, 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type testData struct {
|
|
|
|
bm string
|
|
|
|
path storj.Path
|
|
|
|
}
|
|
|
|
|
2019-04-25 09:46:32 +01:00
|
|
|
func populateTestData(t *testing.T, planet *testplanet.Planet, expiration *timestamp.Timestamp) ([]testData, *audit.Cursor, *metainfo.Service) {
|
2019-03-23 10:52:51 +00:00
|
|
|
tests := []testData{
|
|
|
|
{bm: "success-1", path: "folder1/file1"},
|
|
|
|
{bm: "success-2", path: "foodFolder1/file1/file2"},
|
|
|
|
{bm: "success-3", path: "foodFolder1/file1/file2/foodFolder2/file3"},
|
|
|
|
{bm: "success-4", path: "projectFolder/project1.txt/"},
|
|
|
|
{bm: "success-5", path: "newProjectFolder/project2.txt"},
|
|
|
|
{bm: "success-6", path: "Pictures/image1.png"},
|
|
|
|
{bm: "success-7", path: "Pictures/Nature/mountains.png"},
|
|
|
|
{bm: "success-8", path: "Pictures/City/streets.png"},
|
|
|
|
{bm: "success-9", path: "Pictures/Animals/Dogs/dogs.png"},
|
|
|
|
{bm: "success-10", path: "Nada/ビデオ/😶"},
|
|
|
|
}
|
2019-04-25 09:46:32 +01:00
|
|
|
metainfo := planet.Satellites[0].Metainfo.Service
|
|
|
|
cursor := audit.NewCursor(metainfo)
|
2019-03-23 10:52:51 +00:00
|
|
|
|
|
|
|
// put 10 pointers in db with expirations
|
|
|
|
t.Run("putToDB", func(t *testing.T) {
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.bm, func(t *testing.T) {
|
2019-03-30 11:21:49 +00:00
|
|
|
pointer := makePointer(tt.path, expiration)
|
2019-04-25 09:46:32 +01:00
|
|
|
require.NoError(t, metainfo.Put(tt.path, pointer))
|
2019-03-23 10:52:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2019-04-25 09:46:32 +01:00
|
|
|
return tests, cursor, metainfo
|
2019-03-23 10:52:51 +00:00
|
|
|
}
|
2019-03-30 11:21:49 +00:00
|
|
|
|
|
|
|
func makePointer(path storj.Path, expiration *timestamp.Timestamp) *pb.Pointer {
|
|
|
|
var rps []*pb.RemotePiece
|
|
|
|
rps = append(rps, &pb.RemotePiece{
|
|
|
|
PieceNum: 1,
|
|
|
|
NodeId: teststorj.NodeIDFromString("testId"),
|
|
|
|
})
|
|
|
|
return &pb.Pointer{
|
|
|
|
ExpirationDate: expiration,
|
|
|
|
Type: pb.Pointer_REMOTE,
|
|
|
|
Remote: &pb.RemoteSegment{
|
|
|
|
Redundancy: &pb.RedundancyScheme{
|
|
|
|
Type: pb.RedundancyScheme_RS,
|
|
|
|
MinReq: 1,
|
|
|
|
Total: 3,
|
|
|
|
RepairThreshold: 2,
|
|
|
|
SuccessThreshold: 3,
|
|
|
|
ErasureShareSize: 2,
|
|
|
|
},
|
|
|
|
RootPieceId: teststorj.PieceIDFromString("testId"),
|
|
|
|
RemotePieces: rps,
|
|
|
|
},
|
|
|
|
SegmentSize: int64(10),
|
|
|
|
}
|
|
|
|
}
|