]> granicus.if.org Git - strace/blob - tests/recvmsg.c
tests: extend TEST_NETLINK_OBJECT macro
[strace] / tests / recvmsg.c
1 /*
2  * Check decoding of recvmsg and sendmsg syscalls.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/socket.h>
36 #include <sys/uio.h>
37
38 int
39 main(void)
40 {
41         tprintf("%s", "");
42
43         int fds[2];
44         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
45                 perror_msg_and_skip("socketpair");
46         assert(0 == fds[0]);
47         assert(1 == fds[1]);
48
49         static const char w0_c[] = "012";
50         const char *w0_d = hexdump_strdup(w0_c);
51         void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
52
53         static const char w1_c[] = "34567";
54         const char *w1_d = hexdump_strdup(w1_c);
55         void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
56
57         static const char w2_c[] = "89abcde";
58         const char *w2_d = hexdump_strdup(w2_c);
59         void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
60
61         static const char r0_c[] = "01234567";
62         const char *r0_d = hexdump_strdup(r0_c);
63         static const char r1_c[] = "89abcde";
64         const char *r1_d = hexdump_strdup(r1_c);
65
66         const struct iovec w_iov_[] = {
67                 {
68                         .iov_base = w0,
69                         .iov_len = LENGTH_OF(w0_c)
70                 }, {
71                         .iov_base = w1,
72                         .iov_len = LENGTH_OF(w1_c)
73                 }, {
74                         .iov_base = w2,
75                         .iov_len = LENGTH_OF(w2_c)
76                 }
77         };
78         struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
79         const unsigned int w_len =
80                 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
81
82         const struct msghdr w_mh_ = {
83                 .msg_iov = w_iov,
84                 .msg_iovlen = ARRAY_SIZE(w_iov_)
85         };
86         const struct msghdr *w_mh = tail_memdup(&w_mh_, sizeof(w_mh_));
87
88         assert(sendmsg(1, w_mh, 0) == (int) w_len);
89         close(1);
90         tprintf("sendmsg(1, {msg_name=NULL, msg_namelen=0, msg_iov="
91                 "[{iov_base=\"%s\", iov_len=%u}, {iov_base=\"%s\", iov_len=%u}"
92                 ", {iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
93                 ", msg_controllen=0, msg_flags=0}, 0) = %u\n"
94                 " * %u bytes in buffer 0\n"
95                 " | 00000 %-49s  %-16s |\n"
96                 " * %u bytes in buffer 1\n"
97                 " | 00000 %-49s  %-16s |\n"
98                 " * %u bytes in buffer 2\n"
99                 " | 00000 %-49s  %-16s |\n",
100                 w0_c, LENGTH_OF(w0_c),
101                 w1_c, LENGTH_OF(w1_c),
102                 w2_c, LENGTH_OF(w2_c),
103                 (unsigned int) ARRAY_SIZE(w_iov_), w_len,
104                 LENGTH_OF(w0_c), w0_d, w0_c,
105                 LENGTH_OF(w1_c), w1_d, w1_c,
106                 LENGTH_OF(w2_c), w2_d, w2_c);
107
108         const unsigned int r_len = (w_len + 1) / 2;
109         void *r0 = tail_alloc(r_len);
110         const struct iovec r0_iov_[] = {
111                 {
112                         .iov_base = r0,
113                         .iov_len = r_len
114                 }
115         };
116         struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
117
118         const struct msghdr r_mh_ = {
119                 .msg_iov = r_iov,
120                 .msg_iovlen = ARRAY_SIZE(r0_iov_)
121         };
122         struct msghdr *r_mh = tail_memdup(&r_mh_, sizeof(r_mh_));
123
124         assert(recvmsg(0, r_mh, 0) == (int) r_len);
125         tprintf("recvmsg(0, {msg_name=NULL, msg_namelen=0, msg_iov="
126                 "[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u"
127                 ", msg_controllen=0, msg_flags=0}, 0) = %u\n"
128                 " * %u bytes in buffer 0\n"
129                 " | 00000 %-49s  %-16s |\n",
130                 r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_),
131                 r_len, r_len, r0_d, r0_c);
132
133         void *r1 = tail_alloc(r_len);
134         void *r2 = tail_alloc(w_len);
135         const struct iovec r1_iov_[] = {
136                 {
137                         .iov_base = r1,
138                         .iov_len = r_len
139                 },
140                 {
141                         .iov_base = r2,
142                         .iov_len = w_len
143                 }
144         };
145         r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
146         r_mh->msg_iov = r_iov;
147         r_mh->msg_iovlen = ARRAY_SIZE(r1_iov_);
148
149         assert(recvmsg(0, r_mh, 0) == (int) w_len - (int) r_len);
150         tprintf("recvmsg(0, {msg_name=NULL, msg_namelen=0, msg_iov="
151                 "[{iov_base=\"%s\", iov_len=%u}, {iov_base=\"\", iov_len=%u}]"
152                 ", msg_iovlen=%u, msg_controllen=0, msg_flags=0}, 0) = %u\n"
153                 " * %u bytes in buffer 0\n"
154                 " | 00000 %-49s  %-16s |\n",
155                 r1_c, r_len, w_len, (unsigned int) ARRAY_SIZE(r1_iov_),
156                 w_len - r_len, w_len - r_len, r1_d, r1_c);
157         close(0);
158
159         tprintf("+++ exited with 0 +++\n");
160         return 0;
161 }