diff --git a/bench_test.go b/bench_test.go index 5445db23..3dd312f0 100644 --- a/bench_test.go +++ b/bench_test.go @@ -108,7 +108,7 @@ type T3 struct { var decodeBenchmarks = []struct { name string - cborData []byte + data []byte decodeToTypes []reflect.Type }{ {"bool", hexDecode("f5"), []reflect.Type{typeIntf, typeBool}}, // true @@ -126,9 +126,9 @@ var decodeBenchmarks = []struct { } var encodeBenchmarks = []struct { - name string - cborData []byte - values []interface{} + name string + data []byte + values []interface{} }{ {"bool", hexDecode("f5"), []interface{}{true}}, {"positive int", hexDecode("1bffffffffffffffff"), []interface{}{uint64(18446744073709551615)}}, @@ -150,7 +150,7 @@ func BenchmarkUnmarshal(b *testing.B) { b.Run(name, func(b *testing.B) { for i := 0; i < b.N; i++ { vPtr := reflect.New(t).Interface() - if err := Unmarshal(bm.cborData, vPtr); err != nil { + if err := Unmarshal(bm.data, vPtr); err != nil { b.Fatal("Unmarshal:", err) } } @@ -159,7 +159,7 @@ func BenchmarkUnmarshal(b *testing.B) { } var moreBenchmarks = []struct { name string - cborData []byte + data []byte decodeToType reflect.Type }{ // Unmarshal CBOR map with string key to map[string]interface{}. @@ -203,7 +203,7 @@ func BenchmarkUnmarshal(b *testing.B) { b.Run(bm.name, func(b *testing.B) { for i := 0; i < b.N; i++ { vPtr := reflect.New(bm.decodeToType).Interface() - if err := Unmarshal(bm.cborData, vPtr); err != nil { + if err := Unmarshal(bm.data, vPtr); err != nil { b.Fatal("Unmarshal:", err) } } @@ -220,8 +220,8 @@ func BenchmarkUnmarshalFirst(b *testing.B) { if t.Kind() == reflect.Struct { name = "CBOR " + bm.name + " to Go " + t.Kind().String() } - data := make([]byte, 0, len(bm.cborData)+len(trailingData)) - data = append(data, bm.cborData...) + data := make([]byte, 0, len(bm.data)+len(trailingData)) + data = append(data, bm.data...) data = append(data, trailingData...) b.Run(name, func(b *testing.B) { for i := 0; i < b.N; i++ { @@ -244,8 +244,8 @@ func BenchmarkUnmarshalFirstViaDecoder(b *testing.B) { if t.Kind() == reflect.Struct { name = "CBOR " + bm.name + " to Go " + t.Kind().String() } - data := make([]byte, 0, len(bm.cborData)+len(trailingData)) - data = append(data, bm.cborData...) + data := make([]byte, 0, len(bm.data)+len(trailingData)) + data = append(data, bm.data...) data = append(data, trailingData...) b.Run(name, func(b *testing.B) { for i := 0; i < b.N; i++ { @@ -266,7 +266,7 @@ func BenchmarkDecode(b *testing.B) { if t.Kind() == reflect.Struct { name = "CBOR " + bm.name + " to Go " + t.Kind().String() } - buf := bytes.NewReader(bm.cborData) + buf := bytes.NewReader(bm.data) decoder := NewDecoder(buf) b.Run(name, func(b *testing.B) { for i := 0; i < b.N; i++ { @@ -282,16 +282,16 @@ func BenchmarkDecode(b *testing.B) { } func BenchmarkDecodeStream(b *testing.B) { - var cborData []byte + var data []byte for _, bm := range decodeBenchmarks { for i := 0; i < len(bm.decodeToTypes); i++ { - cborData = append(cborData, bm.cborData...) + data = append(data, bm.data...) } } b.ResetTimer() for i := 0; i < b.N; i++ { - buf := bytes.NewReader(cborData) + buf := bytes.NewReader(data) decoder := NewDecoder(buf) for j := 0; j < rounds; j++ { for _, bm := range decodeBenchmarks { @@ -428,9 +428,9 @@ func BenchmarkMarshalCanonical(b *testing.B) { N string `cbor:"n"` } for _, bm := range []struct { - name string - cborData []byte - values []interface{} + name string + data []byte + values []interface{} }{ {"map", hexDecode("ad616161416162614261636143616461446165614561666146616761476168614861696149616a614a616c614c616d614d616e614e"), []interface{}{map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E", "f": "F", "g": "G", "h": "H", "i": "I", "j": "J", "l": "L", "m": "M", "n": "N"}, strc{A: "A", B: "B", C: "C", D: "D", E: "E", F: "F", G: "G", H: "H", I: "I", J: "J", L: "L", M: "M", N: "N"}}}, } { @@ -524,8 +524,8 @@ func BenchmarkEncodeStream(b *testing.B) { func BenchmarkUnmarshalCOSE(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.2 testCases := []struct { - name string - cborData []byte + name string + data []byte }{ {"128-Bit Symmetric Key", hexDecode("a42050231f4c4d4d3051fdc2ec0a3851d5b3830104024c53796d6d6574726963313238030a")}, {"256-Bit Symmetric Key", hexDecode("a4205820403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d795693880104024c53796d6d6574726963323536030a")}, @@ -535,7 +535,7 @@ func BenchmarkUnmarshalCOSE(b *testing.B) { b.Run(tc.name, func(b *testing.B) { for i := 0; i < b.N; i++ { var v coseKey - if err := Unmarshal(tc.cborData, &v); err != nil { + if err := Unmarshal(tc.data, &v); err != nil { b.Fatal("Unmarshal:", err) } } @@ -546,8 +546,8 @@ func BenchmarkUnmarshalCOSE(b *testing.B) { func BenchmarkMarshalCOSE(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.2 testCases := []struct { - name string - cborData []byte + name string + data []byte }{ {"128-Bit Symmetric Key", hexDecode("a42050231f4c4d4d3051fdc2ec0a3851d5b3830104024c53796d6d6574726963313238030a")}, {"256-Bit Symmetric Key", hexDecode("a4205820403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d795693880104024c53796d6d6574726963323536030a")}, @@ -555,7 +555,7 @@ func BenchmarkMarshalCOSE(b *testing.B) { } for _, tc := range testCases { var v coseKey - if err := Unmarshal(tc.cborData, &v); err != nil { + if err := Unmarshal(tc.data, &v); err != nil { b.Fatal("Unmarshal:", err) } b.Run(tc.name, func(b *testing.B) { @@ -570,10 +570,10 @@ func BenchmarkMarshalCOSE(b *testing.B) { func BenchmarkUnmarshalCWTClaims(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.1 - cborData := hexDecode("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") + data := hexDecode("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") for i := 0; i < b.N; i++ { var v claims - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } } @@ -581,11 +581,11 @@ func BenchmarkUnmarshalCWTClaims(b *testing.B) { func BenchmarkUnmarshalCWTClaimsWithDupMapKeyOpt(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.1 - cborData := hexDecode("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") + data := hexDecode("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() for i := 0; i < b.N; i++ { var v claims - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } } @@ -593,9 +593,9 @@ func BenchmarkUnmarshalCWTClaimsWithDupMapKeyOpt(b *testing.B) { func BenchmarkMarshalCWTClaims(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.1 - cborData := hexDecode("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") + data := hexDecode("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") var v claims - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } for i := 0; i < b.N; i++ { @@ -607,10 +607,10 @@ func BenchmarkMarshalCWTClaims(b *testing.B) { func BenchmarkUnmarshalSenML(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8428#section-6 - cborData := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") + data := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") for i := 0; i < b.N; i++ { var v []SenMLRecord - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } } @@ -618,9 +618,9 @@ func BenchmarkUnmarshalSenML(b *testing.B) { func BenchmarkMarshalSenML(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8428#section-6 - cborData := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") + data := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") var v []SenMLRecord - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } for i := 0; i < b.N; i++ { @@ -632,9 +632,9 @@ func BenchmarkMarshalSenML(b *testing.B) { func BenchmarkMarshalSenMLShortestFloat16(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8428#section-6 - cborData := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") + data := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") var v []SenMLRecord - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } em, _ := EncOptions{ShortestFloat: ShortestFloat16}.EncMode() @@ -647,10 +647,10 @@ func BenchmarkMarshalSenMLShortestFloat16(b *testing.B) { func BenchmarkUnmarshalWebAuthn(b *testing.B) { // Data generated from Yubico security key - cborData := hexDecode("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3") + data := hexDecode("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3") for i := 0; i < b.N; i++ { var v attestationObject - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } } @@ -658,9 +658,9 @@ func BenchmarkUnmarshalWebAuthn(b *testing.B) { func BenchmarkMarshalWebAuthn(b *testing.B) { // Data generated from Yubico security key - cborData := hexDecode("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3") + data := hexDecode("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3") var v attestationObject - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } for i := 0; i < b.N; i++ { @@ -672,11 +672,11 @@ func BenchmarkMarshalWebAuthn(b *testing.B) { func BenchmarkUnmarshalCOSEMAC(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.4 - cborData := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") + data := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") for i := 0; i < b.N; i++ { var v macedCOSE - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } } @@ -684,7 +684,7 @@ func BenchmarkUnmarshalCOSEMAC(b *testing.B) { func BenchmarkUnmarshalCOSEMACWithTag(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.4 - cborData := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") + data := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") // Register tag CBOR Web Token (CWT) 61 and COSE_Mac0 17 with macedCOSE type tags := NewTagSet() @@ -697,17 +697,17 @@ func BenchmarkUnmarshalCOSEMACWithTag(b *testing.B) { for i := 0; i < b.N; i++ { var v macedCOSE - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal:", err) } } } func BenchmarkMarshalCOSEMAC(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.4 - cborData := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") + data := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") var v macedCOSE - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal():", err) } @@ -720,7 +720,7 @@ func BenchmarkMarshalCOSEMAC(b *testing.B) { func BenchmarkMarshalCOSEMACWithTag(b *testing.B) { // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.4 - cborData := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") + data := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") // Register tag CBOR Web Token (CWT) 61 and COSE_Mac0 17 with macedCOSE type tags := NewTagSet() @@ -733,7 +733,7 @@ func BenchmarkMarshalCOSEMACWithTag(b *testing.B) { em, _ := EncOptions{}.EncModeWithTags(tags) var v macedCOSE - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { b.Fatal("Unmarshal():", err) } diff --git a/decode_test.go b/decode_test.go index 920610b1..8bfcbf5d 100644 --- a/decode_test.go +++ b/decode_test.go @@ -2336,73 +2336,73 @@ func TestUnmarshalFloat(t *testing.T) { } } -func testFloat(t *testing.T, cborData []byte, f interface{}, wantf interface{}, equalityThreshold float64) { +func testFloat(t *testing.T, data []byte, f interface{}, wantf interface{}, equalityThreshold float64) { switch wantf := wantf.(type) { case float32: f, ok := f.(float32) if !ok { - t.Errorf("Unmarshal(0x%x) returned value of type %T, want float32", cborData, f) + t.Errorf("Unmarshal(0x%x) returned value of type %T, want float32", data, f) return } if math.IsNaN(float64(wantf)) { if !math.IsNaN(float64(f)) { - t.Errorf("Unmarshal(0x%x) = %f, want NaN", cborData, f) + t.Errorf("Unmarshal(0x%x) = %f, want NaN", data, f) } } else if math.IsInf(float64(wantf), 0) { if f != wantf { - t.Errorf("Unmarshal(0x%x) = %f, want %f", cborData, f, wantf) + t.Errorf("Unmarshal(0x%x) = %f, want %f", data, f, wantf) } } else if math.Abs(float64(f-wantf)) > equalityThreshold { - t.Errorf("Unmarshal(0x%x) = %.18f, want %.18f, diff %.18f > threshold %.18f", cborData, f, wantf, math.Abs(float64(f-wantf)), equalityThreshold) + t.Errorf("Unmarshal(0x%x) = %.18f, want %.18f, diff %.18f > threshold %.18f", data, f, wantf, math.Abs(float64(f-wantf)), equalityThreshold) } case float64: f, ok := f.(float64) if !ok { - t.Errorf("Unmarshal(0x%x) returned value of type %T, want float64", cborData, f) + t.Errorf("Unmarshal(0x%x) returned value of type %T, want float64", data, f) return } if math.IsNaN(wantf) { if !math.IsNaN(f) { - t.Errorf("Unmarshal(0x%x) = %f, want NaN", cborData, f) + t.Errorf("Unmarshal(0x%x) = %f, want NaN", data, f) } } else if math.IsInf(wantf, 0) { if f != wantf { - t.Errorf("Unmarshal(0x%x) = %f, want %f", cborData, f, wantf) + t.Errorf("Unmarshal(0x%x) = %f, want %f", data, f, wantf) } } else if math.Abs(f-wantf) > equalityThreshold { - t.Errorf("Unmarshal(0x%x) = %.18f, want %.18f, diff %.18f > threshold %.18f", cborData, f, wantf, math.Abs(f-wantf), equalityThreshold) + t.Errorf("Unmarshal(0x%x) = %.18f, want %.18f, diff %.18f > threshold %.18f", data, f, wantf, math.Abs(f-wantf), equalityThreshold) } } } func TestNegIntOverflow(t *testing.T) { - cborData := hexDecode("3bffffffffffffffff") // -18446744073709551616 + data := hexDecode("3bffffffffffffffff") // -18446744073709551616 // Decode CBOR neg int that overflows Go int64 to empty interface var v1 interface{} wantObj := bigIntOrPanic("-18446744073709551616") - if err := Unmarshal(cborData, &v1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", cborData, err) + if err := Unmarshal(data, &v1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", data, err) } else if !reflect.DeepEqual(v1, wantObj) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", cborData, v1, v1, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", data, v1, v1, wantObj, wantObj) } // Decode CBOR neg int that overflows Go int64 to big.Int var v2 big.Int - if err := Unmarshal(cborData, &v2); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", cborData, err) + if err := Unmarshal(data, &v2); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", data, err) } else if !reflect.DeepEqual(v2, wantObj) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", cborData, v2, v2, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", data, v2, v2, wantObj, wantObj) } // Decode CBOR neg int that overflows Go int64 to int64 var v3 int64 - if err := Unmarshal(cborData, &v3); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &v3); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), "cannot unmarshal") { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), "cannot unmarshal") + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), "cannot unmarshal") } } @@ -2470,7 +2470,7 @@ func TestUnmarshalIntoPtrPrimitives(t *testing.T) { } func TestUnmarshalIntoPtrArrayPtrElem(t *testing.T) { - cborData := hexDecode("83010203") // []int{1, 2, 3} + data := hexDecode("83010203") // []int{1, 2, 3} n1, n2, n3 := 1, 2, 3 @@ -2483,21 +2483,21 @@ func TestUnmarshalIntoPtrArrayPtrElem(t *testing.T) { ppslc := &pslc // Unmarshal CBOR array into a non-nil pointer. - if err := Unmarshal(cborData, &ppslc); err != nil { - t.Errorf("Unmarshal(0x%x, %s) returned error %v", cborData, reflect.TypeOf(ppslc), err) + if err := Unmarshal(data, &ppslc); err != nil { + t.Errorf("Unmarshal(0x%x, %s) returned error %v", data, reflect.TypeOf(ppslc), err) } else if !reflect.DeepEqual(slc, wantArray) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", cborData, slc, slc, wantArray) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", data, slc, slc, wantArray) } // Unmarshal CBOR array into a nil pointer. - if err := Unmarshal(cborData, &p); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &p); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(*p, wantArray) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", cborData, *p, p, wantArray) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", data, *p, p, wantArray) } } func TestUnmarshalIntoPtrMapPtrElem(t *testing.T) { - cborData := hexDecode("a201020304") // {1: 2, 3: 4} + data := hexDecode("a201020304") // {1: 2, 3: 4} n1, n2, n3, n4 := 1, 2, 3, 4 @@ -2510,16 +2510,16 @@ func TestUnmarshalIntoPtrMapPtrElem(t *testing.T) { ppm := &pm // Unmarshal CBOR map into a non-nil pointer. - if err := Unmarshal(cborData, &ppm); err != nil { - t.Errorf("Unmarshal(0x%x, %s) returned error %v", cborData, reflect.TypeOf(ppm), err) + if err := Unmarshal(data, &ppm); err != nil { + t.Errorf("Unmarshal(0x%x, %s) returned error %v", data, reflect.TypeOf(ppm), err) } else if !reflect.DeepEqual(m, wantMap) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", cborData, m, m, wantMap) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", data, m, m, wantMap) } // Unmarshal CBOR map into a nil pointer. - if err := Unmarshal(cborData, &p); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &p); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(*p, wantMap) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", cborData, *p, p, wantMap) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", data, *p, p, wantMap) } } @@ -2532,7 +2532,7 @@ func TestUnmarshalIntoPtrStructPtrElem(t *testing.T) { E *string `cbor:"e"` } - cborData := hexDecode("a56161614161626142616361436164614461656145") // map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"} + data := hexDecode("a56161614161626142616361436164614461656145") // map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"} a, b, c, d, e := "A", "B", "C", "D", "E" wantObj := s1{A: &a, B: &b, C: &c, D: &d, E: &e} @@ -2544,44 +2544,44 @@ func TestUnmarshalIntoPtrStructPtrElem(t *testing.T) { pps := &ps // Unmarshal CBOR map into a non-nil pointer. - if err := Unmarshal(cborData, &pps); err != nil { - t.Errorf("Unmarshal(0x%x, %s) returned error %v", cborData, reflect.TypeOf(pps), err) + if err := Unmarshal(data, &pps); err != nil { + t.Errorf("Unmarshal(0x%x, %s) returned error %v", data, reflect.TypeOf(pps), err) } else if !reflect.DeepEqual(s, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", cborData, s, s, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", data, s, s, wantObj) } // Unmarshal CBOR map into a nil pointer. - if err := Unmarshal(cborData, &p); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &p); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(*p, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", cborData, *p, p, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v", data, *p, p, wantObj) } } func TestUnmarshalIntoArray(t *testing.T) { - cborData := hexDecode("83010203") // []int{1, 2, 3} + data := hexDecode("83010203") // []int{1, 2, 3} // Unmarshal CBOR array into Go array. var arr1 [3]int - if err := Unmarshal(cborData, &arr1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &arr1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if arr1 != [3]int{1, 2, 3} { - t.Errorf("Unmarshal(0x%x) = %v (%T), want [3]int{1, 2, 3}", cborData, arr1, arr1) + t.Errorf("Unmarshal(0x%x) = %v (%T), want [3]int{1, 2, 3}", data, arr1, arr1) } // Unmarshal CBOR array into Go array with more elements. var arr2 [5]int - if err := Unmarshal(cborData, &arr2); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &arr2); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if arr2 != [5]int{1, 2, 3, 0, 0} { - t.Errorf("Unmarshal(0x%x) = %v (%T), want [5]int{1, 2, 3, 0, 0}", cborData, arr2, arr2) + t.Errorf("Unmarshal(0x%x) = %v (%T), want [5]int{1, 2, 3, 0, 0}", data, arr2, arr2) } // Unmarshal CBOR array into Go array with less elements. var arr3 [1]int - if err := Unmarshal(cborData, &arr3); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &arr3); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if arr3 != [1]int{1} { - t.Errorf("Unmarshal(0x%x) = %v (%T), want [0]int{1}", cborData, arr3, arr3) + t.Errorf("Unmarshal(0x%x) = %v (%T), want [0]int{1}", data, arr3, arr3) } } @@ -2601,7 +2601,7 @@ func TestUnmarshalNil(t *testing.T) { I int } - cborData := [][]byte{hexDecode("f6"), hexDecode("f7")} // CBOR null and undefined values + data := [][]byte{hexDecode("f6"), hexDecode("f7")} // CBOR null and undefined values testCases := []struct { name string @@ -2659,7 +2659,7 @@ func TestUnmarshalNil(t *testing.T) { // Unmarshalling to values of specified Go types. for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - for _, data := range cborData { + for _, data := range data { v := reflect.New(reflect.TypeOf(tc.value)) v.Elem().Set(reflect.ValueOf(tc.value)) @@ -2684,17 +2684,17 @@ var invalidUnmarshalTests = []struct { } func TestInvalidUnmarshal(t *testing.T) { - cborData := []byte{0x00} + data := []byte{0x00} for _, tc := range invalidUnmarshalTests { t.Run(tc.name, func(t *testing.T) { - err := Unmarshal(cborData, tc.v) + err := Unmarshal(data, tc.v) if err == nil { - t.Errorf("Unmarshal(0x%x, %v) didn't return an error", cborData, tc.v) + t.Errorf("Unmarshal(0x%x, %v) didn't return an error", data, tc.v) } else if _, ok := err.(*InvalidUnmarshalError); !ok { - t.Errorf("Unmarshal(0x%x, %v) error type %T, want *InvalidUnmarshalError", cborData, tc.v, err) + t.Errorf("Unmarshal(0x%x, %v) error type %T, want *InvalidUnmarshalError", data, tc.v, err) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x, %v) error %q, want %q", cborData, tc.v, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x, %v) error %q, want %q", data, tc.v, err.Error(), tc.wantErrorMsg) } }) } @@ -2702,7 +2702,7 @@ func TestInvalidUnmarshal(t *testing.T) { var invalidCBORUnmarshalTests = []struct { name string - cborData []byte + data []byte wantErrorMsg string errorMsgPartialMatch bool }{ @@ -2814,13 +2814,13 @@ func TestInvalidCBORUnmarshal(t *testing.T) { for _, tc := range invalidCBORUnmarshalTests { t.Run(tc.name, func(t *testing.T) { var i interface{} - err := Unmarshal(tc.cborData, &i) + err := Unmarshal(tc.data, &i) if err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", tc.cborData) + t.Errorf("Unmarshal(0x%x) didn't return an error", tc.data) } else if !tc.errorMsgPartialMatch && err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } else if tc.errorMsgPartialMatch && !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } }) } @@ -2837,56 +2837,56 @@ func TestValidUTF8String(t *testing.T) { } testCases := []struct { - name string - cborData []byte - dm DecMode - wantObj interface{} + name string + data []byte + dm DecMode + wantObj interface{} }{ { - name: "with UTF8RejectInvalid", - cborData: hexDecode("6973747265616d696e67"), - dm: dmRejectInvalidUTF8, - wantObj: "streaming", + name: "with UTF8RejectInvalid", + data: hexDecode("6973747265616d696e67"), + dm: dmRejectInvalidUTF8, + wantObj: "streaming", }, { - name: "with UTF8DecodeInvalid", - cborData: hexDecode("6973747265616d696e67"), - dm: dmDecodeInvalidUTF8, - wantObj: "streaming", + name: "with UTF8DecodeInvalid", + data: hexDecode("6973747265616d696e67"), + dm: dmDecodeInvalidUTF8, + wantObj: "streaming", }, { - name: "indef length with UTF8RejectInvalid", - cborData: hexDecode("7f657374726561646d696e67ff"), - dm: dmRejectInvalidUTF8, - wantObj: "streaming", + name: "indef length with UTF8RejectInvalid", + data: hexDecode("7f657374726561646d696e67ff"), + dm: dmRejectInvalidUTF8, + wantObj: "streaming", }, { - name: "indef length with UTF8DecodeInvalid", - cborData: hexDecode("7f657374726561646d696e67ff"), - dm: dmDecodeInvalidUTF8, - wantObj: "streaming", + name: "indef length with UTF8DecodeInvalid", + data: hexDecode("7f657374726561646d696e67ff"), + dm: dmDecodeInvalidUTF8, + wantObj: "streaming", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { // Decode to empty interface var i interface{} - err = tc.dm.Unmarshal(tc.cborData, &i) + err = tc.dm.Unmarshal(tc.data, &i) if err != nil { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } if !reflect.DeepEqual(i, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, i, i, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, i, i, tc.wantObj, tc.wantObj) } // Decode to string var v string - err = tc.dm.Unmarshal(tc.cborData, &v) + err = tc.dm.Unmarshal(tc.data, &v) if err != nil { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } if !reflect.DeepEqual(v, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v, v, tc.wantObj, tc.wantObj) } }) } @@ -2904,34 +2904,34 @@ func TestInvalidUTF8String(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte dm DecMode wantObj interface{} wantErrorMsg string }{ { name: "with UTF8RejectInvalid", - cborData: hexDecode("61fe"), + data: hexDecode("61fe"), dm: dmRejectInvalidUTF8, wantErrorMsg: invalidUTF8ErrorMsg, }, { - name: "with UTF8DecodeInvalid", - cborData: hexDecode("61fe"), - dm: dmDecodeInvalidUTF8, - wantObj: string([]byte{0xfe}), + name: "with UTF8DecodeInvalid", + data: hexDecode("61fe"), + dm: dmDecodeInvalidUTF8, + wantObj: string([]byte{0xfe}), }, { name: "indef length with UTF8RejectInvalid", - cborData: hexDecode("7f62e6b061b4ff"), + data: hexDecode("7f62e6b061b4ff"), dm: dmRejectInvalidUTF8, wantErrorMsg: invalidUTF8ErrorMsg, }, { - name: "indef length with UTF8DecodeInvalid", - cborData: hexDecode("7f62e6b061b4ff"), - dm: dmDecodeInvalidUTF8, - wantObj: string([]byte{0xe6, 0xb0, 0xb4}), + name: "indef length with UTF8DecodeInvalid", + data: hexDecode("7f62e6b061b4ff"), + dm: dmDecodeInvalidUTF8, + wantObj: string([]byte{0xe6, 0xb0, 0xb4}), }, } @@ -2939,37 +2939,37 @@ func TestInvalidUTF8String(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // Decode to empty interface var v interface{} - err = tc.dm.Unmarshal(tc.cborData, &v) + err = tc.dm.Unmarshal(tc.data, &v) if tc.wantErrorMsg != "" { if err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error", tc.cborData) + t.Errorf("Unmarshal(0x%x) didn't return error", tc.data) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } else { if err != nil { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } if !reflect.DeepEqual(v, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v, v, tc.wantObj, tc.wantObj) } } // Decode to string var s string - err = tc.dm.Unmarshal(tc.cborData, &s) + err = tc.dm.Unmarshal(tc.data, &s) if tc.wantErrorMsg != "" { if err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error", tc.cborData) + t.Errorf("Unmarshal(0x%x) didn't return error", tc.data) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } else { if err != nil { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } if !reflect.DeepEqual(s, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, s, s, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, s, s, tc.wantObj, tc.wantObj) } } }) @@ -2977,8 +2977,8 @@ func TestInvalidUTF8String(t *testing.T) { // Test decoding of mixed invalid text string and valid text string // with UTF8RejectInvalid option (default) - cborData := hexDecode("7f62e6b061b4ff7f657374726561646d696e67ff") - dec := NewDecoder(bytes.NewReader(cborData)) + data := hexDecode("7f62e6b061b4ff7f657374726561646d696e67ff") + dec := NewDecoder(bytes.NewReader(data)) var s string if err := dec.Decode(&s); err == nil { t.Errorf("Decode() didn't return an error") @@ -2993,7 +2993,7 @@ func TestInvalidUTF8String(t *testing.T) { // Test decoding of mixed invalid text string and valid text string // with UTF8DecodeInvalid option - dec = dmDecodeInvalidUTF8.NewDecoder(bytes.NewReader(cborData)) + dec = dmDecodeInvalidUTF8.NewDecoder(bytes.NewReader(data)) if err := dec.Decode(&s); err != nil { t.Errorf("Decode() returned error %q", err) } else if s != string([]byte{0xe6, 0xb0, 0xb4}) { @@ -3020,9 +3020,9 @@ func TestUnmarshalStruct(t *testing.T) { } tests := []struct { - name string - cborData []byte - want interface{} + name string + data []byte + want interface{} }{ {"case-insensitive field name match", hexDecode("a868696e746669656c64187b6a666c6f61746669656c64fa47c3500069626f6f6c6669656c64f56b537472696e674669656c6464746573746f42797465537472696e674669656c64430103056a41727261794669656c64826568656c6c6f65776f726c64684d61704669656c64a2676d6f726e696e67f56961667465726e6f6f6ef4714e65737465645374727563744669656c64a261581903e861591a000f4240"), want}, {"exact field name match", hexDecode("a868496e744669656c64187b6a466c6f61744669656c64fa47c3500069426f6f6c4669656c64f56b537472696e674669656c6464746573746f42797465537472696e674669656c64430103056a41727261794669656c64826568656c6c6f65776f726c64684d61704669656c64a2676d6f726e696e67f56961667465726e6f6f6ef4714e65737465645374727563744669656c64a261581903e861591a000f4240"), want}, @@ -3030,10 +3030,10 @@ func TestUnmarshalStruct(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { var v outer - if err := Unmarshal(tc.cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", tc.cborData, err) + if err := Unmarshal(tc.data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", tc.data, err) } else if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v, v, want, want) } }) } @@ -3063,35 +3063,35 @@ func TestUnmarshalStructError1(t *testing.T) { unexportedField: 0, } - cborData := hexDecode("a868496e744669656c64187b6a466c6f61744669656c64fa47c3500069426f6f6c4669656c64f56b537472696e674669656c6464746573746f42797465537472696e674669656c64430103056a41727261794669656c64826568656c6c6f65776f726c64684d61704669656c64a2676d6f726e696e67f56961667465726e6f6f6ef4714e65737465645374727563744669656c64a261581903e861591a000f4240") + data := hexDecode("a868496e744669656c64187b6a466c6f61744669656c64fa47c3500069426f6f6c4669656c64f56b537472696e674669656c6464746573746f42797465537472696e674669656c64430103056a41727261794669656c64826568656c6c6f65776f726c64684d61704669656c64a2676d6f726e696e67f56961667465726e6f6f6ef4714e65737465645374727563744669656c64a261581903e861591a000f4240") wantCBORType := "UTF-8 text string" wantGoType := "int" wantStructFieldName := "cbor.outer2.ArrayField" wantErrorMsg := "cannot unmarshal UTF-8 text string into Go struct field cbor.outer2.ArrayField of type int" var v outer2 - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else { if typeError, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*UnmarshalTypeError)", data, err) } else { if typeError.CBORType != wantCBORType { - t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).CBORType %s, want %s", cborData, typeError.CBORType, wantCBORType) + t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).CBORType %s, want %s", data, typeError.CBORType, wantCBORType) } if typeError.GoType != wantGoType { - t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).GoType %s, want %s", cborData, typeError.GoType, wantGoType) + t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).GoType %s, want %s", data, typeError.GoType, wantGoType) } if typeError.StructFieldName != wantStructFieldName { - t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).StructFieldName %s, want %s", cborData, typeError.StructFieldName, wantStructFieldName) + t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).StructFieldName %s, want %s", data, typeError.StructFieldName, wantStructFieldName) } if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } } } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, want, want) } } @@ -3107,108 +3107,108 @@ func TestUnmarshalStructError2(t *testing.T) { } // Unmarshal returns first error encountered, which is *UnmarshalTypeError (failed to unmarshal int into Go string) - cborData := hexDecode("a3fa47c35000026161614161fe6142") // {100000.0:2, "a":"A", 0xfe: B} + data := hexDecode("a3fa47c35000026161614161fe6142") // {100000.0:2, "a":"A", 0xfe: B} wantCBORType := "primitives" wantGoType := "string" wantErrorMsg := "cannot unmarshal primitives into Go value of type string" v := strc{} - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else { if typeError, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*UnmarshalTypeError)", data, err) } else { if typeError.CBORType != wantCBORType { - t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).CBORType %s, want %s", cborData, typeError.CBORType, wantCBORType) + t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).CBORType %s, want %s", data, typeError.CBORType, wantCBORType) } if typeError.GoType != wantGoType { - t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).GoType %s, want %s", cborData, typeError.GoType, wantGoType) + t.Errorf("Unmarshal(0x%x) returned (*UnmarshalTypeError).GoType %s, want %s", data, typeError.GoType, wantGoType) } if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } } } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, want, want) } // Unmarshal returns first error encountered, which is *cbor.SemanticError (invalid UTF8 string) - cborData = hexDecode("a361fe6142010261616141") // {0xfe: B, 1:2, "a":"A"} + data = hexDecode("a361fe6142010261616141") // {0xfe: B, 1:2, "a":"A"} v = strc{} - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else { if _, ok := err.(*SemanticError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*SemanticError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*SemanticError)", data, err) } else if err.Error() != invalidUTF8ErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", cborData, err.Error(), invalidUTF8ErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", data, err.Error(), invalidUTF8ErrorMsg) } } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, want, want) } // Unmarshal returns first error encountered, which is *cbor.SemanticError (invalid UTF8 string) - cborData = hexDecode("a3616261fe010261616141") // {"b": 0xfe, 1:2, "a":"A"} + data = hexDecode("a3616261fe010261616141") // {"b": 0xfe, 1:2, "a":"A"} v = strc{} - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else { if _, ok := err.(*SemanticError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*SemanticError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*SemanticError)", data, err) } else if err.Error() != invalidUTF8ErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", cborData, err.Error(), invalidUTF8ErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", data, err.Error(), invalidUTF8ErrorMsg) } } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, want, want) } } func TestUnmarshalPrefilledArray(t *testing.T) { prefilledArr := []int{1, 2, 3, 4, 5} want := []int{10, 11, 3, 4, 5} - cborData := hexDecode("820a0b") // []int{10, 11} - if err := Unmarshal(cborData, &prefilledArr); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data := hexDecode("820a0b") // []int{10, 11} + if err := Unmarshal(data, &prefilledArr); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if len(prefilledArr) != 2 || cap(prefilledArr) != 5 { - t.Errorf("Unmarshal(0x%x) = %v (len %d, cap %d), want len == 2, cap == 5", cborData, prefilledArr, len(prefilledArr), cap(prefilledArr)) + t.Errorf("Unmarshal(0x%x) = %v (len %d, cap %d), want len == 2, cap == 5", data, prefilledArr, len(prefilledArr), cap(prefilledArr)) } if !reflect.DeepEqual(prefilledArr[:cap(prefilledArr)], want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, prefilledArr, prefilledArr, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, prefilledArr, prefilledArr, want, want) } - cborData = hexDecode("80") // empty array - if err := Unmarshal(cborData, &prefilledArr); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data = hexDecode("80") // empty array + if err := Unmarshal(data, &prefilledArr); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if len(prefilledArr) != 0 || cap(prefilledArr) != 0 { - t.Errorf("Unmarshal(0x%x) = %v (len %d, cap %d), want len == 0, cap == 0", cborData, prefilledArr, len(prefilledArr), cap(prefilledArr)) + t.Errorf("Unmarshal(0x%x) = %v (len %d, cap %d), want len == 0, cap == 0", data, prefilledArr, len(prefilledArr), cap(prefilledArr)) } } func TestUnmarshalPrefilledMap(t *testing.T) { prefilledMap := map[string]string{"key": "value", "a": "1"} want := map[string]string{"key": "value", "a": "A", "b": "B", "c": "C", "d": "D", "e": "E"} - cborData := hexDecode("a56161614161626142616361436164614461656145") // map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"} - if err := Unmarshal(cborData, &prefilledMap); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data := hexDecode("a56161614161626142616361436164614461656145") // map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"} + if err := Unmarshal(data, &prefilledMap); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(prefilledMap, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, prefilledMap, prefilledMap, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, prefilledMap, prefilledMap, want, want) } prefilledMap = map[string]string{"key": "value"} want = map[string]string{"key": "value"} - cborData = hexDecode("a0") // map[string]string{} - if err := Unmarshal(cborData, &prefilledMap); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data = hexDecode("a0") // map[string]string{} + if err := Unmarshal(data, &prefilledMap); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(prefilledMap, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, prefilledMap, prefilledMap, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, prefilledMap, prefilledMap, want, want) } } @@ -3220,18 +3220,18 @@ func TestUnmarshalPrefilledStruct(t *testing.T) { } prefilledStruct := s{a: 100, B: []int{200, 300, 400, 500}, C: true} want := s{a: 100, B: []int{2, 3}, C: true} - cborData := hexDecode("a26161016162820203") // map[string]interface{} {"a": 1, "b": []int{2, 3}} - if err := Unmarshal(cborData, &prefilledStruct); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data := hexDecode("a26161016162820203") // map[string]interface{} {"a": 1, "b": []int{2, 3}} + if err := Unmarshal(data, &prefilledStruct); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(prefilledStruct, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, prefilledStruct, prefilledStruct, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, prefilledStruct, prefilledStruct, want, want) } if len(prefilledStruct.B) != 2 || cap(prefilledStruct.B) != 4 { - t.Errorf("Unmarshal(0x%x) = %v (len %d, cap %d), want len == 2, cap == 5", cborData, prefilledStruct.B, len(prefilledStruct.B), cap(prefilledStruct.B)) + t.Errorf("Unmarshal(0x%x) = %v (len %d, cap %d), want len == 2, cap == 5", data, prefilledStruct.B, len(prefilledStruct.B), cap(prefilledStruct.B)) } if !reflect.DeepEqual(prefilledStruct.B[:cap(prefilledStruct.B)], []int{2, 3, 400, 500}) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, prefilledStruct.B, prefilledStruct.B, []int{2, 3, 400, 500}, []int{2, 3, 400, 500}) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, prefilledStruct.B, prefilledStruct.B, []int{2, 3, 400, 500}, []int{2, 3, 400, 500}) } } @@ -3242,28 +3242,28 @@ func TestStructFieldNil(t *testing.T) { PPI **int } var struc TestStruct - cborData, err := Marshal(struc) + data, err := Marshal(struc) if err != nil { t.Fatalf("Marshal(%+v) returned error %v", struc, err) } var struc2 TestStruct - err = Unmarshal(cborData, &struc2) + err = Unmarshal(data, &struc2) if err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(struc, struc2) { - t.Errorf("Unmarshal(0x%x) returned %+v, want %+v", cborData, struc2, struc) + t.Errorf("Unmarshal(0x%x) returned %+v, want %+v", data, struc2, struc) } } func TestLengthOverflowsInt(t *testing.T) { // Data is generating by go-fuzz. // string/slice/map length in uint64 cast to int causes integer overflow. - cborData := [][]byte{ + data := [][]byte{ hexDecode("bbcf30303030303030cfd697829782"), hexDecode("5bcf30303030303030cfd697829782"), } wantErrorMsg := "is too large" - for _, data := range cborData { + for _, data := range data { var intf interface{} if err := Unmarshal(data, &intf); err == nil { t.Errorf("Unmarshal(0x%x) didn't return an error, want error containing substring %q", data, wantErrorMsg) @@ -3276,7 +3276,7 @@ func TestLengthOverflowsInt(t *testing.T) { func TestMapKeyUnhashable(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantErrorMsg string }{ {"slice as map key", hexDecode("bf8030ff"), "cbor: invalid map key type: []interface {}"}, // {[]: -17} @@ -3291,17 +3291,17 @@ func TestMapKeyUnhashable(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v interface{} - if err := Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.cborData, tc.wantErrorMsg) + if err := Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } if _, ok := v.(map[interface{}]interface{}); ok { var v map[interface{}]interface{} - if err := Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.cborData, tc.wantErrorMsg) + if err := Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } }) @@ -3310,10 +3310,10 @@ func TestMapKeyUnhashable(t *testing.T) { func TestMapKeyNaN(t *testing.T) { // Data is generating by go-fuzz. - cborData := hexDecode("b0303030303030303030303030303030303038303030faffff30303030303030303030303030") // {-17: -17, NaN: -17} + data := hexDecode("b0303030303030303030303030303030303038303030faffff30303030303030303030303030") // {-17: -17, NaN: -17} var intf interface{} - if err := Unmarshal(cborData, &intf); err != nil { - t.Fatalf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &intf); err != nil { + t.Fatalf("Unmarshal(0x%x) returned error %v", data, err) } em, err := EncOptions{Sort: SortCanonical}.EncMode() if err != nil { @@ -3326,13 +3326,13 @@ func TestMapKeyNaN(t *testing.T) { func TestUnmarshalUndefinedElement(t *testing.T) { // Data is generating by go-fuzz. - cborData := hexDecode("bfd1a388f730303030303030303030303030ff") // {17({[undefined, -17, -17, -17, -17, -17, -17, -17]: -17, -17: -17}): -17} + data := hexDecode("bfd1a388f730303030303030303030303030ff") // {17({[undefined, -17, -17, -17, -17, -17, -17, -17]: -17, -17: -17}): -17} var intf interface{} wantErrorMsg := "invalid map key type" - if err := Unmarshal(cborData, &intf); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want error containing substring %q", cborData, wantErrorMsg) + if err := Unmarshal(data, &intf); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want error containing substring %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing substring %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing substring %q", data, err.Error(), wantErrorMsg) } } @@ -3493,42 +3493,42 @@ func TestDecodeTimeWithTag(t *testing.T) { func TestDecodeTimeError(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantErrorMsg string }{ { name: "invalid RFC3339 time string", - cborData: hexDecode("7f657374726561646d696e67ff"), + data: hexDecode("7f657374726561646d696e67ff"), wantErrorMsg: "cbor: cannot set streaming for time.Time", }, { name: "byte string data cannot be decoded into time.Time", - cborData: hexDecode("4f013030303030303030e03031ed3030"), + data: hexDecode("4f013030303030303030e03031ed3030"), wantErrorMsg: "cbor: cannot unmarshal byte string into Go value of type time.Time", }, { name: "bool cannot be decoded into time.Time", - cborData: hexDecode("f4"), + data: hexDecode("f4"), wantErrorMsg: "cbor: cannot unmarshal primitives into Go value of type time.Time", }, { name: "invalid UTF-8 string", - cborData: hexDecode("7f62e6b061b4ff"), + data: hexDecode("7f62e6b061b4ff"), wantErrorMsg: "cbor: invalid UTF-8 string", }, { name: "negative integer overflow", - cborData: hexDecode("3bffffffffffffffff"), + data: hexDecode("3bffffffffffffffff"), wantErrorMsg: "cbor: cannot unmarshal negative integer into Go value of type time.Time", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { tm := time.Now() - if err := Unmarshal(tc.cborData, &tm); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want error msg %q", tc.cborData, tc.wantErrorMsg) + if err := Unmarshal(tc.data, &tm); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want error msg %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } }) } @@ -3539,61 +3539,61 @@ func TestDecodeInvalidTagTime(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte decodeToTypes []reflect.Type wantErrorMsg string }{ { name: "Tag 0 with invalid RFC3339 time string", - cborData: hexDecode("c07f657374726561646d696e67ff"), + data: hexDecode("c07f657374726561646d696e67ff"), decodeToTypes: []reflect.Type{typeIntf, typeTime}, wantErrorMsg: "cbor: cannot set streaming for time.Time", }, { name: "Tag 0 with invalid UTF-8 string", - cborData: hexDecode("c07f62e6b061b4ff"), + data: hexDecode("c07f62e6b061b4ff"), decodeToTypes: []reflect.Type{typeIntf, typeTime}, wantErrorMsg: "cbor: invalid UTF-8 string", }, { name: "Tag 0 with integer content", - cborData: hexDecode("c01a514b67b0"), + data: hexDecode("c01a514b67b0"), decodeToTypes: []reflect.Type{typeIntf, typeTime}, wantErrorMsg: "cbor: tag number 0 must be followed by text string, got positive integer", }, { name: "Tag 0 with byte string content", - cborData: hexDecode("c04f013030303030303030e03031ed3030"), + data: hexDecode("c04f013030303030303030e03031ed3030"), decodeToTypes: []reflect.Type{typeIntf, typeTime}, wantErrorMsg: "cbor: tag number 0 must be followed by text string, got byte string", }, { name: "Tag 0 with integer content as array element", - cborData: hexDecode("81c01a514b67b0"), + data: hexDecode("81c01a514b67b0"), decodeToTypes: []reflect.Type{typeIntf, typeTimeSlice}, wantErrorMsg: "cbor: tag number 0 must be followed by text string, got positive integer", }, { name: "Tag 1 with negative integer overflow", - cborData: hexDecode("c13bffffffffffffffff"), + data: hexDecode("c13bffffffffffffffff"), decodeToTypes: []reflect.Type{typeIntf, typeTime}, wantErrorMsg: "cbor: cannot unmarshal tag into Go value of type time.Time", }, { name: "Tag 1 with string content", - cborData: hexDecode("c174323031332d30332d32315432303a30343a30305a"), + data: hexDecode("c174323031332d30332d32315432303a30343a30305a"), decodeToTypes: []reflect.Type{typeIntf, typeTime}, wantErrorMsg: "cbor: tag number 1 must be followed by integer or floating-point number, got UTF-8 text string", }, { name: "Tag 1 with simple value", - cborData: hexDecode("d801f6"), // 1(null) + data: hexDecode("d801f6"), // 1(null) decodeToTypes: []reflect.Type{typeIntf, typeTime}, wantErrorMsg: "cbor: tag number 1 must be followed by integer or floating-point number, got primitive", }, { name: "Tag 1 with string content as array element", - cborData: hexDecode("81c174323031332d30332d32315432303a30343a30305a"), + data: hexDecode("81c174323031332d30332d32315432303a30343a30305a"), decodeToTypes: []reflect.Type{typeIntf, typeTimeSlice}, wantErrorMsg: "cbor: tag number 1 must be followed by integer or floating-point number, got UTF-8 text string", }, @@ -3603,10 +3603,10 @@ func TestDecodeInvalidTagTime(t *testing.T) { for _, decodeToType := range tc.decodeToTypes { t.Run(tc.name+" decode to "+decodeToType.String(), func(t *testing.T) { v := reflect.New(decodeToType) - if err := dm.Unmarshal(tc.cborData, v.Interface()); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", tc.cborData, tc.wantErrorMsg) + if err := dm.Unmarshal(tc.data, v.Interface()); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err, tc.wantErrorMsg) } }) } @@ -3614,7 +3614,7 @@ func TestDecodeInvalidTagTime(t *testing.T) { } func TestDecodeTag0Error(t *testing.T) { - cborData := hexDecode("c01a514b67b0") // 0(1363896240) + data := hexDecode("c01a514b67b0") // 0(1363896240) wantErrorMsg := "cbor: tag number 0 must be followed by text string, got positive integer" timeTagIgnoredDM, _ := DecOptions{TimeTag: DecTagIgnored}.DecMode() @@ -3634,33 +3634,33 @@ func TestDecodeTag0Error(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // Decode to interface{} var v interface{} - if err := tc.dm.Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", cborData, wantErrorMsg) + if err := tc.dm.Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err, wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err, wantErrorMsg) } // Decode to time.Time var tm time.Time - if err := tc.dm.Unmarshal(cborData, &tm); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", cborData, wantErrorMsg) + if err := tc.dm.Unmarshal(data, &tm); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err, wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err, wantErrorMsg) } // Decode to uint64 var ui uint64 - if err := tc.dm.Unmarshal(cborData, &ui); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", cborData, wantErrorMsg) + if err := tc.dm.Unmarshal(data, &ui); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err, wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err, wantErrorMsg) } }) } } func TestDecodeTag1Error(t *testing.T) { - cborData := hexDecode("c174323031332d30332d32315432303a30343a30305a") // 1("2013-03-21T20:04:00Z") + data := hexDecode("c174323031332d30332d32315432303a30343a30305a") // 1("2013-03-21T20:04:00Z") wantErrorMsg := "cbor: tag number 1 must be followed by integer or floating-point number, got UTF-8 text string" timeTagIgnoredDM, _ := DecOptions{TimeTag: DecTagIgnored}.DecMode() @@ -3680,26 +3680,26 @@ func TestDecodeTag1Error(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // Decode to interface{} var v interface{} - if err := tc.dm.Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", cborData, wantErrorMsg) + if err := tc.dm.Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err, wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err, wantErrorMsg) } // Decode to time.Time var tm time.Time - if err := tc.dm.Unmarshal(cborData, &tm); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", cborData, wantErrorMsg) + if err := tc.dm.Unmarshal(data, &tm); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err, wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err, wantErrorMsg) } // Decode to string var s string - if err := tc.dm.Unmarshal(cborData, &s); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", cborData, wantErrorMsg) + if err := tc.dm.Unmarshal(data, &s); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err, wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err, wantErrorMsg) } }) } @@ -3708,82 +3708,82 @@ func TestDecodeTag1Error(t *testing.T) { func TestDecodeTimeStreaming(t *testing.T) { // Decoder decodes from mixed invalid and valid time. testCases := []struct { - cborData []byte + data []byte wantErrorMsg string wantObj time.Time }{ { - cborData: hexDecode("c07f62e6b061b4ff"), + data: hexDecode("c07f62e6b061b4ff"), wantErrorMsg: "cbor: invalid UTF-8 string", }, { - cborData: hexDecode("c074323031332d30332d32315432303a30343a30305a"), - wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), + data: hexDecode("c074323031332d30332d32315432303a30343a30305a"), + wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), }, { - cborData: hexDecode("c01a514b67b0"), + data: hexDecode("c01a514b67b0"), wantErrorMsg: "cbor: tag number 0 must be followed by text string, got positive integer", }, { - cborData: hexDecode("c074323031332d30332d32315432303a30343a30305a"), - wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), + data: hexDecode("c074323031332d30332d32315432303a30343a30305a"), + wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), }, { - cborData: hexDecode("c13bffffffffffffffff"), + data: hexDecode("c13bffffffffffffffff"), wantErrorMsg: "cbor: cannot unmarshal tag into Go value of type time.Time", }, { - cborData: hexDecode("c11a514b67b0"), - wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), + data: hexDecode("c11a514b67b0"), + wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), }, { - cborData: hexDecode("c174323031332d30332d32315432303a30343a30305a"), + data: hexDecode("c174323031332d30332d32315432303a30343a30305a"), wantErrorMsg: "tag number 1 must be followed by integer or floating-point number, got UTF-8 text string", }, { - cborData: hexDecode("c11a514b67b0"), - wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), + data: hexDecode("c11a514b67b0"), + wantObj: time.Date(2013, 3, 21, 20, 4, 0, 0, time.UTC), }, } // Data is a mixed stream of valid and invalid time data - var cborData []byte + var data []byte for _, tc := range testCases { - cborData = append(cborData, tc.cborData...) + data = append(data, tc.data...) } dm, _ := DecOptions{TimeTag: DecTagOptional}.DecMode() - dec := dm.NewDecoder(bytes.NewReader(cborData)) + dec := dm.NewDecoder(bytes.NewReader(data)) for _, tc := range testCases { var v interface{} err := dec.Decode(&v) if tc.wantErrorMsg != "" { if err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", tc.cborData, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error msg %q, want %q", tc.cborData, err, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error msg %q, want %q", tc.data, err, tc.wantErrorMsg) } } else { tm, ok := v.(time.Time) if !ok { - t.Errorf("Unmarshal(0x%x) returned %s (%T), want time.Time", tc.cborData, v, v) + t.Errorf("Unmarshal(0x%x) returned %s (%T), want time.Time", tc.data, v, v) } if !tc.wantObj.Equal(tm) { - t.Errorf("Unmarshal(0x%x) returned %s, want %s", tc.cborData, tm, tc.wantObj) + t.Errorf("Unmarshal(0x%x) returned %s, want %s", tc.data, tm, tc.wantObj) } } } - dec = dm.NewDecoder(bytes.NewReader(cborData)) + dec = dm.NewDecoder(bytes.NewReader(data)) for _, tc := range testCases { var tm time.Time err := dec.Decode(&tm) if tc.wantErrorMsg != "" { if err == nil { - t.Errorf("Unmarshal(0x%x) did't return error, want error msg %q", tc.cborData, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) did't return error, want error msg %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error msg %q, want %q", tc.cborData, err, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error msg %q, want %q", tc.data, err, tc.wantErrorMsg) } } else { if !tc.wantObj.Equal(tm) { - t.Errorf("Unmarshal(0x%x) returned %s, want %s", tc.cborData, tm, tc.wantObj) + t.Errorf("Unmarshal(0x%x) returned %s, want %s", tc.data, tm, tc.wantObj) } } } @@ -3913,14 +3913,14 @@ func TestUnmarshalStructTag1(t *testing.T) { B: "B", C: "C", } - cborData := hexDecode("a3616161416162614261636143") // {"a":"A", "b":"B", "c":"C"} + data := hexDecode("a3616161416162614261636143") // {"a":"A", "b":"B", "c":"C"} var v strc - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, want, want) } } @@ -3935,14 +3935,14 @@ func TestUnmarshalStructTag2(t *testing.T) { B: "B", C: "C", } - cborData := hexDecode("a3616161416162614261636143") // {"a":"A", "b":"B", "c":"C"} + data := hexDecode("a3616161416162614261636143") // {"a":"A", "b":"B", "c":"C"} var v strc - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, want, want) } } @@ -3957,14 +3957,14 @@ func TestUnmarshalStructTag3(t *testing.T) { B: "B", C: "C", } - cborData := hexDecode("a36161614161626142617a6143") // {"a":"A", "b":"B", "z":"C"} + data := hexDecode("a36161614161626142617a6143") // {"a":"A", "b":"B", "z":"C"} var v strc - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, v, v, want, want) } } @@ -3978,14 +3978,14 @@ func TestUnmarshalStructTag4(t *testing.T) { A: "A", B: "B", } - cborData := hexDecode("a3616161416162614261636143") // {"a":"A", "b":"B", "c":"C"} + data := hexDecode("a3616161416162614261636143") // {"a":"A", "b":"B", "c":"C"} var v strc - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, v, v, want, want) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, v, v, want, want) } } @@ -4064,29 +4064,29 @@ func TestBinaryUnmarshalerError(t *testing.T) { //nolint:dupl testCases := []struct { name string typ reflect.Type - cborData []byte + data []byte wantErrorMsg string }{ { name: "primitive type", typ: reflect.TypeOf(number(0)), - cborData: hexDecode("44499602d2"), + data: hexDecode("44499602d2"), wantErrorMsg: "number:UnmarshalBinary: invalid length", }, { name: "struct type", typ: reflect.TypeOf(stru{}), - cborData: hexDecode("47612C622C632C64"), + data: hexDecode("47612C622C632C64"), wantErrorMsg: "stru:UnmarshalBinary: invalid element count", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { v := reflect.New(tc.typ) - if err := Unmarshal(tc.cborData, v.Interface()); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want error msg %q", tc.cborData, tc.wantErrorMsg) + if err := Unmarshal(tc.data, v.Interface()); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want error msg %q", tc.data, tc.wantErrorMsg) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } }) } @@ -4172,29 +4172,29 @@ func TestUnmarshalerError(t *testing.T) { //nolint:dupl testCases := []struct { name string typ reflect.Type - cborData []byte + data []byte wantErrorMsg string }{ { name: "primitive type", typ: reflect.TypeOf(number2(0)), - cborData: hexDecode("44499602d2"), + data: hexDecode("44499602d2"), wantErrorMsg: "cbor: cannot unmarshal byte string into Go value of type map[string]uint64", }, { name: "struct type", typ: reflect.TypeOf(stru2{}), - cborData: hexDecode("47612C622C632C64"), + data: hexDecode("47612C622C632C64"), wantErrorMsg: "cbor: cannot unmarshal byte string into Go value of type []string", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { v := reflect.New(tc.typ) - if err := Unmarshal(tc.cborData, v.Interface()); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want error msg %q", tc.cborData, tc.wantErrorMsg) + if err := Unmarshal(tc.data, v.Interface()); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want error msg %q", tc.data, tc.wantErrorMsg) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } }) } @@ -4212,25 +4212,25 @@ func TestMarshalerError(t *testing.T) { // Found at https://github.com/oasislabs/oasis-core/blob/master/go/common/cbor/cbor_test.go func TestOutOfMem1(t *testing.T) { - cborData := []byte("\x9b\x00\x00000000") + data := []byte("\x9b\x00\x00000000") var f []byte - if err := Unmarshal(cborData, &f); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &f); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } } // Found at https://github.com/oasislabs/oasis-core/blob/master/go/common/cbor/cbor_test.go func TestOutOfMem2(t *testing.T) { - cborData := []byte("\x9b\x00\x00\x81112233") + data := []byte("\x9b\x00\x00\x81112233") var f []byte - if err := Unmarshal(cborData, &f); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &f); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } } // Found at https://github.com/cose-wg/Examples/tree/master/RFC8152 func TestCOSEExamples(t *testing.T) { - cborData := [][]byte{ + data := [][]byte{ hexDecode("D8608443A10101A1054C02D1F7E6F26C43D4868D87CE582464F84D913BA60A76070A9A48F26E97E863E2852948658F0811139868826E89218A75715B818440A101225818DBD43C4E9D719C27C6275C67D628D493F090593DB8218F11818344A1013818A220A401022001215820B2ADD44368EA6D641F9CA9AF308B4079AEB519F11E9B8A55A600B21233E86E6822F40458246D65726961646F632E6272616E64796275636B406275636B6C616E642E6578616D706C6540"), hexDecode("D8628440A054546869732069732074686520636F6E74656E742E818343A10126A1044231315840E2AEAFD40D69D19DFE6E52077C5D7FF4E408282CBEFB5D06CBF414AF2E19D982AC45AC98B8544C908B4507DE1E90B717C3D34816FE926A2B98F53AFD2FA0F30A"), hexDecode("D8628440A054546869732069732074686520636F6E74656E742E828343A10126A1044231315840E2AEAFD40D69D19DFE6E52077C5D7FF4E408282CBEFB5D06CBF414AF2E19D982AC45AC98B8544C908B4507DE1E90B717C3D34816FE926A2B98F53AFD2FA0F30A8344A1013823A104581E62696C626F2E62616767696E7340686F626269746F6E2E6578616D706C65588400A2D28A7C2BDB1587877420F65ADF7D0B9A06635DD1DE64BB62974C863F0B160DD2163734034E6AC003B01E8705524C5C4CA479A952F0247EE8CB0B4FB7397BA08D009E0C8BF482270CC5771AA143966E5A469A09F613488030C5B07EC6D722E3835ADB5B2D8C44E95FFB13877DD2582866883535DE3BB03D01753F83AB87BB4F7A0297"), @@ -4249,7 +4249,7 @@ func TestCOSEExamples(t *testing.T) { hexDecode("D8618543A10105A054546869732069732074686520636F6E74656E742E5820BF48235E809B5C42E995F2B7D5FA13620E7ED834E337F6AA43DF161E49E9323E828344A101381CA220A4010220032158420043B12669ACAC3FD27898FFBA0BCD2E6C366D53BC4DB71F909A759304ACFB5E18CDC7BA0B13FF8C7636271A6924B1AC63C02688075B55EF2D613574E7DC242F79C322F504581E62696C626F2E62616767696E7340686F626269746F6E2E6578616D706C655828339BC4F79984CDC6B3E6CE5F315A4C7D2B0AC466FCEA69E8C07DFBCA5BB1F661BC5F8E0DF9E3EFF58340A2012404582430313863306165352D346439622D343731622D626664362D65656633313462633730333758280B2C7CFCE04E98276342D6476A7723C090DFDD15F9A518E7736549E998370695E6D6A83B4AE507BB"), hexDecode("D18443A1010FA054546869732069732074686520636F6E74656E742E48726043745027214F"), } - for _, d := range cborData { + for _, d := range data { var v interface{} if err := Unmarshal(d, &v); err != nil { t.Errorf("Unmarshal(0x%x) returned error %v", d, err) @@ -4261,14 +4261,14 @@ func TestUnmarshalStructKeyAsIntError(t *testing.T) { type T1 struct { F1 int `cbor:"1,keyasint"` } - cborData := hexDecode("a13bffffffffffffffff01") // {1: -18446744073709551616} + data := hexDecode("a13bffffffffffffffff01") // {1: -18446744073709551616} var v T1 - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), "cannot unmarshal") { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), "cannot unmarshal") + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), "cannot unmarshal") } } @@ -4280,8 +4280,8 @@ func TestUnmarshalArrayToStruct(t *testing.T) { C int } testCases := []struct { - name string - cborData []byte + name string + data []byte }{ {"definite length array", hexDecode("83010203")}, {"indefinite length array", hexDecode("9f010203ff")}, @@ -4289,8 +4289,8 @@ func TestUnmarshalArrayToStruct(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v T - if err := Unmarshal(tc.cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", tc.cborData, err) + if err := Unmarshal(tc.data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", tc.data, err) } }) } @@ -4302,10 +4302,10 @@ func TestUnmarshalArrayToStructNoToArrayOptionError(t *testing.T) { B int C int } - cborData := hexDecode("8301020383010203") + data := hexDecode("8301020383010203") var v1 T wantT := T{} - dec := NewDecoder(bytes.NewReader(cborData)) + dec := NewDecoder(bytes.NewReader(data)) if err := dec.Decode(&v1); err == nil { t.Errorf("Decode(%+v) didn't return an error", v1) } else if _, ok := err.(*UnmarshalTypeError); !ok { @@ -4334,8 +4334,8 @@ func TestUnmarshalNonArrayDataToStructToArray(t *testing.T) { C int } testCases := []struct { - name string - cborData []byte + name string + data []byte }{ {"CBOR positive int", hexDecode("00")}, // 0 {"CBOR negative int", hexDecode("20")}, // -1 @@ -4350,15 +4350,15 @@ func TestUnmarshalNonArrayDataToStructToArray(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v T - if err := Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", tc.cborData) + if err := Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", tc.data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", tc.data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", tc.cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", tc.data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v, wantT) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", tc.cborData, v, v, wantT, wantT) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", tc.data, v, v, wantT, wantT) } }) } @@ -4370,10 +4370,10 @@ func TestUnmarshalArrayToStructWrongSizeError(t *testing.T) { A int B int } - cborData := hexDecode("8301020383010203") + data := hexDecode("8301020383010203") var v1 T wantT := T{} - dec := NewDecoder(bytes.NewReader(cborData)) + dec := NewDecoder(bytes.NewReader(data)) if err := dec.Decode(&v1); err == nil { t.Errorf("Decode(%+v) didn't return an error", v1) } else if _, ok := err.(*UnmarshalTypeError); !ok { @@ -4403,7 +4403,7 @@ func TestUnmarshalArrayToStructWrongFieldTypeError(t *testing.T) { } testCases := []struct { name string - cborData []byte + data []byte wantErrorMsg string wantV interface{} }{ @@ -4415,13 +4415,13 @@ func TestUnmarshalArrayToStructWrongFieldTypeError(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v T - if err := Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", tc.cborData) + if err := Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", tc.data) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", tc.data, err.Error(), tc.wantErrorMsg) } if !reflect.DeepEqual(v, tc.wantV) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", tc.cborData, v, v, tc.wantV, tc.wantV) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", tc.data, v, v, tc.wantV, tc.wantV) } }) } @@ -4443,15 +4443,15 @@ func TestUnmarshalArrayToStructCannotSetEmbeddedPointerError(t *testing.T) { *S2 } ) - cborData := []byte{0x82, 0x02, 0x04} // [2, 4] + data := []byte{0x82, 0x02, 0x04} // [2, 4] const wantErrorMsg = "cannot set embedded pointer to unexported struct" wantV := S{S2: &S2{Y: 4}} var v S - err := Unmarshal(cborData, &v) + err := Unmarshal(data, &v) if err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want error %q", cborData, wantErrorMsg) + t.Errorf("Unmarshal(0x%x) didn't return an error, want error %q", data, wantErrorMsg) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v, wantV) { t.Errorf("Decode() = %+v (%T), want %+v (%T)", v, v, wantV, wantV) @@ -4459,68 +4459,68 @@ func TestUnmarshalArrayToStructCannotSetEmbeddedPointerError(t *testing.T) { } func TestUnmarshalIntoSliceError(t *testing.T) { - cborData := []byte{0x83, 0x61, 0x61, 0x61, 0xfe, 0x61, 0x62} // ["a", 0xfe, "b"] + data := []byte{0x83, 0x61, 0x61, 0x61, 0xfe, 0x61, 0x62} // ["a", 0xfe, "b"] wantErrorMsg := invalidUTF8ErrorMsg var want interface{} // Unmarshal CBOR array into Go empty interface. var v1 interface{} want = []interface{}{"a", interface{}(nil), "b"} - if err := Unmarshal(cborData, &v1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", cborData, wantErrorMsg) + if err := Unmarshal(data, &v1); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", data, wantErrorMsg) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v1, want) { - t.Errorf("Unmarshal(0x%x) = %v, want %v", cborData, v1, want) + t.Errorf("Unmarshal(0x%x) = %v, want %v", data, v1, want) } // Unmarshal CBOR array into Go slice. var v2 []string want = []string{"a", "", "b"} - if err := Unmarshal(cborData, &v2); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", cborData, wantErrorMsg) + if err := Unmarshal(data, &v2); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", data, wantErrorMsg) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v2, want) { - t.Errorf("Unmarshal(0x%x) = %v, want %v", cborData, v2, want) + t.Errorf("Unmarshal(0x%x) = %v, want %v", data, v2, want) } // Unmarshal CBOR array into Go array. var v3 [3]string want = [3]string{"a", "", "b"} - if err := Unmarshal(cborData, &v3); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", cborData, wantErrorMsg) + if err := Unmarshal(data, &v3); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", data, wantErrorMsg) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v3, want) { - t.Errorf("Unmarshal(0x%x) = %v, want %v", cborData, v3, want) + t.Errorf("Unmarshal(0x%x) = %v, want %v", data, v3, want) } // Unmarshal CBOR array into populated Go slice. v4 := []string{"hello", "to", "you"} want = []string{"a", "to", "b"} - if err := Unmarshal(cborData, &v4); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", cborData, wantErrorMsg) + if err := Unmarshal(data, &v4); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", data, wantErrorMsg) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v4, want) { - t.Errorf("Unmarshal(0x%x) = %v, want %v", cborData, v4, want) + t.Errorf("Unmarshal(0x%x) = %v, want %v", data, v4, want) } } func TestUnmarshalIntoMapError(t *testing.T) { - cborData := [][]byte{ + data := [][]byte{ {0xa3, 0x61, 0x61, 0x61, 0x41, 0x61, 0xfe, 0x61, 0x43, 0x61, 0x62, 0x61, 0x42}, // {"a":"A", 0xfe: "C", "b":"B"} {0xa3, 0x61, 0x61, 0x61, 0x41, 0x61, 0x63, 0x61, 0xfe, 0x61, 0x62, 0x61, 0x42}, // {"a":"A", "c": 0xfe, "b":"B"} } wantErrorMsg := invalidUTF8ErrorMsg var want interface{} - for _, data := range cborData { + for _, data := range data { // Unmarshal CBOR map into Go empty interface. var v1 interface{} want = map[interface{}]interface{}{"a": "A", "b": "B"} @@ -4588,7 +4588,7 @@ func TestUnmarshalDeepNesting(t *testing.T) { if err != nil { t.Errorf("EncMode() returned error %v", err) } - cborData, err := em.Marshal(root) + data, err := em.Marshal(root) if err != nil { t.Errorf("Marshal() deeply nested object returned error %v", err) } @@ -4599,7 +4599,7 @@ func TestUnmarshalDeepNesting(t *testing.T) { t.Errorf("DecMode() returned error %v", err) } var readback TestNode - err = dm.Unmarshal(cborData, &readback) + err = dm.Unmarshal(data, &readback) if err != nil { t.Errorf("Unmarshal() of deeply nested object returned error: %v", err) } @@ -4622,7 +4622,7 @@ func TestStructToArrayError(t *testing.T) { Ciphertext []byte } for _, tc := range []struct { - cborData []byte + data []byte wantErrorMsg string }{ // [-17, [-17, -17], -17] @@ -4631,10 +4631,10 @@ func TestStructToArrayError(t *testing.T) { {hexDecode("9f9fff9fff9f65933030303030ffff"), "cbor: cannot unmarshal array into Go struct field cbor.nestedCWT.Unprotected of type cbor.coseHeader (cannot decode CBOR array to struct without toarray option)"}, } { var v nestedCWT - if err := Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.cborData, tc.wantErrorMsg) + if err := Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.data, tc.wantErrorMsg) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } } @@ -4649,31 +4649,31 @@ func TestStructKeyAsIntError(t *testing.T) { Iat float64 `cbor:"6,keyasint"` Cti []byte `cbor:"7,keyasint"` } - cborData := hexDecode("bf0783e662f03030ff") // {7: [simple(6), "\xF00", -17]} + data := hexDecode("bf0783e662f03030ff") // {7: [simple(6), "\xF00", -17]} wantErrorMsg := invalidUTF8ErrorMsg wantV := claims{Cti: []byte{6, 0, 0}} var v claims - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", cborData, wantErrorMsg) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", data, wantErrorMsg) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v, wantV) { - t.Errorf("Unmarshal(0x%x) = %v, want %v", cborData, v, wantV) + t.Errorf("Unmarshal(0x%x) = %v, want %v", data, v, wantV) } } func TestUnmarshalToNotNilInterface(t *testing.T) { - cborData := hexDecode("83010203") // []uint64{1, 2, 3} - s := "hello" //nolint:goconst - var v interface{} = s // Unmarshal() sees v as type interface{} and sets CBOR data as default Go type. s is unmodified. Same behavior as encoding/json. + data := hexDecode("83010203") // []uint64{1, 2, 3} + s := "hello" //nolint:goconst + var v interface{} = s // Unmarshal() sees v as type interface{} and sets CBOR data as default Go type. s is unmodified. Same behavior as encoding/json. wantV := []interface{}{uint64(1), uint64(2), uint64(3)} - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantV, wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantV, wantV) } else if s != "hello" { - t.Errorf("Unmarshal(0x%x) modified s %q", cborData, s) + t.Errorf("Unmarshal(0x%x) modified s %q", data, s) } } @@ -4972,19 +4972,19 @@ func TestUnmarshalStructKeyAsIntNumError(t *testing.T) { } testCases := []struct { name string - cborData []byte + data []byte obj interface{} wantErrorMsg string }{ { name: "string as key", - cborData: hexDecode("a1616101"), + data: hexDecode("a1616101"), obj: T1{}, wantErrorMsg: "cbor: failed to parse field name \"a\" to int", }, { name: "out of range int as key", - cborData: hexDecode("a13bffffffffffffffff01"), + data: hexDecode("a13bffffffffffffffff01"), obj: T2{}, wantErrorMsg: "cbor: failed to parse field name \"-18446744073709551616\" to int", }, @@ -4992,11 +4992,11 @@ func TestUnmarshalStructKeyAsIntNumError(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { v := reflect.New(reflect.TypeOf(tc.obj)) - err := Unmarshal(tc.cborData, v.Interface()) + err := Unmarshal(tc.data, v.Interface()) if err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error, want error %q", tc.cborData, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) didn't return an error, want error %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) error %v, want %v", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) error %v, want %v", tc.data, err.Error(), tc.wantErrorMsg) } }) } @@ -5004,19 +5004,19 @@ func TestUnmarshalStructKeyAsIntNumError(t *testing.T) { func TestUnmarshalEmptyMapWithDupMapKeyOpt(t *testing.T) { testCases := []struct { - name string - cborData []byte - wantV interface{} + name string + data []byte + wantV interface{} }{ { - name: "empty map", - cborData: hexDecode("a0"), - wantV: map[interface{}]interface{}{}, + name: "empty map", + data: hexDecode("a0"), + wantV: map[interface{}]interface{}{}, }, { - name: "indefinite empty map", - cborData: hexDecode("bfff"), - wantV: map[interface{}]interface{}{}, + name: "indefinite empty map", + data: hexDecode("bfff"), + wantV: map[interface{}]interface{}{}, }, } @@ -5028,27 +5028,27 @@ func TestUnmarshalEmptyMapWithDupMapKeyOpt(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v interface{} - if err := dm.Unmarshal(tc.cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", tc.cborData, err) + if err := dm.Unmarshal(tc.data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", tc.data, err) } if !reflect.DeepEqual(v, tc.wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantV, tc.wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v, v, tc.wantV, tc.wantV) } }) } } func TestUnmarshalDupMapKeyToEmptyInterface(t *testing.T) { - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} // Duplicate key overwrites previous value (default). wantV := map[interface{}]interface{}{"a": "F", "b": "B", "c": "C", "d": "D", "e": "E"} var v interface{} - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantV, wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantV, wantV) } // Duplicate key triggers error. @@ -5056,24 +5056,24 @@ func TestUnmarshalDupMapKeyToEmptyInterface(t *testing.T) { wantErrorMsg := "cbor: found duplicate map key \"a\" at map element index 3" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var v2 interface{} - if err := dm.Unmarshal(cborData, &v2); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := dm.Unmarshal(data, &v2); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v2, wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v2, v2, wantV, wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v2, v2, wantV, wantV) } } func TestStreamDupMapKeyToEmptyInterface(t *testing.T) { - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // map with duplicate key "c": {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // map with duplicate key "c": {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } // Duplicate key overwrites previous value (default). @@ -5108,7 +5108,7 @@ func TestStreamDupMapKeyToEmptyInterface(t *testing.T) { t.Errorf("Decode() returned error %q, want error containing %q", err.Error(), wantErrorMsg) } if !reflect.DeepEqual(v2, wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v2, v2, wantV, wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v2, v2, wantV, wantV) } } if err := dec.Decode(&v); err != io.EOF { @@ -5117,16 +5117,16 @@ func TestStreamDupMapKeyToEmptyInterface(t *testing.T) { } func TestUnmarshalDupMapKeyToEmptyMap(t *testing.T) { - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} // Duplicate key overwrites previous value (default). wantM := map[string]string{"a": "F", "b": "B", "c": "C", "d": "D", "e": "E"} var m map[string]string - if err := Unmarshal(cborData, &m); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &m); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(m, wantM) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, m, m, wantM, wantM) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, m, m, wantM, wantM) } // Duplicate key triggers error. @@ -5134,24 +5134,24 @@ func TestUnmarshalDupMapKeyToEmptyMap(t *testing.T) { wantErrorMsg := "cbor: found duplicate map key \"a\" at map element index 3" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var m2 map[string]string - if err := dm.Unmarshal(cborData, &m2); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := dm.Unmarshal(data, &m2); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(m2, wantM) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, m2, m2, wantM, wantM) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, m2, m2, wantM, wantM) } } func TestStreamDupMapKeyToEmptyMap(t *testing.T) { - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } // Duplicate key overwrites previous value (default). @@ -5186,7 +5186,7 @@ func TestStreamDupMapKeyToEmptyMap(t *testing.T) { t.Errorf("Decode() returned error %q, want error containing %q", err.Error(), wantErrorMsg) } if !reflect.DeepEqual(m2, wantM) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, m2, m2, wantM, wantM) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, m2, m2, wantM, wantM) } } if err := dec.Decode(&v); err != io.EOF { @@ -5195,16 +5195,16 @@ func TestStreamDupMapKeyToEmptyMap(t *testing.T) { } func TestUnmarshalDupMapKeyToNotEmptyMap(t *testing.T) { - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} // Duplicate key overwrites previous value (default). m := map[string]string{"a": "Z", "b": "Z", "c": "Z", "d": "Z", "e": "Z", "f": "Z"} wantM := map[string]string{"a": "F", "b": "B", "c": "C", "d": "D", "e": "E", "f": "Z"} - if err := Unmarshal(cborData, &m); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &m); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(m, wantM) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, m, m, wantM, wantM) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, m, m, wantM, wantM) } // Duplicate key triggers error. @@ -5212,24 +5212,24 @@ func TestUnmarshalDupMapKeyToNotEmptyMap(t *testing.T) { wantM = map[string]string{"a": "", "b": "B", "c": "C", "d": "Z", "e": "Z", "f": "Z"} wantErrorMsg := "cbor: found duplicate map key \"a\" at map element index 3" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() - if err := dm.Unmarshal(cborData, &m2); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := dm.Unmarshal(data, &m2); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(m2, wantM) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, m2, m2, wantM, wantM) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, m2, m2, wantM, wantM) } } func TestStreamDupMapKeyToNotEmptyMap(t *testing.T) { - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } // Duplicate key overwrites previous value (default). @@ -5264,7 +5264,7 @@ func TestStreamDupMapKeyToNotEmptyMap(t *testing.T) { t.Errorf("Decode() returned error %q, want error containing %q", err.Error(), wantErrorMsg) } if !reflect.DeepEqual(m2, wantM) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, m2, m2, wantM, wantM) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, m2, m2, wantM, wantM) } } if err := dec.Decode(&v); err != io.EOF { @@ -5280,16 +5280,16 @@ func TestUnmarshalDupMapKeyToStruct(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} // Duplicate key doesn't overwrite previous value (default). wantS := s{A: "A", B: "B", C: "C", D: "D", E: "E"} var s1 s - if err := Unmarshal(cborData, &s1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &s1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error. @@ -5297,15 +5297,15 @@ func TestUnmarshalDupMapKeyToStruct(t *testing.T) { wantErrorMsg := "cbor: found duplicate map key \"a\" at map element index 3" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5317,11 +5317,11 @@ func TestStreamDupMapKeyToStruct(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } // Duplicate key overwrites previous value (default). @@ -5356,7 +5356,7 @@ func TestStreamDupMapKeyToStruct(t *testing.T) { t.Errorf("Decode() returned error %q, want error containing %q", err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } if err := dec.Decode(&v); err != io.EOF { @@ -5371,16 +5371,16 @@ func TestUnmarshalDupMapKeyToStructKeyAsInt(t *testing.T) { B int `cbor:"3,keyasint"` C int `cbor:"5,keyasint"` } - cborData := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} + data := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} // Duplicate key doesn't overwrite previous value (default). wantS := s{A: 2, B: 4, C: 6} var s1 s - if err := Unmarshal(cborData, &s1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &s1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error. @@ -5388,15 +5388,15 @@ func TestUnmarshalDupMapKeyToStructKeyAsInt(t *testing.T) { wantErrorMsg := "cbor: found duplicate map key \"1\" at map element index 2" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5406,11 +5406,11 @@ func TestStreamDupMapKeyToStructKeyAsInt(t *testing.T) { B int `cbor:"3,keyasint"` C int `cbor:"5,keyasint"` } - cborData := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} + data := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } // Duplicate key overwrites previous value (default). @@ -5445,7 +5445,7 @@ func TestStreamDupMapKeyToStructKeyAsInt(t *testing.T) { t.Errorf("Decode() returned error %q, want error containing %q", err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } if err := dec.Decode(&v); err != io.EOF { @@ -5460,15 +5460,15 @@ func TestUnmarshalDupMapKeyToStructNoMatchingField(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} wantS := s{B: "B", C: "C", D: "D", E: "E"} var s1 s - if err := Unmarshal(cborData, &s1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &s1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error even though map key "a" doesn't have a corresponding struct field. @@ -5476,15 +5476,15 @@ func TestUnmarshalDupMapKeyToStructNoMatchingField(t *testing.T) { wantErrorMsg := "cbor: found duplicate map key \"a\" at map element index 3" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5495,11 +5495,11 @@ func TestStreamDupMapKeyToStructNoMatchingField(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} + data := hexDecode("a6616161416162614261636143616161466164614461656145") // {"a": "A", "b": "B", "c": "C", "a": "F", "d": "D", "e": "E"} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } // Duplicate key overwrites previous value (default). @@ -5547,15 +5547,15 @@ func TestUnmarshalDupMapKeyToStructKeyAsIntNoMatchingField(t *testing.T) { B int `cbor:"3,keyasint"` C int `cbor:"5,keyasint"` } - cborData := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} + data := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} wantS := s{B: 4, C: 6} var s1 s - if err := Unmarshal(cborData, &s1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &s1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error even though map key "a" doesn't have a corresponding struct field. @@ -5563,15 +5563,15 @@ func TestUnmarshalDupMapKeyToStructKeyAsIntNoMatchingField(t *testing.T) { wantErrorMsg := "cbor: found duplicate map key \"1\" at map element index 2" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5580,11 +5580,11 @@ func TestStreamDupMapKeyToStructKeyAsIntNoMatchingField(t *testing.T) { B int `cbor:"3,keyasint"` C int `cbor:"5,keyasint"` } - cborData := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} + data := hexDecode("a40102030401030506") // {1:2, 3:4, 1:3, 5:6} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } // Duplicate key overwrites previous value (default). @@ -5635,35 +5635,35 @@ func TestUnmarshalDupMapKeyToStructWrongType(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a861616141fa47c35000026162614261636143fa47c3500003616161466164614461656145") // {"a": "A", 100000.0:2, "b": "B", "c": "C", 100000.0:3, "a": "F", "d": "D", "e": "E"} + data := hexDecode("a861616141fa47c35000026162614261636143fa47c3500003616161466164614461656145") // {"a": "A", 100000.0:2, "b": "B", "c": "C", 100000.0:3, "a": "F", "d": "D", "e": "E"} var s1 s wantS := s{A: "A", B: "B", C: "C", D: "D", E: "E"} wantErrorMsg := "cbor: cannot unmarshal" - if err := Unmarshal(cborData, &s1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &s1); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } wantS = s{A: "A", B: "B", C: "C"} wantErrorMsg = "cbor: found duplicate map key \"100000\" at map element index 4" dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*DupMapKeyError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*DupMapKeyError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5675,11 +5675,11 @@ func TestStreamDupMapKeyToStructWrongType(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a861616141fa47c35000026162614261636143fa47c3500003616161466164614461656145") // {"a": "A", 100000.0:2, "b": "B", "c": "C", 100000.0:3, "a": "F", "d": "D", "e": "E"} + data := hexDecode("a861616141fa47c35000026162614261636143fa47c3500003616161466164614461656145") // {"a": "A", 100000.0:2, "b": "B", "c": "C", 100000.0:3, "a": "F", "d": "D", "e": "E"} var b []byte for i := 0; i < 3; i++ { - b = append(b, cborData...) + b = append(b, data...) } wantS := s{A: "A", B: "B", C: "C", D: "D", E: "E"} @@ -5688,14 +5688,14 @@ func TestStreamDupMapKeyToStructWrongType(t *testing.T) { for i := 0; i < 3; i++ { var s1 s if err := dec.Decode(&s1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } } var v interface{} @@ -5718,7 +5718,7 @@ func TestStreamDupMapKeyToStructWrongType(t *testing.T) { t.Errorf("Decode() returned error %q, want error containing %q", err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } if err := dec.Decode(&v); err != io.EOF { @@ -5734,35 +5734,35 @@ func TestUnmarshalDupMapKeyToStructStringParseError(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a661fe6141616261426163614361fe61466164614461656145") // {"\xFE": "A", "b": "B", "c": "C", "\xFE": "F", "d": "D", "e": "E"} + data := hexDecode("a661fe6141616261426163614361fe61466164614461656145") // {"\xFE": "A", "b": "B", "c": "C", "\xFE": "F", "d": "D", "e": "E"} wantS := s{A: "", B: "B", C: "C", D: "D", E: "E"} wantErrorMsg := "cbor: invalid UTF-8 string" // Duplicate key doesn't overwrite previous value (default). var s1 s - if err := Unmarshal(cborData, &s1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &s1); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*SemanticError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*SemanticError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*SemanticError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error. dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*SemanticError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*SemanticError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*SemanticError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5772,35 +5772,35 @@ func TestUnmarshalDupMapKeyToStructIntParseError(t *testing.T) { B int `cbor:"3,keyasint"` C int `cbor:"5,keyasint"` } - cborData := hexDecode("a43bffffffffffffffff0203043bffffffffffffffff030506") // {-18446744073709551616:2, 3:4, -18446744073709551616:3, 5:6} + data := hexDecode("a43bffffffffffffffff0203043bffffffffffffffff030506") // {-18446744073709551616:2, 3:4, -18446744073709551616:3, 5:6} // Duplicate key doesn't overwrite previous value (default). wantS := s{B: 4, C: 6} wantErrorMsg := "cbor: cannot unmarshal" var s1 s - if err := Unmarshal(cborData, &s1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &s1); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error. dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5812,35 +5812,35 @@ func TestUnmarshalDupMapKeyToStructWrongTypeParseError(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a68161fe614161626142616361438161fe61466164614461656145") // {["\xFE"]: "A", "b": "B", "c": "C", ["\xFE"]: "F", "d": "D", "e": "E"} + data := hexDecode("a68161fe614161626142616361438161fe61466164614461656145") // {["\xFE"]: "A", "b": "B", "c": "C", ["\xFE"]: "F", "d": "D", "e": "E"} // Duplicate key doesn't overwrite previous value (default). wantS := s{A: "", B: "B", C: "C", D: "D", E: "E"} wantErrorMsg := "cbor: cannot unmarshal" var s1 s - if err := Unmarshal(cborData, &s1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &s1); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error. dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5852,35 +5852,35 @@ func TestUnmarshalDupMapKeyToStructWrongTypeUnhashableError(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a6810061416162614261636143810061466164614461656145") // {[0]: "A", "b": "B", "c": "C", [0]: "F", "d": "D", "e": "E"} + data := hexDecode("a6810061416162614261636143810061466164614461656145") // {[0]: "A", "b": "B", "c": "C", [0]: "F", "d": "D", "e": "E"} wantS := s{A: "", B: "B", C: "C", D: "D", E: "E"} // Duplicate key doesn't overwrite previous value (default). wantErrorMsg := "cbor: cannot unmarshal" var s1 s - if err := Unmarshal(cborData, &s1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &s1); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error. dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } @@ -5892,69 +5892,69 @@ func TestUnmarshalDupMapKeyToStructTagTypeError(t *testing.T) { D string `cbor:"d"` E string `cbor:"e"` } - cborData := hexDecode("a6c24901000000000000000061416162614261636143c24901000000000000000061466164614461656145") // {bignum(18446744073709551616): "A", "b": "B", "c": "C", bignum(18446744073709551616): "F", "d": "D", "e": "E"} + data := hexDecode("a6c24901000000000000000061416162614261636143c24901000000000000000061466164614461656145") // {bignum(18446744073709551616): "A", "b": "B", "c": "C", bignum(18446744073709551616): "F", "d": "D", "e": "E"} wantS := s{A: "", B: "B", C: "C", D: "D", E: "E"} // Duplicate key doesn't overwrite previous value (default). wantErrorMsg := "cbor: cannot unmarshal" var s1 s - if err := Unmarshal(cborData, &s1); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := Unmarshal(data, &s1); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s1, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s1, s1, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s1, s1, wantS, wantS) } // Duplicate key triggers error. dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() var s2 s - if err := dm.Unmarshal(cborData, &s2); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, reflect.TypeOf(s2)) + if err := dm.Unmarshal(data, &s2); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, reflect.TypeOf(s2)) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), wantErrorMsg) } if !reflect.DeepEqual(s2, wantS) { - t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", cborData, s2, s2, wantS, wantS) + t.Errorf("Unmarshal(0x%x) = %+v (%T), want %+v (%T)", data, s2, s2, wantS, wantS) } } func TestIndefiniteLengthArrayToArray(t *testing.T) { testCases := []struct { - name string - cborData []byte - wantV interface{} + name string + data []byte + wantV interface{} }{ { - name: "CBOR empty array to Go 5 elem array", - cborData: hexDecode("9fff"), - wantV: [5]byte{}, + name: "CBOR empty array to Go 5 elem array", + data: hexDecode("9fff"), + wantV: [5]byte{}, }, { - name: "CBOR 3 elem array to Go 5 elem array", - cborData: hexDecode("9f010203ff"), - wantV: [5]byte{1, 2, 3, 0, 0}, + name: "CBOR 3 elem array to Go 5 elem array", + data: hexDecode("9f010203ff"), + wantV: [5]byte{1, 2, 3, 0, 0}, }, { - name: "CBOR 10 elem array to Go 5 elem array", - cborData: hexDecode("9f0102030405060708090aff"), - wantV: [5]byte{1, 2, 3, 4, 5}, + name: "CBOR 10 elem array to Go 5 elem array", + data: hexDecode("9f0102030405060708090aff"), + wantV: [5]byte{1, 2, 3, 4, 5}, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { v := reflect.New(reflect.TypeOf(tc.wantV)) - if err := Unmarshal(tc.cborData, v.Interface()); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", tc.cborData, err) + if err := Unmarshal(tc.data, v.Interface()); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", tc.data, err) } if !reflect.DeepEqual(v.Elem().Interface(), tc.wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v.Elem().Interface(), v.Elem().Interface(), tc.wantV, tc.wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v.Elem().Interface(), v.Elem().Interface(), tc.wantV, tc.wantV) } }) } @@ -5964,19 +5964,19 @@ func TestExceedMaxArrayElements(t *testing.T) { testCases := []struct { name string opts DecOptions - cborData []byte + data []byte wantErrorMsg string }{ { name: "array", opts: DecOptions{MaxArrayElements: 16}, - cborData: hexDecode("910101010101010101010101010101010101"), + data: hexDecode("910101010101010101010101010101010101"), wantErrorMsg: "cbor: exceeded max number of elements 16 for CBOR array", }, { name: "indefinite length array", opts: DecOptions{MaxArrayElements: 16}, - cborData: hexDecode("9f0101010101010101010101010101010101ff"), + data: hexDecode("9f0101010101010101010101010101010101ff"), wantErrorMsg: "cbor: exceeded max number of elements 16 for CBOR array", }, } @@ -5984,10 +5984,10 @@ func TestExceedMaxArrayElements(t *testing.T) { t.Run(tc.name, func(t *testing.T) { dm, _ := tc.opts.DecMode() var v interface{} - if err := dm.Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", tc.cborData) + if err := dm.Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", tc.data) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } }) } @@ -5997,19 +5997,19 @@ func TestExceedMaxMapPairs(t *testing.T) { testCases := []struct { name string opts DecOptions - cborData []byte + data []byte wantErrorMsg string }{ { name: "array", opts: DecOptions{MaxMapPairs: 16}, - cborData: hexDecode("b101010101010101010101010101010101010101010101010101010101010101010101"), + data: hexDecode("b101010101010101010101010101010101010101010101010101010101010101010101"), wantErrorMsg: "cbor: exceeded max number of key-value pairs 16 for CBOR map", }, { name: "indefinite length array", opts: DecOptions{MaxMapPairs: 16}, - cborData: hexDecode("bf01010101010101010101010101010101010101010101010101010101010101010101ff"), + data: hexDecode("bf01010101010101010101010101010101010101010101010101010101010101010101ff"), wantErrorMsg: "cbor: exceeded max number of key-value pairs 16 for CBOR map", }, } @@ -6017,10 +6017,10 @@ func TestExceedMaxMapPairs(t *testing.T) { t.Run(tc.name, func(t *testing.T) { dm, _ := tc.opts.DecMode() var v interface{} - if err := dm.Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", tc.cborData) + if err := dm.Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", tc.data) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } }) } @@ -6030,31 +6030,31 @@ func TestDecIndefiniteLengthOption(t *testing.T) { testCases := []struct { name string opts DecOptions - cborData []byte + data []byte wantErrorMsg string }{ { name: "byte string", opts: DecOptions{IndefLength: IndefLengthForbidden}, - cborData: hexDecode("5fff"), + data: hexDecode("5fff"), wantErrorMsg: "cbor: indefinite-length byte string isn't allowed", }, { name: "text string", opts: DecOptions{IndefLength: IndefLengthForbidden}, - cborData: hexDecode("7fff"), + data: hexDecode("7fff"), wantErrorMsg: "cbor: indefinite-length UTF-8 text string isn't allowed", }, { name: "array", opts: DecOptions{IndefLength: IndefLengthForbidden}, - cborData: hexDecode("9fff"), + data: hexDecode("9fff"), wantErrorMsg: "cbor: indefinite-length array isn't allowed", }, { name: "indefinite length array", opts: DecOptions{IndefLength: IndefLengthForbidden}, - cborData: hexDecode("bfff"), + data: hexDecode("bfff"), wantErrorMsg: "cbor: indefinite-length map isn't allowed", }, } @@ -6062,36 +6062,36 @@ func TestDecIndefiniteLengthOption(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // Default option allows indefinite length items var v interface{} - if err := Unmarshal(tc.cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned an error %v", tc.cborData, err) + if err := Unmarshal(tc.data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned an error %v", tc.data, err) } dm, _ := tc.opts.DecMode() - if err := dm.Unmarshal(tc.cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", tc.cborData) + if err := dm.Unmarshal(tc.data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", tc.data) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } }) } } func TestDecTagsMdOption(t *testing.T) { - cborData := hexDecode("c074323031332d30332d32315432303a30343a30305a") + data := hexDecode("c074323031332d30332d32315432303a30343a30305a") wantErrorMsg := "cbor: CBOR tag isn't allowed" // Default option allows CBOR tags var v interface{} - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned an error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned an error %v", data, err) } // Decoding CBOR tags with TagsForbidden option returns error dm, _ := DecOptions{TagsMd: TagsForbidden}.DecMode() - if err := dm.Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := dm.Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if err.Error() != wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err.Error(), wantErrorMsg) } // Create DecMode with TagSet and TagsForbidden option returns error @@ -6149,41 +6149,41 @@ func TestIntDecConvertNone(t *testing.T) { } testCases := []struct { - name string - cborData []byte - wantObj interface{} + name string + data []byte + wantObj interface{} }{ { - name: "CBOR pos int", - cborData: hexDecode("1a000f4240"), - wantObj: uint64(1000000), + name: "CBOR pos int", + data: hexDecode("1a000f4240"), + wantObj: uint64(1000000), }, { - name: "CBOR pos int overflows int64", - cborData: hexDecode("1b8000000000000000"), // math.MaxInt64+1 - wantObj: uint64(math.MaxInt64 + 1), + name: "CBOR pos int overflows int64", + data: hexDecode("1b8000000000000000"), // math.MaxInt64+1 + wantObj: uint64(math.MaxInt64 + 1), }, { - name: "CBOR neg int", - cborData: hexDecode("3903e7"), - wantObj: int64(-1000), + name: "CBOR neg int", + data: hexDecode("3903e7"), + wantObj: int64(-1000), }, { - name: "CBOR neg int overflows int64", - cborData: hexDecode("3b8000000000000000"), // math.MinInt64-1 - wantObj: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)), + name: "CBOR neg int overflows int64", + data: hexDecode("3b8000000000000000"), // math.MinInt64-1 + wantObj: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)), }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v interface{} - err := dm.Unmarshal(tc.cborData, &v) + err := dm.Unmarshal(tc.data, &v) if err == nil { if !reflect.DeepEqual(v, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.data, v, v, tc.wantObj, tc.wantObj) } } else { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } }) } @@ -6200,46 +6200,46 @@ func TestIntDecConvertSigned(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantObj interface{} wantErrorMsg string }{ { - name: "CBOR pos int", - cborData: hexDecode("1a000f4240"), - wantObj: int64(1000000), + name: "CBOR pos int", + data: hexDecode("1a000f4240"), + wantObj: int64(1000000), }, { name: "CBOR pos int overflows int64", - cborData: hexDecode("1b8000000000000000"), // math.MaxInt64+1 + data: hexDecode("1b8000000000000000"), // math.MaxInt64+1 wantErrorMsg: "9223372036854775808 overflows Go's int64", }, { - name: "CBOR neg int", - cborData: hexDecode("3903e7"), - wantObj: int64(-1000), + name: "CBOR neg int", + data: hexDecode("3903e7"), + wantObj: int64(-1000), }, { - name: "CBOR neg int overflows int64", - cborData: hexDecode("3b8000000000000000"), // math.MinInt64-1 - wantObj: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)), + name: "CBOR neg int overflows int64", + data: hexDecode("3b8000000000000000"), // math.MinInt64-1 + wantObj: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)), }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v interface{} - err := dm.Unmarshal(tc.cborData, &v) + err := dm.Unmarshal(tc.data, &v) if err == nil { if tc.wantErrorMsg != "" { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.cborData, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.data, tc.wantErrorMsg) } else if !reflect.DeepEqual(v, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.data, v, v, tc.wantObj, tc.wantObj) } } else { if tc.wantErrorMsg == "" { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } }) @@ -6256,41 +6256,41 @@ func TestIntDecConvertSignedOrBigInt(t *testing.T) { } testCases := []struct { - name string - cborData []byte - wantObj interface{} + name string + data []byte + wantObj interface{} }{ { - name: "CBOR pos int", - cborData: hexDecode("1a000f4240"), - wantObj: int64(1000000), + name: "CBOR pos int", + data: hexDecode("1a000f4240"), + wantObj: int64(1000000), }, { - name: "CBOR pos int overflows int64", - cborData: hexDecode("1b8000000000000000"), - wantObj: new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(1)), + name: "CBOR pos int overflows int64", + data: hexDecode("1b8000000000000000"), + wantObj: new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(1)), }, { - name: "CBOR neg int", - cborData: hexDecode("3903e7"), - wantObj: int64(-1000), + name: "CBOR neg int", + data: hexDecode("3903e7"), + wantObj: int64(-1000), }, { - name: "CBOR neg int overflows int64", - cborData: hexDecode("3b8000000000000000"), - wantObj: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)), + name: "CBOR neg int overflows int64", + data: hexDecode("3b8000000000000000"), + wantObj: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)), }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v interface{} - err := dm.Unmarshal(tc.cborData, &v) + err := dm.Unmarshal(tc.data, &v) if err == nil { if !reflect.DeepEqual(v, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.data, v, v, tc.wantObj, tc.wantObj) } } else { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } }) } @@ -6307,46 +6307,46 @@ func TestIntDecConvertSignedOrError(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantObj interface{} wantErrorMsg string }{ { - name: "CBOR pos int", - cborData: hexDecode("1a000f4240"), - wantObj: int64(1000000), + name: "CBOR pos int", + data: hexDecode("1a000f4240"), + wantObj: int64(1000000), }, { name: "CBOR pos int overflows int64", - cborData: hexDecode("1b8000000000000000"), // math.MaxInt64+1 + data: hexDecode("1b8000000000000000"), // math.MaxInt64+1 wantErrorMsg: "9223372036854775808 overflows Go's int64", }, { - name: "CBOR neg int", - cborData: hexDecode("3903e7"), - wantObj: int64(-1000), + name: "CBOR neg int", + data: hexDecode("3903e7"), + wantObj: int64(-1000), }, { name: "CBOR neg int overflows int64", - cborData: hexDecode("3b8000000000000000"), // math.MinInt64-1 + data: hexDecode("3b8000000000000000"), // math.MinInt64-1 wantErrorMsg: "-9223372036854775809 overflows Go's int64", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v interface{} - err := dm.Unmarshal(tc.cborData, &v) + err := dm.Unmarshal(tc.data, &v) if err == nil { if tc.wantErrorMsg != "" { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.cborData, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.data, tc.wantErrorMsg) } else if !reflect.DeepEqual(v, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.data, v, v, tc.wantObj, tc.wantObj) } } else { if tc.wantErrorMsg == "" { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } }) @@ -6394,48 +6394,48 @@ func TestMapKeyByteString(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantObj interface{} wantErrorMsg string dm DecMode }{ { name: "byte string map key with MapKeyByteStringForbidden", - cborData: hexDecode("a143abcdef187b"), + data: hexDecode("a143abcdef187b"), wantErrorMsg: "cbor: invalid map key type: []uint8", dm: bsForbiddenMode, }, { name: "tagged byte string map key with MapKeyByteStringForbidden", - cborData: hexDecode("a1d86443abcdef187b"), + data: hexDecode("a1d86443abcdef187b"), wantErrorMsg: "cbor: invalid map key type: cbor.Tag", dm: bsForbiddenMode, }, { name: "nested tagged byte string map key with MapKeyByteStringForbidden", - cborData: hexDecode("a1d865d86443abcdef187b"), + data: hexDecode("a1d865d86443abcdef187b"), wantErrorMsg: "cbor: invalid map key type: cbor.Tag", dm: bsForbiddenMode, }, { - name: "byte string map key with MapKeyByteStringAllowed", - cborData: hexDecode("a143abcdef187b"), + name: "byte string map key with MapKeyByteStringAllowed", + data: hexDecode("a143abcdef187b"), wantObj: map[interface{}]interface{}{ ByteString("\xab\xcd\xef"): uint64(123), }, dm: bsAllowedMode, }, { - name: "tagged byte string map key with MapKeyByteStringAllowed", - cborData: hexDecode("a1d86443abcdef187b"), + name: "tagged byte string map key with MapKeyByteStringAllowed", + data: hexDecode("a1d86443abcdef187b"), wantObj: map[interface{}]interface{}{ Tag{Number: 100, Content: ByteString("\xab\xcd\xef")}: uint64(123), }, dm: bsAllowedMode, }, { - name: "nested tagged byte string map key with MapKeyByteStringAllowed", - cborData: hexDecode("a1d865d86443abcdef187b"), + name: "nested tagged byte string map key with MapKeyByteStringAllowed", + data: hexDecode("a1d865d86443abcdef187b"), wantObj: map[interface{}]interface{}{ Tag{Number: 101, Content: Tag{Number: 100, Content: ByteString("\xab\xcd\xef")}}: uint64(123), }, @@ -6447,18 +6447,18 @@ func TestMapKeyByteString(t *testing.T) { for _, typ := range []reflect.Type{typeIntf, typeMapIntfIntf} { v := reflect.New(typ) vPtr := v.Interface() - err = tc.dm.Unmarshal(tc.cborData, vPtr) + err = tc.dm.Unmarshal(tc.data, vPtr) if err == nil { if tc.wantErrorMsg != "" { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.cborData, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.data, tc.wantErrorMsg) } else if !reflect.DeepEqual(v.Elem().Interface(), tc.wantObj) { - t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.cborData, v.Elem().Interface(), v.Elem().Interface(), tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.data, v.Elem().Interface(), v.Elem().Interface(), tc.wantObj, tc.wantObj) } } else { if tc.wantErrorMsg == "" { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } } @@ -6488,44 +6488,44 @@ func TestExtraErrorCondUnknownField(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte dm DecMode wantObj interface{} wantErrorMsg string }{ { - name: "field by field match", - cborData: hexDecode("a3614161616142616261436163"), // map[string]string{"A": "a", "B": "b", "C": "c"} - dm: dm, - wantObj: s{A: "a", B: "b", C: "c"}, + name: "field by field match", + data: hexDecode("a3614161616142616261436163"), // map[string]string{"A": "a", "B": "b", "C": "c"} + dm: dm, + wantObj: s{A: "a", B: "b", C: "c"}, }, { - name: "field by field match with ExtraDecErrorUnknownField", - cborData: hexDecode("a3614161616142616261436163"), // map[string]string{"A": "a", "B": "b", "C": "c"} - dm: dmUnknownFieldError, - wantObj: s{A: "a", B: "b", C: "c"}, + name: "field by field match with ExtraDecErrorUnknownField", + data: hexDecode("a3614161616142616261436163"), // map[string]string{"A": "a", "B": "b", "C": "c"} + dm: dmUnknownFieldError, + wantObj: s{A: "a", B: "b", C: "c"}, }, { - name: "CBOR map less field", - cborData: hexDecode("a26141616161426162"), // map[string]string{"A": "a", "B": "b"} - dm: dm, - wantObj: s{A: "a", B: "b", C: ""}, + name: "CBOR map less field", + data: hexDecode("a26141616161426162"), // map[string]string{"A": "a", "B": "b"} + dm: dm, + wantObj: s{A: "a", B: "b", C: ""}, }, { - name: "CBOR map less field with ExtraDecErrorUnknownField", - cborData: hexDecode("a26141616161426162"), // map[string]string{"A": "a", "B": "b"} - dm: dmUnknownFieldError, - wantObj: s{A: "a", B: "b", C: ""}, + name: "CBOR map less field with ExtraDecErrorUnknownField", + data: hexDecode("a26141616161426162"), // map[string]string{"A": "a", "B": "b"} + dm: dmUnknownFieldError, + wantObj: s{A: "a", B: "b", C: ""}, }, { - name: "CBOR map unknown field", - cborData: hexDecode("a461416161614261626143616361446164"), // map[string]string{"A": "a", "B": "b", "C": "c", "D": "d"} - dm: dm, - wantObj: s{A: "a", B: "b", C: "c"}, + name: "CBOR map unknown field", + data: hexDecode("a461416161614261626143616361446164"), // map[string]string{"A": "a", "B": "b", "C": "c", "D": "d"} + dm: dm, + wantObj: s{A: "a", B: "b", C: "c"}, }, { name: "CBOR map unknown field with ExtraDecErrorUnknownField", - cborData: hexDecode("a461416161614261626143616361446164"), // map[string]string{"A": "a", "B": "b", "C": "c", "D": "d"} + data: hexDecode("a461416161614261626143616361446164"), // map[string]string{"A": "a", "B": "b", "C": "c", "D": "d"} dm: dmUnknownFieldError, wantErrorMsg: "cbor: found unknown field at map element index 3", }, @@ -6533,18 +6533,18 @@ func TestExtraErrorCondUnknownField(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v s - err := tc.dm.Unmarshal(tc.cborData, &v) + err := tc.dm.Unmarshal(tc.data, &v) if err == nil { if tc.wantErrorMsg != "" { - t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.cborData, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) didn't return an error, want %q", tc.data, tc.wantErrorMsg) } else if !reflect.DeepEqual(v, tc.wantObj) { - t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantObj, tc.wantObj) + t.Errorf("Unmarshal(0x%x) return %v (%T), want %v (%T)", tc.data, v, v, tc.wantObj, tc.wantObj) } } else { if tc.wantErrorMsg == "" { - t.Errorf("Unmarshal(0x%x) returned error %q", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned error %q", tc.data, err) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } }) @@ -6586,12 +6586,12 @@ func TestStreamExtraErrorCondUnknownField(t *testing.T) { C string } - cborData := hexDecode("a461416161614461646142616261436163a3614161616142616261436163") // map[string]string{"A": "a", "D": "d", "B": "b", "C": "c"}, map[string]string{"A": "a", "B": "b", "C": "c"} + data := hexDecode("a461416161614461646142616261436163a3614161616142616261436163") // map[string]string{"A": "a", "D": "d", "B": "b", "C": "c"}, map[string]string{"A": "a", "B": "b", "C": "c"} wantErrorMsg := "cbor: found unknown field at map element index 1" wantObj := s{A: "a", B: "b", C: "c"} dmUnknownFieldError, _ := DecOptions{ExtraReturnErrors: ExtraDecErrorUnknownField}.DecMode() - dec := dmUnknownFieldError.NewDecoder(bytes.NewReader(cborData)) + dec := dmUnknownFieldError.NewDecoder(bytes.NewReader(data)) var v1 s err := dec.Decode(&v1) @@ -6617,46 +6617,46 @@ func TestUnmarshalTagNum55799(t *testing.T) { for _, tc := range unmarshalTests { // Prefix tag number 55799 to CBOR test data - cborData := make([]byte, len(tc.data)+6) - copy(cborData, tagNum55799) - copy(cborData[3:], tagNum55799) - copy(cborData[6:], tc.data) + data := make([]byte, len(tc.data)+6) + copy(data, tagNum55799) + copy(data[3:], tagNum55799) + copy(data[6:], tc.data) // Test unmarshalling CBOR into empty interface. var v interface{} - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else { if tm, ok := tc.wantInterfaceValue.(time.Time); ok { if vt, ok := v.(time.Time); !ok || !tm.Equal(vt) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, tc.wantInterfaceValue, tc.wantInterfaceValue) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, tc.wantInterfaceValue, tc.wantInterfaceValue) } } else if !reflect.DeepEqual(v, tc.wantInterfaceValue) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, tc.wantInterfaceValue, tc.wantInterfaceValue) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, tc.wantInterfaceValue, tc.wantInterfaceValue) } } // Test unmarshalling CBOR into RawMessage. var r RawMessage - if err := Unmarshal(cborData, &r); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &r); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !bytes.Equal(r, tc.data) { - t.Errorf("Unmarshal(0x%x) returned RawMessage %v, want %v", cborData, r, tc.data) + t.Errorf("Unmarshal(0x%x) returned RawMessage %v, want %v", data, r, tc.data) } // Test unmarshalling CBOR into compatible data types. for _, value := range tc.wantValues { v := reflect.New(reflect.TypeOf(value)) vPtr := v.Interface() - if err := Unmarshal(cborData, vPtr); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, vPtr); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else { if tm, ok := value.(time.Time); ok { if vt, ok := v.Elem().Interface().(time.Time); !ok || !tm.Equal(vt) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v.Elem().Interface(), v.Elem().Interface(), value, value) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v.Elem().Interface(), v.Elem().Interface(), value, value) } } else if !reflect.DeepEqual(v.Elem().Interface(), value) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v.Elem().Interface(), v.Elem().Interface(), value, value) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v.Elem().Interface(), v.Elem().Interface(), value, value) } } } @@ -6665,12 +6665,12 @@ func TestUnmarshalTagNum55799(t *testing.T) { for _, typ := range tc.wrongTypes { v := reflect.New(typ) vPtr := v.Interface() - if err := Unmarshal(cborData, vPtr); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", cborData, typ.String()) + if err := Unmarshal(data, vPtr); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", data, typ.String()) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), "cannot unmarshal") { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), "cannot unmarshal") + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), "cannot unmarshal") } } } @@ -6683,9 +6683,9 @@ func TestUnmarshalFloatWithTagNum55799(t *testing.T) { for _, tc := range unmarshalFloatTests { // Prefix tag number 55799 to CBOR test data - cborData := make([]byte, len(tc.data)+3) - copy(cborData, tagNum55799) - copy(cborData[3:], tc.data) + data := make([]byte, len(tc.data)+3) + copy(data, tagNum55799) + copy(data[3:], tc.data) // Test unmarshalling CBOR into empty interface. var v interface{} @@ -6732,7 +6732,7 @@ func TestUnmarshalFloatWithTagNum55799(t *testing.T) { func TestUnmarshalTagNum55799AsElement(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte emptyInterfaceValue interface{} values []interface{} wrongTypes []reflect.Type @@ -6756,15 +6756,15 @@ func TestUnmarshalTagNum55799AsElement(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // Test unmarshalling CBOR into empty interface. var v interface{} - if err := Unmarshal(tc.cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", tc.cborData, err) + if err := Unmarshal(tc.data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", tc.data, err) } else { if tm, ok := tc.emptyInterfaceValue.(time.Time); ok { if vt, ok := v.(time.Time); !ok || !tm.Equal(vt) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v, v, tc.emptyInterfaceValue, tc.emptyInterfaceValue) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v, v, tc.emptyInterfaceValue, tc.emptyInterfaceValue) } } else if !reflect.DeepEqual(v, tc.emptyInterfaceValue) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v, v, tc.emptyInterfaceValue, tc.emptyInterfaceValue) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v, v, tc.emptyInterfaceValue, tc.emptyInterfaceValue) } } @@ -6772,15 +6772,15 @@ func TestUnmarshalTagNum55799AsElement(t *testing.T) { for _, value := range tc.values { v := reflect.New(reflect.TypeOf(value)) vPtr := v.Interface() - if err := Unmarshal(tc.cborData, vPtr); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", tc.cborData, err) + if err := Unmarshal(tc.data, vPtr); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", tc.data, err) } else { if tm, ok := value.(time.Time); ok { if vt, ok := v.Elem().Interface().(time.Time); !ok || !tm.Equal(vt) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v.Elem().Interface(), v.Elem().Interface(), value, value) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v.Elem().Interface(), v.Elem().Interface(), value, value) } } else if !reflect.DeepEqual(v.Elem().Interface(), value) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v.Elem().Interface(), v.Elem().Interface(), value, value) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v.Elem().Interface(), v.Elem().Interface(), value, value) } } } @@ -6789,12 +6789,12 @@ func TestUnmarshalTagNum55799AsElement(t *testing.T) { for _, typ := range tc.wrongTypes { v := reflect.New(typ) vPtr := v.Interface() - if err := Unmarshal(tc.cborData, vPtr); err == nil { - t.Errorf("Unmarshal(0x%x, %s) didn't return an error", tc.cborData, typ.String()) + if err := Unmarshal(tc.data, vPtr); err == nil { + t.Errorf("Unmarshal(0x%x, %s) didn't return an error", tc.data, typ.String()) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", tc.data, err) } else if !strings.Contains(err.Error(), "cannot unmarshal") { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", tc.cborData, err.Error(), "cannot unmarshal") + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", tc.data, err.Error(), "cannot unmarshal") } } }) @@ -6802,26 +6802,26 @@ func TestUnmarshalTagNum55799AsElement(t *testing.T) { } func TestUnmarshalTagNum55799ToBinaryUnmarshaler(t *testing.T) { - cborData := hexDecode("d9d9f74800000000499602d2") // 55799(h'00000000499602D2') + data := hexDecode("d9d9f74800000000499602d2") // 55799(h'00000000499602D2') wantObj := number(1234567890) var v number - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantObj, wantObj) } } func TestUnmarshalTagNum55799ToUnmarshaler(t *testing.T) { - cborData := hexDecode("d9d9f7d864a1636e756d01") // 55799(100({"num": 1})) + data := hexDecode("d9d9f7d864a1636e756d01") // 55799(100({"num": 1})) wantObj := number3(1) var v number3 - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantObj, wantObj) } } @@ -6836,88 +6836,88 @@ func TestUnmarshalTagNum55799ToRegisteredGoType(t *testing.T) { dm, _ := DecOptions{}.DecModeWithTags(tags) - cborData := hexDecode("d9d9f7d87d01") // 55799(125(1)) + data := hexDecode("d9d9f7d87d01") // 55799(125(1)) wantObj := myInt(1) var v myInt - if err := dm.Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := dm.Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantObj, wantObj) } } // TODO: wait for clarification from 7049bis https://github.com/cbor-wg/CBORbis/issues/183 // Nested tag number 55799 may be stripeed as well depending on 7049bis clarification. func TestUnmarshalNestedTagNum55799ToEmptyInterface(t *testing.T) { - cborData := hexDecode("d864d9d9f701") // 100(55799(1)) + data := hexDecode("d864d9d9f701") // 100(55799(1)) wantObj := Tag{100, Tag{55799, uint64(1)}} var v interface{} - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantObj, wantObj) } } func TestUnmarshalNestedTagNum55799ToValue(t *testing.T) { - cborData := hexDecode("d864d9d9f701") // 100(55799(1)) + data := hexDecode("d864d9d9f701") // 100(55799(1)) wantObj := 1 var v int - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantObj, wantObj) } } func TestUnmarshalNestedTagNum55799ToTag(t *testing.T) { - cborData := hexDecode("d864d9d9f701") // 100(55799(1)) + data := hexDecode("d864d9d9f701") // 100(55799(1)) wantObj := Tag{100, Tag{55799, uint64(1)}} var v Tag - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantObj, wantObj) } } func TestUnmarshalNestedTagNum55799ToTime(t *testing.T) { - cborData := hexDecode("c0d9d9f774323031332d30332d32315432303a30343a30305a") // 0(55799("2013-03-21T20:04:00Z")) + data := hexDecode("c0d9d9f774323031332d30332d32315432303a30343a30305a") // 0(55799("2013-03-21T20:04:00Z")) wantErrorMsg := "tag number 0 must be followed by text string, got tag" var v time.Time - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error", cborData) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error", data) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %s, want %s", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %s, want %s", data, err.Error(), wantErrorMsg) } } func TestUnmarshalNestedTagNum55799ToBinaryUnmarshaler(t *testing.T) { - cborData := hexDecode("d864d9d9f74800000000499602d2") // 100(55799(h'00000000499602D2')) + data := hexDecode("d864d9d9f74800000000499602d2") // 100(55799(h'00000000499602D2')) wantObj := number(1234567890) var v number - if err := Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } else if !reflect.DeepEqual(v, wantObj) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantObj, wantObj) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantObj, wantObj) } } func TestUnmarshalNestedTagNum55799ToUnmarshaler(t *testing.T) { - cborData := hexDecode("d864d9d9f7a1636e756d01") // 100(55799({"num": 1})) + data := hexDecode("d864d9d9f7a1636e756d01") // 100(55799({"num": 1})) wantErrorMsg := "wrong tag content type" var v number3 - if err := Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error", cborData) + if err := Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error", data) } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %s, want %s", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %s, want %s", data, err.Error(), wantErrorMsg) } } @@ -6932,53 +6932,53 @@ func TestUnmarshalNestedTagNum55799ToRegisteredGoType(t *testing.T) { dm, _ := DecOptions{}.DecModeWithTags(tags) - cborData := hexDecode("d87dd9d9f701") // 125(55799(1)) + data := hexDecode("d87dd9d9f701") // 125(55799(1)) wantErrorMsg := "cbor: wrong tag number for cbor.myInt, got [125 55799], expected [125]" var v myInt - if err := dm.Unmarshal(cborData, &v); err == nil { + if err := dm.Unmarshal(data, &v); err == nil { t.Errorf("Unmarshal() didn't return error") } else if !strings.Contains(err.Error(), wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %s, want %s", cborData, err.Error(), wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %s, want %s", data, err.Error(), wantErrorMsg) } } func TestUnmarshalPosIntToBigInt(t *testing.T) { - cborData := hexDecode("1bffffffffffffffff") // 18446744073709551615 + data := hexDecode("1bffffffffffffffff") // 18446744073709551615 wantEmptyInterfaceValue := uint64(18446744073709551615) wantBigIntValue := bigIntOrPanic("18446744073709551615") var v1 interface{} - if err := Unmarshal(cborData, &v1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", cborData, err) + if err := Unmarshal(data, &v1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", data, err) } else if !reflect.DeepEqual(v1, wantEmptyInterfaceValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", cborData, v1, v1, wantEmptyInterfaceValue, wantEmptyInterfaceValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", data, v1, v1, wantEmptyInterfaceValue, wantEmptyInterfaceValue) } var v2 big.Int - if err := Unmarshal(cborData, &v2); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", cborData, err) + if err := Unmarshal(data, &v2); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", data, err) } else if !reflect.DeepEqual(v2, wantBigIntValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", cborData, v2, v2, wantBigIntValue, wantBigIntValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", data, v2, v2, wantBigIntValue, wantBigIntValue) } } func TestUnmarshalNegIntToBigInt(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantEmptyInterfaceValue interface{} wantBigIntValue big.Int }{ { name: "fit Go int64", - cborData: hexDecode("3b7fffffffffffffff"), // -9223372036854775808 + data: hexDecode("3b7fffffffffffffff"), // -9223372036854775808 wantEmptyInterfaceValue: int64(-9223372036854775808), wantBigIntValue: bigIntOrPanic("-9223372036854775808"), }, { name: "overflow Go int64", - cborData: hexDecode("3b8000000000000000"), // -9223372036854775809 + data: hexDecode("3b8000000000000000"), // -9223372036854775809 wantEmptyInterfaceValue: bigIntOrPanic("-9223372036854775809"), wantBigIntValue: bigIntOrPanic("-9223372036854775809"), }, @@ -6986,17 +6986,17 @@ func TestUnmarshalNegIntToBigInt(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v1 interface{} - if err := Unmarshal(tc.cborData, &v1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", tc.cborData, err) + if err := Unmarshal(tc.data, &v1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", tc.data, err) } else if !reflect.DeepEqual(v1, tc.wantEmptyInterfaceValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v1, v1, tc.wantEmptyInterfaceValue, tc.wantEmptyInterfaceValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v1, v1, tc.wantEmptyInterfaceValue, tc.wantEmptyInterfaceValue) } var v2 big.Int - if err := Unmarshal(tc.cborData, &v2); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", tc.cborData, err) + if err := Unmarshal(tc.data, &v2); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", tc.data, err) } else if !reflect.DeepEqual(v2, tc.wantBigIntValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v2, v2, tc.wantBigIntValue, tc.wantBigIntValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v2, v2, tc.wantBigIntValue, tc.wantBigIntValue) } }) } @@ -7005,13 +7005,13 @@ func TestUnmarshalNegIntToBigInt(t *testing.T) { func TestUnmarshalTag2(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantEmptyInterfaceValue interface{} wantValues []interface{} }{ { name: "fit Go int64", - cborData: hexDecode("c2430f4240"), // 2(1000000) + data: hexDecode("c2430f4240"), // 2(1000000) wantEmptyInterfaceValue: bigIntOrPanic("1000000"), wantValues: []interface{}{ int64(1000000), @@ -7023,7 +7023,7 @@ func TestUnmarshalTag2(t *testing.T) { }, { name: "fit Go uint64", - cborData: hexDecode("c248ffffffffffffffff"), // 2(18446744073709551615) + data: hexDecode("c248ffffffffffffffff"), // 2(18446744073709551615) wantEmptyInterfaceValue: bigIntOrPanic("18446744073709551615"), wantValues: []interface{}{ uint64(18446744073709551615), @@ -7034,7 +7034,7 @@ func TestUnmarshalTag2(t *testing.T) { }, { name: "fit Go uint64 with leading zeros", - cborData: hexDecode("c24900ffffffffffffffff"), // 2(18446744073709551615) + data: hexDecode("c24900ffffffffffffffff"), // 2(18446744073709551615) wantEmptyInterfaceValue: bigIntOrPanic("18446744073709551615"), wantValues: []interface{}{ uint64(18446744073709551615), @@ -7045,7 +7045,7 @@ func TestUnmarshalTag2(t *testing.T) { }, { name: "overflow Go uint64", - cborData: hexDecode("c249010000000000000000"), // 2(18446744073709551616) + data: hexDecode("c249010000000000000000"), // 2(18446744073709551616) wantEmptyInterfaceValue: bigIntOrPanic("18446744073709551616"), wantValues: []interface{}{ bigIntOrPanic("18446744073709551616"), @@ -7053,7 +7053,7 @@ func TestUnmarshalTag2(t *testing.T) { }, { name: "overflow Go uint64 with leading zeros", - cborData: hexDecode("c24b0000010000000000000000"), // 2(18446744073709551616) + data: hexDecode("c24b0000010000000000000000"), // 2(18446744073709551616) wantEmptyInterfaceValue: bigIntOrPanic("18446744073709551616"), wantValues: []interface{}{ bigIntOrPanic("18446744073709551616"), @@ -7063,18 +7063,18 @@ func TestUnmarshalTag2(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v1 interface{} - if err := Unmarshal(tc.cborData, &v1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", tc.cborData, err) + if err := Unmarshal(tc.data, &v1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", tc.data, err) } else if !reflect.DeepEqual(v1, tc.wantEmptyInterfaceValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v1, v1, tc.wantEmptyInterfaceValue, tc.wantEmptyInterfaceValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v1, v1, tc.wantEmptyInterfaceValue, tc.wantEmptyInterfaceValue) } for _, wantValue := range tc.wantValues { v := reflect.New(reflect.TypeOf(wantValue)) - if err := Unmarshal(tc.cborData, v.Interface()); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", tc.cborData, err) + if err := Unmarshal(tc.data, v.Interface()); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", tc.data, err) } else if !reflect.DeepEqual(v.Elem().Interface(), wantValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v.Elem().Interface(), v.Elem().Interface(), wantValue, wantValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v.Elem().Interface(), v.Elem().Interface(), wantValue, wantValue) } } }) @@ -7084,13 +7084,13 @@ func TestUnmarshalTag2(t *testing.T) { func TestUnmarshalTag3(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantEmptyInterfaceValue interface{} wantValues []interface{} }{ { name: "fit Go int64", - cborData: hexDecode("c3487fffffffffffffff"), // 3(-9223372036854775808) + data: hexDecode("c3487fffffffffffffff"), // 3(-9223372036854775808) wantEmptyInterfaceValue: bigIntOrPanic("-9223372036854775808"), wantValues: []interface{}{ int64(-9223372036854775808), @@ -7101,7 +7101,7 @@ func TestUnmarshalTag3(t *testing.T) { }, { name: "fit Go int64 with leading zeros", - cborData: hexDecode("c349007fffffffffffffff"), // 3(-9223372036854775808) + data: hexDecode("c349007fffffffffffffff"), // 3(-9223372036854775808) wantEmptyInterfaceValue: bigIntOrPanic("-9223372036854775808"), wantValues: []interface{}{ int64(-9223372036854775808), @@ -7112,7 +7112,7 @@ func TestUnmarshalTag3(t *testing.T) { }, { name: "overflow Go int64", - cborData: hexDecode("c349010000000000000000"), // 3(-18446744073709551617) + data: hexDecode("c349010000000000000000"), // 3(-18446744073709551617) wantEmptyInterfaceValue: bigIntOrPanic("-18446744073709551617"), wantValues: []interface{}{ bigIntOrPanic("-18446744073709551617"), @@ -7120,7 +7120,7 @@ func TestUnmarshalTag3(t *testing.T) { }, { name: "overflow Go int64 with leading zeros", - cborData: hexDecode("c34b0000010000000000000000"), // 3(-18446744073709551617) + data: hexDecode("c34b0000010000000000000000"), // 3(-18446744073709551617) wantEmptyInterfaceValue: bigIntOrPanic("-18446744073709551617"), wantValues: []interface{}{ bigIntOrPanic("-18446744073709551617"), @@ -7130,18 +7130,18 @@ func TestUnmarshalTag3(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v1 interface{} - if err := Unmarshal(tc.cborData, &v1); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", tc.cborData, err) + if err := Unmarshal(tc.data, &v1); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", tc.data, err) } else if !reflect.DeepEqual(v1, tc.wantEmptyInterfaceValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v1, v1, tc.wantEmptyInterfaceValue, tc.wantEmptyInterfaceValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v1, v1, tc.wantEmptyInterfaceValue, tc.wantEmptyInterfaceValue) } for _, wantValue := range tc.wantValues { v := reflect.New(reflect.TypeOf(wantValue)) - if err := Unmarshal(tc.cborData, v.Interface()); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %+v", tc.cborData, err) + if err := Unmarshal(tc.data, v.Interface()); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %+v", tc.data, err) } else if !reflect.DeepEqual(v.Elem().Interface(), wantValue) { - t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.cborData, v.Elem().Interface(), v.Elem().Interface(), wantValue, wantValue) + t.Errorf("Unmarshal(0x%x) returned %v (%T), want %v (%T)", tc.data, v.Elem().Interface(), v.Elem().Interface(), wantValue, wantValue) } } }) @@ -7153,25 +7153,25 @@ func TestUnmarshalInvalidTagBignum(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte decodeToTypes []reflect.Type wantErrorMsg string }{ { name: "Tag 2 with string", - cborData: hexDecode("c27f657374726561646d696e67ff"), + data: hexDecode("c27f657374726561646d696e67ff"), decodeToTypes: []reflect.Type{typeIntf, typeBigInt}, wantErrorMsg: "cbor: tag number 2 or 3 must be followed by byte string, got UTF-8 text string", }, { name: "Tag 3 with string", - cborData: hexDecode("c37f657374726561646d696e67ff"), + data: hexDecode("c37f657374726561646d696e67ff"), decodeToTypes: []reflect.Type{typeIntf, typeBigInt}, wantErrorMsg: "cbor: tag number 2 or 3 must be followed by byte string, got UTF-8 text string", }, { name: "Tag 3 with negavtive int", - cborData: hexDecode("81C330"), // [3(-17)] + data: hexDecode("81C330"), // [3(-17)] decodeToTypes: []reflect.Type{typeIntf, typeBigIntSlice}, wantErrorMsg: "cbor: tag number 2 or 3 must be followed by byte string, got negative integer", }, @@ -7180,10 +7180,10 @@ func TestUnmarshalInvalidTagBignum(t *testing.T) { for _, decodeToType := range tc.decodeToTypes { t.Run(tc.name+" decode to "+decodeToType.String(), func(t *testing.T) { v := reflect.New(decodeToType) - if err := Unmarshal(tc.cborData, v.Interface()); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", tc.cborData, tc.wantErrorMsg) + if err := Unmarshal(tc.data, v.Interface()); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error, want error msg %q", tc.data, tc.wantErrorMsg) } else if !strings.Contains(err.Error(), tc.wantErrorMsg) { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.cborData, err, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", tc.data, err, tc.wantErrorMsg) } }) } @@ -7558,7 +7558,7 @@ func TestUnmarshalToDefaultMapType(t *testing.T) { testCases := []struct { name string opts DecOptions - cborData []byte + data []byte wantValue interface{} wantErrorMsg string }{ @@ -7566,28 +7566,28 @@ func TestUnmarshalToDefaultMapType(t *testing.T) { { name: "decode CBOR map[int]int to Go map[interface{}]interface{} (default)", opts: decOptionsDefault, - cborData: cborDataMapIntInt, + data: cborDataMapIntInt, wantValue: map[interface{}]interface{}{uint64(1): uint64(2), uint64(3): uint64(4)}, }, { name: "decode CBOR map[string]int to Go map[interface{}]interface{} (default)", opts: decOptionsDefault, - cborData: cborDataMapStringInt, + data: cborDataMapStringInt, wantValue: map[interface{}]interface{}{"a": uint64(1), "b": uint64(2)}, }, { - name: "decode CBOR array of map[string]int to Go []map[interface{}]interface{} (default)", - opts: decOptionsDefault, - cborData: cborDataArrayOfMapStringint, + name: "decode CBOR array of map[string]int to Go []map[interface{}]interface{} (default)", + opts: decOptionsDefault, + data: cborDataArrayOfMapStringint, wantValue: []interface{}{ map[interface{}]interface{}{"a": uint64(1), "b": uint64(2)}, map[interface{}]interface{}{"c": uint64(3), "d": uint64(4)}, }, }, { - name: "decode CBOR nested map to Go map[interface{}]interface{} (default)", - opts: decOptionsDefault, - cborData: cborDataNestedMap, + name: "decode CBOR nested map to Go map[interface{}]interface{} (default)", + opts: decOptionsDefault, + data: cborDataNestedMap, wantValue: map[interface{}]interface{}{ "IntField": uint64(1), "MapField": map[interface{}]interface{}{"a": uint64(1), "b": uint64(2)}, @@ -7597,28 +7597,28 @@ func TestUnmarshalToDefaultMapType(t *testing.T) { { name: "decode CBOR map[int]int to Go map[interface{}]interface{}", opts: decOptionsMapIntfIntfType, - cborData: cborDataMapIntInt, + data: cborDataMapIntInt, wantValue: map[interface{}]interface{}{uint64(1): uint64(2), uint64(3): uint64(4)}, }, { name: "decode CBOR map[string]int to Go map[interface{}]interface{}", opts: decOptionsMapIntfIntfType, - cborData: cborDataMapStringInt, + data: cborDataMapStringInt, wantValue: map[interface{}]interface{}{"a": uint64(1), "b": uint64(2)}, }, { - name: "decode CBOR array of map[string]int to Go []map[interface{}]interface{}", - opts: decOptionsMapIntfIntfType, - cborData: cborDataArrayOfMapStringint, + name: "decode CBOR array of map[string]int to Go []map[interface{}]interface{}", + opts: decOptionsMapIntfIntfType, + data: cborDataArrayOfMapStringint, wantValue: []interface{}{ map[interface{}]interface{}{"a": uint64(1), "b": uint64(2)}, map[interface{}]interface{}{"c": uint64(3), "d": uint64(4)}, }, }, { - name: "decode CBOR nested map to Go map[interface{}]interface{}", - opts: decOptionsMapIntfIntfType, - cborData: cborDataNestedMap, + name: "decode CBOR nested map to Go map[interface{}]interface{}", + opts: decOptionsMapIntfIntfType, + data: cborDataNestedMap, wantValue: map[interface{}]interface{}{ "IntField": uint64(1), "MapField": map[interface{}]interface{}{"a": uint64(1), "b": uint64(2)}, @@ -7628,28 +7628,28 @@ func TestUnmarshalToDefaultMapType(t *testing.T) { { name: "decode CBOR map[int]int to Go map[string]interface{}", opts: decOptionsMapStringIntfType, - cborData: cborDataMapIntInt, + data: cborDataMapIntInt, wantErrorMsg: "cbor: cannot unmarshal positive integer into Go value of type string", }, { name: "decode CBOR map[string]int to Go map[string]interface{}", opts: decOptionsMapStringIntfType, - cborData: cborDataMapStringInt, + data: cborDataMapStringInt, wantValue: map[string]interface{}{"a": uint64(1), "b": uint64(2)}, }, { - name: "decode CBOR array of map[string]int to Go []map[string]interface{}", - opts: decOptionsMapStringIntfType, - cborData: cborDataArrayOfMapStringint, + name: "decode CBOR array of map[string]int to Go []map[string]interface{}", + opts: decOptionsMapStringIntfType, + data: cborDataArrayOfMapStringint, wantValue: []interface{}{ map[string]interface{}{"a": uint64(1), "b": uint64(2)}, map[string]interface{}{"c": uint64(3), "d": uint64(4)}, }, }, { - name: "decode CBOR nested map to Go map[string]interface{}", - opts: decOptionsMapStringIntfType, - cborData: cborDataNestedMap, + name: "decode CBOR nested map to Go map[string]interface{}", + opts: decOptionsMapStringIntfType, + data: cborDataNestedMap, wantValue: map[string]interface{}{ "IntField": uint64(1), "MapField": map[string]interface{}{"a": uint64(1), "b": uint64(2)}, @@ -7659,19 +7659,19 @@ func TestUnmarshalToDefaultMapType(t *testing.T) { { name: "decode CBOR map[int]int to Go map[string]int", opts: decOptionsMapStringIntType, - cborData: cborDataMapIntInt, + data: cborDataMapIntInt, wantErrorMsg: "cbor: cannot unmarshal positive integer into Go value of type string", }, { name: "decode CBOR map[string]int to Go map[string]int", opts: decOptionsMapStringIntType, - cborData: cborDataMapStringInt, + data: cborDataMapStringInt, wantValue: map[string]int{"a": 1, "b": 2}, }, { - name: "decode CBOR array of map[string]int to Go []map[string]int", - opts: decOptionsMapStringIntType, - cborData: cborDataArrayOfMapStringint, + name: "decode CBOR array of map[string]int to Go []map[string]int", + opts: decOptionsMapStringIntType, + data: cborDataArrayOfMapStringint, wantValue: []interface{}{ map[string]int{"a": 1, "b": 2}, map[string]int{"c": 3, "d": 4}, @@ -7680,7 +7680,7 @@ func TestUnmarshalToDefaultMapType(t *testing.T) { { name: "decode CBOR nested map to Go map[string]int", opts: decOptionsMapStringIntType, - cborData: cborDataNestedMap, + data: cborDataNestedMap, wantErrorMsg: "cbor: cannot unmarshal map into Go value of type int", }, } @@ -7690,18 +7690,18 @@ func TestUnmarshalToDefaultMapType(t *testing.T) { decMode, _ := tc.opts.DecMode() var v interface{} - err := decMode.Unmarshal(tc.cborData, &v) + err := decMode.Unmarshal(tc.data, &v) if err != nil { if tc.wantErrorMsg == "" { - t.Errorf("Unmarshal(0x%x) to empty interface returned error %v", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) to empty interface returned error %v", tc.data, err) } else if tc.wantErrorMsg != err.Error() { - t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) error %q, want %q", tc.data, err.Error(), tc.wantErrorMsg) } } else { if tc.wantValue == nil { - t.Errorf("Unmarshal(0x%x) = %v (%T), want error %q", tc.cborData, v, v, tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) = %v (%T), want error %q", tc.data, v, v, tc.wantErrorMsg) } else if !reflect.DeepEqual(v, tc.wantValue) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantValue, tc.wantValue) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v, v, tc.wantValue, tc.wantValue) } } }) @@ -7803,46 +7803,46 @@ func TestDecodeFieldNameMatching(t *testing.T) { testCases := []struct { name string opts DecOptions - cborData []byte + data []byte wantValue s }{ { name: "case-insensitive match", - cborData: hexDecode("a1614101"), // {"A": 1} + data: hexDecode("a1614101"), // {"A": 1} wantValue: s{LowerA: 1}, }, { name: "ignore case-insensitive match", opts: DecOptions{FieldNameMatching: FieldNameMatchingCaseSensitive}, - cborData: hexDecode("a1614101"), // {"A": 1} + data: hexDecode("a1614101"), // {"A": 1} wantValue: s{}, }, { name: "exact match before case-insensitive match", - cborData: hexDecode("a2616101614102"), // {"a": 1, "A": 2} + data: hexDecode("a2616101614102"), // {"a": 1, "A": 2} wantValue: s{LowerA: 1}, }, { name: "case-insensitive match before exact match", - cborData: hexDecode("a2614101616102"), // {"A": 1, "a": 2} + data: hexDecode("a2614101616102"), // {"A": 1, "a": 2} wantValue: s{LowerA: 1}, }, { name: "ignore case-insensitive match before exact match", opts: DecOptions{FieldNameMatching: FieldNameMatchingCaseSensitive}, - cborData: hexDecode("a2614101616102"), // {"A": 1, "a": 2} + data: hexDecode("a2614101616102"), // {"A": 1, "a": 2} wantValue: s{LowerA: 2}, }, { name: "earliest exact match wins", opts: DecOptions{FieldNameMatching: FieldNameMatchingCaseSensitive}, - cborData: hexDecode("a2616101616102"), // {"a": 1, "a": 2} (invalid) + data: hexDecode("a2616101616102"), // {"a": 1, "a": 2} (invalid) wantValue: s{LowerA: 1}, }, { // the field tags themselves are case-insensitive matches for each other name: "duplicate keys decode to different fields", - cborData: hexDecode("a2614201614202"), // {"B": 1, "B": 2} (invalid) + data: hexDecode("a2614201614202"), // {"B": 1, "B": 2} (invalid) wantValue: s{UpperB: 1, LowerB: 2}, }, } @@ -7852,13 +7852,13 @@ func TestDecodeFieldNameMatching(t *testing.T) { decMode, _ := tc.opts.DecMode() var dst s - err := decMode.Unmarshal(tc.cborData, &dst) + err := decMode.Unmarshal(tc.data, &dst) if err != nil { - t.Fatalf("Unmarshal(0x%x) returned unexpected error %v", tc.cborData, err) + t.Fatalf("Unmarshal(0x%x) returned unexpected error %v", tc.data, err) } if !reflect.DeepEqual(dst, tc.wantValue) { - t.Errorf("Unmarshal(0x%x) = %#v, want %#v", tc.cborData, dst, tc.wantValue) + t.Errorf("Unmarshal(0x%x) = %#v, want %#v", tc.data, dst, tc.wantValue) } }) } @@ -7908,43 +7908,43 @@ func TestDecodeBignumToEmptyInterface(t *testing.T) { testCases := []struct { name string opts DecOptions - cborData []byte + data []byte wantValue interface{} }{ { name: "decode positive bignum to big.Int", opts: decOptionsDecodeToBigIntValue, - cborData: cborDataPositiveBignum, + data: cborDataPositiveBignum, wantValue: *pbn, }, { name: "decode negative bignum to big.Int", opts: decOptionsDecodeToBigIntValue, - cborData: cborDataNegativeBignum, + data: cborDataNegativeBignum, wantValue: *nbn, }, { name: "decode large negative int to big.Int", opts: decOptionsDecodeToBigIntValue, - cborData: cborDataLargeNegativeInt, + data: cborDataLargeNegativeInt, wantValue: *ni, }, { name: "decode positive bignum to *big.Int", opts: decOptionsDecodeToBigIntPointer, - cborData: cborDataPositiveBignum, + data: cborDataPositiveBignum, wantValue: pbn, }, { name: "decode negative bignum to *big.Int", opts: decOptionsDecodeToBigIntPointer, - cborData: cborDataNegativeBignum, + data: cborDataNegativeBignum, wantValue: nbn, }, { name: "decode large negative int to *big.Int", opts: decOptionsDecodeToBigIntPointer, - cborData: cborDataLargeNegativeInt, + data: cborDataLargeNegativeInt, wantValue: ni, }, } @@ -7954,12 +7954,12 @@ func TestDecodeBignumToEmptyInterface(t *testing.T) { decMode, _ := tc.opts.DecMode() var v interface{} - err := decMode.Unmarshal(tc.cborData, &v) + err := decMode.Unmarshal(tc.data, &v) if err != nil { - t.Errorf("Unmarshal(0x%x) to empty interface returned error %v", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) to empty interface returned error %v", tc.data, err) } else { if !reflect.DeepEqual(v, tc.wantValue) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.cborData, v, v, tc.wantValue, tc.wantValue) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", tc.data, v, v, tc.wantValue, tc.wantValue) } } }) diff --git a/diagnose_test.go b/diagnose_test.go index 995456d9..8abf6a6f 100644 --- a/diagnose_test.go +++ b/diagnose_test.go @@ -1041,27 +1041,27 @@ func TestInvalidDiagnoseOptions(t *testing.T) { } func TestDiagnoseExtraneousData(t *testing.T) { - cborData := hexDecode("63666F6FF6") - _, err := Diagnose(cborData) + data := hexDecode("63666F6FF6") + _, err := Diagnose(data) if err == nil { - t.Errorf("Diagnose(0x%x) didn't return error", cborData) + t.Errorf("Diagnose(0x%x) didn't return error", data) } else if !strings.Contains(err.Error(), `extraneous data`) { - t.Errorf("Diagnose(0x%x) returned error %q", cborData, err) + t.Errorf("Diagnose(0x%x) returned error %q", data, err) } - _, _, err = DiagnoseFirst(cborData) + _, _, err = DiagnoseFirst(data) if err != nil { - t.Errorf("DiagnoseFirst(0x%x) returned error %v", cborData, err) + t.Errorf("DiagnoseFirst(0x%x) returned error %v", data, err) } } func TestDiagnoseNotwellformedData(t *testing.T) { - cborData := hexDecode("5f4060ff") - _, err := Diagnose(cborData) + data := hexDecode("5f4060ff") + _, err := Diagnose(data) if err == nil { - t.Errorf("Diagnose(0x%x) didn't return error", cborData) + t.Errorf("Diagnose(0x%x) didn't return error", data) } else if !strings.Contains(err.Error(), `wrong element type`) { - t.Errorf("Diagnose(0x%x) returned error %q", cborData, err) + t.Errorf("Diagnose(0x%x) returned error %q", data, err) } } diff --git a/encode_test.go b/encode_test.go index e5d22e08..bf514931 100644 --- a/encode_test.go +++ b/encode_test.go @@ -18,8 +18,8 @@ import ( ) type marshalTest struct { - cborData []byte - values []interface{} + data []byte + values []interface{} } type marshalErrorTest struct { @@ -352,13 +352,13 @@ func TestMarshalLargeByteString(t *testing.T) { lengths := []int{0, 1, 2, 22, 23, 24, 254, 255, 256, 65534, 65535, 65536, 10000000} tests := make([]marshalTest, len(lengths)) for i, length := range lengths { - cborData := bytes.NewBuffer(encodeCborHeader(cborTypeByteString, uint64(length))) + data := bytes.NewBuffer(encodeCborHeader(cborTypeByteString, uint64(length))) value := make([]byte, length) for j := 0; j < length; j++ { - cborData.WriteByte(100) + data.WriteByte(100) value[j] = 100 } - tests[i] = marshalTest{cborData.Bytes(), []interface{}{value}} + tests[i] = marshalTest{data.Bytes(), []interface{}{value}} } testMarshal(t, tests) @@ -369,13 +369,13 @@ func TestMarshalLargeTextString(t *testing.T) { lengths := []int{0, 1, 2, 22, 23, 24, 254, 255, 256, 65534, 65535, 65536, 10000000} tests := make([]marshalTest, len(lengths)) for i, length := range lengths { - cborData := bytes.NewBuffer(encodeCborHeader(cborTypeTextString, uint64(length))) + data := bytes.NewBuffer(encodeCborHeader(cborTypeTextString, uint64(length))) value := make([]byte, length) for j := 0; j < length; j++ { - cborData.WriteByte(100) + data.WriteByte(100) value[j] = 100 } - tests[i] = marshalTest{cborData.Bytes(), []interface{}{string(value)}} + tests[i] = marshalTest{data.Bytes(), []interface{}{string(value)}} } testMarshal(t, tests) @@ -386,13 +386,13 @@ func TestMarshalLargeArray(t *testing.T) { lengths := []int{0, 1, 2, 22, 23, 24, 254, 255, 256, 65534, 65535, 65536, 131072} tests := make([]marshalTest, len(lengths)) for i, length := range lengths { - cborData := bytes.NewBuffer(encodeCborHeader(cborTypeArray, uint64(length))) + data := bytes.NewBuffer(encodeCborHeader(cborTypeArray, uint64(length))) value := make([]string, length) for j := 0; j < length; j++ { - cborData.Write([]byte{0x63, 0xe6, 0xb0, 0xb4}) + data.Write([]byte{0x63, 0xe6, 0xb0, 0xb4}) value[j] = "水" } - tests[i] = marshalTest{cborData.Bytes(), []interface{}{value}} + tests[i] = marshalTest{data.Bytes(), []interface{}{value}} } testMarshal(t, tests) @@ -403,15 +403,15 @@ func TestMarshalLargeMapCanonical(t *testing.T) { lengths := []int{0, 1, 2, 22, 23, 24, 254, 255, 256, 65534, 65535, 65536, 131072} tests := make([]marshalTest, len(lengths)) for i, length := range lengths { - cborData := bytes.NewBuffer(encodeCborHeader(cborTypeMap, uint64(length))) + data := bytes.NewBuffer(encodeCborHeader(cborTypeMap, uint64(length))) value := make(map[int]int, length) for j := 0; j < length; j++ { d := encodeCborHeader(cborTypePositiveInt, uint64(j)) - cborData.Write(d) - cborData.Write(d) + data.Write(d) + data.Write(d) value[j] = j } - tests[i] = marshalTest{cborData.Bytes(), []interface{}{value}} + tests[i] = marshalTest{data.Bytes(), []interface{}{value}} } testMarshal(t, tests) @@ -426,14 +426,14 @@ func TestMarshalLargeMap(t *testing.T) { m1[i] = i } - cborData, err := Marshal(m1) + data, err := Marshal(m1) if err != nil { t.Fatalf("Marshal(%v) returned error %v", m1, err) } m2 := make(map[int]int) - if err = Unmarshal(cborData, &m2); err != nil { - t.Fatalf("Unmarshal(0x%x) returned error %v", cborData, err) + if err = Unmarshal(data, &m2); err != nil { + t.Fatalf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(m1, m2) { @@ -478,11 +478,11 @@ func testMarshal(t *testing.T, testCases []marshalTest) { } if b, err := em.Marshal(value); err != nil { t.Errorf("Marshal(%v) returned error %v", value, err) - } else if !bytes.Equal(b, tc.cborData) { - t.Errorf("Marshal(%v) = 0x%x, want 0x%x", value, b, tc.cborData) + } else if !bytes.Equal(b, tc.data) { + t.Errorf("Marshal(%v) = 0x%x, want 0x%x", value, b, tc.data) } } - r := RawMessage(tc.cborData) + r := RawMessage(tc.data) if b, err := Marshal(r); err != nil { t.Errorf("Marshal(%v) returned error %v", r, err) } else if !bytes.Equal(b, r) { @@ -514,14 +514,14 @@ func TestMarshalStruct(t *testing.T) { NestedStructField: &inner{X: 1000, Y: 1000000}, } - cborData, err := Marshal(v1) + data, err := Marshal(v1) if err != nil { t.Fatalf("Marshal(%v) returned error %v", v1, err) } var v2 outer - if err = Unmarshal(cborData, &v2); err != nil { - t.Fatalf("Unmarshal(0x%x) returned error %v", cborData, err) + if err = Unmarshal(data, &v2); err != nil { + t.Fatalf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(unmarshalWant, v2) { @@ -540,65 +540,65 @@ func TestMarshalStructCanonical(t *testing.T) { NestedStructField: &inner{X: 1000, Y: 1000000, z: 10000000}, unexportedField: 6, } - var cborData bytes.Buffer - cborData.WriteByte(byte(cborTypeMap) | 8) // CBOR header: map type with 8 items (exported fields) - - cborData.WriteByte(byte(cborTypeTextString) | 8) // "IntField" - cborData.WriteString("IntField") - cborData.WriteByte(byte(cborTypePositiveInt) | 24) - cborData.WriteByte(123) - - cborData.WriteByte(byte(cborTypeTextString) | 8) // "MapField" - cborData.WriteString("MapField") - cborData.WriteByte(byte(cborTypeMap) | 2) - cborData.WriteByte(byte(cborTypeTextString) | 7) - cborData.WriteString("morning") - cborData.WriteByte(byte(cborTypePrimitives) | 21) - cborData.WriteByte(byte(cborTypeTextString) | 9) - cborData.WriteString("afternoon") - cborData.WriteByte(byte(cborTypePrimitives) | 20) - - cborData.WriteByte(byte(cborTypeTextString) | 9) // "BoolField" - cborData.WriteString("BoolField") - cborData.WriteByte(byte(cborTypePrimitives) | 21) - - cborData.WriteByte(byte(cborTypeTextString) | 10) // "ArrayField" - cborData.WriteString("ArrayField") - cborData.WriteByte(byte(cborTypeArray) | 2) - cborData.WriteByte(byte(cborTypeTextString) | 5) - cborData.WriteString("hello") - cborData.WriteByte(byte(cborTypeTextString) | 5) - cborData.WriteString("world") - - cborData.WriteByte(byte(cborTypeTextString) | 10) // "FloatField" - cborData.WriteString("FloatField") - cborData.Write([]byte{0xfa, 0x47, 0xc3, 0x50, 0x00}) - - cborData.WriteByte(byte(cborTypeTextString) | 11) // "StringField" - cborData.WriteString("StringField") - cborData.WriteByte(byte(cborTypeTextString) | 4) - cborData.WriteString("test") - - cborData.WriteByte(byte(cborTypeTextString) | 15) // "ByteStringField" - cborData.WriteString("ByteStringField") - cborData.WriteByte(byte(cborTypeByteString) | 3) - cborData.Write([]byte{1, 3, 5}) - - cborData.WriteByte(byte(cborTypeTextString) | 17) // "NestedStructField" - cborData.WriteString("NestedStructField") - cborData.WriteByte(byte(cborTypeMap) | 2) - cborData.WriteByte(byte(cborTypeTextString) | 1) - cborData.WriteString("X") - cborData.WriteByte(byte(cborTypePositiveInt) | 25) + var data bytes.Buffer + data.WriteByte(byte(cborTypeMap) | 8) // CBOR header: map type with 8 items (exported fields) + + data.WriteByte(byte(cborTypeTextString) | 8) // "IntField" + data.WriteString("IntField") + data.WriteByte(byte(cborTypePositiveInt) | 24) + data.WriteByte(123) + + data.WriteByte(byte(cborTypeTextString) | 8) // "MapField" + data.WriteString("MapField") + data.WriteByte(byte(cborTypeMap) | 2) + data.WriteByte(byte(cborTypeTextString) | 7) + data.WriteString("morning") + data.WriteByte(byte(cborTypePrimitives) | 21) + data.WriteByte(byte(cborTypeTextString) | 9) + data.WriteString("afternoon") + data.WriteByte(byte(cborTypePrimitives) | 20) + + data.WriteByte(byte(cborTypeTextString) | 9) // "BoolField" + data.WriteString("BoolField") + data.WriteByte(byte(cborTypePrimitives) | 21) + + data.WriteByte(byte(cborTypeTextString) | 10) // "ArrayField" + data.WriteString("ArrayField") + data.WriteByte(byte(cborTypeArray) | 2) + data.WriteByte(byte(cborTypeTextString) | 5) + data.WriteString("hello") + data.WriteByte(byte(cborTypeTextString) | 5) + data.WriteString("world") + + data.WriteByte(byte(cborTypeTextString) | 10) // "FloatField" + data.WriteString("FloatField") + data.Write([]byte{0xfa, 0x47, 0xc3, 0x50, 0x00}) + + data.WriteByte(byte(cborTypeTextString) | 11) // "StringField" + data.WriteString("StringField") + data.WriteByte(byte(cborTypeTextString) | 4) + data.WriteString("test") + + data.WriteByte(byte(cborTypeTextString) | 15) // "ByteStringField" + data.WriteString("ByteStringField") + data.WriteByte(byte(cborTypeByteString) | 3) + data.Write([]byte{1, 3, 5}) + + data.WriteByte(byte(cborTypeTextString) | 17) // "NestedStructField" + data.WriteString("NestedStructField") + data.WriteByte(byte(cborTypeMap) | 2) + data.WriteByte(byte(cborTypeTextString) | 1) + data.WriteString("X") + data.WriteByte(byte(cborTypePositiveInt) | 25) b := make([]byte, 2) binary.BigEndian.PutUint16(b, uint16(1000)) - cborData.Write(b) - cborData.WriteByte(byte(cborTypeTextString) | 1) - cborData.WriteString("Y") - cborData.WriteByte(byte(cborTypePositiveInt) | 26) + data.Write(b) + data.WriteByte(byte(cborTypeTextString) | 1) + data.WriteString("Y") + data.WriteByte(byte(cborTypePositiveInt) | 26) b = make([]byte, 4) binary.BigEndian.PutUint32(b, uint32(1000000)) - cborData.Write(b) + data.Write(b) em, err := EncOptions{Sort: SortCanonical}.EncMode() if err != nil { @@ -606,8 +606,8 @@ func TestMarshalStructCanonical(t *testing.T) { } if b, err := em.Marshal(v); err != nil { t.Errorf("Marshal(%v) returned error %v", v, err) - } else if !bytes.Equal(b, cborData.Bytes()) { - t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, b, cborData.Bytes()) + } else if !bytes.Equal(b, data.Bytes()) { + t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, b, data.Bytes()) } } @@ -622,12 +622,12 @@ func TestMarshalNullPointerToEmbeddedStruct(t *testing.T) { ) v := T2{} wantCborData := []byte{0xa0} // {} - cborData, err := Marshal(v) + data, err := Marshal(v) if err != nil { t.Fatalf("Marshal(%v) returned error %v", v, err) } - if !bytes.Equal(wantCborData, cborData) { - t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, cborData, wantCborData) + if !bytes.Equal(wantCborData, data) { + t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, data, wantCborData) } } @@ -642,12 +642,12 @@ func TestMarshalNullPointerToStruct(t *testing.T) { ) v := T2{} wantCborData := []byte{0xa1, 0x61, 0x54, 0xf6} // {X: nil} - cborData, err := Marshal(v) + data, err := Marshal(v) if err != nil { t.Fatalf("Marshal(%v) returned error %v", v, err) } - if !bytes.Equal(wantCborData, cborData) { - t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, cborData, wantCborData) + if !bytes.Equal(wantCborData, data) { + t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, data, wantCborData) } } @@ -2085,19 +2085,19 @@ func TestCyclicDataStructure(t *testing.T) { } v := Node{1, &Node{2, &Node{3, nil}}} // linked list: 1, 2, 3 wantCborData := []byte{0xa2, 0x61, 0x76, 0x01, 0x61, 0x6e, 0xa2, 0x61, 0x76, 0x02, 0x61, 0x6e, 0xa1, 0x61, 0x76, 0x03} // {v: 1, n: {v: 2, n: {v: 3}}} - cborData, err := Marshal(v) + data, err := Marshal(v) if err != nil { t.Fatalf("Marshal(%v) returned error %v", v, err) } - if !bytes.Equal(wantCborData, cborData) { - t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, cborData, wantCborData) + if !bytes.Equal(wantCborData, data) { + t.Errorf("Marshal(%v) = 0x%x, want 0x%x", v, data, wantCborData) } var v1 Node - if err = Unmarshal(cborData, &v1); err != nil { - t.Fatalf("Unmarshal(0x%x) returned error %v", cborData, err) + if err = Unmarshal(data, &v1); err != nil { + t.Fatalf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, v1) { - t.Errorf("Unmarshal(0x%x) returned %+v, want %+v", cborData, v1, v) + t.Errorf("Unmarshal(0x%x) returned %+v, want %+v", data, v1, v) } } @@ -3270,7 +3270,7 @@ func TestInvalidNaNConvert(t *testing.T) { func TestMarshalSenML(t *testing.T) { // Data from https://tools.ietf.org/html/rfc8428#section-6 // Data contains 13 floating-point numbers. - cborData := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") + data := hexDecode("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") testCases := []struct { name string opts EncOptions @@ -3281,7 +3281,7 @@ func TestMarshalSenML(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v []SenMLRecord - if err := Unmarshal(cborData, &v); err != nil { + if err := Unmarshal(data, &v); err != nil { t.Errorf("Marshal() returned error %v", err) } em, err := tc.opts.EncMode() diff --git a/example_test.go b/example_test.go index 3c46ffc2..5b18893d 100644 --- a/example_test.go +++ b/example_test.go @@ -125,9 +125,9 @@ func ExampleUnmarshal() { Owners []string Male bool } - cborData, _ := hex.DecodeString("a46341676504644e616d656543616e6479664f776e65727382644d617279634a6f65644d616c65f4") + data, _ := hex.DecodeString("a46341676504644e616d656543616e6479664f776e65727382644d617279634a6f65644d616c65f4") var animal Animal - err := cbor.Unmarshal(cborData, &animal) + err := cbor.Unmarshal(data, &animal) if err != nil { fmt.Println("error:", err) } @@ -334,8 +334,8 @@ func ExampleDecoder() { Owners []string Male bool } - cborData, _ := hex.DecodeString("a46341676504644d616c65f4644e616d656543616e6479664f776e65727382644d617279634a6f65a46341676506644d616c65f5644e616d656452756479664f776e657273816543696e6479a46341676502644d616c65f5644e616d656444756b65664f776e65727381664e6f72746f6e") - dec := cbor.NewDecoder(bytes.NewReader(cborData)) + data, _ := hex.DecodeString("a46341676504644d616c65f4644e616d656543616e6479664f776e65727382644d617279634a6f65a46341676506644d616c65f5644e616d656452756479664f776e657273816543696e6479a46341676502644d616c65f5644e616d656444756b65664f776e65727381664e6f72746f6e") + dec := cbor.NewDecoder(bytes.NewReader(data)) for { var animal Animal if err := dec.Decode(&animal); err != nil { @@ -364,9 +364,9 @@ func Example_cWT() { Cti []byte `cbor:"7,keyasint"` } // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.1 - cborData, _ := hex.DecodeString("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") + data, _ := hex.DecodeString("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") var v claims - if err := cbor.Unmarshal(cborData, &v); err != nil { + if err := cbor.Unmarshal(data, &v); err != nil { fmt.Println("error:", err) } if _, err := cbor.Marshal(v); err != nil { @@ -389,12 +389,12 @@ func Example_cWTWithDupMapKeyOption() { } // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.1 - cborData, _ := hex.DecodeString("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") + data, _ := hex.DecodeString("a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b71") dm, _ := cbor.DecOptions{DupMapKey: cbor.DupMapKeyEnforcedAPF}.DecMode() var v claims - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { fmt.Println("error:", err) } fmt.Printf("%+v", v) @@ -419,9 +419,9 @@ func Example_signedCWT() { Signature []byte } // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.3 - cborData, _ := hex.DecodeString("d28443a10126a104524173796d6d657472696345434453413235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a52b9b63632c57209120e1c9e30") + data, _ := hex.DecodeString("d28443a10126a104524173796d6d657472696345434453413235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a52b9b63632c57209120e1c9e30") var v signedCWT - if err := cbor.Unmarshal(cborData, &v); err != nil { + if err := cbor.Unmarshal(data, &v); err != nil { fmt.Println("error:", err) } if _, err := cbor.Marshal(v); err != nil { @@ -450,7 +450,7 @@ func Example_signedCWTWithTag() { } // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.3 - cborData, _ := hex.DecodeString("d28443a10126a104524173796d6d657472696345434453413235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a52b9b63632c57209120e1c9e30") + data, _ := hex.DecodeString("d28443a10126a104524173796d6d657472696345434453413235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a52b9b63632c57209120e1c9e30") // Register tag COSE_Sign1 18 with signedCWT type. tags := cbor.NewTagSet() @@ -465,7 +465,7 @@ func Example_signedCWTWithTag() { em, _ := cbor.EncOptions{}.EncModeWithTags(tags) var v signedCWT - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { fmt.Println("error:", err) } @@ -493,9 +493,9 @@ func Example_cOSE() { } // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.2 // 128-Bit Symmetric Key - cborData, _ := hex.DecodeString("a42050231f4c4d4d3051fdc2ec0a3851d5b3830104024c53796d6d6574726963313238030a") + data, _ := hex.DecodeString("a42050231f4c4d4d3051fdc2ec0a3851d5b3830104024c53796d6d6574726963313238030a") var v coseKey - if err := cbor.Unmarshal(cborData, &v); err != nil { + if err := cbor.Unmarshal(data, &v); err != nil { fmt.Println("error:", err) } if _, err := cbor.Marshal(v); err != nil { @@ -526,9 +526,9 @@ func Example_senML() { Sum float64 `cbor:"5,keyasint,omitempty"` } // Data from https://tools.ietf.org/html/rfc8428#section-6 - cborData, _ := hex.DecodeString("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") + data, _ := hex.DecodeString("87a721781b75726e3a6465763a6f773a3130653230373361303130383030363a22fb41d303a15b00106223614120050067766f6c7461676501615602fb405e066666666666a3006763757272656e74062402fb3ff3333333333333a3006763757272656e74062302fb3ff4cccccccccccda3006763757272656e74062202fb3ff6666666666666a3006763757272656e74062102f93e00a3006763757272656e74062002fb3ff999999999999aa3006763757272656e74060002fb3ffb333333333333") var v []*SenMLRecord - if err := cbor.Unmarshal(cborData, &v); err != nil { + if err := cbor.Unmarshal(data, &v); err != nil { fmt.Println("error:", err) } // Encoder uses ShortestFloat16 option to use float16 as the shortest form that preserves floating-point value. @@ -559,9 +559,9 @@ func Example_webAuthn() { Fmt string `cbor:"fmt"` AttStmt cbor.RawMessage `cbor:"attStmt"` } - cborData, _ := hex.DecodeString("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3") + data, _ := hex.DecodeString("a363666d74686669646f2d7532666761747453746d74a26373696758483046022100e7ab373cfbd99fcd55fd59b0f6f17fef5b77a20ddec3db7f7e4d55174e366236022100828336b4822125fb56541fb14a8a273876acd339395ec2dad95cf41c1dd2a9ae637835638159024e3082024a30820132a0030201020204124a72fe300d06092a864886f70d01010b0500302e312c302a0603550403132359756269636f2055324620526f6f742043412053657269616c203435373230303633313020170d3134303830313030303030305a180f32303530303930343030303030305a302c312a302806035504030c2159756269636f205532462045452053657269616c203234393431343937323135383059301306072a8648ce3d020106082a8648ce3d030107034200043d8b1bbd2fcbf6086e107471601468484153c1c6d3b4b68a5e855e6e40757ee22bcd8988bf3befd7cdf21cb0bf5d7a150d844afe98103c6c6607d9faae287c02a33b3039302206092b0601040182c40a020415312e332e362e312e342e312e34313438322e312e313013060b2b0601040182e51c020101040403020520300d06092a864886f70d01010b05000382010100a14f1eea0076f6b8476a10a2be72e60d0271bb465b2dfbfc7c1bd12d351989917032631d795d097fa30a26a325634e85721bc2d01a86303f6bc075e5997319e122148b0496eec8d1f4f94cf4110de626c289443d1f0f5bbb239ca13e81d1d5aa9df5af8e36126475bfc23af06283157252762ff68879bcf0ef578d55d67f951b4f32b63c8aea5b0f99c67d7d814a7ff5a6f52df83e894a3a5d9c8b82e7f8bc8daf4c80175ff8972fda79333ec465d806eacc948f1bab22045a95558a48c20226dac003d41fbc9e05ea28a6bb5e10a49de060a0a4f6a2676a34d68c4abe8c61874355b9027e828ca9e064b002d62e8d8cf0744921753d35e3c87c5d5779453e7768617574684461746158c449960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d976341000000000000000000000000000000000000000000408903fd7dfd2c9770e98cae0123b13a2c27828a106349bc6277140e7290b7e9eb7976aa3c04ed347027caf7da3a2fa76304751c02208acfc4e7fc6c7ebbc375c8a5010203262001215820ad7f7992c335b90d882b2802061b97a4fabca7e2ee3e7a51e728b8055e4eb9c7225820e0966ba7005987fece6f0e0e13447aa98cec248e4000a594b01b74c1cb1d40b3") var v attestationObject - if err := cbor.Unmarshal(cborData, &v); err != nil { + if err := cbor.Unmarshal(data, &v); err != nil { fmt.Println("error:", err) } if _, err := cbor.Marshal(v); err != nil { diff --git a/stream_test.go b/stream_test.go index a5298cfa..e6c75414 100644 --- a/stream_test.go +++ b/stream_test.go @@ -279,11 +279,11 @@ func TestDecoderNoData(t *testing.T) { } func TestDecoderRecoverableReadError(t *testing.T) { - cborData := hexDecode("83010203") // [1,2,3] + data := hexDecode("83010203") // [1,2,3] wantValue := []interface{}{uint64(1), uint64(2), uint64(3)} recoverableReaderErr := errors.New("recoverable reader error") - decoder := NewDecoder(newRecoverableReader(cborData, 1, recoverableReaderErr)) + decoder := NewDecoder(newRecoverableReader(data, 1, recoverableReaderErr)) var v interface{} err := decoder.Decode(&v) @@ -298,8 +298,8 @@ func TestDecoderRecoverableReadError(t *testing.T) { if !reflect.DeepEqual(v, wantValue) { t.Errorf("Decode() = %v (%T), want %v (%T)", v, v, wantValue, wantValue) } - if decoder.NumBytesRead() != len(cborData) { - t.Errorf("NumBytesRead() = %v, want %v", decoder.NumBytesRead(), len(cborData)) + if decoder.NumBytesRead() != len(data) { + t.Errorf("NumBytesRead() = %v, want %v", decoder.NumBytesRead(), len(data)) } // no more data @@ -529,10 +529,10 @@ func TestDecoderSkipNoData(t *testing.T) { } func TestDecoderSkipRecoverableReadError(t *testing.T) { - cborData := hexDecode("83010203") // [1,2,3] + data := hexDecode("83010203") // [1,2,3] recoverableReaderErr := errors.New("recoverable reader error") - decoder := NewDecoder(newRecoverableReader(cborData, 1, recoverableReaderErr)) + decoder := NewDecoder(newRecoverableReader(data, 1, recoverableReaderErr)) err := decoder.Skip() if err != recoverableReaderErr { @@ -543,8 +543,8 @@ func TestDecoderSkipRecoverableReadError(t *testing.T) { if err != nil { t.Fatalf("Skip() returned error %v", err) } - if decoder.NumBytesRead() != len(cborData) { - t.Errorf("NumBytesRead() = %v, want %v", decoder.NumBytesRead(), len(cborData)) + if decoder.NumBytesRead() != len(data) { + t.Errorf("NumBytesRead() = %v, want %v", decoder.NumBytesRead(), len(data)) } // no more data @@ -565,10 +565,10 @@ func TestDecoderStructTag(t *testing.T) { B: "B", C: "C", } - cborData := hexDecode("a36161614161626142617a6143") // {"a":"A", "b":"B", "z":"C"} + data := hexDecode("a36161614161626142617a6143") // {"a":"A", "b":"B", "z":"C"} var v strc - dec := NewDecoder(bytes.NewReader(cborData)) + dec := NewDecoder(bytes.NewReader(data)) if err := dec.Decode(&v); err != nil { t.Errorf("Decode() returned error %v", err) } @@ -580,30 +580,30 @@ func TestDecoderStructTag(t *testing.T) { func TestDecoderBuffered(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte buffered []byte decodeErr error }{ { name: "empty", - cborData: []byte{}, + data: []byte{}, buffered: []byte{}, decodeErr: io.EOF, }, { name: "malformed CBOR data item", - cborData: []byte{0xc0}, + data: []byte{0xc0}, buffered: []byte{0xc0}, decodeErr: io.ErrUnexpectedEOF, }, { name: "1 CBOR data item", - cborData: []byte{0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, + data: []byte{0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, buffered: []byte{}, }, { name: "2 CBOR data items", - cborData: []byte{ + data: []byte{ // First CBOR data item 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Second CBOR data item @@ -616,7 +616,7 @@ func TestDecoderBuffered(t *testing.T) { }, { name: "1 CBOR data item followed by non-CBOR data", - cborData: []byte{ + data: []byte{ // CBOR data item 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Extraneous non-CBOR data ("abc") @@ -631,7 +631,7 @@ func TestDecoderBuffered(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - r := bytes.NewReader(tc.cborData) + r := bytes.NewReader(tc.data) decoder := NewDecoder(r) @@ -673,7 +673,7 @@ func TestEncoder(t *testing.T) { encoder := em.NewEncoder(&w) for _, tc := range marshalTests { for _, value := range tc.values { - want.Write(tc.cborData) + want.Write(tc.data) if err := encoder.Encode(value); err != nil { t.Fatalf("Encode() returned error %v", err) @@ -903,25 +903,25 @@ func TestRawMessage(t *testing.T) { B *RawMessage `cbor:"b"` C *RawMessage `cbor:"c"` } - cborData := hexDecode("a361610161628202036163f6") // {"a": 1, "b": [2, 3], "c": nil}, + data := hexDecode("a361610161628202036163f6") // {"a": 1, "b": [2, 3], "c": nil}, r := RawMessage(hexDecode("820203")) want := strc{ A: RawMessage([]byte{0x01}), B: &r, } var v strc - if err := Unmarshal(cborData, &v); err != nil { - t.Fatalf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, &v); err != nil { + t.Fatalf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, want) { - t.Errorf("Unmarshal(0x%x) returned v %v, want %v", cborData, v, want) + t.Errorf("Unmarshal(0x%x) returned v %v, want %v", data, v, want) } b, err := Marshal(v) if err != nil { t.Fatalf("Marshal(%+v) returned error %v", v, err) } - if !bytes.Equal(b, cborData) { - t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, cborData) + if !bytes.Equal(b, data) { + t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, data) } address := fmt.Sprintf("%p", *v.B) @@ -931,8 +931,8 @@ func TestRawMessage(t *testing.T) { if address != fmt.Sprintf("%p", *v.B) { t.Fatalf("Unmarshal RawMessage should reuse underlying array if it has sufficient capacity") } - if err := Unmarshal(cborData, v.B); err != nil { - t.Fatalf("Unmarshal(0x%x) returned error %v", cborData, err) + if err := Unmarshal(data, v.B); err != nil { + t.Fatalf("Unmarshal(0x%x) returned error %v", data, err) } if address == fmt.Sprintf("%p", *v.B) { t.Fatalf("Unmarshal RawMessage should allocate a new underlying array if it does not have sufficient capacity") @@ -966,8 +966,8 @@ func TestEmptyRawMessage(t *testing.T) { func TestNilRawMessageUnmarshalCBORError(t *testing.T) { wantErrorMsg := "cbor.RawMessage: UnmarshalCBOR on nil pointer" var r *RawMessage - cborData := hexDecode("01") - if err := r.UnmarshalCBOR(cborData); err == nil { + data := hexDecode("01") + if err := r.UnmarshalCBOR(data); err == nil { t.Errorf("UnmarshalCBOR() didn't return error") } else if err.Error() != wantErrorMsg { t.Errorf("UnmarshalCBOR() returned error %q, want %q", err.Error(), wantErrorMsg) diff --git a/tag_test.go b/tag_test.go index 560af472..4f243314 100644 --- a/tag_test.go +++ b/tag_test.go @@ -205,17 +205,17 @@ func TestTagStruct(t *testing.T) { em, _ := EncOptions{}.EncModeWithTags(tags) dm, _ := DecOptions{}.DecModeWithTags(tags) - cborData := hexDecode("d864a0") // {} + data := hexDecode("d864a0") // {} var v T - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { t.Errorf("Unmarshal() returned error %v", err) } b, err := em.Marshal(v) if err != nil { t.Errorf("Marshal(%+v) returned error %v", v, err) } - if !bytes.Equal(b, cborData) { - t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, cborData) + if !bytes.Equal(b, data) { + t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, data) } } @@ -234,17 +234,17 @@ func TestTagFixedLengthStruct(t *testing.T) { em, _ := EncOptions{}.EncModeWithTags(tags) dm, _ := DecOptions{}.DecModeWithTags(tags) - cborData := hexDecode("d864a1617360") // {"s":""} + data := hexDecode("d864a1617360") // {"s":""} var v T - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { t.Errorf("Unmarshal() returned error %v", err) } b, err := em.Marshal(v) if err != nil { t.Errorf("Marshal(%+v) returned error %v", v, err) } - if !bytes.Equal(b, cborData) { - t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, cborData) + if !bytes.Equal(b, data) { + t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, data) } } @@ -273,17 +273,17 @@ func TestTagToArrayStruct(t *testing.T) { dm, _ := DecOptions{}.DecModeWithTags(tags) // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.3 - cborData := hexDecode("d28443a10126a104524173796d6d657472696345434453413235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a52b9b63632c57209120e1c9e30") + data := hexDecode("d28443a10126a104524173796d6d657472696345434453413235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a52b9b63632c57209120e1c9e30") var v signedCWT - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { t.Errorf("Unmarshal() returned error %v", err) } b, err := em.Marshal(v) if err != nil { t.Errorf("Marshal(%+v) returned error %v", v, err) } - if !bytes.Equal(b, cborData) { - t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, cborData) + if !bytes.Equal(b, data) { + t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, data) } } @@ -313,17 +313,17 @@ func TestNestedTagStruct(t *testing.T) { dm, _ := DecOptions{}.DecModeWithTags(tags) // Data from https://tools.ietf.org/html/rfc8392#appendix-A section A.4 - cborData := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") + data := hexDecode("d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f0061a5610d9f007420b7148093101ef6d789200") var v macedCOSE - if err := dm.Unmarshal(cborData, &v); err != nil { + if err := dm.Unmarshal(data, &v); err != nil { t.Errorf("Unmarshal() returned error %v", err) } b, err := em.Marshal(v) if err != nil { t.Errorf("Marshal(%+v) returned error %v", v, err) } - if !bytes.Equal(b, cborData) { - t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, cborData) + if !bytes.Equal(b, data) { + t.Errorf("Marshal(%+v) = 0x%x, want 0x%x", v, b, data) } } @@ -906,31 +906,31 @@ func TestDecodeWrongTag(t *testing.T) { testCases := []struct { name string obj interface{} - cborData []byte + data []byte wantErrorMsg string }{ { name: "BinaryMarshaler non-struct", obj: number(1234567890), - cborData: hexDecode("d87d4800000000499602d2"), + data: hexDecode("d87d4800000000499602d2"), wantErrorMsg: "cbor: wrong tag number for cbor.number, got [125], expected [123]", }, { name: "BinaryMarshaler struct", obj: stru{a: "a", b: "b", c: "c"}, - cborData: hexDecode("d87d45612C622C63"), + data: hexDecode("d87d45612C622C63"), wantErrorMsg: "cbor: wrong tag number for cbor.stru, got [125], expected [124]", }, { name: "non-struct", obj: myInt(1), - cborData: hexDecode("d87d01"), + data: hexDecode("d87d01"), wantErrorMsg: "cbor: wrong tag number for cbor.myInt, got [125], expected [100]", }, { name: "struct", obj: s{A: "A", B: "B", C: "C"}, - cborData: hexDecode("d87ea3616161416162614261636143"), // {"a":"A", "b":"B", "c":"C"} + data: hexDecode("d87ea3616161416162614261636143"), // {"a":"A", "b":"B", "c":"C"} wantErrorMsg: "cbor: wrong tag number for cbor.s, got [126], expected [101 102]", }, } @@ -941,13 +941,13 @@ func TestDecodeWrongTag(t *testing.T) { t.Run(name, func(t *testing.T) { dm, _ := DecOptions{}.DecModeWithTags(tag.tagSet) v := reflect.New(reflect.TypeOf(tc.obj)) - if err := dm.Unmarshal(tc.cborData, v.Interface()); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", tc.cborData) + if err := dm.Unmarshal(tc.data, v.Interface()); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", tc.data) } else { if _, ok := err.(*WrongTagError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*WrongTagError)", tc.cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong type of error %T, want (*WrongTagError)", tc.data, err) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want error %q", tc.data, err.Error(), tc.wantErrorMsg) } } }) @@ -960,7 +960,7 @@ func TestDecodeWrongTag(t *testing.T) { t.Run(name, func(t *testing.T) { dm, _ := DecOptions{}.DecModeWithTags(tagsDecIgnored) v := reflect.New(reflect.TypeOf(tc.obj)) - if err := dm.Unmarshal(tc.cborData, v.Interface()); err != nil { + if err := dm.Unmarshal(tc.data, v.Interface()); err != nil { t.Errorf("Unmarshal() returned error %v", err) } if !reflect.DeepEqual(tc.obj, v.Elem().Interface()) { @@ -1050,12 +1050,12 @@ func TestDecodeSharedTag(t *testing.T) { // Decode myInt with tag number 123 var v myInt wantV := myInt(1) - cborData := hexDecode("d87b01") - if err = dm.Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data := hexDecode("d87b01") + if err = dm.Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantV, wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantV, wantV) } // Unregister myInt type @@ -1063,12 +1063,12 @@ func TestDecodeSharedTag(t *testing.T) { // Decode myInt without tag number wantV = myInt(2) - cborData = hexDecode("02") - if err := dm.Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data = hexDecode("02") + if err := dm.Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantV, wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantV, wantV) } // Register myInt type with tag number 234 @@ -1078,12 +1078,12 @@ func TestDecodeSharedTag(t *testing.T) { // Decode myInt with tag number 234 wantV = myInt(3) - cborData = hexDecode("d8ea03") - if err := dm.Unmarshal(cborData, &v); err != nil { - t.Errorf("Unmarshal(0x%x) returned error %v", cborData, err) + data = hexDecode("d8ea03") + if err := dm.Unmarshal(data, &v); err != nil { + t.Errorf("Unmarshal(0x%x) returned error %v", data, err) } if !reflect.DeepEqual(v, wantV) { - t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", cborData, v, v, wantV, wantV) + t.Errorf("Unmarshal(0x%x) = %v (%T), want %v (%T)", data, v, v, wantV, wantV) } } @@ -1162,8 +1162,8 @@ func TestEncModeWithTagsError(t *testing.T) { func TestNilRawTagUnmarshalCBORError(t *testing.T) { wantErrorMsg := "cbor.RawTag: UnmarshalCBOR on nil pointer" var tag *RawTag - cborData := hexDecode("c249010000000000000000") - if err := tag.UnmarshalCBOR(cborData); err == nil { + data := hexDecode("c249010000000000000000") + if err := tag.UnmarshalCBOR(data); err == nil { t.Errorf("UnmarshalCBOR() didn't return error") } else if err.Error() != wantErrorMsg { t.Errorf("UnmarshalCBOR() returned error %q, want %q", err.Error(), wantErrorMsg) @@ -1171,12 +1171,12 @@ func TestNilRawTagUnmarshalCBORError(t *testing.T) { } func TestTagUnmarshalError(t *testing.T) { - cborData := hexDecode("d87b61fe") // invalid UTF-8 string + data := hexDecode("d87b61fe") // invalid UTF-8 string var tag Tag - if err := Unmarshal(cborData, &tag); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return error", cborData) + if err := Unmarshal(data, &tag); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return error", data) } else if err.Error() != invalidUTF8ErrorMsg { - t.Errorf("Unmarshal(0x%x) returned error %q, want %q", cborData, err.Error(), invalidUTF8ErrorMsg) + t.Errorf("Unmarshal(0x%x) returned error %q, want %q", data, err.Error(), invalidUTF8ErrorMsg) } } @@ -1293,41 +1293,41 @@ func TestDecodeTagToEmptyIface(t *testing.T) { dmSharedTags, _ := DecOptions{}.DecModeWithSharedTags(tags) testCases := []struct { - name string - cborData []byte - wantObj interface{} + name string + data []byte + wantObj interface{} }{ { - name: "registered myBool", - cborData: hexDecode("d864f5"), // 100(true) - wantObj: myBool(true), + name: "registered myBool", + data: hexDecode("d864f5"), // 100(true) + wantObj: myBool(true), }, { - name: "registered myUint", - cborData: hexDecode("d865d86600"), // 101(102(0)) - wantObj: myUint(0), + name: "registered myUint", + data: hexDecode("d865d86600"), // 101(102(0)) + wantObj: myUint(0), }, { - name: "not registered bool", - cborData: hexDecode("d865f5"), // 101(true) - wantObj: Tag{101, true}, + name: "not registered bool", + data: hexDecode("d865f5"), // 101(true) + wantObj: Tag{101, true}, }, { - name: "not registered uint", - cborData: hexDecode("d865d86700"), // 101(103(0)) - wantObj: Tag{101, Tag{103, uint64(0)}}, + name: "not registered uint", + data: hexDecode("d865d86700"), // 101(103(0)) + wantObj: Tag{101, Tag{103, uint64(0)}}, }, { - name: "not registered uint", - cborData: hexDecode("d865d866d86700"), // 101(102(103(0))) - wantObj: Tag{101, Tag{102, Tag{103, uint64(0)}}}, + name: "not registered uint", + data: hexDecode("d865d866d86700"), // 101(102(103(0))) + wantObj: Tag{101, Tag{102, Tag{103, uint64(0)}}}, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var v1 interface{} - if err := dm.Unmarshal(tc.cborData, &v1); err != nil { + if err := dm.Unmarshal(tc.data, &v1); err != nil { t.Errorf("Unmarshal() returned error %v", err) } if !reflect.DeepEqual(tc.wantObj, v1) { @@ -1335,7 +1335,7 @@ func TestDecodeTagToEmptyIface(t *testing.T) { } var v2 interface{} - if err := dmSharedTags.Unmarshal(tc.cborData, &v2); err != nil { + if err := dmSharedTags.Unmarshal(tc.data, &v2); err != nil { t.Errorf("Unmarshal() returned error %v", err) } if !reflect.DeepEqual(tc.wantObj, v2) { @@ -1357,15 +1357,15 @@ func TestDecodeRegisteredTagToEmptyIfaceError(t *testing.T) { dm, _ := DecOptions{}.DecModeWithTags(tags) - cborData := hexDecode("d865d8663bffffffffffffffff") // 101(102(-18446744073709551616)) + data := hexDecode("d865d8663bffffffffffffffff") // 101(102(-18446744073709551616)) var v interface{} - if err := dm.Unmarshal(cborData, &v); err == nil { - t.Errorf("Unmarshal(0x%x) didn't return an error", cborData) + if err := dm.Unmarshal(data, &v); err == nil { + t.Errorf("Unmarshal(0x%x) didn't return an error", data) } else if _, ok := err.(*UnmarshalTypeError); !ok { - t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", cborData, err) + t.Errorf("Unmarshal(0x%x) returned wrong error type %T, want (*UnmarshalTypeError)", data, err) } else if !strings.Contains(err.Error(), "cannot unmarshal") { - t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", cborData, err.Error(), "cannot unmarshal") + t.Errorf("Unmarshal(0x%x) returned error %q, want error containing %q", data, err.Error(), "cannot unmarshal") } } @@ -1408,7 +1408,7 @@ func TestDecodeRegisterTagForUnmarshaler(t *testing.T) { t.Fatalf("TagSet.Add(%s, %d) returned error %v", typ, 100, err) } - cborData := hexDecode("d864a1636e756d01") // 100({"num": 1}) + data := hexDecode("d864a1636e756d01") // 100({"num": 1}) wantObj := number3(1) dm, _ := DecOptions{}.DecModeWithTags(tags) @@ -1416,7 +1416,7 @@ func TestDecodeRegisterTagForUnmarshaler(t *testing.T) { // Decode to empty interface. Unmarshal() should return object of registered type. var v1 interface{} - if err := dm.Unmarshal(cborData, &v1); err != nil { + if err := dm.Unmarshal(data, &v1); err != nil { t.Errorf("Unmarshal() returned error %v", err) } if !reflect.DeepEqual(wantObj, v1) { @@ -1425,13 +1425,13 @@ func TestDecodeRegisterTagForUnmarshaler(t *testing.T) { b, err := em.Marshal(v1) if err != nil { t.Errorf("Marshal(%v) returned error %v", v1, err) - } else if !bytes.Equal(b, cborData) { - t.Errorf("Marshal(%v) returned %v, want %v", v1, b, cborData) + } else if !bytes.Equal(b, data) { + t.Errorf("Marshal(%v) returned %v, want %v", v1, b, data) } // Decode to registered type. var v2 number3 - if err = dm.Unmarshal(cborData, &v2); err != nil { + if err = dm.Unmarshal(data, &v2); err != nil { t.Errorf("Unmarshal() returned error %v", err) } if !reflect.DeepEqual(wantObj, v2) { @@ -1440,7 +1440,7 @@ func TestDecodeRegisterTagForUnmarshaler(t *testing.T) { b, err = em.Marshal(v2) if err != nil { t.Errorf("Marshal(%v) returned error %v", v2, err) - } else if !bytes.Equal(b, cborData) { - t.Errorf("Marshal(%v) returned %v, want %v", v2, b, cborData) + } else if !bytes.Equal(b, data) { + t.Errorf("Marshal(%v) returned %v, want %v", v2, b, data) } } diff --git a/valid_test.go b/valid_test.go index 39e9e4d6..96ff2105 100644 --- a/valid_test.go +++ b/valid_test.go @@ -10,7 +10,7 @@ import ( func TestValid1(t *testing.T) { for _, mt := range marshalTests { - if err := Wellformed(mt.cborData); err != nil { + if err := Wellformed(mt.data); err != nil { t.Errorf("Wellformed() returned error %v", err) } } @@ -19,7 +19,7 @@ func TestValid1(t *testing.T) { func TestValid2(t *testing.T) { for _, mt := range marshalTests { dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode() - if err := dm.Wellformed(mt.cborData); err != nil { + if err := dm.Wellformed(mt.data); err != nil { t.Errorf("Wellformed() returned error %v", err) } } @@ -28,7 +28,7 @@ func TestValid2(t *testing.T) { func TestValidExtraneousData(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte extraneousDataNumOfBytes int extraneousDataIndex int }{ @@ -39,17 +39,17 @@ func TestValidExtraneousData(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - err := Wellformed(tc.cborData) + err := Wellformed(tc.data) if err == nil { - t.Errorf("Wellformed(0x%x) didn't return an error", tc.cborData) + t.Errorf("Wellformed(0x%x) didn't return an error", tc.data) } else { ederr, ok := err.(*ExtraneousDataError) if !ok { - t.Errorf("Wellformed(0x%x) error type %T, want *ExtraneousDataError", tc.cborData, err) + t.Errorf("Wellformed(0x%x) error type %T, want *ExtraneousDataError", tc.data, err) } else if ederr.numOfBytes != tc.extraneousDataNumOfBytes { - t.Errorf("Wellformed(0x%x) returned %d bytes of extraneous data, want %d", tc.cborData, ederr.numOfBytes, tc.extraneousDataNumOfBytes) + t.Errorf("Wellformed(0x%x) returned %d bytes of extraneous data, want %d", tc.data, ederr.numOfBytes, tc.extraneousDataNumOfBytes) } else if ederr.index != tc.extraneousDataIndex { - t.Errorf("Wellformed(0x%x) returned extraneous data index %d, want %d", tc.cborData, ederr.index, tc.extraneousDataIndex) + t.Errorf("Wellformed(0x%x) returned extraneous data index %d, want %d", tc.data, ederr.index, tc.extraneousDataIndex) } } }) @@ -59,7 +59,7 @@ func TestValidExtraneousData(t *testing.T) { func TestValidOnStreamingData(t *testing.T) { var buf bytes.Buffer for _, t := range marshalTests { - buf.Write(t.cborData) + buf.Write(t.data) } d := decoder{data: buf.Bytes(), dm: defaultDecMode} for i := 0; i < len(marshalTests); i++ { @@ -72,7 +72,7 @@ func TestValidOnStreamingData(t *testing.T) { func TestDepth(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte wantDepth int }{ {"uint", hexDecode("00"), 0}, // 0 @@ -110,13 +110,13 @@ func TestDepth(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - d := decoder{data: tc.cborData, dm: defaultDecMode} + d := decoder{data: tc.data, dm: defaultDecMode} depth, err := d.wellformedInternal(0) if err != nil { - t.Errorf("wellformed(0x%x) returned error %v", tc.cborData, err) + t.Errorf("wellformed(0x%x) returned error %v", tc.data, err) } if depth != tc.wantDepth { - t.Errorf("wellformed(0x%x) returned depth %d, want %d", tc.cborData, depth, tc.wantDepth) + t.Errorf("wellformed(0x%x) returned depth %d, want %d", tc.data, depth, tc.wantDepth) } }) } @@ -125,49 +125,49 @@ func TestDepth(t *testing.T) { func TestDepthError(t *testing.T) { testCases := []struct { name string - cborData []byte + data []byte opts DecOptions wantErrorMsg string }{ { name: "33-level array", - cborData: hexDecode("82018181818181818181818181818181818181818181818181818181818181818101"), + data: hexDecode("82018181818181818181818181818181818181818181818181818181818181818101"), opts: DecOptions{MaxNestedLevels: 4}, wantErrorMsg: "cbor: exceeded max nested level 4", }, { name: "33-level array", - cborData: hexDecode("82018181818181818181818181818181818181818181818181818181818181818101"), + data: hexDecode("82018181818181818181818181818181818181818181818181818181818181818101"), opts: DecOptions{MaxNestedLevels: 10}, wantErrorMsg: "cbor: exceeded max nested level 10", }, { name: "33-level array", - cborData: hexDecode("8201818181818181818181818181818181818181818181818181818181818181818101"), + data: hexDecode("8201818181818181818181818181818181818181818181818181818181818181818101"), opts: DecOptions{}, wantErrorMsg: "cbor: exceeded max nested level 32", }, { name: "33-level indefinite length array", - cborData: hexDecode("9f01818181818181818181818181818181818181818181818181818181818181818101ff"), + data: hexDecode("9f01818181818181818181818181818181818181818181818181818181818181818101ff"), opts: DecOptions{}, wantErrorMsg: "cbor: exceeded max nested level 32", }, { name: "33-level map", - cborData: hexDecode("a101818181818181818181818181818181818181818181818181818181818181818101"), + data: hexDecode("a101818181818181818181818181818181818181818181818181818181818181818101"), opts: DecOptions{}, wantErrorMsg: "cbor: exceeded max nested level 32", }, { name: "33-level indefinite length map", - cborData: hexDecode("bf01818181818181818181818181818181818181818181818181818181818181818101ff"), + data: hexDecode("bf01818181818181818181818181818181818181818181818181818181818181818101ff"), opts: DecOptions{}, wantErrorMsg: "cbor: exceeded max nested level 32", }, { name: "33-level tag", - cborData: hexDecode("d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d86474323031332d30332d32315432303a30343a30305a"), + data: hexDecode("d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d864d86474323031332d30332d32315432303a30343a30305a"), opts: DecOptions{}, wantErrorMsg: "cbor: exceeded max nested level 32", }, @@ -175,13 +175,13 @@ func TestDepthError(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { dm, _ := tc.opts.decMode() - d := decoder{data: tc.cborData, dm: dm} + d := decoder{data: tc.data, dm: dm} if _, err := d.wellformedInternal(0); err == nil { - t.Errorf("wellformed(0x%x) didn't return an error", tc.cborData) + t.Errorf("wellformed(0x%x) didn't return an error", tc.data) } else if _, ok := err.(*MaxNestedLevelError); !ok { - t.Errorf("wellformed(0x%x) returned wrong error type %T, want (*MaxNestedLevelError)", tc.cborData, err) + t.Errorf("wellformed(0x%x) returned wrong error type %T, want (*MaxNestedLevelError)", tc.data, err) } else if err.Error() != tc.wantErrorMsg { - t.Errorf("wellformed(0x%x) returned error %q, want error %q", tc.cborData, err.Error(), tc.wantErrorMsg) + t.Errorf("wellformed(0x%x) returned error %q, want error %q", tc.data, err.Error(), tc.wantErrorMsg) } }) }