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
|
/*
* test_shl - Test shl library
*
* Copyright (c) 2022 Victor Westerhuis <victor@westerhu.is>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "shl_misc.h"
#include "test_common.h"
#define check_assert_string_list_eq(X, Y) \
do { \
unsigned int i; \
const char **x, **y; \
\
x = (X); \
y = (Y); \
\
for (i = 0; x[i] && y[i]; ++i) \
ck_assert_str_eq(x[i], y[i]); \
ck_assert_ptr_eq(x[i], NULL); \
ck_assert_ptr_eq(y[i], NULL); \
} while (0)
START_TEST(test_split_command_string)
{
int ret;
unsigned int i, n, n_list, n_expected;
char **list;
const char *invalid_command_strings[] = {
"\"", "'", "\\", "\"/bin/true", "'/bin/true", "/bin/true\\", "ls -h \"*.c'",
};
n = sizeof(invalid_command_strings) / sizeof(invalid_command_strings[0]);
for (i = 0; i < n; ++i) {
list = TEST_INVALID_PTR;
n_list = -10;
ret = shl_split_command_string(invalid_command_strings[i], &list, &n_list);
ck_assert_int_eq(ret, -EINVAL);
ck_assert_ptr_eq(list, TEST_INVALID_PTR);
ck_assert_uint_eq(n_list, (unsigned int)-10);
}
const char *expected_command_list[] = {"'/bin/command with space",
"\t\\argument=\"quoted\"",
"plain\3argument",
" an\tother='ere",
"\"ends with \\",
"\\\"more\\bquotes\\",
NULL};
n_expected = sizeof(expected_command_list) / sizeof(expected_command_list[0]) - 1;
const char *valid_command_strings[] = {
"\\'/bin/command\\ with\\ space \\\t\\\\argument=\\\"quoted\\\" plain\3argument \\ "
"an\\\tother=\\'ere \\\"ends\\ with\\ \\\\ \\\\\\\"more\\\\bquotes\\\\",
"\"'/bin/command with space\" \"\t\\argument=\\\"quoted\\\"\" \"plain\3argument\" "
"\" an\tother='ere\" \"\\\"ends with \\\\\" \"\\\\\\\"more\\bquotes\\\\\"",
"\"'\"'/bin/command with space' '\t\\argument=\"quoted\"' 'plain\3argument' ' "
"an\tother='\"'\"'ere' '\"ends with \\' '\\\"more\\bquotes\\'",
" \\'/bin/command\\ with\\ space\t\t\\\t\\\\argument=\\\"quoted\\\"\t "
"plain\3argument \t\\ an\\\tother=\\'ere \\\"ends\\ with\\ \\\\ \t "
"\\\\\\\"more\\\\\\bquotes\\\\ \t \t",
};
n = sizeof(valid_command_strings) / sizeof(valid_command_strings[0]);
for (i = 0; i < n; ++i) {
list = TEST_INVALID_PTR;
n_list = -10;
ret = shl_split_command_string(valid_command_strings[i], &list, &n_list);
ck_assert_int_eq(ret, 0);
ck_assert_ptr_ne(list, TEST_INVALID_PTR);
check_assert_string_list_eq((const char **)list, expected_command_list);
ck_assert_uint_eq(n_list, n_expected);
}
const char *empty_command_strings[] = {
"",
" ",
"\t\t \t",
};
n = sizeof(empty_command_strings) / sizeof(empty_command_strings[0]);
for (i = 0; i < n; ++i) {
list = TEST_INVALID_PTR;
n_list = -10;
ret = shl_split_command_string(empty_command_strings[i], &list, &n_list);
ck_assert_int_eq(ret, 0);
ck_assert_ptr_ne(list, TEST_INVALID_PTR);
ck_assert_ptr_eq(list[0], NULL);
ck_assert_uint_eq(n_list, 0);
}
{
list = TEST_INVALID_PTR;
n_list = -10;
ret = shl_split_command_string(valid_command_strings[0], &list, &n_list);
ck_assert_int_eq(ret, 0);
ck_assert_ptr_ne(list, TEST_INVALID_PTR);
check_assert_string_list_eq((const char **)list, expected_command_list);
ck_assert_uint_eq(n_list, n_expected);
}
{
list = TEST_INVALID_PTR;
ret = shl_split_command_string(valid_command_strings[0], &list, NULL);
ck_assert_int_eq(ret, 0);
ck_assert_ptr_ne(list, TEST_INVALID_PTR);
check_assert_string_list_eq((const char **)list, expected_command_list);
}
{
n_list = -10;
ret = shl_split_command_string(valid_command_strings[0], NULL, &n_list);
ck_assert_int_eq(ret, -EINVAL);
ck_assert_uint_eq(n_list, (unsigned int)-10);
}
{
list = TEST_INVALID_PTR;
n_list = -10;
ret = shl_split_command_string(NULL, &list, &n_list);
ck_assert_int_eq(ret, -EINVAL);
ck_assert_ptr_eq(list, TEST_INVALID_PTR);
ck_assert_uint_eq(n_list, (unsigned int)-10);
}
{
n_list = -10;
ret = shl_split_command_string(NULL, NULL, &n_list);
ck_assert_int_eq(ret, -EINVAL);
ck_assert_uint_eq(n_list, (unsigned int)-10);
}
}
END_TEST
TEST_DEFINE_CASE(misc)
TEST(test_split_command_string)
TEST_END_CASE
TEST_DEFINE(TEST_SUITE(shl, TEST_CASE(misc), TEST_END))
|