From: Dmitry V. Levin Date: Sun, 9 Jul 2017 18:43:34 +0000 (+0000) Subject: tests: check decoding of MCAST_JOIN_GROUP/MCAST_LEAVE_GROUP X-Git-Tag: v4.19~288 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b53552b2d834ae83ff723cf5d6927ae73c257632;p=strace tests: check decoding of MCAST_JOIN_GROUP/MCAST_LEAVE_GROUP * tests/group_req.c: New file. * tests/gen_tests.in (group_req): New entry. * tests/pure_executables.list: Add group_req. * tests/.gitignore: Likewise. --- diff --git a/tests/.gitignore b/tests/.gitignore index 66c4d163..0cf1287e 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -110,6 +110,7 @@ gettid getuid getuid32 getxxid +group_req inet-cmsg init_module inotify diff --git a/tests/gen_tests.in b/tests/gen_tests.in index a719409f..e3e15f81 100644 --- a/tests/gen_tests.in +++ b/tests/gen_tests.in @@ -115,6 +115,7 @@ getsockname -a27 gettid -a9 getuid32 +getuid.test getxxid -a10 -e trace=getxpid,getxuid,getxgid +group_req -e trace=setsockopt inet-cmsg -e trace=recvmsg init_module -a27 inotify -a23 -e trace=inotify_add_watch,inotify_rm_watch diff --git a/tests/group_req.c b/tests/group_req.c new file mode 100644 index 00000000..41b4ea38 --- /dev/null +++ b/tests/group_req.c @@ -0,0 +1,161 @@ +/* + * Check decoding of MCAST_JOIN_GROUP/MCAST_LEAVE_GROUP. + * + * Copyright (c) 2015-2017 Dmitry V. Levin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "tests.h" +#include +#include + +#if defined HAVE_IF_INDEXTONAME \ + && defined MCAST_JOIN_GROUP && defined MCAST_LEAVE_GROUP + +# include +# include +# include +# include +# include + +#define multi4addr "224.0.0.3" +#define multi6addr "ff01::c" + +static const char *errstr; + +static int +set_opt(const int fd, const int level, const int opt, + const void *const val, const socklen_t len) +{ + int rc = setsockopt(fd, level, opt, val, len); + errstr = sprintrc(rc); + return rc; +} + +int +main(void) +{ + TAIL_ALLOC_OBJECT_CONST_PTR(struct group_req, greq4); + TAIL_ALLOC_OBJECT_CONST_PTR(struct group_req, greq6); + unsigned int i; + + greq6->gr_interface = greq4->gr_interface = if_nametoindex("lo"); + if (!greq4->gr_interface) + perror_msg_and_skip("lo"); + + greq4->gr_group.ss_family = AF_INET; + inet_pton(AF_INET, multi4addr, &greq4->gr_group.ss_family + 2); + + greq6->gr_group.ss_family = AF_INET6; + inet_pton(AF_INET6, multi6addr, &greq6->gr_group.ss_family + 4); + + (void) close(0); + if (socket(AF_INET, SOCK_DGRAM, 0)) + perror_msg_and_skip("socket"); + + struct { + const int level; + const char *const str_level; + const int name; + const char *const str_name; + const struct group_req *const val; + const char *const addr; + } opts[] = { + { + ARG_STR(SOL_IP), ARG_STR(MCAST_JOIN_GROUP), greq4, + "gr_group={sa_family=AF_INET, sin_port=htons(65535)" + ", sin_addr=inet_addr(\"" multi4addr "\")}" + }, + { + ARG_STR(SOL_IP), ARG_STR(MCAST_LEAVE_GROUP), greq4, + "gr_group={sa_family=AF_INET, sin_port=htons(65535)" + ", sin_addr=inet_addr(\"" multi4addr "\")}" + }, + { + ARG_STR(SOL_IPV6), ARG_STR(MCAST_JOIN_GROUP), greq6, + "gr_group={sa_family=AF_INET6, sin6_port=htons(65535)" + ", inet_pton(AF_INET6, \"" multi6addr "\", &sin6_addr)" + ", sin6_flowinfo=htonl(4294967295)" + ", sin6_scope_id=4294967295}" + }, + { + ARG_STR(SOL_IPV6), ARG_STR(MCAST_LEAVE_GROUP), greq6, + "gr_group={sa_family=AF_INET6, sin6_port=htons(65535)" + ", inet_pton(AF_INET6, \"" multi6addr "\", &sin6_addr)" + ", sin6_flowinfo=htonl(4294967295)" + ", sin6_scope_id=4294967295}" + } + }; + + for (i = 0; i < ARRAY_SIZE(opts); ++i) { + /* optlen < 0, EINVAL */ + set_opt(0, opts[i].level, opts[i].name, opts[i].val, -1U); + printf("setsockopt(0, %s, %s, %p, -1) = %s\n", + opts[i].str_level, opts[i].str_name, + opts[i].val, errstr); + + /* optlen < sizeof(struct group_req), EINVAL */ + set_opt(0, opts[i].level, opts[i].name, opts[i].val, + sizeof(*opts[i].val) - 1); + printf("setsockopt(0, %s, %s, %p, %u) = %s\n", + opts[i].str_level, opts[i].str_name, + opts[i].val, (unsigned int) sizeof(*opts[i].val) - 1, + errstr); + + /* optval EFAULT */ + set_opt(0, opts[i].level, opts[i].name, + (const char *) opts[i].val + 1, sizeof(*opts[i].val)); + printf("setsockopt(0, %s, %s, %p, %u) = %s\n", + opts[i].str_level, opts[i].str_name, + (const char *) opts[i].val + 1, + (unsigned int) sizeof(*opts[i].val), errstr); + + /* classic */ + set_opt(0, opts[i].level, opts[i].name, + opts[i].val, sizeof(*opts[i].val)); + printf("setsockopt(0, %s, %s" + ", {gr_interface=if_nametoindex(\"lo\")" + ", %s}, %u) = %s\n", + opts[i].str_level, opts[i].str_name, opts[i].addr, + (unsigned int) sizeof(*opts[i].val), errstr); + + /* optlen > sizeof(struct group_req), shortened */ + set_opt(0, opts[i].level, opts[i].name, opts[i].val, INT_MAX); + printf("setsockopt(0, %s, %s" + ", {gr_interface=if_nametoindex(\"lo\")" + ", %s}, %u) = %s\n", + opts[i].str_level, opts[i].str_name, + opts[i].addr, INT_MAX, errstr); + } + + puts("+++ exited with 0 +++"); + return 0; +} + +#else + +SKIP_MAIN_UNDEFINED("HAVE_IF_INDEXTONAME && MCAST_JOIN_GROUP && MCAST_LEAVE_GROUP") + +#endif diff --git a/tests/pure_executables.list b/tests/pure_executables.list index 65c4a91a..9e165bc0 100755 --- a/tests/pure_executables.list +++ b/tests/pure_executables.list @@ -92,6 +92,7 @@ getsockname getuid getuid32 getxxid +group_req inet-cmsg init_module inotify