VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTUdp-1.cpp@ 70425

Last change on this file since 70425 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: tstRTUdp-1.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT testcase - UDP.
4 */
5
6/*
7 * Copyright (C) 2015-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28#include <iprt/udp.h>
29
30#include <iprt/string.h>
31#include <iprt/test.h>
32
33/* Server address must be "localhost" */
34#define RT_TEST_UDP_LOCAL_HOST "localhost"
35#define RT_TEST_UDP_SERVER_PORT 52000
36
37
38/*********************************************************************************************************************************
39* Global Variables *
40*********************************************************************************************************************************/
41static RTTEST g_hTest;
42
43
44/* * * * * * * * Test 1 * * * * * * * */
45
46static DECLCALLBACK(int) test1Server(RTSOCKET hSocket, void *pvUser)
47{
48 RTTestSetDefault(g_hTest, NULL);
49
50 char szBuf[512];
51 RTTESTI_CHECK_RET(pvUser == NULL, VERR_UDP_SERVER_STOP);
52
53 RTNETADDR ClientAddress;
54
55 /* wait for exclamation! */
56 size_t cbReallyRead;
57 RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("dude!\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
58 VERR_UDP_SERVER_STOP);
59 szBuf[sizeof("dude!\n") - 1] = '\0';
60 RTTESTI_CHECK_RET(cbReallyRead == sizeof("dude!\n") - 1, VERR_UDP_SERVER_STOP);
61 RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_UDP_SERVER_STOP);
62
63 /* say hello. */
64 RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "hello\n", sizeof("hello\n") - 1, &ClientAddress), VINF_SUCCESS,
65 VERR_UDP_SERVER_STOP);
66
67 /* wait for goodbye. */
68 RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("byebye\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
69 VERR_UDP_SERVER_STOP);
70 RTTESTI_CHECK_RET(cbReallyRead == sizeof("byebye\n") - 1, VERR_UDP_SERVER_STOP);
71 szBuf[sizeof("byebye\n") - 1] = '\0';
72 RTTESTI_CHECK_RET(strcmp(szBuf, "byebye\n") == 0, VERR_UDP_SERVER_STOP);
73
74 /* say buhbye */
75 RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "buh bye\n", sizeof("buh bye\n") - 1, &ClientAddress), VINF_SUCCESS,
76 VERR_UDP_SERVER_STOP);
77
78 return VINF_SUCCESS;
79}
80
81
82static void test1()
83{
84 RTTestSub(g_hTest, "Simple server-client setup");
85
86 /*
87 * Set up server address (port) for UDP.
88 */
89 RTNETADDR ServerAddress;
90 RTTESTI_CHECK_RC_RETV(RTSocketParseInetAddress(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, &ServerAddress),
91 VINF_SUCCESS);
92
93 PRTUDPSERVER pServer;
94 RTTESTI_CHECK_RC_RETV(RTUdpServerCreate(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, RTTHREADTYPE_DEFAULT, "server-1",
95 test1Server, NULL, &pServer), VINF_SUCCESS);
96
97 int rc;
98 RTSOCKET hSocket;
99 RTTESTI_CHECK_RC(rc = RTUdpCreateClientSocket(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, NULL, &hSocket), VINF_SUCCESS);
100 if (RT_SUCCESS(rc))
101 {
102 do /* break non-loop */
103 {
104 char szBuf[512];
105 RT_ZERO(szBuf);
106 RTTESTI_CHECK_RC_BREAK(RTSocketWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
107
108 RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
109 szBuf[sizeof("hello!\n") - 1] = '\0';
110 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
111
112 RTTESTI_CHECK_RC_BREAK(RTSocketWrite(hSocket, "byebye\n", sizeof("byebye\n") - 1), VINF_SUCCESS);
113 RT_ZERO(szBuf);
114 RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("buh bye\n") - 1, NULL), VINF_SUCCESS);
115 RTTESTI_CHECK_BREAK(strcmp(szBuf, "buh bye\n") == 0);
116 } while (0);
117
118 RTTESTI_CHECK_RC(RTSocketClose(hSocket), VINF_SUCCESS);
119 }
120
121 RTTESTI_CHECK_RC(RTUdpServerDestroy(pServer), VINF_SUCCESS);
122}
123
124
125int main()
126{
127 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTUdp-1", &g_hTest);
128 if (rcExit != RTEXITCODE_SUCCESS)
129 return rcExit;
130 RTTestBanner(g_hTest);
131
132 test1();
133
134 /** @todo test the full RTUdp API. */
135
136 return RTTestSummaryAndDestroy(g_hTest);
137}
138
Note: See TracBrowser for help on using the repository browser.

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