aboutsummaryrefslogtreecommitdiffstats
path: root/remsim/RemsimServer_Tests.ttcn
blob: fd7a1a47770487a2d116bde22738c4e7df67bfc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
module RemsimServer_Tests {

/* Integration Tests for osmo-remsim-server
 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This test suite tests osmo-remsim-server by attaching to the external interfaces
 * such as RSPRO for simulated clients + bankds and RSRES (REST backend interface).
 */

import from Osmocom_Types all;

import from RSPRO all;
import from RSRES all;
import from RSPRO_Types all;
import from REMSIM_Tests all;

import from IPA_Emulation all;

import from HTTPmsg_Types all;
import from HTTPmsg_PortType all;
import from HTTP_Adapter all;
import from JSON_Types all;

/* run a HTTP GET on specified URL expecting json in RSRES format as response */
function f_rsres_get(charstring url, template integer exp_sts := 200)
runs on http_CT return JsRoot {
	var HTTPMessage http_resp;
	http_resp := f_http_transact(url, exp := tr_HTTP_Resp(exp_sts));
	return f_dec_JsRoot(char2oct(http_resp.response.body));
}

/* run a HTTP PUT to add a new slotmap to the remsim-server */
function f_rsres_post_slotmap(JsSlotmap slotmap, template integer exp_sts := 201)
runs on http_CT return HTTPResponse {
	var charstring body := oct2char(f_enc_JsSlotmap(slotmap));
	var HTTPMessage http_resp;
	http_resp := f_http_transact(url := "/api/backend/v1/slotmaps", method := "POST",
				     body := body, exp := tr_HTTP_Resp(exp_sts))
	return http_resp.response;
}

/* run a HTTP PUT to add a new slotmap to the remsim-server */
function f_rsres_post_reset(template integer exp_sts := 200)
runs on http_CT return HTTPResponse {
	var HTTPMessage http_resp;
	http_resp := f_http_transact(url := "/api/backend/v1/global-reset", method := "POST",
				     body := "", exp := tr_HTTP_Resp(exp_sts))
	return http_resp.response;
}


/* run a HTTP DELETE to remove a slotmap from te remsim-server */
function f_rsres_delete_slotmap(BankSlot bs, template integer exp_sts := 200)
runs on http_CT return HTTPResponse {
	var HTTPMessage http_resp;
	var integer slotmap_id := bs.bankId * 65536 + bs.slotNr;
	http_resp := f_http_transact(url := "/api/backend/v1/slotmaps/" & int2str(slotmap_id),
				     method := "DELETE", exp := tr_HTTP_Resp(exp_sts));
	return http_resp.response;
}


function f_rsres_init() runs on http_CT {
	f_http_init(mp_server_ip, mp_rsres_port);
	f_rsres_post_reset();
}

type component test_CT extends rspro_client_CT, http_CT {
};


testcase TC_connect_and_nothing() runs on rspro_client_CT {
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimClient, "foobar"));
	timer T := 20.0;

	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	T.start;
	/* expect that we're disconnected if we never send a ConnectClientReq */
	alt {
	[] RSPRO[0].receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_ID_ACK)) { repeat; }
	[] RSPRO[0].receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_DOWN)) {
		setverdict(pass);
		}
	[] T.timeout {
		setverdict(fail, "Timeout waiting for disconnect");
		}
	}
}

testcase TC_connect_client() runs on test_CT {
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimClient, "foobar"));
	var JsRoot js;

	f_rsres_init();
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_client_slot := valueof(ts_ClientSlot(3,4));

	js := f_rsres_get("/api/backend/v1/clients");
	if (not match(js.clients, JsClients:{})) {
		setverdict(fail, "Initial state not empty");
		mtc.stop;
	}
	f_rspro_connect_client(0);
	js := f_rsres_get("/api/backend/v1/clients");
	if (not match(js.clients[0], tr_JsClient(CONNECTED_CLIENT, rspro[0].rspro_id))) {
		setverdict(fail, "Non-matching JSON response");
		mtc.stop;
	}
	//as_rspro_cfg_client_id(0, cslot);
	setverdict(pass);
}

testcase TC_connect_bank() runs on test_CT {
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimBankd, "foobar"));
	var JsRoot js;

	f_rsres_init();
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_bank_id := 1;
	rspro[0].rspro_bank_nslots := 8;

	js := f_rsres_get("/api/backend/v1/banks");
	if (not match(js.banks, JsBanks:{})) {
		setverdict(fail, "Initial state not empty");
		mtc.stop;
	}
	f_rspro_connect_client(0);
	js := f_rsres_get("/api/backend/v1/banks");
	if (not match(js.banks[0], tr_JsBank(CONNECTED_BANKD, rspro[0].rspro_id, rspro[0].rspro_bank_id,
					     rspro[0].rspro_bank_nslots))) {
		setverdict(fail, "Non-matching JSON response");
		mtc.stop;
	}
	setverdict(pass);
}

function f_ensure_slotmaps(template JsSlotmaps maps)
runs on http_CT {
	var JsRoot js;

	/* check that it is actually added */
	js := f_rsres_get("/api/backend/v1/slotmaps");
	if (match(js.slotmaps, maps)) {
		setverdict(pass);
	} else {
		setverdict(fail, "Unexpected slotmaps: ", js);
	}
}

/* verify that exactly only one slotmap exists (the specified one) */
function f_ensure_slotmap_exists_only(template ClientSlot cslot, template BankSlot bslot,
					template SlotmapState state := ?)
runs on http_CT {
	f_ensure_slotmaps({ tr_JsSlotmap(bslot, cslot, state) } );
}

/* verify that exactly only one slotmap exists (possibly among others) */
function f_ensure_slotmap_exists(template ClientSlot cslot, template BankSlot bslot,
				 template SlotmapState state := ?)
runs on http_CT {
	f_ensure_slotmaps({ *, tr_JsSlotmap(bslot, cslot, state), * } );
}


/* test adding a single slotmap */
testcase TC_slotmap_add() runs on test_CT {
	f_rsres_init();

	var JsSlotmap sm := valueof(ts_JsSlotmap(ts_BankSlot(1,2), ts_ClientSlot(3,4)));
	var HTTPResponse res := f_rsres_post_slotmap(sm);

	/* check that it is actually added */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, NEW);
}

/* test adding a single slotmap with out-of-range values */
testcase TC_slotmap_add_out_of_range() runs on test_CT {
	f_rsres_init();

	var HTTPMessage http_resp;
	var charstring body;

	body := "{ \"bank\": { \"bankId\": 10000, \"slotNr\": 2 }, \"client\": { \"clientId\": 3, \"slotNr\": 4 } }";
	http_resp := f_http_transact(url := "/api/backend/v1/slotmaps", method := "POST",
				     body := body, exp := tr_HTTP_Resp(400));

	body := "{ \"bank\": { \"bankId\": 100, \"slotNr\": 2000 }, \"client\": { \"clientId\": 3, \"slotNr\": 4 } }";
	http_resp := f_http_transact(url := "/api/backend/v1/slotmaps", method := "POST",
				     body := body, exp := tr_HTTP_Resp(400));

	body := "{ \"bank\": { \"bankId\": 100, \"slotNr\": 2 }, \"client\": { \"clientId\": 3000, \"slotNr\": 4 } }";
	http_resp := f_http_transact(url := "/api/backend/v1/slotmaps", method := "POST",
				     body := body, exp := tr_HTTP_Resp(400));

	body := "{ \"bank\": { \"bankId\": 100, \"slotNr\": 2 }, \"client\": { \"clientId\": 3, \"slotNr\": 4000 } }";
	http_resp := f_http_transact(url := "/api/backend/v1/slotmaps", method := "POST",
				     body := body, exp := tr_HTTP_Resp(400));
}


/* test adding a slotmap and then connecting a client + bankd */
testcase TC_slotmap_add_conn_cl_b() runs on test_CT {
	/* Simulate one client */
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimClient, testcasename()));
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_client_slot := valueof(ts_ClientSlot(3,4));

	/* Simulate one bankd */
	var BankSlot bslot := valueof(ts_BankSlot(1,2));
	var ComponentIdentity rspro_bank_id := valueof(ts_CompId(remsimBankd, testcasename()));
	f_rspro_init(rspro[1], mp_server_ip, mp_server_port, rspro_bank_id, 1);
	rspro[1].rspro_bank_id := bslot.bankId;
	rspro[1].rspro_bank_nslots := 8

	f_rsres_init();
	var JsSlotmap sm := valueof(ts_JsSlotmap(bslot, rspro[0].rspro_client_slot));
	var HTTPResponse res;

	/* 1) Create a new slotmap via HTTP */
	res := f_rsres_post_slotmap(sm);

	/* 2) verify that the slotmap exists and is NEW */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, NEW);

	/* 3) connect a client for that slotmap */
	f_rspro_connect_client(0);

	/* 4) connect a bankd for that slotmap */
	f_rspro_connect_client(1);

	/* 5) verify that the slotmap exists and is UNACKNOWLEDGED */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, UNACKNOWLEDGED);

	/* 6) expect bankd to receive that mapping */
	as_rspro_create_mapping(1, sm.client, sm.bank);

	/* 7) verify that the slotmap exists and is ACTIVE */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, ACTIVE);

	/* 8) expect the client to be configured with bankd side settings */
	as_rspro_cfg_client_bank(0, bslot, ?);
}

/* test connecting a client and later adding a slotmap for it */
testcase TC_conn_cl_b_slotmap_add() runs on test_CT {
	/* Simulate one client */
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimClient, testcasename()));
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_client_slot := valueof(ts_ClientSlot(3,4));

	/* Simulate one bankd */
	var BankSlot bslot := valueof(ts_BankSlot(1,2));
	var ComponentIdentity rspro_bank_id := valueof(ts_CompId(remsimBankd, testcasename()));
	f_rspro_init(rspro[1], mp_server_ip, mp_server_port, rspro_bank_id, 1);
	rspro[1].rspro_bank_id := bslot.bankId;
	rspro[1].rspro_bank_nslots := 8

	f_rsres_init();
	var JsSlotmap sm := valueof(ts_JsSlotmap(bslot, rspro[0].rspro_client_slot));
	var HTTPResponse res;

	/* 1) connect a client for that slotmap */
	f_rspro_connect_client(0);

	/* 2) Create a new slotmap via HTTP */
	res := f_rsres_post_slotmap(sm);

	/* 3) verify that the slotmap exists and is NEW */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, NEW);

	/* 4) connect a bankd for that slotmap */
	f_rspro_connect_client(1);

	/* 5) verify that the slotmap exists and is UNACKNOWLEDGED */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, UNACKNOWLEDGED);

	/* 6) expect bankd to receive that mapping */
	as_rspro_create_mapping(1, sm.client, sm.bank);

	/* 7) verify that the slotmap exists and is ACTIVE */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, ACTIVE);

	/* 8) expect the client to be configured with bankd IP/port */
	as_rspro_cfg_client_bank(0, bslot, ?);
}

/* simple delete of a 'NEW' slotmap */
testcase TC_slotmap_del_new() runs on test_CT {
	f_rsres_init();

	var JsSlotmap sm := valueof(ts_JsSlotmap(ts_BankSlot(1,2), ts_ClientSlot(3,4)));
	var HTTPResponse res := f_rsres_post_slotmap(sm);
	log(res);
	res := f_rsres_delete_slotmap(sm.bank);
	log(res);
}

/* simple delete of a non-existant slotmap */
testcase TC_slotmap_del_nonexistant() runs on test_CT {
	f_rsres_init();

	var JsSlotmap sm := valueof(ts_JsSlotmap(ts_BankSlot(11,12), ts_ClientSlot(13,14)));
	var HTTPResponse res := f_rsres_delete_slotmap(sm.bank, exp_sts:=404);
	log(res);
}


/* simple delete of a 'UNACKNOWLEDGED' slotmap */
testcase TC_slotmap_del_unack() runs on test_CT {
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimBankd, testcasename()));
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_bank_id := 1;
	rspro[0].rspro_bank_nslots := 8;

	f_rsres_init();
	var JsSlotmap sm := valueof(ts_JsSlotmap(ts_BankSlot(1,2), ts_ClientSlot(3,4)));
	var HTTPResponse res;

	/* Create a new slotmap via HTTP */
	res := f_rsres_post_slotmap(sm);

	/* verify that the slotmap exists and is NEW */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, NEW);

	/* connect a bankd for that slotmap */
	f_rspro_connect_client(0);

	/* expect the slotmap to be pushed to bank but don't ACK it */
	f_rspro_exp(tr_RSPRO_CreateMappingReq(sm.client, sm.bank));

	/* verify that the slotmap exists and is UNACKNOWLEDGED */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, UNACKNOWLEDGED);

	/* delete the slotmap via REST */
	res := f_rsres_delete_slotmap(sm.bank);

	/* verify the slotmap is gone */
	f_ensure_slotmaps({});
}

/* simple delete of a 'ACTIVE' slotmap from server + bankd */
testcase TC_slotmap_del_active() runs on test_CT {
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimBankd, testcasename()));
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_bank_id := 1;
	rspro[0].rspro_bank_nslots := 8;

	f_rsres_init();
	var JsSlotmap sm := valueof(ts_JsSlotmap(ts_BankSlot(1,2), ts_ClientSlot(3,4)));
	var HTTPResponse res;

	/* Create a new slotmap via HTTP */
	res := f_rsres_post_slotmap(sm);

	/* verify that the slotmap exists and is NEW */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, NEW);

	/* connect a bankd for that slotmap */
	f_rspro_connect_client(0);

	/* expect the slotmap to be pushed to bank and ACK it */
	as_rspro_create_mapping(0, sm.client, sm.bank);

	/* verify that the slotmap exists and is ACTIVE */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, ACTIVE);

	f_sleep(1.0);

	/* delete the slotmap via REST */
	res := f_rsres_delete_slotmap(sm.bank);

	/* verify the slotmap is gone from REST interface immediately */
	f_ensure_slotmaps({});

	/* verify the slotmap is removed from bankd */
	as_rspro_remove_mapping(0, sm.client, sm.bank);
}


/* simple delete of a 'ACTIVE' slotmap from client */
testcase TC_slotmap_del_active_client() runs on test_CT {
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimBankd, testcasename()));
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_bank_id := 1;
	rspro[0].rspro_bank_nslots := 8;

	rspro_id := valueof(ts_CompId(remsimClient, testcasename()));
	f_rspro_init(rspro[1], mp_server_ip, mp_server_port, rspro_id, 1);
	rspro[1].rspro_client_slot := valueof(ts_ClientSlot(3,4));

	f_rsres_init();
	var JsSlotmap sm := valueof(ts_JsSlotmap(ts_BankSlot(1,2), ts_ClientSlot(3,4)));
	var HTTPResponse res;

	/* Create a new slotmap via HTTP */
	res := f_rsres_post_slotmap(sm);

	/* verify that the slotmap exists and is NEW */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, NEW);

	/* connect a bankd for that slotmap */
	f_rspro_connect_client(0);

	/* connect a client for that slotmap */
	f_rspro_connect_client(1);

	/* expect the slotmap to be pushed to bank and ACK it */
	as_rspro_create_mapping(0, sm.client, sm.bank);

	/* verify that the slotmap exists and is ACTIVE */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, ACTIVE);

	/* expect the client to be configured with bankd side settings */
	as_rspro_cfg_client_bank(1, sm.bank, ?/*FIXME*/);

	f_sleep(1.0);

	/* delete the slotmap via REST */
	res := f_rsres_delete_slotmap(sm.bank);

	/* verify the slotmap is gone from REST interface immediately */
	f_ensure_slotmaps({});

	/* verify the slotmap is removed from bankd */
	as_rspro_remove_mapping(0, sm.client, sm.bank);

	/* verify the slotmap is removed from client by setting IP/port to '0' */
	as_rspro_cfg_client_bank(1, ?, tr_IpPort(ts_IPv4("0.0.0.0"), 0));
}


/* Add a slotmap to a currently active bank */
testcase TC_slotmap_add_active_bank() runs on test_CT {
	var ComponentIdentity rspro_id := valueof(ts_CompId(remsimBankd, testcasename()));
	f_rspro_init(rspro[0], mp_server_ip, mp_server_port, rspro_id, 0);
	rspro[0].rspro_bank_id := 1;
	rspro[0].rspro_bank_nslots := 8;

	f_rsres_init();
	var JsSlotmap sm := valueof(ts_JsSlotmap(ts_BankSlot(1,2), ts_ClientSlot(3,4)));
	var HTTPResponse res;

	/* connect a bankd for that slotmap */
	f_rspro_connect_client(0);

	/* Create a new slotmap via HTTP */
	res := f_rsres_post_slotmap(sm);

	/* expect the slotmap to be pushed to bank and ACK it */
	as_rspro_create_mapping(0, sm.client, sm.bank);

	/* verify that the slotmap exists and is ACTIVE */
	f_ensure_slotmap_exists_only(sm.client, sm.bank, ACTIVE);
}




/* TODO
 * - connect client w/slotmap; delete slotmap via REST (see if it is deleted)
 *   - don't acknowledge delete from client, disconnect client (see if slotmap is deleted)

 * - connect from unknown client (name not known, no clientId provisioned?
 * - add client name/ID mappings from REST API?
 */


control {
	execute( TC_connect_and_nothing() );
	execute( TC_connect_client() );
	execute( TC_connect_bank() );
	execute( TC_slotmap_add() );
	execute( TC_slotmap_add_conn_cl_b() );
	execute( TC_slotmap_add_out_of_range() );
	execute( TC_conn_cl_b_slotmap_add() );
	execute( TC_slotmap_del_new() );
	execute( TC_slotmap_del_nonexistant() );
	execute( TC_slotmap_del_unack() );
	execute( TC_slotmap_del_active() );
	execute( TC_slotmap_del_active_client() );
	execute( TC_slotmap_add_active_bank() );
}


}