github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/vm/splice_test.go (about) 1 package vm 2 3 import ( 4 "testing" 5 6 "github.com/bytom/bytom/testutil" 7 ) 8 9 func TestSpliceOps(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_CAT, 18 startVM: &virtualMachine{ 19 runLimit: 50000, 20 dataStack: [][]byte{[]byte("hello"), []byte("world")}, 21 }, 22 wantVM: &virtualMachine{ 23 runLimit: 49986, 24 deferredCost: -18, 25 dataStack: [][]byte{[]byte("helloworld")}, 26 }, 27 }, { 28 op: OP_CAT, 29 startVM: &virtualMachine{ 30 runLimit: 4, 31 dataStack: [][]byte{[]byte("hello"), []byte("world")}, 32 }, 33 wantErr: ErrRunLimitExceeded, 34 }, { 35 op: OP_SUBSTR, 36 startVM: &virtualMachine{ 37 runLimit: 50000, 38 dataStack: [][]byte{[]byte("helloworld"), {3}, {5}}, 39 }, 40 wantVM: &virtualMachine{ 41 runLimit: 49991, 42 deferredCost: -28, 43 dataStack: [][]byte{[]byte("lowor")}, 44 }, 45 }, { 46 op: OP_SUBSTR, 47 startVM: &virtualMachine{ 48 runLimit: 50000, 49 dataStack: [][]byte{[]byte("helloworld"), {6}, {5}}, 50 }, 51 wantErr: ErrBadValue, 52 }, { 53 op: OP_SUBSTR, 54 startVM: &virtualMachine{ 55 runLimit: 4, 56 dataStack: [][]byte{[]byte("helloworld"), {3}, {5}}, 57 }, 58 wantErr: ErrRunLimitExceeded, 59 }, { 60 op: OP_LEFT, 61 startVM: &virtualMachine{ 62 runLimit: 50000, 63 dataStack: [][]byte{[]byte("helloworld"), {5}}, 64 }, 65 wantVM: &virtualMachine{ 66 runLimit: 49991, 67 deferredCost: -19, 68 dataStack: [][]byte{[]byte("hello")}, 69 }, 70 }, { 71 op: OP_LEFT, 72 startVM: &virtualMachine{ 73 runLimit: 50000, 74 dataStack: [][]byte{[]byte("helloworld"), {11}}, 75 }, 76 wantErr: ErrBadValue, 77 }, { 78 op: OP_LEFT, 79 startVM: &virtualMachine{ 80 runLimit: 4, 81 dataStack: [][]byte{[]byte("helloworld"), {5}}, 82 }, 83 wantErr: ErrRunLimitExceeded, 84 }, { 85 op: OP_RIGHT, 86 startVM: &virtualMachine{ 87 runLimit: 50000, 88 dataStack: [][]byte{[]byte("helloworld"), {5}}, 89 }, 90 wantVM: &virtualMachine{ 91 runLimit: 49991, 92 deferredCost: -19, 93 dataStack: [][]byte{[]byte("world")}, 94 }, 95 }, { 96 op: OP_RIGHT, 97 startVM: &virtualMachine{ 98 runLimit: 50000, 99 dataStack: [][]byte{[]byte("helloworld"), {11}}, 100 }, 101 wantErr: ErrBadValue, 102 }, { 103 op: OP_RIGHT, 104 startVM: &virtualMachine{ 105 runLimit: 4, 106 dataStack: [][]byte{[]byte("helloworld"), {5}}, 107 }, 108 wantErr: ErrRunLimitExceeded, 109 }, { 110 op: OP_SIZE, 111 startVM: &virtualMachine{ 112 runLimit: 50000, 113 dataStack: [][]byte{[]byte("helloworld")}, 114 }, 115 wantVM: &virtualMachine{ 116 runLimit: 49999, 117 deferredCost: 9, 118 dataStack: [][]byte{[]byte("helloworld"), {10}}, 119 }, 120 }, { 121 op: OP_CATPUSHDATA, 122 startVM: &virtualMachine{ 123 runLimit: 50000, 124 dataStack: [][]byte{{0xff}, {0xab, 0xcd}}, 125 }, 126 wantVM: &virtualMachine{ 127 runLimit: 49993, 128 deferredCost: -10, 129 dataStack: [][]byte{{0xff, 0x02, 0xab, 0xcd}}, 130 }, 131 }, { 132 op: OP_CATPUSHDATA, 133 startVM: &virtualMachine{ 134 runLimit: 4, 135 dataStack: [][]byte{{0xff}, {0xab, 0xcd}}, 136 }, 137 wantErr: ErrRunLimitExceeded, 138 }} 139 140 spliceops := []Op{OP_CAT, OP_SUBSTR, OP_LEFT, OP_RIGHT, OP_CATPUSHDATA, OP_SIZE} 141 for _, op := range spliceops { 142 cases = append(cases, testStruct{ 143 op: op, 144 startVM: &virtualMachine{runLimit: 0}, 145 wantErr: ErrRunLimitExceeded, 146 }) 147 } 148 149 for i, c := range cases { 150 err := ops[c.op].fn(c.startVM) 151 152 if err != c.wantErr { 153 t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr) 154 continue 155 } 156 if c.wantErr != nil { 157 continue 158 } 159 160 if !testutil.DeepEqual(c.startVM, c.wantVM) { 161 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) 162 } 163 } 164 }