forked from karrick/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocf_reader_test.go
94 lines (72 loc) · 4.17 KB
/
ocf_reader_test.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package goavro_test
import (
"bytes"
"testing"
"github.com/karrick/goavro"
)
// readOCFHeader, magic bytes
func TestReadOCFHeaderMagicBytes(t *testing.T) {
_, err := goavro.NewOCFReader(bytes.NewBuffer([]byte("Obj"))) // missing fourth byte
ensureError(t, err, "cannot create OCF")
_, err = goavro.NewOCFReader(bytes.NewBuffer([]byte("....")))
ensureError(t, err, "cannot create OCF")
}
//
// cannot read OCF header
//
func testCannotReadOCFHeader(t *testing.T, input []byte, expected ...string) {
_, err := goavro.NewOCFReader(bytes.NewBuffer(append([]byte("Obj\x01"), input...)))
ensureError(t, err, append([]string{"cannot read OCF header"}, expected...)...)
}
// readOCFHeader, metadataBinaryReader, block count
func TestReadOCFHeaderMetadataBinaryReaderBlockCount(t *testing.T) {
testCannotReadOCFHeader(t, nil, "cannot read map block count", "EOF")
testCannotReadOCFHeader(t, mostNegativeBlockCount, "cannot read map with block count")
testCannotReadOCFHeader(t, []byte("\x01"), "cannot read map block size", "EOF")
testCannotReadOCFHeader(t, morePositiveThanMaxBlockCount, "cannot read map when block count exceeds")
}
// readOCFHeader, metadataBinaryReader, bytesBinaryReader
func TestReadOCFHeaderMetadataBinaryReaderMapKey(t *testing.T) {
testCannotReadOCFHeader(t, []byte("\x02"), "cannot read map key", "cannot read bytes", "cannot read size", "EOF")
testCannotReadOCFHeader(t, []byte("\x02\x01"), "cannot read map key", "cannot read bytes", "size is negative")
testCannotReadOCFHeader(t, append([]byte("\x02"), morePositiveThanMaxBlockCount...), "cannot read map key", "cannot read bytes", "size exceeds MaxBlockSize")
testCannotReadOCFHeader(t, append([]byte("\x02"), mostNegativeBlockCount...), "cannot read map key", "cannot read bytes", "size is negative")
testCannotReadOCFHeader(t, append([]byte("\x02"), moreNegativeThanMaxBlockCount...), "cannot read map key", "cannot read bytes", "size is negative")
testCannotReadOCFHeader(t, []byte("\x02\x02"), "cannot read map key", "cannot read bytes", "EOF")
testCannotReadOCFHeader(t, []byte("\x02\x04k1\x04v1\x02\x04k1"), "cannot read map", "duplicate key")
testCannotReadOCFHeader(t, []byte("\x04\x04k1\x04v1\x04k1"), "cannot read map", "duplicate key")
}
func TestReadOCFHeaderMetadataBinaryReaderMapValue(t *testing.T) {
testCannotReadOCFHeader(t, []byte("\x02\x04k1"), "cannot read map value for key", "cannot read bytes", "EOF")
// have already tested all other binaryBytesReader errors above
testCannotReadOCFHeader(t, []byte("\x02\x04k1\x04v1"), "cannot read map block count", "EOF")
testCannotReadOCFHeader(t, append([]byte("\x02\x04k1\x04v1"), mostNegativeBlockCount...), "cannot read map with block count")
testCannotReadOCFHeader(t, []byte("\x02\x04k1\x04v1"), "cannot read map block count", "EOF")
testCannotReadOCFHeader(t, []byte("\x02\x04k1\x04v1\x01"), "cannot read map block size", "EOF")
testCannotReadOCFHeader(t, append(append([]byte("\x02\x04k1\x04v1"), moreNegativeThanMaxBlockCount...), []byte("\x02")...), "cannot read map when block count exceeds")
testCannotReadOCFHeader(t, append([]byte("\x02\x04k1\x04v1"), morePositiveThanMaxBlockCount...), "cannot read map when block count exceeds")
}
// readOCFHeader, avro.codec
func TestReadOCFHeaderMetadataAvroCodecUnknown(t *testing.T) {
testCannotReadOCFHeader(t, []byte("\x02\x14avro.codec\x06bad\x00"), "cannot read OCF header", "unrecognized compression", "bad")
}
// readOCFHeader, avro.schema
func TestReadOCFHeaderMetadataAvroSchemaMissing(t *testing.T) {
testCannotReadOCFHeader(t, []byte("\x00"), "without avro.schema")
testCannotReadOCFHeader(t, []byte("\x02\x16avro.schema\x04{}\x00"), "invalid avro.schema")
}
// readOCFHeader, sync marker
func TestReadOCFHeaderMetadataSyncMarker(t *testing.T) {
testCannotReadOCFHeader(t, []byte("\x02\x16avro.schema\x1e{\"type\":\"null\"}\x00"), "sync marker", "EOF")
}
// TODO: writeOCFHeader
//
// OCFReader
//
// func testOCFReader(t *testing.T, schema string, input []byte, expected ...string) {
// _, err := goavro.NewOCFReader(bytes.NewBuffer(append([]byte("Obj\x01"), input...)))
// ensureError(t, err, append([]string{"any prefix?"}, expected...)...)
// }
// func TestOCFReaderRead(t *testing.T) {
// testOCFReader(t,
// }