forked from mtusi2026/mtusi2026lab4
лаба 4 на оценку 5
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -47,11 +49,13 @@ func TestCursorCodecRejectsTamperingAndWrongScope(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
replacement := "A"
|
||||
if token[len(token)-1:] == replacement {
|
||||
replacement = "B"
|
||||
parts := strings.Split(token, ".")
|
||||
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tampered := token[:len(token)-1] + replacement
|
||||
signature[0] ^= 1
|
||||
tampered := parts[0] + "." + base64.RawURLEncoding.EncodeToString(signature)
|
||||
if _, err = codec.DecodeChat(tampered, "9fa3f49c-e62e-4209-8414-d6d5f69805bf"); !errors.Is(err, ErrInvalidCursor) {
|
||||
t.Fatalf("tampered cursor: got %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
@@ -68,3 +72,25 @@ func TestCursorCodecRequiresStrongSharedKey(t *testing.T) {
|
||||
t.Fatal("expected short cursor signing key to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorCodecRejectsSignedInvalidFieldTypesBeforeSQL(t *testing.T) {
|
||||
codec, err := NewCursorCodec(cursorTestKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chatID := "9fa3f49c-e62e-4209-8414-d6d5f69805bf"
|
||||
validMessageID := "42eea32e-d9b5-4bc6-b4fc-94e9b0477adf"
|
||||
testCases := []cursorPayload{
|
||||
{Version: cursorVersion, Kind: "message", CreatedAt: "not-a-time", ID: validMessageID, ScopeID: chatID},
|
||||
{Version: cursorVersion, Kind: "message", CreatedAt: time.Now().UTC().Format(time.RFC3339Nano), ID: "not-a-uuid", ScopeID: chatID},
|
||||
}
|
||||
for _, payload := range testCases {
|
||||
token, encodeErr := codec.encode(payload)
|
||||
if encodeErr != nil {
|
||||
t.Fatal(encodeErr)
|
||||
}
|
||||
if _, decodeErr := codec.DecodeMessage(token, chatID); !errors.Is(decodeErr, ErrInvalidCursor) {
|
||||
t.Fatalf("invalid signed cursor returned %v, want ErrInvalidCursor", decodeErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user