1package webkit
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func TestSanitizeFilename(t *testing.T) {
10 tests := []struct {
11 name string
12 input string
13 expected string
14 }{
15 {
16 name: "normal filename",
17 input: "document.pdf",
18 expected: "document.pdf",
19 },
20 {
21 name: "filename with spaces",
22 input: "my document.pdf",
23 expected: "my document.pdf",
24 },
25 {
26 name: "path traversal with parent dirs",
27 input: "../../../etc/passwd",
28 expected: "passwd",
29 },
30 {
31 name: "path traversal hidden file",
32 input: "../.ssh/id_rsa",
33 expected: "id_rsa",
34 },
35 {
36 name: "nested path",
37 input: "foo/bar/baz.txt",
38 expected: "baz.txt",
39 },
40 {
41 name: "absolute path",
42 input: "/etc/passwd",
43 expected: "passwd",
44 },
45 {
46 name: "dot only",
47 input: ".",
48 expected: "download",
49 },
50 {
51 name: "double dot only",
52 input: "..",
53 expected: "download",
54 },
55 {
56 name: "empty string",
57 input: "",
58 expected: "download",
59 },
60 {
61 name: "hidden file",
62 input: ".bashrc",
63 expected: ".bashrc",
64 },
65 {
66 name: "windows style path",
67 input: "..\\..\\Windows\\System32\\config",
68 expected: "config",
69 },
70 }
71
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 result := sanitizeFilename(tt.input)
75 assert.Equal(t, tt.expected, result)
76 })
77 }
78}
79
80func TestExtractFilename(t *testing.T) {
81 tests := []struct {
82 name string
83 input string
84 expected string
85 }{
86 {
87 name: "file URI with path",
88 input: "file:///home/user/Downloads/document.pdf",
89 expected: "document.pdf",
90 },
91 {
92 name: "file URI root",
93 input: "file:///document.pdf",
94 expected: "document.pdf",
95 },
96 {
97 name: "plain path",
98 input: "/home/user/Downloads/image.png",
99 expected: "image.png",
100 },
101 {
102 name: "filename only",
103 input: "video.mp4",
104 expected: "video.mp4",
105 },
106 {
107 name: "empty string",
108 input: "",
109 expected: "download",
110 },
111 {
112 name: "file URI with spaces",
113 input: "file:///home/user/My Documents/file.txt",
114 expected: "file.txt",
115 },
116 }
117
118 for _, tt := range tests {
119 t.Run(tt.name, func(t *testing.T) {
120 result := extractFilename(tt.input)
121 assert.Equal(t, tt.expected, result)
122 })
123 }
124}
125
126func TestNewDownloadHandler(t *testing.T) {
127 t.Run("creates handler with path and nil event handler", func(t *testing.T) {
128 handler := NewDownloadHandler("/tmp/downloads", nil)
129
130 assert.NotNil(t, handler)
131 assert.Equal(t, "/tmp/downloads", handler.downloadPath)
132 assert.Nil(t, handler.eventHandler)
133 })
134
135 t.Run("creates handler with custom path", func(t *testing.T) {
136 handler := NewDownloadHandler("/custom/path", nil)
137
138 assert.Equal(t, "/custom/path", handler.downloadPath)
139 })
140}
141
142func TestDownloadHandler_SetDownloadPath(t *testing.T) {
143 handler := NewDownloadHandler("/initial/path", nil)
144
145 handler.SetDownloadPath("/new/path")
146
147 assert.Equal(t, "/new/path", handler.downloadPath)
148}