blob: 6091e3cf9510229f49dd21ffdd18e23f7dd29c9d [file] [log] [blame]
Oliver Smith06455ea2019-11-20 10:56:35 +01001/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 *
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 Affero General Public License as published by
7 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <stdbool.h>
21#include <string.h>
22#include <unistd.h>
23#include <osmocom/core/select.h>
24#include <osmocom/core/application.h>
25#include <osmocom/hlr/logging.h>
26#include <osmocom/mslookup/mslookup.h>
27#include <osmocom/mslookup/mslookup_client.h>
28#include <osmocom/mslookup/mslookup_client_mdns.h>
29#include <osmocom/mslookup/mdns.h>
30#include <osmocom/mslookup/mdns_sock.h>
31
32void *ctx = NULL;
33
34#define TEST_IP OSMO_MSLOOKUP_MDNS_IP4
35#define TEST_PORT OSMO_MSLOOKUP_MDNS_PORT
36#define TEST_DOMAIN_SUFFIX "mslookup_client_mdns_test.dgsm.osmocom.org"
37
38/*
39 * Test server (emulates the mDNS server in OsmoHLR) and client
40 */
41struct osmo_mdns_sock *server_mc;
42
43
44static void server_reply(struct osmo_mslookup_query *query, uint16_t packet_id)
45{
46 struct osmo_mslookup_result result = {0};
47 struct msgb *msg;
48
49 result.rc = OSMO_MSLOOKUP_RC_RESULT;
50 result.age = 3;
51 osmo_sockaddr_str_from_str(&result.host_v4, "42.42.42.42", 444);
52 osmo_sockaddr_str_from_str(&result.host_v6, "1122:3344:5566:7788:99aa:bbcc:ddee:ff00", 666);
53
54 msg = osmo_mdns_result_encode(ctx, packet_id, query, &result, TEST_DOMAIN_SUFFIX);
55 OSMO_ASSERT(msg);
56 OSMO_ASSERT(osmo_mdns_sock_send(server_mc, msg) == 0);
57}
58
59static int server_recv(struct osmo_fd *osmo_fd, unsigned int what)
60{
61 int n;
62 uint8_t buffer[1024];
63 uint16_t packet_id;
64 struct osmo_mslookup_query *query;
65
66 fprintf(stderr, "%s\n", __func__);
67
68 /* Parse the message and print it */
69 n = read(osmo_fd->fd, buffer, sizeof(buffer));
70 OSMO_ASSERT(n >= 0);
71
72 query = osmo_mdns_query_decode(ctx, buffer, n, &packet_id, TEST_DOMAIN_SUFFIX);
73 if (!query)
74 return -1; /* server receiving own answer is expected */
75
76 fprintf(stderr, "received request\n");
77 server_reply(query, packet_id);
78 talloc_free(query);
79 return n;
80}
81
82static void server_init()
83{
84 fprintf(stderr, "%s\n", __func__);
85 server_mc = osmo_mdns_sock_init(ctx, TEST_IP, TEST_PORT, server_recv, NULL, 0);
86 OSMO_ASSERT(server_mc);
87}
88
89static void server_stop()
90{
91 fprintf(stderr, "%s\n", __func__);
92 OSMO_ASSERT(server_mc);
93 osmo_mdns_sock_cleanup(server_mc);
94 server_mc = NULL;
95}
96
97struct osmo_mslookup_client* client;
98struct osmo_mslookup_client_method* client_method;
99
100static void client_init()
101{
102 fprintf(stderr, "%s\n", __func__);
103 client = osmo_mslookup_client_new(ctx);
104 OSMO_ASSERT(client);
105 client_method = osmo_mslookup_client_add_mdns(client, TEST_IP, TEST_PORT, 1337, TEST_DOMAIN_SUFFIX);
106 OSMO_ASSERT(client_method);
107}
108
109static void client_recv(struct osmo_mslookup_client *client, uint32_t request_handle,
110 const struct osmo_mslookup_query *query, const struct osmo_mslookup_result *result)
111{
112 char buf[256];
113 fprintf(stderr, "%s\n", __func__);
114 fprintf(stderr, "client_recv(): %s\n", osmo_mslookup_result_name_b(buf, sizeof(buf), query, result));
115
116 osmo_mslookup_client_request_cancel(client, request_handle);
117}
118
119static void client_query()
120{
121 struct osmo_mslookup_id id = {.type = OSMO_MSLOOKUP_ID_IMSI,
122 .imsi = "123456789012345"};
123 const struct osmo_mslookup_query query = {
124 .service = "gsup.hlr",
125 .id = id,
126 };
127 struct osmo_mslookup_query_handling handling = {
128 .result_timeout_milliseconds = 2000,
129 .result_cb = client_recv,
130 };
131
132 fprintf(stderr, "%s\n", __func__);
133 osmo_mslookup_client_request(client, &query, &handling);
134}
135
136static void client_stop()
137{
138 fprintf(stderr, "%s\n", __func__);
139 osmo_mslookup_client_free(client);
140 client = NULL;
141}
142const struct timeval fake_time_start_time = { 0, 0 };
143
144#define fake_time_passes(secs, usecs) do \
145{ \
146 struct timeval diff; \
147 osmo_gettimeofday_override_add(secs, usecs); \
148 osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \
149 timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \
150 LOGP(DMSLOOKUP, LOGL_DEBUG, "Total time passed: %d.%06d s\n", \
151 (int)diff.tv_sec, (int)diff.tv_usec); \
152 osmo_timers_prepare(); \
153 osmo_timers_update(); \
154} while (0)
155
156static void fake_time_start()
157{
158 struct timespec *clock_override;
159
160 osmo_gettimeofday_override_time = fake_time_start_time;
161 osmo_gettimeofday_override = true;
162 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
163 OSMO_ASSERT(clock_override);
164 clock_override->tv_sec = fake_time_start_time.tv_sec;
165 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
166 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
167 fake_time_passes(0, 0);
168}
169static void test_server_client()
170{
171 fprintf(stderr, "-- %s --\n", __func__);
172 server_init();
173 client_init();
174 client_query();
175
176 /* Let the server receive the query and indirectly call server_recv(). As side effect of using the same IP and
177 * port, the client will also receive its own question. The client will dismiss its own question, as it is just
178 * looking for answers. */
179 OSMO_ASSERT(osmo_select_main_ctx(1) == 1);
180
181 /* Let the mslookup client receive the answer (also same side effect as above). It does not call the callback
182 * (client_recv()) just yet, because it is waiting for the best result within two seconds. */
183 OSMO_ASSERT(osmo_select_main_ctx(1) == 1);
184
185 /* Time flies by, client_recv() gets called. */
186 fake_time_passes(5, 0);
187
188 server_stop();
189 client_stop();
190}
191
192/*
193 * Run all tests
194 */
195int main()
196{
197 talloc_enable_null_tracking();
198 ctx = talloc_named_const(NULL, 0, "main");
199 osmo_init_logging2(ctx, NULL);
200
201 log_set_print_filename(osmo_stderr_target, 0);
202 log_set_print_level(osmo_stderr_target, 0);
203 log_set_print_category(osmo_stderr_target, 0);
204 log_set_print_category_hex(osmo_stderr_target, 0);
205 log_set_use_color(osmo_stderr_target, 0);
206 log_set_category_filter(osmo_stderr_target, DMSLOOKUP, true, LOGL_DEBUG);
207
208 fake_time_start();
209
210 test_server_client();
211
212 log_fini();
213
214 OSMO_ASSERT(talloc_total_blocks(ctx) == 1);
215 talloc_free(ctx);
216 OSMO_ASSERT(talloc_total_blocks(NULL) == 1);
217 talloc_disable_null_tracking();
218
219 return 0;
220}