diff options
| author | Shulhan <ms@kilabit.info> | 2026-03-30 19:23:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-03-30 21:07:31 +0700 |
| commit | 61a44bf239f5545080af83f890d488ceaf257a80 (patch) | |
| tree | 26273fdb5618c9623919f6830a24baa5a2c945b3 /lib/paseto/message_test.go | |
| parent | 6e3d2f12d6617a6caa45b4a2eeec2dc54aa8d17a (diff) | |
| download | pakakeh.go-61a44bf239f5545080af83f890d488ceaf257a80.tar.xz | |
lib/paseto: add unit tests for Payload and Message types
Diffstat (limited to 'lib/paseto/message_test.go')
| -rw-r--r-- | lib/paseto/message_test.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/paseto/message_test.go b/lib/paseto/message_test.go new file mode 100644 index 00000000..4dd63755 --- /dev/null +++ b/lib/paseto/message_test.go @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: BSD-3-Clause +// SPDX-FileCopyrightText: 2026 Shulhan <ms@kilabit.info> + +package paseto + +import ( + "encoding/json" + "testing" + "testing/synctest" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func TestNewMessage(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + sender := Peer{ + ID: `sender`, + } + receiver := Peer{ + ID: `receiver`, + } + msg := NewMessage(sender, receiver, `test`) + + gotmsg, err := json.MarshalIndent(msg, ``, ` `) + if err != nil { + t.Fatal(err) + } + + expmsg := `{ + "Footer": { + "peer_id": "sender" + }, + "Payload": { + "data": null, + "iss": "sender", + "sub": "test", + "aud": "receiver", + "exp": 946684860, + "nbf": 946684800, + "iat": 946684800 + } +}` + test.Assert(t, `NewMessage`, expmsg, string(gotmsg)) + }) +} + +func TestMessage_Unpack(t *testing.T) { + listcase := []struct { + desc string + token string + expError string + expMsg Message + }{{ + desc: `ErrTokenHeader`, + token: `v5.public.xxx.xxx`, + expError: `Unpack: invalid token header: want v4.public.`, + }, { + desc: `ErrTokenFooter`, + token: `v4.public.xxx.+++`, + expError: `Unpack: invalid token footer: illegal base64 data at input byte 0`, + }, { + desc: `ErrTokenPayload`, + token: `v4.public.WvLTlMrX9NpYDQlEIFlnDB==.xxx`, + expError: `Unpack: invalid token payload: illegal base64 data at input byte 22`, + }, { + desc: `ErrTokenSize`, + token: `v4.public.WvLTlMrX9NpYDQlEIFlnDB.xxx`, + expError: `Unpack: invalid token payload size 16`, + }, { + desc: `Valid`, + token: `v4.public.eyJkYXRhIjp7ImVtYWlsIjoiaGVsbG9AZXhhbXBsZS5jb20ifSwiaXNzIjoiam9obkBleGFtcGxlLmNvbSIsInN1YiI6ImhlbGxvIiwiYXVkIjoiamFuZUBleGFtcGxlLm9yZyIsImV4cCI6MTc3NDg1OTg4OSwibmJmIjoxNzc0ODU5ODI5LCJpYXQiOjE3NzQ4NTk4Mjl9eGHMkOlYiVchKl1GqQwSMyTC8eWCpXhNJHGHq_MEJNqlKur-kPpHqY-hWHoh4I1RaBq68MN_TNveqwPIbk9iBQ.eyJkYXRhIjp7InF1b3RlIjoiTGl2ZSB0byBlYXQifSwicGVlcl9pZCI6ImpvaG5AZXhhbXBsZS5jb20ifQ`, + expMsg: Message{ + RawPayload: []byte(`{"data":{"email":"hello@example.com"},"iss":"john@example.com","sub":"hello","aud":"jane@example.org","exp":1774859889,"nbf":1774859829,"iat":1774859829}`), + RawFooter: []byte(`{"data":{"quote":"Live to eat"},"peer_id":"john@example.com"}`), + Sig: []byte{0x78, 0x61, 0xcc, 0x90, 0xe9, 0x58, 0x89, 0x57, 0x21, 0x2a, 0x5d, 0x46, 0xa9, 0xc, 0x12, 0x33, 0x24, 0xc2, 0xf1, 0xe5, 0x82, 0xa5, 0x78, 0x4d, 0x24, 0x71, 0x87, 0xab, 0xf3, 0x4, 0x24, 0xda, 0xa5, 0x2a, 0xea, 0xfe, 0x90, 0xfa, 0x47, 0xa9, 0x8f, 0xa1, 0x58, 0x7a, 0x21, 0xe0, 0x8d, 0x51, 0x68, 0x1a, 0xba, 0xf0, 0xc3, 0x7f, 0x4c, 0xdb, 0xde, 0xab, 0x3, 0xc8, 0x6e, 0x4f, 0x62, 0x5}, + PAE: []byte{0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x76, 0x34, 0x2e, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x99, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7b, 0x22, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3a, 0x7b, 0x22, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x22, 0x3a, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x22, 0x7d, 0x2c, 0x22, 0x69, 0x73, 0x73, 0x22, 0x3a, 0x22, 0x6a, 0x6f, 0x68, 0x6e, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x73, 0x75, 0x62, 0x22, 0x3a, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x2c, 0x22, 0x61, 0x75, 0x64, 0x22, 0x3a, 0x22, 0x6a, 0x61, 0x6e, 0x65, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x22, 0x2c, 0x22, 0x65, 0x78, 0x70, 0x22, 0x3a, 0x31, 0x37, 0x37, 0x34, 0x38, 0x35, 0x39, 0x38, 0x38, 0x39, 0x2c, 0x22, 0x6e, 0x62, 0x66, 0x22, 0x3a, 0x31, 0x37, 0x37, 0x34, 0x38, 0x35, 0x39, 0x38, 0x32, 0x39, 0x2c, 0x22, 0x69, 0x61, 0x74, 0x22, 0x3a, 0x31, 0x37, 0x37, 0x34, 0x38, 0x35, 0x39, 0x38, 0x32, 0x39, 0x7d, 0x3d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7b, 0x22, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3a, 0x7b, 0x22, 0x71, 0x75, 0x6f, 0x74, 0x65, 0x22, 0x3a, 0x22, 0x4c, 0x69, 0x76, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x65, 0x61, 0x74, 0x22, 0x7d, 0x2c, 0x22, 0x70, 0x65, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x22, 0x3a, 0x22, 0x6a, 0x6f, 0x68, 0x6e, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x22, 0x7d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, + }, + }} + + for _, tc := range listcase { + var msg Message + err := msg.Unpack(`v4.public.`, tc.token, nil) + if err != nil { + test.Assert(t, tc.desc, tc.expError, err.Error()) + continue + } + test.Assert(t, tc.desc, tc.expMsg, msg) + } +} |
