Line data Source code
1 : /*
2 : test_http_config.c
3 : Created by Danny Goossen, Gioxa Ltd on 9/4/17.
4 :
5 : MIT License
6 :
7 : Copyright (c) 2017 deployctl, Gioxa Ltd.
8 :
9 : Permission is hereby granted, free of charge, to any person obtaining a copy
10 : of this software and associated documentation files (the "Software"), to deal
11 : in the Software without restriction, including without limitation the rights
12 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 : copies of the Software, and to permit persons to whom the Software is
14 : furnished to do so, subject to the following conditions:
15 :
16 : The above copyright notice and this permission notice shall be included in all
17 : copies or substantial portions of the Software.
18 :
19 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 : SOFTWARE.
26 :
27 : */
28 :
29 :
30 : #include <check.h>
31 : #include "../src/deployd.h"
32 :
33 :
34 : typedef struct test_stubs_s
35 : {
36 : int url_verify;
37 : int verify_http_config;
38 : int reload_http_config;
39 : int letsencrypt;
40 : } test_stubs_t;
41 :
42 : // stubfunctions for http_config:
43 2 : int url_verify(char * commit_sha,char * baseHREF,struct trace_Struct * trace)
44 : {
45 : ;
46 : // answer according feedback->tests
47 2 : return( ((test_stubs_t *)trace->tests)->url_verify);
48 : }
49 :
50 7 : int update_details(void * userp)
51 : {
52 7 : return 0;
53 : }
54 : /*
55 : To test:
56 :
57 : int url_check(void * opaque, char * basePATH,char * baseHREF);
58 : int delete_http_config(void * opaque,char * basePATH);
59 : int write_http_config(void * opaque, int is_https,char * basePATH, char * server_name);
60 :
61 : int check_namespace(void * opaque,char * filepath);
62 : int write_namespace(void * opaque, char * filepath );
63 : */
64 :
65 :
66 : //int url_check(void * opaque, char * basePATH,char * baseHREF);
67 1 : START_TEST(check_url_check )
68 : {
69 1 : cJSON * env_json= cJSON_CreateObject();
70 1 : ck_assert_ptr_ne(env_json,NULL);
71 : parameter_t parameters;
72 : data_exchange_t data_exchange;
73 1 : void * opaque=&data_exchange;
74 : char * newarg[4];
75 :
76 :
77 1 : data_exchange.needenvp=0;
78 1 : data_exchange.gid=getgid();
79 1 : data_exchange.uid=geteuid() ;
80 1 : data_exchange.env_json=env_json;
81 1 : data_exchange.paramlist=(char **)newarg;
82 1 : data_exchange.timeout=1;
83 1 : parameters.timeout=data_exchange.timeout;
84 :
85 1 : data_exchange.parameters=¶meters;
86 : test_stubs_t tests;
87 1 : int temp=0;
88 1 : struct trace_Struct * trace=NULL;
89 1 : init_dynamic_trace(&trace,"","",1);
90 1 : data_exchange.trace=trace;
91 1 : data_exchange.trace->tests=&tests;
92 1 : int exitcode=0;
93 :
94 : char cwd[1024];
95 2 : ck_assert_ptr_ne(getcwd(cwd, sizeof(cwd)),NULL);
96 : char basePATH[1024];
97 1 : sprintf(basePATH,"%s/testurl_check",cwd);
98 : //parameters.prefix=cwd;
99 1 : cJSON_AddItemToObject(env_json,"CI_COMMIT_SHA" , cJSON_CreateString("CI_COMMIT_SHA_data" ));
100 1 : ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
101 : char this_command[256];
102 1 : data_exchange.this_command=this_command;
103 1 : sprintf(this_command,"rmdir");
104 :
105 : // OK
106 1 : tests.url_verify=0;
107 :
108 1 : newarg[0]="/bin/rm";
109 1 : newarg[1]="-rf";
110 1 : newarg[2]=(char *)basePATH;
111 1 : newarg[3]=NULL;
112 1 : temp=cmd_exec(opaque);
113 :
114 : char pubpath[1024];
115 1 : sprintf(pubpath,"%s/public",basePATH);
116 1 : newarg[0]="/bin/mkdir";
117 1 : newarg[1]="-p";
118 1 : newarg[2]=(char *)pubpath;
119 1 : newarg[3]=NULL;
120 1 : temp=cmd_exec(opaque);
121 :
122 1 : exitcode=url_check(opaque, basePATH,"mydomain.com");
123 2 : ck_assert_int_eq(exitcode,0);
124 :
125 : // can't write file
126 1 : newarg[0]="/bin/rm";
127 1 : newarg[1]="-rf";
128 1 : newarg[2]=(char *)basePATH;
129 1 : newarg[3]=NULL;
130 1 : temp=cmd_exec(opaque);
131 :
132 1 : exitcode=url_check(opaque, basePATH,"mydomain.com");
133 2 : ck_assert_int_ne(exitcode,0);
134 :
135 : // url_verify fail
136 1 : exitcode=0;
137 1 : tests.url_verify=1;
138 1 : newarg[0]="/bin/rm";
139 1 : newarg[1]="-rf";
140 1 : newarg[2]=(char *)basePATH;
141 1 : newarg[3]=NULL;
142 1 : temp=cmd_exec(opaque);
143 :
144 1 : newarg[0]="/bin/mkdir";
145 1 : newarg[1]="-p";
146 1 : newarg[2]=(char *)pubpath;
147 1 : newarg[3]=NULL;
148 1 : temp=cmd_exec(opaque);
149 :
150 1 : exitcode=url_check(opaque, basePATH,"mydomain.com");
151 2 : ck_assert_int_ne(exitcode,0);
152 : // cleanup
153 1 : newarg[0]="/bin/rm";
154 1 : newarg[1]="-rf";
155 1 : newarg[2]=(char *)basePATH;
156 1 : newarg[3]=NULL;
157 1 : temp=cmd_exec(opaque);
158 1 : cJSON_Delete(env_json);
159 1 : free_dynamic_trace(&trace);
160 : }
161 1 : END_TEST
162 :
163 : //int write_http_config(void * opaque, int is_https,char * basePATH, char * server_name);
164 : //int delete_http_config(void * opaque,char * basePATH);
165 1 : START_TEST(check_http_config_op)
166 : {
167 : parameter_t parameters;
168 : data_exchange_t data_exchange;
169 :
170 1 : void * opaque=&data_exchange;
171 : char output_buf[0x10000];
172 1 : int exitcode=0;
173 : char * newarg[4];
174 1 : data_exchange.needenvp=0;
175 1 : data_exchange.gid=getgid();
176 1 : data_exchange.uid=geteuid() ;
177 1 : data_exchange.env_json=NULL;
178 1 : data_exchange.paramlist=(char **)newarg;
179 1 : data_exchange.timeout=1;
180 1 : parameters.timeout=data_exchange.timeout;
181 1 : data_exchange.parameters=¶meters;
182 :
183 1 : struct trace_Struct * trace=NULL;
184 1 : init_dynamic_trace(&trace,"","",1);
185 1 : data_exchange.trace=trace;
186 1 : int temp=0;
187 : char cwd[1024];
188 1 : ck_assert_ptr_ne(getcwd(cwd, sizeof(cwd)),NULL);
189 : char basePATH[1024];
190 1 : sprintf(basePATH,"%s/http_config_op",cwd);
191 : //parameters.prefix=cwd;
192 1 : ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
193 : char this_command[256];
194 1 : data_exchange.this_command=this_command;
195 1 : sprintf(this_command,"rmdir");
196 : char filepath[1024];
197 1 : sprintf(filepath,"%s/server.conf",basePATH);
198 : // OK
199 : // http OK
200 1 : newarg[0]="/bin/rm";
201 1 : newarg[1]="-rf";
202 1 : newarg[2]=(char *)basePATH;
203 1 : newarg[3]=NULL;
204 1 : temp=cmd_exec(opaque);
205 :
206 1 : newarg[0]="/bin/mkdir";
207 1 : newarg[1]="-p";
208 1 : newarg[2]=(char *)basePATH;
209 1 : newarg[3]=NULL;
210 1 : temp=cmd_exec(opaque);
211 :
212 1 : exitcode=write_http_config(opaque, 0,basePATH, "mydomain.com");
213 2 : ck_assert_int_eq(exitcode,0);
214 2 : ck_assert_int_ne(access( filepath, F_OK ),-1);
215 : // TODO diff with testfiles/server.conf.mydomain.com.http
216 :
217 : // https OK, with already existing http config
218 1 : exitcode=write_http_config(opaque, 1,basePATH, "mydomain.com");
219 2 : ck_assert_int_eq(exitcode,0);
220 2 : ck_assert_int_ne(access( filepath, F_OK ),-1);
221 : // TODO diff with testfiles/server.conf.mydomain.com.https
222 :
223 : // delete config_file
224 1 : exitcode=delete_http_config(opaque,basePATH);
225 2 : ck_assert_int_eq(exitcode,0);
226 2 : ck_assert_int_eq(access( filepath, F_OK ),-1);
227 :
228 : // can't write file
229 1 : newarg[0]="/bin/rm";
230 1 : newarg[1]="-rf";
231 1 : newarg[2]=(char *)basePATH;
232 1 : newarg[3]=NULL;
233 1 : temp=cmd_exec(opaque);
234 : // base path does not exists
235 1 : exitcode=write_http_config(opaque, 0,basePATH, "mydomain.com");
236 2 : ck_assert_int_ne(exitcode,0);
237 2 : ck_assert_int_eq(access( filepath, F_OK ),-1);
238 1 : free_dynamic_trace(&trace);
239 : }
240 1 : END_TEST
241 :
242 : //int write_namespace(void * opaque, char * filepath );
243 : //int check_namespace(void * opaque,char * filepath);
244 1 : START_TEST(check_namespace_op)
245 : {
246 : //ck_assert_ptr_ne(env_json,NULL);
247 : parameter_t parameters;
248 : data_exchange_t data_exchange;
249 1 : void * opaque=&data_exchange;
250 1 : int exitcode=0;
251 : char * newarg[4];
252 1 : data_exchange.needenvp=0;
253 1 : data_exchange.gid=getgid();
254 1 : data_exchange.uid=geteuid() ;
255 1 : data_exchange.env_json=cJSON_CreateObject();
256 1 : data_exchange.paramlist=(char **)newarg;
257 1 : data_exchange.timeout=1;
258 1 : parameters.timeout=data_exchange.timeout;
259 1 : data_exchange.parameters=¶meters;
260 1 : struct trace_Struct * trace=NULL;
261 1 : init_dynamic_trace(&trace,"","",1);
262 1 : data_exchange.trace=trace;
263 1 : int temp=0;
264 :
265 : char cwd[1024];
266 1 : ck_assert_ptr_ne(getcwd(cwd, sizeof(cwd)),NULL);
267 : char basePATH[1024];
268 1 : sprintf(basePATH,"%s/namespace_check",cwd);
269 : //parameters.prefix=cwd;
270 2 : ck_assert_ptr_ne(data_exchange.env_json,NULL);
271 1 : cJSON_AddItemToObject(data_exchange.env_json,"CI_PROJECT_URL" , cJSON_CreateString("CI_PROJECT_URL_data" ));
272 :
273 1 : ((data_exchange_t *)opaque)->paramlist=(char **)newarg;
274 : char this_command[256];
275 1 : data_exchange.this_command=this_command;
276 1 : sprintf(this_command,"rmdir");
277 : char filepath[1024];
278 1 : sprintf(filepath,"%s/.namespace",basePATH);
279 :
280 : //OK
281 1 : newarg[0]="/bin/rm";
282 1 : newarg[1]="-rf";
283 1 : newarg[2]=(char *)basePATH;
284 1 : newarg[3]=NULL;
285 1 : temp=cmd_exec(opaque);
286 :
287 1 : newarg[0]="/bin/mkdir";
288 1 : newarg[1]="-p";
289 1 : newarg[2]=(char *)basePATH;
290 1 : newarg[3]=NULL;
291 1 : temp=cmd_exec(opaque);
292 :
293 : // if file does not exists: ok
294 1 : exitcode=check_namespace(opaque, basePATH);
295 : //printf("%s\n",output_buf);
296 2 : ck_assert_int_eq(exitcode,0);
297 : //reset output_buf & exitcode
298 1 : clear_dynamic_trace(trace);
299 1 : exitcode=0;
300 : // write name space, existing dir and no file
301 1 : exitcode=write_namespace(opaque, basePATH);
302 2 : ck_assert_int_eq(exitcode,0);
303 : //printf("%s\n",output_buf);
304 2 : ck_assert_int_ne(access( filepath, F_OK ),-1);
305 : //reset output_buf & exitcode
306 1 : clear_dynamic_trace(trace);
307 1 : exitcode=0;
308 : // if file exists and content same as CI_PROJECT_URL=> ok
309 1 : exitcode=check_namespace(opaque, basePATH);
310 : // printf("%s\n",output_buf);
311 2 : ck_assert_int_eq(exitcode,0);
312 : //reset output_buf & exitcode
313 1 : clear_dynamic_trace(trace);
314 1 : exitcode=0;
315 1 : cJSON_Delete(data_exchange.env_json);
316 1 : data_exchange.env_json=cJSON_CreateObject();
317 1 : cJSON_AddItemToObject(data_exchange.env_json,"CI_PROJECT_URL" , cJSON_CreateString("CI_PROJECT_URL_data_wrong" ));
318 :
319 : // if file exists and content not same as CI_PROJECT_URL=> NOK
320 1 : exitcode=check_namespace(opaque, basePATH);
321 : //printf("%s\n",output_buf);
322 2 : ck_assert_int_ne(exitcode,0);
323 : //reset output_buf & exitcode
324 1 : clear_dynamic_trace(trace);
325 1 : exitcode=0;
326 :
327 : // can't write file
328 1 : newarg[0]="/bin/rm";
329 1 : newarg[1]="-rf";
330 1 : newarg[2]=(char *)basePATH;
331 1 : newarg[3]=NULL;
332 1 : temp=cmd_exec(opaque);
333 1 : exitcode=check_namespace(opaque, basePATH);
334 : //printf("%s\n",output_buf);
335 2 : ck_assert_int_eq(exitcode,0);
336 1 : exitcode=0;
337 1 : clear_dynamic_trace(trace);
338 : // dir does not exists should be OK, not for name space to check
339 1 : exitcode=check_namespace(opaque, basePATH);
340 : //printf("%s\n",output_buf);
341 2 : ck_assert_int_eq(exitcode,0);
342 : //reset output_buf & exitcode
343 1 : clear_dynamic_trace(trace);
344 1 : exitcode=0;
345 :
346 1 : cJSON_Delete(data_exchange.env_json);
347 1 : free_dynamic_trace(&trace);
348 : }
349 1 : END_TEST
350 :
351 4 : Suite * http_config_suite(void)
352 : {
353 : Suite *s;
354 : TCase *tc_core;
355 : //TCase *tc_progress;
356 4 : s = suite_create("test_http_config");
357 : /* Core test case */
358 4 : tc_core = tcase_create("Core");
359 4 : tcase_add_checked_fixture(tc_core, NULL,NULL);
360 : //tcase_add_unchecked_fixture(tc_core, setup, teardown);
361 4 : tcase_set_timeout(tc_core,2);
362 4 : tcase_add_test(tc_core, check_url_check);
363 4 : tcase_add_test(tc_core, check_http_config_op);
364 4 : tcase_add_test(tc_core, check_namespace_op);
365 :
366 4 : suite_add_tcase(s, tc_core);
367 4 : return s;
368 : }
369 :
370 4 : int main(void)
371 : {
372 : int number_failed;
373 : Suite *s;
374 : SRunner *sr;
375 :
376 4 : s = http_config_suite();
377 4 : sr = srunner_create(s);
378 4 : srunner_run_all(sr, CK_VERBOSE );
379 1 : number_failed = srunner_ntests_failed(sr);
380 1 : srunner_free(sr);
381 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
382 : }
|