2 #include "opensrf/transport_client.h"
4 transport_client *a_client;
5 transport_message *a_message;
7 //Set up the test fixture
9 a_client = client_init("server", 1234, "unixpath", 123);
10 a_message = message_init("body", "subject", "thread", "recipient", "sender");
13 //Clean up the test fixture
19 // Stub functions to simulate behavior of transport_session functions used in
20 // transport_client.c (to isolate system under test)
23 * init_transport() returns a new transport_session object - just return an
24 * empty one for the purpose of testing transport_client
26 transport_session* init_transport(const char* server, int port,
27 const char* unix_path, void* user_data, int component) {
29 transport_session* session = (transport_session*) safe_malloc(sizeof(transport_session));
34 * va_list_to_string takes a format and any number of arguments, and returns
35 * a formatted string of those args. Its only used once here, return what is
38 char* va_list_to_string(const char* format, ...) {
39 return "user@server/resource";
42 /* The rest of these functions return 1 or 0 depending on the result.
43 * The transport_client functions that call these are just wrappers for
44 * functions in transport_session.c
47 int session_connect(transport_session* session,
48 const char* username, const char* password,
49 const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
54 int session_disconnect(transport_session* session) {
58 int session_connected(transport_session* session) {
62 int session_send_msg(transport_session* session, transport_message* message) {
66 int session_wait(transport_session* session, int timeout) {
67 if (session == a_client->session && timeout == -1) {
68 transport_message* recvd_msg = message_init("body1", "subject1", "thread1", "recipient1", "sender1");
69 a_client->msg_q_head = recvd_msg;
72 else if (session == a_client->session && timeout > 0) {
83 START_TEST(test_transport_client_init)
84 fail_unless(client_init(NULL, 1234, "some_path", 123) == NULL,
85 "When given a NULL client arg, client_init should return NULL");
86 transport_client *test_client = client_init("server", 1234, "unixpath", 123);
87 fail_unless(test_client->msg_q_head == NULL,
88 "client->msg_q_head should be NULL on new client creation");
89 fail_unless(test_client->msg_q_tail == NULL,
90 "client->msg_q_tail should be NULL on new client creation");
91 fail_if(test_client->session == NULL,
92 "client->session should not be NULL - it is initialized by a call to init_transport");
93 //fail_unless(test_client->session->message_callback == client_message_handler,
94 // "The default message_callback function should be client_message_handler");
95 fail_unless(test_client->error == 0, "client->error should be false on new client creation");
96 fail_unless(strcmp(test_client->host, "server") == 0, "client->host should be set to the host arg");
97 fail_unless(test_client->xmpp_id == NULL, "xmpp_id should be NULL on new client creation");
100 START_TEST(test_transport_client_connect)
101 fail_unless(client_connect(NULL, "user", "password", "resource", 10, AUTH_PLAIN) == 0,
102 "Passing a NULL client to client_connect should return a failure");
103 fail_unless(client_connect(a_client, "user", "password", "resource", 10, AUTH_PLAIN) == 1,
104 "A successful call to client_connect should return a 1, provided session_connect is successful");
105 fail_unless(strcmp(a_client->xmpp_id, "user@server/resource") == 0,
106 "A successful call to client_connect should set the correct xmpp_id in the client");
109 START_TEST(test_transport_client_disconnect)
110 fail_unless(client_disconnect(NULL) == 0,
111 "client_disconnect should return 0 if no client arg is passed");
112 fail_unless(client_disconnect(a_client) == 1,
113 "client_disconnect should return 1 if successful");
116 START_TEST(test_transport_client_connected)
117 fail_unless(client_connected(NULL) == 0,
118 "client_connected should return 0 if no client arg is passed");
119 fail_unless(client_connected(a_client) == 1,
120 "client_connected should return 1 if successful");
123 START_TEST(test_transport_client_send_message)
124 fail_unless(client_send_message(NULL, a_message) == -1,
125 "client_send_message should return -1 if client arg is NULL");
127 //fail_unless(client_send_message(a_client->session, a_message") == -1,
128 //"client_send_message should return -1 if client->error is true");
130 //fail_unless(client_send_message(a_client->session, a_message) == 0,
131 //"client_send_message should return 0 on success");
132 //fail_unless(strcmp(a_message->sender, "user") == 0,
133 //"client_send_message shoud set msg->sender to the value of client->xmpp_id");
136 START_TEST(test_transport_client_recv)
138 fail_unless(client_recv(NULL, 10) == NULL,
139 "client_recv should return NULL if the client arg is NULL");
141 //Message at head of queue
142 a_client->msg_q_head = a_message; //put a message at the head of the queue
143 transport_message *msg = client_recv(a_client, 10);
145 "client_recv should return a transport_message on success");
146 fail_unless(a_client->msg_q_head == NULL,
147 "client_recv should remove the message from client->msg_q_head if it is successful");
148 fail_unless(msg->next == NULL,
149 "client_recv should set msg->next to NULL");
150 fail_unless(a_client->msg_q_tail == NULL,
151 "client_recv should set client->msg_q_tail to NULL if there was only one message in the queue");
153 //session_wait failure with no timeout
154 transport_client* other_client = client_init("server2", 4321, "unixpath2", 321);
155 transport_message *msg2 = client_recv(other_client, -1);
156 fail_unless(msg2 == NULL,
157 "client_recv should return NULL if the call to session_wait() returns an error");
158 fail_unless(other_client->error == 1,
159 "client_recv should set client->error to true if an error has occured");
161 //message in queue with no timeout
162 transport_message *msg3 = client_recv(a_client, -1);
163 fail_unless(msg3->next == NULL,
164 "client_recv should set msg->next to NULL");
165 fail_unless(a_client->msg_q_head == NULL,
166 "client_recv should set client->msg_q_head to NULL if there are no more queued messages");
167 fail_unless(a_client->msg_q_tail == NULL,
168 "client_recv should set client->msg_q_tail to NULL if client->msg_q_head was NULL");
169 fail_unless(strcmp(msg3->body, "body1") == 0,
170 "the message returned by client_recv should contain the contents of the message that was received");
171 fail_unless(strcmp(msg3->subject, "subject1") == 0,
172 "the message returned by client_recv should contain the contents of the message that was received");
173 fail_unless(strcmp(msg3->thread, "thread1") == 0,
174 "the message returned by client_recv should contain the contents of the message that was received");
175 fail_unless(strcmp(msg3->recipient, "recipient1") == 0,
176 "the message returned by client_recv should contain the contents of the message that was received");
177 fail_unless(strcmp(msg3->sender, "sender1") == 0,
178 "the message returned by client_recv should contain the contents of the message that was received");
180 //No message in queue with timeout
182 transport_message *msg4 = client_recv(a_client, 1); //only 1 sec timeout so we dont slow down testing
183 fail_unless(msg4 == NULL,
184 "client_recv should return NULL if there is no message in queue to receive");
185 fail_unless(a_client->error == 0,
186 "client->error should be 0 since there was no error");
188 //session_wait failure with timeout
189 other_client->error = 0;
190 transport_message *msg5 = client_recv(other_client, 1); //only 1 sec again...
191 fail_unless(msg5 == NULL,
192 "client_recv should return NULL if there is an error");
193 fail_unless(other_client->error == 1,
194 "client_recv should set client->error to 1 if there is an error");
197 START_TEST(test_transport_client_free)
198 fail_unless(client_free(NULL) == 0,
199 "client_free should retun 0 if passed a NULL arg");
200 transport_client* client1 = client_init("server", 1234, "unixpath", 123);
201 fail_unless(client_free(client1) == 1,
202 "client_free should return 0 if successful");
205 START_TEST(test_transport_client_discard)
206 fail_unless(client_discard(NULL) == 0,
207 "client_discard should return 0 if passed a NULL arg");
208 transport_client* client1 = client_init("server", 1234, "unixpath", 123);
209 fail_unless(client_discard(client1) == 1,
210 "client_discard should return 1 if successful");
213 START_TEST(test_transport_client_sock_fd)
214 fail_unless(client_sock_fd(NULL) == 0,
215 "client_sock_fd should return 0 if passed a NULL arg");
216 a_client->session->sock_id = 1;
217 fail_unless(client_sock_fd(a_client) == 1,
218 "client_sock_fd should return client->session->sock_id");
223 Suite *transport_client_suite(void) {
224 //Create test suite, test case, initialize fixture
225 Suite *s = suite_create("transport_client");
226 TCase *tc_core = tcase_create("Core");
227 tcase_add_checked_fixture(tc_core, setup, teardown);
229 //Add tests to test case
230 tcase_add_test(tc_core, test_transport_client_init);
231 tcase_add_test(tc_core, test_transport_client_connect);
232 tcase_add_test(tc_core, test_transport_client_disconnect);
233 tcase_add_test(tc_core, test_transport_client_connected);
234 tcase_add_test(tc_core, test_transport_client_send_message);
235 tcase_add_test(tc_core, test_transport_client_recv);
236 tcase_add_test(tc_core, test_transport_client_free);
237 tcase_add_test(tc_core, test_transport_client_discard);
238 tcase_add_test(tc_core, test_transport_client_sock_fd);
240 //Add test case to test suite
241 suite_add_tcase(s, tc_core);
246 void run_tests(SRunner *sr) {
247 srunner_add_suite(sr, transport_client_suite());