TestMakeScriptNum ensures that converting from byte representations to integral script numbers works as expected.
(t *testing.T)
| 89 | // TestMakeScriptNum ensures that converting from byte representations to |
| 90 | // integral script numbers works as expected. |
| 91 | func TestMakeScriptNum(t *testing.T) { |
| 92 | t.Parallel() |
| 93 | |
| 94 | // Errors used in the tests below defined here for convenience and to |
| 95 | // keep the horizontal test size shorter. |
| 96 | errNumTooBig := scriptError(ErrNumberTooBig, "") |
| 97 | errMinimalData := scriptError(ErrMinimalData, "") |
| 98 | |
| 99 | tests := []struct { |
| 100 | serialized []byte |
| 101 | num scriptNum |
| 102 | numLen int |
| 103 | minimalEncoding bool |
| 104 | err error |
| 105 | }{ |
| 106 | // Minimal encoding must reject negative 0. |
| 107 | {hexToBytes("80"), 0, maxScriptNumLen, true, errMinimalData}, |
| 108 | |
| 109 | // Minimally encoded valid values with minimal encoding flag. |
| 110 | // Should not error and return expected integral number. |
| 111 | {nil, 0, maxScriptNumLen, true, nil}, |
| 112 | {hexToBytes("01"), 1, maxScriptNumLen, true, nil}, |
| 113 | {hexToBytes("81"), -1, maxScriptNumLen, true, nil}, |
| 114 | {hexToBytes("7f"), 127, maxScriptNumLen, true, nil}, |
| 115 | {hexToBytes("ff"), -127, maxScriptNumLen, true, nil}, |
| 116 | {hexToBytes("8000"), 128, maxScriptNumLen, true, nil}, |
| 117 | {hexToBytes("8080"), -128, maxScriptNumLen, true, nil}, |
| 118 | {hexToBytes("8100"), 129, maxScriptNumLen, true, nil}, |
| 119 | {hexToBytes("8180"), -129, maxScriptNumLen, true, nil}, |
| 120 | {hexToBytes("0001"), 256, maxScriptNumLen, true, nil}, |
| 121 | {hexToBytes("0081"), -256, maxScriptNumLen, true, nil}, |
| 122 | {hexToBytes("ff7f"), 32767, maxScriptNumLen, true, nil}, |
| 123 | {hexToBytes("ffff"), -32767, maxScriptNumLen, true, nil}, |
| 124 | {hexToBytes("008000"), 32768, maxScriptNumLen, true, nil}, |
| 125 | {hexToBytes("008080"), -32768, maxScriptNumLen, true, nil}, |
| 126 | {hexToBytes("ffff00"), 65535, maxScriptNumLen, true, nil}, |
| 127 | {hexToBytes("ffff80"), -65535, maxScriptNumLen, true, nil}, |
| 128 | {hexToBytes("000008"), 524288, maxScriptNumLen, true, nil}, |
| 129 | {hexToBytes("000088"), -524288, maxScriptNumLen, true, nil}, |
| 130 | {hexToBytes("000070"), 7340032, maxScriptNumLen, true, nil}, |
| 131 | {hexToBytes("0000f0"), -7340032, maxScriptNumLen, true, nil}, |
| 132 | {hexToBytes("00008000"), 8388608, maxScriptNumLen, true, nil}, |
| 133 | {hexToBytes("00008080"), -8388608, maxScriptNumLen, true, nil}, |
| 134 | {hexToBytes("ffffff7f"), 2147483647, maxScriptNumLen, true, nil}, |
| 135 | {hexToBytes("ffffffff"), -2147483647, maxScriptNumLen, true, nil}, |
| 136 | {hexToBytes("ffffffff7f"), 549755813887, 5, true, nil}, |
| 137 | {hexToBytes("ffffffffff"), -549755813887, 5, true, nil}, |
| 138 | {hexToBytes("ffffffffffffff7f"), 9223372036854775807, 8, true, nil}, |
| 139 | {hexToBytes("ffffffffffffffff"), -9223372036854775807, 8, true, nil}, |
| 140 | {hexToBytes("ffffffffffffffff7f"), -1, 9, true, nil}, |
| 141 | {hexToBytes("ffffffffffffffffff"), 1, 9, true, nil}, |
| 142 | {hexToBytes("ffffffffffffffffff7f"), -1, 10, true, nil}, |
| 143 | {hexToBytes("ffffffffffffffffffff"), 1, 10, true, nil}, |
| 144 | |
| 145 | // Minimally encoded values that are out of range for data that |
| 146 | // is interpreted as script numbers with the minimal encoding |
| 147 | // flag set. Should error and return 0. |
| 148 | {hexToBytes("0000008000"), 0, maxScriptNumLen, true, errNumTooBig}, |
nothing calls this directly
no test coverage detected
searching dependent graphs…