1 /* lib/vfs - test vfs_parse_ls_lga() functionality
3 Copyright (C) 2011 Free Software Foundation, Inc.
6 Slava Zanko <slavazanko@gmail.com>, 2011
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define TEST_SUITE_NAME "/lib/vfs"
30 #include "lib/global.h"
31 #include "lib/vfs/utilvfs.h"
32 #include "lib/vfs/xdirentry.h"
33 #include "lib/strutil.h"
35 #include "src/vfs/local/local.c"
38 struct vfs_s_subclass test_subclass1
;
39 struct vfs_class vfs_test_ops1
;
41 struct vfs_s_entry
*vfs_root_entry
;
42 static struct vfs_s_inode
*vfs_root_inode
;
43 static struct vfs_s_super
*vfs_test_super
;
45 void message (int flags
, const char *title
, const char *text
, ...);
51 static struct stat initstat
;
53 str_init_strings (NULL
);
57 vfs_setup_work_dir ();
59 test_subclass1
.flags
= VFS_S_REMOTE
;
60 vfs_s_init_class (&vfs_test_ops1
, &test_subclass1
);
61 vfs_test_ops1
.name
= "testfs1";
62 vfs_test_ops1
.flags
= VFSF_NOLINKS
;
63 vfs_test_ops1
.prefix
= "test1:";
64 vfs_register_class (&vfs_test_ops1
);
66 vfs_test_super
= g_new0 (struct vfs_s_super
, 1);
67 vfs_test_super
->me
= &vfs_test_ops1
;
69 vfs_root_inode
= vfs_s_new_inode (&vfs_test_ops1
, vfs_test_super
, &initstat
);
70 vfs_root_entry
= vfs_s_new_entry (&vfs_test_ops1
, "/", vfs_root_inode
);
76 vfs_s_free_entry (&vfs_test_ops1
, vfs_root_entry
);
78 str_uninit_strings ();
83 message (int flags
, const char *title
, const char *text
, ...)
92 p
= g_strdup_vprintf (text
, ap
);
94 printf("message(): %s\n", p
);
98 /* --------------------------------------------------------------------------------------------- */
100 #define check_one_stat_field(etalon_stat, test_stat, field, format, input_str)\
102 fail_unless(etalon_stat.field == test_stat.field,\
103 "\ninput string: %s\netalon."#field" = " format "\nactual."#field" = " format "\n",\
104 input_str, etalon_stat.field, test_stat.field);\
107 #define check_stat_struct(etalon_stat, test_stat, input_str)\
109 check_one_stat_field(etalon_stat, test_stat, st_dev, "%zu", input_str);\
110 check_one_stat_field(etalon_stat, test_stat, st_ino, "%zu", input_str);\
111 check_one_stat_field(etalon_stat, test_stat, st_ino, "%zu", input_str);\
112 check_one_stat_field(etalon_stat, test_stat, st_mode, "%04x", input_str);\
113 check_one_stat_field(etalon_stat, test_stat, st_uid, "%u", input_str);\
114 check_one_stat_field(etalon_stat, test_stat, st_gid, "%u", input_str);\
115 check_one_stat_field(etalon_stat, test_stat, st_rdev, "%zu", input_str);\
116 check_one_stat_field(etalon_stat, test_stat, st_size, "%zd", input_str);\
117 check_one_stat_field(etalon_stat, test_stat, st_blksize, "%zu", input_str);\
118 check_one_stat_field(etalon_stat, test_stat, st_blocks, "%zd", input_str);\
119 check_one_stat_field(etalon_stat, test_stat, st_atime, "%zd", input_str);\
120 check_one_stat_field(etalon_stat, test_stat, st_mtime, "%zd", input_str);\
121 check_one_stat_field(etalon_stat, test_stat, st_ctime, "%zd", input_str);\
124 static void check_vfs_parse_ls_lga_call(const char *input_data
, int etalon_result
,
125 const char *etalon_filename
, const char *etalon_linkname
, struct stat etalon_stat
, size_t *filepos
)
127 static struct stat test_stat
;
128 char *filename
= NULL
;
129 char *linkname
= NULL
;
132 result
= vfs_parse_ls_lga (input_data
, &test_stat
, &filename
, &linkname
, filepos
);
134 fail_if (result
!= etalon_result
,
135 "\nactual result: %d\netalon result: %d\n", result
, etalon_result
);
137 fail_unless((filename
!= NULL
&& etalon_filename
!= NULL
&& strcmp(filename
, etalon_filename
) == 0)
138 || (filename
== NULL
&& etalon_filename
== filename
),
139 "\nactual filename '%s'\netalon filename '%s'", filename
, etalon_filename
);
141 fail_unless((linkname
!= NULL
&& etalon_linkname
!= NULL
&& strcmp(linkname
, etalon_linkname
) == 0)
142 || (linkname
== NULL
&& etalon_linkname
== linkname
),
143 "\nactual linkname '%s'\netalon linkname '%s'", linkname
, etalon_linkname
);
145 check_stat_struct(etalon_stat
, test_stat
, input_data
);
148 START_TEST (test_vfs_parse_ls_lga
)
152 vfs_parse_ls_lga_init();
154 check_vfs_parse_ls_lga_call(
155 "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
156 1, "build_root", NULL
, (struct stat
)
168 .st_atime
= 1308838140,
169 .st_mtime
= 1308838140,
170 .st_ctime
= 1308838140
175 check_vfs_parse_ls_lga_call(
176 "lrwxrwxrwx 1 500 500 11 Mar 13 2010 COPYING -> doc/COPYING",
177 1, "COPYING", "doc/COPYING",
190 .st_atime
= 1268431200,
191 .st_mtime
= 1268431200,
192 .st_ctime
= 1268431200
197 check_vfs_parse_ls_lga_call(
198 "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
199 1, "..", NULL
, (struct stat
)
211 .st_atime
= 1308838140,
212 .st_mtime
= 1308838140,
213 .st_ctime
= 1308838140
218 check_vfs_parse_ls_lga_call(
219 "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root",
220 1, "build_root", NULL
, (struct stat
)
232 .st_atime
= 1308838140,
233 .st_mtime
= 1308838140,
234 .st_ctime
= 1308838140
242 /* --------------------------------------------------------------------------------------------- */
244 START_TEST (test_vfs_parse_ls_lga_reorder
)
247 struct vfs_s_entry
*ent1
, *ent2
, *ent3
;
250 vfs_parse_ls_lga_init();
252 ent1
= vfs_s_generate_entry (&vfs_test_ops1
, NULL
, vfs_root_inode
, 0);
253 i
= ent1
->ino
->st
.st_nlink
;
254 if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1",
255 &ent1
->ino
->st
, &ent1
->name
, &ent1
->ino
->linkname
, &filepos
))
257 fail ("An error occured while parse ls output");
260 vfs_s_store_filename_leading_spaces (ent1
, filepos
);
261 vfs_s_insert_entry (&vfs_test_ops1
, vfs_root_inode
, ent1
);
264 ent2
= vfs_s_generate_entry (&vfs_test_ops1
, NULL
, vfs_root_inode
, 0);
265 i
= ent2
->ino
->st
.st_nlink
;
266 if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2",
267 &ent2
->ino
->st
, &ent2
->name
, &ent2
->ino
->linkname
, &filepos
))
269 fail ("An error occured while parse ls output");
272 vfs_s_store_filename_leading_spaces (ent2
, filepos
);
273 vfs_s_insert_entry (&vfs_test_ops1
, vfs_root_inode
, ent2
);
275 ent3
= vfs_s_generate_entry (&vfs_test_ops1
, NULL
, vfs_root_inode
, 0);
276 i
= ent3
->ino
->st
.st_nlink
;
277 if (! vfs_parse_ls_lga ("drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..",
278 &ent3
->ino
->st
, &ent3
->name
, &ent3
->ino
->linkname
, &filepos
))
280 fail ("An error occured while parse ls output");
283 vfs_s_store_filename_leading_spaces (ent3
, filepos
);
284 vfs_s_insert_entry (&vfs_test_ops1
, vfs_root_inode
, ent3
);
286 vfs_s_normalize_filename_leading_spaces (vfs_root_inode
, vfs_parse_ls_lga_get_final_spaces ());
288 fail_unless(strcmp(ent1
->name
, " build_root1") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent1
->name
, " build_root1");
289 fail_unless(strcmp(ent2
->name
, " build_root2") == 0, "\nactual '%s'\nnot equal to '%s'\n", ent2
->name
, " build_root2");
293 /* --------------------------------------------------------------------------------------------- */
294 #define parce_one_line(ent_index, ls_output) {\
295 ent[ent_index] = vfs_s_generate_entry (&vfs_test_ops1, NULL, vfs_root_inode, 0);\
296 if (! vfs_parse_ls_lga (ls_output,\
297 &ent[ent_index]->ino->st, &ent[ent_index]->name, &ent[ent_index]->ino->linkname, &filepos))\
299 fail ("An error occured while parse ls output");\
302 vfs_s_store_filename_leading_spaces (ent[ent_index], filepos);\
303 vfs_s_insert_entry (&vfs_test_ops1, vfs_root_inode, ent[ent_index]);\
306 #define fail_unless_ent(ent_index, etalon_str){\
308 strcmp(ent[ent_index]->name, etalon_str) == 0,\
309 "\nactual '%s'\nnot equal to '%s'\n", ent[ent_index]->name, etalon_str\
313 START_TEST (test_vfs_parse_ls_lga_unaligned
)
316 struct vfs_s_entry
*ent
[4];
318 vfs_parse_ls_lga_init();
320 parce_one_line(0, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root1");
321 parce_one_line(1, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root2");
322 parce_one_line(2, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 ..");
323 parce_one_line(3, "drwxrwxr-x 10 500 500 4096 Jun 23 17:09 build_root 0");
325 vfs_s_normalize_filename_leading_spaces (vfs_root_inode
, vfs_parse_ls_lga_get_final_spaces ());
327 fail_unless_ent(0, "build_root1");
328 fail_unless_ent(1, " build_root2");
329 fail_unless_ent(3, " build_root 0");
334 /* --------------------------------------------------------------------------------------------- */
341 Suite
*s
= suite_create (TEST_SUITE_NAME
);
342 TCase
*tc_core
= tcase_create ("Core");
345 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
347 /* Add new tests here: *************** */
348 tcase_add_test (tc_core
, test_vfs_parse_ls_lga
);
349 tcase_add_test (tc_core
, test_vfs_parse_ls_lga_reorder
);
350 tcase_add_test (tc_core
, test_vfs_parse_ls_lga_unaligned
);
351 /* *********************************** */
353 suite_add_tcase (s
, tc_core
);
354 sr
= srunner_create (s
);
355 srunner_set_log (sr
, "vfs_parse_ls_lga.log");
356 srunner_run_all (sr
, CK_NORMAL
);
357 number_failed
= srunner_ntests_failed (sr
);
359 return (number_failed
== 0) ? 0 : 1;
362 /* --------------------------------------------------------------------------------------------- */