Skip to content

Commit a180adf

Browse files
Improve hotspot state handling
1 parent ad1ff21 commit a180adf

File tree

2 files changed

+159
-2
lines changed

2 files changed

+159
-2
lines changed

internal_filesystem/lib/mpos/net/wifi_service.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _get_hotspot_config():
8383
"enabled": prefs.get_bool("enabled", False),
8484
"ssid": prefs.get_string("ssid", "MicroPythonOS"),
8585
"password": prefs.get_string("password", ""),
86-
"authmode": prefs.get_string("authmode", "wpa2"),
86+
"authmode": prefs.get_string("authmode", None),
8787
}
8888

8989
@staticmethod
@@ -142,6 +142,12 @@ def enable_hotspot(network_module=None):
142142
print("WifiService: Hotspot enabled")
143143
return True
144144
except Exception as e:
145+
try:
146+
ap = WifiService._get_ap_wlan(net)
147+
ap.active(False)
148+
except Exception:
149+
pass
150+
WifiService.hotspot_enabled = False
145151
print(f"WifiService: Failed to enable hotspot: {e}")
146152
return False
147153

@@ -168,7 +174,8 @@ def is_hotspot_enabled(network_module=None):
168174
try:
169175
net = WifiService._get_network_module(network_module)
170176
ap = WifiService._get_ap_wlan(net)
171-
return ap.active()
177+
WifiService.hotspot_enabled = ap.active()
178+
return WifiService.hotspot_enabled
172179
except Exception:
173180
return WifiService.hotspot_enabled
174181

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"""
2+
Graphical test for hotspot start flow with security none and invalid password handling.
3+
4+
This test verifies:
5+
1) Starting hotspot with default settings and Security: None succeeds.
6+
2) Starting hotspot with an invalid WPA2 password fails and leaves hotspot disabled.
7+
8+
Usage:
9+
Desktop: ./tests/unittest.sh tests/test_graphical_hotspot_security_none.py
10+
Device: ./tests/unittest.sh tests/test_graphical_hotspot_security_none.py --ondevice
11+
"""
12+
13+
import unittest
14+
import lvgl as lv
15+
import mpos.ui
16+
from mpos import (
17+
AppManager,
18+
WifiService,
19+
SharedPreferences,
20+
wait_for_render,
21+
click_button,
22+
print_screen_labels,
23+
verify_text_present,
24+
)
25+
26+
27+
class TestGraphicalHotspotSecurityNone(unittest.TestCase):
28+
"""Graphical tests for hotspot security handling."""
29+
30+
def _reset_hotspot_preferences(self):
31+
prefs = SharedPreferences("com.micropythonos.settings.hotspot")
32+
editor = prefs.edit()
33+
editor.remove_all()
34+
editor.commit()
35+
36+
def _set_hotspot_preferences(self, ssid=None, password=None, authmode=None):
37+
prefs = SharedPreferences("com.micropythonos.settings.hotspot")
38+
editor = prefs.edit()
39+
if ssid is not None:
40+
editor.put_string("ssid", ssid)
41+
if password is not None:
42+
editor.put_string("password", password)
43+
if authmode is not None:
44+
editor.put_string("authmode", authmode)
45+
editor.commit()
46+
47+
def _open_hotspot_screen(self):
48+
result = AppManager.start_app("com.micropythonos.settings.hotspot")
49+
self.assertTrue(result, "Failed to start hotspot settings app")
50+
wait_for_render(iterations=20)
51+
screen = lv.screen_active()
52+
print("\nHotspot screen labels:")
53+
print_screen_labels(screen)
54+
return screen
55+
56+
def tearDown(self):
57+
try:
58+
WifiService.disable_hotspot()
59+
except Exception:
60+
pass
61+
62+
try:
63+
mpos.ui.back_screen()
64+
wait_for_render(5)
65+
except Exception:
66+
pass
67+
68+
def test_security_none_allows_open_hotspot(self):
69+
"""Ensure Security: None starts an open hotspot successfully."""
70+
print("\n=== Starting hotspot security none test ===")
71+
72+
self._reset_hotspot_preferences()
73+
screen = self._open_hotspot_screen()
74+
75+
self.assertFalse(
76+
WifiService.is_hotspot_enabled(),
77+
"Hotspot should be disabled before pressing Start",
78+
)
79+
80+
WifiService.wifi_busy = False
81+
82+
self.assertTrue(
83+
click_button("Start"),
84+
"Could not find Start button in hotspot app",
85+
)
86+
wait_for_render(iterations=40)
87+
88+
self.assertTrue(
89+
WifiService.is_hotspot_enabled(),
90+
"Hotspot should be enabled with Security: None",
91+
)
92+
93+
screen = lv.screen_active()
94+
print("\nHotspot screen labels after Start:")
95+
print_screen_labels(screen)
96+
self.assertTrue(
97+
verify_text_present(screen, "Security: None"),
98+
"Hotspot should display Security: None after start",
99+
)
100+
self.assertTrue(
101+
verify_text_present(screen, "Status: Running"),
102+
"Hotspot should display Status: Running after start",
103+
)
104+
105+
print("\n=== Hotspot security none test completed ===")
106+
107+
@unittest.skipIf(
108+
WifiService._is_desktop_mode(None),
109+
"Invalid password handling requires device network stack",
110+
)
111+
def test_invalid_password_fails_and_reports_disabled(self):
112+
"""Ensure invalid WPA2 password fails and hotspot remains disabled."""
113+
print("\n=== Starting hotspot invalid password test ===")
114+
115+
self._reset_hotspot_preferences()
116+
self._set_hotspot_preferences(password="123", authmode="wpa2")
117+
118+
screen = self._open_hotspot_screen()
119+
120+
self.assertFalse(
121+
WifiService.is_hotspot_enabled(),
122+
"Hotspot should be disabled before pressing Start",
123+
)
124+
125+
WifiService.wifi_busy = False
126+
127+
self.assertTrue(
128+
click_button("Start"),
129+
"Could not find Start button in hotspot app",
130+
)
131+
wait_for_render(iterations=40)
132+
133+
self.assertFalse(
134+
WifiService.is_hotspot_enabled(),
135+
"Hotspot should remain disabled when password is invalid",
136+
)
137+
138+
screen = lv.screen_active()
139+
print("\nHotspot screen labels after invalid password attempt:")
140+
print_screen_labels(screen)
141+
self.assertTrue(
142+
verify_text_present(screen, "Status: Stopped"),
143+
"Hotspot should display Status: Stopped after failed start",
144+
)
145+
146+
print("\n=== Hotspot invalid password test completed ===")
147+
148+
149+
if __name__ == "__main__":
150+
pass

0 commit comments

Comments
 (0)