blob: 1e4d3bda392edb23a57c1c55f6081806619a3f84 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brunca1cff02023-06-26 17:52:44 +02004package efivarfs
5
6import (
7 "bytes"
8 "testing"
9
10 "github.com/google/uuid"
11)
12
13func TestMarshalExamples(t *testing.T) {
14 cases := []struct {
15 name string
16 path DevicePath
17 expected []byte
18 expectError bool
19 }{
20 {
21 name: "TestNone",
22 path: DevicePath{},
23 expected: []byte{
24 0x7f, 0xff, // End of HW device path
25 0x04, 0x00, // Length: 4 bytes
26 },
27 },
28 {
29 // From UEFI Device Path Examples, extracted single entry
30 name: "TestHD",
31 path: DevicePath{
32 &HardDrivePath{
33 PartitionNumber: 1,
34 PartitionStartBlock: 0x22,
35 PartitionSizeBlocks: 0x2710000,
36 PartitionMatch: PartitionGPT{
37 PartitionUUID: uuid.MustParse("15E39A00-1DD2-1000-8D7F-00A0C92408FC"),
38 },
39 },
40 },
41 expected: []byte{
42 0x04, 0x01, // Hard Disk type
43 0x2a, 0x00, // Length
44 0x01, 0x00, 0x00, 0x00, // Partition Number
45 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Part Start
46 0x00, 0x00, 0x71, 0x02, 0x00, 0x00, 0x00, 0x00, // Part Size
47 0x00, 0x9a, 0xe3, 0x15, 0xd2, 0x1d, 0x00, 0x10,
48 0x8d, 0x7f, 0x00, 0xa0, 0xc9, 0x24, 0x08, 0xfc, // Signature
49 0x02, // Part Format GPT
50 0x02, // Signature GPT
51 0x7f, 0xff, // End of HW device path
52 0x04, 0x00, // Length: 4 bytes
53 },
54 },
55 {
56 name: "TestFilePath",
57 path: DevicePath{
58 FilePath("asdf"),
59 },
60 expected: []byte{
61 0x04, 0x04, // File Path type
62 0x0e, 0x00, // Length
63 'a', 0x00, 's', 0x00, 'd', 0x00, 'f', 0x00,
64 0x00, 0x00,
65 0x7f, 0xff, // End of HW device path
66 0x04, 0x00, // Length: 4 bytes
67 },
68 },
69 }
70
71 for _, c := range cases {
72 t.Run(c.name, func(t *testing.T) {
73 got, err := c.path.Marshal()
74 if err != nil && !c.expectError {
75 t.Fatalf("unexpected error: %v", err)
76 }
77 if err == nil && c.expectError {
78 t.Fatalf("expected error, got %x", got)
79 }
80 if err != nil && c.expectError {
81 // Do not compare result in case error is expected
82 return
83 }
84 if !bytes.Equal(got, c.expected) {
85 t.Fatalf("expected %x, got %x", c.expected, got)
86 }
Lorenz Brunf025d1b2023-08-07 14:52:49 +020087 _, rest, err := UnmarshalDevicePath(got)
88 if err != nil {
Lorenz Brunca1cff02023-06-26 17:52:44 +020089 t.Errorf("failed to unmarshal value again: %v", err)
90 }
Lorenz Brunf025d1b2023-08-07 14:52:49 +020091 if len(rest) != 0 {
92 t.Errorf("rest is non-zero after single valid device path: %x", rest)
93 }
Lorenz Brunca1cff02023-06-26 17:52:44 +020094 })
95 }
96}