]> git.evergreen-ils.org Git - OpenSRF.git/blob - tests/check_transport_client.c
LP1999823: Bump libtool library version
[OpenSRF.git] / tests / check_transport_client.c
1 #include <check.h>
2 #include "opensrf/transport_client.h"
3
4 transport_client *a_client;
5 transport_message *a_message; 
6
7 //Set up the test fixture
8 void setup(void) {
9   a_client = client_init("server", 1234, "unixpath", 123);
10   a_message = message_init("body", "subject", "thread", "recipient", "sender");
11 }
12
13 //Clean up the test fixture
14 void teardown(void) {
15   free(a_client);
16   free(a_message);
17 }
18
19 // Stub functions to simulate behavior of transport_session functions used in
20 // transport_client.c (to isolate system under test)
21
22 /*
23  * init_transport() returns a new transport_session object - just return an
24  * empty one for the purpose of testing transport_client
25 */
26 transport_session* init_transport(const char* server, int port, 
27     const char* unix_path, void* user_data, int component) {
28
29   transport_session* session = (transport_session*) safe_malloc(sizeof(transport_session));
30   return session;
31 }
32
33 /*
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
36  * expected.
37 */
38 char* va_list_to_string(const char* format, ...) {
39   return "user@server/resource";
40 }
41
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
45 */
46
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 ) {
50
51   return 1;
52 }
53
54 int session_disconnect(transport_session* session) {
55   return 1;
56 }
57
58 int session_connected(transport_session* session) {
59   return 1;
60 }
61
62 int session_send_msg(transport_session* session, transport_message* message) {
63   return 0;
64 }
65
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;
70     return 0;
71   }
72   else if (session == a_client->session && timeout > 0) {
73     return 0;
74   }
75   else
76     return 1;
77 }
78
79 //End Stubs
80
81 // BEGIN TESTS
82
83 START_TEST(test_transport_client_init)
84 {
85   fail_unless(client_init(NULL, 1234, "some_path", 123) == NULL,
86       "When given a NULL client arg, client_init should return NULL");
87   transport_client *test_client = client_init("server", 1234, "unixpath", 123);
88   fail_unless(test_client->msg_q_head == NULL,
89       "client->msg_q_head should be NULL on new client creation");
90   fail_unless(test_client->msg_q_tail == NULL,
91       "client->msg_q_tail should be NULL on new client creation");
92   fail_if(test_client->session == NULL,
93       "client->session should not be NULL - it is initialized by a call to init_transport");
94   //fail_unless(test_client->session->message_callback == client_message_handler,
95   //  "The default message_callback function should be client_message_handler");
96   fail_unless(test_client->error == 0, "client->error should be false on new client creation");
97   fail_unless(strcmp(test_client->host, "server") == 0, "client->host should be set to the host arg");
98   fail_unless(test_client->xmpp_id == NULL, "xmpp_id should be NULL on new client creation");
99 }
100 END_TEST
101
102 START_TEST(test_transport_client_connect)
103 {
104   fail_unless(client_connect(NULL, "user", "password", "resource", 10, AUTH_PLAIN) == 0,
105       "Passing a NULL client to client_connect should return a failure");
106   fail_unless(client_connect(a_client, "user", "password", "resource", 10, AUTH_PLAIN) == 1,
107       "A successful call to client_connect should return a 1, provided session_connect is successful");
108   fail_unless(strcmp(a_client->xmpp_id, "user@server/resource") == 0,
109       "A successful call to client_connect should set the correct xmpp_id in the client");
110 }
111 END_TEST
112
113 START_TEST(test_transport_client_disconnect)
114 {
115   fail_unless(client_disconnect(NULL) == 0,
116       "client_disconnect should return 0 if no client arg is passed");
117   fail_unless(client_disconnect(a_client) == 1,
118       "client_disconnect should return 1 if successful");
119 }
120 END_TEST
121
122 START_TEST(test_transport_client_connected)
123 {
124   fail_unless(client_connected(NULL) == 0,
125       "client_connected should return 0 if no client arg is passed");
126   fail_unless(client_connected(a_client) == 1,
127       "client_connected should return 1 if successful");
128 }
129 END_TEST
130
131 START_TEST(test_transport_client_send_message)
132 {
133   fail_unless(client_send_message(NULL, a_message) == -1,
134       "client_send_message should return -1 if client arg is NULL");
135   a_client->error = 1;
136   //fail_unless(client_send_message(a_client->session, a_message") == -1,
137   //"client_send_message should return -1 if client->error is true");
138   a_client->error = 0;
139   //fail_unless(client_send_message(a_client->session, a_message) == 0,
140   //"client_send_message should return 0 on success");
141   //fail_unless(strcmp(a_message->sender, "user") == 0,
142   //"client_send_message shoud set msg->sender to the value of client->xmpp_id");
143 }
144 END_TEST
145
146 START_TEST(test_transport_client_recv)
147 {
148   //NULL client case
149   fail_unless(client_recv(NULL, 10) == NULL,
150       "client_recv should return NULL if the client arg is NULL");
151
152   //Message at head of queue
153   a_client->msg_q_head = a_message; //put a message at the head of the queue
154   transport_message *msg = client_recv(a_client, 10);
155   fail_if(msg == NULL,
156       "client_recv should return a transport_message on success");
157   fail_unless(a_client->msg_q_head == NULL,
158       "client_recv should remove the message from client->msg_q_head if it is successful");
159   fail_unless(msg->next == NULL,
160       "client_recv should set msg->next to NULL");
161   fail_unless(a_client->msg_q_tail == NULL,
162       "client_recv should set client->msg_q_tail to NULL if there was only one message in the queue");
163
164   //session_wait failure with no timeout
165   transport_client* other_client = client_init("server2", 4321, "unixpath2", 321);
166   transport_message *msg2 = client_recv(other_client, -1);
167   fail_unless(msg2 == NULL,
168       "client_recv should return NULL if the call to session_wait() returns an error");
169
170   //message in queue with no timeout
171   transport_message *msg3 = client_recv(a_client, -1);
172   fail_unless(msg3->next == NULL,
173       "client_recv should set msg->next to NULL");
174   fail_unless(a_client->msg_q_head == NULL,
175       "client_recv should set client->msg_q_head to NULL if there are no more queued messages");
176   fail_unless(a_client->msg_q_tail == NULL,
177       "client_recv should set client->msg_q_tail to NULL if client->msg_q_head was NULL");
178   fail_unless(strcmp(msg3->body, "body1") == 0,
179       "the message returned by client_recv should contain the contents of the message that was received");
180   fail_unless(strcmp(msg3->subject, "subject1") == 0,
181       "the message returned by client_recv should contain the contents of the message that was received");
182   fail_unless(strcmp(msg3->thread, "thread1") == 0,
183       "the message returned by client_recv should contain the contents of the message that was received");
184   fail_unless(strcmp(msg3->recipient, "recipient1") == 0,
185       "the message returned by client_recv should contain the contents of the message that was received");
186   fail_unless(strcmp(msg3->sender, "sender1") == 0,
187       "the message returned by client_recv should contain the contents of the message that was received");
188
189   //No message in queue with timeout
190   a_client->error = 0;
191   transport_message *msg4 = client_recv(a_client, 1); //only 1 sec timeout so we dont slow down testing
192   fail_unless(msg4 == NULL,
193       "client_recv should return NULL if there is no message in queue to receive");
194   fail_unless(a_client->error == 0,
195       "client->error should be 0 since there was no error");
196
197   //session_wait failure with timeout
198   other_client->error = 0;
199   transport_message *msg5 = client_recv(other_client, 1); //only 1 sec again...
200   fail_unless(msg5 == NULL,
201       "client_recv should return NULL if there is an error");
202 }
203 END_TEST
204
205 START_TEST(test_transport_client_free)
206 {
207   fail_unless(client_free(NULL) == 0,
208       "client_free should retun 0 if passed a NULL arg");
209   transport_client* client1 = client_init("server", 1234, "unixpath", 123);
210   fail_unless(client_free(client1) == 1,
211       "client_free should return 0 if successful");
212 }
213 END_TEST
214
215 START_TEST(test_transport_client_discard)
216 {
217   fail_unless(client_discard(NULL) == 0,
218       "client_discard should return 0 if passed a NULL arg");
219   transport_client* client1 = client_init("server", 1234, "unixpath", 123);
220   fail_unless(client_discard(client1) == 1,
221       "client_discard should return 1 if successful");
222 }
223 END_TEST
224
225 START_TEST(test_transport_client_sock_fd)
226 {
227   fail_unless(client_sock_fd(NULL) == 0,
228       "client_sock_fd should return 0 if passed a NULL arg");
229   a_client->session->sock_id = 1;
230   fail_unless(client_sock_fd(a_client) == 1,
231       "client_sock_fd should return client->session->sock_id");
232 }
233 END_TEST
234
235 //END TESTS
236
237 Suite *transport_client_suite(void) {
238   //Create test suite, test case, initialize fixture
239   Suite *s = suite_create("transport_client");
240   TCase *tc_core = tcase_create("Core");
241   tcase_add_checked_fixture(tc_core, setup, teardown);
242
243   //Add tests to test case
244   tcase_add_test(tc_core, test_transport_client_init);
245   tcase_add_test(tc_core, test_transport_client_connect);
246   tcase_add_test(tc_core, test_transport_client_disconnect);
247   tcase_add_test(tc_core, test_transport_client_connected);
248   tcase_add_test(tc_core, test_transport_client_send_message);
249   tcase_add_test(tc_core, test_transport_client_recv);
250   tcase_add_test(tc_core, test_transport_client_free);
251   tcase_add_test(tc_core, test_transport_client_discard);
252   tcase_add_test(tc_core, test_transport_client_sock_fd);
253
254   //Add test case to test suite
255   suite_add_tcase(s, tc_core);
256
257   return s;
258 }
259
260 void run_tests(SRunner *sr) {
261   srunner_add_suite(sr, transport_client_suite());
262 }