-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter_advanced_test.go
More file actions
282 lines (237 loc) · 6.96 KB
/
Copy pathwriter_advanced_test.go
File metadata and controls
282 lines (237 loc) · 6.96 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package squashfs_test
import (
"bytes"
"fmt"
"io/fs"
"strings"
"testing"
"testing/fstest"
"github.com/KarpelesLab/squashfs"
)
func TestWriterWithSubdirectories(t *testing.T) {
// Create a test filesystem with subdirectories
testFS := fstest.MapFS{
"file1.txt": {Data: []byte("hello world")},
"dir1/file2.txt": {Data: []byte("file in dir1")},
"dir1/file3.txt": {Data: []byte("another file in dir1")},
"dir1/subdir/file4.txt": {Data: []byte("file in subdir")},
"dir2/file5.txt": {Data: []byte("file in dir2")},
"empty_dir/.placeholder": {Data: []byte("")}, // Use placeholder for empty dir
}
var buf bytes.Buffer
w, err := squashfs.NewWriter(&buf)
if err != nil {
t.Fatalf("NewWriter failed: %s", err)
}
// Add all files from testFS
err = w.AddFS(testFS)
if err != nil {
t.Fatalf("WalkDir failed: %s", err)
}
// Finalize
err = w.Finalize()
if err != nil {
t.Fatalf("Finalize failed: %s", err)
}
t.Logf("Created SquashFS with subdirectories: %d bytes", buf.Len())
// Read it back
sqfs, err := squashfs.New(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Failed to read back SquashFS: %s", err)
}
// Verify directory structure
entries, err := sqfs.ReadDir(".")
if err != nil {
t.Fatalf("Failed to read root directory: %s", err)
}
t.Logf("Root directory has %d entries", len(entries))
// Check that we can read dir1
entries, err = sqfs.ReadDir("dir1")
if err != nil {
t.Fatalf("Failed to read dir1: %s", err)
}
if len(entries) < 2 {
t.Errorf("Expected at least 2 entries in dir1, got %d", len(entries))
}
// Check that we can read file content
data, err := fs.ReadFile(sqfs, "file1.txt")
if err != nil {
t.Fatalf("Failed to read file1.txt: %s", err)
}
if string(data) != "hello world" {
t.Errorf("Expected 'hello world', got '%s'", string(data))
}
// Check nested file
data, err = fs.ReadFile(sqfs, "dir1/subdir/file4.txt")
if err != nil {
t.Fatalf("Failed to read dir1/subdir/file4.txt: %s", err)
}
if string(data) != "file in subdir" {
t.Errorf("Expected 'file in subdir', got '%s'", string(data))
}
}
func TestWriterWithLargeDirectory(t *testing.T) {
// Create a directory with many files to test indexing
testFS := make(fstest.MapFS)
// Add 1000 files to trigger directory indexing
for i := 0; i < 1000; i++ {
name := fmt.Sprintf("file_%04d.txt", i)
testFS[name] = &fstest.MapFile{
Data: []byte(fmt.Sprintf("content of file %d", i)),
}
}
var buf bytes.Buffer
w, err := squashfs.NewWriter(&buf)
if err != nil {
t.Fatalf("NewWriter failed: %s", err)
}
// Add all files
err = w.AddFS(testFS)
if err != nil {
t.Fatalf("WalkDir failed: %s", err)
}
// Finalize
err = w.Finalize()
if err != nil {
t.Fatalf("Finalize failed: %s", err)
}
t.Logf("Created SquashFS with large directory: %d bytes", buf.Len())
// Read it back
sqfs, err := squashfs.New(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Failed to read back SquashFS: %s", err)
}
// Verify we can read the directory
entries, err := sqfs.ReadDir(".")
if err != nil {
t.Fatalf("Failed to read root directory: %s", err)
}
if len(entries) != 1000 {
t.Errorf("Expected 1000 entries, got %d", len(entries))
}
// Verify we can read a file in the middle
data, err := fs.ReadFile(sqfs, "file_0500.txt")
if err != nil {
t.Fatalf("Failed to read file_0500.txt: %s", err)
}
if string(data) != "content of file 500" {
t.Errorf("Expected 'content of file 500', got '%s'", string(data))
}
// Verify we can read first and last files
_, err = fs.ReadFile(sqfs, "file_0000.txt")
if err != nil {
t.Fatalf("Failed to read file_0000.txt: %s", err)
}
_, err = fs.ReadFile(sqfs, "file_0999.txt")
if err != nil {
t.Fatalf("Failed to read file_0999.txt: %s", err)
}
}
func TestWriterWithNestedDirectories(t *testing.T) {
// Create deeply nested directory structure
testFS := make(fstest.MapFS)
// Create a deep hierarchy: level0/level1/level2/.../level9/file.txt
path := ""
for i := 0; i < 10; i++ {
if i > 0 {
path += "/"
}
path += fmt.Sprintf("level%d", i)
}
testFS[path+"/deep_file.txt"] = &fstest.MapFile{
Data: []byte("deeply nested content"),
}
var buf bytes.Buffer
w, err := squashfs.NewWriter(&buf)
if err != nil {
t.Fatalf("NewWriter failed: %s", err)
}
// Add all files
err = w.AddFS(testFS)
if err != nil {
t.Fatalf("WalkDir failed: %s", err)
}
// Finalize
err = w.Finalize()
if err != nil {
t.Fatalf("Finalize failed: %s", err)
}
t.Logf("Created SquashFS with nested directories: %d bytes", buf.Len())
// Read it back
sqfs, err := squashfs.New(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Failed to read back SquashFS: %s", err)
}
// Verify we can read the deeply nested file
data, err := fs.ReadFile(sqfs, path+"/deep_file.txt")
if err != nil {
t.Fatalf("Failed to read deep_file.txt: %s", err)
}
if string(data) != "deeply nested content" {
t.Errorf("Expected 'deeply nested content', got '%s'", string(data))
}
// Verify intermediate directories exist
for i := 0; i < 10; i++ {
dirPath := strings.Join(strings.Split(path, "/")[:i+1], "/")
entries, err := sqfs.ReadDir(dirPath)
if err != nil {
t.Fatalf("Failed to read directory %s: %s", dirPath, err)
}
if len(entries) == 0 {
t.Errorf("Directory %s is empty", dirPath)
}
}
}
func TestWriterMixedContent(t *testing.T) {
// Create a mixed filesystem with various file sizes
testFS := make(fstest.MapFS)
// Empty file
testFS["empty.txt"] = &fstest.MapFile{Data: []byte{}}
// Small file
testFS["small.txt"] = &fstest.MapFile{Data: []byte("x")}
// Medium file
testFS["medium.txt"] = &fstest.MapFile{Data: bytes.Repeat([]byte("medium content\n"), 100)}
// Large file (over 1MB to test block handling)
testFS["large.txt"] = &fstest.MapFile{Data: bytes.Repeat([]byte("large content\n"), 80000)}
// Directory with files
testFS["data/file1.dat"] = &fstest.MapFile{Data: []byte("data1")}
testFS["data/file2.dat"] = &fstest.MapFile{Data: []byte("data2")}
var buf bytes.Buffer
w, err := squashfs.NewWriter(&buf)
if err != nil {
t.Fatalf("NewWriter failed: %s", err)
}
// Add all files
err = w.AddFS(testFS)
if err != nil {
t.Fatalf("WalkDir failed: %s", err)
}
// Finalize
err = w.Finalize()
if err != nil {
t.Fatalf("Finalize failed: %s", err)
}
t.Logf("Created SquashFS with mixed content: %d bytes", buf.Len())
// Read it back
sqfs, err := squashfs.New(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Failed to read back SquashFS: %s", err)
}
// Verify empty file
data, err := fs.ReadFile(sqfs, "empty.txt")
if err != nil {
t.Fatalf("Failed to read empty.txt: %s", err)
}
if len(data) != 0 {
t.Errorf("Expected empty file, got %d bytes", len(data))
}
// Verify large file
data, err = fs.ReadFile(sqfs, "large.txt")
if err != nil {
t.Fatalf("Failed to read large.txt: %s", err)
}
expectedSize := len(bytes.Repeat([]byte("large content\n"), 80000))
if len(data) != expectedSize {
t.Errorf("Expected %d bytes, got %d", expectedSize, len(data))
}
}