2016-08-25 15:16:11 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
// Note: this file has tests for code in both delta.go and doubledelta.go --
|
|
|
|
// it may make sense to split those out later, but given that the tests are
|
|
|
|
// near-identical and share a helper, this feels simpler for now.
|
|
|
|
|
2016-09-21 21:44:27 +00:00
|
|
|
package chunk
|
2016-08-25 15:16:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnmarshalingCorruptedDeltaReturnsAnError(t *testing.T) {
|
|
|
|
|
2016-08-29 15:58:31 +00:00
|
|
|
var verifyUnmarshallingError = func(
|
|
|
|
err error,
|
|
|
|
chunkTypeName string,
|
|
|
|
unmarshalMethod string,
|
2016-12-13 15:51:02 +00:00
|
|
|
expectedStr string,
|
|
|
|
) {
|
2016-08-29 15:58:31 +00:00
|
|
|
|
|
|
|
if err == nil {
|
2016-12-13 15:51:02 +00:00
|
|
|
t.Errorf("Failed to obtain an error when unmarshalling corrupt %s (from %s)", chunkTypeName, unmarshalMethod)
|
2016-08-29 15:58:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), expectedStr) {
|
|
|
|
t.Errorf(
|
2016-12-13 15:51:02 +00:00
|
|
|
"'%s' not present in error when unmarshalling corrupt %s (from %s): '%s'",
|
2016-08-29 15:58:31 +00:00
|
|
|
expectedStr,
|
|
|
|
chunkTypeName,
|
|
|
|
unmarshalMethod,
|
|
|
|
err.Error())
|
|
|
|
}
|
2016-08-25 15:16:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 15:58:31 +00:00
|
|
|
cases := []struct {
|
|
|
|
chunkTypeName string
|
2016-09-21 15:56:55 +00:00
|
|
|
chunkConstructor func(deltaBytes, deltaBytes, bool, int) Chunk
|
2016-08-29 15:58:31 +00:00
|
|
|
minHeaderLen int
|
|
|
|
chunkLenPos int
|
2016-12-13 15:51:02 +00:00
|
|
|
timeBytesPos int
|
2016-08-29 15:58:31 +00:00
|
|
|
}{
|
|
|
|
{
|
2016-08-30 11:54:40 +00:00
|
|
|
chunkTypeName: "deltaEncodedChunk",
|
2016-09-21 15:56:55 +00:00
|
|
|
chunkConstructor: func(a, b deltaBytes, c bool, d int) Chunk {
|
2016-08-29 15:58:31 +00:00
|
|
|
return newDeltaEncodedChunk(a, b, c, d)
|
|
|
|
},
|
2016-08-30 11:54:40 +00:00
|
|
|
minHeaderLen: deltaHeaderBytes,
|
|
|
|
chunkLenPos: deltaHeaderBufLenOffset,
|
2016-12-13 15:51:02 +00:00
|
|
|
timeBytesPos: deltaHeaderTimeBytesOffset,
|
2016-08-29 15:58:31 +00:00
|
|
|
},
|
|
|
|
{
|
2016-08-30 11:54:40 +00:00
|
|
|
chunkTypeName: "doubleDeltaEncodedChunk",
|
2016-09-21 15:56:55 +00:00
|
|
|
chunkConstructor: func(a, b deltaBytes, c bool, d int) Chunk {
|
2016-08-29 15:58:31 +00:00
|
|
|
return newDoubleDeltaEncodedChunk(a, b, c, d)
|
|
|
|
},
|
2016-08-30 11:54:40 +00:00
|
|
|
minHeaderLen: doubleDeltaHeaderMinBytes,
|
|
|
|
chunkLenPos: doubleDeltaHeaderBufLenOffset,
|
2016-12-13 15:51:02 +00:00
|
|
|
timeBytesPos: doubleDeltaHeaderTimeBytesOffset,
|
2016-08-29 15:58:31 +00:00
|
|
|
},
|
2016-08-25 15:16:11 +00:00
|
|
|
}
|
2016-08-29 15:58:31 +00:00
|
|
|
for _, c := range cases {
|
2016-09-21 21:44:27 +00:00
|
|
|
chunk := c.chunkConstructor(d1, d4, false, ChunkLen)
|
2016-08-25 15:16:11 +00:00
|
|
|
|
2016-09-21 15:56:55 +00:00
|
|
|
cs, err := chunk.Add(model.SamplePair{
|
2016-08-29 15:58:31 +00:00
|
|
|
Timestamp: model.Now(),
|
|
|
|
Value: model.SampleValue(100),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't add sample to empty %s: %s", c.chunkTypeName, err)
|
|
|
|
}
|
2016-08-25 15:16:11 +00:00
|
|
|
|
2016-09-21 21:44:27 +00:00
|
|
|
buf := make([]byte, ChunkLen)
|
2016-08-25 15:16:11 +00:00
|
|
|
|
2016-09-21 15:56:55 +00:00
|
|
|
cs[0].MarshalToBuf(buf)
|
2016-08-25 15:16:11 +00:00
|
|
|
|
2016-12-13 15:51:02 +00:00
|
|
|
// Corrupt time byte to 0, which is illegal.
|
|
|
|
buf[c.timeBytesPos] = 0
|
|
|
|
err = cs[0].UnmarshalFromBuf(buf)
|
|
|
|
verifyUnmarshallingError(err, c.chunkTypeName, "buf", "invalid number of time bytes")
|
|
|
|
|
|
|
|
err = cs[0].Unmarshal(bytes.NewBuffer(buf))
|
|
|
|
verifyUnmarshallingError(err, c.chunkTypeName, "Reader", "invalid number of time bytes")
|
|
|
|
|
|
|
|
// Fix the corruption to go on.
|
|
|
|
buf[c.timeBytesPos] = byte(d1)
|
|
|
|
|
2016-08-29 15:58:31 +00:00
|
|
|
// Corrupt the length to be every possible too-small value
|
|
|
|
for i := 0; i < c.minHeaderLen; i++ {
|
|
|
|
binary.LittleEndian.PutUint16(buf[c.chunkLenPos:], uint16(i))
|
2016-08-25 15:16:11 +00:00
|
|
|
|
2016-09-21 15:56:55 +00:00
|
|
|
err = cs[0].UnmarshalFromBuf(buf)
|
2016-12-13 15:51:02 +00:00
|
|
|
verifyUnmarshallingError(err, c.chunkTypeName, "buf", "header size")
|
2016-08-25 15:16:11 +00:00
|
|
|
|
2016-09-21 15:56:55 +00:00
|
|
|
err = cs[0].Unmarshal(bytes.NewBuffer(buf))
|
2016-12-13 15:51:02 +00:00
|
|
|
verifyUnmarshallingError(err, c.chunkTypeName, "Reader", "header size")
|
2016-08-29 15:58:31 +00:00
|
|
|
}
|
2016-08-25 15:16:11 +00:00
|
|
|
}
|
|
|
|
}
|