fix cookie -- not working

This commit is contained in:
2026-05-12 11:25:32 +02:00
parent 669d24c6a3
commit 8c4e664ca8
23 changed files with 836 additions and 166 deletions
+47 -1
View File
@@ -1,6 +1,11 @@
package cookie
import "testing"
import (
"fmt"
"hash/crc32"
"strings"
"testing"
)
const (
testCookieKey = "def000008bf3d70e7012b7493c382d561e193218d0c74ab162fb0ea8029ce20e926531b4bcf0aaec9381152e6c161f198e06918b2d1aad67cc7cf40819a51ee328c63830"
@@ -66,3 +71,44 @@ func TestNativeCodecRoundTrip(t *testing.T) {
t.Fatalf("plaintext mismatch after roundtrip\n got: %s\nwant: %s", redecoded.Plaintext, decoded.Plaintext)
}
}
func TestNativeCodecEncodeRecomputesPrestashopChecksum(t *testing.T) {
codec, err := NewCodec(Config{
CookieName: "PrestaShop-test",
CookieKey: testCookieKey,
CookieIV: "vfRFMV42",
})
if err != nil {
t.Fatalf("NewCodec() error = %v", err)
}
decoded, err := codec.Decode(testCookie)
if err != nil {
t.Fatalf("Decode() error = %v", err)
}
decoded.Values["iso_code_country"] = "PL"
decoded.Values["id_currency"] = "6"
decoded.Values["checksum"] = "stale"
decoded.Plaintext = ""
encoded, err := codec.Encode(decoded)
if err != nil {
t.Fatalf("Encode() error = %v", err)
}
redecoded, err := codec.Decode(encoded)
if err != nil {
t.Fatalf("Decode(encoded) error = %v", err)
}
pairs := strings.Split(redecoded.Plaintext, fieldSeparator)
if len(pairs) < 2 {
t.Fatalf("plaintext too short: %q", redecoded.Plaintext)
}
body := strings.Join(pairs[:len(pairs)-1], fieldSeparator) + fieldSeparator
wantChecksum := fmt.Sprintf("%d", crc32.ChecksumIEEE([]byte("vfRFMV42"+body)))
if got := redecoded.Values["checksum"]; got != wantChecksum {
t.Fatalf("checksum = %q, want %q", got, wantChecksum)
}
}