@@ -0,0 +1,65 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestArtifactLocalPathRejectsTraversal(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if _, err := artifactLocalPath(root, "/artifacts/../secret.txt"); err == nil {
|
||||
t.Fatal("expected traversal rejection")
|
||||
}
|
||||
got, err := artifactLocalPath(root, "/artifacts/a/image.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := filepath.Join(root, "a", "image.png")
|
||||
if got != want {
|
||||
t.Fatalf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatermarkSVG(t *testing.T) {
|
||||
out, err := watermarkSVG([]byte(`<svg xmlns="http://www.w3.org/2000/svg"><rect width="10" height="10"/></svg>`), "NEURAL HUNT PREVIEW")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(out), "WATERMARKED LEADERBOARD PREVIEW") || !strings.Contains(string(out), "nh-preview-watermark") {
|
||||
t.Fatalf("watermark missing: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatermarkRasterProducesPNG(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "source.png")
|
||||
img := image.NewNRGBA(image.Rect(0, 0, 320, 240))
|
||||
for y := 0; y < 240; y++ {
|
||||
for x := 0; x < 320; x++ {
|
||||
img.Set(x, y, color.NRGBA{20, 40, 60, 255})
|
||||
}
|
||||
}
|
||||
var src bytes.Buffer
|
||||
if err := png.Encode(&src, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, src.Bytes(), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, ct, err := watermarkPreviewFile(path, "NEURAL HUNT PREVIEW")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ct != "image/png" || len(out) == 0 {
|
||||
t.Fatalf("unexpected preview %q %d", ct, len(out))
|
||||
}
|
||||
if bytes.Equal(out, src.Bytes()) {
|
||||
t.Fatal("preview should differ from source")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user