forked from hugelgupf/u-root
-
Notifications
You must be signed in to change notification settings - Fork 7
/
linux_test.go
99 lines (91 loc) · 1.88 KB
/
linux_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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package boot
import (
"errors"
"strings"
"testing"
"github.com/u-root/u-root/pkg/cpio"
)
func imageEqual(li1, li2 *LinuxImage) bool {
return cpio.ReaderAtEqual(li1.Kernel, li2.Kernel) &&
cpio.ReaderAtEqual(li1.Kernel, li2.Kernel) &&
li1.Cmdline == li2.Cmdline
}
var errSkip = errors.New("foo")
type errorReaderAt struct {
err error
}
func (e *errorReaderAt) ReadAt([]byte, int64) (int, error) {
return 0, e.err
}
func TestLinuxImage(t *testing.T) {
for _, tt := range []struct {
li *LinuxImage
err error
}{
{
li: &LinuxImage{
Kernel: strings.NewReader("foo"),
Initrd: strings.NewReader("bar"),
Cmdline: "foo=bar",
},
err: nil,
},
{
li: &LinuxImage{
Kernel: strings.NewReader("foo"),
Initrd: nil,
Cmdline: "foo=bar",
},
err: nil,
},
{
li: &LinuxImage{
Kernel: nil,
Initrd: nil,
Cmdline: "foo=bar",
},
err: ErrKernelMissing,
},
{
li: &LinuxImage{
Kernel: &errorReaderAt{err: errSkip},
Initrd: nil,
Cmdline: "foo=bar",
},
err: errSkip,
},
{
li: &LinuxImage{
Kernel: strings.NewReader("foo"),
Initrd: &errorReaderAt{err: errSkip},
Cmdline: "foo=bar",
},
err: errSkip,
},
{
li: &LinuxImage{
Kernel: strings.NewReader("foo"),
Initrd: nil,
Cmdline: "",
},
err: nil,
},
} {
a := cpio.InMemArchive()
sw := NewSigningWriter(a)
if err := tt.li.Pack(sw); err != tt.err {
t.Errorf("Pack(%v) = %v, want %v", tt.li, err, tt.err)
} else if err == nil {
li, err := NewLinuxImageFromArchive(a)
if err != nil {
t.Errorf("Linux image from %v: %v", a, err)
}
if !imageEqual(tt.li, li) {
t.Errorf("Images are not equal: got %v\nwant %v", li, tt.li)
}
}
}
}