-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpk_test.go
636 lines (552 loc) · 15 KB
/
wpk_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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package wpk_test
import (
"bytes"
"io/fs"
"os"
"testing"
"github.com/schwarzlichtbezirk/wpk"
)
const mediadir = "testdata/media/"
var testpack = wpk.TempPath("testpack.wpk")
var testpkgt = wpk.TempPath("testpack.wpt")
var testpkgf = wpk.TempPath("testpack.wpf")
var memdata = map[string][]byte{
"sample.txt": wpk.S2B("The quick brown fox jumps over the lazy dog"),
"array.dat": {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
},
}
// Test package content on nested and external files equivalent.
func CheckPackage(t *testing.T, fwpt, fwpf *os.File, tagsnum int) {
var err error
// Open package files tags table
var pkg = wpk.NewPackage()
if err = pkg.OpenStream(fwpt); err != nil {
t.Fatal(err)
}
var label, _ = pkg.GetInfo().TagStr(wpk.TIDlabel)
t.Logf("package info: data size %d, label '%s'", pkg.DataSize(), label)
var realtagsnum int
pkg.Enum(func(fkey string, ts wpk.TagsetRaw) bool {
var link, haslink = ts.TagStr(wpk.TIDlink)
var offset, size = ts.Pos()
var fid, _ = ts.TagUint(wpk.TIDfid)
realtagsnum++
var orig []byte
if haslink {
if orig, err = os.ReadFile(link); err != nil {
t.Fatal(err)
}
} else {
var is bool
if orig, is = memdata[fkey]; !is {
t.Fatalf("memory block named as '%s' not found", fkey)
}
}
if size != uint(len(orig)) {
t.Errorf("size of file '%s' (%d) in package is defer from original (%d)",
fkey, size, len(orig))
}
var extr = make([]byte, size)
var n int
if n, err = fwpf.ReadAt(extr, int64(offset)); err != nil {
t.Fatal(err)
}
if n != len(extr) {
t.Errorf("can not extract content of file '%s' completely", fkey)
}
if !bytes.Equal(orig, extr) {
t.Errorf("content of file '%s' is defer from original", fkey)
}
if t.Failed() {
return false
}
if haslink {
t.Logf("check file #%d '%s' is ok", fid, fkey)
} else {
t.Logf("check data #%d '%s' is ok", fid, fkey)
}
return true
})
if realtagsnum != tagsnum {
t.Fatalf("expected %d entries in package, really got %d entries", tagsnum, realtagsnum)
}
}
// Test package SetInfo function and GetPackageInfo.
func TestInfo(t *testing.T) {
var err error
var fwpk *os.File
var pkg = wpk.NewPackage()
const (
label = "empty-package"
link = "github.com/schwarzlichtbezirk/wpk"
author = "schwarzlichtbezirk"
)
defer os.Remove(testpack)
// open temporary file for read/write
if fwpk, err = os.OpenFile(testpack, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpk.Close()
// starts new package
if err = pkg.Begin(fwpk, nil); err != nil {
t.Fatal(err)
}
// put package info somewhere at the code before finalize
pkg.SetInfo(wpk.TagsetRaw{}.
Put(wpk.TIDlabel, wpk.StrTag(label)).
Put(wpk.TIDlink, wpk.StrTag(link)).
Put(wpk.TIDauthor, wpk.StrTag(author)))
// finalize
if err = pkg.Sync(fwpk, nil); err != nil {
t.Fatal(err)
}
// at the end checkup package info
var ts wpk.TagsetRaw
if _, ts, err = wpk.GetPackageInfo(fwpk); err != nil {
t.Fatal(err)
}
var ok bool
var str string
if str, ok = ts.TagStr(wpk.TIDlabel); !ok {
t.Fatal("label tag not found in package info")
}
if str != label {
t.Fatal("label in package info is not equal to original")
}
if str, ok = ts.TagStr(wpk.TIDlink); !ok {
t.Fatal("link tag not found in package info")
}
if str != link {
t.Fatal("link in package info is not equal to original")
}
if str, ok = ts.TagStr(wpk.TIDauthor); !ok {
t.Fatal("author tag not found in package info")
}
if str != author {
t.Fatal("author in package info is not equal to original")
}
}
// Test packing the directory.
func TestPackDir(t *testing.T) {
var err error
var fwpk *os.File
var tagsnum = 0
var fidcount uint
var pkg = wpk.NewPackage()
defer os.Remove(testpack)
// open temporary file for read/write
if fwpk, err = os.OpenFile(testpack, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpk.Close()
// starts new package
if err = pkg.Begin(fwpk, nil); err != nil {
t.Fatal(err)
}
// put package info somewhere before finalize
pkg.SetInfo(wpk.TagsetRaw{}.
Put(wpk.TIDlabel, wpk.StrTag("packed-dir")))
// put media directory to file
fs.WalkDir(os.DirFS(mediadir), ".", func(fkey string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil // file is directory
}
var fpath = wpk.JoinPath(mediadir, fkey)
var file fs.File
var ts wpk.TagsetRaw
if file, err = os.Open(fpath); err != nil {
return err
}
defer file.Close()
if ts, err = pkg.PackFile(fwpk, file, fkey); err != nil {
return err
}
var size = ts.Size()
tagsnum++
fidcount++
pkg.SetupTagset(ts.
Put(wpk.TIDfid, wpk.UintTag(fidcount)).
Put(wpk.TIDlink, wpk.StrTag(fpath)))
t.Logf("put file #%d '%s', %d bytes", fidcount, fkey, size)
return nil
})
// finalize
if err = pkg.Sync(fwpk, nil); err != nil {
t.Fatal(err)
}
// make package file check up
CheckPackage(t, fwpk, fwpk, tagsnum)
}
// Test to read abnormal closed package in database mode.
func TestBrokenDB(t *testing.T) {
var err error
var fwpt, fwpf *os.File
var pkg = wpk.NewPackage()
defer os.Remove(testpkgt)
defer os.Remove(testpkgf)
// open temporary header file for read/write
if fwpt, err = os.OpenFile(testpkgt, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpt.Close()
// open temporary data file for read/write
if fwpf, err = os.OpenFile(testpkgf, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpf.Close()
// starts new package
if err = pkg.Begin(fwpt, fwpf); err != nil {
t.Fatal(err)
}
// try to read files tags table in empty package
if err = pkg.OpenStream(fwpt); err != nil {
t.Fatal(err)
}
// put package info
pkg.SetInfo(wpk.TagsetRaw{}.
Put(wpk.TIDlabel, wpk.StrTag("broken-pkg")))
// put somewhat
for name, data := range memdata {
if _, err = pkg.PackData(fwpf, bytes.NewReader(data), name); err != nil {
t.Fatal(err)
}
}
// finalize
if err = pkg.Sync(fwpt, fwpf); err != nil {
t.Fatal(err)
}
// try to read files tags table with some data
if err = pkg.OpenStream(fwpt); err != nil {
t.Fatal(err)
}
}
// Test package writing to splitted header and data files.
func TestPackDirSplit(t *testing.T) {
var err error
var fwpt, fwpf *os.File
var tagsnum = 0
var fidcount uint
var pkg = wpk.NewPackage()
defer os.Remove(testpkgt)
defer os.Remove(testpkgf)
// open temporary header file for read/write
if fwpt, err = os.OpenFile(testpkgt, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpt.Close()
// open temporary data file for read/write
if fwpf, err = os.OpenFile(testpkgf, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpf.Close()
// starts new package
if err = pkg.Begin(fwpt, fwpf); err != nil {
t.Fatal(err)
}
// put package info somewhere before finalize
pkg.SetInfo(wpk.TagsetRaw{}.
Put(wpk.TIDlabel, wpk.StrTag("splitted-pkg")))
// put media directory to file
fs.WalkDir(os.DirFS(mediadir), ".", func(fkey string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil // file is directory
}
var fpath = wpk.JoinPath(mediadir, fkey)
var file fs.File
var ts wpk.TagsetRaw
if file, err = os.Open(fpath); err != nil {
return err
}
defer file.Close()
if ts, err = pkg.PackFile(fwpf, file, fkey); err != nil {
return err
}
var size = ts.Size()
tagsnum++
fidcount++
pkg.SetupTagset(ts.
Put(wpk.TIDfid, wpk.UintTag(fidcount)).
Put(wpk.TIDlink, wpk.StrTag(fpath)))
t.Logf("put file #%d '%s', %d bytes", fidcount, fkey, size)
return nil
})
// finalize
if err = pkg.Sync(fwpt, fwpf); err != nil {
t.Fatal(err)
}
// make package file check up
CheckPackage(t, fwpt, fwpf, tagsnum)
}
// Test ability of files sequence packing, and make alias.
func TestPutFiles(t *testing.T) {
var err error
var fwpk *os.File
var tagsnum = 0
var fidcount uint
var pkg = wpk.NewPackage()
defer os.Remove(testpack)
// helper functions
var putfile = func(name string) {
var fpath = mediadir + name
var file fs.File
if file, err = os.Open(fpath); err != nil {
t.Fatal(err)
}
defer file.Close()
var ts wpk.TagsetRaw
if ts, err = pkg.PackFile(fwpk, file, name); err != nil {
t.Fatal(err)
}
tagsnum++
fidcount++
pkg.SetupTagset(ts.
Put(wpk.TIDfid, wpk.UintTag(fidcount)).
Put(wpk.TIDlink, wpk.StrTag(fpath)))
var size = ts.Size()
t.Logf("put file #%d '%s', %d bytes", fidcount, name, size)
}
var putdata = func(name string, data []byte) {
var r = bytes.NewReader(data)
var ts wpk.TagsetRaw
if ts, err = pkg.PackData(fwpk, r, name); err != nil {
t.Fatal(err)
}
tagsnum++
fidcount++
pkg.SetupTagset(ts.Put(wpk.TIDfid, wpk.UintTag(fidcount)))
var size = ts.Size()
t.Logf("put data #%d '%s', %d bytes", fidcount, name, size)
}
var putalias = func(oldname, newname string) {
if err = pkg.PutAlias(oldname, newname); err != nil {
t.Fatal(err)
}
tagsnum++
t.Logf("put alias '%s' to '%s'", newname, oldname)
}
var delalias = func(name string) {
if _, ok := pkg.DelTagset(name); !ok {
t.Fatalf("alias '%s' not deleted", name)
}
tagsnum--
t.Logf("del alias '%s'", name)
}
// open temporary file for read/write
if fwpk, err = os.OpenFile(testpack, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpk.Close()
// starts new package
if err = pkg.Begin(fwpk, nil); err != nil {
t.Fatal(err)
}
// put package info somewhere before finalize
pkg.SetInfo(wpk.TagsetRaw{}.
Put(wpk.TIDlabel, wpk.StrTag("put-files")))
// put content
putfile("bounty.jpg")
putfile("img1/claustral.jpg")
putfile("img1/Qarataşlar.jpg")
putfile("img2/marble.jpg")
putfile("img2/Uzuncı.jpg")
putalias("img1/claustral.jpg", "basaltbay.jpg")
for name, data := range memdata {
putdata(name, data)
}
putalias("img1/claustral.jpg", "jasper.jpg")
delalias("basaltbay.jpg")
// finalize
if err = pkg.Sync(fwpk, nil); err != nil {
t.Fatal(err)
}
// make package file check up
CheckPackage(t, fwpk, fwpk, tagsnum)
// check alias existence
if _, ok := pkg.GetTagset("jasper.jpg"); !ok {
t.Fatal("'jasper.jpg' alias not found")
}
if _, ok := pkg.GetTagset("basaltbay.jpg"); ok {
t.Fatal("'basaltbay.jpg' alias not deleted")
}
// check renamedir call
var count int
if count, err = pkg.RenameDir("img2", "img3", false); err != nil {
t.Fatal(err)
}
if count != 2 {
t.Fatalf("expected 2 files to rename directory from 'img2' to 'img3', got %d", count)
}
if _, ok := pkg.GetTagset("img3/marble.jpg"); !ok {
t.Fatal("'img3/marble.jpg' not found")
}
if _, ok := pkg.GetTagset("img3/Uzuncı.jpg"); !ok {
t.Fatal("'img3/Uzuncı.jpg' not found")
}
}
// Test to make package in two steps on single package open:
// creates package file, make package, do some job,
// then append new files to existing package.
func TestAppendContinues(t *testing.T) {
var err error
var fwpk *os.File
var tagsnum = 0
var fidcount uint
var pkg = wpk.NewPackage()
defer os.Remove(testpack)
// helper functions
var putfile = func(name string) {
var fpath = mediadir + name
var file fs.File
if file, err = os.Open(fpath); err != nil {
t.Fatal(err)
}
defer file.Close()
var ts wpk.TagsetRaw
if ts, err = pkg.PackFile(fwpk, file, name); err != nil {
t.Fatal(err)
}
tagsnum++
fidcount++
pkg.SetupTagset(ts.
Put(wpk.TIDfid, wpk.UintTag(fidcount)).
Put(wpk.TIDlink, wpk.StrTag(fpath)))
var size = ts.Size()
t.Logf("put file #%d '%s', %d bytes", fidcount, name, size)
}
// open temporary file for read/write
if fwpk, err = os.OpenFile(testpack, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpk.Close()
// starts new package
if err = pkg.Begin(fwpk, nil); err != nil {
t.Fatal(err)
}
// put package info somewhere before finalize
pkg.SetInfo(wpk.TagsetRaw{}.
Put(wpk.TIDlabel, wpk.StrTag("append-continues")))
// put content
putfile("bounty.jpg")
putfile("img1/claustral.jpg")
putfile("img1/Qarataşlar.jpg")
// finalize
if err = pkg.Sync(fwpk, nil); err != nil {
t.Fatal(err)
}
// make package file check up
CheckPackage(t, fwpk, fwpk, tagsnum)
//
// here can be any job using package
//
// starts append to existing package
if err = pkg.Append(fwpk, nil); err != nil {
t.Fatal(err)
}
// put content
putfile("img2/marble.jpg")
putfile("img2/Uzuncı.jpg")
// finalize
if err = pkg.Sync(fwpk, nil); err != nil {
t.Fatal(err)
}
// make package file check up
CheckPackage(t, fwpk, fwpk, tagsnum)
}
// Test to make package in two steps on twice package opens:
// creates package file, make package, close file,
// then open package file again and append new files.
func TestAppendDiscrete(t *testing.T) {
var err error
var fwpk *os.File
var tagsnum = 0
var fidcount uint
var pkg = wpk.NewPackage()
defer os.Remove(testpack)
// helper functions
var putfile = func(name string) {
var fpath = mediadir + name
var file fs.File
if file, err = os.Open(fpath); err != nil {
t.Fatal(err)
}
defer file.Close()
var ts wpk.TagsetRaw
if ts, err = pkg.PackFile(fwpk, file, name); err != nil {
t.Fatal(err)
}
tagsnum++
fidcount++
pkg.SetupTagset(ts.
Put(wpk.TIDfid, wpk.UintTag(fidcount)).
Put(wpk.TIDlink, wpk.StrTag(fpath)))
var size = ts.Size()
t.Logf("put file #%d '%s', %d bytes", fidcount, name, size)
}
t.Run("step1", func(t *testing.T) {
// open temporary file for read/write
if fwpk, err = os.OpenFile(testpack, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
t.Fatal(err)
}
defer fwpk.Close()
// starts new package
if err = pkg.Begin(fwpk, nil); err != nil {
t.Fatal(err)
}
// put package info somewhere before finalize
pkg.SetInfo(wpk.CopyTagset(pkg.GetInfo()).
Set(wpk.TIDlabel, wpk.StrTag("discrete-step#1")))
// put content
putfile("bounty.jpg")
putfile("img1/claustral.jpg")
putfile("img1/Qarataşlar.jpg")
// finalize
if err = pkg.Sync(fwpk, nil); err != nil {
t.Fatal(err)
}
// make package file check up
CheckPackage(t, fwpk, fwpk, tagsnum)
})
//
// here can be any job using package
//
t.Run("step2", func(t *testing.T) {
// open temporary file for read/write
if fwpk, err = os.OpenFile(testpack, os.O_RDWR, 0644); err != nil {
t.Fatal(err)
}
defer fwpk.Close()
// read package content again.
// pkg value already contains data from previous step
// and this call can be skipped,
// but we want to test here read functionality
if err = pkg.OpenStream(fwpk); err != nil {
t.Fatal(err)
}
// starts append to existing package
if err = pkg.Append(fwpk, nil); err != nil {
t.Fatal(err)
}
// put package info somewhere before finalize
pkg.SetInfo(wpk.CopyTagset(pkg.GetInfo()).
Set(wpk.TIDlabel, wpk.StrTag("discrete-step#2")))
// put content
putfile("img2/marble.jpg")
putfile("img2/Uzuncı.jpg")
// finalize
if err = pkg.Sync(fwpk, nil); err != nil {
t.Fatal(err)
}
// make package file check up
CheckPackage(t, fwpk, fwpk, tagsnum)
})
}
// The End.