2018-07-19 23:57:22 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-08-24 05:01:03 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-19 15:48:08 +01:00
|
|
|
proto "storj.io/storj/protos/overlay"
|
|
|
|
)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
var ctx = context.Background()
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
func TestLookup(t *testing.T) {
|
|
|
|
cases := []struct {
|
2018-08-27 18:28:16 +01:00
|
|
|
self proto.Node
|
|
|
|
to proto.Node
|
|
|
|
find proto.Node
|
|
|
|
expectedErr error
|
2018-07-19 15:48:08 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-09-11 08:27:12 +01:00
|
|
|
self: proto.Node{Id: NewNodeID(t), Address: &proto.NodeAddress{Address: ":7070"}},
|
2018-09-07 10:42:04 +01:00
|
|
|
to: proto.Node{}, // filled after server has been started
|
2018-09-11 08:27:12 +01:00
|
|
|
find: proto.Node{Id: NewNodeID(t), Address: &proto.NodeAddress{Address: ":9090"}},
|
2018-07-19 15:48:08 +01:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2018-09-07 10:42:04 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-09-11 08:27:12 +01:00
|
|
|
v.to = proto.Node{Id: NewNodeID(t), Address: &proto.NodeAddress{Address: lis.Addr().String()}}
|
2018-09-07 10:42:04 +01:00
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
srv, mock, err := newTestServer(ctx)
|
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
go func() { assert.NoError(t, srv.Serve(lis)) }()
|
2018-07-19 15:48:08 +01:00
|
|
|
defer srv.Stop()
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
nc, err := NewNodeClient(identity, v.self)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
_, err = nc.Lookup(ctx, v.to, v.find)
|
2018-07-19 15:48:08 +01:00
|
|
|
assert.Equal(t, v.expectedErr, err)
|
|
|
|
assert.Equal(t, 1, mock.queryCalled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
func newTestServer(ctx context.Context) (*grpc.Server, *mockNodeServer, error) {
|
|
|
|
ca, err := provider.NewCA(ctx, 12, 4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
identOpt, err := identity.ServerOption()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer(identOpt)
|
2018-07-19 15:48:08 +01:00
|
|
|
mn := &mockNodeServer{queryCalled: 0}
|
|
|
|
|
|
|
|
proto.RegisterNodesServer(grpcServer, mn)
|
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
return grpcServer, mn, nil
|
2018-07-19 15:48:08 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNodeServer struct {
|
|
|
|
queryCalled int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mn *mockNodeServer) Query(ctx context.Context, req *proto.QueryRequest) (*proto.QueryResponse, error) {
|
|
|
|
mn.queryCalled++
|
|
|
|
return &proto.QueryResponse{}, nil
|
|
|
|
}
|
2018-09-11 08:27:12 +01:00
|
|
|
|
|
|
|
// NewNodeID returns the string representation of a dht node ID
|
|
|
|
func NewNodeID(t *testing.T) string {
|
|
|
|
id, err := kademlia.NewID()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return id.String()
|
|
|
|
}
|