Line data Source code
1 : /*
2 : test_letsencrypt.c
3 : Created by Danny Goossen, Gioxa Ltd on 28/3/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 : #include <check.h>
30 : #include "../src/deployd.h"
31 :
32 : int cmd_exec(void * opaque);
33 :
34 : typedef struct test_stubs_s
35 : {
36 : char * arg0;
37 : char * arg1;
38 : char * arg2;
39 : char * arg3;
40 : int exitcode;
41 : } test_stubs_t;
42 :
43 :
44 : // stubfunctions:
45 0 : int cmd_exec(void * opaque)
46 : {
47 0 : test_stubs_t * teststubs= (test_stubs_t *)((data_exchange_t *)opaque)->trace->tests;
48 0 : ck_assert_str_eq(teststubs->arg0, ((data_exchange_t *)opaque)->paramlist[0]);
49 0 : ck_assert_str_eq(teststubs->arg1, ((data_exchange_t *)opaque)->paramlist[1]);
50 0 : ck_assert_str_eq(teststubs->arg2, ((data_exchange_t *)opaque)->paramlist[2]);
51 0 : return(teststubs->exitcode);
52 : }
53 :
54 2 : int exec_color(void * opaque)
55 : {
56 2 : test_stubs_t * teststubs= (test_stubs_t *)((data_exchange_t *)opaque)->trace->tests;
57 2 : return(teststubs->exitcode);
58 :
59 : }
60 :
61 : //helper function for tests
62 2 : int write_test_file(const char * filename, const char * data)
63 : {
64 2 : debug("write test file\n");
65 2 : FILE *f = fopen(filename, "w");
66 2 : if (f == NULL){ debug("Error opening file : %s\n",strerror(errno));return -1;}
67 : /* print some text */
68 : int retvalue=0;
69 : int errvalue=0;
70 2 : do{ retvalue=fprintf(f, "%s",data);} while ( retvalue==-1 && (errvalue=errno)== EINTR );
71 2 : if (retvalue<0) { debug("Error io-command %s: %s\n", filename,strerror(errvalue)); return -1; }
72 2 : if (fclose(f) <0 ) { debug("Error io-command %s: %s\n", filename,strerror(errno));return -1;}
73 : return retvalue;
74 : }
75 :
76 :
77 2 : int update_details(void * userp)
78 : {
79 2 : return 0;
80 : }
81 :
82 1 : START_TEST(check_lets_encrypt)
83 : {
84 :
85 : data_exchange_t data_exchange;
86 : parameter_t parameters;
87 1 : void * opaque=&data_exchange;
88 : char * newarg[4];
89 :
90 1 : struct trace_Struct * trace=NULL;
91 1 : init_dynamic_trace( &trace,"token","url",1);
92 1 : data_exchange.trace=trace;
93 :
94 1 : data_exchange.needenvp=0;
95 : //data_exchange.gid=getgid();
96 : //data_exchange.uid=geteuid() ;
97 : //setdebug();
98 1 : cJSON * env_json= cJSON_CreateObject();
99 1 : ck_assert_ptr_ne(env_json,NULL);
100 1 : data_exchange.env_json=env_json;
101 :
102 1 : data_exchange.paramlist=(char **)newarg;
103 1 : data_exchange.timeout=1;
104 1 : data_exchange.parameters=¶meters;
105 1 : parameters.testprefix="./test_letsencrypt_d";
106 : test_stubs_t test_stubs;
107 1 : trace->tests=&test_stubs;
108 : // set results for tests
109 1 : test_stubs.exitcode=0;
110 1 : test_stubs.arg0="/bin/sh";
111 1 : test_stubs.arg1="-c";
112 1 : test_stubs.arg2="/opt/deploy/.acme.sh/acme.sh --issue --keylength 4096 --home /opt/deploy/.acme.sh -w /opt/deploy/var -d test.com"; //--email test@test.com
113 1 : test_stubs.arg3=NULL;
114 1 : const char * domain_dir="./test_letsencrypt_d/opt/deploy/.acme.sh/test.com/";
115 1 : const char * cert_filename="./test_letsencrypt_d/opt/deploy/.acme.sh/test.com/fullchain.cer";
116 1 : const char * key_filename="./test_letsencrypt_d/opt/deploy/.acme.sh/test.com/test.com.key";
117 :
118 1 : _rmdir("./test_letsencrypt_d","");
119 :
120 :
121 1 : int result=letsencrypt(opaque, "test.com", "test@test.com");
122 2 : ck_assert_int_ne(result, 0);
123 : //const char * cert_missing="+ /bin/sh -c /usr/bin/certbot certonly --no-self-upgrade --non-interactive --agree-tos --rsa-key-size 4096 --webroot -w /opt/deploy/var --email test@test.com -d test.com \nERROR: certificate missing\n";
124 : //ck_assert_str_eq(output_buf,cert_missing);
125 1 : debug("%s\n",get_dynamic_trace(trace));
126 1 : clear_dynamic_trace(trace);
127 :
128 1 : _mkdir("/opt/deploy/.acme.sh/test.com/","./test_letsencrypt_d");
129 :
130 : // add cert, should report missing key
131 1 : write_test_file(key_filename, "key");
132 1 : write_test_file(cert_filename, "cert");
133 :
134 : // add cert and key so should return OK
135 :
136 1 : result=letsencrypt(opaque, "test.com", "test@test.com");
137 2 : ck_assert_int_eq(result, 0);
138 : //const char * result_ok="+ /bin/sh -c /usr/bin/certbot certonly --no-self-upgrade --non-interactive --agree-tos --rsa-key-size 4096 --webroot -w /opt/deploy/var --email test@test.com -d test.com \n";
139 : //ck_assert_str_eq(output_buf,result_ok);
140 :
141 1 : debug("%s\n",get_dynamic_trace(trace));
142 1 : clear_dynamic_trace(trace);
143 : // now set prefix and make dir with exec mkdir -p ..... and create cert
144 : // and then key
145 1 : _rmdir("./test_letsencrypt_d","");
146 1 : free_dynamic_trace(&trace);
147 : }
148 1 : END_TEST
149 :
150 2 : Suite * externals_suite(void)
151 : {
152 : Suite *s;
153 : TCase *tc_core;
154 : //TCase *tc_progress;
155 2 : s = suite_create("test_externals");
156 : /* Core test case */
157 2 : tc_core = tcase_create("Core");
158 2 : tcase_add_checked_fixture(tc_core, NULL,NULL);
159 : //tcase_add_unchecked_fixture(tc_core, setup, teardown);
160 2 : tcase_set_timeout(tc_core,65);
161 2 : tcase_add_test(tc_core, check_lets_encrypt);
162 :
163 2 : suite_add_tcase(s, tc_core);
164 2 : return s;
165 : }
166 :
167 2 : int main(void)
168 : {
169 : int number_failed;
170 : Suite *s;
171 : SRunner *sr;
172 :
173 2 : s = externals_suite();
174 2 : sr = srunner_create(s);
175 2 : srunner_run_all(sr, CK_VERBOSE | CK_NOFORK);
176 1 : number_failed = srunner_ntests_failed(sr);
177 1 : srunner_free(sr);
178 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
179 : }
|