Skip to content

Commit

Permalink
Increased test coverage for error branches for util/encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosz committed Feb 15, 2024
1 parent 2d94d52 commit 5ee4043
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions util/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package encoding

import (
"bytes"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -60,3 +61,38 @@ func TestScalarHexString(t *testing.T) {
ErrFatal(err)
require.True(t, sc.Equal(s2))
}

// Tests for error cases
type MockFailingReader struct {
data []byte
}
type MockEmptyReader struct {
data []byte
}

func (m *MockFailingReader) Read(p []byte) (n int, err error) {
return copy(p, m.data), io.EOF
}
func (m *MockEmptyReader) Read(p []byte) (n int, err error) {
return 0, nil
}

func TestReadHexPointError(t *testing.T) {
// Test case: invalid hex encoding
reader := bytes.NewReader([]byte("invalidhex"))
_, err := ReadHexPoint(s, reader)
require.Error(t, err, "Expected error when reading invalid hex encoding, but got nil")

// Test case: reader fails
mockReader1 := &MockFailingReader{data: []byte("abc")}
_, err = ReadHexPoint(s, mockReader1)
require.Error(t, err, "Expected error when reader fails, but got nil")

// Test case: not enough bytes from stream
mockReader2 := &MockEmptyReader{data: []byte("abc")}
_, err = ReadHexPoint(s, mockReader2)
require.Error(t, err, "Expected error when not enough bytes from stream, but got nil")
if err.Error() != "didn't get enough bytes from stream" {
t.Errorf("Expected error message: didn't get enough bytes from stream, but got %s", err.Error())
}
}

0 comments on commit 5ee4043

Please sign in to comment.