| 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 fuzzingclient |
| 14 | // |
| 15 | // See https://github.com/crossbario/autobahn-testsuite for details. |
| 16 | |
| 17 | use futuresrust-analyzer cargo futures 0.3.34 crate/::iorust-analyzer cargo futures-util 0.3.34 io/::{BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#, BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#}; |
| 18 | use sokettorust-analyzer cargo soketto 0.8.1 crate/::{connectionrust-analyzer cargo soketto 0.8.1 connection/, handshakerust-analyzer cargo soketto 0.8.1 handshake/, BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#}; |
| 19 | use tokiorust-analyzer cargo tokio 1.53.1 crate/::netrust-analyzer cargo tokio 1.53.1 net/::{TcpListenerrust-analyzer cargo tokio 1.53.1 net/tcp/listener/TcpListener#, TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#}; |
| 20 | use tokio_streamrust-analyzer cargo tokio-stream 0.1.19 crate/::{wrappersrust-analyzer cargo tokio-stream 0.1.19 wrappers/::TcpListenerStreamrust-analyzer cargo tokio-stream 0.1.19 wrappers/tcp_listener/TcpListenerStream#, StreamExtrust-analyzer cargo tokio-stream 0.1.19 stream_ext/StreamExt#}; |
| 21 | use tokio_utilrust-analyzer cargo tokio-util 0.7.19 crate/::compatrust-analyzer cargo tokio-util 0.7.19 compat/::{Compatrust-analyzer cargo tokio-util 0.7.19 compat/Compat#, TokioAsyncReadCompatExtrust-analyzer cargo tokio-util 0.7.19 compat/TokioAsyncReadCompatExt#}; |
| 22 | #[tokiorust-analyzer cargo tokio 1.53.1 crate/::mainrust-analyzer cargo tokio-macros 2.7.2 main:] |
| 23 | async fn mainrust-analyzer cargo soketto 0.8.1 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), BoxedErrorrust-analyzer cargo soketto 0.8.1 BoxedError#> { |
| 24 | let listener = TcpListenerrust-analyzer cargo tokio 1.53.1 net/tcp/listener/TcpListener#::bindrust-analyzer cargo tokio 1.53.1 net/tcp/listener/impl#[TcpListener]bind().("127.0.0.1:9001").rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 25 | let mut incoming = TcpListenerStreamrust-analyzer cargo tokio-stream 0.1.19 wrappers/tcp_listener/TcpListenerStream#::newrust-analyzer cargo tokio-stream 0.1.19 wrappers/tcp_listener/impl#[TcpListenerStream]new().(listener); |
| 26 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(socket) = incoming.nextrust-analyzer cargo tokio-stream 0.1.19 stream_ext/StreamExt#next().().rust-analyzer cargo tokio-stream 0.1.19 stream_ext/next/impl#[`Next<'_, St>`][Future]poll().awaitrust-analyzer cargo tokio-stream 0.1.19 stream_ext/next/impl#[`Next<'_, St>`][Future]poll(). { |
| 27 | let mut server = new_serverrust-analyzer cargo soketto 0.8.1 new_server().(socket?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 28 | let key = { |
| 29 | let req = server.receive_requestrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]receive_request().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 30 | req.keyrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`ClientRequest<'a>`]key().() |
| 31 | }; |
| 32 | let accept = handshakerust-analyzer cargo soketto 0.8.1 handshake/::serverrust-analyzer cargo soketto 0.8.1 handshake/server/::Responserust-analyzer cargo soketto 0.8.1 handshake/server/Response#::Acceptrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept# { keyrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept#key., protocolrust-analyzer cargo soketto 0.8.1 handshake/server/Response#Accept#protocol.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# }; |
| 33 | server.send_responserust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]send_response().(&accept).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 34 | let (mut sender, mut receiver) = server.into_builderrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]into_builder().().finishrust-analyzer cargo soketto 0.8.1 connection/impl#[`Builder<T>`]finish().(); |
| 35 | let mut message = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 36 | loop { |
| 37 | message.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]clear().(); |
| 38 | match receiver.receive_datarust-analyzer cargo soketto 0.8.1 connection/impl#[`Receiver<T>`]receive_data().(&mut message).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll(). { |
| 39 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sokettorust-analyzer cargo soketto 0.8.1 crate/::Datarust-analyzer cargo soketto 0.8.1 data/Data#::Binaryrust-analyzer cargo soketto 0.8.1 data/Data#Binary#(n)) => { |
| 40 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(n, message.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 41 | sender.send_binary_mutrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]send_binary_mut().(&mut message).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 42 | sender.flushrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]flush().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 43 | } |
| 44 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(sokettorust-analyzer cargo soketto 0.8.1 crate/::Datarust-analyzer cargo soketto 0.8.1 data/Data#::Textrust-analyzer cargo soketto 0.8.1 data/Data#Text#(n)) => { |
| 45 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(n, message.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 46 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(txt) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&message) { |
| 47 | sender.send_textrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]send_text().(txt).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 48 | sender.flushrust-analyzer cargo soketto 0.8.1 connection/impl#[`Sender<T>`]flush().().rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 49 | } else { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(connectionrust-analyzer cargo soketto 0.8.1 connection/::Errorrust-analyzer cargo soketto 0.8.1 connection/Error#::Closedrust-analyzer cargo soketto 0.8.1 connection/Error#Closed#) => break, |
| 54 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => { |
| 55 | logrust-analyzer cargo log 0.4.34 crate/::errorrust-analyzer cargo log 0.4.34 macros/error!!("connection error: {}", e); |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 62 | } |
| 63 | |
| 64 | #[cfg(not(feature = "deflate"))] |
| 65 | fn new_server<'a>(socket: TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#) -> handshakerust-analyzer cargo soketto 0.8.1 handshake/::Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server#<'a, BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#<BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#<Compatrust-analyzer cargo tokio-util 0.7.19 compat/Compat#<TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#>>>> { |
| 66 | handshakerust-analyzer cargo soketto 0.8.1 handshake/::Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server#::new(BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#::new(BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#::new(socket.compat()))) |
| 67 | } |
| 68 | |
| 69 | #[cfg(feature = "deflate")] |
| 70 | fn new_serverrust-analyzer cargo soketto 0.8.1 new_server().<'a>(socket: TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#) -> handshakerust-analyzer cargo soketto 0.8.1 handshake/::Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server#<'a, BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#<BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#<Compatrust-analyzer cargo tokio-util 0.7.19 compat/Compat#<TcpStreamrust-analyzer cargo tokio 1.53.1 net/tcp/stream/TcpStream#>>>> { |
| 71 | let socket = BufReaderrust-analyzer cargo futures-util 0.3.34 io/buf_reader/BufReader#::with_capacityrust-analyzer cargo futures-util 0.3.34 io/buf_reader/impl#[`BufReader<R>`]with_capacity().(8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024, BufWriterrust-analyzer cargo futures-util 0.3.34 io/buf_writer/BufWriter#::with_capacityrust-analyzer cargo futures-util 0.3.34 io/buf_writer/impl#[`BufWriter<W>`]with_capacity().(16 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().1024, socket.compatrust-analyzer cargo tokio-util 0.7.19 compat/TokioAsyncReadCompatExt#compat().())); |
| 72 | let mut server = handshakerust-analyzer cargo soketto 0.8.1 handshake/::Serverrust-analyzer cargo soketto 0.8.1 handshake/server/Server#::newrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]new().(socket); |
| 73 | let deflate = sokettorust-analyzer cargo soketto 0.8.1 crate/::extensionrust-analyzer cargo soketto 0.8.1 extension/::deflaterust-analyzer cargo soketto 0.8.1 extension/deflate/::Deflaterust-analyzer cargo soketto 0.8.1 extension/deflate/Deflate#::newrust-analyzer cargo soketto 0.8.1 extension/deflate/impl#[Deflate]new().(sokettorust-analyzer cargo soketto 0.8.1 crate/::Moderust-analyzer cargo soketto 0.8.1 connection/Mode#::Serverrust-analyzer cargo soketto 0.8.1 connection/Mode#Server#); |
| 74 | server.add_extensionrust-analyzer cargo soketto 0.8.1 handshake/server/impl#[`Server<'a, T>`]add_extension().(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(deflate)); |
| 75 | server |
| 76 | } |