63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package comicanimator
|
|
|
|
import (
|
|
"bytes"
|
|
"html/template"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSignedURLTampering(t *testing.T) {
|
|
dir := t.TempDir()
|
|
tool := &Tool{cfg: Config{PublicBaseURL: "https://example.test", SigningSecret: string(make([]byte, 32)), SignedURLTTL: time.Minute}, store: newStore()}
|
|
u := Upload{ID: "upl_test", Path: filepath.Join(dir, "x.png")}
|
|
os.WriteFile(u.Path, []byte("x"), 0600)
|
|
tool.store.putUpload(u)
|
|
raw := tool.signedURL(u.ID)
|
|
parsed, err := url.Parse(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if parsed.Query().Get("signature") == "" || parsed.Query().Get("expires") == "" {
|
|
t.Fatal("missing signature fields")
|
|
}
|
|
if !stringsContains(parsed.Path, u.ID) {
|
|
t.Fatal("missing upload id")
|
|
}
|
|
}
|
|
func stringsContains(s, part string) bool { return len(s) >= len(part) && s[len(s)-len(part):] == part }
|
|
func TestValidateWebP(t *testing.T) {
|
|
good := append([]byte("RIFF\x10\x00\x00\x00WEBPVP8X"), make([]byte, 4)...)
|
|
if err := validateWebP(good); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := validateWebP([]byte("not webp")); err == nil {
|
|
t.Fatal("corrupt webp accepted")
|
|
}
|
|
}
|
|
|
|
func TestTemplatesExecute(t *testing.T) {
|
|
if err := template.Must(template.New("tool").Parse(toolPage)).Execute(&bytes.Buffer{}, map[string]any{"CSRF": "x", "Duration": 6, "AspectRatio": "16:9"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := template.Must(template.New("instructor").Parse(instructorPage)).Execute(&bytes.Buffer{}, map[string]any{"Outputs": []outputFile{{Name: "safe.mp4", Size: 10, Modified: time.Now()}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestVideoSignatures(t *testing.T) {
|
|
mp4 := append([]byte{0, 0, 0, 16}, []byte("ftypisom")...)
|
|
if !validVideoHeader("video/mp4", mp4) {
|
|
t.Fatal("valid MP4 rejected")
|
|
}
|
|
if validVideoHeader("video/mp4", []byte("not a video")) {
|
|
t.Fatal("invalid MP4 accepted")
|
|
}
|
|
if !validVideoHeader("video/webm", []byte{0x1a, 0x45, 0xdf, 0xa3}) {
|
|
t.Fatal("valid WebM rejected")
|
|
}
|
|
}
|