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] - VerifyArbitrary #2070

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

[Unit Tests] - VerifyArbitrary #2070

tomsmith8 opened this issue Dec 3, 2024 · 0 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for "VerifyArbitrary"


Stakwork Run


Unit Test Code


package auth

import (
  "encoding/base64"
  "errors"
  "testing"

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

// Mock for VerifyAndExtract function
type MockVerifier struct {
  mock.Mock
}

func (m *MockVerifier) VerifyAndExtract(msg, sig []byte) (string, bool, error) {
  args := m.Called(msg, sig)
  return args.String(0), args.Bool(1), args.Error(2)
}

func TestVerifyArbitrary(t *testing.T) {
  mockVerifier := new(MockVerifier)

  // Test case 1: Valid Signature and Message
  t.Run("ValidSignatureAndMessage", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("valid_signature"))
  	msg := "valid_message"
  	expectedPubKey := "valid_pubkey"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("valid_signature")).Return(expectedPubKey, true, nil)

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.NoError(t, err)
  	assert.Equal(t, expectedPubKey, pubkey)
  })

  // Test case 2: Empty Signature
  t.Run("EmptySignature", func(t *testing.T) {
  	sig := ""
  	msg := "valid_message"

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.Error(t, err)
  	assert.Empty(t, pubkey)
  })

  // Test case 3: Empty Message
  t.Run("EmptyMessage", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("valid_signature"))
  	msg := ""

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("valid_signature")).Return("", false, errors.New("invalid message"))

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.Error(t, err)
  	assert.Empty(t, pubkey)
  })

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

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.Error(t, err)
  	assert.Empty(t, pubkey)
  })

  // Test case 5: Maximum Length Signature
  t.Run("MaximumLengthSignature", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("very_long_valid_signature"))
  	msg := "valid_message"
  	expectedPubKey := "valid_pubkey"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("very_long_valid_signature")).Return(expectedPubKey, true, nil)

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.NoError(t, err)
  	assert.Equal(t, expectedPubKey, pubkey)
  })

  // Test case 6: Invalid Base64 Signature
  t.Run("InvalidBase64Signature", func(t *testing.T) {
  	sig := "invalid_base64"
  	msg := "valid_message"

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.Error(t, err)
  	assert.Empty(t, pubkey)
  })

  // Test case 7: Invalid Signature Format
  t.Run("InvalidSignatureFormat", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("invalid_signature"))
  	msg := "valid_message"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("invalid_signature")).Return("", false, errors.New("invalid signature"))

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.Error(t, err)
  	assert.Empty(t, pubkey)
  })

  // Test case 8: Invalid Message Format
  t.Run("InvalidMessageFormat", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("valid_signature"))
  	msg := "invalid_message"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("valid_signature")).Return("", false, errors.New("invalid message"))

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.Error(t, err)
  	assert.Empty(t, pubkey)
  })

  // Test case 9: Corrupted Signature
  t.Run("CorruptedSignature", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("corrupted_signature"))
  	msg := "valid_message"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("corrupted_signature")).Return("", false, errors.New("signature verification failed"))

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.Error(t, err)
  	assert.Empty(t, pubkey)
  })

  // Test case 10: Large Message
  t.Run("LargeMessage", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("valid_signature"))
  	msg := "very_large_message"
  	expectedPubKey := "valid_pubkey"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("valid_signature")).Return(expectedPubKey, true, nil)

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.NoError(t, err)
  	assert.Equal(t, expectedPubKey, pubkey)
  })

  // Test case 11: Signature with Special Characters
  t.Run("SignatureWithSpecialCharacters", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("special_signature!@#"))
  	msg := "valid_message"
  	expectedPubKey := "valid_pubkey"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("special_signature!@#")).Return(expectedPubKey, true, nil)

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.NoError(t, err)
  	assert.Equal(t, expectedPubKey, pubkey)
  })

  // Test case 12: Signature with Padding Characters
  t.Run("SignatureWithPaddingCharacters", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("padded_signature==="))
  	msg := "valid_message"
  	expectedPubKey := "valid_pubkey"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("padded_signature===")).Return(expectedPubKey, true, nil)

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.NoError(t, err)
  	assert.Equal(t, expectedPubKey, pubkey)
  })

  // Test case 13: Signature with Non-Standard Base64 Characters
  t.Run("SignatureWithNonStandardBase64Characters", func(t *testing.T) {
  	sig := base64.URLEncoding.EncodeToString([]byte("url_safe_signature-_"))
  	msg := "valid_message"
  	expectedPubKey := "valid_pubkey"

  	mockVerifier.On("VerifyAndExtract", []byte(msg), []byte("url_safe_signature-_")).Return(expectedPubKey, true, nil)

  	pubkey, err := VerifyArbitrary(sig, msg)
  	assert.NoError(t, err)
  	assert.Equal(t, expectedPubKey, pubkey)
  })
}
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