2 #include "opensrf/transport_message.h"
4 transport_message *a_message;
6 //Set up the test fixture
8 a_message = message_init("body", "subject", "thread", "recipient", "sender");
11 //Clean up the test fixture
13 message_free(a_message);
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");
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");
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");
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");
112 START_TEST(test_transport_message_new_message_from_xml_populated)
113 const char* xml_jabber_msg =
114 "<message from=\"sender\" to=\"receiver\" 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>";
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");
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");
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");
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");
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");
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");
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");
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);
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 fail_unless(strcmp(a_message->msg_xml, "<message to=\"recipient\" from=\"sender\" router_from=\"routerfrom\" router_to=\"routerto\" router_class=\"routerclass\" router_command=\"routercommand\" osrf_xid=\"osrfxid\" broadcast=\"1\"><error type=\"errortype\" code=\"123\"/><thread>thread</thread><subject>subject</subject><body>body</body></message>") == 0,
203 "message_prepare_xml should store the correct xml in msg->msg_xml");
206 START_TEST(test_transport_message_jid_get_username)
208 char buffer[buf_size];
209 jid_get_username("testuser@domain.com/stuff", buffer, buf_size);
210 fail_unless(strcmp(buffer, "testuser") == 0,
211 "jid_get_username should set the buffer to the username extracted from the jid arg");
214 START_TEST(test_transport_message_jid_get_resource)
216 char buffer[buf_size];
217 jid_get_resource("testuser@domain.com/stuff", buffer, buf_size);
218 fail_unless(strcmp(buffer, "stuff") == 0,
219 "jid_get_resource should set the buffer to the resource extracted from the jid arg");
220 jid_get_resource("testuser@domain.com", buffer, buf_size);
221 fail_unless(strcmp(buffer, "") == 0,
222 "jid_get_resource should set the buffer to an empty string if there is no resource");
225 START_TEST(test_transport_message_jid_get_domain)
227 char buffer[buf_size];
228 jid_get_domain("testuser@domain.com/stuff", buffer, buf_size);
229 fail_unless(strcmp(buffer, "domain.com") == 0,
230 "jid_get_domain should set the buffer to the domain extracted from the jid arg");
231 jid_get_domain("ksdljflksd", buffer, buf_size);
232 fail_unless(strcmp(buffer, "") == 0,
233 "jid_get_domain should set the buffer to an empty string if the jid is malformed");
236 START_TEST(test_transport_message_set_msg_error)
237 set_msg_error(a_message, NULL, 111);
238 fail_unless(a_message->is_error == 1,
239 "set_msg_error called with a NULL error type should only set msg->is_error to 1");
240 set_msg_error(a_message, "fatal", 123);
241 fail_unless(a_message->is_error == 1,
242 "set_msg_error should set msg->is_error to 1");
243 fail_unless(strcmp(a_message->error_type, "fatal") == 0,
244 "set_msg_error should set msg->error_type if error_type arg is passed");
245 fail_unless(a_message->error_code == 123,
246 "set_msg_error should set msg->error_code to the value of the err_code arg");
250 Suite *transport_message_suite(void) {
251 //Create test suite, test case, initialize fixture
252 Suite *s = suite_create("transport_message");
253 TCase *tc_core = tcase_create("Core");
254 tcase_add_checked_fixture(tc_core, setup, teardown);
256 //Add tests to test case
257 tcase_add_test(tc_core, test_transport_message_init_empty);
258 tcase_add_test(tc_core, test_transport_message_init_populated);
259 tcase_add_test(tc_core, test_transport_message_new_message_from_xml_empty);
260 tcase_add_test(tc_core, test_transport_message_new_message_from_xml_populated);
261 tcase_add_test(tc_core, test_transport_message_set_osrf_xid);
262 tcase_add_test(tc_core, test_transport_message_set_router_info_empty);
263 tcase_add_test(tc_core, test_transport_message_set_router_info_populated);
264 tcase_add_test(tc_core, test_transport_message_free);
265 tcase_add_test(tc_core, test_transport_message_prepare_xml);
266 tcase_add_test(tc_core, test_transport_message_jid_get_username);
267 tcase_add_test(tc_core, test_transport_message_jid_get_resource);
268 tcase_add_test(tc_core, test_transport_message_jid_get_domain);
269 tcase_add_test(tc_core, test_transport_message_set_msg_error);
271 //Add test case to test suite
272 suite_add_tcase(s, tc_core);
277 void run_tests(SRunner *sr) {
278 srunner_add_suite(sr, transport_message_suite());