Add unit tests for lttng_ust_strerror
[lttng-ust.git] / tests / unit / ust-error / ust-error.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * Copyright (C) 2021 Michael Jeanson <mjeanson@efficios.com>
5 */
6
7 #include <limits.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <lttng/ust-error.h>
14
15 #include "tap.h"
16
17 /*
18 * Sync with liblttng-ust-comm/lttng-ust-comm.c
19 */
20 static const char *ok_str = "Success";
21 static const char *unknown_str = "Unknown error";
22 static const char *noent_str = "No entry";
23 static const char *peercred_str = "Peer credentials PID is invalid. Socket appears to belong to a distinct, non-nested pid namespace.";
24
25 #define NUM_TESTS 12
26
27 static
28 void test_ust_error(void)
29 {
30 const char *error_str = NULL;
31
32 error_str = lttng_ust_strerror(LTTNG_UST_OK);
33 ok(strcmp(ok_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_OK returns '%s' (%s)", ok_str, error_str);
34
35 error_str = lttng_ust_strerror(-LTTNG_UST_OK);
36 ok(strcmp(ok_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_OK returns '%s' (%s)", ok_str, error_str);
37
38 error_str = lttng_ust_strerror(INT_MAX);
39 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive large int returns '%s' (%s)", unknown_str, error_str);
40
41 error_str = lttng_ust_strerror(INT_MIN);
42 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Negative large int returns '%s' (%s)", unknown_str, error_str);
43
44 error_str = lttng_ust_strerror(LTTNG_UST_ERR_NR);
45 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_NR returns '%s' (%s)", unknown_str, error_str);
46
47 error_str = lttng_ust_strerror(-LTTNG_UST_ERR_NR);
48 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_NR returns '%s' (%s)", unknown_str, error_str);
49
50 error_str = lttng_ust_strerror(LTTNG_UST_ERR_NR + 1);
51 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_NR + 1 returns '%s' (%s)", unknown_str, error_str);
52
53 error_str = lttng_ust_strerror(-LTTNG_UST_ERR_NR - 1);
54 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_NR - 1 returns '%s' (%s)", unknown_str, error_str);
55
56 error_str = lttng_ust_strerror(LTTNG_UST_ERR_NOENT);
57 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_NOENT returns '%s' (%s)", unknown_str, error_str);
58
59 error_str = lttng_ust_strerror(-LTTNG_UST_ERR_NOENT);
60 ok(strcmp(noent_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_NOENT returns '%s' (%s)", noent_str, error_str);
61
62 /* Last error code */
63 error_str = lttng_ust_strerror(LTTNG_UST_ERR_PEERCRED_PID);
64 ok(strcmp(unknown_str, error_str) == 0, "lttng_ust_strerror - Positive LTTNG_UST_ERR_PEERCRED_PID returns '%s' (%s)", unknown_str, error_str);
65
66 error_str = lttng_ust_strerror(-LTTNG_UST_ERR_PEERCRED_PID);
67 ok(strcmp(peercred_str, error_str) == 0, "lttng_ust_strerror - Negative LTTNG_UST_ERR_PEERCRED_PID returns '%s' (%s)", peercred_str, error_str);
68 }
69
70 int main(void)
71 {
72 plan_tests(NUM_TESTS);
73
74 test_ust_error();
75
76 return exit_status();
77 }
This page took 0.031042 seconds and 4 git commands to generate.