Skip to content

Commit d63aa56

Browse files
Add more X-chain tests (ava-labs#1487)
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
1 parent bdfa043 commit d63aa56

File tree

5 files changed

+747
-17
lines changed

5 files changed

+747
-17
lines changed

codec/manager.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ const (
2323
)
2424

2525
var (
26-
ErrUnknownVersion = errors.New("unknown codec version")
27-
28-
errMarshalNil = errors.New("can't marshal nil pointer or interface")
29-
errUnmarshalNil = errors.New("can't unmarshal nil")
30-
errUnmarshalTooBig = errors.New("byte array exceeds maximum length")
31-
errCantPackVersion = errors.New("couldn't pack codec version")
32-
errCantUnpackVersion = errors.New("couldn't unpack codec version")
33-
errDuplicatedVersion = errors.New("duplicated codec version")
26+
ErrUnknownVersion = errors.New("unknown codec version")
27+
ErrMarshalNil = errors.New("can't marshal nil pointer or interface")
28+
ErrUnmarshalNil = errors.New("can't unmarshal nil")
29+
ErrUnmarshalTooBig = errors.New("byte array exceeds maximum length")
30+
ErrCantPackVersion = errors.New("couldn't pack codec version")
31+
ErrCantUnpackVersion = errors.New("couldn't unpack codec version")
32+
ErrDuplicatedVersion = errors.New("duplicated codec version")
3433
)
3534

3635
var _ Manager = (*manager)(nil)
@@ -43,7 +42,7 @@ type Manager interface {
4342
// Size returns the size, in bytes, of [value] when it's marshaled
4443
// using the codec with the given version.
4544
// RegisterCodec must have been called with that version.
46-
// If [value] is nil, returns [errMarshalNil]
45+
// If [value] is nil, returns [ErrMarshalNil]
4746
Size(version uint16, value interface{}) (int, error)
4847

4948
// Marshal the given value using the codec with the given version.
@@ -82,15 +81,15 @@ func (m *manager) RegisterCodec(version uint16, codec Codec) error {
8281
defer m.lock.Unlock()
8382

8483
if _, exists := m.codecs[version]; exists {
85-
return errDuplicatedVersion
84+
return ErrDuplicatedVersion
8685
}
8786
m.codecs[version] = codec
8887
return nil
8988
}
9089

9190
func (m *manager) Size(version uint16, value interface{}) (int, error) {
9291
if value == nil {
93-
return 0, errMarshalNil // can't marshal nil
92+
return 0, ErrMarshalNil // can't marshal nil
9493
}
9594

9695
m.lock.RLock()
@@ -110,7 +109,7 @@ func (m *manager) Size(version uint16, value interface{}) (int, error) {
110109
// To marshal an interface, [value] must be a pointer to the interface.
111110
func (m *manager) Marshal(version uint16, value interface{}) ([]byte, error) {
112111
if value == nil {
113-
return nil, errMarshalNil // can't marshal nil
112+
return nil, ErrMarshalNil // can't marshal nil
114113
}
115114

116115
m.lock.RLock()
@@ -126,7 +125,7 @@ func (m *manager) Marshal(version uint16, value interface{}) ([]byte, error) {
126125
}
127126
p.PackShort(version)
128127
if p.Errored() {
129-
return nil, errCantPackVersion // Should never happen
128+
return nil, ErrCantPackVersion // Should never happen
130129
}
131130
return p.Bytes, c.MarshalInto(value, &p)
132131
}
@@ -135,19 +134,19 @@ func (m *manager) Marshal(version uint16, value interface{}) ([]byte, error) {
135134
// interface.
136135
func (m *manager) Unmarshal(bytes []byte, dest interface{}) (uint16, error) {
137136
if dest == nil {
138-
return 0, errUnmarshalNil
137+
return 0, ErrUnmarshalNil
139138
}
140139

141140
if byteLen := len(bytes); byteLen > m.maxSize {
142-
return 0, fmt.Errorf("%w: %d > %d", errUnmarshalTooBig, byteLen, m.maxSize)
141+
return 0, fmt.Errorf("%w: %d > %d", ErrUnmarshalTooBig, byteLen, m.maxSize)
143142
}
144143

145144
p := wrappers.Packer{
146145
Bytes: bytes,
147146
}
148147
version := p.UnpackShort()
149148
if p.Errored() { // Make sure the codec version is correct
150-
return 0, errCantUnpackVersion
149+
return 0, ErrCantUnpackVersion
151150
}
152151

153152
m.lock.RLock()

codec/test_codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ func TestTooLargeUnmarshal(codec GeneralCodec, t testing.TB) {
746746

747747
s := inner{}
748748
_, err := manager.Unmarshal(bytes, &s)
749-
require.ErrorIs(err, errUnmarshalTooBig)
749+
require.ErrorIs(err, ErrUnmarshalTooBig)
750750
}
751751

752752
type outerInterface interface {

vms/avm/blocks/block_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ var (
2525
assetID = ids.GenerateTestID()
2626
)
2727

28+
func TestInvalidBlock(t *testing.T) {
29+
require := require.New(t)
30+
31+
parser, err := NewParser([]fxs.Fx{
32+
&secp256k1fx.Fx{},
33+
})
34+
require.NoError(err)
35+
36+
_, err = parser.ParseBlock(nil)
37+
require.ErrorIs(err, codec.ErrCantUnpackVersion)
38+
}
39+
2840
func TestStandardBlocks(t *testing.T) {
2941
// check standard block can be built and parsed
3042
require := require.New(t)

0 commit comments

Comments
 (0)