Clean-up: remove instances of loop initial declarations
[lttng-tools.git] / tests / unit / test_relayd_backward_compat_group_by_session.c
1 /*
2 * Copyright (C) 2019 Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <assert.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <tap/tap.h>
14
15 #include "backward-compatibility-group-by.h"
16
17 /* Number of TAP tests in this file */
18 #define NUM_TESTS_PER_TEST 1
19
20 struct test {
21 char *stream_path;
22 char *session_name;
23 char *hostname;
24 char *creation_time;
25 char *extra_path;
26 char *leftover;
27 bool is_valid;
28 };
29
30 int lttng_opt_quiet;
31 int lttng_opt_mi;
32 int lttng_opt_verbose;
33
34 struct test tests[] = {
35 /* Default name session streaming. */
36 {"hostname/auto-20190918-164429/ust/uid/1000/64-bit",
37 "auto-20190918-164429", "hostname",
38 "20190918-164429", "", "ust/uid/1000/64-bit",
39 true},
40 /* Custom default name session */
41 {"hostname/custom_auto-20190319-120000/ust/uid/1000/64-bit",
42 "custom_auto-20190319-120000", "hostname",
43 "20190319-120000", "", "ust/uid/1000/64-bit",
44 true},
45 /* Named session streaming */
46 {"hostname/test-20190918-164709/ust/uid/1000/64-bit", "test",
47 "hostname", "20190918-164709", "",
48 "ust/uid/1000/64-bit", true},
49 /* Default session snapshot streaming */
50 {"hostname//snapshot-1-20190918-164829-0/ust//uid/1000/64-bit",
51 "my_session", "hostname", "", "",
52 "snapshot-1-20190918-164829-0/ust//uid/1000/64-bit",
53 true},
54 /* Named session snapshot streaming */
55 {"hostname//snapshot-1-20190918-175919-0/ust//uid/1000/64-bit",
56 "my_session", "hostname", "", "",
57 "snapshot-1-20190918-175919-0/ust//uid/1000/64-bit",
58 true},
59 /* Default name session, live */
60 {"hostname//auto-20190918-171641/ust/uid/1000/64-bit",
61 "auto-20190918-171641", "hostname",
62 "20190918-171641", "", "ust/uid/1000/64-bit",
63 true},
64 /* Named session, live */
65 {"hostname//test-20190918-180333//ust/uid/1000/64-bit",
66 "test-20190918-180333", "hostname",
67 "20190918-180333", "", "/ust/uid/1000/64-bit",
68 true},
69 /* Default name session, streaming & live , extra path */
70 {"hostname/extra/path/ust/uid/1000/64-bit",
71 "auto-20190919-122110", "hostname",
72 "20190919-122110", "extra",
73 "path/ust/uid/1000/64-bit", true},
74 /* Named session, live, extra path */
75 {"hostname/extra/path/ust/uid/1000/64-bit", "test", "hostname",
76 "", "extra", "path/ust/uid/1000/64-bit", true},
77 /* Named session, snapshot, extra path */
78 {"hostname/extra/path/snapshot-1-20190919-140702-0/ust//uid/1000/64-bit",
79 "test", "hostname", "", "extra",
80 "path/snapshot-1-20190919-140702-0/ust//uid/1000/64-bit",
81 true},
82 /* Corner cases*/
83 /* Named session with valid datetime in it */
84 /* Default name session, extra path with session name in it*/
85 {"hostname/test-20190319-120000-20190918-180921/ust/uid/1000/64-bit",
86 "test-20190319-120000", "hostname",
87 "20190918-180921", "", "ust/uid/1000/64-bit",
88 true},
89 /* Empty path */
90 {"", "test", "", "", "", "", false},
91 /* Path without second token */
92 {"hostname", "test", "hostname", "", "", "", false},
93 /* No leftover */
94 {"hostname/test", "test", "hostname", "", "", "", true},
95 /* Path with ession name but no datetime */
96 {"hostname/test/ust/uid/1000/64-bit", "test", "hostname", "",
97 "", "ust/uid/1000/64-bit", true},
98 };
99
100 static char *craft_expected(struct test *test)
101 {
102 int ret;
103 char *result = NULL;
104
105 ret = asprintf(&result, "%s/%s%s%s/%s%s%s", test->session_name,
106 test->hostname,
107 test->creation_time[0] != '\0' ? "-" : "",
108 test->creation_time, test->extra_path,
109 test->extra_path[0] != '\0' ? "/" : "", test->leftover);
110 if (ret < 0) {
111 result = NULL;
112 }
113 return result;
114 }
115
116 int main(int argc, char **argv)
117 {
118 int i;
119 int num_test = sizeof(tests) / sizeof(struct test);
120
121 plan_tests(NUM_TESTS_PER_TEST * num_test);
122 diag("Backward compatibility utils for lttng-relayd --group-by-session");
123 for (i = 0; i < num_test; i++) {
124 char *expected = NULL;
125 char *result = NULL;
126
127 expected = craft_expected(&tests[i]);
128 if (!expected) {
129 fprintf(stderr, "Failed to craft expected output\n");
130 goto loop;
131 }
132
133 result = backward_compat_group_by_session(
134 tests[i].stream_path, tests[i].session_name);
135 if (!result && tests[i].is_valid) {
136 fprintf(stderr, "Failed to get result\n");
137 goto loop;
138 } else if (!result && tests[i].is_valid == false) {
139 pass("Returned null as expected");
140 goto loop;
141 }
142
143 ok(strncmp(expected, result, strlen(expected)) == 0,
144 "In: %s, out: %s, expected: %s",
145 tests[i].stream_path, result, expected);
146 loop:
147 free(expected);
148 free(result);
149 }
150 return exit_status();
151 }
This page took 0.048702 seconds and 4 git commands to generate.