-
Notifications
You must be signed in to change notification settings - Fork 3
/
roundtripper.go
39 lines (29 loc) · 1.02 KB
/
roundtripper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2020 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package swid
import (
"reflect"
"testing"
cbor "github.com/fxamacker/cbor/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// marshal + unmarshal
func roundTripper(t *testing.T, tv interface{}, expectedCBOR []byte) interface{} {
encMode, err := cbor.EncOptions{TimeTag: cbor.EncTagRequired}.EncMode()
require.Nil(t, err)
data, err := encMode.Marshal(tv)
assert.Nil(t, err)
t.Logf("CBOR(hex): %x\n", data)
assert.Equal(t, expectedCBOR, data)
decMode, err := cbor.DecOptions{TimeTag: cbor.DecTagOptional}.DecMode()
require.Nil(t, err)
actual := reflect.New(reflect.TypeOf(tv))
err = decMode.Unmarshal(data, actual.Interface())
assert.Nil(t, err)
assert.Equal(t, tv, actual.Elem().Interface())
// Return the an interface wrapping the roundtripped test vector.
// In case it's needed for further processing it can be extracted
// with a type assertion.
return actual.Elem().Interface()
}