-
Notifications
You must be signed in to change notification settings - Fork 0
/
iso_test.go
355 lines (298 loc) · 7.88 KB
/
iso_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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package joint_test
import (
"fmt"
"hash/crc32"
"io"
"io/fs"
"path"
"testing"
jnt "github.com/schwarzlichtbezirk/joint"
)
const isosize = 0x128000 // size of external ISO image
const foxsize = 44 // size of "fox.txt" file
// Precalculated CRC32 codes with IEEE polynomial of files in ISO-images.
var filecrc = map[string]uint32{
"fox.txt": 0x519025e9,
"doc1.txt": 0x98b2c5bd,
"doc2.txt": 0xf3cb012f,
"lorem1.txt": 0x7e4d5e9b,
"lorem2.txt": 0xa764fec7,
"lorem3.txt": 0x71a2e97e,
"рыба.txt": 0xcd6fc22a, // cyrillic name with Windows-1251 encoding for ISO-9660
"док1.txt": 0x3d4fdf17, // cyrillic name
"док2.txt": 0x42d2236a, // cyrillic name
"internal.iso": 0xf4c1b74d,
}
// Files list in external ISO-disk.
var extfiles = []string{
"fox.txt",
"data/lorem1.txt",
"data/lorem2.txt",
"data/lorem3.txt",
"data/рыба.txt",
"data/docs/doc1.txt",
"data/docs/doc2.txt",
"data/доки/док1.txt",
"data/доки/док2.txt",
//"disk/internal.iso", // its a directory!
}
// Files list in internal ISO-disk.
var intfiles = []string{
"fox.txt",
"docs/doc1.txt",
"docs/doc2.txt",
"доки/док1.txt",
"доки/док2.txt",
}
// Directories list in external ISO-disk.
var extdirs = map[string][]string{
"": {"fox.txt", "data", "disk"},
"data": {"lorem1.txt", "lorem2.txt", "lorem3.txt", "рыба.txt", "docs", "доки", "empty"},
"disk": {"internal.iso"},
"data/docs": {"doc1.txt", "doc2.txt"},
"data/доки": {"док1.txt", "док2.txt"},
"data/empty": {},
}
// Directories list in internal ISO-disk.
var intdirs = map[string][]string{
"": {"fox.txt", "docs", "доки"},
"docs": {"doc1.txt", "doc2.txt"},
"доки": {"док1.txt", "док2.txt"},
}
func checkReadDir(j jnt.Joint) (err error) {
if j.Busy() {
return fmt.Errorf("joint is busy before opening")
}
var f fs.File
if f, err = j.Open("testdata"); err != nil {
return
}
defer f.Close()
if !j.Busy() {
return fmt.Errorf("joint is not busy after opening")
}
var list []fs.DirEntry
if list, err = j.ReadDir(-1); err != nil {
return
}
if len(list) != 1 {
return fmt.Errorf("expected 1 file in 'testdata' directory, found %d files", len(list))
}
var fi = list[0].(jnt.JointFileInfo)
if fi.Name() != "external.iso" {
return fmt.Errorf("expected 'external.iso' file in 'testdata' directory")
}
if fi.IsRealDir() {
return fmt.Errorf("file 'external.iso' should be recognized as real file")
}
if !fi.IsDir() {
return fmt.Errorf("file 'external.iso' should be recognized as virtual folder")
}
if fi.Mode().Type() != fs.ModeDir {
return fmt.Errorf("file 'external.iso' have wrong file type")
}
return
}
func checkFile(j jnt.Joint, fpath string) (err error) {
if j.Busy() {
return fmt.Errorf("joint '%s' is busy before opening", fpath)
}
if _, err = j.Open(fpath); err != nil {
return err
}
defer j.Close()
if !j.Busy() {
return fmt.Errorf("joint '%s' is not busy after opening", fpath)
}
var fi fs.FileInfo
if fi, err = j.Stat(); err != nil {
return err
}
if fi.IsDir() {
return fmt.Errorf("path '%s' is directory, file expected", fpath)
}
var data []byte
if data, err = io.ReadAll(j); err != nil {
return err
}
var crc = crc32.ChecksumIEEE(data)
var master, ok = filecrc[path.Base(fpath)]
if !ok {
return fmt.Errorf("file with path '%s' is not found", fpath)
}
if crc != master {
return fmt.Errorf("file content of '%s' does not match by CRC-code to precalculated value", fpath)
}
return nil
}
func checkDir(j jnt.Joint, fpath string, dirs map[string][]string) (err error) {
if j.Busy() {
return fmt.Errorf("joint '%s' is busy before opening", fpath)
}
if _, err = j.Open(fpath); err != nil {
return err
}
defer j.Close()
if !j.Busy() {
return fmt.Errorf("joint '%s' is not busy after opening", fpath)
}
var fi fs.FileInfo
if fi, err = j.Stat(); err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("path '%s' is file, directory expected", fpath)
}
var files []fs.DirEntry
if files, err = j.ReadDir(-1); err != nil {
return err
}
var list, ok = dirs[fpath]
if !ok {
return fmt.Errorf("directory with path '%s' is not found", fpath)
}
if len(files) != len(list) {
return fmt.Errorf("expected %d files, found %d files at directory '%s'", len(list), len(files), fpath)
}
var found = true
for _, fi := range files {
var name = fi.Name()
for _, n := range list {
if n == name {
found = true
break
}
}
if !found {
return fmt.Errorf("expected file '%s' at path '%s' is not found", name, fpath)
}
}
return nil
}
// Open file "fox.txt" with content "The quick brown fox
// jumps over the lazy dog." and read chunks from it.
func readChunk(j, base jnt.Joint) (err error) {
if err = j.Make(base, "testdata/external.iso"); err != nil {
return
}
defer j.Cleanup()
if _, err = j.Open("fox.txt"); err != nil {
return
}
defer j.Close()
var size int64
if size, err = j.Size(); err != nil {
return
}
if size != foxsize {
return fmt.Errorf("size of 'external.iso' file does not equal to predefined value")
}
var b1 [9]byte // buffer for "brown fox" chunk from file content
if _, err = j.ReadAt(b1[:], 10); err != nil {
return
}
if string(b1[:]) != "brown fox" {
return fmt.Errorf("read string does not match to pattern")
}
var b2 [8]byte // buffer for "lazy dog" chunk from file content
if _, err = j.Seek(35, io.SeekStart); err != nil {
return
}
if _, err = j.Read(b2[:]); err != nil {
return
}
if string(b2[:]) != "lazy dog" {
return fmt.Errorf("read string does not match to pattern")
}
return
}
func TestSysJoint(t *testing.T) {
var err error
var j = &jnt.SysJoint{}
if err = j.Make(nil, "."); err != nil {
t.Fatal(err)
}
defer j.Cleanup()
var fi fs.FileInfo
if fi, err = j.Info("testdata/external.iso"); err != nil {
t.Fatal(err)
}
if fi.Size() != isosize {
t.Fatal("ISO-file size does not match")
}
if err = checkReadDir(j); err != nil {
t.Fatal(err)
}
}
// Check file reading in external ISO-disk placed at primary filesystem.
func TestIsoExtReadFile(t *testing.T) {
var err error
var j jnt.Joint = &jnt.IsoJoint{}
if err = j.Make(nil, "testdata/external.iso"); err != nil {
t.Fatal(err)
}
defer j.Cleanup()
for _, fpath := range extfiles {
if err = checkFile(j, fpath); err != nil {
t.Fatal(err)
}
}
}
// Check file reading in internal ISO-disk placed at primary filesystem.
func TestIsoIntReadFile(t *testing.T) {
var err error
var j1 jnt.Joint = &jnt.IsoJoint{}
if err = j1.Make(nil, "testdata/external.iso"); err != nil {
t.Fatal(err)
}
var j2 jnt.Joint = &jnt.IsoJoint{}
if err = j2.Make(j1, "disk/internal.iso"); err != nil {
t.Fatal(err)
}
defer j2.Cleanup() // only top-level joint must be called for Cleanup
for _, fpath := range intfiles {
if err = checkFile(j2, fpath); err != nil {
t.Fatal(err)
}
}
}
// Check directory list in external ISO-disk placed at primary filesystem.
func TestIsoExtDirList(t *testing.T) {
var err error
var j jnt.Joint = &jnt.IsoJoint{}
if err = j.Make(nil, "testdata/external.iso"); err != nil {
t.Fatal(err)
}
defer j.Cleanup()
for fpath := range extdirs {
if err = checkDir(j, fpath, extdirs); err != nil {
t.Fatal(err)
}
}
}
// Check directory list in internal ISO-disk placed at primary filesystem.
func TestIsoIntDirList(t *testing.T) {
var err error
var j1 jnt.Joint = &jnt.IsoJoint{}
if err = j1.Make(nil, "testdata/external.iso"); err != nil {
t.Fatal(err)
}
var j2 jnt.Joint = &jnt.IsoJoint{}
if err = j2.Make(j1, "disk/internal.iso"); err != nil {
t.Fatal(err)
}
defer j2.Cleanup() // only top-level joint must be called for Cleanup
for fpath := range intdirs {
if err = checkDir(j2, fpath, intdirs); err != nil {
t.Fatal(err)
}
}
}
// Read files chunks on IsoJoint.
func TestIsoReadChunk(t *testing.T) {
var err error
var j jnt.Joint = &jnt.IsoJoint{}
if err = readChunk(j, nil); err != nil {
t.Fatal(err)
}
}