oxedyne/fe2o3/fe2o3_steel/tests/member_session.rs
20.2 KiB, 15 runs
created by r1870400018:72146, 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 | //! `srv::member::session` against a real Ozone store, under the target directory rather than |
| 2 | //! `/tmp`. |
| 3 | //! |
| 4 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 5 | //! Anthropic Claude |
| 6 | |
| 7 | mod member { |
| 8 | |
| 9 | use oxedyne_fe2o3_steel::{ |
| 10 | app::{ |
| 11 | constant as app_const, |
| 12 | server::VhostDb, |
| 13 | }, |
| 14 | srv::{ |
| 15 | context::new_db, |
| 16 | id::Uid, |
| 17 | member::session::{ |
| 18 | Clock, |
| 19 | SessionCfg, |
| 20 | Sessions, |
| 21 | Token, |
| 22 | }, |
| 23 | }, |
| 24 | }; |
| 25 | |
| 26 | use oxedyne_fe2o3_core::prelude::*; |
| 27 | use oxedyne_fe2o3_iop_db::api::{ |
| 28 | Database, |
| 29 | ScanOpts, |
| 30 | }; |
| 31 | use oxedyne_fe2o3_jdat::prelude::*; |
| 32 | |
| 33 | use std::{ |
| 34 | path::{ |
| 35 | Path, |
| 36 | PathBuf, |
| 37 | }, |
| 38 | sync::{ |
| 39 | atomic::{ |
| 40 | AtomicU64, |
| 41 | Ordering, |
| 42 | }, |
| 43 | Arc, |
| 44 | RwLock, |
| 45 | }, |
| 46 | time::Duration, |
| 47 | }; |
| 48 | |
| 49 | const T0: u64 = 1_800_000_000; |
| 50 | |
| 51 | struct Store { |
| 52 | db: (Arc<RwLock<VhostDb>>, Uid), |
| 53 | root: PathBuf, |
| 54 | } |
| 55 | |
| 56 | impl Drop for Store { |
| 57 | fn drop(&mut self) { |
| 58 | let _ = std::fs::remove_dir_all(&self.root); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn store(label: &str) -> Outcome<Store> { |
| 63 | let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")) |
| 64 | .join(fmt!("member-session-{}-{}", label, std::process::id())); |
| 65 | let _ = std::fs::remove_dir_all(&root); |
| 66 | res!(std::fs::create_dir_all(&root), IO, File); |
| 67 | let mut db = res!(new_db(&root, &[3u8; 32], false)); |
| 68 | res!(db.start("member_session_test")); |
| 69 | res!(ok!(db.updated_api()).activate_gc(true)); |
| 70 | std::thread::sleep(Duration::from_millis(200)); |
| 71 | res!(db.api().ping_bots(app_const::GET_DATA_WAIT)); |
| 72 | Ok(Store { db: (Arc::new(RwLock::new(db)), Uid::default()), root }) |
| 73 | } |
| 74 | |
| 75 | fn cfg() -> SessionCfg { |
| 76 | SessionCfg { |
| 77 | prefix: fmt!("t/"), |
| 78 | idle_secs: 100, |
| 79 | absolute_secs: 1_000, |
| 80 | per_principal: 3, |
| 81 | touch_secs: 10, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn clock() -> (Clock, Arc<AtomicU64>) { |
| 86 | let t = Arc::new(AtomicU64::new(T0)); |
| 87 | (Clock::Manual(t.clone()), t) |
| 88 | } |
| 89 | |
| 90 | fn keys(s: &Store) -> Outcome<Vec<String>> { |
| 91 | let dbr = lock_read!(s.db.0); |
| 92 | let rows = res!(dbr.scan(&ScanOpts::with_str_prefix("t/s/"), None)); |
| 93 | let mut out = Vec::new(); |
| 94 | for (k, _, _) in rows { |
| 95 | if let Dat::Str(name) = k { |
| 96 | // A scan lists a deleted key whose tombstone it meets; only live ones count. |
| 97 | if res!(dbr.get(&Dat::Str(name.clone()), None)).is_some() { |
| 98 | out.push(name); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | Ok(out) |
| 103 | } |
| 104 | |
| 105 | fn files_contain(dir: &Path, needle: &[u8]) -> Outcome<bool> { |
| 106 | for entry in res!(std::fs::read_dir(dir), IO, File) { |
| 107 | let p = res!(entry, IO, File).path(); |
| 108 | if p.is_dir() { |
| 109 | if res!(files_contain(&p, needle)) { |
| 110 | return Ok(true); |
| 111 | } |
| 112 | } else { |
| 113 | let bytes = res!(std::fs::read(&p), IO, File); |
| 114 | if bytes.windows(needle.len()).any(|w| w == needle) { |
| 115 | return Ok(true); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | Ok(false) |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn start_check_end() -> Outcome<()> { |
| 124 | let s = res!(store("basic")); |
| 125 | let (clk, _) = clock(); |
| 126 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 127 | let tok = res!(ss.start(&s.db, b"alice", dat!("phone"))); |
| 128 | let got = res!(ss.check(&s.db, &tok)); |
| 129 | let sess = res!(got.ok_or_else(|| err!("A fresh session must check."; Test))); |
| 130 | assert_eq!(sess.principal, b"alice".to_vec()); |
| 131 | assert_eq!(sess.data, dat!("phone")); |
| 132 | assert_eq!(sess.created, T0); |
| 133 | |
| 134 | // The cookie form round-trips, and a token never formats as its bytes. |
| 135 | let cookie = tok.to_hex(); |
| 136 | assert_eq!(cookie.len(), 64); |
| 137 | let back = res!(Token::from_hex(&cookie)); |
| 138 | assert!(res!(ss.check(&s.db, &back)).is_some()); |
| 139 | assert!(Token::from_hex("short").is_err()); |
| 140 | let shown = fmt!("{:?} {:?} {:?}", tok, sess, ss.list(b"alice")); |
| 141 | assert!(!shown.to_lowercase().contains(&cookie.to_lowercase()), "{}", shown); |
| 142 | assert_ne!(sess.id.to_lowercase(), cookie.to_lowercase()); |
| 143 | |
| 144 | assert!(res!(ss.end(&s.db, &tok))); |
| 145 | assert!(res!(ss.check(&s.db, &tok)).is_none()); |
| 146 | assert!(!res!(ss.end(&s.db, &tok))); |
| 147 | assert!(res!(keys(&s)).is_empty()); |
| 148 | // A token nobody issued checks as nothing. |
| 149 | let stranger = res!(Token::from_hex(&"ab".repeat(32))); |
| 150 | assert!(res!(ss.check(&s.db, &stranger)).is_none()); |
| 151 | Ok(()) |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn idle_and_absolute_expiry() -> Outcome<()> { |
| 156 | let s = res!(store("expiry")); |
| 157 | let (clk, t) = clock(); |
| 158 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 159 | |
| 160 | // Idle: unused for the whole idle window, it is gone. |
| 161 | let idle = res!(ss.start(&s.db, b"bob", Dat::Empty)); |
| 162 | t.store(T0 + 99, Ordering::SeqCst); |
| 163 | assert!(res!(ss.check(&s.db, &idle)).is_some(), "one second inside the idle window"); |
| 164 | t.store(T0 + 99 + 100, Ordering::SeqCst); |
| 165 | assert!(res!(ss.check(&s.db, &idle)).is_none(), "a full idle window unused"); |
| 166 | |
| 167 | // Absolute: kept busy, it still ends at the ceiling. |
| 168 | let start = T0 + 500; |
| 169 | t.store(start, Ordering::SeqCst); |
| 170 | let busy = res!(ss.start(&s.db, b"bob", Dat::Empty)); |
| 171 | let mut now = start; |
| 172 | while now + 50 < start + 1_000 { |
| 173 | now += 50; |
| 174 | t.store(now, Ordering::SeqCst); |
| 175 | assert!(res!(ss.check(&s.db, &busy)).is_some(), "busy at {}", now - start); |
| 176 | } |
| 177 | t.store(start + 1_000, Ordering::SeqCst); |
| 178 | assert!(res!(ss.check(&s.db, &busy)).is_none(), "past the absolute ceiling"); |
| 179 | |
| 180 | // The sweep ends what nobody presents again. |
| 181 | t.store(T0 + 2_000, Ordering::SeqCst); |
| 182 | let _ = res!(ss.start(&s.db, b"carol", Dat::Empty)); |
| 183 | let _ = res!(ss.start(&s.db, b"dave", Dat::Empty)); |
| 184 | t.store(T0 + 2_050, Ordering::SeqCst); |
| 185 | let kept = res!(ss.start(&s.db, b"erin", Dat::Empty)); |
| 186 | t.store(T0 + 2_100, Ordering::SeqCst); |
| 187 | assert_eq!(res!(ss.sweep(&s.db)), 2); |
| 188 | assert_eq!(res!(ss.len()), 1); |
| 189 | assert!(res!(ss.check(&s.db, &kept)).is_some()); |
| 190 | assert_eq!(res!(keys(&s)).len(), 1); |
| 191 | Ok(()) |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn the_cap_drops_the_oldest() -> Outcome<()> { |
| 196 | let s = res!(store("cap")); |
| 197 | let (clk, t) = clock(); |
| 198 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 199 | let mut toks = Vec::new(); |
| 200 | for i in 0..4u64 { |
| 201 | t.store(T0 + i, Ordering::SeqCst); |
| 202 | toks.push(res!(ss.start(&s.db, b"fay", dat!(i)))); |
| 203 | } |
| 204 | assert!(res!(ss.check(&s.db, &toks[0])).is_none(), "the oldest went past the cap"); |
| 205 | for tok in &toks[1..] { |
| 206 | assert!(res!(ss.check(&s.db, tok)).is_some()); |
| 207 | } |
| 208 | let listed = res!(ss.list(b"fay")); |
| 209 | assert_eq!(listed.iter().map(|x| x.data.clone()).collect::<Vec<_>>(), |
| 210 | vec![dat!(1u64), dat!(2u64), dat!(3u64)]); |
| 211 | assert_eq!(res!(keys(&s)).len(), 3); |
| 212 | Ok(()) |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn end_all_ends_one_principal_only() -> Outcome<()> { |
| 217 | let s = res!(store("end_all")); |
| 218 | let (clk, t) = clock(); |
| 219 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 220 | let a1 = res!(ss.start(&s.db, b"gus", Dat::Empty)); |
| 221 | let a2 = res!(ss.start(&s.db, b"gus", Dat::Empty)); |
| 222 | let b1 = res!(ss.start(&s.db, b"hal", Dat::Empty)); |
| 223 | assert_eq!(res!(ss.end_all(&s.db, b"gus")), 2); |
| 224 | assert!(res!(ss.check(&s.db, &a1)).is_none()); |
| 225 | assert!(res!(ss.check(&s.db, &a2)).is_none()); |
| 226 | assert!(res!(ss.check(&s.db, &b1)).is_some()); |
| 227 | assert_eq!(res!(ss.end_all(&s.db, b"gus")), 0); |
| 228 | // One device revoked from another, by the id a listing shows. |
| 229 | t.store(T0 + 1, Ordering::SeqCst); // so the listing puts b1 first |
| 230 | let c1 = res!(ss.start(&s.db, b"hal", Dat::Empty)); |
| 231 | let id = res!(ss.list(b"hal"))[0].id.clone(); |
| 232 | assert!(res!(ss.end_id(&s.db, &id))); |
| 233 | assert!(res!(ss.check(&s.db, &b1)).is_none()); |
| 234 | assert!(res!(ss.check(&s.db, &c1)).is_some()); |
| 235 | // Revocation holds across a restart. |
| 236 | let (clk, t2) = clock(); |
| 237 | t2.store(T0 + 1, Ordering::SeqCst); |
| 238 | let again = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 239 | assert!(res!(again.check(&s.db, &a1)).is_none()); |
| 240 | assert!(res!(again.check(&s.db, &c1)).is_some()); |
| 241 | Ok(()) |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn a_restart_keeps_live_sessions() -> Outcome<()> { |
| 246 | let s = res!(store("restart")); |
| 247 | let (clk, t) = clock(); |
| 248 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 249 | let old = res!(ss.start(&s.db, b"ivy", dat!("old"))); |
| 250 | t.store(T0 + 60, Ordering::SeqCst); |
| 251 | let live = res!(ss.start(&s.db, b"ivy", dat!("live"))); |
| 252 | // Used at +95, past `touch_secs`, so `last_seen` is written through. |
| 253 | t.store(T0 + 95, Ordering::SeqCst); |
| 254 | assert!(res!(ss.check(&s.db, &old)).is_some()); |
| 255 | drop(ss); |
| 256 | |
| 257 | // At +150 the first was last seen at +95 and the second at +60, so both live. |
| 258 | let (clk, t2) = clock(); |
| 259 | t2.store(T0 + 150, Ordering::SeqCst); |
| 260 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk.clone())); |
| 261 | assert_eq!(res!(ss.len()), 2); |
| 262 | let got = res!(res!(ss.check(&s.db, &live)).ok_or_else(|| err!("Lost at restart."; Test))); |
| 263 | assert_eq!(got.data, dat!("live")); |
| 264 | assert_eq!(got.created, T0 + 60); |
| 265 | let got = res!(res!(ss.check(&s.db, &old)).ok_or_else(|| err!("Lost at restart."; Test))); |
| 266 | assert_eq!(got.data, dat!("old")); |
| 267 | drop(ss); |
| 268 | |
| 269 | // Long after, a load drops what expired while nothing ran. |
| 270 | let (clk, t3) = clock(); |
| 271 | t3.store(T0 + 5_000, Ordering::SeqCst); |
| 272 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 273 | assert_eq!(res!(ss.len()), 0); |
| 274 | assert!(res!(keys(&s)).is_empty()); |
| 275 | Ok(()) |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn set_data_writes_through_and_survives_a_restart() -> Outcome<()> { |
| 280 | let s = res!(store("set_data")); |
| 281 | let (clk, t) = clock(); |
| 282 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 283 | let tok = res!(ss.start(&s.db, b"kit", dat!("before"))); |
| 284 | let other = res!(ss.start(&s.db, b"kit", dat!("other"))); |
| 285 | t.store(T0 + 10, Ordering::SeqCst); |
| 286 | assert!(res!(ss.set_data(&s.db, &tok, dat!("after")))); |
| 287 | let got = res!(res!(ss.check(&s.db, &tok)).ok_or_else(|| err!("Lost."; Test))); |
| 288 | assert_eq!(got.data, dat!("after")); |
| 289 | let got = res!(res!(ss.check(&s.db, &other)).ok_or_else(|| err!("Lost."; Test))); |
| 290 | assert_eq!(got.data, dat!("other")); |
| 291 | drop(ss); |
| 292 | let (clk, t2) = clock(); |
| 293 | t2.store(T0 + 20, Ordering::SeqCst); |
| 294 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 295 | let got = res!(res!(ss.check(&s.db, &tok)).ok_or_else(|| err!("Lost at restart."; Test))); |
| 296 | assert_eq!(got.data, dat!("after")); |
| 297 | // An ended or unknown session takes no data and is not revived. |
| 298 | assert!(res!(ss.end(&s.db, &tok))); |
| 299 | assert!(!res!(ss.set_data(&s.db, &tok, dat!("ghost")))); |
| 300 | assert!(res!(ss.check(&s.db, &tok)).is_none()); |
| 301 | assert!(!res!(ss.set_data(&s.db, &res!(Token::from_hex(&"ab".repeat(32))), dat!("ghost")))); |
| 302 | Ok(()) |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn the_store_holds_the_hash_and_never_the_token() -> Outcome<()> { |
| 307 | let s = res!(store("hash")); |
| 308 | let (clk, _) = clock(); |
| 309 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 310 | let tok = res!(ss.start(&s.db, b"jo", dat!("laptop"))); |
| 311 | let id = res!(ss.list(b"jo"))[0].id.clone(); |
| 312 | let cookie = tok.to_hex(); |
| 313 | assert_eq!(res!(keys(&s)), vec![fmt!("t/s/{}", id)]); |
| 314 | // The hash of the token, not the token. |
| 315 | assert_ne!(id, cookie); |
| 316 | let raw = res!(oxedyne_fe2o3_text::base2x::HEX.from_str(&cookie)); |
| 317 | assert_eq!(raw.len(), 32); |
| 318 | assert!(!res!(files_contain(&s.root, &raw)), "the raw token is in the store's files"); |
| 319 | assert!(!res!(files_contain(&s.root, cookie.as_bytes())), "the token's text is in the store's files"); |
| 320 | // And what the record holds names no token. |
| 321 | let dbr = lock_read!(s.db.0); |
| 322 | let val = res!(res!(dbr.get(&Dat::Str(fmt!("t/s/{}", id)), None)) |
| 323 | .ok_or_else(|| err!("The record is missing."; Test))); |
| 324 | let shown = fmt!("{:?}", val.0); |
| 325 | assert!(!shown.to_lowercase().contains(&cookie.to_lowercase()), "{}", shown); |
| 326 | Ok(()) |
| 327 | } |
| 328 | |
| 329 | // A legacy token: an app's own 32 ASCII characters, adopted rather than reissued. |
| 330 | const LEGACY: &[u8; 32] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
| 331 | |
| 332 | fn legacy() -> Token { |
| 333 | Token::from_secret(*LEGACY) |
| 334 | } |
| 335 | |
| 336 | #[test] |
| 337 | fn an_adopted_secret_checks_across_a_reload() -> Outcome<()> { |
| 338 | let s = res!(store("adopt")); |
| 339 | let (clk, _) = clock(); |
| 340 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 341 | assert!(res!(ss.adopt(&s.db, &legacy(), b"kit", dat!("tablet"), T0 - 500, T0 - 50))); |
| 342 | assert_eq!(res!(keys(&s)).len(), 1); |
| 343 | drop(ss); |
| 344 | |
| 345 | // A fresh token from the same bytes finds it after a restart, with its times intact. |
| 346 | let (clk, _) = clock(); |
| 347 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 348 | let got = res!(res!(ss.peek(&s.db, &Token::from_secret(*LEGACY))) |
| 349 | .ok_or_else(|| err!("The adopted session was lost at restart."; Test))); |
| 350 | assert_eq!(got.principal, b"kit".to_vec()); |
| 351 | assert_eq!(got.data, dat!("tablet")); |
| 352 | assert_eq!(got.created, T0 - 500); |
| 353 | assert_eq!(got.last_seen, T0 - 50); |
| 354 | assert!(res!(ss.check(&s.db, &legacy())).is_some()); |
| 355 | // Ended like any other. |
| 356 | assert!(res!(ss.end(&s.db, &legacy()))); |
| 357 | assert!(res!(keys(&s)).is_empty()); |
| 358 | Ok(()) |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn adoption_carries_both_expiries_exactly() -> Outcome<()> { |
| 363 | let s = res!(store("adopt_exp")); |
| 364 | let (clk, t) = clock(); |
| 365 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 366 | // Idle: last seen 60 s ago, so 40 s remain. |
| 367 | let idle = Token::from_secret([1u8; 32]); |
| 368 | assert!(res!(ss.adopt(&s.db, &idle, b"lou", Dat::Empty, T0 - 100, T0 - 60))); |
| 369 | // Absolute: created 950 s ago, so 50 s remain however busy it is. |
| 370 | let abs = Token::from_secret([2u8; 32]); |
| 371 | assert!(res!(ss.adopt(&s.db, &abs, b"lou", Dat::Empty, T0 - 950, T0 - 10))); |
| 372 | t.store(T0 + 39, Ordering::SeqCst); |
| 373 | assert!(res!(ss.peek(&s.db, &idle)).is_some()); |
| 374 | t.store(T0 + 40, Ordering::SeqCst); |
| 375 | assert!(res!(ss.peek(&s.db, &idle)).is_none(), "the idle window ran out at the adopted time"); |
| 376 | t.store(T0 + 49, Ordering::SeqCst); |
| 377 | assert!(res!(ss.check(&s.db, &abs)).is_some()); |
| 378 | t.store(T0 + 50, Ordering::SeqCst); |
| 379 | assert!(res!(ss.check(&s.db, &abs)).is_none(), "the absolute ceiling held from the adopted creation"); |
| 380 | assert!(res!(keys(&s)).is_empty()); |
| 381 | Ok(()) |
| 382 | } |
| 383 | |
| 384 | #[test] |
| 385 | fn adopting_an_expired_session_writes_nothing() -> Outcome<()> { |
| 386 | let s = res!(store("adopt_dead")); |
| 387 | let (clk, _) = clock(); |
| 388 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 389 | assert!(!res!(ss.adopt(&s.db, &legacy(), b"max", Dat::Empty, T0 - 200, T0 - 100)), "idle"); |
| 390 | assert!(!res!(ss.adopt(&s.db, &legacy(), b"max", Dat::Empty, T0 - 1_000, T0)), "absolute"); |
| 391 | assert!(res!(keys(&s)).is_empty()); |
| 392 | assert_eq!(res!(ss.len()), 0); |
| 393 | assert!(res!(ss.check(&s.db, &legacy())).is_none()); |
| 394 | // Nonsense times are refused rather than stored. |
| 395 | assert!(ss.adopt(&s.db, &legacy(), b"max", Dat::Empty, T0 - 10, T0 - 20).is_err(), "seen before made"); |
| 396 | assert!(ss.adopt(&s.db, &legacy(), b"max", Dat::Empty, T0 - 10, T0 + 5).is_err(), "seen in the future"); |
| 397 | assert!(res!(keys(&s)).is_empty()); |
| 398 | Ok(()) |
| 399 | } |
| 400 | |
| 401 | #[test] |
| 402 | fn adopting_twice_leaves_one_record_and_the_later_use() -> Outcome<()> { |
| 403 | let s = res!(store("adopt_twice")); |
| 404 | let (clk, _) = clock(); |
| 405 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 406 | assert!(res!(ss.adopt(&s.db, &legacy(), b"ned", dat!(1u8), T0 - 300, T0 - 20))); |
| 407 | // A re-run from an older copy must not wind the use back. |
| 408 | assert!(res!(ss.adopt(&s.db, &legacy(), b"ned", dat!(1u8), T0 - 300, T0 - 80))); |
| 409 | assert_eq!(res!(keys(&s)).len(), 1); |
| 410 | assert_eq!(res!(ss.len()), 1); |
| 411 | let listed = res!(ss.list(b"ned")); |
| 412 | assert_eq!(listed.len(), 1); |
| 413 | assert_eq!(listed[0].last_seen, T0 - 20); |
| 414 | drop(ss); |
| 415 | let (clk, _) = clock(); |
| 416 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 417 | let listed = res!(ss.list(b"ned")); |
| 418 | assert_eq!(listed.len(), 1); |
| 419 | assert_eq!(listed[0].last_seen, T0 - 20, "the store kept the later use too"); |
| 420 | // The same secret cannot be moved to another principal. |
| 421 | assert!(ss.adopt(&s.db, &legacy(), b"oz", dat!(1u8), T0 - 300, T0 - 20).is_err()); |
| 422 | assert!(res!(ss.list(b"oz")).is_empty()); |
| 423 | Ok(()) |
| 424 | } |
| 425 | |
| 426 | #[test] |
| 427 | fn adoption_honours_the_cap_oldest_first() -> Outcome<()> { |
| 428 | let s = res!(store("adopt_cap")); |
| 429 | let (clk, _) = clock(); |
| 430 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 431 | for i in 0..3u8 { |
| 432 | let tok = Token::from_secret([10 + i; 32]); |
| 433 | assert!(res!(ss.adopt(&s.db, &tok, b"pia", dat!(i), T0 - 50 + i as u64, T0))); |
| 434 | } |
| 435 | // Older than all three: it would be the first ended, so it is never written. |
| 436 | let older = Token::from_secret([20u8; 32]); |
| 437 | assert!(!res!(ss.adopt(&s.db, &older, b"pia", dat!(9u8), T0 - 90, T0))); |
| 438 | assert!(res!(ss.peek(&s.db, &older)).is_none()); |
| 439 | // Newer than all three: the oldest makes room. |
| 440 | let newer = Token::from_secret([21u8; 32]); |
| 441 | assert!(res!(ss.adopt(&s.db, &newer, b"pia", dat!(7u8), T0 - 5, T0))); |
| 442 | assert_eq!(res!(ss.list(b"pia")).iter().map(|x| x.data.clone()).collect::<Vec<_>>(), |
| 443 | vec![dat!(1u8), dat!(2u8), dat!(7u8)]); |
| 444 | assert_eq!(res!(keys(&s)).len(), 3); |
| 445 | Ok(()) |
| 446 | } |
| 447 | |
| 448 | #[test] |
| 449 | fn peek_never_moves_last_seen() -> Outcome<()> { |
| 450 | let s = res!(store("peek")); |
| 451 | let (clk, t) = clock(); |
| 452 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 453 | let tok = res!(ss.start(&s.db, b"quin", Dat::Empty)); |
| 454 | // Well past `touch_secs`, where a check would write a touch through. |
| 455 | t.store(T0 + 60, Ordering::SeqCst); |
| 456 | let got = res!(res!(ss.peek(&s.db, &tok)).ok_or_else(|| err!("A live session must peek."; Test))); |
| 457 | assert_eq!(got.last_seen, T0); |
| 458 | drop(ss); |
| 459 | let (clk, t2) = clock(); |
| 460 | t2.store(T0 + 61, Ordering::SeqCst); |
| 461 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 462 | assert_eq!(res!(ss.list(b"quin"))[0].last_seen, T0, "a peek wrote a touch"); |
| 463 | // Peeking does not keep it alive: it ends at the idle window from its last real use. |
| 464 | t2.store(T0 + 99, Ordering::SeqCst); |
| 465 | assert!(res!(ss.peek(&s.db, &tok)).is_some()); |
| 466 | t2.store(T0 + 100, Ordering::SeqCst); |
| 467 | assert!(res!(ss.peek(&s.db, &tok)).is_none()); |
| 468 | assert!(res!(keys(&s)).is_empty(), "an expired session found by a peek is ended"); |
| 469 | Ok(()) |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn an_adopted_secret_never_reaches_the_store() -> Outcome<()> { |
| 474 | let s = res!(store("adopt_hash")); |
| 475 | let (clk, _) = clock(); |
| 476 | let ss = res!(Sessions::load_with_clock(&s.db, cfg(), clk)); |
| 477 | assert!(res!(ss.adopt(&s.db, &legacy(), b"rae", dat!("phone"), T0 - 10, T0))); |
| 478 | let id = res!(ss.list(b"rae"))[0].id.clone(); |
| 479 | assert_eq!(res!(keys(&s)), vec![fmt!("t/s/{}", id)]); |
| 480 | let cookie = legacy().to_hex(); |
| 481 | assert!(!res!(files_contain(&s.root, LEGACY)), "the raw secret is in the store's files"); |
| 482 | assert!(!res!(files_contain(&s.root, cookie.as_bytes())), "the secret's hex is in the store's files"); |
| 483 | assert!(!res!(files_contain(&s.root, cookie.to_lowercase().as_bytes()))); |
| 484 | Ok(()) |
| 485 | } |
| 486 | } |