-
Notifications
You must be signed in to change notification settings - Fork 43
/
image_reader_test.go
180 lines (140 loc) · 4.94 KB
/
image_reader_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
//go:build !integration
// +build !integration
package iso9660
import (
"io"
"io/fs"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
func TestImageReader(t *testing.T) {
tz := time.FixedZone("", 3600*2)
recordTime := time.Date(2018, 07, 25, 22, 01, 02, 0, tz)
f, err := os.Open("fixtures/test.iso")
assert.NoError(t, err)
defer f.Close() // nolint: errcheck
image, err := OpenImage(f)
if assert.NoError(t, err) {
assert.Equal(t, 2, len(image.volumeDescriptors))
assert.Equal(t, volumeTypePrimary, image.volumeDescriptors[0].Header.Type)
assert.Equal(t, volumeTypeTerminator, image.volumeDescriptors[1].Header.Type)
}
label, err := image.Label()
assert.NoError(t, err)
assert.Equal(t, "my-vol-id", label)
rootDir, err := image.RootDir()
assert.NoError(t, err)
assert.True(t, rootDir.IsDir())
assert.Equal(t, string([]byte{0}), rootDir.Name())
assert.Equal(t, int64(sectorSize), rootDir.Size())
assert.Equal(t, recordTime, rootDir.ModTime())
children, err := rootDir.GetChildren()
assert.NoError(t, err)
assert.Len(t, children, 4)
cicero := children[0]
dir1 := children[1]
dir2 := children[2]
dir4 := children[3]
assert.Equal(t, "CICERO.TXT", cicero.Name())
assert.Equal(t, int64(845), cicero.Size())
assert.Nil(t, cicero.susp) // has no SUSP / RR
if assert.Equal(t, "DIR1", dir1.Name()) {
dir1Children, err := dir1.GetChildren()
assert.NoError(t, err)
assert.Len(t, dir1Children, 1)
loremFile := dir1Children[0]
assert.Equal(t, "LOREM_IP.TXT", loremFile.Name())
assert.Equal(t, int64(446), loremFile.Size())
data, err := io.ReadAll(loremFile.Reader())
assert.NoError(t, err)
assert.Equal(t, loremIpsum, string(data))
}
if assert.Equal(t, "DIR2", dir2.Name()) {
dir2Children, err := dir2.GetChildren()
assert.NoError(t, err)
assert.Len(t, dir2Children, 2)
assert.Equal(t, "DIR3", dir2Children[0].Name())
dir3Children, err := dir2Children[0].GetChildren()
assert.NoError(t, err)
assert.Len(t, dir3Children, 1)
assert.Equal(t, "DATA.BIN", dir3Children[0].Name())
assert.Equal(t, int64(512), dir3Children[0].Size())
assert.Equal(t, "LARGE.TXT", dir2Children[1].Name())
assert.Equal(t, int64(2808), dir2Children[1].Size())
assert.False(t, dir2Children[1].IsDir())
}
if assert.Equal(t, "DIR4", dir4.Name()) {
dir4Children, err := dir4.GetChildren()
assert.NoError(t, err)
assert.Len(t, dir4Children, 1000)
assert.Equal(t, "FILE1012", dir4Children[12].Name())
}
}
func TestFile(t *testing.T) {
f := File{
ra: nil,
de: &DirectoryEntry{
FileFlags: dirFlagDir,
},
children: []*File{},
}
assert.Nil(t, f.Reader())
assert.Equal(t, os.ModeDir, f.Mode())
}
func TestImage(t *testing.T) {
imageWithoutDescriptors := Image{
ra: nil,
volumeDescriptors: []volumeDescriptor{},
}
_, err := imageWithoutDescriptors.RootDir()
assert.Error(t, os.ErrNotExist, err)
}
func TestImageReaderSUSP(t *testing.T) {
f, err := os.Open("fixtures/test_rockridge.iso")
assert.NoError(t, err)
defer f.Close() // nolint: errcheck
image, err := OpenImage(f)
assert.NoError(t, err)
// root dir
rootDir, err := image.RootDir()
assert.NoError(t, err)
rootDot, err := rootDir.GetDotEntry()
assert.NoError(t, err)
ers, err := rootDot.de.SystemUseEntries.GetExtensionRecords()
assert.NoError(t, err)
// has Rock Ridge
if assert.Len(t, ers, 1) {
assert.Equal(t, "RRIP_1991A", ers[0].Identifier)
assert.Equal(t, 1, ers[0].Version)
}
children, err := rootDir.GetChildren()
assert.NoError(t, err)
assert.Len(t, children, 5)
// symlink
symlink := children[4]
assert.Equal(t, "this-is-a-symlink", symlink.Name())
assert.Equal(t, os.ModeSymlink, symlink.Mode()&os.ModeSymlink)
dir1 := children[1]
assert.Equal(t, "dir1", dir1.Name())
dir1Children, err := dir1.GetChildren()
assert.NoError(t, err)
assert.Len(t, dir1Children, 1)
// lorem ipsum
loremFile := dir1Children[0]
assert.Equal(t, "lorem_ipsum.txt", loremFile.Name())
assert.Equal(t, int64(446), loremFile.Size())
assert.Equal(t, fs.FileMode(0640), loremFile.Mode().Perm(), "expected mode %o, got %o", 0640, loremFile.Mode().Perm())
assert.NotNil(t, loremFile.susp)
assert.True(t, loremFile.susp.HasRockRidge)
data, err := io.ReadAll(loremFile.Reader())
assert.NoError(t, err)
assert.Equal(t, loremIpsum, string(data))
assert.Len(t, loremFile.de.SystemUseEntries, 4)
assert.Equal(t, "RR", loremFile.de.SystemUseEntries[0].Type())
assert.Equal(t, "PX", loremFile.de.SystemUseEntries[2].Type())
assert.Equal(t, "TF", loremFile.de.SystemUseEntries[3].Type())
}