Home | History | Annotate | Download | only in tests-mx32
      1 /*
      2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) gmail.com>
      3  * Copyright (c) 2017-2018 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 #include <string.h>
     31 #include <stdio.h>
     32 #include <sys/socket.h>
     33 #include <arpa/inet.h>
     34 #include "netlink.h"
     35 #include "netlink_kobject_uevent.h"
     36 
     37 static const char *errstr;
     38 
     39 static ssize_t
     40 sys_send(const int fd, const void *const buf, const size_t len)
     41 {
     42 	const ssize_t rc = sendto(fd, buf, len, MSG_DONTWAIT, NULL, 0);
     43 	errstr = sprintrc(rc);
     44 	return rc;
     45 }
     46 
     47 static void
     48 test_nlmsg_type_udev(const int fd)
     49 {
     50 	static const char extra[] = "12345678";
     51 	struct udev_monitor_netlink_header uh = {
     52 		.prefix = "libudev",
     53 		.magic = htonl(0xfeedcafe),
     54 		.header_size = sizeof(uh),
     55 		.properties_off = 40,
     56 		.properties_len = 299,
     57 		.filter_subsystem_hash = htonl(0xc370b302),
     58 		.filter_devtype_hash = htonl(0x10800000),
     59 		.filter_tag_bloom_hi = htonl(0x2000400),
     60 		.filter_tag_bloom_lo = htonl(0x10800000),
     61 	};
     62 	const unsigned int extra_len = LENGTH_OF(extra);
     63 	const unsigned int uh_len = sizeof(uh);
     64 
     65 	char *const buf = tail_alloc(uh_len + extra_len);
     66 	memcpy(buf + extra_len, &uh, uh_len);
     67 
     68 	sys_send(fd, buf + extra_len, uh_len);
     69 	printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
     70 	       ", header_size=%u, properties_off=%u, properties_len=%u"
     71 	       ", filter_subsystem_hash=htonl(%#x)"
     72 	       ", filter_devtype_hash=htonl(%#x)"
     73 	       ", filter_tag_bloom_hi=htonl(%#x)"
     74 	       ", filter_tag_bloom_lo=htonl(%#x)}}, %u, MSG_DONTWAIT, NULL, "
     75 	       "0) = %s\n"
     76 	       , fd, uh.prefix,
     77 	       ntohl(uh.magic), uh.header_size, uh.properties_off,
     78 	       uh.properties_len, ntohl(uh.filter_subsystem_hash),
     79 	       ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
     80 	       ntohl(uh.filter_tag_bloom_lo), uh_len, errstr);
     81 
     82 	memcpy(buf, &uh, uh_len);
     83 	memcpy(buf + uh_len, extra, extra_len);
     84 	sys_send(fd, buf, uh_len + extra_len);
     85 	printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
     86 	       ", header_size=%u, properties_off=%u, properties_len=%u"
     87 	       ", filter_subsystem_hash=htonl(%#x)"
     88 	       ", filter_devtype_hash=htonl(%#x)"
     89 	       ", filter_tag_bloom_hi=htonl(%#x)"
     90 	       ", filter_tag_bloom_lo=htonl(%#x)}, "
     91 	       , fd, uh.prefix,
     92 	       ntohl(uh.magic), uh.header_size, uh.properties_off,
     93 	       uh.properties_len, ntohl(uh.filter_subsystem_hash),
     94 	       ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
     95 	       ntohl(uh.filter_tag_bloom_lo));
     96 	print_quoted_memory(buf + uh_len, extra_len);
     97 	printf("}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
     98 	       uh_len + extra_len, errstr);
     99 
    100 	memcpy(buf + extra_len + 1, &uh, uh_len - 1);
    101 	sys_send(fd, buf + extra_len + 1, uh_len);
    102 	printf("sendto(%d, ", fd);
    103 	print_quoted_memory(&uh, MIN(uh_len - 1, DEFAULT_STRLEN));
    104 	printf("%s, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
    105 	       (uh_len - 1 > DEFAULT_STRLEN ? "..." : ""),
    106 	       uh_len, errstr);
    107 }
    108 
    109 static void
    110 test_nlmsg_type_kernel(const int fd)
    111 {
    112 	struct udev_monitor_netlink_header uh = {
    113 		.prefix = "change@",
    114 		.magic = htonl(0xfeedcafe),
    115 		.header_size = sizeof(uh),
    116 		.properties_off = 10,
    117 		.properties_len = 299,
    118 		.filter_subsystem_hash = htonl(0xfffffff),
    119 		.filter_devtype_hash = htonl(0x10000000),
    120 		.filter_tag_bloom_hi = htonl(0x2000400),
    121 	};
    122 	const unsigned int uh_len = sizeof(uh);
    123 
    124 	TAIL_ALLOC_OBJECT_CONST_PTR(struct udev_monitor_netlink_header, p);
    125 	memcpy(p, &uh, uh_len);
    126 
    127 	sys_send(fd, p, uh_len);
    128 	printf("sendto(%d, ", fd);
    129 	print_quoted_memory(&uh, MIN(uh_len, DEFAULT_STRLEN));
    130 	printf("%s, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
    131 	       (uh_len > DEFAULT_STRLEN ? "..." : ""),
    132 	       uh_len, errstr);
    133 }
    134 
    135 int
    136 main(void)
    137 {
    138 	skip_if_unavailable("/proc/self/fd/");
    139 
    140 	int fd = create_nl_socket(NETLINK_KOBJECT_UEVENT);
    141 
    142 	test_nlmsg_type_udev(fd);
    143 	test_nlmsg_type_kernel(fd);
    144 	/* test using data that looks like a zero-length C string */
    145 	char *const buf = tail_alloc(DEFAULT_STRLEN + 1);
    146 	buf[0] = '=';
    147 	fill_memory_ex(buf + 1, DEFAULT_STRLEN, 0, DEFAULT_STRLEN);
    148 
    149 	sys_send(fd, buf + 1, DEFAULT_STRLEN);
    150 	printf("sendto(%d, ", fd);
    151 	print_quoted_memory(buf + 1, DEFAULT_STRLEN);
    152 	printf(", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
    153 	       DEFAULT_STRLEN, errstr);
    154 
    155 	sys_send(fd, buf, DEFAULT_STRLEN + 1);
    156 	printf("sendto(%d, ", fd);
    157 	print_quoted_memory(buf, DEFAULT_STRLEN);
    158 	printf("..., %u, MSG_DONTWAIT, NULL, 0) = %s\n",
    159 	       DEFAULT_STRLEN + 1, errstr);
    160 
    161 	puts("+++ exited with 0 +++");
    162 	return 0;
    163 }
    164