1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10
11 #include <stdio.h>
12 #include "test_nlattr.h"
13 #include <linux/if.h>
14 #include <linux/if_arp.h>
15 #include <linux/if_link.h>
16 #include <linux/rtnetlink.h>
17
18 #define XLAT_MACROS_ONLY
19 # include "xlat/rtnl_ifla_proto_down_reason_attrs.h"
20 #undef XLAT_MACROS_ONLY
21
22 #define IFLA_ATTR IFLA_PROTO_DOWN_REASON
23 #include "nlattr_ifla.h"
24
25 int
26 main(void)
27 {
28 skip_if_unavailable("/proc/self/fd/");
29
30 const union {
31 uint8_t bytes[4];
32 uint32_t num;
33 } num = { .num=0xabacdbcd };
34 const int fd = create_nl_socket(NETLINK_ROUTE);
35 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
36 NLA_HDRLEN + sizeof(num));
37
38 static char pattern[4096];
39 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
40
41 static const struct {
42 uint8_t val;
43 const char *str;
44 } inv_attrs[] = {
45 { ARG_STR(IFLA_PROTO_DOWN_REASON_UNSPEC) },
46 { ARG_STR(0x3) " /* IFLA_PROTO_DOWN_REASON_??? */" },
47 { ARG_STR(0xfe) " /* IFLA_PROTO_DOWN_REASON_??? */" },
48 };
49
50 for (size_t i = 0; i < ARRAY_SIZE(inv_attrs); i++) {
51 TEST_NESTED_NLATTR_OBJECT_EX_(fd, nlh0, hdrlen,
52 init_ifinfomsg, print_ifinfomsg,
53 inv_attrs[i].val,
54 inv_attrs[i].str,
55 pattern, num,
56 print_quoted_hex, 1,
57 printf("\"\\x%x\\x%x\\x%x\\x%x\"",
58 num.bytes[0], num.bytes[1],
59 num.bytes[2], num.bytes[3])
60 );
61 }
62
63 static const struct {
64 uint32_t val;
65 const char *str;
66 } attrs[] = {
67 { ARG_STR(IFLA_PROTO_DOWN_REASON_MASK) },
68 { ARG_STR(IFLA_PROTO_DOWN_REASON_VALUE) },
69 };
70
71 for (size_t i = 0; i < ARRAY_SIZE(attrs); i++) {
72 TEST_NESTED_NLATTR_OBJECT_EX_(fd, nlh0, hdrlen,
73 init_ifinfomsg, print_ifinfomsg,
74 attrs[i].val, attrs[i].str,
75 pattern, num,
76 print_quoted_hex, 1,
77 printf("%#x", num.num));
78 }
79
80 puts("+++ exited with 0 +++");
81 return 0;
82 }