github.com/MetalBlockchain/metalgo@v1.11.9/utils/formatting/encoding_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package formatting 5 6 import ( 7 "encoding/hex" 8 "encoding/json" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestEncodingMarshalJSON(t *testing.T) { 15 require := require.New(t) 16 17 enc := Hex 18 jsonBytes, err := enc.MarshalJSON() 19 require.NoError(err) 20 require.Equal(`"hex"`, string(jsonBytes)) 21 } 22 23 func TestEncodingUnmarshalJSON(t *testing.T) { 24 require := require.New(t) 25 26 jsonBytes := []byte(`"hex"`) 27 var enc Encoding 28 require.NoError(json.Unmarshal(jsonBytes, &enc)) 29 require.Equal(Hex, enc) 30 31 var serr *json.SyntaxError 32 jsonBytes = []byte("") 33 require.ErrorAs(json.Unmarshal(jsonBytes, &enc), &serr) 34 35 jsonBytes = []byte(`""`) 36 err := json.Unmarshal(jsonBytes, &enc) 37 require.ErrorIs(err, errInvalidEncoding) 38 } 39 40 func TestEncodingString(t *testing.T) { 41 enc := Hex 42 require.Equal(t, "hex", enc.String()) 43 } 44 45 // Test encoding bytes to a string and decoding back to bytes 46 func TestEncodeDecode(t *testing.T) { 47 require := require.New(t) 48 49 type test struct { 50 encoding Encoding 51 bytes []byte 52 str string 53 } 54 55 id := [32]byte{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} 56 tests := []test{ 57 { 58 Hex, 59 []byte{}, 60 "0x7852b855", 61 }, 62 { 63 Hex, 64 []byte{0}, 65 "0x0017afa01d", 66 }, 67 { 68 Hex, 69 []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255}, 70 "0x00010203040506070809ff4482539c", 71 }, 72 { 73 Hex, 74 id[:], 75 "0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20b7a612c9", 76 }, 77 } 78 79 for _, test := range tests { 80 // Encode the bytes 81 strResult, err := Encode(test.encoding, test.bytes) 82 require.NoError(err) 83 // Make sure the string repr. is what we expected 84 require.Equal(test.str, strResult) 85 // Decode the string 86 bytesResult, err := Decode(test.encoding, strResult) 87 require.NoError(err) 88 // Make sure we got the same bytes back 89 require.Equal(test.bytes, bytesResult) 90 } 91 } 92 93 // Test that encoding nil bytes works 94 func TestEncodeNil(t *testing.T) { 95 require := require.New(t) 96 97 str, err := Encode(Hex, nil) 98 require.NoError(err) 99 require.Equal("0x7852b855", str) 100 } 101 102 func TestDecodeHexInvalid(t *testing.T) { 103 tests := []struct { 104 inputStr string 105 expectedErr error 106 }{ 107 { 108 inputStr: "0", 109 expectedErr: errMissingHexPrefix, 110 }, 111 { 112 inputStr: "x", 113 expectedErr: errMissingHexPrefix, 114 }, 115 { 116 inputStr: "0xg", 117 expectedErr: hex.InvalidByteError('g'), 118 }, 119 { 120 inputStr: "0x0017afa0Zd", 121 expectedErr: hex.InvalidByteError('Z'), 122 }, 123 { 124 inputStr: "0xafafafafaf", 125 expectedErr: errBadChecksum, 126 }, 127 } 128 for _, test := range tests { 129 _, err := Decode(Hex, test.inputStr) 130 require.ErrorIs(t, err, test.expectedErr) 131 } 132 } 133 134 func TestDecodeNil(t *testing.T) { 135 require := require.New(t) 136 result, err := Decode(Hex, "") 137 require.NoError(err) 138 require.Empty(result) 139 } 140 141 func FuzzEncodeDecode(f *testing.F) { 142 f.Fuzz(func(t *testing.T, bytes []byte) { 143 require := require.New(t) 144 145 str, err := Encode(Hex, bytes) 146 require.NoError(err) 147 148 decoded, err := Decode(Hex, str) 149 require.NoError(err) 150 151 require.Equal(bytes, decoded) 152 }) 153 }