github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/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"), {3}, Int64Bytes(-1)}, 50 }, 51 wantErr: ErrBadValue, 52 }, { 53 op: OP_SUBSTR, 54 startVM: &virtualMachine{ 55 runLimit: 50000, 56 dataStack: [][]byte{[]byte("helloworld"), Int64Bytes(-1), {5}}, 57 }, 58 wantErr: ErrBadValue, 59 }, { 60 op: OP_SUBSTR, 61 startVM: &virtualMachine{ 62 runLimit: 50000, 63 dataStack: [][]byte{[]byte("helloworld"), {6}, {5}}, 64 }, 65 wantErr: ErrBadValue, 66 }, { 67 op: OP_SUBSTR, 68 startVM: &virtualMachine{ 69 runLimit: 4, 70 dataStack: [][]byte{[]byte("helloworld"), {3}, {5}}, 71 }, 72 wantErr: ErrRunLimitExceeded, 73 }, { 74 op: OP_LEFT, 75 startVM: &virtualMachine{ 76 runLimit: 50000, 77 dataStack: [][]byte{[]byte("helloworld"), {5}}, 78 }, 79 wantVM: &virtualMachine{ 80 runLimit: 49991, 81 deferredCost: -19, 82 dataStack: [][]byte{[]byte("hello")}, 83 }, 84 }, { 85 op: OP_LEFT, 86 startVM: &virtualMachine{ 87 runLimit: 50000, 88 dataStack: [][]byte{[]byte("helloworld"), Int64Bytes(-1)}, 89 }, 90 wantErr: ErrBadValue, 91 }, { 92 op: OP_LEFT, 93 startVM: &virtualMachine{ 94 runLimit: 50000, 95 dataStack: [][]byte{[]byte("helloworld"), {11}}, 96 }, 97 wantErr: ErrBadValue, 98 }, { 99 op: OP_LEFT, 100 startVM: &virtualMachine{ 101 runLimit: 4, 102 dataStack: [][]byte{[]byte("helloworld"), {5}}, 103 }, 104 wantErr: ErrRunLimitExceeded, 105 }, { 106 op: OP_RIGHT, 107 startVM: &virtualMachine{ 108 runLimit: 50000, 109 dataStack: [][]byte{[]byte("helloworld"), {5}}, 110 }, 111 wantVM: &virtualMachine{ 112 runLimit: 49991, 113 deferredCost: -19, 114 dataStack: [][]byte{[]byte("world")}, 115 }, 116 }, { 117 op: OP_RIGHT, 118 startVM: &virtualMachine{ 119 runLimit: 50000, 120 dataStack: [][]byte{[]byte("helloworld"), Int64Bytes(-1)}, 121 }, 122 wantErr: ErrBadValue, 123 }, { 124 op: OP_RIGHT, 125 startVM: &virtualMachine{ 126 runLimit: 50000, 127 dataStack: [][]byte{[]byte("helloworld"), {11}}, 128 }, 129 wantErr: ErrBadValue, 130 }, { 131 op: OP_RIGHT, 132 startVM: &virtualMachine{ 133 runLimit: 4, 134 dataStack: [][]byte{[]byte("helloworld"), {5}}, 135 }, 136 wantErr: ErrRunLimitExceeded, 137 }, { 138 op: OP_SIZE, 139 startVM: &virtualMachine{ 140 runLimit: 50000, 141 dataStack: [][]byte{[]byte("helloworld")}, 142 }, 143 wantVM: &virtualMachine{ 144 runLimit: 49999, 145 deferredCost: 9, 146 dataStack: [][]byte{[]byte("helloworld"), {10}}, 147 }, 148 }, { 149 op: OP_CATPUSHDATA, 150 startVM: &virtualMachine{ 151 runLimit: 50000, 152 dataStack: [][]byte{{0xff}, {0xab, 0xcd}}, 153 }, 154 wantVM: &virtualMachine{ 155 runLimit: 49993, 156 deferredCost: -10, 157 dataStack: [][]byte{{0xff, 0x02, 0xab, 0xcd}}, 158 }, 159 }, { 160 op: OP_CATPUSHDATA, 161 startVM: &virtualMachine{ 162 runLimit: 4, 163 dataStack: [][]byte{{0xff}, {0xab, 0xcd}}, 164 }, 165 wantErr: ErrRunLimitExceeded, 166 }} 167 168 spliceops := []Op{OP_CAT, OP_SUBSTR, OP_LEFT, OP_RIGHT, OP_CATPUSHDATA, OP_SIZE} 169 for _, op := range spliceops { 170 cases = append(cases, testStruct{ 171 op: op, 172 startVM: &virtualMachine{runLimit: 0}, 173 wantErr: ErrRunLimitExceeded, 174 }) 175 } 176 177 for i, c := range cases { 178 err := ops[c.op].fn(c.startVM) 179 180 if err != c.wantErr { 181 t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr) 182 continue 183 } 184 if c.wantErr != nil { 185 continue 186 } 187 188 if !testutil.DeepEqual(c.startVM, c.wantVM) { 189 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) 190 } 191 } 192 }