-
Notifications
You must be signed in to change notification settings - Fork 44
/
package_test.go
221 lines (209 loc) · 6.07 KB
/
package_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
package rpm
import (
"bytes"
"fmt"
"hash/crc32"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func getTestFiles() map[string][]byte {
path := os.Getenv("RPM_DIR")
if path == "" {
path = "./testdata"
}
dir, err := ioutil.ReadDir(path)
if err != nil {
panic(err)
}
files := make([]string, 0)
for _, f := range dir {
if strings.HasSuffix(f.Name(), ".rpm") {
files = append(files, filepath.Join(path, f.Name()))
}
}
if len(files) == 0 {
panic("No rpm packages found for testing")
}
testFiles := make(map[string][]byte, len(files))
for _, filename := range files {
b, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
testFiles[filename] = b
}
return testFiles
}
func openPackage(name string) *Package {
p, err := Open(name)
if err != nil {
panic(err)
}
return p
}
func openPackages(path string) []*Package {
dir, err := ioutil.ReadDir(path)
if err != nil {
panic(err)
}
files := make([]string, 0)
for _, f := range dir {
if strings.HasSuffix(f.Name(), ".rpm") {
files = append(files, filepath.Join(path, f.Name()))
}
}
packages := make([]*Package, len(files))
for i, f := range files {
p := openPackage(f)
packages[i] = p
}
return packages
}
func TestReadRPMFile(t *testing.T) {
// load package file paths
files := getTestFiles()
valid := 0
for path, b := range files {
// Load package info
rpm, err := Read(bytes.NewReader(b))
if err != nil {
t.Errorf("Error loading rpm file %s: %s", path, err)
} else {
t.Logf("Loaded package: %v", rpm)
valid++
}
}
t.Logf("Validated %d rpm files", valid)
}
func TestPackageFiles(t *testing.T) {
names := []string{
"/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7",
"/etc/yum.repos.d/epel-testing.repo",
"/etc/yum.repos.d/epel.repo",
"/usr/lib/rpm/macros.d/macros.epel",
"/usr/lib/systemd/system-preset/90-epel.preset",
"/usr/share/doc/epel-release-7",
"/usr/share/doc/epel-release-7/GPL",
}
modes := []int64{0644, 0644, 0644, 0644, 0644, 0755, 0644}
sizes := []int64{1662, 1056, 957, 41, 2813, 4096, 18385}
owners := []string{"root", "root", "root", "root", "root", "root", "root"}
groups := []string{"root", "root", "root", "root", "root", "root", "root"}
modtimes := []time.Time{
time.Unix(1416932629, 0),
time.Unix(1416932629, 0),
time.Unix(1416932629, 0),
time.Unix(1416932629, 0),
time.Unix(1416932629, 0),
time.Unix(1416932778, 0),
time.Unix(1416932629, 0),
}
digests := []string{
"028b9accc59bab1d21f2f3f544df5469910581e728a64fd8c411a725a82300c2",
"d9662befdbfb661b20b3af4a7feb34c6f58b4dc689bbeb0f29c73438015701b9",
"87d225d205a6263509508ac5cd4ca1bf1dc3e87960c9d305b3eb6c560f270297",
"6a43fe82450861a67ab673151972515069fe7fab44679f60345c826ac37e3e08",
"3de82a16cbc9eba0aa7c7edd7ef5e326a081afc8325aaf21ad11a68698b6b1d0",
"", // digests field only populated for regular files
"03a55cfbbbfcdfc75fed8aeca5383fef12de4f019d5ff15c58f1e6581465007e",
}
// the test RPM has no links
linknames := []string{"", "", "", "", "", "", ""}
path := "./testdata/epel-release-7-5.noarch.rpm"
p := openPackage(path)
files := p.Files()
if len(files) != len(names) {
t.Fatalf("expected %v files in rpm package but got %v", len(names), len(files))
}
for i, fi := range files {
name := fi.Name()
if name != names[i] {
t.Errorf("expected file %v with name %v but got %v", i, names[i], name)
continue
}
if mode := int64(fi.Mode().Perm()); mode != modes[i] {
t.Errorf("expected mode %v but got %v for %v", modes[i], mode, name)
}
if size := fi.Size(); size != sizes[i] {
t.Errorf("expected size %v but got %v for %v", sizes[i], size, name)
}
if owner := fi.Owner(); owner != owners[i] {
t.Errorf("expected owner %v but got %v for %v", owners[i], owner, name)
}
if group := fi.Group(); group != groups[i] {
t.Errorf("expected group %v but got %v for %v", groups[i], group, name)
}
if modtime := fi.ModTime(); modtime != modtimes[i] {
t.Errorf("expected modtime %v but got %v for %v", modtimes[i], modtime.Unix(), name)
}
if digest := fi.Digest(); digest != digests[i] {
t.Errorf("expected digest %v but got %v for %v", digests[i], digest, name)
}
if linkname := fi.Linkname(); linkname != linknames[i] {
t.Errorf("expected linkname %v but got %v for %v", linknames[i], linkname, name)
}
}
}
func TestByteTags(t *testing.T) {
tests := []struct {
Path string
GPGSignatureCRC uint32
}{
{
Path: "testdata/centos-release-6-0.el6.centos.5.i686.rpm",
GPGSignatureCRC: 1788312322,
},
{
Path: "testdata/centos-release-6-0.el6.centos.5.x86_64.rpm",
GPGSignatureCRC: 3194808352,
},
{
Path: "testdata/centos-release-7-2.1511.el7.centos.2.10.x86_64.rpm",
GPGSignatureCRC: 3466078337,
},
{
Path: "testdata/epel-release-7-5.noarch.rpm",
GPGSignatureCRC: 2817187108,
},
}
for _, test := range tests {
p := openPackage(test.Path)
if crc := crc32.ChecksumIEEE(p.GPGSignature()); crc != test.GPGSignatureCRC {
t.Errorf("expected GPG Signature CRC %v, got %v for %v", test.GPGSignatureCRC, crc, test.Path)
}
}
}
// Lists all the files in an rpm package.
func ExamplePackage_Files() {
// open a package file
pkg, err := Open("./testdata/epel-release-7-5.noarch.rpm")
if err != nil {
log.Fatal(err)
}
// list each file
files := pkg.Files()
fmt.Printf("total %v\n", len(files))
for _, fi := range files {
fmt.Printf("%v %v %v %5v %v %v\n",
fi.Mode().Perm(),
fi.Owner(),
fi.Group(),
fi.Size(),
fi.ModTime().UTC().Format("Jan 02 15:04"),
fi.Name())
}
// Output:
// total 7
// -rw-r--r-- root root 1662 Nov 25 16:23 /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
// -rw-r--r-- root root 1056 Nov 25 16:23 /etc/yum.repos.d/epel-testing.repo
// -rw-r--r-- root root 957 Nov 25 16:23 /etc/yum.repos.d/epel.repo
// -rw-r--r-- root root 41 Nov 25 16:23 /usr/lib/rpm/macros.d/macros.epel
// -rw-r--r-- root root 2813 Nov 25 16:23 /usr/lib/systemd/system-preset/90-epel.preset
// -rwxr-xr-x root root 4096 Nov 25 16:26 /usr/share/doc/epel-release-7
// -rw-r--r-- root root 18385 Nov 25 16:23 /usr/share/doc/epel-release-7/GPL
}