github.com/linuxboot/fiano@v1.2.0/pkg/uefi/section_test.go (about) 1 // Copyright 2018 the LinuxBoot Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package uefi 6 7 import ( 8 "fmt" 9 "reflect" 10 "testing" 11 12 "github.com/linuxboot/fiano/pkg/guid" 13 ) 14 15 var ( 16 // Section examples 17 emptySec = make([]byte, 0) // Empty section 18 tinySec = []byte{4, 0, 0, byte(SectionTypeRaw)} // Section header with no data 19 wrongSizeSec = append([]byte{40, 0, 0, byte(SectionTypeRaw)}, make([]byte, 20)...) // Section with a size mismatch 20 largeSizeSec = append([]byte{10, 0, 0, byte(SectionTypeRaw)}, make([]byte, 20)...) // Section with a big buffer 21 smallSec = append([]byte{22, 0, 0, byte(SectionTypeRaw)}, make([]byte, 18)...) // 20 byte Section 22 linuxSec = []byte{0x10, 0x00, 0x00, 0x15, 0x4c, 0x00, 0x69, 0x00, 23 0x6e, 0x00, 0x75, 0x00, 0x78, 0x00, 0x00, 0x00} // Linux UI section 24 ) 25 26 func TestSetType(t *testing.T) { 27 var tests = []struct { 28 name string 29 t SectionType 30 typeString string 31 }{ 32 {"SetUIType", SectionTypeUserInterface, "EFI_SECTION_USER_INTERFACE"}, 33 {"SetBadType", 0x55, "UNKNOWN"}, 34 } 35 for _, test := range tests { 36 t.Run(test.name, func(t *testing.T) { 37 s := Section{} 38 s.SetType(test.t) 39 if s.Type != test.typeString { 40 t.Errorf("Section Type field mismatch, expected \"%v\", got \"%v\"", test.typeString, s.Type) 41 } 42 }) 43 } 44 } 45 46 func TestUISection(t *testing.T) { 47 var tests = []struct { 48 name string 49 buf []byte 50 fileOrder int 51 val string 52 }{ 53 {"UISection", linuxSec, 1, "Linux"}, 54 {"nonUISection", smallSec, 1, ""}, 55 } 56 for _, test := range tests { 57 t.Run(test.name, func(t *testing.T) { 58 s, err := NewSection(test.buf, test.fileOrder) 59 if err != nil { 60 t.Fatalf("Unable to parse section object %v, got %v", test.buf, err.Error()) 61 } 62 if s.Name != test.val { 63 t.Errorf("Section Name field mismatch, expected \"%v\", got \"%v\"", test.val, s.Name) 64 } 65 }) 66 } 67 } 68 69 func TestNewSection(t *testing.T) { 70 var tests = []struct { 71 name string 72 buf []byte 73 fileOrder int 74 msg string 75 }{ 76 {"emptySec", emptySec, 0, "EOF"}, 77 {"wrongSizeSec", wrongSizeSec, 0, 78 fmt.Sprintf("section size mismatch! Section has size %v, but buffer is %v bytes big", 79 40, len(wrongSizeSec))}, 80 {"largeSizeSec", largeSizeSec, 0, ""}, 81 {"tinySec", tinySec, 0, ""}, 82 {"smallSec", smallSec, 0, ""}, 83 {"linuxSec", linuxSec, 0, ""}, 84 } 85 for _, test := range tests { 86 t.Run(test.name, func(t *testing.T) { 87 _, err := NewSection(test.buf, test.fileOrder) 88 if err == nil && test.msg != "" { 89 t.Errorf("Error was not returned, expected %v", test.msg) 90 } else if err != nil && err.Error() != test.msg { 91 t.Errorf("Mismatched Error returned, expected \n%v\n got \n%v\n", test.msg, err.Error()) 92 } 93 }) 94 } 95 } 96 97 func TestParseDepEx(t *testing.T) { 98 var tests = []struct { 99 name string 100 in []byte 101 out []DepExOp 102 err string 103 }{ 104 { 105 name: "empty", 106 in: []byte{}, 107 err: "invalid DEPEX, no END", 108 }, 109 { 110 name: "end", 111 in: []byte{0x08}, 112 out: []DepExOp{{OpCode: "END"}}, 113 }, 114 { 115 name: "no end", 116 in: []byte{0x06}, 117 err: "invalid DEPEX, no END", 118 }, 119 { 120 name: "simple", 121 in: []byte{0x06, 0x08}, 122 out: []DepExOp{{OpCode: "TRUE"}, {OpCode: "END"}}, 123 }, 124 { 125 name: "spec example", 126 // Example from the Platform Initialization Specification, Vol. 2, Chapter 10. 127 in: []byte{ 128 0x02, // PUSH 129 0xF6, 0x3F, 0x5E, 0x66, 0xCC, 0x46, 0xD4, 0x11, 130 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D, 131 0x02, // PUSH 132 0xB1, 0xCC, 0xBA, 0x26, 0x42, 0x6F, 0xD4, 0x11, 133 0xBC, 0xE7, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81, 134 0x03, // AND 135 0x02, // PUSH 136 0xB2, 0xCC, 0xBA, 0x26, 0x42, 0x6F, 0xD4, 0x11, 137 0xBC, 0xE7, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81, 138 0x02, // PUSH 139 0x72, 0x70, 0xA9, 0x1D, 0xDC, 0xBD, 0x30, 0x4B, 140 0x99, 0xF1, 0x72, 0xA0, 0xB5, 0x6F, 0xFF, 0x2A, 141 0x03, // AND 142 0x03, // AND 143 0x02, // PUSH 144 0x87, 0xAC, 0xCF, 0x27, 0xCC, 0x46, 0xD4, 0x11, 145 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D, 146 0x02, // PUSH 147 0x88, 0xAC, 0xCF, 0x27, 0xCC, 0x46, 0xD4, 0x11, 148 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D, 149 0x03, // AND 150 0x02, // PUSH 151 0x53, 0x82, 0xD0, 0x96, 0x83, 0x84, 0xD4, 0x11, 152 0xBC, 0xF1, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81, 153 0x02, // PUSH 154 0xE3, 0x23, 0x64, 0xA4, 0x17, 0x46, 0xF1, 0x49, 155 0xB9, 0xFF, 0xD1, 0xBF, 0xA9, 0x11, 0x58, 0x39, 156 0x02, // PUSH 157 0xB3, 0xCC, 0xBA, 0x26, 0x42, 0x6F, 0xD4, 0x11, 158 0xBC, 0xE7, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81, 159 0x03, // AND 160 0x02, // PUSH 161 0xE2, 0x68, 0x56, 0x1E, 0x81, 0x84, 0xD4, 0x11, 162 0xBC, 0xF1, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81, 163 0x02, // PUSH 164 0x18, 0xF8, 0x41, 0x64, 0x62, 0x63, 0x44, 0x4E, 165 0xB5, 0x70, 0x7D, 0xBA, 0x31, 0xDD, 0x24, 0x53, 166 0x03, // AND 167 0x03, // AND 168 0x03, // AND 169 0x02, // PUSH 170 0xF5, 0x3F, 0x5E, 0x66, 0xCC, 0x46, 0xD4, 0x11, 171 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D, 172 0x03, // AND 173 0x08, // END 174 }, 175 out: []DepExOp{ 176 {OpCode: "PUSH", GUID: guid.MustParse("665E3FF6-46CC-11D4-9A38-0090273FC14D")}, 177 {OpCode: "PUSH", GUID: guid.MustParse("26BACCB1-6F42-11D4-BCE7-0080C73C8881")}, 178 {OpCode: "AND"}, 179 {OpCode: "PUSH", GUID: guid.MustParse("26BACCB2-6F42-11D4-BCE7-0080C73C8881")}, 180 {OpCode: "PUSH", GUID: guid.MustParse("1DA97072-BDDC-4B30-99F1-72A0B56FFF2A")}, 181 {OpCode: "AND"}, 182 {OpCode: "AND"}, 183 {OpCode: "PUSH", GUID: guid.MustParse("27CFAC87-46CC-11D4-9A38-0090273FC14D")}, 184 {OpCode: "PUSH", GUID: guid.MustParse("27CFAC88-46CC-11D4-9A38-0090273FC14D")}, 185 {OpCode: "AND"}, 186 {OpCode: "PUSH", GUID: guid.MustParse("96D08253-8483-11D4-BCF1-0080C73C8881")}, 187 {OpCode: "PUSH", GUID: guid.MustParse("A46423E3-4617-49F1-B9FF-D1BFA9115839")}, 188 {OpCode: "PUSH", GUID: guid.MustParse("26BACCB3-6F42-11D4-BCE7-0080C73C8881")}, 189 {OpCode: "AND"}, 190 {OpCode: "PUSH", GUID: guid.MustParse("1E5668E2-8481-11D4-BCF1-0080C73C8881")}, 191 {OpCode: "PUSH", GUID: guid.MustParse("6441F818-6362-4E44-B570-7DBA31DD2453")}, 192 {OpCode: "AND"}, 193 {OpCode: "AND"}, 194 {OpCode: "AND"}, 195 {OpCode: "PUSH", GUID: guid.MustParse("665E3FF5-46CC-11D4-9A38-0090273FC14D")}, 196 {OpCode: "AND"}, 197 {OpCode: "END"}}, 198 }, 199 } 200 for _, tt := range tests { 201 t.Run(tt.name, func(t *testing.T) { 202 depEx, err := parseDepEx(tt.in) 203 if tt.err == "" { 204 // Expects no error. 205 if err != nil { 206 t.Fatalf("unexpected error, %v", err) 207 } 208 if !reflect.DeepEqual(depEx, tt.out) { 209 t.Fatalf("expected %s, got %s", tt.out, depEx) 210 } 211 } else { 212 // Expects error. 213 if err == nil { 214 t.Fatal("expected error") 215 } 216 if tt.err != err.Error() { 217 t.Fatalf("expected error %q, got %q", tt.err, err) 218 } 219 } 220 }) 221 } 222 }