storj/pkg/pointerdb/pointerdb_test.go
Egon Elbre 78dc02b758 Satellite Peer (#1034)
* add satellite peer

* Add overlay

* reorganize kademlia

* add RunRefresh

* add refresh to storagenode.Peer

* add discovery

* add agreements and metainfo

* rename

* add datarepair checker

* add repair

* add todo notes for audit

* add testing interface

* add into testplanet

* fixes

* fix compilation errors

* fix compilation errors

* make testplanet run

* remove audit refrences

* ensure that audit tests run

* dev

* checker tests compilable

* fix discovery

* fix compilation

* fix

* fix

* dev

* fix

* disable auth

* fixes

* revert go.mod/sum

* fix linter errors

* fix

* fix copyright

* Add address param for SN dashboard (#1076)

* Rename storj-sdk to storj-sim (#1078)

* Storagenode logs and config improvements  (#1075)

* Add more info to SN logs

* remove config-dir from user config

* add output where config was stored

* add message for successful connection

* fix linter

* remove storage.path from user config

* resolve config path

* move success  message to info

* log improvements

* Remove captplanet (#1070)

* pkg/server: include production cert (#1082)

Change-Id: Ie8e6fe78550be83c3bd797db7a1e58d37c684792

* Generate Payments Report (#1079)

* memory.Size: autoformat sizes based on value entropy (#1081)

* Jj/bytes (#1085)

* run tally and rollup

* sets dev default tally and rollup intervals

* nonessential storj-sim edits (#1086)

* Closing context doesn't stop storage node (#1084)

* Print when cancelled

* Close properly

* Don't log nil

* Don't print error when closing dashboard

* Fix panic in inspector if ping fails (#1088)

* Consolidate identity management to identity cli commands (#1083)

* Consolidate identity management:

Move identity cretaion/signing out of storagenode setup command.

* fixes

* linters

* Consolidate identity management:

Move identity cretaion/signing out of storagenode setup command.

* fixes

* sava backups before saving signed certs

* add "-prebuilt-test-cmds" test flag

* linters

* prepare cli tests for travis

* linter fixes

* more fixes

* linter gods

* sp/sdk/sim

* remove ca.difficulty

* remove unused difficulty

* return setup to its rightful place

* wip travis

* Revert "wip travis"

This reverts commit 56834849dcf066d3cc0a4f139033fc3f6d7188ca.

* typo in travis.yaml

* remove tests

* remove more

* make it only create one identity at a time for consistency

* add config-dir for consitency

* add identity creation to storj-sim

* add flags

* simplify

* fix nolint and compile

* prevent overwrite and pass difficulty, concurrency, and parent creds

* goimports
2019-01-18 08:54:08 -05:00

348 lines
9.0 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package pointerdb
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"testing"
"github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"storj.io/storj/internal/testidentity"
"storj.io/storj/pkg/auth"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storage/meta"
"storj.io/storj/storage"
"storj.io/storj/storage/teststore"
)
func TestServicePut(t *testing.T) {
for i, tt := range []struct {
apiKey []byte
err error
errString string
}{
{nil, nil, ""},
{[]byte("wrong key"), nil, status.Errorf(codes.Unauthenticated, "Invalid API credential").Error()},
{nil, errors.New("put error"), status.Errorf(codes.Internal, "internal error").Error()},
} {
ctx := context.Background()
ctx = auth.WithAPIKey(ctx, tt.apiKey)
errTag := fmt.Sprintf("Test case #%d", i)
db := teststore.New()
s := Server{DB: db, logger: zap.NewNop()}
path := "a/b/c"
pr := pb.Pointer{}
if tt.err != nil {
db.ForceError++
}
req := pb.PutRequest{Path: path, Pointer: &pr}
_, err := s.Put(ctx, &req)
if err != nil {
assert.EqualError(t, err, tt.errString, errTag)
} else {
assert.NoError(t, err, errTag)
}
}
}
func TestServiceGet(t *testing.T) {
ctx := context.Background()
ca, err := testidentity.NewTestCA(ctx)
assert.NoError(t, err)
identity, err := ca.NewIdentity()
assert.NoError(t, err)
peerCertificates := make([]*x509.Certificate, 2)
peerCertificates[0] = identity.Leaf
peerCertificates[1] = identity.CA
info := credentials.TLSInfo{State: tls.ConnectionState{PeerCertificates: peerCertificates}}
for i, tt := range []struct {
apiKey []byte
err error
errString string
}{
{nil, nil, ""},
{[]byte("wrong key"), nil, status.Errorf(codes.Unauthenticated, "Invalid API credential").Error()},
{nil, errors.New("get error"), status.Errorf(codes.Internal, "internal error").Error()},
} {
ctx = auth.WithAPIKey(ctx, tt.apiKey)
ctx = peer.NewContext(ctx, &peer.Peer{AuthInfo: info})
errTag := fmt.Sprintf("Test case #%d", i)
db := teststore.New()
s := Server{DB: db, logger: zap.NewNop(), identity: identity}
path := "a/b/c"
pr := &pb.Pointer{SegmentSize: 123}
prBytes, err := proto.Marshal(pr)
assert.NoError(t, err, errTag)
_ = db.Put(storage.Key(path), storage.Value(prBytes))
if tt.err != nil {
db.ForceError++
}
req := pb.GetRequest{Path: path}
resp, err := s.Get(ctx, &req)
if err != nil {
assert.EqualError(t, err, tt.errString, errTag)
} else {
assert.NoError(t, err, errTag)
assert.NoError(t, err, errTag)
assert.True(t, proto.Equal(pr, resp.Pointer), errTag)
assert.NotNil(t, resp.GetAuthorization())
assert.NotNil(t, resp.GetPba())
}
}
}
func TestServiceDelete(t *testing.T) {
for i, tt := range []struct {
apiKey []byte
err error
errString string
}{
{nil, nil, ""},
{[]byte("wrong key"), nil, status.Errorf(codes.Unauthenticated, "Invalid API credential").Error()},
{nil, errors.New("delete error"), status.Errorf(codes.Internal, "internal error").Error()},
} {
ctx := context.Background()
ctx = auth.WithAPIKey(ctx, tt.apiKey)
errTag := fmt.Sprintf("Test case #%d", i)
path := "a/b/c"
db := teststore.New()
_ = db.Put(storage.Key(path), storage.Value("hello"))
s := Server{DB: db, logger: zap.NewNop()}
if tt.err != nil {
db.ForceError++
}
req := pb.DeleteRequest{Path: path}
_, err := s.Delete(ctx, &req)
if err != nil {
assert.EqualError(t, err, tt.errString, errTag)
} else {
assert.NoError(t, err, errTag)
}
}
}
func TestServiceList(t *testing.T) {
db := teststore.New()
server := Server{DB: db, logger: zap.NewNop()}
pointer := &pb.Pointer{}
pointer.CreationDate = ptypes.TimestampNow()
pointerBytes, err := proto.Marshal(pointer)
if err != nil {
t.Fatal(err)
}
pointerValue := storage.Value(pointerBytes)
err = storage.PutAll(db, []storage.ListItem{
{Key: storage.Key("sample.😶"), Value: pointerValue},
{Key: storage.Key("müsic"), Value: pointerValue},
{Key: storage.Key("müsic/söng1.mp3"), Value: pointerValue},
{Key: storage.Key("müsic/söng2.mp3"), Value: pointerValue},
{Key: storage.Key("müsic/album/söng3.mp3"), Value: pointerValue},
{Key: storage.Key("müsic/söng4.mp3"), Value: pointerValue},
{Key: storage.Key("ビデオ/movie.mkv"), Value: pointerValue},
}...)
if err != nil {
t.Fatal(err)
}
type Test struct {
APIKey string
Request pb.ListRequest
Expected *pb.ListResponse
Error func(i int, err error)
}
// TODO: ZZZ temporarily disabled until endpoint and service split
// errorWithCode := func(code codes.Code) func(i int, err error) {
// t.Helper()
// return func(i int, err error) {
// t.Helper()
// if status.Code(err) != code {
// t.Fatalf("%d: should fail with %v, got: %v", i, code, err)
// }
// }
// }
tests := []Test{
{
Request: pb.ListRequest{Recursive: true},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "müsic"},
{Path: "müsic/album/söng3.mp3"},
{Path: "müsic/söng1.mp3"},
{Path: "müsic/söng2.mp3"},
{Path: "müsic/söng4.mp3"},
{Path: "sample.😶"},
{Path: "ビデオ/movie.mkv"},
},
},
}, {
Request: pb.ListRequest{Recursive: true, MetaFlags: meta.All},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "müsic", Pointer: pointer},
{Path: "müsic/album/söng3.mp3", Pointer: pointer},
{Path: "müsic/söng1.mp3", Pointer: pointer},
{Path: "müsic/söng2.mp3", Pointer: pointer},
{Path: "müsic/söng4.mp3", Pointer: pointer},
{Path: "sample.😶", Pointer: pointer},
{Path: "ビデオ/movie.mkv", Pointer: pointer},
},
},
},
// { // TODO: ZZZ temporarily disabled until endpoint and service split
// APIKey: "wrong key",
// Request: pb.ListRequest{Recursive: true, MetaFlags: meta.All}, //, APIKey: []byte("wrong key")},
// Error: errorWithCode(codes.Unauthenticated),
// },
{
Request: pb.ListRequest{Recursive: true, Limit: 3},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "müsic"},
{Path: "müsic/album/söng3.mp3"},
{Path: "müsic/söng1.mp3"},
},
More: true,
},
}, {
Request: pb.ListRequest{MetaFlags: meta.All},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "müsic", Pointer: pointer},
{Path: "müsic/", IsPrefix: true},
{Path: "sample.😶", Pointer: pointer},
{Path: "ビデオ/", IsPrefix: true},
},
More: false,
},
}, {
Request: pb.ListRequest{EndBefore: "ビデオ"},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "müsic"},
{Path: "müsic/", IsPrefix: true},
{Path: "sample.😶"},
},
More: false,
},
}, {
Request: pb.ListRequest{Recursive: true, Prefix: "müsic/"},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "album/söng3.mp3"},
{Path: "söng1.mp3"},
{Path: "söng2.mp3"},
{Path: "söng4.mp3"},
},
},
}, {
Request: pb.ListRequest{Recursive: true, Prefix: "müsic/", StartAfter: "album/söng3.mp3"},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "söng1.mp3"},
{Path: "söng2.mp3"},
{Path: "söng4.mp3"},
},
},
}, {
Request: pb.ListRequest{Prefix: "müsic/"},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "album/", IsPrefix: true},
{Path: "söng1.mp3"},
{Path: "söng2.mp3"},
{Path: "söng4.mp3"},
},
},
}, {
Request: pb.ListRequest{Prefix: "müsic/", StartAfter: "söng1.mp3"},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "söng2.mp3"},
{Path: "söng4.mp3"},
},
},
}, {
Request: pb.ListRequest{Prefix: "müsic/", EndBefore: "söng4.mp3"},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
{Path: "album/", IsPrefix: true},
{Path: "söng1.mp3"},
{Path: "söng2.mp3"},
},
},
}, {
Request: pb.ListRequest{Prefix: "müs", Recursive: true, EndBefore: "ic/söng4.mp3", Limit: 1},
Expected: &pb.ListResponse{
Items: []*pb.ListResponse_Item{
// {Path: "ic/söng2.mp3"},
},
// More: true,
},
},
}
// TODO:
// pb.ListRequest{Prefix: "müsic/", StartAfter: "söng1.mp3", EndBefore: "söng4.mp3"},
// failing database
for i, test := range tests {
ctx := context.Background()
ctx = auth.WithAPIKey(ctx, []byte(test.APIKey))
resp, err := server.List(ctx, &test.Request)
if test.Error == nil {
if err != nil {
t.Fatalf("%d: failed %v", i, err)
}
} else {
test.Error(i, err)
}
if diff := cmp.Diff(test.Expected, resp, cmp.Comparer(proto.Equal)); diff != "" {
t.Errorf("%d: (-want +got) %v\n%s", i, test.Request.String(), diff)
}
}
}