gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/exp/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/boot/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  		KernelCode:   []byte{5, 6, 7, 8},
    23  		KernelBase:   0x100000,
    24  		KernelOffset: 620,
    25  		Header: bzimage.LinuxHeader{
    26  			MBRCode: [192]byte{
    27  				0xea, 0x05, 0x00, 0xc0, 0x07, 0x8c, 0xc8, 0x8e,
    28  				0xd8, 0x8e, 0xc0, 0x8e, 0xd0, 0x31, 0xe4, 0xfb,
    29  				0xfc, 0xbe, 0x2d, 0x00, 0xac, 0x20, 0xc0, 0x74,
    30  				0x09, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10,
    31  				0xeb, 0xf2, 0x31, 0xc0, 0xcd, 0x16, 0xcd, 0x19,
    32  				0xea, 0xf0, 0xff, 0x00, 0xf0, 0x55, 0x73, 0x65,
    33  				0x20, 0x61, 0x20, 0x62, 0x6f, 0x6f, 0x74, 0x20,
    34  				0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x0d,
    35  				0x0a, 0x0a, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65,
    36  				0x20, 0x64, 0x69, 0x73, 0x6b, 0x20, 0x61, 0x6e,
    37  				0x64, 0x20, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20,
    38  				0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20,
    39  				0x74, 0x6f, 0x20, 0x72, 0x65, 0x62, 0x6f, 0x6f,
    40  				0x74, 0x2e, 0x2e, 0x2e, 0x0d, 0x0a, 0x00, 0x00,
    41  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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  			ExtRamdiskImage:     00,
    52  			ExtRamdiskSize:      00,
    53  			ExtCmdlinePtr:       00,
    54  			SetupSects:          0x1e,
    55  			RootFlags:           0x01,
    56  			Syssize:             0xb51d,
    57  			RamSize:             0x00,
    58  			Vidmode:             0xffff,
    59  			RootDev:             0x00,
    60  			Bootsectormagic:     0xaa55,
    61  			Jump:                0x66eb,
    62  			HeaderMagic:         [4]byte{0x48, 0x64, 0x72, 0x53},
    63  			Protocolversion:     0x20d,
    64  			RealModeSwitch:      0x00,
    65  			StartSys:            0x1000,
    66  			Kveraddr:            0x3140,
    67  			TypeOfLoader:        0x00,
    68  			Loadflags:           0x01,
    69  			Setupmovesize:       0x8000,
    70  			Code32Start:         0x100000,
    71  			RamDiskImage:        0x00,
    72  			RamDiskSize:         0x00,
    73  			BootSectKludge:      [4]uint8{},
    74  			Heapendptr:          0x5320,
    75  			ExtLoaderVer:        0x00,
    76  			ExtLoaderType:       0x00,
    77  			Cmdlineptr:          0x00,
    78  			InitrdAddrMax:       0x7fffffff,
    79  			Kernelalignment:     0x200000,
    80  			RelocatableKernel:   0x00,
    81  			MinAlignment:        0x15,
    82  			XLoadFlags:          0x01,
    83  			CmdLineSize:         0x7ff,
    84  			HardwareSubArch:     0x00,
    85  			HardwareSubArchData: 0x00,
    86  			PayloadOffset:       0x255,
    87  			PayloadSize:         0x9532c,
    88  			SetupData:           0x00,
    89  			PrefAddress:         0x1000000,
    90  			InitSize:            0x6e0000,
    91  			HandoverOffset:      0x00,
    92  		},
    93  	}
    94  	uskip = len("2018/08/10 21:20:42 ")
    95  )
    96  
    97  func TestSimple(t *testing.T) {
    98  	tmpDir, err := ioutil.TempDir("", "bzImage")
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	defer os.RemoveAll(tmpDir)
   103  
   104  	var tests = []struct {
   105  		args   []string
   106  		name   string
   107  		status int
   108  		out    string
   109  		skip   int
   110  	}{
   111  		{
   112  			args:   []string{"initramfs", "bzImage", "init.cpio", "zz/zz/zz"},
   113  			name:   "too big initramfs",
   114  			status: 1,
   115  			out:    "new initramfs is 1536 bytes, won't fit in 480 byte old one\n",
   116  			skip:   uskip,
   117  		},
   118  		{
   119  			args:   []string{"initramfs", "bzImage", "/dev/null", "zz/zz/zz"},
   120  			name:   "Bad output file",
   121  			status: 1,
   122  			out:    "open zz/zz/zz: no such file or directory\n",
   123  			skip:   uskip,
   124  		},
   125  		{
   126  			args:   []string{"initramfs", "bzImage", "/dev/null", filepath.Join(tmpDir, "zz")},
   127  			name:   "correct initramfs test",
   128  			status: 0,
   129  			out:    "",
   130  		},
   131  		{
   132  			args:   []string{},
   133  			name:   "no args",
   134  			status: 1,
   135  			out:    cmdUsage + "\n",
   136  			skip:   uskip,
   137  		},
   138  		{
   139  			args:   []string{"dump", "bzImage"},
   140  			name:   "dump",
   141  			status: 0,
   142  			out:    "MBRCode:0xea0500c0078cc88ed88ec08ed031e4fbfcbe2d00ac20c07409b40ebb0700cd10ebf231c0cd16cd19eaf0ff00f0557365206120626f6f74206c6f616465722e0d0a0a52656d6f7665206469736b20616e6420707265737320616e79206b657920746f207265626f6f742e2e2e0d0a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\nExtRamdiskImage:0x00\nExtRamdiskSize:0x00\nExtCmdlinePtr:0x00\nO:0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffff\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",
   143  		},
   144  		{
   145  			args:   []string{"initramfs"},
   146  			name:   "initramfs with no args",
   147  			status: 1,
   148  			out:    cmdUsage + "\n",
   149  			skip:   uskip,
   150  		},
   151  		{
   152  			args:   []string{"initramfs", "a", "b", "c", "too many"},
   153  			name:   "initramfs with too many args",
   154  			status: 1,
   155  			out:    cmdUsage + "\n",
   156  			skip:   uskip,
   157  		},
   158  		{
   159  			args:   []string{"initramfs", "a", "b", "c"},
   160  			name:   "initramfs with bad input file",
   161  			status: 1,
   162  			out:    "open a: no such file or directory\n",
   163  			skip:   uskip,
   164  		},
   165  		{
   166  			args:   []string{"initramfs", "bzImage", "b", "c"},
   167  			name:   "initramfs with bad initramfs file",
   168  			status: 1,
   169  			out:    "open b: no such file or directory\n",
   170  			skip:   uskip,
   171  		},
   172  	}
   173  
   174  	// Table-driven testing
   175  	for _, tt := range tests {
   176  		t.Run(tt.name, func(t *testing.T) {
   177  			c := testutil.Command(t, tt.args...)
   178  			c.Dir = "../../../pkg/boot/bzimage/testdata"
   179  			// ignore the error, we deal with it via process status,
   180  			// and most of these commands are supposed to get an error.
   181  			out, _ := c.CombinedOutput()
   182  			status := c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
   183  			if tt.status != status {
   184  				t.Errorf("err got: %v want %v", status, tt.status)
   185  			}
   186  			m := string(out[tt.skip:])
   187  			if m != tt.out {
   188  				t.Errorf("got:'%q'(%d bytes)want:'%q'(%d bytes)", m, len(m), tt.out, len(tt.out))
   189  			}
   190  		})
   191  	}
   192  }
   193  
   194  func TestMain(m *testing.M) {
   195  	testutil.Run(m, main)
   196  }