forked from mtusi2026/mtusi2026lab2
51 lines
1.7 KiB
Go
51 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPasswordHashSaltAndFullUnicode(t *testing.T) {
|
|
p := strings.Repeat("😀", 127) + ":"
|
|
a, b := hashPassword(p), hashPassword(p)
|
|
if a == b {
|
|
t.Fatal("salt reused")
|
|
}
|
|
if !verifyPassword(a, p) || verifyPassword(a, strings.Repeat("😀", 127)+"!") {
|
|
t.Fatal("password truncated or incorrect")
|
|
}
|
|
for _, bad := range []string{"", p, strings.Replace(a, "m=65536", "m=1", 1), a[:len(a)-5]} {
|
|
if verifyPassword(bad, p) {
|
|
t.Fatal("invalid hash accepted")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCursorIntegrityResourceAndRestart(t *testing.T) {
|
|
a := &App{secret: []byte("stable-local-test-secret-32-bytes")}
|
|
encoded := a.encodeCursor("messages:one", "2026-01-01T00:00:00.000000Z", uuid())
|
|
restarted := &App{secret: append([]byte(nil), a.secret...)}
|
|
req := httptest.NewRequest("GET", "/messages?cursor="+encoded+"&limit=7", nil)
|
|
if limit, _, e := restarted.pagination(req, "messages:one"); e != nil || limit != 7 {
|
|
t.Fatal("cursor lost across restart")
|
|
}
|
|
if _, _, e := a.pagination(req, "messages:two"); e == nil {
|
|
t.Fatal("cross-resource cursor accepted")
|
|
}
|
|
forged := httptest.NewRequest("GET", "/messages?cursor="+encoded[:len(encoded)-4]+"AAAA", nil)
|
|
if _, _, e := a.pagination(forged, "messages:one"); e == nil {
|
|
t.Fatal("forged cursor accepted")
|
|
}
|
|
}
|
|
|
|
func TestStrictJSON(t *testing.T) {
|
|
for _, raw := range []string{`{"text":"a","text":"b"}`, `{"text":"\ud800"}`, `{} {}`, `{"unknown":1}`} {
|
|
req := httptest.NewRequest("POST", "/", strings.NewReader(raw))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if _, e := readObject(httptest.NewRecorder(), req, 1024, []string{"text"}); e == nil {
|
|
t.Fatalf("accepted %q", raw)
|
|
}
|
|
}
|
|
}
|