openethereum/ipc/tests/examples.rs

105 lines
2.9 KiB
Rust
Raw Normal View History

2016-03-24 22:07:01 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// 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::*;
use ipc::*;
2016-03-26 17:04:12 +01:00
use devtools::*;
2016-04-07 22:18:48 +02:00
use semver::Version;
2016-03-24 22:07:01 +01:00
#[test]
fn call_service() {
// method_num = 0, f = 10 (method Service::commit)
let mut socket = TestSocket::new_ready(vec![0, 16, 0, 0, 0, 10]);
2016-03-24 22:07:01 +01:00
let service = Service::new();
assert_eq!(0, *service.commits.read().unwrap());
service.dispatch(&mut socket);
assert_eq!(10, *service.commits.read().unwrap());
}
2016-04-12 05:43:45 +02:00
#[test]
fn call_service_handshake() {
let mut socket = TestSocket::new_ready(vec![0, 0,
// protocol version
0, 0, 0, 0, 0, 0, 0, 5, b'1', b'.', b'0', b'.', b'0',
// api version
0, 0, 0, 0, 0, 0, 0, 5, b'1', b'.', b'0', b'.', b'0',
// reserved
0, 0, 0, 0, 0, 0, 0, 64,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);
let service = Service::new();
let result = service.dispatch(&mut socket);
// single `true`
assert_eq!(vec![1], result);
}
#[test]
2016-04-12 06:13:31 +02:00
fn call_service_client() {
let mut socket = TestSocket::new();
socket.read_buffer = vec![0, 0, 0, 10];
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);
assert_eq!(vec![0, 16, 0, 0, 0, 5], service_client.socket().borrow().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();
socket.read_buffer = vec![0, 0, 0, 10];
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
assert_eq!(vec![0, 17, 1, 0, 0, 0, 5, 0, 0, 0, 10], service_client.socket().borrow().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();
assert_eq!(ver, Version::parse("1.0.0").unwrap());
let ver = Service::api_version();
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-03-24 22:07:01 +01:00
}