2020-04-29 08:52:05 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-05-18 10:14:18 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2020-04-29 08:52:05 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-05-18 10:11:39 +00:00
|
|
|
func TestPToken_PrivateKey(t *testing.T) {
|
2020-04-29 08:52:05 +00:00
|
|
|
// create new private token
|
2020-04-29 11:11:19 +00:00
|
|
|
pToken, err := NewPrivateToken(0)
|
2020-04-29 08:52:05 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-18 10:11:39 +00:00
|
|
|
require.NotNil(t, pToken.PrivateKey())
|
2020-04-29 08:52:05 +00:00
|
|
|
}
|
2020-04-29 11:11:19 +00:00
|
|
|
|
|
|
|
func TestPToken_Expired(t *testing.T) {
|
|
|
|
e := uint64(10)
|
|
|
|
|
|
|
|
var token PrivateToken = &pToken{
|
|
|
|
validUntil: e,
|
|
|
|
}
|
|
|
|
|
|
|
|
// must not be expired in the epoch before last
|
|
|
|
require.False(t, token.Expired(e-1))
|
|
|
|
|
|
|
|
// must not be expired in the last epoch
|
|
|
|
require.False(t, token.Expired(e))
|
|
|
|
|
|
|
|
// must be expired in the epoch after last
|
|
|
|
require.True(t, token.Expired(e+1))
|
|
|
|
}
|
2020-05-08 10:20:12 +00:00
|
|
|
|
|
|
|
func TestPrivateTokenKey_SetOwnerID(t *testing.T) {
|
|
|
|
ownerID := OwnerID{1, 2, 3}
|
|
|
|
|
|
|
|
s := new(PrivateTokenKey)
|
|
|
|
|
|
|
|
s.SetOwnerID(ownerID)
|
|
|
|
|
|
|
|
require.Equal(t, ownerID, s.owner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrivateTokenKey_SetTokenID(t *testing.T) {
|
|
|
|
tokenID := TokenID{1, 2, 3}
|
|
|
|
|
|
|
|
s := new(PrivateTokenKey)
|
|
|
|
|
|
|
|
s.SetTokenID(tokenID)
|
|
|
|
|
|
|
|
require.Equal(t, tokenID, s.token)
|
|
|
|
}
|
2020-05-18 10:14:18 +00:00
|
|
|
|
|
|
|
func TestPublicSessionToken(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// nil PrivateToken
|
|
|
|
_, err = PublicSessionToken(nil)
|
|
|
|
require.EqualError(t, err, ErrNilPrivateToken.Error())
|
|
|
|
|
|
|
|
// empty private key
|
|
|
|
var pToken PrivateToken = new(pToken)
|
|
|
|
_, err = PublicSessionToken(pToken)
|
|
|
|
require.EqualError(t, err, crypto.ErrEmptyPrivateKey.Error())
|
|
|
|
|
|
|
|
// correct PrivateToken
|
|
|
|
pToken, err = NewPrivateToken(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
key := pToken.PrivateKey()
|
|
|
|
require.NotNil(t, key)
|
|
|
|
|
|
|
|
res, err := PublicSessionToken(pToken)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, res, crypto.MarshalPublicKey(&key.PublicKey))
|
|
|
|
}
|