Skip to content

Commit 4ebdf94

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 4ebdf94

File tree

4 files changed

+342
-0
lines changed

4 files changed

+342
-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: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
if ! CRED=$(get_wifi_credentials "$1" "$2") || [ -z "$CRED" ]; then
39+
log_fail "SSID and password not provided via argument, env, or file."
40+
echo "$TESTNAME FAIL" > "$res_file"
41+
exit 1
42+
fi
43+
44+
SSID=$(echo "$CRED" | awk '{print $1}')
45+
PASSWORD=$(echo "$CRED" | awk '{print $2}')
46+
SSID=$(echo "$SSID" | xargs)
47+
PASSWORD=$(echo "$PASSWORD" | xargs)
48+
if [ -z "$SSID" ] || [ -z "$PASSWORD" ]; then
49+
log_fail "SSID and password could not be extracted."
50+
echo "$TESTNAME FAIL" > "$res_file"
51+
exit 1
52+
fi
53+
54+
log_info "Using SSID='$SSID' and PASSWORD='[hidden]'"
55+
56+
# Check required dependencies
57+
check_dependencies nmcli wpa_supplicant udhcpc ping iw
58+
check_systemd_services systemd-networkd.service || {
59+
log_error "Network services check failed"
60+
echo "$TESTNAME FAIL" > "$res_file"
61+
exit 1
62+
}
63+
64+
# Find WiFi interface
65+
WIFI_IFACE="$(iw dev 2>/dev/null | awk '/Interface/ {print $2; exit}')"
66+
if [ -z "$WIFI_IFACE" ]; then
67+
log_fail "No WiFi interface found (via iw dev)"
68+
echo "$TESTNAME FAIL" > "$res_file"
69+
exit 1
70+
fi
71+
log_info "Using WiFi interface: $WIFI_IFACE"
72+
73+
cleanup() {
74+
log_info "Cleaning up WiFi test environment..."
75+
killall -q wpa_supplicant 2>/dev/null
76+
rm -f /tmp/wpa_supplicant.conf nmcli.log wpa.log
77+
ip link set "$WIFI_IFACE" down 2>/dev/null || ifconfig "$WIFI_IFACE" down 2>/dev/null
78+
}
79+
80+
# Try nmcli first
81+
if command -v nmcli >/dev/null 2>&1; then
82+
log_info "Trying to connect using nmcli..."
83+
if nmcli dev wifi connect "$SSID" password "$PASSWORD" ifname "$WIFI_IFACE" 2>&1 | tee nmcli.log; then
84+
log_pass "Connected to $SSID using nmcli"
85+
IP=$(ip addr show "$WIFI_IFACE" | awk '/inet / {print $2}' | cut -d/ -f1)
86+
log_info "IP Address: $IP"
87+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
88+
log_pass "Internet connectivity verified via ping"
89+
echo "$TESTNAME PASS" > "$res_file"
90+
cleanup
91+
exit 0
92+
else
93+
log_fail "Ping test failed after nmcli connection"
94+
fi
95+
fi
96+
fi
97+
98+
# Fall back to wpa_supplicant + udhcpc
99+
if command -v wpa_supplicant >/dev/null 2>&1 && command -v udhcpc >/dev/null 2>&1; then
100+
log_info "Falling back to wpa_supplicant + udhcpc"
101+
WPA_CONF="/tmp/wpa_supplicant.conf"
102+
{
103+
echo "ctrl_interface=/var/run/wpa_supplicant"
104+
echo "network={"
105+
echo " ssid=\"$SSID\""
106+
echo " key_mgmt=WPA-PSK"
107+
echo " pairwise=CCMP TKIP"
108+
echo " group=CCMP TKIP"
109+
echo " psk=\"$PASSWORD\""
110+
echo "}"
111+
} > "$WPA_CONF"
112+
113+
killall -q wpa_supplicant 2>/dev/null
114+
wpa_supplicant -B -i "$WIFI_IFACE" -D nl80211 -c "$WPA_CONF" 2>&1 | tee wpa.log
115+
sleep 4
116+
udhcpc -i "$WIFI_IFACE" >/dev/null 2>&1
117+
sleep 2
118+
119+
IP=$(ip addr show "$WIFI_IFACE" | awk '/inet / {print $2}' | cut -d/ -f1)
120+
if [ -n "$IP" ]; then
121+
log_pass "Got IP via udhcpc: $IP"
122+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
123+
log_pass "Internet connectivity verified via ping"
124+
echo "$TESTNAME PASS" > "$res_file"
125+
cleanup
126+
exit 0
127+
else
128+
log_fail "Ping test failed after wpa_supplicant connection"
129+
fi
130+
else
131+
log_fail "Failed to acquire IP via udhcpc"
132+
fi
133+
else
134+
log_error "Neither nmcli nor wpa_supplicant+udhcpc available"
135+
fi
136+
137+
log_fail "$TESTNAME : Test Failed"
138+
echo "$TESTNAME FAIL" > "$res_file"
139+
cleanup
140+
exit 1

Runner/suites/Connectivity/WiFi/ssid_list.txt

Whitespace-only changes.

Runner/utils/functestlib.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,133 @@ 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+
# Extracts WiFi SSID and password from arguments, env, or ssid_list.txt
456+
get_wifi_credentials() {
457+
ssid="$1"
458+
pass="$2"
459+
460+
# Try arguments first, then env
461+
if [ -z "$ssid" ] || [ -z "$pass" ]; then
462+
ssid="${SSID_ENV:-$ssid}"
463+
pass="${PASSWORD_ENV:-$pass}"
464+
fi
465+
466+
# Try ssid_list.txt if still missing
467+
if [ -z "$ssid" ] || [ -z "$pass" ]; then
468+
if [ -f "./ssid_list.txt" ]; then
469+
read -r ssid pass _ < ./ssid_list.txt
470+
fi
471+
fi
472+
473+
# Final trim & validate
474+
ssid=$(echo "$ssid" | xargs)
475+
pass=$(echo "$pass" | xargs)
476+
if [ -z "$ssid" ] || [ -z "$pass" ]; then
477+
return 1
478+
fi
479+
printf '%s %s\n' "$ssid" "$pass"
480+
return 0
481+
}
482+
483+
# Find the first available WiFi interface.
484+
# Prints the interface name and sets WIFI_IF variable.
485+
get_wifi_interface() {
486+
# Try with 'ip' first
487+
if command -v ip >/dev/null 2>&1; then
488+
WIFI_IF=$(ip link | awk -F: '/ wl/ {print $2}' | tr -d ' ' | head -n1)
489+
if [ -z "$WIFI_IF" ]; then
490+
WIFI_IF=$(ip link | awk -F: '/^[0-9]+: wl/ {print $2}' | tr -d ' ' | head -n1)
491+
fi
492+
if [ -z "$WIFI_IF" ]; then
493+
# fallback: see if wlan0 exists
494+
if ip link show wlan0 >/dev/null 2>&1; then
495+
WIFI_IF="wlan0"
496+
fi
497+
fi
498+
else
499+
# Fallback to ifconfig
500+
WIFI_IF=$(ifconfig -a 2>/dev/null | grep -o '^wl[^:]*' | head -n1)
501+
if [ -z "$WIFI_IF" ]; then
502+
if ifconfig wlan0 >/dev/null 2>&1; then
503+
WIFI_IF="wlan0"
504+
fi
505+
fi
506+
fi
507+
508+
if [ -n "$WIFI_IF" ]; then
509+
echo "$WIFI_IF"
510+
return 0
511+
else
512+
return 1
513+
fi
514+
}

0 commit comments

Comments
 (0)