blob: 741a1e665bb6d14cb8c024f86b6220cd962400a4 [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 "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
16var ref, _ = hex.DecodeString(
17 "010000004a004500780061006d0070006c006500000004012a00010000000" +
18 "500000000000000080000000000000014b8a76bad9dd11180b400c04fd430" +
19 "c8020204041c005c0074006500730074005c0061002e00650066006900000" +
20 "07fff0400",
21)
22
23func 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
55func 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}