From c3376fe056dc16859e2f1c0bb7f28913fa02b726 Mon Sep 17 00:00:00 2001 From: David Theodore Date: Mon, 25 Mar 2024 15:05:00 -0500 Subject: [PATCH 1/2] fixed gwei unmarshal panic --- spec/phase0/gwei.go | 4 +++ spec/phase0/gwei_test.go | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 spec/phase0/gwei_test.go diff --git a/spec/phase0/gwei.go b/spec/phase0/gwei.go index 0d2ec884..8c603713 100644 --- a/spec/phase0/gwei.go +++ b/spec/phase0/gwei.go @@ -30,6 +30,10 @@ func (g *Gwei) UnmarshalJSON(input []byte) error { return errors.New("input missing") } + if len(input) < 3 { + return errors.New("input malformed") + } + if !bytes.HasPrefix(input, []byte{'"'}) { return errors.New("invalid prefix") } diff --git a/spec/phase0/gwei_test.go b/spec/phase0/gwei_test.go new file mode 100644 index 00000000..3b7a5412 --- /dev/null +++ b/spec/phase0/gwei_test.go @@ -0,0 +1,73 @@ +// Copyright © 2020, 2021 Attestant Limited. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package phase0_test + +// Create a test to verify gwei.unmarshalJSON +import ( + "testing" + + "github.com/attestantio/go-eth2-client/spec/phase0" +) + +func TestGweiUnmarshalJSON(t *testing.T) { + // Test cases + tests := []struct { + name string + input []byte + expected phase0.Gwei + wantErr bool + }{ + { + name: "Valid input 1000000000", + input: []byte("\"1000000000\""), + expected: phase0.Gwei(1000000000), + wantErr: false, + }, + + { + name: "Valid input", + input: []byte("\"1\""), + expected: phase0.Gwei(1), + wantErr: false, + }, + + { + name: "Invalid input text", + input: []byte("not-a-number"), + expected: 0, + wantErr: true, + }, + { + name: "Invalid input single quote", + input: []byte("\""), + expected: 0, + wantErr: true, + }, + } + + // Run tests + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var g phase0.Gwei + err := g.UnmarshalJSON(tt.input) + if (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if g != tt.expected { + t.Errorf("UnmarshalJSON() got = %v, expected %v", g, tt.expected) + } + }) + } +} From 666ff6334053eb9b827a96dfb6e3de51453d1a30 Mon Sep 17 00:00:00 2001 From: David Theodore Date: Mon, 25 Mar 2024 15:17:33 -0500 Subject: [PATCH 2/2] changed spacing --- spec/phase0/gwei.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/phase0/gwei.go b/spec/phase0/gwei.go index 8c603713..7041de6d 100644 --- a/spec/phase0/gwei.go +++ b/spec/phase0/gwei.go @@ -29,11 +29,9 @@ func (g *Gwei) UnmarshalJSON(input []byte) error { if len(input) == 0 { return errors.New("input missing") } - if len(input) < 3 { return errors.New("input malformed") } - if !bytes.HasPrefix(input, []byte{'"'}) { return errors.New("invalid prefix") }