Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/ws_sign_in_limited.rs

2.5 KiB, 1 run

created by r1191540701:77672, 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 WebSocket `login` and `register` commands verify a passphrase, so they are sign-ins, and
2//! the sign-in limiter is asked before each, keyed on the peer, as it is for `/manage/login`.
3//!
4//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
5//! Anthropic Claude
6
7use oxedyne_fe2o3_core::prelude::*;
8use oxedyne_fe2o3_crypto::enc::EncryptionScheme;
9use oxedyne_fe2o3_hash::hash::HashScheme;
10use oxedyne_fe2o3_jdat::version::SemVer;
11use oxedyne_fe2o3_net::ws::{
12 core::WebSocketMessage,
13 handler::WebSocketHandler,
14};
15use oxedyne_fe2o3_steel::srv::{
16 admin::guard,
17 cfg::ServerConfig,
18 id,
19 ws::{
20 handler::AppWebSocketHandler,
21 syntax::WebSocketSyntax,
22 },
23};
24
25use std::net::{
26 IpAddr,
27 Ipv4Addr,
28};
29
30mod common;
31
32// The replies to six rapid `cmd` frames on one connection from one peer.
33fn six_replies(cmd: &str, ip: IpAddr) -> Outcome<Vec<String>> {
34 let cfg = ServerConfig::default();
35 let auth = res!(guard::new_auth_shared_with(cfg.get_auth_guard_settings()));
36 let mut h = AppWebSocketHandler::new(None)
37 .with_auth_guard(auth)
38 .attach_sid(Some(fmt!("anon-guesser-sid")))
39 .with_peer(ip);
40 let syntax = res!(WebSocketSyntax::new(
41 "steel_ws", &SemVer::new(0, 1, 0), "sign-in limit test"));
42 let id = fmt!("test");
43 let mut out = Vec::new();
44 for i in 0..6 {
45 let txt = fmt!("{} (str|alice) (str|guess-{})", cmd, i);
46 let reply = res!(h.handle_text::<
47 { id::UID_LEN }, id::Uid, EncryptionScheme, HashScheme, common::TestDb,
48 >(txt, None, syntax.clone(), &id));
49 match reply {
50 Some(WebSocketMessage::Text(s)) => out.push(s),
51 other => return Err(err!("Expected a text reply, got {:?}.", other; Test, Mismatch)),
52 }
53 }
54 Ok(out)
55}
56
57fn assert_limited(cmd: &str, replies: &[String]) {
58 assert!(!replies[0].contains("too many"), "the first {} was refused: {}", cmd, replies[0]);
59 assert!(replies[5].contains("too many"),
60 "six rapid {} frames on one connection were all let through: {:?}", cmd, replies);
61}
62
63#[test]
64fn six_rapid_ws_logins_are_limited() -> Outcome<()> {
65 let ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 21)); // fixed: documentation range
66 assert_limited("login", &res!(six_replies("login", ip)));
67 Ok(())
68}
69
70#[test]
71fn six_rapid_ws_registers_are_limited() -> Outcome<()> {
72 let ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 22)); // fixed: documentation range
73 assert_limited("register", &res!(six_replies("register", ip)));
74 Ok(())
75}