2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-09-01 10:16:04 +02: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/>.
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
//! WebSockets server tests.
|
|
|
|
|
2016-09-01 10:16:04 +02:00
|
|
|
use std::sync::Arc;
|
2016-10-17 15:53:33 +02:00
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
use jsonrpc_core::MetaIoHandler;
|
|
|
|
use ws;
|
2016-09-01 10:16:04 +02:00
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
use v1::{extractors, informant};
|
|
|
|
use tests::helpers::{GuardedAuthCodes, Server};
|
2018-12-28 10:33:49 +01:00
|
|
|
use tests::http_client;
|
2017-01-11 20:02:27 +01:00
|
|
|
|
|
|
|
/// Setup a mock signer for tests
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn serve() -> (Server<ws::Server>, usize, GuardedAuthCodes) {
|
2018-11-11 11:20:04 +01:00
|
|
|
let address = "127.0.0.1:0".parse().unwrap();
|
2017-05-24 12:24:07 +02:00
|
|
|
let io = MetaIoHandler::default();
|
|
|
|
let authcodes = GuardedAuthCodes::new();
|
|
|
|
let stats = Arc::new(informant::RpcStats::default());
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
let res = Server::new(|_| ::start_ws(
|
2017-05-24 12:24:07 +02:00
|
|
|
&address,
|
|
|
|
io,
|
|
|
|
ws::DomainsValidation::Disabled,
|
|
|
|
ws::DomainsValidation::Disabled,
|
2018-04-02 12:33:09 +02:00
|
|
|
5,
|
2017-05-24 12:24:07 +02:00
|
|
|
extractors::WsExtractor::new(Some(&authcodes.path)),
|
|
|
|
extractors::WsExtractor::new(Some(&authcodes.path)),
|
|
|
|
extractors::WsStats::new(stats),
|
|
|
|
).unwrap());
|
2018-11-11 11:20:04 +01:00
|
|
|
let port = res.addr().port() as usize;
|
2017-05-24 12:24:07 +02:00
|
|
|
|
|
|
|
(res, port, authcodes)
|
2016-09-01 10:16:04 +02:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:18:48 +01:00
|
|
|
/// Test a single request to running server
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn request(server: Server<ws::Server>, request: &str) -> http_client::Response {
|
2017-01-11 20:02:27 +01:00
|
|
|
http_client::request(server.server.addr(), request)
|
2016-10-22 15:21:41 +02:00
|
|
|
}
|
|
|
|
|
2016-12-14 14:18:48 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod testing {
|
|
|
|
use std::time;
|
2017-08-31 11:35:41 +02:00
|
|
|
use hash::keccak;
|
2018-12-28 10:33:49 +01:00
|
|
|
use super::{serve, request, http_client};
|
2016-12-14 14:18:48 +01:00
|
|
|
|
2017-02-04 09:52:14 +01:00
|
|
|
#[test]
|
|
|
|
fn should_not_redirect_to_parity_host() {
|
|
|
|
// given
|
|
|
|
let (server, port, _) = serve();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let response = request(server,
|
|
|
|
&format!("\
|
|
|
|
GET / HTTP/1.1\r\n\
|
|
|
|
Host: 127.0.0.1:{}\r\n\
|
|
|
|
Connection: close\r\n\
|
|
|
|
\r\n\
|
|
|
|
{{}}
|
|
|
|
", port)
|
|
|
|
);
|
|
|
|
|
|
|
|
// then
|
2017-05-24 12:24:07 +02:00
|
|
|
assert_eq!(response.status, "HTTP/1.1 200 Ok".to_owned());
|
2016-12-14 14:18:48 +01:00
|
|
|
}
|
2016-09-21 12:44:49 +02:00
|
|
|
|
2016-12-14 14:18:48 +01:00
|
|
|
#[test]
|
|
|
|
fn should_block_if_authorization_is_incorrect() {
|
|
|
|
// given
|
|
|
|
let (server, port, _) = serve();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let response = request(server,
|
|
|
|
&format!("\
|
|
|
|
GET / HTTP/1.1\r\n\
|
|
|
|
Host: 127.0.0.1:{}\r\n\
|
|
|
|
Connection: Upgrade\r\n\
|
|
|
|
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
|
|
|
Sec-WebSocket-Protocol: wrong\r\n\
|
|
|
|
Sec-WebSocket-Version: 13\r\n\
|
|
|
|
\r\n\
|
|
|
|
{{}}
|
|
|
|
", port)
|
|
|
|
);
|
|
|
|
|
|
|
|
// then
|
2017-05-24 12:24:07 +02:00
|
|
|
assert_eq!(response.status, "HTTP/1.1 403 Forbidden".to_owned());
|
2016-12-14 14:18:48 +01:00
|
|
|
http_client::assert_security_headers_present(&response.headers, None);
|
|
|
|
}
|
2016-09-21 12:44:49 +02:00
|
|
|
|
2018-10-31 11:50:38 +01:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2016-12-14 14:18:48 +01:00
|
|
|
#[test]
|
|
|
|
fn should_allow_if_authorization_is_correct() {
|
|
|
|
// given
|
|
|
|
let (server, port, mut authcodes) = serve();
|
|
|
|
let code = authcodes.generate_new().unwrap().replace("-", "");
|
|
|
|
authcodes.to_file(&authcodes.path).unwrap();
|
|
|
|
let timestamp = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
|
|
|
|
|
|
|
// when
|
|
|
|
let response = request(server,
|
|
|
|
&format!("\
|
|
|
|
GET / HTTP/1.1\r\n\
|
|
|
|
Host: 127.0.0.1:{}\r\n\
|
|
|
|
Connection: Close\r\n\
|
|
|
|
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
2018-04-02 13:12:52 +02:00
|
|
|
Sec-WebSocket-Protocol: {:x}_{}\r\n\
|
2016-12-14 14:18:48 +01:00
|
|
|
Sec-WebSocket-Version: 13\r\n\
|
|
|
|
\r\n\
|
|
|
|
{{}}
|
|
|
|
",
|
|
|
|
port,
|
2017-08-31 11:35:41 +02:00
|
|
|
keccak(format!("{}:{}", code, timestamp)),
|
2016-12-14 14:18:48 +01:00
|
|
|
timestamp,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(response.status, "HTTP/1.1 101 Switching Protocols".to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-13 10:58:52 +02:00
|
|
|
fn should_not_allow_initial_connection_even_once() {
|
2016-12-14 14:18:48 +01:00
|
|
|
// given
|
|
|
|
let (server, port, authcodes) = serve();
|
|
|
|
let code = "initial";
|
|
|
|
let timestamp = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
|
|
|
assert!(authcodes.is_empty());
|
|
|
|
|
|
|
|
// when
|
|
|
|
let response1 = http_client::request(server.addr(),
|
|
|
|
&format!("\
|
|
|
|
GET / HTTP/1.1\r\n\
|
|
|
|
Host: 127.0.0.1:{}\r\n\
|
|
|
|
Connection: Close\r\n\
|
|
|
|
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n\
|
2018-04-02 13:12:52 +02:00
|
|
|
Sec-WebSocket-Protocol:{:x}_{}\r\n\
|
2016-12-14 14:18:48 +01:00
|
|
|
Sec-WebSocket-Version: 13\r\n\
|
|
|
|
\r\n\
|
|
|
|
{{}}
|
|
|
|
",
|
|
|
|
port,
|
2017-08-31 11:35:41 +02:00
|
|
|
keccak(format!("{}:{}", code, timestamp)),
|
2016-12-14 14:18:48 +01:00
|
|
|
timestamp,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// then
|
2018-09-13 10:58:52 +02:00
|
|
|
assert_eq!(response1.status, "HTTP/1.1 403 Forbidden".to_owned());
|
|
|
|
http_client::assert_security_headers_present(&response1.headers, None);
|
2016-12-14 14:18:48 +01:00
|
|
|
}
|
2016-09-21 12:44:49 +02:00
|
|
|
}
|