diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go index b5c9b05186b..aa411532de3 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go @@ -24,12 +24,14 @@ import ( "time" "github.com/fxamacker/cbor/v2" + "github.com/google/go-cmp/cmp" ) func nilPointerFor[T interface{}]() *T { return nil } +// TestRoundtrip roundtrips object serialization to interface{} and back via CBOR. func TestRoundtrip(t *testing.T) { type modePair struct { enc cbor.EncMode @@ -101,10 +103,6 @@ func TestRoundtrip(t *testing.T) { name: "int64 zero", obj: int64(math.MinInt64), }, - { - name: "uint64 max", - obj: uint64(math.MaxUint64), - }, { name: "uint64 zero", obj: uint64(0), @@ -285,18 +283,54 @@ func TestRoundtrip(t *testing.T) { t.Run(fmt.Sprintf("enc=%s/dec=%s/%s", encModeName, decModeName, tc.name), func(t *testing.T) { original := tc.obj - b, err := modePair.enc.Marshal(original) + cborFromOriginal, err := modePair.enc.Marshal(original) if err != nil { - t.Fatalf("unexpected error from Marshal: %v", err) + t.Fatalf("unexpected error from Marshal of original: %v", err) } - final := reflect.New(reflect.TypeOf(original)) - err = modePair.dec.Unmarshal(b, final.Interface()) - if err != nil { - t.Fatalf("unexpected error from Unmarshal: %v", err) + var iface interface{} + if err := modePair.dec.Unmarshal(cborFromOriginal, &iface); err != nil { + t.Fatalf("unexpected error from Unmarshal into %T: %v", &iface, err) } - if !reflect.DeepEqual(original, final.Elem().Interface()) { - t.Errorf("roundtrip difference:\nwant: %#v\ngot: %#v", original, final) + + cborFromIface, err := modePair.enc.Marshal(iface) + if err != nil { + t.Fatalf("unexpected error from Marshal of iface: %v", err) + } + + { + // interface{} to interface{} + var iface2 interface{} + if err := modePair.dec.Unmarshal(cborFromIface, &iface2); err != nil { + t.Fatalf("unexpected error from Unmarshal into %T: %v", &iface2, err) + } + if diff := cmp.Diff(iface, iface2); diff != "" { + t.Errorf("unexpected difference on roundtrip from interface{} to interface{}:\n%s", diff) + } + } + + { + // original to original + final := reflect.New(reflect.TypeOf(original)) + err = modePair.dec.Unmarshal(cborFromOriginal, final.Interface()) + if err != nil { + t.Fatalf("unexpected error from Unmarshal into %T: %v", final.Interface(), err) + } + if diff := cmp.Diff(original, final.Elem().Interface()); diff != "" { + t.Errorf("unexpected difference on roundtrip from original to original:\n%s", diff) + } + } + + { + // original to interface{} to original + finalViaIface := reflect.New(reflect.TypeOf(original)) + err = modePair.dec.Unmarshal(cborFromIface, finalViaIface.Interface()) + if err != nil { + t.Fatalf("unexpected error from Unmarshal into %T: %v", finalViaIface.Interface(), err) + } + if diff := cmp.Diff(original, finalViaIface.Elem().Interface()); diff != "" { + t.Errorf("unexpected difference on roundtrip from original to interface{} to original:\n%s", diff) + } } }) }