2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-20 18:29:05 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
package authorization
|
2018-12-20 18:29:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2019-01-02 17:39:17 +00:00
|
|
|
"net"
|
2018-12-20 18:29:05 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
"github.com/btcsuite/btcutil/base58"
|
2018-12-20 18:29:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-01 17:28:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity/testidentity"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
2019-09-26 17:11:05 +01:00
|
|
|
"storj.io/storj/certificate/certificateclient"
|
2018-12-20 18:29:05 +00:00
|
|
|
)
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
var (
|
2019-11-14 08:31:30 +00:00
|
|
|
t1 = Token{
|
2019-06-18 01:28:40 +01:00
|
|
|
UserID: "user@mail.test",
|
2018-12-31 15:45:43 +00:00
|
|
|
Data: [tokenDataLength]byte{1, 2, 3},
|
|
|
|
}
|
|
|
|
t2 = Token{
|
2019-06-18 01:28:40 +01:00
|
|
|
UserID: "user2@mail.test",
|
2018-12-31 15:45:43 +00:00
|
|
|
Data: [tokenDataLength]byte{4, 5, 6},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
func TestNewAuthorization(t *testing.T) {
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2018-12-31 15:45:43 +00:00
|
|
|
auth, err := NewAuthorization(userID)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, auth)
|
|
|
|
|
2018-12-20 18:29:05 +00:00
|
|
|
assert.NotZero(t, auth.Token)
|
2018-12-31 15:45:43 +00:00
|
|
|
assert.Equal(t, userID, auth.Token.UserID)
|
|
|
|
assert.NotEmpty(t, auth.Token.Data)
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizations_Marshal(t *testing.T) {
|
2019-09-05 16:11:21 +01:00
|
|
|
expectedAuths := Group{
|
2018-12-20 18:29:05 +00:00
|
|
|
{Token: t1},
|
|
|
|
{Token: t2},
|
|
|
|
}
|
|
|
|
|
|
|
|
authsBytes, err := expectedAuths.Marshal()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, authsBytes)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
var actualAuths Group
|
2018-12-20 18:29:05 +00:00
|
|
|
decoder := gob.NewDecoder(bytes.NewBuffer(authsBytes))
|
|
|
|
err = decoder.Decode(&actualAuths)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, actualAuths)
|
|
|
|
assert.Equal(t, expectedAuths, actualAuths)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizations_Unmarshal(t *testing.T) {
|
2019-09-05 16:11:21 +01:00
|
|
|
expectedAuths := Group{
|
2018-12-20 18:29:05 +00:00
|
|
|
{Token: t1},
|
|
|
|
{Token: t2},
|
|
|
|
}
|
|
|
|
|
|
|
|
authsBytes, err := expectedAuths.Marshal()
|
2019-04-08 19:15:19 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, authsBytes)
|
2018-12-20 18:29:05 +00:00
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
var actualAuths Group
|
2018-12-20 18:29:05 +00:00
|
|
|
err = actualAuths.Unmarshal(authsBytes)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, actualAuths)
|
|
|
|
assert.Equal(t, expectedAuths, actualAuths)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizations_Group(t *testing.T) {
|
2019-09-05 16:11:21 +01:00
|
|
|
auths := make(Group, 10)
|
2018-12-20 18:29:05 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if i%2 == 0 {
|
|
|
|
auths[i] = &Authorization{
|
|
|
|
Token: t1,
|
|
|
|
Claim: &Claim{
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auths[i] = &Authorization{
|
|
|
|
Token: t2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
claimed, open := auths.GroupByClaimed()
|
2018-12-20 18:29:05 +00:00
|
|
|
for _, a := range claimed {
|
|
|
|
assert.NotNil(t, a.Claim)
|
|
|
|
}
|
|
|
|
for _, a := range open {
|
|
|
|
assert.Nil(t, a.Claim)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 15:45:43 +00:00
|
|
|
func TestParseToken_Valid(t *testing.T) {
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2018-12-31 15:45:43 +00:00
|
|
|
data := [tokenDataLength]byte{1, 2, 3}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
userID string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid token",
|
|
|
|
userID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple delimiters",
|
2019-06-18 01:28:40 +01:00
|
|
|
"us" + tokenDelimiter + "er@mail.test",
|
2018-12-31 15:45:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-05-29 14:30:16 +01:00
|
|
|
testCase := c
|
|
|
|
t.Run(testCase.testID, func(t *testing.T) {
|
2018-12-31 15:45:43 +00:00
|
|
|
b58Data := base58.CheckEncode(data[:], tokenVersion)
|
2019-05-29 14:30:16 +01:00
|
|
|
tokenString := testCase.userID + tokenDelimiter + b58Data
|
2018-12-31 15:45:43 +00:00
|
|
|
token, err := ParseToken(tokenString)
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, token)
|
2018-12-31 15:45:43 +00:00
|
|
|
|
2019-05-29 14:30:16 +01:00
|
|
|
assert.Equal(t, testCase.userID, token.UserID)
|
2018-12-31 15:45:43 +00:00
|
|
|
assert.Equal(t, data[:], token.Data[:])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseToken_Invalid(t *testing.T) {
|
2019-06-18 01:28:40 +01:00
|
|
|
userID := "user@mail.test"
|
2018-12-31 15:45:43 +00:00
|
|
|
data := [tokenDataLength]byte{1, 2, 3}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
testID string
|
|
|
|
tokenString string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no delimiter",
|
|
|
|
userID + base58.CheckEncode(data[:], tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"missing userID",
|
|
|
|
tokenDelimiter + base58.CheckEncode(data[:], tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"not enough data",
|
|
|
|
userID + tokenDelimiter + base58.CheckEncode(data[:len(data)-10], tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"too much data",
|
|
|
|
userID + tokenDelimiter + base58.CheckEncode(append(data[:], []byte{0, 0, 0}...), tokenVersion),
|
|
|
|
},
|
|
|
|
{
|
2020-04-28 15:42:40 +01:00
|
|
|
"data checksum or format error",
|
2018-12-31 15:45:43 +00:00
|
|
|
userID + tokenDelimiter + base58.CheckEncode(data[:], tokenVersion)[:len(base58.CheckEncode(data[:], tokenVersion))-4] + "0000",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2019-05-29 14:30:16 +01:00
|
|
|
testCase := c
|
|
|
|
t.Run(testCase.testID, func(t *testing.T) {
|
|
|
|
token, err := ParseToken(testCase.tokenString)
|
2018-12-31 15:45:43 +00:00
|
|
|
assert.Nil(t, token)
|
|
|
|
assert.True(t, ErrInvalidToken.Has(err))
|
|
|
|
})
|
|
|
|
}
|
2018-12-20 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
func TestToken_Equal(t *testing.T) {
|
|
|
|
assert.True(t, t1.Equal(&t1))
|
|
|
|
assert.False(t, t1.Equal(&t2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient(t *testing.T) {
|
2020-05-11 06:26:32 +01:00
|
|
|
t.Skip("needs proper rpc listener to work")
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Cleanup()
|
2019-12-17 14:16:38 +00:00
|
|
|
|
2019-11-14 08:31:30 +00:00
|
|
|
ident, err := testidentity.PregeneratedIdentity(0, storj.LatestIDVersion())
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ident)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, listener)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(listener.Close)
|
|
|
|
ctx.Go(func() error {
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-08-19 23:10:38 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(ident, tlsopts.Config{}, nil)
|
2019-02-11 11:17:32 +00:00
|
|
|
require.NoError(t, err)
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
dialer := rpc.NewDefaultDialer(tlsOptions)
|
2019-02-11 11:17:32 +00:00
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
t.Run("Basic", func(t *testing.T) {
|
2019-09-19 05:46:39 +01:00
|
|
|
client, err := certificateclient.New(ctx, dialer, listener.Addr().String())
|
2019-02-01 17:28:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, client)
|
2019-01-02 17:39:17 +00:00
|
|
|
|
2019-02-01 17:28:40 +00:00
|
|
|
defer ctx.Check(client.Close)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ClientFrom", func(t *testing.T) {
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := dialer.DialAddressInsecure(ctx, listener.Addr().String())
|
2019-02-01 17:28:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, conn)
|
|
|
|
|
|
|
|
defer ctx.Check(conn.Close)
|
|
|
|
|
2020-03-25 12:15:27 +00:00
|
|
|
client := certificateclient.NewClientFrom(pb.NewDRPCCertificatesClient(conn))
|
2019-02-01 17:28:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, client)
|
|
|
|
|
|
|
|
defer ctx.Check(client.Close)
|
|
|
|
})
|
2019-01-02 17:39:17 +00:00
|
|
|
}
|