-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquashfs_test.go
152 lines (132 loc) · 4.22 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
package squashfs_test
import (
"crypto/sha256"
"encoding/hex"
"errors"
"io/fs"
"log"
"testing"
"time"
"github.com/KarpelesLab/squashfs"
)
// testdata/zlib-dev.squashfs
func s256(buf []byte) string {
hash := sha256.Sum256(buf)
return hex.EncodeToString(hash[:])
}
func TestSquashfs(t *testing.T) {
sqfs, err := squashfs.Open("testdata/zlib-dev.squashfs")
if err != nil {
t.Fatalf("failed to open testdata/zlib-dev.squashfs: %s", err)
}
defer sqfs.Close()
data, err := fs.ReadFile(sqfs, "pkgconfig/zlib.pc")
if err != nil {
t.Errorf("failed to read pkgconfig/zlib.pc: %s", err)
} else {
//log.Printf("zlib.pc = %s", s256(data))
if s256(data) != "2bbfca2364630d3ad2bbc9d44f45fe5470236539a906e11e2072157709e54692" {
t.Errorf("invalid hash for pkgconfig/zlib.pc")
}
}
// ensure we get the right inode
ino, err := sqfs.FindInode("lib/libz.a", false)
if err != nil {
t.Errorf("failed to find lib/libz.a")
} else {
// should be inode 6
if ino.Ino != 6 {
t.Errorf("invalid inode found for lib/libz.a")
}
}
// test glob (will test readdir etc)
res, err := fs.Glob(sqfs, "lib/*.so")
if err != nil {
t.Errorf("failed to glob lib/*.so: %s", err)
} else {
if len(res) != 1 || res[0] != "lib/libz.so" {
log.Printf("bad response for glob lib/*.so")
}
}
st, err := fs.Stat(sqfs, "include/zlib.h")
if err != nil {
t.Errorf("failed to stat include/zlib.h: %s", err)
} else {
if st.Size() != 97323 {
t.Errorf("bad file size on stat include/zlib.h")
}
}
// test stat vs lstat
st, err = fs.Stat(sqfs, "lib")
if err != nil {
t.Errorf("failed to stat lib: %s", err)
} else if !st.IsDir() {
t.Errorf("failed: stat(lib) did not return a directory")
}
st, err = sqfs.Lstat("lib")
if err != nil {
t.Errorf("failed to lstat lib: %s", err)
} else if st.IsDir() {
t.Errorf("failed: lstat(lib) should have returned something that is not a directory")
}
// test error
_, err = fs.ReadFile(sqfs, "pkgconfig/zlib.pc/foo")
if !errors.Is(err, squashfs.ErrNotDirectory) {
t.Errorf("readfile pkgconfig/zlib.pc/foo returned unexpected err=%s", err)
}
// test other error
_, err = sqfs.FindInode("lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/../lib/libz.a", false)
if !errors.Is(err, squashfs.ErrTooManySymlinks) {
t.Errorf("readfile lib/../lib/../(...)/libz.a returned unexpected err=%s", err)
}
}
func TestBigdir(t *testing.T) {
sqfs, err := squashfs.Open("testdata/bigdir.squashfs")
if err != nil {
t.Fatalf("failed to open testdata/bigdir.squashfs: %s", err)
}
defer sqfs.Close()
t1 := time.Now()
data, err := fs.ReadFile(sqfs, "bigdir/99999.txt")
d := time.Since(t1)
if err != nil {
t.Errorf("failed to read bigdir/99999.txt: %s", err)
} else {
//log.Printf("zlib.pc = %s", s256(data))
if string(data) != "" {
t.Errorf("invalid value for bigdir/99999.txt")
}
if d > 2*time.Millisecond {
t.Errorf("read of bigdir/99999.txt took too long: %s (expected sub-millisecond read time)", d)
}
}
data, err = fs.ReadFile(sqfs, "bigdir/999.txt")
if err != nil {
t.Errorf("failed to read bigdir/999.txt: %s", err)
} else if string(data) != "" {
t.Errorf("invalid value for bigdir/999.txt")
}
_, err = fs.ReadFile(sqfs, "bigdir/999999.txt")
if err == nil {
t.Errorf("failed to fail to read bigdir/999999.txt: %s", err)
}
_, err = fs.ReadFile(sqfs, "bigdir/12345.txt")
if err != nil {
t.Errorf("failed to read bigdir/12345.txt: %s", err)
}
_, err = fs.ReadFile(sqfs, "bigdir/76543.txt")
if err != nil {
t.Errorf("failed to read bigdir/76543.txt: %s", err)
}
// test for failure on:
// ~/pkg/main/azusa.symlinks.core/full/lib64/libLLVMIRReader.a
sqfs, err = squashfs.Open("testdata/azusa_symlinks.squashfs")
if err != nil {
t.Fatalf("failed to open testdata/azusa_symlinks.squashfs: %s", err)
}
defer sqfs.Close()
_, err = sqfs.FindInode("full/lib64/libLLVMIRReader.a", false)
if err != nil {
t.Errorf("failed to find inode full/lib64/libLLVMIRReader.a: %s", err)
}
}