forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uroot_test.go
350 lines (331 loc) · 9.01 KB
/
uroot_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2015-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 main
import (
"bufio"
"bytes"
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/u-root/u-root/pkg/cpio"
"github.com/u-root/u-root/pkg/golang"
"github.com/u-root/u-root/pkg/testutil"
"github.com/u-root/u-root/pkg/uio"
itest "github.com/u-root/u-root/pkg/uroot/initramfs/test"
)
var twocmds = []string{
"github.com/u-root/u-root/cmds/core/ls",
"github.com/u-root/u-root/cmds/core/init",
}
func xTestDCE(t *testing.T) {
delFiles := false
f, _ := buildIt(
t,
[]string{
"-build=bb", "-no-strip",
"world",
"-github.com/u-root/u-root/cmds/exp/builtin",
"-github.com/u-root/u-root/cmds/exp/run",
"github.com/u-root/u-root/pkg/uroot/test/foo",
},
nil,
nil)
defer func() {
if delFiles {
os.RemoveAll(f.Name())
}
}()
st, _ := f.Stat()
t.Logf("Built %s, size %d", f.Name(), st.Size())
cmd := golang.Default().GoCmd("tool", "nm", f.Name())
nmOutput, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("failed to run nm: %s %s", err, nmOutput)
}
symScanner := bufio.NewScanner(bytes.NewBuffer(nmOutput))
syms := map[string]bool{}
for symScanner.Scan() {
line := symScanner.Text()
parts := strings.Split(line, " ")
if len(parts) == 0 {
continue
}
sym := parts[len(parts)-1]
syms[sym] = true
t.Logf("%s", sym)
}
}
type noDeadCode struct {
Path string
}
func (v noDeadCode) Validate(a *cpio.Archive) error {
// 1. Extract BB binary into a temporary file.
delFiles := true
bbRecord, ok := a.Get(v.Path)
if !ok {
return fmt.Errorf("archive does not contain %s, but should", v.Path)
}
tf, err := os.CreateTemp("", "u-root-temp-bb-")
if err != nil {
return err
}
bbData, _ := uio.ReadAll(bbRecord)
tf.Write(bbData)
tf.Close()
defer func() {
if delFiles {
os.RemoveAll(tf.Name())
}
}()
// 2. Run "go nm" on it and build symbol table.
cmd := golang.Default().GoCmd("tool", "nm", tf.Name())
nmOutput, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run nm: %s %s", err, nmOutput)
}
symScanner := bufio.NewScanner(bytes.NewBuffer(nmOutput))
syms := map[string]bool{}
for symScanner.Scan() {
line := symScanner.Text()
parts := strings.Split(line, " ")
if len(parts) == 0 {
continue
}
sym := parts[len(parts)-1]
syms[sym] = true
}
// 3. Check for presence and absence of particular symbols.
if !syms["github.com/u-root/u-root/pkg/uroot/test/bar.Bar.UsedInterfaceMethod"] {
// Sanity check of the test itself: this method must be in the binary.
return fmt.Errorf("expected symbol not found, something is wrong with the build")
}
if syms["github.com/u-root/u-root/pkg/uroot/test/bar.Bar.UnusedNonInterfaceMethod"] {
// Sanity check of the test itself: this method must be in the binary.
delFiles = false
return fmt.Errorf(
"Unused non-interface method has not been eliminated, dead code elimination is not working properly.\n"+
"The most likely reason is use of reflect.Value.Method or .MethodByName somewhere "+
"(could be a command or vendor dependency, apologies for not being more precise here).\n"+
"See https://golang.org/src/cmd/link/internal/ld/deadcode.go for explanation.\n"+
"%s contains the resulting binary.\n", tf.Name())
}
return nil
}
func TestUrootCmdline(t *testing.T) {
samplef, err := os.CreateTemp("", "u-root-test-")
if err != nil {
t.Fatal(err)
}
samplef.Close()
defer os.RemoveAll(samplef.Name())
sampledir := t.TempDir()
if err = os.WriteFile(filepath.Join(sampledir, "foo"), nil, 0o644); err != nil {
t.Fatal(err)
}
if err = os.WriteFile(filepath.Join(sampledir, "bar"), nil, 0o644); err != nil {
t.Fatal(err)
}
type testCase struct {
name string
env []string
args []string
err error
validators []itest.ArchiveValidator
}
noCmdTests := []testCase{
{
name: "include one extra file",
args: []string{"-nocmd", "-files=/bin/bash"},
env: []string{"GO111MODULE=off"},
err: nil,
validators: []itest.ArchiveValidator{
itest.HasFile{"bin/bash"},
},
},
{
name: "fix usage of an absolute path",
args: []string{"-nocmd", fmt.Sprintf("-files=%s:/bin", sampledir)},
env: []string{"GO111MODULE=off"},
err: nil,
validators: []itest.ArchiveValidator{
itest.HasFile{"/bin/foo"},
itest.HasFile{"/bin/bar"},
},
},
{
name: "include multiple extra files",
args: []string{"-nocmd", "-files=/bin/bash", "-files=/bin/ls", fmt.Sprintf("-files=%s", samplef.Name())},
env: []string{"GO111MODULE=off"},
validators: []itest.ArchiveValidator{
itest.HasFile{"bin/bash"},
itest.HasFile{"bin/ls"},
itest.HasFile{samplef.Name()},
},
},
{
name: "include one extra file with rename",
args: []string{"-nocmd", "-files=/bin/bash:bin/bush"},
env: []string{"GO111MODULE=off"},
validators: []itest.ArchiveValidator{
itest.HasFile{"bin/bush"},
},
},
}
bareTests := []testCase{
{
name: "uinitcmd",
args: []string{"-uinitcmd=echo foobar fuzz", "-defaultsh=", "github.com/u-root/u-root/cmds/core/init", "github.com/u-root/u-root/cmds/core/echo"},
err: nil,
validators: []itest.ArchiveValidator{
itest.HasRecord{cpio.Symlink("bin/uinit", "../bbin/echo")},
itest.HasContent{
Path: "etc/uinit.flags",
Content: "\"foobar\"\n\"fuzz\"",
},
},
},
{
name: "dead_code_elimination",
args: []string{
// Build the world + test symbols, unstripped.
// Change default shell to gosh for this test as elvish uses the reflect package
"-defaultsh=/bbin/gosh", "-no-strip", "world", "github.com/u-root/u-root/pkg/uroot/test/foo",
// These are known to disable DCE and need to be exluded.
// elvish uses reflect.Value.Call and is expected to not use DCE.
"-github.com/u-root/u-root/cmds/core/elvish",
},
err: nil,
validators: []itest.ArchiveValidator{
noDeadCode{Path: "bbin/bb"},
},
},
{
name: "hosted mode",
args: append([]string{"-base=/dev/null", "-defaultsh=", "-initcmd="}, twocmds...),
},
{
name: "AMD64 build",
env: []string{"GOARCH=amd64"},
args: []string{"all"},
},
{
name: "MIPS build",
env: []string{"GOARCH=mips"},
args: []string{"all"},
},
{
name: "MIPSLE build",
env: []string{"GOARCH=mipsle"},
args: []string{"all"},
},
{
name: "MIPS64 build",
env: []string{"GOARCH=mips64"},
args: []string{"all"},
},
{
name: "MIPS64LE build",
env: []string{"GOARCH=mips64le"},
args: []string{"all"},
},
{
name: "ARM7 build",
env: []string{"GOARCH=arm", "GOARM=7"},
args: []string{"all"},
},
{
name: "ARM64 build",
env: []string{"GOARCH=arm64"},
args: []string{"all"},
},
{
name: "386 (32 bit) build",
env: []string{"GOARCH=386"},
args: []string{"all"},
},
{
name: "Power 64bit build",
env: []string{"GOARCH=ppc64le"},
args: []string{"all"},
},
{
name: "RISCV 64bit build",
env: []string{"GOARCH=riscv64"},
args: []string{"all"},
},
}
var bbTests []testCase
for _, test := range bareTests {
bbTest := test
bbTest.name = bbTest.name + " bb"
bbTest.args = append([]string{"-build=bb"}, bbTest.args...)
bbTest.env = append(bbTest.env, "GO111MODULE=off")
gbbTest := test
gbbTest.name = gbbTest.name + " gbb"
gbbTest.args = append([]string{"-build=gbb"}, gbbTest.args...)
gbbTest.env = append(gbbTest.env, "GO111MODULE=on")
bbTests = append(bbTests, gbbTest, bbTest)
}
for _, tt := range append(noCmdTests, bbTests...) {
t.Run(tt.name, func(t *testing.T) {
delFiles := true
f, sum1 := buildIt(t, tt.args, tt.env, tt.err)
defer func() {
if delFiles {
os.RemoveAll(f.Name())
}
}()
a, err := itest.ReadArchive(f.Name())
if err != nil {
t.Fatal(err)
}
for _, v := range tt.validators {
if err := v.Validate(a); err != nil {
t.Errorf("validator failed: %v / archive:\n%s", err, a)
}
}
f2, sum2 := buildIt(t, tt.args, tt.env, tt.err)
defer func() {
if delFiles {
os.RemoveAll(f2.Name())
}
}()
if !bytes.Equal(sum1, sum2) {
delFiles = false
t.Errorf("not reproducible, hashes don't match")
t.Errorf("env: %v args: %v", tt.env, tt.args)
t.Errorf("file1: %v file2: %v", f.Name(), f2.Name())
}
})
}
}
func buildIt(t *testing.T, args, env []string, want error) (*os.File, []byte) {
f, err := os.CreateTemp("", "u-root-")
if err != nil {
t.Fatal(err)
}
// Use the u-root command outside of the $GOPATH tree to make sure it
// still works.
arg := append([]string{"-o", f.Name()}, args...)
c := testutil.Command(t, arg...)
t.Logf("Commandline: %v u-root %v", strings.Join(env, " "), strings.Join(arg, " "))
c.Env = append(c.Env, env...)
if out, err := c.CombinedOutput(); err != want {
t.Fatalf("Error: %v\nOutput:\n%s", err, out)
} else if err != nil {
h1 := sha256.New()
if _, err := io.Copy(h1, f); err != nil {
t.Fatal()
}
return f, h1.Sum(nil)
}
return f, nil
}
func TestMain(m *testing.M) {
testutil.Run(m, main)
}