]> git.evergreen-ils.org Git - OpenSRF.git/blob - tests/check_transport_client.c
LP#1268619: websockets: README typo repairs
[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   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");
98 END_TEST
99
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");
107 END_TEST
108
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");
114 END_TEST
115
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");
121 END_TEST
122
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");
126   a_client->error = 1;
127   //fail_unless(client_send_message(a_client->session, a_message") == -1,
128   //"client_send_message should return -1 if client->error is true");
129   a_client->error = 0;
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");
134 END_TEST
135
136 START_TEST(test_transport_client_recv)
137   //NULL client case
138   fail_unless(client_recv(NULL, 10) == NULL,
139       "client_recv should return NULL if the client arg is NULL");
140
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);
144   fail_if(msg == NULL,
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");
152
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
159   //message in queue with no timeout
160   transport_message *msg3 = client_recv(a_client, -1);
161   fail_unless(msg3->next == NULL,
162       "client_recv should set msg->next to NULL");
163   fail_unless(a_client->msg_q_head == NULL,
164       "client_recv should set client->msg_q_head to NULL if there are no more queued messages");
165   fail_unless(a_client->msg_q_tail == NULL,
166       "client_recv should set client->msg_q_tail to NULL if client->msg_q_head was NULL");
167   fail_unless(strcmp(msg3->body, "body1") == 0,
168       "the message returned by client_recv should contain the contents of the message that was received");
169   fail_unless(strcmp(msg3->subject, "subject1") == 0,
170       "the message returned by client_recv should contain the contents of the message that was received");
171   fail_unless(strcmp(msg3->thread, "thread1") == 0,
172       "the message returned by client_recv should contain the contents of the message that was received");
173   fail_unless(strcmp(msg3->recipient, "recipient1") == 0,
174       "the message returned by client_recv should contain the contents of the message that was received");
175   fail_unless(strcmp(msg3->sender, "sender1") == 0,
176       "the message returned by client_recv should contain the contents of the message that was received");
177
178   //No message in queue with timeout
179   a_client->error = 0;
180   transport_message *msg4 = client_recv(a_client, 1); //only 1 sec timeout so we dont slow down testing
181   fail_unless(msg4 == NULL,
182       "client_recv should return NULL if there is no message in queue to receive");
183   fail_unless(a_client->error == 0,
184       "client->error should be 0 since there was no error");
185
186   //session_wait failure with timeout
187   other_client->error = 0;
188   transport_message *msg5 = client_recv(other_client, 1); //only 1 sec again...
189   fail_unless(msg5 == NULL,
190       "client_recv should return NULL if there is an error");
191 END_TEST
192
193 START_TEST(test_transport_client_free)
194   fail_unless(client_free(NULL) == 0,
195       "client_free should retun 0 if passed a NULL arg");
196   transport_client* client1 = client_init("server", 1234, "unixpath", 123);
197   fail_unless(client_free(client1) == 1,
198       "client_free should return 0 if successful");
199 END_TEST
200
201 START_TEST(test_transport_client_discard)
202   fail_unless(client_discard(NULL) == 0,
203       "client_discard should return 0 if passed a NULL arg");
204   transport_client* client1 = client_init("server", 1234, "unixpath", 123);
205   fail_unless(client_discard(client1) == 1,
206       "client_discard should return 1 if successful");
207 END_TEST
208
209 START_TEST(test_transport_client_sock_fd)
210   fail_unless(client_sock_fd(NULL) == 0,
211       "client_sock_fd should return 0 if passed a NULL arg");
212   a_client->session->sock_id = 1;
213   fail_unless(client_sock_fd(a_client) == 1,
214       "client_sock_fd should return client->session->sock_id");
215 END_TEST
216
217 //END TESTS
218
219 Suite *transport_client_suite(void) {
220   //Create test suite, test case, initialize fixture
221   Suite *s = suite_create("transport_client");
222   TCase *tc_core = tcase_create("Core");
223   tcase_add_checked_fixture(tc_core, setup, teardown);
224
225   //Add tests to test case
226   tcase_add_test(tc_core, test_transport_client_init);
227   tcase_add_test(tc_core, test_transport_client_connect);
228   tcase_add_test(tc_core, test_transport_client_disconnect);
229   tcase_add_test(tc_core, test_transport_client_connected);
230   tcase_add_test(tc_core, test_transport_client_send_message);
231   tcase_add_test(tc_core, test_transport_client_recv);
232   tcase_add_test(tc_core, test_transport_client_free);
233   tcase_add_test(tc_core, test_transport_client_discard);
234   tcase_add_test(tc_core, test_transport_client_sock_fd);
235
236   //Add test case to test suite
237   suite_add_tcase(s, tc_core);
238
239   return s;
240 }
241
242 void run_tests(SRunner *sr) {
243   srunner_add_suite(sr, transport_client_suite());
244 }