1 | // |
2 | // ICMPClientTest.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 "ICMPClientTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Net/ICMPSocket.h" |
15 | #include "Poco/Net/ICMPClient.h" |
16 | #include "Poco/Net/ICMPEventArgs.h" |
17 | #include "Poco/Net/SocketAddress.h" |
18 | #include "Poco/Net/NetException.h" |
19 | #include "Poco/AutoPtr.h" |
20 | #include "Poco/Delegate.h" |
21 | #include <sstream> |
22 | #include <iostream> |
23 | |
24 | |
25 | using Poco::Net::ICMPSocket; |
26 | using Poco::Net::ICMPClient; |
27 | using Poco::Net::ICMPEventArgs; |
28 | using Poco::Net::SocketAddress; |
29 | using Poco::Net::IPAddress; |
30 | using Poco::Net::HostNotFoundException; |
31 | using Poco::Delegate; |
32 | using Poco::AutoPtr; |
33 | |
34 | |
35 | Poco::FastMutex ICMPClientTest::_mutex; |
36 | |
37 | |
38 | ICMPClientTest::ICMPClientTest(const std::string& name): |
39 | CppUnit::TestCase(name) |
40 | { |
41 | } |
42 | |
43 | |
44 | ICMPClientTest::~ICMPClientTest() |
45 | { |
46 | } |
47 | |
48 | |
49 | void ICMPClientTest::testPing() |
50 | { |
51 | assertTrue (ICMPClient::pingIPv4("127.0.0.1" ) > 0); |
52 | |
53 | Poco::Net::ICMPClient icmpClient(IPAddress::IPv4); |
54 | |
55 | registerDelegates(icmpClient); |
56 | |
57 | assertTrue (icmpClient.ping("127.0.0.1" ) > 0); |
58 | #if POCO_OS == POCO_OS_ANDROID |
59 | assertTrue (icmpClient.ping("10.0.2.15" , 4) > 0); |
60 | assertTrue (icmpClient.ping("10.0.2.2" , 4) > 0); |
61 | #else |
62 | assertTrue (icmpClient.ping("www.appinf.com" , 4) > 0); |
63 | |
64 | // warning: may fail depending on the existence of the addresses at test site |
65 | // if so, adjust accordingly (i.e. specify non-existent or unreachable IP addresses) |
66 | assertTrue (0 == icmpClient.ping("192.168.243.1" )); |
67 | assertTrue (0 == icmpClient.ping("10.11.12.13" )); |
68 | #endif |
69 | |
70 | unregisterDelegates(icmpClient); |
71 | // wait for delegates to finish printing |
72 | Poco::FastMutex::ScopedLock l(_mutex); |
73 | } |
74 | |
75 | |
76 | void ICMPClientTest::testBigPing() |
77 | { |
78 | assertTrue (ICMPClient::pingIPv4("127.0.0.1" , 1, 96) > 0); |
79 | |
80 | Poco::Net::ICMPClient icmpClient(IPAddress::IPv4, 96); |
81 | |
82 | registerDelegates(icmpClient); |
83 | |
84 | assertTrue (icmpClient.ping("127.0.0.1" , 1) > 0); |
85 | #if POCO_OS == POCO_OS_ANDROID |
86 | assertTrue (icmpClient.ping("10.0.2.15" , 4) > 0); |
87 | assertTrue (icmpClient.ping("10.0.2.2" , 4) > 0); |
88 | #else |
89 | assertTrue (icmpClient.ping("www.appinf.com" , 4) > 0); |
90 | |
91 | // warning: may fail depending on the existence of the addresses at test site |
92 | // if so, adjust accordingly (i.e. specify non-existent or unreachable IP addresses) |
93 | assertTrue (0 == icmpClient.ping("192.168.243.1" )); |
94 | assertTrue (0 == icmpClient.ping("10.11.12.13" )); |
95 | #endif |
96 | |
97 | unregisterDelegates(icmpClient); |
98 | // wait for delegates to finish printing |
99 | Poco::FastMutex::ScopedLock l(_mutex); |
100 | } |
101 | |
102 | |
103 | void ICMPClientTest::registerDelegates(const ICMPClient& icmpClient) |
104 | { |
105 | icmpClient.pingBegin += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onBegin); |
106 | icmpClient.pingReply += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onReply); |
107 | icmpClient.pingError += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onError); |
108 | icmpClient.pingEnd += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onEnd); |
109 | } |
110 | |
111 | |
112 | void ICMPClientTest::unregisterDelegates(const ICMPClient& icmpClient) |
113 | { |
114 | icmpClient.pingBegin -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onBegin); |
115 | icmpClient.pingReply -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onReply); |
116 | icmpClient.pingError -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onError); |
117 | icmpClient.pingEnd -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onEnd); |
118 | } |
119 | |
120 | |
121 | void ICMPClientTest::setUp() |
122 | { |
123 | } |
124 | |
125 | |
126 | void ICMPClientTest::tearDown() |
127 | { |
128 | } |
129 | |
130 | |
131 | void ICMPClientTest::onBegin(const void* pSender, ICMPEventArgs& args) |
132 | { |
133 | Poco::FastMutex::ScopedLock l(_mutex); |
134 | std::ostringstream os; |
135 | os << std::endl << "Pinging " << args.hostName() << " [" << args.hostAddress() << "] with " |
136 | << args.dataSize() << " bytes of data:" |
137 | << std::endl << "-------------------------------------------------------" << std::endl; |
138 | std::cout << os.str() << std::endl; |
139 | } |
140 | |
141 | |
142 | void ICMPClientTest::onReply(const void* pSender, ICMPEventArgs& args) |
143 | { |
144 | Poco::FastMutex::ScopedLock l(_mutex); |
145 | std::ostringstream os; |
146 | os << "Reply from " << args.hostAddress() |
147 | << " bytes=" << args.dataSize() |
148 | << " time=" << args.replyTime() << "ms" |
149 | << " TTL=" << args.ttl(); |
150 | std::cout << os.str() << std::endl; |
151 | } |
152 | |
153 | |
154 | void ICMPClientTest::onError(const void* pSender, ICMPEventArgs& args) |
155 | { |
156 | Poco::FastMutex::ScopedLock l(_mutex); |
157 | std::ostringstream os; |
158 | os << args.error(); |
159 | std::cerr << os.str() << std::endl; |
160 | } |
161 | |
162 | |
163 | void ICMPClientTest::onEnd(const void* pSender, ICMPEventArgs& args) |
164 | { |
165 | Poco::FastMutex::ScopedLock l(_mutex); |
166 | std::ostringstream os; |
167 | int received = args.received(); |
168 | os << std::endl << "--- Ping statistics for " << args.hostAddress() << " ---" |
169 | << std::endl << "Packets: Sent=" << args.sent() << ", Received=" << received |
170 | << " Lost=" << args.repetitions() - received << " (" << 100.0 - args.percent() << "% loss)," |
171 | << std::endl << "Approximate round trip times in milliseconds: " << std::endl |
172 | << "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT() |
173 | << "ms, Average=" << args.avgRTT() << "ms" |
174 | << std::endl << "-----------------------------------------------" << std::endl; |
175 | std::cout << os.str() << std::endl; |
176 | } |
177 | |
178 | |
179 | CppUnit::Test* ICMPClientTest::suite() |
180 | { |
181 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ICMPClientTest" ); |
182 | |
183 | CppUnit_addTest(pSuite, ICMPClientTest, testPing); |
184 | CppUnit_addTest(pSuite, ICMPClientTest, testBigPing); |
185 | |
186 | return pSuite; |
187 | } |
188 | |