Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | |
| 27 | #include <sys/socket.h> |
| 28 | #include <arpa/inet.h> |
| 29 | #include <netinet/in.h> |
| 30 | |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 31 | #include <osmocom/core/application.h> |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
| 33 | #include <osmocom/core/socket.h> |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 34 | #include <osmocom/core/logging.h> |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 35 | |
| 36 | #include "../config.h" |
| 37 | |
| 38 | static int test_sockinit(void) |
| 39 | { |
| 40 | int fd, rc; |
| 41 | char *name; |
| 42 | |
| 43 | printf("Checking osmo_sock_init() with bind to a random local UDP port\n"); |
| 44 | fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 45 | "0.0.0.0", 0, OSMO_SOCK_F_BIND); |
| 46 | OSMO_ASSERT(fd >= 0); |
| 47 | name = osmo_sock_get_name(NULL, fd); |
| 48 | /* expect it to be not connected. We cannot match on INADDR_ANY, |
| 49 | * as apparently that won't work on FreeBSD if there's only one |
| 50 | * address (e.g. 127.0.0.1) assigned to the entire system, like |
| 51 | * the Osmocom FreeBSD build slaves */ |
| 52 | OSMO_ASSERT(!strncmp(name, "(NULL<->", 7)); |
| 53 | talloc_free(name); |
| 54 | /* expect it to be blocking */ |
| 55 | rc = fcntl(fd, F_GETFL); |
| 56 | OSMO_ASSERT(!(rc & O_NONBLOCK)); |
| 57 | close(fd); |
| 58 | |
| 59 | printf("Checking for OSMO_SOCK_F_NONBLOCK\n"); |
| 60 | fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 61 | "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK); |
| 62 | OSMO_ASSERT(fd >= 0); |
| 63 | /* expect it to be blocking */ |
| 64 | rc = fcntl(fd, F_GETFL); |
| 65 | OSMO_ASSERT(rc & O_NONBLOCK); |
| 66 | close(fd); |
| 67 | |
| 68 | printf("Checking for invalid flags\n"); |
| 69 | fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 70 | "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT); |
| 71 | OSMO_ASSERT(fd < 0); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 76 | static int test_sockinit2(void) |
| 77 | { |
| 78 | int fd, rc; |
| 79 | char *name; |
| 80 | |
| 81 | printf("Checking osmo_sock_init2() with bind to a random local UDP port\n"); |
| 82 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 83 | "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND); |
| 84 | OSMO_ASSERT(fd >= 0); |
| 85 | name = osmo_sock_get_name(NULL, fd); |
| 86 | /* expect it to be not connected. We cannot match on INADDR_ANY, |
| 87 | * as apparently that won't work on FreeBSD if there's only one |
| 88 | * address (e.g. 127.0.0.1) assigned to the entire system, like |
| 89 | * the Osmocom FreeBSD build slaves */ |
| 90 | OSMO_ASSERT(!strncmp(name, "(NULL<->", 7)); |
| 91 | talloc_free(name); |
| 92 | /* expect it to be blocking */ |
| 93 | rc = fcntl(fd, F_GETFL); |
| 94 | OSMO_ASSERT(!(rc & O_NONBLOCK)); |
| 95 | close(fd); |
| 96 | |
| 97 | printf("Checking osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK\n"); |
| 98 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 99 | "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK); |
| 100 | OSMO_ASSERT(fd >= 0); |
| 101 | /* expect it to be blocking */ |
| 102 | rc = fcntl(fd, F_GETFL); |
| 103 | OSMO_ASSERT(rc & O_NONBLOCK); |
| 104 | close(fd); |
| 105 | |
| 106 | printf("Checking osmo_sock_init2() for invalid flags\n"); |
| 107 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "0.0.0.0", 0, NULL, 0, 0); |
| 108 | OSMO_ASSERT(fd < 0); |
| 109 | |
| 110 | printf("Checking osmo_sock_init2() for combined BIND + CONNECT\n"); |
| 111 | fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "127.0.0.1", 53, |
| 112 | OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT); |
| 113 | OSMO_ASSERT(fd >= 0); |
| 114 | name = osmo_sock_get_name(NULL, fd); |
| 115 | #ifndef __FreeBSD__ |
| 116 | /* For some reason, on the jenkins.osmocom.org build slave with |
| 117 | * FreeBSD 10 inside a jail, it fails. Works fine on laforge's |
| 118 | * FreeBSD 10 or 11 VM at home */ |
| 119 | OSMO_ASSERT(!strncmp(name, "(127.0.0.1:53<->127.0.0.1", 25)); |
| 120 | #endif |
| 121 | talloc_free(name); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 127 | const struct log_info_cat default_categories[] = { |
| 128 | }; |
| 129 | |
| 130 | static struct log_info info = { |
| 131 | .cat = default_categories, |
| 132 | .num_cat = ARRAY_SIZE(default_categories), |
| 133 | }; |
| 134 | |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 135 | int main(int argc, char *argv[]) |
| 136 | { |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 137 | osmo_init_logging(&info); |
| 138 | log_set_use_color(osmo_stderr_target, 0); |
| 139 | log_set_print_filename(osmo_stderr_target, 0); |
| 140 | |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 141 | test_sockinit(); |
Harald Welte | dda70fc | 2017-04-08 20:52:33 +0200 | [diff] [blame] | 142 | test_sockinit2(); |
Philipp Maier | 6f0f560 | 2017-02-09 14:09:06 +0100 | [diff] [blame] | 143 | |
| 144 | return EXIT_SUCCESS; |
Harald Welte | 4ffb43f | 2017-01-27 10:29:49 +0100 | [diff] [blame] | 145 | } |