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