2018-06-29 21:06:25 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-06 20:43:53 +01:00
|
|
|
package pointerdb
|
2018-06-29 21:06:25 +01:00
|
|
|
|
|
|
|
import (
|
2018-08-22 16:07:00 +01:00
|
|
|
"context"
|
2018-10-30 16:24:46 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-07-19 23:57:22 +01:00
|
|
|
"errors"
|
2018-07-27 07:02:59 +01:00
|
|
|
"fmt"
|
2018-06-29 21:06:25 +01:00
|
|
|
"testing"
|
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-09-07 15:20:15 +01:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-07-19 23:57:22 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-16 12:43:44 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc/codes"
|
2018-10-30 16:24:46 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/peer"
|
2018-10-16 12:43:44 +01:00
|
|
|
"google.golang.org/grpc/status"
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-10-11 21:25:54 +01:00
|
|
|
"storj.io/storj/pkg/auth"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-30 16:24:46 +00:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-30 19:57:50 +01:00
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-07-27 07:02:59 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-09-07 15:20:15 +01:00
|
|
|
"storj.io/storj/storage/teststore"
|
2018-08-22 16:07:00 +01:00
|
|
|
)
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
func TestServicePut(t *testing.T) {
|
2018-07-19 23:57:22 +01:00
|
|
|
for i, tt := range []struct {
|
2018-07-27 07:02:59 +01:00
|
|
|
apiKey []byte
|
|
|
|
err error
|
2018-07-19 23:57:22 +01:00
|
|
|
errString string
|
|
|
|
}{
|
2018-07-27 07:02:59 +01:00
|
|
|
{nil, nil, ""},
|
2018-08-27 18:28:16 +01:00
|
|
|
{[]byte("wrong key"), nil, status.Errorf(codes.Unauthenticated, "Invalid API credential").Error()},
|
2018-09-11 08:27:12 +01:00
|
|
|
{nil, errors.New("put error"), status.Errorf(codes.Internal, "internal error").Error()},
|
2018-07-27 07:02:59 +01:00
|
|
|
} {
|
2018-10-09 15:39:14 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = auth.WithAPIKey(ctx, tt.apiKey)
|
|
|
|
|
2018-07-19 23:57:22 +01:00
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
db := teststore.New()
|
2018-07-27 07:02:59 +01:00
|
|
|
s := Server{DB: db, logger: zap.NewNop()}
|
|
|
|
|
|
|
|
path := "a/b/c"
|
|
|
|
pr := pb.Pointer{}
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
if tt.err != nil {
|
|
|
|
db.ForceError++
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
req := pb.PutRequest{Path: path, Pointer: &pr}
|
2018-07-27 07:02:59 +01:00
|
|
|
_, err := s.Put(ctx, &req)
|
2018-07-19 23:57:22 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, errTag)
|
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
func TestServiceGet(t *testing.T) {
|
2018-10-30 16:24:46 +00:00
|
|
|
ctx := context.Background()
|
2018-11-01 15:48:43 +00:00
|
|
|
ca, err := provider.NewTestCA(ctx)
|
2018-10-30 16:24:46 +00:00
|
|
|
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}}
|
|
|
|
|
2018-07-19 23:57:22 +01:00
|
|
|
for i, tt := range []struct {
|
2018-07-27 07:02:59 +01:00
|
|
|
apiKey []byte
|
|
|
|
err error
|
2018-07-19 23:57:22 +01:00
|
|
|
errString string
|
|
|
|
}{
|
2018-07-27 07:02:59 +01:00
|
|
|
{nil, nil, ""},
|
2018-08-27 18:28:16 +01:00
|
|
|
{[]byte("wrong key"), nil, status.Errorf(codes.Unauthenticated, "Invalid API credential").Error()},
|
2018-09-11 08:27:12 +01:00
|
|
|
{nil, errors.New("get error"), status.Errorf(codes.Internal, "internal error").Error()},
|
2018-07-27 07:02:59 +01:00
|
|
|
} {
|
2018-10-09 15:39:14 +01:00
|
|
|
ctx = auth.WithAPIKey(ctx, tt.apiKey)
|
2018-10-30 16:24:46 +00:00
|
|
|
ctx = peer.NewContext(ctx, &peer.Peer{AuthInfo: info})
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
db := teststore.New()
|
2018-10-30 16:24:46 +00:00
|
|
|
s := Server{DB: db, logger: zap.NewNop(), identity: identity}
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
path := "a/b/c"
|
2018-09-11 08:27:12 +01:00
|
|
|
|
|
|
|
pr := &pb.Pointer{Size: 123}
|
|
|
|
prBytes, err := proto.Marshal(pr)
|
2018-07-27 07:02:59 +01:00
|
|
|
assert.NoError(t, err, errTag)
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
_ = db.Put(storage.Key(path), storage.Value(prBytes))
|
|
|
|
|
|
|
|
if tt.err != nil {
|
|
|
|
db.ForceError++
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
req := pb.GetRequest{Path: path}
|
2018-07-27 07:02:59 +01:00
|
|
|
resp, err := s.Get(ctx, &req)
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-07-19 23:57:22 +01:00
|
|
|
if err != nil {
|
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, errTag)
|
2018-07-27 07:02:59 +01:00
|
|
|
assert.NoError(t, err, errTag)
|
2018-10-04 22:00:19 +01:00
|
|
|
assert.True(t, proto.Equal(pr, resp.Pointer), errTag)
|
2018-11-05 15:12:19 +00:00
|
|
|
|
|
|
|
assert.NotNil(t, resp.GetAuthorization())
|
|
|
|
assert.NotNil(t, resp.GetPba())
|
2018-07-19 23:57:22 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
func TestServiceDelete(t *testing.T) {
|
2018-07-19 23:57:22 +01:00
|
|
|
for i, tt := range []struct {
|
2018-07-27 07:02:59 +01:00
|
|
|
apiKey []byte
|
|
|
|
err error
|
2018-07-19 23:57:22 +01:00
|
|
|
errString string
|
|
|
|
}{
|
2018-07-27 07:02:59 +01:00
|
|
|
{nil, nil, ""},
|
2018-08-27 18:28:16 +01:00
|
|
|
{[]byte("wrong key"), nil, status.Errorf(codes.Unauthenticated, "Invalid API credential").Error()},
|
2018-09-11 08:27:12 +01:00
|
|
|
{nil, errors.New("delete error"), status.Errorf(codes.Internal, "internal error").Error()},
|
2018-07-27 07:02:59 +01:00
|
|
|
} {
|
2018-10-09 15:39:14 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = auth.WithAPIKey(ctx, tt.apiKey)
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
errTag := fmt.Sprintf("Test case #%d", i)
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
path := "a/b/c"
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
db := teststore.New()
|
|
|
|
_ = db.Put(storage.Key(path), storage.Value("hello"))
|
|
|
|
s := Server{DB: db, logger: zap.NewNop()}
|
|
|
|
|
|
|
|
if tt.err != nil {
|
|
|
|
db.ForceError++
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
|
2018-10-09 15:39:14 +01:00
|
|
|
req := pb.DeleteRequest{Path: path}
|
2018-07-27 07:02:59 +01:00
|
|
|
_, err := s.Delete(ctx, &req)
|
2018-06-29 21:06:25 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2018-07-19 23:57:22 +01:00
|
|
|
assert.EqualError(t, err, tt.errString, errTag)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err, errTag)
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:02:59 +01:00
|
|
|
func TestServiceList(t *testing.T) {
|
2018-09-07 15:20:15 +01:00
|
|
|
db := teststore.New()
|
|
|
|
server := Server{DB: db, logger: zap.NewNop()}
|
2018-07-19 23:57:22 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
pointer := &pb.Pointer{}
|
|
|
|
pointer.CreationDate = ptypes.TimestampNow()
|
2018-07-19 23:57:22 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
pointerBytes, err := proto.Marshal(pointer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
pointerValue := storage.Value(pointerBytes)
|
|
|
|
|
|
|
|
err = storage.PutAll(db, []storage.ListItem{
|
2018-10-25 21:28:16 +01:00
|
|
|
{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},
|
2018-09-07 15:20:15 +01:00
|
|
|
}...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
type Test struct {
|
2018-10-11 21:25:54 +01:00
|
|
|
APIKey string
|
2018-09-07 15:20:15 +01:00
|
|
|
Request pb.ListRequest
|
|
|
|
Expected *pb.ListResponse
|
|
|
|
Error func(i int, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-07 15:20:15 +01:00
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
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},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
2018-10-11 21:25:54 +01:00
|
|
|
APIKey: "wrong key",
|
|
|
|
Request: pb.ListRequest{Recursive: true, MetaFlags: meta.All}, //, APIKey: []byte("wrong key")},
|
2018-09-07 15:20:15 +01:00
|
|
|
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{
|
2018-09-09 07:34:23 +01:00
|
|
|
{Path: "album/söng3.mp3"},
|
|
|
|
{Path: "söng1.mp3"},
|
|
|
|
{Path: "söng2.mp3"},
|
|
|
|
{Path: "söng4.mp3"},
|
2018-09-07 15:20:15 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Request: pb.ListRequest{Recursive: true, Prefix: "müsic/", StartAfter: "album/söng3.mp3"},
|
|
|
|
Expected: &pb.ListResponse{
|
|
|
|
Items: []*pb.ListResponse_Item{
|
2018-09-09 07:34:23 +01:00
|
|
|
{Path: "söng1.mp3"},
|
|
|
|
{Path: "söng2.mp3"},
|
|
|
|
{Path: "söng4.mp3"},
|
2018-09-07 15:20:15 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Request: pb.ListRequest{Prefix: "müsic/"},
|
|
|
|
Expected: &pb.ListResponse{
|
|
|
|
Items: []*pb.ListResponse_Item{
|
2018-09-09 07:34:23 +01:00
|
|
|
{Path: "album/", IsPrefix: true},
|
|
|
|
{Path: "söng1.mp3"},
|
|
|
|
{Path: "söng2.mp3"},
|
|
|
|
{Path: "söng4.mp3"},
|
2018-09-07 15:20:15 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Request: pb.ListRequest{Prefix: "müsic/", StartAfter: "söng1.mp3"},
|
|
|
|
Expected: &pb.ListResponse{
|
|
|
|
Items: []*pb.ListResponse_Item{
|
2018-09-09 07:34:23 +01:00
|
|
|
{Path: "söng2.mp3"},
|
|
|
|
{Path: "söng4.mp3"},
|
2018-09-07 15:20:15 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Request: pb.ListRequest{Prefix: "müsic/", EndBefore: "söng4.mp3"},
|
|
|
|
Expected: &pb.ListResponse{
|
|
|
|
Items: []*pb.ListResponse_Item{
|
2018-09-09 07:34:23 +01:00
|
|
|
{Path: "album/", IsPrefix: true},
|
|
|
|
{Path: "söng1.mp3"},
|
|
|
|
{Path: "söng2.mp3"},
|
2018-09-07 15:20:15 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-07-27 07:02:59 +01:00
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
// TODO:
|
|
|
|
// pb.ListRequest{Prefix: "müsic/", StartAfter: "söng1.mp3", EndBefore: "söng4.mp3"},
|
|
|
|
// failing database
|
|
|
|
for i, test := range tests {
|
2018-10-09 15:39:14 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = auth.WithAPIKey(ctx, []byte(test.APIKey))
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
resp, err := server.List(ctx, &test.Request)
|
|
|
|
if test.Error == nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%d: failed %v", i, err)
|
2018-07-27 07:02:59 +01:00
|
|
|
}
|
2018-09-07 15:20:15 +01:00
|
|
|
} 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)
|
2018-07-19 23:57:22 +01:00
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
}
|
|
|
|
}
|