Browse Source

v2: Make tests work on Windows (#2782)

* file_server: Make tests work on Windows

* caddyfile: Fix escaping when character is not escapable

We only escape certain characters depending on inside or outside of
quotes (mainly newlines and quotes). We don't want everyone to have to
escape Windows file paths like C:\\Windows\\... but we can't drop the
\ either if it's just C:\Windows\...
master
Matt Holt 6 years ago
committed by GitHub
parent
commit
2f91b44587
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      caddyconfig/caddyfile/lexer.go
  2. 6
      caddyconfig/caddyfile/lexer_test.go
  3. 26
      modules/caddyhttp/fileserver/staticfiles_test.go

5
caddyconfig/caddyfile/lexer.go

@ -153,6 +153,11 @@ func (l *lexer) next() bool {
} }
} }
if escaped {
val = append(val, '\\')
escaped = false
}
val = append(val, ch) val = append(val, ch)
} }
} }

6
caddyconfig/caddyfile/lexer_test.go

@ -151,6 +151,12 @@ func TestLexer(t *testing.T) {
{Line: 1, Text: `don't\\escape`}, {Line: 1, Text: `don't\\escape`},
}, },
}, },
{
input: `un\escapable`,
expected: []Token{
{Line: 1, Text: `un\escapable`},
},
},
{ {
input: `A "quoted value with line input: `A "quoted value with line
break inside" { break inside" {

26
modules/caddyhttp/fileserver/staticfiles_test.go

@ -16,14 +16,15 @@ package fileserver
import ( import (
"net/url" "net/url"
"path/filepath"
"testing" "testing"
) )
func TestSanitizedPathJoin(t *testing.T) { func TestSanitizedPathJoin(t *testing.T) {
// For easy reference: // For easy reference:
// %2E = . // %2e = .
// %2F = / // %2f = /
// %5C = \ // %5c = \
for i, tc := range []struct { for i, tc := range []struct {
inputRoot string inputRoot string
inputPath string inputPath string
@ -43,12 +44,12 @@ func TestSanitizedPathJoin(t *testing.T) {
}, },
{ {
inputPath: "/foo/bar", inputPath: "/foo/bar",
expect: "foo/bar", expect: filepath.Join("foo", "bar"),
}, },
{ {
inputRoot: "/a", inputRoot: "/a",
inputPath: "/foo/bar", inputPath: "/foo/bar",
expect: "/a/foo/bar", expect: filepath.Join("/", "a", "foo", "bar"),
}, },
{ {
inputPath: "/foo/../bar", inputPath: "/foo/../bar",
@ -57,24 +58,29 @@ func TestSanitizedPathJoin(t *testing.T) {
{ {
inputRoot: "/a/b", inputRoot: "/a/b",
inputPath: "/foo/../bar", inputPath: "/foo/../bar",
expect: "/a/b/bar", expect: filepath.Join("/", "a", "b", "bar"),
}, },
{ {
inputRoot: "/a/b", inputRoot: "/a/b",
inputPath: "/..%2fbar", inputPath: "/..%2fbar",
expect: "/a/b/bar", expect: filepath.Join("/", "a", "b", "bar"),
}, },
{ {
inputRoot: "/a/b", inputRoot: "/a/b",
inputPath: "/%2e%2e%2fbar", inputPath: "/%2e%2e%2fbar",
expect: "/a/b/bar", expect: filepath.Join("/", "a", "b", "bar"),
}, },
{ {
inputRoot: "/a/b", inputRoot: "/a/b",
inputPath: "/%2e%2e%2f%2e%2e%2f", inputPath: "/%2e%2e%2f%2e%2e%2f",
expect: "/a/b", expect: filepath.Join("/", "a", "b"),
}, },
// TODO: test windows paths... on windows... sigh. {
inputRoot: "C:\\www",
inputPath: "/foo/bar",
expect: filepath.Join("C:\\www", "foo", "bar"),
},
// TODO: test more windows paths... on windows... sigh.
} { } {
// we don't *need* to use an actual parsed URL, but it // we don't *need* to use an actual parsed URL, but it
// adds some authenticity to the tests since real-world // adds some authenticity to the tests since real-world

Loading…
Cancel
Save