summaryrefslogtreecommitdiff
path: root/network/limited_reader_slurper_test.go
blob: 45b3fc72795df8a9a64e053fd2bd9a9b45c50d17 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package network

import (
	"bytes"
	"fmt"
	"io"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/test/partitiontest"
)

func TestLimitedReaderSlurper(t *testing.T) {
	partitiontest.PartitionTest(t)

	for _, arraySize := range []uint64{30000, 90000, 200000} {
		// create a random bytes array.
		bytesBlob := make([]byte, arraySize)
		crypto.RandBytes(bytesBlob[:])
		for baseBufferSize := uint64(0); baseBufferSize < uint64(len(bytesBlob)); baseBufferSize += 731 {
			for _, maxSize := range []uint64{arraySize - 10000, arraySize, arraySize + 10000} {
				buffer := bytes.NewBuffer(bytesBlob)
				reader := MakeLimitedReaderSlurper(baseBufferSize, maxSize)
				err := reader.Read(buffer)
				if maxSize < uint64(len(bytesBlob)) {
					require.Equal(t, ErrIncomingMsgTooLarge, err)
					continue
				}

				require.NoError(t, err)
				bytes := reader.Bytes()
				require.Equal(t, bytesBlob, bytes)
			}
		}
	}
}

type fuzzReader struct {
	pos int
	buf []byte
}

func (f *fuzzReader) Read(b []byte) (n int, err error) {
	s := int(crypto.RandUint64() % 19)
	if s > len(b) {
		s = len(b)
	}
	if f.pos >= len(f.buf) {
		return 0, io.EOF
	}
	if f.pos+s >= len(f.buf) {
		// we want a chunk that ends at ( or after ) the end of the data.
		n = len(f.buf) - f.pos
		err = io.EOF
	} else {
		n = s
	}
	copy(b, f.buf[f.pos:f.pos+n])
	f.pos += n
	return
}

func TestLimitedReaderSlurper_FuzzedBlippedSource(t *testing.T) {
	partitiontest.PartitionTest(t)

	arraySize := uint64(300000)
	bytesBlob := make([]byte, arraySize)
	crypto.RandBytes(bytesBlob[:])
	for i := 0; i < 500; i++ {
		for _, maxSize := range []uint64{arraySize - 10000, arraySize, arraySize + 10000} {
			reader := MakeLimitedReaderSlurper(512, maxSize)
			err := reader.Read(&fuzzReader{buf: bytesBlob})
			if maxSize < uint64(len(bytesBlob)) {
				require.Equal(t, ErrIncomingMsgTooLarge, err, "i: %d\nmaxSize: %d", i, maxSize)
				continue
			}
			require.NoError(t, err)
			bytes := reader.Bytes()
			require.Equal(t, bytesBlob, bytes)
		}
	}
}

func benchmarkLimitedReaderSlurper(b *testing.B, arraySize uint64) {
	bytesBlob := make([]byte, arraySize)
	crypto.RandBytes(bytesBlob[:])
	readers := make([]*LimitedReaderSlurper, b.N)
	buffers := make([]*bytes.Buffer, b.N)
	for i := 0; i < b.N; i++ {
		buffers[i] = bytes.NewBuffer(bytesBlob)
		readers[i] = MakeLimitedReaderSlurper(1024, 1024*1024)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		reader := readers[i]
		err := reader.Read(buffers[i])
		require.NoError(b, err)
		reader.Bytes()
		reader.Reset(0)
	}
}
func BenchmarkLimitedReaderSlurper(b *testing.B) {
	for _, arraySize := range []uint64{200, 2048, 300000} {
		b.Run(fmt.Sprintf("%dbytes_message", arraySize), func(b *testing.B) {
			benchmarkLimitedReaderSlurper(b, arraySize)
		})
	}
}

func TestLimitedReaderSlurperMemoryConsumption(t *testing.T) {
	partitiontest.PartitionTest(t)

	for _, arraySize := range []uint64{1024, 2048, 65536, 1024 * 1024} {
		result := testing.Benchmark(func(b *testing.B) {
			benchmarkLimitedReaderSlurper(b, arraySize)
		})
		require.True(t, uint64(result.AllocedBytesPerOp()) < 2*arraySize+allocationStep, "AllocedBytesPerOp:%d\nmessage size:%d", result.AllocedBytesPerOp(), arraySize)
	}
}

func TestLimitedReaderSlurperBufferAllocations(t *testing.T) {
	partitiontest.PartitionTest(t)

	for baseAllocation := uint64(512); baseAllocation < 100000; baseAllocation += 2048 {
		for maxAllocation := uint64(512); maxAllocation < 100000; maxAllocation += 512 {
			lrs := MakeLimitedReaderSlurper(baseAllocation, maxAllocation)
			// check to see if the allocated buffers count is exactly what needed to match the allocation needs.
			allocationNeeds := 1
			remainingBytes := int64(maxAllocation - baseAllocation)
			for remainingBytes > 0 {
				allocationNeeds++
				remainingBytes -= int64(allocationStep)
			}
			require.Equal(t, allocationNeeds, len(lrs.buffers))

		}
	}
}

func TestLimitedReaderSlurperPerMessageMaxSize(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	type randMode int

	const (
		modeLessThan randMode = iota
		modeEqual
		modeGreaterThan
	)

	maxMessageSize := 1024
	slurper := MakeLimitedReaderSlurper(512, uint64(maxMessageSize))
	for i := 0; i < 30; i++ {
		var b []byte
		randPick := randMode(crypto.RandUint64() % uint64(3))
		currentSize := crypto.RandUint64()%uint64(maxMessageSize) + 1
		slurper.Reset(currentSize)
		if randPick == modeLessThan {
			dataSize := crypto.RandUint64() % currentSize
			b = make([]byte, dataSize)
			crypto.RandBytes(b[:])
			err := slurper.Read(bytes.NewBuffer(b))
			require.NoError(t, err)
			require.Len(t, slurper.Bytes(), int(dataSize))
		} else if randPick == modeEqual {
			dataSize := currentSize
			b = make([]byte, dataSize)
			crypto.RandBytes(b[:])
			err := slurper.Read(bytes.NewBuffer(b))
			require.NoError(t, err)
			require.Len(t, slurper.Bytes(), int(currentSize))
		} else if randPick == modeGreaterThan {
			dataSize := currentSize + 1
			b = make([]byte, dataSize)
			crypto.RandBytes(b[:])
			err := slurper.Read(bytes.NewBuffer(b))
			require.Error(t, err)
		}
	}
}