Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - VerifyAndExtract #2068

Closed
tomsmith8 opened this issue Dec 3, 2024 · 0 comments
Closed

[Unit Tests] - VerifyAndExtract #2068

tomsmith8 opened this issue Dec 3, 2024 · 0 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for "VerifyAndExtract"


Stakwork Run


Unit Test Code


package auth

import (
  "errors"
  "testing"

  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

// Mocking dependencies
type MockBtcecdsa struct {
  mock.Mock
}

func (m *MockBtcecdsa) RecoverCompact(sig, digest []byte) ([]byte, bool, error) {
  args := m.Called(sig, digest)
  return args.Get(0).([]byte), args.Bool(1), args.Error(2)
}

type MockChainhash struct {
  mock.Mock
}

func (m *MockChainhash) DoubleHashB(msg []byte) []byte {
  args := m.Called(msg)
  return args.Get(0).([]byte)
}

func TestVerifyAndExtract(t *testing.T) {
  mockBtcecdsa := new(MockBtcecdsa)
  mockChainhash := new(MockChainhash)

  // Replace actual dependencies with mocks
  btcecdsa = mockBtcecdsa
  chainhash = mockChainhash

  // Test case 1: Valid Signature and Message
  t.Run("Valid Signature and Message", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("valid signature")
  	expectedPubKey := []byte("public key")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(expectedPubKey, true, nil)

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Equal(t, "public key", pubKey)
  	assert.True(t, valid)
  	assert.Nil(t, err)
  })

  // Test case 2: Invalid Signature
  t.Run("Invalid Signature", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("invalid signature")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(nil, false, errors.New("invalid signature"))

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.NotNil(t, err)
  })

  // Test case 3: Invalid Message
  t.Run("Invalid Message", func(t *testing.T) {
  	msg := []byte("invalid message")
  	sig := []byte("valid signature")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(nil, false, errors.New("invalid message"))

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.NotNil(t, err)
  })

  // Test case 4: Empty Message and Signature
  t.Run("Empty Message and Signature", func(t *testing.T) {
  	msg := []byte{}
  	sig := []byte{}

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.EqualError(t, err, "bad input")
  })

  // Test case 5: Nil Message and Signature
  t.Run("Nil Message and Signature", func(t *testing.T) {
  	pubKey, valid, err := VerifyAndExtract(nil, nil)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.EqualError(t, err, "bad input")
  })

  // Test case 6: Maximum Length Message
  t.Run("Maximum Length Message", func(t *testing.T) {
  	msg := make([]byte, maxMessageLength) // assuming maxMessageLength is defined
  	sig := []byte("valid signature")
  	expectedPubKey := []byte("public key")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(expectedPubKey, true, nil)

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Equal(t, "public key", pubKey)
  	assert.True(t, valid)
  	assert.Nil(t, err)
  })

  // Test case 7: Corrupted Signature
  t.Run("Corrupted Signature", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("corrupted signature")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(nil, false, errors.New("corrupted signature"))

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.NotNil(t, err)
  })

  // Test case 8: Unsupported Signature Format
  t.Run("Unsupported Signature Format", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("unsupported format")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(nil, false, errors.New("unsupported format"))

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.NotNil(t, err)
  })

  // Test case 9: Malformed Signature
  t.Run("Malformed Signature", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("malformed")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(nil, false, errors.New("malformed signature"))

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.NotNil(t, err)
  })

  // Test case 10: Large Message
  t.Run("Large Message", func(t *testing.T) {
  	msg := make([]byte, largeMessageSize) // assuming largeMessageSize is defined
  	sig := []byte("valid signature")
  	expectedPubKey := []byte("public key")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(expectedPubKey, true, nil)

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Equal(t, "public key", pubKey)
  	assert.True(t, valid)
  	assert.Nil(t, err)
  })

  // Test case 11: High Volume of Requests
  t.Run("High Volume of Requests", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("valid signature")
  	expectedPubKey := []byte("public key")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest).Times(highVolumeCount) // assuming highVolumeCount is defined
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(expectedPubKey, true, nil).Times(highVolumeCount)

  	for i := 0; i < highVolumeCount; i++ {
  		pubKey, valid, err := VerifyAndExtract(msg, sig)

  		assert.Equal(t, "public key", pubKey)
  		assert.True(t, valid)
  		assert.Nil(t, err)
  	}
  })

  // Test case 12: Boundary Signature Values
  t.Run("Boundary Signature Values", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("boundary signature")
  	expectedPubKey := []byte("public key")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(expectedPubKey, true, nil)

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Equal(t, "public key", pubKey)
  	assert.True(t, valid)
  	assert.Nil(t, err)
  })

  // Test case 13: Signature with Special Characters
  t.Run("Signature with Special Characters", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("special!@#$%^&*()")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest)
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(nil, false, errors.New("special characters"))

  	pubKey, valid, err := VerifyAndExtract(msg, sig)

  	assert.Empty(t, pubKey)
  	assert.False(t, valid)
  	assert.NotNil(t, err)
  })

  // Test case 14: Repeated Signature Verification
  t.Run("Repeated Signature Verification", func(t *testing.T) {
  	msg := []byte("valid message")
  	sig := []byte("valid signature")
  	expectedPubKey := []byte("public key")
  	expectedDigest := []byte("digest")

  	mockChainhash.On("DoubleHashB", append(signedMsgPrefix, msg...)).Return(expectedDigest).Times(repeatCount) // assuming repeatCount is defined
  	mockBtcecdsa.On("RecoverCompact", sig, expectedDigest).Return(expectedPubKey, true, nil).Times(repeatCount)

  	for i := 0; i < repeatCount; i++ {
  		pubKey, valid, err := VerifyAndExtract(msg, sig)

  		assert.Equal(t, "public key", pubKey)
  		assert.True(t, valid)
  		assert.Nil(t, err)
  	}
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant