| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 4 | package efivarfs |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "encoding/hex" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/google/go-cmp/cmp" |
| 12 | "github.com/google/uuid" |
| 13 | ) |
| 14 | |
| 15 | // Generated with old working marshaler and manually double-checked |
| 16 | var ref, _ = hex.DecodeString( |
| 17 | "010000004a004500780061006d0070006c006500000004012a00010000000" + |
| 18 | "500000000000000080000000000000014b8a76bad9dd11180b400c04fd430" + |
| 19 | "c8020204041c005c0074006500730074005c0061002e00650066006900000" + |
| 20 | "07fff0400", |
| 21 | ) |
| 22 | |
| 23 | func TestEncoding(t *testing.T) { |
| 24 | opt := LoadOption{ |
| 25 | Description: "Example", |
| 26 | FilePath: DevicePath{ |
| 27 | &HardDrivePath{ |
| 28 | PartitionNumber: 1, |
| 29 | PartitionStartBlock: 5, |
| 30 | PartitionSizeBlocks: 8, |
| 31 | PartitionMatch: PartitionGPT{ |
| 32 | PartitionUUID: uuid.NameSpaceX500, |
| 33 | }, |
| 34 | }, |
| 35 | FilePath("/test/a.efi"), |
| 36 | }, |
| 37 | } |
| 38 | got, err := opt.Marshal() |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if !bytes.Equal(ref, got) { |
| 43 | t.Fatalf("expected %x, got %x", ref, got) |
| 44 | } |
| 45 | got2, err := UnmarshalLoadOption(got) |
| 46 | if err != nil { |
| 47 | t.Fatalf("failed to unmarshal marshaled LoadOption: %v", err) |
| 48 | } |
| 49 | diff := cmp.Diff(&opt, got2) |
| 50 | if diff != "" { |
| 51 | t.Errorf("marshal/unmarshal wasn't transparent: %v", diff) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func FuzzDecode(f *testing.F) { |
| 56 | f.Add(ref) |
| 57 | f.Fuzz(func(t *testing.T, a []byte) { |
| 58 | // Just try to see if it crashes |
| 59 | _, _ = UnmarshalLoadOption(a) |
| 60 | }) |
| 61 | } |