| 1 | // Copyright (c) 2019 Parity Technologies (UK) Ltd. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 |
| 4 | // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT |
| 5 | // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. All files in the project carrying such notice may not be copied, |
| 7 | // modified, or distributed except according to those terms. |
| 8 | |
| 9 | // Example to be used with the autobahn test suite, a fully automated test |
| 10 | // suite to verify client and server implementations of websocket |
| 11 | // implementation. |
| 12 | // |
| 13 | // Once started, the tests can be executed with: wstest -m fuzzingserver |
| 14 | // |
| 15 | // See https://github.com/crossbario/autobahn-testsuite for details. |
| 16 | |
| 17 | use futures::io::{BufReader, BufWriter}; |
| 18 | use soketto::{connection, handshake, BoxedError}; |
| 19 | use std::str::FromStr; |
| 20 | use tokio::net::TcpStream; |
| 21 | use tokio_util::compat::{Compat, TokioAsyncReadCompatExt}; |
| 22 | |
| 23 | const SOKETTO_VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 24 | |
| 25 | #[tokio::main] |
| 26 | async fn main() -> Result<(), BoxedError> { |
| 27 | let n = num_of_cases().await?; |
| 28 | for i in 1..=n { |
| 29 | if let Err(e) = run_case(i).await { |
| 30 | log::error!("case {}: {:?}", i, e) |
| 31 | } |
| 32 | } |
| 33 | update_report().await?; |
| 34 | Ok(()) |
| 35 | } |
| 36 | |
| 37 | async fn num_of_cases() -> Result<usize, BoxedError> { |
| 38 | let socket = TcpStream::connect("127.0.0.1:9001").await?; |
| 39 | let mut client = new_client(socket, "/getCaseCount"); |
| 40 | assert!(matches!(client.handshake().await?, handshake::ServerResponse::Accepted { .. })); |
| 41 | let (_, mut receiver) = client.into_builder().finish(); |
| 42 | let mut data = Vec::new(); |
| 43 | let kind = receiver.receive_data(&mut data).await?; |
| 44 | assert!(kind.is_text()); |
| 45 | let num = usize::from_str(std::str::from_utf8(&data)?)?; |
| 46 | log::info!("{} cases to run", num); |
| 47 | Ok(num) |
| 48 | } |
| 49 | |
| 50 | async fn run_case(n: usize) -> Result<(), BoxedError> { |
| 51 | log::info!("running case {}", n); |
| 52 | let resource = format!("/runCase?case={}&agent=soketto-{}", n, SOKETTO_VERSION); |
| 53 | let socket = TcpStream::connect("127.0.0.1:9001").await?; |
| 54 | let mut client = new_client(socket, &resource); |
| 55 | assert!(matches!(client.handshake().await?, handshake::ServerResponse::Accepted { .. })); |
| 56 | let (mut sender, mut receiver) = client.into_builder().finish(); |
| 57 | let mut message = Vec::new(); |
| 58 | loop { |
| 59 | message.clear(); |
| 60 | match receiver.receive_data(&mut message).await { |
| 61 | Ok(soketto::Data::Binary(n)) => { |
| 62 | assert_eq!(n, message.len()); |
| 63 | sender.send_binary_mut(&mut message).await?; |
| 64 | sender.flush().await? |
| 65 | } |
| 66 | Ok(soketto::Data::Text(n)) => { |
| 67 | assert_eq!(n, message.len()); |
| 68 | sender.send_text(std::str::from_utf8(&message)?).await?; |
| 69 | sender.flush().await? |
| 70 | } |
| 71 | Err(connection::Error::Closed) => return Ok(()), |
| 72 | Err(e) => return Err(e.into()), |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | async fn update_report() -> Result<(), BoxedError> { |
| 78 | log::info!("requesting report generation"); |
| 79 | let resource = format!("/updateReports?agent=soketto-{}", SOKETTO_VERSION); |
| 80 | let socket = TcpStream::connect("127.0.0.1:9001").await?; |
| 81 | let mut client = new_client(socket, &resource); |
| 82 | assert!(matches!(client.handshake().await?, handshake::ServerResponse::Accepted { .. })); |
| 83 | client.into_builder().finish().0.close().await?; |
| 84 | Ok(()) |
| 85 | } |
| 86 | |
| 87 | #[cfg(not(feature = "deflate"))] |
| 88 | fn new_client(socket: TcpStream, path: &str) -> handshake::Client<'_, BufReader<BufWriter<Compat<TcpStream>>>> { |
| 89 | handshake::Client::new(BufReader::new(BufWriter::new(socket.compat())), "127.0.0.1:9001", path) |
| 90 | } |
| 91 | |
| 92 | #[cfg(feature = "deflate")] |
| 93 | fn new_client(socket: TcpStream, path: &str) -> handshake::Client<'_, BufReader<BufWriter<Compat<TcpStream>>>> { |
| 94 | let socket = BufReader::with_capacity(8 * 1024, BufWriter::with_capacity(64 * 1024, socket.compat())); |
| 95 | let mut client = handshake::Client::new(socket, "127.0.0.1:9001", path); |
| 96 | let deflate = soketto::extension::deflate::Deflate::new(soketto::Mode::Client); |
| 97 | client.add_extension(Box::new(deflate)); |
| 98 | client |
| 99 | } |