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