github.com/ph/moby@v1.13.1/pkg/ioutils/multireader_test.go (about)

     1  package ioutils
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestMultiReadSeekerReadAll(t *testing.T) {
    15  	str := "hello world"
    16  	s1 := strings.NewReader(str + " 1")
    17  	s2 := strings.NewReader(str + " 2")
    18  	s3 := strings.NewReader(str + " 3")
    19  	mr := MultiReadSeeker(s1, s2, s3)
    20  
    21  	expectedSize := int64(s1.Len() + s2.Len() + s3.Len())
    22  
    23  	b, err := ioutil.ReadAll(mr)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	expected := "hello world 1hello world 2hello world 3"
    29  	if string(b) != expected {
    30  		t.Fatalf("ReadAll failed, got: %q, expected %q", string(b), expected)
    31  	}
    32  
    33  	size, err := mr.Seek(0, os.SEEK_END)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if size != expectedSize {
    38  		t.Fatalf("reader size does not match, got %d, expected %d", size, expectedSize)
    39  	}
    40  
    41  	// Reset the position and read again
    42  	pos, err := mr.Seek(0, os.SEEK_SET)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if pos != 0 {
    47  		t.Fatalf("expected position to be set to 0, got %d", pos)
    48  	}
    49  
    50  	b, err = ioutil.ReadAll(mr)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	if string(b) != expected {
    56  		t.Fatalf("ReadAll failed, got: %q, expected %q", string(b), expected)
    57  	}
    58  }
    59  
    60  func TestMultiReadSeekerReadEach(t *testing.T) {
    61  	str := "hello world"
    62  	s1 := strings.NewReader(str + " 1")
    63  	s2 := strings.NewReader(str + " 2")
    64  	s3 := strings.NewReader(str + " 3")
    65  	mr := MultiReadSeeker(s1, s2, s3)
    66  
    67  	var totalBytes int64
    68  	for i, s := range []*strings.Reader{s1, s2, s3} {
    69  		sLen := int64(s.Len())
    70  		buf := make([]byte, s.Len())
    71  		expected := []byte(fmt.Sprintf("%s %d", str, i+1))
    72  
    73  		if _, err := mr.Read(buf); err != nil && err != io.EOF {
    74  			t.Fatal(err)
    75  		}
    76  
    77  		if !bytes.Equal(buf, expected) {
    78  			t.Fatalf("expected %q to be %q", string(buf), string(expected))
    79  		}
    80  
    81  		pos, err := mr.Seek(0, os.SEEK_CUR)
    82  		if err != nil {
    83  			t.Fatalf("iteration: %d, error: %v", i+1, err)
    84  		}
    85  
    86  		// check that the total bytes read is the current position of the seeker
    87  		totalBytes += sLen
    88  		if pos != totalBytes {
    89  			t.Fatalf("expected current position to be: %d, got: %d, iteration: %d", totalBytes, pos, i+1)
    90  		}
    91  
    92  		// This tests not only that SEEK_SET and SEEK_CUR give the same values, but that the next iteration is in the expected position as well
    93  		newPos, err := mr.Seek(pos, os.SEEK_SET)
    94  		if err != nil {
    95  			t.Fatal(err)
    96  		}
    97  		if newPos != pos {
    98  			t.Fatalf("expected to get same position when calling SEEK_SET with value from SEEK_CUR, cur: %d, set: %d", pos, newPos)
    99  		}
   100  	}
   101  }
   102  
   103  func TestMultiReadSeekerReadSpanningChunks(t *testing.T) {
   104  	str := "hello world"
   105  	s1 := strings.NewReader(str + " 1")
   106  	s2 := strings.NewReader(str + " 2")
   107  	s3 := strings.NewReader(str + " 3")
   108  	mr := MultiReadSeeker(s1, s2, s3)
   109  
   110  	buf := make([]byte, s1.Len()+3)
   111  	_, err := mr.Read(buf)
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	// expected is the contents of s1 + 3 bytes from s2, ie, the `hel` at the end of this string
   117  	expected := "hello world 1hel"
   118  	if string(buf) != expected {
   119  		t.Fatalf("expected %s to be %s", string(buf), expected)
   120  	}
   121  }
   122  
   123  func TestMultiReadSeekerNegativeSeek(t *testing.T) {
   124  	str := "hello world"
   125  	s1 := strings.NewReader(str + " 1")
   126  	s2 := strings.NewReader(str + " 2")
   127  	s3 := strings.NewReader(str + " 3")
   128  	mr := MultiReadSeeker(s1, s2, s3)
   129  
   130  	s1Len := s1.Len()
   131  	s2Len := s2.Len()
   132  	s3Len := s3.Len()
   133  
   134  	s, err := mr.Seek(int64(-1*s3.Len()), os.SEEK_END)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	if s != int64(s1Len+s2Len) {
   139  		t.Fatalf("expected %d to be %d", s, s1.Len()+s2.Len())
   140  	}
   141  
   142  	buf := make([]byte, s3Len)
   143  	if _, err := mr.Read(buf); err != nil && err != io.EOF {
   144  		t.Fatal(err)
   145  	}
   146  	expected := fmt.Sprintf("%s %d", str, 3)
   147  	if string(buf) != fmt.Sprintf("%s %d", str, 3) {
   148  		t.Fatalf("expected %q to be %q", string(buf), expected)
   149  	}
   150  }
   151  
   152  func TestMultiReadSeekerCurAfterSet(t *testing.T) {
   153  	str := "hello world"
   154  	s1 := strings.NewReader(str + " 1")
   155  	s2 := strings.NewReader(str + " 2")
   156  	s3 := strings.NewReader(str + " 3")
   157  	mr := MultiReadSeeker(s1, s2, s3)
   158  
   159  	mid := int64(s1.Len() + s2.Len()/2)
   160  
   161  	size, err := mr.Seek(mid, os.SEEK_SET)
   162  	if err != nil {
   163  		t.Fatal(err)
   164  	}
   165  	if size != mid {
   166  		t.Fatalf("reader size does not match, got %d, expected %d", size, mid)
   167  	}
   168  
   169  	size, err = mr.Seek(3, os.SEEK_CUR)
   170  	if err != nil {
   171  		t.Fatal(err)
   172  	}
   173  	if size != mid+3 {
   174  		t.Fatalf("reader size does not match, got %d, expected %d", size, mid+3)
   175  	}
   176  	size, err = mr.Seek(5, os.SEEK_CUR)
   177  	if err != nil {
   178  		t.Fatal(err)
   179  	}
   180  	if size != mid+8 {
   181  		t.Fatalf("reader size does not match, got %d, expected %d", size, mid+8)
   182  	}
   183  
   184  	size, err = mr.Seek(10, os.SEEK_CUR)
   185  	if err != nil {
   186  		t.Fatal(err)
   187  	}
   188  	if size != mid+18 {
   189  		t.Fatalf("reader size does not match, got %d, expected %d", size, mid+18)
   190  	}
   191  }
   192  
   193  func TestMultiReadSeekerSmallReads(t *testing.T) {
   194  	readers := []io.ReadSeeker{}
   195  	for i := 0; i < 10; i++ {
   196  		integer := make([]byte, 4, 4)
   197  		binary.BigEndian.PutUint32(integer, uint32(i))
   198  		readers = append(readers, bytes.NewReader(integer))
   199  	}
   200  
   201  	reader := MultiReadSeeker(readers...)
   202  	for i := 0; i < 10; i++ {
   203  		var integer uint32
   204  		if err := binary.Read(reader, binary.BigEndian, &integer); err != nil {
   205  			t.Fatalf("Read from NewMultiReadSeeker failed: %v", err)
   206  		}
   207  		if uint32(i) != integer {
   208  			t.Fatalf("Read wrong value from NewMultiReadSeeker: %d != %d", i, integer)
   209  		}
   210  	}
   211  }