blob: 37e028194b7a451e679569c120e3f7e92dcd584e [file] [log] [blame]
Harald Welte4ffb43f2017-01-27 10:29:49 +01001/*
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 Maier6f0f5602017-02-09 14:09:06 +010031#include <osmocom/core/application.h>
Harald Welte4ffb43f2017-01-27 10:29:49 +010032#include <osmocom/core/utils.h>
33#include <osmocom/core/socket.h>
Philipp Maier6f0f5602017-02-09 14:09:06 +010034#include <osmocom/core/logging.h>
Harald Welte4ffb43f2017-01-27 10:29:49 +010035
36#include "../config.h"
37
Neels Hofmeyra829b452018-04-05 03:02:35 +020038void *ctx = NULL;
39
Harald Welte4ffb43f2017-01-27 10:29:49 +010040static int test_sockinit(void)
41{
42 int fd, rc;
43 char *name;
44
45 printf("Checking osmo_sock_init() with bind to a random local UDP port\n");
46 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
47 "0.0.0.0", 0, OSMO_SOCK_F_BIND);
48 OSMO_ASSERT(fd >= 0);
Neels Hofmeyra829b452018-04-05 03:02:35 +020049 name = osmo_sock_get_name(ctx, fd);
Harald Welte4ffb43f2017-01-27 10:29:49 +010050 /* expect it to be not connected. We cannot match on INADDR_ANY,
51 * as apparently that won't work on FreeBSD if there's only one
52 * address (e.g. 127.0.0.1) assigned to the entire system, like
53 * the Osmocom FreeBSD build slaves */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +020054 OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9));
Harald Welte4ffb43f2017-01-27 10:29:49 +010055 talloc_free(name);
56 /* expect it to be blocking */
57 rc = fcntl(fd, F_GETFL);
58 OSMO_ASSERT(!(rc & O_NONBLOCK));
59 close(fd);
60
61 printf("Checking for OSMO_SOCK_F_NONBLOCK\n");
62 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
63 "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
64 OSMO_ASSERT(fd >= 0);
65 /* expect it to be blocking */
66 rc = fcntl(fd, F_GETFL);
67 OSMO_ASSERT(rc & O_NONBLOCK);
68 close(fd);
69
70 printf("Checking for invalid flags\n");
71 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
72 "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
73 OSMO_ASSERT(fd < 0);
74
75 return 0;
76}
77
Harald Weltedda70fc2017-04-08 20:52:33 +020078static int test_sockinit2(void)
79{
80 int fd, rc;
81 char *name;
82
83 printf("Checking osmo_sock_init2() with bind to a random local UDP port\n");
84 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
85 "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND);
86 OSMO_ASSERT(fd >= 0);
Neels Hofmeyra829b452018-04-05 03:02:35 +020087 name = osmo_sock_get_name(ctx, fd);
Harald Weltedda70fc2017-04-08 20:52:33 +020088 /* expect it to be not connected. We cannot match on INADDR_ANY,
89 * as apparently that won't work on FreeBSD if there's only one
90 * address (e.g. 127.0.0.1) assigned to the entire system, like
91 * the Osmocom FreeBSD build slaves */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +020092 OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9));
Harald Weltedda70fc2017-04-08 20:52:33 +020093 talloc_free(name);
94 /* expect it to be blocking */
95 rc = fcntl(fd, F_GETFL);
96 OSMO_ASSERT(!(rc & O_NONBLOCK));
97 close(fd);
98
99 printf("Checking osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK\n");
100 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
101 "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
102 OSMO_ASSERT(fd >= 0);
103 /* expect it to be blocking */
104 rc = fcntl(fd, F_GETFL);
105 OSMO_ASSERT(rc & O_NONBLOCK);
106 close(fd);
107
108 printf("Checking osmo_sock_init2() for invalid flags\n");
109 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "0.0.0.0", 0, NULL, 0, 0);
110 OSMO_ASSERT(fd < 0);
111
112 printf("Checking osmo_sock_init2() for combined BIND + CONNECT\n");
113 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "127.0.0.1", 53,
114 OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
115 OSMO_ASSERT(fd >= 0);
Neels Hofmeyra829b452018-04-05 03:02:35 +0200116 name = osmo_sock_get_name(ctx, fd);
Harald Weltedda70fc2017-04-08 20:52:33 +0200117#ifndef __FreeBSD__
118 /* For some reason, on the jenkins.osmocom.org build slave with
119 * FreeBSD 10 inside a jail, it fails. Works fine on laforge's
120 * FreeBSD 10 or 11 VM at home */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +0200121 OSMO_ASSERT(!strncmp(name, "(r=127.0.0.1:53<->l=127.0.0.1", 29));
Harald Weltedda70fc2017-04-08 20:52:33 +0200122#endif
123 talloc_free(name);
124
125 return 0;
126}
127
128
Philipp Maier6f0f5602017-02-09 14:09:06 +0100129const struct log_info_cat default_categories[] = {
130};
131
132static struct log_info info = {
133 .cat = default_categories,
134 .num_cat = ARRAY_SIZE(default_categories),
135};
136
Harald Welte4ffb43f2017-01-27 10:29:49 +0100137int main(int argc, char *argv[])
138{
Neels Hofmeyra829b452018-04-05 03:02:35 +0200139 ctx = talloc_named_const(NULL, 0, "socket_test");
140 osmo_init_logging2(ctx, &info);
Philipp Maier6f0f5602017-02-09 14:09:06 +0100141 log_set_use_color(osmo_stderr_target, 0);
142 log_set_print_filename(osmo_stderr_target, 0);
143
Harald Welte4ffb43f2017-01-27 10:29:49 +0100144 test_sockinit();
Harald Weltedda70fc2017-04-08 20:52:33 +0200145 test_sockinit2();
Philipp Maier6f0f5602017-02-09 14:09:06 +0100146
147 return EXIT_SUCCESS;
Harald Welte4ffb43f2017-01-27 10:29:49 +0100148}