Files

72 lines
2.4 KiB
Go
Raw Permalink Normal View History

package media
import (
2026-11-17 13:35:39 +01:00
"net/http"
"testing"
)
2026-11-17 13:35:39 +01:00
func TestUpstreamMediaPathRefusesTraversal(t *testing.T) {
for _, stored := range []string{"../x.mp4", "media/../../x.mp4", `..\x.mp4`} {
if _, ok := upstreamMediaPath(stored); ok {
t.Errorf("upstreamMediaPath(%q) accepted, want refused", stored)
}
}
}
2026-11-17 13:35:39 +01:00
// CarrierNode's router matches a raw request path literally, so a name must reach it with no raw path:
// a %2C for a comma would never match "Hello, World.mp4". Query and fragment markers are escaped, not refused.
func TestUpstreamMediaPathReachesCarrierNodeAsTheLiteralName(t *testing.T) {
for _, stored := range []string{"Hello, World.mp4", "a;b.mp4", "x?y.mp4", "a#b.mp4", "100%.mp4", "dir one/x y.mp4"} {
upstream, ok := upstreamMediaPath(stored)
if !ok {
t.Fatalf("upstreamMediaPath(%q) refused", stored)
}
request, err := http.NewRequest(http.MethodGet, "http://carrier.example/media/"+upstream, nil)
if err != nil {
t.Fatalf("NewRequest for %q: %v", stored, err)
}
if request.URL.RawPath != "" {
t.Errorf("%q produced raw path %q; CarrierNode would match that instead of the name", stored, request.URL.RawPath)
}
if want := "/media/" + urlPath(stored); request.URL.Path != want {
t.Errorf("%q decodes to %q, want %q", stored, request.URL.Path, want)
}
}
}
func TestUpstreamMediaPathEscapesEachSegment(t *testing.T) {
cases := map[string]string{
2026-11-17 13:35:39 +01:00
"clips/clip.mp4": "clips/clip.mp4",
"my clip.mp4": "my%20clip.mp4",
"100%.mp4": "100%25.mp4",
"media/a b/c.mp4": "a%20b/c.mp4",
"/leading.mp4": "leading.mp4",
"Hello, World.mp4": "Hello,%20World.mp4",
"a;b.mp4": "a;b.mp4",
"x?y.mp4": "x%3Fy.mp4",
"a#b.mp4": "a%23b.mp4",
}
for stored, want := range cases {
got, ok := upstreamMediaPath(stored)
if !ok || got != want {
t.Errorf("upstreamMediaPath(%q) = %q, %v; want %q", stored, got, ok, want)
}
}
}
func TestCanonicalMediaIDAcceptsOnlyCanonicalDecimals(t *testing.T) {
for _, value := range []string{"", "007", "+1", "-1", "4a", " 4", "4 ", "99999999999999999999"} {
if id, ok := canonicalMediaID(value); ok {
t.Errorf("canonicalMediaID(%q) = %d, want refused", value, id)
}
}
if id, ok := canonicalMediaID("42"); !ok || id != 42 {
t.Errorf("canonicalMediaID(42) = %d, %v; want 42, true", id, ok)
}
if mediaIDArg("abc") != nil {
t.Error("mediaIDArg(non-numeric) is not NULL")
}
if mediaIDArg("42") != int64(42) {
t.Error("mediaIDArg(42) is not 42")
}
}