hex_translate_to_regex.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. libmc - checks for hex pattern parsing
  3. Copyright (C) 2017-2025
  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 <https://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. static const struct test_hex_translate_to_regex_ds
  23. {
  24. const char *input_value;
  25. const char *expected_result;
  26. mc_search_hex_parse_error_t expected_error;
  27. } test_hex_translate_to_regex_ds[] = {
  28. {
  29. // Simplest case
  30. "12 34",
  31. "\\x12\\x34",
  32. MC_SEARCH_HEX_E_OK,
  33. },
  34. {
  35. // Prefixes (0x, 0X)
  36. "0x12 0X34",
  37. "\\x12\\x34",
  38. MC_SEARCH_HEX_E_OK,
  39. },
  40. {
  41. // Prefix "0" doesn't signify octal! Numbers are always interpreted in hex.
  42. "012",
  43. "\\x12",
  44. MC_SEARCH_HEX_E_OK,
  45. },
  46. {
  47. // Extra whitespace
  48. " 12 34 ",
  49. "\\x12\\x34",
  50. MC_SEARCH_HEX_E_OK,
  51. },
  52. {
  53. // Min/max values
  54. "0 ff",
  55. "\\x00\\xFF",
  56. MC_SEARCH_HEX_E_OK,
  57. },
  58. {
  59. // Error: Number out of range
  60. "100",
  61. NULL,
  62. MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
  63. },
  64. {
  65. // Error: Number out of range (negative)
  66. "-1",
  67. NULL,
  68. MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
  69. },
  70. {
  71. // Error: Invalid characters
  72. "1 z 2",
  73. NULL,
  74. MC_SEARCH_HEX_E_INVALID_CHARACTER,
  75. },
  76. /*
  77. * Quotes.
  78. */
  79. {
  80. " \"abc\" ",
  81. "abc",
  82. MC_SEARCH_HEX_E_OK,
  83. },
  84. {
  85. // Preserve upper/lower case
  86. "\"aBc\"",
  87. "aBc",
  88. MC_SEARCH_HEX_E_OK,
  89. },
  90. {
  91. " 12\"abc\"34 ",
  92. "\\x12abc\\x34",
  93. MC_SEARCH_HEX_E_OK,
  94. },
  95. {
  96. "\"a\"\"b\"",
  97. "ab",
  98. MC_SEARCH_HEX_E_OK,
  99. },
  100. // Empty quotes
  101. {
  102. "\"\"",
  103. "",
  104. MC_SEARCH_HEX_E_OK,
  105. },
  106. {
  107. "12 \"\"",
  108. "\\x12",
  109. MC_SEARCH_HEX_E_OK,
  110. },
  111. // Error: Unmatched quotes
  112. {
  113. "\"a",
  114. NULL,
  115. MC_SEARCH_HEX_E_UNMATCHED_QUOTES,
  116. },
  117. {
  118. "\"",
  119. NULL,
  120. MC_SEARCH_HEX_E_UNMATCHED_QUOTES,
  121. },
  122. // Escaped quotes
  123. {
  124. "\"a\\\"b\"",
  125. "a\"b",
  126. MC_SEARCH_HEX_E_OK,
  127. },
  128. {
  129. "\"a\\\\b\"",
  130. "a\\b",
  131. MC_SEARCH_HEX_E_OK,
  132. },
  133. };
  134. /* @Test(dataSource = "test_hex_translate_to_regex_ds") */
  135. START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_regex_ds)
  136. {
  137. GString *tmp, *dest_str;
  138. mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
  139. // given
  140. tmp = g_string_new (data->input_value);
  141. // when
  142. dest_str = mc_search__hex_translate_to_regex (tmp, &error, NULL);
  143. g_string_free (tmp, TRUE);
  144. // then
  145. if (dest_str != NULL)
  146. {
  147. mctest_assert_str_eq (dest_str->str, data->expected_result);
  148. g_string_free (dest_str, TRUE);
  149. }
  150. else
  151. ck_assert_int_eq (error, data->expected_error);
  152. }
  153. END_PARAMETRIZED_TEST
  154. /* --------------------------------------------------------------------------------------------- */
  155. int
  156. main (void)
  157. {
  158. TCase *tc_core;
  159. tc_core = tcase_create ("Core");
  160. // Add new tests here: ***************
  161. mctest_add_parameterized_test (tc_core, test_hex_translate_to_regex,
  162. test_hex_translate_to_regex_ds);
  163. // ***********************************
  164. return mctest_run_all (tc_core);
  165. }
  166. /* --------------------------------------------------------------------------------------------- */