File: efi_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (173 lines) | stat: -rw-r--r-- 5,287 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
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2020 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package efi_test

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/bootloader/bootloadertest"
	"github.com/snapcore/snapd/bootloader/efi"
	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/testutil"
)

type efiVarsSuite struct {
	testutil.BaseTest

	rootdir string
}

var _ = Suite(&efiVarsSuite{})

func TestBoot(t *testing.T) { TestingT(t) }

func (s *efiVarsSuite) SetUpTest(c *C) {
	s.BaseTest.SetUpTest(c)

	s.rootdir = c.MkDir()
	dirs.SetRootDir(s.rootdir)
	s.AddCleanup(func() { dirs.SetRootDir("") })

	err := os.MkdirAll(filepath.Join(s.rootdir, "/sys/firmware/efi/efivars"), 0755)
	c.Assert(err, IsNil)

	efivarfsMount := `
38 24 0:32 / /sys/firmware/efi/efivars rw,nosuid,nodev,noexec,relatime shared:13 - efivarfs efivarfs rw
`
	restore := osutil.MockMountInfo(strings.TrimSpace(efivarfsMount))
	s.AddCleanup(restore)
}

func (s *efiVarsSuite) TestNoEFISystem(c *C) {
	// no efivarfs
	osutil.MockMountInfo("")

	_, _, err := efi.ReadVarBytes("my-cool-efi-var")
	c.Check(err, Equals, efi.ErrNoEFISystem)

	_, _, err = efi.ReadVarString("my-cool-efi-var")
	c.Check(err, Equals, efi.ErrNoEFISystem)
}

func (s *efiVarsSuite) TestSizeError(c *C) {
	// mock the efi var file
	varPath := filepath.Join(s.rootdir, "/sys/firmware/efi/efivars", "my-cool-efi-var")
	err := os.WriteFile(varPath, []byte("\x06"), 0644)
	c.Assert(err, IsNil)

	_, _, err = efi.ReadVarBytes("my-cool-efi-var")
	c.Check(err, ErrorMatches, `cannot read EFI var "my-cool-efi-var": unexpected size: 1`)
}

func (s *efiVarsSuite) TestReadVarBytes(c *C) {
	// mock the efi var file
	varPath := filepath.Join(s.rootdir, "/sys/firmware/efi/efivars", "my-cool-efi-var")
	err := os.WriteFile(varPath, []byte("\x06\x00\x00\x00\x01"), 0644)
	c.Assert(err, IsNil)

	data, attr, err := efi.ReadVarBytes("my-cool-efi-var")
	c.Assert(err, IsNil)
	c.Check(attr, Equals, efi.VariableBootServiceAccess|efi.VariableRuntimeAccess)
	c.Assert(string(data), Equals, "\x01")
}

func (s *efiVarsSuite) TestReadVarString(c *C) {
	// mock the efi var file
	varPath := filepath.Join(s.rootdir, "/sys/firmware/efi/efivars", "my-cool-efi-var")
	err := os.WriteFile(varPath, []byte("\x06\x00\x00\x00A\x009\x00F\x005\x00C\x009\x004\x009\x00-\x00A\x00B\x008\x009\x00-\x005\x00B\x004\x007\x00-\x00A\x007\x00B\x00F\x00-\x005\x006\x00D\x00D\x002\x008\x00F\x009\x006\x00E\x006\x005\x00\x00\x00"), 0644)
	c.Assert(err, IsNil)

	data, attr, err := efi.ReadVarString("my-cool-efi-var")
	c.Assert(err, IsNil)
	c.Check(attr, Equals, efi.VariableBootServiceAccess|efi.VariableRuntimeAccess)
	c.Assert(data, Equals, "A9F5C949-AB89-5B47-A7BF-56DD28F96E65")
}

func (s *efiVarsSuite) TestEmpty(c *C) {
	// mock the efi var file
	varPath := filepath.Join(s.rootdir, "/sys/firmware/efi/efivars", "my-cool-efi-var")
	err := os.WriteFile(varPath, []byte("\x06\x00\x00\x00"), 0644)
	c.Assert(err, IsNil)

	b, _, err := efi.ReadVarBytes("my-cool-efi-var")
	c.Assert(err, IsNil)
	c.Check(b, HasLen, 0)

	v, _, err := efi.ReadVarString("my-cool-efi-var")
	c.Assert(err, IsNil)
	c.Check(v, HasLen, 0)
}

func (s *efiVarsSuite) TestMockVars(c *C) {
	restore := efi.MockVars(map[string][]byte{
		"a": []byte("\x01"),
		"b": []byte("\x02"),
	}, map[string]efi.VariableAttr{
		"b": efi.VariableNonVolatile | efi.VariableRuntimeAccess | efi.VariableBootServiceAccess,
	})
	defer restore()

	b, attr, err := efi.ReadVarBytes("a")
	c.Assert(err, IsNil)
	c.Check(attr, Equals, efi.VariableBootServiceAccess|efi.VariableRuntimeAccess)
	c.Assert(string(b), Equals, "\x01")

	b, attr, err = efi.ReadVarBytes("b")
	c.Assert(err, IsNil)
	c.Check(attr, Equals, efi.VariableBootServiceAccess|efi.VariableRuntimeAccess|efi.VariableNonVolatile)
	c.Assert(string(b), Equals, "\x02")

}

func (s *efiVarsSuite) TestMockStringVars(c *C) {
	restore := efi.MockVars(map[string][]byte{
		"a": bootloadertest.UTF16Bytes("foo-bar-baz"),
	}, nil)
	defer restore()

	v, attr, err := efi.ReadVarString("a")
	c.Assert(err, IsNil)
	c.Check(attr, Equals, efi.VariableBootServiceAccess|efi.VariableRuntimeAccess)
	c.Assert(v, Equals, "foo-bar-baz")
}

func (s *efiVarsSuite) TestMockVarsNoEFISystem(c *C) {
	restore := efi.MockVars(nil, nil)
	defer restore()

	_, _, err := efi.ReadVarBytes("a")
	c.Check(err, Equals, efi.ErrNoEFISystem)
}

func (s *efiVarsSuite) TestStringOddSize(c *C) {
	restore := efi.MockVars(map[string][]byte{
		"a": []byte("\x0a"),
	}, nil)
	defer restore()

	_, _, err := efi.ReadVarString("a")
	c.Check(err, ErrorMatches, `EFI var "a" is not a valid UTF16 string, it has an extra byte`)
}