1 | /* $Id: tstRTUdp-1.cpp 57970 2015-09-30 14:56:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT testcase - UDP.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015 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 | * Global Variables *
|
---|
39 | *********************************************************************************************************************************/
|
---|
40 | static RTTEST g_hTest;
|
---|
41 |
|
---|
42 |
|
---|
43 | /* * * * * * * * Test 1 * * * * * * * */
|
---|
44 |
|
---|
45 | static DECLCALLBACK(int) test1Server(RTSOCKET hSocket, void *pvUser)
|
---|
46 | {
|
---|
47 | RTTestSetDefault(g_hTest, NULL);
|
---|
48 |
|
---|
49 | char szBuf[512];
|
---|
50 | RTTESTI_CHECK_RET(pvUser == NULL, VERR_UDP_SERVER_STOP);
|
---|
51 |
|
---|
52 | RTNETADDR ClientAddress;
|
---|
53 |
|
---|
54 | /* wait for exclamation! */
|
---|
55 | size_t cbReallyRead;
|
---|
56 | RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("dude!\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
|
---|
57 | VERR_UDP_SERVER_STOP);
|
---|
58 | szBuf[sizeof("dude!\n") - 1] = '\0';
|
---|
59 | RTTESTI_CHECK_RET(cbReallyRead == sizeof("dude!\n") - 1, VERR_UDP_SERVER_STOP);
|
---|
60 | RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_UDP_SERVER_STOP);
|
---|
61 |
|
---|
62 | /* say hello. */
|
---|
63 | RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "hello\n", sizeof("hello\n") - 1, &ClientAddress), VINF_SUCCESS,
|
---|
64 | VERR_UDP_SERVER_STOP);
|
---|
65 |
|
---|
66 | /* wait for goodbye. */
|
---|
67 | RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("byebye\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
|
---|
68 | VERR_UDP_SERVER_STOP);
|
---|
69 | RTTESTI_CHECK_RET(cbReallyRead == sizeof("byebye\n") - 1, VERR_UDP_SERVER_STOP);
|
---|
70 | szBuf[sizeof("byebye\n") - 1] = '\0';
|
---|
71 | RTTESTI_CHECK_RET(strcmp(szBuf, "byebye\n") == 0, VERR_UDP_SERVER_STOP);
|
---|
72 |
|
---|
73 | /* say buhbye */
|
---|
74 | RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "buh bye\n", sizeof("buh bye\n") - 1, &ClientAddress), VINF_SUCCESS,
|
---|
75 | VERR_UDP_SERVER_STOP);
|
---|
76 |
|
---|
77 | return VINF_SUCCESS;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | static void test1()
|
---|
82 | {
|
---|
83 | RTTestSub(g_hTest, "Simple server-client setup");
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Set up server address (port) for UDP.
|
---|
87 | */
|
---|
88 | RTNETADDR ServerAddress;
|
---|
89 | RTTESTI_CHECK_RC_RETV(RTSocketParseInetAddress(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, &ServerAddress),
|
---|
90 | VINF_SUCCESS);
|
---|
91 |
|
---|
92 | PRTUDPSERVER pServer;
|
---|
93 | RTTESTI_CHECK_RC_RETV(RTUdpServerCreate(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, RTTHREADTYPE_DEFAULT, "server-1",
|
---|
94 | test1Server, NULL, &pServer), VINF_SUCCESS);
|
---|
95 |
|
---|
96 | int rc;
|
---|
97 | RTSOCKET hSocket;
|
---|
98 | RTTESTI_CHECK_RC(rc = RTUdpCreateClientSocket(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, NULL, &hSocket), VINF_SUCCESS);
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | do /* break non-loop */
|
---|
102 | {
|
---|
103 | char szBuf[512];
|
---|
104 | RT_ZERO(szBuf);
|
---|
105 | RTTESTI_CHECK_RC_BREAK(RTSocketWrite(hSocket, "dude!\n", sizeof("dude!\n") - 1), VINF_SUCCESS);
|
---|
106 |
|
---|
107 | RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
|
---|
108 | szBuf[sizeof("hello!\n") - 1] = '\0';
|
---|
109 | RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
|
---|
110 |
|
---|
111 | RTTESTI_CHECK_RC_BREAK(RTSocketWrite(hSocket, "byebye\n", sizeof("byebye\n") - 1), VINF_SUCCESS);
|
---|
112 | RT_ZERO(szBuf);
|
---|
113 | RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("buh bye\n") - 1, NULL), VINF_SUCCESS);
|
---|
114 | RTTESTI_CHECK_BREAK(strcmp(szBuf, "buh bye\n") == 0);
|
---|
115 | } while (0);
|
---|
116 |
|
---|
117 | RTTESTI_CHECK_RC(RTSocketClose(hSocket), VINF_SUCCESS);
|
---|
118 | }
|
---|
119 |
|
---|
120 | RTTESTI_CHECK_RC(RTUdpServerDestroy(pServer), VINF_SUCCESS);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | int main()
|
---|
125 | {
|
---|
126 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTUdp-1", &g_hTest);
|
---|
127 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
128 | return rcExit;
|
---|
129 | RTTestBanner(g_hTest);
|
---|
130 |
|
---|
131 | test1();
|
---|
132 |
|
---|
133 | /** @todo test the full RTUdp API. */
|
---|
134 |
|
---|
135 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
136 | }
|
---|
137 |
|
---|