diff --git a/whisper/cli/src/main.rs b/whisper/cli/src/main.rs index 2f243d98665..67a63a95951 100644 --- a/whisper/cli/src/main.rs +++ b/whisper/cli/src/main.rs @@ -193,7 +193,6 @@ fn execute(command: I) -> Result<(), Error> where I: IntoIterator, let url = format!("{}:{}", args.flag_address, args.flag_port); // Filter manager that will dispatch `decryption tasks` - // This provides the `Whisper` trait with all rpcs methods let manager = Arc::new(whisper::rpc::FilterManager::new()?); // Whisper protocol network handler @@ -231,234 +230,3 @@ fn execute(command: I) -> Result<(), Error> where I: IntoIterator, // This will never return Ok(()) } - -#[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::>(); - - 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::>(); - - 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::>(); - - 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() { - - - } -}