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
|
/*
* SPDX-FileCopyrightText: 2017 Philippe Proulx <pproulx@efficios.com>
*
* SPDX-License-Identifier: GPL-2.0-only
*
*/
#include <common/string-utils/string-utils.hpp>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <tap/tap.h>
/* Number of TAP tests in this file */
#define NUM_TESTS 69
/* For error.h */
int lttng_opt_quiet = 1;
int lttng_opt_verbose;
int lttng_opt_mi;
static void test_one_split(const char *input, char delim, int escape_delim, ...)
{
va_list vl;
bool all_ok = true;
struct lttng_dynamic_pointer_array strings;
int split_ret;
size_t i, string_count;
split_ret = strutils_split(input, delim, escape_delim, &strings);
LTTNG_ASSERT(split_ret == 0);
va_start(vl, escape_delim);
string_count = lttng_dynamic_pointer_array_get_count(&strings);
for (i = 0; i < string_count; i++) {
const char *expected_substring = va_arg(vl, const char *);
const char *substring =
(const char *) lttng_dynamic_pointer_array_get_pointer(&strings, i);
diag(" got `%s`, expecting `%s`", substring, expected_substring);
if (!expected_substring) {
all_ok = false;
break;
}
if (strcmp(substring, expected_substring) != 0) {
all_ok = false;
break;
}
}
lttng_dynamic_pointer_array_reset(&strings);
va_end(vl);
ok(all_ok,
"strutils_split() produces the expected substrings: `%s` (delim. `%c`, escape `%d`)",
input,
delim,
escape_delim);
}
static void test_split()
{
test_one_split("a/b/c/d/e", '/', false, "a", "b", "c", "d", "e", NULL);
test_one_split("a/b//d/e", '/', false, "a", "b", "", "d", "e", NULL);
test_one_split("/b/c/d/e", '/', false, "", "b", "c", "d", "e", NULL);
test_one_split("a/b/c/d/", '/', false, "a", "b", "c", "d", "", NULL);
test_one_split("/b/c/d/", '/', false, "", "b", "c", "d", "", NULL);
test_one_split("", '/', false, "", NULL);
test_one_split("/", '/', false, "", "", NULL);
test_one_split("//", '/', false, "", "", "", NULL);
test_one_split("hello+world", '+', false, "hello", "world", NULL);
test_one_split("hello\\+world", '+', false, "hello\\", "world", NULL);
test_one_split("hello\\+world", '+', true, "hello+world", NULL);
test_one_split("hello\\++world", '+', true, "hello+", "world", NULL);
test_one_split("hello\\\\++world", '+', true, "hello\\\\", "", "world", NULL);
test_one_split("hello+world\\", '+', false, "hello", "world\\", NULL);
test_one_split("hello+world\\", '+', true, "hello", "world\\", NULL);
test_one_split("\\+", '+', false, "\\", "", NULL);
test_one_split("\\+", '+', true, "+", NULL);
}
static void test_one_is_star_at_the_end_only_glob_pattern(const char *pattern, bool expected)
{
ok(strutils_is_star_at_the_end_only_glob_pattern(pattern) == expected,
"strutils_is_star_at_the_end_only_glob_pattern() returns the expected result: `%s` -> %d",
pattern,
expected);
}
static void test_is_star_at_the_end_only_glob_pattern()
{
test_one_is_star_at_the_end_only_glob_pattern("allo*", true);
test_one_is_star_at_the_end_only_glob_pattern("allo\\\\*", true);
test_one_is_star_at_the_end_only_glob_pattern("allo", false);
test_one_is_star_at_the_end_only_glob_pattern("al*lo", false);
test_one_is_star_at_the_end_only_glob_pattern("al\\*lo", false);
test_one_is_star_at_the_end_only_glob_pattern("*allo", false);
test_one_is_star_at_the_end_only_glob_pattern("al*lo*", false);
test_one_is_star_at_the_end_only_glob_pattern("allo**", false);
test_one_is_star_at_the_end_only_glob_pattern("allo*\\*", false);
test_one_is_star_at_the_end_only_glob_pattern("allo\\*", false);
}
static void test_one_is_star_glob_pattern(const char *pattern, bool expected)
{
ok(strutils_is_star_glob_pattern(pattern) == expected,
"strutils_is_star_glob_pattern() returns the expected result: `%s` -> %d",
pattern,
expected);
}
static void test_is_star_glob_pattern()
{
test_one_is_star_glob_pattern("allo*", true);
test_one_is_star_glob_pattern("*allo", true);
test_one_is_star_glob_pattern("*allo*", true);
test_one_is_star_glob_pattern("*al*lo*", true);
test_one_is_star_glob_pattern("al\\**lo", true);
test_one_is_star_glob_pattern("al\\*l*o", true);
test_one_is_star_glob_pattern("all*o\\", true);
test_one_is_star_glob_pattern("*", true);
test_one_is_star_glob_pattern("\\\\*", true);
test_one_is_star_glob_pattern("allo", false);
test_one_is_star_glob_pattern("allo\\*", false);
test_one_is_star_glob_pattern("al\\*lo", false);
test_one_is_star_glob_pattern("\\*allo", false);
test_one_is_star_glob_pattern("\\*", false);
test_one_is_star_glob_pattern("allo\\", false);
}
static void test_one_normalize_star_glob_pattern(const char *pattern, const char *expected)
{
char *rw_pattern = strdup(pattern);
LTTNG_ASSERT(rw_pattern);
strutils_normalize_star_glob_pattern(rw_pattern);
ok(strcmp(rw_pattern, expected) == 0,
"strutils_normalize_star_glob_pattern() produces the expected result: `%s` -> `%s`",
pattern,
expected);
free(rw_pattern);
}
static void test_normalize_star_glob_pattern()
{
test_one_normalize_star_glob_pattern("salut", "salut");
test_one_normalize_star_glob_pattern("sal*ut", "sal*ut");
test_one_normalize_star_glob_pattern("sal**ut", "sal*ut");
test_one_normalize_star_glob_pattern("sal***ut", "sal*ut");
test_one_normalize_star_glob_pattern("*salut", "*salut");
test_one_normalize_star_glob_pattern("**salut", "*salut");
test_one_normalize_star_glob_pattern("***salut", "*salut");
test_one_normalize_star_glob_pattern("salut*", "salut*");
test_one_normalize_star_glob_pattern("salut**", "salut*");
test_one_normalize_star_glob_pattern("salut***", "salut*");
test_one_normalize_star_glob_pattern("sa\\*lut", "sa\\*lut");
test_one_normalize_star_glob_pattern("sa\\**lut", "sa\\**lut");
test_one_normalize_star_glob_pattern("sa*\\**lut", "sa*\\**lut");
test_one_normalize_star_glob_pattern("sa*\\***lut", "sa*\\**lut");
test_one_normalize_star_glob_pattern("\\*salu**t", "\\*salu*t");
test_one_normalize_star_glob_pattern("\\*salut**", "\\*salut*");
test_one_normalize_star_glob_pattern("\\*salut**\\*", "\\*salut*\\*");
test_one_normalize_star_glob_pattern("\\*salut", "\\*salut");
test_one_normalize_star_glob_pattern("\\***salut", "\\**salut");
test_one_normalize_star_glob_pattern("salut\\", "salut\\");
test_one_normalize_star_glob_pattern("salut\\**", "salut\\**");
test_one_normalize_star_glob_pattern("salut\\\\*", "salut\\\\*");
test_one_normalize_star_glob_pattern("salut\\\\***", "salut\\\\*");
test_one_normalize_star_glob_pattern("*", "*");
test_one_normalize_star_glob_pattern("**", "*");
test_one_normalize_star_glob_pattern("***", "*");
test_one_normalize_star_glob_pattern("**\\***", "*\\**");
}
int main()
{
plan_tests(NUM_TESTS);
diag("String utils unit tests");
test_normalize_star_glob_pattern();
test_is_star_glob_pattern();
test_is_star_at_the_end_only_glob_pattern();
test_split();
return exit_status();
}
|