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