gopkg.in/hugelgupf/u-root.v2@v2.0.0-20180831055005-3f8fdb0ce09d/cmds/bzimage/bzimage_test.go (about) 1 // Copyright 2017-2018 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 main 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "syscall" 12 "testing" 13 14 "github.com/u-root/u-root/pkg/bzimage" 15 "github.com/u-root/u-root/pkg/testutil" 16 ) 17 18 var ( 19 // Test BzImage we are not using yet. 20 b = bzimage.BzImage{ 21 BootCode: []byte{1, 2, 3, 4}, 22 Kernel: []byte{5, 6, 7, 8}, 23 InitRAMFS: []byte{9, 10}, 24 KernelBase: 0x100000, 25 KernelOffset: 620, 26 Header: bzimage.LinuxHeader{ 27 MBRCode: [192]byte{ 28 0xea, 0x05, 0x00, 0xc0, 0x07, 0x8c, 0xc8, 0x8e, 29 0xd8, 0x8e, 0xc0, 0x8e, 0xd0, 0x31, 0xe4, 0xfb, 30 0xfc, 0xbe, 0x2d, 0x00, 0xac, 0x20, 0xc0, 0x74, 31 0x09, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 32 0xeb, 0xf2, 0x31, 0xc0, 0xcd, 0x16, 0xcd, 0x19, 33 0xea, 0xf0, 0xff, 0x00, 0xf0, 0x55, 0x73, 0x65, 34 0x20, 0x61, 0x20, 0x62, 0x6f, 0x6f, 0x74, 0x20, 35 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x0d, 36 0x0a, 0x0a, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 37 0x20, 0x64, 0x69, 0x73, 0x6b, 0x20, 0x61, 0x6e, 38 0x64, 0x20, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 39 0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, 40 0x74, 0x6f, 0x20, 0x72, 0x65, 0x62, 0x6f, 0x6f, 41 0x74, 0x2e, 0x2e, 0x2e, 0x0d, 0x0a, 0x00, 0x00, 42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 52 ExtRamdiskImage: 00, 53 ExtRamdiskSize: 00, 54 ExtCmdlinePtr: 00, 55 SetupSects: 0x1e, 56 RootFlags: 0x01, 57 Syssize: 0xb51d, 58 RamSize: 0x00, 59 Vidmode: 0xffff, 60 RootDev: 0x00, 61 Bootsectormagic: 0xaa55, 62 Jump: 0x66eb, 63 HeaderMagic: [4]byte{0x48, 0x64, 0x72, 0x53}, 64 Protocolversion: 0x20d, 65 RealModeSwitch: 0x00, 66 StartSys: 0x1000, 67 Kveraddr: 0x3140, 68 TypeOfLoader: 0x00, 69 Loadflags: 0x01, 70 Setupmovesize: 0x8000, 71 Code32Start: 0x100000, 72 RamDiskImage: 0x00, 73 RamDiskSize: 0x00, 74 BootSectKludge: [4]uint8{}, 75 Heapendptr: 0x5320, 76 ExtLoaderVer: 0x00, 77 ExtLoaderType: 0x00, 78 Cmdlineptr: 0x00, 79 InitrdAddrMax: 0x7fffffff, 80 Kernelalignment: 0x200000, 81 RelocatableKernel: 0x00, 82 MinAlignment: 0x15, 83 XLoadFlags: 0x01, 84 CmdLineSize: 0x7ff, 85 HardwareSubArch: 0x00, 86 HardwareSubArchData: 0x00, 87 PayloadOffset: 0x255, 88 PayloadSize: 0x9532c, 89 SetupData: 0x00, 90 PrefAddress: 0x1000000, 91 InitSize: 0x6e0000, 92 HandoverOffset: 0x00, 93 }, 94 } 95 uskip = len("2018/08/10 21:20:42 ") 96 ) 97 98 func TestSimple(t *testing.T) { 99 tmpDir, err := ioutil.TempDir("", "bzImage") 100 if err != nil { 101 t.Fatal(err) 102 } 103 defer os.RemoveAll(tmpDir) 104 105 var tests = []struct { 106 args []string 107 name string 108 status int 109 out string 110 skip int 111 }{ 112 { 113 args: []string{"initramfs", "bzImage", "init.cpio", "zz/zz/zz"}, 114 name: "Bad output file", 115 status: 1, 116 out: "open zz/zz/zz: no such file or directory\n", 117 skip: uskip, 118 }, 119 { 120 args: []string{"initramfs", "bzImage", "init.cpio", filepath.Join(tmpDir, "zz")}, 121 name: "correct initramfs test", 122 status: 0, 123 out: "", 124 }, 125 { 126 args: []string{}, 127 name: "no args", 128 status: 1, 129 out: cmdUsage + "\n", 130 skip: uskip, 131 }, 132 { 133 args: []string{"dump", "bzImage"}, 134 name: "dump", 135 status: 0, 136 out: "MBRCode:0xea0500c0078cc88ed88ec08ed031e4fbfcbe2d00ac20c07409b40ebb0700cd10ebf231c0cd16cd19eaf0ff00f0557365206120626f6f74206c6f616465722e0d0a0a52656d6f7665206469736b20616e6420707265737320616e79206b657920746f207265626f6f742e2e2e0d0a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\nExtRamdiskImage:0x00\nExtRamdiskSize:0x00\nExtCmdlinePtr:0x00\n_:0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\nSetupSects:0x1e\nRootFlags:0x01\nSyssize:0xb51d\nRamSize:0x00\nVidmode:0xffff\nRootDev:0x00\nBootsectormagic:0xaa55\nJump:0x66eb\nHeaderMagic:0x48647253\nProtocolversion:0x20d\nRealModeSwitch:0x00\nStartSys:0x1000\nKveraddr:0x3140\nTypeOfLoader:0x00\nLoadflags:0x01\nSetupmovesize:0x8000\nCode32Start:0x100000\nRamDiskImage:0x00\nRamDiskSize:0x00\nBootSectKludge:0x00000000\nHeapendptr:0x5320\nExtLoaderVer:0x00\nExtLoaderType:0x00\nCmdlineptr:0x00\nInitrdAddrMax:0x7fffffff\nKernelalignment:0x200000\nRelocatableKernel:0x00\nMinAlignment:0x15\nXLoadFlags:0x01\nCmdLineSize:0x7ff\nHardwareSubArch:0x00\nHardwareSubArchData:0x00\nPayloadOffset:0x255\nPayloadSize:0x9532c\nSetupData:0x00\nPrefAddress:0x1000000\nInitSize:0x6e0000\nHandoverOffset:0x00\n", 137 }, 138 { 139 args: []string{"initramfs"}, 140 name: "initramfs with no args", 141 status: 1, 142 out: cmdUsage + "\n", 143 skip: uskip, 144 }, 145 { 146 args: []string{"initramfs", "a", "b", "c", "too many"}, 147 name: "initramfs with too many args", 148 status: 1, 149 out: cmdUsage + "\n", 150 skip: uskip, 151 }, 152 { 153 args: []string{"initramfs", "a", "b", "c"}, 154 name: "initramfs with bad input file", 155 status: 1, 156 out: "open a: no such file or directory\n", 157 skip: uskip, 158 }, 159 { 160 args: []string{"initramfs", "bzImage", "b", "c"}, 161 name: "initramfs with bad initramfs file", 162 status: 1, 163 out: "open b: no such file or directory\n", 164 skip: uskip, 165 }, 166 } 167 168 // Table-driven testing 169 for _, tt := range tests { 170 t.Run(tt.name, func(t *testing.T) { 171 c := testutil.Command(t, tt.args...) 172 c.Dir = "../../pkg/bzimage/testdata" 173 // ignore the error, we deal with it via process status, 174 // and most of these commands are supposed to get an error. 175 out, _ := c.CombinedOutput() 176 status := c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() 177 if tt.status != status { 178 t.Errorf("err got: %v want %v", status, tt.status) 179 } 180 m := string(out[tt.skip:]) 181 if m != tt.out { 182 t.Errorf("got:'%q'(%d bytes)want:'%q'(%d bytes)", m, len(m), tt.out, len(tt.out)) 183 } 184 }) 185 } 186 } 187 188 func TestMain(m *testing.M) { 189 testutil.Run(m, main) 190 }