2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-11 15:35:55 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-01-02 17:39:17 +00:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2019-02-07 20:39:20 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2018-10-11 15:35:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateSignature(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-11-29 18:39:27 +00:00
|
|
|
ca, err := testidentity.NewTestCA(ctx)
|
2018-10-11 15:35:55 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tt := range []struct {
|
|
|
|
data []byte
|
|
|
|
verified bool
|
|
|
|
}{
|
|
|
|
{identity.ID.Bytes(), true},
|
|
|
|
{[]byte("non verifiable data"), false},
|
|
|
|
} {
|
2018-10-30 16:24:46 +00:00
|
|
|
signature, err := GenerateSignature(identity.ID.Bytes(), identity)
|
2018-10-11 15:35:55 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-02-07 20:39:20 +00:00
|
|
|
verifyError := pkcrypto.HashAndVerifySignature(identity.Leaf.PublicKey, tt.data, signature)
|
|
|
|
if tt.verified {
|
|
|
|
assert.NoError(t, verifyError)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, verifyError)
|
|
|
|
}
|
2018-10-11 15:35:55 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 12:40:11 +01:00
|
|
|
|
|
|
|
func TestSignedMessageVerifier(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-11-29 18:39:27 +00:00
|
|
|
ca, err := testidentity.NewTestCA(ctx)
|
2018-10-17 12:40:11 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-10-30 16:24:46 +00:00
|
|
|
signature, err := GenerateSignature(identity.ID.Bytes(), identity)
|
2018-10-17 12:40:11 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2018-11-05 15:12:19 +00:00
|
|
|
signedMessage, err := NewSignedMessage(signature, identity)
|
2018-10-17 12:40:11 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tt := range []struct {
|
|
|
|
signature []byte
|
|
|
|
data []byte
|
|
|
|
publicKey []byte
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{signedMessage.Signature, signedMessage.Data, signedMessage.PublicKey, ""},
|
|
|
|
{nil, signedMessage.Data, signedMessage.PublicKey, "auth error: missing signature for verification"},
|
|
|
|
{signedMessage.Signature, nil, signedMessage.PublicKey, "auth error: missing data for verification"},
|
|
|
|
{signedMessage.Signature, signedMessage.Data, nil, "auth error: missing public key for verification"},
|
|
|
|
|
2019-02-07 20:39:20 +00:00
|
|
|
{signedMessage.Signature, []byte("malformed data"), signedMessage.PublicKey, "signature verification error: signature is not valid"},
|
2018-10-17 12:40:11 +01:00
|
|
|
} {
|
|
|
|
signedMessage.Signature = tt.signature
|
|
|
|
signedMessage.Data = tt.data
|
|
|
|
signedMessage.PublicKey = tt.publicKey
|
|
|
|
|
|
|
|
err := NewSignedMessageVerifier()(signedMessage)
|
|
|
|
if tt.errString != "" {
|
|
|
|
assert.EqualError(t, err, tt.errString)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|