utilunix__mc_pstream_get_string.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. lib - Read string from mc_pipe_stream
  3. Copyright (C) 2021-2024
  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 <http://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. /* *INDENT-OFF* */
  42. static const struct data_source
  43. {
  44. /* input */
  45. const char *buf; /* string to read */
  46. /* output */
  47. int pos[MAX_CHUNKS]; /* ps.pos values */
  48. const char *str[MAX_CHUNKS]; /* chunks */
  49. size_t len[MAX_CHUNKS]; /* chunk lengths */
  50. }
  51. data_source[] =
  52. {
  53. /* 0 */
  54. {
  55. .buf = "",
  56. .pos = { 0 },
  57. .str = { "" },
  58. .len = { 0 }
  59. },
  60. /* 1 */
  61. {
  62. .buf = "\n",
  63. .pos = { 0, 1 },
  64. .str = { "\n" },
  65. .len = { 1, 0 }
  66. },
  67. /* 2 */
  68. {
  69. .buf = "\\\n",
  70. .pos = { 0, 2 },
  71. .str = { "\\\n" },
  72. .len = { 2, 0 }
  73. },
  74. /* 3 */
  75. {
  76. .buf = "\\\\\n",
  77. .pos = { 0, 3 },
  78. .str = { "\\\\\n" },
  79. .len = { 3, 0 }
  80. },
  81. /* 4 */
  82. {
  83. .buf = "\\\\\\\n",
  84. .pos = { 0, 4 },
  85. .str = { "\\\\\\\n" },
  86. .len = { 4, 0 }
  87. },
  88. /* 5 */
  89. {
  90. .buf = "\\\\\\\\\n",
  91. .pos = { 0, 5 },
  92. .str = { "\\\\\\\\\n" },
  93. .len = { 5, 0 }
  94. },
  95. /* 6 */
  96. {
  97. .buf = "12345",
  98. .pos = { 0, 5 },
  99. .str = { "12345" },
  100. .len = { 5, 0 }
  101. },
  102. /* 7 */
  103. {
  104. .buf = "12345\n",
  105. .pos = { 0, 6 },
  106. .str = { "12345\n" },
  107. .len = { 6, 0 }
  108. },
  109. /* 8 */
  110. {
  111. .buf = "12345\\\n",
  112. .pos = { 0, 7 },
  113. .str = { "12345\\\n" },
  114. .len = { 7, 0 }
  115. },
  116. /* 9 */
  117. {
  118. .buf = "12345\\\\\n",
  119. .pos = { 0, 8 },
  120. .str = { "12345\\\\\n" },
  121. .len = { 8, 0 }
  122. },
  123. /* 10 */
  124. {
  125. .buf = "12345\nabcd",
  126. .pos = { 0, 6, 10 },
  127. .str = { "12345\n", "abcd" },
  128. .len = { 6, 4, 0 }
  129. },
  130. /* 11 */
  131. {
  132. .buf = "12345\\\nabcd",
  133. .pos = { 0, 11 },
  134. .str = { "12345\\\nabcd" },
  135. .len = { 11, 0 }
  136. },
  137. /* 12 */
  138. {
  139. .buf = "12345\\\\\nabcd",
  140. .pos = { 0, 8, 12 },
  141. .str = { "12345\\\\\n", "abcd" },
  142. .len = { 8, 4, 0 }
  143. },
  144. /* 13 */
  145. {
  146. .buf = "12345\\\\\\\nabcd",
  147. .pos = { 0, 13 },
  148. .str = { "12345\\\\\\\nabcd" },
  149. .len = { 13, 0 }
  150. },
  151. /* 14 */
  152. {
  153. .buf = "12345\\\\\\\\\nabcd",
  154. .pos = { 0, 10, 14 },
  155. .str = { "12345\\\\\\\\\n", "abcd" },
  156. .len = { 10, 4, 0 }
  157. },
  158. /* 15 */
  159. {
  160. .buf = "12345\nabcd\n",
  161. .pos = { 0, 6, 11 },
  162. .str = { "12345\n", "abcd\n" },
  163. .len = { 6, 5, 0 }
  164. },
  165. /* 16 */
  166. {
  167. .buf = "12345\nabcd\n~!@#$%^",
  168. .pos = { 0, 6, 11, 18 },
  169. .str = { "12345\n", "abcd\n", "~!@#$%^" },
  170. .len = { 6, 5, 7, 0 }
  171. },
  172. /* 17 */
  173. {
  174. .buf = "12345\nabcd\n~!@#$%^\n",
  175. .pos = { 0, 6, 11, 19 },
  176. .str = { "12345\n", "abcd\n", "~!@#$%^\n" },
  177. .len = { 6, 5, 8, 0 }
  178. }
  179. };
  180. /* *INDENT-ON* */
  181. /* @Test(dataSource = "data_source") */
  182. /* *INDENT-OFF* */
  183. START_PARAMETRIZED_TEST (mc_pstream_get_string_test, data_source)
  184. /* *INDENT-ON* */
  185. {
  186. /* given */
  187. int j = 0;
  188. /* when */
  189. memset (&stream, 0, sizeof (stream));
  190. stream.len = strlen (data->buf);
  191. memmove (&stream.buf, data->buf, stream.len);
  192. /* then */
  193. do
  194. {
  195. GString *ret;
  196. ck_assert_int_eq (stream.pos, data->pos[j]);
  197. ret = mc_pstream_get_string (&stream);
  198. if (ret == NULL)
  199. break;
  200. ck_assert_int_eq (ret->len, data->len[j]);
  201. mctest_assert_str_eq (ret->str, data->str[j]);
  202. g_string_free (ret, TRUE);
  203. j++;
  204. }
  205. while (TRUE);
  206. }
  207. /* *INDENT-OFF* */
  208. END_PARAMETRIZED_TEST
  209. /* *INDENT-ON* */
  210. /* --------------------------------------------------------------------------------------------- */
  211. static mc_pipe_t *
  212. test_mc_popen (void)
  213. {
  214. mc_pipe_t *p;
  215. p = g_try_new0 (mc_pipe_t, 1);
  216. /* make less than sizeof (etalon_long_file_list) */
  217. p->out.len = 128;
  218. etalon_long_file_list_pos = 0;
  219. return p;
  220. }
  221. static void
  222. test_mc_pread (mc_pipe_t *p)
  223. {
  224. size_t len;
  225. p->out.pos = 0;
  226. if (etalon_long_file_list_pos >= sizeof (etalon_long_file_list))
  227. {
  228. etalon_long_file_list_pos = sizeof (etalon_long_file_list);
  229. p->out.len = MC_PIPE_STREAM_EOF;
  230. return;
  231. }
  232. len = sizeof (etalon_long_file_list) - etalon_long_file_list_pos;
  233. len = MIN (len, (size_t) p->out.len);
  234. memmove (p->out.buf, etalon_long_file_list + etalon_long_file_list_pos, len);
  235. p->out.len = (ssize_t) len;
  236. etalon_long_file_list_pos += len;
  237. }
  238. /* *INDENT-OFF* */
  239. START_TEST (mc_pstream_get_long_file_list_test)
  240. /* *INDENT-ON* */
  241. {
  242. /* given */
  243. GString *result_long_file_list = NULL;
  244. mc_pipe_t *pip;
  245. GString *remain_file_name = NULL;
  246. /* when */
  247. /* fill the list */
  248. memset (etalon_long_file_list, 'a', sizeof (etalon_long_file_list) - 1);
  249. /* create an \n-separated list */
  250. etalon_long_file_list[5] = '\n';
  251. etalon_long_file_list[25] = '\n';
  252. etalon_long_file_list[50] = '\n';
  253. etalon_long_file_list[75] = '\n';
  254. etalon_long_file_list[127] = '\n';
  255. etalon_long_file_list[200] = '\n';
  256. etalon_long_file_list[310] = '\n';
  257. etalon_long_file_list[325] = '\n';
  258. etalon_long_file_list[360] = '\n';
  259. etalon_long_file_list[512] = '\n';
  260. etalon_long_file_list[701] = '\n';
  261. etalon_long_file_list[725] = '\n';
  262. etalon_long_file_list[800] = '\n';
  263. etalon_long_file_list[sizeof (etalon_long_file_list) - 2] = '\n';
  264. etalon_long_file_list[sizeof (etalon_long_file_list) - 1] = '\0';
  265. /* then */
  266. /* read file list */
  267. pip = test_mc_popen ();
  268. while (TRUE)
  269. {
  270. GString *line;
  271. test_mc_pread (pip);
  272. if (pip->out.len == MC_PIPE_STREAM_EOF)
  273. break;
  274. while ((line = mc_pstream_get_string (&pip->out)) != NULL)
  275. {
  276. /* handle an \n-separated file list */
  277. if (line->str[line->len - 1] == '\n')
  278. {
  279. /* entire file name or last chunk */
  280. g_string_truncate (line, line->len - 1);
  281. /* join filename chunks */
  282. if (remain_file_name != NULL)
  283. {
  284. g_string_append_len (remain_file_name, line->str, line->len);
  285. g_string_free (line, TRUE);
  286. line = remain_file_name;
  287. remain_file_name = NULL;
  288. }
  289. }
  290. else
  291. {
  292. /* first or middle chunk of file name */
  293. if (remain_file_name == NULL)
  294. remain_file_name = line;
  295. else
  296. {
  297. g_string_append_len (remain_file_name, line->str, line->len);
  298. g_string_free (line, TRUE);
  299. }
  300. line = NULL;
  301. }
  302. /* collect file names to assemble the result string */
  303. if (line == NULL)
  304. continue;
  305. if (result_long_file_list == NULL)
  306. result_long_file_list = line;
  307. else
  308. {
  309. g_string_append_len (result_long_file_list, line->str, line->len);
  310. g_string_free (line, TRUE);
  311. }
  312. g_string_append_c (result_long_file_list, '\n');
  313. }
  314. }
  315. mctest_assert_str_eq (etalon_long_file_list, result_long_file_list->str);
  316. g_string_free (result_long_file_list, TRUE);
  317. }
  318. /* *INDENT-OFF* */
  319. END_TEST
  320. /* *INDENT-ON* */
  321. /* --------------------------------------------------------------------------------------------- */
  322. int
  323. main (void)
  324. {
  325. TCase *tc_core;
  326. tc_core = tcase_create ("Core");
  327. tcase_add_checked_fixture (tc_core, setup, teardown);
  328. /* Add new tests here: *************** */
  329. mctest_add_parameterized_test (tc_core, mc_pstream_get_string_test, data_source);
  330. tcase_add_test (tc_core, mc_pstream_get_long_file_list_test);
  331. /* *********************************** */
  332. return mctest_run_all (tc_core);
  333. }
  334. /* --------------------------------------------------------------------------------------------- */