File: iolimits_test.go

package info (click to toggle)
golang-github-containers-image 5.36.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,152 kB
  • sloc: sh: 267; makefile: 100
file content (37 lines) | stat: -rw-r--r-- 782 bytes parent folder | download | duplicates (4)
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
package iolimits

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

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestReadAtMost(t *testing.T) {
	rng := rand.New(rand.NewSource(0))
	for _, c := range []struct {
		input, limit  int
		shouldSucceed bool
	}{
		{0, 0, true},
		{0, 1, true},
		{1, 0, false},
		{1, 1, true},
		{bytes.MinRead*5 - 1, bytes.MinRead * 5, true},
		{bytes.MinRead * 5, bytes.MinRead * 5, true},
		{bytes.MinRead*5 + 1, bytes.MinRead * 5, false},
	} {
		input := make([]byte, c.input)
		_, err := rng.Read(input)
		require.NoError(t, err)
		result, err := ReadAtMost(bytes.NewReader(input), c.limit)
		if c.shouldSucceed {
			assert.NoError(t, err)
			assert.Equal(t, result, input)
		} else {
			assert.Error(t, err)
		}
	}
}