161 lines
6.0 KiB
Go
161 lines
6.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPasswordUnicodeAndNoTruncation(t *testing.T) {
|
|
password := strings.Repeat("🔑", 128)
|
|
if !validPassword(password) || validPassword(password+"x") || validPassword("too-short") || validPassword(strings.Repeat(" ", 12)) {
|
|
t.Fatal("password code point boundaries")
|
|
}
|
|
hash := hashPassword(password)
|
|
if !verifyPassword(hash, password) || verifyPassword(hash, password[:len(password)-4]+"x") || verifyPassword("corrupt", password) {
|
|
t.Fatal("password verification/truncation")
|
|
}
|
|
}
|
|
|
|
func TestCursorScopeAndValidation(t *testing.T) {
|
|
a := &app{}
|
|
now := time.Now().UTC()
|
|
p := cursor{"messages:chat-a", now, "11111111-1111-4111-8111-111111111111"}
|
|
cases := []struct {
|
|
query string
|
|
valid bool
|
|
}{
|
|
{"", true},
|
|
{"limit=", false}, {"cursor=", false}, {"limit", false}, {"cursor", false},
|
|
{"limit=&cursor=" + encodeCursor(p), false},
|
|
{"limit=1&cursor=", false},
|
|
{"limit=1&limit=2", false},
|
|
{"cursor=" + encodeCursor(p) + "&cursor=", false},
|
|
{"limit=1&cursor=" + encodeCursor(p), true},
|
|
{"limit=100&cursor=" + encodeCursor(p), true},
|
|
{"limit=0", false}, {"limit=101", false}, {"limit=1.2", false}, {"cursor=garbage", false},
|
|
{"cursor=" + encodeCursor(cursor{"messages:chat-b", now, p.ID}), false},
|
|
{"cursor=" + encodeCursor(cursor{p.Resource, now, "bad-id"}), false},
|
|
{"cursor=" + encodeCursor(cursor{p.Resource, time.Time{}, p.ID}), false},
|
|
}
|
|
for _, c := range cases {
|
|
r := httptest.NewRequest("GET", "/messages?"+c.query, nil)
|
|
w := httptest.NewRecorder()
|
|
_, _, ok := a.position(w, r, p.Resource)
|
|
if ok != c.valid {
|
|
t.Errorf("cursor query %s: valid=%v", c.query, ok)
|
|
}
|
|
if !c.valid {
|
|
assertInvalidRequest(t, w)
|
|
} else if c.query == "" {
|
|
_, limit, _ := a.position(httptest.NewRecorder(), r, p.Resource)
|
|
if limit != 50 {
|
|
t.Fatal("absent limit must default to 50")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func assertInvalidRequest(t *testing.T, w *httptest.ResponseRecorder) {
|
|
t.Helper()
|
|
var body struct {
|
|
Error struct {
|
|
Code string `json:"code"`
|
|
} `json:"error"`
|
|
}
|
|
if w.Code != 400 || json.Unmarshal(w.Body.Bytes(), &body) != nil || body.Error.Code != "invalid_request" {
|
|
t.Fatalf("expected 400 invalid_request, received %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestExactJSONFieldNames(t *testing.T) {
|
|
a := &app{limits: limits{maxBodyBytes: 1024}}
|
|
cases := []struct {
|
|
name, data string
|
|
target func() any
|
|
valid bool
|
|
}{
|
|
{"message", `{"text":"valid"}`, func() any { return &createMessage{} }, true},
|
|
{"escaped exact key", `{"te\u0078t":"valid"}`, func() any { return &createMessage{} }, true},
|
|
{"case overwrite", `{"text":"valid","TEXT":"unexpected-property"}`, func() any { return &createMessage{} }, false},
|
|
{"case first", `{"TEXT":"unexpected-property","text":"valid"}`, func() any { return &createMessage{} }, false},
|
|
{"mixed case", `{"Text":"unexpected-property"}`, func() any { return &createMessage{} }, false},
|
|
{"escaped uppercase", `{"\u0054EXT":"unexpected-property"}`, func() any { return &createMessage{} }, false},
|
|
{"duplicate", `{"text":"valid","text":"overwrite"}`, func() any { return &createMessage{} }, false},
|
|
{"escaped duplicate", `{"text":"valid","te\u0078t":"overwrite"}`, func() any { return &createMessage{} }, false},
|
|
{"null object", `null`, func() any { return &createMessage{} }, false},
|
|
{"array", `[]`, func() any { return &createMessage{} }, false},
|
|
{"user", `{"username":"alice","display_name":"Alice","password":"long-password"}`, func() any { return &createUser{} }, true},
|
|
{"user case", `{"username":"alice","display_name":"Alice","PASSWORD":"long-password"}`, func() any { return &createUser{} }, false},
|
|
{"Go field name", `{"Username":"alice"}`, func() any { return &createUser{} }, false},
|
|
{"chat", `{"title":"Chat","member_ids":[]}`, func() any { return &createChat{} }, true},
|
|
{"chat case", `{"TITLE":"Chat","member_ids":[]}`, func() any { return &createChat{} }, false},
|
|
{"trailing object", `{"text":"valid"} {}`, func() any { return &createMessage{} }, false},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
target := c.target()
|
|
if got := a.decode(w, httptest.NewRequest("POST", "/", strings.NewReader(c.data)), target); got != c.valid {
|
|
t.Fatalf("valid=%v, expected %v", got, c.valid)
|
|
}
|
|
if !c.valid {
|
|
assertInvalidRequest(t, w)
|
|
}
|
|
if c.name == "message" || c.name == "escaped exact key" {
|
|
if target.(*createMessage).Text != "valid" {
|
|
t.Fatal("valid text changed")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStrictJSONAndBodyLimit(t *testing.T) {
|
|
a := &app{limits: limits{maxBodyBytes: 128}}
|
|
for _, data := range []string{`{"text":12}`, `{"text":"ok","sender_id":"spoof"}`, `{"text":"ok"} {}`, `{`, "{\"text\":\"\xff\"}"} {
|
|
w := httptest.NewRecorder()
|
|
var input createMessage
|
|
if a.decode(w, httptest.NewRequest("POST", "/", strings.NewReader(data)), &input) || w.Code != 400 {
|
|
t.Errorf("accepted invalid JSON %q", data)
|
|
}
|
|
}
|
|
w := httptest.NewRecorder()
|
|
var input createMessage
|
|
if a.decode(w, httptest.NewRequest("POST", "/", strings.NewReader(strings.Repeat("x", 129))), &input) || w.Code != 413 {
|
|
t.Fatal("body limit")
|
|
}
|
|
}
|
|
|
|
func TestBearerDoesNotTrustUserHeader(t *testing.T) {
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.Header.Set("X-User-Id", newID())
|
|
if bearer(r) != "" {
|
|
t.Fatal("trusted X-User-Id")
|
|
}
|
|
r.Header.Set("Authorization", "Basic dXNlcjpwYXNz")
|
|
if bearer(r) != "" {
|
|
t.Fatal("accepted Basic")
|
|
}
|
|
r.Header.Set("Authorization", "Bearer "+strings.Repeat("A", 43))
|
|
if bearer(r) == "" {
|
|
t.Fatal("rejected valid 256 bit token encoding")
|
|
}
|
|
r.Header.Set("Authorization", "Bearer "+strings.Repeat("!", 43))
|
|
if bearer(r) != "" {
|
|
t.Fatal("accepted malformed token")
|
|
}
|
|
}
|
|
|
|
func TestLogoutMissingTokenChallenge(t *testing.T) {
|
|
a := &app{}
|
|
w := httptest.NewRecorder()
|
|
a.logout(w, httptest.NewRequest(http.MethodDelete, "/api/v1/auth/sessions/current", nil))
|
|
if w.Code != 401 || w.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` {
|
|
t.Fatal("logout challenge")
|
|
}
|
|
}
|