forked from pactus-project/pactus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_test.go
239 lines (198 loc) · 6.03 KB
/
io_test.go
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
package util
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const invalidDirName = "/invalid:path/\x00*folder?\\CON"
func TestWriteFile(t *testing.T) {
p := TempDirPath()
d := []byte("some-data")
assert.NoError(t, WriteFile(p+"/d.dat", d))
assert.NoError(t, WriteFile(p+"/another-folder/d.dat", d))
}
func TestEmptyPath(t *testing.T) {
p := TempDirPath()
assert.Equal(t, p, MakeAbs(p))
assert.True(t, IsDirEmpty(p))
f := TempFilePath()
d := []byte("pactus")
assert.NoError(t, WriteFile(f, d))
o, err := ReadFile(f)
assert.NoError(t, err)
assert.Equal(t, d, o)
}
func TestAbsPath(t *testing.T) {
abs := MakeAbs(".")
assert.True(t, IsAbsPath(abs))
assert.False(t, IsAbsPath("abs"))
assert.False(t, IsDirEmpty(abs))
assert.False(t, IsDirNotExistsOrEmpty(abs))
}
func TestTempDir(t *testing.T) {
tmpDir := TempDirPath()
assert.True(t, IsAbsPath(tmpDir))
assert.True(t, IsDirEmpty(tmpDir))
assert.True(t, PathExists(tmpDir))
assert.True(t, IsDirNotExistsOrEmpty(tmpDir))
}
func TestTempFile(t *testing.T) {
tmpFile := TempFilePath()
assert.True(t, IsAbsPath(tmpFile))
t.Run("Should panic because it doesn't exist", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
IsDirEmpty(tmpFile)
})
assert.False(t, PathExists(tmpFile))
assert.True(t, IsDirNotExistsOrEmpty(tmpFile))
assert.NoError(t, Mkdir(tmpFile))
assert.True(t, IsDirNotExistsOrEmpty(tmpFile))
assert.True(t, IsDirEmpty(tmpFile)) // no panic now
}
func isRoot() bool {
cmd := exec.Command("id", "-u")
output, err := cmd.Output()
if err != nil {
fmt.Println(err)
}
// 0 = root, 501 = non-root user
i, err := strconv.Atoi(string(output[:len(output)-1]))
if err != nil {
fmt.Println(err)
}
return i == 0
}
func TestIsValidPath(t *testing.T) {
// To pass this tests inside docker
if runtime.GOOS != "windows" && !isRoot() {
assert.False(t, IsValidDirPath("/root"))
assert.False(t, IsValidDirPath("/test"))
}
assert.False(t, IsValidDirPath(invalidDirName))
assert.False(t, IsValidDirPath("./io_test.go"))
assert.True(t, IsValidDirPath("/tmp"))
assert.True(t, IsValidDirPath("/tmp/pactus"))
}
func TestMoveDirectory(t *testing.T) {
t.Run("DestinationDirectoryExistsAndNotEmpty", func(t *testing.T) {
srcDir := TempDirPath()
dstDir := TempDirPath()
err := MoveDirectory(srcDir, dstDir)
assert.Error(t, err)
assert.Contains(t, err.Error(), "destination directory")
})
t.Run("ParentDirectoryCreationFailure", func(t *testing.T) {
srcDir := TempDirPath()
err := MoveDirectory(srcDir, invalidDirName)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to create parent directories")
})
t.Run("SourceDirectoryRenameFailure", func(t *testing.T) {
srcDir := TempDirPath()
dstDir := TempDirPath()
err := os.RemoveAll(dstDir)
assert.NoError(t, err)
// Remove the source directory to simulate the rename failure
err = os.RemoveAll(srcDir)
assert.NoError(t, err)
err = MoveDirectory(srcDir, dstDir)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to move directory")
})
t.Run("MoveDirectorySuccess", func(t *testing.T) {
// Create temporary directories
srcDir := TempDirPath()
dstDir := TempDirPath()
defer func() { _ = os.RemoveAll(srcDir) }()
defer func() { _ = os.RemoveAll(dstDir) }()
// Create a subdirectory in the source directory
subDir := filepath.Join(srcDir, "subdir")
err := Mkdir(subDir)
assert.NoError(t, err)
// Create multiple files in the subdirectory
files := []struct {
name string
content string
}{
{"file1.txt", "content 1"},
{"file2.txt", "content 2"},
}
for _, file := range files {
filePath := filepath.Join(subDir, file.name)
err = WriteFile(filePath, []byte(file.content))
assert.NoError(t, err)
}
// Move the directory
dstDirPath := filepath.Join(dstDir, "movedir")
err = MoveDirectory(srcDir, dstDirPath)
assert.NoError(t, err)
// Assert the source directory no longer exists
assert.False(t, PathExists(srcDir))
// Assert the destination directory exists
assert.True(t, PathExists(dstDirPath))
// Verify that all files have been moved and their contents are correct
for _, file := range files {
movedFilePath := filepath.Join(dstDirPath, "subdir", file.name)
data, err := ReadFile(movedFilePath)
assert.NoError(t, err)
assert.Equal(t, file.content, string(data))
}
})
}
func TestSanitizeArchivePath(t *testing.T) {
if runtime.GOOS == "windows" {
return
}
baseDir := "/safe/directory"
tests := []struct {
name string
inputPath string
expected string
expectErr bool
}{
{"Valid path", "file.txt", "/safe/directory/file.txt", false},
{"Valid path in subdirectory", "subdir/file.txt", "/safe/directory/subdir/file.txt", false},
{"Path with parent directory traversal", "../outside/file.txt", "", true},
{"Absolute path outside base directory", "/etc/passwd", "/safe/directory/etc/passwd", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := SanitizeArchivePath(baseDir, tt.inputPath)
if tt.expectErr {
assert.Error(t, err, "Expected error but got none")
assert.Empty(t, result, "Expected empty result due to error")
} else {
assert.NoError(t, err, "Unexpected error occurred")
assert.Equal(t, tt.expected, result, "Sanitized path did not match expected")
}
})
}
}
func TestListFilesInDir(t *testing.T) {
tmpDir := TempDirPath()
file1Path := filepath.Join(tmpDir, "public_file")
file1, err := os.Create(file1Path)
require.NoError(t, err)
require.NoError(t, file1.Close())
file2Path := filepath.Join(tmpDir, ".hidden_file")
file2, err := os.Create(file2Path)
require.NoError(t, err)
require.NoError(t, file2.Close())
err = os.Mkdir(filepath.Join(tmpDir, "directory"), 0o750)
require.NoError(t, err)
files, err := ListFilesInDir(tmpDir)
require.NoError(t, err)
assert.Len(t, files, 2)
assert.Contains(t, files, file1Path)
assert.Contains(t, files, file2Path)
}