sdk: Add unit-testing and fuzz-testing for leftpadbytes (#2997)

This commit is contained in:
Jonathan Claudius 2023-05-30 16:48:17 -04:00 committed by GitHub
parent 703fbe32c7
commit c5523d39fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package vaa
import (
"bytes"
"encoding/hex"
"testing"
@ -162,3 +163,40 @@ func TestBodyIbcReceiverUpdateChannelChain(t *testing.T) {
}
assert.Equal(t, expected, hex.EncodeToString(bodyIbcReceiverUpdateChannelChain.Serialize()))
}
func TestLeftPadBytes(t *testing.T) {
payload := "AAAA"
paddedPayload := LeftPadBytes(payload, int(8))
buf := &bytes.Buffer{}
buf.WriteByte(0x00)
buf.WriteByte(0x00)
buf.WriteByte(0x00)
buf.WriteByte(0x00)
buf.Write([]byte(payload))
assert.Equal(t, paddedPayload, buf)
}
func FuzzLeftPadBytes(f *testing.F) {
// Add examples to our fuzz corpus
f.Add("FOO", 8)
f.Add("123", 8)
f.Fuzz(func(t *testing.T, payload string, length int) {
// We know length could be negative, but we panic if it is in the implementation
if length < 0 {
t.Skip()
}
// We know we cannot left pad something shorter than the payload being provided, but we panic if it is
if len(payload) > length {
t.Skip()
}
paddedPayload := LeftPadBytes(payload, length)
// paddedPayload must always be equal to length
assert.Equal(t, paddedPayload.Len(), length)
})
}