| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
|
| 3 |
#include "nl-default.h" |
| 4 |
|
| 5 |
#include <check.h> |
| 6 |
|
| 7 |
#include <linux/snmp.h> |
| 8 |
#include <linux/if_bridge.h> |
| 9 |
|
| 10 |
#include <netlink/route/link.h> |
| 11 |
#include <netlink/route/link/bridge.h> |
| 12 |
|
| 13 |
#include "c-list.h" |
| 14 |
|
| 15 |
#include "nl-priv-static-route/nl-priv-static-route.h" |
| 16 |
#include "nl-aux-core/nl-core.h" |
| 17 |
|
| 18 |
#define CASES 5 |
| 19 |
#define MAX_ATTR 7 |
| 20 |
|
| 21 |
START_TEST(static_checks) |
| 22 |
{ |
| 23 |
int i, j; |
| 24 |
char strbuf[100]; |
| 25 |
|
| 26 |
_NL_STATIC_ASSERT(RTNL_LINK_RX_PACKETS == 0); |
| 27 |
assert(_nltst_map_stat_id_from_IPSTATS_MIB_v2[0] == |
| 28 |
RTNL_LINK_RX_PACKETS); |
| 29 |
for (i = 1; i < __IPSTATS_MIB_MAX; i++) { |
| 30 |
assert(_nltst_map_stat_id_from_IPSTATS_MIB_v2[i] > 0); |
| 31 |
assert(_nltst_map_stat_id_from_IPSTATS_MIB_v2[i] < |
| 32 |
__RTNL_LINK_STATS_MAX); |
| 33 |
for (j = 1; j < i; j++) |
| 34 |
assert(_nltst_map_stat_id_from_IPSTATS_MIB_v2[i] != |
| 35 |
_nltst_map_stat_id_from_IPSTATS_MIB_v2[j]); |
| 36 |
} |
| 37 |
|
| 38 |
for (i = 0; i <= RTNL_LINK_STATS_MAX + 1; i++) { |
| 39 |
const char *s; |
| 40 |
|
| 41 |
s = rtnl_link_stat2str(i, strbuf, sizeof(strbuf)); |
| 42 |
assert(s); |
| 43 |
assert(s == strbuf); |
| 44 |
assert(strlen(s) < sizeof(strbuf)); |
| 45 |
if (strncmp(s, "0x", 2) == 0) { |
| 46 |
assert(i == RTNL_LINK_STATS_MAX + 1); |
| 47 |
ck_assert_int_eq(strtoll(&s[2], NULL, 16), i); |
| 48 |
} else |
| 49 |
ck_assert_int_le(i, RTNL_LINK_STATS_MAX); |
| 50 |
ck_assert_int_eq(i, rtnl_link_str2stat(s)); |
| 51 |
} |
| 52 |
|
| 53 |
ck_assert_int_eq(nl_str2ip_proto(""), -NLE_OBJ_NOTFOUND); |
| 54 |
ck_assert_int_eq(nl_str2ip_proto("5"), 5); |
| 55 |
ck_assert_int_eq(nl_str2ip_proto(" 13 "), -NLE_OBJ_NOTFOUND); |
| 56 |
ck_assert_int_eq(nl_str2ip_proto("13"), 13); |
| 57 |
ck_assert_int_eq(nl_str2ip_proto("0x13"), 0x13); |
| 58 |
ck_assert_int_eq(nl_str2ip_proto("0342"), 0342); |
| 59 |
ck_assert_int_eq(nl_str2ip_proto("2147483647"), 2147483647); |
| 60 |
ck_assert_int_eq(nl_str2ip_proto("2147483648"), -NLE_OBJ_NOTFOUND); |
| 61 |
} |
| 62 |
END_TEST |
| 63 |
|
| 64 |
static void set_bitmap_range(u_int32_t start, u_int32_t end, |
| 65 |
struct rtnl_link_bridge_vlan *vlan_info, |
| 66 |
int untagged) |
| 67 |
{ |
| 68 |
for (u_int32_t i = start; i <= end; i++) { |
| 69 |
vlan_info->vlan_bitmap[i / 32] |= (((uint32_t)1) << (i % 32)); |
| 70 |
if (untagged) { |
| 71 |
vlan_info->untagged_bitmap[i / 32] |= |
| 72 |
(((uint32_t)1) << (i % 32)); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
START_TEST(vlan_attribute_check) |
| 78 |
{ |
| 79 |
struct nlmsghdr *nlh; |
| 80 |
struct nlattr *a; |
| 81 |
int attr_count, rem; |
| 82 |
struct bridge_vlan_info *vlan_attr; |
| 83 |
struct rtnl_link_bridge_vlan vlan_info[CASES]; |
| 84 |
struct bridge_vlan_info expected_attr[CASES][MAX_ATTR]; |
| 85 |
|
| 86 |
for (int i = 0; i < CASES; i++) { |
| 87 |
memset(&vlan_info[i], 0, sizeof(struct rtnl_link_bridge_vlan)); |
| 88 |
memset(&expected_attr[i], 0, |
| 89 |
sizeof(struct bridge_vlan_info) * MAX_ATTR); |
| 90 |
} |
| 91 |
|
| 92 |
// Case 1 setting pvid untagged. |
| 93 |
vlan_info[0].pvid = 1; |
| 94 |
set_bitmap_range(1, 1, &vlan_info[0], 1); |
| 95 |
expected_attr[0][0].vid = 1; |
| 96 |
expected_attr[0][0].flags = BRIDGE_VLAN_INFO_PVID | |
| 97 |
BRIDGE_VLAN_INFO_UNTAGGED; |
| 98 |
|
| 99 |
// Case 2 setting vid range. |
| 100 |
vlan_info[1].pvid = 0; |
| 101 |
set_bitmap_range(1, 4094, &vlan_info[1], 0); |
| 102 |
expected_attr[1][0].vid = 1; |
| 103 |
expected_attr[1][0].flags = BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 104 |
expected_attr[1][1].vid = 4094; |
| 105 |
expected_attr[1][1].flags = BRIDGE_VLAN_INFO_RANGE_END; |
| 106 |
|
| 107 |
// Case 3 interweaving pvid with vid range. |
| 108 |
vlan_info[2].pvid = 7; |
| 109 |
set_bitmap_range(1, 27, &vlan_info[2], 0); |
| 110 |
set_bitmap_range(7, 7, &vlan_info[2], 1); |
| 111 |
expected_attr[2][0].vid = 1; |
| 112 |
expected_attr[2][0].flags = BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 113 |
expected_attr[2][1].vid = 6; |
| 114 |
expected_attr[2][1].flags = BRIDGE_VLAN_INFO_RANGE_END; |
| 115 |
expected_attr[2][2].vid = 8; |
| 116 |
expected_attr[2][2].flags = BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 117 |
expected_attr[2][3].vid = 27; |
| 118 |
expected_attr[2][3].flags = BRIDGE_VLAN_INFO_RANGE_END; |
| 119 |
expected_attr[2][4].vid = 7; |
| 120 |
expected_attr[2][4].flags = BRIDGE_VLAN_INFO_PVID | |
| 121 |
BRIDGE_VLAN_INFO_UNTAGGED; |
| 122 |
|
| 123 |
// Case 4 interweaving untagged and tagged vid ranges. |
| 124 |
vlan_info[3].pvid = 1; |
| 125 |
set_bitmap_range(1, 1, &vlan_info[3], 1); |
| 126 |
set_bitmap_range(1, 25, &vlan_info[3], 0); |
| 127 |
set_bitmap_range(26, 50, &vlan_info[3], 1); |
| 128 |
set_bitmap_range(51, 75, &vlan_info[3], 0); |
| 129 |
expected_attr[3][0].vid = 2; |
| 130 |
expected_attr[3][0].flags = BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 131 |
expected_attr[3][1].vid = 25; |
| 132 |
expected_attr[3][1].flags = BRIDGE_VLAN_INFO_RANGE_END; |
| 133 |
expected_attr[3][2].vid = 26; |
| 134 |
expected_attr[3][2].flags = BRIDGE_VLAN_INFO_RANGE_BEGIN | |
| 135 |
BRIDGE_VLAN_INFO_UNTAGGED; |
| 136 |
expected_attr[3][3].vid = 50; |
| 137 |
expected_attr[3][3].flags = BRIDGE_VLAN_INFO_RANGE_END | |
| 138 |
BRIDGE_VLAN_INFO_UNTAGGED; |
| 139 |
expected_attr[3][4].vid = 51; |
| 140 |
expected_attr[3][4].flags = BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 141 |
expected_attr[3][5].vid = 75; |
| 142 |
expected_attr[3][5].flags = BRIDGE_VLAN_INFO_RANGE_END; |
| 143 |
expected_attr[3][6].vid = 1; |
| 144 |
expected_attr[3][6].flags = BRIDGE_VLAN_INFO_PVID | |
| 145 |
BRIDGE_VLAN_INFO_UNTAGGED; |
| 146 |
|
| 147 |
// Case 5 individual vid. |
| 148 |
vlan_info[4].pvid = 0; |
| 149 |
set_bitmap_range(5, 5, &vlan_info[4], 0); |
| 150 |
set_bitmap_range(3067, 3067, &vlan_info[4], 1); |
| 151 |
expected_attr[4][0].vid = 5; |
| 152 |
expected_attr[4][0].flags = 0; |
| 153 |
expected_attr[4][1].vid = 3067; |
| 154 |
expected_attr[4][1].flags = BRIDGE_VLAN_INFO_UNTAGGED; |
| 155 |
|
| 156 |
for (int i = 0; i < CASES; i++) { |
| 157 |
_nl_auto_nl_msg struct nl_msg *msg = nlmsg_alloc(); |
| 158 |
attr_count = 0; |
| 159 |
ck_assert_msg(msg, "Unable to allocate netlink message"); |
| 160 |
ck_assert_int_eq(0, |
| 161 |
_nl_bridge_fill_vlan_info(msg, &vlan_info[i])); |
| 162 |
|
| 163 |
nlh = nlmsg_hdr(msg); |
| 164 |
|
| 165 |
nlmsg_for_each_attr(a, nlh, 0, rem) { |
| 166 |
ck_assert_msg(expected_attr[i][attr_count].vid != 0, |
| 167 |
"Attribute number %d unexpected", |
| 168 |
attr_count); |
| 169 |
ck_assert_msg( |
| 170 |
nla_type(a) == IFLA_BRIDGE_VLAN_INFO, |
| 171 |
"Expected attribute IFLA_BRIDGE_VLAN_INFO %d", |
| 172 |
IFLA_BRIDGE_VLAN_INFO); |
| 173 |
vlan_attr = (struct bridge_vlan_info *)nla_data(a); |
| 174 |
ck_assert_int_eq(vlan_attr->vid, |
| 175 |
expected_attr[i][attr_count].vid); |
| 176 |
ck_assert_int_eq(vlan_attr->flags, |
| 177 |
expected_attr[i][attr_count].flags); |
| 178 |
attr_count++; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
END_TEST |
| 183 |
|
| 184 |
static Suite *make_suite(void) |
| 185 |
{ |
| 186 |
Suite *suite = suite_create("Direct"); |
| 187 |
TCase *tc = tcase_create("Core"); |
| 188 |
|
| 189 |
tcase_add_test(tc, static_checks); |
| 190 |
tcase_add_test(tc, vlan_attribute_check); |
| 191 |
suite_add_tcase(suite, tc); |
| 192 |
return suite; |
| 193 |
} |
| 194 |
|
| 195 |
int main(int argc, char *argv[]) |
| 196 |
{ |
| 197 |
SRunner *runner; |
| 198 |
int nfailed; |
| 199 |
|
| 200 |
runner = srunner_create(suite_create("main")); |
| 201 |
|
| 202 |
srunner_add_suite(runner, make_suite()); |
| 203 |
|
| 204 |
srunner_run_all(runner, CK_ENV); |
| 205 |
|
| 206 |
nfailed = srunner_ntests_failed(runner); |
| 207 |
srunner_free(runner); |
| 208 |
return nfailed != 0; |
| 209 |
} |