aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/new.rs13
-rw-r--r--tests/sign_typeddata.rs50
-rw-r--r--tests/util/mod.rs77
3 files changed, 118 insertions, 22 deletions
diff --git a/tests/new.rs b/tests/new.rs
index 4ddbf0c..a44dd2d 100644
--- a/tests/new.rs
+++ b/tests/new.rs
@@ -1,20 +1,23 @@
mod util;
use hdwallet::mnemonic::Mnemonic;
+use util::Hdwallet;
#[test]
fn generates_random_mnemonic() {
- let mnemonic = util::exec("new", &[]);
+ let mnemonic = Hdwallet::run("new", &[]);
assert!(Mnemonic::from_phrase(mnemonic).is_ok())
}
#[test]
fn errors_on_invalid_language() {
- assert!(util::try_exec("new", &["--language", "klingon"]).is_err());
+ assert!(Hdwallet::new("new", &["--language", "klingon"])
+ .execute()
+ .is_err());
}
#[test]
fn errors_on_invalid_length() {
- assert!(util::try_exec("new", &["--length", "1"]).is_err());
- assert!(util::try_exec("new", &["--length", "42"]).is_err());
-} \ No newline at end of file
+ assert!(Hdwallet::new("new", &["--length", "1"]).execute().is_err());
+ assert!(Hdwallet::new("new", &["--length", "42"]).execute().is_err());
+}
diff --git a/tests/sign_typeddata.rs b/tests/sign_typeddata.rs
new file mode 100644
index 0000000..4c6da7f
--- /dev/null
+++ b/tests/sign_typeddata.rs
@@ -0,0 +1,50 @@
+mod util;
+
+use crate::util::Hdwallet;
+
+#[test]
+fn signs_typed_data() {
+ let signature = Hdwallet::new("sign", &["typeddata", "-"])
+ .stdin(
+ r#"{
+ "types": {
+ "EIP712Domain": [
+ { "name": "name", "type": "string" },
+ { "name": "version", "type": "string" },
+ { "name": "chainId", "type": "uint256" },
+ { "name": "verifyingContract", "type": "address" }
+ ],
+ "Person": [
+ { "name": "name", "type": "string" },
+ { "name": "wallet", "type": "address" }
+ ],
+ "Mail": [
+ { "name": "from", "type": "Person" },
+ { "name": "to", "type": "Person" },
+ { "name": "contents", "type": "string" }
+ ]
+ },
+ "primaryType": "Mail",
+ "domain": {
+ "name": "Ether Mail",
+ "version": "1",
+ "chainId": 1,
+ "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
+ },
+ "message": {
+ "from": {
+ "name": "Cow",
+ "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
+ },
+ "to": {
+ "name": "Bob",
+ "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
+ },
+ "contents": "Hello, Bob!"
+ }
+ }"#,
+ )
+ .execute()
+ .unwrap();
+ assert_eq!(signature, "0x12bdd486cb42c3b3c414bb04253acfe7d402559e7637562987af6bd78508f38623c1cc09880613762cc913d49fd7d3c091be974c0dee83fb233300b6b58727311c");
+}
diff --git a/tests/util/mod.rs b/tests/util/mod.rs
index 3cc4c66..cd29b28 100644
--- a/tests/util/mod.rs
+++ b/tests/util/mod.rs
@@ -1,29 +1,72 @@
+#![allow(dead_code)]
+
#[path = "../../src/ganache.rs"]
mod ganache;
-use std::{str, process::Command};
pub use ganache::*;
+use std::{
+ io::Write as _,
+ process::{Command, Stdio},
+ str, thread,
+};
-/// Utility function for running the `hdwallet` command for integration tests.
-pub fn exec(subcommand: &str, args: &[&str]) -> String {
- try_exec(subcommand, args).unwrap()
+/// Utility type for building an `hdwallet` command for integration tests.
+pub struct Hdwallet {
+ command: Command,
+ stdin: Option<Vec<u8>>,
}
-/// Try and execute a command, returning an error if there was one.
-pub fn try_exec(subcommand: &str, args: &[&str]) -> Result<String, String> {
- let output = Command::new("cargo")
- .env("MNEMONIC", ganache::DETERMINISTIC_MNEMONIC)
- .args(["run", "--", subcommand])
- .args(args)
- .output()
- .unwrap();
- if output.status.success() {
- Ok(string_from_utf8(output.stdout))
- } else {
- Err(string_from_utf8(output.stderr))
+impl Hdwallet {
+ /// Create a new `hdwallet` command builder.
+ pub fn new(subcommand: &str, args: &[&str]) -> Self {
+ let mut command = Command::new("cargo");
+ command
+ .env("MNEMONIC", ganache::DETERMINISTIC_MNEMONIC)
+ .args(["run", "--", subcommand])
+ .args(args);
+ Self {
+ command,
+ stdin: None,
+ }
+ }
+
+ /// Sets the standard input to be used for the command.
+ pub fn stdin(mut self, data: impl Into<Vec<u8>>) -> Self {
+ self.stdin = Some(data.into());
+ self
+ }
+
+ /// Executes the command and returns the standard output on success and
+ /// standard error on failure.
+ pub fn execute(mut self) -> Result<String, String> {
+ self.command.stdout(Stdio::piped()).stderr(Stdio::piped());
+ if self.stdin.is_some() {
+ self.command.stdin(Stdio::piped());
+ }
+
+ let mut process = self.command.spawn().map_err(|err| err.to_string())?;
+
+ if let Some(data) = self.stdin {
+ let mut stdin = process.stdin.take().unwrap();
+ thread::spawn(move || {
+ stdin.write_all(&data).unwrap();
+ });
+ }
+
+ let output = process.wait_with_output().map_err(|err| err.to_string())?;
+ if output.status.success() {
+ Ok(string_from_utf8(output.stdout))
+ } else {
+ Err(string_from_utf8(output.stderr))
+ }
+ }
+
+ /// Builds and executes an `hdwallet` command.
+ pub fn run(subcommand: &str, args: &[&str]) -> String {
+ Hdwallet::new(subcommand, args).execute().unwrap()
}
}
fn string_from_utf8(bytes: Vec<u8>) -> String {
str::from_utf8(&bytes).unwrap().trim().to_string()
-} \ No newline at end of file
+}