|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Friedt Professional Engineering Services, Inc |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/logging/log.h> |
| 8 | +#include <zephyr/net/net_if.h> |
| 9 | +#include <zephyr/posix/net/if.h> |
| 10 | +#include <zephyr/sys/util.h> |
| 11 | +#include <zephyr/ztest.h> |
| 12 | + |
| 13 | +ZTEST(net, test_if_indextoname) |
| 14 | +{ |
| 15 | + char *name; |
| 16 | + size_t n = 0; |
| 17 | + struct net_if *iface; |
| 18 | + char a[IF_NAMESIZE]; |
| 19 | + char b[IF_NAMESIZE]; |
| 20 | + |
| 21 | + NET_IFACE_COUNT(&n); |
| 22 | + TC_PRINT("%zu interfaces\n", n); |
| 23 | + for (size_t i = 0; i < n; i++) { |
| 24 | + memset(a, 0, sizeof(a)); |
| 25 | + memset(b, 0, sizeof(b)); |
| 26 | + name = if_indextoname(i + 1, a); |
| 27 | + zassert_equal(name, a); |
| 28 | + TC_PRINT("interface %zu: %s\n", i + 1, name); |
| 29 | + iface = net_if_get_by_index(i + 1); |
| 30 | + zassert_not_null(iface); |
| 31 | + zassert_true(net_if_get_name(iface, b, IF_NAMESIZE) >= 0); |
| 32 | + zassert_mem_equal(a, b, IF_NAMESIZE); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +ZTEST(net, test_if_freenameindex) |
| 37 | +{ |
| 38 | + if_freenameindex(NULL); |
| 39 | + if_freenameindex(if_nameindex()); |
| 40 | +} |
| 41 | + |
| 42 | +ZTEST(net, test_if_nameindex) |
| 43 | +{ |
| 44 | + size_t n = 0; |
| 45 | + struct if_nameindex *ni; |
| 46 | + |
| 47 | + NET_IFACE_COUNT(&n); |
| 48 | + TC_PRINT("%zu interfaces\n", n); |
| 49 | + |
| 50 | + ni = if_nameindex(); |
| 51 | + if (ni == NULL) { |
| 52 | + zassert_equal(errno, ENOBUFS); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + for (size_t i = 0; i < n; i++) { |
| 57 | + zassert_equal(i + 1, ni[i].if_index); |
| 58 | + zassert_not_null(ni[i].if_name); |
| 59 | + TC_PRINT("interface %zu: %s\n", i + 1, ni[i].if_name); |
| 60 | + zassert_equal(0, ni[n].if_index); |
| 61 | + zassert_is_null(ni[n].if_name); |
| 62 | + } |
| 63 | + |
| 64 | + if_freenameindex(ni); |
| 65 | +} |
| 66 | + |
| 67 | +ZTEST(net, test_if_nametoindex) |
| 68 | +{ |
| 69 | + size_t n = 0; |
| 70 | + char buf[IF_NAMESIZE]; |
| 71 | + |
| 72 | + NET_IFACE_COUNT(&n); |
| 73 | + TC_PRINT("%zu interfaces\n", n); |
| 74 | + for (size_t i = 0; i < n; i++) { |
| 75 | + memset(buf, 0, sizeof(buf)); |
| 76 | + zassert_not_null(if_indextoname(i + 1, buf)); |
| 77 | + TC_PRINT("interface %zu: %s\n", i + 1, buf); |
| 78 | + zassert_equal(i + 1, if_nametoindex(buf)); |
| 79 | + } |
| 80 | +} |
0 commit comments