File: filepath_internal_test.go

package info (click to toggle)
golang-github-canonical-go-efilib 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,836 kB
  • sloc: makefile: 3
file content (371 lines) | stat: -rw-r--r-- 13,451 bytes parent folder | download
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.

package linux

import (
	"errors"
	"os"
	"path/filepath"
	"syscall"

	efi "github.com/canonical/go-efilib"
	"golang.org/x/sys/unix"

	. "gopkg.in/check.v1"
)

type FilepathMockMixin struct{}

func (s *FilepathMockMixin) MockFilepathEvalSymlinks(m map[string]string) (restore func()) {
	orig := filepathEvalSymlinks
	filepathEvalSymlinks = func(path string) (string, error) {
		p, ok := m[path]
		switch {
		case ok && p == "":
			return "", &os.PathError{Op: "lstat", Path: filepath.Dir(path), Err: syscall.EEXIST}
		case ok:
			return p, nil
		default:
			return path, nil
		}
	}

	return func() {
		filepathEvalSymlinks = orig
	}
}

func (s *FilepathMockMixin) MockUnixStat(m map[string]unix.Stat_t) (restore func()) {
	orig := unixStat
	unixStat = func(path string, st *unix.Stat_t) error {
		if s, ok := m[path]; ok {
			*st = s
			return nil
		}
		return orig(path, st)
	}

	return func() {
		unixStat = orig
	}
}

type filepathSuite struct {
	FilepathMockMixin
	TarFileMixin
}

var _ = Suite(&filepathSuite{})

func (s *filepathSuite) TestScanBlockDeviceMounts(c *C) {
	restore := MockMountsPath("testdata/mounts-nvme")
	defer restore()

	mounts, err := scanBlockDeviceMounts()
	c.Check(err, IsNil)
	c.Check(mounts, DeepEquals, []*mountPoint{
		{dev: unix.Mkdev(253, 1), root: "/", mountDir: "/", mountSource: "/dev/mapper/vgubuntu-root"},
		{dev: unix.Mkdev(259, 2), root: "/", mountDir: "/boot", mountSource: "/dev/nvme0n1p2"},
		{dev: unix.Mkdev(259, 1), root: "/", mountDir: "/boot/efi", mountSource: "/dev/nvme0n1p1"},
		{dev: unix.Mkdev(7, 1), root: "/", mountDir: "/snap/core/11993", mountSource: "/dev/loop1"},
		{dev: unix.Mkdev(7, 2), root: "/", mountDir: "/snap/gnome-3-38-2004/87", mountSource: "/dev/loop2"},
		{dev: unix.Mkdev(7, 3), root: "/", mountDir: "/snap/snap-store/558", mountSource: "/dev/loop3"},
		{dev: unix.Mkdev(7, 4), root: "/", mountDir: "/snap/gtk-common-themes/1519", mountSource: "/dev/loop4"},
		{dev: unix.Mkdev(259, 1), root: "/EFI", mountDir: "/efi", mountSource: "/dev/nvme0n1p1"}})
}

func (s *filepathSuite) TestGetFileMountPoint(c *C) {
	restore := s.MockFilepathEvalSymlinks(map[string]string{})
	defer restore()
	restore = MockMountsPath("testdata/mounts-nvme")
	defer restore()
	restore = s.MockUnixStat(map[string]unix.Stat_t{
		"/boot/efi/EFI/ubuntu/shimx64.efi": unix.Stat_t{Dev: unix.Mkdev(259, 1)},
	})
	defer restore()

	mount, err := getFileMountPoint("/boot/efi/EFI/ubuntu/shimx64.efi")
	c.Check(err, IsNil)
	c.Check(mount, DeepEquals, &mountPoint{dev: unix.Mkdev(259, 1), root: "/", mountDir: "/boot/efi", mountSource: "/dev/nvme0n1p1"})
}

func (s *filepathSuite) TestGetFileMountPointBindMount(c *C) {
	restore := s.MockFilepathEvalSymlinks(map[string]string{})
	defer restore()
	restore = MockMountsPath("testdata/mounts-nvme")
	defer restore()
	restore = s.MockUnixStat(map[string]unix.Stat_t{
		"/efi/ubuntu/shimx64.efi": unix.Stat_t{Dev: unix.Mkdev(259, 1)},
	})
	defer restore()

	mount, err := getFileMountPoint("/efi/ubuntu/shimx64.efi")
	c.Check(err, IsNil)
	c.Check(mount, DeepEquals, &mountPoint{dev: unix.Mkdev(259, 1), root: "/EFI", mountDir: "/efi", mountSource: "/dev/nvme0n1p1"})
}

func (s *filepathSuite) TestNewFilePath(c *C) {
	restore := s.MockFilepathEvalSymlinks(map[string]string{})
	defer restore()
	restore = MockMountsPath("testdata/mounts-nvme")
	defer restore()
	restore = s.MockUnixStat(map[string]unix.Stat_t{
		"/boot/efi/EFI/ubuntu/shimx64.efi": unix.Stat_t{Dev: unix.Mkdev(259, 1)},
	})
	defer restore()

	sysfs := filepath.Join(s.UnpackTar(c, "testdata/sys.tar"), "sys")
	restore = MockSysfsPath(sysfs)
	defer restore()

	path, err := newFilePath("/boot/efi/EFI/ubuntu/shimx64.efi")
	c.Check(err, IsNil)
	c.Check(path, DeepEquals, &filePath{
		dev: dev{
			sysfsPath: filepath.Join(sysfs, "devices/pci0000:00/0000:00:1d.0/0000:3d:00.0/nvme/nvme0/nvme0n1"),
			devPath:   "/dev/nvme0n1",
			part:      1},
		path: "/EFI/ubuntu/shimx64.efi"})
}

func (s *filepathSuite) TestNewFilePathBindMount(c *C) {
	restore := s.MockFilepathEvalSymlinks(map[string]string{})
	defer restore()
	restore = MockMountsPath("testdata/mounts-nvme")
	defer restore()
	restore = s.MockUnixStat(map[string]unix.Stat_t{
		"/efi/ubuntu/shimx64.efi": unix.Stat_t{Dev: unix.Mkdev(259, 1)},
	})
	defer restore()

	sysfs := filepath.Join(s.UnpackTar(c, "testdata/sys.tar"), "sys")
	restore = MockSysfsPath(sysfs)
	defer restore()

	path, err := newFilePath("/efi/ubuntu/shimx64.efi")
	c.Check(err, IsNil)
	c.Check(path, DeepEquals, &filePath{
		dev: dev{
			sysfsPath: filepath.Join(sysfs, "devices/pci0000:00/0000:00:1d.0/0000:3d:00.0/nvme/nvme0/nvme0n1"),
			devPath:   "/dev/nvme0n1",
			part:      1},
		path: "/EFI/ubuntu/shimx64.efi"})
}

func (s *filepathSuite) TestNewFilePathSymlink(c *C) {
	restore := s.MockFilepathEvalSymlinks(map[string]string{"/foo/bar/shimx64.efi": "/efi/ubuntu/shimx64.efi"})
	defer restore()
	restore = MockMountsPath("testdata/mounts-nvme")
	defer restore()
	restore = s.MockUnixStat(map[string]unix.Stat_t{
		"/efi/ubuntu/shimx64.efi": unix.Stat_t{Dev: unix.Mkdev(259, 1)},
	})
	defer restore()

	sysfs := filepath.Join(s.UnpackTar(c, "testdata/sys.tar"), "sys")
	restore = MockSysfsPath(sysfs)
	defer restore()

	path, err := newFilePath("/foo/bar/shimx64.efi")
	c.Check(err, IsNil)
	c.Check(path, DeepEquals, &filePath{
		dev: dev{
			sysfsPath: filepath.Join(sysfs, "devices/pci0000:00/0000:00:1d.0/0000:3d:00.0/nvme/nvme0/nvme0n1"),
			devPath:   "/dev/nvme0n1",
			part:      1},
		path: "/EFI/ubuntu/shimx64.efi"})
}

func (s *filepathSuite) TestNewFilePathNotPartitioned(c *C) {
	restore := s.MockFilepathEvalSymlinks(map[string]string{})
	defer restore()
	restore = MockMountsPath("testdata/mounts-nvme")
	defer restore()
	restore = s.MockUnixStat(map[string]unix.Stat_t{
		"/snap/core/11993/bin/ls": unix.Stat_t{Dev: unix.Mkdev(7, 1)},
	})
	defer restore()

	sysfs := filepath.Join(s.UnpackTar(c, "testdata/sys.tar"), "sys")
	restore = MockSysfsPath(sysfs)
	defer restore()

	path, err := newFilePath("/snap/core/11993/bin/ls")
	c.Check(err, IsNil)
	c.Check(path, DeepEquals, &filePath{
		dev: dev{
			sysfsPath: filepath.Join(sysfs, "devices/virtual/block/loop1"),
			devPath:   "/dev/loop1",
			part:      0},
		path: "/bin/ls"})
}

func (s *filepathSuite) TestNewDevicePathBuilder(c *C) {
	sysfs := filepath.Join(s.UnpackTar(c, "testdata/sys.tar"), "sys")
	restore := MockSysfsPath(sysfs)
	defer restore()

	dev := &dev{sysfsPath: filepath.Join(sysfs, "devices/pci0000:00/0000:00:1d.0/0000:3d:00.0/nvme/nvme0/nvme0n1")}
	builder, err := newDevicePathBuilder(dev)
	c.Check(err, IsNil)
	c.Check(builder, DeepEquals, &devicePathBuilder{
		devicePathBuilderState: devicePathBuilderState{
			remaining: []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}})
}

func (s *filepathSuite) TestDevicePathBuilderStateSysfsComponentsRemaining(c *C) {
	state := &devicePathBuilderState{remaining: []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}
	c.Check(state.SysfsComponentsRemaining(), Equals, 6)

	state.remaining = []string{"0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}
	c.Check(state.SysfsComponentsRemaining(), Equals, 4)
}

func (s *filepathSuite) TestDevicePathBuilderStatePeekUnhandledSysfsComponents(c *C) {
	state := &devicePathBuilderState{remaining: []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}
	c.Check(state.PeekUnhandledSysfsComponents(1), Equals, "pci0000:00")
	c.Check(state.PeekUnhandledSysfsComponents(2), Equals, "pci0000:00/0000:00:1d.0")
	c.Check(state.PeekUnhandledSysfsComponents(-1), Equals, "pci0000:00/0000:00:1d.0/0000:3d:00.0/nvme/nvme0/nvme0n1")
}

func (s *filepathSuite) TestDevicePathBuilderStateSysfsPath(c *C) {
	state := &devicePathBuilderState{processed: []string{"pci0000:00"}}
	c.Check(state.SysfsPath(), Equals, "/sys/devices/pci0000:00")
	state.processed = append(state.processed, "0000:00:1d.0")
	c.Check(state.SysfsPath(), Equals, "/sys/devices/pci0000:00/0000:00:1d.0")
}

func (s *filepathSuite) TestDevicePathBuilderStateAdvanceSysfsPath(c *C) {
	state := &devicePathBuilderState{remaining: []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}

	state.AdvanceSysfsPath(1)
	c.Check(state.processed, DeepEquals, []string{"pci0000:00"})
	c.Check(state.remaining, DeepEquals, []string{"0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"})

	state.AdvanceSysfsPath(2)
	c.Check(state.processed, DeepEquals, []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0"})
	c.Check(state.remaining, DeepEquals, []string{"nvme", "nvme0", "nvme0n1"})
}

func (s *filepathSuite) TestDevicePathBuilderDone(c *C) {
	builder := &devicePathBuilder{
		devicePathBuilderState: devicePathBuilderState{remaining: []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}}
	c.Check(builder.done(), Equals, false)

	builder.remaining = []string{}
	c.Check(builder.done(), Equals, true)
}

func (s *filepathSuite) TestDevicePathBuilderProcessNextComponent(c *C) {
	builder := &devicePathBuilder{
		devicePathBuilderState: devicePathBuilderState{
			remaining: []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}}

	hid, _ := efi.NewEISAID("PNP", 0x0a03)

	var skipped int
	skipHandler := func(state *devicePathBuilderState) error {
		skipped += 1
		state.AdvanceSysfsPath(2)
		return errSkipDevicePathNodeHandler
	}
	realHandler := func(state *devicePathBuilderState) error {
		state.Interface = interfaceTypePCI
		state.Path = append(state.Path, &efi.ACPIDevicePathNode{HID: hid})
		state.AdvanceSysfsPath(1)
		return nil
	}
	restore := MockDevicePathNodeHandlers(map[interfaceType][]registeredDpHandler{
		interfaceTypeUnknown: []registeredDpHandler{
			{name: "skip1", fn: skipHandler},
			{name: "skip2", fn: skipHandler},
			{name: "acpi", fn: realHandler}},
		interfaceTypePCI: []registeredDpHandler{
			{name: "pci", fn: skipHandler}}})
	defer restore()

	c.Check(builder.ProcessNextComponent(), IsNil)
	c.Check(skipped, Equals, 2)
	c.Check(builder.Interface, Equals, interfaceTypePCI)
	c.Check(builder.Path, DeepEquals, efi.DevicePath{&efi.ACPIDevicePathNode{HID: hid}})
	c.Check(builder.remaining, DeepEquals, []string{"0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"})
	c.Check(builder.processed, DeepEquals, []string{"pci0000:00"})
}

func (s *filepathSuite) TestDevicePathBuilderProcessNextComponentUnhandled(c *C) {
	hid, _ := efi.NewEISAID("PNP", 0x0a03)

	builder := &devicePathBuilder{
		devicePathBuilderState: devicePathBuilderState{
			Interface: interfaceTypePCI,
			Path:      efi.DevicePath{&efi.ACPIDevicePathNode{HID: hid}},
			processed: []string{"pci0000:00"},
			remaining: []string{"0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}}

	var skipped int
	skipHandler := func(_ *devicePathBuilderState) error {
		skipped += 1
		return errSkipDevicePathNodeHandler
	}
	restore := MockDevicePathNodeHandlers(map[interfaceType][]registeredDpHandler{
		interfaceTypeUnknown: []registeredDpHandler{
			{name: "acpi-skip", fn: skipHandler}},
		interfaceTypePCI: []registeredDpHandler{
			{name: "skip1", fn: skipHandler},
			{name: "skip2", fn: skipHandler}}})
	defer restore()

	c.Check(func() { builder.ProcessNextComponent() }, PanicMatches, "all handlers skipped handling interface type 1")
	c.Check(skipped, Equals, 2)
}

func (s *filepathSuite) TestDevicePathBuilderProcessNextComponentUnhandledRoot(c *C) {
	builder := &devicePathBuilder{
		devicePathBuilderState: devicePathBuilderState{
			Interface: interfaceTypeUnknown,
			remaining: []string{"pci0000:00", "0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}}

	var skipped int
	skipHandler := func(state *devicePathBuilderState) error {
		skipped += 1
		state.Interface = interfaceTypePCI
		return errSkipDevicePathNodeHandler
	}
	restore := MockDevicePathNodeHandlers(map[interfaceType][]registeredDpHandler{
		interfaceTypeUnknown: []registeredDpHandler{
			{name: "acpi-skip", fn: skipHandler}},
		interfaceTypePCI: []registeredDpHandler{
			{name: "skip1", fn: skipHandler},
			{name: "skip2", fn: skipHandler}}})
	defer restore()

	c.Check(builder.ProcessNextComponent(), ErrorMatches, "unsupported device: unhandled root node")
	c.Check(skipped, Equals, 1)
	c.Check(builder.Interface, Equals, interfaceTypeUnknown)
}

func (s *filepathSuite) TestDevicePathBuilderProcessNextComponentError(c *C) {
	hid, _ := efi.NewEISAID("PNP", 0x0a03)

	builder := &devicePathBuilder{
		devicePathBuilderState: devicePathBuilderState{
			Interface: interfaceTypePCI,
			Path:      efi.DevicePath{&efi.ACPIDevicePathNode{HID: hid}},
			processed: []string{"pci0000:00"},
			remaining: []string{"0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"}}}

	handler := func(state *devicePathBuilderState) error {
		state.AdvanceSysfsPath(1)
		return errors.New("error")
	}
	restore := MockDevicePathNodeHandlers(map[interfaceType][]registeredDpHandler{
		interfaceTypePCI: []registeredDpHandler{
			{name: "pci", fn: handler}}})
	defer restore()

	c.Check(builder.ProcessNextComponent(), ErrorMatches, "\\[handler pci\\]: error")
	c.Check(builder.remaining, DeepEquals, []string{"0000:00:1d.0", "0000:3d:00.0", "nvme", "nvme0", "nvme0n1"})
	c.Check(builder.processed, DeepEquals, []string{"pci0000:00"})
}