VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTTcp-1.cpp@ 33971

最後變更 在這個檔案從33971是 32810,由 vboxsync 提交於 14 年 前

tcp.cpp: Outlined fix rtTcpClose fix for 3.2.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.6 KB
 
1#include <iprt/tcp.h>
2
3#include <iprt/string.h>
4#include <iprt/test.h>
5
6
7/*******************************************************************************
8* Global Variables *
9*******************************************************************************/
10static RTTEST g_hTest;
11
12
13/* * * * * * * * Test 3 * * * * * * * */
14
15static DECLCALLBACK(int) test3Server(RTSOCKET hSocket, void *pvUser)
16{
17 RTTestSetDefault(g_hTest, NULL);
18 char szBuf[4096];
19
20 /* say hello */
21 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, "hello\n", sizeof("hello\n") - 1), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
22 RT_ZERO(szBuf);
23 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("dude!\n") - 1, NULL), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
24 szBuf[sizeof("dude!\n") - 1] = '\0';
25 RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_TCP_SERVER_STOP);
26
27 /* Send ~20 MB of data that the client receives while trying to disconnect. */
28 RT_ZERO(szBuf);
29 size_t cbSent = 0;
30 while (cbSent < 20 * _1M)
31 {
32 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, szBuf, sizeof(szBuf)), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
33 cbSent += sizeof(szBuf);
34 }
35
36 return VERR_TCP_SERVER_STOP;
37}
38
39
40void test3()
41{
42 RTTestSub(g_hTest, "Graceful disconnect");
43
44 uint32_t cStartErrors = RTTestErrorCount(g_hTest);
45 for (unsigned i = 0; i < 100 && cStartErrors == RTTestErrorCount(g_hTest); i++)
46 {
47 PRTTCPSERVER pServer;
48 RTTESTI_CHECK_RC_RETV(RTTcpServerCreate("localhost", 9999, RTTHREADTYPE_DEFAULT, "server-2",
49 test3Server, NULL, &pServer), VINF_SUCCESS);
50
51 int rc;
52 RTSOCKET hSocket;
53 RTTESTI_CHECK_RC(rc = RTTcpClientConnect("localhost", 9999, &hSocket), VINF_SUCCESS);
54 if (RT_SUCCESS(rc))
55 {
56 char szBuf[512];
57 RT_ZERO(szBuf);
58 do /* break non-loop */
59 {
60 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
61 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
62 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
63 } while (0);
64
65 RTTESTI_CHECK_RC(RTTcpClientClose(hSocket), VINF_SUCCESS);
66 }
67
68 RTTESTI_CHECK_RC(RTTcpServerDestroy(pServer), VINF_SUCCESS);
69 }
70}
71
72
73/* * * * * * * * Test 2 * * * * * * * */
74
75static DECLCALLBACK(int) test2Server(RTSOCKET hSocket, void *pvUser)
76{
77 RTTestSetDefault(g_hTest, NULL);
78 char szBuf[512];
79
80 /* say hello */
81 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, "hello\n", sizeof("hello\n") - 1), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
82 RT_ZERO(szBuf);
83 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("dude!\n") - 1, NULL), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
84 szBuf[sizeof("dude!\n") - 1] = '\0';
85 RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_TCP_SERVER_STOP);
86
87 /* wait for a goodbye which doesn't arrive. */
88 RT_ZERO(szBuf);
89 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("byebye\n") - 1, NULL), VERR_NET_SHUTDOWN, VERR_TCP_SERVER_STOP);
90
91 return VERR_TCP_SERVER_STOP;
92}
93
94
95void test2()
96{
97 RTTestSub(g_hTest, "Rude client");
98
99 PRTTCPSERVER pServer;
100 RTTESTI_CHECK_RC_RETV(RTTcpServerCreate("localhost", 9999, RTTHREADTYPE_DEFAULT, "server-2",
101 test2Server, NULL, &pServer), VINF_SUCCESS);
102
103 int rc;
104 RTSOCKET hSocket;
105 RTTESTI_CHECK_RC(rc = RTTcpClientConnect("localhost", 9999, &hSocket), VINF_SUCCESS);
106 if (RT_SUCCESS(rc))
107 {
108 char szBuf[512];
109 RT_ZERO(szBuf);
110 do /* break non-loop */
111 {
112 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
113 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
114 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
115 } while (0);
116
117 RTTESTI_CHECK_RC(RTTcpClientClose(hSocket), VINF_SUCCESS);
118 }
119
120 RTTESTI_CHECK_RC(RTTcpServerDestroy(pServer), VINF_SUCCESS);
121}
122
123
124/* * * * * * * * Test 1 * * * * * * * */
125
126static DECLCALLBACK(int) test1Server(RTSOCKET hSocket, void *pvUser)
127{
128 RTTestSetDefault(g_hTest, NULL);
129
130 char szBuf[512];
131 RTTESTI_CHECK_RET(pvUser == NULL, VERR_TCP_SERVER_STOP);
132
133 /* say hello */
134 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, "hello\n", sizeof("hello\n") - 1), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
135 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("dude!\n") - 1, NULL), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
136 szBuf[sizeof("dude!\n") - 1] = '\0';
137 RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_TCP_SERVER_STOP);
138
139 /* say goodbye */
140 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("byebye\n") - 1, NULL), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
141 szBuf[sizeof("byebye\n") - 1] = '\0';
142 RTTESTI_CHECK_RET(strcmp(szBuf, "byebye\n") == 0, VERR_TCP_SERVER_STOP);
143 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, "bye\n", sizeof("bye\n") - 1), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
144
145 return VERR_TCP_SERVER_STOP;
146}
147
148
149void test1()
150{
151 RTTestSub(g_hTest, "Simple server-client setup");
152
153 PRTTCPSERVER pServer;
154 RTTESTI_CHECK_RC_RETV(RTTcpServerCreate("localhost", 9999, RTTHREADTYPE_DEFAULT, "server-1",
155 test1Server, NULL, &pServer), VINF_SUCCESS);
156
157 int rc;
158 RTSOCKET hSocket;
159 RTTESTI_CHECK_RC(rc = RTTcpClientConnect("localhost", 9999, &hSocket), VINF_SUCCESS);
160 if (RT_SUCCESS(rc))
161 {
162 do /* break non-loop */
163 {
164 char szBuf[512];
165 RT_ZERO(szBuf);
166 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
167 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
168 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
169
170 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "byebye\n", sizeof("byebye\n") - 1), VINF_SUCCESS);
171 RT_ZERO(szBuf);
172 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("bye\n") - 1, NULL), VINF_SUCCESS);
173 RTTESTI_CHECK_BREAK(strcmp(szBuf, "bye\n") == 0);
174 } while (0);
175
176 RTTESTI_CHECK_RC(RTTcpClientClose(hSocket), VINF_SUCCESS);
177 }
178
179 RTTESTI_CHECK_RC(RTTcpServerDestroy(pServer), VINF_SUCCESS);
180}
181
182
183int main()
184{
185 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTTcp-1", &g_hTest);
186 if (rcExit != RTEXITCODE_SUCCESS)
187 return rcExit;
188 RTTestBanner(g_hTest);
189
190 test1();
191 test2();
192 test3();
193
194 /** @todo test the full RTTcp API. */
195
196 return RTTestSummaryAndDestroy(g_hTest);
197}
198
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette