PageRenderTime 221ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/3rd-party/glib-2.16.6/gio/tests/live-g-file.c

https://bitbucket.org/super119/plu2youku
C | 1099 lines | 877 code | 155 blank | 67 comment | 239 complexity | 86cdf83696e7d7ecd8910bc2b6583ae9 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-3.0
  1. /* GLib testing framework examples and tests
  2. * Copyright (C) 2008 Red Hat, Inc.
  3. * Authors: Tomas Bzatek <tbzatek@redhat.com>
  4. *
  5. * This work is provided "as is"; redistribution and modification
  6. * in whole or in part, in any medium, physical or electronic is
  7. * permitted without restriction.
  8. *
  9. * This work is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. * In no event shall the authors or contributors be liable for any
  14. * direct, indirect, incidental, special, exemplary, or consequential
  15. * damages (including, but not limited to, procurement of substitute
  16. * goods or services; loss of use, data, or profits; or business
  17. * interruption) however caused and on any theory of liability, whether
  18. * in contract, strict liability, or tort (including negligence or
  19. * otherwise) arising in any way out of the use of this software, even
  20. * if advised of the possibility of such damage.
  21. */
  22. #include <glib/gtestutils.h>
  23. #include <glib/glib.h>
  24. #include <gio/gio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #define PATTERN_FILE_SIZE 0x10000
  28. #define TEST_HANDLE_SPECIAL TRUE
  29. enum StructureExtraFlags
  30. {
  31. TEST_DELETE_NORMAL = 1 << 0,
  32. TEST_DELETE_TRASH = 1 << 1,
  33. TEST_DELETE_NON_EMPTY = 1 << 2,
  34. TEST_DELETE_FAILURE = 1 << 3,
  35. TEST_NOT_EXISTS = 1 << 4,
  36. TEST_ENUMERATE_FILE = 1 << 5,
  37. TEST_NO_ACCESS = 1 << 6,
  38. TEST_COPY = 1 << 7,
  39. TEST_MOVE = 1 << 8,
  40. TEST_COPY_ERROR_RECURSE = 1 << 9,
  41. TEST_ALREADY_EXISTS = 1 << 10,
  42. TEST_TARGET_IS_FILE = 1 << 11,
  43. TEST_CREATE = 1 << 12,
  44. TEST_REPLACE = 1 << 13,
  45. TEST_APPEND = 1 << 14,
  46. TEST_OPEN = 1 << 15,
  47. TEST_OVERWRITE = 1 << 16,
  48. TEST_INVALID_SYMLINK = 1 << 17,
  49. };
  50. struct StructureItem
  51. {
  52. const char *filename;
  53. const char *link_to;
  54. GFileType file_type;
  55. GFileCreateFlags create_flags;
  56. guint32 mode;
  57. gboolean handle_special;
  58. enum StructureExtraFlags extra_flags;
  59. };
  60. #define TEST_DIR_NO_ACCESS "dir_no-access"
  61. #define TEST_DIR_NO_WRITE "dir_no-write"
  62. #define TEST_DIR_TARGET "dir-target"
  63. #define TEST_NAME_NOT_EXISTS "not_exists"
  64. #define TEST_TARGET_FILE "target-file"
  65. static const struct StructureItem sample_struct[] = {
  66. /* filename link file_type create_flags mode | handle_special | extra_flags */
  67. {"dir1", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_DELETE_NON_EMPTY | TEST_REPLACE | TEST_OPEN},
  68. {"dir1/subdir", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE | TEST_APPEND},
  69. {"dir2", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_MOVE | TEST_CREATE},
  70. {TEST_DIR_TARGET, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE},
  71. {TEST_DIR_NO_ACCESS, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_PRIVATE, S_IRUSR + S_IWUSR + S_IRGRP + S_IWGRP + S_IROTH + S_IWOTH, 0, TEST_NO_ACCESS | TEST_OPEN},
  72. {TEST_DIR_NO_WRITE, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_PRIVATE, S_IRUSR + S_IXUSR + S_IRGRP + S_IXGRP + S_IROTH + S_IXOTH, 0, 0},
  73. {TEST_TARGET_FILE, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OPEN},
  74. {"normal_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_CREATE | TEST_OVERWRITE},
  75. {"normal_file-symlink", "normal_file", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_COPY | TEST_OPEN},
  76. {"executable_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, S_IRWXU + S_IRWXG + S_IRWXO, 0, TEST_DELETE_TRASH | TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_REPLACE},
  77. {"private_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_PRIVATE, 0, 0, TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_APPEND},
  78. {"normal_file2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OVERWRITE | TEST_REPLACE},
  79. {"readonly_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, S_IRUSR + S_IRGRP + S_IROTH, 0, TEST_DELETE_NORMAL | TEST_OPEN},
  80. {"UTF_p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
  81. NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_CREATE | TEST_OPEN | TEST_OVERWRITE},
  82. {"dir_p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
  83. NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_CREATE},
  84. {"pattern_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_COPY | TEST_OPEN | TEST_APPEND},
  85. {TEST_NAME_NOT_EXISTS, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_NORMAL | TEST_NOT_EXISTS | TEST_COPY | TEST_OPEN},
  86. {TEST_NAME_NOT_EXISTS, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_TRASH | TEST_NOT_EXISTS | TEST_MOVE},
  87. {"not_exists2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_CREATE},
  88. {"not_exists3", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_REPLACE},
  89. {"not_exists4", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_APPEND},
  90. {"dir_no-execute/file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_NORMAL | TEST_DELETE_FAILURE | TEST_NOT_EXISTS | TEST_OPEN},
  91. {"lost_symlink", "nowhere", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_DELETE_NORMAL | TEST_OPEN | TEST_INVALID_SYMLINK},
  92. };
  93. static gboolean write_test;
  94. static gboolean verbose;
  95. static gboolean posix_compat;
  96. #define log(msg...) if (verbose) g_print (msg)
  97. static GFile *
  98. create_empty_file (GFile * parent, const char *filename,
  99. GFileCreateFlags create_flags)
  100. {
  101. GFile *child;
  102. gboolean res;
  103. GError *error;
  104. GFileOutputStream *outs;
  105. child = g_file_get_child (parent, filename);
  106. g_assert (child != NULL);
  107. error = NULL;
  108. outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
  109. g_assert (error == NULL);
  110. g_assert (outs != NULL);
  111. error = NULL;
  112. res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
  113. g_object_unref (outs);
  114. return child;
  115. }
  116. static GFile *
  117. create_empty_dir (GFile * parent, const char *filename)
  118. {
  119. GFile *child;
  120. gboolean res;
  121. GError *error;
  122. child = g_file_get_child (parent, filename);
  123. g_assert (child != NULL);
  124. error = NULL;
  125. res = g_file_make_directory (child, NULL, &error);
  126. g_assert_cmpint (res, ==, TRUE);
  127. g_assert (error == NULL);
  128. return child;
  129. }
  130. static GFile *
  131. create_symlink (GFile * parent, const char *filename, const char *points_to)
  132. {
  133. GFile *child;
  134. gboolean res;
  135. GError *error;
  136. child = g_file_get_child (parent, filename);
  137. g_assert (child != NULL);
  138. error = NULL;
  139. res = g_file_make_symbolic_link (child, points_to, NULL, &error);
  140. g_assert_cmpint (res, ==, TRUE);
  141. g_assert (error == NULL);
  142. return child;
  143. }
  144. static void
  145. test_create_structure (gconstpointer test_data)
  146. {
  147. GFile *root;
  148. GFile *child;
  149. gboolean res;
  150. GError *error;
  151. GFileOutputStream *outs;
  152. GDataOutputStream *outds;
  153. int i;
  154. struct StructureItem item;
  155. g_assert (test_data != NULL);
  156. log ("\n Going to create testing structure in '%s'...\n",
  157. (char *) test_data);
  158. root = g_file_new_for_commandline_arg ((char *) test_data);
  159. g_assert (root != NULL);
  160. /* create root directory */
  161. res = g_file_make_directory (root, NULL, NULL);
  162. /* don't care about errors here */
  163. /* create any other items */
  164. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  165. {
  166. item = sample_struct[i];
  167. if ((item.handle_special)
  168. || ((!posix_compat)
  169. && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)))
  170. continue;
  171. child = NULL;
  172. switch (item.file_type)
  173. {
  174. case G_FILE_TYPE_REGULAR:
  175. log (" Creating file '%s'...\n", item.filename);
  176. child = create_empty_file (root, item.filename, item.create_flags);
  177. break;
  178. case G_FILE_TYPE_DIRECTORY:
  179. log (" Creating directory '%s'...\n", item.filename);
  180. child = create_empty_dir (root, item.filename);
  181. break;
  182. case G_FILE_TYPE_SYMBOLIC_LINK:
  183. log (" Creating symlink '%s' --> '%s'...\n", item.filename,
  184. item.link_to);
  185. child = create_symlink (root, item.filename, item.link_to);
  186. break;
  187. default:
  188. break;
  189. }
  190. g_assert (child != NULL);
  191. if ((item.mode > 0) && (posix_compat))
  192. {
  193. error = NULL;
  194. res =
  195. g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
  196. item.mode,
  197. G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
  198. NULL, &error);
  199. g_assert_cmpint (res, ==, TRUE);
  200. g_assert (error == NULL);
  201. }
  202. g_object_unref (child);
  203. }
  204. /* create a pattern file */
  205. log (" Creating pattern file...");
  206. child = g_file_get_child (root, "pattern_file");
  207. g_assert (child != NULL);
  208. error = NULL;
  209. outs =
  210. g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
  211. g_assert (error == NULL);
  212. g_assert (outs != NULL);
  213. outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
  214. g_assert (outds != NULL);
  215. for (i = 0; i < PATTERN_FILE_SIZE; i++)
  216. {
  217. error = NULL;
  218. res = g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
  219. g_assert (error == NULL);
  220. }
  221. error = NULL;
  222. res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
  223. g_assert (error == NULL);
  224. g_object_unref (outds);
  225. g_object_unref (outs);
  226. g_object_unref (child);
  227. log (" done.\n");
  228. g_object_unref (root);
  229. }
  230. static GFile *
  231. file_exists (GFile * parent, const char *filename, gboolean * result)
  232. {
  233. GFile *child;
  234. gboolean res;
  235. if (result)
  236. *result = FALSE;
  237. child = g_file_get_child (parent, filename);
  238. g_assert (child != NULL);
  239. res = g_file_query_exists (child, NULL);
  240. if (result)
  241. *result = res;
  242. return child;
  243. }
  244. static void
  245. test_attributes (struct StructureItem item, GFileInfo * info)
  246. {
  247. GFileType ftype;
  248. guint32 mode;
  249. const char *name, *display_name, *edit_name, *copy_name, *symlink_target;
  250. gboolean utf8_valid;
  251. gboolean has_attr;
  252. gboolean is_symlink;
  253. gboolean can_read, can_write;
  254. /* standard::type */
  255. has_attr = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
  256. g_assert_cmpint (has_attr, ==, TRUE);
  257. ftype = g_file_info_get_file_type (info);
  258. g_assert_cmpint (ftype, !=, G_FILE_TYPE_UNKNOWN);
  259. g_assert_cmpint (ftype, ==, item.file_type);
  260. /* unix::mode */
  261. if ((item.mode > 0) && (posix_compat))
  262. {
  263. mode =
  264. g_file_info_get_attribute_uint32 (info,
  265. G_FILE_ATTRIBUTE_UNIX_MODE) & 0xFFF;
  266. g_assert_cmpint (mode, ==, item.mode);
  267. }
  268. /* access::can-read */
  269. if (item.file_type != G_FILE_TYPE_SYMBOLIC_LINK)
  270. {
  271. can_read =
  272. g_file_info_get_attribute_boolean (info,
  273. G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
  274. g_assert_cmpint (can_read, ==, TRUE);
  275. }
  276. /* access::can-write */
  277. if ((write_test) && ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE))
  278. {
  279. can_write =
  280. g_file_info_get_attribute_boolean (info,
  281. G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
  282. g_assert_cmpint (can_write, ==, TRUE);
  283. }
  284. /* standard::name */
  285. name = g_file_info_get_name (info);
  286. g_assert (name != NULL);
  287. /* standard::display-name */
  288. display_name = g_file_info_get_display_name (info);
  289. g_assert (display_name != NULL);
  290. utf8_valid = g_utf8_validate (display_name, -1, NULL);
  291. g_assert_cmpint (utf8_valid, ==, TRUE);
  292. /* standard::edit-name */
  293. edit_name = g_file_info_get_edit_name (info);
  294. if (edit_name)
  295. {
  296. utf8_valid = g_utf8_validate (edit_name, -1, NULL);
  297. g_assert_cmpint (utf8_valid, ==, TRUE);
  298. }
  299. /* standard::copy-name */
  300. copy_name =
  301. g_file_info_get_attribute_string (info,
  302. G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
  303. if (copy_name)
  304. {
  305. utf8_valid = g_utf8_validate (copy_name, -1, NULL);
  306. g_assert_cmpint (utf8_valid, ==, TRUE);
  307. }
  308. /* standard::is-symlink */
  309. if (posix_compat)
  310. {
  311. is_symlink = g_file_info_get_is_symlink (info);
  312. g_assert_cmpint (is_symlink, ==,
  313. item.file_type == G_FILE_TYPE_SYMBOLIC_LINK);
  314. }
  315. /* standard::symlink-target */
  316. if ((item.file_type == G_FILE_TYPE_SYMBOLIC_LINK) && (posix_compat))
  317. {
  318. symlink_target = g_file_info_get_symlink_target (info);
  319. g_assert_cmpstr (symlink_target, ==, item.link_to);
  320. }
  321. }
  322. static void
  323. test_initial_structure (gconstpointer test_data)
  324. {
  325. GFile *root;
  326. GFile *child;
  327. gboolean res;
  328. GError *error;
  329. GFileInputStream *ins;
  330. int i;
  331. GFileInfo *info;
  332. guint32 size;
  333. guchar *buffer;
  334. gssize read, total_read;
  335. struct StructureItem item;
  336. g_assert (test_data != NULL);
  337. log ("\n Testing sample structure in '%s'...\n", (char *) test_data);
  338. root = g_file_new_for_commandline_arg ((char *) test_data);
  339. g_assert (root != NULL);
  340. res = g_file_query_exists (root, NULL);
  341. g_assert_cmpint (res, ==, TRUE);
  342. /* test the structure */
  343. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  344. {
  345. item = sample_struct[i];
  346. if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
  347. || (item.handle_special))
  348. continue;
  349. log (" Testing file '%s'...\n", item.filename);
  350. child = file_exists (root, item.filename, &res);
  351. g_assert (child != NULL);
  352. g_assert_cmpint (res, ==, TRUE);
  353. error = NULL;
  354. info =
  355. g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
  356. NULL, &error);
  357. g_assert (error == NULL);
  358. g_assert (info != NULL);
  359. test_attributes (item, info);
  360. g_object_unref (child);
  361. }
  362. /* read and test the pattern file */
  363. log (" Testing pattern file...\n");
  364. child = file_exists (root, "pattern_file", &res);
  365. g_assert (child != NULL);
  366. g_assert_cmpint (res, ==, TRUE);
  367. error = NULL;
  368. info =
  369. g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
  370. &error);
  371. g_assert (error == NULL);
  372. g_assert (info != NULL);
  373. size = g_file_info_get_size (info);
  374. g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);
  375. error = NULL;
  376. ins = g_file_read (child, NULL, &error);
  377. g_assert (ins != NULL);
  378. g_assert (error == NULL);
  379. buffer = g_malloc (PATTERN_FILE_SIZE);
  380. total_read = 0;
  381. while (total_read < PATTERN_FILE_SIZE)
  382. {
  383. error = NULL;
  384. read =
  385. g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,
  386. PATTERN_FILE_SIZE, NULL, &error);
  387. g_assert (error == NULL);
  388. total_read += read;
  389. log (" read %d bytes, total = %d of %d.\n", read, total_read,
  390. PATTERN_FILE_SIZE);
  391. }
  392. g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
  393. error = NULL;
  394. res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
  395. g_assert (error == NULL);
  396. g_assert_cmpint (res, ==, TRUE);
  397. for (i = 0; i < PATTERN_FILE_SIZE; i++)
  398. g_assert_cmpint (*(buffer + i), ==, i % 256);
  399. g_object_unref (ins);
  400. g_object_unref (child);
  401. g_free (buffer);
  402. g_object_unref (root);
  403. }
  404. static void
  405. traverse_recurse_dirs (GFile * parent, GFile * root)
  406. {
  407. gboolean res;
  408. GError *error;
  409. GFileEnumerator *enumerator;
  410. GFileInfo *info;
  411. GFile *descend;
  412. char *relative_path;
  413. int i;
  414. gboolean found;
  415. g_assert (root != NULL);
  416. error = NULL;
  417. enumerator =
  418. g_file_enumerate_children (parent, "*",
  419. G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
  420. &error);
  421. g_assert (enumerator != NULL);
  422. g_assert (error == NULL);
  423. error = NULL;
  424. info = g_file_enumerator_next_file (enumerator, NULL, &error);
  425. while ((info) && (!error))
  426. {
  427. descend = g_file_get_child (parent, g_file_info_get_name (info));
  428. g_assert (descend != NULL);
  429. relative_path = g_file_get_relative_path (root, descend);
  430. g_assert (relative_path != NULL);
  431. found = FALSE;
  432. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  433. {
  434. if (strcmp (sample_struct[i].filename, relative_path) == 0)
  435. {
  436. /* test the attributes again */
  437. test_attributes (sample_struct[i], info);
  438. found = TRUE;
  439. break;
  440. }
  441. }
  442. g_assert_cmpint (found, ==, TRUE);
  443. log (" Found file %s, relative to root: %s\n",
  444. g_file_info_get_display_name (info), relative_path);
  445. if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
  446. traverse_recurse_dirs (descend, root);
  447. g_object_unref (descend);
  448. error = NULL;
  449. info = g_file_enumerator_next_file (enumerator, NULL, &error);
  450. }
  451. g_assert (error == NULL);
  452. error = NULL;
  453. res = g_file_enumerator_close (enumerator, NULL, &error);
  454. g_assert_cmpint (res, ==, TRUE);
  455. g_assert (error == NULL);
  456. }
  457. static void
  458. test_traverse_structure (gconstpointer test_data)
  459. {
  460. GFile *root;
  461. gboolean res;
  462. g_assert (test_data != NULL);
  463. log ("\n Traversing through the sample structure in '%s'...\n",
  464. (char *) test_data);
  465. root = g_file_new_for_commandline_arg ((char *) test_data);
  466. g_assert (root != NULL);
  467. res = g_file_query_exists (root, NULL);
  468. g_assert_cmpint (res, ==, TRUE);
  469. traverse_recurse_dirs (root, root);
  470. g_object_unref (root);
  471. }
  472. static void
  473. test_enumerate (gconstpointer test_data)
  474. {
  475. GFile *root, *child;
  476. gboolean res;
  477. GError *error;
  478. GFileEnumerator *enumerator;
  479. GFileInfo *info;
  480. int i;
  481. struct StructureItem item;
  482. g_assert (test_data != NULL);
  483. log ("\n Test enumerate '%s'...\n", (char *) test_data);
  484. root = g_file_new_for_commandline_arg ((char *) test_data);
  485. g_assert (root != NULL);
  486. res = g_file_query_exists (root, NULL);
  487. g_assert_cmpint (res, ==, TRUE);
  488. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  489. {
  490. item = sample_struct[i];
  491. if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
  492. continue;
  493. if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
  494. (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
  495. && posix_compat)
  496. || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
  497. TEST_ENUMERATE_FILE))
  498. {
  499. log (" Testing file '%s'\n", item.filename);
  500. child = g_file_get_child (root, item.filename);
  501. g_assert (child != NULL);
  502. error = NULL;
  503. enumerator =
  504. g_file_enumerate_children (child, "*",
  505. G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
  506. NULL, &error);
  507. if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
  508. {
  509. g_assert (enumerator == NULL);
  510. g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
  511. }
  512. if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
  513. {
  514. g_assert (enumerator == NULL);
  515. g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);
  516. }
  517. if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
  518. {
  519. g_assert (enumerator != NULL);
  520. error = NULL;
  521. info = g_file_enumerator_next_file (enumerator, NULL, &error);
  522. g_assert (info == NULL);
  523. g_assert (error == NULL);
  524. /* no items should be found, no error should be logged */
  525. }
  526. if (error)
  527. g_error_free (error);
  528. if (enumerator)
  529. {
  530. error = NULL;
  531. res = g_file_enumerator_close (enumerator, NULL, &error);
  532. g_assert_cmpint (res, ==, TRUE);
  533. g_assert (error == NULL);
  534. }
  535. g_object_unref (child);
  536. }
  537. }
  538. g_object_unref (root);
  539. }
  540. static void
  541. do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
  542. enum StructureExtraFlags extra_flags)
  543. {
  544. GFile *dst_dir, *src_file, *dst_file;
  545. gboolean res;
  546. GError *error;
  547. log (" do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
  548. dst_dir = g_file_get_child (root, target_dir);
  549. g_assert (dst_dir != NULL);
  550. src_file = g_file_get_child (root, item.filename);
  551. g_assert (src_file != NULL);
  552. dst_file = g_file_get_child (dst_dir, item.filename);
  553. g_assert (dst_file != NULL);
  554. error = NULL;
  555. if ((item.extra_flags & TEST_COPY) == TEST_COPY)
  556. res =
  557. g_file_copy (src_file, dst_file,
  558. G_FILE_COPY_NOFOLLOW_SYMLINKS |
  559. ((extra_flags ==
  560. TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
  561. G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
  562. else
  563. res =
  564. g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
  565. NULL, NULL, &error);
  566. if (error)
  567. log (" res = %d, error code %d = %s\n", res, error->code,
  568. error->message);
  569. /* copying file/directory to itself (".") */
  570. if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
  571. (extra_flags == TEST_ALREADY_EXISTS))
  572. {
  573. g_assert_cmpint (res, ==, FALSE);
  574. g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
  575. }
  576. /* target file is a file, overwrite is not set */
  577. else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
  578. (extra_flags == TEST_TARGET_IS_FILE))
  579. {
  580. g_assert_cmpint (res, ==, FALSE);
  581. if (item.file_type == G_FILE_TYPE_DIRECTORY)
  582. g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);
  583. else
  584. g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);
  585. }
  586. /* source file is directory */
  587. else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
  588. TEST_COPY_ERROR_RECURSE)
  589. {
  590. g_assert_cmpint (res, ==, FALSE);
  591. g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);
  592. }
  593. /* source or target path doesn't exist */
  594. else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
  595. (extra_flags == TEST_NOT_EXISTS))
  596. {
  597. g_assert_cmpint (res, ==, FALSE);
  598. g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
  599. }
  600. /* source or target path permission denied */
  601. else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
  602. (extra_flags == TEST_NO_ACCESS))
  603. {
  604. g_assert_cmpint (res, ==, FALSE);
  605. g_assert_cmpint (error->code, ==, G_IO_ERROR_PERMISSION_DENIED);
  606. }
  607. /* no error should be found, all exceptions defined above */
  608. else
  609. {
  610. g_assert_cmpint (res, ==, TRUE);
  611. g_assert (error == NULL);
  612. }
  613. if (error)
  614. g_error_free (error);
  615. g_object_unref (dst_dir);
  616. g_object_unref (src_file);
  617. g_object_unref (dst_file);
  618. }
  619. static void
  620. test_copy_move (gconstpointer test_data)
  621. {
  622. GFile *root;
  623. gboolean res;
  624. int i;
  625. struct StructureItem item;
  626. log ("\n");
  627. g_assert (test_data != NULL);
  628. root = g_file_new_for_commandline_arg ((char *) test_data);
  629. g_assert (root != NULL);
  630. res = g_file_query_exists (root, NULL);
  631. g_assert_cmpint (res, ==, TRUE);
  632. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  633. {
  634. item = sample_struct[i];
  635. if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
  636. continue;
  637. if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
  638. ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
  639. {
  640. /* test copy/move to a directory, expecting no errors if source files exist */
  641. do_copy_move (root, item, TEST_DIR_TARGET, 0);
  642. /* some files have been already moved so we can't count with them in the tests */
  643. if ((item.extra_flags & TEST_COPY) == TEST_COPY)
  644. {
  645. /* test overwrite for flagged files */
  646. if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
  647. {
  648. do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
  649. }
  650. /* source = target, should return G_IO_ERROR_EXISTS */
  651. do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
  652. /* target is file */
  653. do_copy_move (root, item, TEST_TARGET_FILE,
  654. TEST_TARGET_IS_FILE);
  655. /* target path is invalid */
  656. do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
  657. TEST_NOT_EXISTS);
  658. /* tests on POSIX-compatible filesystems */
  659. if (posix_compat)
  660. {
  661. /* target directory is not accessible (no execute flag) */
  662. do_copy_move (root, item, TEST_DIR_NO_ACCESS,
  663. TEST_NO_ACCESS);
  664. /* target directory is readonly */
  665. do_copy_move (root, item, TEST_DIR_NO_WRITE,
  666. TEST_NO_ACCESS);
  667. }
  668. }
  669. }
  670. }
  671. g_object_unref (root);
  672. }
  673. static void
  674. test_create (gconstpointer test_data)
  675. {
  676. GFile *root, *child;
  677. gboolean res;
  678. GError *error;
  679. int i;
  680. struct StructureItem item;
  681. GFileOutputStream *os;
  682. g_assert (test_data != NULL);
  683. log ("\n");
  684. root = g_file_new_for_commandline_arg ((char *) test_data);
  685. g_assert (root != NULL);
  686. res = g_file_query_exists (root, NULL);
  687. g_assert_cmpint (res, ==, TRUE);
  688. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  689. {
  690. item = sample_struct[i];
  691. if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
  692. ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
  693. ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
  694. {
  695. log (" test_create: '%s'\n", item.filename);
  696. child = g_file_get_child (root, item.filename);
  697. g_assert (child != NULL);
  698. error = NULL;
  699. os = NULL;
  700. if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
  701. os = g_file_create (child, item.create_flags, NULL, &error);
  702. else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
  703. os =
  704. g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
  705. &error);
  706. else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
  707. os = g_file_append_to (child, item.create_flags, NULL, &error);
  708. if (error)
  709. log (" error code %d = %s\n", error->code, error->message);
  710. if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
  711. ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
  712. {
  713. g_assert (os == NULL);
  714. g_assert (error != NULL);
  715. g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
  716. }
  717. else if (item.file_type == G_FILE_TYPE_DIRECTORY)
  718. {
  719. g_assert (os == NULL);
  720. g_assert (error != NULL);
  721. if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
  722. g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
  723. else
  724. g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY);
  725. }
  726. else
  727. {
  728. g_assert (os != NULL);
  729. g_assert (error == NULL);
  730. }
  731. if (error)
  732. g_error_free (error);
  733. if (os)
  734. {
  735. error = NULL;
  736. res =
  737. g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
  738. if (error)
  739. log (" g_output_stream_close: error %d = %s\n",
  740. error->code, error->message);
  741. g_assert_cmpint (res, ==, TRUE);
  742. g_assert (error == NULL);
  743. }
  744. g_object_unref (child);
  745. }
  746. }
  747. g_object_unref (root);
  748. }
  749. static void
  750. test_open (gconstpointer test_data)
  751. {
  752. GFile *root, *child;
  753. gboolean res;
  754. GError *error;
  755. int i;
  756. struct StructureItem item;
  757. GFileInputStream *input_stream;
  758. g_assert (test_data != NULL);
  759. log ("\n");
  760. root = g_file_new_for_commandline_arg ((char *) test_data);
  761. g_assert (root != NULL);
  762. res = g_file_query_exists (root, NULL);
  763. g_assert_cmpint (res, ==, TRUE);
  764. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  765. {
  766. item = sample_struct[i];
  767. if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
  768. continue;
  769. if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
  770. {
  771. log (" test_open: '%s'\n", item.filename);
  772. child = g_file_get_child (root, item.filename);
  773. g_assert (child != NULL);
  774. error = NULL;
  775. input_stream = g_file_read (child, NULL, &error);
  776. if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
  777. ((item.extra_flags & TEST_INVALID_SYMLINK) ==
  778. TEST_INVALID_SYMLINK))
  779. {
  780. g_assert (input_stream == NULL);
  781. g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
  782. }
  783. else if (item.file_type == G_FILE_TYPE_DIRECTORY)
  784. {
  785. g_assert (input_stream == NULL);
  786. g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY);
  787. }
  788. else
  789. {
  790. g_assert (input_stream != NULL);
  791. g_assert (error == NULL);
  792. }
  793. if (error)
  794. g_error_free (error);
  795. if (input_stream)
  796. {
  797. error = NULL;
  798. res =
  799. g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
  800. &error);
  801. g_assert_cmpint (res, ==, TRUE);
  802. g_assert (error == NULL);
  803. }
  804. g_object_unref (child);
  805. }
  806. }
  807. g_object_unref (root);
  808. }
  809. static void
  810. test_delete (gconstpointer test_data)
  811. {
  812. GFile *root;
  813. GFile *child;
  814. gboolean res;
  815. GError *error;
  816. int i;
  817. struct StructureItem item;
  818. g_assert (test_data != NULL);
  819. log ("\n");
  820. root = g_file_new_for_commandline_arg ((char *) test_data);
  821. g_assert (root != NULL);
  822. res = g_file_query_exists (root, NULL);
  823. g_assert_cmpint (res, ==, TRUE);
  824. for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
  825. {
  826. item = sample_struct[i];
  827. if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
  828. continue;
  829. if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
  830. ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
  831. {
  832. child = file_exists (root, item.filename, &res);
  833. g_assert (child != NULL);
  834. /* we don't care about result here */
  835. log (" Deleting %s, path = %s\n", item.filename,
  836. g_file_get_path (child));
  837. error = NULL;
  838. if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
  839. res = g_file_delete (child, NULL, &error);
  840. else
  841. res = g_file_trash (child, NULL, &error);
  842. if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
  843. TEST_DELETE_NON_EMPTY)
  844. {
  845. g_assert_cmpint (res, ==, FALSE);
  846. g_assert (error != NULL);
  847. g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_EMPTY);
  848. }
  849. if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
  850. {
  851. g_assert_cmpint (res, ==, FALSE);
  852. g_assert (error != NULL);
  853. g_assert_cmpint (error->code, !=, 0);
  854. }
  855. if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
  856. {
  857. g_assert_cmpint (res, ==, FALSE);
  858. g_assert (error != NULL);
  859. g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
  860. }
  861. if (error)
  862. {
  863. log (" result = %d, error = %s\n", res, error->message);
  864. g_error_free (error);
  865. }
  866. g_object_unref (child);
  867. }
  868. }
  869. g_object_unref (root);
  870. }
  871. int
  872. main (int argc, char *argv[])
  873. {
  874. static gboolean create_struct;
  875. static char *target_path;
  876. GError *error;
  877. GOptionContext *context;
  878. static GOptionEntry cmd_entries[] = {
  879. {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
  880. "Perform write tests (incl. structure creation)", NULL},
  881. {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &create_struct,
  882. "Only create testing structure (no tests)", NULL},
  883. {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
  884. {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
  885. "Test POSIX-specific features (unix permissions, symlinks)", NULL},
  886. {NULL}
  887. };
  888. verbose = FALSE;
  889. write_test = FALSE;
  890. create_struct = FALSE;
  891. target_path = NULL;
  892. posix_compat = FALSE;
  893. /* strip all gtester-specific args */
  894. g_type_init ();
  895. g_test_init (&argc, &argv, NULL);
  896. /* add trailing args */
  897. error = NULL;
  898. context = g_option_context_new ("target_path");
  899. g_option_context_add_main_entries (context, cmd_entries, NULL);
  900. if (!g_option_context_parse (context, &argc, &argv, &error))
  901. {
  902. g_print ("option parsing failed: %s\n", error->message);
  903. return g_test_run ();
  904. }
  905. /* missing mandatory arg for target dir */
  906. if (argc < 2)
  907. {
  908. g_print (g_option_context_get_help (context, TRUE, NULL));
  909. return g_test_run ();
  910. }
  911. target_path = strdup (argv[1]);
  912. /* Write test - create new testing structure */
  913. if (write_test || create_struct)
  914. g_test_add_data_func ("/live-g-file/create_structure", target_path,
  915. test_create_structure);
  916. /* Read test - test the sample structure - expect defined attributes to be there */
  917. if (!create_struct)
  918. g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
  919. test_initial_structure);
  920. /* Read test - test traverse the structure - no special file should appear */
  921. if (!create_struct)
  922. g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
  923. test_traverse_structure);
  924. /* Read test - enumerate */
  925. if (!create_struct)
  926. g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
  927. test_enumerate);
  928. /* Read test - open (g_file_read()) */
  929. if (!create_struct)
  930. g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
  931. /* Write test - create */
  932. if (write_test && (!create_struct))
  933. g_test_add_data_func ("/live-g-file/test_create", target_path,
  934. test_create);
  935. /* Write test - copy, move */
  936. if (write_test && (!create_struct))
  937. g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
  938. test_copy_move);
  939. /* Write test - delete, trash */
  940. if (write_test && (!create_struct))
  941. g_test_add_data_func ("/live-g-file/test_delete", target_path,
  942. test_delete);
  943. return g_test_run ();
  944. }