Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package fileserver |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "testing" |
| 22 | "time" |
| 23 | |
| 24 | "github.com/caddyserver/caddy/v2" |
| 25 | ) |
| 26 | |
| 27 | func TestBrowseOffsetPastEnd(t *testing.T) { |
| 28 | listing := browseTemplateContext{Items: []fileInfo{{Name: "a"}, {Name: "b"}}} |
| 29 | listing.applySortAndLimit(sortByName, sortOrderAsc, "1", "3") |
| 30 | if len(listing.Items) != 0 { |
| 31 | t.Errorf("got %d items past the end; want none", len(listing.Items)) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestBreadcrumbs(t *testing.T) { |
| 36 | testdata := []struct { |
| 37 | path string |
| 38 | expected []crumb |
| 39 | }{ |
| 40 | {"", []crumb{}}, |
| 41 | {"/", []crumb{{Text: "/"}}}, |
| 42 | {"/foo/", []crumb{ |
| 43 | {Link: "../", Text: "/"}, |
| 44 | {Link: "", Text: "foo"}, |
| 45 | }}, |
| 46 | {"/foo/bar/", []crumb{ |
| 47 | {Link: "../../", Text: "/"}, |
| 48 | {Link: "../", Text: "foo"}, |
| 49 | {Link: "", Text: "bar"}, |
| 50 | }}, |
| 51 | {"/foo bar/", []crumb{ |
| 52 | {Link: "../", Text: "/"}, |
| 53 | {Link: "", Text: "foo bar"}, |
| 54 | }}, |
| 55 | {"/foo bar/baz/", []crumb{ |
| 56 | {Link: "../../", Text: "/"}, |
| 57 | {Link: "../", Text: "foo bar"}, |
| 58 | {Link: "", Text: "baz"}, |
| 59 | }}, |
| 60 | {"/100%25 test coverage/is a lie/", []crumb{ |
| 61 | {Link: "../../", Text: "/"}, |
| 62 | {Link: "../", Text: "100% test coverage"}, |
| 63 | {Link: "", Text: "is a lie"}, |
| 64 | }}, |
| 65 | {"/AC%2FDC/", []crumb{ |
| 66 | {Link: "../", Text: "/"}, |
| 67 | {Link: "", Text: "AC/DC"}, |
| 68 | }}, |
| 69 | {"/foo/%2e%2e%2f/bar", []crumb{ |
| 70 | {Link: "../../../", Text: "/"}, |
| 71 | {Link: "../../", Text: "foo"}, |
| 72 | {Link: "../", Text: "../"}, |
| 73 | {Link: "", Text: "bar"}, |
| 74 | }}, |
| 75 | {"/foo/../bar", []crumb{ |
| 76 | {Link: "../../../", Text: "/"}, |
| 77 | {Link: "../../", Text: "foo"}, |
| 78 | {Link: "../", Text: ".."}, |
| 79 | {Link: "", Text: "bar"}, |
| 80 | }}, |
| 81 | {"foo/bar/baz", []crumb{ |
| 82 | {Link: "../../", Text: "foo"}, |
| 83 | {Link: "../", Text: "bar"}, |
| 84 | {Link: "", Text: "baz"}, |
| 85 | }}, |
| 86 | {"/qux/quux/corge/", []crumb{ |
| 87 | {Link: "../../../", Text: "/"}, |
| 88 | {Link: "../../", Text: "qux"}, |
| 89 | {Link: "../", Text: "quux"}, |
| 90 | {Link: "", Text: "corge"}, |
| 91 | }}, |
| 92 | {"/مجلد/", []crumb{ |
| 93 | {Link: "../", Text: "/"}, |
| 94 | {Link: "", Text: "مجلد"}, |
| 95 | }}, |
| 96 | {"/مجلد-1/مجلد-2", []crumb{ |
| 97 | {Link: "../../", Text: "/"}, |
| 98 | {Link: "../", Text: "مجلد-1"}, |
| 99 | {Link: "", Text: "مجلد-2"}, |
| 100 | }}, |
| 101 | {"/مجلد%2F1", []crumb{ |
| 102 | {Link: "../", Text: "/"}, |
| 103 | {Link: "", Text: "مجلد/1"}, |
| 104 | }}, |
| 105 | } |
| 106 | |
| 107 | for testNum, d := range testdata { |
| 108 | l := browseTemplateContext{Path: d.path} |
| 109 | actual := l.Breadcrumbs() |
| 110 | if len(actual) != len(d.expected) { |
| 111 | t.Errorf("Test %d: Got %d components but expected %d; got: %+v", testNum, len(actual), len(d.expected), actual) |
| 112 | continue |
| 113 | } |
| 114 | for i, c := range actual { |
| 115 | if c != d.expected[i] { |
| 116 | t.Errorf("Test %d crumb %d: got %#v but expected %#v at index %d", testNum, i, c, d.expected[i], i) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestDirectoryListingSymlinkInEscapedPath(t *testing.T) { |
| 123 | root := t.TempDir() |
| 124 | target := filepath.Join(root, "target") |
| 125 | if err := os.Mkdir(target, 0o755); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | |
| 129 | for i, tc := range []struct { |
| 130 | dir string |
| 131 | urlPath string // escaped, as serveBrowse passes it |
| 132 | }{ |
| 133 | {dir: "plain", urlPath: "/plain"}, |
| 134 | {dir: "with space", urlPath: "/with%20space"}, |
| 135 | {dir: "ünïcode", urlPath: "/%C3%BCn%C3%AFcode"}, |
| 136 | {dir: "100%", urlPath: "/100%25"}, |
| 137 | } { |
| 138 | dir := filepath.Join(root, tc.dir) |
| 139 | if err := os.Mkdir(dir, 0o755); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | if err := os.Symlink(target, filepath.Join(dir, "link")); err != nil { |
| 143 | t.Skipf("symlink not supported on this platform: %v", err) |
| 144 | } |
| 145 | |
| 146 | fileSystem, entries := readBenchDirEntries(t, dir) |
| 147 | listing := benchFileServer().directoryListing(context.Background(), fileSystem, time.Time{}, entries, true, dir, tc.urlPath, caddy.NewReplacer()) |
| 148 | if len(listing.Items) != 1 || !listing.Items[0].IsDir { |
| 149 | t.Errorf("Test %d: expected the symlink in %q to be listed as a directory", i, tc.dir) |
| 150 | } |
| 151 | } |
| 152 | } |