2019-08-19 13:16:10 -07:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-19 13:16:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockedFlusher struct {
|
|
|
|
flushed int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockedFlusher) Flush() {
|
|
|
|
f.flushed++
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChunkedReaderCanReadFromChunkedWriter(t *testing.T) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
f := &mockedFlusher{}
|
|
|
|
w := NewChunkedWriter(b, f)
|
|
|
|
r := NewChunkedReader(b, 20, nil)
|
|
|
|
|
|
|
|
msgs := [][]byte{
|
|
|
|
[]byte("test1"),
|
|
|
|
[]byte("test2"),
|
|
|
|
[]byte("test3"),
|
|
|
|
[]byte("test4"),
|
2020-04-15 03:17:41 -07:00
|
|
|
{}, // This is ignored by writer.
|
2019-08-19 13:16:10 -07:00
|
|
|
[]byte("test5-after-empty"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
n, err := w.Write(msg)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(msg), n)
|
2019-08-19 13:16:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for ; i < 4; i++ {
|
|
|
|
msg, err := r.Next()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Less(t, i, len(msgs), "more messages then expected")
|
|
|
|
require.Equal(t, msgs[i], msg)
|
2019-08-19 13:16:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Empty byte slice is skipped.
|
|
|
|
i++
|
|
|
|
|
|
|
|
msg, err := r.Next()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Less(t, i, len(msgs), "more messages then expected")
|
|
|
|
require.Equal(t, msgs[i], msg)
|
2019-08-19 13:16:10 -07:00
|
|
|
|
|
|
|
_, err = r.Next()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, err, "expected io.EOF")
|
|
|
|
require.Equal(t, io.EOF, err)
|
2019-08-19 13:16:10 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, 5, f.flushed)
|
2019-08-19 13:16:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChunkedReader_Overflow(t *testing.T) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
_, err := NewChunkedWriter(b, &mockedFlusher{}).Write([]byte("twelve bytes"))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-08-19 13:16:10 -07:00
|
|
|
|
|
|
|
b2 := make([]byte, 12)
|
|
|
|
copy(b2, b.Bytes())
|
|
|
|
|
|
|
|
ret, err := NewChunkedReader(b, 12, nil).Next()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "twelve bytes", string(ret))
|
2019-08-19 13:16:10 -07:00
|
|
|
|
|
|
|
_, err = NewChunkedReader(bytes.NewReader(b2), 11, nil).Next()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, err, "expect exceed limit error")
|
|
|
|
require.Equal(t, "chunkedReader: message size exceeded the limit 11 bytes; got: 12 bytes", err.Error())
|
2019-08-19 13:16:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChunkedReader_CorruptedFrame(t *testing.T) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
w := NewChunkedWriter(b, &mockedFlusher{})
|
|
|
|
|
|
|
|
n, err := w.Write([]byte("test1"))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 5, n)
|
2019-08-19 13:16:10 -07:00
|
|
|
|
|
|
|
bs := b.Bytes()
|
|
|
|
bs[9] = 1 // Malform the frame by changing one byte.
|
|
|
|
|
|
|
|
_, err = NewChunkedReader(bytes.NewReader(bs), 20, nil).Next()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, err, "expected malformed frame")
|
|
|
|
require.Equal(t, "chunkedReader: corrupted frame; checksum mismatch", err.Error())
|
2019-08-19 13:16:10 -07:00
|
|
|
}
|