Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Mar 23, 2018
1 parent a019682 commit b5b4ec1
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 60 deletions.
60 changes: 5 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions whisper/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ docopt = "0.8"
serde = "1.0"
serde_derive = "1.0"
panic_hook = { path = "../../util/panic_hook" }
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
jsonrpc-pubsub = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" }
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.10" }
jsonrpc-pubsub = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.10" }
jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.10" }

[dev-dependencies]
ethcore-devtools = { path = "../../devtools" }
serde_json = "1.0"

[[bin]]
name = "whisper-cli"
Expand Down
232 changes: 230 additions & 2 deletions whisper/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ extern crate jsonrpc_core;
extern crate jsonrpc_pubsub;
extern crate jsonrpc_http_server;

#[cfg(test)]
extern crate ethcore_devtools;

#[cfg(test)]
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

Expand Down Expand Up @@ -228,9 +234,231 @@ fn execute<S, I>(command: I) -> Result<(), Error> where I: IntoIterator<Item=S>,

#[cfg(test)]
mod test {
use super::execute;
use ethcore_devtools::http_client;
use std::thread;
use std::net::SocketAddr;
use serde_json;
use whisper::rpc::crypto;


const UNIQUE_ID_SIZE_HEXSTRING: usize = 66;
const PUBLIC_KEY_SIZE_HEXSTRING: usize = 130;
const PRIVATE_KEY_SIZE_HEXSTRING: usize = 66;
const SYMMETRIC_KEY_SIZE_HEXSTRING: usize = 66;

#[derive(Debug, Deserialize)]
struct JsonRpcResponse {
id: usize,
result: serde_json::Value,
jsonrpc: String,
}

fn request<'a>(address: &SocketAddr, request: &'a str) -> http_client::Response {
http_client::request(
address,
&format!("\
POST / HTTP/1.1\r\n\
Host: {}\r\n\
Content-Type: application/json\r\n\
Content-Length: {}\r\n\
Connection: close\r\n\
\r\n\
{}",
address, request.len(), request)
)
}

fn parse_json<'a>(body: &'a str) -> JsonRpcResponse {
let filter: String = body
.split_whitespace()
.filter(|c| c.starts_with("{"))
.collect();
serde_json::from_str(&filter).unwrap()
}

#[test]
fn generate_keypair_and_post_message() {
let address = &"127.0.0.1:8545".parse().unwrap();

thread::spawn(move || {
let command = vec!["whisper-cli", "-p", "8545"]
.into_iter()
.map(Into::into)
.collect::<Vec<String>>();

execute(command).unwrap();
});

let unique_id = {
let req = r#"{
"method":"shh_newKeyPair",
"params":[],
"jsonrpc":"2.0",
"id":1
}"#;
println!("req: {}", req);
let response = request(address, req);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(unique_id.as_str().unwrap().len(), UNIQUE_ID_SIZE_HEXSTRING);

let post_message = {
let req = r#"{
"method":"shh_post",
"params":[{
"from":"#.to_owned() + format!("{}", unique_id).as_ref() + r#",
"topics":["0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"],
"payload":"0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6",
"priority":40,
"ttl":400
}],
"jsonrpc":"2.0",
"id": 2
}"#;
let response = request(address, &req);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(post_message, true);
}

#[test]
fn generate_keypair_and_delete() {
let address = &"127.0.0.1:8546".parse().unwrap();

thread::spawn(move || {
let command = vec!["whisper-cli", "-p", "8546"]
.into_iter()
.map(Into::into)
.collect::<Vec<String>>();

execute(command).unwrap();
});


let unique_id = {
let req = r#"{
"method":"shh_newKeyPair",
"params":[],
"jsonrpc":"2.0",
"id":1
}"#;
let response = request(address, req);
let response_json = parse_json(&response.body);
response_json.result
};
assert_eq!(unique_id.as_str().unwrap().len(), UNIQUE_ID_SIZE_HEXSTRING);

let public_key = {
let req = r#"{
"method": "shh_getPublicKey",
"params": ["#.to_owned() + format!("{}", unique_id).as_ref() + r#"],
"jsonrpc": "2.0",
"id": 2
}"#;
let response = request(address, &req);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(public_key.as_str().unwrap().len(), PUBLIC_KEY_SIZE_HEXSTRING);

let private_key = {
let req = r#"{
"method": "shh_getPrivateKey",
"params": ["#.to_owned() + format!("{}", unique_id).as_ref() + r#"],
"jsonrpc": "2.0",
"id": 2
}"#;
let response = request(address, &req);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(private_key.as_str().unwrap().len(), PRIVATE_KEY_SIZE_HEXSTRING);
println!("private_key: {}", private_key);

let is_deleted = {
let req = r#"{
"method": "shh_deleteKey",
"params": ["#.to_owned() + format!("{}", unique_id).as_ref() + r#"],
"jsonrpc": "2.0",
"id": 2
}"#;
let response = request(address, &req);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(is_deleted, true);
}

#[test]
fn generate_symmetric_key_and_delete() {
let address = &"127.0.0.1:8547".parse().unwrap();

thread::spawn(move || {
let command = vec!["whisper-cli", "-p", "8547"]
.into_iter()
.map(Into::into)
.collect::<Vec<String>>();

execute(command).unwrap();
});


let unique_id = {
let req = r#"{
"method":"shh_newSymKey",
"params":[],
"jsonrpc":"2.0",
"id":1
}"#;
println!("req: {:?}", req);
let response = request(address, req);
println!("res: {:?}", response.body);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(unique_id.as_str().unwrap().len(), UNIQUE_ID_SIZE_HEXSTRING);

let key = {
let req = r#"{
"method":"shh_getSymKey",
"params":["#.to_owned() + &format!("{}", unique_id) + r#"],
"jsonrpc":"2.0",
"id": 2
}"#;
let response = request(address, &req);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(key.as_str().unwrap().len(), SYMMETRIC_KEY_SIZE_HEXSTRING);

let is_deleted = {
let req = r#"{
"method": "shh_deleteKey",
"params": ["#.to_owned() + format!("{}", unique_id).as_ref() + r#"],
"jsonrpc": "2.0",
"id": 2
}"#;
let response = request(address, &req);
let response_json = parse_json(&response.body);
response_json.result
};

assert_eq!(is_deleted, true);
}

#[test]
fn message_filter() {


fn test_rpcs() {
assert!(true);
}
}

0 comments on commit b5b4ec1

Please sign in to comment.