code.flowtr.dev/mirrors/u-root@v1.0.0/pkg/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  
    12  var badmagic = []byte("hi there")
    13  
    14  func TestUnmarshal(t *testing.T) {
    15  	Debug = t.Logf
    16  	image, err := ioutil.ReadFile("testdata/bzImage")
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	var b BzImage
    21  	if err := b.UnmarshalBinary(image); err != nil {
    22  		t.Fatal(err)
    23  	}
    24  }
    25  
    26  func TestMarshal(t *testing.T) {
    27  	Debug = t.Logf
    28  	image, err := ioutil.ReadFile("testdata/bzImage")
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	var b BzImage
    33  	if err := b.UnmarshalBinary(image); err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	d, err := b.MarshalBinary()
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if len(d) != len(image) {
    41  		t.Fatalf("Marshal: want %d as output len, got %d", len(image), len(d))
    42  	}
    43  	if err := Equal(image, d); err != nil {
    44  		t.Fatalf("Check if images are the same: want nil, got %v", err)
    45  	}
    46  	// Corrupt little bits of thing.
    47  	x := d[0x203]
    48  	d[0x203] = 1
    49  	if err := Equal(image, d); err == nil {
    50  		t.Fatalf("Corrupting marshaled image: got nil, want err")
    51  	}
    52  	d[0x203] = x
    53  	image[0x203] = 1
    54  	if err := Equal(image, d); err == nil {
    55  		t.Fatalf("Corrupting original image: got nil, want err")
    56  	}
    57  	image[0x203] = x
    58  	x = d[0x208]
    59  	d[0x208] = x + 1
    60  	if err := Equal(image, d); err == nil {
    61  		t.Fatalf("Corrupting marshaled header: got nil, want err")
    62  	}
    63  	d[0x208] = x
    64  	d[20000] = d[20000] + 1
    65  	if err := Equal(image, d); err == nil {
    66  		t.Fatalf("Corrupting marshaled kernel: got nil, want err")
    67  	}
    68  }
    69  
    70  func TestBadMagic(t *testing.T) {
    71  	var b BzImage
    72  	Debug = t.Logf
    73  	if err := b.UnmarshalBinary(badmagic); err == nil {
    74  		t.Fatal("Want err, got nil")
    75  	}
    76  }
    77  
    78  func TestAddInitRAMFS(t *testing.T) {
    79  	Debug = t.Logf
    80  	initramfsimage, err := ioutil.ReadFile("testdata/bzImage")
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	var b BzImage
    85  	if err := b.UnmarshalBinary(initramfsimage); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	b.AddInitRAMFS("testdata/init.cpio")
    89  	d, err := b.MarshalBinary()
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	// For testing, you can enable this write, and then:
    94  	// qemu-system-x86_64 -serial stdio -kernel /tmp/x
    95  	// I mainly left this here as a memo.
    96  	if false {
    97  		if err := ioutil.WriteFile("/tmp/x", d, 0644); err != nil {
    98  			t.Fatal(err)
    99  		}
   100  	}
   101  }
   102  func TestHeaderString(t *testing.T) {
   103  	Debug = t.Logf
   104  	initramfsimage, err := ioutil.ReadFile("testdata/bzImage")
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	var b BzImage
   109  	if err := b.UnmarshalBinary(initramfsimage); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	t.Logf("%s", b.Header.String())
   113  }