@@ -148,9 +148,11 @@ func TestValidScripts(t *testing.T) {
148148
149149 for _ , tt := range tests {
150150 t .Run (tt .name , func (t * testing.T ) {
151- err := testVerifyScript (t , tt .scriptPubkeyHex , tt .amount , tt .txToHex , tt .inputIndex )
151+ valid , err := testVerifyScript (t , tt .scriptPubkeyHex , tt .amount , tt .txToHex , tt .inputIndex )
152152 if err != nil {
153153 t .Errorf ("testVerifyScript() error = %v" , err )
154+ } else if ! valid {
155+ t .Errorf ("testVerifyScript() expected valid script, got invalid" )
154156 }
155157 })
156158 }
@@ -203,17 +205,82 @@ func TestInvalidScripts(t *testing.T) {
203205
204206 for _ , tt := range tests {
205207 t .Run (tt .name , func (t * testing.T ) {
206- var scriptVerifyError * ScriptVerifyError
207- err := testVerifyScript (t , tt .scriptPubkeyHex , tt .amount , tt .txToHex , tt .inputIndex )
208- if err == nil || ! errors .As (err , & scriptVerifyError ) {
209- t .Errorf ("testVerifyScript() was expected to fail with ScriptVerifyError, got: %v" , err )
208+ valid , err := testVerifyScript (t , tt .scriptPubkeyHex , tt .amount , tt .txToHex , tt .inputIndex )
209+ if err != nil {
210+ t .Errorf ("testVerifyScript() unexpected error = %v" , err )
211+ } else if valid {
212+ t .Errorf ("testVerifyScript() expected invalid script, got valid" )
213+ }
214+ })
215+ }
216+ }
217+
218+ func TestScriptVerifyErrors (t * testing.T ) {
219+ // Use a valid transaction for testing error conditions
220+ validScriptHex := "76a9144bfbaf6afb76cc5771bc6404810d1cc041a6933988ac"
221+ validTxHex := "02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700"
222+
223+ scriptBytes , err := hex .DecodeString (validScriptHex )
224+ if err != nil {
225+ t .Fatalf ("Failed to decode script hex: %v" , err )
226+ }
227+
228+ scriptPubkey := NewScriptPubkey (scriptBytes )
229+ defer scriptPubkey .Destroy ()
230+
231+ txBytes , err := hex .DecodeString (validTxHex )
232+ if err != nil {
233+ t .Fatalf ("Failed to decode transaction hex: %v" , err )
234+ }
235+
236+ tx , err := NewTransaction (txBytes )
237+ if err != nil {
238+ t .Fatalf ("Failed to create transaction: %v" , err )
239+ }
240+ defer tx .Destroy ()
241+
242+ tests := []struct {
243+ name string
244+ inputIndex uint
245+ flags ScriptFlags
246+ spentOutputs []* TransactionOutput
247+ expectedError error
248+ description string
249+ }{
250+ {
251+ name : "invalid_input_index" ,
252+ inputIndex : 999 ,
253+ flags : ScriptFlagsVerifyAll ,
254+ spentOutputs : nil ,
255+ expectedError : ErrVerifyScriptVerifyTxInputIndex ,
256+ description : "input index out of bounds should return error" ,
257+ },
258+ {
259+ name : "invalid_flags" ,
260+ inputIndex : 0 ,
261+ flags : ScriptFlags (0xFFFFFFFF ), // Invalid flags
262+ spentOutputs : nil ,
263+ expectedError : ErrVerifyScriptVerifyInvalidFlags ,
264+ description : "invalid flags should return error" ,
265+ },
266+ }
267+
268+ for _ , tt := range tests {
269+ t .Run (tt .name , func (t * testing.T ) {
270+ valid , err := scriptPubkey .Verify (0 , tx , tt .spentOutputs , tt .inputIndex , tt .flags )
271+ if err == nil {
272+ t .Errorf ("Expected error %v, got nil (valid=%v)" , tt .expectedError , valid )
273+ return
274+ }
275+ if ! errors .Is (err , tt .expectedError ) {
276+ t .Errorf ("Expected error %v, got %v" , tt .expectedError , err )
210277 }
211278 })
212279 }
213280}
214281
215282// testVerifyScript is a helper function that creates the necessary objects and calls VerifyScript
216- func testVerifyScript (t * testing.T , scriptPubkeyHex string , amount int64 , txToHex string , inputIndex uint ) error {
283+ func testVerifyScript (t * testing.T , scriptPubkeyHex string , amount int64 , txToHex string , inputIndex uint ) ( bool , error ) {
217284 scriptPubkeyBytes , err := hex .DecodeString (scriptPubkeyHex )
218285 if err != nil {
219286 t .Fatalf ("Failed to decode script pubkey hex: %v" , err )
0 commit comments