|
| 1 | +#include "config.h" |
| 2 | +#include "../shutdown_scriptpubkey.c" |
| 3 | +#include <assert.h> |
| 4 | +#include <ccan/array_size/array_size.h> |
| 5 | +#include <ccan/err/err.h> |
| 6 | +#include <common/amount.h> |
| 7 | +#include <common/setup.h> |
| 8 | +#include <common/utils.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <wire/wire.h> |
| 11 | + |
| 12 | +/* AUTOGENERATED MOCKS START */ |
| 13 | +/* Generated stub for amount_asset_is_main */ |
| 14 | +bool amount_asset_is_main(struct amount_asset *asset UNNEEDED) |
| 15 | +{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); } |
| 16 | +/* Generated stub for amount_asset_to_sat */ |
| 17 | +struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) |
| 18 | +{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); } |
| 19 | +/* Generated stub for amount_feerate */ |
| 20 | + bool amount_feerate(u32 *feerate UNNEEDED, struct amount_sat fee UNNEEDED, size_t weight UNNEEDED) |
| 21 | +{ fprintf(stderr, "amount_feerate called!\n"); abort(); } |
| 22 | +/* Generated stub for amount_sat */ |
| 23 | +struct amount_sat amount_sat(u64 satoshis UNNEEDED) |
| 24 | +{ fprintf(stderr, "amount_sat called!\n"); abort(); } |
| 25 | +/* Generated stub for amount_sat_add */ |
| 26 | + bool amount_sat_add(struct amount_sat *val UNNEEDED, |
| 27 | + struct amount_sat a UNNEEDED, |
| 28 | + struct amount_sat b UNNEEDED) |
| 29 | +{ fprintf(stderr, "amount_sat_add called!\n"); abort(); } |
| 30 | +/* Generated stub for amount_sat_eq */ |
| 31 | +bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) |
| 32 | +{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); } |
| 33 | +/* Generated stub for amount_sat_greater_eq */ |
| 34 | +bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) |
| 35 | +{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); } |
| 36 | +/* Generated stub for amount_sat_sub */ |
| 37 | + bool amount_sat_sub(struct amount_sat *val UNNEEDED, |
| 38 | + struct amount_sat a UNNEEDED, |
| 39 | + struct amount_sat b UNNEEDED) |
| 40 | +{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); } |
| 41 | +/* Generated stub for amount_sat_to_asset */ |
| 42 | +struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED) |
| 43 | +{ fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); } |
| 44 | +/* Generated stub for amount_tx_fee */ |
| 45 | +struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) |
| 46 | +{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); } |
| 47 | +/* AUTOGENERATED MOCKS END */ |
| 48 | + |
| 49 | +/* Thanks ChatGPT! */ |
| 50 | +static const u8 *construct_script(const tal_t *ctx, |
| 51 | + const u8 *data, size_t data_len) |
| 52 | +{ |
| 53 | + u8 *script = tal_arr(ctx, u8, 0); |
| 54 | + towire_u8(&script, OP_RETURN); |
| 55 | + if (data_len >= 6 && data_len <= 75) { |
| 56 | + towire_u8(&script, data_len); |
| 57 | + towire(&script, data, data_len); |
| 58 | + } else if (data_len >= 76 && data_len <= 80) { |
| 59 | + towire_u8(&script, 76); |
| 60 | + towire_u8(&script, data_len); |
| 61 | + towire(&script, data, data_len); |
| 62 | + } else { |
| 63 | + return tal_free(script); // Invalid case |
| 64 | + } |
| 65 | + return script; |
| 66 | +} |
| 67 | + |
| 68 | +static void test_valid_op_returns(void) |
| 69 | +{ |
| 70 | + u8 data[80]; |
| 71 | + const u8 *script; |
| 72 | + |
| 73 | + for (size_t i = 6; i <= 80; i++) { |
| 74 | + memset(data, i, sizeof(data)); |
| 75 | + script = construct_script(tmpctx, data, i); |
| 76 | + assert(is_valid_op_return(script, tal_bytelen(script))); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +static void test_invalid_op_return_too_short(void) |
| 81 | +{ |
| 82 | + u8 data[80]; |
| 83 | + const u8 *script; |
| 84 | + |
| 85 | + for (size_t i = 0; i < 6; i++) { |
| 86 | + memset(data, i, sizeof(data)); |
| 87 | + script = construct_script(tmpctx, data, i); |
| 88 | + assert(!is_valid_op_return(script, tal_bytelen(script))); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +static void test_invalid_op_return_too_long(void) |
| 93 | +{ |
| 94 | + u8 data[100]; |
| 95 | + const u8 *script; |
| 96 | + |
| 97 | + for (size_t i = 81; i < sizeof(data); i++) { |
| 98 | + memset(data, i, sizeof(data)); |
| 99 | + script = construct_script(tmpctx, data, i); |
| 100 | + assert(!is_valid_op_return(script, tal_bytelen(script))); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Test case: Invalid OP_RETURN with incorrect push length (e.g., 77 bytes using wrong prefix) |
| 105 | +static void test_invalid_op_return_wrong_push_length(void) |
| 106 | +{ |
| 107 | + u8 script[90] = {0x6a, 0x76, 0x77}; // Invalid push of 77 bytes |
| 108 | + assert(!is_valid_op_return(script, 3 + 77)); |
| 109 | +} |
| 110 | + |
| 111 | +// Test case: Invalid OP_RETURN with incorrect OP_RETURN opcode |
| 112 | +static void test_invalid_op_return_wrong_opcode(void) |
| 113 | +{ |
| 114 | + u8 script[10] = {0x00, 0x06, 1, 2, 3, 4, 5, 6}; // 0x00 instead of 0x6a |
| 115 | + assert(!is_valid_op_return(script, 8)); |
| 116 | +} |
| 117 | + |
| 118 | +// Test case: Invalid OP_RETURN with no data |
| 119 | +static void test_invalid_op_return_empty(void) |
| 120 | +{ |
| 121 | + u8 script[1] = {0x6a}; // Only OP_RETURN, no data |
| 122 | + assert(!is_valid_op_return(script, 1)); |
| 123 | +} |
| 124 | + |
| 125 | +static const u8 *construct_witness_script(const tal_t *ctx, u8 version, |
| 126 | + const u8 *data, size_t data_len) |
| 127 | +{ |
| 128 | + u8 *script = tal_arr(ctx, u8, 0); |
| 129 | + |
| 130 | + if (version < OP_1 || version > OP_16 || data_len < 2 || data_len > 40) |
| 131 | + return tal_free(script); // Invalid case |
| 132 | + |
| 133 | + towire_u8(&script, version); // OP_1 to OP_16 |
| 134 | + towire_u8(&script, data_len); // Push length |
| 135 | + towire(&script, data, data_len); // Data |
| 136 | + |
| 137 | + return script; |
| 138 | +} |
| 139 | + |
| 140 | +static void test_valid_witnessprogs(void) |
| 141 | +{ |
| 142 | + u8 data[40]; |
| 143 | + const u8 *script; |
| 144 | + |
| 145 | + for (u8 version = OP_1; version <= OP_16; version++) { |
| 146 | + for (size_t i = 2; i <= 40; i++) { |
| 147 | + memset(data, i, sizeof(data)); |
| 148 | + script = construct_witness_script(tmpctx, version, data, i); |
| 149 | + assert(is_valid_witnessprog(script, tal_bytelen(script))); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +static void test_invalid_witnessprogs(void) |
| 155 | +{ |
| 156 | + u8 data[41]; |
| 157 | + const u8 *script; |
| 158 | + |
| 159 | + // Test: Invalid versions (0 and >16) |
| 160 | + memset(data, 0xAA, sizeof(data)); |
| 161 | + script = construct_witness_script(tmpctx, OP_0, data, 20); |
| 162 | + assert(!is_valid_witnessprog(script, tal_bytelen(script))); |
| 163 | + |
| 164 | + script = construct_witness_script(tmpctx, OP_16 + 1, data, 20); |
| 165 | + assert(!is_valid_witnessprog(script, tal_bytelen(script))); |
| 166 | + |
| 167 | + // Test: Invalid data lengths (1 byte and >40 bytes) |
| 168 | + script = construct_witness_script(tmpctx, OP_1, data, 1); |
| 169 | + assert(!is_valid_witnessprog(script, tal_bytelen(script))); |
| 170 | + |
| 171 | + script = construct_witness_script(tmpctx, OP_2, data, 41); |
| 172 | + assert(!is_valid_witnessprog(script, tal_bytelen(script))); |
| 173 | + |
| 174 | + // Test: Completely empty script (invalid) |
| 175 | + script = tal_arr(tmpctx, u8, 0); |
| 176 | + assert(!is_valid_witnessprog(script, tal_bytelen(script))); |
| 177 | +} |
| 178 | + |
| 179 | +// Test runner |
| 180 | +int main(int argc, char *argv[]) |
| 181 | +{ |
| 182 | + common_setup(argv[0]); |
| 183 | + |
| 184 | + test_valid_op_returns(); |
| 185 | + test_invalid_op_return_too_short(); |
| 186 | + test_invalid_op_return_too_long(); |
| 187 | + test_invalid_op_return_wrong_push_length(); |
| 188 | + test_invalid_op_return_wrong_opcode(); |
| 189 | + test_invalid_op_return_empty(); |
| 190 | + test_valid_witnessprogs(); |
| 191 | + test_invalid_witnessprogs(); |
| 192 | + common_shutdown(); |
| 193 | + return 0; |
| 194 | +} |
0 commit comments