blob: 75088e5f39c6d242198f37b592b97ac73af3bcf2 [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
31#include <osmocom/core/utils.h>
32#include <osmocom/core/socket.h>
33
34#include "../config.h"
35
36static int test_sockinit(void)
37{
38 int fd, rc;
39 char *name;
40
41 printf("Checking osmo_sock_init() with bind to a random local UDP port\n");
42 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
43 "0.0.0.0", 0, OSMO_SOCK_F_BIND);
44 OSMO_ASSERT(fd >= 0);
45 name = osmo_sock_get_name(NULL, fd);
46 /* expect it to be not connected. We cannot match on INADDR_ANY,
47 * as apparently that won't work on FreeBSD if there's only one
48 * address (e.g. 127.0.0.1) assigned to the entire system, like
49 * the Osmocom FreeBSD build slaves */
50 OSMO_ASSERT(!strncmp(name, "(NULL<->", 7));
51 talloc_free(name);
52 /* expect it to be blocking */
53 rc = fcntl(fd, F_GETFL);
54 OSMO_ASSERT(!(rc & O_NONBLOCK));
55 close(fd);
56
57 printf("Checking for OSMO_SOCK_F_NONBLOCK\n");
58 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
59 "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
60 OSMO_ASSERT(fd >= 0);
61 /* expect it to be blocking */
62 rc = fcntl(fd, F_GETFL);
63 OSMO_ASSERT(rc & O_NONBLOCK);
64 close(fd);
65
66 printf("Checking for invalid flags\n");
67 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
68 "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
69 OSMO_ASSERT(fd < 0);
70
71 return 0;
72}
73
74int main(int argc, char *argv[])
75{
76 test_sockinit();
77 return 0;
78}