MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / test_netdev_dhcp

Function test_netdev_dhcp

components/net/utest/tc_netdev.c:235–296  ·  view source on GitHub ↗

* @brief Test DHCP functionality * * This function tests: * - DHCP disable operation * - DHCP state verification * - DHCP state restoration * - Network configuration stability after DHCP operations * * @note Includes retry mechanism to wait for network configuration restoration * to prevent interference with subsequent ping tests */

Source from the content-addressed store, hash-verified

233 * to prevent interference with subsequent ping tests
234 */
235static void test_netdev_dhcp(void)
236{
237 rt_err_t res;
238 rt_bool_t initial_state;
239 ip_addr_t initial_ip, initial_gw;
240 int retry_count = 0;
241 const int max_retries = 10;
242
243 /* Save initial network configuration for restoration */
244 initial_state = netdev_is_dhcp_enabled(netdev_default);
245 initial_ip = netdev_default->ip_addr;
246 initial_gw = netdev_default->gw;
247 rt_kprintf("Initial DHCP state: %s\n", initial_state ? "enabled" : "disabled");
248 rt_kprintf("Initial IP: %s, Gateway: %s\n", inet_ntoa(initial_ip), inet_ntoa(initial_gw));
249
250 /* Test DHCP disable operation */
251 res = netdev_dhcp_enabled(netdev_default, RT_FALSE);
252 if (res == RT_EOK)
253 {
254 uassert_false(netdev_is_dhcp_enabled(netdev_default));
255 rt_kprintf("DHCP successfully disabled\n");
256 }
257 else
258 {
259 rt_kprintf("Failed to disable DHCP: error code %d\n", res);
260 uassert_true(RT_FALSE);
261 return;
262 }
263
264 /* Restore initial DHCP state */
265 res = netdev_dhcp_enabled(netdev_default, initial_state);
266 uassert_true(res == RT_EOK);
267
268 /* Wait for network configuration to stabilize after DHCP restoration */
269 if (initial_state)
270 {
271 rt_kprintf("Waiting for DHCP to restore network configuration...\n");
272 while (retry_count < max_retries)
273 {
274 rt_thread_mdelay(1000); /* Wait 1000ms between checks */
275
276 /* Check if IP and gateway are properly restored */
277 if (!ip_addr_isany(&netdev_default->ip_addr) &&
278 !ip_addr_isany(&netdev_default->gw))
279 {
280 rt_kprintf("Network configuration restored after %d retries\n", retry_count);
281 rt_kprintf("Current IP: %s, Gateway: %s\n",
282 inet_ntoa(netdev_default->ip_addr),
283 inet_ntoa(netdev_default->gw));
284 break;
285 }
286 retry_count++;
287 }
288
289 /* Fail the test if network configuration is not restored within timeout */
290 if (retry_count >= max_retries)
291 {
292 rt_kprintf("Failed: Network configuration not fully restored within timeout\n");

Callers

nothing calls this directly

Calls 4

rt_kprintfFunction · 0.85
netdev_dhcp_enabledFunction · 0.85
rt_thread_mdelayFunction · 0.85
ip_addr_isanyFunction · 0.85

Tested by

no test coverage detected