1 | /* $Id: tstRTUdp-1.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT testcase - UDP.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/udp.h>
|
---|
42 |
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/test.h>
|
---|
46 |
|
---|
47 | /* Server address must be "localhost" */
|
---|
48 | #define RT_TEST_UDP_LOCAL_HOST "localhost"
|
---|
49 | #define RT_TEST_UDP_SERVER_PORT 52000
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Global Variables *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | static RTTEST g_hTest;
|
---|
56 |
|
---|
57 |
|
---|
58 | /* * * * * * * * Test 1 * * * * * * * */
|
---|
59 |
|
---|
60 | static DECLCALLBACK(int) test1Server(RTSOCKET hSocket, void *pvUser)
|
---|
61 | {
|
---|
62 | RTTestSetDefault(g_hTest, NULL);
|
---|
63 |
|
---|
64 | char szBuf[512];
|
---|
65 | RTTESTI_CHECK_RET(pvUser == NULL, VERR_UDP_SERVER_STOP);
|
---|
66 |
|
---|
67 | RTNETADDR ClientAddress;
|
---|
68 |
|
---|
69 | /* wait for exclamation! */
|
---|
70 | size_t cbReallyRead;
|
---|
71 | RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("dude!\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
|
---|
72 | VERR_UDP_SERVER_STOP);
|
---|
73 | szBuf[sizeof("dude!\n") - 1] = '\0';
|
---|
74 | RTTESTI_CHECK_RET(cbReallyRead == sizeof("dude!\n") - 1, VERR_UDP_SERVER_STOP);
|
---|
75 | RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_UDP_SERVER_STOP);
|
---|
76 |
|
---|
77 | /* say hello. */
|
---|
78 | RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "hello\n", sizeof("hello\n") - 1, &ClientAddress), VINF_SUCCESS,
|
---|
79 | VERR_UDP_SERVER_STOP);
|
---|
80 |
|
---|
81 | /* wait for goodbye. */
|
---|
82 | RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("byebye\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
|
---|
83 | VERR_UDP_SERVER_STOP);
|
---|
84 | RTTESTI_CHECK_RET(cbReallyRead == sizeof("byebye\n") - 1, VERR_UDP_SERVER_STOP);
|
---|
85 | szBuf[sizeof("byebye\n") - 1] = '\0';
|
---|
86 | RTTESTI_CHECK_RET(strcmp(szBuf, "byebye\n") == 0, VERR_UDP_SERVER_STOP);
|
---|
87 |
|
---|
88 | /* say buhbye */
|
---|
89 | RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "buh bye\n", sizeof("buh bye\n") - 1, &ClientAddress), VINF_SUCCESS,
|
---|
90 | VERR_UDP_SERVER_STOP);
|
---|
91 |
|
---|
92 | return VINF_SUCCESS;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | static void test1()
|
---|
97 | {
|
---|
98 | RTTestSub(g_hTest, "Simple server-client setup");
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Set up server address (port) for UDP.
|
---|
102 | */
|
---|
103 | RTNETADDR ServerAddress;
|
---|
104 | RTTESTI_CHECK_RC_RETV(RTSocketParseInetAddress(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, &ServerAddress),
|
---|
105 | VINF_SUCCESS);
|
---|
106 |
|
---|
107 | PRTUDPSERVER pServer;
|
---|
108 | RTTESTI_CHECK_RC_RETV(RTUdpServerCreate(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, RTTHREADTYPE_DEFAULT, "server-1",
|
---|
109 | test1Server, NULL, &pServer), VINF_SUCCESS);
|
---|
110 |
|
---|
111 | int rc;
|
---|
112 | RTSOCKET hSocket;
|
---|
113 | RTTESTI_CHECK_RC(rc = RTUdpCreateClientSocket(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, NULL, &hSocket), VINF_SUCCESS);
|
---|
114 | if (RT_SUCCESS(rc))
|
---|
115 | {
|
---|
116 | do /* break non-loop */
|
---|
117 | {
|
---|
118 | char szBuf[512];
|
---|
119 | RT_ZERO(szBuf);
|
---|
120 | RTTESTI_CHECK_RC_BREAK(RTSocketWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
|
---|
121 |
|
---|
122 | RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
|
---|
123 | szBuf[sizeof("hello!\n") - 1] = '\0';
|
---|
124 | RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
|
---|
125 |
|
---|
126 | RTTESTI_CHECK_RC_BREAK(RTSocketWrite(hSocket, "byebye\n", sizeof("byebye\n") - 1), VINF_SUCCESS);
|
---|
127 | RT_ZERO(szBuf);
|
---|
128 | RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("buh bye\n") - 1, NULL), VINF_SUCCESS);
|
---|
129 | RTTESTI_CHECK_BREAK(strcmp(szBuf, "buh bye\n") == 0);
|
---|
130 | } while (0);
|
---|
131 |
|
---|
132 | RTTESTI_CHECK_RC(RTSocketClose(hSocket), VINF_SUCCESS);
|
---|
133 | }
|
---|
134 |
|
---|
135 | RTTESTI_CHECK_RC(RTUdpServerDestroy(pServer), VINF_SUCCESS);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | int main()
|
---|
140 | {
|
---|
141 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTUdp-1", &g_hTest);
|
---|
142 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
143 | return rcExit;
|
---|
144 | RTTestBanner(g_hTest);
|
---|
145 |
|
---|
146 | test1();
|
---|
147 |
|
---|
148 | /** @todo test the full RTUdp API. */
|
---|
149 |
|
---|
150 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
151 | }
|
---|
152 |
|
---|