forked from mtusi2026/mtusi2026lab2
fixed a bug with NUL in strings
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -128,3 +129,49 @@ func TestMessageRequiresMembership(t *testing.T) {
|
||||
t.Fatalf("err = %v, want ErrNotFound (→ 404)", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNULRoundTrip(t *testing.T) {
|
||||
d, _ := testDB(t)
|
||||
ctx := context.Background()
|
||||
const name = "A\x00B\\0\\"
|
||||
u, err := d.CreateUser(ctx, "nul_"+suffix(), name, "$argon2id$test")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
if got, err := d.UserByID(ctx, u.ID); err != nil || got.DisplayName != name {
|
||||
t.Fatalf("UserByID = %q, %v; want %q", got.DisplayName, err, name)
|
||||
}
|
||||
if _, _, err := d.CredentialsByUsername(ctx, "a\x00b"); !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Fatalf("CredentialsByUsername(NUL) err = %v, want ErrNotFound", err)
|
||||
}
|
||||
|
||||
chat, err := d.CreateChat(ctx, u.ID, name, []string{u.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChat: %v", err)
|
||||
}
|
||||
if got, err := d.ChatByID(ctx, u.ID, chat.ID); err != nil || got.Title != name {
|
||||
t.Fatalf("ChatByID = %q, %v; want %q", got.Title, err, name)
|
||||
}
|
||||
|
||||
texts := []string{"\x00", "a\x00b", `\0`, `\\`, strings.Repeat("\x00", 4000), strings.Repeat(`\`, 4000)}
|
||||
for _, text := range texts {
|
||||
if _, err := d.AddMessage(ctx, u.ID, chat.ID, text); err != nil {
|
||||
t.Fatalf("AddMessage(%q...): %v", text[:min(len(text), 8)], err)
|
||||
}
|
||||
}
|
||||
if _, err := d.AddMessage(ctx, u.ID, chat.ID, strings.Repeat(`\`, 4001)); !errors.Is(err, domain.ErrInvalidArgument) {
|
||||
t.Fatalf("AddMessage(4001 code points) err = %v, want ErrInvalidArgument", err)
|
||||
}
|
||||
page, err := d.HistoryPage(ctx, chat.ID, "", 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != len(texts) {
|
||||
t.Fatalf("history len = %d, want %d", len(page.Items), len(texts))
|
||||
}
|
||||
for i, m := range page.Items {
|
||||
if m.Text != texts[i] {
|
||||
t.Fatalf("history[%d] = %q, want %q", i, m.Text, texts[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user