forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
squashfs_test.go
936 lines (796 loc) · 28.1 KB
/
squashfs_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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package squashfs_test
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
. "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapdir"
"github.com/snapcore/snapd/snap/squashfs"
"github.com/snapcore/snapd/testutil"
)
// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }
type SquashfsTestSuite struct {
oldStdout, oldStderr, outf *os.File
testutil.BaseTest
}
var _ = Suite(&SquashfsTestSuite{})
func makeSnap(c *C, manifest, data string) *squashfs.Snap {
cur, _ := os.Getwd()
return makeSnapInDir(c, cur, manifest, data)
}
func makeSnapContents(c *C, manifest, data string) string {
tmp := c.MkDir()
err := os.MkdirAll(filepath.Join(tmp, "meta", "hooks", "dir"), 0755)
c.Assert(err, IsNil)
// our regular snap.yaml
err = ioutil.WriteFile(filepath.Join(tmp, "meta", "snap.yaml"), []byte(manifest), 0644)
c.Assert(err, IsNil)
// some hooks
err = ioutil.WriteFile(filepath.Join(tmp, "meta", "hooks", "foo-hook"), nil, 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(tmp, "meta", "hooks", "bar-hook"), nil, 0755)
c.Assert(err, IsNil)
// And a file in another directory in there, just for testing (not a valid
// hook)
err = ioutil.WriteFile(filepath.Join(tmp, "meta", "hooks", "dir", "baz"), nil, 0755)
c.Assert(err, IsNil)
// some empty directories
err = os.MkdirAll(filepath.Join(tmp, "food", "bard", "bazd"), 0755)
c.Assert(err, IsNil)
// some data
err = ioutil.WriteFile(filepath.Join(tmp, "data.bin"), []byte(data), 0644)
c.Assert(err, IsNil)
return tmp
}
func makeSnapInDir(c *C, dir, manifest, data string) *squashfs.Snap {
snapType := "app"
var m struct {
Type string `yaml:"type"`
}
if err := yaml.Unmarshal([]byte(manifest), &m); err == nil && m.Type != "" {
snapType = m.Type
}
tmp := makeSnapContents(c, manifest, data)
// build it
sn := squashfs.New(filepath.Join(dir, "foo.snap"))
err := sn.Build(tmp, &squashfs.BuildOpts{SnapType: snapType})
c.Assert(err, IsNil)
return sn
}
func (s *SquashfsTestSuite) SetUpTest(c *C) {
d := c.MkDir()
dirs.SetRootDir(d)
err := os.Chdir(d)
c.Assert(err, IsNil)
restore := osutil.MockMountInfo("")
s.AddCleanup(restore)
s.outf, err = ioutil.TempFile(c.MkDir(), "")
c.Assert(err, IsNil)
s.oldStdout, s.oldStderr = os.Stdout, os.Stderr
os.Stdout, os.Stderr = s.outf, s.outf
}
func (s *SquashfsTestSuite) TearDownTest(c *C) {
os.Stdout, os.Stderr = s.oldStdout, s.oldStderr
// this ensures things were quiet
_, err := s.outf.Seek(0, 0)
c.Assert(err, IsNil)
outbuf, err := ioutil.ReadAll(s.outf)
c.Assert(err, IsNil)
c.Check(string(outbuf), Equals, "")
}
func (s *SquashfsTestSuite) TestFileHasSquashfsHeader(c *C) {
sn := makeSnap(c, "name: test", "")
c.Check(squashfs.FileHasSquashfsHeader(sn.Path()), Equals, true)
}
func (s *SquashfsTestSuite) TestNotFileHasSquashfsHeader(c *C) {
data := []string{
"hsqs",
"hsqs\x00",
"hsqs" + strings.Repeat("\x00", squashfs.SuperblockSize-4),
"hsqt" + strings.Repeat("\x00", squashfs.SuperblockSize-4+1),
"not a snap",
}
for _, d := range data {
err := ioutil.WriteFile("not-a-snap", []byte(d), 0644)
c.Assert(err, IsNil)
c.Check(squashfs.FileHasSquashfsHeader("not-a-snap"), Equals, false)
}
}
func (s *SquashfsTestSuite) TestInstallSimpleNoCp(c *C) {
// mock cp but still cp
cmd := testutil.MockCommand(c, "cp", `#!/bin/sh
exec /bin/cp "$@"
`)
defer cmd.Restore()
// mock link but still link
linked := 0
r := squashfs.MockLink(func(a, b string) error {
linked++
return os.Link(a, b)
})
defer r()
sn := makeSnap(c, "name: test", "")
targetPath := filepath.Join(c.MkDir(), "target.snap")
mountDir := c.MkDir()
didNothing, err := sn.Install(targetPath, mountDir, nil)
c.Assert(err, IsNil)
c.Assert(didNothing, Equals, false)
c.Check(osutil.FileExists(targetPath), Equals, true)
c.Check(linked, Equals, 1)
c.Check(cmd.Calls(), HasLen, 0)
}
func (s *SquashfsTestSuite) TestInstallSimpleOnOverlayfs(c *C) {
cmd := testutil.MockCommand(c, "cp", "")
defer cmd.Restore()
// mock link but still link
linked := 0
r := squashfs.MockLink(func(a, b string) error {
linked++
return os.Link(a, b)
})
defer r()
// pretend we are on overlayfs
restore := squashfs.MockIsRootWritableOverlay(func() (string, error) {
return "/upper", nil
})
defer restore()
c.Assert(os.MkdirAll(dirs.SnapSeedDir, 0755), IsNil)
sn := makeSnapInDir(c, dirs.SnapSeedDir, "name: test2", "")
targetPath := filepath.Join(c.MkDir(), "target.snap")
_, err := os.Lstat(targetPath)
c.Check(os.IsNotExist(err), Equals, true)
didNothing, err := sn.Install(targetPath, c.MkDir(), nil)
c.Assert(err, IsNil)
c.Assert(didNothing, Equals, false)
// symlink in place
c.Check(osutil.IsSymlink(targetPath), Equals, true)
// no link / no cp
c.Check(linked, Equals, 0)
c.Check(cmd.Calls(), HasLen, 0)
}
func noLink() func() {
return squashfs.MockLink(func(string, string) error { return errors.New("no.") })
}
func (s *SquashfsTestSuite) TestInstallNotCopyTwice(c *C) {
// first, disable os.Link
defer noLink()()
// then, mock cp but still cp
cmd := testutil.MockCommand(c, "cp", `#!/bin/sh
exec /bin/cp "$@"
`)
defer cmd.Restore()
sn := makeSnap(c, "name: test2", "")
targetPath := filepath.Join(c.MkDir(), "target.snap")
mountDir := c.MkDir()
didNothing, err := sn.Install(targetPath, mountDir, nil)
c.Assert(err, IsNil)
c.Assert(didNothing, Equals, false)
c.Check(cmd.Calls(), HasLen, 1)
didNothing, err = sn.Install(targetPath, mountDir, nil)
c.Assert(err, IsNil)
c.Assert(didNothing, Equals, true)
c.Check(cmd.Calls(), HasLen, 1) // and not 2 \o/
}
func (s *SquashfsTestSuite) TestInstallSeedNoLink(c *C) {
defer noLink()()
c.Assert(os.MkdirAll(dirs.SnapSeedDir, 0755), IsNil)
sn := makeSnapInDir(c, dirs.SnapSeedDir, "name: test2", "")
targetPath := filepath.Join(c.MkDir(), "target.snap")
_, err := os.Lstat(targetPath)
c.Check(os.IsNotExist(err), Equals, true)
didNothing, err := sn.Install(targetPath, c.MkDir(), nil)
c.Assert(err, IsNil)
c.Assert(didNothing, Equals, false)
c.Check(osutil.IsSymlink(targetPath), Equals, true) // \o/
}
func (s *SquashfsTestSuite) TestInstallUC20SeedNoLink(c *C) {
defer noLink()()
systemSnapsDir := filepath.Join(dirs.SnapSeedDir, "systems", "20200521", "snaps")
c.Assert(os.MkdirAll(systemSnapsDir, 0755), IsNil)
snap := makeSnapInDir(c, systemSnapsDir, "name: test2", "")
targetPath := filepath.Join(c.MkDir(), "target.snap")
_, err := os.Lstat(targetPath)
c.Check(os.IsNotExist(err), Equals, true)
didNothing, err := snap.Install(targetPath, c.MkDir(), nil)
c.Assert(err, IsNil)
c.Assert(didNothing, Equals, false)
c.Check(osutil.IsSymlink(targetPath), Equals, true) // \o/
}
func (s *SquashfsTestSuite) TestInstallMustNotCrossDevices(c *C) {
defer noLink()()
c.Assert(os.MkdirAll(dirs.SnapSeedDir, 0755), IsNil)
sn := makeSnapInDir(c, dirs.SnapSeedDir, "name: test2", "")
targetPath := filepath.Join(c.MkDir(), "target.snap")
_, err := os.Lstat(targetPath)
c.Check(os.IsNotExist(err), Equals, true)
didNothing, err := sn.Install(targetPath, c.MkDir(), &snap.InstallOptions{MustNotCrossDevices: true})
c.Assert(err, IsNil)
c.Assert(didNothing, Equals, false)
c.Check(osutil.IsSymlink(targetPath), Equals, false)
}
func (s *SquashfsTestSuite) TestInstallNothingToDo(c *C) {
sn := makeSnap(c, "name: test2", "")
targetPath := filepath.Join(c.MkDir(), "foo.snap")
c.Assert(os.Symlink(sn.Path(), targetPath), IsNil)
didNothing, err := sn.Install(targetPath, c.MkDir(), nil)
c.Assert(err, IsNil)
c.Check(didNothing, Equals, true)
}
func (s *SquashfsTestSuite) TestPath(c *C) {
p := "/path/to/foo.snap"
sn := squashfs.New("/path/to/foo.snap")
c.Assert(sn.Path(), Equals, p)
}
func (s *SquashfsTestSuite) TestReadFile(c *C) {
sn := makeSnap(c, "name: foo", "")
content, err := sn.ReadFile("meta/snap.yaml")
c.Assert(err, IsNil)
c.Assert(string(content), Equals, "name: foo")
}
func (s *SquashfsTestSuite) TestReadFileFail(c *C) {
mockUnsquashfs := testutil.MockCommand(c, "unsquashfs", `echo boom; exit 1`)
defer mockUnsquashfs.Restore()
sn := makeSnap(c, "name: foo", "")
_, err := sn.ReadFile("meta/snap.yaml")
c.Assert(err, ErrorMatches, "cannot run unsquashfs: boom")
}
func (s *SquashfsTestSuite) TestRandomAccessFile(c *C) {
sn := makeSnap(c, "name: foo", "")
r, err := sn.RandomAccessFile("meta/snap.yaml")
c.Assert(err, IsNil)
defer r.Close()
c.Assert(r.Size(), Equals, int64(9))
b := make([]byte, 4)
n, err := r.ReadAt(b, 4)
c.Assert(err, IsNil)
c.Assert(n, Equals, 4)
c.Check(string(b), Equals, ": fo")
}
func (s *SquashfsTestSuite) TestListDir(c *C) {
sn := makeSnap(c, "name: foo", "")
fileNames, err := sn.ListDir("meta/hooks")
c.Assert(err, IsNil)
c.Assert(len(fileNames), Equals, 3)
c.Check(fileNames[0], Equals, "bar-hook")
c.Check(fileNames[1], Equals, "dir")
c.Check(fileNames[2], Equals, "foo-hook")
}
func (s *SquashfsTestSuite) TestWalkNative(c *C) {
sub := "."
sn := makeSnap(c, "name: foo", "")
sqw := map[string]os.FileInfo{}
sn.Walk(sub, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == "food" {
return filepath.SkipDir
}
sqw[path] = info
return nil
})
base := c.MkDir()
c.Assert(sn.Unpack("*", base), IsNil)
sdw := map[string]os.FileInfo{}
snapdir.New(base).Walk(sub, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == "food" {
return filepath.SkipDir
}
sdw[path] = info
return nil
})
fpw := map[string]os.FileInfo{}
filepath.Walk(filepath.Join(base, sub), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
path, err = filepath.Rel(base, path)
if err != nil {
return err
}
if path == "food" {
return filepath.SkipDir
}
fpw[path] = info
return nil
})
for k := range fpw {
squashfs.Alike(sqw[k], fpw[k], c, Commentf(k))
squashfs.Alike(sdw[k], fpw[k], c, Commentf(k))
}
for k := range sqw {
squashfs.Alike(fpw[k], sqw[k], c, Commentf(k))
squashfs.Alike(sdw[k], sqw[k], c, Commentf(k))
}
for k := range sdw {
squashfs.Alike(fpw[k], sdw[k], c, Commentf(k))
squashfs.Alike(sqw[k], sdw[k], c, Commentf(k))
}
}
func (s *SquashfsTestSuite) testWalkMockedUnsquashfs(c *C) {
expectingNames := []string{
".",
"data.bin",
"food",
"meta",
"meta/hooks",
"meta/hooks/bar-hook",
"meta/hooks/dir",
"meta/hooks/dir/baz",
"meta/hooks/foo-hook",
"meta/snap.yaml",
}
sub := "."
sn := makeSnap(c, "name: foo", "")
var seen []string
sn.Walk(sub, func(path string, info os.FileInfo, err error) error {
c.Logf("got %v", path)
if err != nil {
return err
}
seen = append(seen, path)
if path == "food" {
return filepath.SkipDir
}
return nil
})
c.Assert(len(seen), Equals, len(expectingNames))
for idx, name := range seen {
c.Check(name, Equals, expectingNames[idx])
}
}
func (s *SquashfsTestSuite) TestWalkMockedUnsquashfs45(c *C) {
// mock behavior of squashfs-tools 4.5 and later
mockUnsquashfs := testutil.MockCommand(c, "unsquashfs", `
cat <<EOF
drwx------ root/root 55 2021-07-27 13:31 .
-rw-r--r-- root/root 0 2021-07-27 13:31 ./data.bin
drwxr-xr-x root/root 27 2021-07-27 13:31 ./food
drwxr-xr-x root/root 27 2021-07-27 13:31 ./food/bard
drwxr-xr-x root/root 3 2021-07-27 13:31 ./food/bard/bazd
drwxr-xr-x root/root 45 2021-07-27 13:31 ./meta
drwxr-xr-x root/root 58 2021-07-27 13:31 ./meta/hooks
-rwxr-xr-x root/root 0 2021-07-27 13:31 ./meta/hooks/bar-hook
drwxr-xr-x root/root 26 2021-07-27 13:31 ./meta/hooks/dir
-rwxr-xr-x root/root 0 2021-07-27 13:31 ./meta/hooks/dir/baz
-rwxr-xr-x root/root 0 2021-07-27 13:31 ./meta/hooks/foo-hook
-rw-r--r-- root/root 9 2021-07-27 13:31 ./meta/snap.yaml
EOF
`)
defer mockUnsquashfs.Restore()
s.testWalkMockedUnsquashfs(c)
}
func (s *SquashfsTestSuite) TestWalkMockedUnsquashfsOld(c *C) {
// mock behavior of pre-4.5 squashfs-tools
mockUnsquashfs := testutil.MockCommand(c, "unsquashfs", `
cat <<EOF
Parallel unsquashfs: Using 1 processor
5 inodes (1 blocks) to write
drwx------ root/root 55 2021-07-27 13:31 .
-rw-r--r-- root/root 0 2021-07-27 13:31 ./data.bin
drwxr-xr-x root/root 27 2021-07-27 13:31 ./food
drwxr-xr-x root/root 27 2021-07-27 13:31 ./food/bard
drwxr-xr-x root/root 3 2021-07-27 13:31 ./food/bard/bazd
drwxr-xr-x root/root 45 2021-07-27 13:31 ./meta
drwxr-xr-x root/root 58 2021-07-27 13:31 ./meta/hooks
-rwxr-xr-x root/root 0 2021-07-27 13:31 ./meta/hooks/bar-hook
drwxr-xr-x root/root 26 2021-07-27 13:31 ./meta/hooks/dir
-rwxr-xr-x root/root 0 2021-07-27 13:31 ./meta/hooks/dir/baz
-rwxr-xr-x root/root 0 2021-07-27 13:31 ./meta/hooks/foo-hook
-rw-r--r-- root/root 9 2021-07-27 13:31 ./meta/snap.yaml
EOF
`)
defer mockUnsquashfs.Restore()
s.testWalkMockedUnsquashfs(c)
}
// TestUnpackGlob tests the internal unpack
func (s *SquashfsTestSuite) TestUnpackGlob(c *C) {
data := "some random data"
sn := makeSnap(c, "", data)
outputDir := c.MkDir()
err := sn.Unpack("data*", outputDir)
c.Assert(err, IsNil)
// this is the file we expect
c.Assert(filepath.Join(outputDir, "data.bin"), testutil.FileEquals, data)
// ensure glob was honored
c.Assert(osutil.FileExists(filepath.Join(outputDir, "meta/snap.yaml")), Equals, false)
}
func (s *SquashfsTestSuite) TestUnpackDetectsFailures(c *C) {
mockUnsquashfs := testutil.MockCommand(c, "unsquashfs", `
cat >&2 <<EOF
Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols, skipping
Write on output file failed because No space left on device
writer: failed to write data block 0
Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols.bin, skipping
Write on output file failed because No space left on device
writer: failed to write data block 0
Failed to write /tmp/1/modules/4.4.0-112-generic/vdso/vdso32.so, skipping
Write on output file failed because No space left on device
writer: failed to write data block 0
Failed to write /tmp/1/modules/4.4.0-112-generic/vdso/vdso64.so, skipping
Write on output file failed because No space left on device
writer: failed to write data block 0
Failed to write /tmp/1/modules/4.4.0-112-generic/vdso/vdsox32.so, skipping
Write on output file failed because No space left on device
writer: failed to write data block 0
Failed to write /tmp/1/snap/manifest.yaml, skipping
Write on output file failed because No space left on device
writer: failed to write data block 0
Failed to write /tmp/1/snap/snapcraft.yaml, skipping
EOF
`)
defer mockUnsquashfs.Restore()
data := "mock kernel snap"
sn := makeSnap(c, "", data)
err := sn.Unpack("*", "some-output-dir")
c.Assert(err, NotNil)
c.Check(err.Error(), Equals, `cannot extract "*" to "some-output-dir": failed: "Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols, skipping", "Write on output file failed because No space left on device", "writer: failed to write data block 0", "Failed to write /tmp/1/modules/4.4.0-112-generic/modules.symbols.bin, skipping", and 15 more`)
}
func (s *SquashfsTestSuite) TestBuildAll(c *C) {
// please keep TestBuildUsesExcludes in sync with this one so it makes sense.
buildDir := c.MkDir()
err := os.MkdirAll(filepath.Join(buildDir, "/random/dir"), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(buildDir, "data.bin"), []byte("data"), 0644)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(buildDir, "random", "data.bin"), []byte("more data"), 0644)
c.Assert(err, IsNil)
sn := squashfs.New(filepath.Join(c.MkDir(), "foo.snap"))
err = sn.Build(buildDir, &squashfs.BuildOpts{SnapType: "app"})
c.Assert(err, IsNil)
// pre-4.5 unsquashfs writes a funny header like:
// "Parallel unsquashfs: Using 1 processor"
// "1 inodes (1 blocks) to write"
outputWithHeader, err := exec.Command("unsquashfs", "-n", "-l", sn.Path()).Output()
c.Assert(err, IsNil)
output := outputWithHeader
if bytes.HasPrefix(outputWithHeader, []byte(`Parallel unsquashfs: `)) {
split := bytes.Split(outputWithHeader, []byte("\n"))
output = bytes.Join(split[3:], []byte("\n"))
}
c.Assert(string(output), Equals, `
squashfs-root
squashfs-root/data.bin
squashfs-root/random
squashfs-root/random/data.bin
squashfs-root/random/dir
`[1:]) // skip the first newline :-)
}
func (s *SquashfsTestSuite) TestBuildUsesExcludes(c *C) {
// please keep TestBuild in sync with this one so it makes sense.
buildDir := c.MkDir()
err := os.MkdirAll(filepath.Join(buildDir, "/random/dir"), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(buildDir, "data.bin"), []byte("data"), 0644)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(buildDir, "random", "data.bin"), []byte("more data"), 0644)
c.Assert(err, IsNil)
excludesFilename := filepath.Join(buildDir, ".snapignore")
err = ioutil.WriteFile(excludesFilename, []byte(`
# ignore just one of the data.bin files we just added (the toplevel one)
data.bin
# also ignore ourselves
.snapignore
# oh and anything called "dir" anywhere
... dir
`), 0644)
c.Assert(err, IsNil)
sn := squashfs.New(filepath.Join(c.MkDir(), "foo.snap"))
err = sn.Build(buildDir, &squashfs.BuildOpts{
SnapType: "app",
ExcludeFiles: []string{excludesFilename},
})
c.Assert(err, IsNil)
outputWithHeader, err := exec.Command("unsquashfs", "-n", "-l", sn.Path()).Output()
c.Assert(err, IsNil)
output := outputWithHeader
if bytes.HasPrefix(outputWithHeader, []byte(`Parallel unsquashfs: `)) {
split := bytes.Split(outputWithHeader, []byte("\n"))
output = bytes.Join(split[3:], []byte("\n"))
}
// compare with TestBuild
c.Assert(string(output), Equals, `
squashfs-root
squashfs-root/random
squashfs-root/random/data.bin
`[1:]) // skip the first newline :-)
}
func (s *SquashfsTestSuite) TestBuildSupportsMultipleExcludesWithOnlyOneWildcardsFlag(c *C) {
defer squashfs.MockCommandFromSystemSnap(func(cmd string, args ...string) (*exec.Cmd, error) {
c.Check(cmd, Equals, "/usr/bin/mksquashfs")
return nil, errors.New("bzzt")
})()
mksq := testutil.MockCommand(c, "mksquashfs", "")
defer mksq.Restore()
snapPath := filepath.Join(c.MkDir(), "foo.snap")
sn := squashfs.New(snapPath)
err := sn.Build(c.MkDir(), &squashfs.BuildOpts{
SnapType: "core",
ExcludeFiles: []string{"exclude1", "exclude2", "exclude3"},
})
c.Assert(err, IsNil)
calls := mksq.Calls()
c.Assert(calls, HasLen, 1)
c.Check(calls[0], DeepEquals, []string{
// the usual:
"mksquashfs", ".", snapPath, "-noappend", "-comp", "xz", "-no-fragments", "-no-progress",
// the interesting bits:
"-wildcards", "-ef", "exclude1", "-ef", "exclude2", "-ef", "exclude3",
})
}
func (s *SquashfsTestSuite) TestBuildUsesMksquashfsFromCoreIfAvailable(c *C) {
usedFromCore := false
defer squashfs.MockCommandFromSystemSnap(func(cmd string, args ...string) (*exec.Cmd, error) {
usedFromCore = true
c.Check(cmd, Equals, "/usr/bin/mksquashfs")
return &exec.Cmd{Path: "/bin/true"}, nil
})()
mksq := testutil.MockCommand(c, "mksquashfs", "exit 1")
defer mksq.Restore()
buildDir := c.MkDir()
sn := squashfs.New(filepath.Join(c.MkDir(), "foo.snap"))
err := sn.Build(buildDir, nil)
c.Assert(err, IsNil)
c.Check(usedFromCore, Equals, true)
c.Check(mksq.Calls(), HasLen, 0)
}
func (s *SquashfsTestSuite) TestBuildUsesMksquashfsFromClassicIfCoreUnavailable(c *C) {
triedFromCore := false
defer squashfs.MockCommandFromSystemSnap(func(cmd string, args ...string) (*exec.Cmd, error) {
triedFromCore = true
c.Check(cmd, Equals, "/usr/bin/mksquashfs")
return nil, errors.New("bzzt")
})()
mksq := testutil.MockCommand(c, "mksquashfs", "")
defer mksq.Restore()
buildDir := c.MkDir()
sn := squashfs.New(filepath.Join(c.MkDir(), "foo.snap"))
err := sn.Build(buildDir, nil)
c.Assert(err, IsNil)
c.Check(triedFromCore, Equals, true)
c.Check(mksq.Calls(), HasLen, 1)
}
func (s *SquashfsTestSuite) TestBuildFailsIfNoMksquashfs(c *C) {
triedFromCore := false
defer squashfs.MockCommandFromSystemSnap(func(cmd string, args ...string) (*exec.Cmd, error) {
triedFromCore = true
c.Check(cmd, Equals, "/usr/bin/mksquashfs")
return nil, errors.New("bzzt")
})()
mksq := testutil.MockCommand(c, "mksquashfs", "exit 1")
defer mksq.Restore()
buildDir := c.MkDir()
sn := squashfs.New(filepath.Join(c.MkDir(), "foo.snap"))
err := sn.Build(buildDir, nil)
c.Assert(err, ErrorMatches, "mksquashfs call failed:.*")
c.Check(triedFromCore, Equals, true)
c.Check(mksq.Calls(), HasLen, 1)
}
func (s *SquashfsTestSuite) TestBuildVariesArgsByType(c *C) {
defer squashfs.MockCommandFromSystemSnap(func(cmd string, args ...string) (*exec.Cmd, error) {
return nil, errors.New("bzzt")
})()
mksq := testutil.MockCommand(c, "mksquashfs", "")
defer mksq.Restore()
buildDir := c.MkDir()
filename := filepath.Join(c.MkDir(), "foo.snap")
snap := squashfs.New(filename)
permissiveTypeArgs := []string{".", filename, "-noappend", "-comp", "xz", "-no-fragments", "-no-progress"}
restrictedTypeArgs := append(permissiveTypeArgs, "-all-root", "-no-xattrs")
tests := []struct {
snapType string
args []string
}{
{"", restrictedTypeArgs},
{"app", restrictedTypeArgs},
{"gadget", restrictedTypeArgs},
{"kernel", restrictedTypeArgs},
{"snapd", restrictedTypeArgs},
{"base", permissiveTypeArgs},
{"os", permissiveTypeArgs},
{"core", permissiveTypeArgs},
}
for _, t := range tests {
mksq.ForgetCalls()
comm := Commentf("type: %s", t.snapType)
c.Check(snap.Build(buildDir, &squashfs.BuildOpts{SnapType: t.snapType}), IsNil, comm)
c.Assert(mksq.Calls(), HasLen, 1, comm)
c.Assert(mksq.Calls()[0], HasLen, len(t.args)+1)
c.Check(mksq.Calls()[0][0], Equals, "mksquashfs", comm)
c.Check(mksq.Calls()[0][1:], DeepEquals, t.args, comm)
}
}
func (s *SquashfsTestSuite) TestBuildReportsFailures(c *C) {
mockUnsquashfs := testutil.MockCommand(c, "mksquashfs", `
echo Yeah, nah. >&2
exit 1
`)
defer mockUnsquashfs.Restore()
data := "mock kernel snap"
dir := makeSnapContents(c, "", data)
sn := squashfs.New("foo.snap")
c.Check(sn.Build(dir, &squashfs.BuildOpts{SnapType: "kernel"}), ErrorMatches, `mksquashfs call failed: Yeah, nah.`)
}
func (s *SquashfsTestSuite) TestUnsquashfsStderrWriter(c *C) {
for _, t := range []struct {
inp []string
expectedErr string
}{
{
inp: []string{"failed to write something\n"},
expectedErr: `failed: "failed to write something"`,
},
{
inp: []string{"fai", "led to write", " something\nunrelated\n"},
expectedErr: `failed: "failed to write something"`,
},
{
inp: []string{"failed to write\nfailed to read\n"},
expectedErr: `failed: "failed to write", and "failed to read"`,
},
{
inp: []string{"failed 1\nfailed 2\n3 failed\n"},
expectedErr: `failed: "failed 1", "failed 2", and "3 failed"`,
},
{
inp: []string{"failed 1\nfailed 2\n3 Failed\n4 Failed\n"},
expectedErr: `failed: "failed 1", "failed 2", "3 Failed", and "4 Failed"`,
},
{
inp: []string{"failed 1\nfailed 2\n3 Failed\n4 Failed\nfailed #5\n"},
expectedErr: `failed: "failed 1", "failed 2", "3 Failed", "4 Failed", and 1 more`,
},
} {
usw := squashfs.NewUnsquashfsStderrWriter()
for _, l := range t.inp {
usw.Write([]byte(l))
}
if t.expectedErr != "" {
c.Check(usw.Err(), ErrorMatches, t.expectedErr, Commentf("inp: %q failed", t.inp))
} else {
c.Check(usw.Err(), IsNil)
}
}
}
func (s *SquashfsTestSuite) TestBuildDate(c *C) {
// This env is used in reproducible builds and will force
// squashfs to use a specific date. We need to unset it
// for this specific test.
if oldEnv := os.Getenv("SOURCE_DATE_EPOCH"); oldEnv != "" {
os.Unsetenv("SOURCE_DATE_EPOCH")
defer func() { os.Setenv("SOURCE_DATE_EPOCH", oldEnv) }()
}
// make a directory
d := c.MkDir()
// set its time waaay back
now := time.Now()
then := now.Add(-10000 * time.Hour)
c.Assert(os.Chtimes(d, then, then), IsNil)
// make a snap using this directory
filename := filepath.Join(c.MkDir(), "foo.snap")
sn := squashfs.New(filename)
c.Assert(sn.Build(d, nil), IsNil)
// and see it's BuildDate is _now_, not _then_.
c.Check(squashfs.BuildDate(filename), Equals, sn.BuildDate())
c.Check(math.Abs(now.Sub(sn.BuildDate()).Seconds()) <= 61, Equals, true, Commentf("Unexpected build date %s", sn.BuildDate()))
}
func (s *SquashfsTestSuite) TestBuildChecksReadDifferentFiles(c *C) {
if os.Geteuid() == 0 {
c.Skip("cannot be tested when running as root")
}
// make a directory
d := c.MkDir()
err := os.MkdirAll(filepath.Join(d, "ro-dir"), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(d, "ro-dir", "in-ro-dir"), []byte("123"), 0664)
c.Assert(err, IsNil)
err = os.Chmod(filepath.Join(d, "ro-dir"), 0000)
c.Assert(err, IsNil)
// so that tear down does not complain
defer os.Chmod(filepath.Join(d, "ro-dir"), 0755)
err = ioutil.WriteFile(filepath.Join(d, "ro-file"), []byte("123"), 0000)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(d, "ro-empty-file"), nil, 0000)
c.Assert(err, IsNil)
err = syscall.Mkfifo(filepath.Join(d, "fifo"), 0000)
c.Assert(err, IsNil)
filename := filepath.Join(c.MkDir(), "foo.snap")
sn := squashfs.New(filename)
err = sn.Build(d, nil)
c.Assert(err, ErrorMatches, `(?s)cannot access the following locations in the snap source directory:
- ro-(file|dir) \(owner [0-9]+:[0-9]+ mode 000\)
- ro-(file|dir) \(owner [0-9]+:[0-9]+ mode 000\)
`)
}
func (s *SquashfsTestSuite) TestBuildChecksReadErrorLimit(c *C) {
if os.Geteuid() == 0 {
c.Skip("cannot be tested when running as root")
}
// make a directory
d := c.MkDir()
// make more than maxErrPaths entries
for i := 0; i < squashfs.MaxErrPaths; i++ {
p := filepath.Join(d, fmt.Sprintf("0%d", i))
err := ioutil.WriteFile(p, []byte("123"), 0000)
c.Assert(err, IsNil)
err = os.Chmod(p, 0000)
c.Assert(err, IsNil)
}
filename := filepath.Join(c.MkDir(), "foo.snap")
sn := squashfs.New(filename)
err := sn.Build(d, nil)
c.Assert(err, ErrorMatches, `(?s)cannot access the following locations in the snap source directory:
(- [0-9]+ \(owner [0-9]+:[0-9]+ mode 000.*\).){10}- too many errors, listing first 10 entries
`)
}
func (s *SquashfsTestSuite) TestBuildBadSource(c *C) {
filename := filepath.Join(c.MkDir(), "foo.snap")
sn := squashfs.New(filename)
err := sn.Build("does-not-exist", nil)
c.Assert(err, ErrorMatches, ".*does-not-exist/: no such file or directory")
}
func (s *SquashfsTestSuite) TestBuildWithCompressionHappy(c *C) {
buildDir := c.MkDir()
err := os.MkdirAll(filepath.Join(buildDir, "/random/dir"), 0755)
c.Assert(err, IsNil)
defaultComp := "xz"
for _, comp := range []string{"", "xz", "gzip", "lzo"} {
sn := squashfs.New(filepath.Join(c.MkDir(), "foo.snap"))
err = sn.Build(buildDir, &squashfs.BuildOpts{
Compression: comp,
})
c.Assert(err, IsNil)
// check compression
outputWithHeader, err := exec.Command("unsquashfs", "-n", "-s", sn.Path()).CombinedOutput()
c.Assert(err, IsNil)
// ensure default is xz
if comp == "" {
comp = defaultComp
}
c.Assert(string(outputWithHeader), Matches, fmt.Sprintf(`(?ms).*Compression %s$`, comp))
}
}
func (s *SquashfsTestSuite) TestBuildWithCompressionUnhappy(c *C) {
buildDir := c.MkDir()
err := os.MkdirAll(filepath.Join(buildDir, "/random/dir"), 0755)
c.Assert(err, IsNil)
sn := squashfs.New(filepath.Join(c.MkDir(), "foo.snap"))
err = sn.Build(buildDir, &squashfs.BuildOpts{
Compression: "silly",
})
c.Assert(err, ErrorMatches, "(?m)^mksquashfs call failed: ")
}