mirror of
https://github.com/rs/zerolog
synced 2026-06-08 17:13:30 +00:00
8148645974
* Add array handling of IPAddr and IPPrefix Updated documentation, benchmark, and tests including catching up some missing. Cleaned up couple lint messages * Added cbor encoding for []net.IP and []net.IPNet * Fix binary_test Typos galore, thanks CI * Remove feral x character * Added IPAddrs and IPPrefixes tests for json encoder * Increase code coverage and test cleanup Added Type to the BenchmarkLogFieldType and BenchmarkContexdtFieldType test cases, also to log_test.go Moved the test-fixture data for them and also shared them with new event test for nil events. Added a couple strings for encodeByteTest for FF, CR, LF Added complete cbor testing for AppendInt*, AppendInts*, AppendUint*, and AppendUints* methods. Added test for float IsNaN and Inf(0) and Inf(-1). Added all min/max/smallest value tests for ints and floats Extended the Array test to include IPPrefix, MACAddr, Interface, and object marshaled. Better test of MarshalZerologObject. Added test for encoder AppendStrings and AppendStringers. * Remove merge detritus. * Remove code duplication when appending objects * Add missing Stringers support to Context * Add test cases for Err and Object in Array * Add missing benchmark for Events and Contexts Events: Any, Bytes, Hex, Float32, Floats32, Stringer, Stringers, Timestamp Context: Any, Bytes, Hex, Float32, Floats32, Stringers (also renamed Float,Floats to Float64,Floats64) * Add test of MarshalZeroLogObject of error * Build out Event test cases Test of nil Event - write - Array, Dict - Fields - Int8, Ints8, Int16, Ints16, Int32, Ints32, Int64, Ints64 - Uint8, Uints8, Uint16, Uints16, Uint32, Uints32, Uint64, Uints64 - RawCBOR, RawJSON, EmbedObject - Stringers - Caller, CallerSkip, Stack - Send, Msg, Msgf, MsgFunc - (renamed tests of Float, Floats to Float64, Floats64) Added test of MsgFunc * Add more tests - Dur (With) - Any (With, Fields, Fields map, Disabled) - Interface - Stack (Fields) Fix issue with testing a nil loggableError * Split out the With tests again type arrays Simplifies the "want" maintenance * Add missing test fixture data Bytes, Floats32, Ints8, Ints16, Ints32, Ints64, Uints8, Uints16, Uints32, Uints64, RawJSON, RawCBOR Stringers is now an array (not a single Stringer) (also renamed Float, Floats to Float64, Floats64) * Add examples for IPAddrs, IPPrefixes, Times Missing examples that increase coverage * Build out the complete ErrorMarshalFunc tests * Add AppendStrings to CBOR tests * Add CBOR AppendTimes, AppendDuration, AppendDurations tests * Add CBOR tests - BeginMarker, EndMarker, AppendArrayDim, AppendLineBreak - AppendObjectData - AppendIterface - AppendType - AppendHex - Empty arrays for - Bools - Floats32, Floats64 - Large arrays for - Bools, - Uints8, Uints16, Uints32, Uints64 - Floats32, Floats64 - IPAddrs, IPPrefixes - Small arrays for - Ints, Ints8, Ints16, Ints32, Ints64 - Uints, Uints32, Uints64 - Floats32, Floats64 * Add CBOR test for AppendKey * Added more string encoding tests Test escape character handling. * Fix test import cycle * Rename hex to hexCharacters to avoid namespace. * Remove leftover comment * Add test cases to share between CBOR and JSON tests * Switched CBOR to shared test cases * Add JSON test for AppendKey * Switch to shared test cases Added test for AppendStringer * Added JSON test - AppendNil - AppendBeginMarker, AppendEndMarker - AppendArrayStart, AppendArrayEnd - AppendArrayDelim - AppendLineBreak - AppendObjectData - AppendInterface - AppendBool, AppendBools * Add JSON time tests - AppendTime, AppendTimes - AppendTime (past/present) (integer/float) - AppendDuration, AppendDurations * Added JSON tests for plurals Split out the Int and Float tests into distinct files. Add - AppendInt - AppendInts8, AppendInts16, AppendInts32, AppendInts64, AppendInts - AppendUints8, AppendUints16, AppendUints32, AppendUints64, AppendUints - AppendFloats32, AppendFloats64
209 lines
6.8 KiB
Go
209 lines
6.8 KiB
Go
package cbor
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/internal"
|
|
)
|
|
|
|
func TestDecodeInteger(t *testing.T) {
|
|
for _, tc := range internal.IntegerTestCases {
|
|
gotv := decodeInteger(getReader(tc.Binary))
|
|
if gotv != int64(tc.Val) {
|
|
t.Errorf("decodeInteger(0x%s)=0x%d, want: 0x%d",
|
|
hex.EncodeToString([]byte(tc.Binary)), gotv, tc.Val)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeString(t *testing.T) {
|
|
for _, tt := range encodeStringTests {
|
|
got := decodeUTF8String(getReader(tt.binary))
|
|
if string(got) != "\""+tt.json+"\"" {
|
|
t.Errorf("DecodeString(0x%s)=%s, want:\"%s\"\n", hex.EncodeToString([]byte(tt.binary)), string(got),
|
|
hex.EncodeToString([]byte(tt.json)))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeArray(t *testing.T) {
|
|
for _, tc := range internal.IntegerArrayTestCases {
|
|
buf := bytes.NewBuffer([]byte{})
|
|
array2Json(getReader(tc.Binary), buf)
|
|
if buf.String() != tc.Json {
|
|
t.Errorf("array2Json(0x%s)=%s, want: %s", hex.EncodeToString([]byte(tc.Binary)), buf.String(), tc.Json)
|
|
}
|
|
}
|
|
//Unspecified Length Array
|
|
var infiniteArrayTestCases = []struct {
|
|
in string
|
|
out string
|
|
}{
|
|
{"\x9f\x20\x00\x18\xc8\x14\xff", "[-1,0,200,20]"},
|
|
{"\x9f\x38\xc7\x29\x18\xc8\x19\x01\x90\xff", "[-200,-10,200,400]"},
|
|
{"\x9f\x01\x02\x03\xff", "[1,2,3]"},
|
|
{"\x9f\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x18\x18\x19\xff",
|
|
"[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]"},
|
|
}
|
|
for _, tc := range infiniteArrayTestCases {
|
|
buf := bytes.NewBuffer([]byte{})
|
|
array2Json(getReader(tc.in), buf)
|
|
if buf.String() != tc.out {
|
|
t.Errorf("array2Json(0x%s)=%s, want: %s", hex.EncodeToString([]byte(tc.out)), buf.String(), tc.out)
|
|
}
|
|
}
|
|
for _, tc := range internal.BooleanArrayTestCases {
|
|
buf := bytes.NewBuffer([]byte{})
|
|
array2Json(getReader(tc.Binary), buf)
|
|
if buf.String() != tc.Json {
|
|
t.Errorf("array2Json(0x%s)=%s, want: %s", hex.EncodeToString([]byte(tc.Binary)), buf.String(), tc.Json)
|
|
}
|
|
}
|
|
//TODO add cases for arrays of other types
|
|
}
|
|
|
|
var infiniteMapDecodeTestCases = []struct {
|
|
Bin []byte
|
|
Json string
|
|
}{
|
|
{[]byte("\xbf\x64IETF\x20\xff"), "{\"IETF\":-1}"},
|
|
{[]byte("\xbf\x65Array\x84\x20\x00\x18\xc8\x14\xff"), "{\"Array\":[-1,0,200,20]}"},
|
|
}
|
|
|
|
var mapDecodeTestCases = []struct {
|
|
Bin []byte
|
|
Json string
|
|
}{
|
|
{[]byte("\xa2\x64IETF\x20"), "{\"IETF\":-1}"},
|
|
{[]byte("\xa2\x65Array\x84\x20\x00\x18\xc8\x14"), "{\"Array\":[-1,0,200,20]}"},
|
|
}
|
|
|
|
func TestDecodeMap(t *testing.T) {
|
|
for _, tc := range mapDecodeTestCases {
|
|
buf := bytes.NewBuffer([]byte{})
|
|
map2Json(getReader(string(tc.Bin)), buf)
|
|
if buf.String() != tc.Json {
|
|
t.Errorf("map2Json(0x%s)=%s, want: %s", hex.EncodeToString(tc.Bin), buf.String(), tc.Json)
|
|
}
|
|
}
|
|
for _, tc := range infiniteMapDecodeTestCases {
|
|
buf := bytes.NewBuffer([]byte{})
|
|
map2Json(getReader(string(tc.Bin)), buf)
|
|
if buf.String() != tc.Json {
|
|
t.Errorf("map2Json(0x%s)=%s, want: %s", hex.EncodeToString(tc.Bin), buf.String(), tc.Json)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeBool(t *testing.T) {
|
|
for _, tc := range internal.BooleanTestCases {
|
|
got := decodeSimpleFloat(getReader(tc.Binary))
|
|
if string(got) != tc.Json {
|
|
t.Errorf("decodeSimpleFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), string(got), tc.Json)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeFloat(t *testing.T) {
|
|
for _, tc := range internal.Float32TestCases {
|
|
got, _ := decodeFloat(getReader(tc.Binary))
|
|
if got != float64(tc.Val) && math.IsNaN(got) != math.IsNaN(float64(tc.Val)) {
|
|
t.Errorf("decodeFloat(0x%s)=%f, want:%f", hex.EncodeToString([]byte(tc.Binary)), got, tc.Val)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeTimestamp(t *testing.T) {
|
|
decodeTimeZone, _ = time.LoadLocation("UTC")
|
|
for _, tc := range internal.TimeIntegerTestcases {
|
|
tm := decodeTagData(getReader(tc.Binary))
|
|
if string(tm) != "\""+tc.RfcStr+"\"" {
|
|
t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), tm, tc.RfcStr)
|
|
}
|
|
}
|
|
for _, tc := range internal.TimeFloatTestcases {
|
|
tm := decodeTagData(getReader(tc.Out))
|
|
//Since we convert to float and back - it may be slightly off - so
|
|
//we cannot check for exact equality instead, we'll check it is
|
|
//very close to each other Less than a Microsecond (lets not yet do nanosec)
|
|
|
|
got, _ := time.Parse(string(tm), string(tm))
|
|
want, _ := time.Parse(tc.RfcStr, tc.RfcStr)
|
|
if got.Sub(want) > time.Microsecond {
|
|
t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Out)), tm, tc.RfcStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeNetworkAddr(t *testing.T) {
|
|
for _, tc := range internal.IpAddrTestCases {
|
|
d1 := decodeTagData(getReader(tc.Binary))
|
|
if string(d1) != tc.Text {
|
|
t.Errorf("decodeNetworkAddr(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), d1, tc.Text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeMACAddr(t *testing.T) {
|
|
for _, tc := range internal.MacAddrTestCases {
|
|
d1 := decodeTagData(getReader(tc.Binary))
|
|
if string(d1) != tc.Text {
|
|
t.Errorf("decodeNetworkAddr(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), d1, tc.Text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDecodeIPPrefix(t *testing.T) {
|
|
for _, tc := range internal.IPPrefixTestCases {
|
|
d1 := decodeTagData(getReader(tc.Binary))
|
|
if string(d1) != tc.Text {
|
|
t.Errorf("decodeIPPrefix(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), d1, tc.Text)
|
|
}
|
|
}
|
|
}
|
|
|
|
var compositeCborTestCases = []struct {
|
|
Binary []byte
|
|
Json string
|
|
}{
|
|
{[]byte("\xbf\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14\xff\xff"), "{\"IETF\":-1,\"Array\":[-1,0,200,20]}\n"},
|
|
{[]byte("\xbf\x64IETF\x64YES!\x65Array\x9f\x20\x00\x18\xc8\x14\xff\xff"), "{\"IETF\":\"YES!\",\"Array\":[-1,0,200,20]}\n"},
|
|
}
|
|
|
|
func TestDecodeCbor2Json(t *testing.T) {
|
|
for _, tc := range compositeCborTestCases {
|
|
buf := bytes.NewBuffer([]byte{})
|
|
err := Cbor2JsonManyObjects(getReader(string(tc.Binary)), buf)
|
|
if buf.String() != tc.Json || err != nil {
|
|
t.Errorf("cbor2JsonManyObjects(0x%s)=%s, want: %s, err:%s", hex.EncodeToString(tc.Binary), buf.String(), tc.Json, err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
var negativeCborTestCases = []struct {
|
|
Binary []byte
|
|
errStr string
|
|
}{
|
|
{[]byte("\xb9\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14"), "Tried to Read 18 Bytes.. But hit end of file"},
|
|
{[]byte("\xbf\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14"), "EOF"},
|
|
{[]byte("\xbf\x14IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14"), "Tried to Read 40736 Bytes.. But hit end of file"},
|
|
{[]byte("\xbf\x64IETF"), "EOF"},
|
|
{[]byte("\xbf\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\xff\xff\xff"), "Invalid Additional Type: 31 in decodeSimpleFloat"},
|
|
{[]byte("\xbf\x64IETF\x20\x65Array"), "EOF"},
|
|
{[]byte("\xbf\x64"), "Tried to Read 4 Bytes.. But hit end of file"},
|
|
}
|
|
|
|
func TestDecodeNegativeCbor2Json(t *testing.T) {
|
|
for _, tc := range negativeCborTestCases {
|
|
buf := bytes.NewBuffer([]byte{})
|
|
err := Cbor2JsonManyObjects(getReader(string(tc.Binary)), buf)
|
|
if err == nil || err.Error() != tc.errStr {
|
|
t.Errorf("Expected error got:%s, want:%s", err, tc.errStr)
|
|
}
|
|
}
|
|
}
|