test(loader): cover 8192-byte EOF boundary for LoadValues

Also drop the extra blank line before the test. Addresses review on
#32525.

Signed-off-by: Dean Chen <862469039@qq.com>
pull/32525/head
Dean Chen 1 month ago
parent 601445e88c
commit fd4ed4952b
No known key found for this signature in database
GPG Key ID: 03656C0AA9B7E279

@ -21,6 +21,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -419,27 +420,31 @@ foo:
} }
} }
func TestLoadValuesEOFBoundary(t *testing.T) { func TestLoadValuesEOFBoundary(t *testing.T) {
// Reproduces #32506: a single logical line whose length is a multiple of // Reproduces #32506: a single logical line whose length is a multiple of
// bufio's default buffer (4096) and has no trailing newline used to be // bufio's default buffer (4096) and has no trailing newline used to be
// dropped entirely by YAMLReader, yielding empty values. // dropped entirely by YAMLReader, yielding empty values.
prefix := []byte(`{"foo":"`) // Also cover 8192 (2x buffer) so we do not only hit the single-buffer case.
suffix := []byte(`"}`) for _, size := range []int{4096, 8192} {
pad := 4096 - len(prefix) - len(suffix) t.Run(fmt.Sprintf("size_%d", size), func(t *testing.T) {
data := make([]byte, 0, 4096) prefix := []byte(`{"foo":"`)
data = append(data, prefix...) suffix := []byte(`"}`)
data = append(data, bytes.Repeat([]byte("x"), pad)...) pad := size - len(prefix) - len(suffix)
data = append(data, suffix...) data := make([]byte, 0, size)
if len(data) != 4096 { data = append(data, prefix...)
t.Fatalf("test setup: want data length 4096, got %d", len(data)) data = append(data, bytes.Repeat([]byte("x"), pad)...)
} data = append(data, suffix...)
if len(data) != size {
t.Fatalf("test setup: want data length %d, got %d", size, len(data))
}
values, err := LoadValues(bytes.NewReader(data)) values, err := LoadValues(bytes.NewReader(data))
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, map[string]any{ assert.Equal(t, map[string]any{
"foo": string(bytes.Repeat([]byte("x"), pad)), "foo": string(bytes.Repeat([]byte("x"), pad)),
}, values) }, values)
})
}
} }
func TestMergeValuesV3(t *testing.T) { func TestMergeValuesV3(t *testing.T) {

@ -21,6 +21,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -463,27 +464,31 @@ foo:
} }
} }
func TestLoadValuesEOFBoundary(t *testing.T) { func TestLoadValuesEOFBoundary(t *testing.T) {
// Reproduces #32506: a single logical line whose length is a multiple of // Reproduces #32506: a single logical line whose length is a multiple of
// bufio's default buffer (4096) and has no trailing newline used to be // bufio's default buffer (4096) and has no trailing newline used to be
// dropped entirely by YAMLReader, yielding empty values. // dropped entirely by YAMLReader, yielding empty values.
prefix := []byte(`{"foo":"`) // Also cover 8192 (2x buffer) so we do not only hit the single-buffer case.
suffix := []byte(`"}`) for _, size := range []int{4096, 8192} {
pad := 4096 - len(prefix) - len(suffix) t.Run(fmt.Sprintf("size_%d", size), func(t *testing.T) {
data := make([]byte, 0, 4096) prefix := []byte(`{"foo":"`)
data = append(data, prefix...) suffix := []byte(`"}`)
data = append(data, bytes.Repeat([]byte("x"), pad)...) pad := size - len(prefix) - len(suffix)
data = append(data, suffix...) data := make([]byte, 0, size)
if len(data) != 4096 { data = append(data, prefix...)
t.Fatalf("test setup: want data length 4096, got %d", len(data)) data = append(data, bytes.Repeat([]byte("x"), pad)...)
} data = append(data, suffix...)
if len(data) != size {
t.Fatalf("test setup: want data length %d, got %d", size, len(data))
}
values, err := LoadValues(bytes.NewReader(data)) values, err := LoadValues(bytes.NewReader(data))
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, map[string]any{ assert.Equal(t, map[string]any{
"foo": string(bytes.Repeat([]byte("x"), pad)), "foo": string(bytes.Repeat([]byte("x"), pad)),
}, values) }, values)
})
}
} }
func TestMergeValuesV2(t *testing.T) { func TestMergeValuesV2(t *testing.T) {

Loading…
Cancel
Save