2020-12-10 14:37:06 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package dynamiccodec
|
|
|
|
|
|
|
|
import (
|
2020-12-10 19:34:39 +00:00
|
|
|
"strings"
|
2020-12-10 14:37:06 +00:00
|
|
|
"testing"
|
2020-12-11 16:11:10 +00:00
|
|
|
"time"
|
2020-12-10 14:37:06 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCodec(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-12-10 19:34:39 +00:00
|
|
|
name string
|
2020-12-11 16:11:10 +00:00
|
|
|
lifespan time.Duration
|
2020-12-10 19:34:39 +00:00
|
|
|
keys func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte)
|
|
|
|
wantEncoderErrorPrefix string
|
|
|
|
wantDecoderError string
|
2020-12-10 14:37:06 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "good signing and encryption keys",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "good signing keys and no encryption key",
|
|
|
|
keys: func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte) {
|
|
|
|
*encoderEncryptionKey = nil
|
|
|
|
*decoderEncryptionKey = nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "good signing keys and bad encoding encryption key",
|
|
|
|
keys: func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte) {
|
|
|
|
*encoderEncryptionKey = []byte("this-secret-is-not-16-bytes")
|
|
|
|
},
|
2020-12-10 19:34:39 +00:00
|
|
|
wantEncoderErrorPrefix: "securecookie: error - caused by: crypto/aes: invalid key size 27",
|
2020-12-10 14:37:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "good signing keys and bad decoding encryption key",
|
|
|
|
keys: func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte) {
|
|
|
|
*decoderEncryptionKey = []byte("this-secret-is-not-16-bytes")
|
|
|
|
},
|
|
|
|
wantDecoderError: "securecookie: error - caused by: crypto/aes: invalid key size 27",
|
|
|
|
},
|
2020-12-11 16:11:10 +00:00
|
|
|
{
|
|
|
|
name: "aaa encoder times stuff out",
|
|
|
|
lifespan: time.Second,
|
|
|
|
wantDecoderError: "securecookie: expired timestamp",
|
|
|
|
},
|
2020-12-10 14:37:06 +00:00
|
|
|
{
|
|
|
|
name: "bad encoder signing key",
|
|
|
|
keys: func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte) {
|
|
|
|
*encoderSigningKey = nil
|
|
|
|
},
|
2020-12-10 19:34:39 +00:00
|
|
|
wantEncoderErrorPrefix: "securecookie: hash key is not set",
|
2020-12-10 14:37:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad decoder signing key",
|
|
|
|
keys: func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte) {
|
|
|
|
*decoderSigningKey = nil
|
|
|
|
},
|
|
|
|
wantDecoderError: "securecookie: hash key is not set",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "signing key mismatch",
|
|
|
|
keys: func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte) {
|
|
|
|
*encoderSigningKey = []byte("this key does not match the decoder key")
|
|
|
|
},
|
|
|
|
wantDecoderError: "securecookie: the value is not valid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "encryption key mismatch",
|
|
|
|
keys: func(encoderSigningKey, encoderEncryptionKey, decoderSigningKey, decoderEncryptionKey *[]byte) {
|
|
|
|
*encoderEncryptionKey = []byte("16-byte-no-match")
|
|
|
|
},
|
2020-12-11 16:46:24 +00:00
|
|
|
wantDecoderError: "securecookie: error - caused by: securecookie: error - caused by: ",
|
2020-12-10 14:37:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
encoderSigningKey = []byte("some-signing-key")
|
|
|
|
encoderEncryptionKey = []byte("16-byte-encr-key")
|
|
|
|
decoderSigningKey = []byte("some-signing-key")
|
|
|
|
decoderEncryptionKey = []byte("16-byte-encr-key")
|
|
|
|
)
|
|
|
|
if test.keys != nil {
|
|
|
|
test.keys(&encoderSigningKey, &encoderEncryptionKey, &decoderSigningKey, &decoderEncryptionKey)
|
|
|
|
}
|
2020-12-11 16:11:10 +00:00
|
|
|
|
|
|
|
lifespan := test.lifespan
|
|
|
|
if lifespan == 0 {
|
|
|
|
lifespan = time.Hour
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder := New(lifespan, func() []byte { return encoderSigningKey },
|
2020-12-10 19:01:03 +00:00
|
|
|
func() []byte { return encoderEncryptionKey })
|
2020-12-10 14:37:06 +00:00
|
|
|
|
|
|
|
encoded, err := encoder.Encode("some-name", "some-message")
|
2020-12-10 19:34:39 +00:00
|
|
|
if test.wantEncoderErrorPrefix != "" {
|
|
|
|
require.EqualError(t, err, test.wantEncoderErrorPrefix)
|
2020-12-10 14:37:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-11 16:11:10 +00:00
|
|
|
if test.lifespan != 0 {
|
|
|
|
time.Sleep(test.lifespan + time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := New(lifespan, func() []byte { return decoderSigningKey },
|
2020-12-10 19:01:03 +00:00
|
|
|
func() []byte { return decoderEncryptionKey })
|
2020-12-10 14:37:06 +00:00
|
|
|
|
|
|
|
var decoded string
|
|
|
|
err = decoder.Decode("some-name", encoded, &decoded)
|
|
|
|
if test.wantDecoderError != "" {
|
2020-12-10 19:34:39 +00:00
|
|
|
require.Error(t, err)
|
2020-12-11 16:11:10 +00:00
|
|
|
require.True(t, strings.HasPrefix(err.Error(), test.wantDecoderError), "expected %q to start with %q", err.Error(), test.wantDecoderError)
|
2020-12-10 14:37:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "some-message", decoded)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|