Repository navigation
Expand file tree
/
Copy pathutils_test.go
More file actions
265 lines (246 loc) · 6.83 KB
/
Copy pathutils_test.go
File metadata and controls
265 lines (246 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"bytes"
"encoding/hex"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
)
func makeTempDir(t *testing.T) string {
t.Helper()
d, err := os.MkdirTemp("", "lsget-test-")
if err != nil {
t.Fatal(err)
}
return d
}
func newTestServer(t *testing.T) *server {
root := makeTempDir(t)
return newServer(root, 4*1024, "","") // small catMax for tests
}
func TestRenderHelp(t *testing.T) {
s := renderHelp()
if !strings.Contains(s, version) {
t.Fatalf("help should contain version, got %q", s)
}
}
func TestCleanJoinRealFromVirtual(t *testing.T) {
s := newTestServer(t)
// cleanVirtual
if cleanVirtual("") != "/" {
t.Fatal("cleanVirtual empty -> /")
}
if cleanVirtual("foo/..//bar") != "/bar" {
t.Fatal("cleanVirtual normalize")
}
// joinVirtual
if joinVirtual("/a", "b") != "/a/b" {
t.Fatal("join relative")
}
if joinVirtual("/a", "/x") != "/x" {
t.Fatal("join absolute wins")
}
// realFromVirtual root
r, err := s.realFromVirtual("/")
if err != nil || r != s.rootAbs {
t.Fatalf("realFromVirtual root: %v %q", err, r)
}
// ensure normalization keeps within root
if p, err := s.realFromVirtual("/../etc"); err != nil || !strings.HasPrefix(p, s.rootAbs) {
t.Fatal("realFromVirtual should keep within root")
}
}
func TestParseArgs(t *testing.T) {
args := parseArgs(`cmd "a b" 'c d' e f`)
exp := []string{"cmd", "a b", "c d", "e", "f"}
if len(args) != len(exp) {
t.Fatalf("args len: %v", args)
}
for i := range exp {
if args[i] != exp[i] {
t.Fatalf("arg %d = %q", i, args[i])
}
}
}
func TestLooksText(t *testing.T) {
if looksText([]byte{0x00, 'a'}) {
t.Fatal("NUL should be binary")
}
if !looksText([]byte("hello")) {
t.Fatal("simple ascii is text")
}
if !looksText([]byte{}) {
t.Fatal("empty is text")
}
}
func TestGetFileColorAndColorizeName(t *testing.T) {
root := makeTempDir(t)
// dir
dir := filepath.Join(root, "dir")
if err := os.Mkdir(dir, 0o755); err != nil {
t.Fatal(err)
}
info, _ := os.Stat(dir)
if c := getFileColor(info, "dir"); c != colorBlue+colorBold {
t.Fatalf("dir color: %q", c)
}
// executable
exe := filepath.Join(root, "exe.sh")
if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
info, _ = os.Stat(exe)
if c := getFileColor(info, "exe.sh"); c != colorGreen {
t.Fatalf("exe color: %q", c)
}
// symlink
ln := filepath.Join(root, "link")
if err := os.Symlink(exe, ln); err != nil {
t.Fatal(err)
}
linfo, _ := os.Lstat(ln)
if c := getFileColor(linfo, "link"); c != colorGreen {
t.Fatalf("symlink color: %q", c)
}
// extension mapping
mp := filepath.Join(root, "f.zip")
if err := os.WriteFile(mp, []byte("z"), 0o644); err != nil {
t.Fatal(err)
}
minfo, _ := os.Stat(mp)
if c := getFileColor(minfo, "f.zip"); c != colorRed {
t.Fatalf("zip color: %q", c)
}
// colorize wraps
name := colorizeName(minfo, "f.zip")
if !strings.HasPrefix(name, colorRed) || !strings.HasSuffix(name, colorReset) {
t.Fatalf("colorizeName not wrapped: %q", name)
}
}
func TestFormatLong(t *testing.T) {
root := makeTempDir(t)
f := filepath.Join(root, "file.txt")
data := []byte("abc")
if err := os.WriteFile(f, data, 0o644); err != nil {
t.Fatal(err)
}
info, _ := os.Stat(f)
line := formatLong(info, "name", false)
if !strings.Contains(line, "name") || !strings.Contains(line, info.Mode().String()) {
t.Fatalf("formatLong: %q", line)
}
}
func TestURLEscapeHelpers(t *testing.T) {
v := "/a b/c#d?e&f+g%h"
esc := urlEscapeVirtual(v)
if !strings.Contains(esc, "%20") || !strings.HasPrefix(esc, "/") {
t.Fatalf("urlEscapeVirtual: %q", esc)
}
// url.PathEscape is used internally, verify it escapes spaces
if !strings.Contains(url.PathEscape("a b"), "%20") {
t.Fatal("url.PathEscape space")
}
}
func TestNewSIDAndGetSession(t *testing.T) {
id := newSID()
if len(id) != 32 {
t.Fatalf("sid length: %d", len(id))
}
if _, err := hex.DecodeString(id); err != nil {
t.Fatalf("sid hex: %v", err)
}
s := newTestServer(t)
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
sess := s.getSession(w, r)
if sess == nil || sess.cwd != "/" {
t.Fatal("new session cwd")
}
ck := w.Result().Cookies()
if len(ck) == 0 || ck[0].Name != "sid" {
t.Fatal("sid cookie not set")
}
// Second time with cookie should return existing
r2 := httptest.NewRequest("GET", "/", nil)
r2.AddCookie(ck[0])
w2 := httptest.NewRecorder()
sess2 := s.getSession(w2, r2)
if sess2 != sess {
t.Fatal("session not reused")
}
}
func TestProcessHTMLTemplate(t *testing.T) {
s := newTestServer(t)
html := []byte("<html>{{HELP_MESSAGE}}::{{INITIAL_PATH}}</html>")
out := s.processHTMLTemplate(html, "/foo/bar")
so := string(out)
if strings.Contains(so, "{{HELP_MESSAGE}}") {
t.Fatal("HELP_MESSAGE not replaced")
}
if !strings.Contains(so, "/foo/bar") {
t.Fatal("INITIAL_PATH not injected")
}
}
func TestHandleIndexServesAndFile(t *testing.T) {
s := newTestServer(t)
// create a file in root
fp := filepath.Join(s.rootAbs, "data.txt")
_ = os.WriteFile(fp, []byte("DATA"), 0o644)
// request /
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
s.handleIndex(w, r)
if w.Result().StatusCode != 200 {
t.Fatalf("index status: %d", w.Result().StatusCode)
}
// request file
w2 := httptest.NewRecorder()
r2 := httptest.NewRequest("GET", path.Join("/", "data.txt"), nil)
s.handleIndex(w2, r2)
if ct := w2.Result().Header.Get("Content-Type"); !strings.Contains(ct, "text/plain") && ct != "application/octet-stream" {
t.Fatalf("serveFile content-type: %q", ct)
}
}
func TestHandleStaticFile(t *testing.T) {
s := newTestServer(t)
fp := filepath.Join(s.rootAbs, "static.js")
_ = os.WriteFile(fp, []byte("var x=1;"), 0o644)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/static/static.js", nil)
s.handleStaticFile(w, r)
if w.Code != 200 {
t.Fatalf("static status: %d", w.Code)
}
}
func TestLogRequests(t *testing.T) {
h := httpHandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) })
wrapped := logRequests(h)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/ping", nil)
wrapped.ServeHTTP(w, r)
if w.Code != 204 {
t.Fatalf("status: %d", w.Code)
}
}
// small adapter to avoid importing net/http in top list twice
// (keeps imports tidy without aliasing)
type httpHandlerFunc func(http.ResponseWriter, *http.Request)
func (f httpHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) { f(w, r) }
func TestFormatLongTimeStability(t *testing.T) {
// Ensure formatLong uses ModTime without panics
root := makeTempDir(t)
f := filepath.Join(root, "t.bin")
if err := os.WriteFile(f, bytes.Repeat([]byte{'A'}, 10), 0o644); err != nil {
t.Fatal(err)
}
info, _ := os.Stat(f)
_ = info.ModTime().Format("Jan _2 15:04")
_ = formatLong(info, "x", false)
_ = time.Now() // just touch time to ensure import used in test logic
}