forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
package cursor
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// TestCodec_EncodeDecode preserves the complete position through an authenticated round trip.
|
||||
func TestCodec_EncodeDecode(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
position := Position{CreatedAt: time.Unix(1700000000, 123456789).UTC(), ID: uuid.New()}
|
||||
|
||||
// Act.
|
||||
encoded, err := codec.Encode("chats:alice", position)
|
||||
|
||||
// Assert.
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if encoded == "" {
|
||||
t.Fatal("encoded position is empty")
|
||||
}
|
||||
decoded, err := codec.Decode(encoded, "chats:alice")
|
||||
if err != nil || decoded != position {
|
||||
t.Fatalf("decoded=%v, want=%v, error=%v", decoded, position, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCodec_Decode checks first-page behavior and independent rejection conditions.
|
||||
func TestCodec_Decode(t *testing.T) {
|
||||
t.Run("Positive: empty token starts first page", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode("", "chats:alice")
|
||||
|
||||
// Assert.
|
||||
if err != nil || got != (Position{}) {
|
||||
t.Fatalf("first-page position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: another resource", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
encoded := encodeFixture(t, codec, "chats:alice")
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode(encoded, "chats:bob")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, ErrInvalid) || got != (Position{}) {
|
||||
t.Fatalf("position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: another process key", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
encoded := encodeFixture(t, codec, "chats:alice")
|
||||
codec = newTestCodec(t)
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode(encoded, "chats:alice")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, ErrInvalid) || got != (Position{}) {
|
||||
t.Fatalf("position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: tampered payload", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
encoded := encodeFixture(t, codec, "chats:alice")
|
||||
signed, err := base64.RawURLEncoding.DecodeString(encoded)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
signed[0] ^= 1
|
||||
encoded = base64.RawURLEncoding.EncodeToString(signed)
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode(encoded, "chats:alice")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, ErrInvalid) || got != (Position{}) {
|
||||
t.Fatalf("position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: oversized token", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
encoded := strings.Repeat("a", 1025)
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode(encoded, "chats:alice")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, ErrInvalid) || got != (Position{}) {
|
||||
t.Fatalf("position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: malformed base64", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
encoded := "!invalid!"
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode(encoded, "chats:alice")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, ErrInvalid) || got != (Position{}) {
|
||||
t.Fatalf("position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: signed malformed JSON", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
encoded := signedPayload(codec, []byte("not JSON"))
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode(encoded, "chats:alice")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, ErrInvalid) || got != (Position{}) {
|
||||
t.Fatalf("position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Negative: signed zero timestamp", func(t *testing.T) {
|
||||
// Arrange.
|
||||
codec := newTestCodec(t)
|
||||
encoded := signedPayload(codec, []byte(`{"r":"chats:alice","t":"0001-01-01T00:00:00Z","i":"11111111-1111-1111-1111-111111111111"}`))
|
||||
|
||||
// Act.
|
||||
got, err := codec.Decode(encoded, "chats:alice")
|
||||
|
||||
// Assert.
|
||||
if !errors.Is(err, ErrInvalid) || got != (Position{}) {
|
||||
t.Fatalf("position=%v, error=%v", got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// signedPayload creates authenticated malformed fixtures to isolate payload validation.
|
||||
func signedPayload(codec *Codec, payload []byte) string {
|
||||
return base64.RawURLEncoding.EncodeToString(append(payload, codec.signature(payload)...))
|
||||
}
|
||||
|
||||
// newTestCodec creates an independent signing key for one scenario.
|
||||
func newTestCodec(t *testing.T) *Codec {
|
||||
t.Helper()
|
||||
codec, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return codec
|
||||
}
|
||||
|
||||
// encodeFixture prepares a valid token before testing a separate decoding condition.
|
||||
func encodeFixture(t *testing.T, codec *Codec, resource string) string {
|
||||
t.Helper()
|
||||
token, err := codec.Encode(resource, Position{ID: uuid.New(), CreatedAt: time.Unix(1700000000, 0).UTC()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
// TestSharedCodec verifies a second process can verify the first process's cursor.
|
||||
func TestSharedCodec(t *testing.T) {
|
||||
key := base64.StdEncoding.EncodeToString(make([]byte, 32))
|
||||
first, err := NewShared(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := NewShared(key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
position := Position{CreatedAt: time.Now().UTC(), ID: uuid.New()}
|
||||
encoded, err := first.Encode("chat", position)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := second.Decode(encoded, "chat")
|
||||
if err != nil || result != position {
|
||||
t.Fatalf("cross-replica cursor: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSharedCodecRejectsInvalidKey requires a full 256-bit decoded secret.
|
||||
func TestSharedCodecRejectsInvalidKey(t *testing.T) {
|
||||
for _, key := range []string{"", "broken!", base64.StdEncoding.EncodeToString([]byte("short"))} {
|
||||
t.Run(key, func(t *testing.T) {
|
||||
if _, err := NewShared(key); err == nil {
|
||||
t.Fatal("invalid key accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user