Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 1 | package efivarfs |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/google/uuid" |
| 8 | ) |
| 9 | |
| 10 | func 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 | } |
Lorenz Brun | f025d1b | 2023-08-07 14:52:49 +0200 | [diff] [blame] | 84 | _, rest, err := UnmarshalDevicePath(got) |
| 85 | if err != nil { |
Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 86 | t.Errorf("failed to unmarshal value again: %v", err) |
| 87 | } |
Lorenz Brun | f025d1b | 2023-08-07 14:52:49 +0200 | [diff] [blame] | 88 | if len(rest) != 0 { |
| 89 | t.Errorf("rest is non-zero after single valid device path: %x", rest) |
| 90 | } |
Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 91 | }) |
| 92 | } |
| 93 | } |