We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Stakwork Run
package auth import ( "errors" "testing" "github.com/btcsuite/btcd/btcec" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) // Mock for btcecdsa.SignCompact type MockBtcecdsa struct { mock.Mock } func (m *MockBtcecdsa) SignCompact(privKey *btcec.PrivateKey, digest []byte, isCompressed bool) ([]byte, error) { args := m.Called(privKey, digest, isCompressed) return args.Get(0).([]byte), args.Error(1) } func TestSign(t *testing.T) { // Setup mock mockBtcecdsa := new(MockBtcecdsa) originalSignCompact := btcecdsa.SignCompact btcecdsa.SignCompact = mockBtcecdsa.SignCompact defer func() { btcecdsa.SignCompact = originalSignCompact }() // Generate a valid private key for testing privKey, _ := btcec.NewPrivateKey(btcec.S256()) // Test cases tests := []struct { name string msg []byte privKey *btcec.PrivateKey mockSetup func() expectedSig []byte expectedError error }{ { name: "Valid Message and Private Key", msg: []byte("Hello, World!"), privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return([]byte("signature"), nil) }, expectedSig: []byte("signature"), expectedError: nil, }, { name: "Empty Message", msg: []byte(""), privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return([]byte("signature"), nil) }, expectedSig: []byte("signature"), expectedError: nil, }, { name: "Maximum Length Message", msg: []byte(repeat("A", max_length)), // Assume max_length is defined elsewhere privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return([]byte("signature"), nil) }, expectedSig: []byte("signature"), expectedError: nil, }, { name: "Nil Message", msg: nil, privKey: privKey, mockSetup: func() { // No need to mock SignCompact as it should not be called }, expectedSig: nil, expectedError: errors.New("no msg"), }, { name: "Nil Private Key", msg: []byte("Hello, World!"), privKey: nil, mockSetup: func() { // No need to mock SignCompact as it should not be called }, expectedSig: nil, expectedError: errors.New("invalid private key"), }, { name: "Invalid Private Key Type", msg: []byte("Hello, World!"), privKey: nil, // Simulating invalid type by passing nil mockSetup: func() { // No need to mock SignCompact as it should not be called }, expectedSig: nil, expectedError: errors.New("invalid private key"), }, { name: "Corrupted Private Key", msg: []byte("Hello, World!"), privKey: privKey, // Assume privKey is corrupted mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return(nil, errors.New("invalid private key")) }, expectedSig: nil, expectedError: errors.New("invalid private key"), }, { name: "Large Message", msg: []byte(repeat("A", very_large_size)), // Assume very_large_size is defined elsewhere privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return([]byte("signature"), nil) }, expectedSig: []byte("signature"), expectedError: nil, }, { name: "Message with Special Characters", msg: []byte("!@#$%^&*()_+"), privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return([]byte("signature"), nil) }, expectedSig: []byte("signature"), expectedError: nil, }, { name: "Message with Non-ASCII Characters", msg: []byte("こんにちは世界"), privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return([]byte("signature"), nil) }, expectedSig: []byte("signature"), expectedError: nil, }, { name: "Message with Null Bytes", msg: []byte("Hello\x00World"), privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return([]byte("signature"), nil) }, expectedSig: []byte("signature"), expectedError: nil, }, { name: "Mock SignCompact Failure", msg: []byte("Hello, World!"), privKey: privKey, mockSetup: func() { mockBtcecdsa.On("SignCompact", privKey, mock.Anything, true).Return(nil, errors.New("signing error")) }, expectedSig: nil, expectedError: errors.New("signing error"), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.mockSetup() sig, err := Sign(tt.msg, tt.privKey) assert.Equal(t, tt.expectedSig, sig) if tt.expectedError != nil { assert.EqualError(t, err, tt.expectedError.Error()) } else { assert.NoError(t, err) } mockBtcecdsa.AssertExpectations(t) }) } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Unit Test Coverage for "Sign"
Stakwork Run
Unit Test Code
The text was updated successfully, but these errors were encountered: