Introduce emission site for syscall event rule
[lttng-tools.git] / tests / unit / test_action.c
1 /*
2 * test_action.c
3 *
4 * Unit tests for the notification API.
5 *
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
7 *
8 * SPDX-License-Identifier: MIT
9 *
10 */
11
12 #include <assert.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <tap/tap.h>
19
20 #include <common/payload-view.h>
21 #include <common/payload.h>
22 #include <lttng/action/action-internal.h>
23 #include <lttng/action/action.h>
24 #include <lttng/action/notify.h>
25 #include <lttng/action/rate-policy-internal.h>
26 #include <lttng/action/rate-policy.h>
27 #include <lttng/action/rotate-session.h>
28 #include <lttng/action/snapshot-session.h>
29 #include <lttng/action/start-session.h>
30 #include <lttng/action/stop-session.h>
31
32 /* For error.h */
33 int lttng_opt_quiet = 1;
34 int lttng_opt_verbose;
35 int lttng_opt_mi;
36
37 #define NUM_TESTS 60
38
39 static void test_action_notify(void)
40 {
41 int ret;
42 enum lttng_action_status status;
43 struct lttng_action *notify_action = NULL,
44 *notify_action_from_buffer = NULL;
45 struct lttng_rate_policy *policy = NULL, *default_policy;
46 struct lttng_payload payload;
47
48 lttng_payload_init(&payload);
49
50 /* To set. */
51 policy = lttng_rate_policy_every_n_create(100);
52 /* For comparison. */
53 default_policy = lttng_rate_policy_every_n_create(1);
54
55 assert(policy && default_policy);
56
57 notify_action = lttng_action_notify_create();
58 ok(notify_action, "Create notify action");
59 ok(lttng_action_get_type(notify_action) == LTTNG_ACTION_TYPE_NOTIFY,
60 "Action has type LTTNG_ACTION_TYPE_NOTIFY");
61
62 /* Validate the default policy for a notify action. */
63 {
64 const struct lttng_rate_policy *cur_policy = NULL;
65 status = lttng_action_notify_get_rate_policy(
66 notify_action, &cur_policy);
67 ok(status == LTTNG_ACTION_STATUS_OK &&
68 lttng_rate_policy_is_equal(
69 default_policy,
70 cur_policy),
71 "Default policy is every n=1");
72 }
73
74 /* Set a custom policy. */
75 status = lttng_action_notify_set_rate_policy(notify_action, policy);
76 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
77
78 /* Validate the custom policy for a notify action. */
79 {
80 const struct lttng_rate_policy *cur_policy = NULL;
81 status = lttng_action_notify_get_rate_policy(
82 notify_action, &cur_policy);
83 ok(status == LTTNG_ACTION_STATUS_OK &&
84 lttng_rate_policy_is_equal(
85 policy,
86 cur_policy),
87 "Notify action policy get");
88 }
89
90 ret = lttng_action_serialize(notify_action, &payload);
91 ok(ret == 0, "Action notify serialized");
92
93 {
94 struct lttng_payload_view view =
95 lttng_payload_view_from_payload(
96 &payload, 0, -1);
97 (void) lttng_action_create_from_payload(
98 &view, &notify_action_from_buffer);
99 }
100 ok(notify_action_from_buffer,
101 "Notify action created from payload is non-null");
102
103 ok(lttng_action_is_equal(notify_action, notify_action_from_buffer),
104 "Serialized and de-serialized notify action are equal");
105
106 lttng_rate_policy_destroy(default_policy);
107 lttng_rate_policy_destroy(policy);
108 lttng_action_destroy(notify_action);
109 lttng_action_destroy(notify_action_from_buffer);
110 lttng_payload_reset(&payload);
111 }
112
113 static void test_action_rotate_session(void)
114 {
115 int ret;
116 enum lttng_action_status status;
117 struct lttng_action *rotate_session_action = NULL,
118 *rotate_session_action_from_buffer = NULL;
119 struct lttng_rate_policy *policy = NULL, *default_policy;
120 struct lttng_payload payload;
121 const char *session_name = "my_session_name";
122 const char *get_session_name;
123
124 lttng_payload_init(&payload);
125
126 /* To set. */
127 policy = lttng_rate_policy_every_n_create(100);
128 /* For comparison. */
129 default_policy = lttng_rate_policy_every_n_create(1);
130
131 assert(policy && default_policy);
132
133 rotate_session_action = lttng_action_rotate_session_create();
134 ok(rotate_session_action, "Create rotate_session action");
135 ok(lttng_action_get_type(rotate_session_action) ==
136 LTTNG_ACTION_TYPE_ROTATE_SESSION,
137 "Action has type LTTNG_ACTION_TYPE_ROTATE_SESSION");
138
139 /* Session name setter. */
140 status = lttng_action_rotate_session_set_session_name(NULL, NULL);
141 ok(status == LTTNG_ACTION_STATUS_INVALID,
142 "Set session name (NULL,NULL) expect invalid");
143 status = lttng_action_rotate_session_set_session_name(
144 rotate_session_action, NULL);
145 ok(status == LTTNG_ACTION_STATUS_INVALID,
146 "Set session name (object,NULL) expect invalid");
147 status = lttng_action_rotate_session_set_session_name(
148 NULL, session_name);
149 ok(status == LTTNG_ACTION_STATUS_INVALID,
150 "Set session name (NULL,object) expect invalid");
151
152 /* Set the session name */
153 status = lttng_action_rotate_session_set_session_name(
154 rotate_session_action, session_name);
155 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
156
157 status = lttng_action_rotate_session_get_session_name(
158 rotate_session_action, &get_session_name);
159 ok(status == LTTNG_ACTION_STATUS_OK &&
160 !strcmp(session_name, get_session_name),
161 "Get session name, expected `%s` got `%s`",
162 session_name, get_session_name);
163
164 /* Validate the default policy for a rotate_session action. */
165 {
166 const struct lttng_rate_policy *cur_policy = NULL;
167 status = lttng_action_rotate_session_get_rate_policy(
168 rotate_session_action, &cur_policy);
169 ok(status == LTTNG_ACTION_STATUS_OK &&
170 lttng_rate_policy_is_equal(
171 default_policy,
172 cur_policy),
173 "Default policy is every n=1");
174 }
175
176 /* Set a custom policy. */
177 status = lttng_action_rotate_session_set_rate_policy(
178 rotate_session_action, policy);
179 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
180
181 /* Validate the custom policy for a rotate_session action. */
182 {
183 const struct lttng_rate_policy *cur_policy = NULL;
184 status = lttng_action_rotate_session_get_rate_policy(
185 rotate_session_action, &cur_policy);
186 ok(status == LTTNG_ACTION_STATUS_OK &&
187 lttng_rate_policy_is_equal(
188 policy,
189 cur_policy),
190 "rotate_session action policy get");
191 }
192
193 /* Ser/des tests. */
194 ret = lttng_action_serialize(rotate_session_action, &payload);
195 ok(ret == 0, "Action rotate_session serialized");
196
197 {
198 struct lttng_payload_view view =
199 lttng_payload_view_from_payload(
200 &payload, 0, -1);
201 (void) lttng_action_create_from_payload(
202 &view, &rotate_session_action_from_buffer);
203 }
204 ok(rotate_session_action_from_buffer,
205 "rotate_session action created from payload is non-null");
206
207 ok(lttng_action_is_equal(rotate_session_action,
208 rotate_session_action_from_buffer),
209 "Serialized and de-serialized rotate_session action are equal");
210
211 lttng_rate_policy_destroy(default_policy);
212 lttng_rate_policy_destroy(policy);
213 lttng_action_destroy(rotate_session_action);
214 lttng_action_destroy(rotate_session_action_from_buffer);
215 lttng_payload_reset(&payload);
216 }
217
218 static void test_action_start_session(void)
219 {
220 int ret;
221 enum lttng_action_status status;
222 struct lttng_action *start_session_action = NULL,
223 *start_session_action_from_buffer = NULL;
224 struct lttng_rate_policy *policy = NULL, *default_policy;
225 struct lttng_payload payload;
226 const char *session_name = "my_session_name";
227 const char *get_session_name;
228
229 lttng_payload_init(&payload);
230
231 /* To set. */
232 policy = lttng_rate_policy_every_n_create(100);
233 /* For comparison. */
234 default_policy = lttng_rate_policy_every_n_create(1);
235
236 assert(policy && default_policy);
237
238 start_session_action = lttng_action_start_session_create();
239 ok(start_session_action, "Create start_session action");
240 ok(lttng_action_get_type(start_session_action) ==
241 LTTNG_ACTION_TYPE_START_SESSION,
242 "Action has type LTTNG_ACTION_TYPE_START_SESSION");
243
244 /* Session name setter. */
245 status = lttng_action_start_session_set_session_name(NULL, NULL);
246 ok(status == LTTNG_ACTION_STATUS_INVALID,
247 "Set session name (NULL,NULL) expect invalid");
248 status = lttng_action_start_session_set_session_name(
249 start_session_action, NULL);
250 ok(status == LTTNG_ACTION_STATUS_INVALID,
251 "Set session name (object,NULL) expect invalid");
252 status = lttng_action_start_session_set_session_name(
253 NULL, session_name);
254 ok(status == LTTNG_ACTION_STATUS_INVALID,
255 "Set session name (NULL,object) expect invalid");
256
257 /* Set the session name */
258 status = lttng_action_start_session_set_session_name(
259 start_session_action, session_name);
260 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
261
262 status = lttng_action_start_session_get_session_name(
263 start_session_action, &get_session_name);
264 ok(status == LTTNG_ACTION_STATUS_OK &&
265 !strcmp(session_name, get_session_name),
266 "Get session name, expected `%s` got `%s`",
267 session_name, get_session_name);
268
269 /* Validate the default policy for a start_session action. */
270 {
271 const struct lttng_rate_policy *cur_policy = NULL;
272 status = lttng_action_start_session_get_rate_policy(
273 start_session_action, &cur_policy);
274 ok(status == LTTNG_ACTION_STATUS_OK &&
275 lttng_rate_policy_is_equal(
276 default_policy,
277 cur_policy),
278 "Default policy is every n=1");
279 }
280
281 /* Set a custom policy. */
282 status = lttng_action_start_session_set_rate_policy(
283 start_session_action, policy);
284 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
285
286 /* Validate the custom policy for a start_session action. */
287 {
288 const struct lttng_rate_policy *cur_policy = NULL;
289 status = lttng_action_start_session_get_rate_policy(
290 start_session_action, &cur_policy);
291 ok(status == LTTNG_ACTION_STATUS_OK &&
292 lttng_rate_policy_is_equal(
293 policy,
294 cur_policy),
295 "start_session action policy get");
296 }
297
298 /* Ser/des tests. */
299 ret = lttng_action_serialize(start_session_action, &payload);
300 ok(ret == 0, "Action start_session serialized");
301
302 {
303 struct lttng_payload_view view =
304 lttng_payload_view_from_payload(
305 &payload, 0, -1);
306 (void) lttng_action_create_from_payload(
307 &view, &start_session_action_from_buffer);
308 }
309 ok(start_session_action_from_buffer,
310 "start_session action created from payload is non-null");
311
312 ok(lttng_action_is_equal(start_session_action,
313 start_session_action_from_buffer),
314 "Serialized and de-serialized start_session action are equal");
315
316 lttng_rate_policy_destroy(default_policy);
317 lttng_rate_policy_destroy(policy);
318 lttng_action_destroy(start_session_action);
319 lttng_action_destroy(start_session_action_from_buffer);
320 lttng_payload_reset(&payload);
321 }
322
323 static void test_action_stop_session(void)
324 {
325 int ret;
326 enum lttng_action_status status;
327 struct lttng_action *stop_session_action = NULL,
328 *stop_session_action_from_buffer = NULL;
329 struct lttng_rate_policy *policy = NULL, *default_policy;
330 struct lttng_payload payload;
331 const char *session_name = "my_session_name";
332 const char *get_session_name;
333
334 lttng_payload_init(&payload);
335
336 /* To set. */
337 policy = lttng_rate_policy_every_n_create(100);
338 /* For comparison. */
339 default_policy = lttng_rate_policy_every_n_create(1);
340
341 assert(policy && default_policy);
342
343 stop_session_action = lttng_action_stop_session_create();
344 ok(stop_session_action, "Create stop_session action");
345 ok(lttng_action_get_type(stop_session_action) ==
346 LTTNG_ACTION_TYPE_STOP_SESSION,
347 "Action has type LTTNG_ACTION_TYPE_STOP_SESSION");
348
349 /* Session name setter. */
350 status = lttng_action_stop_session_set_session_name(NULL, NULL);
351 ok(status == LTTNG_ACTION_STATUS_INVALID,
352 "Set session name (NULL,NULL) expect invalid");
353 status = lttng_action_stop_session_set_session_name(
354 stop_session_action, NULL);
355 ok(status == LTTNG_ACTION_STATUS_INVALID,
356 "Set session name (object,NULL) expect invalid");
357 status = lttng_action_stop_session_set_session_name(NULL, session_name);
358 ok(status == LTTNG_ACTION_STATUS_INVALID,
359 "Set session name (NULL,object) expect invalid");
360
361 /* Set the session name */
362 status = lttng_action_stop_session_set_session_name(
363 stop_session_action, session_name);
364 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
365
366 status = lttng_action_stop_session_get_session_name(
367 stop_session_action, &get_session_name);
368 ok(status == LTTNG_ACTION_STATUS_OK &&
369 !strcmp(session_name, get_session_name),
370 "Get session name, expected `%s` got `%s`",
371 session_name, get_session_name);
372
373 /* Validate the default policy for a stop_session action. */
374 {
375 const struct lttng_rate_policy *cur_policy = NULL;
376 status = lttng_action_stop_session_get_rate_policy(
377 stop_session_action, &cur_policy);
378 ok(status == LTTNG_ACTION_STATUS_OK &&
379 lttng_rate_policy_is_equal(
380 default_policy,
381 cur_policy),
382 "Default policy is every n=1");
383 }
384
385 /* Set a custom policy. */
386 status = lttng_action_stop_session_set_rate_policy(
387 stop_session_action, policy);
388 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
389
390 /* Validate the custom policy for a stop_session action. */
391 {
392 const struct lttng_rate_policy *cur_policy = NULL;
393 status = lttng_action_stop_session_get_rate_policy(
394 stop_session_action, &cur_policy);
395 ok(status == LTTNG_ACTION_STATUS_OK &&
396 lttng_rate_policy_is_equal(
397 policy,
398 cur_policy),
399 "stop_session action policy get");
400 }
401
402 /* Ser/des tests. */
403 ret = lttng_action_serialize(stop_session_action, &payload);
404 ok(ret == 0, "Action stop_session serialized");
405
406 {
407 struct lttng_payload_view view =
408 lttng_payload_view_from_payload(
409 &payload, 0, -1);
410 (void) lttng_action_create_from_payload(
411 &view, &stop_session_action_from_buffer);
412 }
413 ok(stop_session_action_from_buffer,
414 "stop_session action created from payload is non-null");
415
416 ok(lttng_action_is_equal(stop_session_action,
417 stop_session_action_from_buffer),
418 "Serialized and de-serialized stop_session action are equal");
419
420 lttng_rate_policy_destroy(default_policy);
421 lttng_rate_policy_destroy(policy);
422 lttng_action_destroy(stop_session_action);
423 lttng_action_destroy(stop_session_action_from_buffer);
424 lttng_payload_reset(&payload);
425 }
426
427 static void test_action_snapshot_session(void)
428 {
429 int ret;
430 enum lttng_action_status status;
431 struct lttng_action *snapshot_session_action = NULL,
432 *snapshot_session_action_from_buffer = NULL;
433 struct lttng_rate_policy *policy = NULL, *default_policy;
434 struct lttng_payload payload;
435 const char *session_name = "my_session_name";
436 const char *get_session_name;
437
438 lttng_payload_init(&payload);
439
440 /* To set. */
441 policy = lttng_rate_policy_every_n_create(100);
442 /* For comparison. */
443 default_policy = lttng_rate_policy_every_n_create(1);
444
445 assert(policy && default_policy);
446
447 snapshot_session_action = lttng_action_snapshot_session_create();
448 ok(snapshot_session_action, "Create snapshot_session action");
449 ok(lttng_action_get_type(snapshot_session_action) ==
450 LTTNG_ACTION_TYPE_SNAPSHOT_SESSION,
451 "Action has type LTTNG_ACTION_TYPE_SNAPSHOT_SESSION");
452
453 /* Session name setter. */
454 status = lttng_action_snapshot_session_set_session_name(NULL, NULL);
455 ok(status == LTTNG_ACTION_STATUS_INVALID,
456 "Set session name (NULL,NULL) expect invalid");
457 status = lttng_action_snapshot_session_set_session_name(
458 snapshot_session_action, NULL);
459 ok(status == LTTNG_ACTION_STATUS_INVALID,
460 "Set session name (object,NULL) expect invalid");
461 status = lttng_action_snapshot_session_set_session_name(
462 NULL, session_name);
463 ok(status == LTTNG_ACTION_STATUS_INVALID,
464 "Set session name (NULL,object) expect invalid");
465
466 /* Set the session name */
467 status = lttng_action_snapshot_session_set_session_name(
468 snapshot_session_action, session_name);
469 ok(status == LTTNG_ACTION_STATUS_OK, "Set session name");
470
471 status = lttng_action_snapshot_session_get_session_name(
472 snapshot_session_action, &get_session_name);
473 ok(status == LTTNG_ACTION_STATUS_OK &&
474 !strcmp(session_name, get_session_name),
475 "Get session name, expected `%s` got `%s`",
476 session_name, get_session_name);
477
478 /* Validate the default policy for a snapshot_session action. */
479 {
480 const struct lttng_rate_policy *cur_policy = NULL;
481 status = lttng_action_snapshot_session_get_rate_policy(
482 snapshot_session_action, &cur_policy);
483 ok(status == LTTNG_ACTION_STATUS_OK &&
484 lttng_rate_policy_is_equal(
485 default_policy,
486 cur_policy),
487 "Default policy is every n=1");
488 }
489
490 /* Set a custom policy. */
491 status = lttng_action_snapshot_session_set_rate_policy(
492 snapshot_session_action, policy);
493 ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy");
494
495 /* Validate the custom policy for a snapshot_session action. */
496 {
497 const struct lttng_rate_policy *cur_policy = NULL;
498 status = lttng_action_snapshot_session_get_rate_policy(
499 snapshot_session_action, &cur_policy);
500 ok(status == LTTNG_ACTION_STATUS_OK &&
501 lttng_rate_policy_is_equal(
502 policy,
503 cur_policy),
504 "snapshot_session action policy get");
505 }
506
507 /* Ser/des tests. */
508 ret = lttng_action_serialize(snapshot_session_action, &payload);
509 ok(ret == 0, "Action snapshot_session serialized");
510
511 {
512 struct lttng_payload_view view =
513 lttng_payload_view_from_payload(
514 &payload, 0, -1);
515 (void) lttng_action_create_from_payload(
516 &view, &snapshot_session_action_from_buffer);
517 }
518 ok(snapshot_session_action_from_buffer,
519 "snapshot_session action created from payload is non-null");
520
521 ok(lttng_action_is_equal(snapshot_session_action,
522 snapshot_session_action_from_buffer),
523 "Serialized and de-serialized snapshot_session action are equal");
524
525 lttng_rate_policy_destroy(default_policy);
526 lttng_rate_policy_destroy(policy);
527 lttng_action_destroy(snapshot_session_action);
528 lttng_action_destroy(snapshot_session_action_from_buffer);
529 lttng_payload_reset(&payload);
530 }
531
532 int main(int argc, const char *argv[])
533 {
534 plan_tests(NUM_TESTS);
535 test_action_notify();
536 test_action_rotate_session();
537 test_action_start_session();
538 test_action_stop_session();
539 test_action_snapshot_session();
540 return exit_status();
541 }
This page took 0.040025 seconds and 4 git commands to generate.