VirtualBox

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

最後變更 在這個檔案從57956是 57956,由 vboxsync 提交於 9 年 前

Runtime/testcase: Added a simple UDP socket testcase.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.2 KB
 
1/* $Id: tstRTUdp-1.cpp 57956 2015-09-29 22:27:13Z 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.alldomusa.eu.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#define RT_TEST_UDP_CLIENT_ADDRESS "localhost"
34#define RT_TEST_UDP_CLIENT_PORT 52001
35
36/* Server address must be "localhost" */
37#define RT_TEST_UDP_LOCAL_HOST "localhost"
38#define RT_TEST_UDP_SERVER_PORT 52000
39
40/*********************************************************************************************************************************
41* Global Variables *
42*********************************************************************************************************************************/
43static RTTEST g_hTest;
44
45
46/* * * * * * * * Test 1 * * * * * * * */
47
48static DECLCALLBACK(int) test1Server(RTSOCKET hSocket, void *pvUser)
49{
50 RTTestSetDefault(g_hTest, NULL);
51
52 char szBuf[512];
53 RTTESTI_CHECK_RET(pvUser == NULL, VERR_UDP_SERVER_STOP);
54
55 RTNETADDR ClientAddress;
56
57 /* wait for exclamation! */
58 size_t cbReallyRead;
59 RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("dude!\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
60 VERR_UDP_SERVER_STOP);
61 szBuf[sizeof("dude!\n") - 1] = '\0';
62 RTTESTI_CHECK_RET(cbReallyRead == sizeof("dude!\n") - 1, VERR_UDP_SERVER_STOP);
63 RTTESTI_CHECK_RET(strcmp(szBuf, "dude!\n") == 0, VERR_UDP_SERVER_STOP);
64
65 /* say hello. */
66 RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "hello\n", sizeof("hello\n") - 1, &ClientAddress), VINF_SUCCESS,
67 VERR_UDP_SERVER_STOP);
68
69 /* wait for goodbye. */
70 RTTESTI_CHECK_RC_RET(RTSocketReadFrom(hSocket, szBuf, sizeof("byebye\n") - 1, &cbReallyRead, &ClientAddress), VINF_SUCCESS,
71 VERR_UDP_SERVER_STOP);
72 RTTESTI_CHECK_RET(cbReallyRead == sizeof("byebye\n") - 1, VERR_UDP_SERVER_STOP);
73 szBuf[sizeof("byebye\n") - 1] = '\0';
74 RTTESTI_CHECK_RET(strcmp(szBuf, "byebye\n") == 0, VERR_UDP_SERVER_STOP);
75
76 /* say buhbye */
77 RTTESTI_CHECK_RC_RET(RTSocketWriteTo(hSocket, "buh bye\n", sizeof("buh bye\n") - 1, &ClientAddress), VINF_SUCCESS,
78 VERR_UDP_SERVER_STOP);
79
80 return VINF_SUCCESS;
81}
82
83
84static void test1()
85{
86 RTTestSub(g_hTest, "Simple server-client setup");
87
88 /*
89 * Set up server address (port) for UDP.
90 */
91 RTNETADDR ServerAddress;
92 RTTESTI_CHECK_RC_RETV(RTSocketParseInetAddress(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, &ServerAddress),
93 VINF_SUCCESS);
94
95 PRTUDPSERVER pServer;
96 RTTESTI_CHECK_RC_RETV(RTUdpServerCreate(RT_TEST_UDP_LOCAL_HOST, RT_TEST_UDP_SERVER_PORT, RTTHREADTYPE_DEFAULT, "server-1",
97 test1Server, NULL, &pServer), VINF_SUCCESS);
98
99 int rc;
100 RTSOCKET hSocket;
101 RTTESTI_CHECK_RC(rc = RTUdpCreateClientSocket(RT_TEST_UDP_CLIENT_ADDRESS, RT_TEST_UDP_CLIENT_PORT, &hSocket),
102 VINF_SUCCESS);
103 if (RT_SUCCESS(rc))
104 {
105 do /* break non-loop */
106 {
107 char szBuf[512];
108 RT_ZERO(szBuf);
109 RTTESTI_CHECK_RC_BREAK(RTSocketWriteTo(hSocket, "dude!\n", sizeof("dude!\n") - 1, &ServerAddress), VINF_SUCCESS);
110
111 RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("hello\n") - 1, NULL), VINF_SUCCESS);
112 szBuf[sizeof("hello!\n") - 1] = '\0';
113 RTTESTI_CHECK_BREAK(strcmp(szBuf, "hello\n") == 0);
114
115 RTTESTI_CHECK_RC_BREAK(RTSocketWriteTo(hSocket, "byebye\n", sizeof("byebye\n") - 1, &ServerAddress), VINF_SUCCESS);
116 RT_ZERO(szBuf);
117 RTTESTI_CHECK_RC_BREAK(RTSocketRead(hSocket, szBuf, sizeof("buh bye\n") - 1, NULL), VINF_SUCCESS);
118 RTTESTI_CHECK_BREAK(strcmp(szBuf, "buh bye\n") == 0);
119 } while (0);
120
121 RTTESTI_CHECK_RC(RTSocketClose(hSocket), VINF_SUCCESS);
122 }
123
124 RTTESTI_CHECK_RC(RTUdpServerDestroy(pServer), VINF_SUCCESS);
125}
126
127
128int main()
129{
130 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTUdp-1", &g_hTest);
131 if (rcExit != RTEXITCODE_SUCCESS)
132 return rcExit;
133 RTTestBanner(g_hTest);
134
135 test1();
136
137 /** @todo test the full RTUdp API. */
138
139 return RTTestSummaryAndDestroy(g_hTest);
140}
141
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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