github.com/linuxboot/fiano@v1.2.0/pkg/uefi/uefi_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 "encoding/json" 9 "fmt" 10 "testing" 11 ) 12 13 func TestSetErasePolarity(t *testing.T) { 14 type epTest struct { 15 ep byte 16 errStr string 17 } 18 19 var err error 20 var tests = []struct { 21 name string 22 rounds []epTest 23 }{ 24 {name: "badPolarity", rounds: []epTest{ 25 { 26 ep: poisonedPolarity, 27 errStr: fmt.Sprintf("invalid erase polarity requested, should only be 0x00 or 0xFF, got 0x%2X", 28 poisonedPolarity)}, 29 }}, 30 {name: "good0xFF", rounds: []epTest{ 31 { 32 ep: 0xFF, 33 errStr: ""}, 34 }}, 35 {name: "good0x00", rounds: []epTest{ 36 { 37 ep: 0x00, 38 errStr: ""}, 39 }}, 40 {name: "good0xFFSetTwice", rounds: []epTest{ 41 { 42 ep: 0xFF, 43 errStr: ""}, 44 { 45 ep: 0xFF, 46 errStr: ""}, 47 }}, 48 {name: "MismatchedPolarity", rounds: []epTest{ 49 { 50 ep: 0xFF, 51 errStr: ""}, 52 { 53 ep: 0x00, 54 errStr: "conflicting erase polarities, was 0xFF, requested 0x00"}, 55 }}, 56 } 57 for _, test := range tests { 58 t.Run(test.name, func(t *testing.T) { 59 // Reset ErasePolarity 60 Attributes.ErasePolarity = 0xF0 61 for _, r := range test.rounds { 62 err = SetErasePolarity(r.ep) 63 if err != nil { 64 if errStr := err.Error(); errStr != r.errStr { 65 t.Errorf("error mismatch, expected \"%v\", got \"%v\"", 66 r.errStr, errStr) 67 } 68 } else if r.errStr != "" { 69 t.Errorf("Expected Error %v, got nil", r.errStr) 70 } 71 } 72 }) 73 } 74 } 75 76 func TestUnmarshalTypedFirmware(t *testing.T) { 77 inFirmware := MakeTyped(&Section{Name: "CHARLIE"}) 78 79 j, err := json.Marshal(inFirmware) 80 if err != nil { 81 t.Fatal(err) 82 } 83 t.Log(string(j)) 84 85 var outFirmware TypedFirmware 86 if err := json.Unmarshal(j, &outFirmware); err != nil { 87 t.Fatal(err) 88 } 89 90 if outFirmware.Type != "*uefi.Section" { 91 t.Errorf("got %q, expected *uefi.Section", outFirmware.Type) 92 } 93 outSection, ok := outFirmware.Value.(*Section) 94 if !ok { 95 t.Fatalf("got %T; expected *uefi.Section", outFirmware.Value) 96 } 97 if outSection.Name != "CHARLIE" { 98 t.Errorf("got %q, expected CHARLIE", outSection.Name) 99 } 100 } 101 102 var ( 103 // Checksum Tests 104 emptyBuf = []byte{} 105 sampleBuf = []byte{1, 2, 3, 4} 106 overBuf = []byte{0x1, 0x2, 0xFF, 0xFF} 107 zeroBuf = []byte{0, 0, 0, 0} 108 threeBuf = []byte{3, 3, 3} 109 ) 110 111 func TestChecksum8(t *testing.T) { 112 var tests = []struct { 113 name string 114 buf []byte 115 res uint8 116 }{ 117 {"emptyBuf", emptyBuf, 0}, 118 {"sampleBuf", sampleBuf, 10}, 119 {"overBuf", overBuf, 0x1}, 120 {"zeroBuf", zeroBuf, 0}, 121 {"threeBuf", threeBuf, 9}, 122 } 123 for _, test := range tests { 124 t.Run(test.name, func(t *testing.T) { 125 if res := Checksum8(test.buf); res != test.res { 126 t.Errorf("Checksum8 wrong result!, input was %#x, wanted \n%#x\n, got \n%#x\n", test.buf, test.res, res) 127 } 128 }) 129 } 130 } 131 132 func TestChecksum16(t *testing.T) { 133 var tests = []struct { 134 name string 135 buf []byte 136 res uint16 137 msg string 138 }{ 139 {"emptyBuf", emptyBuf, 0, ""}, 140 {"sampleBuf", sampleBuf, 0x604, ""}, 141 {"overBuf", overBuf, 0x200, ""}, 142 {"zeroBuf", zeroBuf, 0, ""}, 143 {"threeBuf", threeBuf, 0, fmt.Sprintf("byte slice does not have even length, not able to do 16 bit checksum. Length was %v", 144 len(threeBuf))}, 145 } 146 for _, test := range tests { 147 t.Run(test.name, func(t *testing.T) { 148 res, err := Checksum16(test.buf) 149 if res != test.res { 150 t.Errorf("Checksum16 wrong result!, input was %#x, wanted \n%#x\n, got \n%#x\n", test.buf, test.res, res) 151 } 152 if err == nil && test.msg != "" { 153 t.Errorf("Error was not returned, expected %v", test.msg) 154 } else if err != nil && err.Error() != test.msg { 155 t.Errorf("Mismatched Error returned, expected \n%v\n got \n%v\n", test.msg, err.Error()) 156 } 157 }) 158 } 159 } 160 161 func TestWrite3Size(t *testing.T) { 162 var tests = []struct { 163 name string 164 val uint64 165 res [3]byte 166 }{ 167 {"emptySize", 0x0, [3]byte{0, 0, 0}}, 168 {"sampleSize", 0xABCDEF, [3]byte{0xEF, 0xCD, 0xAB}}, 169 {"max3ByteSize", 0xFFFFFF, [3]byte{0xFF, 0xFF, 0xFF}}, 170 {"over3ByteSize", 0x1000000, [3]byte{0xFF, 0xFF, 0xFF}}, 171 } 172 for _, test := range tests { 173 t.Run(test.name, func(t *testing.T) { 174 if res := Write3Size(test.val); res != test.res { 175 t.Errorf("Write3Size wrong result!, input was %#x, wanted \n%#x\n, got \n%#x\n", test.val, test.res, res) 176 } 177 }) 178 } 179 } 180 181 func TestRead3Size(t *testing.T) { 182 var tests = []struct { 183 name string 184 val uint64 185 arr [3]byte 186 }{ 187 {"emptySize", 0x0, [3]byte{0, 0, 0}}, 188 {"sampleSize", 0xABCDEF, [3]byte{0xEF, 0xCD, 0xAB}}, 189 {"max3ByteSize", 0xFFFFFF, [3]byte{0xFF, 0xFF, 0xFF}}, 190 } 191 for _, test := range tests { 192 t.Run(test.name, func(t *testing.T) { 193 if val := Read3Size(test.arr); val != test.val { 194 t.Errorf("Read3Size wrong result!, input was %#x, wanted \n%#x\n, got \n%#x\n", test.arr, test.val, val) 195 } 196 }) 197 } 198 } 199 200 func TestAlign4(t *testing.T) { 201 var tests = []struct { 202 val uint64 203 res uint64 204 }{ 205 {0x4, 0x4}, 206 {0x5, 0x8}, 207 } 208 for _, test := range tests { 209 if res := Align4(test.val); res != test.res { 210 t.Errorf("Align4 wrong result!, input was %#x, wanted \n%#x\n, got \n%#x\n", test.val, test.res, res) 211 } 212 } 213 } 214 215 func TestAlign8(t *testing.T) { 216 var tests = []struct { 217 val uint64 218 res uint64 219 }{ 220 {0x4, 0x8}, 221 {0x5, 0x8}, 222 {0x8, 0x8}, 223 {0x9, 0x10}, 224 } 225 for _, test := range tests { 226 if res := Align8(test.val); res != test.res { 227 t.Errorf("Align8 wrong result!, input was %#x, wanted \n%#x\n, got \n%#x\n", test.val, test.res, res) 228 } 229 } 230 }