Exemplo n.º 1
0
func TestWriteAfterWriterClose(t *testing.T) {
	r, w := New()

	s := "hello"

	errs := make(chan error)

	go func() {
		n, err := w.Write([]byte(s))
		assert.MustNoError(err)
		assert.Must(n == len(s))
		assert.MustNoError(w.Close())
		_, err = w.Write([]byte("world"))
		errs <- err
	}()

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

	err = <-errs
	assert.Must(errors.Equal(err, io.ErrClosedPipe))
	assert.MustNoError(r.Close())
}
Exemplo n.º 2
0
func TestBytesizeError(t *testing.T) {
	var err error
	_, err = Parse("--1")
	assert.Must(errors.Equal(err, ErrBadBytesize))
	_, err = Parse("hello world")
	assert.Must(errors.Equal(err, ErrBadBytesize))
	_, err = Parse("123.132.32")
	assert.Must(errors.Equal(err, ErrBadBytesize))
}
Exemplo n.º 3
0
func testPipe2(t *testing.T, fileName string) {
	r, w, f := openPipe(t, fileName)
	defer f.Close()

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

	go func() {
		for i := 0; i < c; i++ {
			m := fmt.Sprintf("[%d]%s ", i, s)
			n, err := w.Write([]byte(m))
			assert.MustNoError(err)
			assert.Must(n == len(m))
		}
		assert.MustNoError(w.Close())
	}()

	time.Sleep(time.Millisecond * 10)

	buf := make([]byte, len(s)*c*2)
	n, err := io.ReadFull(r, buf)
	assert.Must(errors.Equal(err, io.EOF))
	buf = buf[:n]
	for i := 0; i < c; i++ {
		m := fmt.Sprintf("[%d]%s ", i, s)
		assert.Must(len(buf) >= len(m))
		assert.Must(string(buf[:len(m)]) == m)
		buf = buf[len(m):]
	}
	assert.Must(len(buf) == 0)
	assert.MustNoError(r.Close())
}
Exemplo n.º 4
0
func testBacklog(t *testing.T, bl *Backlog, size int) {
	input := randSlice(32)

	r1, err := bl.NewReader()
	assert.MustNoError(err)

	checkWriter(bl, input)
	checkReader(r1, input)
	checkReader(r1, []byte{})

	input = randSlice(size)
	checkWriter(bl, input)
	checkReader(r1, input)
	checkWriter(bl, randSlice(size))

	assert.Must(r1.IsValid() == true)

	r2, err := bl.NewReader()
	assert.MustNoError(err)

	input = []byte{0xde, 0xad, 0xbe, 0xef}
	checkWriter(bl, input)

	assert.Must(r1.IsValid() == false)

	_, err = r1.Read([]byte{0})
	assert.Must(errors.Equal(err, ErrInvalidOffset))

	b := make([]byte, len(input))
	n, err := io.ReadFull(r2, b)
	assert.MustNoError(err)
	assert.Must(n == len(b) && bytes.Equal(b, input))

	bl.Close()

	assert.Must(r1.IsValid() == false)
	assert.Must(r2.IsValid() == false)

	_, err = r1.Read([]byte{0})
	assert.Must(errors.Equal(err, ErrClosedBacklog))

	_, err = r2.Read([]byte{0})
	assert.Must(errors.Equal(err, ErrClosedBacklog))

	_, err = bl.Write([]byte{0})
	assert.Must(errors.Equal(err, ErrClosedBacklog))
}
Exemplo n.º 5
0
func TestPipeReadClose2(t *testing.T) {
	r, w := New()
	c := make(chan int, 1)

	go delayClose(t, r, c, pipeTest{})

	n, err := r.Read(make([]byte, 64))
	<-c

	assert.Must(errors.Equal(err, io.ErrClosedPipe))
	assert.Must(n == 0)
	assert.MustNoError(w.Close())
}
Exemplo n.º 6
0
func testPipe3(t *testing.T, fileName string) {
	r, w, f := openPipe(t, fileName)
	defer f.Close()

	c := make(chan int)

	size := 4096

	go func() {
		buf := make([]byte, size)
		for {
			n, err := r.Read(buf)
			if errors.Equal(err, io.EOF) {
				break
			}
			assert.MustNoError(err)
			c <- n
		}
		assert.MustNoError(r.Close())
		c <- 0
	}()

	go func() {
		buf := make([]byte, size)
		for i := 1; i < size; i++ {
			n, err := w.Write(buf[:i])
			assert.MustNoError(err)
			assert.Must(n == i)
		}
		assert.MustNoError(w.Close())
	}()

	sum := 0
	for i := 1; i < size; i++ {
		sum += i
	}
	for {
		n := <-c
		if n == 0 {
			break
		}
		sum -= n
	}
	assert.Must(sum == 0)
}
Exemplo n.º 7
0
func TestWriteNil(t *testing.T) {
	r, w := New()

	go func() {
		n, err := w.Write(nil)
		assert.MustNoError(err)
		assert.Must(n == 0)
		assert.MustNoError(w.Close())
	}()

	time.Sleep(time.Millisecond * 10)

	buf := make([]byte, 4096)
	n, err := io.ReadFull(r, buf)
	assert.Must(errors.Equal(err, io.EOF))
	assert.Must(n == 0)
	assert.MustNoError(r.Close())
}
Exemplo n.º 8
0
func testPipe4(t *testing.T, fileName string) {
	r, w, f := openPipe(t, fileName)
	defer f.Close()

	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.MustNoError(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 := w.Write(buf)
		assert.MustNoError(err)
		assert.MustNoError(w.Close())
		assert.Must(n == len(buf))
	}()

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

	n, err := io.ReadFull(r, buf)
	assert.MustNoError(err)
	assert.Must(n == len(buf))

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

	for i := 0; i < len(buf); i++ {
		assert.Must(buf[i] == byte(i))
	}
	_, err = io.ReadFull(r, buf)
	assert.Must(errors.Equal(err, io.EOF))
	assert.MustNoError(r.Close())
}
Exemplo n.º 9
0
func testPipe1(t *testing.T, fileName string) {
	r, w, f := openPipe(t, fileName)
	defer f.Close()

	s := "Hello world!!"

	go func(data []byte) {
		n, err := w.Write(data)
		assert.MustNoError(err)
		assert.Must(n == len(data))
		assert.MustNoError(w.Close())
	}([]byte(s))

	buf := make([]byte, 64)
	n, err := io.ReadFull(r, buf)
	assert.Must(errors.Equal(err, io.EOF))
	assert.Must(n == len(s))
	assert.Must(string(buf[:n]) == s)
	assert.MustNoError(r.Close())
}
Exemplo n.º 10
0
func TestPipeWriteClose(t *testing.T) {
	for _, u := range pipeTests {
		r, w := New()
		c := make(chan int, 1)

		if u.async {
			go delayClose(t, r, c, u)
		} else {
			delayClose(t, r, c, u)
		}
		<-c

		n, err := w.Write([]byte("hello, world"))
		expect := u.err
		if expect == nil {
			expect = io.ErrClosedPipe
		}
		assert.Must(errors.Equal(err, expect))
		assert.Must(n == 0)
		assert.MustNoError(w.Close())
	}
}
Exemplo n.º 11
0
func TestPipeReadClose(t *testing.T) {
	for _, u := range pipeTests {
		r, w := New()
		c := make(chan int, 1)

		if u.async {
			go delayClose(t, w, c, u)
		} else {
			delayClose(t, w, c, u)
		}

		buf := make([]byte, 64)
		n, err := r.Read(buf)
		<-c

		expect := u.err
		if expect == nil {
			expect = io.EOF
		}
		assert.Must(errors.Equal(err, expect))
		assert.Must(n == 0)
		assert.MustNoError(r.Close())
	}
}