Parse commandline of sdbd and make use of values provided
[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 void setup(void) {
68
69 }
70
71 void teardown(void) {
72
73 }
74
75
76 START_TEST(test_ok) {
77         char *argv[] = {
78                         "./test",
79                         "--emulator=tizen:101",
80                         "--listen-port=101",
81                         "--sensors=localhost:103",
82                         "--connect-to=localhost:99"
83         };
84
85         SdbdCommandlineArgs sdbd_args = {0};
86
87         apply_sdbd_commandline_defaults(&sdbd_args);
88         int parse_res = parse_sdbd_commandline(&sdbd_args, 5, argv);
89
90         if (parse_res != SDBD_COMMANDLINE_SUCCESS) {
91                 ck_abort_msg("parsing commandline failed");
92                 return;
93         }
94
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);
99
100 } END_TEST
101
102
103 START_TEST(test_empty) {
104         char *argv[] = {
105                         "./test"
106         };
107
108         SdbdCommandlineArgs sdbd_args = {0};
109
110         int parse_res = parse_sdbd_commandline(&sdbd_args, 1, argv);
111
112         if (parse_res != SDBD_COMMANDLINE_SUCCESS) {
113                 ck_abort_msg("parsing commandline failed");
114                 return;
115         }
116
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");
122         }
123
124 } END_TEST
125
126
127 START_TEST(test_unknown) {
128         char *argv[] = {
129                         "./test",
130                         "--emulator=tizen:26101",
131                         "--unknown=true"
132         };
133
134         SdbdCommandlineArgs sdbd_args = {0};
135
136         int parse_res = parse_sdbd_commandline(&sdbd_args, 3, argv);
137
138         if (parse_res != SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT) {
139                 ck_abort_msg("parsing commandline failed");
140                 return;
141         }
142
143 } END_TEST
144
145
146 START_TEST(test_clear_args) {
147         SdbdCommandlineArgs sdbd_args = {0};
148
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;
156
157         clear_sdbd_commandline_args(&sdbd_args);
158
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);
163 } END_TEST
164
165
166 START_TEST(test_double_clear) {
167     SdbdCommandlineArgs sdbd_args = {0};
168     clear_sdbd_commandline_args(&sdbd_args);
169 } END_TEST
170
171
172 START_TEST(test_default_args) {
173         SdbdCommandlineArgs sdbd_args = {0};
174
175         apply_sdbd_commandline_defaults(&sdbd_args);
176
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);
181 } END_TEST
182
183
184 Suite *sdbd_commandline_suite (void) {
185         Suite *s = suite_create ("sdbd commandline");
186
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);
196
197         return s;
198 }
199
200
201 int run_tests(int print_output) {
202         int number_failed;
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);
207         srunner_free (sr);
208         return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
209 }
210
211
212 #ifndef COMMANDLINE_SDBD_TESTS_NO_MAIN
213 int main(int argc, char *argv[]) {
214         return run_tests(CK_NORMAL);
215 }
216 #endif /* COMMANDLINE_SDBD_TESTS_NO_MAIN */