2020-03-28 13:10:39 +07:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-03-13 12:06:19 +07:00
|
|
|
"github.com/fasthttp/session/v2"
|
2020-05-18 09:45:47 +07:00
|
|
|
|
2020-03-28 13:10:39 +07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldEncryptAndDecrypt(t *testing.T) {
|
|
|
|
payload := session.Dict{}
|
|
|
|
payload.Set("key", "value")
|
|
|
|
|
|
|
|
dst, err := payload.MarshalMsg(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serializer := NewEncryptingSerializer("asecret")
|
|
|
|
encryptedDst, err := serializer.Encode(payload)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NotEqual(t, dst, encryptedDst)
|
|
|
|
|
|
|
|
decodedPayload := session.Dict{}
|
|
|
|
err = serializer.Decode(&decodedPayload, encryptedDst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "value", decodedPayload.Get("key"))
|
|
|
|
}
|
|
|
|
|
2021-08-26 18:48:14 +07:00
|
|
|
func TestShouldNotSupportUnencryptedSessionForBackwardCompatibility(t *testing.T) {
|
2020-03-28 13:10:39 +07:00
|
|
|
payload := session.Dict{}
|
|
|
|
payload.Set("key", "value")
|
|
|
|
|
|
|
|
dst, err := payload.MarshalMsg(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serializer := NewEncryptingSerializer("asecret")
|
|
|
|
|
|
|
|
decodedPayload := session.Dict{}
|
|
|
|
err = serializer.Decode(&decodedPayload, dst)
|
2021-08-26 18:48:14 +07:00
|
|
|
assert.EqualError(t, err, "unable to decrypt session: cipher: message authentication failed")
|
2020-03-28 13:10:39 +07:00
|
|
|
}
|