blob: 11ef8da4fac0620515efeba7073af049417ae51d [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
38static 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 */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +020052 OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9));
Harald Welte4ffb43f2017-01-27 10:29:49 +010053 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 Weltedda70fc2017-04-08 20:52:33 +020076static 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 */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +020090 OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9));
Harald Weltedda70fc2017-04-08 20:52:33 +020091 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 */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +0200119 OSMO_ASSERT(!strncmp(name, "(r=127.0.0.1:53<->l=127.0.0.1", 29));
Harald Weltedda70fc2017-04-08 20:52:33 +0200120#endif
121 talloc_free(name);
122
123 return 0;
124}
125
126
Philipp Maier6f0f5602017-02-09 14:09:06 +0100127const struct log_info_cat default_categories[] = {
128};
129
130static struct log_info info = {
131 .cat = default_categories,
132 .num_cat = ARRAY_SIZE(default_categories),
133};
134
Harald Welte4ffb43f2017-01-27 10:29:49 +0100135int main(int argc, char *argv[])
136{
Philipp Maier6f0f5602017-02-09 14:09:06 +0100137 osmo_init_logging(&info);
138 log_set_use_color(osmo_stderr_target, 0);
139 log_set_print_filename(osmo_stderr_target, 0);
140
Harald Welte4ffb43f2017-01-27 10:29:49 +0100141 test_sockinit();
Harald Weltedda70fc2017-04-08 20:52:33 +0200142 test_sockinit2();
Philipp Maier6f0f5602017-02-09 14:09:06 +0100143
144 return EXIT_SUCCESS;
Harald Welte4ffb43f2017-01-27 10:29:49 +0100145}