//! The driver on a simulated radio: setting up, a scan, joining with the //! right password and a wrong one, an open network, leaving, and a network //! lost. use crate::host::{Event, Failure, Security, Wifi, password_fits}; use crate::sim::{AccessPoint, Radio}; use alloc::vec::Vec; struct World { radio: Radio, wifi: Wifi, now: u64, } impl World { fn new() -> World { let radio = Radio::new(alloc::vec![ AccessPoint::new("Cafe", -71, Security::Open, ""), AccessPoint::new("Home, upstairs \"5G\"", -48, Security::Wpa2, "lighthouse keeper"), AccessPoint::new("Neighbours", -83, Security::Wpa3, "not yours at all"), AccessPoint::new("Office", -90, Security::Enterprise, ""), ]); let mut w = World { radio, wifi: Wifi::new(), now: 0, }; assert_eq!(w.settle(), alloc::vec![Event::Ready]); w } /// Carries bytes both ways until nothing moves; what happened. fn settle(&mut self) -> Vec { for _ in 0..100 { let mut moved = false; self.wifi.poll(self.now); while let Some(b) = self.wifi.transmit() { self.radio.receive(&b); moved = true; } while let Some(b) = self.radio.transmit() { self.wifi.receive(&b, self.now); moved = true; } if !moved { break; } } core::iter::from_fn(|| self.wifi.event()).collect() } } #[test] fn sets_up_without_keeping_anything_on_the_radio() { let w = World::new(); assert!(w.wifi.ready()); assert!(w.radio.heard.iter().any(|l| l == "AT+SYSSTORE=0")); assert!(w.radio.heard.iter().any(|l| l == "AT+CWMODE=1")); } #[test] fn a_scan_lists_the_networks_strongest_first_with_their_signal() { let mut w = World::new(); w.wifi.scan(w.now); let events = w.settle(); let [Event::Scanned(found)] = &events[..] else { panic!("{events:?}"); }; let names: Vec<&str> = found.iter().map(|n| n.ssid.as_str()).collect(); // A name with a comma and quotes in it comes through whole. assert_eq!(names, ["Home, upstairs \"5G\"", "Cafe", "Neighbours", "Office"]); let bars: Vec = found.iter().map(|n| n.bars()).collect(); assert_eq!(bars, [4, 2, 1, 0]); assert_eq!(found[0].security, Security::Wpa2); assert_eq!(found[0].bssid, w.radio.access_points[1].bssid); assert_eq!(found[0].channel, w.radio.access_points[1].channel); } #[test] fn one_entry_per_name_the_strongest() { let mut w = World::new(); let mut far = AccessPoint::new("Cafe", -88, Security::Open, ""); far.bssid[0] = 0x06; w.radio.access_points.push(far); w.wifi.scan(w.now); let events = w.settle(); let [Event::Scanned(found)] = &events[..] else { panic!("{events:?}"); }; let cafe: Vec<_> = found.iter().filter(|n| n.ssid == "Cafe").collect(); assert_eq!(cafe.len(), 1); assert_eq!(cafe[0].rssi, -71); } #[test] fn joins_with_the_right_password() { let mut w = World::new(); let ssid = "Home, upstairs \"5G\""; assert!(w.wifi.join(ssid, "lighthouse keeper", w.now)); assert_eq!(w.wifi.joining(), Some(ssid)); assert_eq!(w.settle(), alloc::vec![Event::Joined { ssid: ssid.into() }]); assert_eq!(w.wifi.joined(), Some(ssid)); assert_eq!(w.radio.joined().map(|a| a.ssid.as_str()), Some(ssid)); } #[test] fn a_wrong_password_is_told_as_such() { let mut w = World::new(); w.wifi.join("Neighbours", "guessing wrong", w.now); assert_eq!( w.settle(), alloc::vec![Event::JoinFailed { ssid: "Neighbours".into(), failure: Failure::WrongPassword }] ); assert_eq!(w.wifi.joined(), None); assert!(w.radio.joined().is_none()); } #[test] fn an_open_network_needs_no_password_and_one_out_of_reach_is_not_found() { let mut w = World::new(); w.wifi.join("Cafe", "", w.now); assert_eq!(w.settle(), alloc::vec![Event::Joined { ssid: "Cafe".into() }]); w.wifi.join("Library", "", w.now); assert_eq!( w.settle(), alloc::vec![Event::JoinFailed { ssid: "Library".into(), failure: Failure::NotFound }] ); // The radio left the café to look for it. assert_eq!(w.wifi.joined(), None); } #[test] fn leaving_and_losing_a_network() { let mut w = World::new(); w.wifi.join("Cafe", "", w.now); w.settle(); w.wifi.leave(w.now); assert_eq!(w.settle(), alloc::vec![Event::Left]); assert_eq!(w.wifi.joined(), None); w.wifi.join("Cafe", "", w.now); w.settle(); w.radio.set_in_reach("Cafe", false); assert_eq!(w.settle(), alloc::vec![Event::Lost { ssid: "Cafe".into() }]); assert_eq!(w.wifi.joined(), None); } #[test] fn a_radio_silent_at_first_is_set_up_again() { // The radio's end connects late: what was sent first went nowhere. let mut wifi = Wifi::new(); while wifi.transmit().is_some() {} wifi.poll(0); while wifi.transmit().is_some() {} let mut w = World { radio: Radio::new(Vec::new()), wifi, now: 3000, }; assert_eq!(w.settle(), alloc::vec![Event::Ready]); } #[test] fn a_silent_scan_fails_in_time() { let mut w = World::new(); w.wifi.scan(w.now); while w.wifi.transmit().is_some() {} assert_eq!(w.wifi.next_deadline(), Some(15_000)); w.now = 15_000; assert_eq!(w.settle(), alloc::vec![Event::ScanFailed]); } #[test] fn which_passwords_fit_which_networks() { assert!(password_fits(Security::Open, "")); assert!(!password_fits(Security::Wpa2, "short")); assert!(password_fits(Security::Wpa2, "eight ch")); assert!(!password_fits(Security::Wpa2, &"x".repeat(64))); assert!(password_fits(Security::Wpa3, &"ab".repeat(32))); assert!(!password_fits(Security::Enterprise, "anything at all")); } #[test] fn the_state_goes_across_whole() { use crate::protocol::{Nearby, State}; let mut w = World::new(); w.wifi.scan(w.now); let events = w.settle(); let [Event::Scanned(found)] = &events[..] else { panic!("{events:?}"); }; let state = State { ready: true, scanning: false, joined: Some("Cafe".into()), joining: None, failed: Some(("Neighbours".into(), Failure::WrongPassword)), nearby: found .iter() .map(|n| Nearby { network: n.clone(), saved: n.ssid == "Cafe", }) .collect(), saved: alloc::vec!["Cafe".into(), "Home".into()], }; assert_eq!(State::from_bytes(&state.to_bytes()), Some(state)); assert_eq!( State::from_bytes(&State::default().to_bytes()), Some(State::default()) ); assert_eq!(State::from_bytes(&[1]), None); } #[test] fn a_join_and_the_networks_kept_go_across_whole() { use crate::protocol::{join_body, join_from, saved_from_bytes, saved_to_bytes}; let body = join_body("Home, upstairs", "pass with spaces, and \"quotes\""); assert_eq!( join_from(&body), Some(("Home, upstairs".into(), "pass with spaces, and \"quotes\"".into())) ); let saved = alloc::vec![ ("Cafe".into(), "".into()), ("Home".into(), "lighthouse keeper".into()) ]; assert_eq!(saved_from_bytes(&saved_to_bytes(&saved)), saved); } #[test] fn the_strongest_network_joined_before_is_joined_again() { use crate::protocol::rejoin; let mut w = World::new(); w.wifi.scan(w.now); let events = w.settle(); let [Event::Scanned(found)] = &events[..] else { panic!("{events:?}"); }; let saved: Vec<(alloc::string::String, alloc::string::String)> = alloc::vec![ ("Neighbours".into(), "not yours at all".into()), ("Cafe".into(), "".into()), ("Gone".into(), "somewhere else".into()), ]; assert_eq!(rejoin(&saved, found).map(|(s, _)| s.as_str()), Some("Cafe")); assert_eq!(rejoin(&saved[2..], found), None); }