blob: 5b6abc421d7edd0b392db4f33ced87678fa270b2 [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 */
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
Philipp Maier6f0f5602017-02-09 14:09:06 +010076const struct log_info_cat default_categories[] = {
77};
78
79static struct log_info info = {
80 .cat = default_categories,
81 .num_cat = ARRAY_SIZE(default_categories),
82};
83
Harald Welte4ffb43f2017-01-27 10:29:49 +010084int main(int argc, char *argv[])
85{
Philipp Maier6f0f5602017-02-09 14:09:06 +010086 osmo_init_logging(&info);
87 log_set_use_color(osmo_stderr_target, 0);
88 log_set_print_filename(osmo_stderr_target, 0);
89
Harald Welte4ffb43f2017-01-27 10:29:49 +010090 test_sockinit();
Philipp Maier6f0f5602017-02-09 14:09:06 +010091
92 return EXIT_SUCCESS;
Harald Welte4ffb43f2017-01-27 10:29:49 +010093}