utilunix__mc_pstream_get_string.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. lib - Read string from mc_pipe_stream
  3. Copyright (C) 2021-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2021
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/util"
  20. #include "tests/mctest.h"
  21. #include "lib/util.h"
  22. /* --------------------------------------------------------------------------------------------- */
  23. #define MAX_CHUNKS 8
  24. /* --------------------------------------------------------------------------------------------- */
  25. static mc_pipe_stream_t stream;
  26. static char etalon_long_file_list[BUF_1K];
  27. static size_t etalon_long_file_list_pos;
  28. /* --------------------------------------------------------------------------------------------- */
  29. /* @Before */
  30. static void
  31. setup (void)
  32. {
  33. }
  34. /* @After */
  35. static void
  36. teardown (void)
  37. {
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. /* @DataSource("data_source") */
  41. static const struct data_source
  42. {
  43. // input
  44. const char *buf; // string to read
  45. // output
  46. int pos[MAX_CHUNKS]; // ps.pos values
  47. const char *str[MAX_CHUNKS]; // chunks
  48. size_t len[MAX_CHUNKS]; // chunk lengths
  49. } data_source[] = {
  50. {
  51. // 0
  52. .buf = "",
  53. .pos = { 0 },
  54. .str = { "" },
  55. .len = { 0 },
  56. },
  57. {
  58. // 1
  59. .buf = "\n",
  60. .pos = { 0, 1 },
  61. .str = { "\n" },
  62. .len = { 1, 0 },
  63. },
  64. {
  65. // 2
  66. .buf = "\\\n",
  67. .pos = { 0, 2 },
  68. .str = { "\\\n" },
  69. .len = { 2, 0 },
  70. },
  71. {
  72. // 3
  73. .buf = "\\\\\n",
  74. .pos = { 0, 3 },
  75. .str = { "\\\\\n" },
  76. .len = { 3, 0 },
  77. },
  78. {
  79. // 4
  80. .buf = "\\\\\\\n",
  81. .pos = { 0, 4 },
  82. .str = { "\\\\\\\n" },
  83. .len = { 4, 0 },
  84. },
  85. {
  86. // 5
  87. .buf = "\\\\\\\\\n",
  88. .pos = { 0, 5 },
  89. .str = { "\\\\\\\\\n" },
  90. .len = { 5, 0 },
  91. },
  92. {
  93. // 6
  94. .buf = "12345",
  95. .pos = { 0, 5 },
  96. .str = { "12345" },
  97. .len = { 5, 0 },
  98. },
  99. {
  100. // 7
  101. .buf = "12345\n",
  102. .pos = { 0, 6 },
  103. .str = { "12345\n" },
  104. .len = { 6, 0 },
  105. },
  106. {
  107. // 8
  108. .buf = "12345\\\n",
  109. .pos = { 0, 7 },
  110. .str = { "12345\\\n" },
  111. .len = { 7, 0 },
  112. },
  113. {
  114. // 9
  115. .buf = "12345\\\\\n",
  116. .pos = { 0, 8 },
  117. .str = { "12345\\\\\n" },
  118. .len = { 8, 0 },
  119. },
  120. {
  121. // 10
  122. .buf = "12345\nabcd",
  123. .pos = { 0, 6, 10 },
  124. .str = { "12345\n", "abcd" },
  125. .len = { 6, 4, 0 },
  126. },
  127. {
  128. // 11
  129. .buf = "12345\\\nabcd",
  130. .pos = { 0, 11 },
  131. .str = { "12345\\\nabcd" },
  132. .len = { 11, 0 },
  133. },
  134. {
  135. // 12
  136. .buf = "12345\\\\\nabcd",
  137. .pos = { 0, 8, 12 },
  138. .str = { "12345\\\\\n", "abcd" },
  139. .len = { 8, 4, 0 },
  140. },
  141. {
  142. // 13
  143. .buf = "12345\\\\\\\nabcd",
  144. .pos = { 0, 13 },
  145. .str = { "12345\\\\\\\nabcd" },
  146. .len = { 13, 0 },
  147. },
  148. {
  149. // 14
  150. .buf = "12345\\\\\\\\\nabcd",
  151. .pos = { 0, 10, 14 },
  152. .str = { "12345\\\\\\\\\n", "abcd" },
  153. .len = { 10, 4, 0 },
  154. },
  155. {
  156. // 15
  157. .buf = "12345\nabcd\n",
  158. .pos = { 0, 6, 11 },
  159. .str = { "12345\n", "abcd\n" },
  160. .len = { 6, 5, 0 },
  161. },
  162. {
  163. // 16
  164. .buf = "12345\nabcd\n~!@#$%^",
  165. .pos = { 0, 6, 11, 18 },
  166. .str = { "12345\n", "abcd\n", "~!@#$%^" },
  167. .len = { 6, 5, 7, 0 },
  168. },
  169. {
  170. // 17
  171. .buf = "12345\nabcd\n~!@#$%^\n",
  172. .pos = { 0, 6, 11, 19 },
  173. .str = { "12345\n", "abcd\n", "~!@#$%^\n" },
  174. .len = { 6, 5, 8, 0 },
  175. },
  176. };
  177. /* @Test(dataSource = "data_source") */
  178. START_PARAMETRIZED_TEST (mc_pstream_get_string_test, data_source)
  179. {
  180. // given
  181. int j = 0;
  182. // when
  183. memset (&stream, 0, sizeof (stream));
  184. stream.len = strlen (data->buf);
  185. memmove (&stream.buf, data->buf, stream.len);
  186. // then
  187. do
  188. {
  189. GString *ret;
  190. ck_assert_int_eq (stream.pos, data->pos[j]);
  191. ret = mc_pstream_get_string (&stream);
  192. if (ret == NULL)
  193. break;
  194. ck_assert_int_eq (ret->len, data->len[j]);
  195. mctest_assert_str_eq (ret->str, data->str[j]);
  196. g_string_free (ret, TRUE);
  197. j++;
  198. }
  199. while (TRUE);
  200. }
  201. END_PARAMETRIZED_TEST
  202. /* --------------------------------------------------------------------------------------------- */
  203. static mc_pipe_t *
  204. test_mc_popen (void)
  205. {
  206. mc_pipe_t *p;
  207. p = g_try_new0 (mc_pipe_t, 1);
  208. // make less than sizeof (etalon_long_file_list)
  209. p->out.len = 128;
  210. etalon_long_file_list_pos = 0;
  211. return p;
  212. }
  213. static void
  214. test_mc_pread (mc_pipe_t *p)
  215. {
  216. size_t len;
  217. p->out.pos = 0;
  218. if (etalon_long_file_list_pos >= sizeof (etalon_long_file_list))
  219. {
  220. etalon_long_file_list_pos = sizeof (etalon_long_file_list);
  221. p->out.len = MC_PIPE_STREAM_EOF;
  222. return;
  223. }
  224. len = sizeof (etalon_long_file_list) - etalon_long_file_list_pos;
  225. len = MIN (len, (size_t) p->out.len);
  226. memmove (p->out.buf, etalon_long_file_list + etalon_long_file_list_pos, len);
  227. p->out.len = (ssize_t) len;
  228. etalon_long_file_list_pos += len;
  229. }
  230. START_TEST (mc_pstream_get_long_file_list_test)
  231. {
  232. // given
  233. GString *result_long_file_list = NULL;
  234. mc_pipe_t *pip;
  235. GString *remain_file_name = NULL;
  236. // when
  237. // fill the list
  238. memset (etalon_long_file_list, 'a', sizeof (etalon_long_file_list) - 1);
  239. // create an \n-separated list
  240. etalon_long_file_list[5] = '\n';
  241. etalon_long_file_list[25] = '\n';
  242. etalon_long_file_list[50] = '\n';
  243. etalon_long_file_list[75] = '\n';
  244. etalon_long_file_list[127] = '\n';
  245. etalon_long_file_list[200] = '\n';
  246. etalon_long_file_list[310] = '\n';
  247. etalon_long_file_list[325] = '\n';
  248. etalon_long_file_list[360] = '\n';
  249. etalon_long_file_list[512] = '\n';
  250. etalon_long_file_list[701] = '\n';
  251. etalon_long_file_list[725] = '\n';
  252. etalon_long_file_list[800] = '\n';
  253. etalon_long_file_list[sizeof (etalon_long_file_list) - 2] = '\n';
  254. etalon_long_file_list[sizeof (etalon_long_file_list) - 1] = '\0';
  255. // then
  256. // read file list
  257. pip = test_mc_popen ();
  258. while (TRUE)
  259. {
  260. GString *line;
  261. test_mc_pread (pip);
  262. if (pip->out.len == MC_PIPE_STREAM_EOF)
  263. break;
  264. while ((line = mc_pstream_get_string (&pip->out)) != NULL)
  265. {
  266. // handle an \n-separated file list
  267. if (line->str[line->len - 1] == '\n')
  268. {
  269. // entire file name or last chunk
  270. g_string_truncate (line, line->len - 1);
  271. // join filename chunks
  272. if (remain_file_name != NULL)
  273. {
  274. g_string_append_len (remain_file_name, line->str, line->len);
  275. g_string_free (line, TRUE);
  276. line = remain_file_name;
  277. remain_file_name = NULL;
  278. }
  279. }
  280. else
  281. {
  282. // first or middle chunk of file name
  283. if (remain_file_name == NULL)
  284. remain_file_name = line;
  285. else
  286. {
  287. g_string_append_len (remain_file_name, line->str, line->len);
  288. g_string_free (line, TRUE);
  289. }
  290. line = NULL;
  291. }
  292. // collect file names to assemble the result string
  293. if (line == NULL)
  294. continue;
  295. if (result_long_file_list == NULL)
  296. result_long_file_list = line;
  297. else
  298. {
  299. g_string_append_len (result_long_file_list, line->str, line->len);
  300. g_string_free (line, TRUE);
  301. }
  302. g_string_append_c (result_long_file_list, '\n');
  303. }
  304. }
  305. mctest_assert_str_eq (etalon_long_file_list, result_long_file_list->str);
  306. g_string_free (result_long_file_list, TRUE);
  307. }
  308. END_TEST
  309. /* --------------------------------------------------------------------------------------------- */
  310. int
  311. main (void)
  312. {
  313. TCase *tc_core;
  314. tc_core = tcase_create ("Core");
  315. tcase_add_checked_fixture (tc_core, setup, teardown);
  316. // Add new tests here: ***************
  317. mctest_add_parameterized_test (tc_core, mc_pstream_get_string_test, data_source);
  318. tcase_add_test (tc_core, mc_pstream_get_long_file_list_test);
  319. // ***********************************
  320. return mctest_run_all (tc_core);
  321. }
  322. /* --------------------------------------------------------------------------------------------- */