gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/uefivars/boot/efiDppMedia_test.go (about) 1 // Copyright 2020 the u-root 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 // SPDX-License-Identifier: BSD-3-Clause 6 // 7 8 package boot 9 10 import ( 11 "strings" 12 "testing" 13 "unicode/utf16" 14 ) 15 16 //func ParseDppMediaHdd(h EfiDevicePathProtocolHdr, b []byte) (*DppMediaHDD, error) 17 func TestParseDppMediaHdd(t *testing.T) { 18 in := []byte{ 19 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 20 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 21 0x20, 0x21, 0x22, 0x23, 0x02, 0x02, 22 } 23 hdr := EfiDevicePathProtocolHdr{ 24 ProtoType: 4, 25 ProtoSubType: 1, 26 Length: uint16(len(in) + 4), 27 } 28 p, err := ParseDppMediaHdd(hdr, in) 29 if err != nil { 30 t.Fatal(err) 31 } 32 want := "HD(50462976,GPT,17161514-1918-1b1a-1c1d-1e1f20212223,0xb0a090807060504,0x131211100f0e0d0c)" 33 got := p.String() 34 if want != got { 35 t.Errorf("\nwant %s\n got %s", want, got) 36 } 37 wantp := "HDD" 38 gotp := p.ProtoSubTypeStr() 39 if wantp != gotp { 40 t.Errorf("want %s got %s", wantp, gotp) 41 } 42 } 43 44 //func ParseDppMediaFilePath(h EfiDevicePathProtocolHdr, b []byte) (*DppMediaFilePath, error) 45 func TestParseDppMediaFilePath(t *testing.T) { 46 str := `blah\blah\blah.efi` 47 //convert to utf16 ([]uint16) 48 u16 := utf16.Encode([]rune(str)) 49 //...and then to []byte 50 var in []byte 51 for _, u := range u16 { 52 in = append(in, byte(u&0xff), byte(u>>8&0xff)) 53 } 54 hdr := EfiDevicePathProtocolHdr{ 55 ProtoType: 4, 56 ProtoSubType: 4, 57 Length: uint16(len(in) + 4), 58 } 59 p, err := ParseDppMediaFilePath(hdr, in) 60 if err != nil { 61 t.Fatal(err) 62 } 63 want := "File(" + strings.ReplaceAll(str, "\\", "/") + ")" 64 got := p.String() 65 if want != got { 66 t.Errorf("\nwant %s\n got %s", want, got) 67 } 68 wantp := "FilePath" 69 gotp := p.ProtoSubTypeStr() 70 if wantp != gotp { 71 t.Errorf("want %s got %s", wantp, gotp) 72 } 73 } 74 75 //func ParseDppMediaPIWGFV(h EfiDevicePathProtocolHdr, b []byte) (*DppMediaPIWGFV, error) 76 func TestParseDppMediaPIWGFV(t *testing.T) { 77 in := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} 78 hdr := EfiDevicePathProtocolHdr{ 79 ProtoType: 4, 80 ProtoSubType: 7, 81 Length: uint16(len(in) + 4), 82 } 83 p, err := ParseDppMediaPIWGFV(hdr, in) 84 if err != nil { 85 t.Fatal(err) 86 } 87 want := "Fv(03020100-0504-0706-0809-0a0b0c0d0e0f)" 88 got := p.String() 89 if want != got { 90 t.Errorf("\nwant %s\n got %s", want, got) 91 } 92 wantp := "PIWG Firmware Volume" 93 gotp := p.ProtoSubTypeStr() 94 if wantp != gotp { 95 t.Errorf("want %s got %s", wantp, gotp) 96 } 97 } 98 99 //func ParseDppMediaPIWGFF(h EfiDevicePathProtocolHdr, b []byte) (*DppMediaPIWGFF, error) 100 func TestParseDppMediaPIWGFF(t *testing.T) { 101 in := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} 102 hdr := EfiDevicePathProtocolHdr{ 103 ProtoType: 4, 104 ProtoSubType: 6, 105 Length: uint16(len(in) + 4), 106 } 107 p, err := ParseDppMediaPIWGFF(hdr, in) 108 if err != nil { 109 t.Fatal(err) 110 } 111 want := "FvFile(03020100-0504-0706-0809-0a0b0c0d0e0f)" 112 got := p.String() 113 if want != got { 114 t.Errorf("\nwant %s\n got %s", want, got) 115 } 116 wantp := "PIWG Firmware File" 117 gotp := p.ProtoSubTypeStr() 118 if wantp != gotp { 119 t.Errorf("want %s got %s", wantp, gotp) 120 } 121 }