hex_translate_to_regex.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. libmc - checks for hex pattern parsing
  3. Copyright (C) 2017-2024
  4. Free Software Foundation, Inc.
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software: you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation, either version 3 of the License,
  9. or (at your option) any later version.
  10. The Midnight Commander is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define TEST_SUITE_NAME "lib/search/hex"
  18. #include "tests/mctest.h"
  19. #include "hex.c" /* for testing static functions */
  20. /* --------------------------------------------------------------------------------------------- */
  21. /* @DataSource("test_hex_translate_to_regex_ds") */
  22. /* *INDENT-OFF* */
  23. static const struct test_hex_translate_to_regex_ds
  24. {
  25. const char *input_value;
  26. const char *expected_result;
  27. mc_search_hex_parse_error_t expected_error;
  28. } test_hex_translate_to_regex_ds[] =
  29. {
  30. {
  31. /* Simplest case */
  32. "12 34",
  33. "\\x12\\x34",
  34. MC_SEARCH_HEX_E_OK
  35. },
  36. {
  37. /* Prefixes (0x, 0X) */
  38. "0x12 0X34",
  39. "\\x12\\x34",
  40. MC_SEARCH_HEX_E_OK
  41. },
  42. {
  43. /* Prefix "0" doesn't signify octal! Numbers are always interpreted in hex. */
  44. "012",
  45. "\\x12",
  46. MC_SEARCH_HEX_E_OK
  47. },
  48. {
  49. /* Extra whitespace */
  50. " 12 34 ",
  51. "\\x12\\x34",
  52. MC_SEARCH_HEX_E_OK
  53. },
  54. {
  55. /* Min/max values */
  56. "0 ff",
  57. "\\x00\\xFF",
  58. MC_SEARCH_HEX_E_OK
  59. },
  60. {
  61. /* Error: Number out of range */
  62. "100",
  63. NULL,
  64. MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
  65. },
  66. {
  67. /* Error: Number out of range (negative) */
  68. "-1",
  69. NULL,
  70. MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
  71. },
  72. {
  73. /* Error: Invalid characters */
  74. "1 z 2",
  75. NULL,
  76. MC_SEARCH_HEX_E_INVALID_CHARACTER
  77. },
  78. /*
  79. * Quotes.
  80. */
  81. {
  82. " \"abc\" ",
  83. "abc",
  84. MC_SEARCH_HEX_E_OK
  85. },
  86. {
  87. /* Preserve upper/lower case */
  88. "\"aBc\"",
  89. "aBc",
  90. MC_SEARCH_HEX_E_OK
  91. },
  92. {
  93. " 12\"abc\"34 ",
  94. "\\x12abc\\x34",
  95. MC_SEARCH_HEX_E_OK
  96. },
  97. {
  98. "\"a\"\"b\"",
  99. "ab",
  100. MC_SEARCH_HEX_E_OK
  101. },
  102. /* Empty quotes */
  103. {
  104. "\"\"",
  105. "",
  106. MC_SEARCH_HEX_E_OK
  107. },
  108. {
  109. "12 \"\"",
  110. "\\x12",
  111. MC_SEARCH_HEX_E_OK
  112. },
  113. /* Error: Unmatched quotes */
  114. {
  115. "\"a",
  116. NULL,
  117. MC_SEARCH_HEX_E_UNMATCHED_QUOTES
  118. },
  119. {
  120. "\"",
  121. NULL,
  122. MC_SEARCH_HEX_E_UNMATCHED_QUOTES
  123. },
  124. /* Escaped quotes */
  125. {
  126. "\"a\\\"b\"",
  127. "a\"b",
  128. MC_SEARCH_HEX_E_OK
  129. },
  130. {
  131. "\"a\\\\b\"",
  132. "a\\b",
  133. MC_SEARCH_HEX_E_OK
  134. },
  135. };
  136. /* *INDENT-ON* */
  137. /* @Test(dataSource = "test_hex_translate_to_regex_ds") */
  138. /* *INDENT-OFF* */
  139. START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_regex_ds)
  140. /* *INDENT-ON* */
  141. {
  142. GString *tmp, *dest_str;
  143. mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
  144. /* given */
  145. tmp = g_string_new (data->input_value);
  146. /* when */
  147. dest_str = mc_search__hex_translate_to_regex (tmp, &error, NULL);
  148. g_string_free (tmp, TRUE);
  149. /* then */
  150. if (dest_str != NULL)
  151. {
  152. mctest_assert_str_eq (dest_str->str, data->expected_result);
  153. g_string_free (dest_str, TRUE);
  154. }
  155. else
  156. ck_assert_int_eq (error, data->expected_error);
  157. }
  158. /* *INDENT-OFF* */
  159. END_PARAMETRIZED_TEST
  160. /* *INDENT-ON* */
  161. /* --------------------------------------------------------------------------------------------- */
  162. int
  163. main (void)
  164. {
  165. TCase *tc_core;
  166. tc_core = tcase_create ("Core");
  167. /* Add new tests here: *************** */
  168. mctest_add_parameterized_test (tc_core, test_hex_translate_to_regex,
  169. test_hex_translate_to_regex_ds);
  170. /* *********************************** */
  171. return mctest_run_all (tc_core);
  172. }
  173. /* --------------------------------------------------------------------------------------------- */