forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawSocketTest.cpp
More file actions
107 lines (79 loc) · 2.1 KB
/
Copy pathRawSocketTest.cpp
File metadata and controls
107 lines (79 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// RawSocketTest.cpp
//
// $Id: //poco/1.4/Net/testsuite/src/RawSocketTest.cpp#1 $
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "RawSocketTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/RawSocket.h"
#include "Poco/Net/RawSocketImpl.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/NetException.h"
#include "Poco/Timespan.h"
#include "Poco/Stopwatch.h"
using Poco::Net::Socket;
using Poco::Net::RawSocket;
using Poco::Net::RawSocketImpl;
using Poco::Net::SocketAddress;
using Poco::Net::IPAddress;
using Poco::Timespan;
using Poco::Stopwatch;
using Poco::TimeoutException;
using Poco::InvalidArgumentException;
using Poco::IOException;
RawSocketTest::RawSocketTest(const std::string& name): CppUnit::TestCase(name)
{
}
RawSocketTest::~RawSocketTest()
{
}
void RawSocketTest::testEchoIPv4()
{
SocketAddress sa("localhost", 0);
RawSocket rs(IPAddress::IPv4);
rs.connect(sa);
int n = rs.sendBytes("hello", 5);
assert (5 == n);
char buffer[256] = "";
unsigned char* ptr = (unsigned char*) buffer;
n = rs.receiveBytes(buffer, sizeof(buffer));
int shift = ((buffer[0] & 0x0F) * 4);
ptr += shift;
assert (5 == (n - shift));
assert ("hello" == std::string((char*)ptr, 5));
rs.close();
}
void RawSocketTest::testSendToReceiveFromIPv4()
{
RawSocket rs(IPAddress::IPv4);
int n = rs.sendTo("hello", 5, SocketAddress("localhost", 0));
assert (n == 5);
char buffer[256] = "";
unsigned char* ptr = (unsigned char*) buffer;
SocketAddress sa;
n = rs.receiveFrom(buffer, sizeof(buffer), sa);
int shift = ((buffer[0] & 0x0F) * 4);
ptr += shift;
assert ((n - shift) == 5);
assert ("hello" == std::string((char*)ptr, 5));
rs.close();
}
void RawSocketTest::setUp()
{
}
void RawSocketTest::tearDown()
{
}
CppUnit::Test* RawSocketTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RawSocketTest");
CppUnit_addTest(pSuite, RawSocketTest, testEchoIPv4);
CppUnit_addTest(pSuite, RawSocketTest, testSendToReceiveFromIPv4);
return pSuite;
}