github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/bzimage/bzimage_test.go (about)

     1  // Copyright 2012-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 bzimage
     6  
     7  import (
     8  	"io/ioutil"
     9  	"testing"
    10  
    11  	"github.com/u-root/u-root/pkg/cpio"
    12  )
    13  
    14  var badmagic = []byte("hi there")
    15  
    16  func TestUnmarshal(t *testing.T) {
    17  	Debug = t.Logf
    18  	image, err := ioutil.ReadFile("testdata/bzImage")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	var b BzImage
    23  	if err := b.UnmarshalBinary(image); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  }
    27  
    28  func TestMarshal(t *testing.T) {
    29  	Debug = t.Logf
    30  	image, err := ioutil.ReadFile("testdata/bzImage")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	var b BzImage
    35  	if err := b.UnmarshalBinary(image); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	t.Logf("b header is %s", b.Header.String())
    39  	image, err = b.MarshalBinary()
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	// now unmarshall back into ourselves.
    45  	// We can't perfectly recreate the image the kernel built,
    46  	// but we need to know we are stable.
    47  	if err := b.UnmarshalBinary(image); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	d, err := b.MarshalBinary()
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	var n BzImage
    55  	if err := n.UnmarshalBinary(d); err != nil {
    56  		t.Fatalf("Unmarshalling marshaled image: want nil, got  %v", err)
    57  	}
    58  
    59  	t.Logf("DIFF: %v", b.Header.Diff(&n.Header))
    60  	if d := b.Header.Diff(&n.Header); d != "" {
    61  		t.Errorf("Headers differ: %s", d)
    62  	}
    63  	if len(d) != len(image) {
    64  		t.Fatalf("Marshal: want %d as output len, got %d; diff is %s", len(image), len(d), b.Diff(&b))
    65  	}
    66  
    67  	if err := Equal(image, d); err != nil {
    68  		t.Logf("Check if images are the same: want nil, got %v", err)
    69  	}
    70  
    71  	// Corrupt little bits of thing.
    72  	x := d[0x203]
    73  	d[0x203] = 1
    74  	if err := Equal(image, d); err == nil {
    75  		t.Fatalf("Corrupting marshaled image: got nil, want err")
    76  	}
    77  	d[0x203] = x
    78  	image[0x203] = 1
    79  	if err := Equal(image, d); err == nil {
    80  		t.Fatalf("Corrupting original image: got nil, want err")
    81  	}
    82  	image[0x203] = x
    83  	x = d[0x208]
    84  	d[0x208] = x + 1
    85  	if err := Equal(image, d); err == nil {
    86  		t.Fatalf("Corrupting marshaled header: got nil, want err")
    87  	}
    88  	d[0x208] = x
    89  	d[20000] = d[20000] + 1
    90  	if err := Equal(image, d); err == nil {
    91  		t.Fatalf("Corrupting marshaled kernel: got nil, want err")
    92  	}
    93  }
    94  
    95  func TestBadMagic(t *testing.T) {
    96  	var b BzImage
    97  	Debug = t.Logf
    98  	if err := b.UnmarshalBinary(badmagic); err == nil {
    99  		t.Fatal("Want err, got nil")
   100  	}
   101  }
   102  
   103  func TestAddInitRAMFS(t *testing.T) {
   104  	t.Logf("TestAddInitRAMFS")
   105  	Debug = t.Logf
   106  	initramfsimage, err := ioutil.ReadFile("testdata/bzimage-64kurandominitramfs")
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	var b BzImage
   111  	if err := b.UnmarshalBinary(initramfsimage); err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	if err := b.AddInitRAMFS("testdata/init.cpio"); err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	d, err := b.MarshalBinary()
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	// For testing, you can enable this write, and then:
   122  	// qemu-system-x86_64 -serial stdio -kernel /tmp/x
   123  	// I mainly left this here as a memo.
   124  	if true {
   125  		if err := ioutil.WriteFile("/tmp/x", d, 0644); err != nil {
   126  			t.Fatal(err)
   127  		}
   128  	}
   129  	// Make KernelCode much bigger and watch it fail.
   130  	k := b.KernelCode
   131  	b.KernelCode = append(b.KernelCode, k...)
   132  	b.KernelCode = append(b.KernelCode, k...)
   133  	b.KernelCode = append(b.KernelCode, k...)
   134  	b.KernelCode = append(b.KernelCode, k...)
   135  
   136  	_, err = b.MarshalBinary()
   137  	if err == nil {
   138  		t.Logf("Overflow test, want %v, got nil", "Marshal: compressed KernelCode too big: was 986532, now 1422388")
   139  		t.Fatal(err)
   140  	}
   141  
   142  	b.KernelCode = k[:len(k)-len(k)/2]
   143  
   144  	_, err = b.MarshalBinary()
   145  	if err != nil {
   146  		t.Logf("shrink test, want nil, got %v", err)
   147  		t.Fatal(err)
   148  	}
   149  
   150  }
   151  func TestHeaderString(t *testing.T) {
   152  	Debug = t.Logf
   153  	initramfsimage, err := ioutil.ReadFile("testdata/bzImage")
   154  	if err != nil {
   155  		t.Fatal(err)
   156  	}
   157  	var b BzImage
   158  	if err := b.UnmarshalBinary(initramfsimage); err != nil {
   159  		t.Fatal(err)
   160  	}
   161  	t.Logf("%s", b.Header.String())
   162  }
   163  func TestExtract(t *testing.T) {
   164  	Debug = t.Logf
   165  	initramfsimage, err := ioutil.ReadFile("testdata/bzImage")
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  	var b BzImage
   170  	if err := b.UnmarshalBinary(initramfsimage); err != nil {
   171  		t.Fatal(err)
   172  	}
   173  	t.Logf("%s", b.Header.String())
   174  	// The simplest test: what is extracted should be a valid elf.
   175  	e, err := b.ELF()
   176  	if err != nil {
   177  		t.Fatalf("Extracted bzImage data is an elf: want nil, got %v", err)
   178  	}
   179  	t.Logf("Header: %v", e.FileHeader)
   180  	for i, p := range e.Progs {
   181  		t.Logf("%d: %v", i, *p)
   182  	}
   183  }
   184  
   185  func TestELF(t *testing.T) {
   186  	Debug = t.Logf
   187  	initramfsimage, err := ioutil.ReadFile("testdata/bzImage")
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  	var b BzImage
   192  	if err := b.UnmarshalBinary(initramfsimage); err != nil {
   193  		t.Fatal(err)
   194  	}
   195  	t.Logf("%s", b.Header.String())
   196  	e, err := b.ELF()
   197  	if err != nil {
   198  		t.Fatalf("Extract: want nil, got %v", err)
   199  	}
   200  	t.Logf("%v", e.FileHeader)
   201  }
   202  
   203  func TestInitRAMFS(t *testing.T) {
   204  	Debug = t.Logf
   205  	cpio.Debug = t.Logf
   206  	for _, bz := range []string{"testdata/bzImage", "testdata/bzimage-64kurandominitramfs"} {
   207  		initramfsimage, err := ioutil.ReadFile(bz)
   208  		if err != nil {
   209  			t.Fatal(err)
   210  		}
   211  		var b BzImage
   212  		if err := b.UnmarshalBinary(initramfsimage); err != nil {
   213  			t.Fatal(err)
   214  		}
   215  		s, e, err := b.InitRAMFS()
   216  		if err != nil {
   217  			t.Fatal(err)
   218  		}
   219  		t.Logf("Found %d byte initramfs@%d:%d", e-s, s, e)
   220  	}
   221  
   222  }