gopkg.in/hugelgupf/u-root.v2@v2.0.0-20180831055005-3f8fdb0ce09d/pkg/boot/linux_test.go (about)

     1  // Copyright 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 boot
     6  
     7  import (
     8  	"errors"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/u-root/u-root/pkg/cpio"
    13  )
    14  
    15  func imageEqual(li1, li2 *LinuxImage) bool {
    16  	return cpio.ReaderAtEqual(li1.Kernel, li2.Kernel) &&
    17  		cpio.ReaderAtEqual(li1.Kernel, li2.Kernel) &&
    18  		li1.Cmdline == li2.Cmdline
    19  }
    20  
    21  var errSkip = errors.New("foo")
    22  
    23  type errorReaderAt struct {
    24  	err error
    25  }
    26  
    27  func (e *errorReaderAt) ReadAt([]byte, int64) (int, error) {
    28  	return 0, e.err
    29  }
    30  
    31  func TestLinuxImage(t *testing.T) {
    32  	for _, tt := range []struct {
    33  		li  *LinuxImage
    34  		err error
    35  	}{
    36  		{
    37  			li: &LinuxImage{
    38  				Kernel:  strings.NewReader("foo"),
    39  				Initrd:  strings.NewReader("bar"),
    40  				Cmdline: "foo=bar",
    41  			},
    42  			err: nil,
    43  		},
    44  		{
    45  			li: &LinuxImage{
    46  				Kernel:  strings.NewReader("foo"),
    47  				Initrd:  nil,
    48  				Cmdline: "foo=bar",
    49  			},
    50  			err: nil,
    51  		},
    52  		{
    53  			li: &LinuxImage{
    54  				Kernel:  nil,
    55  				Initrd:  nil,
    56  				Cmdline: "foo=bar",
    57  			},
    58  			err: ErrKernelMissing,
    59  		},
    60  		{
    61  			li: &LinuxImage{
    62  				Kernel:  &errorReaderAt{err: errSkip},
    63  				Initrd:  nil,
    64  				Cmdline: "foo=bar",
    65  			},
    66  			err: errSkip,
    67  		},
    68  		{
    69  			li: &LinuxImage{
    70  				Kernel:  strings.NewReader("foo"),
    71  				Initrd:  &errorReaderAt{err: errSkip},
    72  				Cmdline: "foo=bar",
    73  			},
    74  			err: errSkip,
    75  		},
    76  		{
    77  			li: &LinuxImage{
    78  				Kernel:  strings.NewReader("foo"),
    79  				Initrd:  nil,
    80  				Cmdline: "",
    81  			},
    82  			err: nil,
    83  		},
    84  	} {
    85  		a := cpio.InMemArchive()
    86  		sw := NewSigningWriter(a)
    87  		if err := tt.li.Pack(sw); err != tt.err {
    88  			t.Errorf("Pack(%v) = %v, want %v", tt.li, err, tt.err)
    89  		} else if err == nil {
    90  			li, err := NewLinuxImageFromArchive(a)
    91  			if err != nil {
    92  				t.Errorf("Linux image from %v: %v", a, err)
    93  			}
    94  			if !imageEqual(tt.li, li) {
    95  				t.Errorf("Images are not equal: got %v\nwant %v", li, tt.li)
    96  			}
    97  		}
    98  	}
    99  }