-
Notifications
You must be signed in to change notification settings - Fork 9
/
reader_test.go
173 lines (155 loc) · 4.41 KB
/
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
package iso9660
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/hooklift/assert"
)
func TestNewReader(t *testing.T) {
image, err := os.Open("./fixtures/test.iso")
defer image.Close()
r, err := NewReader(image)
assert.Ok(t, err)
// Test first half of primary volume descriptor
assert.Equals(t, "CD001", string(r.pvd.StandardID[:]))
assert.Equals(t, 1, int(r.pvd.Type))
assert.Equals(t, 1, int(r.pvd.Version))
assert.Equals(t, "Mac OS X", strings.TrimSpace(string(r.pvd.SystemID[:])))
assert.Equals(t, "my-vol-id", strings.TrimSpace(string(r.pvd.ID[:])))
assert.Equals(t, 181, int(r.pvd.VolumeSpaceSizeBE))
assert.Equals(t, 1, int(r.pvd.VolumeSetSizeBE))
assert.Equals(t, 1, int(r.pvd.VolumeSeqNumberBE))
assert.Equals(t, 2048, int(r.pvd.LogicalBlkSizeBE))
assert.Equals(t, 46, int(r.pvd.PathTableSizeBE))
assert.Equals(t, 21, int(r.pvd.LocPathTableBE))
assert.Equals(t, 0, int(r.pvd.LocOptPathTableBE))
// Test root directory record values
assert.Equals(t, 0, int(r.pvd.DirectoryRecord.ExtendedAttrLen))
assert.Equals(t, 23, int(r.pvd.DirectoryRecord.ExtentLocationBE))
assert.Equals(t, 2048, int(r.pvd.DirectoryRecord.ExtentLengthBE))
assert.Equals(t, 2, int(r.pvd.DirectoryRecord.FileFlags))
assert.Equals(t, 0, int(r.pvd.DirectoryRecord.FileUnitSize))
assert.Equals(t, 0, int(r.pvd.DirectoryRecord.InterleaveGapSize))
assert.Equals(t, 1, int(r.pvd.DirectoryRecord.VolumeSeqNumberBE))
assert.Equals(t, 1, int(r.pvd.DirectoryRecord.FileIDLength))
// Test second half of primary volume descriptor
assert.Equals(t, "my-vol-id", strings.TrimSpace(string(r.pvd.ID[:])))
assert.Equals(t, "test-volset-id", strings.TrimSpace(string(r.pvd.VolumeSetID[:])))
assert.Equals(t, "hooklift", strings.TrimSpace(string(r.pvd.PublisherID[:])))
assert.Equals(t, "hooklift", strings.TrimSpace(string(r.pvd.DataPreparerID[:])))
assert.Equals(t, "MKISOFS ISO9660/HFS/UDF FILESYSTEM BUILDER & CDRECORD CD/DVD/BluRay CREATOR (C) 1993 E.YOUNGDALE (C) 1997 J.PEARSON/J.SCHILLING", strings.TrimSpace(string(r.pvd.AppID[:])))
assert.Equals(t, 1, int(r.pvd.FileStructVersion))
}
func TestUnpacking(t *testing.T) {
image, err := os.Open("./fixtures/test.iso")
defer image.Close()
reader, err := NewReader(image)
assert.Ok(t, err)
tests := []struct {
name string
isDir bool
content string
}{
{"/dir1", true, ""},
{"/dir2", true, ""},
{"/file.txt", false, "hola amigo\n"},
{"/dir1/hello.txt", false, "hello there!"},
{"/dir2/dir3", true, ""},
{"/dir2/dir3/blah.txt", false, "do you feel me?\n"},
}
count := 0
for {
fi, err := reader.Next()
if err == io.EOF {
break
}
assert.Ok(t, err)
f := fi.(*File)
//fmt.Printf("%s\n", tests[count].name)
assert.Equals(t, tests[count].name, f.Name())
assert.Equals(t, tests[count].isDir, f.IsDir())
rawBytes := f.Sys()
if !f.IsDir() {
assert.Cond(t, rawBytes != nil, "when it is file, content should not be nil")
content, err := ioutil.ReadAll(rawBytes.(io.Reader))
assert.Ok(t, err)
//fmt.Printf("%s -> %s\n", tests[count].name, content)
assert.Equals(t, tests[count].content, string(content[:]))
} else {
assert.Equals(t, nil, rawBytes)
}
count++
}
assert.Equals(t, 6, count)
}
// func TestBigImage(t *testing.T) {
// image, err := os.Open("./fixtures/test.iso.arch")
// defer image.Close()
// reader, err := NewReader(image)
// assert.Ok(t, err)
//
// count := 0
// for {
// fi, err := reader.Next()
// if err == io.EOF {
// break
// }
// assert.Ok(t, err)
//
// f := fi.(*File)
// fmt.Printf("%s\n", f.Name())
// count++
// }
//
// assert.Equals(t, 121, count)
// }
func ExampleReader() {
file, err := os.Open("archlinux.iso")
if err != nil {
panic(err)
}
r, err := NewReader(file)
if err != nil {
panic(err)
}
destPath := "tmp"
for {
f, err := r.Next()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fp := filepath.Join(destPath, f.Name())
if f.IsDir() {
if err := os.MkdirAll(fp, f.Mode()); err != nil {
panic(err)
}
continue
}
parentDir, _ := filepath.Split(fp)
if err := os.MkdirAll(parentDir, f.Mode()); err != nil {
panic(err)
}
freader := f.Sys().(io.Reader)
ff, err := os.Create(fp)
if err != nil {
panic(err)
}
defer func() {
if err := ff.Close(); err != nil {
panic(err)
}
}()
if err := ff.Chmod(f.Mode()); err != nil {
panic(err)
}
if _, err := io.Copy(ff, freader); err != nil {
panic(err)
}
}
}