2 * Copyright (c) 2014 Samsung Electronics Co., Ltd
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "commandline_sdbd.h"
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.
31 #define print_nullable(s) \
32 (((s) == NULL) ? "(null)" : (s))
36 * @define ck_hostport(hp, h, p)
37 * Check if HostPort contains given host and port
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.
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
46 #define _ck_hostport(hp, h, p) \
47 ( (((hp)->host == (h)) \
48 || (((hp)->host) && (h) && (strcmp((hp)->host, (h)) == 0))) \
49 && (hp)->port == (p) )
53 * @define ck_assert_hostport_eq(hp,h,p)
54 * Makes assertion against HostPort containing given host and port
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
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)))
79 "--emulator=tizen:101",
81 "--sensors=localhost:103",
82 "--connect-to=localhost:99"
85 SdbdCommandlineArgs sdbd_args = {0};
87 apply_sdbd_commandline_defaults(&sdbd_args);
88 int parse_res = parse_sdbd_commandline(&sdbd_args, 5, argv);
90 if (parse_res != SDBD_COMMANDLINE_SUCCESS) {
91 ck_abort_msg("parsing commandline failed");
95 ck_assert_hostport_eq(&sdbd_args.emulator, "tizen", 101);
96 ck_assert_hostport_eq(&sdbd_args.sensors, "localhost", 103);
97 ck_assert_hostport_eq(&sdbd_args.sdb, "localhost", 99);
98 ck_assert_int_eq(sdbd_args.sdbd_port, 101);
103 START_TEST(test_empty) {
108 SdbdCommandlineArgs sdbd_args = {0};
110 int parse_res = parse_sdbd_commandline(&sdbd_args, 1, argv);
112 if (parse_res != SDBD_COMMANDLINE_SUCCESS) {
113 ck_abort_msg("parsing commandline failed");
117 /* Now check if sdbd_commandline_args was not tainted */
118 SdbdCommandlineArgs zero_args;
119 memset(&zero_args, 0, sizeof(SdbdCommandlineArgs));
120 if (memcmp(&sdbd_args, &zero_args, sizeof(SdbdCommandlineArgs)) != 0) {
121 ck_abort_msg("SdbdCommandlineArgs is tainted");
127 START_TEST(test_unknown) {
130 "--emulator=tizen:26101",
134 SdbdCommandlineArgs sdbd_args = {0};
136 int parse_res = parse_sdbd_commandline(&sdbd_args, 3, argv);
138 if (parse_res != SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT) {
139 ck_abort_msg("parsing commandline failed");
146 START_TEST(test_clear_args) {
147 SdbdCommandlineArgs sdbd_args = {0};
149 sdbd_args.emulator.host = strdup("emul_host");
150 sdbd_args.emulator.port = 123456;
151 sdbd_args.sdb.host = strdup("sdb_host");
152 sdbd_args.sdb.port = 623451;
153 sdbd_args.sensors.host = strdup("sdb_host");
154 sdbd_args.sensors.port = 634512;
155 sdbd_args.sdbd_port = 543216;
157 clear_sdbd_commandline_args(&sdbd_args);
159 ck_assert_hostport_eq(&sdbd_args.emulator, NULL, 0);
160 ck_assert_hostport_eq(&sdbd_args.sensors, NULL, 0);
161 ck_assert_hostport_eq(&sdbd_args.sdb, NULL, 0);
162 ck_assert_int_eq(sdbd_args.sdbd_port, 0);
166 START_TEST(test_double_clear) {
167 SdbdCommandlineArgs sdbd_args = {0};
168 clear_sdbd_commandline_args(&sdbd_args);
172 START_TEST(test_default_args) {
173 SdbdCommandlineArgs sdbd_args = {0};
175 apply_sdbd_commandline_defaults(&sdbd_args);
177 ck_assert_hostport_eq(&sdbd_args.emulator, NULL, 0);
178 ck_assert_hostport_eq(&sdbd_args.sensors, QEMU_FORWARD_IP, DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT);
179 ck_assert_hostport_eq(&sdbd_args.sdb, QEMU_FORWARD_IP, DEFAULT_SDB_PORT);
180 ck_assert_int_eq(sdbd_args.sdbd_port, DEFAULT_SDB_LOCAL_TRANSPORT_PORT);
184 Suite *sdbd_commandline_suite (void) {
185 Suite *s = suite_create ("sdbd commandline");
187 TCase *tc_core = tcase_create ("Core");
188 tcase_add_checked_fixture(tc_core, setup, teardown);
189 tcase_add_test (tc_core, test_ok);
190 tcase_add_test (tc_core, test_empty);
191 tcase_add_test (tc_core, test_unknown);
192 tcase_add_test (tc_core, test_clear_args);
193 tcase_add_test (tc_core, test_double_clear);
194 tcase_add_test (tc_core, test_default_args);
195 suite_add_tcase (s, tc_core);
201 int run_tests(int print_output) {
203 Suite *s = sdbd_commandline_suite();
204 SRunner *sr = srunner_create (s);
205 srunner_run_all (sr, print_output);
206 number_failed = srunner_ntests_failed (sr);
208 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
212 #ifndef COMMANDLINE_SDBD_TESTS_NO_MAIN
213 int main(int argc, char *argv[]) {
214 return run_tests(CK_NORMAL);
216 #endif /* COMMANDLINE_SDBD_TESTS_NO_MAIN */