Exemple #1
0
func TestBytesizeError(t *testing.T) {
	var err error
	_, err = Parse("--1")
	assert.Must(t, errors.Equal(err, ErrBadBytesize))
	_, err = Parse("hello world")
	assert.Must(t, errors.Equal(err, ErrBadBytesize))
	_, err = Parse("123.132.32")
	assert.Must(t, errors.Equal(err, ErrBadBytesize))
}
Exemple #2
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())
}
Exemple #3
0
func TestPipeReadClose2(t *testing.T) {
	r, w := Pipe()
	c := make(chan int, 1)

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

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

	assert.Must(t, errors.Equal(err, io.ErrClosedPipe))
	assert.Must(t, n == 0)
	assert.ErrorIsNil(t, w.Close())
}
Exemple #4
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())
}
Exemple #5
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())
}
Exemple #6
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())
}
Exemple #7
0
func testPipe3(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	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.ErrorIsNil(t, err)
			c <- n
		}
		assert.ErrorIsNil(t, r.Close())
		c <- 0
	}()

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

	sum := 0
	for i := 1; i < size; i++ {
		sum += i
	}
	for {
		n := <-c
		if n == 0 {
			break
		}
		sum -= n
	}
	assert.Must(t, sum == 0)
}
Exemple #8
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())
}
Exemple #9
0
func TestPipeWriteClose(t *testing.T) {
	for _, u := range pipeTests {
		r, w := Pipe()
		c := make(chan int, 1)

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

		n, err := ioutils.WriteFull(w, []byte("hello, world"))
		expect := u.err
		if expect == nil {
			expect = io.ErrClosedPipe
		}
		assert.Must(t, errors.Equal(err, expect))
		assert.Must(t, n == 0)
		assert.ErrorIsNil(t, w.Close())
	}
}
Exemple #10
0
func TestPipeReadClose(t *testing.T) {
	for _, u := range pipeTests {
		r, w := Pipe()
		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(t, errors.Equal(err, expect))
		assert.Must(t, n == 0)
		assert.ErrorIsNil(t, r.Close())
	}
}