28 lines
919 B
Go
28 lines
919 B
Go
package attachment_test
|
|
|
|
import (
|
|
"context"
|
|
"messenger/internal/testenv"
|
|
"testing"
|
|
)
|
|
|
|
func TestFilesSchemaRejectsMalformedHashes(t *testing.T) {
|
|
db, err := testenv.StartPostgres(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
_, err = db.Pool.Exec(context.Background(), `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES (decode(repeat('ab',32),'hex'),12,'image/png','ready')`)
|
|
if err != nil {
|
|
t.Fatalf("valid content-addressed file: %v", err)
|
|
}
|
|
_, err = db.Pool.Exec(context.Background(), `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES ('bad',12,'image/png','ready')`)
|
|
if err == nil {
|
|
t.Fatal("accepted a malformed SHA256")
|
|
}
|
|
_, err = db.Pool.Exec(context.Background(), `INSERT INTO files(hash,size_bytes,mime_type,status) VALUES (decode(repeat('ab',32),'hex'),12,'image/png','ready')`)
|
|
if err == nil {
|
|
t.Fatal("accepted duplicate content hash")
|
|
}
|
|
}
|