Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
131 lines (112 loc) · 3.35 KB
/
Copy pathserver_test.go
File metadata and controls
131 lines (112 loc) · 3.35 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
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestCORSHeaders(t *testing.T) {
handler := middlewareCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
req := httptest.NewRequest("OPTIONS", "/api/upload", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected OPTIONS response to be 200, got %d", rec.Code)
}
if rec.Header().Get("Access-Control-Allow-Origin") != "*" {
t.Errorf("missing CORS Origin header")
}
}
func TestDownloadSecurity(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("GET /api/download/{id}", handleDownloadPGTX)
mux.HandleFunc("GET /api/download-md/{id}", handleDownloadMD)
// Create a dummy test file in the presetDir
_ = os.MkdirAll(presetDir, 0755)
defer os.RemoveAll(presetDir)
dummyFile := filepath.Join(presetDir, "test-id.pgtx")
_ = os.WriteFile(dummyFile, []byte("fake zip"), 0644)
dummyMdFile := filepath.Join(presetDir, "test-id.md")
_ = os.WriteFile(dummyMdFile, []byte("# Manual Guide"), 0644)
// Test successful downloads and not founds using mux routing
routeTests := []struct {
name string
url string
expectCode int
}{
{
name: "Download existing preset",
url: "/api/download/test-id",
expectCode: http.StatusOK,
},
{
name: "Download existing guide",
url: "/api/download-md/test-id",
expectCode: http.StatusOK,
},
{
name: "Preset not found",
url: "/api/download/nonexistent",
expectCode: http.StatusNotFound,
},
{
name: "Guide not found",
url: "/api/download-md/nonexistent",
expectCode: http.StatusNotFound,
},
}
for _, tt := range routeTests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", tt.url, nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != tt.expectCode {
t.Errorf("expected response code %d, got %d for %s", tt.expectCode, rec.Code, tt.url)
}
})
}
// Test path traversal protection directly on the handlers using SetPathValue
traversalTests := []struct {
name string
handler http.HandlerFunc
idVal string
expectCode int
}{
{
name: "Path traversal block PGTX",
handler: handleDownloadPGTX,
idVal: "../../etc/passwd",
expectCode: http.StatusForbidden,
},
{
name: "Path traversal block MD",
handler: handleDownloadMD,
idVal: "../../etc/passwd",
expectCode: http.StatusForbidden,
},
}
for _, tt := range traversalTests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/download/ignored", nil)
req.SetPathValue("id", tt.idVal)
rec := httptest.NewRecorder()
tt.handler(rec, req)
if rec.Code != tt.expectCode {
t.Errorf("expected response code %d, got %d for id %s", tt.expectCode, rec.Code, tt.idVal)
}
})
}
}
func TestUploadInvalidForm(t *testing.T) {
req := httptest.NewRequest("POST", "/api/upload", strings.NewReader("bad content"))
req.Header.Set("Content-Type", "application/json") // Should trigger form parsing failure since it's not multipart
rec := httptest.NewRecorder()
handleUpload(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("expected upload response to be 400 Bad Request, got %d", rec.Code)
}
}