-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (C) 2018 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package transport | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/zeebo/errs" | ||
"google.golang.org/grpc" | ||
monkit "gopkg.in/spacemonkeygo/monkit.v2" | ||
|
||
proto "storj.io/storj/protos/overlay" | ||
) | ||
|
||
var ( | ||
mon = monkit.Package() | ||
//Error is the errs class of standard Transport Client errors | ||
Error = errs.Class("transport error") | ||
) | ||
|
||
// Client defines the interface to an transport client. | ||
type Client interface { | ||
DialUnauthenticated(ctx context.Context, addr proto.NodeAddress) (*grpc.ClientConn, error) | ||
DialNode(ctx context.Context, node proto.Node) (*grpc.ClientConn, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (C) 2018 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
|
||
package transport | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
|
||
proto "storj.io/storj/protos/overlay" | ||
) | ||
|
||
// Transport interface structure | ||
type Transport struct { | ||
} | ||
|
||
// DialNode using the authenticated mode | ||
func (o *Transport) DialNode(ctx context.Context, node proto.Node) (conn *grpc.ClientConn, err error) { | ||
defer mon.Task()(&ctx)(&err) | ||
|
||
if node.Address == nil { | ||
return nil, Error.New("no address") | ||
} | ||
/* TODO@ASK security feature under development */ | ||
return o.DialUnauthenticated(ctx, *node.Address) | ||
} | ||
|
||
// DialUnauthenticated using unauthenticated mode | ||
func (o *Transport) DialUnauthenticated(ctx context.Context, addr proto.NodeAddress) (conn *grpc.ClientConn, err error) { | ||
defer mon.Task()(&ctx)(&err) | ||
|
||
if addr.Address == "" { | ||
return nil, Error.New("no address") | ||
} | ||
|
||
return grpc.Dial(addr.Address, grpc.WithInsecure()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (C) 2018 Storj Labs, Inc. | ||
// See LICENSE for copying information. | ||
package transport | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
proto "storj.io/storj/protos/overlay" | ||
) | ||
|
||
func TestDialNode(t *testing.T) { | ||
oc := Transport{} | ||
|
||
// node.Address.Address == "" condition test | ||
node := proto.Node{ | ||
Id: "DUMMYID1", | ||
Address: &proto.NodeAddress{ | ||
Transport: proto.NodeTransport_TCP, | ||
Address: "", | ||
}, | ||
} | ||
conn, err := oc.DialNode(context.Background(), node) | ||
assert.Error(t, err) | ||
assert.Nil(t, conn) | ||
|
||
// node.Address == nil condition test | ||
node = proto.Node{ | ||
Id: "DUMMYID2", | ||
Address: nil, | ||
} | ||
conn, err = oc.DialNode(context.Background(), node) | ||
assert.Error(t, err) | ||
assert.Nil(t, conn) | ||
|
||
// node is valid argument condition test | ||
node = proto.Node{ | ||
Id: "DUMMYID3", | ||
Address: &proto.NodeAddress{ | ||
Transport: proto.NodeTransport_TCP, | ||
Address: "127.0.0.0:9000", | ||
}, | ||
} | ||
conn, err = oc.DialNode(context.Background(), node) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, conn) | ||
} |