]> git.evergreen-ils.org Git - OpenSRF.git/blob - tests/check_transport_message.c
LP1999823: Bump libtool library version
[OpenSRF.git] / tests / check_transport_message.c
1 #include <check.h>
2 #include "opensrf/transport_message.h"
3
4 transport_message *a_message; 
5
6 //Set up the test fixture
7 void setup(void) {
8   a_message = message_init("body", "subject", "thread", "recipient", "sender");
9 }
10
11 //Clean up the test fixture
12 void teardown(void) {
13   message_free(a_message);
14 }
15
16 //BEGIN TESTS
17
18 START_TEST(test_transport_message_init_empty)
19 {
20   transport_message* empty_message = message_init(NULL, NULL, NULL, NULL, NULL);
21   fail_if(empty_message == NULL, "transport_message wasn't created");
22   fail_unless(strcmp(empty_message->body, "") == 0,
23       "When calling message_init, an NULL body arg should yield an empty string");
24   fail_unless(strcmp(empty_message->thread, "") == 0,
25       "When calling message_init, an NULL thread arg should yield an empty string");
26   fail_unless(strcmp(empty_message->subject, "") == 0,
27       "When calling message_init, an NULL subject arg should yield an empty string");
28   fail_unless(strcmp(empty_message->recipient, "") == 0,
29       "When calling message_init, an NULL recipient arg should yield an empty string");
30   fail_unless(strcmp(empty_message->sender, "") == 0,
31       "When calling message_init, an NULL sender arg should yield an empty string");
32   fail_unless(empty_message->router_from == NULL,
33       "message_init should set the router_from field to NULL");
34   fail_unless(empty_message->router_to == NULL,
35       "message_init should set the router_to field to NULL");
36   fail_unless(empty_message->router_class == NULL,
37       "message_init should set the router_class field to NULL");
38   fail_unless(empty_message->router_command == NULL,
39       "message_init should set the router_command field to NULL");
40   fail_unless(empty_message->osrf_xid == NULL,
41       "message_init should set the osrf_xid field to NULL");
42   fail_unless(empty_message->is_error == 0,
43       "message_init should set the is_error field to 0");
44   fail_unless(empty_message->error_type == NULL,
45       "message_init should set the error_type field to NULL");
46   fail_unless(empty_message->error_code == 0,
47       "message_init should set the error_code field to 0");
48   fail_unless(empty_message->broadcast == 0,
49       "message_init should set the broadcast field to 0");
50   fail_unless(empty_message->msg_xml == NULL,
51       "message_init should set the msg_xml field to NULL");
52   fail_unless(empty_message->next == NULL,
53       "message_init should set the next field to NULL");
54 }
55 END_TEST
56
57 START_TEST(test_transport_message_init_populated)
58 {
59   fail_if(a_message == NULL, "transport_message wasn't created");
60   fail_unless(strcmp(a_message->body, "body") == 0,
61       "When calling message_init, an body arg should be stored in the body field");
62   fail_unless(strcmp(a_message->thread, "thread") == 0,
63       "When calling message_init, an thread arg should be stored in the thread field");
64   fail_unless(strcmp(a_message->subject, "subject") == 0,
65       "When calling message_init, a subject arg should be stored in the subject field");
66   fail_unless(strcmp(a_message->recipient, "recipient") == 0,
67       "When calling message_init, a recipient arg should be stored in the recipient field");
68   fail_unless(strcmp(a_message->sender, "sender") == 0,
69       "When calling message_init, a sender arg should be stored in the sender field");
70 }
71 END_TEST
72
73 START_TEST(test_transport_message_new_message_from_xml_empty)
74 {
75   fail_unless(new_message_from_xml(NULL) == NULL,
76       "Passing NULL to new_message_from_xml should return NULL");
77   fail_unless(new_message_from_xml("\0") == NULL,
78       "Passing a NULL string to new_message_from_xml should return NULL");
79
80   const char* empty_msg = "<message/>";
81   transport_message* t_msg = new_message_from_xml(empty_msg);
82   fail_if(t_msg == NULL,
83       "new_message_from_xml should create a new transport_message");
84   fail_unless(strcmp(t_msg->thread, "") == 0,
85       "When passed no thread, msg->thread should be set to an empty string");
86   fail_unless(strcmp(t_msg->subject, "") == 0,
87       "When passed no subject, msg->subject should be set to an empty string");
88   fail_unless(strcmp(t_msg->body, "") == 0,
89       "When passed no body, msg->body should be set to an empty string");
90   fail_unless(t_msg->recipient == NULL,
91       "When passed no recipient, msg->recipient should be NULL");
92   fail_unless(t_msg->sender == NULL,
93       "When passed no sender, msg->sender should be NULL");
94   fail_unless(t_msg->router_from == NULL,
95       "When passed no router_from, msg->router_from should be NULL");
96   fail_unless(t_msg->router_to == NULL,
97       "When passed no router_to, msg->router_to should be NULL");
98   fail_unless(t_msg->router_class == NULL,
99       "When passed no router_class, msg->router_class should be NULL");
100   fail_unless(t_msg->router_command == NULL,
101       "router_command should never be passed, and therefore should be NULL");
102   fail_unless(t_msg->osrf_xid == NULL,
103       "When passed no osrf_xid, msg->osrf_xid should be NULL");
104   fail_unless(t_msg->is_error == 0,
105       "is_error should never be passed, and msg->is_error should be set to 0");
106   fail_unless(t_msg->error_type == NULL,
107       "error_type should never be passed, and should be NULL");
108   fail_unless(t_msg->error_code == 0,
109       "error_code should never be passed, and msg->error_code should be set to 0");
110   fail_unless(t_msg->broadcast == 0,
111       "When passed no broadcast, msg->broadcast should be set to 0");
112   fail_unless(strcmp(t_msg->msg_xml, "<message/>") == 0,
113       "msg->msg_xml should contain the contents of the original xml message");
114   fail_unless(t_msg->next == NULL, "msg->next should be set to NULL");
115 }
116 END_TEST
117
118 START_TEST(test_transport_message_new_message_from_xml_populated)
119 {
120   const char* xml_jabber_msg =
121     "<message from=\"sender\" to=\"receiver\"><opensrf router_from=\"routerfrom\" router_to=\"routerto\" router_class=\"class\" broadcast=\"1\" osrf_xid=\"xid\"/><thread>thread_value</thread><subject>subject_value</subject><body>body_value</body></message>";
122
123   transport_message *my_msg = new_message_from_xml(xml_jabber_msg);
124   fail_if(my_msg == NULL, "new_message_from_xml failed to create a transport_message");
125   fail_unless(strcmp(my_msg->sender, "routerfrom") == 0,
126       "new_message_from_xml should populate the sender field");
127   fail_unless(strcmp(my_msg->recipient, "receiver") == 0,
128       "new_message_from_xml should populate the receiver field");
129   fail_unless(strcmp(my_msg->osrf_xid, "xid") == 0,
130       "new_message_from_xml should populate the osrf_xid field");
131   fail_unless(strcmp(my_msg->router_from, "routerfrom") == 0,
132       "new_message_from_xml should populate the router_from field");
133   fail_unless(strcmp(my_msg->subject, "subject_value") == 0,
134       "new_message_from_xml should populate the subject field");
135   fail_unless(strcmp(my_msg->thread, "thread_value") == 0,
136       "new_message_from_xml should populate the thread field");
137   fail_unless(strcmp(my_msg->router_to, "routerto") == 0,
138       "new_message_from_xml should populate the router_to field");
139   fail_unless(strcmp(my_msg->router_class, "class") == 0,
140       "new_message_from_xml should populate the router_class field");
141   fail_unless(my_msg->broadcast == 1,
142       "new_message_from_xml should populate the broadcast field");
143   fail_unless(strcmp(my_msg->msg_xml, xml_jabber_msg) == 0,
144       "new_message_from_xml should store the original xml msg in msg_xml");
145 }
146 END_TEST
147
148 START_TEST(test_transport_message_set_osrf_xid)
149 {
150   message_set_osrf_xid(a_message, "abcd");
151   fail_unless(strcmp(a_message->osrf_xid, "abcd") == 0,
152       "message_set_osrf_xid should set msg->osrf_xid to the value of the osrf_xid arg");
153   message_set_osrf_xid(a_message, NULL);
154   fail_unless(strcmp(a_message->osrf_xid, "") == 0,
155       "message_set_osrf_xid should set msg->osrf_xid to an empty string if osrf_xid arg is NULL");
156 }
157 END_TEST
158
159 START_TEST(test_transport_message_set_router_info_empty)
160 {
161   message_set_router_info(a_message, NULL, NULL, NULL, NULL, 0);
162   fail_unless(strcmp(a_message->router_from, "") == 0,
163       "message_set_router_info should set msg->router_from to empty string if NULL router_from arg is passed");
164   fail_unless(strcmp(a_message->router_to, "") == 0,
165       "message_set_router_info should set msg->router_to to empty string if NULL router_to arg is passed");
166   fail_unless(strcmp(a_message->router_class, "") == 0,
167       "message_set_router_info should set msg->router_class to empty string if NULL router_class arg is passed");
168   fail_unless(strcmp(a_message->router_command, "") == 0,
169       "message_set_router_info should set msg->router_command to empty string if NULL router_command arg is passed");
170   fail_unless(a_message->broadcast == 0,
171       "message_set_router_info should set msg->broadcast to the content of the broadcast_enabled arg");
172 }
173 END_TEST
174
175 START_TEST(test_transport_message_set_router_info_populated)
176 {
177   message_set_router_info(a_message, "routerfrom", "routerto", "routerclass", "routercmd", 1);
178   fail_unless(strcmp(a_message->router_from, "routerfrom") == 0,
179       "message_set_router_info should set msg->router_from to the value of the router_from arg");
180   fail_unless(strcmp(a_message->router_to, "routerto") == 0,
181       "message_set_router_info should set msg->router_to to the value of the router_to arg");
182   fail_unless(strcmp(a_message->router_class, "routerclass") == 0,
183       "message_set_router_info should set msg->router_class to the value of the router_class arg");
184   fail_unless(strcmp(a_message->router_command, "routercmd") == 0,
185       "message_set_router_info should set msg->router_command to the value of the router_command arg");
186   fail_unless(a_message->broadcast == 1,
187       "message_set_router_info should set msg->broadcast to the value of the broadcast_enabled arg");
188 }
189 END_TEST
190
191 START_TEST(test_transport_message_free)
192 {
193   fail_unless(message_free(NULL) == 0,
194       "message_free should return 0 if passed a NULL msg arg");
195   transport_message *msg = message_init("one", "two", "three", "four", "five");
196   fail_unless(message_free(msg) == 1,
197       "message_free should return 1 if successful");
198 }
199 END_TEST
200
201 START_TEST(test_transport_message_prepare_xml)
202 {
203   fail_unless(message_prepare_xml(NULL) == 0,
204       "Passing a NULL msg arg to message_prepare_xml should return 0");
205
206   transport_message *msg = message_init(NULL,NULL,NULL,NULL,NULL);
207   msg->msg_xml = "somevalue";
208   fail_unless(message_prepare_xml(msg) == 1,
209       "If msg->msg_xml is already populated, message_prepare_xml should return 1");
210
211   message_set_router_info(a_message, "routerfrom", "routerto", "routerclass", "routercommand", 1);
212   message_set_osrf_xid(a_message, "osrfxid");
213   set_msg_error(a_message, "errortype", 123);
214
215   fail_unless(message_prepare_xml(a_message) == 1,
216       "message_prepare_xml should return 1 upon success");
217   fail_if(a_message->msg_xml == NULL,
218       "message_prepare_xml should store the returned xml in msg->msg_xml");
219
220   fail_unless(strcmp(a_message->msg_xml, "<message to=\"recipient\" from=\"sender\"><error type=\"errortype\" code=\"123\"/><opensrf router_from=\"routerfrom\" router_to=\"routerto\" router_class=\"routerclass\" router_command=\"routercommand\" osrf_xid=\"osrfxid\" broadcast=\"1\"/><thread>thread</thread><subject>subject</subject><body>body</body></message>") == 0,
221       "message_prepare_xml should store the correct xml in msg->msg_xml");
222 }
223 END_TEST
224
225 START_TEST(test_transport_message_jid_get_username)
226 {
227   int buf_size = 15;
228   char buffer[buf_size];
229   jid_get_username("testuser@domain.com/stuff", buffer, buf_size);
230   fail_unless(strcmp(buffer, "testuser") == 0,
231       "jid_get_username should set the buffer to the username extracted from the jid arg");
232 }
233 END_TEST
234
235 START_TEST(test_transport_message_jid_get_resource)
236 {
237   char buf_size = 15;
238   char buffer[buf_size];
239   jid_get_resource("testuser@domain.com/stuff", buffer, buf_size);
240   fail_unless(strcmp(buffer, "stuff") == 0,
241       "jid_get_resource should set the buffer to the resource extracted from the jid arg");
242   jid_get_resource("testuser@domain.com", buffer, buf_size);
243   fail_unless(strcmp(buffer, "") == 0,
244       "jid_get_resource should set the buffer to an empty string if there is no resource");
245 }
246 END_TEST
247
248 START_TEST(test_transport_message_jid_get_domain)
249 {
250   char buf_size = 15;
251   char buffer[buf_size];
252   jid_get_domain("testuser@domain.com/stuff", buffer, buf_size);
253   fail_unless(strcmp(buffer, "domain.com") == 0,
254       "jid_get_domain should set the buffer to the domain extracted from the jid arg");
255   jid_get_domain("ksdljflksd",  buffer, buf_size);
256   fail_unless(strcmp(buffer, "") == 0,
257       "jid_get_domain should set the buffer to an empty string if the jid is malformed");
258 }
259 END_TEST
260
261 START_TEST(test_transport_message_set_msg_error)
262 {
263   set_msg_error(a_message, NULL, 111);
264   fail_unless(a_message->is_error == 1,
265       "set_msg_error called with a NULL error type should only set msg->is_error to 1");
266   set_msg_error(a_message, "fatal", 123);
267   fail_unless(a_message->is_error == 1,
268       "set_msg_error should set msg->is_error to 1");
269   fail_unless(strcmp(a_message->error_type, "fatal") == 0,
270       "set_msg_error should set msg->error_type if error_type arg is passed");
271   fail_unless(a_message->error_code == 123,
272       "set_msg_error should set msg->error_code to the value of the err_code arg");
273 }
274 END_TEST
275 //END TESTS
276
277 Suite *transport_message_suite(void) {
278   //Create test suite, test case, initialize fixture
279   Suite *s = suite_create("transport_message");
280   TCase *tc_core = tcase_create("Core");
281   tcase_add_checked_fixture(tc_core, setup, teardown);
282
283   //Add tests to test case
284   tcase_add_test(tc_core, test_transport_message_init_empty);
285   tcase_add_test(tc_core, test_transport_message_init_populated);
286   tcase_add_test(tc_core, test_transport_message_new_message_from_xml_empty);
287   tcase_add_test(tc_core, test_transport_message_new_message_from_xml_populated);
288   tcase_add_test(tc_core, test_transport_message_set_osrf_xid);
289   tcase_add_test(tc_core, test_transport_message_set_router_info_empty);
290   tcase_add_test(tc_core, test_transport_message_set_router_info_populated);
291   tcase_add_test(tc_core, test_transport_message_free);
292   tcase_add_test(tc_core, test_transport_message_prepare_xml);
293   tcase_add_test(tc_core, test_transport_message_jid_get_username);
294   tcase_add_test(tc_core, test_transport_message_jid_get_resource);
295   tcase_add_test(tc_core, test_transport_message_jid_get_domain);
296   tcase_add_test(tc_core, test_transport_message_set_msg_error);
297
298   //Add test case to test suite
299   suite_add_tcase(s, tc_core);
300
301   return s;
302 }
303
304 void run_tests(SRunner *sr) {
305   srunner_add_suite(sr, transport_message_suite());
306 }