| 1 | // |
| 2 | // ICMPSocketTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "ICMPSocketTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "UDPEchoServer.h" |
| 15 | #include "Poco/Net/ICMPSocket.h" |
| 16 | #include "Poco/Net/ICMPPacketImpl.h" |
| 17 | #include "Poco/Net/SocketAddress.h" |
| 18 | #include "Poco/Net/NetException.h" |
| 19 | #include "Poco/Timespan.h" |
| 20 | #include "Poco/Stopwatch.h" |
| 21 | #include <iostream> |
| 22 | |
| 23 | |
| 24 | using Poco::Net::Socket; |
| 25 | using Poco::Net::ICMPSocket; |
| 26 | using Poco::Net::ICMPPacketImpl; |
| 27 | using Poco::Net::SocketAddress; |
| 28 | using Poco::Net::IPAddress; |
| 29 | using Poco::Net::ICMPException; |
| 30 | using Poco::Timespan; |
| 31 | using Poco::Stopwatch; |
| 32 | using Poco::TimeoutException; |
| 33 | using Poco::Net::NetException; |
| 34 | using Poco::Net::ICMPException; |
| 35 | using Poco::Exception; |
| 36 | using Poco::TimeoutException; |
| 37 | |
| 38 | |
| 39 | ICMPSocketTest::ICMPSocketTest(const std::string& name): CppUnit::TestCase(name) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | |
| 44 | ICMPSocketTest::~ICMPSocketTest() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void ICMPSocketTest::testAssign() |
| 50 | { |
| 51 | ICMPSocket s1(IPAddress::IPv4); |
| 52 | ICMPSocket s2(s1); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void ICMPSocketTest::testSendToReceiveFrom() |
| 57 | { |
| 58 | ICMPSocket ss(IPAddress::IPv4); |
| 59 | |
| 60 | SocketAddress sa("www.appinf.com" , 0); |
| 61 | SocketAddress sr(sa); |
| 62 | |
| 63 | try |
| 64 | { |
| 65 | ss.receiveFrom(sa); |
| 66 | fail("must throw" ); |
| 67 | } |
| 68 | catch (ICMPException&) { } |
| 69 | catch (TimeoutException&) { } |
| 70 | catch (Exception&) { } |
| 71 | |
| 72 | ss.sendTo(sa); |
| 73 | ss.receiveFrom(sa); |
| 74 | |
| 75 | assertTrue (sr.host().toString() == sa.host().toString()); |
| 76 | ss.close(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void ICMPSocketTest::testMTU() |
| 81 | { |
| 82 | try |
| 83 | { |
| 84 | Poco::UInt16 sz = ICMPPacketImpl::MAX_PAYLOAD_SIZE + 1; |
| 85 | SocketAddress addr("127.0.0.1:0" ); |
| 86 | Poco::UInt16 mtu = ICMPSocket::mtu(addr, sz); |
| 87 | std::cout << addr.toString() << " : MTU=" << mtu << std::endl; |
| 88 | assertTrue (mtu != 0 && mtu <= ICMPPacketImpl::MAX_PAYLOAD_SIZE); |
| 89 | sz = ICMPPacketImpl::MAX_PAYLOAD_SIZE; |
| 90 | mtu = ICMPSocket::mtu(addr, sz); |
| 91 | std::cout << addr.toString() << " : MTU=" << mtu << std::endl; |
| 92 | assertTrue (mtu != 0); |
| 93 | sz = 1500; |
| 94 | addr = SocketAddress("www.appinf.com:0" ); |
| 95 | mtu = ICMPSocket::mtu(addr, sz); |
| 96 | std::cout << addr.toString() << " : MTU=" << mtu << std::endl; |
| 97 | assertTrue (mtu != 0 && mtu <= sz); |
| 98 | } |
| 99 | catch (Poco::NotImplementedException& ex) |
| 100 | { |
| 101 | std::cerr << ex.displayText() << std::endl; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void ICMPSocketTest::setUp() |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void ICMPSocketTest::tearDown() |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | |
| 116 | CppUnit::Test* ICMPSocketTest::suite() |
| 117 | { |
| 118 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ICMPSocketTest" ); |
| 119 | |
| 120 | CppUnit_addTest(pSuite, ICMPSocketTest, testSendToReceiveFrom); |
| 121 | CppUnit_addTest(pSuite, ICMPSocketTest, testAssign); |
| 122 | CppUnit_addTest(pSuite, ICMPSocketTest, testMTU); |
| 123 | |
| 124 | return pSuite; |
| 125 | } |
| 126 | |