github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  	"bytes"
     9  	"fmt"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/mvdan/u-root-coreutils/pkg/boot/bzimage"
    15  )
    16  
    17  var (
    18  	testdataPath = "../../../pkg/boot/bzimage/testdata/"
    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  			},
    52  			ExtRamdiskImage:     0x00,
    53  			ExtRamdiskSize:      0x00,
    54  			ExtCmdlinePtr:       0x00,
    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  	jsonVer = `{
    97  	"Release": "4.12.7",
    98  	"Version": "#6 Fri Aug 10 14:47:18 PDT 2018",
    99  	"Builder": "rminnich@uroot",
   100  	"BuildNum": 6,
   101  	"BuildTime": "2018-08-10T14:47:18`
   102  	// The rest of this is too sensitive to formatting
   103  	// on the various CI systems, this is enough.
   104  )
   105  
   106  func TestRun(t *testing.T) {
   107  	tmpdir := t.TempDir()
   108  	for _, tt := range []struct {
   109  		name    string
   110  		args    []string
   111  		debug   bool
   112  		jsonOut bool
   113  		want    string
   114  	}{
   115  		{
   116  			name: "too big initramfs",
   117  			args: []string{"initramfs", filepath.Join(testdataPath, "bzImage"), filepath.Join(testdataPath, "init.cpio"), "zz/zz/zz"},
   118  			want: "new initramfs is 1536 bytes, won't fit in 480 byte old one",
   119  		},
   120  		{
   121  			name: "Bad output file",
   122  			args: []string{"initramfs", filepath.Join(testdataPath, "bzImage"), "/dev/null", "zz/zz/zz"},
   123  			want: "open zz/zz/zz: no such file or directory",
   124  		},
   125  		{
   126  			name: "correct initramfs test",
   127  			args: []string{"initramfs", filepath.Join(testdataPath, "bzImage"), "/dev/null", filepath.Join(tmpdir, "zz")},
   128  		},
   129  		{
   130  			name: "no args",
   131  			args: []string{},
   132  		},
   133  		{
   134  			name: "dump",
   135  			args: []string{"dump", filepath.Join(testdataPath, "bzImage")},
   136  			want: "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",
   137  		},
   138  		{
   139  			name: "initramfs too many args",
   140  			args: []string{"initramfs", "a", "b", "c", "too many"},
   141  		},
   142  		{
   143  			name: "initramfs with bad input file",
   144  			args: []string{"initramfs", "a", "b", "c"},
   145  			want: "open a: no such file or directory",
   146  		},
   147  		{
   148  			name: "initramfs with bad initramfs file",
   149  			args: []string{"initramfs", filepath.Join(testdataPath, "bzImage"), "b", "c"},
   150  			want: "open b: no such file or directory",
   151  		},
   152  		{
   153  			name: "kernel version",
   154  			args: []string{"ver", filepath.Join(testdataPath, "bzImage")},
   155  			want: "4.12.7 (rminnich@uroot) #6 Fri Aug 10 14:47:18 PDT 2018\n",
   156  		},
   157  		{
   158  			name:    "kernel version with jsonOut",
   159  			args:    []string{"ver", filepath.Join(testdataPath, "bzImage")},
   160  			jsonOut: true,
   161  			want:    strings.ReplaceAll(jsonVer, "\t", "    "),
   162  		},
   163  		{
   164  			name: "cfg with wrong image file",
   165  			args: []string{"cfg", filepath.Join(testdataPath, "bzImage")},
   166  			want: "embedded config not found",
   167  		},
   168  		{
   169  			name: "diff",
   170  			args: []string{"diff", filepath.Join(testdataPath, "bzImage"), filepath.Join(testdataPath, "bzimage-64kurandominitramfs")},
   171  			want: "MBRCode:0xea0500c0078cc88ed88ec08ed031e4fbfcbe2d00ac20c07409b40ebb0700cd10ebf231c0cd16cd19eaf0ff00f0557365206120626f6f74206c6f616465722e0d0a0a52656d6f7665206469736b20616e6420707265737320616e79206b657920746f207265626f6f742e2e2e0d0a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 != MBRCode:0x4d5aea0700c0078cc88ed88ec08ed031e4fbfcbe4000ac20c07409b40ebb0700cd10ebf231c0cd16cd19eaf0ff00f00000000000000000000000000082000000557365206120626f6f74206c6f616465722e0d0a0a52656d6f7665206469736b20616e6420707265737320616e79206b657920746f207265626f6f742e2e2e0d0a005045000064860400000000000000000001000000a00006020b0202143050120000000000d0ad610010420000000200000000000000000000200000002000O:0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffff != O:0x0000000000000000740000020000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002e73657475700000e03d000000020000e03d000000020000000000000000000000000000200050602e72656c6f63000020000000e03f000020000000e03f0000000000000000000000000000400010422e7465787400000030121200004000003012120000400000000000000000000000000000200050602e62737300000000d0ad6100305212000000000000000000000000000000000000000000800000c8000000000000000000000000000000000000000000ffffSetupSects:0x1e != SetupSects:0x1fSyssize:0xb51d != Syssize:0x12123RelocatableKernel:0x00 != RelocatableKernel:0x01XLoadFlags:0x01 != XLoadFlags:0x0bPayloadOffset:0x255 != PayloadOffset:0x3b4PayloadSize:0x9532c != PayloadSize:0xf0da4InitSize:0x6e0000 != InitSize:0x740000HandoverOffset:0x00 != HandoverOffset:0x190",
   172  		},
   173  		{
   174  			name: "diff with wrong input file",
   175  			args: []string{"diff", filepath.Join(testdataPath, "bzImage"), filepath.Join(tmpdir, "filedoesnotexist")},
   176  			want: fmt.Sprintf("open %s: no such file or directory", filepath.Join(tmpdir, "filedoesnotexist")),
   177  		},
   178  		{
   179  			name: "copy with err in write",
   180  			args: []string{"copy", filepath.Join(testdataPath, "bzImage"), tmpdir},
   181  			want: fmt.Sprintf("writing %s: open %s: is a directory", tmpdir, tmpdir),
   182  		},
   183  		{
   184  			name: "extract",
   185  			args: []string{"extract", filepath.Join(testdataPath, "bzImage"), tmpdir},
   186  			want: "ramfs is 480 bytes",
   187  		},
   188  		{
   189  			name:  "debug on",
   190  			args:  []string{},
   191  			debug: true,
   192  		},
   193  	} {
   194  		t.Run(tt.name, func(t *testing.T) {
   195  			*debug = tt.debug
   196  			*jsonOut = tt.jsonOut
   197  			buf := &bytes.Buffer{}
   198  			if got := run(buf, tt.args...); got != nil {
   199  				if got.Error() != tt.want {
   200  					t.Errorf("run() = %q, want: %q", got.Error(), tt.want)
   201  				}
   202  			} else {
   203  				if !strings.Contains(buf.String(), tt.want) {
   204  					t.Errorf("run() = %q, want: %q", buf.String(), tt.want)
   205  				}
   206  			}
   207  		})
   208  	}
   209  }