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