gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/boot/multiboot/mutiboot_info_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 package multiboot 6 7 import ( 8 "testing" 9 10 "github.com/google/go-cmp/cmp" 11 ) 12 13 func TestInfoMarshal(t *testing.T) { 14 for _, tt := range []struct { 15 name string 16 mi *mutibootInfo 17 want []byte 18 }{ 19 { 20 name: "no elements", 21 mi: &mutibootInfo{ 22 cmdline: 0xdeadbeef, 23 elems: nil, 24 }, 25 want: []byte{ 26 // cmdline 27 0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 28 // 0 elements 29 0, 0, 0, 0, 0, 0, 0, 0, 30 }, 31 }, 32 { 33 name: "one memrange element", 34 mi: &mutibootInfo{ 35 cmdline: 0xdeadbeef, 36 elems: []elem{ 37 &mutibootMemRange{ 38 startAddr: 0xbeefdead, 39 length: 0xdeadbeef, 40 memType: 2, 41 }, 42 }, 43 }, 44 want: []byte{ 45 // cmdline 46 0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 47 // 1 element 48 0x1, 0, 0, 0, 0, 0, 0, 0, 49 50 // TLV -- type, length, value 51 52 // type 53 byte(MUTIBOOT_MEMRANGE_TYPE), 0, 0, 0, 54 // length - 20 bytes + 8 for the length + 4 for the type 55 32, 0, 0, 0, 0, 0, 0, 0, 56 // values 57 0xad, 0xde, 0xef, 0xbe, 0, 0, 0, 0, 58 0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 59 2, 0, 0, 0, 60 }, 61 }, 62 { 63 name: "one module element", 64 mi: &mutibootInfo{ 65 cmdline: 0xdeadbeef, 66 elems: []elem{ 67 &mutibootModule{ 68 cmdline: 0xbeefdead, 69 moduleSize: 0x1000, 70 ranges: []mutibootModuleRange{ 71 { 72 startPageNum: 0x100, 73 numPages: 1, 74 }, 75 }, 76 }, 77 }, 78 }, 79 want: []byte{ 80 // cmdline 81 0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 82 // 1 element 83 0x1, 0, 0, 0, 0, 0, 0, 0, 84 85 // TLV -- type, length, value 86 87 // type 88 byte(MUTIBOOT_MODULE_TYPE), 0, 0, 0, 89 // length - 36 bytes + 8 for the length + 4 for the type 90 48, 0, 0, 0, 0, 0, 0, 0, 91 // values 92 // cmdline 93 0xad, 0xde, 0xef, 0xbe, 0, 0, 0, 0, 94 // moduleSize 95 0x00, 0x10, 0, 0, 0, 0, 0, 0, 96 // numRanges 97 1, 0, 0, 0, 98 // range - startPageNum 99 0x00, 0x01, 0, 0, 0, 0, 0, 0, 100 // range - numPages 101 1, 0, 0, 0, 102 // padding 103 0, 0, 0, 0, 104 }, 105 }, 106 { 107 name: "one zero-length module element", 108 mi: &mutibootInfo{ 109 cmdline: 0xdeadbeef, 110 elems: []elem{ 111 &mutibootModule{ 112 cmdline: 0xbeefdead, 113 moduleSize: 0, 114 }, 115 }, 116 }, 117 want: []byte{ 118 // cmdline 119 0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 120 // 1 element 121 0x1, 0, 0, 0, 0, 0, 0, 0, 122 123 // TLV -- type, length, value 124 125 // type 126 byte(MUTIBOOT_MODULE_TYPE), 0, 0, 0, 127 // length - 20 bytes + 8 for the length + 4 for the type 128 32, 0, 0, 0, 0, 0, 0, 0, 129 // values 130 // cmdline 131 0xad, 0xde, 0xef, 0xbe, 0, 0, 0, 0, 132 // moduleSize 133 0, 0, 0, 0, 0, 0, 0, 0, 134 // numRanges 135 0, 0, 0, 0, 136 }, 137 }, 138 } { 139 t.Run(tt.name, func(t *testing.T) { 140 got := tt.mi.marshal() 141 if !cmp.Equal(got, tt.want) { 142 t.Errorf("marshaled bytes not the same. diff (-want, +got):\n%s", cmp.Diff(tt.want, got)) 143 } 144 }) 145 } 146 }