Line data Source code
1 : //
2 : // test_utils.c
3 : // oc-runner
4 : //
5 : // Created by Danny Goossen on 16/9/18.
6 : // Copyright (c) 2018 Danny Goossen. All rights reserved.
7 : //
8 :
9 : //LCOV_EXCL_START
10 : #include <check.h>
11 : //LCOV_EXCL_STOP
12 :
13 : #include "../src/utils.h"
14 : #include "../src/cJSON_deploy.h"
15 : #include "../src/error.h"
16 :
17 1 : START_TEST(check_dirops)
18 : {
19 1 : vrmdir(NULL);
20 1 : vrmdir_s(NULL);
21 1 : vmkdir(NULL);
22 1 : xis_dir(NULL);
23 1 : ck_assert_int_ne(1, v_exist_dir(NULL));
24 1 : vmkdir("./test/a/b");
25 2 : ck_assert_int_eq(1, v_exist_dir("./test/a/b"));
26 2 : ck_assert_int_ne(1, v_exist_dir("./test/a/b/%s","c"));
27 1 : char * fpath=vcmkdir("%s/%s","./testlink","al/bl");
28 2 : ck_assert_ptr_nonnull(fpath);
29 2 : ck_assert_str_eq("./testlink/al/bl", fpath);
30 2 : if (fpath) free(fpath);
31 1 : symlink("../testlink/al", "./test/al");
32 1 : ck_assert_int_eq(0, xis_dir("./test/a/b"));
33 2 : ck_assert_int_eq(0, xis_dir("./test/al"));
34 1 : vrmdir("./test");
35 2 : ck_assert_int_eq(-1, xis_dir("./test/al"));
36 2 : ck_assert_int_eq(0, xis_dir("./testlink/al/bl"));
37 1 : vmkdir("./test/");
38 1 : symlink("../testlink/al", "./test/al");
39 1 : vrmdir_s("./test");
40 2 : ck_assert_int_eq(0, xis_dir("./test/al"));
41 2 : ck_assert_int_eq(-1, xis_dir("./testlink/al/bl"));
42 1 : vrmdir("./testlink");
43 2 : ck_assert_int_eq(-1, xis_dir("./testlink"));
44 1 : vrmdir("./test");
45 1 : char *dir2=vcmkdir("./test_dir/test/");
46 2 : ck_assert_str_eq(dir2, "./test_dir/test");
47 1 : vrmdir("./test_dir");
48 2 : if (dir2) free(dir2);
49 : }
50 1 : END_TEST
51 :
52 1 : START_TEST(check_fileops)
53 : {
54 1 : char * dir =vcmkdir("./%s","test_fileops");
55 1 : ck_assert_int_eq(v_exist_dir("./%s","test_fileops"),1);
56 2 : ck_assert_int_eq(xis_dir(dir),0);
57 2 : ck_assert_int_eq(xis_dir("test_fileopts/don'texist"),-1);
58 2 : ck_assert_ptr_nonnull(dir);
59 2 : ck_assert_int_eq(write_file_v("1234567890",10,"%s/%s",dir,"ten.txt"),0);
60 2 : ck_assert_int_eq(xis_dir("test_fileopts/ten.txt"),-1);
61 1 : char * r1=read_a_file("./test_fileops/ten.txt");
62 1 : char * r2=read_a_file_v("./test_fileops/%s","ten.txt");
63 2 : ck_assert_ptr_nonnull(r1);
64 2 : ck_assert_ptr_nonnull(r2);
65 2 : ck_assert_str_eq(r1, "1234567890");
66 2 : ck_assert_str_eq(r2, "1234567890");
67 :
68 2 : ck_assert_int_eq(cp_file("./test_fileops/ten.txt","./test_fileops/cpten.txt"),10);
69 2 : ck_assert_int_eq(cp_file("./test_fileops/ten.txt","./test_fileops3/cpten.txt"),-1);
70 2 : ck_assert_int_eq(cp_file("./test_fileops/ten.txt",NULL),-1);
71 2 : ck_assert_int_eq(cp_file("./test_fileops23/ten.txt","./test_fileops/cpten.txt"),-1);
72 2 : ck_assert_int_eq(cp_file(NULL,"./test_fileops/cpten.txt"),-1);
73 :
74 2 : ck_assert_int_eq(append_file_2_file("test_fileops/ten.txt","test_fileops/cpten.txt"),10);
75 2 : ck_assert_int_eq(append_file_2_file("test_fileops22/ten.txt","test_fileops/cpten.txt"),-1);
76 2 : ck_assert_int_eq(append_file_2_file(NULL,"test_fileops/cpten.txt"),-1);
77 2 : ck_assert_int_eq(append_file_2_file("test_fileops/ten.txt","test_fileops234/cpten.txt"),-1);
78 2 : ck_assert_int_eq(append_file_2_file("test_fileops/ten.txt",NULL),-1);
79 2 : ck_assert_int_eq(append_data_2_file("01234","test_fileops/cpten.txt"),5);
80 2 : ck_assert_int_eq(append_data_2_file("01234","test_fileops/aps5.txt"),5);
81 2 : ck_assert_int_eq(append_data_2_file("01234","test_fileops3/aps5.txt"),-1);
82 1 : char * r3=read_a_file("./test_fileops/cpten.txt");
83 1 : char * r4=read_a_file("./test_fileops/aps5.txt");
84 2 : ck_assert_ptr_nonnull(r3);
85 2 : ck_assert_ptr_nonnull(r4);
86 2 : ck_assert_str_eq(r3, "1234567890123456789001234");
87 2 : ck_assert_str_eq(r4, "01234");
88 2 : if (r1) free(r1);
89 1 : if (r2) free(r2);
90 1 : if (r3) free(r3);
91 1 : if (r4) free(r4);
92 1 : vrmdir("%s",dir);
93 1 : ck_assert_int_eq(v_exist_dir("./%s","test_fileops"),0);
94 2 : ck_assert_int_eq(write_file_v("1234567890",10,NULL),-1);
95 1 : r1=read_a_file(NULL);
96 2 : ck_assert_ptr_null(r1);
97 1 : r1=read_a_file_v(NULL);
98 2 : ck_assert_ptr_null(r1);
99 1 : r1=read_a_file("test_fileopts/don'texist");
100 2 : ck_assert_ptr_null(r1);
101 1 : r1=read_a_file_v("test_fileopts/dontexist");
102 2 : ck_assert_ptr_null(r1);
103 2 : ck_assert_int_eq(write_file_v("1234567890",10,"./nonexistdeirectory/test.txt"),-1);
104 2 : if (dir)free(dir);
105 1 : dir =vcmkdir(NULL);
106 1 : ck_assert_ptr_null(dir);
107 : }
108 1 : END_TEST
109 :
110 1 : START_TEST(check_stringops)
111 : {
112 1 : upper_string(NULL);
113 1 : lower_string(NULL);
114 1 : upper_string_n(NULL, 0);
115 1 : upper_string_n(NULL, 5);
116 1 : char test[5]="AaBb\0";
117 1 : upper_string(test);
118 1 : ck_assert_str_eq(test, "AABB\0");
119 1 : lower_string(test);
120 2 : ck_assert_str_eq(test, "aabb\0");
121 1 : upper_string_n(test, 2);
122 2 : ck_assert_str_eq(test, "AAbb\0");
123 1 : char test2[5]="\na\n\n\0";
124 1 : strip_nl(test2);
125 2 : ck_assert_str_eq(test2, "\na\0");
126 : }
127 1 : END_TEST
128 :
129 1 : START_TEST(check_filelist)
130 : {
131 1 : vmkdir("./test_filelist/d1/d11");
132 1 : vmkdir("./test_filelist/d2/d22/d222");
133 1 : vmkdir("./otest_filelist_out/d3");
134 1 : write_file_v("1234567890",10,"%s/%s","./test_filelist/d1/d11/","ten1.txt");
135 1 : write_file_v("1234567890",10,"%s/%s","./test_filelist/d1/d11/","ten2.txt");
136 1 : write_file_v("1234567890",10,"%s/%s","./test_filelist/d1","ten3.txt");
137 1 : write_file_v("1234567890",10,"%s/%s","./test_filelist/d1/","ten4.txt");
138 1 : write_file_v("1234567890",0,"%s/%s","./test_filelist/d1","ten5.txt");
139 1 : write_file_v("1234567890",10,"%s/%s","./test_filelist/d2/d22/","ten6.txt");
140 :
141 1 : write_file_v("1234567890",10,"%s/%s","./test_filelist_out/d3/","ten7.txt");
142 : char pwdb[PATH_MAX];
143 1 : char * pwd=getcwd(pwdb, PATH_MAX);
144 1 : char * projectdir=NULL;
145 1 : asprintf(&projectdir, "%s/test_filelist",pwd);
146 1 : cJSON*paths=cJSON_CreateArray();
147 1 : cJSON_AddItemToArray(paths, cJSON_CreateString("../otest_filelist_out/*"));
148 1 : cJSON_AddItemToArray(paths, cJSON_CreateString("../d1/*"));
149 1 : cJSON_AddItemToArray(paths, cJSON_CreateString("d1/*"));
150 1 : cJSON_AddItemToArray(paths, cJSON_CreateString("d1/;"));
151 1 : cJSON_AddItemToArray(paths, cJSON_CreateString("./d2/../d2"));
152 1 : cJSON * filelist=NULL;
153 :
154 1 : ck_assert_int_eq( getfilelist(projectdir,paths,&filelist),0);
155 2 : ck_assert_ptr_nonnull(filelist);
156 2 : ck_assert_int_eq(cJSON_GetArraySize(filelist), 10);
157 2 : if (filelist) cJSON_Delete(filelist);
158 1 : if (paths) cJSON_Delete(paths);
159 1 : if (projectdir) free(projectdir);
160 1 : vrmdir("./test_filelist");
161 1 : vrmdir("./otest_filelist_out");
162 : }
163 1 : END_TEST
164 :
165 :
166 : struct test_data_optvalue_t
167 : {
168 : char * optv;
169 : char * defaultv;
170 : char * result;
171 : } ;
172 : static const struct test_data_optvalue_t testlist_optvalue[4] = {
173 : { NULL,NULL,""},
174 : { NULL,"AB","AB"},
175 : { "AB","CD","AB"},
176 : { "EF",NULL,"EF"}
177 : };
178 :
179 :
180 4 : START_TEST(check_optvalue)
181 : {
182 4 : char * test=get_opt_value(testlist_optvalue[_i].optv,testlist_optvalue[_i].defaultv);
183 4 : ck_assert_ptr_nonnull(test);
184 8 : ck_assert_str_eq(test, testlist_optvalue[_i].result);
185 8 : if (test) free(test);
186 4 : test=NULL;
187 : }
188 4 : END_TEST
189 :
190 : struct test_data_format_size_t
191 : {
192 : uint64_t sz;
193 : char * format;
194 : } ;
195 : static const struct test_data_format_size_t testlist_format_size[10] = {
196 : { 0,"0 B"},
197 : { 1,"1 B"},
198 : { 9999000,"10 MB"},
199 : { 999900,"1.0 MB"},
200 : { 25000,"25 kB"},
201 : { 1024,"1.0 kB"},
202 : { 8096,"8.1 kB"},
203 : { 16192,"16 kB"},
204 : { 100000000,"100 MB"},
205 : {1230000, "1.2 MB"}
206 : };
207 :
208 10 : START_TEST(check_format_size)
209 : {
210 : char buff[FORMAT_SIZE_BUF];
211 10 : ck_assert_str_eq(testlist_format_size[_i].format, format_size(buff, testlist_format_size[_i].sz));
212 : }
213 10 : END_TEST
214 :
215 1 : START_TEST(check_stderr_redirect)
216 : {
217 : int my_pipe[2];
218 1 : my_pipe[0]=-1;
219 1 : my_pipe[1]=-1;
220 1 : char * redirect=NULL;
221 1 : int saved_stderr=0;
222 1 : saved_stderr=pipe_redirect_stderr(my_pipe);
223 1 : ck_assert_int_ne(0, saved_stderr);
224 1 : fprintf(stderr, "redirectedfromstderr\n");
225 1 : pipe_redirect_undo(my_pipe, saved_stderr, &redirect);
226 2 : ck_assert_ptr_nonnull(redirect);
227 2 : ck_assert_str_eq(redirect, "redirectedfromstderr\n");
228 2 : if(redirect) free(redirect);
229 1 : redirect=NULL;
230 : // if we do 2 times, for sure we got the undo correct.
231 1 : saved_stderr=pipe_redirect_stderr(my_pipe);
232 1 : ck_assert_int_ne(0, saved_stderr);
233 1 : fprintf(stderr, "redirectedfromstderr\n");
234 1 : pipe_redirect_undo(my_pipe, saved_stderr, &redirect);
235 2 : ck_assert_ptr_nonnull(redirect);
236 2 : ck_assert_str_eq(redirect, "redirectedfromstderr\n");
237 2 : if(redirect) free(redirect);
238 1 : redirect=NULL;
239 : }
240 1 : END_TEST
241 :
242 4 : START_TEST(check_split_path_file_2_path)
243 : {
244 : struct test_data_t
245 : {
246 : char * in;
247 : char * out;
248 : } ;
249 : static const struct test_data_t testlist[6] = {
250 : { "/dir/file","/dir/"},
251 : { "dir/dir1/","dir/dir1/"},
252 : { "/dir/dir1/","/dir/dir1/"},
253 : { "dir/dir1/file","dir/dir1/"},
254 : { "file",""},
255 : { NULL, NULL},
256 : };
257 4 : char * result=NULL;
258 :
259 4 : split_path_file_2_path(&result,testlist[_i].in);
260 4 : if(testlist[_i].out)
261 : {
262 4 : ck_assert_ptr_nonnull(result);
263 8 : ck_assert_str_eq(result,testlist[_i].out );
264 : }
265 : else
266 0 : ck_assert_ptr_null(result);
267 : }
268 4 : END_TEST
269 : /*
270 :
271 : covered by other tests:
272 : void split_path_file_2_path(char** p,const char *pf);
273 : char * normalizePath(const char* pwd, const char * src, char* res);
274 : void * memrchr(const void *s,int c, size_t n);
275 :
276 : // no idea yet how to test yet:
277 : int pipe_redirect_stderr(int my_pipe[2]);
278 : void pipe_redirect_undo(int my_pipe[2],int saved_stderr,char**errormsg);
279 : */
280 :
281 24 : Suite * utils_suite(void)
282 : {
283 : Suite *s;
284 : TCase *tc_core;
285 : //TCase *tc_progress;
286 24 : s = suite_create("test_error");
287 : /* Core test case */
288 24 : tc_core = tcase_create("Core");
289 : //tcase_add_checked_fixture(tc_core, setup, teardown);
290 :
291 24 : tcase_add_test (tc_core, check_dirops);
292 24 : tcase_add_test (tc_core, check_stringops);
293 24 : tcase_add_test (tc_core,check_fileops);
294 24 : tcase_add_test (tc_core,check_filelist);
295 24 : tcase_add_loop_test (tc_core,check_format_size,0,10);
296 24 : tcase_add_loop_test (tc_core,check_optvalue,0,4);
297 24 : tcase_add_loop_test (tc_core,check_split_path_file_2_path,0,4);
298 24 : tcase_add_test (tc_core,check_stderr_redirect);
299 24 : tcase_add_unchecked_fixture(tc_core, NULL, NULL);
300 24 : tcase_set_timeout(tc_core,15);
301 24 : suite_add_tcase(s, tc_core);
302 24 : return s;
303 : }
304 :
305 :
306 24 : int main(void)
307 : {
308 : int number_failed;
309 : Suite *s;
310 : SRunner *sr;
311 :
312 24 : s = utils_suite();
313 24 : sr = srunner_create(s);
314 24 : srunner_run_all(sr, CK_NORMAL);
315 1 : number_failed = srunner_ntests_failed(sr);
316 1 : srunner_free(sr);
317 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
318 : }
319 :
|