Exemplo n.º 1
0
func (t *ReadProxyTest) ContentsReturnReadError() {
	// NewFile
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	// Write
	ExpectCall(rwl, "Write")(Any()).
		WillRepeatedly(Invoke(successfulWrite))

	// Downgrade and Revoke
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))
	ExpectCall(rl, "Revoke")()

	// Function
	t.f = func() (rc io.ReadCloser, err error) {
		rc = ioutil.NopCloser(
			iotest.TimeoutReader(
				iotest.OneByteReader(
					strings.NewReader(contents))))

		return
	}

	// Attempt to read.
	_, err := t.proxy.ReadAt(context.Background(), []byte{}, 0)
	ExpectThat(err, Error(HasSubstr("Copy")))
	ExpectThat(err, Error(HasSubstr("timeout")))
}
Exemplo n.º 2
0
func TestOneByteReader(t *testing.T) {
	s := int(1e6)
	n, err := ReadZeros(iotest.OneByteReader(bytes.NewReader(make([]byte, s))))
	if err != nil || n != s {
		t.Errorf("expected %d (<nil>), got %d (%v)", s, n, err)
	}
}
Exemplo n.º 3
0
// testFileListByteReads tests the decoding of a list of files against
// their expected error and md5sum. It uses a one byte input buffer
// and one byte output buffer for each run of the decoder.
func testFileListByteReads(t *testing.T, files []testFile) {
	for _, f := range files {
		func() {
			fr, err := openTestFile(f.file)
			if err != nil {
				t.Fatal(err)
			}
			defer fr.Close()
			hash := md5.New()
			obr := iotest.OneByteReader(fr)
			r, err := xz.NewReader(obr, 0)
			if err == nil {
				b := make([]byte, 1)
				var n int
				for err == nil {
					n, err = r.Read(b)
					if n == 1 {
						_, _ = hash.Write(b)
					}
				}
				if err == io.EOF {
					err = nil
				}
			}
			if err != f.err {
				t.Fatalf("%s: wanted error: %v, got: %v\n", f.file, f.err, err)
			}
			md5sum := fmt.Sprintf("%x", hash.Sum(nil))
			if f.md5sum != md5sum {
				t.Fatalf(
					"%s: wanted md5: %v, got: %v\n", f.file, f.md5sum, md5sum)
			}
		}()
	}
}
Exemplo n.º 4
0
func TestSkipReaderCloseWithPipe(t *testing.T) {
	t.Parallel()
	var input = []byte{'a', 'b', 'c', 'd'}
	var output = []byte{'b', 'c', 'd'}
	r, w := io.Pipe()
	src, err := SkipReadCloser(r, 1)
	if err != nil {
		t.Fatal("unexpected error", err)
	}
	go func() {
		if _, err := w.Write(input); err != nil {
			t.Fatalf("Unexpected Write error: %s", err)
		}
		testutils.ShouldntFail(t, w.Close())
	}()
	defer func() {
		testutils.ShouldntFail(t, src.Close())
	}()

	var result bytes.Buffer
	if _, err := result.ReadFrom(iotest.OneByteReader(src)); err != nil {
		t.Fatalf("Unexpected ReadFrom error: %s", err)
	}
	if result.String() != string(output) {
		t.Fatalf("Expected to skipread [%s] read [%s]", output, result.String())
	}
}
Exemplo n.º 5
0
func (s *CollisionSuite) TestCollisionOrCorrupt(c *check.C) {
	fooMD5 := "acbd18db4cc2f85cedef654fccc4a4d8"

	c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, []byte{'o'}, bytes.NewBufferString("o")),
		check.Equals, CollisionError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, nil, bytes.NewBufferString("oo")),
		check.Equals, CollisionError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, []byte{'o', 'o'}, nil),
		check.Equals, CollisionError)
	c.Check(collisionOrCorrupt(fooMD5, nil, []byte{}, bytes.NewBufferString("foo")),
		check.Equals, CollisionError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o', 'o'}, nil, bytes.NewBufferString("")),
		check.Equals, CollisionError)
	c.Check(collisionOrCorrupt(fooMD5, nil, nil, iotest.NewReadLogger("foo: ", iotest.DataErrReader(iotest.OneByteReader(bytes.NewBufferString("foo"))))),
		check.Equals, CollisionError)

	c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o', 'o'}, nil, bytes.NewBufferString("bar")),
		check.Equals, DiskHashError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, nil, nil),
		check.Equals, DiskHashError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{}, nil, bytes.NewBufferString("")),
		check.Equals, DiskHashError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'O'}, nil, bytes.NewBufferString("o")),
		check.Equals, DiskHashError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'O', 'o'}, nil, nil),
		check.Equals, DiskHashError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, []byte{'O'}, nil),
		check.Equals, DiskHashError)
	c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, nil, bytes.NewBufferString("O")),
		check.Equals, DiskHashError)

	c.Check(collisionOrCorrupt(fooMD5, []byte{}, nil, iotest.TimeoutReader(iotest.OneByteReader(bytes.NewBufferString("foo")))),
		check.Equals, iotest.ErrTimeout)
}
func TestMediaErrHandling(t *testing.T) {
	handler := &myHandler{}
	server := httptest.NewServer(handler)
	defer server.Close()

	client := &http.Client{}
	s, err := storage.New(client)
	if err != nil {
		t.Fatalf("unable to create service: %v", err)
	}
	s.BasePath = server.URL

	const body = "fake media data"
	f := strings.NewReader(body)
	// The combination of TimeoutReader and OneByteReader causes the first byte to
	// be successfully delivered, but then a timeout error is reported.  This
	// allows us to test the goroutine within the getMediaType function.
	r := iotest.TimeoutReader(iotest.OneByteReader(f))
	o := &storage.Object{
		Bucket:          "mybucket",
		Name:            "filename",
		ContentType:     "plain/text",
		ContentEncoding: "utf-8",
		ContentLanguage: "en",
	}
	_, err = s.Objects.Insert("mybucket", o).Media(r).Do()
	if err == nil || !strings.Contains(err.Error(), "timeout") {
		t.Errorf("expected timeout error, got %v", err)
	}
	if handler.err != nil {
		t.Errorf("handler err = %v, want nil", handler.err)
	}
}
Exemplo n.º 7
0
func TestReadHeadersSuccessful(t *testing.T) {
	for _, tt := range headerTests {
		// when the bytes are {0, 0}, we always return nil.
		if tt.m != nil && len(tt.m) == 0 {
			continue
		}

		reader := iotest.OneByteReader(bytes.NewReader(tt.encoding))
		got, err := ReadHeaders(reader)
		assert.NoError(t, err, "ReadHeaders failed")
		assert.Equal(t, tt.m, got, "Map mismatch")

		if tt.encoding2 != nil {
			reader := iotest.OneByteReader(bytes.NewReader(tt.encoding2))
			got, err := ReadHeaders(reader)
			assert.NoError(t, err, "ReadHeaders failed")
			assert.Equal(t, tt.m, got, "Map mismatch")
		}
	}
}
Exemplo n.º 8
0
func TestReadDelimitedPrematureBodyIncremental(t *testing.T) {
	t.Parallel()
	var data = []byte{128, 5, 0, 0, 0} // 256 + 256 + 128
	n, err := ReadDelimited(iotest.OneByteReader(bytes.NewReader(data[:])), nil)
	if got, want := n, 5; got != want {
		t.Errorf("ReadDelimited(%#v, nil) = %#v, ?; want = %v#, ?", data, got, want)
	}
	if got, want := err, io.ErrUnexpectedEOF; got != want {
		t.Errorf("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v", data, got, want)
	}
}
Exemplo n.º 9
0
func (s *ReaderSuite) TestMultipleReads(c *C) {
	text := "SEND\ndestination:xxx\n\nPayload\x00\n" +
		"SEND\ndestination:yyy\ncontent-length:12\n" +
		"dodgy\\c\\n\\cheader:dodgy\\c\\n\\r\\nvalue\\  \\\n\n" +
		"123456789AB\x00\x00"

	ioreaders := []io.Reader{
		strings.NewReader(text),
		iotest.DataErrReader(strings.NewReader(text)),
		iotest.HalfReader(strings.NewReader(text)),
		iotest.OneByteReader(strings.NewReader(text)),
	}

	for _, ioreader := range ioreaders {
		// uncomment the following line to view the bytes being read
		//ioreader = iotest.NewReadLogger("RX", ioreader)
		reader := NewReader(ioreader)
		frame, err := reader.Read()
		c.Assert(err, IsNil)
		c.Assert(frame, NotNil)
		c.Assert(frame.Command, Equals, "SEND")
		c.Assert(frame.Header.Len(), Equals, 1)
		v := frame.Header.Get("destination")
		c.Assert(v, Equals, "xxx")
		c.Assert(string(frame.Body), Equals, "Payload")

		// now read a heart-beat from the input
		frame, err = reader.Read()
		c.Assert(err, IsNil)
		c.Assert(frame, IsNil)

		// this frame has content-length
		frame, err = reader.Read()
		c.Assert(err, IsNil)
		c.Assert(frame, NotNil)
		c.Assert(frame.Command, Equals, "SEND")
		c.Assert(frame.Header.Len(), Equals, 3)
		v = frame.Header.Get("destination")
		c.Assert(v, Equals, "yyy")
		n, ok, err := frame.Header.ContentLength()
		c.Assert(n, Equals, 12)
		c.Assert(ok, Equals, true)
		c.Assert(err, IsNil)
		k, v := frame.Header.GetAt(2)
		c.Assert(k, Equals, "dodgy:\n:header")
		c.Assert(v, Equals, "dodgy:\n\r\nvalue\\  \\")
		c.Assert(string(frame.Body), Equals, "123456789AB\x00")

		// ensure we are at the end of input
		frame, err = reader.Read()
		c.Assert(frame, IsNil)
		c.Assert(err, Equals, io.EOF)
	}
}
Exemplo n.º 10
0
func TestRecordReader(t *testing.T) {
	for i, test := range recordReaderTests {
		buf := bytes.NewBuffer(test.in)
		c := make(chan *record)
		go recordReader(c, buf)
		matchRecordReaderOutput(t, i, test, c)

		buf = bytes.NewBuffer(test.in)
		buf2 := iotest.OneByteReader(buf)
		c = make(chan *record)
		go recordReader(c, buf2)
		matchRecordReaderOutput(t, i*2, test, c)
	}
}
Exemplo n.º 11
0
func (t *RetryBucket_CreateObjectTest) ErrorReading() {
	var err error

	// Pass in a reader that will return an error.
	t.req.Contents = ioutil.NopCloser(
		iotest.OneByteReader(
			iotest.TimeoutReader(
				strings.NewReader("foobar"))))

	// Call
	err = t.call()

	ExpectThat(err, Error(HasSubstr("ReadAll")))
	ExpectThat(err, Error(HasSubstr("timeout")))
}
Exemplo n.º 12
0
func (t *RandomReaderTest) ReaderFails() {
	// Bucket
	r := iotest.OneByteReader(iotest.TimeoutReader(strings.NewReader("xxx")))
	rc := ioutil.NopCloser(r)

	ExpectCall(t.bucket, "NewReader")(Any(), Any()).
		WillOnce(Return(rc, nil))

	// Call
	buf := make([]byte, 3)
	_, err := t.rr.ReadAt(buf, 0)

	ExpectThat(err, Error(HasSubstr("readFull")))
	ExpectThat(err, Error(HasSubstr(iotest.ErrTimeout.Error())))
}
Exemplo n.º 13
0
func TestReadAll(t *testing.T) {
	orw := bytes.NewReader([]byte("tester"))
	rw := iotest.OneByteReader(orw)
	data := []byte("aaaaaa")

	if err := ReadAll(rw, data); err != nil {
		t.Fatal("unexpected error", err)
	}

	rw = iotest.DataErrReader(orw)

	if err := ReadAll(rw, data); err != io.EOF {
		t.Fatal("expected io.EOF")
	}

}
Exemplo n.º 14
0
func TestLLVMIRReadCloser(t *testing.T) {
	const input = `;abc\n;def\n;xyz`

	var buf bytes.Buffer
	buf.WriteString(input)
	r := build.NewLLVMIRReader(ioutil.NopCloser(&buf))
	b, err := ioutil.ReadAll(iotest.OneByteReader(r))
	if err != nil {
		t.Errorf("unexpected error: %s", err)
	}

	str := string(b)
	expected := strings.Replace(str, ";", "//", -1)
	if str != expected {
		t.Errorf("%q != %q", str, expected)
	}
}
Exemplo n.º 15
0
func TestReadByOneByte(t *testing.T) {
	r := New(NewSource(1))
	b1 := make([]byte, 100)
	_, err := io.ReadFull(iotest.OneByteReader(r), b1)
	if err != nil {
		t.Errorf("read by one byte: %v", err)
	}
	r = New(NewSource(1))
	b2 := make([]byte, 100)
	_, err = r.Read(b2)
	if err != nil {
		t.Errorf("read: %v", err)
	}
	if !bytes.Equal(b1, b2) {
		t.Errorf("read by one byte vs single read:\n%x\n%x", b1, b2)
	}
}
Exemplo n.º 16
0
func TestReadRequestChunks(t *testing.T) {
	var tests = []struct {
		buf     []byte
		packets []*packetDATA // DATA packets we expect to receive.
	}{
		{
			// Empty last packet.
			buf: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
			packets: []*packetDATA{
				&packetDATA{blockNr: 1, data: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7}},
				&packetDATA{blockNr: 2, data: []byte{0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}},
				&packetDATA{blockNr: 3, data: []byte{}},
			},
		},
		{
			// Partial last packet.
			buf: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe},
			packets: []*packetDATA{
				&packetDATA{blockNr: 1, data: []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7}},
				&packetDATA{blockNr: 2, data: []byte{0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe}},
			},
		},
	}

	for _, test := range tests {
		h := newHandlerContext()
		h.SetReadCloser(&rcBuffer{iotest.OneByteReader(bytes.NewBuffer(test.buf))})
		h.Negotiate(t, map[string]string{"blksize": "8"})

		for _, expected := range test.packets {
			pdata := <-h.rcv
			assert.IsType(t, &packetDATA{}, pdata)

			actual := pdata.(*packetDATA)
			assert.Equal(t, expected, actual)
			h.snd <- &packetACK{blockNr: actual.blockNr}
		}

		// There should not be any more packets.
		p, ok := <-h.rcv
		assert.False(t, ok)
		assert.Nil(t, p)
	}
}
Exemplo n.º 17
0
func TestCountReaderOneByte(t *testing.T) {
	c := fstestutil.CountReader{}
	data := make([]byte, 3*chunkSize+5)
	r := iotest.OneByteReader(&c)
	n, err := io.ReadFull(r, data)
	if err != nil {
		t.Fatalf("got error from read: %v", err)
	}
	if g, e := n, len(data); g != e {
		t.Errorf("wrong read length: %v != %v", g, e)
	}
	want := strings.Join([]string{
		strings.Repeat("00", chunkSize-8), "0000000000000000",
		strings.Repeat("00", chunkSize-8), "0000000000000001",
		strings.Repeat("00", chunkSize-8), "0000000000000002",
		"0000000000",
	}, "")
	if g, e := hex.EncodeToString(data), want; g != e {
		t.Errorf("wrong data: %v != %v", g, e)
	}
}
Exemplo n.º 18
0
func (*multiReaderSeekerSuite) TestSeekAfterRead(c *gc.C) {
	parts := []string{
		"one",
		"two",
		"three",
		"four",
	}
	all := strings.Join(parts, "")
	r := newMultiStringReader(parts)
	data, err := ioutil.ReadAll(iotest.OneByteReader(r))
	c.Assert(err, gc.IsNil)
	c.Assert(string(data), gc.Equals, all)

	off, err := r.Seek(-8, 2)
	c.Assert(err, gc.IsNil)
	c.Assert(off, gc.Equals, int64(len(all)-8))

	data, err = ioutil.ReadAll(r)
	c.Assert(err, gc.IsNil)
	c.Assert(string(data), gc.Equals, "hreefour")
}
Exemplo n.º 19
0
func TestPartialRead(t *testing.T) {
	f := NewFrame(MaxFramePayloadSize)
	f.Header.size = FrameHeaderSize + 2134
	f.Header.messageType = messageTypeCallReq
	f.Header.ID = 0xDEADBEED

	// We set the full payload but only the first 2134 bytes should be written.
	for i := 0; i < len(f.Payload); i++ {
		val := (i * 37) % 256
		f.Payload[i] = byte(val)
	}
	buf := &bytes.Buffer{}
	require.NoError(t, f.WriteOut(buf))
	assert.Equal(t, f.Header.size, uint16(buf.Len()), "frame size should match written bytes")

	// Read the data back, from a reader that fragments.
	f2 := NewFrame(MaxFramePayloadSize)
	require.NoError(t, f2.ReadIn(iotest.OneByteReader(buf)))

	// Ensure header and payload are the same.
	require.Equal(t, f.Header, f2.Header, "frame headers don't match")
	require.Equal(t, f.SizedPayload(), f2.SizedPayload(), "payload does not match")
}
Exemplo n.º 20
0
func TestOneByteReader(t *testing.T) {
	const (
		data      = "Hello, World!"
		cacheSize = 5
	)

	b := bytes.NewBufferString(data)
	r := newReader(iotest.OneByteReader(b))
	r.CacheFull(cacheSize)
	if !r.Check(0, data[0:cacheSize]) {
		t.Error("Caching failed")
	}
	result := make([]byte, len(data))
	nRead, err := io.ReadFull(r, result)
	if nRead != len(data) {
		t.Error("Full read failed")
	}
	if string(result) != data {
		t.Errorf("Read corrupted. (wanted %#v, got %#v)", data, string(result))
	}
	if err != nil {
		t.Error(err)
	}
}
Exemplo n.º 21
0
func TestChunking(t *testing.T) {
	type testCase struct {
		data       string // the data to read from the Reader
		finalErr   error  // error to return after data has been read
		chunkSize  int
		wantChunks []string
	}

	for _, singleByteReads := range []bool{true, false} {
		for _, tc := range []testCase{
			{
				data:       "abcdefg",
				finalErr:   nil,
				chunkSize:  3,
				wantChunks: []string{"abc", "def", "g"},
			},
			{
				data:       "abcdefg",
				finalErr:   nil,
				chunkSize:  1,
				wantChunks: []string{"a", "b", "c", "d", "e", "f", "g"},
			},
			{
				data:       "abcdefg",
				finalErr:   nil,
				chunkSize:  7,
				wantChunks: []string{"abcdefg"},
			},
			{
				data:       "abcdefg",
				finalErr:   nil,
				chunkSize:  8,
				wantChunks: []string{"abcdefg"},
			},
			{
				data:       "abcdefg",
				finalErr:   io.ErrUnexpectedEOF,
				chunkSize:  3,
				wantChunks: []string{"abc", "def", "g"},
			},
			{
				data:       "abcdefg",
				finalErr:   io.ErrUnexpectedEOF,
				chunkSize:  8,
				wantChunks: []string{"abcdefg"},
			},
		} {
			var r io.Reader = &errReader{buf: []byte(tc.data), err: tc.finalErr}

			if singleByteReads {
				r = iotest.OneByteReader(r)
			}

			rb := NewResumableBuffer(r, tc.chunkSize)
			var gotErr error
			got := []string{}
			for {
				chunk, err := getChunkAsString(t, rb)
				if len(chunk) != 0 {
					got = append(got, string(chunk))
				}
				if err != nil {
					gotErr = err
					break
				}
				rb.Next()
			}

			if !reflect.DeepEqual(got, tc.wantChunks) {
				t.Errorf("Failed reading buffer: got: %v; want:%v", got, tc.wantChunks)
			}

			expectedErr := tc.finalErr
			if expectedErr == nil {
				expectedErr = io.EOF
			}
			if gotErr != expectedErr {
				t.Errorf("Reading buffer error: got: %v; want: %v", gotErr, expectedErr)
			}
		}
	}
}
Exemplo n.º 22
0
func (s *SSHStreamSuite) TestStripCROneByte(c *gc.C) {
	reader := ssh.StripCRReader(strings.NewReader("One\r\r\rTwo"))
	output, err := ioutil.ReadAll(iotest.OneByteReader(reader))
	c.Assert(err, jc.ErrorIsNil)
	c.Check(string(output), gc.Equals, "OneTwo")
}
Exemplo n.º 23
0
func (c *mockConn) Read(buf []byte) (n int, err error) {
	return iotest.OneByteReader(&c.buf).Read(buf)
}
Exemplo n.º 24
0
	// Fixed bugs
	{"%v%v", "FALSE23", args(&truth, &i), args(false, 23), ""},
}

var readers = []struct {
	name string
	f    func(string) io.Reader
}{
	{"StringReader", func(s string) io.Reader {
		return strings.NewReader(s)
	}},
	{"ReaderOnly", func(s string) io.Reader {
		return struct{ io.Reader }{strings.NewReader(s)}
	}},
	{"OneByteReader", func(s string) io.Reader {
		return iotest.OneByteReader(strings.NewReader(s))
	}},
	{"DataErrReader", func(s string) io.Reader {
		return iotest.DataErrReader(strings.NewReader(s))
	}},
}

func testScan(t *testing.T, f func(string) io.Reader, scan func(r io.Reader, a ...interface{}) (int, error)) {
	for _, test := range scanTests {
		r := f(test.text)
		n, err := scan(r, test.in)
		if err != nil {
			m := ""
			if n > 0 {
				m = Sprintf(" (%d fields ok)", n)
			}