-
Notifications
You must be signed in to change notification settings - Fork 221
/
binary_test.go
134 lines (115 loc) · 3.77 KB
/
binary_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright [2019] LinkedIn Corp. 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.
package goavro
import (
"bytes"
"fmt"
"math"
"testing"
)
var morePositiveThanMaxBlockCount, morePositiveThanMaxBlockSize, moreNegativeThanMaxBlockCount, mostNegativeBlockCount []byte
func init() {
c, err := NewCodec(`"long"`)
if err != nil {
panic(err)
}
morePositiveThanMaxBlockCount, err = c.BinaryFromNative(nil, (MaxBlockCount + 1))
if err != nil {
panic(err)
}
morePositiveThanMaxBlockSize, err = c.BinaryFromNative(nil, (MaxBlockSize + 1))
if err != nil {
panic(err)
}
moreNegativeThanMaxBlockCount, err = c.BinaryFromNative(nil, -(MaxBlockCount + 1))
if err != nil {
panic(err)
}
mostNegativeBlockCount, err = c.BinaryFromNative(nil, int64(math.MinInt64))
if err != nil {
panic(err)
}
}
func testBinaryDecodeFail(t *testing.T, schema string, buf []byte, errorMessage string) {
t.Helper()
c, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
}
value, newBuffer, err := c.NativeFromBinary(buf)
ensureError(t, err, errorMessage)
if value != nil {
t.Errorf("GOT: %v; WANT: %v", value, nil)
}
if !bytes.Equal(buf, newBuffer) {
t.Errorf("GOT: %v; WANT: %v", newBuffer, buf)
}
}
func testBinaryEncodeFail(t *testing.T, schema string, datum interface{}, errorMessage string) {
t.Helper()
c, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
}
buf, err := c.BinaryFromNative(nil, datum)
ensureError(t, err, errorMessage)
if buf != nil {
t.Errorf("GOT: %v; WANT: %v", buf, nil)
}
}
func testBinaryEncodeFailBadDatumType(t *testing.T, schema string, datum interface{}) {
t.Helper()
testBinaryEncodeFail(t, schema, datum, "received: ")
}
func testBinaryDecodeFailShortBuffer(t *testing.T, schema string, buf []byte) {
t.Helper()
testBinaryDecodeFail(t, schema, buf, "short buffer")
}
func testBinaryDecodePass(t *testing.T, schema string, datum interface{}, encoded []byte) {
t.Helper()
codec, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
}
value, remaining, err := codec.NativeFromBinary(encoded)
if err != nil {
t.Fatalf("schema: %s; %s", schema, err)
}
// remaining ought to be empty because there is nothing remaining to be
// decoded
if actual, expected := len(remaining), 0; actual != expected {
t.Errorf("schema: %s; Datum: %v; Actual: %#v; Expected: %#v", schema, datum, actual, expected)
}
// for testing purposes, to prevent big switch statement, convert each to
// string and compare.
if actual, expected := fmt.Sprintf("%v", value), fmt.Sprintf("%v", datum); actual != expected {
t.Errorf("schema: %s; Datum: %v; Actual: %#v; Expected: %#v", schema, datum, actual, expected)
}
}
func testBinaryEncodePass(t *testing.T, schema string, datum interface{}, expected []byte) {
t.Helper()
codec, err := NewCodec(schema)
if err != nil {
t.Fatalf("Schma: %q %s", schema, err)
}
actual, err := codec.BinaryFromNative(nil, datum)
if err != nil {
t.Fatalf("schema: %s; Datum: %v; %s", schema, datum, err)
}
if !bytes.Equal(actual, expected) {
t.Errorf("schema: %s; Datum: %v; Actual: %#v; Expected: %#v", schema, datum, actual, expected)
}
}
// testBinaryCodecPass does a bi-directional codec check, by encoding datum to
// bytes, then decoding bytes back to datum.
func testBinaryCodecPass(t *testing.T, schema string, datum interface{}, buf []byte) {
t.Helper()
testBinaryDecodePass(t, schema, datum, buf)
testBinaryEncodePass(t, schema, datum, buf)
}