| 144 | } |
| 145 | |
| 146 | void Test(const std::string& str) |
| 147 | { |
| 148 | UniValue test; |
| 149 | if (!test.read(str) || !test.isObject()) throw std::runtime_error("Non-object test input"); |
| 150 | |
| 151 | CMutableTransaction tx = TxFromHex(test["tx"].get_str()); |
| 152 | const std::vector<CTxOut> prevouts = TxOutsFromJSON(test["prevouts"]); |
| 153 | if (prevouts.size() != tx.vin.size()) throw std::runtime_error("Incorrect number of prevouts"); |
| 154 | size_t idx = test["index"].getInt<int64_t>(); |
| 155 | if (idx >= tx.vin.size()) throw std::runtime_error("Invalid index"); |
| 156 | script_verify_flags test_flags = ParseScriptFlags(test["flags"].get_str()); |
| 157 | bool final = test.exists("final") && test["final"].get_bool(); |
| 158 | |
| 159 | if (test.exists("success")) { |
| 160 | tx.vin[idx].scriptSig = ScriptFromHex(test["success"]["scriptSig"].get_str()); |
| 161 | tx.vin[idx].scriptWitness = ScriptWitnessFromJSON(test["success"]["witness"]); |
| 162 | PrecomputedTransactionData txdata; |
| 163 | txdata.Init(tx, std::vector<CTxOut>(prevouts)); |
| 164 | MutableTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, txdata, MissingDataBehavior::ASSERT_FAIL); |
| 165 | for (const auto flags : ALL_FLAGS) { |
| 166 | // "final": true tests are valid for all flags. Others are only valid with flags that are |
| 167 | // a subset of test_flags. |
| 168 | if (final || ((flags & test_flags) == flags)) { |
| 169 | (void)VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.vin[idx].scriptWitness, flags, txcheck, nullptr); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (test.exists("failure")) { |
| 175 | tx.vin[idx].scriptSig = ScriptFromHex(test["failure"]["scriptSig"].get_str()); |
| 176 | tx.vin[idx].scriptWitness = ScriptWitnessFromJSON(test["failure"]["witness"]); |
| 177 | PrecomputedTransactionData txdata; |
| 178 | txdata.Init(tx, std::vector<CTxOut>(prevouts)); |
| 179 | MutableTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, txdata, MissingDataBehavior::ASSERT_FAIL); |
| 180 | for (const auto flags : ALL_FLAGS) { |
| 181 | // If a test is supposed to fail with test_flags, it should also fail with any superset thereof. |
| 182 | if ((flags & test_flags) == test_flags) { |
| 183 | (void)VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.vin[idx].scriptWitness, flags, txcheck, nullptr); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void test_init() {} |
| 190 |
no test coverage detected