aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/binfmt_test.go
blob: 7345b09473940e4cfe5967033b65c4abdcee2bec (plain)
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
package container

import (
	"strings"
	"testing"

	"hakurei.app/fhs"
)

func TestEscapeBinfmt(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name  string
		magic string
		want  string
	}{
		{"packed DOS applications", "\x0eDEX", "\x0eDEX"},

		{"riscv64 magic",
			"\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xf3\x00",
			"\x7fELF\x02\x01\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\x02\\x00\xf3\\x00"},
		{"riscv64 mask",
			"\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff",
			"\xff\xff\xff\xff\xff\xff\xff\\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			got := escapeBinfmt(new(strings.Builder), tc.magic)
			if got != tc.want {
				t.Errorf("escapeBinfmt: %q, want %q", got, tc.want)
			}
		})
	}
}

func TestBinfmtEntry(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name  string
		e     BinfmtEntry
		valid bool
	}{
		{"zero", BinfmtEntry{}, false},
		{"large offset", BinfmtEntry{Offset: 128}, false},
		{"long magic", BinfmtEntry{Magic: strings.Repeat("\x00", 128)}, false},
		{"long mask", BinfmtEntry{Mask: strings.Repeat("\x00", 128)}, false},
		{"valid", BinfmtEntry{Interpreter: fhs.AbsRoot}, true},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			if tc.e.Valid() != tc.valid {
				t.Errorf("Valid: %v", !tc.valid)
			}
		})
	}
}