]> granicus.if.org Git - strace/commitdiff
tests: check decoding of MCAST_JOIN_GROUP/MCAST_LEAVE_GROUP
authorDmitry V. Levin <ldv@altlinux.org>
Sun, 9 Jul 2017 18:43:34 +0000 (18:43 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Sun, 9 Jul 2017 18:43:34 +0000 (18:43 +0000)
* tests/group_req.c: New file.
* tests/gen_tests.in (group_req): New entry.
* tests/pure_executables.list: Add group_req.
* tests/.gitignore: Likewise.

tests/.gitignore
tests/gen_tests.in
tests/group_req.c [new file with mode: 0644]
tests/pure_executables.list

index 66c4d16303a16e0650e03b3f43506ba99f0527ec..0cf1287e09b21ebc1fc8a4493f60de997d6e290b 100644 (file)
@@ -110,6 +110,7 @@ gettid
 getuid
 getuid32
 getxxid
+group_req
 inet-cmsg
 init_module
 inotify
index a719409f12fa811c9fc21631a636cf780661fa26..e3e15f8174270d55b42bc84485ea48e1eb6c890e 100644 (file)
@@ -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 (file)
index 0000000..41b4ea3
--- /dev/null
@@ -0,0 +1,161 @@
+/*
+ * Check decoding of MCAST_JOIN_GROUP/MCAST_LEAVE_GROUP.
+ *
+ * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
+ * 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 <net/if.h>
+#include <netinet/in.h>
+
+#if defined HAVE_IF_INDEXTONAME \
+ && defined MCAST_JOIN_GROUP && defined MCAST_LEAVE_GROUP
+
+# include <stdio.h>
+# include <unistd.h>
+# include <sys/param.h>
+# include <sys/socket.h>
+# include <arpa/inet.h>
+
+#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
index 65c4a91a7c6829dccdcbc18fcdc65e320bd22b14..9e165bc0618c79e33b4d7ec19bb9ae49be2be60d 100755 (executable)
@@ -92,6 +92,7 @@ getsockname
 getuid
 getuid32
 getxxid
+group_req
 inet-cmsg
 init_module
 inotify