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

Function test_netdev_ifconfig_set

components/net/utest/tc_netdev.c:309–381  ·  view source on GitHub ↗

* @brief Test setting network interface configuration parameters * * This function tests: * - IP address setting and verification * - Gateway address setting and verification * - Network configuration restoration * - DHCP disable/enable during configuration changes * * @note DHCP will be restored in utest_tc_cleanup */

Source from the content-addressed store, hash-verified

307 * @note DHCP will be restored in utest_tc_cleanup
308 */
309static void test_netdev_ifconfig_set(void)
310{
311#define UTEST_WRONG_IP_ADDR "192.1.4.125" /* Test IP address */
312#define UTEST_HOST_ADDR "www.rt-thread.org" /* Test hostname */
313#define UTEST_WRONG_GW_IP_ADDR "192.168.99.1" /* Test gateway address */
314
315 ip_addr_t ipaddr, true_ipaddr;
316 rt_err_t res = RT_EOK;
317
318 /* Save original IP address for restoration */
319 true_ipaddr = netdev_default->ip_addr;
320
321 /* Disable DHCP before manual configuration */
322 res = netdev_dhcp_enabled(netdev_default, RT_FALSE);
323 if (res != RT_EOK)
324 {
325 rt_kprintf("Failed to disable DHCP: %d\n", res);
326 uassert_true(RT_FALSE);
327 }
328
329 /* Test setting a different valid IP address */
330 inet_aton(UTEST_WRONG_IP_ADDR, &ipaddr);
331 res = netdev_set_ipaddr(netdev_default, &ipaddr);
332 if (res != RT_EOK)
333 {
334 rt_kprintf("Failed to set IP address: %d\n", res);
335 uassert_true(RT_FALSE);
336 }
337
338 /* Verify IP address was set correctly */
339 uassert_true(ip_addr_cmp(&netdev_default->ip_addr, &ipaddr));
340
341 /* Test ping with new IP configuration (may fail due to network setup) */
342 res = multiple_ping_test(netdev_default, UTEST_HOST_ADDR, 1);
343 /* Don't assert on ping result as it depends on network setup */
344
345 /* Restore original IP address */
346 res = netdev_set_ipaddr(netdev_default, &true_ipaddr);
347 if (res != RT_EOK)
348 {
349 rt_kprintf("Failed to restore IP address: %d\n", res);
350 uassert_true(RT_FALSE);
351 }
352
353 /* Verify IP address was restored */
354 uassert_true(ip_addr_cmp(&netdev_default->ip_addr, &true_ipaddr));
355
356 /* Test gateway setting */
357 ip_addr_t original_gw = netdev_default->gw;
358 inet_aton(UTEST_WRONG_GW_IP_ADDR, &ipaddr);
359 res = netdev_set_gw(netdev_default, &ipaddr);
360 if (res != RT_EOK)
361 {
362 rt_kprintf("Failed to set gateway: %d\n", res);
363 uassert_true(RT_FALSE);
364 }
365
366 /* Verify gateway was set correctly */

Callers

nothing calls this directly

Calls 6

netdev_dhcp_enabledFunction · 0.85
rt_kprintfFunction · 0.85
netdev_set_ipaddrFunction · 0.85
ip_addr_cmpFunction · 0.85
multiple_ping_testFunction · 0.85
netdev_set_gwFunction · 0.85

Tested by

no test coverage detected