forked from cavaliergopher/rpm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lead_test.go
67 lines (57 loc) · 1.59 KB
/
lead_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
package rpm
import (
"bytes"
"io"
"testing"
"github.com/cavaliercoder/badio"
)
func TestLeadErrors(t *testing.T) {
// load package file paths
files := getTestFiles()
// load each package
for _, fb := range files {
// local copy the file so the following corruptions don't break
// other tests
b := make([]byte, len(fb))
copy(b, fb)
// simulate read error
f := bytes.NewReader(b)
r := badio.NewBreakReader(f, 95)
_, err := ReadPackageLead(r)
if err == nil {
t.Errorf("Expected read error in lead section, got: %v", err)
}
// simulate length error
f.Seek(0, 0) // io.SeekStart since ~1.7
r = badio.NewTruncateReader(f, 95)
_, err = ReadPackageLead(r)
if err != io.ErrUnexpectedEOF {
t.Errorf("Expected bad length error in lead section, got: %v", err)
}
// simulate version error
f.Seek(0, 0) // io.SeekStart since ~1.7
b[4] = 0x02
_, err = ReadPackageLead(bytes.NewReader(b))
if err != ErrUnsupportedVersion {
t.Errorf("Expected version error in lead section, got: %v", err)
}
b[4] = 0x05
_, err = ReadPackageLead(bytes.NewReader(b))
if err != ErrUnsupportedVersion {
t.Errorf("Expected version error in lead section, got: %v", err)
}
}
// simulate magic number error
b := make([]byte, 96)
r := bytes.NewReader(b)
_, err := ReadPackageLead(r)
if err != ErrNotRPMFile {
t.Errorf("Expected bad descriptor error in lead section, got: %v", err)
}
// test handler in ReadPackageFile
r = bytes.NewReader(b)
_, err = ReadPackageFile(r)
if err != ErrNotRPMFile {
t.Errorf("Expected lead section error in bad package, got: %v", err)
}
}