|
| 1 | +// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers |
| 2 | +// |
| 3 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 4 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 6 | +// You may not use this file except in accordance with one or both of these |
| 7 | +// licenses. |
| 8 | + |
| 9 | +//! bdk-cli Integration Test Framework |
| 10 | +//! |
| 11 | +//! This modules performs the necessary integration test for bdk-cli |
| 12 | +//! The tests can be run using `cargo test` |
| 13 | +
|
| 14 | +use serde_json::{json, Value}; |
| 15 | +use std::convert::From; |
| 16 | +use std::process::Command; |
| 17 | + |
| 18 | +/// Testing errors for integration tests |
| 19 | +#[derive(Debug)] |
| 20 | +enum IntTestError { |
| 21 | + // IO error |
| 22 | + IO(std::io::Error), |
| 23 | + // Command execution error |
| 24 | + CmdExec(String), |
| 25 | + // Json Data error |
| 26 | + JsonData(String), |
| 27 | +} |
| 28 | + |
| 29 | +impl From<std::io::Error> for IntTestError { |
| 30 | + fn from(e: std::io::Error) -> Self { |
| 31 | + IntTestError::IO(e) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Helper function |
| 36 | +// Runs a system command with given args |
| 37 | +fn run_cmd_with_args(cmd: &str, args: &[&str]) -> Result<serde_json::Value, IntTestError> { |
| 38 | + let output = Command::new(cmd).args(args).output().unwrap(); |
| 39 | + let mut value = output.stdout; |
| 40 | + let error = output.stderr; |
| 41 | + if value.len() == 0 { |
| 42 | + return Err(IntTestError::CmdExec(String::from_utf8(error).unwrap())); |
| 43 | + } |
| 44 | + value.pop(); // remove `\n` at end |
| 45 | + let output_string = std::str::from_utf8(&value).unwrap(); |
| 46 | + let json_value: serde_json::Value = match serde_json::from_str(output_string) { |
| 47 | + Ok(value) => value, |
| 48 | + Err(_) => json!(output_string), // bitcoin-cli will sometime return raw string |
| 49 | + }; |
| 50 | + Ok(json_value) |
| 51 | +} |
| 52 | + |
| 53 | +// Helper Function |
| 54 | +// Transforms a json value to string |
| 55 | +fn value_to_string(value: &Value) -> Result<String, IntTestError> { |
| 56 | + match value { |
| 57 | + Value::Bool(bool) => match bool { |
| 58 | + true => Ok("true".to_string()), |
| 59 | + false => Ok("false".to_string()), |
| 60 | + }, |
| 61 | + Value::Number(n) => Ok(n.to_string()), |
| 62 | + Value::String(s) => Ok(s.to_string()), |
| 63 | + _ => Err(IntTestError::JsonData( |
| 64 | + "Value parsing not implemented for this type".to_string(), |
| 65 | + )), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Helper Function |
| 70 | +// Extracts value from a given json object and key |
| 71 | +fn get_value(json: &Value, key: &str) -> Result<String, IntTestError> { |
| 72 | + let map = json |
| 73 | + .as_object() |
| 74 | + .ok_or(IntTestError::JsonData("Json is not an object".to_string()))?; |
| 75 | + let value = map |
| 76 | + .get(key) |
| 77 | + .ok_or(IntTestError::JsonData("Invalid key".to_string()))? |
| 78 | + .to_owned(); |
| 79 | + let string_value = value_to_string(&value)?; |
| 80 | + Ok(string_value) |
| 81 | +} |
| 82 | + |
| 83 | +/// The bdk-cli command struct |
| 84 | +/// Use it to perform all bdk-cli operations |
| 85 | +struct BdkCli { |
| 86 | + target: String, |
| 87 | + network: String, |
| 88 | + verbosity: bool, |
| 89 | + recv_desc: Option<String>, |
| 90 | + chang_desc: Option<String>, |
| 91 | +} |
| 92 | + |
| 93 | +impl BdkCli { |
| 94 | + /// Construct a new [`BdkCli`] struct |
| 95 | + fn new(network: &str, verbosity: bool, features: &[&str]) -> Result<Self, IntTestError> { |
| 96 | + // Build bdk-cli with given features |
| 97 | + let mut feat = "--features=".to_string(); |
| 98 | + for item in features { |
| 99 | + feat.push_str(item); |
| 100 | + feat.push_str(","); |
| 101 | + } |
| 102 | + feat.pop(); // remove the last comma |
| 103 | + let _build = Command::new("cargo").args(&["build", &feat]).output()?; |
| 104 | + |
| 105 | + let mut bdk_cli = Self { |
| 106 | + target: "./target/debug/bdk-cli".to_string(), |
| 107 | + network: network.to_string(), |
| 108 | + verbosity, |
| 109 | + recv_desc: None, |
| 110 | + chang_desc: None, |
| 111 | + }; |
| 112 | + |
| 113 | + let bdk_master_key = bdk_cli.key_exec(&["generate"])?; |
| 114 | + let bdk_xprv = get_value(&bdk_master_key, "xprv")?; |
| 115 | + |
| 116 | + let bdk_recv_desc = |
| 117 | + bdk_cli.key_exec(&["derive", "--path", "m/84h/1h/0h/0", "--xprv", &bdk_xprv])?; |
| 118 | + let bdk_recv_desc = get_value(&bdk_recv_desc, "xprv")?; |
| 119 | + let bdk_recv_desc = format!("wpkh({})", bdk_recv_desc); |
| 120 | + |
| 121 | + let bdk_chng_desc = |
| 122 | + bdk_cli.key_exec(&["derive", "--path", "m/84h/1h/0h/1", "--xprv", &bdk_xprv])?; |
| 123 | + let bdk_chng_desc = get_value(&bdk_chng_desc, "xprv")?; |
| 124 | + let bdk_chng_desc = format!("wpkh({})", bdk_chng_desc); |
| 125 | + |
| 126 | + bdk_cli.recv_desc = Some(bdk_recv_desc); |
| 127 | + bdk_cli.chang_desc = Some(bdk_chng_desc); |
| 128 | + |
| 129 | + Ok(bdk_cli) |
| 130 | + } |
| 131 | + |
| 132 | + /// Execute bdk-cli wallet commands with given args |
| 133 | + fn wallet_exec(&self, args: &[&str]) -> Result<Value, IntTestError> { |
| 134 | + let mut wallet_args = ["--network", &self.network, "wallet"].to_vec(); |
| 135 | + if self.verbosity { |
| 136 | + wallet_args.push("-v"); |
| 137 | + } |
| 138 | + |
| 139 | + wallet_args.push("-d"); |
| 140 | + wallet_args.push(self.recv_desc.as_ref().unwrap()); |
| 141 | + wallet_args.push("-c"); |
| 142 | + wallet_args.push(&self.chang_desc.as_ref().unwrap()); |
| 143 | + |
| 144 | + for arg in args { |
| 145 | + wallet_args.push(arg); |
| 146 | + } |
| 147 | + run_cmd_with_args(&self.target, &wallet_args) |
| 148 | + } |
| 149 | + |
| 150 | + /// Execute bdk-cli key commands with given args |
| 151 | + fn key_exec(&self, args: &[&str]) -> Result<Value, IntTestError> { |
| 152 | + let mut key_args = ["key"].to_vec(); |
| 153 | + for arg in args { |
| 154 | + key_args.push(arg); |
| 155 | + } |
| 156 | + run_cmd_with_args(&self.target, &key_args) |
| 157 | + } |
| 158 | + |
| 159 | + /// Execute bdk-cli node command |
| 160 | + fn node_exec(&self, args: &[&str]) -> Result<Value, IntTestError> { |
| 161 | + let mut node_args = ["node"].to_vec(); |
| 162 | + for arg in args { |
| 163 | + node_args.push(arg); |
| 164 | + } |
| 165 | + run_cmd_with_args(&self.target, &node_args) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +#[test] |
| 170 | +fn test_basic() { |
| 171 | + // Test basic building, fmt and unit tests |
| 172 | + let features = [ |
| 173 | + "default", |
| 174 | + "electrum", |
| 175 | + "esplora-ureq", |
| 176 | + "esplora-reqwest", |
| 177 | + "compiler", |
| 178 | + "compact_filters", |
| 179 | + "reserves", |
| 180 | + "reserves,electrum", |
| 181 | + "reserves,esplora-ureq", |
| 182 | + "reserves,compact_filters", |
| 183 | + "rpc", |
| 184 | + "regtest-bitcoin", |
| 185 | + "regtest-electrum", |
| 186 | + "regtest-esplora-ureq", |
| 187 | + "regtest-esplora-reqwest" |
| 188 | + ]; |
| 189 | + |
| 190 | + // Test for build and fmt |
| 191 | + for feature in features { |
| 192 | + Command::new("cargo") |
| 193 | + .args(["build", "--features", feature, "--locked"]) |
| 194 | + .output() |
| 195 | + .unwrap(); |
| 196 | + Command::new("cargo") |
| 197 | + .args(["fmt", "--features", feature, "--locked"]) |
| 198 | + .output() |
| 199 | + .unwrap(); |
| 200 | + Command::new("cargo") |
| 201 | + .args(["clippy", "--features", feature, "--locked"]) |
| 202 | + .output() |
| 203 | + .unwrap(); |
| 204 | + println!("Building with {} feature completed", feature); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +#[test] |
| 209 | +fn test_basic_wallet_ops() { |
| 210 | + // Create bdk-cli instance |
| 211 | + let bdk_cli = BdkCli::new("regtest", false, &["regtest-bitcoin"]).unwrap(); |
| 212 | + |
| 213 | + // Generate 101 blocks |
| 214 | + bdk_cli.node_exec(&["generate", "101"]).unwrap(); |
| 215 | + |
| 216 | + // Get a bdk address |
| 217 | + let bdk_addr_json = bdk_cli.wallet_exec(&["get_new_address"]).unwrap(); |
| 218 | + let bdk_addr = get_value(&bdk_addr_json, "address").unwrap(); |
| 219 | + |
| 220 | + // Send coins from core to bdk |
| 221 | + bdk_cli |
| 222 | + .node_exec(&["sendtoaddress", &bdk_addr, "1000000000"]) |
| 223 | + .unwrap(); |
| 224 | + |
| 225 | + bdk_cli.node_exec(&["generate", "1"]).unwrap(); |
| 226 | + |
| 227 | + // Sync the bdk wallet |
| 228 | + bdk_cli.wallet_exec(&["sync"]).unwrap(); |
| 229 | + |
| 230 | + // Get the balance |
| 231 | + let balance_json = bdk_cli.wallet_exec(&["get_balance"]).unwrap(); |
| 232 | + let balance = get_value(&balance_json, "satoshi").unwrap(); |
| 233 | + assert_eq!(balance, "1000000000"); |
| 234 | +} |
0 commit comments