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

     1  // Copyright 2020 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  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/uio"
    15  )
    16  
    17  type file struct {
    18  	name    string
    19  	content []byte
    20  }
    21  
    22  func (f file) String() string {
    23  	return f.name
    24  }
    25  
    26  func (f file) ReadAt(p []byte, off int64) (int, error) {
    27  	if off >= int64(len(f.content)) {
    28  		return 0, io.EOF
    29  	}
    30  	return copy(p, f.content[off:]), nil
    31  }
    32  
    33  func TestCatInitrds(t *testing.T) {
    34  	for _, tt := range []struct {
    35  		readers     []io.ReaderAt
    36  		wantName    string
    37  		wantContent []byte
    38  		wantSize    int
    39  	}{
    40  		{
    41  			readers: []io.ReaderAt{
    42  				bytes.NewReader(make([]byte, 512)),
    43  				bytes.NewReader(make([]byte, 512)),
    44  			},
    45  			wantName:    "*bytes.Reader,*bytes.Reader",
    46  			wantContent: make([]byte, 1024),
    47  			wantSize:    1024,
    48  		},
    49  		{
    50  			readers: []io.ReaderAt{
    51  				strings.NewReader("yay"),
    52  				bytes.NewReader(make([]byte, 777)),
    53  			},
    54  			wantName:    "*strings.Reader,*bytes.Reader",
    55  			wantContent: append([]byte("yay"), make([]byte, 509+777)...),
    56  			wantSize:    3 + 509 + 777,
    57  		},
    58  		{
    59  			readers: []io.ReaderAt{
    60  				strings.NewReader("yay"),
    61  			},
    62  			wantName:    "*strings.Reader",
    63  			wantContent: []byte("yay"),
    64  			wantSize:    3,
    65  		},
    66  		{
    67  			readers: []io.ReaderAt{
    68  				strings.NewReader("foo"),
    69  				strings.NewReader("bar"),
    70  			},
    71  			wantName:    "*strings.Reader,*strings.Reader",
    72  			wantContent: append(append([]byte("foo"), make([]byte, 509)...), []byte("bar")...),
    73  			wantSize:    3 + 509 + 3,
    74  		},
    75  		{
    76  			readers: []io.ReaderAt{
    77  				file{
    78  					name:    "/bar/foo",
    79  					content: []byte("foo"),
    80  				},
    81  				file{
    82  					name:    "/bar/bar",
    83  					content: []byte("bar"),
    84  				},
    85  			},
    86  			wantName:    "/bar/foo,/bar/bar",
    87  			wantContent: append(append([]byte("foo"), make([]byte, 509)...), []byte("bar")...),
    88  			wantSize:    3 + 509 + 3,
    89  		},
    90  	} {
    91  		got := CatInitrds(tt.readers...)
    92  
    93  		by, err := uio.ReadAll(got)
    94  		if err != nil {
    95  			t.Errorf("CatInitrdReader errored: %v", err)
    96  		}
    97  
    98  		if len(by) != tt.wantSize {
    99  			t.Errorf("Cat(%v) = len %d, want len %d", tt.readers, len(by), tt.wantSize)
   100  		}
   101  		if !bytes.Equal(by, tt.wantContent) {
   102  			t.Errorf("Cat(%v) = %v, want %v", tt.readers, by, tt.wantContent)
   103  		}
   104  		s := fmt.Sprintf("%s", got)
   105  		if s != tt.wantName {
   106  			t.Errorf("Cat(%v) = name %s, want %s", tt.readers, s, tt.wantName)
   107  		}
   108  	}
   109  }