github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/state/oracle_test.go (about)

     1  package state
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/nspcc-dev/neo-go/internal/random"
     8  	"github.com/nspcc-dev/neo-go/internal/testserdes"
     9  	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestOracleRequestToFromSI(t *testing.T) {
    14  	t.Run("Valid", func(t *testing.T) {
    15  		r := &OracleRequest{
    16  			OriginalTxID:     random.Uint256(),
    17  			GasForResponse:   12345,
    18  			URL:              "https://get.value",
    19  			CallbackContract: random.Uint160(),
    20  			CallbackMethod:   "method",
    21  			UserData:         []byte{1, 2, 3},
    22  		}
    23  		testserdes.ToFromStackItem(t, r, new(OracleRequest))
    24  
    25  		t.Run("WithFilter", func(t *testing.T) {
    26  			s := "filter"
    27  			r.Filter = &s
    28  			testserdes.ToFromStackItem(t, r, new(OracleRequest))
    29  		})
    30  	})
    31  	t.Run("Invalid", func(t *testing.T) {
    32  		var res = new(OracleRequest)
    33  		t.Run("NotArray", func(t *testing.T) {
    34  			it := stackitem.NewByteArray([]byte{})
    35  			require.Error(t, res.FromStackItem(it))
    36  		})
    37  
    38  		items := []stackitem.Item{
    39  			stackitem.NewByteArray(random.Uint256().BytesBE()),
    40  			stackitem.NewBigInteger(big.NewInt(123)),
    41  			stackitem.Make("url"),
    42  			stackitem.Null{},
    43  			stackitem.NewByteArray(random.Uint160().BytesBE()),
    44  			stackitem.Make("method"),
    45  			stackitem.NewByteArray([]byte{1, 2, 3}),
    46  		}
    47  		arrItem := stackitem.NewArray(items)
    48  		runInvalid := func(i int, elem stackitem.Item) func(t *testing.T) {
    49  			return func(t *testing.T) {
    50  				before := items[i]
    51  				items[i] = elem
    52  				require.Error(t, res.FromStackItem(arrItem))
    53  				items[i] = before
    54  			}
    55  		}
    56  		t.Run("TxID", func(t *testing.T) {
    57  			t.Run("Type", runInvalid(0, stackitem.NewMap()))
    58  			t.Run("Length", runInvalid(0, stackitem.NewByteArray([]byte{0, 1, 2})))
    59  		})
    60  		t.Run("Gas", runInvalid(1, stackitem.NewMap()))
    61  		t.Run("URL", runInvalid(2, stackitem.NewMap()))
    62  		t.Run("Filter", runInvalid(3, stackitem.NewMap()))
    63  		t.Run("Contract", func(t *testing.T) {
    64  			t.Run("Type", runInvalid(4, stackitem.NewMap()))
    65  			t.Run("Length", runInvalid(4, stackitem.NewByteArray([]byte{0, 1, 2})))
    66  		})
    67  		t.Run("Method", runInvalid(5, stackitem.NewMap()))
    68  		t.Run("UserData", runInvalid(6, stackitem.NewMap()))
    69  	})
    70  }