~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/net/socket.c

Version: ~ [ linux-6.0-rc6 ] ~ [ linux-5.19.10 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.69 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.144 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.214 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.259 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.294 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.329 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <stdio.h>
  3 #include <errno.h>
  4 #include <unistd.h>
  5 #include <string.h>
  6 #include <sys/types.h>
  7 #include <sys/socket.h>
  8 #include <netinet/in.h>
  9 
 10 struct socket_testcase {
 11         int     domain;
 12         int     type;
 13         int     protocol;
 14 
 15         /* 0    = valid file descriptor
 16          * -foo = error foo
 17          */
 18         int     expect;
 19 
 20         /* If non-zero, accept EAFNOSUPPORT to handle the case
 21          * of the protocol not being configured into the kernel.
 22          */
 23         int     nosupport_ok;
 24 };
 25 
 26 static struct socket_testcase tests[] = {
 27         { AF_MAX,  0,           0,           -EAFNOSUPPORT,    0 },
 28         { AF_INET, SOCK_STREAM, IPPROTO_TCP, 0,                1  },
 29         { AF_INET, SOCK_DGRAM,  IPPROTO_TCP, -EPROTONOSUPPORT, 1  },
 30         { AF_INET, SOCK_DGRAM,  IPPROTO_UDP, 0,                1  },
 31         { AF_INET, SOCK_STREAM, IPPROTO_UDP, -EPROTONOSUPPORT, 1  },
 32 };
 33 
 34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 35 #define ERR_STRING_SZ   64
 36 
 37 static int run_tests(void)
 38 {
 39         char err_string1[ERR_STRING_SZ];
 40         char err_string2[ERR_STRING_SZ];
 41         int i, err;
 42 
 43         err = 0;
 44         for (i = 0; i < ARRAY_SIZE(tests); i++) {
 45                 struct socket_testcase *s = &tests[i];
 46                 int fd;
 47 
 48                 fd = socket(s->domain, s->type, s->protocol);
 49                 if (fd < 0) {
 50                         if (s->nosupport_ok &&
 51                             errno == EAFNOSUPPORT)
 52                                 continue;
 53 
 54                         if (s->expect < 0 &&
 55                             errno == -s->expect)
 56                                 continue;
 57 
 58                         strerror_r(-s->expect, err_string1, ERR_STRING_SZ);
 59                         strerror_r(errno, err_string2, ERR_STRING_SZ);
 60 
 61                         fprintf(stderr, "socket(%d, %d, %d) expected "
 62                                 "err (%s) got (%s)\n",
 63                                 s->domain, s->type, s->protocol,
 64                                 err_string1, err_string2);
 65 
 66                         err = -1;
 67                         break;
 68                 } else {
 69                         close(fd);
 70 
 71                         if (s->expect < 0) {
 72                                 strerror_r(errno, err_string1, ERR_STRING_SZ);
 73 
 74                                 fprintf(stderr, "socket(%d, %d, %d) expected "
 75                                         "success got err (%s)\n",
 76                                         s->domain, s->type, s->protocol,
 77                                         err_string1);
 78 
 79                                 err = -1;
 80                                 break;
 81                         }
 82                 }
 83         }
 84 
 85         return err;
 86 }
 87 
 88 int main(void)
 89 {
 90         int err = run_tests();
 91 
 92         return err;
 93 }
 94 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp