Skip to content

Commit 16f073a

Browse files
ppiaoldv-alt
authored andcommitted
tests: check decoding of TCA_STAB netlink attribute of tcmsg
* tests/test_nlattr.h (TEST_NESTED_NLATTR_ARRAY): New macro. * tests/nlattr_tca_stab.c: New file. * tests/gen_tests.in (nlattr_tca_stab): New entry. * tests/pure_executables.list: Add nlattr_tca_stab. * tests/.gitignore: Likewise.
1 parent e1a1f80 commit 16f073a

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ nlattr_rtgenmsg
241241
nlattr_rtmsg
242242
nlattr_smc_diag_msg
243243
nlattr_tc_stats
244+
nlattr_tca_stab
244245
nlattr_tcamsg
245246
nlattr_tcmsg
246247
nlattr_unix_diag_msg

tests/gen_tests.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ nlattr_rtgenmsg +netlink_sock_diag.test
220220
nlattr_rtmsg +netlink_sock_diag.test
221221
nlattr_smc_diag_msg +netlink_sock_diag.test
222222
nlattr_tc_stats +netlink_sock_diag.test
223+
nlattr_tca_stab +netlink_sock_diag.test
223224
nlattr_tcamsg +netlink_sock_diag.test
224225
nlattr_tcmsg +netlink_sock_diag.test
225226
nlattr_unix_diag_msg +netlink_sock_diag.test

tests/nlattr_tca_stab.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2017 JingPiao Chen <[email protected]>
3+
* Copyright (c) 2017 The strace developers.
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* 3. The name of the author may not be used to endorse or promote products
15+
* derived from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include "tests.h"
30+
31+
#include <stdio.h>
32+
#include "test_nlattr.h"
33+
#include <linux/pkt_sched.h>
34+
#include <linux/rtnetlink.h>
35+
36+
#ifndef TCA_STAB
37+
# define TCA_STAB 8
38+
#endif
39+
#ifndef TCA_STAB_DATA
40+
# define TCA_STAB_DATA 2
41+
#endif
42+
43+
const unsigned int hdrlen = sizeof(struct tcmsg);
44+
45+
static void
46+
init_tcmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
47+
{
48+
SET_STRUCT(struct nlmsghdr, nlh,
49+
.nlmsg_len = msg_len,
50+
.nlmsg_type = RTM_GETQDISC,
51+
.nlmsg_flags = NLM_F_DUMP
52+
);
53+
54+
struct tcmsg *const msg = NLMSG_DATA(nlh);
55+
SET_STRUCT(struct tcmsg, msg,
56+
.tcm_family = AF_UNIX,
57+
.tcm_ifindex = ifindex_lo()
58+
);
59+
60+
struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
61+
SET_STRUCT(struct nlattr, nla,
62+
.nla_len = msg_len - NLMSG_SPACE(hdrlen),
63+
.nla_type = TCA_STAB
64+
);
65+
}
66+
67+
static void
68+
print_tcmsg(const unsigned int msg_len)
69+
{
70+
printf("{len=%u, type=RTM_GETQDISC, flags=NLM_F_DUMP"
71+
", seq=0, pid=0}, {tcm_family=AF_UNIX"
72+
", tcm_ifindex=" IFINDEX_LO_STR
73+
", tcm_handle=0, tcm_parent=0, tcm_info=0}"
74+
", {{nla_len=%u, nla_type=TCA_STAB}",
75+
msg_len, msg_len - NLMSG_SPACE(hdrlen));
76+
}
77+
78+
static void
79+
print_uint16(const uint16_t *p)
80+
{
81+
printf("%u", *p);
82+
}
83+
84+
int
85+
main(void)
86+
{
87+
skip_if_unavailable("/proc/self/fd/");
88+
89+
const int fd = create_nl_socket(NETLINK_ROUTE);
90+
void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
91+
92+
static char pattern[4096];
93+
fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
94+
95+
#ifdef HAVE_STRUCT_TC_SIZESPEC
96+
static const struct tc_sizespec s = {
97+
.cell_log = 0xab,
98+
.size_log = 0xcd,
99+
.cell_align = 0xefab,
100+
.overhead = 0xcdadeefa,
101+
.linklayer = 0xefbaafeb,
102+
.mpu = 0xfebfaefb,
103+
.mtu = 0xacdbefab,
104+
.tsize = 0xbdeaabed
105+
};
106+
TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
107+
init_tcmsg, print_tcmsg,
108+
TCA_STAB_BASE, pattern, s,
109+
PRINT_FIELD_U("{", s, cell_log);
110+
PRINT_FIELD_U(", ", s, size_log);
111+
PRINT_FIELD_D(", ", s, cell_align);
112+
PRINT_FIELD_D(", ", s, overhead);
113+
PRINT_FIELD_U(", ", s, linklayer);
114+
PRINT_FIELD_U(", ", s, mpu);
115+
PRINT_FIELD_U(", ", s, mtu);
116+
PRINT_FIELD_U(", ", s, tsize);
117+
printf("}"));
118+
#endif
119+
120+
uint16_t data[2] = { 0xacbd, 0xefba };
121+
TEST_NESTED_NLATTR_ARRAY(fd, nlh0, hdrlen,
122+
init_tcmsg, print_tcmsg,
123+
TCA_STAB_DATA, pattern, data, print_uint16);
124+
125+
puts("+++ exited with 0 +++");
126+
return 0;
127+
}

tests/pure_executables.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ nlattr_rtgenmsg
203203
nlattr_rtmsg
204204
nlattr_smc_diag_msg
205205
nlattr_tc_stats
206+
nlattr_tca_stab
206207
nlattr_tcamsg
207208
nlattr_tcmsg
208209
nlattr_unix_diag_msg

tests/test_nlattr.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,64 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type)
237237
__VA_ARGS__, \
238238
printf("}")); \
239239
} while (0)
240+
241+
#define TEST_NESTED_NLATTR_ARRAY(fd_, nlh0_, hdrlen_, \
242+
init_msg_, print_msg_, \
243+
nla_type_, pattern_, obj_, print_elem_)\
244+
do { \
245+
const unsigned int plen = \
246+
sizeof((obj_)[0]) - 1 > DEFAULT_STRLEN \
247+
? DEFAULT_STRLEN : (int) sizeof((obj_)[0]) - 1; \
248+
/* len < sizeof((obj_)[0]) */ \
249+
TEST_NLATTR_((fd_), (nlh0_) - NLA_HDRLEN, \
250+
(hdrlen_) + NLA_HDRLEN, \
251+
(init_msg_), (print_msg_), \
252+
(nla_type_), #nla_type_, \
253+
plen, (pattern_), plen, \
254+
print_quoted_hex((pattern_), plen); \
255+
printf("}")); \
256+
/* sizeof((obj_)[0]) < len < sizeof(obj_) */ \
257+
TEST_NLATTR_((fd_), (nlh0_) - NLA_HDRLEN, \
258+
(hdrlen_) + NLA_HDRLEN, \
259+
(init_msg_), (print_msg_), \
260+
(nla_type_), #nla_type_, \
261+
sizeof(obj_) - 1, \
262+
&(obj_), sizeof(obj_) - 1, \
263+
printf("["); \
264+
size_t i; \
265+
for (i = 0; i < ARRAY_SIZE(obj_) - 1; ++i) { \
266+
if (i) printf(", "); \
267+
(print_elem_)(&(obj_)[i]); \
268+
} \
269+
printf("]}")); \
270+
/* short read of sizeof(obj_) */ \
271+
TEST_NLATTR_((fd_), (nlh0_) - NLA_HDRLEN, \
272+
(hdrlen_) + NLA_HDRLEN, \
273+
(init_msg_), (print_msg_), \
274+
(nla_type_), #nla_type_, \
275+
sizeof(obj_), \
276+
&(obj_), sizeof(obj_) - 1, \
277+
printf("["); \
278+
size_t i; \
279+
for (i = 0; i < ARRAY_SIZE(obj_) - 1; ++i) { \
280+
if (i) printf(", "); \
281+
(print_elem_)(&(obj_)[i]); \
282+
} \
283+
printf(", %p]}", \
284+
RTA_DATA(TEST_NLATTR_nla) \
285+
+ sizeof((obj_)[0]))); \
286+
/* sizeof(obj_) */ \
287+
TEST_NLATTR_((fd_), (nlh0_) - NLA_HDRLEN, \
288+
(hdrlen_) + NLA_HDRLEN, \
289+
(init_msg_), (print_msg_), \
290+
(nla_type_), #nla_type_, \
291+
sizeof(obj_), \
292+
&(obj_), sizeof(obj_), \
293+
printf("["); \
294+
size_t i; \
295+
for (i = 0; i < ARRAY_SIZE(obj_); ++i) { \
296+
if (i) printf(", "); \
297+
(print_elem_)(&(obj_)[i]); \
298+
} \
299+
printf("]}")); \
300+
} while (0)

0 commit comments

Comments
 (0)