oxedyne/fe2o3/fe2o3_shield/tests/common/mod.rs
6.5 KiB, 1 run
created by r656224696:77722, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The two-peer loopback harness shared by the wire tests: real chunking, proof of work, |
| 2 | //! signatures, guards and assembly, with nothing stubbed. |
| 3 | #![allow(dead_code)] |
| 4 | |
| 5 | use oxedyne_fe2o3_shield::srv::{ |
| 6 | cfg::ServerConfig, |
| 7 | client::Client, |
| 8 | constant, |
| 9 | context::ServerContext, |
| 10 | msg::{ |
| 11 | app::{ |
| 12 | Answer, |
| 13 | AppMsgKind, |
| 14 | }, |
| 15 | protocol::{ |
| 16 | DefaultProtocolTypes, |
| 17 | Protocol, |
| 18 | ProtocolMode, |
| 19 | }, |
| 20 | syntax as srv_syntax, |
| 21 | }, |
| 22 | schemes::WireSchemesInput, |
| 23 | server::Server, |
| 24 | }; |
| 25 | |
| 26 | use oxedyne_fe2o3_core::{ |
| 27 | prelude::*, |
| 28 | alt::Alt, |
| 29 | path::NormalPath, |
| 30 | rand::RanDef, |
| 31 | }; |
| 32 | use oxedyne_fe2o3_crypto::{ |
| 33 | enc::EncryptionScheme, |
| 34 | sign::SignatureScheme, |
| 35 | }; |
| 36 | use oxedyne_fe2o3_hash::{ |
| 37 | csum::ChecksumScheme, |
| 38 | hash::HashScheme, |
| 39 | }; |
| 40 | use oxedyne_fe2o3_net::id; |
| 41 | use oxedyne_fe2o3_o3db_sync::O3db; |
| 42 | use oxedyne_fe2o3_syntax::SyntaxRef; |
| 43 | |
| 44 | use std::{ |
| 45 | net::SocketAddr, |
| 46 | path::Path, |
| 47 | time::Duration, |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | /// Length of the per-peer proof-of-work challenge code. |
| 52 | pub const CODE_LEN: usize = 8; |
| 53 | |
| 54 | /// Bytes per chunk. Small, so that a payload of a few kilobytes is genuinely |
| 55 | /// carried by several packets and reassembled rather than fitting in one. |
| 56 | pub const CHUNK_BYTES: u64 = 400; |
| 57 | |
| 58 | /// Proof-of-work difficulty, fixed: minimum and maximum are the same, so the |
| 59 | /// difficulty this peer demands does not move with the request rate. |
| 60 | /// |
| 61 | /// A difficulty that moves is only usable once a peer can be *told* the new |
| 62 | /// one, and telling it is the handshake response that is still deferred. Until |
| 63 | /// then, a fixed difficulty is the honest setting, and this test uses one |
| 64 | /// rather than pretending otherwise. |
| 65 | pub const POW_ZBITS: u16 = 2; |
| 66 | |
| 67 | /// How long a test waits for an answer before deciding none is coming. |
| 68 | pub const PATIENCE: Duration = Duration::from_secs(20); |
| 69 | |
| 70 | /// How long a test waits when it expects nothing. |
| 71 | pub const QUIET: Duration = Duration::from_millis(500); |
| 72 | |
| 73 | pub type Types = DefaultProtocolTypes<{ id::MID_LEN }, { id::SID_LEN }, { id::UID_LEN }>; |
| 74 | |
| 75 | pub type Proto = Protocol< |
| 76 | CODE_LEN, |
| 77 | { id::MID_LEN }, |
| 78 | { id::SID_LEN }, |
| 79 | { id::UID_LEN }, |
| 80 | Types, |
| 81 | >; |
| 82 | |
| 83 | /// The database type the server is parameterised by. No database is opened |
| 84 | /// here: the server needs the type to exist, not an instance of it. |
| 85 | pub type Db = O3db< |
| 86 | { id::UID_LEN }, |
| 87 | id::Uid, |
| 88 | EncryptionScheme, |
| 89 | HashScheme, |
| 90 | HashScheme, |
| 91 | ChecksumScheme, |
| 92 | >; |
| 93 | |
| 94 | pub type Srv = Server< |
| 95 | CODE_LEN, |
| 96 | { id::MID_LEN }, |
| 97 | { id::SID_LEN }, |
| 98 | { id::UID_LEN }, |
| 99 | Types, |
| 100 | EncryptionScheme, |
| 101 | HashScheme, |
| 102 | Db, |
| 103 | >; |
| 104 | |
| 105 | pub type Cli = Client< |
| 106 | CODE_LEN, |
| 107 | { id::MID_LEN }, |
| 108 | { id::SID_LEN }, |
| 109 | { id::UID_LEN }, |
| 110 | Types, |
| 111 | >; |
| 112 | |
| 113 | /// A server configuration that binds loopback on whatever port is free, and takes |
| 114 | /// peers it has not met: there is no handshake yet through which to meet them. |
| 115 | pub fn loopback_config() -> ServerConfig { |
| 116 | ServerConfig { |
| 117 | server_address: fmt!("127.0.0.1"), |
| 118 | server_port_udp: 0, |
| 119 | wire_chunk_bytes: CHUNK_BYTES, |
| 120 | wire_chunk_threshold: CHUNK_BYTES, |
| 121 | server_pow_zbits_min: POW_ZBITS, |
| 122 | server_pow_zbits_max: POW_ZBITS, |
| 123 | server_accept_unknown_users: true, |
| 124 | ..Default::default() |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// A protocol instance with its own signing key and identifier. |
| 129 | /// |
| 130 | /// Every peer signs with a key of its own, and carries the public half in each |
| 131 | /// packet: without a session there is nowhere else for the other side to have |
| 132 | /// got it from. |
| 133 | pub fn protocol(cfg: &ServerConfig, uid: u128) -> Outcome<Proto> { |
| 134 | Protocol::new( |
| 135 | cfg, |
| 136 | WireSchemesInput { |
| 137 | enc: Alt::Specific(None::<EncryptionScheme>), |
| 138 | csum: Alt::Specific(None::<ChecksumScheme>), |
| 139 | powh: Alt::Specific(ServerConfig::default_packet_pow_hash_scheme()), |
| 140 | sign: Alt::Specific(Some(SignatureScheme::new_ed25519())), |
| 141 | hsenc: Alt::Specific(None::<EncryptionScheme>), |
| 142 | chnk: Some(ServerConfig::new_chunk_cfg( |
| 143 | CHUNK_BYTES as usize, |
| 144 | CHUNK_BYTES as usize, |
| 145 | false, // Do not wrap each chunk in a daticle. |
| 146 | false, // Do not pad the last chunk. |
| 147 | )), |
| 148 | }, |
| 149 | [0u8; CODE_LEN], |
| 150 | id::Mid::default(), |
| 151 | id::Sid::default(), |
| 152 | id::Uid::new(uid), |
| 153 | ProtocolMode::Test, |
| 154 | ) |
| 155 | } |
| 156 | |
| 157 | /// Build a server on loopback, hand back the address it landed on, and leave it |
| 158 | /// running with the given handler. |
| 159 | pub async fn serve<H, F>(handler: H) -> Outcome<SocketAddr> |
| 160 | where |
| 161 | H: Fn(Vec<u8>, SocketAddr) -> F + Send + Sync + 'static, |
| 162 | F: std::future::Future<Output = Outcome<Answer>> + Send, |
| 163 | { |
| 164 | serve_with(loopback_config(), handler).await |
| 165 | } |
| 166 | |
| 167 | /// As `serve`, under the given configuration. |
| 168 | pub async fn serve_with<H, F>(cfg: ServerConfig, handler: H) -> Outcome<SocketAddr> |
| 169 | where |
| 170 | H: Fn(Vec<u8>, SocketAddr) -> F + Send + Sync + 'static, |
| 171 | F: std::future::Future<Output = Outcome<Answer>> + Send, |
| 172 | { |
| 173 | let proto = res!(protocol(&cfg, 1)); |
| 174 | let root = Path::new(".").normalise().absolute(); |
| 175 | let context = ServerContext::new(cfg, root, None::<(Db, id::Uid)>, proto); |
| 176 | let syntax = res!(srv_syntax::base_msg()); |
| 177 | let (mut server, _cmd_chan): (Srv, _) = Server::new(context, syntax); |
| 178 | let sock = res!(server.bind().await); |
| 179 | let addr = res!(sock.local_addr(), IO, Network); |
| 180 | tokio::spawn(async move { |
| 181 | if let Err(e) = server.run(sock, handler).await { |
| 182 | error!(e); |
| 183 | } |
| 184 | }); |
| 185 | Ok(addr) |
| 186 | } |
| 187 | |
| 188 | /// A client on loopback, on whatever port is free. |
| 189 | pub async fn dial(uid: u128) -> Outcome<(Cli, SyntaxRef)> { |
| 190 | let cfg = loopback_config(); |
| 191 | let proto = res!(protocol(&cfg, uid)); |
| 192 | let syntax = res!(srv_syntax::base_msg()); |
| 193 | let addr = res!("127.0.0.1:0".parse::<SocketAddr>(), Test); |
| 194 | let client = res!(Client::bind(addr, proto, syntax.clone()).await); |
| 195 | Ok((client, syntax)) |
| 196 | } |
| 197 | |
| 198 | /// A handler that answers with the payload it was given, reversed. |
| 199 | /// |
| 200 | /// Reversed rather than echoed, so that an answer which is somehow the |
| 201 | /// question coming back off a wire rather than out of the handler cannot pass. |
| 202 | pub async fn reverse(payload: Vec<u8>, _src_addr: SocketAddr) -> Outcome<Answer> { |
| 203 | Ok(Answer::Reply(payload.into_iter().rev().collect())) |
| 204 | } |
| 205 | |
| 206 | /// A payload of `n` bytes that is not the same at both ends and not the same |
| 207 | /// as any other length's. |
| 208 | pub fn payload(n: usize, seed: u8) -> Vec<u8> { |
| 209 | (0..n).map(|i| (i as u8).wrapping_mul(31).wrapping_add(seed)).collect() |
| 210 | } |