• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2
3#
4# Copyright 2021, The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19# This script simulates different combinations of scans and config to allow a tester to manually
20# verify behavior in the Wi-Fi picker without setting up an actual test environment with real APs.
21# This is especially useful for verifying interactions between scans and configs of multiple
22# security types from the same security family, along with suggestions for the same networks.
23#
24# INSTRUCTIONS:
25# 1) Connect an adb debuggable test device.
26# 2) Open the test device and navigate to the Wi-Fi picker.
27# 3) In main(), uncomment any test cases that you want to test.
28# 4) If testing suggestions INSTEAD of configs, only set ADD_SUGGESTION_INSTEAD_OF_CONFIG to True.
29# 5) If testing suggestions AND configs, only set ADD_IDENTICAL_SUGGESTION to True.
30# 6) Run this script 'py simulate_wifi.py'
31# 7) Follow the prompts from the script. The prompts will ask you to verify the behavior of the
32#    Wi-Fi picker through user interaction and visual confirmation.
33#
34# NOTE: Suggestions may take several seconds to appear. This is expected since it may be some scans
35#       cycles until WifiManager#getWifiConfigForMatchedNetworkSuggestionsSharedWithUser() returns
36#       the matching suggestion.
37
38import subprocess
39import sys
40import time
41
42OPEN_SSID = "fakeOpen"
43OWE_SSID = "fakeOwe"
44OPEN_OWE_SSID = "fakeOpenOwe"
45WPA2_SSID = "fakeWpa2"
46WPA3_SSID = "fakeWpa3"
47WPA2_WPA3_SSID = "fakeWpa2Wpa3"
48
49# only one of these should be True (possibly none for just adding base configuration)
50ADD_SUGGESTION_INSTEAD_OF_CONFIG = False
51ADD_IDENTICAL_SUGGESTION = False
52
53def main() -> None:
54  root()
55  time.sleep(5)
56
57  # Single Open scan
58  # testOpenScanNoConfigs()
59  # testOpenScanOpenConfig()
60  # testOpenScanOweConfig()
61  # testOpenScanOpenOweConfig()
62
63  # Single OWE scan
64  # testOweScanNoConfigs()
65  # testOweScanOpenConfig()
66  # testOweScanOweConfig()
67  # testOweScanOpenOweConfig()
68
69  # Single Open/OWE scan
70  # testOpenOweScanNoConfigs()
71  # testOpenOweScanOpenConfig()
72  # testOpenOweScanOweConfig()
73  # testOpenOweScanOpenOweConfig()
74
75  # Open scan and OWE scan
76  # testOpenScanOweScanNoConfigs()
77  # testOpenScanOweScanOpenConfig()
78  # testOpenScanOweScanOweConfig()
79  # testOpenScanOweScanOpenOweConfig()
80
81  # Open scan and Open/OWE scan
82  # testOpenScanOpenOweScanNoConfigs()
83  # testOpenScanOpenOweScanOpenConfig()
84  # testOpenScanOpenOweScanOweConfig()
85  # testOpenScanOpenOweScanOpenOweConfig()
86
87  # Open/OWE scan and OWE scan
88  # testOpenOweScanOweScanNoConfigs()
89  # testOpenOweScanOweScanOpenConfig()
90  # testOpenOweScanOweScanOweConfig()
91  # testOpenOweScanOweScanOpenOweConfig()
92
93  # Open, Open/OWE, and OWE scans
94  # testOpenScanOpenOweScanOweScanNoConfigs()
95  # testOpenScanOpenOweScanOweScanOpenConfig()
96  # testOpenScanOpenOweScanOweScanOweConfig()
97  # testOpenScanOpenOweScanOweScanOpenOweConfig()
98
99  # Single WPA2 scan
100  # testWpa2ScanNoConfigs()
101  # testWpa2ScanWpa2Config()
102  # testWpa2ScanWpa3Config()
103  # testWpa2ScanWpa2Wpa3Config()
104
105  # Single WPA3 scan
106  # testWpa3ScanNoConfigs()
107  # testWpa3ScanWpa2Config()
108  # testWpa3ScanWpa3Config()
109  # testWpa3ScanWpa2Wpa3Config()
110
111  # Single WP2/WPA3 scan
112  # testWpa2Wpa3ScanNoConfigs()
113  # testWpa2Wpa3ScanWpa2Config()
114  # testWpa2Wpa3ScanWpa3Config()
115  # testWpa2Wpa3ScanWpa2Wpa3Config()
116
117  # WPA2 scan and WPA3 scan
118  # testWpa2ScanWpa3ScanNoConfigs()
119  # testWpa2ScanWpa3ScanWpa2Config()
120  # testWpa2ScanWpa3ScanWpa3Config()
121  # testWpa2ScanWpa3ScanWpa2Wpa3Config()
122
123  # WPA2 scan and WPA2/WPA3 scan
124  # testWpa2ScanWpa2Wpa3ScanNoConfigs()
125  # testWpa2ScanWpa2Wpa3ScanWpa2Config()
126  # testWpa2ScanWpa2Wpa3ScanWpa3Config()
127  # testWpa2ScanWpa2Wpa3ScanWpa2Wpa3Config()
128
129  # WPA2/WPA3 scan and WPA3 scan
130  # testWpa2Wpa3ScanWpa3ScanNoConfigs()
131  # testWpa2Wpa3ScanWpa3ScanWpa2Config()
132  # testWpa2Wpa3ScanWpa3ScanWpa3Config()
133  # testWpa2Wpa3ScanWpa3ScanWpa2Wpa3Config()
134
135  # WPA2, WPA2/WPA3, and WPA3 scans
136  # testWpa2ScanWpa2Wpa3ScanWpa3ScanNoConfigs()
137  # testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa2Config()
138  # testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa3Config()
139  # testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa2Wpa3Config()
140
141  return 0
142
143
144def testTemplate(method_name, fake_scans, fake_configs, pre_instructions, post_instructions) -> None:
145  print("")
146  print("")
147  print("****** Test: " + method_name)
148  print("** Resetting all scans/configs")
149  settings_reset()
150  print("** Starting to fake scans")
151  fake_scans()
152  startFakingScans()
153  startScan()
154  time.sleep(5)
155  print("** Fake scan results:")
156  for scan in getScanResults():
157    print(scan.decode())
158  print("** Inserting fake configurations (and optionally suggestions)")
159  fake_configs()
160  print("** Pre action saved networks:")
161  for config in getSavedConfigs():
162    print(config.decode())
163  if ADD_IDENTICAL_SUGGESTION or ADD_SUGGESTION_INSTEAD_OF_CONFIG:
164    print("** Pre action suggestions:")
165    for sugg in getSuggestions():
166      print(sugg.decode())
167  print("---> " + pre_instructions)
168  input("** Then press Enter to continue ...")
169  print("** Post action saved networks:")
170  for config in getSavedConfigs():
171    print(config.decode())
172  print("---> " + post_instructions)
173  input("** Press Enter to continue ...")
174
175#
176# Single WPA2 scan: different WPA2/WPA3 configurations
177#
178
179def testWpa2ScanNoConfigs() -> None:
180  testTemplate(
181    testWpa2ScanNoConfigs.__name__,
182    lambda : addFakeWpa2Scan(WPA2_SSID, "80:01:02:03:04:05"),
183    lambda : print("no configs added"),
184    "Open picker: select " + WPA2_SSID,
185    "Should ask for passphrase, then be WPA2 (+ WPA3^ if device supports auto-upgrade)"
186  )
187
188def testWpa2ScanWpa2Config() -> None:
189  testTemplate(
190    testWpa2ScanWpa2Config.__name__,
191    lambda : addFakeWpa2Scan(WPA2_SSID, "80:01:02:03:04:05"),
192    lambda : addWpa2Config(WPA2_SSID),
193    "Open picker: select " + WPA2_SSID,
194    "Should not ask for passphrase, then be WPA2 only if device does not support auto-upgrade (otherwise + WPA3^)"
195  )
196
197def testWpa2ScanWpa3Config() -> None:
198  testTemplate(
199    testWpa2ScanWpa3Config.__name__,
200    lambda : addFakeWpa2Scan(WPA2_SSID, "80:01:02:03:04:05"),
201    lambda : addWpa3Config(WPA2_SSID),
202    "Open picker: select " + WPA2_SSID,
203    "Should ask for passphrase, then be WPA2+WPA3"
204  )
205
206def testWpa2ScanWpa2Wpa3Config() -> None:
207  testTemplate(
208    testWpa2ScanWpa2Wpa3Config.__name__,
209    lambda : addFakeWpa2Scan(WPA2_SSID, "80:01:02:03:04:05"),
210    lambda : (addWpa2Config(WPA2_SSID),
211              addWpa3Config(WPA2_SSID)),
212    "Open picker: select " + WPA2_SSID,
213    "Should not ask for passphrase, then be WPA2+WPA3"
214  )
215
216#
217# Single WPA3 scan: different WPA2/WPA3 configurations
218#
219
220def testWpa3ScanNoConfigs() -> None:
221  testTemplate(
222    testWpa3ScanNoConfigs.__name__,
223    lambda : addFakeWpa3Scan(WPA3_SSID, "80:01:02:03:04:05"),
224    lambda : print("no configs added"),
225    "Open picker: select " + WPA3_SSID,
226    "Should ask for passphrase, then be WPA3"
227  )
228
229def testWpa3ScanWpa2Config() -> None:
230  testTemplate(
231    testWpa3ScanWpa2Config.__name__,
232    lambda : addFakeWpa3Scan(WPA3_SSID, "80:01:02:03:04:05"),
233    lambda : addWpa2Config(WPA3_SSID),
234    "Open picker: select " + WPA3_SSID,
235    "If no auto-upgrade: Should ask for passphrase, then be WPA2 + WPA3, otherwise should not ask for passphrase and be WPA2+WPA3^"
236  )
237
238def testWpa3ScanWpa3Config() -> None:
239  testTemplate(
240    testWpa3ScanWpa3Config.__name__,
241    lambda : addFakeWpa3Scan(WPA3_SSID, "80:01:02:03:04:05"),
242    lambda : addWpa3Config(WPA3_SSID),
243    "Open picker: select " + WPA3_SSID,
244    "Should not ask for passphrase, then be WPA3"
245  )
246
247def testWpa3ScanWpa2Wpa3Config() -> None:
248  testTemplate(
249    testWpa3ScanWpa2Wpa3Config.__name__,
250    lambda : addFakeWpa3Scan(WPA3_SSID, "80:01:02:03:04:05"),
251    lambda : (addWpa2Config(WPA3_SSID),
252              addWpa3Config(WPA3_SSID)),
253    "Open picker: select " + WPA3_SSID,
254    "Should not ask for passphrase, then be WPA2+WPA3"
255  )
256
257#
258# Single WPA2/WPA3 scan: different WPA2/WPA3 configurations
259#
260
261def testWpa2Wpa3ScanNoConfigs() -> None:
262  testTemplate(
263    testWpa2Wpa3ScanNoConfigs.__name__,
264    lambda : addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
265    lambda : print("no configs added"),
266    "Open picker: select " + WPA2_WPA3_SSID,
267    "Should ask for passphrase, then be WPA2 if no auto-upgrade, WPA2+WPA3^ if auto-upgrade"
268  )
269
270def testWpa2Wpa3ScanWpa2Config() -> None:
271  testTemplate(
272    testWpa2Wpa3ScanWpa2Config.__name__,
273    lambda : addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
274    lambda : addWpa2Config(WPA2_WPA3_SSID),
275    "Open picker: select " + WPA2_WPA3_SSID,
276    "Should not ask for passphrase, then be WPA2 if no auto-upgrade (otherwise WPA2+WPA3^)"
277  )
278
279def testWpa2Wpa3ScanWpa3Config() -> None:
280  testTemplate(
281    testWpa2Wpa3ScanWpa3Config.__name__,
282    lambda : addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
283    lambda : addWpa3Config(WPA2_WPA3_SSID),
284    "Open picker: select " + WPA2_WPA3_SSID,
285    "Should not ask for passphrase, then be WPA3"
286  )
287
288def testWpa2Wpa3ScanWpa2Wpa3Config() -> None:
289  testTemplate(
290    testWpa2Wpa3ScanWpa2Wpa3Config.__name__,
291    lambda : addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
292    lambda : (addWpa2Config(WPA2_WPA3_SSID),
293              addWpa3Config(WPA2_WPA3_SSID)),
294    "Open picker: select " + WPA2_WPA3_SSID,
295    "Should not ask for passphrase, then be WPA2+WPA3"
296  )
297
298#
299# Single WPA2 scan and single WPA3 scan: different WPA2/WPA3 configurations
300#
301
302def testWpa2ScanWpa3ScanNoConfigs() -> None:
303  testTemplate(
304    testWpa2ScanWpa3ScanNoConfigs.__name__,
305    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
306              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
307    lambda : print("no configs added"),
308    "Open picker: select " + WPA2_WPA3_SSID,
309    "Should ask for passphrase, then be WPA2 if no auto-upgrade, WPA2+WPA3^ if auto-upgrade"
310  )
311
312def testWpa2ScanWpa3ScanWpa2Config() -> None:
313  testTemplate(
314    testWpa2ScanWpa3ScanWpa2Config.__name__,
315    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
316              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
317    lambda : addWpa2Config(WPA2_WPA3_SSID),
318    "Open picker: select " + WPA2_WPA3_SSID,
319    "Should not ask for passphrase, then be WPA2 if no auto-upgrade (otherwise WPA2+WPA3^)"
320  )
321
322def testWpa2ScanWpa3ScanWpa3Config() -> None:
323  testTemplate(
324    testWpa2ScanWpa3ScanWpa3Config.__name__,
325    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
326              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
327    lambda : addWpa3Config(WPA2_WPA3_SSID),
328    "Open picker: select " + WPA2_WPA3_SSID,
329    "Should not ask for passphrase, then be WPA3"
330  )
331
332def testWpa2ScanWpa3ScanWpa2Wpa3Config() -> None:
333  testTemplate(
334    testWpa2ScanWpa3ScanWpa2Wpa3Config.__name__,
335    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
336              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
337    lambda : (addWpa2Config(WPA2_WPA3_SSID),
338              addWpa3Config(WPA2_WPA3_SSID)),
339    "Open picker: select " + WPA2_WPA3_SSID,
340    "Should not ask for passphrase, then be WPA2+WPA3"
341  )
342
343#
344# Single WPA2 scan and single WPA2+WPA3 transition scan: different WPA2/WPA3 configurations
345#
346
347def testWpa2ScanWpa2Wpa3ScanNoConfigs() -> None:
348  testTemplate(
349    testWpa2ScanWpa2Wpa3ScanNoConfigs.__name__,
350    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
351              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
352    lambda : print("no configs added"),
353    "Open picker: select " + WPA2_WPA3_SSID,
354    "Should ask for passphrase, then be WPA2 if no auto-upgrade, WPA2+WPA3^ if auto-upgrade"
355  )
356
357def testWpa2ScanWpa2Wpa3ScanWpa2Config() -> None:
358  testTemplate(
359    testWpa2ScanWpa2Wpa3ScanWpa2Config.__name__,
360    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
361              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
362    lambda : addWpa2Config(WPA2_WPA3_SSID),
363    "Open picker: select " + WPA2_WPA3_SSID,
364    "Should not ask for passphrase, then be WPA2 if no auto-upgrade (otherwise WPA2+WPA3^)"
365  )
366
367def testWpa2ScanWpa2Wpa3ScanWpa3Config() -> None:
368  testTemplate(
369    testWpa2ScanWpa2Wpa3ScanWpa3Config.__name__,
370    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
371              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
372    lambda : addWpa3Config(WPA2_WPA3_SSID),
373    "Open picker: select " + WPA2_WPA3_SSID,
374    "Should not ask for passphrase, then be WPA3"
375  )
376
377def testWpa2ScanWpa2Wpa3ScanWpa2Wpa3Config() -> None:
378  testTemplate(
379    testWpa2ScanWpa2Wpa3ScanWpa2Wpa3Config.__name__,
380    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
381              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
382    lambda : (addWpa2Config(WPA2_WPA3_SSID),
383              addWpa3Config(WPA2_WPA3_SSID)),
384    "Open picker: select " + WPA2_WPA3_SSID,
385    "Should not ask for passphrase, then be WPA2+WPA3"
386  )
387
388#
389# Single WPA2+WPA3 transition scan and single WPA3 scan: different WPA2/WPA3 configurations
390#
391
392def testWpa2Wpa3ScanWpa3ScanNoConfigs() -> None:
393  testTemplate(
394    testWpa2Wpa3ScanWpa3ScanNoConfigs.__name__,
395    lambda : (addFakeWpa3Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
396              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
397    lambda : print("no configs added"),
398    "Open picker: select " + WPA2_WPA3_SSID,
399    "Should ask for passphrase, then be WPA2 if no auto-upgrade, WPA2+WPA3^ if auto-upgrade"
400  )
401
402def testWpa2Wpa3ScanWpa3ScanWpa2Config() -> None:
403  testTemplate(
404    testWpa2Wpa3ScanWpa3ScanWpa2Config.__name__,
405    lambda : (addFakeWpa3Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
406              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
407    lambda : addWpa2Config(WPA2_WPA3_SSID),
408    "Open picker: select " + WPA2_WPA3_SSID,
409    "Should not ask for passphrase, then be WPA2 if no auto-upgrade (otherwise WPA2+WPA3^)"
410  )
411
412def testWpa2Wpa3ScanWpa3ScanWpa3Config() -> None:
413  testTemplate(
414    testWpa2Wpa3ScanWpa3ScanWpa3Config.__name__,
415    lambda : (addFakeWpa3Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
416              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
417    lambda : addWpa3Config(WPA2_WPA3_SSID),
418    "Open picker: select " + WPA2_WPA3_SSID,
419    "Should not ask for passphrase, then be WPA3"
420  )
421
422def testWpa2Wpa3ScanWpa3ScanWpa2Wpa3Config() -> None:
423  testTemplate(
424    testWpa2Wpa3ScanWpa3ScanWpa2Wpa3Config.__name__,
425    lambda : (addFakeWpa3Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
426              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:02:02:03:04:06")),
427    lambda : (addWpa2Config(WPA2_WPA3_SSID),
428              addWpa3Config(WPA2_WPA3_SSID)),
429    "Open picker: select " + WPA2_WPA3_SSID,
430    "Should not ask for passphrase, then be WPA2+WPA3"
431  )
432
433#
434# Single WPA2 scan, single WPA2+WPA3 transition scan, and single WPA3 scan: different WPA2/WPA3 configurations
435#
436
437def testWpa2ScanWpa2Wpa3ScanWpa3ScanNoConfigs() -> None:
438  testTemplate(
439    testWpa2ScanWpa2Wpa3ScanWpa3ScanNoConfigs.__name__,
440    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
441              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06"),
442              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:03:02:03:04:07")),
443    lambda : print("no configs added"),
444    "Open picker: select " + WPA2_WPA3_SSID,
445    "Should ask for passphrase, then be WPA2 if no auto-upgrade, WPA2+WPA3^ if auto-upgrade"
446  )
447
448def testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa2Config() -> None:
449  testTemplate(
450    testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa2Config.__name__,
451    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
452              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06"),
453              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:03:02:03:04:07")),
454    lambda : addWpa2Config(WPA2_WPA3_SSID),
455    "Open picker: select " + WPA2_WPA3_SSID,
456    "Should not ask for passphrase, then be WPA2 if no auto-upgrade (otherwise WPA2+WPA3^)"
457  )
458
459def testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa3Config() -> None:
460  testTemplate(
461    testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa3Config.__name__,
462    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
463              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06"),
464              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:03:02:03:04:07")),
465    lambda : addWpa3Config(WPA2_WPA3_SSID),
466    "Open picker: select " + WPA2_WPA3_SSID,
467    "Should not ask for passphrase, then be WPA3"
468  )
469
470def testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa2Wpa3Config() -> None:
471  testTemplate(
472    testWpa2ScanWpa2Wpa3ScanWpa3ScanWpa2Wpa3Config.__name__,
473    lambda : (addFakeWpa2Scan(WPA2_WPA3_SSID, "80:01:02:03:04:05"),
474              addFakeWpa3Scan(WPA2_WPA3_SSID, "80:02:02:03:04:06"),
475              addFakeWpa2Wpa3TransitionScan(WPA2_WPA3_SSID, "80:03:02:03:04:07")),
476    lambda : (addWpa2Config(WPA2_WPA3_SSID),
477              addWpa3Config(WPA2_WPA3_SSID)),
478    "Open picker: select " + WPA2_WPA3_SSID,
479    "Should not ask for passphrase, then be WPA2+WPA3"
480  )
481
482#
483# Single open scan: different open/OWE configurations
484#
485
486def testOpenScanNoConfigs() -> None:
487  testTemplate(
488    testOpenScanNoConfigs.__name__,
489    lambda : addFakeOpenScan(OPEN_SSID, "80:01:02:03:04:05"),
490    lambda : print("no configs added"),
491    "Open picker: select " + OPEN_SSID,
492    "Should be Open + OWE^ if device supports auto-upgrade"
493  )
494
495def testOpenScanOpenConfig() -> None:
496  testTemplate(
497    testOpenScanOpenConfig.__name__,
498    lambda : addFakeOpenScan(OPEN_SSID, "80:01:02:03:04:05"),
499    lambda : addOpenConfig(OPEN_SSID),
500    "Open picker: select " + OPEN_SSID,
501    "Should be Open only if device does not support auto-upgrade (otherwise + OWE^)"
502  )
503
504def testOpenScanOweConfig() -> None:
505  testTemplate(
506    testOpenScanOweConfig.__name__,
507    lambda : addFakeOpenScan(OPEN_SSID, "80:01:02:03:04:05"),
508    lambda : addOweConfig(OPEN_SSID),
509    "Open picker: select " + OPEN_SSID,
510    "Should be Open + OWE"
511  )
512
513def testOpenScanOpenOweConfig() -> None:
514  testTemplate(
515    testOpenScanOpenOweConfig.__name__,
516    lambda : addFakeOpenScan(OPEN_SSID, "80:01:02:03:04:05"),
517    lambda : (addOpenConfig(OPEN_SSID),
518              addOweConfig(OPEN_SSID)),
519    "Open picker: select " + OPEN_SSID,
520    "Should be Open + OWE"
521  )
522
523#
524# Single OWE scan: different open/OWE configurations
525#
526
527def testOweScanNoConfigs() -> None:
528  testTemplate(
529    testOweScanNoConfigs.__name__,
530    lambda : addFakeOweScan(OWE_SSID, "80:01:02:03:04:05"),
531    lambda : print("no configs added"),
532    "Open picker: select " + OWE_SSID,
533    "Should be OWE"
534  )
535
536def testOweScanOpenConfig() -> None:
537  testTemplate(
538    testOweScanOpenConfig.__name__,
539    lambda : addFakeOweScan(OWE_SSID, "80:01:02:03:04:05"),
540    lambda : addOpenConfig(OWE_SSID),
541    "Open picker: select " + OWE_SSID,
542    "Should be Open+OWE^"
543  )
544
545def testOweScanOweConfig() -> None:
546  testTemplate(
547    testOweScanOweConfig.__name__,
548    lambda : addFakeOweScan(OWE_SSID, "80:01:02:03:04:05"),
549    lambda : addOweConfig(OWE_SSID),
550    "Open picker: select " + OWE_SSID,
551    "Should be OWE"
552  )
553
554def testOweScanOpenOweConfig() -> None:
555  testTemplate(
556    testOweScanOpenOweConfig.__name__,
557    lambda : addFakeOweScan(OWE_SSID, "80:01:02:03:04:05"),
558    lambda : (addOpenConfig(OWE_SSID),
559              addOweConfig(OWE_SSID)),
560    "Open picker: select " + OWE_SSID,
561    "Should be Open + OWE"
562  )
563
564#
565# Single Open/OWE transition mode scan: different open/OWE configurations
566#
567
568def testOpenOweScanNoConfigs() -> None:
569  testTemplate(
570    testOpenOweScanNoConfigs.__name__,
571    lambda : addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
572    lambda : print("no configs added"),
573    "Open picker: select " + OPEN_OWE_SSID,
574    "Should be Open+OWE"
575  )
576
577def testOpenOweScanOpenConfig() -> None:
578  testTemplate(
579    testOpenOweScanOpenConfig.__name__,
580    lambda : addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
581    lambda : addOpenConfig(OPEN_OWE_SSID),
582    "Open picker: select " + OPEN_OWE_SSID,
583    "Should be Open+OWE^"
584  )
585
586def testOpenOweScanOweConfig() -> None:
587  testTemplate(
588    testOpenOweScanOweConfig.__name__,
589    lambda : addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
590    lambda : addOweConfig(OPEN_OWE_SSID),
591    "Open picker: select " + OPEN_OWE_SSID,
592    "Should be OWE"
593  )
594
595def testOpenOweScanOpenOweConfig() -> None:
596  testTemplate(
597    testOpenOweScanOpenOweConfig.__name__,
598    lambda : addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
599    lambda : (addOpenConfig(OPEN_OWE_SSID),
600              addOweConfig(OPEN_OWE_SSID)),
601    "Open picker: select " + OPEN_OWE_SSID,
602    "Should be Open + OWE"
603  )
604
605#
606# Single Open scan and single OWE scan: different Open/OWE configurations
607#
608
609def testOpenScanOweScanNoConfigs() -> None:
610  testTemplate(
611    testOpenScanOweScanNoConfigs.__name__,
612    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
613              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
614    lambda : print("no configs added"),
615    "Open picker: select " + OPEN_OWE_SSID,
616    "Should be Open+OWE"
617  )
618
619def testOpenScanOweScanOpenConfig() -> None:
620  testTemplate(
621    testOpenScanOweScanOpenConfig.__name__,
622    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
623              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
624    lambda : addOpenConfig(OPEN_OWE_SSID),
625    "Open picker: select " + OPEN_OWE_SSID,
626    "Should be Open if no auto-upgrade (otherwise Open+OWE^)"
627  )
628
629def testOpenScanOweScanOweConfig() -> None:
630  testTemplate(
631    testOpenScanOweScanOweConfig.__name__,
632    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
633              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
634    lambda : addOweConfig(OPEN_OWE_SSID),
635    "Open picker: select " + OPEN_OWE_SSID,
636    "Should be OWE"
637  )
638
639def testOpenScanOweScanOpenOweConfig() -> None:
640  testTemplate(
641    testOpenScanOweScanOpenOweConfig.__name__,
642    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
643              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
644    lambda : (addOpenConfig(OPEN_OWE_SSID),
645              addOweConfig(OPEN_OWE_SSID)),
646    "Open picker: select " + OPEN_OWE_SSID,
647    "Should be Open+OWE"
648  )
649
650#
651# Single Open scan and single Open+OWE transition scan: different Open/OWE configurations
652#
653
654def testOpenScanOpenOweScanNoConfigs() -> None:
655  testTemplate(
656    testOpenScanOpenOweScanNoConfigs.__name__,
657    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
658              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
659    lambda : print("no configs added"),
660    "Open picker: select " + OPEN_OWE_SSID,
661    "Should be Open+OWE"
662  )
663
664def testOpenScanOpenOweScanOpenConfig() -> None:
665  testTemplate(
666    testOpenScanOpenOweScanOpenConfig.__name__,
667    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
668              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
669    lambda : addOpenConfig(OPEN_OWE_SSID),
670    "Open picker: select " + OPEN_OWE_SSID,
671    "Should be Open if no auto-upgrade (otherwise Open+OWE^)"
672  )
673
674def testOpenScanOpenOweScanOweConfig() -> None:
675  testTemplate(
676    testOpenScanOpenOweScanOweConfig.__name__,
677    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
678              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
679    lambda : addOweConfig(OPEN_OWE_SSID),
680    "Open picker: select " + OPEN_OWE_SSID,
681    "Should be OWE"
682  )
683
684def testOpenScanOpenOweScanOpenOweConfig() -> None:
685  testTemplate(
686    testOpenScanOpenOweScanOpenOweConfig.__name__,
687    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
688              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
689    lambda : (addOpenConfig(OPEN_OWE_SSID),
690              addOweConfig(OPEN_OWE_SSID)),
691    "Open picker: select " + OPEN_OWE_SSID,
692    "Should be Open+OWE"
693  )
694
695#
696# Single Open+OWE transition scan and single OWE scan: different Open/OWE configurations
697#
698
699def testOpenOweScanOweScanNoConfigs() -> None:
700  testTemplate(
701    testOpenOweScanOweScanNoConfigs.__name__,
702    lambda : (addFakeOweScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
703              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
704    lambda : print("no configs added"),
705    "Open picker: select " + OPEN_OWE_SSID,
706    "Should be Open+OWE"
707  )
708
709def testOpenOweScanOweScanOpenConfig() -> None:
710  testTemplate(
711    testOpenOweScanOweScanOpenConfig.__name__,
712    lambda : (addFakeOweScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
713              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
714    lambda : addOpenConfig(OPEN_OWE_SSID),
715    "Open picker: select " + OPEN_OWE_SSID,
716    "Should be Open if no auto-upgrade (otherwise Open+OWE^)"
717  )
718
719def testOpenOweScanOweScanOweConfig() -> None:
720  testTemplate(
721    testOpenOweScanOweScanOweConfig.__name__,
722    lambda : (addFakeOweScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
723              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
724    lambda : addOweConfig(OPEN_OWE_SSID),
725    "Open picker: select " + OPEN_OWE_SSID,
726    "Should be OWE"
727  )
728
729def testOpenOweScanOweScanOpenOweConfig() -> None:
730  testTemplate(
731    testOpenOweScanOweScanOpenOweConfig.__name__,
732    lambda : (addFakeOweScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
733              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:02:02:03:04:06")),
734    lambda : (addOpenConfig(OPEN_OWE_SSID),
735              addOweConfig(OPEN_OWE_SSID)),
736    "Open picker: select " + OPEN_OWE_SSID,
737    "Should be Open+OWE"
738  )
739
740#
741# Single Open scan, single Open+OWE transition scan, and single OWE scan: different Open/OWE configurations
742#
743
744def testOpenScanOpenOweScanOweScanNoConfigs() -> None:
745  testTemplate(
746    testOpenScanOpenOweScanOweScanNoConfigs.__name__,
747    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
748              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06"),
749              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:03:02:03:04:07")),
750    lambda : print("no configs added"),
751    "Open picker: select " + OPEN_OWE_SSID,
752    "Should be Open+OWE"
753  )
754
755def testOpenScanOpenOweScanOweScanOpenConfig() -> None:
756  testTemplate(
757    testOpenScanOpenOweScanOweScanOpenConfig.__name__,
758    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
759              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06"),
760              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:03:02:03:04:07")),
761    lambda : addOpenConfig(OPEN_OWE_SSID),
762    "Open picker: select " + OPEN_OWE_SSID,
763    "Should be Open if no auto-upgrade (otherwise Open+OWE^)"
764  )
765
766def testOpenScanOpenOweScanOweScanOweConfig() -> None:
767  testTemplate(
768    testOpenScanOpenOweScanOweScanOweConfig.__name__,
769    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
770              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06"),
771              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:03:02:03:04:07")),
772    lambda : addOweConfig(OPEN_OWE_SSID),
773    "Open picker: select " + OPEN_OWE_SSID,
774    "Should be OWE"
775  )
776
777def testOpenScanOpenOweScanOweScanOpenOweConfig() -> None:
778  testTemplate(
779    testOpenScanOpenOweScanOweScanOpenOweConfig.__name__,
780    lambda : (addFakeOpenScan(OPEN_OWE_SSID, "80:01:02:03:04:05"),
781              addFakeOweScan(OPEN_OWE_SSID, "80:02:02:03:04:06"),
782              addFakeOpenOweTransitionScan(OPEN_OWE_SSID, "80:03:02:03:04:07")),
783    lambda : (addOpenConfig(OPEN_OWE_SSID),
784              addOweConfig(OPEN_OWE_SSID)),
785    "Open picker: select " + OPEN_OWE_SSID,
786    "Should be Open+OWE"
787  )
788
789def root() -> None:
790  subprocess.run(["adb", "root"])
791
792def settings_reset() -> None:
793  subprocess.run(["adb", "shell", "cmd", "wifi", "settings-reset"])
794
795def getSavedConfigs() -> str:
796  return subprocess.check_output(["adb", "shell", "cmd", "wifi", "list-networks"]).splitlines()
797
798def getSuggestions() -> str:
799  return subprocess.check_output(["adb", "shell", "cmd", "wifi", "list-suggestions"]).splitlines()
800
801def getScanResults() -> str:
802  return subprocess.check_output(["adb", "shell", "cmd", "wifi", "list-scan-results"]).splitlines()
803
804def startScan() -> None:
805  subprocess.run(["adb", "shell", "cmd", "wifi", "start-scan"])
806
807def startFakingScans() -> None:
808  subprocess.run(["adb", "shell", "cmd", "wifi", "start-faking-scans"])
809
810def resetFakeScans() -> None:
811  subprocess.run(["adb", "shell", "cmd", "wifi", "reset-fake-scans"])
812
813#
814# Add fake scans
815#
816
817def addFakeScan(ssid: str, bssid: str, cap: str, freq: int = 2412, dbm: int = -55) -> None:
818  subprocess.run(["adb", "shell", "cmd", "wifi", "add-fake-scan", ssid, bssid, cap, str(freq), str(dbm)])
819
820def addFakeOpenScan(ssid: str, bssid: str, freq: int = 2412, dbm: int = -55) -> None:
821  addFakeScan(ssid, bssid, "[ESS]", freq, dbm)
822
823def addFakeWpa2Scan(ssid: str, bssid: str, freq: int = 2412, dbm: int = -55) -> None:
824  addFakeScan(ssid, bssid, "[WPA2-PSK-CCMP][RSN-PSK-CCMP][ESS]", freq, dbm)
825
826def addFakeWpa3Scan(ssid: str, bssid: str, freq: int = 2412, dbm: int = -55) -> None:
827  addFakeScan(ssid, bssid, "[RSN-SAE+FT/SAE-CCMP][ESS]", freq, dbm)
828
829def addFakeOweScan(ssid: str, bssid: str, freq: int = 2412, dbm: int = -55) -> None:
830  addFakeScan(ssid, bssid, "[RSN-OWE-CCMP]", freq, dbm)
831
832def addFakeWpa2Wpa3TransitionScan(ssid: str, bssid: str, freq: int = 2412, dbm: int = -55) -> None:
833  addFakeScan(ssid, bssid, "[WPA2-PSK-CCMP][RSN-PSK+SAE-CCMP][ESS][MFPC]", freq, dbm)
834
835def addFakeOpenOweTransitionScan(ssid: str, bssid: str, freq: int = 2412, dbm: int = -55) -> None:
836  addFakeScan(ssid, bssid, "[RSN-OWE_TRANSITION-CCMP][ESS]", freq, dbm)
837
838def addFakePasspointScan(ssid: str, bssid: str, freq: int = 2412, dbm: int = -55) -> None:
839  addFakeScan(ssid, bssid, "[WPA2-EAP/SHA1-CCMP][RSN-EAP/SHA1-CCMP][ESS][MFPR][MFPC][PASSPOINT]", freq, dbm)
840
841#
842# Add configs
843#
844
845def addOpenConfig(ssid: str) -> None:
846  if not ADD_SUGGESTION_INSTEAD_OF_CONFIG:
847    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "open"])
848  if ADD_IDENTICAL_SUGGESTION or ADD_SUGGESTION_INSTEAD_OF_CONFIG:
849    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "open"])
850
851def addOweConfig(ssid: str) -> None:
852  if not ADD_SUGGESTION_INSTEAD_OF_CONFIG:
853    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "owe"])
854  if ADD_IDENTICAL_SUGGESTION or ADD_SUGGESTION_INSTEAD_OF_CONFIG:
855    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "owe"])
856
857def addOpenOweConfig(ssid: str) -> None:
858  if not ADD_SUGGESTION_INSTEAD_OF_CONFIG:
859    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "open"])
860    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "owe"])
861  if ADD_IDENTICAL_SUGGESTION or ADD_SUGGESTION_INSTEAD_OF_CONFIG:
862    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "open"])
863    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "owe"])
864
865def addWpa2Config(ssid: str) -> None:
866  if not ADD_SUGGESTION_INSTEAD_OF_CONFIG:
867    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "wpa2", "SomePassphrase"])
868  if ADD_IDENTICAL_SUGGESTION or ADD_SUGGESTION_INSTEAD_OF_CONFIG:
869    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "wpa2", "SomePassphrase", "-s"])
870
871def addWpa3Config(ssid: str) -> None:
872  if not ADD_SUGGESTION_INSTEAD_OF_CONFIG:
873    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "wpa3", "SomePassphrase"])
874  if ADD_IDENTICAL_SUGGESTION or ADD_SUGGESTION_INSTEAD_OF_CONFIG:
875    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "wpa3", "SomePassphrase", "-s"])
876
877def addWpa2Wpa3Config(ssid: str) -> None:
878  if not ADD_SUGGESTION_INSTEAD_OF_CONFIG:
879    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "wpa2", "SomePassphrase"])
880    subprocess.run(["adb", "shell", "cmd", "wifi", "add-network", ssid, "wpa3", "SomePassphrase"])
881  if ADD_IDENTICAL_SUGGESTION or ADD_SUGGESTION_INSTEAD_OF_CONFIG:
882    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "wpa2", "SomePassphrase", "-s"])
883    subprocess.run(["adb", "shell", "cmd", "wifi", "add-suggestion", ssid, "wpa3", "SomePassphrase", "-s"])
884
885if __name__ == '__main__':
886    exit_code = main()
887    sys.exit(exit_code)
888
889
890
891