File: binfmtmisc_linux_test.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (105 lines) | stat: -rw-r--r-- 2,451 bytes parent folder | download | duplicates (2)
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
//go:build !remote

package emulation

import (
	"fmt"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// parseBinfmtMisc parses a binfmt_misc registry entry.  It returns the offset,
// magic, and mask values, or an error if there was an error parsing the data.
// If the returned offset is negative, the entry was disabled or should be
// non-fatally ignored for some other reason.
func TestParseBinfmtMisc(t *testing.T) {
	vectors := []struct {
		platform, contents string
	}{
		{
			"linux/386",
			`
			enabled
			interpreter /usr/bin/qemu-i386-static
			flags: F
			offset 0
			magic 7f454c4601010100000000000000000002000300
			mask  fffffffffffefe00fffffffffffffffffeffffff
			`,
		},
		{
			"linux/amd64",
			`
			enabled
			interpreter /usr/bin/qemu-x86_64-static
			flags: F
			offset 0
			magic 7f454c4602010100000000000000000002003e00
			mask  fffffffffffefe00fffffffffffffffffeffffff
			`,
		},
		{
			"linux/arm",
			`
			enabled
			interpreter /usr/bin/qemu-arm-static
			flags: F
			offset 0
			magic 7f454c4601010100000000000000000002002800
			mask  ffffffffffffff00fffffffffffffffffeffffff
			`,
		},
		{
			"linux/arm64",
			`
			enabled
			interpreter /usr/bin/qemu-aarch64-static
			flags: F
			offset 0
			magic 7f454c460201010000000000000000000200b700
			mask  ffffffffffffff00fffffffffffffffffeffffff
			`,
		},
		{
			"linux/ppc64le",
			`
			enabled
			interpreter /usr/bin/qemu-ppc64le-static
			flags: F
			offset 0
			magic 7f454c4602010100000000000000000002001500
			mask  ffffffffffffff00fffffffffffffffffeffff00
			`,
		},
		{
			"linux/s390x",
			`
			enabled
			interpreter /usr/bin/qemu-s390x-static
			flags: F
			offset 0
			magic 7f454c4602020100000000000000000000020016
			mask  ffffffffffffff00fffffffffffffffffffeffff
			`,
		},
	}
	for i := range vectors {
		v := vectors[i]
		t.Run(v.platform, func(t *testing.T) {
			offset, magic, mask, err := parseBinfmtMisc(fmt.Sprintf("test vector %d", i), strings.NewReader(v.contents))
			require.NoError(t, err, "parseBinfmtMisc: %v", err)
			require.GreaterOrEqual(t, offset, 0, "%q shouldn't have been disabled", v.platform)
			headers := getKnownELFPlatformHeaders()[v.platform]
			matched := false
			for _, header := range headers {
				if magicMatch(header, offset, mask, magic) {
					matched = true
				}
			}
			assert.True(t, matched, "%q did not match an expected header match", v.platform)
		})
	}
}