Skip to content

Commit a9071a6

Browse files
committed
WiFi Test: Add cleanup logic and improve hybrid connection handling
- Register a cleanup() function to terminate background wpa_supplicant processes - Ensure WPA config is temporary and removed post test - Improve robustness of fallback to wpa_supplicant + udhcpc if nmcli fails - Ensure IP and internet connectivity checks are logged - Preserve original argument/env/file logic for SSID/password - Maintain logging, result output, and functestlib usage Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent cb65b42 commit a9071a6

File tree

4 files changed

+373
-0
lines changed

4 files changed

+373
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# WiFi Connectivity Validation
2+
3+
## 📋 Overview
4+
5+
This test validates WiFi functionality by:
6+
7+
- Connecting to an access point (AP) using either `nmcli` or `wpa_supplicant`.
8+
- Verifying IP acquisition via DHCP.
9+
- Checking internet connectivity with a `ping` test.
10+
- Handling systemd network service status.
11+
- Supporting flexible SSID/password input via arguments, environment, or file.
12+
13+
## ✅ SSID/PASSWORD Input Priority (Hybrid Approach)
14+
15+
1. **Command-line arguments**:
16+
```sh
17+
./run.sh "MySSID" "MyPassword"
18+
```
19+
20+
2. **Environment variables**:
21+
```sh
22+
SSID_ENV=MySSID PASSWORD_ENV=MyPassword ./run.sh
23+
```
24+
25+
3. **Fallback to `ssid_list.txt` file** (if above not set):
26+
```txt
27+
MySSID MyPassword
28+
```
29+
30+
## ⚙️ Supported Tools
31+
32+
- Primary: `nmcli`
33+
- Fallback: `wpa_supplicant`, `udhcpc`, `ifconfig`
34+
35+
Ensure these tools are available in the system before running the test. Missing tools are detected and logged as skipped/failure.
36+
37+
## 🧪 Test Flow
38+
39+
1. **Dependency check** – verifies necessary binaries are present.
40+
2. **Systemd services check** – attempts to start network services if inactive.
41+
3. **WiFi connect (nmcli or wpa_supplicant)** – based on tool availability.
42+
4. **IP assignment check** – validates `ifconfig wlan0` output.
43+
5. **Internet test** – pings `8.8.8.8` to confirm outbound reachability.
44+
6. **Result logging** – writes `.res` file and logs all actions.
45+
46+
## 🧾 Output
47+
48+
- `WiFi_Connectivity.res`: Contains `WiFi_Connectivity PASS` or `FAIL`.
49+
- Logs are printed using `log_info`, `log_pass`, and `log_fail` from `functestlib.sh`.
50+
51+
## 📂 Directory Structure
52+
53+
```
54+
WiFi/
55+
├── run.sh
56+
├── ssid_list.txt (optional)
57+
├── README.md
58+
```
59+
60+
## 🌐 Integration (meta-qcom_PreMerge.yaml)
61+
62+
Add this test with SSID parameters as follows:
63+
64+
```yaml
65+
- name: WiFi_Connectivity
66+
path: Runner/suites/Connectivity/WiFi
67+
timeout:
68+
minutes: 5
69+
params:
70+
SSID_ENV: "xxxx"
71+
PASSWORD_ENV: "xxxx"
72+
```
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Robustly find and source init_env
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
INIT_ENV=""
9+
SEARCH="$SCRIPT_DIR"
10+
while [ "$SEARCH" != "/" ]; do
11+
if [ -f "$SEARCH/init_env" ]; then
12+
INIT_ENV="$SEARCH/init_env"
13+
break
14+
fi
15+
SEARCH=$(dirname "$SEARCH")
16+
done
17+
18+
if [ -z "$INIT_ENV" ]; then
19+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
20+
exit 1
21+
fi
22+
23+
# shellcheck disable=SC1090
24+
if [ -z "$__INIT_ENV_LOADED" ]; then
25+
. "$INIT_ENV"
26+
fi
27+
28+
# shellcheck disable=SC1090,SC1091
29+
. "$TOOLS/functestlib.sh"
30+
31+
TESTNAME="WiFi"
32+
res_file="./$TESTNAME.res"
33+
test_path=$(find_test_case_by_name "$TESTNAME")
34+
cd "$test_path" || exit 1
35+
36+
log_info "-------------------------------------------------------------"
37+
log_info "------------------- Starting $TESTNAME Test -----------------"
38+
39+
# Fetch credentials from function; expects functestlib.sh to have get_wifi_credentials
40+
if ! CRED=$(get_wifi_credentials "$1" "$2") || [ -z "$CRED" ]; then
41+
log_fail "SSID and password not provided via argument, env, or file."
42+
echo "$TESTNAME FAIL" > "$res_file"
43+
exit 1
44+
fi
45+
SSID=$(echo "$CRED" | awk '{print $1}')
46+
PASSWORD=$(echo "$CRED" | awk '{print $2}')
47+
if [ -z "$SSID" ] || [ -z "$PASSWORD" ]; then
48+
log_fail "SSID and password could not be extracted."
49+
echo "$TESTNAME FAIL" > "$res_file"
50+
exit 1
51+
fi
52+
53+
# Define cleanup function
54+
cleanup() {
55+
log_info "Cleaning up WiFi test environment..."
56+
killall -q wpa_supplicant 2>/dev/null
57+
rm -f /tmp/wpa_supplicant.conf nmcli.log wpa.log
58+
if command -v ip >/dev/null 2>&1; then
59+
for iface in $(ip link | awk -F: '/ wl/ {print $2}' | tr -d ' '); do
60+
ip link set "$iface" down 2>/dev/null
61+
done
62+
else
63+
ifconfig wlan0 down 2>/dev/null
64+
fi
65+
}
66+
67+
# Check for ping, nmcli, wpa_supplicant, udhcpc etc
68+
check_net_tools || {
69+
log_fail "Core network utilities missing."
70+
echo "$TESTNAME FAIL" > "$res_file"
71+
exit 1
72+
}
73+
check_dependencies ping nmcli wpa_supplicant udhcpc
74+
75+
# Systemd service check if systemd exists (ok to skip on kernel-only build)
76+
check_systemd_services systemd-networkd.service
77+
78+
# Find WiFi interface dynamically
79+
WIFI_IF=$(get_wifi_interface)
80+
if [ -z "$WIFI_IF" ]; then
81+
log_fail "No WiFi interface found"
82+
echo "$TESTNAME FAIL" > "$res_file"
83+
exit 1
84+
fi
85+
log_info "Using WiFi interface: $WIFI_IF"
86+
87+
# Try nmcli first if available
88+
if command -v nmcli >/dev/null 2>&1; then
89+
log_info "Trying to connect using nmcli..."
90+
if nmcli dev wifi connect "$SSID" password "$PASSWORD" ifname "$WIFI_IF" 2>&1 | tee nmcli.log; then
91+
log_pass "Connected to $SSID using nmcli"
92+
IP=$(get_ip_address "$WIFI_IF")
93+
log_info "IP Address: $IP"
94+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
95+
log_pass "Internet connectivity verified via ping"
96+
echo "$TESTNAME PASS" > "$res_file"
97+
cleanup
98+
exit 0
99+
else
100+
log_fail "Ping test failed after nmcli connection"
101+
fi
102+
else
103+
log_warn "nmcli failed to connect. Will try fallback."
104+
fi
105+
fi
106+
107+
# Fallback to wpa_supplicant + udhcpc if available
108+
if command -v wpa_supplicant >/dev/null 2>&1 && command -v udhcpc >/dev/null 2>&1; then
109+
log_info "Falling back to wpa_supplicant + udhcpc"
110+
WPA_CONF="/tmp/wpa_supplicant.conf"
111+
{
112+
echo "ctrl_interface=/var/run/wpa_supplicant"
113+
echo "network={"
114+
echo " ssid=\"$SSID\""
115+
echo " key_mgmt=WPA-PSK"
116+
echo " pairwise=CCMP TKIP"
117+
echo " group=CCMP TKIP"
118+
echo " psk=\"$PASSWORD\""
119+
echo "}"
120+
} > "$WPA_CONF"
121+
122+
killall -q wpa_supplicant 2>/dev/null
123+
wpa_supplicant -B -i "$WIFI_IF" -D nl80211 -c "$WPA_CONF" 2>&1 | tee wpa.log
124+
sleep 4
125+
udhcpc -i "$WIFI_IF" >/dev/null 2>&1
126+
sleep 2
127+
128+
IP=$(get_ip_address "$WIFI_IF")
129+
if [ -n "$IP" ]; then
130+
log_pass "Got IP via udhcpc: $IP"
131+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
132+
log_pass "Internet connectivity verified via ping"
133+
echo "$TESTNAME PASS" > "$res_file"
134+
cleanup
135+
exit 0
136+
else
137+
log_fail "Ping test failed after wpa_supplicant connection"
138+
fi
139+
else
140+
log_fail "Failed to acquire IP via udhcpc"
141+
fi
142+
else
143+
log_error "Neither nmcli nor wpa_supplicant+udhcpc available"
144+
fi
145+
146+
log_fail "$TESTNAME : Test Failed"
147+
echo "$TESTNAME FAIL" > "$res_file"
148+
cleanup
149+
exit 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#<SSID> <PASSWORD>
2+
#<SSID_1> <PASSWORD_1>

Runner/utils/functestlib.sh

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,153 @@ weston_start() {
382382
fi
383383
}
384384

385+
# Retry a shell command up to N times with a delay
386+
retry_command() {
387+
cmd="$1"
388+
retries="$2"
389+
delay="$3"
390+
attempt=1
391+
while [ "$attempt" -le "$retries" ]; do
392+
if eval "$cmd"; then
393+
return 0
394+
fi
395+
log_warn "Attempt $attempt/$retries failed for: $cmd"
396+
attempt=$((attempt + 1))
397+
sleep "$delay"
398+
done
399+
return 1
400+
}
401+
402+
# Check and ensure given systemd services are active (with retries and logging)
403+
check_systemd_services() {
404+
# Skip if systemd is not present
405+
if ! command -v systemctl >/dev/null 2>&1; then
406+
log_warn "systemd is not available. Skipping systemd service checks."
407+
return 0
408+
fi
409+
410+
for service in "$@"; do
411+
if systemctl is-enabled "$service" >/dev/null 2>&1; then
412+
if ! systemctl is-active --quiet "$service"; then
413+
log_warn "$service is not running. Attempting to start with retries..."
414+
retry_command "systemctl start $service" 3 2
415+
if systemctl is-active --quiet "$service"; then
416+
log_pass "$service started successfully after retry."
417+
else
418+
log_fail "$service failed to start after 3 retries."
419+
return 1
420+
fi
421+
else
422+
log_info "$service is already active."
423+
fi
424+
else
425+
log_warn "$service is not enabled or not found."
426+
fi
427+
done
428+
return 0
429+
}
430+
431+
# Ensure at least one network tool is available
432+
check_net_tools() {
433+
# At least one of ifconfig or ip must be available
434+
if command -v ifconfig >/dev/null 2>&1 || command -v ip >/dev/null 2>&1; then
435+
return 0
436+
else
437+
log_error "Neither ifconfig nor ip found in PATH"
438+
return 1
439+
fi
440+
}
441+
442+
# Get IP address using ifconfig or ip (fallback logic)
443+
get_ip_address() {
444+
iface="$1"
445+
ip=""
446+
if command -v ifconfig >/dev/null 2>&1; then
447+
ip=$(ifconfig "$iface" 2>/dev/null | awk '/inet / {print $2; exit}')
448+
fi
449+
if [ -z "$ip" ] && command -v ip >/dev/null 2>&1; then
450+
ip=$(ip addr show "$iface" 2>/dev/null | awk '/inet / {print $2}' | cut -d/ -f1 | head -n1)
451+
fi
452+
echo "$ip"
453+
}
454+
455+
# get_wifi_credentials: Retrieves SSID and password for WiFi connection
456+
# Usage: get_wifi_credentials "$1" "$2"
457+
# - $1: SSID (optional, from argument)
458+
# - $2: Password (optional, from argument)
459+
# Returns: prints "<ssid> <password>" to stdout, or nothing on failure (return 1)
460+
get_wifi_credentials() {
461+
ssid="$1"
462+
pass="$2"
463+
464+
# Try arguments first
465+
if [ -z "$ssid" ] || [ -z "$pass" ]; then
466+
# Try environment variables
467+
ssid="${SSID_ENV:-$ssid}"
468+
pass="${PASSWORD_ENV:-$pass}"
469+
fi
470+
471+
# Try ssid_list.txt if still missing
472+
if [ -z "$ssid" ] || [ -z "$pass" ]; then
473+
if [ -f "./ssid_list.txt" ]; then
474+
# Read only first line; tolerate tabs/spaces
475+
read -r ssid pass _ < ./ssid_list.txt
476+
# Trim whitespace just in case
477+
ssid=$(echo "$ssid" | xargs)
478+
pass=$(echo "$pass" | xargs)
479+
if [ -z "$ssid" ] || [ -z "$pass" ]; then
480+
log_error "Corrupted ssid_list.txt (missing SSID or password)"
481+
return 1
482+
fi
483+
log_info "Using SSID and password from ssid_list.txt"
484+
else
485+
log_error "SSID and password not provided via argument, env, or file."
486+
return 1
487+
fi
488+
fi
489+
490+
# Validate again (trim extra whitespace)
491+
ssid=$(echo "$ssid" | xargs)
492+
pass=$(echo "$pass" | xargs)
493+
if [ -z "$ssid" ] || [ -z "$pass" ]; then
494+
log_error "SSID and password values are empty after all methods."
495+
return 1
496+
fi
497+
498+
# Output only if BOTH are non-empty
499+
printf '%s %s\n' "$ssid" "$pass"
500+
return 0
501+
}
502+
503+
# Find the first available WiFi interface.
504+
# Prints the interface name and sets WIFI_IF variable.
505+
get_wifi_interface() {
506+
# Try with 'ip' first
507+
if command -v ip >/dev/null 2>&1; then
508+
WIFI_IF=$(ip link | awk -F: '/ wl/ {print $2}' | tr -d ' ' | head -n1)
509+
if [ -z "$WIFI_IF" ]; then
510+
WIFI_IF=$(ip link | awk -F: '/^[0-9]+: wl/ {print $2}' | tr -d ' ' | head -n1)
511+
fi
512+
if [ -z "$WIFI_IF" ]; then
513+
# fallback: see if wlan0 exists
514+
if ip link show wlan0 >/dev/null 2>&1; then
515+
WIFI_IF="wlan0"
516+
fi
517+
fi
518+
else
519+
# Fallback to ifconfig
520+
WIFI_IF=$(ifconfig -a 2>/dev/null | grep -o '^wl[^:]*' | head -n1)
521+
if [ -z "$WIFI_IF" ]; then
522+
if ifconfig wlan0 >/dev/null 2>&1; then
523+
WIFI_IF="wlan0"
524+
fi
525+
fi
526+
fi
527+
528+
if [ -n "$WIFI_IF" ]; then
529+
echo "$WIFI_IF"
530+
return 0
531+
else
532+
return 1
533+
fi
534+
}

0 commit comments

Comments
 (0)