Skip to content

Commit b3476db

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 d75bcd4 commit b3476db

File tree

4 files changed

+268
-0
lines changed

4 files changed

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

385+
# Get systemd service status
386+
check_systemd_services() {
387+
for service in "$@"; do
388+
if systemctl is-enabled "$service" >/dev/null 2>&1; then
389+
if ! systemctl is-active --quiet "$service"; then
390+
log_warn "$service is not running, attempting to start..."
391+
systemctl start "$service"
392+
sleep 2
393+
if systemctl is-active --quiet "$service"; then
394+
log_pass "$service started successfully."
395+
else
396+
log_fail "$service failed to start."
397+
return 1
398+
fi
399+
else
400+
log_info "$service is already active."
401+
fi
402+
else
403+
log_warn "$service is not enabled or not found"
404+
fi
405+
done
406+
return 0
407+
}
408+
409+
# Ensure at least one network tool is available
410+
check_net_tools() {
411+
if ! command -v ifconfig >/dev/null 2>&1 && ! command -v ip >/dev/null 2>&1; then
412+
log_error "Neither 'ifconfig' nor 'ip' is available. Please install one."
413+
return 1
414+
fi
415+
return 0
416+
}
417+
418+
# Get IP address using ifconfig or ip (fallback logic)
419+
get_ip_address() {
420+
iface="$1"
421+
if command -v ifconfig >/dev/null 2>&1; then
422+
ifconfig "$iface" 2>/dev/null | awk '/inet / { print $2 }'
423+
elif command -v ip >/dev/null 2>&1; then
424+
ip -4 addr show "$iface" 2>/dev/null | awk '/inet / { print $2 }' | cut -d/ -f1
425+
else
426+
return 1
427+
fi
428+
}

0 commit comments

Comments
 (0)