Exemplo n.º 1
0
func testPipe2(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	c := 1024 * 128
	s := "Hello world!!"

	go func() {
		for i := 0; i < c; i++ {
			m := fmt.Sprintf("[%d]%s ", i, s)
			_, err := ioutils.WriteFull(w, []byte(m))
			assert.ErrorIsNil(t, err)
		}
		assert.ErrorIsNil(t, w.Close())
	}()

	time.Sleep(time.Millisecond * 100)

	buf := make([]byte, len(s)*c*2)
	n, err := ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	buf = buf[:n]
	for i := 0; i < c; i++ {
		m := fmt.Sprintf("[%d]%s ", i, s)
		assert.Must(t, len(buf) >= len(m))
		assert.Must(t, string(buf[:len(m)]) == m)
		buf = buf[len(m):]
	}
	assert.Must(t, len(buf) == 0)
	assert.ErrorIsNil(t, r.Close())
}
Exemplo n.º 2
0
func (s *testIoPipeSuite) testPipe2(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	cc := 1024 * 128
	ss := "Hello world!!"

	go func() {
		for i := 0; i < cc; i++ {
			m := fmt.Sprintf("[%d]%s ", i, ss)
			_, err := ioutils.WriteFull(w, []byte(m))
			c.Assert(err, IsNil)
		}
		c.Assert(w.Close(), IsNil)
	}()

	time.Sleep(time.Millisecond * 100)

	buf := make([]byte, len(ss)*cc*2)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)

	buf = buf[:n]
	for i := 0; i < cc; i++ {
		m := fmt.Sprintf("[%d]%s ", i, ss)
		c.Assert(len(buf) >= len(m), Equals, true)
		c.Assert(string(buf[:len(m)]), Equals, m)
		buf = buf[len(m):]
	}

	c.Assert(len(buf), Equals, 0)
	c.Assert(r.Close(), IsNil)
}
Exemplo n.º 3
0
func (s *testIoPipeSuite) TestWriteAfterWriterClose(c *C) {
	r, w := Pipe()

	ss := "hello"

	errs := make(chan error)

	go func() {
		_, err := ioutils.WriteFull(w, []byte(ss))
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)

		_, err = w.Write([]byte("world"))
		errs <- err
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(string(buf[:n]), Equals, ss)

	err = <-errs
	c.Assert(errors2.ErrorEqual(err, io.ErrClosedPipe), Equals, true)
	c.Assert(r.Close(), IsNil)
}
Exemplo n.º 4
0
func (s *testIoPipeSuite) testPipe4(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	key := []byte("spinlock aes-128")

	block := aes.BlockSize
	count := 1024 * 1024 * 128 / block

	go func() {
		buf := make([]byte, count*block)
		m, err := aes.NewCipher(key)
		c.Assert(err, IsNil)

		for i := 0; i < len(buf); i++ {
			buf[i] = byte(i)
		}

		e := cipher.NewCBCEncrypter(m, make([]byte, block))
		e.CryptBlocks(buf, buf)

		n, err := ioutils.WriteFull(w, buf)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
		c.Assert(n, Equals, len(buf))
	}()

	buf := make([]byte, count*block)
	m, err := aes.NewCipher(key)
	c.Assert(err, IsNil)

	_, err = ioutils.ReadFull(r, buf)
	c.Assert(err, IsNil)

	e := cipher.NewCBCDecrypter(m, make([]byte, block))
	e.CryptBlocks(buf, buf)

	for i := 0; i < len(buf); i++ {
		// make gocheck faster
		if buf[i] != byte(i) {
			c.Assert(buf[i], Equals, byte(i))
		}
	}

	_, err = ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(r.Close(), IsNil)
}
Exemplo n.º 5
0
func testPipe4(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	key := []byte("spinlock aes-128")

	block := aes.BlockSize
	count := 1024 * 1024 * 128 / block

	go func() {
		buf := make([]byte, count*block)
		m, err := aes.NewCipher(key)
		assert.ErrorIsNil(t, err)
		for i := 0; i < len(buf); i++ {
			buf[i] = byte(i)
		}

		e := cipher.NewCBCEncrypter(m, make([]byte, block))
		e.CryptBlocks(buf, buf)

		n, err := ioutils.WriteFull(w, buf)
		assert.ErrorIsNil(t, err)
		assert.ErrorIsNil(t, w.Close())
		assert.Must(t, n == len(buf))
	}()

	buf := make([]byte, count*block)
	m, err := aes.NewCipher(key)
	assert.ErrorIsNil(t, err)

	_, err = ioutils.ReadFull(r, buf)
	assert.ErrorIsNil(t, err)

	e := cipher.NewCBCDecrypter(m, make([]byte, block))
	e.CryptBlocks(buf, buf)

	for i := 0; i < len(buf); i++ {
		assert.Must(t, buf[i] == byte(i))
	}
	_, err = ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	assert.ErrorIsNil(t, r.Close())
}
Exemplo n.º 6
0
func TestWriteNil(t *testing.T) {
	r, w := Pipe()

	go func() {
		_, err := w.Write(nil)
		assert.ErrorIsNil(t, err)
		assert.ErrorIsNil(t, w.Close())
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	assert.Must(t, n == 0)
	assert.ErrorIsNil(t, r.Close())
}
Exemplo n.º 7
0
func (s *testIoPipeSuite) TestWriteNil(c *C) {
	r, w := Pipe()

	go func() {
		_, err := w.Write(nil)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(n, Equals, 0)
	c.Assert(r.Close(), IsNil)
}
Exemplo n.º 8
0
func testPipe1(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	s := "Hello world!!"

	go func(data []byte) {
		_, err := ioutils.WriteFull(w, data)
		assert.ErrorIsNil(t, err)
		assert.ErrorIsNil(t, w.Close())
	}([]byte(s))

	buf := make([]byte, 64)
	n, err := ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	assert.Must(t, n == len(s))
	assert.Must(t, string(buf[:n]) == s)
	assert.ErrorIsNil(t, r.Close())
}
Exemplo n.º 9
0
func (s *testIoPipeSuite) testPipe1(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	ss := "Hello world!!"

	go func(data []byte) {
		_, err := ioutils.WriteFull(w, data)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
	}([]byte(ss))

	buf := make([]byte, 64)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(n, Equals, len(ss))
	c.Assert(string(buf[:n]), Equals, ss)
	c.Assert(r.Close(), IsNil)
}
Exemplo n.º 10
0
func (d *decoder) decodeBulkBytes() ([]byte, error) {
	n, err := d.decodeInt()
	if err != nil {
		return nil, err
	}
	if n < -1 {
		return nil, errors.Trace(ErrBadRespBytesLen)
	} else if n == -1 {
		return nil, nil
	}
	b := make([]byte, n+2)
	if _, err := ioutils.ReadFull(d.r, b); err != nil {
		return nil, errors.Trace(err)
	}
	if b[n] != '\r' || b[n+1] != '\n' {
		return nil, errors.Trace(ErrBadRespEnd)
	}
	return b[:n], nil
}
Exemplo n.º 11
0
func TestWriteAfterWriterClose(t *testing.T) {
	r, w := Pipe()

	s := "hello"

	errs := make(chan error)

	go func() {
		_, err := ioutils.WriteFull(w, []byte(s))
		assert.ErrorIsNil(t, err)
		assert.ErrorIsNil(t, w.Close())
		_, err = w.Write([]byte("world"))
		errs <- err
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	assert.Must(t, string(buf[:n]) == s)

	err = <-errs
	assert.Must(t, errors.Equal(err, io.ErrClosedPipe))
	assert.ErrorIsNil(t, r.Close())
}
Exemplo n.º 12
0
Arquivo: rowbuf.go Projeto: CowLeo/qdb
func (r *BufReader) ReadBytes(n int) ([]byte, error) {
	p := make([]byte, n)
	_, err := ioutils.ReadFull(r.r, p)
	return p, errors.Trace(err)
}
Exemplo n.º 13
0
Arquivo: reader.go Projeto: CowLeo/qdb
func (r *rdbReader) readFull(p []byte) error {
	_, err := ioutils.ReadFull(r, p)
	return err
}
Exemplo n.º 14
0
Arquivo: reader.go Projeto: CowLeo/qdb
func (r *rdbReader) readByte() (byte, error) {
	b := r.buf[:1]
	_, err := ioutils.ReadFull(r, b)
	return b[0], err
}