Add --usage/--help commandline options
[sdk/target/sdbd.git] / test / test_commandline_sdbd.c
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "commandline_sdbd.h"
18 #include "sdb.h"
19
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23
24 #include <check.h>
25
26 /*!
27  * @define print_nullable(s)
28  * Takes string (<tt>const char *</tt>) and returns it or "(null)" literal
29  * in case \c s is NULL.
30  */
31 #define print_nullable(s) \
32         (((s) == NULL) ? "(null)" : (s))
33
34
35 /*!
36  * @define ck_hostport(hp, h, p)
37  * Check if HostPort contains given host and port
38  *
39  * Host strings are equal if both point to the same address (including NULL)
40  * or, provided none of them is NULL, if strcmp() == 0.
41  *
42  * @param hp \ref HostPort to be checked (<tt>const HostPort *</tt>)
43  * @param h hostname (<tt>const char *</tt>) to be checked against
44  * @param p port (\c int) to be checked against
45  */
46 #define _ck_hostport(hp, h, p) \
47         ( (((hp)->host == (h)) \
48                         || (((hp)->host) && (h) && (strcmp((hp)->host, (h)) == 0))) \
49                 && (hp)->port == (p) )
50
51
52 /*!
53  * @define ck_assert_hostport_eq(hp,h,p)
54  * Makes assertion against HostPort containing given host and port
55  *
56  * @param hp \ref HostPort to be checked (<tt>const HostPort *</tt>)
57  * @param h hostname (<tt>const char *</tt>) to be checked against
58  * @param p port (\c int) to be checked against
59  *
60  * @see ck_hostport
61  */
62 #define ck_assert_hostport_eq(hp,h,p) \
63         (fail_unless(_ck_hostport(hp,h,p), "Assertion failed (%s,%d) != (%s, %d)", \
64                         print_nullable((hp)->host), (hp)->port, print_nullable(h), (p)))
65
66
67 #define array_size(a) \
68     (sizeof(a) / sizeof((a)[0]))
69
70
71 void setup(void) {
72
73 }
74
75 void teardown(void) {
76
77 }
78
79
80 START_TEST(test_all_opts) {
81         char *test_argv[] = {
82                         "./test",
83                         "--emulator=tizen:101",
84                         "--listen-port=101",
85                         "--sensors=localhost:103",
86                         "--connect-to=localhost:99"
87         };
88
89         SdbdCommandlineArgs sdbd_args = {0};
90
91         apply_sdbd_commandline_defaults(&sdbd_args);
92         int parse_res = parse_sdbd_commandline(&sdbd_args, array_size(test_argv), test_argv);
93
94         if (parse_res != SDBD_COMMANDLINE_SUCCESS) {
95                 ck_abort_msg("parsing commandline failed");
96                 return;
97         }
98
99         ck_assert_hostport_eq(&sdbd_args.emulator, "tizen", 101);
100         ck_assert_hostport_eq(&sdbd_args.sensors, "localhost", 103);
101         ck_assert_hostport_eq(&sdbd_args.sdb, "localhost", 99);
102         ck_assert_int_eq(sdbd_args.sdbd_port, 101);
103
104 } END_TEST
105
106
107 START_TEST(test_empty) {
108         char *test_argv[] = {
109                         "./test"
110         };
111
112         SdbdCommandlineArgs sdbd_args = {0};
113
114         int parse_res = parse_sdbd_commandline(&sdbd_args, array_size(test_argv), test_argv);
115
116         if (parse_res != SDBD_COMMANDLINE_SUCCESS) {
117                 ck_abort_msg("parsing commandline failed");
118                 return;
119         }
120
121         /* Now check if sdbd_commandline_args was not tainted */
122         SdbdCommandlineArgs zero_args;
123         memset(&zero_args, 0, sizeof(SdbdCommandlineArgs));
124         if (memcmp(&sdbd_args, &zero_args, sizeof(SdbdCommandlineArgs)) != 0) {
125                 ck_abort_msg("SdbdCommandlineArgs is tainted");
126         }
127
128 } END_TEST
129
130
131 START_TEST(test_unknown) {
132         char *test_argv[] = {
133                         "./test",
134                         "--emulator=tizen:26101",
135                         "--unknown=true"
136         };
137
138         SdbdCommandlineArgs sdbd_args = {0};
139
140         int parse_res = parse_sdbd_commandline(&sdbd_args, array_size(test_argv), test_argv);
141
142         if (parse_res != SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT) {
143                 ck_abort_msg("parsing commandline failed");
144                 return;
145         }
146
147 } END_TEST
148
149
150 START_TEST(test_clear_args) {
151         SdbdCommandlineArgs sdbd_args = {0};
152
153         sdbd_args.emulator.host = strdup("emul_host");
154         sdbd_args.emulator.port = 123456;
155         sdbd_args.sdb.host = strdup("sdb_host");
156         sdbd_args.sdb.port = 623451;
157         sdbd_args.sensors.host = strdup("sdb_host");
158         sdbd_args.sensors.port = 634512;
159         sdbd_args.sdbd_port = 543216;
160
161         clear_sdbd_commandline_args(&sdbd_args);
162
163         ck_assert_hostport_eq(&sdbd_args.emulator, NULL, 0);
164         ck_assert_hostport_eq(&sdbd_args.sensors, NULL, 0);
165         ck_assert_hostport_eq(&sdbd_args.sdb, NULL, 0);
166         ck_assert_int_eq(sdbd_args.sdbd_port, 0);
167 } END_TEST
168
169
170 START_TEST(test_double_clear) {
171     SdbdCommandlineArgs sdbd_args = {0};
172     clear_sdbd_commandline_args(&sdbd_args);
173 } END_TEST
174
175
176 START_TEST(test_default_args) {
177         SdbdCommandlineArgs sdbd_args = {0};
178
179         apply_sdbd_commandline_defaults(&sdbd_args);
180
181         ck_assert_hostport_eq(&sdbd_args.emulator, NULL, 0);
182         ck_assert_hostport_eq(&sdbd_args.sensors, QEMU_FORWARD_IP, DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT);
183         ck_assert_hostport_eq(&sdbd_args.sdb, QEMU_FORWARD_IP, DEFAULT_SDB_PORT);
184         ck_assert_int_eq(sdbd_args.sdbd_port, DEFAULT_SDB_LOCAL_TRANSPORT_PORT);
185 } END_TEST
186
187
188 START_TEST(test_usage_message) {
189     FILE *stream;
190     char *buffer = NULL;
191     size_t buf_len = 0;
192
193     stream = open_memstream(&buffer, &buf_len);
194     print_sdbd_usage_message(stream);
195     fclose(stream);
196
197     // Just check if all options are mentioned in usage message
198     ck_assert(strstr(buffer, "--"ARG_EMULATOR_VM_NAME) != NULL);
199     ck_assert(strstr(buffer, "--"ARG_SDBD_LISTEN_PORT) != NULL);
200     ck_assert(strstr(buffer, "--"ARG_SDB) != NULL);
201     ck_assert(strstr(buffer, "--"ARG_SENSORS) != NULL);
202     ck_assert(strstr(buffer, "--"ARG_HELP) != NULL);
203     ck_assert(strstr(buffer, "--"ARG_USAGE) != NULL);
204
205     free(buffer);
206 } END_TEST
207
208 START_TEST(test_usage) {
209     char *test_argv[] = {
210             "./test",
211             "--usage"
212     };
213
214     SdbdCommandlineArgs sdbd_args = {0};
215
216     int parse_res = parse_sdbd_commandline(&sdbd_args, array_size(test_argv), test_argv);
217     ck_assert_int_eq(parse_res, SDBD_COMMANDLINE_USAGE);
218
219 } END_TEST
220
221
222 START_TEST(test_help) {
223     char *test_argv[] = {
224             "./test",
225             "--help"
226     };
227
228     SdbdCommandlineArgs sdbd_args = {0};
229
230     int parse_res = parse_sdbd_commandline(&sdbd_args, array_size(test_argv), test_argv);
231     ck_assert_int_eq(parse_res, SDBD_COMMANDLINE_HELP);
232
233 } END_TEST
234
235
236 START_TEST(test_help_other_opt) {
237     char *test_argv[] = {
238             "./test",
239             "--listen-port=1234",
240             "--help"
241     };
242
243     SdbdCommandlineArgs sdbd_args = {0};
244
245     int parse_res = parse_sdbd_commandline(&sdbd_args, array_size(test_argv), test_argv);
246     ck_assert_int_eq(parse_res, SDBD_COMMANDLINE_HELP);
247
248 } END_TEST
249
250
251 Suite *sdbd_commandline_suite (void) {
252         Suite *s = suite_create ("sdbd commandline");
253
254         TCase *tc_core = tcase_create ("Core");
255         tcase_add_checked_fixture(tc_core, setup, teardown);
256         tcase_add_test (tc_core, test_all_opts);
257         tcase_add_test (tc_core, test_empty);
258         tcase_add_test (tc_core, test_unknown);
259         tcase_add_test (tc_core, test_clear_args);
260         tcase_add_test (tc_core, test_double_clear);
261         tcase_add_test (tc_core, test_default_args);
262         tcase_add_test (tc_core, test_usage_message);
263         tcase_add_test (tc_core, test_usage);
264         tcase_add_test (tc_core, test_help);
265         tcase_add_test (tc_core, test_help_other_opt);
266         suite_add_tcase (s, tc_core);
267
268         return s;
269 }
270
271
272 int run_tests(int print_output) {
273         int number_failed;
274         Suite *s = sdbd_commandline_suite();
275         SRunner *sr = srunner_create (s);
276         srunner_run_all (sr, print_output);
277         number_failed = srunner_ntests_failed (sr);
278         srunner_free (sr);
279         return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
280 }
281
282
283 #ifndef COMMANDLINE_SDBD_TESTS_NO_MAIN
284 int main(int argc, char *argv[]) {
285         return run_tests(CK_NORMAL);
286 }
287 #endif /* COMMANDLINE_SDBD_TESTS_NO_MAIN */