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