aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/jackc/pgx/v5/pgproto3/chunkreader_test.go
blob: e1c2b0da7c9362b16c40668cbf6dad52e16727c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package pgproto3

import (
	"bytes"
	"math/rand"
	"testing"
)

func TestChunkReaderNextDoesNotReadIfAlreadyBuffered(t *testing.T) {
	server := &bytes.Buffer{}
	r := newChunkReader(server, 4)

	src := []byte{1, 2, 3, 4}
	server.Write(src)

	n1, err := r.Next(2)
	if err != nil {
		t.Fatal(err)
	}
	if bytes.Compare(n1, src[0:2]) != 0 {
		t.Fatalf("Expected read bytes to be %v, but they were %v", src[0:2], n1)
	}

	n2, err := r.Next(2)
	if err != nil {
		t.Fatal(err)
	}
	if bytes.Compare(n2, src[2:4]) != 0 {
		t.Fatalf("Expected read bytes to be %v, but they were %v", src[2:4], n2)
	}

	if bytes.Compare((*r.buf)[:len(src)], src) != 0 {
		t.Fatalf("Expected r.buf to be %v, but it was %v", src, r.buf)
	}

	_, err = r.Next(0) // Trigger the buffer reset.
	if err != nil {
		t.Fatal(err)
	}

	if r.rp != 0 {
		t.Fatalf("Expected r.rp to be %v, but it was %v", 0, r.rp)
	}
	if r.wp != 0 {
		t.Fatalf("Expected r.wp to be %v, but it was %v", 0, r.wp)
	}
}

type randomReader struct {
	rnd *rand.Rand
}

// Read reads a random number of random bytes.
func (r *randomReader) Read(p []byte) (n int, err error) {
	n = r.rnd.Intn(len(p) + 1)
	return r.rnd.Read(p[:n])
}

func TestChunkReaderNextFuzz(t *testing.T) {
	rr := &randomReader{rnd: rand.New(rand.NewSource(1))}
	r := newChunkReader(rr, 8192)

	randomSizes := rand.New(rand.NewSource(0))

	for i := 0; i < 100000; i++ {
		size := randomSizes.Intn(16384) + 1
		buf, err := r.Next(size)
		if err != nil {
			t.Fatal(err)
		}
		if len(buf) != size {
			t.Fatalf("Expected to get %v bytes but got %v bytes", size, len(buf))
		}
	}
}