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
|
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/select.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/fsm.h>
#include <osmocom/ctrl/control_if.h>
enum {
DMAIN,
};
static void *g_ctx;
static int safe_strcmp(const char *a, const char *b)
{
if (!a || !b)
return a == b ? 0 : 1;
return strcmp(a, b);
}
enum test_fsm_states {
ST_NULL = 0,
ST_ONE,
ST_TWO,
};
enum test_fsm_evt {
EV_A,
EV_B,
};
static const struct value_string test_fsm_event_names[] = {
OSMO_VALUE_STRING(EV_A),
OSMO_VALUE_STRING(EV_B),
{ 0, NULL }
};
static void test_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
switch (event) {
case EV_A:
OSMO_ASSERT(data == (void *) 23);
osmo_fsm_inst_state_chg(fi, ST_ONE, 0, 0);
break;
default:
OSMO_ASSERT(0);
break;
}
}
static void test_fsm_one(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
switch (event) {
case EV_B:
OSMO_ASSERT(data == (void *) 42);
osmo_fsm_inst_state_chg(fi,ST_TWO, 1, 2342);
break;
default:
OSMO_ASSERT(0);
break;
}
}
static bool main_loop_run = true;
static int test_fsm_tmr_cb(struct osmo_fsm_inst *fi)
{
OSMO_ASSERT(fi->T == 2342);
OSMO_ASSERT(fi->state == ST_TWO);
LOGP(DMAIN, LOGL_INFO, "Timer\n");
main_loop_run = false;
return 0;
}
static struct osmo_fsm_state test_fsm_states[] = {
[ST_NULL] = {
.in_event_mask = (1 << EV_A),
.out_state_mask = (1 << ST_ONE),
.name = "NULL",
.action = test_fsm_null,
},
[ST_ONE]= {
.in_event_mask = (1 << EV_B),
.out_state_mask = (1 << ST_TWO),
.name = "ONE",
.action= test_fsm_one,
},
[ST_TWO]= {
.in_event_mask = 0,
.name = "TWO",
.action = NULL,
},
};
static struct osmo_fsm fsm = {
.name = "Test_FSM",
.states = test_fsm_states,
.num_states = ARRAY_SIZE(test_fsm_states),
.log_subsys = DMAIN,
.event_names = test_fsm_event_names,
};
static struct ctrl_handle *g_ctrl;
static struct ctrl_cmd *exec_ctrl_cmd(const char *cmdstr)
{
struct ctrl_cmd *cmd;
cmd = ctrl_cmd_exec_from_string(g_ctrl, cmdstr);
OSMO_ASSERT(cmd);
return cmd;
}
static void assert_cmd_reply(const char *cmdstr, const char *expres)
{
struct ctrl_cmd *cmd;
cmd = exec_ctrl_cmd(cmdstr);
if (safe_strcmp(cmd->reply, expres)) {
fprintf(stderr, "Reply '%s' doesn't match expected '%s'\n", cmd->reply, expres);
OSMO_ASSERT(0);
}
talloc_free(cmd);
}
static struct osmo_fsm_inst *foo(void)
{
struct osmo_fsm_inst *fi;
struct ctrl_cmd *cmd;
LOGP(DMAIN, LOGL_INFO, "Checking FSM allocation\n");
fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, "my_id");
OSMO_ASSERT(fi);
OSMO_ASSERT(fi->fsm == &fsm);
OSMO_ASSERT(!strncmp(osmo_fsm_inst_name(fi), fsm.name, strlen(fsm.name)));
OSMO_ASSERT(fi->state == ST_NULL);
OSMO_ASSERT(fi->log_level == LOGL_DEBUG);
assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "NULL");
assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.timer", "0,0,0");
/* Try invalid state transition */
osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42);
OSMO_ASSERT(fi->state == ST_NULL);
assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "NULL");
/* Legitimate state transition */
osmo_fsm_inst_dispatch(fi, EV_A, (void *) 23);
OSMO_ASSERT(fi->state == ST_ONE);
assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "ONE");
/* Legitimate transition with timer */
fsm.timer_cb = test_fsm_tmr_cb;
osmo_fsm_inst_dispatch(fi, EV_B, (void *) 42);
OSMO_ASSERT(fi->state == ST_TWO);
assert_cmd_reply("GET 1 fsm.Test_FSM.id.my_id.state", "TWO");
cmd = exec_ctrl_cmd("GET 2 fsm.Test_FSM.id.my_id.dump");
const char *exp = "'Test_FSM(my_id)','my_id','DEBUG','TWO',2342,timeout_sec=";
OSMO_ASSERT(!strncmp(cmd->reply, exp, strlen(exp)));
talloc_free(cmd);
return fi;
}
static void test_id_api()
{
struct osmo_fsm_inst *fi;
fprintf(stderr, "\n--- %s()\n", __func__);
/* Assert the instance has this name and can be looked up by it */
#define assert_name(expected_name) \
do { \
const char *name = osmo_fsm_inst_name(fi); \
fprintf(stderr, " osmo_fsm_inst_name() == %s\n", osmo_quote_str(name, -1)); \
if (safe_strcmp(name, expected_name)) { \
fprintf(stderr, " ERROR: expected %s\n", osmo_quote_str(expected_name, -1)); \
OSMO_ASSERT(false); \
} \
OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, expected_name) == fi); \
fprintf(stderr, " osmo_fsm_inst_find_by_name(%s) == fi\n", osmo_quote_str(expected_name, -1)); \
} while(0)
/* Assert the instance can be looked up by this id string */
#define assert_id(expected_id) \
do { \
OSMO_ASSERT(osmo_fsm_inst_find_by_id(&fsm, expected_id) == fi); \
fprintf(stderr, " osmo_fsm_inst_find_by_id(%s) == fi\n", osmo_quote_str(expected_id, -1)); \
} while(0)
/* Update the id, assert the proper rc, and expect a resulting fsm inst name + lookup */
#define test_id(new_id, expect_rc, expect_name_suffix) do { \
int rc; \
fprintf(stderr, "osmo_fsm_inst_update_id(%s)\n", osmo_quote_str(new_id, -1)); \
rc = osmo_fsm_inst_update_id(fi, new_id); \
fprintf(stderr, " rc == %d", rc); \
if (rc == (expect_rc)) \
fprintf(stderr, ", ok\n"); \
else { \
fprintf(stderr, ", ERROR: expected rc == %d\n", expect_rc); \
OSMO_ASSERT(rc == expect_rc); \
} \
assert_name("Test_FSM" expect_name_suffix); \
}while (0)
/* Successfully set a new id, along with name and id lookup assertions */
#define change_id(new_id) \
test_id(new_id, 0, "(" new_id ")"); \
assert_id(new_id)
/* allocate FSM instance without id, there should be a name without id */
fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
OSMO_ASSERT(fi);
assert_name("Test_FSM");
change_id("my_id");
change_id("another_id");
test_id(NULL, 0, "");
/* clear already cleared id */
test_id(NULL, 0, "");
change_id("arbitrary_id");
/* clear id by empty string doesn't work */
test_id("", -EINVAL, "(arbitrary_id)");
test_id("invalid.id", -EINVAL, "(arbitrary_id)");
fprintf(stderr, "--- id format tests...\n");
/* Update the id, assert the proper rc, and expect a resulting fsm inst name + lookup */
#define test_id_f(expect_rc, expect_name_suffix, new_id_fmt, args...) do { \
int rc; \
fprintf(stderr, "osmo_fsm_inst_update_id_f(%s, " #args ")\n", \
osmo_quote_str(new_id_fmt, -1)); \
rc = osmo_fsm_inst_update_id_f(fi, new_id_fmt, ## args); \
fprintf(stderr, " rc == %d", rc); \
if (rc == (expect_rc)) \
fprintf(stderr, ", ok\n"); \
else { \
fprintf(stderr, ", ERROR: expected rc == %d\n", expect_rc); \
OSMO_ASSERT(rc == expect_rc); \
} \
assert_name("Test_FSM" expect_name_suffix); \
}while (0)
test_id_f(-EINVAL, "(arbitrary_id)", "format%cid", '.');
test_id_f(-EINVAL, "(arbitrary_id)", "%s", "");
test_id_f(0, "(format23id42)", "format%xid%d", 0x23, 42);
test_id_f(0, "", NULL);
test_id_f(0, "", NULL);
test_id_f(0, "(arbitrary_id)", "%s%c%s", "arbitrary", '_', "id");
fprintf(stderr, "\n--- %s() done\n\n", __func__);
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
}
const struct timeval fake_time_start_time = { 123, 456 };
#define fake_time_passes(secs, usecs) do \
{ \
struct timeval diff; \
osmo_gettimeofday_override_add(secs, usecs); \
osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \
timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \
fprintf(stderr, "Total time passed: %d.%06d s\n", \
(int)diff.tv_sec, (int)diff.tv_usec); \
osmo_timers_prepare(); \
osmo_timers_update(); \
} while (0)
void fake_time_start()
{
struct timespec *clock_override;
osmo_gettimeofday_override_time = fake_time_start_time;
osmo_gettimeofday_override = true;
clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
OSMO_ASSERT(clock_override);
clock_override->tv_sec = fake_time_start_time.tv_sec;
clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
osmo_clock_override_enable(CLOCK_MONOTONIC, true);
fake_time_passes(0, 0);
}
static int timeout_fired = 0;
static int timer_cb(struct osmo_fsm_inst *fi)
{
timeout_fired = fi->T;
return 0;
}
static void test_state_chg_keep_timer()
{
struct osmo_fsm_inst *fi;
fprintf(stderr, "\n--- %s()\n", __func__);
fsm.timer_cb = timer_cb;
/* Test that no timer remains no timer */
fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
OSMO_ASSERT(fi);
osmo_fsm_inst_state_chg(fi, ST_ONE, 0, 0);
timeout_fired = -1;
osmo_fsm_inst_state_chg_keep_timer(fi, ST_TWO);
OSMO_ASSERT(timeout_fired == -1);
OSMO_ASSERT(fi->T == 0);
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
/* Test that a set time continues with exact precision */
fake_time_start();
fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
OSMO_ASSERT(fi);
osmo_fsm_inst_state_chg(fi, ST_ONE, 10, 10);
timeout_fired = -1;
fake_time_passes(2, 342);
osmo_fsm_inst_state_chg_keep_timer(fi, ST_TWO);
fake_time_passes(0, 0);
OSMO_ASSERT(timeout_fired == -1);
fake_time_passes(7, 1000000 - 342 - 1);
OSMO_ASSERT(timeout_fired == -1);
fake_time_passes(0, 1);
OSMO_ASSERT(timeout_fired == 10);
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
fprintf(stderr, "--- %s() done\n", __func__);
}
static void test_state_chg_T()
{
struct osmo_fsm_inst *fi;
fprintf(stderr, "\n--- %s()\n", __func__);
fsm.timer_cb = NULL;
/* Test setting to timeout_secs = 0, T = 0 */
fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
OSMO_ASSERT(fi);
osmo_fsm_inst_state_chg(fi, ST_ONE, 23, 42);
printf("T = %d\n", fi->T);
OSMO_ASSERT(fi->T == 42);
osmo_fsm_inst_state_chg(fi, ST_TWO, 0, 0);
printf("T = %d\n", fi->T);
OSMO_ASSERT(fi->T == 0);
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
/* Test setting to timeout_secs = 0, T != 0 */
fi = osmo_fsm_inst_alloc(&fsm, g_ctx, NULL, LOGL_DEBUG, NULL);
OSMO_ASSERT(fi);
osmo_fsm_inst_state_chg(fi, ST_ONE, 23, 42);
printf("T = %d\n", fi->T);
OSMO_ASSERT(fi->T == 42);
osmo_fsm_inst_state_chg(fi, ST_TWO, 0, 11);
printf("T = %d\n", fi->T);
OSMO_ASSERT(fi->T == 11);
osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REQUEST, NULL);
fprintf(stderr, "--- %s() done\n", __func__);
}
static const struct log_info_cat default_categories[] = {
[DMAIN] = {
.name = "DMAIN",
.description = "Main",
.enabled = 1, .loglevel = LOGL_DEBUG,
},
};
static const struct log_info log_info = {
.cat = default_categories,
.num_cat = ARRAY_SIZE(default_categories),
};
int main(int argc, char **argv)
{
struct log_target *stderr_target;
struct osmo_fsm_inst *finst;
osmo_fsm_log_addr(false);
/* Using fake time to get deterministic timeout logging */
osmo_fsm_log_timeouts(true);
log_init(&log_info, NULL);
stderr_target = log_target_create_stderr();
log_add_target(stderr_target);
log_set_print_filename(stderr_target, 0);
g_ctrl = ctrl_handle_alloc(NULL, NULL, NULL);
g_ctx = NULL;
OSMO_ASSERT(osmo_fsm_find_by_name(fsm.name) == NULL);
OSMO_ASSERT(osmo_fsm_register(&fsm) == 0);
OSMO_ASSERT(osmo_fsm_find_by_name(fsm.name) == &fsm);
OSMO_ASSERT(osmo_fsm_inst_find_by_name(&fsm, "my_id") == NULL);
finst = foo();
while (main_loop_run) {
osmo_select_main(0);
}
osmo_fsm_inst_free(finst);
test_id_api();
test_state_chg_keep_timer();
test_state_chg_T();
osmo_fsm_unregister(&fsm);
exit(0);
}
|