-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
116 lines (103 loc) · 2.44 KB
/
main_test.go
File metadata and controls
116 lines (103 loc) · 2.44 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
package main
import (
"os"
"path/filepath"
"testing"
)
func TestValidatePath(t *testing.T) {
tests := []struct {
name string
path string
wantErr bool
}{
{
name: "valid home directory path",
path: os.Getenv("HOME") + "/Downloads",
wantErr: false,
},
{
name: "valid temp directory path",
path: os.TempDir(),
wantErr: false,
},
{
name: "empty path",
path: "",
wantErr: true,
},
{
name: "path with traversal",
path: "/tmp/../etc/passwd",
wantErr: true,
},
{
name: "path with double dots",
path: "/home/user/../../etc",
wantErr: true,
},
{
name: "system directory outside allowed",
path: "/etc",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validatePath(tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("validatePath() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetDefaultDownloadsPath(t *testing.T) {
path, err := getDefaultDownloadsPath()
if err != nil {
t.Errorf("getDefaultDownloadsPath() error = %v", err)
return
}
if path == "" {
t.Error("getDefaultDownloadsPath() returned empty path")
}
// Check if the path exists or can be created
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
t.Logf("Downloads directory does not exist: %s", path)
} else if err != nil {
t.Errorf("Error checking downloads path: %v", err)
}
}
func TestFileInfo(t *testing.T) {
// Create a temporary file for testing
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.txt")
err := os.WriteFile(tmpFile, []byte("test content"), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Test FileInfo creation
info, err := os.Stat(tmpFile)
if err != nil {
t.Fatalf("Failed to stat test file: %v", err)
}
fileInfo := FileInfo{
Path: tmpFile,
Name: info.Name(),
Size: info.Size(),
Extension: ".txt",
Category: "Documents",
Hash: "",
LastModified: info.ModTime(),
IsDuplicate: false,
IsZip: false,
}
if fileInfo.Name != "test.txt" {
t.Errorf("Expected name 'test.txt', got '%s'", fileInfo.Name)
}
if fileInfo.Size != int64(len("test content")) {
t.Errorf("Expected size %d, got %d", len("test content"), fileInfo.Size)
}
if fileInfo.Category != "Documents" {
t.Errorf("Expected category 'Documents', got '%s'", fileInfo.Category)
}
}