github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/amino_test.go (about) 1 package amino_test 2 3 import ( 4 "bytes" 5 "testing" 6 "time" 7 8 amino "github.com/gnolang/gno/tm2/pkg/amino" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestMarshal(t *testing.T) { 13 t.Parallel() 14 15 cdc := amino.NewCodec() 16 17 type SimpleStruct struct { 18 String string 19 Bytes []byte 20 Time time.Time 21 } 22 23 s := SimpleStruct{ 24 String: "hello", 25 Bytes: []byte("goodbye"), 26 Time: time.Now().UTC().Truncate(time.Millisecond), // strip monotonic and timezone. 27 } 28 29 b, err := cdc.MarshalSized(s) 30 assert.Nil(t, err) 31 t.Logf("MarshalSized(s) -> %X", b) 32 33 var s2 SimpleStruct 34 err = cdc.UnmarshalSized(b, &s2) 35 assert.Nil(t, err) 36 assert.Equal(t, s, s2) 37 } 38 39 func TestUnmarshalReader(t *testing.T) { 40 t.Parallel() 41 42 cdc := amino.NewCodec() 43 44 type SimpleStruct struct { 45 String string 46 Bytes []byte 47 Time time.Time 48 } 49 50 s := SimpleStruct{ 51 String: "hello", 52 Bytes: []byte("goodbye"), 53 Time: time.Now().UTC().Truncate(time.Millisecond), // strip monotonic and timezone. 54 } 55 56 b, err := cdc.MarshalSized(s) 57 assert.Nil(t, err) 58 t.Logf("MarshalSized(s) -> %X", b) 59 60 var s2 SimpleStruct 61 _, err = cdc.UnmarshalSizedReader(bytes.NewBuffer(b), &s2, 0) 62 assert.Nil(t, err) 63 64 assert.Equal(t, s, s2) 65 } 66 67 type stringWrapper struct { 68 S string 69 } 70 71 func TestUnmarshalReaderSize(t *testing.T) { 72 t.Parallel() 73 74 cdc := amino.NewCodec() 75 76 s1 := stringWrapper{"foo"} 77 b, err := cdc.MarshalSized(s1) 78 assert.Nil(t, err) 79 t.Logf("MarshalSized(s) -> %X", b) 80 81 var s2 stringWrapper 82 var n int64 83 n, err = cdc.UnmarshalSizedReader(bytes.NewBuffer(b), &s2, 0) 84 assert.Nil(t, err) 85 assert.Equal(t, s1, s2) 86 frameLengthBytes, msgLengthBytes, embedOverhead := 1, 1, 1 87 assert.Equal(t, frameLengthBytes+msgLengthBytes+embedOverhead+len(s1.S), int(n)) 88 } 89 90 func TestUnmarshalReaderSizeLimit(t *testing.T) { 91 t.Parallel() 92 93 cdc := amino.NewCodec() 94 95 s1 := stringWrapper{"foo"} 96 b, err := cdc.MarshalSized(s1) 97 assert.Nil(t, err) 98 t.Logf("MarshalSized(s) -> %X", b) 99 100 var s2 stringWrapper 101 var n int64 102 _, err = cdc.UnmarshalSizedReader(bytes.NewBuffer(b), &s2, int64(len(b)-1)) 103 assert.NotNil(t, err, "insufficient limit should lead to failure") 104 n, err = cdc.UnmarshalSizedReader(bytes.NewBuffer(b), &s2, int64(len(b))) 105 assert.Nil(t, err, "sufficient limit should not cause failure") 106 assert.Equal(t, s1, s2) 107 frameLengthBytes, msgLengthBytes, embedOverhead := 1, 1, 1 108 assert.Equal(t, frameLengthBytes+msgLengthBytes+embedOverhead+len(s1.S), int(n)) 109 } 110 111 func TestUnmarshalReaderTooLong(t *testing.T) { 112 t.Parallel() 113 114 cdc := amino.NewCodec() 115 116 type SimpleStruct struct { 117 String string 118 Bytes []byte 119 Time time.Time 120 } 121 122 s := SimpleStruct{ 123 String: "hello", 124 Bytes: []byte("goodbye"), 125 Time: time.Now().UTC().Truncate(time.Millisecond), // strip monotonic and timezone. 126 } 127 128 b, err := cdc.MarshalSized(s) 129 assert.Nil(t, err) 130 t.Logf("MarshalSized(s) -> %X", b) 131 132 var s2 SimpleStruct 133 _, err = cdc.UnmarshalSizedReader(bytes.NewBuffer(b), &s2, 1) // 1 byte limit is ridiculous. 134 assert.NotNil(t, err) 135 } 136 137 func TestUnmarshalBufferedWritesReads(t *testing.T) { 138 t.Parallel() 139 140 cdc := amino.NewCodec() 141 buf := bytes.NewBuffer(nil) 142 143 // Write 3 times. 144 s1 := stringWrapper{"foo"} 145 _, err := cdc.MarshalSizedWriter(buf, s1) 146 assert.Nil(t, err) 147 _, err = cdc.MarshalSizedWriter(buf, s1) 148 assert.Nil(t, err) 149 _, err = cdc.MarshalSizedWriter(buf, s1) 150 assert.Nil(t, err) 151 152 // Read 3 times. 153 s2 := stringWrapper{} 154 _, err = cdc.UnmarshalSizedReader(buf, &s2, 0) 155 assert.Nil(t, err) 156 assert.Equal(t, s1, s2) 157 _, err = cdc.UnmarshalSizedReader(buf, &s2, 0) 158 assert.Nil(t, err) 159 assert.Equal(t, s1, s2) 160 _, err = cdc.UnmarshalSizedReader(buf, &s2, 0) 161 assert.Nil(t, err) 162 assert.Equal(t, s1, s2) 163 164 // Reading 4th time fails. 165 _, err = cdc.UnmarshalSizedReader(buf, &s2, 0) 166 assert.NotNil(t, err) 167 } 168 169 func TestBoolPointers(t *testing.T) { 170 t.Parallel() 171 172 cdc := amino.NewCodec() 173 type SimpleStruct struct { 174 BoolPtrTrue *bool 175 BoolPtrFalse *bool 176 } 177 178 ttrue := true 179 ffalse := false 180 181 s := SimpleStruct{ 182 BoolPtrTrue: &ttrue, 183 BoolPtrFalse: &ffalse, 184 } 185 186 b, err := cdc.Marshal(s) 187 assert.NoError(t, err) 188 189 var s2 SimpleStruct 190 err = cdc.Unmarshal(b, &s2) 191 192 assert.NoError(t, err) 193 assert.NotNil(t, s2.BoolPtrTrue) 194 assert.NotNil(t, s2.BoolPtrFalse) 195 }