etcd/wal/record_test.go

87 lines
2.4 KiB
Go
Raw Normal View History

2016-05-13 06:50:04 +03:00
// Copyright 2015 The etcd 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.
2014-07-28 07:58:39 +04:00
2014-07-28 04:23:04 +04:00
package wal
import (
"bytes"
2014-09-04 02:10:15 +04:00
"hash/crc32"
2014-07-28 04:23:04 +04:00
"io"
2014-09-04 02:10:15 +04:00
"io/ioutil"
2014-07-28 04:23:04 +04:00
"reflect"
"testing"
2014-09-04 03:46:42 +04:00
"go.etcd.io/etcd/wal/walpb"
2014-07-28 04:23:04 +04:00
)
var (
infoData = []byte("\b\xef\xfd\x02")
infoRecord = append([]byte("\x0e\x00\x00\x00\x00\x00\x00\x00\b\x01\x10\x99\xb5\xe4\xd0\x03\x1a\x04"), infoData...)
)
2014-08-05 02:46:12 +04:00
func TestReadRecord(t *testing.T) {
2014-09-04 02:10:15 +04:00
badInfoRecord := make([]byte, len(infoRecord))
copy(badInfoRecord, infoRecord)
badInfoRecord[len(badInfoRecord)-1] = 'a'
2014-07-28 04:23:04 +04:00
tests := []struct {
data []byte
2014-09-04 03:46:42 +04:00
wr *walpb.Record
2014-07-28 04:23:04 +04:00
we error
}{
2014-09-04 03:46:42 +04:00
{infoRecord, &walpb.Record{Type: 1, Crc: crc32.Checksum(infoData, crcTable), Data: infoData}, nil},
{[]byte(""), &walpb.Record{}, io.EOF},
{infoRecord[:8], &walpb.Record{}, io.ErrUnexpectedEOF},
2014-09-04 03:46:42 +04:00
{infoRecord[:len(infoRecord)-len(infoData)-8], &walpb.Record{}, io.ErrUnexpectedEOF},
{infoRecord[:len(infoRecord)-len(infoData)], &walpb.Record{}, io.ErrUnexpectedEOF},
{infoRecord[:len(infoRecord)-8], &walpb.Record{}, io.ErrUnexpectedEOF},
{badInfoRecord, &walpb.Record{}, walpb.ErrCRCMismatch},
2014-07-28 04:23:04 +04:00
}
2014-09-04 03:46:42 +04:00
rec := &walpb.Record{}
2014-07-28 04:23:04 +04:00
for i, tt := range tests {
buf := bytes.NewBuffer(tt.data)
2014-09-04 02:10:15 +04:00
decoder := newDecoder(ioutil.NopCloser(buf))
e := decoder.decode(rec)
2014-08-05 02:46:12 +04:00
if !reflect.DeepEqual(rec, tt.wr) {
t.Errorf("#%d: block = %v, want %v", i, rec, tt.wr)
2014-07-28 04:23:04 +04:00
}
if !reflect.DeepEqual(e, tt.we) {
t.Errorf("#%d: err = %v, want %v", i, e, tt.we)
}
2014-09-04 03:46:42 +04:00
rec = &walpb.Record{}
2014-07-28 04:23:04 +04:00
}
}
2014-08-05 02:46:12 +04:00
func TestWriteRecord(t *testing.T) {
2014-09-04 03:46:42 +04:00
b := &walpb.Record{}
2014-07-28 04:23:04 +04:00
typ := int64(0xABCD)
d := []byte("Hello world!")
buf := new(bytes.Buffer)
e := newEncoder(buf, 0, 0)
2014-09-04 03:46:42 +04:00
e.encode(&walpb.Record{Type: typ, Data: d})
2014-09-04 02:10:15 +04:00
e.flush()
decoder := newDecoder(ioutil.NopCloser(buf))
err := decoder.decode(b)
2014-07-28 04:23:04 +04:00
if err != nil {
t.Errorf("err = %v, want nil", err)
}
2014-08-05 02:46:12 +04:00
if b.Type != typ {
t.Errorf("type = %d, want %d", b.Type, typ)
2014-07-28 04:23:04 +04:00
}
2014-08-05 02:46:12 +04:00
if !reflect.DeepEqual(b.Data, d) {
t.Errorf("data = %v, want %v", b.Data, d)
2014-07-28 04:23:04 +04:00
}
}