VirtualBox

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

最後變更 在這個檔案從48919是 45259,由 vboxsync 提交於 12 年 前

tstRTTcp-1.cpp: testboxsh1 fudging.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 7.1 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 int rc = RTTcpServerCreate("localhost", 9999, RTTHREADTYPE_DEFAULT, "server-2", test3Server, NULL, &pServer);
49#ifdef RT_OS_SOLARIS
50 /** @todo testboxsh1 occationally hits this for some stupid reason. i=21 in
51 * one occurrence. Fudge a bit for now and see if it helps. */
52 if (rc == VERR_NET_ADDRESS_IN_USE)
53 {
54 RTThreadSleep(500);
55 rc = RTTcpServerCreate("localhost", 9999, RTTHREADTYPE_DEFAULT, "server-2", test3Server, NULL, &pServer);
56 }
57#endif
58 if (rc != VINF_SUCCESS)
59 {
60 RTTestIFailed("RTTcpServerCreate -> %Rrc, i=%d", rc, i);
61 return;
62 }
63
64 RTSOCKET hSocket;
65 RTTESTI_CHECK_RC(rc = RTTcpClientConnect("localhost", 9999, &hSocket), VINF_SUCCESS);
66 if (RT_SUCCESS(rc))
67 {
68 char szBuf[512];
69 RT_ZERO(szBuf);
70 do /* break non-loop */
71 {
72 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
73 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
74 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
75 } while (0);
76
77 RTTESTI_CHECK_RC(RTTcpClientClose(hSocket), VINF_SUCCESS);
78 }
79
80 RTTESTI_CHECK_RC(RTTcpServerDestroy(pServer), VINF_SUCCESS);
81 }
82}
83
84
85/* * * * * * * * Test 2 * * * * * * * */
86
87static DECLCALLBACK(int) test2Server(RTSOCKET hSocket, void *pvUser)
88{
89 RTTestSetDefault(g_hTest, NULL);
90 char szBuf[512];
91
92 /* say hello */
93 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, "hello\n", sizeof("hello\n") - 1), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
94 RT_ZERO(szBuf);
95 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("dude!\n") - 1, NULL), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
96 szBuf[sizeof("dude!\n") - 1] = '\0';
97 RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_TCP_SERVER_STOP);
98
99 /* wait for a goodbye which doesn't arrive. */
100 RT_ZERO(szBuf);
101 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("byebye\n") - 1, NULL), VERR_NET_SHUTDOWN, VERR_TCP_SERVER_STOP);
102
103 return VERR_TCP_SERVER_STOP;
104}
105
106
107void test2()
108{
109 RTTestSub(g_hTest, "Rude client");
110
111 PRTTCPSERVER pServer;
112 RTTESTI_CHECK_RC_RETV(RTTcpServerCreate("localhost", 9999, RTTHREADTYPE_DEFAULT, "server-2",
113 test2Server, NULL, &pServer), VINF_SUCCESS);
114
115 int rc;
116 RTSOCKET hSocket;
117 RTTESTI_CHECK_RC(rc = RTTcpClientConnect("localhost", 9999, &hSocket), VINF_SUCCESS);
118 if (RT_SUCCESS(rc))
119 {
120 char szBuf[512];
121 RT_ZERO(szBuf);
122 do /* break non-loop */
123 {
124 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
125 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
126 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
127 } while (0);
128
129 RTTESTI_CHECK_RC(RTTcpClientClose(hSocket), VINF_SUCCESS);
130 }
131
132 RTTESTI_CHECK_RC(RTTcpServerDestroy(pServer), VINF_SUCCESS);
133}
134
135
136/* * * * * * * * Test 1 * * * * * * * */
137
138static DECLCALLBACK(int) test1Server(RTSOCKET hSocket, void *pvUser)
139{
140 RTTestSetDefault(g_hTest, NULL);
141
142 char szBuf[512];
143 RTTESTI_CHECK_RET(pvUser == NULL, VERR_TCP_SERVER_STOP);
144
145 /* say hello */
146 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, "hello\n", sizeof("hello\n") - 1), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
147 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("dude!\n") - 1, NULL), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
148 szBuf[sizeof("dude!\n") - 1] = '\0';
149 RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_TCP_SERVER_STOP);
150
151 /* say goodbye */
152 RTTESTI_CHECK_RC_RET(RTTcpRead(hSocket, szBuf, sizeof("byebye\n") - 1, NULL), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
153 szBuf[sizeof("byebye\n") - 1] = '\0';
154 RTTESTI_CHECK_RET(strcmp(szBuf, "byebye\n") == 0, VERR_TCP_SERVER_STOP);
155 RTTESTI_CHECK_RC_RET(RTTcpWrite(hSocket, "bye\n", sizeof("bye\n") - 1), VINF_SUCCESS, VERR_TCP_SERVER_STOP);
156
157 return VERR_TCP_SERVER_STOP;
158}
159
160
161void test1()
162{
163 RTTestSub(g_hTest, "Simple server-client setup");
164
165 PRTTCPSERVER pServer;
166 RTTESTI_CHECK_RC_RETV(RTTcpServerCreate("localhost", 9999, RTTHREADTYPE_DEFAULT, "server-1",
167 test1Server, NULL, &pServer), VINF_SUCCESS);
168
169 int rc;
170 RTSOCKET hSocket;
171 RTTESTI_CHECK_RC(rc = RTTcpClientConnect("localhost", 9999, &hSocket), VINF_SUCCESS);
172 if (RT_SUCCESS(rc))
173 {
174 do /* break non-loop */
175 {
176 char szBuf[512];
177 RT_ZERO(szBuf);
178 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
179 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
180 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
181
182 RTTESTI_CHECK_RC_BREAK(RTTcpWrite(hSocket, "byebye\n", sizeof("byebye\n") - 1), VINF_SUCCESS);
183 RT_ZERO(szBuf);
184 RTTESTI_CHECK_RC_BREAK(RTTcpRead(hSocket, szBuf, sizeof("bye\n") - 1, NULL), VINF_SUCCESS);
185 RTTESTI_CHECK_BREAK(strcmp(szBuf, "bye\n") == 0);
186 } while (0);
187
188 RTTESTI_CHECK_RC(RTTcpClientClose(hSocket), VINF_SUCCESS);
189 }
190
191 RTTESTI_CHECK_RC(RTTcpServerDestroy(pServer), VINF_SUCCESS);
192}
193
194
195int main()
196{
197 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTTcp-1", &g_hTest);
198 if (rcExit != RTEXITCODE_SUCCESS)
199 return rcExit;
200 RTTestBanner(g_hTest);
201
202 test1();
203 test2();
204 test3();
205
206 /** @todo test the full RTTcp API. */
207
208 return RTTestSummaryAndDestroy(g_hTest);
209}
210
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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