2020-03-28 06:10:39 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-03-13 05:06:19 +00:00
|
|
|
"github.com/fasthttp/session/v2"
|
2020-03-28 06:10:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldEncryptAndDecrypt(t *testing.T) {
|
2023-01-09 04:57:40 +00:00
|
|
|
payload := session.Dict{KV: map[string]interface{}{"key": "value"}}
|
2020-03-28 06:10:39 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-01-09 04:57:40 +00:00
|
|
|
assert.Equal(t, "value", decodedPayload.KV["key"])
|
2020-03-28 06:10:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 11:48:14 +00:00
|
|
|
func TestShouldNotSupportUnencryptedSessionForBackwardCompatibility(t *testing.T) {
|
2023-01-09 04:57:40 +00:00
|
|
|
payload := session.Dict{KV: map[string]interface{}{"key": "value"}}
|
2020-03-28 06:10:39 +00:00
|
|
|
|
|
|
|
dst, err := payload.MarshalMsg(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
serializer := NewEncryptingSerializer("asecret")
|
|
|
|
|
|
|
|
decodedPayload := session.Dict{}
|
|
|
|
err = serializer.Decode(&decodedPayload, dst)
|
2021-08-26 11:48:14 +00:00
|
|
|
assert.EqualError(t, err, "unable to decrypt session: cipher: message authentication failed")
|
2020-03-28 06:10:39 +00:00
|
|
|
}
|