vfs_parse_ls_lga.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* lib/vfs - test vfs_parse_ls_lga() functionality
  2. Copyright (C) 2011 Free Software Foundation, Inc.
  3. Written by:
  4. Slava Zanko <slavazanko@gmail.com>, 2011
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program 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. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #define TEST_SUITE_NAME "/lib/vfs"
  18. #include <check.h>
  19. #include <stdio.h>
  20. #include "lib/global.h"
  21. #include "lib/vfs/utilvfs.h"
  22. #include "lib/vfs/xdirentry.h"
  23. #include "lib/strutil.h"
  24. #include "src/vfs/local/local.c"
  25. struct vfs_s_subclass test_subclass1;
  26. struct vfs_class vfs_test_ops1;
  27. struct vfs_s_entry *vfs_root_entry;
  28. static struct vfs_s_inode *vfs_root_inode;
  29. static struct vfs_s_super *vfs_test_super;
  30. void message (int flags, const char *title, const char *text, ...);
  31. static void
  32. setup (void)
  33. {
  34. static struct stat initstat;
  35. str_init_strings (NULL);
  36. vfs_init ();
  37. init_localfs ();
  38. vfs_setup_work_dir ();
  39. test_subclass1.flags = VFS_S_REMOTE;
  40. vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
  41. vfs_test_ops1.name = "testfs1";
  42. vfs_test_ops1.flags = VFSF_NOLINKS;
  43. vfs_test_ops1.prefix = "test1:";
  44. vfs_register_class (&vfs_test_ops1);
  45. vfs_test_super = g_new0 (struct vfs_s_super, 1);
  46. vfs_test_super->me = &vfs_test_ops1;
  47. vfs_root_inode = vfs_s_new_inode (&vfs_test_ops1, vfs_test_super, &initstat);
  48. vfs_root_entry = vfs_s_new_entry (&vfs_test_ops1, "/", vfs_root_inode);
  49. }
  50. static void
  51. teardown (void)
  52. {
  53. vfs_s_free_entry (&vfs_test_ops1, vfs_root_entry);
  54. vfs_shut ();
  55. str_uninit_strings ();
  56. }
  57. void
  58. message (int flags, const char *title, const char *text, ...)
  59. {
  60. char *p;
  61. va_list ap;
  62. (void) flags;
  63. (void) title;
  64. va_start (ap, text);
  65. p = g_strdup_vprintf (text, ap);
  66. va_end (ap);
  67. printf("message(): %s\n", p);
  68. g_free(p);
  69. }
  70. /* --------------------------------------------------------------------------------------------- */
  71. #define check_one_stat_field(etalon_stat, test_stat, field, format)\
  72. {\
  73. fail_unless(etalon_stat.field == test_stat.field,\
  74. "\netalon."#field" = " format "\nactual."#field" = " format "\n",\
  75. etalon_stat.field, test_stat.field);\
  76. }
  77. #define check_stat_struct(etalon_stat, test_stat)\
  78. {\
  79. check_one_stat_field(etalon_stat, test_stat, st_dev, "%zu");\
  80. check_one_stat_field(etalon_stat, test_stat, st_ino, "%zu");\
  81. check_one_stat_field(etalon_stat, test_stat, st_ino, "%zu");\
  82. check_one_stat_field(etalon_stat, test_stat, st_mode, "%04x");\
  83. check_one_stat_field(etalon_stat, test_stat, st_uid, "%u");\
  84. check_one_stat_field(etalon_stat, test_stat, st_gid, "%u");\
  85. check_one_stat_field(etalon_stat, test_stat, st_rdev, "%zu");\
  86. check_one_stat_field(etalon_stat, test_stat, st_size, "%zd");\
  87. check_one_stat_field(etalon_stat, test_stat, st_blksize, "%zu");\
  88. check_one_stat_field(etalon_stat, test_stat, st_blocks, "%zd");\
  89. check_one_stat_field(etalon_stat, test_stat, st_atime, "%zd");\
  90. check_one_stat_field(etalon_stat, test_stat, st_mtime, "%zd");\
  91. check_one_stat_field(etalon_stat, test_stat, st_ctime, "%zd");\
  92. }
  93. static void check_vfs_parse_ls_lga_call(const char *input_data, int etalon_result,
  94. const char *etalon_filename, const char *etalon_linkname, struct stat etalon_stat, size_t *filepos)
  95. {
  96. static struct stat test_stat;
  97. char *filename = NULL;
  98. char *linkname = NULL;
  99. gboolean result;
  100. result = vfs_parse_ls_lga (input_data, &test_stat, &filename, &linkname, filepos);
  101. fail_if (result != etalon_result,
  102. "\nactual result: %d\netalon result: %d\n", result, etalon_result);
  103. fail_unless((filename != NULL && etalon_filename != NULL && strcmp(filename, etalon_filename) == 0)
  104. || (filename == NULL && etalon_filename == filename),
  105. "\nactual filename '%s'\netalon filename '%s'", filename, etalon_filename);
  106. fail_unless((linkname != NULL && etalon_linkname != NULL && strcmp(linkname, etalon_linkname) == 0)
  107. || (linkname == NULL && etalon_linkname == linkname),
  108. "\nactual linkname '%s'\netalon linkname '%s'", linkname, etalon_linkname);
  109. check_stat_struct(etalon_stat, test_stat);
  110. }
  111. START_TEST (test_vfs_parse_ls_lga)
  112. {
  113. size_t filepos = 0;
  114. check_vfs_parse_ls_lga_call(
  115. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  116. 1, "build_root", NULL, (struct stat)
  117. {
  118. .st_dev = 0,
  119. .st_ino = 0,
  120. .st_mode = 0x41fd,
  121. .st_nlink = 10,
  122. .st_uid = 500,
  123. .st_gid = 500,
  124. .st_rdev = 0,
  125. .st_size = 4096,
  126. .st_blksize = 512,
  127. .st_blocks = 8,
  128. .st_atime = 1308838140,
  129. .st_mtime = 1308838140,
  130. .st_ctime = 1308838140
  131. },
  132. NULL
  133. );
  134. check_vfs_parse_ls_lga_call(
  135. "lrwxrwxrwx 1 500 500 11 Mar 13 2010 COPYING -> doc/COPYING",
  136. 1, "COPYING", "doc/COPYING",
  137. (struct stat)
  138. {
  139. .st_dev = 0,
  140. .st_ino = 0,
  141. .st_mode = 0xa1ff,
  142. .st_nlink = 10,
  143. .st_uid = 500,
  144. .st_gid = 500,
  145. .st_rdev = 0,
  146. .st_size = 11,
  147. .st_blksize = 512,
  148. .st_blocks = 1,
  149. .st_atime = 1268431200,
  150. .st_mtime = 1268431200,
  151. .st_ctime = 1268431200
  152. },
  153. NULL
  154. );
  155. check_vfs_parse_ls_lga_call(
  156. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  157. 1, "..", NULL, (struct stat)
  158. {
  159. .st_dev = 0,
  160. .st_ino = 0,
  161. .st_mode = 0x41fd,
  162. .st_nlink = 10,
  163. .st_uid = 500,
  164. .st_gid = 500,
  165. .st_rdev = 0,
  166. .st_size = 4096,
  167. .st_blksize = 512,
  168. .st_blocks = 8,
  169. .st_atime = 1308838140,
  170. .st_mtime = 1308838140,
  171. .st_ctime = 1308838140
  172. },
  173. &filepos
  174. );
  175. check_vfs_parse_ls_lga_call(
  176. "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
  177. 1, " build_root", NULL, (struct stat)
  178. {
  179. .st_dev = 0,
  180. .st_ino = 0,
  181. .st_mode = 0x41fd,
  182. .st_nlink = 10,
  183. .st_uid = 500,
  184. .st_gid = 500,
  185. .st_rdev = 0,
  186. .st_size = 4096,
  187. .st_blksize = 512,
  188. .st_blocks = 8,
  189. .st_atime = 1308838140,
  190. .st_mtime = 1308838140,
  191. .st_ctime = 1308838140
  192. },
  193. &filepos
  194. );
  195. }
  196. END_TEST
  197. /* --------------------------------------------------------------------------------------------- */
  198. START_TEST (test_vfs_parse_ls_lga_reorder)
  199. {
  200. size_t filepos = 0;
  201. struct vfs_s_entry *ent1, *ent2, *ent3;
  202. int i;
  203. ent1 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  204. i = ent1->ino->st.st_nlink;
  205. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1",
  206. &ent1->ino->st, &ent1->name, &ent1->ino->linkname, &filepos))
  207. {
  208. fail ("An error occured while parse ls output");
  209. return;
  210. }
  211. vfs_s_store_filename_pos (ent1, filepos);
  212. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent1);
  213. ent2 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  214. i = ent2->ino->st.st_nlink;
  215. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2",
  216. &ent2->ino->st, &ent2->name, &ent2->ino->linkname, &filepos))
  217. {
  218. fail ("An error occured while parse ls output");
  219. return;
  220. }
  221. vfs_s_store_filename_pos (ent2, filepos);
  222. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent2);
  223. ent3 = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);
  224. i = ent3->ino->st.st_nlink;
  225. if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
  226. &ent3->ino->st, &ent3->name, &ent3->ino->linkname, &filepos))
  227. {
  228. fail ("An error occured while parse ls output");
  229. return;
  230. }
  231. vfs_s_store_filename_pos (ent3, filepos);
  232. vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent3);
  233. vfs_s_normalize_filename_pos (vfs_root_inode, filepos);
  234. fail_unless(strcmp(ent1->name, " build_root1") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent1->name, " build_root1");
  235. fail_unless(strcmp(ent2->name, " build_root2") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent2->name, " build_root2");
  236. }
  237. END_TEST
  238. /* --------------------------------------------------------------------------------------------- */
  239. int
  240. main (void)
  241. {
  242. int number_failed;
  243. Suite *s = suite_create (TEST_SUITE_NAME);
  244. TCase *tc_core = tcase_create ("Core");
  245. SRunner *sr;
  246. tcase_add_checked_fixture (tc_core, setup, teardown);
  247. /* Add new tests here: *************** */
  248. tcase_add_test (tc_core, test_vfs_parse_ls_lga);
  249. tcase_add_test (tc_core, test_vfs_parse_ls_lga_reorder);
  250. /* *********************************** */
  251. suite_add_tcase (s, tc_core);
  252. sr = srunner_create (s);
  253. srunner_set_log (sr, "vfs_parse_ls_lga.log");
  254. srunner_run_all (sr, CK_NORMAL);
  255. number_failed = srunner_ntests_failed (sr);
  256. srunner_free (sr);
  257. return (number_failed == 0) ? 0 : 1;
  258. }
  259. /* --------------------------------------------------------------------------------------------- */