github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/txscript/opcode_test.go (about)

     1  // Copyright (c) 2013-2015 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package txscript
     7  
     8  import (
     9  	"bytes"
    10  	"fmt"
    11  	"strconv"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  // TestOpcodeDisabled tests the opcodeDisabled function manually because all
    17  // disabled opcodes result in a script execution failure when executed normally,
    18  // so the function is not called under normal circumstances.
    19  func TestOpcodeDisabled(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	tests := []byte{OP_CAT, OP_SUBSTR, OP_LEFT, OP_RIGHT, OP_INVERT,
    23  		OP_AND, OP_OR, OP_2MUL, OP_2DIV, OP_MUL, OP_DIV, OP_MOD,
    24  		OP_LSHIFT, OP_RSHIFT,
    25  	}
    26  	for _, opcodeVal := range tests {
    27  		pop := parsedOpcode{opcode: &opcodeArray[opcodeVal], data: nil}
    28  		if err := opcodeDisabled(&pop, nil); err != ErrStackOpDisabled {
    29  			t.Errorf("opcodeDisabled: unexpected error - got %v, "+
    30  				"want %v", err, ErrStackOpDisabled)
    31  			return
    32  		}
    33  	}
    34  }
    35  
    36  // TestOpcodeDisasm tests the print function for all opcodes in both the oneline
    37  // and full modes to ensure it provides the expected disassembly.
    38  func TestOpcodeDisasm(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	// First, test the oneline disassembly.
    42  
    43  	// The expected strings for the data push opcodes are replaced in the
    44  	// test loops below since they involve repeating bytes.  Also, the
    45  	// OP_NOP# and OP_UNKNOWN# are replaced below too, since it's easier
    46  	// than manually listing them here.
    47  	oneBytes := []byte{0x01}
    48  	oneStr := "01"
    49  	expectedStrings := [256]string{0x00: "0", 0x4f: "-1",
    50  		0x50: "OP_RESERVED", 0x61: "OP_NOP", 0x62: "OP_VER",
    51  		0x63: "OP_IF", 0x64: "OP_NOTIF", 0x65: "OP_VERIF",
    52  		0x66: "OP_VERNOTIF", 0x67: "OP_ELSE", 0x68: "OP_ENDIF",
    53  		0x69: "OP_VERIFY", 0x6a: "OP_RETURN", 0x6b: "OP_TOALTSTACK",
    54  		0x6c: "OP_FROMALTSTACK", 0x6d: "OP_2DROP", 0x6e: "OP_2DUP",
    55  		0x6f: "OP_3DUP", 0x70: "OP_2OVER", 0x71: "OP_2ROT",
    56  		0x72: "OP_2SWAP", 0x73: "OP_IFDUP", 0x74: "OP_DEPTH",
    57  		0x75: "OP_DROP", 0x76: "OP_DUP", 0x77: "OP_NIP",
    58  		0x78: "OP_OVER", 0x79: "OP_PICK", 0x7a: "OP_ROLL",
    59  		0x7b: "OP_ROT", 0x7c: "OP_SWAP", 0x7d: "OP_TUCK",
    60  		0x7e: "OP_CAT", 0x7f: "OP_SUBSTR", 0x80: "OP_LEFT",
    61  		0x81: "OP_RIGHT", 0x82: "OP_SIZE", 0x83: "OP_INVERT",
    62  		0x84: "OP_AND", 0x85: "OP_OR", 0x86: "OP_XOR",
    63  		0x87: "OP_EQUAL", 0x88: "OP_EQUALVERIFY", 0x89: "OP_RESERVED1",
    64  		0x8a: "OP_RESERVED2", 0x8b: "OP_1ADD", 0x8c: "OP_1SUB",
    65  		0x8d: "OP_2MUL", 0x8e: "OP_2DIV", 0x8f: "OP_NEGATE",
    66  		0x90: "OP_ABS", 0x91: "OP_NOT", 0x92: "OP_0NOTEQUAL",
    67  		0x93: "OP_ADD", 0x94: "OP_SUB", 0x95: "OP_MUL", 0x96: "OP_DIV",
    68  		0x97: "OP_MOD", 0x98: "OP_LSHIFT", 0x99: "OP_RSHIFT",
    69  		0x9a: "OP_BOOLAND", 0x9b: "OP_BOOLOR", 0x9c: "OP_NUMEQUAL",
    70  		0x9d: "OP_NUMEQUALVERIFY", 0x9e: "OP_NUMNOTEQUAL",
    71  		0x9f: "OP_LESSTHAN", 0xa0: "OP_GREATERTHAN",
    72  		0xa1: "OP_LESSTHANOREQUAL", 0xa2: "OP_GREATERTHANOREQUAL",
    73  		0xa3: "OP_MIN", 0xa4: "OP_MAX", 0xa5: "OP_WITHIN",
    74  		0xa6: "OP_RIPEMD160", 0xa7: "OP_SHA1", 0xa8: "OP_SHA256",
    75  		0xa9: "OP_HASH160", 0xaa: "OP_HASH256", 0xab: "OP_CODESEPARATOR",
    76  		0xac: "OP_CHECKSIG", 0xad: "OP_CHECKSIGVERIFY",
    77  		0xae: "OP_CHECKMULTISIG", 0xaf: "OP_CHECKMULTISIGVERIFY",
    78  		0xf9: "OP_SMALLDATA", 0xfa: "OP_SMALLINTEGER",
    79  		0xfb: "OP_PUBKEYS", 0xfd: "OP_PUBKEYHASH", 0xfe: "OP_PUBKEY",
    80  		0xff: "OP_INVALIDOPCODE",
    81  	}
    82  	for opcodeVal, expectedStr := range expectedStrings {
    83  		var data []byte
    84  		switch {
    85  		// OP_DATA_1 through OP_DATA_65 display the pushed data.
    86  		case opcodeVal >= 0x01 && opcodeVal < 0x4c:
    87  			data = bytes.Repeat(oneBytes, opcodeVal)
    88  			expectedStr = strings.Repeat(oneStr, opcodeVal)
    89  
    90  		// OP_PUSHDATA1.
    91  		case opcodeVal == 0x4c:
    92  			data = bytes.Repeat(oneBytes, 1)
    93  			expectedStr = strings.Repeat(oneStr, 1)
    94  
    95  		// OP_PUSHDATA2.
    96  		case opcodeVal == 0x4d:
    97  			data = bytes.Repeat(oneBytes, 2)
    98  			expectedStr = strings.Repeat(oneStr, 2)
    99  
   100  		// OP_PUSHDATA4.
   101  		case opcodeVal == 0x4e:
   102  			data = bytes.Repeat(oneBytes, 3)
   103  			expectedStr = strings.Repeat(oneStr, 3)
   104  
   105  		// OP_1 through OP_16 display the numbers themselves.
   106  		case opcodeVal >= 0x51 && opcodeVal <= 0x60:
   107  			val := byte(opcodeVal - (0x51 - 1))
   108  			data = []byte{val}
   109  			expectedStr = strconv.Itoa(int(val))
   110  
   111  		// OP_NOP1 through OP_NOP10.
   112  		case opcodeVal >= 0xb0 && opcodeVal <= 0xb9:
   113  			// OP_NOP2 is an alias of OP_CHECKLOCKTIMEVERIFY
   114  			if opcodeVal == 0xb1 {
   115  				expectedStr = "OP_CHECKLOCKTIMEVERIFY"
   116  			} else {
   117  				val := byte(opcodeVal - (0xb0 - 1))
   118  				expectedStr = "OP_NOP" + strconv.Itoa(int(val))
   119  			}
   120  
   121  		// OP_UNKNOWN#.
   122  		case opcodeVal >= 0xba && opcodeVal <= 0xf8 || opcodeVal == 0xfc:
   123  			expectedStr = "OP_UNKNOWN" + strconv.Itoa(int(opcodeVal))
   124  		}
   125  
   126  		pop := parsedOpcode{opcode: &opcodeArray[opcodeVal], data: data}
   127  		gotStr := pop.print(true)
   128  		if gotStr != expectedStr {
   129  			t.Errorf("pop.print (opcode %x): Unexpected disasm "+
   130  				"string - got %v, want %v", opcodeVal, gotStr,
   131  				expectedStr)
   132  			continue
   133  		}
   134  	}
   135  
   136  	// Now, replace the relevant fields and test the full disassembly.
   137  	expectedStrings[0x00] = "OP_0"
   138  	expectedStrings[0x4f] = "OP_1NEGATE"
   139  	for opcodeVal, expectedStr := range expectedStrings {
   140  		var data []byte
   141  		switch {
   142  		// OP_DATA_1 through OP_DATA_65 display the opcode followed by
   143  		// the pushed data.
   144  		case opcodeVal >= 0x01 && opcodeVal < 0x4c:
   145  			data = bytes.Repeat(oneBytes, opcodeVal)
   146  			expectedStr = fmt.Sprintf("OP_DATA_%d 0x%s", opcodeVal,
   147  				strings.Repeat(oneStr, opcodeVal))
   148  
   149  		// OP_PUSHDATA1.
   150  		case opcodeVal == 0x4c:
   151  			data = bytes.Repeat(oneBytes, 1)
   152  			expectedStr = fmt.Sprintf("OP_PUSHDATA1 0x%02x 0x%s",
   153  				len(data), strings.Repeat(oneStr, 1))
   154  
   155  		// OP_PUSHDATA2.
   156  		case opcodeVal == 0x4d:
   157  			data = bytes.Repeat(oneBytes, 2)
   158  			expectedStr = fmt.Sprintf("OP_PUSHDATA2 0x%04x 0x%s",
   159  				len(data), strings.Repeat(oneStr, 2))
   160  
   161  		// OP_PUSHDATA4.
   162  		case opcodeVal == 0x4e:
   163  			data = bytes.Repeat(oneBytes, 3)
   164  			expectedStr = fmt.Sprintf("OP_PUSHDATA4 0x%08x 0x%s",
   165  				len(data), strings.Repeat(oneStr, 3))
   166  
   167  		// OP_1 through OP_16.
   168  		case opcodeVal >= 0x51 && opcodeVal <= 0x60:
   169  			val := byte(opcodeVal - (0x51 - 1))
   170  			data = []byte{val}
   171  			expectedStr = "OP_" + strconv.Itoa(int(val))
   172  
   173  		// OP_NOP1 through OP_NOP10.
   174  		case opcodeVal >= 0xb0 && opcodeVal <= 0xb9:
   175  			// OP_NOP2 is an alias of OP_CHECKLOCKTIMEVERIFY
   176  			if opcodeVal == 0xb1 {
   177  				expectedStr = "OP_CHECKLOCKTIMEVERIFY"
   178  			} else {
   179  				val := byte(opcodeVal - (0xb0 - 1))
   180  				expectedStr = "OP_NOP" + strconv.Itoa(int(val))
   181  			}
   182  
   183  		// OP_UNKNOWN#.
   184  		case opcodeVal >= 0xba && opcodeVal <= 0xf8 || opcodeVal == 0xfc:
   185  			expectedStr = "OP_UNKNOWN" + strconv.Itoa(int(opcodeVal))
   186  		}
   187  
   188  		pop := parsedOpcode{opcode: &opcodeArray[opcodeVal], data: data}
   189  		gotStr := pop.print(false)
   190  		if gotStr != expectedStr {
   191  			t.Errorf("pop.print (opcode %x): Unexpected disasm "+
   192  				"string - got %v, want %v", opcodeVal, gotStr,
   193  				expectedStr)
   194  			continue
   195  		}
   196  	}
   197  }