@@ -33,63 +33,195 @@ local function setup_wifi_test_environment()
3333 return wifi_service , bus , bus_pkg .new_msg
3434end
3535
36+ local function ssid_path (radio_index , interface )
37+ return { ' hal' , ' capability' , ' wireless' , radio_index , ' info' , ' interface' , interface , ' ssid' }
38+ end
39+
40+ local function client_event_path (radio_index , interface , client_mac )
41+ return { ' hal' , ' capability' , ' wireless' , radio_index , ' info' , ' interface' , interface , ' client' , client_mac }
42+ end
43+
44+ local function session_start_path ()
45+ return { ' wifi' , ' clients' , ' +' , ' sessions' , ' +' , ' session_start' }
46+ end
47+
48+ local function session_end_path ()
49+ return { ' wifi' , ' clients' , ' +' , ' sessions' , ' +' , ' session_end' }
50+ end
51+
3652function TestWifiClientEvents :test_normal_client_connection ()
53+ -- Setup test environment
3754 local wifi , bus , new_msg = setup_wifi_test_environment ()
3855 local conn = bus :connect ()
39- local bg_ctx = context .background ()
40- local ctx , cancel = context .with_cancel (bg_ctx )
56+ local ctx = context .with_cancel (context .background ())
4157
58+ -- Configure radio and interface
4259 local radio_index = " radio0"
43- local ssid = " ssid_name"
4460 local interface = " wlan0"
61+ local ssid = " test_ssid"
62+
4563 local radio = wifi .new_radio (ctx , bus :connect (), radio_index )
46- radio .band_ch :put (" 2g" ) -- Radio will not listen for clients unless band is set
47- -- We need to link the interface to an index
48- radio .interface_ch :put ({ name = ssid , index = 1 }) -- First link ssid to index
49- conn :publish (new_msg ( -- Then link interface to ssid
50- {
51- ' hal' ,
52- ' capability' ,
53- ' wireless' ,
54- radio_index ,
55- ' info' ,
56- ' interface' ,
57- interface ,
58- ' ssid'
59- },
60- ssid
61- ))
64+ radio .band_ch :put (" 2g" )
65+ radio .interface_ch :put ({ name = ssid , index = 1 })
66+ conn :publish (new_msg (ssid_path (radio_index , interface ), ssid ))
6267 fiber .yield ()
6368
69+ -- Subscribe before publishing the connection event
70+ local session_sub = conn :subscribe (session_start_path ())
71+
72+ -- Publish client connection event
6473 local client_mac = " AA:BB:CC:DD:EE:FF"
6574 local timestamp = 123456789
6675 conn :publish (new_msg (
67- { ' hal' , ' capability' , ' wireless' , radio_index , ' info' , ' interface' , interface , ' client' , client_mac },
68- {
69- connected = true ,
70- timestamp = timestamp
71- }
76+ client_event_path (radio_index , interface , client_mac ),
77+ { connected = true , timestamp = timestamp }
7278 ))
73- local session_sub = conn :subscribe ({ ' wifi' , ' clients' , ' +' , ' sessions' , ' +' , ' session_start' })
7479
80+ -- Verify session_start message was published with correct timestamp
7581 local session_msg , err = session_sub :next_msg_with_context (context .with_timeout (ctx , 0.1 ))
76- luaunit .assertNil (err )
77-
78- local session_timestamp = session_msg .payload or nil
79- luaunit .assertNotNil (session_timestamp )
80- luaunit .assertEquals (session_timestamp , timestamp ) -- Check timestamp matches
82+ luaunit .assertNil (err , " Expected session_start message" )
83+ luaunit .assertNotNil (session_msg .payload )
84+ luaunit .assertEquals (session_msg .payload , timestamp )
8185end
8286
8387function TestWifiClientEvents :test_duplicate_connection_event ()
84- -- Test a double client event (same MAC) disregards the second event and does not make a new session
88+ -- Setup test environment
89+ local wifi , bus , new_msg = setup_wifi_test_environment ()
90+ local conn = bus :connect ()
91+ local ctx = context .with_cancel (context .background ())
92+
93+ -- Configure radio and interface
94+ local radio_index = " radio0"
95+ local interface = " wlan0"
96+ local ssid = " test_ssid"
97+
98+ local radio = wifi .new_radio (ctx , bus :connect (), radio_index )
99+ radio .band_ch :put (" 2g" )
100+ radio .interface_ch :put ({ name = ssid , index = 1 })
101+ conn :publish (new_msg (ssid_path (radio_index , interface ), ssid ))
102+ fiber .yield ()
103+
104+ local session_sub = conn :subscribe (session_start_path ())
105+ local client_mac = " AA:BB:CC:DD:EE:FF"
106+
107+ -- Publish first connection event
108+ conn :publish (new_msg (
109+ client_event_path (radio_index , interface , client_mac ),
110+ { connected = true , timestamp = 1 }
111+ ))
112+
113+ -- Publish duplicate connection event (same MAC, still connected)
114+ conn :publish (new_msg (
115+ client_event_path (radio_index , interface , client_mac ),
116+ { connected = true , timestamp = 2 }
117+ ))
118+
119+ -- Verify first connection event created a session
120+ local session_msg , err = session_sub :next_msg_with_context (context .with_timeout (ctx , 0.1 ))
121+ luaunit .assertNil (err , " Expected first session_start message" )
122+ luaunit .assertEquals (session_msg .payload , 1 )
123+
124+ -- Verify duplicate connection event was ignored (no second session_start)
125+ local session_msg2 , err2 = session_sub :next_msg_with_context (context .with_timeout (ctx , 0.1 ))
126+ luaunit .assertNotNil (err2 , " Expected timeout - duplicate should be ignored" )
127+ luaunit .assertNil (session_msg2 )
85128end
86129
87130function TestWifiClientEvents :test_normal_disconnection_event ()
88- -- Test a normal disconnection event
131+ -- Setup test environment
132+ local wifi , bus , new_msg = setup_wifi_test_environment ()
133+ local conn = bus :connect ()
134+ local ctx = context .with_cancel (context .background ())
135+
136+ -- Configure radio and interface
137+ local radio_index = " radio0"
138+ local interface = " wlan0"
139+ local ssid = " test_ssid"
140+
141+ local radio = wifi .new_radio (ctx , bus :connect (), radio_index )
142+ radio .band_ch :put (" 2g" )
143+ radio .interface_ch :put ({ name = ssid , index = 1 })
144+ conn :publish (new_msg (ssid_path (radio_index , interface ), ssid ))
145+ fiber .yield ()
146+
147+ local client_mac = " AA:BB:CC:DD:EE:FF"
148+ local connect_time = 100
149+ local disconnect_time = 200
150+
151+ -- Connect the client
152+ conn :publish (new_msg (
153+ client_event_path (radio_index , interface , client_mac ),
154+ { connected = true , timestamp = connect_time }
155+ ))
156+ fiber .yield ()
157+
158+ -- Subscribe before disconnecting
159+ local session_sub = conn :subscribe (session_end_path ())
160+
161+ -- Disconnect the client
162+ conn :publish (new_msg (
163+ client_event_path (radio_index , interface , client_mac ),
164+ { connected = false , timestamp = disconnect_time }
165+ ))
166+
167+ -- Verify session_end message was published with correct timestamp
168+ local session_msg , err = session_sub :next_msg_with_context (context .with_timeout (ctx , 0.1 ))
169+ luaunit .assertNil (err , " Expected session_end message" )
170+ luaunit .assertNotNil (session_msg .payload )
171+ luaunit .assertEquals (session_msg .payload , disconnect_time )
89172end
90173
91174function TestWifiClientEvents :test_duplicate_disconnection_event ()
92- -- Test a double disconnection event (same MAC) disregards the second event and does not try to close a non-existent session
175+ local wifi , bus , new_msg = setup_wifi_test_environment ()
176+ local conn = bus :connect ()
177+ local bg_ctx = context .background ()
178+ local ctx , cancel = context .with_cancel (bg_ctx )
179+
180+ -- Setup: Create radio and interface
181+ local radio_index = " radio0"
182+ local interface = " wlan0"
183+ local ssid = " ssid_name"
184+
185+ local radio = wifi .new_radio (ctx , bus :connect (), radio_index )
186+ radio .band_ch :put (" 2g" )
187+ radio .interface_ch :put ({ name = ssid , index = 1 })
188+ conn :publish (new_msg (ssid_path (radio_index , interface ), ssid ))
189+ fiber .yield ()
190+
191+ local client_mac = " AA:BB:CC:DD:EE:FF"
192+ local connect_time = 100
193+ local disconnect_time = 200
194+
195+ -- Connect the client first
196+ conn :publish (new_msg (
197+ client_event_path (radio_index , interface , client_mac ),
198+ { connected = true , timestamp = connect_time }
199+ ))
200+ fiber .yield ()
201+
202+ local session_sub = conn :subscribe (session_end_path ())
203+
204+ -- Action: Publish first disconnection event
205+ conn :publish (new_msg (
206+ client_event_path (radio_index , interface , client_mac ),
207+ { connected = false , timestamp = disconnect_time }
208+ ))
209+
210+ -- Action: Publish duplicate disconnection event (should be ignored)
211+ conn :publish (new_msg (
212+ client_event_path (radio_index , interface , client_mac ),
213+ { connected = false , timestamp = disconnect_time + 1 }
214+ ))
215+
216+ -- Verify: First disconnection created a session_end message
217+ local session_msg , err = session_sub :next_msg_with_context (context .with_timeout (ctx , 0.1 ))
218+ luaunit .assertNil (err , " Expected first session_end message" )
219+ luaunit .assertEquals (session_msg .payload , disconnect_time , " Session end timestamp should match first disconnect" )
220+
221+ -- Verify: Duplicate disconnection was ignored (no second session_end)
222+ local session_msg2 , err2 = session_sub :next_msg_with_context (context .with_timeout (ctx , 0.1 ))
223+ luaunit .assertNotNil (err2 , " Expected timeout - duplicate disconnect should be ignored" )
224+ luaunit .assertNil (session_msg2 , " No second session_end should be published" )
93225end
94226
95227-- Only run tests if this file is executed directly (not via dofile)
0 commit comments