1 | #if defined(__linux__) || defined(__bsd__) || defined(__sunos__) |
2 | #include <unistd.h> |
3 | #include <arpa/inet.h> |
4 | #include <netinet/in.h> |
5 | #include <sys/socket.h> |
6 | #include <string.h> |
7 | #endif |
8 | |
9 | #include <stdio.h> |
10 | #include <stdlib.h> |
11 | #include <string.h> |
12 | |
13 | #include "websocket.h" |
14 | #include "message.h" |
15 | |
16 | #define CTEST_MAIN |
17 | #include "ctest.h" |
18 | |
19 | #include "common.h" |
20 | |
21 | cstr content = "content" ; |
22 | cstr uri = "ws://localhost:8181/websocket" ; |
23 | |
24 | #define check_reply(c, value) \ |
25 | { \ |
26 | vws_msg* m = vws_msg_recv(c); \ |
27 | ASSERT_TRUE(m != NULL); \ |
28 | ASSERT_TRUE(strncmp((cstr)m->data->data, value, m->data->size) == 0); \ |
29 | vws_msg_free(m);\ |
30 | } |
31 | |
32 | CTEST_DATA(test) |
33 | { |
34 | unsigned char* buffer; |
35 | vws_cnx* c; |
36 | }; |
37 | |
38 | CTEST_SETUP(test) |
39 | { |
40 | data->c = vws_cnx_new(); |
41 | ASSERT_TRUE(data->c != NULL); |
42 | ASSERT_TRUE(vws_connect(data->c, uri)); |
43 | |
44 | // Enable tracing |
45 | vws.tracelevel = VT_PROTOCOL; |
46 | } |
47 | |
48 | CTEST_TEARDOWN(test) |
49 | { |
50 | vws_cnx_free(data->c); |
51 | } |
52 | |
53 | CTEST2(test, send_receive) |
54 | { |
55 | vws_frame_send_text(data->c, content); |
56 | check_reply(data->c, content); |
57 | } |
58 | |
59 | CTEST2(test, send_data) |
60 | { |
61 | vws_frame_send_data(data->c, (ucstr)content, strlen(content), 0x1); |
62 | check_reply(data->c, content); |
63 | } |
64 | |
65 | CTEST2(test, send_frame) |
66 | { |
67 | vws_frame* frame = vws_frame_new((ucstr)content, strlen(content), 0x1); |
68 | vws_frame_send(data->c, frame); |
69 | check_reply(data->c, content); |
70 | } |
71 | |
72 | CTEST2(test, message) |
73 | { |
74 | cstr payload = "payload" ; |
75 | |
76 | // Create |
77 | vrtql_msg* request = vrtql_msg_new(); |
78 | vrtql_msg_set_content(request, payload); |
79 | |
80 | // Send |
81 | ASSERT_TRUE(vrtql_msg_send(data->c, request) > 0); |
82 | |
83 | // Receive |
84 | vrtql_msg* reply = vrtql_msg_recv(data->c); |
85 | ASSERT_TRUE(reply != NULL); |
86 | |
87 | // Check |
88 | cstr content = (cstr)reply->content->data; |
89 | size_t size = reply->content->size; |
90 | ASSERT_TRUE(strncmp(payload, content, size) == 0); |
91 | |
92 | // Cleanup |
93 | vrtql_msg_free(request); |
94 | vrtql_msg_free(reply); |
95 | } |
96 | |
97 | int main(int argc, const char* argv[]) |
98 | { |
99 | return ctest_main(argc, argv); |
100 | } |
101 | |