github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/pkg/pxe/util_test.go (about)

     1  package pxe
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"testing"
     8  )
     9  
    10  type mockReader struct {
    11  	// called is whether Read has been called.
    12  	called bool
    13  
    14  	// err is the error to return on Read.
    15  	err error
    16  }
    17  
    18  func (m *mockReader) Read([]byte) (int, error) {
    19  	m.called = true
    20  	return 0, m.err
    21  }
    22  
    23  func TestLazyOpenerRead(t *testing.T) {
    24  	for i, tt := range []struct {
    25  		openErr    error
    26  		openReader *mockReader
    27  		wantCalled bool
    28  	}{
    29  		{
    30  			openErr:    nil,
    31  			openReader: &mockReader{},
    32  			wantCalled: true,
    33  		},
    34  		{
    35  			openErr:    io.EOF,
    36  			openReader: nil,
    37  			wantCalled: false,
    38  		},
    39  		{
    40  			openErr: nil,
    41  			openReader: &mockReader{
    42  				err: io.ErrUnexpectedEOF,
    43  			},
    44  			wantCalled: true,
    45  		},
    46  	} {
    47  		t.Run(fmt.Sprintf("Test #%02d", i), func(t *testing.T) {
    48  			var opened bool
    49  			lr := NewLazyOpener(func() (io.Reader, error) {
    50  				opened = true
    51  				return tt.openReader, tt.openErr
    52  			})
    53  			_, err := lr.Read([]byte{})
    54  			if !opened {
    55  				t.Fatalf("Read(): Reader was not opened")
    56  			}
    57  			if tt.openErr != nil && err != tt.openErr {
    58  				t.Errorf("Read() = %v, want %v", err, tt.openErr)
    59  			}
    60  			if tt.openReader != nil {
    61  				if got, want := tt.openReader.called, tt.wantCalled; got != want {
    62  					t.Errorf("mockReader.Read() called is %v, want %v", got, want)
    63  				}
    64  				if tt.openReader.err != nil && err != tt.openReader.err {
    65  					t.Errorf("Read() = %v, want %v", err, tt.openReader.err)
    66  				}
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestCachingReaderRead(t *testing.T) {
    73  	type read struct {
    74  		// Buffer sizes to call Read with.
    75  		size int
    76  
    77  		// Buffer value corresponding Read(size) we want.
    78  		want []byte
    79  
    80  		// Error corresponding to Read(size) we want.
    81  		err error
    82  	}
    83  
    84  	for i, tt := range []struct {
    85  		// Content of the underlying io.Reader.
    86  		content []byte
    87  
    88  		// Read calls to make in order.
    89  		reads []read
    90  	}{
    91  		{
    92  			content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
    93  			reads: []read{
    94  				{
    95  					size: 0,
    96  				},
    97  				{
    98  					size: 1,
    99  					want: []byte{0x11},
   100  				},
   101  				{
   102  					size: 2,
   103  					want: []byte{0x22, 0x33},
   104  				},
   105  				{
   106  					size: 0,
   107  				},
   108  				{
   109  					size: 3,
   110  					want: []byte{0x44, 0x55, 0x66},
   111  				},
   112  				{
   113  					size: 4,
   114  					want: []byte{0x77, 0x88, 0x99},
   115  					err:  io.EOF,
   116  				},
   117  			},
   118  		},
   119  		{
   120  			content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   121  			reads: []read{
   122  				{
   123  					size: 11,
   124  					want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   125  					err:  io.EOF,
   126  				},
   127  			},
   128  		},
   129  		{
   130  			content: nil,
   131  			reads: []read{
   132  				{
   133  					size: 2,
   134  					err:  io.EOF,
   135  				},
   136  				{
   137  					size: 0,
   138  				},
   139  			},
   140  		},
   141  		{
   142  			content: []byte{0x33, 0x22, 0x11},
   143  			reads: []read{
   144  				{
   145  					size: 3,
   146  					want: []byte{0x33, 0x22, 0x11},
   147  					err:  nil,
   148  				},
   149  				{
   150  					size: 0,
   151  				},
   152  				{
   153  					size: 1,
   154  					err:  io.EOF,
   155  				},
   156  			},
   157  		},
   158  	} {
   159  		t.Run(fmt.Sprintf("Test [%02d]", i), func(t *testing.T) {
   160  			buf := NewCachingReader(bytes.NewBuffer(tt.content))
   161  			for j, r := range tt.reads {
   162  				p := make([]byte, r.size)
   163  				m, err := buf.Read(p)
   164  				if err != r.err {
   165  					t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, err, r.err)
   166  				}
   167  				if m != len(r.want) {
   168  					t.Errorf("Read#%d(%d) = %d, want %d", j, r.size, m, len(r.want))
   169  				}
   170  				if !bytes.Equal(r.want, p[:m]) {
   171  					t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, p[:m], r.want)
   172  				}
   173  			}
   174  		})
   175  	}
   176  }
   177  
   178  func TestCachingReaderReadAt(t *testing.T) {
   179  	type readAt struct {
   180  		// Buffer sizes to call Read with.
   181  		size int
   182  
   183  		// Offset to read from.
   184  		off int64
   185  
   186  		// Buffer value corresponding Read(size) we want.
   187  		want []byte
   188  
   189  		// Error corresponding to Read(size) we want.
   190  		err error
   191  	}
   192  
   193  	for i, tt := range []struct {
   194  		// Content of the underlying io.Reader.
   195  		content []byte
   196  
   197  		// Read calls to make in order.
   198  		reads []readAt
   199  	}{
   200  		{
   201  			content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   202  			reads: []readAt{
   203  				{
   204  					off:  0,
   205  					size: 0,
   206  				},
   207  				{
   208  					off:  0,
   209  					size: 1,
   210  					want: []byte{0x11},
   211  				},
   212  				{
   213  					off:  1,
   214  					size: 2,
   215  					want: []byte{0x22, 0x33},
   216  				},
   217  				{
   218  					off:  0,
   219  					size: 0,
   220  				},
   221  				{
   222  					off:  3,
   223  					size: 3,
   224  					want: []byte{0x44, 0x55, 0x66},
   225  				},
   226  				{
   227  					off:  6,
   228  					size: 4,
   229  					want: []byte{0x77, 0x88, 0x99},
   230  					err:  io.EOF,
   231  				},
   232  				{
   233  					off:  0,
   234  					size: 9,
   235  					want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   236  				},
   237  				{
   238  					off:  0,
   239  					size: 10,
   240  					want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   241  					err:  io.EOF,
   242  				},
   243  				{
   244  					off:  0,
   245  					size: 8,
   246  					want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
   247  				},
   248  			},
   249  		},
   250  		{
   251  			content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   252  			reads: []readAt{
   253  				{
   254  					off:  10,
   255  					size: 10,
   256  					err:  io.EOF,
   257  				},
   258  				{
   259  					off:  5,
   260  					size: 4,
   261  					want: []byte{0x66, 0x77, 0x88, 0x99},
   262  				},
   263  			},
   264  		},
   265  		{
   266  			content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   267  			reads: []readAt{
   268  				{
   269  					size: 9,
   270  					want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99},
   271  				},
   272  				{
   273  					off:  5,
   274  					size: 4,
   275  					want: []byte{0x66, 0x77, 0x88, 0x99},
   276  				},
   277  				{
   278  					off:  9,
   279  					size: 1,
   280  					err:  io.EOF,
   281  				},
   282  			},
   283  		},
   284  	} {
   285  		t.Run(fmt.Sprintf("Test [%02d]", i), func(t *testing.T) {
   286  			buf := NewCachingReader(bytes.NewBuffer(tt.content))
   287  			for j, r := range tt.reads {
   288  				p := make([]byte, r.size)
   289  				m, err := buf.ReadAt(p, r.off)
   290  				if err != r.err {
   291  					t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, err, r.err)
   292  				}
   293  				if m != len(r.want) {
   294  					t.Errorf("Read#%d(%d) = %d, want %d", j, r.size, m, len(r.want))
   295  				}
   296  				if !bytes.Equal(r.want, p[:m]) {
   297  					t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, p[:m], r.want)
   298  				}
   299  			}
   300  		})
   301  	}
   302  }