github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/vm/bitwise_test.go (about)

     1  package vm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bytom/bytom/testutil"
     7  )
     8  
     9  func TestBitwiseOps(t *testing.T) {
    10  	type testStruct struct {
    11  		op      Op
    12  		startVM *virtualMachine
    13  		wantErr error
    14  		wantVM  *virtualMachine
    15  	}
    16  	cases := []testStruct{{
    17  		op: OP_INVERT,
    18  		startVM: &virtualMachine{
    19  			runLimit:  50000,
    20  			dataStack: [][]byte{{255}},
    21  		},
    22  		wantVM: &virtualMachine{
    23  			runLimit:  49998,
    24  			dataStack: [][]byte{{0}},
    25  		},
    26  	}, {
    27  		op: OP_INVERT,
    28  		startVM: &virtualMachine{
    29  			runLimit:  50000,
    30  			dataStack: [][]byte{{255, 0}},
    31  		},
    32  		wantVM: &virtualMachine{
    33  			runLimit:  49997,
    34  			dataStack: [][]byte{{0, 255}},
    35  		},
    36  	}, {
    37  		op: OP_AND,
    38  		startVM: &virtualMachine{
    39  			runLimit:  50000,
    40  			dataStack: [][]byte{{0xff}, {0x80}},
    41  		},
    42  		wantVM: &virtualMachine{
    43  			runLimit:     49998,
    44  			deferredCost: -9,
    45  			dataStack:    [][]byte{{0x80}},
    46  		},
    47  	}, {
    48  		op: OP_AND,
    49  		startVM: &virtualMachine{
    50  			runLimit:  50000,
    51  			dataStack: [][]byte{{0xff}, {0x80, 0xff}},
    52  		},
    53  		wantVM: &virtualMachine{
    54  			runLimit:     49998,
    55  			deferredCost: -10,
    56  			dataStack:    [][]byte{{0x80}},
    57  		},
    58  	}, {
    59  		op: OP_AND,
    60  		startVM: &virtualMachine{
    61  			runLimit:  50000,
    62  			dataStack: [][]byte{{0x80, 0xff}, {0xff}},
    63  		},
    64  		wantVM: &virtualMachine{
    65  			runLimit:     49998,
    66  			deferredCost: -10,
    67  			dataStack:    [][]byte{{0x80}},
    68  		},
    69  	}, {
    70  		op: OP_OR,
    71  		startVM: &virtualMachine{
    72  			runLimit:  50000,
    73  			dataStack: [][]byte{{0xff}, {0x80}},
    74  		},
    75  		wantVM: &virtualMachine{
    76  			runLimit:     49998,
    77  			deferredCost: -9,
    78  			dataStack:    [][]byte{{0xff}},
    79  		},
    80  	}, {
    81  		op: OP_OR,
    82  		startVM: &virtualMachine{
    83  			runLimit:  50000,
    84  			dataStack: [][]byte{{0xff}, {0x80, 0x10}},
    85  		},
    86  		wantVM: &virtualMachine{
    87  			runLimit:     49997,
    88  			deferredCost: -9,
    89  			dataStack:    [][]byte{{0xff, 0x10}},
    90  		},
    91  	}, {
    92  		op: OP_OR,
    93  		startVM: &virtualMachine{
    94  			runLimit:  50000,
    95  			dataStack: [][]byte{{0xff, 0x10}, {0x80}},
    96  		},
    97  		wantVM: &virtualMachine{
    98  			runLimit:     49997,
    99  			deferredCost: -9,
   100  			dataStack:    [][]byte{{0xff, 0x10}},
   101  		},
   102  	}, {
   103  		op: OP_XOR,
   104  		startVM: &virtualMachine{
   105  			runLimit:  50000,
   106  			dataStack: [][]byte{{0xff}, {0x80}},
   107  		},
   108  		wantVM: &virtualMachine{
   109  			runLimit:     49998,
   110  			deferredCost: -9,
   111  			dataStack:    [][]byte{{0x7f}},
   112  		},
   113  	}, {
   114  		op: OP_XOR,
   115  		startVM: &virtualMachine{
   116  			runLimit:  50000,
   117  			dataStack: [][]byte{{0xff}, {0x80, 0x10}},
   118  		},
   119  		wantVM: &virtualMachine{
   120  			runLimit:     49997,
   121  			deferredCost: -9,
   122  			dataStack:    [][]byte{{0x7f, 0x10}},
   123  		},
   124  	}, {
   125  		op: OP_XOR,
   126  		startVM: &virtualMachine{
   127  			runLimit:  50000,
   128  			dataStack: [][]byte{{0xff, 0x10}, {0x80}},
   129  		},
   130  		wantVM: &virtualMachine{
   131  			runLimit:     49997,
   132  			deferredCost: -9,
   133  			dataStack:    [][]byte{{0x7f, 0x10}},
   134  		},
   135  	}, {
   136  		op: OP_EQUAL,
   137  		startVM: &virtualMachine{
   138  			runLimit:  50000,
   139  			dataStack: [][]byte{{0xff}, {0xff}},
   140  		},
   141  		wantVM: &virtualMachine{
   142  			runLimit:     49998,
   143  			deferredCost: -9,
   144  			dataStack:    [][]byte{{1}},
   145  		},
   146  	}, {
   147  		op: OP_EQUAL,
   148  		startVM: &virtualMachine{
   149  			runLimit:  50000,
   150  			dataStack: [][]byte{{0xff, 0x10}, {0xff, 0x10}},
   151  		},
   152  		wantVM: &virtualMachine{
   153  			runLimit:     49997,
   154  			deferredCost: -11,
   155  			dataStack:    [][]byte{{1}},
   156  		},
   157  	}, {
   158  		op: OP_EQUAL,
   159  		startVM: &virtualMachine{
   160  			runLimit:  50000,
   161  			dataStack: [][]byte{{0xff}, {0x80}},
   162  		},
   163  		wantVM: &virtualMachine{
   164  			runLimit:     49998,
   165  			deferredCost: -10,
   166  			dataStack:    [][]byte{{}},
   167  		},
   168  	}, {
   169  		op: OP_EQUAL,
   170  		startVM: &virtualMachine{
   171  			runLimit:  50000,
   172  			dataStack: [][]byte{{0xff}, {0xff, 0x80}},
   173  		},
   174  		wantVM: &virtualMachine{
   175  			runLimit:     49998,
   176  			deferredCost: -11,
   177  			dataStack:    [][]byte{{}},
   178  		},
   179  	}, {
   180  		op: OP_EQUAL,
   181  		startVM: &virtualMachine{
   182  			runLimit:  50000,
   183  			dataStack: [][]byte{{0xff, 0x80}, {0xff}},
   184  		},
   185  		wantVM: &virtualMachine{
   186  			runLimit:     49998,
   187  			deferredCost: -11,
   188  			dataStack:    [][]byte{{}},
   189  		},
   190  	}, {
   191  		op: OP_EQUALVERIFY,
   192  		startVM: &virtualMachine{
   193  			runLimit:  50000,
   194  			dataStack: [][]byte{{0xff}, {0xff}},
   195  		},
   196  		wantVM: &virtualMachine{
   197  			runLimit:     49998,
   198  			deferredCost: -18,
   199  			dataStack:    [][]byte{},
   200  		},
   201  	}, {
   202  		op: OP_EQUALVERIFY,
   203  		startVM: &virtualMachine{
   204  			runLimit:  50000,
   205  			dataStack: [][]byte{{0xff, 0x10}, {0xff, 0x10}},
   206  		},
   207  		wantVM: &virtualMachine{
   208  			runLimit:     49997,
   209  			deferredCost: -20,
   210  			dataStack:    [][]byte{},
   211  		},
   212  	}, {
   213  		op: OP_EQUALVERIFY,
   214  		startVM: &virtualMachine{
   215  			runLimit:  50000,
   216  			dataStack: [][]byte{{0xff}, {0x80}},
   217  		},
   218  		wantErr: ErrVerifyFailed,
   219  	}, {
   220  		op: OP_EQUALVERIFY,
   221  		startVM: &virtualMachine{
   222  			runLimit:  50000,
   223  			dataStack: [][]byte{{0xff}, {0xff, 0x80}},
   224  		},
   225  		wantErr: ErrVerifyFailed,
   226  	}, {
   227  		op: OP_EQUALVERIFY,
   228  		startVM: &virtualMachine{
   229  			runLimit:  50000,
   230  			dataStack: [][]byte{{0xff, 0x80}, {0xff}},
   231  		},
   232  		wantErr: ErrVerifyFailed,
   233  	}}
   234  
   235  	bitops := []Op{OP_INVERT, OP_AND, OP_OR, OP_XOR, OP_EQUAL, OP_EQUALVERIFY}
   236  	for _, op := range bitops {
   237  		cases = append(cases, testStruct{
   238  			op: op,
   239  			startVM: &virtualMachine{
   240  				runLimit:  0,
   241  				dataStack: [][]byte{{0xff}, {0xff}},
   242  			},
   243  			wantErr: ErrRunLimitExceeded,
   244  		}, testStruct{
   245  			op: op,
   246  			startVM: &virtualMachine{
   247  				runLimit:  1,
   248  				dataStack: [][]byte{{0xff}, {0xff}},
   249  			},
   250  			wantErr: ErrRunLimitExceeded,
   251  		})
   252  	}
   253  
   254  	for i, c := range cases {
   255  		err := ops[c.op].fn(c.startVM)
   256  
   257  		if err != c.wantErr {
   258  			t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
   259  			continue
   260  		}
   261  		if c.wantErr != nil {
   262  			continue
   263  		}
   264  
   265  		if !testutil.DeepEqual(c.startVM, c.wantVM) {
   266  			t.Errorf("case %d, op %s: unexpected vm result\n\tgot:  %+v\n\twant: %+v\n", i, ops[c.op].name, c.startVM, c.wantVM)
   267  		}
   268  	}
   269  }