Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests to improve consistency and readability #458

Merged
merged 5 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions bench_test.go

Large diffs are not rendered by default.

4,340 changes: 2,901 additions & 1,439 deletions decode_test.go

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -1082,15 +1082,15 @@ func TestDiagnoseEmptyData(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s, err := tc.dm.Diagnose(emptyData)
if len(s) != 0 {
if s != "" {
t.Errorf("Diagnose() didn't return empty notation for empty data")
}
if err != io.EOF {
t.Errorf("Diagnose() didn't return io.EOF for empty data")
}

s, rest, err := tc.dm.DiagnoseFirst(emptyData)
if len(s) != 0 {
if s != "" {
t.Errorf("DiagnoseFirst() didn't return empty notation for empty data")
}
if len(rest) != 0 {
Expand Down
450 changes: 236 additions & 214 deletions encode_test.go

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Loading
Loading