openethereum/ipc/tests/examples.rs

174 lines
4.6 KiB
Rust
Raw Permalink Normal View History

// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2016-03-24 22:07:01 +01:00
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
#[cfg(test)]
mod tests {
use super::super::service::*;
2016-04-21 22:03:05 +02:00
use super::super::binary::*;
use super::super::nested::{DBClient, DBWriter};
2016-03-24 22:07:01 +01:00
use ipc::*;
2016-03-26 17:04:12 +01:00
use devtools::*;
2016-04-07 22:18:48 +02:00
use semver::Version;
use std::sync::Arc;
2016-03-24 22:07:01 +01:00
#[test]
fn call_service() {
// method_num = 0, f = 10 (method Service::commit)
2016-04-23 17:15:50 +02:00
let mut socket = TestSocket::new_ready(vec![
0, 16,
0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 0, 0, 0, 0,
10, 0, 0, 0]);
2016-03-24 22:07:01 +01:00
let service = Arc::new(Service::new());
2016-03-24 22:07:01 +01:00
assert_eq!(0, *service.commits.read().unwrap());
service.dispatch(&mut socket);
assert_eq!(10, *service.commits.read().unwrap());
}
#[test]
2016-04-12 06:13:31 +02:00
fn call_service_client() {
let mut socket = TestSocket::new();
2016-04-23 17:15:50 +02:00
socket.read_buffer = vec![10, 0, 0, 0];
2016-04-12 09:18:39 +02:00
let service_client = ServiceClient::init(socket);
2016-03-30 17:19:03 +02:00
let result = service_client.commit(5);
2016-04-23 17:15:50 +02:00
assert_eq!(
vec![0, 16,
0, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0],
2016-06-30 15:23:10 +02:00
service_client.socket().write().unwrap().write_buffer.clone());
assert_eq!(10, result);
}
2016-03-30 01:20:01 +02:00
#[test]
2016-04-12 06:13:31 +02:00
fn call_service_client_optional() {
2016-03-30 01:20:01 +02:00
let mut socket = TestSocket::new();
2016-04-23 17:15:50 +02:00
socket.read_buffer = vec![10, 0, 0, 0];
2016-04-12 09:18:39 +02:00
let service_client = ServiceClient::init(socket);
2016-03-30 01:20:01 +02:00
2016-03-30 17:19:03 +02:00
let result = service_client.rollback(Some(5), 10);
2016-03-30 01:20:01 +02:00
2016-04-23 17:15:50 +02:00
assert_eq!(vec![
0, 17,
1, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 0, 0, 0, 0,
8, 0, 0, 0, 0, 0, 0, 0,
2016-06-30 15:23:10 +02:00
5, 0, 0, 0, 10, 0, 0, 0], service_client.socket().write().unwrap().write_buffer.clone());
2016-03-30 01:20:01 +02:00
assert_eq!(10, result);
}
2016-04-07 22:18:48 +02:00
#[test]
fn query_default_version() {
let ver = Service::protocol_version();
2016-04-07 22:18:48 +02:00
assert_eq!(ver, Version::parse("1.0.0").unwrap());
let ver = Service::api_version();
2016-04-07 22:18:48 +02:00
assert_eq!(ver, Version::parse("1.0.0").unwrap());
}
2016-04-12 06:13:31 +02:00
#[test]
fn call_service_client_handshake() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![1];
2016-04-12 09:18:39 +02:00
let service_client = ServiceClient::init(socket);
2016-04-12 06:13:31 +02:00
let result = service_client.handshake();
assert!(result.is_ok());
}
2016-04-13 12:46:49 +02:00
#[test]
fn can_use_custom_params() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![1];
let service_client = ServiceClient::init(socket);
let result = service_client.push_custom(CustomData { a: 3, b: 11});
assert_eq!(vec![
// message num..
0, 18,
2016-04-23 17:15:50 +02:00
// variable size length-s
1, 0, 0, 0, 0, 0, 0, 0,
16, 0, 0, 0, 0, 0, 0, 0,
// total length
16, 0, 0, 0, 0, 0, 0, 0,
// items
2016-04-13 12:46:49 +02:00
3, 0, 0, 0, 0, 0, 0, 0,
11, 0, 0, 0, 0, 0, 0, 0],
2016-06-30 15:23:10 +02:00
service_client.socket().write().unwrap().write_buffer.clone());
2016-04-13 12:46:49 +02:00
assert_eq!(true, result);
}
2016-04-15 15:16:58 +02:00
#[test]
fn can_invoke_generic_service() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
2016-04-15 15:16:58 +02:00
let db_client = DBClient::<u64, _>::init(socket);
let result = db_client.write(vec![1u8; 1]);
assert_eq!(vec![0, 16, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
db_client.socket().write().unwrap().write_buffer.clone());
2016-04-15 15:16:58 +02:00
assert!(result.is_ok());
}
2016-04-23 17:15:50 +02:00
2016-04-15 15:16:58 +02:00
#[test]
fn can_handshake_generic_service() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![1];
let db_client = DBClient::<u64, _>::init(socket);
let result = db_client.handshake();
assert!(result.is_ok());
}
2016-04-21 22:03:05 +02:00
#[test]
fn can_serialize_dummy_structs() {
let mut socket = TestSocket::new();
let struct_ = DoubleRoot { x1: 0, x2: 100, x3: 100000};
let res = ::ipc::binary::serialize_into(&struct_, &mut socket);
assert!(res.is_ok());
let mut read_socket = TestSocket::new_ready(socket.write_buffer.clone());
let new_struct: DoubleRoot = ::ipc::binary::deserialize_from(&mut read_socket).unwrap();
assert_eq!(struct_, new_struct);
}
2016-07-05 10:11:09 +02:00
#[test]
fn can_call_void_method() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![1];
let service_client = ServiceClient::init(socket);
service_client.void(99);
assert_eq!(vec![
0, 19,
0, 0, 0, 0, 0, 0, 0, 0,
8, 0, 0, 0, 0, 0, 0, 0,
99, 0, 0, 0, 0, 0, 0, 0],
service_client.socket().write().unwrap().write_buffer.clone());
}
2016-03-24 22:07:01 +01:00
}