| 1 | /* |
| 2 | * Copyright (c) - 2019 Jonathan Rajotte-Julien |
| 3 | * <jonathan.rajotte-julien@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by as |
| 7 | * published by the Free Software Foundation; only version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <tap/tap.h> |
| 25 | |
| 26 | #include "backward-compatibility-group-by.h" |
| 27 | |
| 28 | /* Number of TAP tests in this file */ |
| 29 | #define NUM_TESTS_PER_TEST 1 |
| 30 | |
| 31 | struct test { |
| 32 | char *stream_path; |
| 33 | char *session_name; |
| 34 | char *hostname; |
| 35 | char *creation_time; |
| 36 | char *extra_path; |
| 37 | char *leftover; |
| 38 | bool is_valid; |
| 39 | }; |
| 40 | |
| 41 | int lttng_opt_quiet; |
| 42 | int lttng_opt_mi; |
| 43 | int lttng_opt_verbose; |
| 44 | |
| 45 | struct test tests[] = { |
| 46 | /* Default name session streaming. */ |
| 47 | {"hostname/auto-20190918-164429/ust/uid/1000/64-bit", |
| 48 | "auto-20190918-164429", "hostname", |
| 49 | "20190918-164429", "", "ust/uid/1000/64-bit", |
| 50 | true}, |
| 51 | /* Custom default name session */ |
| 52 | {"hostname/custom_auto-20190319-120000/ust/uid/1000/64-bit", |
| 53 | "custom_auto-20190319-120000", "hostname", |
| 54 | "20190319-120000", "", "ust/uid/1000/64-bit", |
| 55 | true}, |
| 56 | /* Named session streaming */ |
| 57 | {"hostname/test-20190918-164709/ust/uid/1000/64-bit", "test", |
| 58 | "hostname", "20190918-164709", "", |
| 59 | "ust/uid/1000/64-bit", true}, |
| 60 | /* Default session snapshot streaming */ |
| 61 | {"hostname//snapshot-1-20190918-164829-0/ust//uid/1000/64-bit", |
| 62 | "my_session", "hostname", "", "", |
| 63 | "snapshot-1-20190918-164829-0/ust//uid/1000/64-bit", |
| 64 | true}, |
| 65 | /* Named session snapshot streaming */ |
| 66 | {"hostname//snapshot-1-20190918-175919-0/ust//uid/1000/64-bit", |
| 67 | "my_session", "hostname", "", "", |
| 68 | "snapshot-1-20190918-175919-0/ust//uid/1000/64-bit", |
| 69 | true}, |
| 70 | /* Default name session, live */ |
| 71 | {"hostname//auto-20190918-171641/ust/uid/1000/64-bit", |
| 72 | "auto-20190918-171641", "hostname", |
| 73 | "20190918-171641", "", "ust/uid/1000/64-bit", |
| 74 | true}, |
| 75 | /* Named session, live */ |
| 76 | {"hostname//test-20190918-180333//ust/uid/1000/64-bit", |
| 77 | "test-20190918-180333", "hostname", |
| 78 | "20190918-180333", "", "/ust/uid/1000/64-bit", |
| 79 | true}, |
| 80 | /* Default name session, streaming & live , extra path */ |
| 81 | {"hostname/extra/path/ust/uid/1000/64-bit", |
| 82 | "auto-20190919-122110", "hostname", |
| 83 | "20190919-122110", "extra", |
| 84 | "path/ust/uid/1000/64-bit", true}, |
| 85 | /* Named session, live, extra path */ |
| 86 | {"hostname/extra/path/ust/uid/1000/64-bit", "test", "hostname", |
| 87 | "", "extra", "path/ust/uid/1000/64-bit", true}, |
| 88 | /* Named session, snapshot, extra path */ |
| 89 | {"hostname/extra/path/snapshot-1-20190919-140702-0/ust//uid/1000/64-bit", |
| 90 | "test", "hostname", "", "extra", |
| 91 | "path/snapshot-1-20190919-140702-0/ust//uid/1000/64-bit", |
| 92 | true}, |
| 93 | /* Corner cases*/ |
| 94 | /* Named session with valid datetime in it */ |
| 95 | /* Default name session, extra path with session name in it*/ |
| 96 | {"hostname/test-20190319-120000-20190918-180921/ust/uid/1000/64-bit", |
| 97 | "test-20190319-120000", "hostname", |
| 98 | "20190918-180921", "", "ust/uid/1000/64-bit", |
| 99 | true}, |
| 100 | /* Empty path */ |
| 101 | {"", "test", "", "", "", "", false}, |
| 102 | /* Path without second token */ |
| 103 | {"hostname", "test", "hostname", "", "", "", false}, |
| 104 | /* No leftover */ |
| 105 | {"hostname/test", "test", "hostname", "", "", "", true}, |
| 106 | /* Path with ession name but no datetime */ |
| 107 | {"hostname/test/ust/uid/1000/64-bit", "test", "hostname", "", |
| 108 | "", "ust/uid/1000/64-bit", true}, |
| 109 | }; |
| 110 | |
| 111 | static char *craft_expected(struct test *test) |
| 112 | { |
| 113 | int ret; |
| 114 | char *result = NULL; |
| 115 | |
| 116 | ret = asprintf(&result, "%s/%s%s%s/%s%s%s", test->session_name, |
| 117 | test->hostname, |
| 118 | test->creation_time[0] != '\0' ? "-" : "", |
| 119 | test->creation_time, test->extra_path, |
| 120 | test->extra_path[0] != '\0' ? "/" : "", test->leftover); |
| 121 | if (ret < 0) { |
| 122 | result = NULL; |
| 123 | } |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | int main(int argc, char **argv) |
| 128 | { |
| 129 | int num_test = sizeof(tests) / sizeof(struct test); |
| 130 | |
| 131 | plan_tests(NUM_TESTS_PER_TEST * num_test); |
| 132 | diag("Backward compatibility utils for lttng-relayd --group-by-session"); |
| 133 | for (int i = 0; i < num_test; i++) { |
| 134 | char *expected = NULL; |
| 135 | char *result = NULL; |
| 136 | |
| 137 | expected = craft_expected(&tests[i]); |
| 138 | if (!expected) { |
| 139 | fprintf(stderr, "Failed to craft expected output\n"); |
| 140 | goto loop; |
| 141 | } |
| 142 | |
| 143 | result = backward_compat_group_by_session( |
| 144 | tests[i].stream_path, tests[i].session_name); |
| 145 | if (!result && tests[i].is_valid) { |
| 146 | fprintf(stderr, "Failed to get result\n"); |
| 147 | goto loop; |
| 148 | } else if (!result && tests[i].is_valid == false) { |
| 149 | pass("Returned null as expected"); |
| 150 | goto loop; |
| 151 | } |
| 152 | |
| 153 | ok(strncmp(expected, result, strlen(expected)) == 0, |
| 154 | "In: %s, out: %s, expected: %s", |
| 155 | tests[i].stream_path, result, expected); |
| 156 | loop: |
| 157 | free(expected); |
| 158 | free(result); |
| 159 | } |
| 160 | return exit_status(); |
| 161 | } |