| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "encoding/hex" |
| 5 | "testing" |
| 6 | |
| 7 | cbor "github.com/fxamacker/cbor/v2" |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func Test_DefaultCBOREncoder(t *testing.T) { |
| 12 | t.Parallel() |
| 13 | |
| 14 | var ( |
| 15 | ss = &sampleStructure{ |
| 16 | ImportantString: "Hello World", |
| 17 | } |
| 18 | importantString = `a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64` |
| 19 | cborEncoder CBORMarshal = cbor.Marshal |
| 20 | ) |
| 21 | |
| 22 | raw, err := cborEncoder(ss) |
| 23 | require.NoError(t, err) |
| 24 | |
| 25 | require.Equal(t, hex.EncodeToString(raw), importantString) |
| 26 | } |
| 27 | |
| 28 | func Test_DefaultCBORDecoder(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | |
| 31 | var ( |
| 32 | ss sampleStructure |
| 33 | importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64") |
| 34 | cborDecoder CBORUnmarshal = cbor.Unmarshal |
| 35 | ) |
| 36 | if err != nil { |
| 37 | t.Error("Failed to decode hex string") |
| 38 | } |
| 39 | |
| 40 | err = cborDecoder(importantString, &ss) |
| 41 | require.NoError(t, err) |
| 42 | require.Equal(t, "Hello World", ss.ImportantString) |
| 43 | } |
| 44 | |
| 45 | func Test_DefaultCBOREncoderWithEmptyString(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | |
| 48 | var ( |
| 49 | ss = &sampleStructure{ |
| 50 | ImportantString: "", |
| 51 | } |
| 52 | importantString = `a170696d706f7274616e745f737472696e6760` |
| 53 | cborEncoder CBORMarshal = cbor.Marshal |
| 54 | ) |
| 55 | |
| 56 | raw, err := cborEncoder(ss) |
| 57 | require.NoError(t, err) |
| 58 | |
| 59 | require.Equal(t, hex.EncodeToString(raw), importantString) |
| 60 | } |
| 61 | |
| 62 | func Test_DefaultCBORDecoderWithEmptyString(t *testing.T) { |
| 63 | t.Parallel() |
| 64 | |
| 65 | var ( |
| 66 | ss sampleStructure |
| 67 | importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e6760") |
| 68 | cborDecoder CBORUnmarshal = cbor.Unmarshal |
| 69 | ) |
| 70 | if err != nil { |
| 71 | t.Error("Failed to decode hex string") |
| 72 | } |
| 73 | |
| 74 | err = cborDecoder(importantString, &ss) |
| 75 | require.NoError(t, err) |
| 76 | require.Empty(t, ss.ImportantString) |
| 77 | } |
| 78 | |
| 79 | func Test_DefaultCBOREncoderWithUnitializedStruct(t *testing.T) { |
| 80 | t.Parallel() |
| 81 | |
| 82 | var ( |
| 83 | ss sampleStructure |
| 84 | importantString = `a170696d706f7274616e745f737472696e6760` |
| 85 | cborEncoder CBORMarshal = cbor.Marshal |
| 86 | ) |
| 87 | |
| 88 | raw, err := cborEncoder(ss) |
| 89 | require.NoError(t, err) |
| 90 | |
| 91 | require.Equal(t, hex.EncodeToString(raw), importantString) |
| 92 | } |
| 93 | |
| 94 | func Test_DefaultCBORDecoderWithUnitializedStruct(t *testing.T) { |
| 95 | t.Parallel() |
| 96 | |
| 97 | var ( |
| 98 | ss sampleStructure |
| 99 | emptySs sampleStructure |
| 100 | importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e6760") |
| 101 | cborDecoder CBORUnmarshal = cbor.Unmarshal |
| 102 | ) |
| 103 | if err != nil { |
| 104 | t.Error("Failed to decode hex string") |
| 105 | } |
| 106 | |
| 107 | err = cborDecoder(importantString, &ss) |
| 108 | require.NoError(t, err) |
| 109 | require.Equal(t, emptySs, ss) |
| 110 | } |
| 111 | |
| 112 | func Test_CBORDecodeInvalid(t *testing.T) { |
| 113 | t.Parallel() |
| 114 | |
| 115 | data := []byte{0xff, 0xff} |
| 116 | var ss sampleStructure |
| 117 | err := cbor.Unmarshal(data, &ss) |
| 118 | require.Error(t, err) |
| 119 | } |