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