File: | usr/src/googletest/googletest/include/gtest/gtest.h |
Warning: | line 1457, column 1 An undefined value may be read from 'errno' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | // Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC") | |||
2 | // | |||
3 | // This Source Code Form is subject to the terms of the Mozilla Public | |||
4 | // License, v. 2.0. If a copy of the MPL was not distributed with this | |||
5 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. | |||
6 | ||||
7 | #include <config.h> | |||
8 | ||||
9 | #include <asiolink/io_address.h> | |||
10 | #include <dhcp/dhcp4.h> | |||
11 | #include <dhcp/iface_mgr.h> | |||
12 | #include <dhcp/option.h> | |||
13 | #include <dhcp/pkt6.h> | |||
14 | #include <dhcp/pkt_filter.h> | |||
15 | #include <dhcp/testutils/iface_mgr_test_config.h> | |||
16 | #include <dhcp/tests/pkt_filter6_test_utils.h> | |||
17 | #include <dhcp/tests/packet_queue_testutils.h> | |||
18 | #include <testutils/gtest_utils.h> | |||
19 | ||||
20 | #include <boost/scoped_ptr.hpp> | |||
21 | #include <gtest/gtest.h> | |||
22 | ||||
23 | #include <fcntl.h> | |||
24 | #include <fstream> | |||
25 | #include <functional> | |||
26 | #include <iostream> | |||
27 | #include <sstream> | |||
28 | ||||
29 | #include <arpa/inet.h> | |||
30 | #include <unistd.h> | |||
31 | ||||
32 | using namespace std; | |||
33 | using namespace isc; | |||
34 | using namespace isc::asiolink; | |||
35 | using namespace isc::dhcp; | |||
36 | using namespace isc::dhcp::test; | |||
37 | using boost::scoped_ptr; | |||
38 | namespace ph = std::placeholders; | |||
39 | ||||
40 | namespace { | |||
41 | ||||
42 | // Note this is for the *real* loopback interface, *not* the fake one. | |||
43 | // So in tests using it you have LOOPBACK_NAME, LOOPBACK_INDEX and | |||
44 | // no "eth0" nor "eth1". In tests not using it you can have "lo", LO_INDEX, | |||
45 | // "eth0" or "eth1". | |||
46 | // Name of loopback interface detection. | |||
47 | const size_t BUF_SIZE = 32; | |||
48 | // Can be overwritten to "lo0" for instance on BSD systems. | |||
49 | char LOOPBACK_NAME[BUF_SIZE] = "lo"; | |||
50 | // In fact is never 0, 1 is by far the most likely. | |||
51 | uint32_t LOOPBACK_INDEX = 0; | |||
52 | ||||
53 | // Ports used during testing | |||
54 | const uint16_t PORT1 = 10547; // V6 socket | |||
55 | const uint16_t PORT2 = 10548; // V4 socket | |||
56 | ||||
57 | // On some systems measured duration of receive6() and receive4() appears to be | |||
58 | // shorter than select() timeout. This may be the case if different time | |||
59 | // resolutions are used by these functions. For such cases we set the | |||
60 | // tolerance to 0.01s. | |||
61 | const uint32_t TIMEOUT_TOLERANCE = 10000; | |||
62 | ||||
63 | // Macro for making select wait time arguments for receive functions | |||
64 | #define RECEIVE_WAIT_MS(m)0,(m*1000) 0,(m*1000) | |||
65 | ||||
66 | /// This test verifies that the socket read buffer can be used to | |||
67 | /// receive the data and that the data can be read from it. | |||
68 | TEST(IfaceTest, readBuffer)static_assert(sizeof("IfaceTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("readBuffer") > 1, "test_name must not be empty" ); class IfaceTest_readBuffer_Test : public ::testing::Test { public: IfaceTest_readBuffer_Test() = default; ~IfaceTest_readBuffer_Test () override = default; IfaceTest_readBuffer_Test (const IfaceTest_readBuffer_Test &) = delete; IfaceTest_readBuffer_Test & operator=( const IfaceTest_readBuffer_Test &) = delete; IfaceTest_readBuffer_Test (IfaceTest_readBuffer_Test &&) noexcept = delete; IfaceTest_readBuffer_Test & operator=( IfaceTest_readBuffer_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceTest_readBuffer_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceTest", "readBuffer" , nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 68), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("iface_mgr_unittest.cc", 68), ::testing::internal::SuiteApiResolver < ::testing::Test>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 68), new ::testing::internal::TestFactoryImpl<IfaceTest_readBuffer_Test >); void IfaceTest_readBuffer_Test::TestBody() { | |||
69 | // Create fake interface object. | |||
70 | Iface iface("em0", 0); | |||
71 | // The size of read buffer should initially be 0 and the returned | |||
72 | // pointer should be NULL. | |||
73 | ASSERT_EQ(0, iface.getReadBufferSize())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "iface.getReadBufferSize()" , 0, iface.getReadBufferSize()))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 73, gtest_ar.failure_message()) = ::testing::Message(); | |||
74 | EXPECT_EQ(NULL, iface.getReadBuffer())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("__null", "iface.getReadBuffer()", __null, iface.getReadBuffer()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 74, gtest_ar.failure_message ()) = ::testing::Message(); | |||
75 | ||||
76 | // Let's resize the buffer. | |||
77 | iface.resizeReadBuffer(256); | |||
78 | // Check that the buffer has expected size. | |||
79 | ASSERT_EQ(256, iface.getReadBufferSize())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("256", "iface.getReadBufferSize()" , 256, iface.getReadBufferSize()))) ; else return ::testing:: internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "iface_mgr_unittest.cc", 79, gtest_ar.failure_message()) = :: testing::Message(); | |||
80 | // The returned pointer should now be non-NULL. | |||
81 | uint8_t* buf_ptr = iface.getReadBuffer(); | |||
82 | ASSERT_FALSE(buf_ptr == NULL)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(buf_ptr == __null)) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 82, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "buf_ptr == __null", "true", "false") .c_str()) = ::testing:: Message(); | |||
83 | ||||
84 | // Use the pointer to set some data. | |||
85 | for (size_t i = 0; i < iface.getReadBufferSize(); ++i) { | |||
86 | buf_ptr[i] = i; | |||
87 | } | |||
88 | ||||
89 | // Get the pointer again and validate the data. | |||
90 | buf_ptr = iface.getReadBuffer(); | |||
91 | ASSERT_EQ(256, iface.getReadBufferSize())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("256", "iface.getReadBufferSize()" , 256, iface.getReadBufferSize()))) ; else return ::testing:: internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "iface_mgr_unittest.cc", 91, gtest_ar.failure_message()) = :: testing::Message(); | |||
92 | for (size_t i = 0; i < iface.getReadBufferSize(); ++i) { | |||
93 | // Use assert so as it fails on the first failure, no need | |||
94 | // to continue further checks. | |||
95 | ASSERT_EQ(i, buf_ptr[i])switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("i", "buf_ptr[i]" , i, buf_ptr[i]))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 95, gtest_ar.failure_message()) = ::testing::Message(); | |||
96 | } | |||
97 | } | |||
98 | ||||
99 | // Check that counting the number of active addresses on the interface | |||
100 | // works as expected. | |||
101 | TEST(IfaceTest, countActive4)static_assert(sizeof("IfaceTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("countActive4") > 1, "test_name must not be empty" ); class IfaceTest_countActive4_Test : public ::testing::Test { public: IfaceTest_countActive4_Test() = default; ~IfaceTest_countActive4_Test () override = default; IfaceTest_countActive4_Test (const IfaceTest_countActive4_Test &) = delete; IfaceTest_countActive4_Test & operator= ( const IfaceTest_countActive4_Test &) = delete; IfaceTest_countActive4_Test (IfaceTest_countActive4_Test &&) noexcept = delete; IfaceTest_countActive4_Test & operator=( IfaceTest_countActive4_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceTest_countActive4_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "IfaceTest", "countActive4" , nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 101), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("iface_mgr_unittest.cc", 101), ::testing::internal::SuiteApiResolver < ::testing::Test>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 101), new ::testing::internal::TestFactoryImpl<IfaceTest_countActive4_Test >); void IfaceTest_countActive4_Test::TestBody() { | |||
102 | Iface iface("eth0", 0); | |||
103 | ASSERT_EQ(0, iface.countActive4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "iface.countActive4()" , 0, iface.countActive4()))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 103, gtest_ar.failure_message()) = ::testing::Message(); | |||
104 | ||||
105 | iface.addAddress(IOAddress("192.168.0.2")); | |||
106 | ASSERT_EQ(1, iface.countActive4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "iface.countActive4()" , 1, iface.countActive4()))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 106, gtest_ar.failure_message()) = ::testing::Message(); | |||
107 | ||||
108 | iface.addAddress(IOAddress("2001:db8:1::1")); | |||
109 | ASSERT_EQ(1, iface.countActive4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "iface.countActive4()" , 1, iface.countActive4()))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 109, gtest_ar.failure_message()) = ::testing::Message(); | |||
110 | ||||
111 | iface.addAddress(IOAddress("192.168.0.3")); | |||
112 | ASSERT_EQ(2, iface.countActive4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("2", "iface.countActive4()" , 2, iface.countActive4()))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 112, gtest_ar.failure_message()) = ::testing::Message(); | |||
113 | ||||
114 | ASSERT_NO_THROW(iface.setActive(IOAddress("192.168.0.2"), false))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface.setActive(IOAddress("192.168.0.2"), false); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_114; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_114; } } else gtest_label_testnothrow_114 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 114, ("Expected: " "iface.setActive(IOAddress(\"192.168.0.2\"), false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
115 | ASSERT_EQ(1, iface.countActive4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "iface.countActive4()" , 1, iface.countActive4()))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 115, gtest_ar.failure_message()) = ::testing::Message(); | |||
116 | ||||
117 | ASSERT_NO_THROW(iface.setActive(IOAddress("192.168.0.3"), false))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface.setActive(IOAddress("192.168.0.3"), false); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_117; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_117; } } else gtest_label_testnothrow_117 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 117, ("Expected: " "iface.setActive(IOAddress(\"192.168.0.3\"), false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
118 | ASSERT_EQ(0, iface.countActive4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "iface.countActive4()" , 0, iface.countActive4()))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 118, gtest_ar.failure_message()) = ::testing::Message(); | |||
119 | } | |||
120 | ||||
121 | /// Mock object implementing PktFilter class. It is used by | |||
122 | /// IfaceMgrTest::setPacketFilter to verify that IfaceMgr::setPacketFilter | |||
123 | /// sets this object as a handler for opening sockets. This dummy | |||
124 | /// class simply records that openSocket function was called by | |||
125 | /// the IfaceMgr as expected. | |||
126 | /// | |||
127 | /// @todo This class currently doesn't verify that send/receive functions | |||
128 | /// were called. In order to test it, there is a need to supply dummy | |||
129 | /// function performing select() on certain sockets. The system select() | |||
130 | /// call will fail when dummy socket descriptor is provided and thus | |||
131 | /// TestPktFilter::receive will never be called. The appropriate extension | |||
132 | /// to IfaceMgr is planned along with implementation of other "Packet | |||
133 | /// Filters" such as these supporting Linux Packet Filtering and | |||
134 | /// Berkeley Packet Filtering. | |||
135 | class TestPktFilter : public PktFilter { | |||
136 | public: | |||
137 | ||||
138 | /// Constructor | |||
139 | TestPktFilter() | |||
140 | : open_socket_called_(false) { | |||
141 | } | |||
142 | ||||
143 | virtual bool isDirectResponseSupported() const { | |||
144 | return (false); | |||
145 | } | |||
146 | ||||
147 | virtual bool isSocketReceivedTimeSupported() const { | |||
148 | return (false); | |||
149 | } | |||
150 | ||||
151 | /// @brief Pretend to open a socket. | |||
152 | /// | |||
153 | /// This function doesn't open a real socket. It always returns the | |||
154 | /// same fake socket descriptor. It also records the fact that it has | |||
155 | /// been called in the public open_socket_called_ member. | |||
156 | /// As in the case of opening a real socket, this function will check | |||
157 | /// if there is another fake socket "bound" to the same address and port. | |||
158 | /// If there is, it will throw an exception. This allows to simulate the | |||
159 | /// conditions when one of the sockets can't be open because there is | |||
160 | /// a socket already open and test how IfaceMgr will handle it. | |||
161 | /// | |||
162 | /// @param iface An interface on which the socket is to be opened. | |||
163 | /// @param addr An address to which the socket is to be bound. | |||
164 | /// @param port A port to which the socket is to be bound. | |||
165 | virtual SocketInfo openSocket(Iface& iface, | |||
166 | const isc::asiolink::IOAddress& addr, | |||
167 | const uint16_t port, | |||
168 | const bool join_multicast, | |||
169 | const bool) { | |||
170 | // Check if there is any other socket bound to the specified address | |||
171 | // and port on this interface. | |||
172 | const Iface::SocketCollection& sockets = iface.getSockets(); | |||
173 | for (auto const& socket : sockets) { | |||
174 | if (((socket.addr_ == addr) || | |||
175 | ((socket.addr_ == IOAddress("::")) && join_multicast)) && | |||
176 | socket.port_ == port) { | |||
177 | isc_throw(SocketConfigError, "test socket bind error")do { std::ostringstream oss__; oss__ << "test socket bind error" ; throw SocketConfigError("iface_mgr_unittest.cc", 177, oss__ .str().c_str()); } while (1); | |||
178 | } | |||
179 | } | |||
180 | open_socket_called_ = true; | |||
181 | return (SocketInfo(addr, port, 255)); | |||
182 | } | |||
183 | ||||
184 | /// Does nothing | |||
185 | virtual Pkt4Ptr receive(Iface&, const SocketInfo&) { | |||
186 | return (Pkt4Ptr()); | |||
187 | } | |||
188 | ||||
189 | /// Does nothing | |||
190 | virtual int send(const Iface&, uint16_t, const Pkt4Ptr&) { | |||
191 | return (0); | |||
192 | } | |||
193 | ||||
194 | /// Holds the information whether openSocket was called on this | |||
195 | /// object after its creation. | |||
196 | bool open_socket_called_; | |||
197 | }; | |||
198 | ||||
199 | class NakedIfaceMgr: public IfaceMgr { | |||
200 | // "Naked" Interface Manager, exposes internal fields | |||
201 | public: | |||
202 | ||||
203 | /// @brief Constructor. | |||
204 | NakedIfaceMgr() { | |||
205 | loDetect(); | |||
206 | } | |||
207 | ||||
208 | /// @brief detects name of the loopback interface | |||
209 | /// | |||
210 | /// This method detects name of the loopback interface. | |||
211 | static void loDetect() { | |||
212 | // Poor man's interface detection. It will go away as soon as proper | |||
213 | // interface detection is implemented | |||
214 | if (if_nametoindex("lo") > 0) { | |||
215 | snprintf(LOOPBACK_NAME, BUF_SIZE - 1, "lo"); | |||
216 | } else if (if_nametoindex("lo0") > 0) { | |||
217 | snprintf(LOOPBACK_NAME, BUF_SIZE - 1, "lo0"); | |||
218 | } else { | |||
219 | cout << "Failed to detect loopback interface. Neither " | |||
220 | << "lo nor lo0 worked. I give up." << endl; | |||
221 | FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 221, "Failed") = :: testing::Message(); | |||
222 | } | |||
223 | LOOPBACK_INDEX = if_nametoindex(LOOPBACK_NAME); | |||
224 | } | |||
225 | ||||
226 | /// @brief Returns the collection of existing interfaces. | |||
227 | IfaceCollection& getIfacesLst() { return (ifaces_); } | |||
228 | ||||
229 | /// @brief This function creates fictitious interfaces with fictitious | |||
230 | /// addresses. | |||
231 | /// | |||
232 | /// These interfaces can be used in tests that don't actually try | |||
233 | /// to open the sockets on these interfaces. Some tests use mock | |||
234 | /// objects to mimic sockets being open. These interfaces are | |||
235 | /// suitable for such tests. | |||
236 | void createIfaces() { | |||
237 | ||||
238 | ifaces_.clear(); | |||
239 | ||||
240 | // local loopback | |||
241 | IfacePtr lo = createIface("lo", LO_INDEX); | |||
242 | lo->addAddress(IOAddress("127.0.0.1")); | |||
243 | lo->addAddress(IOAddress("::1")); | |||
244 | ifaces_.push_back(lo); | |||
245 | // eth0 | |||
246 | IfacePtr eth0 = createIface("eth0", ETH0_INDEX); | |||
247 | eth0->addAddress(IOAddress("10.0.0.1")); | |||
248 | eth0->addAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")); | |||
249 | eth0->addAddress(IOAddress("2001:db8:1::1")); | |||
250 | ifaces_.push_back(eth0); | |||
251 | // eth1 | |||
252 | IfacePtr eth1 = createIface("eth1", ETH1_INDEX); | |||
253 | eth1->addAddress(IOAddress("192.0.2.3")); | |||
254 | eth1->addAddress(IOAddress("fe80::3a60:77ff:fed5:abcd")); | |||
255 | ifaces_.push_back(eth1); | |||
256 | } | |||
257 | ||||
258 | /// @brief Create an object representing interface. | |||
259 | /// | |||
260 | /// Apart from creating an interface, this function also sets the | |||
261 | /// interface flags: | |||
262 | /// - loopback flag if interface name is "lo" | |||
263 | /// - up always true | |||
264 | /// - running always true | |||
265 | /// - inactive always to false | |||
266 | /// - multicast always to true | |||
267 | /// - broadcast always to false | |||
268 | /// | |||
269 | /// If one needs to modify the default flag settings, the setIfaceFlags | |||
270 | /// function should be used. | |||
271 | /// | |||
272 | /// @param name A name of the interface to be created. | |||
273 | /// @param ifindex An index of the interface to be created. | |||
274 | /// | |||
275 | /// @return An object representing interface. | |||
276 | static IfacePtr createIface(const std::string& name, const unsigned int ifindex) { | |||
277 | IfacePtr iface(new Iface(name, ifindex)); | |||
278 | if (name == "lo") { | |||
279 | iface->flag_loopback_ = true; | |||
280 | // Don't open sockets on loopback interface. | |||
281 | iface->inactive4_ = true; | |||
282 | iface->inactive6_ = true; | |||
283 | } else { | |||
284 | iface->inactive4_ = false; | |||
285 | iface->inactive6_ = false; | |||
286 | } | |||
287 | iface->flag_multicast_ = true; | |||
288 | // On BSD systems, the SO_BINDTODEVICE option is not supported. | |||
289 | // Therefore the IfaceMgr will throw an exception on attempt to | |||
290 | // open sockets on more than one broadcast-capable interface at | |||
291 | // the same time. In order to prevent this error, we mark all | |||
292 | // interfaces broadcast-incapable for unit testing. | |||
293 | iface->flag_broadcast_ = false; | |||
294 | iface->flag_up_ = true; | |||
295 | iface->flag_running_ = true; | |||
296 | return (iface); | |||
297 | } | |||
298 | ||||
299 | /// @brief Checks if the specified interface has a socket bound to a | |||
300 | /// specified address. | |||
301 | /// | |||
302 | /// @param iface_name A name of the interface. | |||
303 | /// @param addr An address to be checked for binding. | |||
304 | /// | |||
305 | /// @return true if there is a socket bound to the specified address. | |||
306 | bool isBound(const std::string& iface_name, const std::string& addr) { | |||
307 | IfacePtr iface = getIface(iface_name); | |||
308 | if (!iface) { | |||
309 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 309, "Failed") = :: testing::Message() << "the interface " << iface_name << " doesn't exist"; | |||
310 | return (false); | |||
311 | } | |||
312 | const Iface::SocketCollection& sockets = iface->getSockets(); | |||
313 | for (auto const& sock : sockets) { | |||
314 | if (sock.addr_ == IOAddress(addr)) { | |||
315 | return (true); | |||
316 | ||||
317 | } else if ((sock.addr_ == IOAddress("::")) && | |||
318 | (IOAddress(addr).isV6LinkLocal())) { | |||
319 | for (auto const& a : iface->getAddresses()) { | |||
320 | if (a.get() == IOAddress(addr)) { | |||
321 | return (true); | |||
322 | } | |||
323 | } | |||
324 | } | |||
325 | } | |||
326 | return (false); | |||
327 | } | |||
328 | ||||
329 | /// @brief Modify flags on the interface. | |||
330 | /// | |||
331 | /// @param name A name of the interface. | |||
332 | /// @param loopback A new value of the loopback flag. | |||
333 | /// @param up A new value of the up flag. | |||
334 | /// @param running A new value of the running flag. | |||
335 | /// @param inactive A new value of the inactive flag. | |||
336 | void setIfaceFlags(const std::string& name, const bool loopback, | |||
337 | const bool up, const bool running, | |||
338 | const bool inactive4, | |||
339 | const bool inactive6) { | |||
340 | for (auto const& iface : ifaces_) { | |||
341 | if (iface->getName() == name) { | |||
342 | iface->flag_loopback_ = loopback; | |||
343 | iface->flag_up_ = up; | |||
344 | iface->flag_running_ = running; | |||
345 | iface->inactive4_ = inactive4; | |||
346 | iface->inactive6_ = inactive6; | |||
347 | } | |||
348 | } | |||
349 | } | |||
350 | }; | |||
351 | ||||
352 | /// @brief A test fixture class for IfaceMgr. | |||
353 | /// | |||
354 | /// @todo Sockets being opened by IfaceMgr tests should be managed by | |||
355 | /// the test fixture. In particular, the class should close sockets after | |||
356 | /// each test. Current approach where test cases are responsible for | |||
357 | /// closing sockets is resource leak prone, especially in case of the | |||
358 | /// test failure path. | |||
359 | class IfaceMgrTest : public ::testing::Test { | |||
360 | public: | |||
361 | /// @brief Constructor. | |||
362 | IfaceMgrTest() | |||
363 | : errors_count_(0) { | |||
364 | } | |||
365 | ||||
366 | ~IfaceMgrTest() { | |||
367 | } | |||
368 | ||||
369 | /// @brief Tests the number of IPv6 sockets on interface | |||
370 | /// | |||
371 | /// This function checks the expected number of open IPv6 sockets on the | |||
372 | /// specified interface. On non-Linux systems, sockets are bound to a | |||
373 | /// link-local address and the number of unicast addresses specified. | |||
374 | /// On Linux systems, there is one more socket bound to a ff02::1:2 | |||
375 | /// multicast address. | |||
376 | /// | |||
377 | /// @param iface An interface on which sockets are open. | |||
378 | /// @param unicast_num A number of unicast addresses bound. | |||
379 | /// @param link_local_num A number of link local addresses bound. | |||
380 | void checkSocketsCount6(const Iface& iface, const int unicast_num, | |||
381 | const int link_local_num = 1) { | |||
382 | // On local-loopback interface, there should be no sockets. | |||
383 | if (iface.flag_loopback_) { | |||
384 | ASSERT_TRUE(iface.getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface.getSockets().empty ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 384 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface.getSockets().empty()", "false", "true") .c_str()) = :: testing::Message() | |||
385 | << "expected empty socket set on loopback interface " | |||
386 | << iface.getName(); | |||
387 | return; | |||
388 | } | |||
389 | #if defined OS_LINUX1 | |||
390 | // On Linux, for each link-local address there may be an | |||
391 | // additional socket opened and bound to ff02::1:2. This socket | |||
392 | // is only opened if the interface is multicast-capable. | |||
393 | ASSERT_EQ(unicast_num + (iface.flag_multicast_ ? link_local_num : 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("unicast_num + (iface.flag_multicast_ ? link_local_num : 0) + link_local_num" , "iface.getSockets().size()", unicast_num + (iface.flag_multicast_ ? link_local_num : 0) + link_local_num, iface.getSockets().size ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 394 , gtest_ar.failure_message()) = ::testing::Message() | |||
394 | + link_local_num, iface.getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("unicast_num + (iface.flag_multicast_ ? link_local_num : 0) + link_local_num" , "iface.getSockets().size()", unicast_num + (iface.flag_multicast_ ? link_local_num : 0) + link_local_num, iface.getSockets().size ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 394 , gtest_ar.failure_message()) = ::testing::Message() | |||
395 | << "invalid number of sockets on interface " | |||
396 | << iface.getName(); | |||
397 | #else | |||
398 | // On non-Linux, there is no additional socket. | |||
399 | ASSERT_EQ(unicast_num + link_local_num, iface.getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("unicast_num + link_local_num" , "iface.getSockets().size()", unicast_num + link_local_num, iface .getSockets().size()))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 399, gtest_ar.failure_message()) = ::testing::Message() | |||
400 | << "invalid number of sockets on interface " | |||
401 | << iface.getName(); | |||
402 | ||||
403 | #endif | |||
404 | } | |||
405 | ||||
406 | // Get the number of IPv4 or IPv6 sockets on the loopback interface | |||
407 | int getOpenSocketsCount(const Iface& iface, uint16_t family) const { | |||
408 | // Get all sockets. | |||
409 | Iface::SocketCollection sockets = iface.getSockets(); | |||
410 | ||||
411 | // Loop through sockets and try to find the ones which match the | |||
412 | // specified type. | |||
413 | int sockets_count = 0; | |||
414 | for (auto const& sock : sockets) { | |||
415 | // Match found, increase the counter. | |||
416 | if (sock.family_ == family) { | |||
417 | ++sockets_count; | |||
418 | } | |||
419 | } | |||
420 | return (sockets_count); | |||
421 | } | |||
422 | ||||
423 | /// @brief returns socket bound to a specific address (or NULL) | |||
424 | /// | |||
425 | /// A helper function, used to pick a socketinfo that is bound to a given | |||
426 | /// address. | |||
427 | /// | |||
428 | /// @param sockets sockets collection | |||
429 | /// @param addr address the socket is bound to | |||
430 | /// | |||
431 | /// @return socket info structure (or NULL) | |||
432 | const isc::dhcp::SocketInfo* | |||
433 | getSocketByAddr(const isc::dhcp::Iface::SocketCollection& sockets, | |||
434 | const IOAddress& addr) { | |||
435 | for (auto const& s : sockets) { | |||
436 | if (s.addr_ == addr) { | |||
437 | return (&s); | |||
438 | } | |||
439 | } | |||
440 | return (NULL__null); | |||
441 | } | |||
442 | ||||
443 | /// @brief Implements an IfaceMgr error handler. | |||
444 | /// | |||
445 | /// This function can be installed as an error handler for the | |||
446 | /// IfaceMgr::openSockets4 function. The error handler is invoked | |||
447 | /// when an attempt to open a particular socket fails for any reason. | |||
448 | /// Typically, the error handler will log a warning. When the error | |||
449 | /// handler returns, the openSockets4 function should continue opening | |||
450 | /// sockets on other interfaces. | |||
451 | /// | |||
452 | /// @param errmsg An error string indicating the reason for failure. | |||
453 | void ifaceMgrErrorHandler(const std::string&) { | |||
454 | // Increase the counter of invocations to this function. By checking | |||
455 | // this number, a test may check if the expected number of errors | |||
456 | // has occurred. | |||
457 | ++errors_count_; | |||
458 | } | |||
459 | ||||
460 | /// @brief Tests the ability to send and receive DHCPv6 packets | |||
461 | /// | |||
462 | /// This test calls @r IfaceMgr::configureDHCPPacketQueue, passing in the | |||
463 | /// given queue configuration. It then calls IfaceMgr::startDHCPReceiver | |||
464 | /// and verifies whether or not the receive thread has been started as | |||
465 | /// expected. Next it creates a generic DHCPv6 packet and sends it over | |||
466 | /// the loop back interface. It invokes IfaceMgr::receive6 to receive the | |||
467 | /// packet sent, and compares to the packets for equality. | |||
468 | /// | |||
469 | /// @param dhcp_queue_control dhcp-queue-control contents to use for the test | |||
470 | /// @param exp_queue_enabled flag that indicates if packet queuing is expected | |||
471 | /// to be enabled. | |||
472 | void sendReceive6Test(data::ConstElementPtr dhcp_queue_control, bool exp_queue_enabled) { | |||
473 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
474 | ||||
475 | // Testing socket operation in a portable way is tricky | |||
476 | // without interface detection implemented | |||
477 | // let's assume that every supported OS have lo interface | |||
478 | IOAddress lo_addr("::1"); | |||
479 | int socket1 = 0, socket2 = 0; | |||
480 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547 ); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_483; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_483 ; } } else gtest_label_testnothrow_483 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 483, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
481 | socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547 ); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_483; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_483 ; } } else gtest_label_testnothrow_483 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 483, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
482 | socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547 ); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_483; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_483 ; } } else gtest_label_testnothrow_483 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 483, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
483 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547 ); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_483; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_483 ; } } else gtest_label_testnothrow_483 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 483, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10546);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
484 | ||||
485 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 485 , gtest_ar.failure_message()) = ::testing::Message(); | |||
486 | EXPECT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 486 , gtest_ar.failure_message()) = ::testing::Message(); | |||
487 | ||||
488 | // Configure packet queueing as desired. | |||
489 | bool queue_enabled = false; | |||
490 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET6, dhcp_queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(10, dhcp_queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_490; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_490 ; } } else gtest_label_testnothrow_490 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 490, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, dhcp_queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
491 | ||||
492 | // Verify that we have a queue only if we expected one. | |||
493 | ASSERT_EQ(exp_queue_enabled, queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("exp_queue_enabled" , "queue_enabled", exp_queue_enabled, queue_enabled))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 493, gtest_ar.failure_message ()) = ::testing::Message(); | |||
494 | ||||
495 | // Thread should only start when there is a packet queue. | |||
496 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET6))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(10); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_496 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_496 ; } } else gtest_label_testnothrow_496 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 496, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
497 | ASSERT_TRUE(queue_enabled == ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(queue_enabled == ifacemgr ->isDHCPReceiverRunning())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 497, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled == ifacemgr->isDHCPReceiverRunning()", "false" , "true") .c_str()) = ::testing::Message(); | |||
498 | ||||
499 | // If the thread is already running, trying to start it again should fail. | |||
500 | if (queue_enabled) { | |||
501 | ASSERT_THROW(ifacemgr->startDHCPReceiver(AF_INET6), InvalidOperation)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver (10); } else static_assert(true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< InvalidOperation>:: type>::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->startDHCPReceiver(10)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_501; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(10)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_501; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(10)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_501; } } else gtest_label_testthrow_501 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 501, gtest_msg.value .c_str()) = ::testing::Message(); | |||
502 | // Should still have one running. | |||
503 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 503 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
504 | } | |||
505 | ||||
506 | // Let's build our DHCPv6 packet. | |||
507 | // prepare dummy payload | |||
508 | uint8_t data[128]; | |||
509 | for (uint8_t i = 0; i < 128; i++) { | |||
510 | data[i] = i; | |||
511 | } | |||
512 | ||||
513 | Pkt6Ptr sendPkt = Pkt6Ptr(new Pkt6(data, 128)); | |||
514 | sendPkt->repack(); | |||
515 | sendPkt->setRemotePort(10547); | |||
516 | sendPkt->setRemoteAddr(IOAddress("::1")); | |||
517 | sendPkt->setIndex(LOOPBACK_INDEX); | |||
518 | sendPkt->setIface(LOOPBACK_NAME); | |||
519 | ||||
520 | // Send the packet. | |||
521 | EXPECT_EQ(true, ifacemgr->send(sendPkt))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("true", "ifacemgr->send(sendPkt)" , true, ifacemgr->send(sendPkt)))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 521, gtest_ar.failure_message()) = ::testing::Message(); | |||
522 | ||||
523 | // Now, let's try and receive it. | |||
524 | Pkt6Ptr rcvPkt; | |||
525 | rcvPkt = ifacemgr->receive6(10); | |||
526 | ASSERT_TRUE(rcvPkt)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(rcvPkt)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 526, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "rcvPkt", "false" , "true") .c_str()) = ::testing::Message(); // received our own packet | |||
527 | ||||
528 | // let's check that we received what was sent | |||
529 | ASSERT_EQ(sendPkt->data_.size(), rcvPkt->data_.size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->data_.size()" , "rcvPkt->data_.size()", sendPkt->data_.size(), rcvPkt ->data_.size()))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 529, gtest_ar.failure_message()) = ::testing::Message(); | |||
530 | EXPECT_EQ(0, memcmp(&sendPkt->data_[0], &rcvPkt->data_[0],switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "memcmp(&sendPkt->data_[0], &rcvPkt->data_[0], rcvPkt->data_.size())" , 0, memcmp(&sendPkt->data_[0], &rcvPkt->data_[ 0], rcvPkt->data_.size())))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 531, gtest_ar.failure_message()) = ::testing::Message() | |||
531 | rcvPkt->data_.size()))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "memcmp(&sendPkt->data_[0], &rcvPkt->data_[0], rcvPkt->data_.size())" , 0, memcmp(&sendPkt->data_[0], &rcvPkt->data_[ 0], rcvPkt->data_.size())))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 531, gtest_ar.failure_message()) = ::testing::Message(); | |||
532 | ||||
533 | EXPECT_EQ(sendPkt->getRemoteAddr(), rcvPkt->getRemoteAddr())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getRemoteAddr()" , "rcvPkt->getRemoteAddr()", sendPkt->getRemoteAddr(), rcvPkt ->getRemoteAddr()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 533, gtest_ar.failure_message()) = ::testing::Message(); | |||
534 | ||||
535 | // since we opened 2 sockets on the same interface and none of them is multicast, | |||
536 | // none is preferred over the other for sending data, so we really should not | |||
537 | // assume the one or the other will always be chosen for sending data. Therefore | |||
538 | // we should accept both values as source ports. | |||
539 | EXPECT_TRUE((rcvPkt->getRemotePort() == 10546) || (rcvPkt->getRemotePort() == 10547))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult((rcvPkt->getRemotePort () == 10546) || (rcvPkt->getRemotePort() == 10547))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 539, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "(rcvPkt->getRemotePort() == 10546) || (rcvPkt->getRemotePort() == 10547)" , "false", "true") .c_str()) = ::testing::Message(); | |||
540 | ||||
541 | // Stop the thread. This should be no harm/no foul if we're not | |||
542 | // queueuing. Either way, we should not have a thread afterwards. | |||
543 | ASSERT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_543 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_543 ; } } else gtest_label_testnothrow_543 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 543, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
544 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 544 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
545 | } | |||
546 | ||||
547 | /// @brief Tests the ability to send and receive DHCPv4 packets | |||
548 | /// | |||
549 | /// This test calls @r IfaceMgr::configureDHCPPacketQueue, passing in the | |||
550 | /// given queue configuration. It then calls IfaceMgr::startDHCPReceiver | |||
551 | /// and verifies whether or not the receive thread has been started as | |||
552 | /// expected. Next it creates a DISCOVER packet and sends it over | |||
553 | /// the loop back interface. It invokes IfaceMgr::receive4 to receive the | |||
554 | /// packet sent, and compares to the packets for equality. | |||
555 | /// | |||
556 | /// @param dhcp_queue_control dhcp-queue-control contents to use for the test | |||
557 | /// @param exp_queue_enabled flag that indicates if packet queuing is expected | |||
558 | /// to be enabled. | |||
559 | void sendReceive4Test(data::ConstElementPtr dhcp_queue_control, bool exp_queue_enabled) { | |||
560 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
561 | ||||
562 | // Testing socket operation in a portable way is tricky | |||
563 | // without interface detection implemented. | |||
564 | // Let's assume that every supported OS has lo interface | |||
565 | IOAddress lo_addr("127.0.0.1"); | |||
566 | int socket1 = 0; | |||
567 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_570; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_570 ; } } else gtest_label_testnothrow_570 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 570, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
568 | socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr,switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_570; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_570 ; } } else gtest_label_testnothrow_570 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 570, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
569 | DHCP4_SERVER_PORT + 10000);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_570; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_570 ; } } else gtest_label_testnothrow_570 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 570, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
570 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_570; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_570 ; } } else gtest_label_testnothrow_570 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 570, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
571 | ||||
572 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 572 , gtest_ar.failure_message()) = ::testing::Message(); | |||
573 | ||||
574 | // Configure packet queueing as desired. | |||
575 | bool queue_enabled = false; | |||
576 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, dhcp_queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(2, dhcp_queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_576; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_576 ; } } else gtest_label_testnothrow_576 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 576, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, dhcp_queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
577 | ||||
578 | // Verify that we have a queue only if we expected one. | |||
579 | ASSERT_EQ(exp_queue_enabled, queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("exp_queue_enabled" , "queue_enabled", exp_queue_enabled, queue_enabled))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 579, gtest_ar.failure_message ()) = ::testing::Message(); | |||
580 | ||||
581 | // Thread should only start when there is a packet queue. | |||
582 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(2); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_582 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_582 ; } } else gtest_label_testnothrow_582 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 582, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
583 | ASSERT_TRUE(queue_enabled == ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(queue_enabled == ifacemgr ->isDHCPReceiverRunning())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 583, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled == ifacemgr->isDHCPReceiverRunning()", "false" , "true") .c_str()) = ::testing::Message(); | |||
584 | ||||
585 | // If the thread is already running, trying to start it again should fail. | |||
586 | if (queue_enabled) { | |||
587 | ASSERT_THROW(ifacemgr->startDHCPReceiver(AF_INET), InvalidOperation)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver (2); } else static_assert(true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< InvalidOperation>:: type>::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->startDHCPReceiver(2)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_587; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(2)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_587; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(2)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_587; } } else gtest_label_testthrow_587 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 587, gtest_msg.value .c_str()) = ::testing::Message(); | |||
588 | // Should still have one running. | |||
589 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 589 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
590 | } | |||
591 | ||||
592 | // Let's construct the packet to send. | |||
593 | boost::shared_ptr<Pkt4> sendPkt(new Pkt4(DHCPDISCOVER, 1234) ); | |||
594 | sendPkt->setLocalAddr(IOAddress("127.0.0.1")); | |||
595 | sendPkt->setLocalPort(DHCP4_SERVER_PORT + 10000 + 1); | |||
596 | sendPkt->setRemotePort(DHCP4_SERVER_PORT + 10000); | |||
597 | sendPkt->setRemoteAddr(IOAddress("127.0.0.1")); | |||
598 | sendPkt->setIndex(LOOPBACK_INDEX); | |||
599 | sendPkt->setIface(string(LOOPBACK_NAME)); | |||
600 | sendPkt->setHops(6); | |||
601 | sendPkt->setSecs(42); | |||
602 | sendPkt->setCiaddr(IOAddress("192.0.2.1")); | |||
603 | sendPkt->setSiaddr(IOAddress("192.0.2.2")); | |||
604 | sendPkt->setYiaddr(IOAddress("192.0.2.3")); | |||
605 | sendPkt->setGiaddr(IOAddress("192.0.2.4")); | |||
606 | ||||
607 | // Unpack() now checks if mandatory DHCP_MESSAGE_TYPE is present. | |||
608 | // Workarounds (creating DHCP Message Type Option by hand) are no longer | |||
609 | // needed as setDhcpType() is called in constructor. | |||
610 | ||||
611 | uint8_t sname[] = "That's just a string that will act as SNAME"; | |||
612 | sendPkt->setSname(sname, strlen((const char*)sname)); | |||
613 | uint8_t file[] = "/another/string/that/acts/as/a/file_name.txt"; | |||
614 | sendPkt->setFile(file, strlen((const char*)file)); | |||
615 | ||||
616 | ASSERT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { sendPkt->pack();; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_618 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_618 ; } } else gtest_label_testnothrow_618 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 618, ("Expected: " "sendPkt->pack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
617 | sendPkt->pack();switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { sendPkt->pack();; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_618 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_618 ; } } else gtest_label_testnothrow_618 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 618, ("Expected: " "sendPkt->pack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
618 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { sendPkt->pack();; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_618 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_618 ; } } else gtest_label_testnothrow_618 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 618, ("Expected: " "sendPkt->pack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
619 | ||||
620 | // OK, Send the PACKET! | |||
621 | bool result = false; | |||
622 | EXPECT_NO_THROW(result = ifacemgr->send(sendPkt))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { result = ifacemgr->send(sendPkt); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_622 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_622 ; } } else gtest_label_testnothrow_622 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 622, ("Expected: " "result = ifacemgr->send(sendPkt)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
623 | EXPECT_TRUE(result)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(result)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 623, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "result", "false", "true") .c_str()) = ::testing ::Message(); | |||
624 | ||||
625 | // Now let's try and receive it. | |||
626 | boost::shared_ptr<Pkt4> rcvPkt; | |||
627 | ASSERT_NO_THROW(rcvPkt = ifacemgr->receive4(10))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { rcvPkt = ifacemgr->receive4(10); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_627 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_627 ; } } else gtest_label_testnothrow_627 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 627, ("Expected: " "rcvPkt = ifacemgr->receive4(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
628 | ASSERT_TRUE(rcvPkt)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(rcvPkt)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 628, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "rcvPkt", "false" , "true") .c_str()) = ::testing::Message(); // received our own packet | |||
629 | ASSERT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { rcvPkt->unpack();; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_631 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_631 ; } } else gtest_label_testnothrow_631 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 631, ("Expected: " "rcvPkt->unpack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
630 | rcvPkt->unpack();switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { rcvPkt->unpack();; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_631 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_631 ; } } else gtest_label_testnothrow_631 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 631, ("Expected: " "rcvPkt->unpack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
631 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { rcvPkt->unpack();; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_631 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_631 ; } } else gtest_label_testnothrow_631 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 631, ("Expected: " "rcvPkt->unpack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
632 | ||||
633 | // let's check that we received what was sent | |||
634 | EXPECT_EQ(sendPkt->len(), rcvPkt->len())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->len()" , "rcvPkt->len()", sendPkt->len(), rcvPkt->len()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 634, gtest_ar.failure_message ()) = ::testing::Message(); | |||
635 | EXPECT_EQ("127.0.0.1", rcvPkt->getRemoteAddr().toText())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"127.0.0.1\"" , "rcvPkt->getRemoteAddr().toText()", "127.0.0.1", rcvPkt-> getRemoteAddr().toText()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 635, gtest_ar.failure_message()) = ::testing::Message(); | |||
636 | EXPECT_EQ(sendPkt->getRemotePort(), rcvPkt->getLocalPort())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getRemotePort()" , "rcvPkt->getLocalPort()", sendPkt->getRemotePort(), rcvPkt ->getLocalPort()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 636, gtest_ar.failure_message()) = ::testing::Message(); | |||
637 | EXPECT_EQ(sendPkt->getHops(), rcvPkt->getHops())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getHops()" , "rcvPkt->getHops()", sendPkt->getHops(), rcvPkt->getHops ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 637, gtest_ar.failure_message ()) = ::testing::Message(); | |||
638 | EXPECT_EQ(sendPkt->getOp(), rcvPkt->getOp())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getOp()" , "rcvPkt->getOp()", sendPkt->getOp(), rcvPkt->getOp ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 638, gtest_ar.failure_message ()) = ::testing::Message(); | |||
639 | EXPECT_EQ(sendPkt->getSecs(), rcvPkt->getSecs())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getSecs()" , "rcvPkt->getSecs()", sendPkt->getSecs(), rcvPkt->getSecs ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 639, gtest_ar.failure_message ()) = ::testing::Message(); | |||
640 | EXPECT_EQ(sendPkt->getFlags(), rcvPkt->getFlags())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getFlags()" , "rcvPkt->getFlags()", sendPkt->getFlags(), rcvPkt-> getFlags()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 640 , gtest_ar.failure_message()) = ::testing::Message(); | |||
641 | EXPECT_EQ(sendPkt->getCiaddr(), rcvPkt->getCiaddr())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getCiaddr()" , "rcvPkt->getCiaddr()", sendPkt->getCiaddr(), rcvPkt-> getCiaddr()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 641 , gtest_ar.failure_message()) = ::testing::Message(); | |||
642 | EXPECT_EQ(sendPkt->getSiaddr(), rcvPkt->getSiaddr())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getSiaddr()" , "rcvPkt->getSiaddr()", sendPkt->getSiaddr(), rcvPkt-> getSiaddr()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 642 , gtest_ar.failure_message()) = ::testing::Message(); | |||
643 | EXPECT_EQ(sendPkt->getYiaddr(), rcvPkt->getYiaddr())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getYiaddr()" , "rcvPkt->getYiaddr()", sendPkt->getYiaddr(), rcvPkt-> getYiaddr()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 643 , gtest_ar.failure_message()) = ::testing::Message(); | |||
644 | EXPECT_EQ(sendPkt->getGiaddr(), rcvPkt->getGiaddr())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getGiaddr()" , "rcvPkt->getGiaddr()", sendPkt->getGiaddr(), rcvPkt-> getGiaddr()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 644 , gtest_ar.failure_message()) = ::testing::Message(); | |||
645 | EXPECT_EQ(sendPkt->getTransid(), rcvPkt->getTransid())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getTransid()" , "rcvPkt->getTransid()", sendPkt->getTransid(), rcvPkt ->getTransid()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 645, gtest_ar.failure_message()) = ::testing::Message(); | |||
646 | EXPECT_TRUE(sendPkt->getSname() == rcvPkt->getSname())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(sendPkt->getSname( ) == rcvPkt->getSname())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 646, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "sendPkt->getSname() == rcvPkt->getSname()", "false", "true") .c_str()) = ::testing::Message(); | |||
647 | EXPECT_TRUE(sendPkt->getFile() == rcvPkt->getFile())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(sendPkt->getFile() == rcvPkt->getFile())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 647, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "sendPkt->getFile() == rcvPkt->getFile()", "false", "true" ) .c_str()) = ::testing::Message(); | |||
648 | EXPECT_EQ(sendPkt->getHtype(), rcvPkt->getHtype())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getHtype()" , "rcvPkt->getHtype()", sendPkt->getHtype(), rcvPkt-> getHtype()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 648 , gtest_ar.failure_message()) = ::testing::Message(); | |||
649 | EXPECT_EQ(sendPkt->getHlen(), rcvPkt->getHlen())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sendPkt->getHlen()" , "rcvPkt->getHlen()", sendPkt->getHlen(), rcvPkt->getHlen ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 649, gtest_ar.failure_message ()) = ::testing::Message(); | |||
650 | ||||
651 | // since we opened 2 sockets on the same interface and none of them is multicast, | |||
652 | // none is preferred over the other for sending data, so we really should not | |||
653 | // assume the one or the other will always be chosen for sending data. We should | |||
654 | // skip checking source port of sent address. | |||
655 | ||||
656 | // Close the socket. Further we will test if errors are reported | |||
657 | // properly on attempt to use closed socket. | |||
658 | close(socket1); | |||
659 | ||||
660 | // @todo Closing the socket does NOT cause a read error out of the | |||
661 | // receiveDHCP<X>Packets() select. Apparently this is because the | |||
662 | // thread is already inside the select when the socket is closed, | |||
663 | // and (at least under Centos 7.5), this does not interrupt the | |||
664 | // select. For now, we'll only test this for direct receive. | |||
665 | if (!queue_enabled) { | |||
666 | EXPECT_THROW(ifacemgr->receive4(10), SocketReadError)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->receive4(10 ); } else static_assert(true, ""); } catch (SocketReadError const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketReadError>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->receive4(10)" " throws an exception of type " "SocketReadError" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_666; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(10)" " throws an exception of type " "SocketReadError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_666; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(10)" " throws an exception of type " "SocketReadError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_666; } } else gtest_label_testthrow_666 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 666, gtest_msg.value .c_str()) = ::testing::Message(); | |||
667 | } | |||
668 | ||||
669 | // Verify write fails. | |||
670 | EXPECT_THROW(ifacemgr->send(sendPkt), SocketWriteError)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->send(sendPkt ); } else static_assert(true, ""); } catch (SocketWriteError const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketWriteError>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->send(sendPkt)" " throws an exception of type " "SocketWriteError" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_670; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->send(sendPkt)" " throws an exception of type " "SocketWriteError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_670; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->send(sendPkt)" " throws an exception of type " "SocketWriteError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_670; } } else gtest_label_testthrow_670 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 670, gtest_msg.value .c_str()) = ::testing::Message(); | |||
671 | ||||
672 | // Stop the thread. This should be no harm/no foul if we're not | |||
673 | // queueuing. Either way, we should not have a thread afterwards. | |||
674 | ASSERT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_674 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_674 ; } } else gtest_label_testnothrow_674 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 674, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
675 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 675 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
676 | } | |||
677 | ||||
678 | /// @brief Verifies that IfaceMgr DHCPv4 receive calls detect and | |||
679 | /// purge external sockets that have gone bad without affecting | |||
680 | /// affecting normal operations. It can be run with or without | |||
681 | /// packet queuing. | |||
682 | /// | |||
683 | /// @param use_queue determines if packet queuing is used or not. | |||
684 | void purgeExternalSockets4Test(bool use_queue = false) { | |||
685 | bool callback_ok = false; | |||
686 | bool callback2_ok = false; | |||
687 | ||||
688 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
689 | ||||
690 | if (use_queue) { | |||
691 | bool queue_enabled = false; | |||
692 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500); | |||
693 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, config))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(2, config ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_693; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_693 ; } } else gtest_label_testnothrow_693 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 693, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
694 | ASSERT_TRUE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(queue_enabled)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 694, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "false", "true") .c_str()) = ::testing::Message(); | |||
695 | ||||
696 | // Thread should only start when there is a packet queue. | |||
697 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(2); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_697 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_697 ; } } else gtest_label_testnothrow_697 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 697, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
698 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 698 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
699 | } | |||
700 | ||||
701 | // Create first pipe and register it as extra socket | |||
702 | int pipefd[2]; | |||
703 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 703, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
704 | ASSERT_FALSE(ifacemgr->isExternalSocket(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocket (pipefd[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 704, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
705 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0],switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_708; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_708; } } else gtest_label_testnothrow_708 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 708, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
706 | [&callback_ok, &pipefd](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_708; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_708; } } else gtest_label_testnothrow_708 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 708, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
707 | callback_ok = (pipefd[0] == fd);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_708; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_708; } } else gtest_label_testnothrow_708 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 708, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
708 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_708; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_708; } } else gtest_label_testnothrow_708 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 708, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
709 | ASSERT_TRUE(ifacemgr->isExternalSocket(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isExternalSocket (pipefd[0]))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 709, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
710 | ||||
711 | // Let's create a second pipe and register it as well | |||
712 | int secondpipe[2]; | |||
713 | EXPECT_TRUE(pipe(secondpipe) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(secondpipe) == 0 )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 713, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(secondpipe) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
714 | ASSERT_FALSE(ifacemgr->isExternalSocket(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocket (secondpipe[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 714, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
715 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0],switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_718; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_718 ; } } else gtest_label_testnothrow_718 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 718, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
716 | [&callback2_ok, &secondpipe](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_718; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_718 ; } } else gtest_label_testnothrow_718 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 718, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
717 | callback2_ok = (secondpipe[0] == fd);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_718; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_718 ; } } else gtest_label_testnothrow_718 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 718, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
718 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_718; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_718 ; } } else gtest_label_testnothrow_718 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 718, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
719 | ASSERT_TRUE(ifacemgr->isExternalSocket(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isExternalSocket (secondpipe[0]))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 719, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
720 | ||||
721 | // Verify a call with no data and normal external sockets works ok. | |||
722 | Pkt4Ptr pkt4; | |||
723 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(RECEIVE_WAIT_MS(10)))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(0,(10*1000)); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_723; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_723; } } else gtest_label_testnothrow_723 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 723, ("Expected: " "pkt4 = ifacemgr->receive4(0,(10*1000))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
724 | ||||
725 | // No callback invocations and no DHCPv4 pkt. | |||
726 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 726, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
727 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 727, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
728 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 728, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
729 | ||||
730 | // Now close the first pipe. This should make it's external socket invalid. | |||
731 | close(pipefd[1]); | |||
732 | close(pipefd[0]); | |||
733 | ||||
734 | // We call receive4() which should detect and remove the invalid socket. | |||
735 | try { | |||
736 | pkt4 = ifacemgr->receive4(RECEIVE_WAIT_MS(10)0,(10*1000)); | |||
737 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 737, "Failed") = :: testing::Message() << "receive4 should have failed"; | |||
738 | } catch (const SocketReadError& ex) { | |||
739 | EXPECT_EQ(std::string("SELECT interrupted by one invalid sockets,"switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(\"SELECT interrupted by one invalid sockets,\" \" purged 1 socket descriptors\")" , "std::string(ex.what())", std::string("SELECT interrupted by one invalid sockets," " purged 1 socket descriptors"), std::string(ex.what())))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 741, gtest_ar.failure_message ()) = ::testing::Message() | |||
740 | " purged 1 socket descriptors"),switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(\"SELECT interrupted by one invalid sockets,\" \" purged 1 socket descriptors\")" , "std::string(ex.what())", std::string("SELECT interrupted by one invalid sockets," " purged 1 socket descriptors"), std::string(ex.what())))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 741, gtest_ar.failure_message ()) = ::testing::Message() | |||
741 | std::string(ex.what()))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(\"SELECT interrupted by one invalid sockets,\" \" purged 1 socket descriptors\")" , "std::string(ex.what())", std::string("SELECT interrupted by one invalid sockets," " purged 1 socket descriptors"), std::string(ex.what())))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 741, gtest_ar.failure_message ()) = ::testing::Message(); | |||
742 | } catch (const std::exception& ex) { | |||
743 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 743, "Failed") = :: testing::Message() << "wrong exception thrown: " << ex.what(); | |||
744 | } | |||
745 | ||||
746 | // No callback invocations and no DHCPv4 pkt. | |||
747 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 747, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
748 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 748, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
749 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 749, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
750 | ||||
751 | // Now check whether the second callback is still functional | |||
752 | EXPECT_EQ(38, write(secondpipe[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(secondpipe[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(secondpipe[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 752 , gtest_ar.failure_message()) = ::testing::Message(); | |||
753 | ||||
754 | // Call receive4 again, this should work. | |||
755 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(RECEIVE_WAIT_MS(10)))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(0,(10*1000)); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_755; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_755; } } else gtest_label_testnothrow_755 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 755, ("Expected: " "pkt4 = ifacemgr->receive4(0,(10*1000))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
756 | ||||
757 | // Should have callback2 data only. | |||
758 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 758, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
759 | EXPECT_TRUE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback2_ok)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 759, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "false" , "true") .c_str()) = ::testing::Message(); | |||
760 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 760, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
761 | ||||
762 | // Stop the thread. This should be no harm/no foul if we're not | |||
763 | // queueuing. Either way, we should not have a thread afterwards. | |||
764 | ASSERT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_764 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_764 ; } } else gtest_label_testnothrow_764 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 764, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
765 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 765 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
766 | } | |||
767 | ||||
768 | /// @brief Verifies that IfaceMgr DHCPv6 receive calls detect and | |||
769 | /// purge external sockets that have gone bad without affecting | |||
770 | /// affecting normal operations. It can be run with or without | |||
771 | /// packet queuing. | |||
772 | /// | |||
773 | /// @param use_queue determines if packet queuing is used or not. | |||
774 | void purgeExternalSockets6Test(bool use_queue = false) { | |||
775 | bool callback_ok = false; | |||
776 | bool callback2_ok = false; | |||
777 | ||||
778 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
779 | ||||
780 | if (use_queue) { | |||
781 | bool queue_enabled = false; | |||
782 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500); | |||
783 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET6, config))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(10, config ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_783; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_783 ; } } else gtest_label_testnothrow_783 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 783, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
784 | ASSERT_TRUE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(queue_enabled)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 784, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "false", "true") .c_str()) = ::testing::Message(); | |||
785 | ||||
786 | // Thread should only start when there is a packet queue. | |||
787 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET6))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(10); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_787 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_787 ; } } else gtest_label_testnothrow_787 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 787, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
788 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 788 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
789 | } | |||
790 | ||||
791 | // Create first pipe and register it as extra socket | |||
792 | int pipefd[2]; | |||
793 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 793, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
794 | ASSERT_FALSE(ifacemgr->isExternalSocket(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocket (pipefd[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 794, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
795 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0],switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_798; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_798; } } else gtest_label_testnothrow_798 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 798, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
796 | [&callback_ok, &pipefd](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_798; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_798; } } else gtest_label_testnothrow_798 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 798, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
797 | callback_ok = (pipefd[0] == fd);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_798; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_798; } } else gtest_label_testnothrow_798 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 798, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
798 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_798; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_798; } } else gtest_label_testnothrow_798 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 798, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&callback_ok, &pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
799 | ASSERT_TRUE(ifacemgr->isExternalSocket(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isExternalSocket (pipefd[0]))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 799, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
800 | ||||
801 | // Let's create a second pipe and register it as well | |||
802 | int secondpipe[2]; | |||
803 | EXPECT_TRUE(pipe(secondpipe) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(secondpipe) == 0 )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 803, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(secondpipe) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
804 | ASSERT_FALSE(ifacemgr->isExternalSocket(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocket (secondpipe[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 804, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
805 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0],switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_808; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_808 ; } } else gtest_label_testnothrow_808 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 808, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
806 | [&callback2_ok, &secondpipe](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_808; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_808 ; } } else gtest_label_testnothrow_808 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 808, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
807 | callback2_ok = (secondpipe[0] == fd);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_808; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_808 ; } } else gtest_label_testnothrow_808 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 808, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
808 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok , &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); }); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_808; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_808 ; } } else gtest_label_testnothrow_808 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 808, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&callback2_ok, &secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
809 | ASSERT_TRUE(ifacemgr->isExternalSocket(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isExternalSocket (secondpipe[0]))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 809, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
810 | ||||
811 | // Verify a call with no data and normal external sockets works ok. | |||
812 | Pkt6Ptr pkt6; | |||
813 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(RECEIVE_WAIT_MS(10)))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(0,(10*1000)); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_813; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_813; } } else gtest_label_testnothrow_813 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 813, ("Expected: " "pkt6 = ifacemgr->receive6(0,(10*1000))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
814 | ||||
815 | // No callback invocations and no DHCPv6 pkt. | |||
816 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 816, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
817 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 817, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
818 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 818, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
819 | ||||
820 | // Now close the first pipe. This should make it's external socket invalid. | |||
821 | close(pipefd[1]); | |||
822 | close(pipefd[0]); | |||
823 | ||||
824 | // We call receive6() which should detect and remove the invalid socket. | |||
825 | try { | |||
826 | pkt6 = ifacemgr->receive6(RECEIVE_WAIT_MS(10)0,(10*1000)); | |||
827 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 827, "Failed") = :: testing::Message() << "receive6 should have failed"; | |||
828 | } catch (const SocketReadError& ex) { | |||
829 | EXPECT_EQ(std::string("SELECT interrupted by one invalid sockets,"switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(\"SELECT interrupted by one invalid sockets,\" \" purged 1 socket descriptors\")" , "std::string(ex.what())", std::string("SELECT interrupted by one invalid sockets," " purged 1 socket descriptors"), std::string(ex.what())))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 831, gtest_ar.failure_message ()) = ::testing::Message() | |||
830 | " purged 1 socket descriptors"),switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(\"SELECT interrupted by one invalid sockets,\" \" purged 1 socket descriptors\")" , "std::string(ex.what())", std::string("SELECT interrupted by one invalid sockets," " purged 1 socket descriptors"), std::string(ex.what())))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 831, gtest_ar.failure_message ()) = ::testing::Message() | |||
831 | std::string(ex.what()))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(\"SELECT interrupted by one invalid sockets,\" \" purged 1 socket descriptors\")" , "std::string(ex.what())", std::string("SELECT interrupted by one invalid sockets," " purged 1 socket descriptors"), std::string(ex.what())))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 831, gtest_ar.failure_message ()) = ::testing::Message(); | |||
832 | } catch (const std::exception& ex) { | |||
833 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 833, "Failed") = :: testing::Message() << "wrong exception thrown: " << ex.what(); | |||
834 | } | |||
835 | ||||
836 | // No callback invocations and no DHCPv6 pkt. | |||
837 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 837, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
838 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 838, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
839 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 839, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
840 | ||||
841 | // Now check whether the second callback is still functional | |||
842 | EXPECT_EQ(38, write(secondpipe[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(secondpipe[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(secondpipe[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 842 , gtest_ar.failure_message()) = ::testing::Message(); | |||
843 | ||||
844 | // Call receive6 again, this should work. | |||
845 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(RECEIVE_WAIT_MS(10)))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(0,(10*1000)); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_845; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_845; } } else gtest_label_testnothrow_845 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 845, ("Expected: " "pkt6 = ifacemgr->receive6(0,(10*1000))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
846 | ||||
847 | // Should have callback2 data only. | |||
848 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 848, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
849 | EXPECT_TRUE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback2_ok)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 849, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "false" , "true") .c_str()) = ::testing::Message(); | |||
850 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 850, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
851 | ||||
852 | // Stop the thread. This should be no harm/no foul if we're not | |||
853 | // queueuing. Either way, we should not have a thread afterwards. | |||
854 | ASSERT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_854 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_854 ; } } else gtest_label_testnothrow_854 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 854, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
855 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 855 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
856 | } | |||
857 | ||||
858 | /// Holds the invocation counter for ifaceMgrErrorHandler. | |||
859 | int errors_count_; | |||
860 | }; | |||
861 | ||||
862 | // We need some known interface to work reliably. Loopback interface is named | |||
863 | // lo on Linux and lo0 on BSD boxes. We need to find out which is available. | |||
864 | // This is not a real test, but rather a workaround that will go away when | |||
865 | // interface detection is implemented on all OSes. | |||
866 | TEST_F(IfaceMgrTest, loDetect)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("loDetect") > 1, "test_name must not be empty" ); class IfaceMgrTest_loDetect_Test : public IfaceMgrTest { public : IfaceMgrTest_loDetect_Test() = default; ~IfaceMgrTest_loDetect_Test () override = default; IfaceMgrTest_loDetect_Test (const IfaceMgrTest_loDetect_Test &) = delete; IfaceMgrTest_loDetect_Test & operator=( const IfaceMgrTest_loDetect_Test &) = delete; IfaceMgrTest_loDetect_Test (IfaceMgrTest_loDetect_Test &&) noexcept = delete; IfaceMgrTest_loDetect_Test & operator=( IfaceMgrTest_loDetect_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceMgrTest_loDetect_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "loDetect" , nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 866), (::testing::internal::GetTypeId<IfaceMgrTest>() ), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("iface_mgr_unittest.cc", 866), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("iface_mgr_unittest.cc", 866), new ::testing::internal::TestFactoryImpl <IfaceMgrTest_loDetect_Test>); void IfaceMgrTest_loDetect_Test ::TestBody() { | |||
867 | NakedIfaceMgr::loDetect(); | |||
868 | } | |||
869 | ||||
870 | // Uncomment this test to create packet writer. It will | |||
871 | // write incoming DHCPv6 packets as C arrays. That is useful | |||
872 | // for generating test sequences based on actual traffic | |||
873 | // | |||
874 | // TODO: this potentially should be moved to a separate tool | |||
875 | // | |||
876 | ||||
877 | #if 0 | |||
878 | TEST_F(IfaceMgrTest, dhcp6Sniffer)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("dhcp6Sniffer") > 1, "test_name must not be empty" ); class IfaceMgrTest_dhcp6Sniffer_Test : public IfaceMgrTest { public: IfaceMgrTest_dhcp6Sniffer_Test() = default; ~IfaceMgrTest_dhcp6Sniffer_Test () override = default; IfaceMgrTest_dhcp6Sniffer_Test (const IfaceMgrTest_dhcp6Sniffer_Test &) = delete; IfaceMgrTest_dhcp6Sniffer_Test & operator =( const IfaceMgrTest_dhcp6Sniffer_Test &) = delete; IfaceMgrTest_dhcp6Sniffer_Test (IfaceMgrTest_dhcp6Sniffer_Test &&) noexcept = delete ; IfaceMgrTest_dhcp6Sniffer_Test & operator=( IfaceMgrTest_dhcp6Sniffer_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_dhcp6Sniffer_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "dhcp6Sniffer", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 878), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 878), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 878), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_dhcp6Sniffer_Test >); void IfaceMgrTest_dhcp6Sniffer_Test::TestBody() { | |||
879 | // Testing socket operation in a portable way is tricky | |||
880 | // without interface detection implemented | |||
881 | ||||
882 | static_cast<void>(remove("interfaces.txt")); | |||
883 | ||||
884 | ofstream interfaces("interfaces.txt", ios::ate); | |||
885 | interfaces << "eth0 fe80::21e:8cff:fe9b:7349"; | |||
886 | interfaces.close(); | |||
887 | ||||
888 | boost::scoped_ptr<NakedIfaceMgr> ifacemgr = new NakedIfaceMgr(); | |||
889 | ||||
890 | Pkt6Ptr pkt; | |||
891 | int cnt = 0; | |||
892 | cout << "---8X-----------------------------------------" << endl; | |||
893 | while (true) { | |||
894 | pkt.reset(ifacemgr->receive()); | |||
895 | ||||
896 | cout << "// this code is autogenerated. Do NOT edit." << endl; | |||
897 | cout << "// Received " << pkt->data_len_ << " bytes packet:" << endl; | |||
898 | cout << "Pkt6 *capture" << cnt++ << "() {" << endl; | |||
899 | cout << " Pkt6* pkt;" << endl; | |||
900 | cout << " pkt = new Pkt6(" << pkt->data_len_ << ");" << endl; | |||
901 | cout << " pkt->remote_port_ = " << pkt-> remote_port_ << ";" << endl; | |||
902 | cout << " pkt->remote_addr_ = IOAddress(\"" | |||
903 | << pkt->remote_addr_ << "\");" << endl; | |||
904 | cout << " pkt->local_port_ = " << pkt-> local_port_ << ";" << endl; | |||
905 | cout << " pkt->local_addr_ = IOAddress(\"" | |||
906 | << pkt->local_addr_ << "\");" << endl; | |||
907 | cout << " pkt->ifindex_ = " << pkt->ifindex_ << ";" << endl; | |||
908 | cout << " pkt->iface_ = \"" << pkt->iface_ << "\";" << endl; | |||
909 | ||||
910 | // TODO it is better to declare statically initialize the array | |||
911 | // and then memcpy it to packet. | |||
912 | for (int i=0; i< pkt->data_len_; i++) { | |||
913 | cout << " pkt->data_[" << i << "]=" | |||
914 | << (int)(unsigned char)pkt->data_[i] << "; "; | |||
915 | if (!(i%4)) | |||
916 | cout << endl; | |||
917 | } | |||
918 | cout << endl; | |||
919 | cout << " return (pkt);" << endl; | |||
920 | cout << "}" << endl << endl; | |||
921 | ||||
922 | pkt.reset(); | |||
923 | } | |||
924 | cout << "---8X-----------------------------------------" << endl; | |||
925 | ||||
926 | // Never happens. Infinite loop is infinite | |||
927 | } | |||
928 | #endif | |||
929 | ||||
930 | // This test verifies that creation of the IfaceMgr instance doesn't | |||
931 | // cause an exception. | |||
932 | TEST_F(IfaceMgrTest, instance)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("instance") > 1, "test_name must not be empty" ); class IfaceMgrTest_instance_Test : public IfaceMgrTest { public : IfaceMgrTest_instance_Test() = default; ~IfaceMgrTest_instance_Test () override = default; IfaceMgrTest_instance_Test (const IfaceMgrTest_instance_Test &) = delete; IfaceMgrTest_instance_Test & operator=( const IfaceMgrTest_instance_Test &) = delete; IfaceMgrTest_instance_Test (IfaceMgrTest_instance_Test &&) noexcept = delete; IfaceMgrTest_instance_Test & operator=( IfaceMgrTest_instance_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceMgrTest_instance_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "instance" , nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 932), (::testing::internal::GetTypeId<IfaceMgrTest>() ), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("iface_mgr_unittest.cc", 932), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("iface_mgr_unittest.cc", 932), new ::testing::internal::TestFactoryImpl <IfaceMgrTest_instance_Test>); void IfaceMgrTest_instance_Test ::TestBody() { | |||
933 | EXPECT_NO_THROW(IfaceMgr::instance())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { IfaceMgr::instance(); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_933 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_933 ; } } else gtest_label_testnothrow_933 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 933, ("Expected: " "IfaceMgr::instance()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
934 | } | |||
935 | ||||
936 | // Basic tests for Iface inner class. | |||
937 | TEST_F(IfaceMgrTest, ifaceClass)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("ifaceClass") > 1, "test_name must not be empty" ); class IfaceMgrTest_ifaceClass_Test : public IfaceMgrTest { public: IfaceMgrTest_ifaceClass_Test() = default; ~IfaceMgrTest_ifaceClass_Test () override = default; IfaceMgrTest_ifaceClass_Test (const IfaceMgrTest_ifaceClass_Test &) = delete; IfaceMgrTest_ifaceClass_Test & operator =( const IfaceMgrTest_ifaceClass_Test &) = delete; IfaceMgrTest_ifaceClass_Test (IfaceMgrTest_ifaceClass_Test &&) noexcept = delete; IfaceMgrTest_ifaceClass_Test & operator=( IfaceMgrTest_ifaceClass_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_ifaceClass_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "ifaceClass", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 937), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 937), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 937), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_ifaceClass_Test >); void IfaceMgrTest_ifaceClass_Test::TestBody() { | |||
938 | ||||
939 | IfacePtr iface(new Iface("eth5", 7)); | |||
940 | EXPECT_STREQ("eth5/7", iface->getFullName().c_str())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTREQ("\"eth5/7\"" , "iface->getFullName().c_str()", "eth5/7", iface->getFullName ().c_str()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 940 , gtest_ar.failure_message()) = ::testing::Message(); | |||
941 | ||||
942 | EXPECT_THROW_MSG(iface.reset(new Iface("", 10)), BadValue,{ try { iface.reset(new Iface("", 10)); ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 943, "Failed") = ::testing::Message() << "no exception, expected: " << "BadValue"; } catch (const BadValue& ex) { switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(ex.what())" , "\"Interface name must not be empty\"", std::string(ex.what ()), "Interface name must not be empty"))) ; else ::testing:: internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 943, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 943, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } } | |||
943 | "Interface name must not be empty"){ try { iface.reset(new Iface("", 10)); ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 943, "Failed") = ::testing::Message() << "no exception, expected: " << "BadValue"; } catch (const BadValue& ex) { switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(ex.what())" , "\"Interface name must not be empty\"", std::string(ex.what ()), "Interface name must not be empty"))) ; else ::testing:: internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 943, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 943, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } }; | |||
944 | ||||
945 | EXPECT_NO_THROW(iface.reset(new Iface("big-index", 66666)))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface.reset(new Iface("big-index", 66666)); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_945; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_945; } } else gtest_label_testnothrow_945 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 945, ("Expected: " "iface.reset(new Iface(\"big-index\", 66666))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
946 | EXPECT_EQ(66666, iface->getIndex())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("66666", "iface->getIndex()" , 66666, iface->getIndex()))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 946, gtest_ar.failure_message()) = ::testing::Message(); | |||
947 | } | |||
948 | ||||
949 | // This test checks the getIface by index method. | |||
950 | TEST_F(IfaceMgrTest, getIfaceByIndex)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("getIfaceByIndex") > 1, "test_name must not be empty" ); class IfaceMgrTest_getIfaceByIndex_Test : public IfaceMgrTest { public: IfaceMgrTest_getIfaceByIndex_Test() = default; ~IfaceMgrTest_getIfaceByIndex_Test () override = default; IfaceMgrTest_getIfaceByIndex_Test (const IfaceMgrTest_getIfaceByIndex_Test &) = delete; IfaceMgrTest_getIfaceByIndex_Test & operator=( const IfaceMgrTest_getIfaceByIndex_Test & ) = delete; IfaceMgrTest_getIfaceByIndex_Test (IfaceMgrTest_getIfaceByIndex_Test &&) noexcept = delete; IfaceMgrTest_getIfaceByIndex_Test & operator=( IfaceMgrTest_getIfaceByIndex_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_getIfaceByIndex_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "getIfaceByIndex", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 950), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 950), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 950), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_getIfaceByIndex_Test >); void IfaceMgrTest_getIfaceByIndex_Test::TestBody() { | |||
951 | NakedIfaceMgr ifacemgr; | |||
952 | ||||
953 | // Create a set of fake interfaces. At the same time, remove the actual | |||
954 | // interfaces that have been detected by the IfaceMgr. | |||
955 | ifacemgr.createIfaces(); | |||
956 | ||||
957 | // Getting an unset index should throw. | |||
958 | EXPECT_THROW_MSG(ifacemgr.getIface(UNSET_IFINDEX), BadValue, "interface index was not set"){ try { ifacemgr.getIface(UNSET_IFINDEX); ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 958, "Failed") = ::testing::Message() << "no exception, expected: " << "BadValue"; } catch (const BadValue& ex) { switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(ex.what())" , "\"interface index was not set\"", std::string(ex.what()), "interface index was not set" ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 958, gtest_ar.failure_message ()) = ::testing::Message(); } catch (...) { ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 958, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } }; | |||
959 | ||||
960 | // Historically -1 was used as an unset value. Let's also check that it throws in case we didn't | |||
961 | // migrate all code to UNSET_IFINDEX and in case the values diverge. | |||
962 | EXPECT_THROW_MSG(ifacemgr.getIface(-1), BadValue, "interface index was not set"){ try { ifacemgr.getIface(-1); ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 962, "Failed") = ::testing::Message() << "no exception, expected: " << "BadValue"; } catch (const BadValue& ex) { switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("std::string(ex.what())" , "\"interface index was not set\"", std::string(ex.what()), "interface index was not set" ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 962, gtest_ar.failure_message ()) = ::testing::Message(); } catch (...) { ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 962, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } }; | |||
963 | ||||
964 | // Get the first interface defined. | |||
965 | IfacePtr iface(ifacemgr.getIface(0)); | |||
966 | ASSERT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 966, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false" , "true") .c_str()) = ::testing::Message(); | |||
967 | EXPECT_EQ("lo", iface->getName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"lo\"", "iface->getName()", "lo", iface->getName()))) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 967, gtest_ar.failure_message()) = ::testing::Message(); | |||
968 | ||||
969 | // Attemt to get an undefined interface. | |||
970 | iface = ifacemgr.getIface(3); | |||
971 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 971, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing:: Message(); | |||
972 | ||||
973 | // Check that we can go past INT_MAX. | |||
974 | unsigned int int_max(numeric_limits<int>::max()); | |||
975 | iface = ifacemgr.getIface(int_max); | |||
976 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 976, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing:: Message(); | |||
977 | iface = ifacemgr.createIface("wlan0", int_max); | |||
978 | ifacemgr.addInterface(iface); | |||
979 | iface = ifacemgr.getIface(int_max); | |||
980 | EXPECT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 980, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "false", "true") .c_str()) = ::testing:: Message(); | |||
981 | iface = ifacemgr.getIface(int_max + 1); | |||
982 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 982, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing:: Message(); | |||
983 | iface = ifacemgr.createIface("wlan1", int_max + 1); | |||
984 | ifacemgr.addInterface(iface); | |||
985 | iface = ifacemgr.getIface(int_max + 1); | |||
986 | EXPECT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 986, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "false", "true") .c_str()) = ::testing:: Message(); | |||
987 | } | |||
988 | ||||
989 | // This test checks the getIface by packet method. | |||
990 | TEST_F(IfaceMgrTest, getIfaceByPkt)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("getIfaceByPkt") > 1, "test_name must not be empty" ); class IfaceMgrTest_getIfaceByPkt_Test : public IfaceMgrTest { public: IfaceMgrTest_getIfaceByPkt_Test() = default; ~IfaceMgrTest_getIfaceByPkt_Test () override = default; IfaceMgrTest_getIfaceByPkt_Test (const IfaceMgrTest_getIfaceByPkt_Test &) = delete; IfaceMgrTest_getIfaceByPkt_Test & operator=( const IfaceMgrTest_getIfaceByPkt_Test & ) = delete; IfaceMgrTest_getIfaceByPkt_Test (IfaceMgrTest_getIfaceByPkt_Test &&) noexcept = delete; IfaceMgrTest_getIfaceByPkt_Test & operator=( IfaceMgrTest_getIfaceByPkt_Test &&) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_getIfaceByPkt_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "getIfaceByPkt", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 990), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 990), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 990), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_getIfaceByPkt_Test >); void IfaceMgrTest_getIfaceByPkt_Test::TestBody() { | |||
991 | NakedIfaceMgr ifacemgr; | |||
992 | // Create a set of fake interfaces. At the same time, remove the actual | |||
993 | // interfaces that have been detected by the IfaceMgr. | |||
994 | ifacemgr.createIfaces(); | |||
995 | ||||
996 | // Try IPv4 packet by name. | |||
997 | Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 1234)); | |||
998 | IfacePtr iface = ifacemgr.getIface(pkt4); | |||
999 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 999, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing:: Message(); | |||
1000 | pkt4->setIface("eth0"); | |||
1001 | iface = ifacemgr.getIface(pkt4); | |||
1002 | EXPECT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1002, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "false", "true") .c_str()) = ::testing:: Message(); | |||
1003 | EXPECT_FALSE(pkt4->indexSet())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4->indexSet() ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1003, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4->indexSet()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1004 | ||||
1005 | // Try IPv6 packet by index. | |||
1006 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_REPLY, 123456)); | |||
1007 | iface = ifacemgr.getIface(pkt6); | |||
1008 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1008, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing:: Message(); | |||
1009 | ASSERT_TRUE(ifacemgr.getIface("eth0"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth0" ))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1009 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")", "false", "true") .c_str()) = ::testing::Message(); | |||
1010 | pkt6->setIndex(ifacemgr.getIface("eth0")->getIndex() + 1); | |||
1011 | iface = ifacemgr.getIface(pkt6); | |||
1012 | ASSERT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1012, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false" , "true") .c_str()) = ::testing::Message(); | |||
1013 | EXPECT_TRUE(pkt6->indexSet())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pkt6->indexSet())) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1013, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6->indexSet()" , "false", "true") .c_str()) = ::testing::Message(); | |||
1014 | ||||
1015 | // Index has precedence when both name and index are available. | |||
1016 | EXPECT_EQ("eth1", iface->getName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"eth1\"" , "iface->getName()", "eth1", iface->getName()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1016, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1017 | pkt6->setIface("eth0"); | |||
1018 | iface = ifacemgr.getIface(pkt6); | |||
1019 | ASSERT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1019, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false" , "true") .c_str()) = ::testing::Message(); | |||
1020 | EXPECT_EQ("eth1", iface->getName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"eth1\"" , "iface->getName()", "eth1", iface->getName()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1020, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1021 | ||||
1022 | // Not existing name fails. | |||
1023 | pkt4->setIface("eth2"); | |||
1024 | iface = ifacemgr.getIface(pkt4); | |||
1025 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1025, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing:: Message(); | |||
1026 | ||||
1027 | // Not existing index fails. | |||
1028 | pkt6->setIndex(3); | |||
1029 | iface = ifacemgr.getIface(pkt6); | |||
1030 | ASSERT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1030, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
1031 | ||||
1032 | // Test that resetting the index is verifiable. | |||
1033 | pkt4->resetIndex(); | |||
1034 | EXPECT_FALSE(pkt4->indexSet())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4->indexSet() ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1034, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4->indexSet()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1035 | pkt6->resetIndex(); | |||
1036 | EXPECT_FALSE(pkt6->indexSet())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6->indexSet() ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1036, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6->indexSet()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1037 | ||||
1038 | // Test that you can also reset the index via setIndex(). | |||
1039 | pkt4->setIndex(UNSET_IFINDEX); | |||
1040 | EXPECT_FALSE(pkt4->indexSet())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4->indexSet() ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1040, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4->indexSet()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1041 | pkt6->setIndex(UNSET_IFINDEX); | |||
1042 | EXPECT_FALSE(pkt6->indexSet())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6->indexSet() ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1042, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6->indexSet()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1043 | } | |||
1044 | ||||
1045 | // Test that the IPv4 address can be retrieved for the interface. | |||
1046 | TEST_F(IfaceMgrTest, ifaceGetAddress)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("ifaceGetAddress") > 1, "test_name must not be empty" ); class IfaceMgrTest_ifaceGetAddress_Test : public IfaceMgrTest { public: IfaceMgrTest_ifaceGetAddress_Test() = default; ~IfaceMgrTest_ifaceGetAddress_Test () override = default; IfaceMgrTest_ifaceGetAddress_Test (const IfaceMgrTest_ifaceGetAddress_Test &) = delete; IfaceMgrTest_ifaceGetAddress_Test & operator=( const IfaceMgrTest_ifaceGetAddress_Test & ) = delete; IfaceMgrTest_ifaceGetAddress_Test (IfaceMgrTest_ifaceGetAddress_Test &&) noexcept = delete; IfaceMgrTest_ifaceGetAddress_Test & operator=( IfaceMgrTest_ifaceGetAddress_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_ifaceGetAddress_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "ifaceGetAddress", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1046), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1046), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1046), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_ifaceGetAddress_Test >); void IfaceMgrTest_ifaceGetAddress_Test::TestBody() { | |||
1047 | Iface iface("eth0", 0); | |||
1048 | ||||
1049 | IOAddress addr("::1"); | |||
1050 | // Initially, the Iface has no addresses assigned. | |||
1051 | EXPECT_FALSE(iface.getAddress4(addr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface.getAddress4(addr )))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1051, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface.getAddress4(addr)" , "true", "false") .c_str()) = ::testing::Message(); | |||
1052 | // Add some addresses with IPv4 address in the middle. | |||
1053 | iface.addAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")); | |||
1054 | iface.addAddress(IOAddress("10.1.2.3")); | |||
1055 | iface.addAddress(IOAddress("2001:db8:1::2")); | |||
1056 | // The v4 address should be returned. | |||
1057 | EXPECT_TRUE(iface.getAddress4(addr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface.getAddress4(addr ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1057, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface.getAddress4(addr)" , "false", "true") .c_str()) = ::testing::Message(); | |||
1058 | EXPECT_EQ("10.1.2.3", addr.toText())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"10.1.2.3\"" , "addr.toText()", "10.1.2.3", addr.toText()))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1058, gtest_ar.failure_message()) = ::testing::Message(); | |||
1059 | // Delete the IPv4 address and leave only two IPv6 addresses. | |||
1060 | ASSERT_NO_THROW(iface.delAddress(IOAddress("10.1.2.3")))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface.delAddress(IOAddress("10.1.2.3")); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1060; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1060; } } else gtest_label_testnothrow_1060 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1060, ("Expected: " "iface.delAddress(IOAddress(\"10.1.2.3\"))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1061 | // The IPv4 address should not be returned. | |||
1062 | EXPECT_FALSE(iface.getAddress4(addr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface.getAddress4(addr )))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1062, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface.getAddress4(addr)" , "true", "false") .c_str()) = ::testing::Message(); | |||
1063 | // Add a different IPv4 address at the end of the list. | |||
1064 | iface.addAddress(IOAddress("192.0.2.3")); | |||
1065 | // This new address should now be returned. | |||
1066 | EXPECT_TRUE(iface.getAddress4(addr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface.getAddress4(addr ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1066, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface.getAddress4(addr)" , "false", "true") .c_str()) = ::testing::Message(); | |||
1067 | EXPECT_EQ("192.0.2.3", addr.toText())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"192.0.2.3\"" , "addr.toText()", "192.0.2.3", addr.toText()))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1067, gtest_ar.failure_message()) = ::testing::Message(); | |||
1068 | } | |||
1069 | ||||
1070 | // This test checks if it is possible to check that the specific address is | |||
1071 | // assigned to the interface. | |||
1072 | TEST_F(IfaceMgrTest, ifaceHasAddress)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("ifaceHasAddress") > 1, "test_name must not be empty" ); class IfaceMgrTest_ifaceHasAddress_Test : public IfaceMgrTest { public: IfaceMgrTest_ifaceHasAddress_Test() = default; ~IfaceMgrTest_ifaceHasAddress_Test () override = default; IfaceMgrTest_ifaceHasAddress_Test (const IfaceMgrTest_ifaceHasAddress_Test &) = delete; IfaceMgrTest_ifaceHasAddress_Test & operator=( const IfaceMgrTest_ifaceHasAddress_Test & ) = delete; IfaceMgrTest_ifaceHasAddress_Test (IfaceMgrTest_ifaceHasAddress_Test &&) noexcept = delete; IfaceMgrTest_ifaceHasAddress_Test & operator=( IfaceMgrTest_ifaceHasAddress_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_ifaceHasAddress_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "ifaceHasAddress", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1072), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1072), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1072), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_ifaceHasAddress_Test >); void IfaceMgrTest_ifaceHasAddress_Test::TestBody() { | |||
1073 | IfaceMgrTestConfig config(true); | |||
1074 | ||||
1075 | IfacePtr iface = IfaceMgr::instance().getIface("eth0"); | |||
1076 | ASSERT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1076, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false" , "true") .c_str()) = ::testing::Message(); | |||
1077 | EXPECT_TRUE(iface->hasAddress(IOAddress("10.0.0.1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface->hasAddress( IOAddress("10.0.0.1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1077, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"10.0.0.1\"))", "false", "true" ) .c_str()) = ::testing::Message(); | |||
1078 | EXPECT_FALSE(iface->hasAddress(IOAddress("10.0.0.2")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface->hasAddress (IOAddress("10.0.0.2"))))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1078, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"10.0.0.2\"))", "true", "false" ) .c_str()) = ::testing::Message(); | |||
1079 | EXPECT_TRUE(iface->hasAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface->hasAddress( IOAddress("fe80::3a60:77ff:fed5:cdef")))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1079, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
1080 | EXPECT_TRUE(iface->hasAddress(IOAddress("2001:db8:1::1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface->hasAddress( IOAddress("2001:db8:1::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1080, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"2001:db8:1::1\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
1081 | EXPECT_FALSE(iface->hasAddress(IOAddress("2001:db8:1::2")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface->hasAddress (IOAddress("2001:db8:1::2"))))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1081, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"2001:db8:1::2\"))", "true" , "false") .c_str()) = ::testing::Message(); | |||
1082 | } | |||
1083 | ||||
1084 | // This test checks it is not allowed to add duplicate interfaces. | |||
1085 | TEST_F(IfaceMgrTest, addInterface)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("addInterface") > 1, "test_name must not be empty" ); class IfaceMgrTest_addInterface_Test : public IfaceMgrTest { public: IfaceMgrTest_addInterface_Test() = default; ~IfaceMgrTest_addInterface_Test () override = default; IfaceMgrTest_addInterface_Test (const IfaceMgrTest_addInterface_Test &) = delete; IfaceMgrTest_addInterface_Test & operator =( const IfaceMgrTest_addInterface_Test &) = delete; IfaceMgrTest_addInterface_Test (IfaceMgrTest_addInterface_Test &&) noexcept = delete ; IfaceMgrTest_addInterface_Test & operator=( IfaceMgrTest_addInterface_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_addInterface_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "addInterface", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1085), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1085), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1085), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_addInterface_Test >); void IfaceMgrTest_addInterface_Test::TestBody() { | |||
1086 | IfaceMgrTestConfig config(true); | |||
1087 | ||||
1088 | IfacePtr dup_name(new Iface("eth1", 123)); | |||
1089 | EXPECT_THROW_MSG(IfaceMgr::instance().addInterface(dup_name), Unexpected,{ try { IfaceMgr::instance().addInterface(dup_name); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1090, "Failed") = ::testing::Message () << "no exception, expected: " << "Unexpected"; } catch (const Unexpected& ex) { switch (0) case 0: default : if (const ::testing::AssertionResult gtest_ar = (::testing:: internal::EqHelper::Compare("std::string(ex.what())", "\"Can't add eth1/123 when eth1/2 already exists.\"" , std::string(ex.what()), "Can't add eth1/123 when eth1/2 already exists." ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1090, gtest_ar.failure_message ()) = ::testing::Message(); } catch (...) { ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1090, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } } | |||
1090 | "Can't add eth1/123 when eth1/2 already exists."){ try { IfaceMgr::instance().addInterface(dup_name); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1090, "Failed") = ::testing::Message () << "no exception, expected: " << "Unexpected"; } catch (const Unexpected& ex) { switch (0) case 0: default : if (const ::testing::AssertionResult gtest_ar = (::testing:: internal::EqHelper::Compare("std::string(ex.what())", "\"Can't add eth1/123 when eth1/2 already exists.\"" , std::string(ex.what()), "Can't add eth1/123 when eth1/2 already exists." ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1090, gtest_ar.failure_message ()) = ::testing::Message(); } catch (...) { ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1090, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } }; | |||
1091 | IfacePtr dup_index(new Iface("eth2", 2)); | |||
1092 | EXPECT_THROW_MSG(IfaceMgr::instance().addInterface(dup_index), Unexpected,{ try { IfaceMgr::instance().addInterface(dup_index); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1093, "Failed") = ::testing::Message () << "no exception, expected: " << "Unexpected"; } catch (const Unexpected& ex) { switch (0) case 0: default : if (const ::testing::AssertionResult gtest_ar = (::testing:: internal::EqHelper::Compare("std::string(ex.what())", "\"Can't add eth2/2 when eth1/2 already exists.\"" , std::string(ex.what()), "Can't add eth2/2 when eth1/2 already exists." ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1093, gtest_ar.failure_message ()) = ::testing::Message(); } catch (...) { ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1093, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } } | |||
1093 | "Can't add eth2/2 when eth1/2 already exists."){ try { IfaceMgr::instance().addInterface(dup_index); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1093, "Failed") = ::testing::Message () << "no exception, expected: " << "Unexpected"; } catch (const Unexpected& ex) { switch (0) case 0: default : if (const ::testing::AssertionResult gtest_ar = (::testing:: internal::EqHelper::Compare("std::string(ex.what())", "\"Can't add eth2/2 when eth1/2 already exists.\"" , std::string(ex.what()), "Can't add eth2/2 when eth1/2 already exists." ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1093, gtest_ar.failure_message ()) = ::testing::Message(); } catch (...) { ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1093, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } }; | |||
1094 | ||||
1095 | IfacePtr eth2(new Iface("eth2", 3)); | |||
1096 | EXPECT_NO_THROW(IfaceMgr::instance().addInterface(eth2))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { IfaceMgr::instance().addInterface(eth2); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1096; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1096; } } else gtest_label_testnothrow_1096 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1096, ("Expected: " "IfaceMgr::instance().addInterface(eth2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1097 | } | |||
1098 | ||||
1099 | // TODO: Implement getPlainMac() test as soon as interface detection | |||
1100 | // is implemented. | |||
1101 | TEST_F(IfaceMgrTest, getIface)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("getIface") > 1, "test_name must not be empty" ); class IfaceMgrTest_getIface_Test : public IfaceMgrTest { public : IfaceMgrTest_getIface_Test() = default; ~IfaceMgrTest_getIface_Test () override = default; IfaceMgrTest_getIface_Test (const IfaceMgrTest_getIface_Test &) = delete; IfaceMgrTest_getIface_Test & operator=( const IfaceMgrTest_getIface_Test &) = delete; IfaceMgrTest_getIface_Test (IfaceMgrTest_getIface_Test &&) noexcept = delete; IfaceMgrTest_getIface_Test & operator=( IfaceMgrTest_getIface_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceMgrTest_getIface_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "getIface" , nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 1101), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("iface_mgr_unittest.cc", 1101), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("iface_mgr_unittest.cc", 1101), new ::testing::internal::TestFactoryImpl <IfaceMgrTest_getIface_Test>); void IfaceMgrTest_getIface_Test ::TestBody() { | |||
1102 | ||||
1103 | cout << "Interface checks. Please ignore socket binding errors." << endl; | |||
1104 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1105 | ||||
1106 | // Interface name, ifindex | |||
1107 | IfacePtr iface1(new Iface("lo1", 100)); | |||
1108 | IfacePtr iface2(new Iface("eth9", 101)); | |||
1109 | IfacePtr iface3(new Iface("en99", 102)); | |||
1110 | IfacePtr iface4(new Iface("e1000g4", 103)); | |||
1111 | cout << "This test assumes that there are less than 100 network interfaces" | |||
1112 | << " in the tested system and there are no lo1, eth9, en99, e1000g4" | |||
1113 | << " or wifi15 interfaces present." << endl; | |||
1114 | ||||
1115 | // Note: real interfaces may be detected as well | |||
1116 | ifacemgr->getIfacesLst().push_back(iface1); | |||
1117 | ifacemgr->getIfacesLst().push_back(iface2); | |||
1118 | ifacemgr->getIfacesLst().push_back(iface3); | |||
1119 | ifacemgr->getIfacesLst().push_back(iface4); | |||
1120 | ||||
1121 | cout << "There are " << ifacemgr->getIfacesLst().size() | |||
1122 | << " interfaces." << endl; | |||
1123 | for (auto const& iface : ifacemgr->getIfacesLst()) { | |||
1124 | cout << " " << iface->getFullName() << endl; | |||
1125 | } | |||
1126 | ||||
1127 | // Check that interface can be retrieved by ifindex | |||
1128 | IfacePtr tmp = ifacemgr->getIface(102); | |||
1129 | ASSERT_TRUE(tmp)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(tmp)) ; else return :: testing::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "iface_mgr_unittest.cc", 1129, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "tmp", "false", "true") .c_str()) = ::testing::Message (); | |||
1130 | ||||
1131 | EXPECT_EQ("en99", tmp->getName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"en99\"" , "tmp->getName()", "en99", tmp->getName()))) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1131, gtest_ar.failure_message()) = ::testing::Message(); | |||
1132 | EXPECT_EQ(102, tmp->getIndex())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("102", "tmp->getIndex()" , 102, tmp->getIndex()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1132, gtest_ar.failure_message()) = ::testing::Message(); | |||
1133 | ||||
1134 | // Check that interface can be retrieved by name | |||
1135 | tmp = ifacemgr->getIface("lo1"); | |||
1136 | ASSERT_TRUE(tmp)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(tmp)) ; else return :: testing::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "iface_mgr_unittest.cc", 1136, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "tmp", "false", "true") .c_str()) = ::testing::Message (); | |||
1137 | ||||
1138 | EXPECT_EQ("lo1", tmp->getName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"lo1\"" , "tmp->getName()", "lo1", tmp->getName()))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1138, gtest_ar.failure_message()) = ::testing::Message(); | |||
1139 | EXPECT_EQ(100, tmp->getIndex())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("100", "tmp->getIndex()" , 100, tmp->getIndex()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1139, gtest_ar.failure_message()) = ::testing::Message(); | |||
1140 | ||||
1141 | // Check that non-existing interfaces are not returned | |||
1142 | EXPECT_FALSE(ifacemgr->getIface("wifi15") )switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getIface ("wifi15")))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1142 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getIface(\"wifi15\")", "true", "false") .c_str ()) = ::testing::Message(); | |||
1143 | } | |||
1144 | ||||
1145 | TEST_F(IfaceMgrTest, clearIfaces)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("clearIfaces") > 1, "test_name must not be empty" ); class IfaceMgrTest_clearIfaces_Test : public IfaceMgrTest { public: IfaceMgrTest_clearIfaces_Test() = default; ~IfaceMgrTest_clearIfaces_Test () override = default; IfaceMgrTest_clearIfaces_Test (const IfaceMgrTest_clearIfaces_Test &) = delete; IfaceMgrTest_clearIfaces_Test & operator =( const IfaceMgrTest_clearIfaces_Test &) = delete; IfaceMgrTest_clearIfaces_Test (IfaceMgrTest_clearIfaces_Test &&) noexcept = delete ; IfaceMgrTest_clearIfaces_Test & operator=( IfaceMgrTest_clearIfaces_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_clearIfaces_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "clearIfaces", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1145), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1145), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1145), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_clearIfaces_Test >); void IfaceMgrTest_clearIfaces_Test::TestBody() { | |||
1146 | NakedIfaceMgr ifacemgr; | |||
1147 | // Create a set of fake interfaces. At the same time, remove the actual | |||
1148 | // interfaces that have been detected by the IfaceMgr. | |||
1149 | ifacemgr.createIfaces(); | |||
1150 | ||||
1151 | ASSERT_GT(ifacemgr.countIfaces(), 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGT("ifacemgr.countIfaces()" , "0", ifacemgr.countIfaces(), 0))) ; else return ::testing:: internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "iface_mgr_unittest.cc", 1151, gtest_ar.failure_message()) = ::testing::Message(); | |||
1152 | ||||
1153 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
1154 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1154, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
1155 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1155; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1155; } } else gtest_label_testnothrow_1155 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1155, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1156 | ||||
1157 | ASSERT_NO_THROW(ifacemgr.openSockets4())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1157 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1157 ; } } else gtest_label_testnothrow_1157 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1157, ("Expected: " "ifacemgr.openSockets4()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1158 | ||||
1159 | ifacemgr.clearIfaces(); | |||
1160 | ||||
1161 | EXPECT_EQ(0, ifacemgr.countIfaces())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "ifacemgr.countIfaces()" , 0, ifacemgr.countIfaces()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1161, gtest_ar.failure_message()) = ::testing::Message(); | |||
1162 | } | |||
1163 | ||||
1164 | // Verify that we have the expected default DHCPv4 packet queue. | |||
1165 | TEST_F(IfaceMgrTest, packetQueue4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("packetQueue4") > 1, "test_name must not be empty" ); class IfaceMgrTest_packetQueue4_Test : public IfaceMgrTest { public: IfaceMgrTest_packetQueue4_Test() = default; ~IfaceMgrTest_packetQueue4_Test () override = default; IfaceMgrTest_packetQueue4_Test (const IfaceMgrTest_packetQueue4_Test &) = delete; IfaceMgrTest_packetQueue4_Test & operator =( const IfaceMgrTest_packetQueue4_Test &) = delete; IfaceMgrTest_packetQueue4_Test (IfaceMgrTest_packetQueue4_Test &&) noexcept = delete ; IfaceMgrTest_packetQueue4_Test & operator=( IfaceMgrTest_packetQueue4_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_packetQueue4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "packetQueue4", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1165), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1165), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1165), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_packetQueue4_Test >); void IfaceMgrTest_packetQueue4_Test::TestBody() { | |||
1166 | NakedIfaceMgr ifacemgr; | |||
1167 | ||||
1168 | // Should not have a queue at start up. | |||
1169 | ASSERT_FALSE(ifacemgr.getPacketQueue4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.getPacketQueue4 ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1169 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getPacketQueue4()", "true", "false") .c_str()) = :: testing::Message(); | |||
1170 | ||||
1171 | // Verify that we can create a queue with default factory. | |||
1172 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 2000); | |||
1173 | ASSERT_NO_THROW(ifacemgr.getPacketQueueMgr4()->createPacketQueue(config))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.getPacketQueueMgr4()->createPacketQueue(config); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1173; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1173 ; } } else gtest_label_testnothrow_1173 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1173, ("Expected: " "ifacemgr.getPacketQueueMgr4()->createPacketQueue(config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1174 | CHECK_QUEUE_INFO(ifacemgr.getPacketQueue4(), "{ \"capacity\": 2000, \"queue-type\": \""{ std::ostringstream oss__; oss__ << "{ \"capacity\": 2000, \"queue-type\": \"" << PacketQueueMgr4::DEFAULT_QUEUE_TYPE4 << "\", \"size\": 0 }" ; checkInfo(ifacemgr.getPacketQueue4(), oss__.str().c_str()); } | |||
1175 | << PacketQueueMgr4::DEFAULT_QUEUE_TYPE4 << "\", \"size\": 0 }"){ std::ostringstream oss__; oss__ << "{ \"capacity\": 2000, \"queue-type\": \"" << PacketQueueMgr4::DEFAULT_QUEUE_TYPE4 << "\", \"size\": 0 }" ; checkInfo(ifacemgr.getPacketQueue4(), oss__.str().c_str()); }; | |||
1176 | } | |||
1177 | ||||
1178 | // Verify that we have the expected default DHCPv6 packet queue. | |||
1179 | TEST_F(IfaceMgrTest, packetQueue6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("packetQueue6") > 1, "test_name must not be empty" ); class IfaceMgrTest_packetQueue6_Test : public IfaceMgrTest { public: IfaceMgrTest_packetQueue6_Test() = default; ~IfaceMgrTest_packetQueue6_Test () override = default; IfaceMgrTest_packetQueue6_Test (const IfaceMgrTest_packetQueue6_Test &) = delete; IfaceMgrTest_packetQueue6_Test & operator =( const IfaceMgrTest_packetQueue6_Test &) = delete; IfaceMgrTest_packetQueue6_Test (IfaceMgrTest_packetQueue6_Test &&) noexcept = delete ; IfaceMgrTest_packetQueue6_Test & operator=( IfaceMgrTest_packetQueue6_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_packetQueue6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "packetQueue6", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1179), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1179), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1179), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_packetQueue6_Test >); void IfaceMgrTest_packetQueue6_Test::TestBody() { | |||
1180 | NakedIfaceMgr ifacemgr; | |||
1181 | ||||
1182 | // Should not have a queue at start up. | |||
1183 | ASSERT_FALSE(ifacemgr.getPacketQueue6())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.getPacketQueue6 ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1183 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getPacketQueue6()", "true", "false") .c_str()) = :: testing::Message(); | |||
1184 | ||||
1185 | // Verify that we can create a queue with default factory. | |||
1186 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 2000); | |||
1187 | ASSERT_NO_THROW(ifacemgr.getPacketQueueMgr6()->createPacketQueue(config))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.getPacketQueueMgr6()->createPacketQueue(config); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1187; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1187 ; } } else gtest_label_testnothrow_1187 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1187, ("Expected: " "ifacemgr.getPacketQueueMgr6()->createPacketQueue(config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1188 | CHECK_QUEUE_INFO(ifacemgr.getPacketQueue6(), "{ \"capacity\": 2000, \"queue-type\": \""{ std::ostringstream oss__; oss__ << "{ \"capacity\": 2000, \"queue-type\": \"" << PacketQueueMgr6::DEFAULT_QUEUE_TYPE6 << "\", \"size\": 0 }" ; checkInfo(ifacemgr.getPacketQueue6(), oss__.str().c_str()); } | |||
1189 | << PacketQueueMgr6::DEFAULT_QUEUE_TYPE6 << "\", \"size\": 0 }"){ std::ostringstream oss__; oss__ << "{ \"capacity\": 2000, \"queue-type\": \"" << PacketQueueMgr6::DEFAULT_QUEUE_TYPE6 << "\", \"size\": 0 }" ; checkInfo(ifacemgr.getPacketQueue6(), oss__.str().c_str()); }; | |||
1190 | } | |||
1191 | ||||
1192 | TEST_F(IfaceMgrTest, receiveTimeout6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("receiveTimeout6") > 1, "test_name must not be empty" ); class IfaceMgrTest_receiveTimeout6_Test : public IfaceMgrTest { public: IfaceMgrTest_receiveTimeout6_Test() = default; ~IfaceMgrTest_receiveTimeout6_Test () override = default; IfaceMgrTest_receiveTimeout6_Test (const IfaceMgrTest_receiveTimeout6_Test &) = delete; IfaceMgrTest_receiveTimeout6_Test & operator=( const IfaceMgrTest_receiveTimeout6_Test & ) = delete; IfaceMgrTest_receiveTimeout6_Test (IfaceMgrTest_receiveTimeout6_Test &&) noexcept = delete; IfaceMgrTest_receiveTimeout6_Test & operator=( IfaceMgrTest_receiveTimeout6_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_receiveTimeout6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "receiveTimeout6", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1192), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1192), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1192), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_receiveTimeout6_Test >); void IfaceMgrTest_receiveTimeout6_Test::TestBody() { | |||
1193 | using namespace boost::posix_time; | |||
1194 | std::cout << "Testing DHCPv6 packet reception timeouts." | |||
1195 | << " Test will block for a few seconds when waiting" | |||
1196 | << " for timeout to occur." << std::endl; | |||
1197 | ||||
1198 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1199 | // Open socket on the lo interface. | |||
1200 | IOAddress lo_addr("::1"); | |||
1201 | int socket1 = 0; | |||
1202 | ASSERT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547 ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1204; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1204 ; } } else gtest_label_testnothrow_1204 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1204, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1203 | socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547 ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1204; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1204 ; } } else gtest_label_testnothrow_1204 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1204, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1204 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547 ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1204; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1204 ; } } else gtest_label_testnothrow_1204 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1204, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1205 | // Socket is open if result is non-negative. | |||
1206 | ASSERT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1206, gtest_ar.failure_message()) = ::testing::Message(); | |||
1207 | // Start receiver. | |||
1208 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET6))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(10); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1208 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1208 ; } } else gtest_label_testnothrow_1208 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1208, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1209 | ||||
1210 | // Remember when we call receive6(). | |||
1211 | ptime start_time = microsec_clock::universal_time(); | |||
1212 | // Call receive with timeout of 1s + 400000us = 1.4s. | |||
1213 | Pkt6Ptr pkt; | |||
1214 | ASSERT_NO_THROW(pkt = ifacemgr->receive6(1, 400000))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt = ifacemgr->receive6(1, 400000); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1214; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1214; } } else gtest_label_testnothrow_1214 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1214, ("Expected: " "pkt = ifacemgr->receive6(1, 400000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1215 | // Remember when call to receive6() ended. | |||
1216 | ptime stop_time = microsec_clock::universal_time(); | |||
1217 | // We did not send a packet to lo interface so we expect that | |||
1218 | // nothing has been received and timeout has been reached. | |||
1219 | ASSERT_FALSE(pkt)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1219, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "pkt", "true", "false" ) .c_str()) = ::testing::Message(); | |||
1220 | // Calculate duration of call to receive6(). | |||
1221 | time_duration duration = stop_time - start_time; | |||
1222 | // We stop the clock when the call completes so it does not | |||
1223 | // precisely reflect the receive timeout. However the | |||
1224 | // uncertainty should be low enough to expect that measured | |||
1225 | // value is in the range <1.4s; 1.7s>. | |||
1226 | EXPECT_GE(duration.total_microseconds(),switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "1400000 - TIMEOUT_TOLERANCE", duration.total_microseconds( ), 1400000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1227, gtest_ar.failure_message()) = ::testing::Message() | |||
1227 | 1400000 - TIMEOUT_TOLERANCE)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "1400000 - TIMEOUT_TOLERANCE", duration.total_microseconds( ), 1400000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1227, gtest_ar.failure_message()) = ::testing::Message(); | |||
1228 | EXPECT_LE(duration.total_microseconds(), 1700000)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("duration.total_microseconds()" , "1700000", duration.total_microseconds(), 1700000))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1228, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1229 | ||||
1230 | // Test timeout shorter than 1s. | |||
1231 | start_time = microsec_clock::universal_time(); | |||
1232 | ASSERT_NO_THROW(pkt = ifacemgr->receive6(0, 500000))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt = ifacemgr->receive6(0, 500000); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1232; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1232; } } else gtest_label_testnothrow_1232 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1232, ("Expected: " "pkt = ifacemgr->receive6(0, 500000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1233 | stop_time = microsec_clock::universal_time(); | |||
1234 | ASSERT_FALSE(pkt)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1234, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "pkt", "true", "false" ) .c_str()) = ::testing::Message(); | |||
1235 | duration = stop_time - start_time; | |||
1236 | // Check if measured duration is within <0.5s; 0.8s>. | |||
1237 | EXPECT_GE(duration.total_microseconds(),switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "500000 - TIMEOUT_TOLERANCE", duration.total_microseconds() , 500000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1238, gtest_ar.failure_message()) = ::testing::Message() | |||
1238 | 500000 - TIMEOUT_TOLERANCE)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "500000 - TIMEOUT_TOLERANCE", duration.total_microseconds() , 500000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1238, gtest_ar.failure_message()) = ::testing::Message(); | |||
1239 | EXPECT_LE(duration.total_microseconds(), 800000)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("duration.total_microseconds()" , "800000", duration.total_microseconds(), 800000))) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1239, gtest_ar.failure_message()) = ::testing::Message(); | |||
1240 | ||||
1241 | // Test with invalid fractional timeout values. | |||
1242 | EXPECT_THROW(ifacemgr->receive6(0, 1000000), isc::BadValue)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->receive6(0, 1000000); } else static_assert(true, ""); } catch (isc::BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::BadValue>::type >::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->receive6(0, 1000000)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_1242; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->receive6(0, 1000000)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1242; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->receive6(0, 1000000)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1242; } } else gtest_label_testthrow_1242 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1242, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1243 | EXPECT_THROW(ifacemgr->receive6(1, 1000010), isc::BadValue)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->receive6(1, 1000010); } else static_assert(true, ""); } catch (isc::BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::BadValue>::type >::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->receive6(1, 1000010)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_1243; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->receive6(1, 1000010)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1243; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->receive6(1, 1000010)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1243; } } else gtest_label_testthrow_1243 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1243, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1244 | ||||
1245 | // Stop receiver. | |||
1246 | EXPECT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1246 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1246 ; } } else gtest_label_testnothrow_1246 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1246, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1247 | } | |||
1248 | ||||
1249 | TEST_F(IfaceMgrTest, receiveTimeout4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("receiveTimeout4") > 1, "test_name must not be empty" ); class IfaceMgrTest_receiveTimeout4_Test : public IfaceMgrTest { public: IfaceMgrTest_receiveTimeout4_Test() = default; ~IfaceMgrTest_receiveTimeout4_Test () override = default; IfaceMgrTest_receiveTimeout4_Test (const IfaceMgrTest_receiveTimeout4_Test &) = delete; IfaceMgrTest_receiveTimeout4_Test & operator=( const IfaceMgrTest_receiveTimeout4_Test & ) = delete; IfaceMgrTest_receiveTimeout4_Test (IfaceMgrTest_receiveTimeout4_Test &&) noexcept = delete; IfaceMgrTest_receiveTimeout4_Test & operator=( IfaceMgrTest_receiveTimeout4_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_receiveTimeout4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "receiveTimeout4", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1249), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1249), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1249), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_receiveTimeout4_Test >); void IfaceMgrTest_receiveTimeout4_Test::TestBody() { | |||
1250 | using namespace boost::posix_time; | |||
1251 | std::cout << "Testing DHCPv4 packet reception timeouts." | |||
1252 | << " Test will block for a few seconds when waiting" | |||
1253 | << " for timeout to occur." << std::endl; | |||
1254 | ||||
1255 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1256 | // Open socket on the lo interface. | |||
1257 | IOAddress lo_addr("127.0.0.1"); | |||
1258 | int socket1 = 0; | |||
1259 | ASSERT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067 ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1261; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1261 ; } } else gtest_label_testnothrow_1261 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1261, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1260 | socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067 ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1261; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1261 ; } } else gtest_label_testnothrow_1261 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1261, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1261 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067 ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1261; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1261 ; } } else gtest_label_testnothrow_1261 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1261, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1262 | // Socket is open if returned value is non-negative. | |||
1263 | ASSERT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1263, gtest_ar.failure_message()) = ::testing::Message(); | |||
1264 | ||||
1265 | // Start receiver. | |||
1266 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(2); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1266 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1266 ; } } else gtest_label_testnothrow_1266 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1266, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1267 | ||||
1268 | Pkt4Ptr pkt; | |||
1269 | // Remember when we call receive4(). | |||
1270 | ptime start_time = microsec_clock::universal_time(); | |||
1271 | // Call receive with timeout of 2s + 300000us = 2.3s. | |||
1272 | ASSERT_NO_THROW(pkt = ifacemgr->receive4(2, 300000))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt = ifacemgr->receive4(2, 300000); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1272; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1272; } } else gtest_label_testnothrow_1272 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1272, ("Expected: " "pkt = ifacemgr->receive4(2, 300000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1273 | // Remember when call to receive4() ended. | |||
1274 | ptime stop_time = microsec_clock::universal_time(); | |||
1275 | // We did not send a packet to lo interface so we expect that | |||
1276 | // nothing has been received and timeout has been reached. | |||
1277 | ASSERT_FALSE(pkt)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1277, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "pkt", "true", "false" ) .c_str()) = ::testing::Message(); | |||
1278 | // Calculate duration of call to receive4(). | |||
1279 | time_duration duration = stop_time - start_time; | |||
1280 | // We stop the clock when the call completes so it does not | |||
1281 | // precisely reflect the receive timeout. However the | |||
1282 | // uncertainty should be low enough to expect that measured | |||
1283 | // value is in the range <2.3s; 2.6s>. | |||
1284 | EXPECT_GE(duration.total_microseconds(),switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "2300000 - TIMEOUT_TOLERANCE", duration.total_microseconds( ), 2300000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1285, gtest_ar.failure_message()) = ::testing::Message() | |||
1285 | 2300000 - TIMEOUT_TOLERANCE)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "2300000 - TIMEOUT_TOLERANCE", duration.total_microseconds( ), 2300000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1285, gtest_ar.failure_message()) = ::testing::Message(); | |||
1286 | EXPECT_LE(duration.total_microseconds(), 2600000)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("duration.total_microseconds()" , "2600000", duration.total_microseconds(), 2600000))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1286, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1287 | ||||
1288 | // Test timeout shorter than 1s. | |||
1289 | start_time = microsec_clock::universal_time(); | |||
1290 | ASSERT_NO_THROW(pkt = ifacemgr->receive4(0, 400000))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt = ifacemgr->receive4(0, 400000); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1290; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1290; } } else gtest_label_testnothrow_1290 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1290, ("Expected: " "pkt = ifacemgr->receive4(0, 400000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1291 | stop_time = microsec_clock::universal_time(); | |||
1292 | ASSERT_FALSE(pkt)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1292, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "pkt", "true", "false" ) .c_str()) = ::testing::Message(); | |||
1293 | duration = stop_time - start_time; | |||
1294 | // Check if measured duration is within <0.4s; 0.7s>. | |||
1295 | EXPECT_GE(duration.total_microseconds(),switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "400000 - TIMEOUT_TOLERANCE", duration.total_microseconds() , 400000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1296, gtest_ar.failure_message()) = ::testing::Message() | |||
1296 | 400000 - TIMEOUT_TOLERANCE)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("duration.total_microseconds()" , "400000 - TIMEOUT_TOLERANCE", duration.total_microseconds() , 400000 - TIMEOUT_TOLERANCE))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1296, gtest_ar.failure_message()) = ::testing::Message(); | |||
1297 | EXPECT_LE(duration.total_microseconds(), 700000)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("duration.total_microseconds()" , "700000", duration.total_microseconds(), 700000))) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1297, gtest_ar.failure_message()) = ::testing::Message(); | |||
1298 | ||||
1299 | // Test with invalid fractional timeout values. | |||
1300 | EXPECT_THROW(ifacemgr->receive4(0, 1000000), isc::BadValue)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->receive4(0, 1000000); } else static_assert(true, ""); } catch (isc::BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::BadValue>::type >::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->receive4(0, 1000000)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_1300; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(0, 1000000)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1300; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(0, 1000000)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1300; } } else gtest_label_testthrow_1300 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1300, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1301 | EXPECT_THROW(ifacemgr->receive4(2, 1000005), isc::BadValue)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->receive4(2, 1000005); } else static_assert(true, ""); } catch (isc::BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::BadValue>::type >::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->receive4(2, 1000005)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_1301; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(2, 1000005)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1301; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(2, 1000005)" " throws an exception of type " "isc::BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1301; } } else gtest_label_testthrow_1301 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1301, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1302 | ||||
1303 | // Stop receiver. | |||
1304 | EXPECT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1304 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1304 ; } } else gtest_label_testnothrow_1304 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1304, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1305 | } | |||
1306 | ||||
1307 | TEST_F(IfaceMgrTest, multipleSockets)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("multipleSockets") > 1, "test_name must not be empty" ); class IfaceMgrTest_multipleSockets_Test : public IfaceMgrTest { public: IfaceMgrTest_multipleSockets_Test() = default; ~IfaceMgrTest_multipleSockets_Test () override = default; IfaceMgrTest_multipleSockets_Test (const IfaceMgrTest_multipleSockets_Test &) = delete; IfaceMgrTest_multipleSockets_Test & operator=( const IfaceMgrTest_multipleSockets_Test & ) = delete; IfaceMgrTest_multipleSockets_Test (IfaceMgrTest_multipleSockets_Test &&) noexcept = delete; IfaceMgrTest_multipleSockets_Test & operator=( IfaceMgrTest_multipleSockets_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_multipleSockets_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "multipleSockets", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1307), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1307), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1307), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_multipleSockets_Test >); void IfaceMgrTest_multipleSockets_Test::TestBody() { | |||
1308 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1309 | ||||
1310 | // Container for initialized socket descriptors | |||
1311 | std::list<uint16_t> init_sockets; | |||
1312 | ||||
1313 | // Create socket #1 | |||
1314 | int socket1 = 0; | |||
1315 | ASSERT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1 , 2);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1317; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1317 ; } } else gtest_label_testnothrow_1317 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1317, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| ||||
1316 | socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, AF_INET);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1 , 2);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1317; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1317 ; } } else gtest_label_testnothrow_1317 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1317, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1317 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1 , 2);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1317; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1317 ; } } else gtest_label_testnothrow_1317 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1317, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1318 | ASSERT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1318, gtest_ar.failure_message()) = ::testing::Message(); | |||
1319 | init_sockets.push_back(socket1); | |||
1320 | ||||
1321 | // Create socket #2 | |||
1322 | IOAddress lo_addr("127.0.0.1"); | |||
1323 | int socket2 = 0; | |||
1324 | ASSERT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1326; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1326 ; } } else gtest_label_testnothrow_1326 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1326, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1325 | socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1326; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1326 ; } } else gtest_label_testnothrow_1326 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1326, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1326 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1326; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1326 ; } } else gtest_label_testnothrow_1326 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1326, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1327 | ASSERT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1327, gtest_ar.failure_message()) = ::testing::Message(); | |||
1328 | init_sockets.push_back(socket2); | |||
1329 | ||||
1330 | // Get loopback interface. If we don't find one we are unable to run | |||
1331 | // this test but we don't want to fail. | |||
1332 | IfacePtr iface_ptr = ifacemgr->getIface(LOOPBACK_NAME); | |||
1333 | if (iface_ptr == NULL__null) { | |||
1334 | cout << "Local loopback interface not found. Skipping test. " << endl; | |||
1335 | return; | |||
1336 | } | |||
1337 | ASSERT_EQ(LOOPBACK_INDEX, iface_ptr->getIndex())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("LOOPBACK_INDEX" , "iface_ptr->getIndex()", LOOPBACK_INDEX, iface_ptr->getIndex ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1337 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1338 | // Once sockets have been successfully opened, they are supposed to | |||
1339 | // be on the list. Here we start to test if all expected sockets | |||
1340 | // are on the list and no other (unexpected) socket is there. | |||
1341 | Iface::SocketCollection sockets = iface_ptr->getSockets(); | |||
1342 | int matched_sockets = 0; | |||
1343 | for (std::list<uint16_t>::iterator init_sockets_it = | |||
1344 | init_sockets.begin(); | |||
1345 | init_sockets_it != init_sockets.end(); ++init_sockets_it) { | |||
1346 | // Set socket descriptors non blocking in order to be able | |||
1347 | // to call recv() on them without hang. | |||
1348 | int flags = fcntl(*init_sockets_it, F_GETFL3, 0); | |||
1349 | ASSERT_GE(flags, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("flags", "0", flags , 0))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1349 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1350 | ASSERT_GE(fcntl(*init_sockets_it, F_SETFL, flags | O_NONBLOCK), 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("fcntl(*init_sockets_it, 4, flags | 04000)" , "0", fcntl(*init_sockets_it, 4, flags | 04000), 0))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1350, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1351 | // recv() is expected to result in EWOULDBLOCK error on non-blocking | |||
1352 | // socket in case socket is valid but simply no data are coming in. | |||
1353 | char buf; | |||
1354 | recv(*init_sockets_it, &buf, 1, MSG_PEEKMSG_PEEK); | |||
1355 | EXPECT_EQ(EWOULDBLOCK, errno)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("11", "(*__errno_location ())" , 11, (*__errno_location ())))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1355, gtest_ar.failure_message()) = ::testing::Message(); | |||
1356 | // Apart from the ability to use the socket we want to make | |||
1357 | // sure that socket on the list is the one that we created. | |||
1358 | for (Iface::SocketCollection::const_iterator socket_it = | |||
1359 | sockets.begin(); socket_it != sockets.end(); ++socket_it) { | |||
1360 | if (*init_sockets_it == socket_it->sockfd_) { | |||
1361 | // This socket is the one that we created. | |||
1362 | ++matched_sockets; | |||
1363 | break; | |||
1364 | } | |||
1365 | } | |||
1366 | } | |||
1367 | // All created sockets have been matched if this condition works. | |||
1368 | EXPECT_EQ(sockets.size(), matched_sockets)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("sockets.size()" , "matched_sockets", sockets.size(), matched_sockets))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1368, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1369 | ||||
1370 | // closeSockets() is the other function that we want to test. It | |||
1371 | // is supposed to close all sockets so as we will not be able to use | |||
1372 | // them anymore communication. | |||
1373 | ifacemgr->closeSockets(); | |||
1374 | ||||
1375 | // Closed sockets are supposed to be removed from the list | |||
1376 | sockets = iface_ptr->getSockets(); | |||
1377 | ASSERT_EQ(0, sockets.size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "sockets.size()" , 0, sockets.size()))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1377, gtest_ar.failure_message()) = ::testing::Message(); | |||
1378 | ||||
1379 | // We are still in possession of socket descriptors that we created | |||
1380 | // on the beginning of this test. We can use them to check whether | |||
1381 | // closeSockets() only removed them from the list or they have been | |||
1382 | // really closed. | |||
1383 | for (std::list<uint16_t>::const_iterator init_sockets_it = | |||
1384 | init_sockets.begin(); | |||
1385 | init_sockets_it != init_sockets.end(); ++init_sockets_it) { | |||
1386 | // recv() must result in error when using invalid socket. | |||
1387 | char buf; | |||
1388 | static_cast<void>(recv(*init_sockets_it, &buf, 1, MSG_PEEKMSG_PEEK)); | |||
1389 | // EWOULDBLOCK would mean that socket is valid/open but | |||
1390 | // simply no data is received so we have to check for | |||
1391 | // other errors. | |||
1392 | EXPECT_NE(EWOULDBLOCK, errno)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("11", "(*__errno_location ())" , 11, (*__errno_location ())))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1392, gtest_ar.failure_message()) = ::testing::Message(); | |||
1393 | } | |||
1394 | } | |||
1395 | ||||
1396 | TEST_F(IfaceMgrTest, sockets6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("sockets6") > 1, "test_name must not be empty" ); class IfaceMgrTest_sockets6_Test : public IfaceMgrTest { public : IfaceMgrTest_sockets6_Test() = default; ~IfaceMgrTest_sockets6_Test () override = default; IfaceMgrTest_sockets6_Test (const IfaceMgrTest_sockets6_Test &) = delete; IfaceMgrTest_sockets6_Test & operator=( const IfaceMgrTest_sockets6_Test &) = delete; IfaceMgrTest_sockets6_Test (IfaceMgrTest_sockets6_Test &&) noexcept = delete; IfaceMgrTest_sockets6_Test & operator=( IfaceMgrTest_sockets6_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceMgrTest_sockets6_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "sockets6" , nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 1396), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("iface_mgr_unittest.cc", 1396), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("iface_mgr_unittest.cc", 1396), new ::testing::internal::TestFactoryImpl <IfaceMgrTest_sockets6_Test>); void IfaceMgrTest_sockets6_Test ::TestBody() { | |||
1397 | // Testing socket operation in a portable way is tricky | |||
1398 | // without interface detection implemented. | |||
1399 | ||||
1400 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1401 | ||||
1402 | IOAddress lo_addr("::1"); | |||
1403 | ||||
1404 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, 123)); | |||
1405 | pkt6->setIface(LOOPBACK_NAME); | |||
1406 | ||||
1407 | // Bind multicast socket to port 10547 | |||
1408 | int socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); | |||
1409 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1409 , gtest_ar.failure_message()) = ::testing::Message(); // socket >= 0 | |||
1410 | ||||
1411 | EXPECT_EQ(socket1, ifacemgr->getSocket(pkt6))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("socket1" , "ifacemgr->getSocket(pkt6)", socket1, ifacemgr->getSocket (pkt6)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1411 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1412 | ||||
1413 | // Bind unicast socket to port 10548 | |||
1414 | int socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10548); | |||
1415 | EXPECT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1415 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1416 | ||||
1417 | // Removed code for binding socket twice to the same address/port | |||
1418 | // as it caused problems on some platforms (e.g. Mac OS X) | |||
1419 | ||||
1420 | // Close sockets here because the following tests will want to | |||
1421 | // open sockets on the same ports. | |||
1422 | ifacemgr->closeSockets(); | |||
1423 | ||||
1424 | // Use address that is not assigned to LOOPBACK iface. | |||
1425 | IOAddress invalidAddr("::2"); | |||
1426 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( LOOPBACK_NAME, invalidAddr, 10547); } else static_assert(true , ""); } catch (SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< SocketConfigError>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1429 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1429; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1429; } } else gtest_label_testthrow_1429 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1429, gtest_msg. value.c_str()) = ::testing::Message() | |||
1427 | ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( LOOPBACK_NAME, invalidAddr, 10547); } else static_assert(true , ""); } catch (SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< SocketConfigError>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1429 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1429; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1429; } } else gtest_label_testthrow_1429 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1429, gtest_msg. value.c_str()) = ::testing::Message() | |||
1428 | SocketConfigErrorswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( LOOPBACK_NAME, invalidAddr, 10547); } else static_assert(true , ""); } catch (SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< SocketConfigError>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1429 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1429; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1429; } } else gtest_label_testthrow_1429 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1429, gtest_msg. value.c_str()) = ::testing::Message() | |||
1429 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( LOOPBACK_NAME, invalidAddr, 10547); } else static_assert(true , ""); } catch (SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< SocketConfigError>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1429 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1429; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(LOOPBACK_NAME, invalidAddr, 10547)" " throws an exception of type " "SocketConfigError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1429; } } else gtest_label_testthrow_1429 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1429, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1430 | ||||
1431 | // Use non-existing interface name. | |||
1432 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( "non_existing_interface", lo_addr, 10548); } else static_assert (true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1435 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1435; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1435; } } else gtest_label_testthrow_1435 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1435, gtest_msg. value.c_str()) = ::testing::Message() | |||
1433 | ifacemgr->openSocket("non_existing_interface", lo_addr, 10548),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( "non_existing_interface", lo_addr, 10548); } else static_assert (true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1435 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1435; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1435; } } else gtest_label_testthrow_1435 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1435, gtest_msg. value.c_str()) = ::testing::Message() | |||
1434 | BadValueswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( "non_existing_interface", lo_addr, 10548); } else static_assert (true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1435 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1435; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1435; } } else gtest_label_testthrow_1435 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1435, gtest_msg. value.c_str()) = ::testing::Message() | |||
1435 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocket( "non_existing_interface", lo_addr, 10548); } else static_assert (true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1435 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1435; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocket(\"non_existing_interface\", lo_addr, 10548)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1435; } } else gtest_label_testthrow_1435 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1435, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1436 | ||||
1437 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
1438 | // virtual destructor. | |||
1439 | } | |||
1440 | ||||
1441 | TEST_F(IfaceMgrTest, socketsFromIface)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("socketsFromIface") > 1, "test_name must not be empty" ); class IfaceMgrTest_socketsFromIface_Test : public IfaceMgrTest { public: IfaceMgrTest_socketsFromIface_Test() = default; ~IfaceMgrTest_socketsFromIface_Test () override = default; IfaceMgrTest_socketsFromIface_Test (const IfaceMgrTest_socketsFromIface_Test &) = delete; IfaceMgrTest_socketsFromIface_Test & operator=( const IfaceMgrTest_socketsFromIface_Test & ) = delete; IfaceMgrTest_socketsFromIface_Test (IfaceMgrTest_socketsFromIface_Test &&) noexcept = delete; IfaceMgrTest_socketsFromIface_Test & operator=( IfaceMgrTest_socketsFromIface_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_socketsFromIface_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketsFromIface", nullptr, nullptr, ::testing::internal:: CodeLocation("iface_mgr_unittest.cc", 1441), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1441), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1441), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketsFromIface_Test >); void IfaceMgrTest_socketsFromIface_Test::TestBody() { | |||
1442 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1443 | ||||
1444 | // Open v6 socket on loopback interface and bind to port | |||
1445 | int socket1 = 0; | |||
1446 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1 , 10);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1448; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1448 ; } } else gtest_label_testnothrow_1448 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1448, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 10);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1447 | socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, AF_INET6);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1 , 10);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1448; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1448 ; } } else gtest_label_testnothrow_1448 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1448, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 10);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1448 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1 , 10);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1448; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1448 ; } } else gtest_label_testnothrow_1448 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1448, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 10);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1449 | // Socket descriptor must be non-negative integer | |||
1450 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1450 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1451 | close(socket1); | |||
1452 | ||||
1453 | // Open v4 socket on loopback interface and bind to different port | |||
1454 | int socket2 = 0; | |||
1455 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2 , 2);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1457; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1457 ; } } else gtest_label_testnothrow_1457 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1457, ("Expected: " "socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1456 | socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2, AF_INET);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2 , 2);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1457; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1457 ; } } else gtest_label_testnothrow_1457 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1457, ("Expected: " "socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1457 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2 , 2);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1457; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1457 ; } } else gtest_label_testnothrow_1457 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1457, ("Expected: " "socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1458 | // socket descriptor must be non-negative integer | |||
1459 | EXPECT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1459 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1460 | close(socket2); | |||
1461 | ||||
1462 | // Close sockets here because the following tests will want to | |||
1463 | // open sockets on the same ports. | |||
1464 | ifacemgr->closeSockets(); | |||
1465 | ||||
1466 | // Use invalid interface name. | |||
1467 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocketFromIface ("non_existing_interface", PORT1, 2); } else static_assert(true , ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1470 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1470; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1470; } } else gtest_label_testthrow_1470 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1470, gtest_msg. value.c_str()) = ::testing::Message() | |||
1468 | ifacemgr->openSocketFromIface("non_existing_interface", PORT1, AF_INET),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocketFromIface ("non_existing_interface", PORT1, 2); } else static_assert(true , ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1470 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1470; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1470; } } else gtest_label_testthrow_1470 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1470, gtest_msg. value.c_str()) = ::testing::Message() | |||
1469 | BadValueswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocketFromIface ("non_existing_interface", PORT1, 2); } else static_assert(true , ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1470 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1470; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1470; } } else gtest_label_testthrow_1470 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1470, gtest_msg. value.c_str()) = ::testing::Message() | |||
1470 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocketFromIface ("non_existing_interface", PORT1, 2); } else static_assert(true , ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< BadValue>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown&, const std::exception& >::type e) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1470 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1470; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromIface(\"non_existing_interface\", PORT1, 2)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1470; } } else gtest_label_testthrow_1470 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1470, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1471 | ||||
1472 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
1473 | // virtual destructor. | |||
1474 | } | |||
1475 | ||||
1476 | ||||
1477 | TEST_F(IfaceMgrTest, socketsFromAddress)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("socketsFromAddress") > 1, "test_name must not be empty" ); class IfaceMgrTest_socketsFromAddress_Test : public IfaceMgrTest { public: IfaceMgrTest_socketsFromAddress_Test() = default; ~ IfaceMgrTest_socketsFromAddress_Test() override = default; IfaceMgrTest_socketsFromAddress_Test (const IfaceMgrTest_socketsFromAddress_Test &) = delete; IfaceMgrTest_socketsFromAddress_Test & operator=( const IfaceMgrTest_socketsFromAddress_Test &) = delete; IfaceMgrTest_socketsFromAddress_Test (IfaceMgrTest_socketsFromAddress_Test &&) noexcept = delete; IfaceMgrTest_socketsFromAddress_Test & operator=( IfaceMgrTest_socketsFromAddress_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_socketsFromAddress_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketsFromAddress", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1477), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1477), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1477), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketsFromAddress_Test >); void IfaceMgrTest_socketsFromAddress_Test::TestBody() { | |||
1478 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1479 | ||||
1480 | // Open v6 socket on loopback interface and bind to port | |||
1481 | int socket1 = 0; | |||
1482 | IOAddress lo_addr6("::1"); | |||
1483 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1485; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1485 ; } } else gtest_label_testnothrow_1485 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1485, ("Expected: " "socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1484 | socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1485; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1485 ; } } else gtest_label_testnothrow_1485 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1485, ("Expected: " "socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1485 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1485; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1485 ; } } else gtest_label_testnothrow_1485 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1485, ("Expected: " "socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1486 | // socket descriptor must be non-negative integer | |||
1487 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1487 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1488 | ||||
1489 | // Open v4 socket on loopback interface and bind to different port | |||
1490 | int socket2 = 0; | |||
1491 | IOAddress lo_addr("127.0.0.1"); | |||
1492 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2) ;; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1494; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1494 ; } } else gtest_label_testnothrow_1494 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1494, ("Expected: " "socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1493 | socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2) ;; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1494; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1494 ; } } else gtest_label_testnothrow_1494 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1494, ("Expected: " "socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1494 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2) ;; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1494; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1494 ; } } else gtest_label_testnothrow_1494 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1494, ("Expected: " "socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1495 | // socket descriptor must be positive integer | |||
1496 | EXPECT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1496 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1497 | ||||
1498 | // Close sockets here because the following tests will want to | |||
1499 | // open sockets on the same ports. | |||
1500 | ifacemgr->closeSockets(); | |||
1501 | ||||
1502 | // Use non-existing address. | |||
1503 | IOAddress invalidAddr("1.2.3.4"); | |||
1504 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocketFromAddress (invalidAddr, PORT1); } else static_assert(true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std:: remove_cv<typename std::remove_reference< BadValue>:: type>::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1506 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1506; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1506; } } else gtest_label_testthrow_1506 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1506, gtest_msg. value.c_str()) = ::testing::Message() | |||
1505 | ifacemgr->openSocketFromAddress(invalidAddr, PORT1), BadValueswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocketFromAddress (invalidAddr, PORT1); } else static_assert(true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std:: remove_cv<typename std::remove_reference< BadValue>:: type>::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1506 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1506; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1506; } } else gtest_label_testthrow_1506 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1506, gtest_msg. value.c_str()) = ::testing::Message() | |||
1506 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->openSocketFromAddress (invalidAddr, PORT1); } else static_assert(true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std:: remove_cv<typename std::remove_reference< BadValue>:: type>::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1506 ; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1506; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->openSocketFromAddress(invalidAddr, PORT1)" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1506; } } else gtest_label_testthrow_1506 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1506, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1507 | ||||
1508 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
1509 | // virtual destructor. | |||
1510 | } | |||
1511 | ||||
1512 | TEST_F(IfaceMgrTest, socketsFromRemoteAddress)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("socketsFromRemoteAddress") > 1, "test_name must not be empty" ); class IfaceMgrTest_socketsFromRemoteAddress_Test : public IfaceMgrTest { public: IfaceMgrTest_socketsFromRemoteAddress_Test() = default ; ~IfaceMgrTest_socketsFromRemoteAddress_Test() override = default ; IfaceMgrTest_socketsFromRemoteAddress_Test (const IfaceMgrTest_socketsFromRemoteAddress_Test &) = delete; IfaceMgrTest_socketsFromRemoteAddress_Test & operator=( const IfaceMgrTest_socketsFromRemoteAddress_Test & ) = delete; IfaceMgrTest_socketsFromRemoteAddress_Test (IfaceMgrTest_socketsFromRemoteAddress_Test &&) noexcept = delete; IfaceMgrTest_socketsFromRemoteAddress_Test & operator=( IfaceMgrTest_socketsFromRemoteAddress_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_socketsFromRemoteAddress_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketsFromRemoteAddress", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1512), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1512), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1512), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketsFromRemoteAddress_Test >); void IfaceMgrTest_socketsFromRemoteAddress_Test::TestBody () { | |||
1513 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1514 | ||||
1515 | // Open v6 socket to connect to remote address. | |||
1516 | // Loopback address is the only one that we know | |||
1517 | // so let's treat it as remote address. | |||
1518 | int socket1 = 0; | |||
1519 | IOAddress lo_addr6("::1"); | |||
1520 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1522; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1522 ; } } else gtest_label_testnothrow_1522 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1522, ("Expected: " "socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1521 | socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1522; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1522 ; } } else gtest_label_testnothrow_1522 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1522, ("Expected: " "socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1522 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1522; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1522 ; } } else gtest_label_testnothrow_1522 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1522, ("Expected: " "socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1523 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1523 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1524 | ||||
1525 | // Open v4 socket to connect to remote address. | |||
1526 | int socket2 = 0; | |||
1527 | IOAddress lo_addr("127.0.0.1"); | |||
1528 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1530; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1530 ; } } else gtest_label_testnothrow_1530 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1530, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1529 | socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1530; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1530 ; } } else gtest_label_testnothrow_1530 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1530, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1530 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2 );; } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1530; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1530 ; } } else gtest_label_testnothrow_1530 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1530, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1531 | EXPECT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1531 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1532 | ||||
1533 | // Close sockets here because the following tests will want to | |||
1534 | // open sockets on the same ports. | |||
1535 | ifacemgr->closeSockets(); | |||
1536 | ||||
1537 | // There used to be a check here that verified the ability to open | |||
1538 | // suitable socket for sending broadcast request. However, | |||
1539 | // there is no guarantee for such test to work on all systems | |||
1540 | // because some systems may have no broadcast capable interfaces at all. | |||
1541 | // Thus, this check has been removed. | |||
1542 | ||||
1543 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
1544 | // virtual destructor. | |||
1545 | } | |||
1546 | ||||
1547 | // TODO: disabled due to other naming on various systems | |||
1548 | // (lo in Linux, lo0 in BSD systems) | |||
1549 | TEST_F(IfaceMgrTest, DISABLED_sockets6Mcast)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DISABLED_sockets6Mcast") > 1, "test_name must not be empty" ); class IfaceMgrTest_DISABLED_sockets6Mcast_Test : public IfaceMgrTest { public: IfaceMgrTest_DISABLED_sockets6Mcast_Test() = default ; ~IfaceMgrTest_DISABLED_sockets6Mcast_Test() override = default ; IfaceMgrTest_DISABLED_sockets6Mcast_Test (const IfaceMgrTest_DISABLED_sockets6Mcast_Test &) = delete; IfaceMgrTest_DISABLED_sockets6Mcast_Test & operator=( const IfaceMgrTest_DISABLED_sockets6Mcast_Test & ) = delete; IfaceMgrTest_DISABLED_sockets6Mcast_Test (IfaceMgrTest_DISABLED_sockets6Mcast_Test &&) noexcept = delete; IfaceMgrTest_DISABLED_sockets6Mcast_Test & operator=( IfaceMgrTest_DISABLED_sockets6Mcast_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_DISABLED_sockets6Mcast_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DISABLED_sockets6Mcast", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1549), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1549), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1549), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DISABLED_sockets6Mcast_Test >); void IfaceMgrTest_DISABLED_sockets6Mcast_Test::TestBody () { | |||
1550 | // testing socket operation in a portable way is tricky | |||
1551 | // without interface detection implemented | |||
1552 | ||||
1553 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1554 | ||||
1555 | IOAddress lo_addr("::1"); | |||
1556 | IOAddress mcastAddr("ff02::1:2"); | |||
1557 | ||||
1558 | // bind multicast socket to port 10547 | |||
1559 | int socket1 = ifacemgr->openSocket(LOOPBACK_NAME, mcastAddr, 10547); | |||
1560 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1560 , gtest_ar.failure_message()) = ::testing::Message(); // socket > 0 | |||
1561 | ||||
1562 | // expect success. This address/port is already bound, but | |||
1563 | // we are using SO_REUSEADDR, so we can bind it twice | |||
1564 | int socket2 = ifacemgr->openSocket(LOOPBACK_NAME, mcastAddr, 10547); | |||
1565 | EXPECT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1565 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1566 | ||||
1567 | // there's no good way to test negative case here. | |||
1568 | // we would need non-multicast interface. We will be able | |||
1569 | // to iterate thru available interfaces and check if there | |||
1570 | // are interfaces without multicast-capable flag. | |||
1571 | ||||
1572 | close(socket1); | |||
1573 | close(socket2); | |||
1574 | } | |||
1575 | ||||
1576 | // Verifies that basic DHCPv6 packet send and receive operates | |||
1577 | // in either direct or indirect mode. | |||
1578 | TEST_F(IfaceMgrTest, sendReceive6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("sendReceive6") > 1, "test_name must not be empty" ); class IfaceMgrTest_sendReceive6_Test : public IfaceMgrTest { public: IfaceMgrTest_sendReceive6_Test() = default; ~IfaceMgrTest_sendReceive6_Test () override = default; IfaceMgrTest_sendReceive6_Test (const IfaceMgrTest_sendReceive6_Test &) = delete; IfaceMgrTest_sendReceive6_Test & operator =( const IfaceMgrTest_sendReceive6_Test &) = delete; IfaceMgrTest_sendReceive6_Test (IfaceMgrTest_sendReceive6_Test &&) noexcept = delete ; IfaceMgrTest_sendReceive6_Test & operator=( IfaceMgrTest_sendReceive6_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_sendReceive6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "sendReceive6", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1578), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1578), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1578), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_sendReceive6_Test >); void IfaceMgrTest_sendReceive6_Test::TestBody() { | |||
1579 | data::ElementPtr queue_control; | |||
1580 | ||||
1581 | // Given an empty pointer, queueing should be disabled. | |||
1582 | // This should do direct reception. | |||
1583 | sendReceive6Test(queue_control, false); | |||
1584 | ||||
1585 | // Now let's populate queue control. | |||
1586 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, false); | |||
1587 | // With queueing disabled, we should use direct reception. | |||
1588 | sendReceive6Test(queue_control, false); | |||
1589 | ||||
1590 | // Queuing enabled, indirection reception should work. | |||
1591 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, true); | |||
1592 | sendReceive6Test(queue_control, true); | |||
1593 | } | |||
1594 | ||||
1595 | // Verifies that basic DHCPv4 packet send and receive operates | |||
1596 | // in either direct or indirect mode. | |||
1597 | TEST_F(IfaceMgrTest, sendReceive4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("sendReceive4") > 1, "test_name must not be empty" ); class IfaceMgrTest_sendReceive4_Test : public IfaceMgrTest { public: IfaceMgrTest_sendReceive4_Test() = default; ~IfaceMgrTest_sendReceive4_Test () override = default; IfaceMgrTest_sendReceive4_Test (const IfaceMgrTest_sendReceive4_Test &) = delete; IfaceMgrTest_sendReceive4_Test & operator =( const IfaceMgrTest_sendReceive4_Test &) = delete; IfaceMgrTest_sendReceive4_Test (IfaceMgrTest_sendReceive4_Test &&) noexcept = delete ; IfaceMgrTest_sendReceive4_Test & operator=( IfaceMgrTest_sendReceive4_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_sendReceive4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "sendReceive4", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1597), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1597), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1597), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_sendReceive4_Test >); void IfaceMgrTest_sendReceive4_Test::TestBody() { | |||
1598 | data::ElementPtr queue_control; | |||
1599 | ||||
1600 | // Given an empty pointer, queueing should be disabled. | |||
1601 | // This should do direct reception. | |||
1602 | sendReceive4Test(queue_control, false); | |||
1603 | ||||
1604 | // Now let's populate queue control. | |||
1605 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, false); | |||
1606 | // With queueing disabled, we should use direct reception. | |||
1607 | sendReceive4Test(queue_control, false); | |||
1608 | ||||
1609 | // Queuing enabled, indirection reception should work. | |||
1610 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, true); | |||
1611 | sendReceive4Test(queue_control, true); | |||
1612 | } | |||
1613 | ||||
1614 | // Verifies that it is possible to set custom packet filter object | |||
1615 | // to handle sockets opening and send/receive operation. | |||
1616 | TEST_F(IfaceMgrTest, setPacketFilter)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("setPacketFilter") > 1, "test_name must not be empty" ); class IfaceMgrTest_setPacketFilter_Test : public IfaceMgrTest { public: IfaceMgrTest_setPacketFilter_Test() = default; ~IfaceMgrTest_setPacketFilter_Test () override = default; IfaceMgrTest_setPacketFilter_Test (const IfaceMgrTest_setPacketFilter_Test &) = delete; IfaceMgrTest_setPacketFilter_Test & operator=( const IfaceMgrTest_setPacketFilter_Test & ) = delete; IfaceMgrTest_setPacketFilter_Test (IfaceMgrTest_setPacketFilter_Test &&) noexcept = delete; IfaceMgrTest_setPacketFilter_Test & operator=( IfaceMgrTest_setPacketFilter_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_setPacketFilter_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setPacketFilter", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1616), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1616), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1616), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setPacketFilter_Test >); void IfaceMgrTest_setPacketFilter_Test::TestBody() { | |||
1617 | ||||
1618 | // Create an instance of IfaceMgr. | |||
1619 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr(new NakedIfaceMgr()); | |||
1620 | ASSERT_TRUE(iface_mgr)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface_mgr)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1620, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr", "false" , "true") .c_str()) = ::testing::Message(); | |||
1621 | ||||
1622 | // Try to set NULL packet filter object and make sure it is rejected. | |||
1623 | boost::shared_ptr<TestPktFilter> custom_packet_filter; | |||
1624 | EXPECT_THROW(iface_mgr->setPacketFilter(custom_packet_filter),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (isc::dhcp::InvalidPacketFilter const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< isc::dhcp::InvalidPacketFilter>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1625; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1625 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1625; } } else gtest_label_testthrow_1625 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1625, gtest_msg. value.c_str()) = ::testing::Message() | |||
1625 | isc::dhcp::InvalidPacketFilter)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (isc::dhcp::InvalidPacketFilter const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< isc::dhcp::InvalidPacketFilter>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1625; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1625 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1625; } } else gtest_label_testthrow_1625 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1625, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1626 | ||||
1627 | // Create valid object and check if it can be set. | |||
1628 | custom_packet_filter.reset(new TestPktFilter()); | |||
1629 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1629, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
1630 | ASSERT_NO_THROW(iface_mgr->setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1630; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1630; } } else gtest_label_testnothrow_1630 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1630, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1631 | ||||
1632 | // Try to open socket using IfaceMgr. It should call the openSocket() function | |||
1633 | // on the packet filter object we have set. | |||
1634 | IOAddress lo_addr("127.0.0.1"); | |||
1635 | int socket1 = 0; | |||
1636 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1639; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1639 ; } } else gtest_label_testnothrow_1639 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1639, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1637 | socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr,switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1639; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1639 ; } } else gtest_label_testnothrow_1639 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1639, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1638 | DHCP4_SERVER_PORT + 10000);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1639; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1639 ; } } else gtest_label_testnothrow_1639 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1639, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1639 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1639; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1639 ; } } else gtest_label_testnothrow_1639 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1639, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1640 | ||||
1641 | // Check that openSocket function was called. | |||
1642 | EXPECT_TRUE(custom_packet_filter->open_socket_called_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter-> open_socket_called_)) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1642, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter->open_socket_called_", "false", "true" ) .c_str()) = ::testing::Message(); | |||
1643 | // This function always returns fake socket descriptor equal to 255. | |||
1644 | EXPECT_EQ(255, socket1)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("255", "socket1" , 255, socket1))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1644, gtest_ar.failure_message()) = ::testing::Message(); | |||
1645 | ||||
1646 | // Replacing current packet filter object while there are IPv4 | |||
1647 | // sockets open is not allowed! | |||
1648 | EXPECT_THROW(iface_mgr->setPacketFilter(custom_packet_filter),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (PacketFilterChangeDenied const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< PacketFilterChangeDenied>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1649; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1649; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1649; } } else gtest_label_testthrow_1649 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1649, gtest_msg. value.c_str()) = ::testing::Message() | |||
1649 | PacketFilterChangeDenied)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (PacketFilterChangeDenied const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< PacketFilterChangeDenied>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1649; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1649; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1649; } } else gtest_label_testthrow_1649 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1649, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1650 | ||||
1651 | // So, let's close the open sockets and retry. Now it should succeed. | |||
1652 | iface_mgr->closeSockets(); | |||
1653 | EXPECT_NO_THROW(iface_mgr->setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1653; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1653; } } else gtest_label_testnothrow_1653 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1653, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1654 | } | |||
1655 | ||||
1656 | // This test checks that the default packet filter for DHCPv6 can be replaced | |||
1657 | // with the custom one. | |||
1658 | TEST_F(IfaceMgrTest, setPacketFilter6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("setPacketFilter6") > 1, "test_name must not be empty" ); class IfaceMgrTest_setPacketFilter6_Test : public IfaceMgrTest { public: IfaceMgrTest_setPacketFilter6_Test() = default; ~IfaceMgrTest_setPacketFilter6_Test () override = default; IfaceMgrTest_setPacketFilter6_Test (const IfaceMgrTest_setPacketFilter6_Test &) = delete; IfaceMgrTest_setPacketFilter6_Test & operator=( const IfaceMgrTest_setPacketFilter6_Test & ) = delete; IfaceMgrTest_setPacketFilter6_Test (IfaceMgrTest_setPacketFilter6_Test &&) noexcept = delete; IfaceMgrTest_setPacketFilter6_Test & operator=( IfaceMgrTest_setPacketFilter6_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_setPacketFilter6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setPacketFilter6", nullptr, nullptr, ::testing::internal:: CodeLocation("iface_mgr_unittest.cc", 1658), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1658), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1658), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setPacketFilter6_Test >); void IfaceMgrTest_setPacketFilter6_Test::TestBody() { | |||
1659 | // Create an instance of IfaceMgr. | |||
1660 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr(new NakedIfaceMgr()); | |||
1661 | ASSERT_TRUE(iface_mgr)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface_mgr)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1661, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr", "false" , "true") .c_str()) = ::testing::Message(); | |||
1662 | ||||
1663 | // Try to set NULL packet filter object and make sure it is rejected. | |||
1664 | boost::shared_ptr<PktFilter6Stub> custom_packet_filter; | |||
1665 | EXPECT_THROW(iface_mgr->setPacketFilter(custom_packet_filter),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (isc::dhcp::InvalidPacketFilter const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< isc::dhcp::InvalidPacketFilter>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1666; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1666 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1666; } } else gtest_label_testthrow_1666 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1666, gtest_msg. value.c_str()) = ::testing::Message() | |||
1666 | isc::dhcp::InvalidPacketFilter)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (isc::dhcp::InvalidPacketFilter const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< isc::dhcp::InvalidPacketFilter>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1666; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1666 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "isc::dhcp::InvalidPacketFilter" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1666; } } else gtest_label_testthrow_1666 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1666, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1667 | ||||
1668 | // Create valid object and check if it can be set. | |||
1669 | custom_packet_filter.reset(new PktFilter6Stub()); | |||
1670 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1670, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
1671 | ASSERT_NO_THROW(iface_mgr->setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1671; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1671; } } else gtest_label_testnothrow_1671 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1671, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1672 | ||||
1673 | // Try to open socket using IfaceMgr. It should call the openSocket() | |||
1674 | // function on the packet filter object we have set. | |||
1675 | IOAddress lo_addr("::1"); | |||
1676 | int socket1 = 0; | |||
1677 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1680; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1680 ; } } else gtest_label_testnothrow_1680 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1680, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1678 | socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr,switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1680; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1680 ; } } else gtest_label_testnothrow_1680 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1680, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1679 | DHCP6_SERVER_PORT + 10000);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1680; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1680 ; } } else gtest_label_testnothrow_1680 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1680, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1680 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1680; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1680 ; } } else gtest_label_testnothrow_1680 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1680, ("Expected: " "socket1 = iface_mgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP6_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1681 | // Check that openSocket function has been actually called on the packet | |||
1682 | // filter object. | |||
1683 | EXPECT_EQ(1, custom_packet_filter->open_socket_count_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "custom_packet_filter->open_socket_count_" , 1, custom_packet_filter->open_socket_count_))) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1683, gtest_ar.failure_message()) = ::testing::Message(); | |||
1684 | // Also check that the returned socket descriptor has an expected value. | |||
1685 | EXPECT_EQ(0, socket1)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "socket1" , 0, socket1))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1685 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1686 | ||||
1687 | // Replacing current packet filter object, while there are sockets open, | |||
1688 | // is not allowed! | |||
1689 | EXPECT_THROW(iface_mgr->setPacketFilter(custom_packet_filter),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (PacketFilterChangeDenied const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< PacketFilterChangeDenied>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1690; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1690; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1690; } } else gtest_label_testthrow_1690 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1690, gtest_msg. value.c_str()) = ::testing::Message() | |||
1690 | PacketFilterChangeDenied)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter (custom_packet_filter); } else static_assert(true, ""); } catch (PacketFilterChangeDenied const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< PacketFilterChangeDenied>::type>::type, std::exception >::value, const ::testing::internal::NeverThrown&, const std::exception&>::type e) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1690; } catch (...) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_1690; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " throws an exception of type " "PacketFilterChangeDenied" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1690; } } else gtest_label_testthrow_1690 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1690, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1691 | ||||
1692 | // So, let's close the sockets and retry. Now it should succeed. | |||
1693 | iface_mgr->closeSockets(); | |||
1694 | EXPECT_NO_THROW(iface_mgr->setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1694; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1694; } } else gtest_label_testnothrow_1694 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1694, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1695 | } | |||
1696 | ||||
1697 | #if defined OS_LINUX1 || OS_BSD | |||
1698 | ||||
1699 | // This test is only supported on Linux and BSD systems. It checks | |||
1700 | // if it is possible to use the IfaceMgr to select the packet filter | |||
1701 | // object which can be used to send direct responses to the host | |||
1702 | // which doesn't have an address yet. | |||
1703 | TEST_F(IfaceMgrTest, setMatchingPacketFilter)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("setMatchingPacketFilter") > 1, "test_name must not be empty" ); class IfaceMgrTest_setMatchingPacketFilter_Test : public IfaceMgrTest { public: IfaceMgrTest_setMatchingPacketFilter_Test() = default ; ~IfaceMgrTest_setMatchingPacketFilter_Test() override = default ; IfaceMgrTest_setMatchingPacketFilter_Test (const IfaceMgrTest_setMatchingPacketFilter_Test &) = delete; IfaceMgrTest_setMatchingPacketFilter_Test & operator=( const IfaceMgrTest_setMatchingPacketFilter_Test & ) = delete; IfaceMgrTest_setMatchingPacketFilter_Test (IfaceMgrTest_setMatchingPacketFilter_Test &&) noexcept = delete; IfaceMgrTest_setMatchingPacketFilter_Test & operator=( IfaceMgrTest_setMatchingPacketFilter_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_setMatchingPacketFilter_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setMatchingPacketFilter", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1703), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1703), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1703), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setMatchingPacketFilter_Test >); void IfaceMgrTest_setMatchingPacketFilter_Test::TestBody () { | |||
1704 | ||||
1705 | // Create an instance of IfaceMgr. | |||
1706 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr(new NakedIfaceMgr()); | |||
1707 | ASSERT_TRUE(iface_mgr)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface_mgr)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1707, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr", "false" , "true") .c_str()) = ::testing::Message(); | |||
1708 | ||||
1709 | // Let IfaceMgr figure out which Packet Filter to use when | |||
1710 | // direct response capability is not desired. It should pick | |||
1711 | // PktFilterInet on Linux. | |||
1712 | EXPECT_NO_THROW(iface_mgr->setMatchingPacketFilter(false))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setMatchingPacketFilter(false); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1712; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1712; } } else gtest_label_testnothrow_1712 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1712, ("Expected: " "iface_mgr->setMatchingPacketFilter(false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1713 | // The PktFilterInet is supposed to report lack of direct | |||
1714 | // response capability. | |||
1715 | EXPECT_FALSE(iface_mgr->isDirectResponseSupported())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface_mgr->isDirectResponseSupported ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1715, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr->isDirectResponseSupported()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1716 | ||||
1717 | // There is working implementation of direct responses on Linux | |||
1718 | // and BSD (using PktFilterLPF and PktFilterBPF. When direct | |||
1719 | // responses are desired the object of this class should be set. | |||
1720 | EXPECT_NO_THROW(iface_mgr->setMatchingPacketFilter(true))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setMatchingPacketFilter(true); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1720; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1720; } } else gtest_label_testnothrow_1720 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1720, ("Expected: " "iface_mgr->setMatchingPacketFilter(true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1721 | // This object should report that direct responses are supported. | |||
1722 | EXPECT_TRUE(iface_mgr->isDirectResponseSupported())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface_mgr->isDirectResponseSupported ())) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1722, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr->isDirectResponseSupported()" , "false", "true") .c_str()) = ::testing::Message(); | |||
1723 | } | |||
1724 | ||||
1725 | // This test checks that it is not possible to open two sockets: IP/UDP | |||
1726 | // and raw socket and bind to the same address and port. The | |||
1727 | // raw socket should be opened together with the fallback IP/UDP socket. | |||
1728 | // The fallback socket should fail to open when there is another IP/UDP | |||
1729 | // socket bound to the same address and port. Failing to open the fallback | |||
1730 | // socket should preclude the raw socket from being open. | |||
1731 | TEST_F(IfaceMgrTest, checkPacketFilterRawSocket)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("checkPacketFilterRawSocket") > 1, "test_name must not be empty"); class IfaceMgrTest_checkPacketFilterRawSocket_Test : public IfaceMgrTest { public: IfaceMgrTest_checkPacketFilterRawSocket_Test () = default; ~IfaceMgrTest_checkPacketFilterRawSocket_Test() override = default; IfaceMgrTest_checkPacketFilterRawSocket_Test (const IfaceMgrTest_checkPacketFilterRawSocket_Test &) = delete; IfaceMgrTest_checkPacketFilterRawSocket_Test & operator =( const IfaceMgrTest_checkPacketFilterRawSocket_Test &) = delete; IfaceMgrTest_checkPacketFilterRawSocket_Test (IfaceMgrTest_checkPacketFilterRawSocket_Test &&) noexcept = delete; IfaceMgrTest_checkPacketFilterRawSocket_Test & operator=( IfaceMgrTest_checkPacketFilterRawSocket_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_checkPacketFilterRawSocket_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "checkPacketFilterRawSocket", nullptr, nullptr, ::testing:: internal::CodeLocation("iface_mgr_unittest.cc", 1731), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 1731), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 1731), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_checkPacketFilterRawSocket_Test >); void IfaceMgrTest_checkPacketFilterRawSocket_Test::TestBody () { | |||
1732 | IOAddress lo_addr("127.0.0.1"); | |||
1733 | int socket1 = -1, socket2 = -1; | |||
1734 | // Create two instances of IfaceMgr. | |||
1735 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr1(new NakedIfaceMgr()); | |||
1736 | ASSERT_TRUE(iface_mgr1)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface_mgr1)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1736, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr1", "false" , "true") .c_str()) = ::testing::Message(); | |||
1737 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr2(new NakedIfaceMgr()); | |||
1738 | ASSERT_TRUE(iface_mgr2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface_mgr2)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1738, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr2", "false" , "true") .c_str()) = ::testing::Message(); | |||
1739 | ||||
1740 | // Let IfaceMgr figure out which Packet Filter to use when | |||
1741 | // direct response capability is not desired. It should pick | |||
1742 | // PktFilterInet. | |||
1743 | EXPECT_NO_THROW(iface_mgr1->setMatchingPacketFilter(false))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr1->setMatchingPacketFilter(false); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1743; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1743; } } else gtest_label_testnothrow_1743 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1743, ("Expected: " "iface_mgr1->setMatchingPacketFilter(false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1744 | // Let's open a loopback socket with handy unpriviliged port number | |||
1745 | socket1 = iface_mgr1->openSocket(LOOPBACK_NAME, lo_addr, | |||
1746 | DHCP4_SERVER_PORT + 10000); | |||
1747 | ||||
1748 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1748 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1749 | ||||
1750 | // Then the second use PkFilterLPF mode | |||
1751 | EXPECT_NO_THROW(iface_mgr2->setMatchingPacketFilter(true))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr2->setMatchingPacketFilter(true); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1751; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1751; } } else gtest_label_testnothrow_1751 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1751, ("Expected: " "iface_mgr2->setMatchingPacketFilter(true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1752 | ||||
1753 | // The socket is open and bound. Another attempt to open socket and | |||
1754 | // bind to the same address and port should result in an exception. | |||
1755 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { socket2 = iface_mgr2-> openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000) ; } else static_assert(true, ""); } catch (isc::dhcp::SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::dhcp::SocketConfigError >::type>::type, std::exception>::value, const ::testing ::internal::NeverThrown&, const std::exception&>:: type e) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1759; } catch (...) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1759 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1759 ; } } else gtest_label_testthrow_1759 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1759, gtest_msg.value.c_str()) = ::testing::Message() | |||
1756 | socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr,switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { socket2 = iface_mgr2-> openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000) ; } else static_assert(true, ""); } catch (isc::dhcp::SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::dhcp::SocketConfigError >::type>::type, std::exception>::value, const ::testing ::internal::NeverThrown&, const std::exception&>:: type e) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1759; } catch (...) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1759 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1759 ; } } else gtest_label_testthrow_1759 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1759, gtest_msg.value.c_str()) = ::testing::Message() | |||
1757 | DHCP4_SERVER_PORT + 10000),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { socket2 = iface_mgr2-> openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000) ; } else static_assert(true, ""); } catch (isc::dhcp::SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::dhcp::SocketConfigError >::type>::type, std::exception>::value, const ::testing ::internal::NeverThrown&, const std::exception&>:: type e) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1759; } catch (...) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1759 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1759 ; } } else gtest_label_testthrow_1759 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1759, gtest_msg.value.c_str()) = ::testing::Message() | |||
1758 | isc::dhcp::SocketConfigErrorswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { socket2 = iface_mgr2-> openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000) ; } else static_assert(true, ""); } catch (isc::dhcp::SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::dhcp::SocketConfigError >::type>::type, std::exception>::value, const ::testing ::internal::NeverThrown&, const std::exception&>:: type e) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1759; } catch (...) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1759 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1759 ; } } else gtest_label_testthrow_1759 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1759, gtest_msg.value.c_str()) = ::testing::Message() | |||
1759 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { socket2 = iface_mgr2-> openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000) ; } else static_assert(true, ""); } catch (isc::dhcp::SocketConfigError const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< isc::dhcp::SocketConfigError >::type>::type, std::exception>::value, const ::testing ::internal::NeverThrown&, const std::exception&>:: type e) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1759; } catch (...) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1759 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "socket2 = iface_mgr2->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1759 ; } } else gtest_label_testthrow_1759 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1759, gtest_msg.value.c_str()) = ::testing::Message(); | |||
1760 | // Surprisingly we managed to open another socket. We have to close it | |||
1761 | // to prevent resource leak. | |||
1762 | if (socket2 >= 0) { | |||
1763 | close(socket2); | |||
1764 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1764, "Failed") = :: testing::Message() << "Two sockets opened and bound to the same IP" | |||
1765 | " address and UDP port"; | |||
1766 | } | |||
1767 | ||||
1768 | if (socket1 >= 0) { | |||
1769 | close(socket1); | |||
1770 | } | |||
1771 | } | |||
1772 | ||||
1773 | #else | |||
1774 | ||||
1775 | // Note: This test will only run on non-Linux and non-BSD systems. | |||
1776 | // This test checks whether it is possible to use IfaceMgr to figure | |||
1777 | // out which Packet Filter object should be used when direct responses | |||
1778 | // to hosts, having no address assigned are desired or not desired. | |||
1779 | // Since direct responses aren't supported on systems other than Linux | |||
1780 | // and BSD the function under test should always set object of | |||
1781 | // PktFilterInet type as current Packet Filter. This object does not | |||
1782 | //support direct responses. Once implementation is added on systems | |||
1783 | // other than BSD and Linux the OS specific version of the test will | |||
1784 | // be removed. | |||
1785 | TEST_F(IfaceMgrTest, setMatchingPacketFilter)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("setMatchingPacketFilter") > 1, "test_name must not be empty" ); class IfaceMgrTest_setMatchingPacketFilter_Test : public IfaceMgrTest { public: IfaceMgrTest_setMatchingPacketFilter_Test() = default ; ~IfaceMgrTest_setMatchingPacketFilter_Test() override = default ; IfaceMgrTest_setMatchingPacketFilter_Test (const IfaceMgrTest_setMatchingPacketFilter_Test &) = delete; IfaceMgrTest_setMatchingPacketFilter_Test & operator=( const IfaceMgrTest_setMatchingPacketFilter_Test & ) = delete; IfaceMgrTest_setMatchingPacketFilter_Test (IfaceMgrTest_setMatchingPacketFilter_Test &&) noexcept = delete; IfaceMgrTest_setMatchingPacketFilter_Test & operator=( IfaceMgrTest_setMatchingPacketFilter_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_setMatchingPacketFilter_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setMatchingPacketFilter", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1785), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1785), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1785), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setMatchingPacketFilter_Test >); void IfaceMgrTest_setMatchingPacketFilter_Test::TestBody () { | |||
1786 | ||||
1787 | // Create an instance of IfaceMgr. | |||
1788 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr(new NakedIfaceMgr()); | |||
1789 | ASSERT_TRUE(iface_mgr)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface_mgr)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 1789, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr", "false" , "true") .c_str()) = ::testing::Message(); | |||
1790 | ||||
1791 | // Let IfaceMgr figure out which Packet Filter to use when | |||
1792 | // direct response capability is not desired. It should pick | |||
1793 | // PktFilterInet. | |||
1794 | EXPECT_NO_THROW(iface_mgr->setMatchingPacketFilter(false))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setMatchingPacketFilter(false); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1794; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1794; } } else gtest_label_testnothrow_1794 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1794, ("Expected: " "iface_mgr->setMatchingPacketFilter(false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1795 | // The PktFilterInet is supposed to report lack of direct | |||
1796 | // response capability. | |||
1797 | EXPECT_FALSE(iface_mgr->isDirectResponseSupported())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface_mgr->isDirectResponseSupported ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1797, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr->isDirectResponseSupported()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1798 | ||||
1799 | // On non-Linux systems, we are missing the direct traffic | |||
1800 | // implementation. Therefore, we expect that PktFilterInet | |||
1801 | // object will be set. | |||
1802 | EXPECT_NO_THROW(iface_mgr->setMatchingPacketFilter(true))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface_mgr->setMatchingPacketFilter(true); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1802; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1802; } } else gtest_label_testnothrow_1802 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1802, ("Expected: " "iface_mgr->setMatchingPacketFilter(true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1803 | // This object should report lack of direct response capability. | |||
1804 | EXPECT_FALSE(iface_mgr->isDirectResponseSupported())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface_mgr->isDirectResponseSupported ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1804, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface_mgr->isDirectResponseSupported()" , "true", "false") .c_str()) = ::testing::Message(); | |||
1805 | } | |||
1806 | ||||
1807 | #endif | |||
1808 | ||||
1809 | TEST_F(IfaceMgrTest, socket4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("socket4") > 1, "test_name must not be empty" ); class IfaceMgrTest_socket4_Test : public IfaceMgrTest { public : IfaceMgrTest_socket4_Test() = default; ~IfaceMgrTest_socket4_Test () override = default; IfaceMgrTest_socket4_Test (const IfaceMgrTest_socket4_Test &) = delete; IfaceMgrTest_socket4_Test & operator=( const IfaceMgrTest_socket4_Test &) = delete; IfaceMgrTest_socket4_Test (IfaceMgrTest_socket4_Test &&) noexcept = delete; IfaceMgrTest_socket4_Test & operator=( IfaceMgrTest_socket4_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceMgrTest_socket4_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "socket4" , nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 1809), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("iface_mgr_unittest.cc", 1809), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("iface_mgr_unittest.cc", 1809), new ::testing::internal::TestFactoryImpl <IfaceMgrTest_socket4_Test>); void IfaceMgrTest_socket4_Test ::TestBody() { | |||
1810 | ||||
1811 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
1812 | ||||
1813 | // Let's assume that every supported OS have lo interface. | |||
1814 | IOAddress lo_addr("127.0.0.1"); | |||
1815 | // Use unprivileged port (it's convenient for running tests as non-root). | |||
1816 | int socket1 = 0; | |||
1817 | ||||
1818 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1821; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1821 ; } } else gtest_label_testnothrow_1821 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1821, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1819 | socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr,switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1821; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1821 ; } } else gtest_label_testnothrow_1821 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1821, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1820 | DHCP4_SERVER_PORT + 10000);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1821; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1821 ; } } else gtest_label_testnothrow_1821 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1821, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1821 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1821; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1821 ; } } else gtest_label_testnothrow_1821 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1821, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, DHCP4_SERVER_PORT + 10000);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1822 | ||||
1823 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1823 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1824 | ||||
1825 | Pkt4Ptr pkt(new Pkt4(DHCPDISCOVER, 1234)); | |||
1826 | pkt->setIface(LOOPBACK_NAME); | |||
1827 | pkt->setIndex(LOOPBACK_INDEX); | |||
1828 | ||||
1829 | // Expect that we get the socket that we just opened. | |||
1830 | EXPECT_EQ(socket1, ifacemgr->getSocket(pkt).sockfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("socket1" , "ifacemgr->getSocket(pkt).sockfd_", socket1, ifacemgr-> getSocket(pkt).sockfd_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1830, gtest_ar.failure_message()) = ::testing::Message(); | |||
1831 | ||||
1832 | close(socket1); | |||
1833 | } | |||
1834 | ||||
1835 | // This test verifies that IPv4 sockets are open on all interfaces (except | |||
1836 | // loopback), when interfaces are up, running and active (not disabled from | |||
1837 | // the DHCP configuration). | |||
1838 | TEST_F(IfaceMgrTest, openSockets4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets4") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets4_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets4_Test() = default; ~IfaceMgrTest_openSockets4_Test () override = default; IfaceMgrTest_openSockets4_Test (const IfaceMgrTest_openSockets4_Test &) = delete; IfaceMgrTest_openSockets4_Test & operator =( const IfaceMgrTest_openSockets4_Test &) = delete; IfaceMgrTest_openSockets4_Test (IfaceMgrTest_openSockets4_Test &&) noexcept = delete ; IfaceMgrTest_openSockets4_Test & operator=( IfaceMgrTest_openSockets4_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_openSockets4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 1838), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1838), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1838), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4_Test >); void IfaceMgrTest_openSockets4_Test::TestBody() { | |||
1839 | NakedIfaceMgr ifacemgr; | |||
1840 | ||||
1841 | // Remove all real interfaces and create a set of dummy interfaces. | |||
1842 | ifacemgr.createIfaces(); | |||
1843 | ||||
1844 | // Use the custom packet filter object. This object mimics the socket | |||
1845 | // opening operation - the real socket is not open. | |||
1846 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
1847 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1847, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
1848 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1848; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1848; } } else gtest_label_testnothrow_1848 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1848, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1849 | ||||
1850 | // Simulate opening sockets using the dummy packet filter. | |||
1851 | ASSERT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1851; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1851; } } else gtest_label_testnothrow_1851 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1851, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1852 | ||||
1853 | // Expect that the sockets are open on both eth0 and eth1. | |||
1854 | EXPECT_EQ(1, ifacemgr.getIface("eth0")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"eth0\")->getSockets().size()" , 1, ifacemgr.getIface("eth0")->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1854, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1855 | EXPECT_EQ(1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(ETH0_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1855, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1856 | EXPECT_EQ(1, ifacemgr.getIface("eth1")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"eth1\")->getSockets().size()" , 1, ifacemgr.getIface("eth1")->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1856, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1857 | EXPECT_EQ(1, ifacemgr.getIface(ETH1_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(ETH1_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(ETH1_INDEX)->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1857, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1858 | // Socket shouldn't have been opened on loopback. | |||
1859 | EXPECT_TRUE(ifacemgr.getIface("lo")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("lo" )->getSockets().empty())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1859, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"lo\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
1860 | EXPECT_TRUE(ifacemgr.getIface(LO_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface(LO_INDEX )->getSockets().empty())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1860, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(LO_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
1861 | } | |||
1862 | ||||
1863 | // This test verifies that IPv4 sockets are open on the loopback interface | |||
1864 | // when the loopback is active and allowed. | |||
1865 | TEST_F(IfaceMgrTest, openSockets4Loopback)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets4Loopback") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets4Loopback_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets4Loopback_Test() = default ; ~IfaceMgrTest_openSockets4Loopback_Test() override = default ; IfaceMgrTest_openSockets4Loopback_Test (const IfaceMgrTest_openSockets4Loopback_Test &) = delete; IfaceMgrTest_openSockets4Loopback_Test & operator=( const IfaceMgrTest_openSockets4Loopback_Test & ) = delete; IfaceMgrTest_openSockets4Loopback_Test (IfaceMgrTest_openSockets4Loopback_Test &&) noexcept = delete; IfaceMgrTest_openSockets4Loopback_Test & operator=( IfaceMgrTest_openSockets4Loopback_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets4Loopback_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4Loopback", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1865), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1865), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1865), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4Loopback_Test >); void IfaceMgrTest_openSockets4Loopback_Test::TestBody( ) { | |||
1866 | NakedIfaceMgr ifacemgr; | |||
1867 | ||||
1868 | // Remove all real interfaces and create a set of dummy interfaces. | |||
1869 | ifacemgr.createIfaces(); | |||
1870 | ||||
1871 | // Allow the loopback interface. | |||
1872 | ifacemgr.setAllowLoopBack(true); | |||
1873 | ||||
1874 | // Make the loopback interface active. | |||
1875 | ifacemgr.getIface("lo")->inactive4_ = false; | |||
1876 | ||||
1877 | // Use the custom packet filter object. This object mimics the socket | |||
1878 | // opening operation - the real socket is not open. | |||
1879 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
1880 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1880, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
1881 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1881; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1881; } } else gtest_label_testnothrow_1881 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1881, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1882 | ||||
1883 | // Simulate opening sockets using the dummy packet filter. | |||
1884 | ASSERT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1884; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1884; } } else gtest_label_testnothrow_1884 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1884, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1885 | ||||
1886 | // Expect that the sockets are open on all interfaces. | |||
1887 | EXPECT_EQ(1, ifacemgr.getIface("eth0")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"eth0\")->getSockets().size()" , 1, ifacemgr.getIface("eth0")->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1887, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1888 | EXPECT_EQ(1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(ETH0_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1888, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1889 | EXPECT_EQ(1, ifacemgr.getIface("eth1")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"eth1\")->getSockets().size()" , 1, ifacemgr.getIface("eth1")->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1889, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1890 | EXPECT_EQ(1, ifacemgr.getIface(ETH1_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(ETH1_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(ETH1_INDEX)->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1890, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1891 | EXPECT_EQ(1, ifacemgr.getIface("lo")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"lo\")->getSockets().size()" , 1, ifacemgr.getIface("lo")->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1891, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1892 | EXPECT_EQ(1, ifacemgr.getIface(LO_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(LO_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(LO_INDEX)->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1892, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1893 | } | |||
1894 | ||||
1895 | // This test verifies that the socket is not open on the interface which is | |||
1896 | // down, but sockets are open on all other non-loopback interfaces. | |||
1897 | TEST_F(IfaceMgrTest, openSockets4IfaceDown)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets4IfaceDown") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets4IfaceDown_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets4IfaceDown_Test() = default ; ~IfaceMgrTest_openSockets4IfaceDown_Test() override = default ; IfaceMgrTest_openSockets4IfaceDown_Test (const IfaceMgrTest_openSockets4IfaceDown_Test &) = delete; IfaceMgrTest_openSockets4IfaceDown_Test & operator=( const IfaceMgrTest_openSockets4IfaceDown_Test & ) = delete; IfaceMgrTest_openSockets4IfaceDown_Test (IfaceMgrTest_openSockets4IfaceDown_Test &&) noexcept = delete; IfaceMgrTest_openSockets4IfaceDown_Test & operator=( IfaceMgrTest_openSockets4IfaceDown_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets4IfaceDown_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4IfaceDown", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1897), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1897), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1897), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4IfaceDown_Test >); void IfaceMgrTest_openSockets4IfaceDown_Test::TestBody () { | |||
1898 | IfaceMgrTestConfig config(true); | |||
1899 | ||||
1900 | // Boolean parameters specify that eth0 is: | |||
1901 | // - not a loopback | |||
1902 | // - is "down" (not up) | |||
1903 | // - is not running | |||
1904 | // - is active (is not inactive) | |||
1905 | config.setIfaceFlags("eth0", FlagLoopback(false), FlagUp(false), | |||
1906 | FlagRunning(false), FlagInactive4(false), | |||
1907 | FlagInactive6(false)); | |||
1908 | ASSERT_FALSE(IfaceMgr::instance().getIface("eth0")->flag_up_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(IfaceMgr::instance( ).getIface("eth0")->flag_up_))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1908, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "IfaceMgr::instance().getIface(\"eth0\")->flag_up_", "true" , "false") .c_str()) = ::testing::Message(); | |||
1909 | ASSERT_FALSE(IfaceMgr::instance().getIface(ETH0_INDEX)->flag_up_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(IfaceMgr::instance( ).getIface(ETH0_INDEX)->flag_up_))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "iface_mgr_unittest.cc", 1909, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "IfaceMgr::instance().getIface(ETH0_INDEX)->flag_up_" , "true", "false") .c_str()) = ::testing::Message(); | |||
1910 | ||||
1911 | // Install an error handler before trying to open sockets. This handler | |||
1912 | // should be called when the IfaceMgr fails to open socket on an interface | |||
1913 | // on which the server is configured to listen. | |||
1914 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
1915 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
1916 | ||||
1917 | ASSERT_NO_THROW(IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true,switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true, error_handler ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1918; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1918 ; } } else gtest_label_testnothrow_1918 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1918, ("Expected: " "IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1918 | error_handler))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true, error_handler ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1918; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1918 ; } } else gtest_label_testnothrow_1918 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1918, ("Expected: " "IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1919 | // Since the interface is down, an attempt to open a socket should result | |||
1920 | // in error. | |||
1921 | EXPECT_EQ(1, errors_count_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "errors_count_" , 1, errors_count_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1921, gtest_ar.failure_message()) = ::testing::Message(); | |||
1922 | ||||
1923 | // There should be no socket on eth0 open, because interface was down. | |||
1924 | EXPECT_TRUE(IfaceMgr::instance().getIface("eth0")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(IfaceMgr::instance(). getIface("eth0")->getSockets().empty())) ; else ::testing:: internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1924, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "IfaceMgr::instance().getIface(\"eth0\")->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
1925 | EXPECT_TRUE(IfaceMgr::instance().getIface(ETH0_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(IfaceMgr::instance(). getIface(ETH0_INDEX)->getSockets().empty())) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1925, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "IfaceMgr::instance().getIface(ETH0_INDEX)->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
1926 | ||||
1927 | // Expecting that the socket is open on eth1 because it was up, running | |||
1928 | // and active. | |||
1929 | EXPECT_EQ(2, IfaceMgr::instance().getIface("eth1")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("2", "IfaceMgr::instance().getIface(\"eth1\")->getSockets().size()" , 2, IfaceMgr::instance().getIface("eth1")->getSockets().size ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1929, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1930 | EXPECT_EQ(2, IfaceMgr::instance().getIface(ETH1_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("2", "IfaceMgr::instance().getIface(ETH1_INDEX)->getSockets().size()" , 2, IfaceMgr::instance().getIface(ETH1_INDEX)->getSockets ().size()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1930 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1931 | // Same for eth1961. | |||
1932 | EXPECT_EQ(1, IfaceMgr::instance().getIface("eth1961")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "IfaceMgr::instance().getIface(\"eth1961\")->getSockets().size()" , 1, IfaceMgr::instance().getIface("eth1961")->getSockets( ).size()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1932 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1933 | EXPECT_EQ(1, IfaceMgr::instance().getIface(ETH1961_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "IfaceMgr::instance().getIface(ETH1961_INDEX)->getSockets().size()" , 1, IfaceMgr::instance().getIface(ETH1961_INDEX)->getSockets ().size()))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 1933 , gtest_ar.failure_message()) = ::testing::Message(); | |||
1934 | // Never open socket on loopback interface. | |||
1935 | EXPECT_TRUE(IfaceMgr::instance().getIface("lo")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(IfaceMgr::instance(). getIface("lo")->getSockets().empty())) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1935, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "IfaceMgr::instance().getIface(\"lo\")->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
1936 | EXPECT_TRUE(IfaceMgr::instance().getIface(LO_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(IfaceMgr::instance(). getIface(LO_INDEX)->getSockets().empty())) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 1936, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "IfaceMgr::instance().getIface(LO_INDEX)->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
1937 | } | |||
1938 | ||||
1939 | // This test verifies that the socket is not open on the interface which is | |||
1940 | // disabled from the DHCP configuration, but sockets are open on all other | |||
1941 | // non-loopback interfaces. | |||
1942 | TEST_F(IfaceMgrTest, openSockets4IfaceInactive)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets4IfaceInactive") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets4IfaceInactive_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets4IfaceInactive_Test () = default; ~IfaceMgrTest_openSockets4IfaceInactive_Test() override = default; IfaceMgrTest_openSockets4IfaceInactive_Test (const IfaceMgrTest_openSockets4IfaceInactive_Test &) = delete; IfaceMgrTest_openSockets4IfaceInactive_Test & operator=( const IfaceMgrTest_openSockets4IfaceInactive_Test &) = delete ; IfaceMgrTest_openSockets4IfaceInactive_Test (IfaceMgrTest_openSockets4IfaceInactive_Test &&) noexcept = delete; IfaceMgrTest_openSockets4IfaceInactive_Test & operator=( IfaceMgrTest_openSockets4IfaceInactive_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_openSockets4IfaceInactive_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4IfaceInactive", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 1942), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 1942), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 1942), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4IfaceInactive_Test >); void IfaceMgrTest_openSockets4IfaceInactive_Test::TestBody () { | |||
1943 | NakedIfaceMgr ifacemgr; | |||
1944 | ||||
1945 | // Remove all real interfaces and create a set of dummy interfaces. | |||
1946 | ifacemgr.createIfaces(); | |||
1947 | ||||
1948 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
1949 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1949, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
1950 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1950; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1950; } } else gtest_label_testnothrow_1950 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1950, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1951 | ||||
1952 | // Boolean parameters specify that eth1 is: | |||
1953 | // - not a loopback | |||
1954 | // - is up | |||
1955 | // - is running | |||
1956 | // - is inactive | |||
1957 | ifacemgr.setIfaceFlags("eth1", false, true, true, true, false); | |||
1958 | ASSERT_TRUE(ifacemgr.getIface("eth1")->inactive4_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth1" )->inactive4_)) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1958, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth1\")->inactive4_", "false", "true" ) .c_str()) = ::testing::Message(); | |||
1959 | ASSERT_TRUE(ifacemgr.getIface(ETH1_INDEX)->inactive4_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface(ETH1_INDEX )->inactive4_)) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1959, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH1_INDEX)->inactive4_", "false", "true" ) .c_str()) = ::testing::Message(); | |||
1960 | ASSERT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1960; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1960; } } else gtest_label_testnothrow_1960 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1960, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1961 | ||||
1962 | // The socket on eth0 should be open because interface is up, running and | |||
1963 | // active (not disabled through DHCP configuration, for example). | |||
1964 | EXPECT_EQ(1, ifacemgr.getIface("eth0")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"eth0\")->getSockets().size()" , 1, ifacemgr.getIface("eth0")->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 1964, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1965 | EXPECT_EQ(1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(ETH0_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1965, gtest_ar.failure_message ()) = ::testing::Message(); | |||
1966 | // There should be no socket open on eth1 because it was marked inactive. | |||
1967 | EXPECT_TRUE(ifacemgr.getIface("eth1")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth1" )->getSockets().empty())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1967, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth1\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
1968 | EXPECT_TRUE(ifacemgr.getIface(ETH1_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface(ETH1_INDEX )->getSockets().empty())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1968, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH1_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
1969 | // Sockets are not open on loopback interfaces too. | |||
1970 | EXPECT_TRUE(ifacemgr.getIface("lo")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("lo" )->getSockets().empty())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1970, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"lo\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
1971 | EXPECT_TRUE(ifacemgr.getIface(LO_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface(LO_INDEX )->getSockets().empty())) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 1971, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(LO_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
1972 | } | |||
1973 | ||||
1974 | // Test that exception is thrown when trying to bind a new socket to the port | |||
1975 | // and address which is already in use by another socket. | |||
1976 | TEST_F(IfaceMgrTest, openSockets4NoErrorHandler)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets4NoErrorHandler") > 1, "test_name must not be empty"); class IfaceMgrTest_openSockets4NoErrorHandler_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets4NoErrorHandler_Test () = default; ~IfaceMgrTest_openSockets4NoErrorHandler_Test() override = default; IfaceMgrTest_openSockets4NoErrorHandler_Test (const IfaceMgrTest_openSockets4NoErrorHandler_Test &) = delete; IfaceMgrTest_openSockets4NoErrorHandler_Test & operator =( const IfaceMgrTest_openSockets4NoErrorHandler_Test &) = delete; IfaceMgrTest_openSockets4NoErrorHandler_Test (IfaceMgrTest_openSockets4NoErrorHandler_Test &&) noexcept = delete; IfaceMgrTest_openSockets4NoErrorHandler_Test & operator=( IfaceMgrTest_openSockets4NoErrorHandler_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_openSockets4NoErrorHandler_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4NoErrorHandler", nullptr, nullptr, ::testing:: internal::CodeLocation("iface_mgr_unittest.cc", 1976), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 1976), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 1976), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4NoErrorHandler_Test >); void IfaceMgrTest_openSockets4NoErrorHandler_Test::TestBody () { | |||
1977 | NakedIfaceMgr ifacemgr; | |||
1978 | ||||
1979 | // Remove all real interfaces and create a set of dummy interfaces. | |||
1980 | ifacemgr.createIfaces(); | |||
1981 | ||||
1982 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
1983 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 1983, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
1984 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_1984; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1984; } } else gtest_label_testnothrow_1984 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 1984, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
1985 | ||||
1986 | // Open socket on eth1. The openSockets4 should detect that this | |||
1987 | // socket has been already open and an attempt to open another socket | |||
1988 | // and bind to this address and port should fail. | |||
1989 | ASSERT_NO_THROW(ifacemgr.openSocket("eth1", IOAddress("192.0.2.3"),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth1", IOAddress("192.0.2.3"), DHCP4_SERVER_PORT ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1990; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1990 ; } } else gtest_label_testnothrow_1990 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1990, ("Expected: " "ifacemgr.openSocket(\"eth1\", IOAddress(\"192.0.2.3\"), DHCP4_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
1990 | DHCP4_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth1", IOAddress("192.0.2.3"), DHCP4_SERVER_PORT ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1990; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1990 ; } } else gtest_label_testnothrow_1990 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 1990, ("Expected: " "ifacemgr.openSocket(\"eth1\", IOAddress(\"192.0.2.3\"), DHCP4_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
1991 | ||||
1992 | // The function throws an exception when it tries to open a socket | |||
1993 | // and bind it to the address in use. | |||
1994 | EXPECT_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT , true, 0); } else static_assert(true, ""); } catch (isc::dhcp ::SocketConfigError const&) { gtest_caught_expected = true ; } catch (typename std::conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< isc::dhcp ::SocketConfigError>::type>::type, std::exception>:: value, const ::testing::internal::NeverThrown&, const std ::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1995; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1995 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1995; } } else gtest_label_testthrow_1995 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1995, gtest_msg. value.c_str()) = ::testing::Message() | |||
1995 | isc::dhcp::SocketConfigError)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT , true, 0); } else static_assert(true, ""); } catch (isc::dhcp ::SocketConfigError const&) { gtest_caught_expected = true ; } catch (typename std::conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< isc::dhcp ::SocketConfigError>::type>::type, std::exception>:: value, const ::testing::internal::NeverThrown&, const std ::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testthrow_1995; } catch (...) { gtest_msg.value = "Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1995 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " throws an exception of type " "isc::dhcp::SocketConfigError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_1995; } } else gtest_label_testthrow_1995 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 1995, gtest_msg. value.c_str()) = ::testing::Message(); | |||
1996 | } | |||
1997 | ||||
1998 | // Test that the external error handler is called when trying to bind a new | |||
1999 | // socket to the address and port being in use. The sockets on the other | |||
2000 | // interfaces should open just fine. | |||
2001 | TEST_F(IfaceMgrTest, openSocket4ErrorHandler)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSocket4ErrorHandler") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSocket4ErrorHandler_Test : public IfaceMgrTest { public: IfaceMgrTest_openSocket4ErrorHandler_Test() = default ; ~IfaceMgrTest_openSocket4ErrorHandler_Test() override = default ; IfaceMgrTest_openSocket4ErrorHandler_Test (const IfaceMgrTest_openSocket4ErrorHandler_Test &) = delete; IfaceMgrTest_openSocket4ErrorHandler_Test & operator=( const IfaceMgrTest_openSocket4ErrorHandler_Test & ) = delete; IfaceMgrTest_openSocket4ErrorHandler_Test (IfaceMgrTest_openSocket4ErrorHandler_Test &&) noexcept = delete; IfaceMgrTest_openSocket4ErrorHandler_Test & operator=( IfaceMgrTest_openSocket4ErrorHandler_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSocket4ErrorHandler_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSocket4ErrorHandler", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2001), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2001), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2001), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSocket4ErrorHandler_Test >); void IfaceMgrTest_openSocket4ErrorHandler_Test::TestBody () { | |||
2002 | NakedIfaceMgr ifacemgr; | |||
2003 | ||||
2004 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2005 | ifacemgr.createIfaces(); | |||
2006 | ||||
2007 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
2008 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 2008, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
2009 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2009; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2009; } } else gtest_label_testnothrow_2009 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2009, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2010 | ||||
2011 | // Open socket on eth0. | |||
2012 | ASSERT_NO_THROW(ifacemgr.openSocket("eth0", IOAddress("10.0.0.1"),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("10.0.0.1"), DHCP4_SERVER_PORT ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2013; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2013 ; } } else gtest_label_testnothrow_2013 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2013, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"10.0.0.1\"), DHCP4_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2013 | DHCP4_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("10.0.0.1"), DHCP4_SERVER_PORT ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2013; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2013 ; } } else gtest_label_testnothrow_2013 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2013, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"10.0.0.1\"), DHCP4_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2014 | ||||
2015 | // Install an error handler before trying to open sockets. This handler | |||
2016 | // should be called when the IfaceMgr fails to open socket on eth0. | |||
2017 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
2018 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
2019 | // The openSockets4 should detect that there is another socket already | |||
2020 | // open and bound to the same address and port. An attempt to open | |||
2021 | // another socket and bind to this address and port should fail. | |||
2022 | ASSERT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2022; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2022 ; } } else gtest_label_testnothrow_2022 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2022, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2023 | // We expect that an error occurred when we tried to open a socket on | |||
2024 | // eth0, but the socket on eth1 should open just fine. | |||
2025 | EXPECT_EQ(1, errors_count_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "errors_count_" , 1, errors_count_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2025, gtest_ar.failure_message()) = ::testing::Message(); | |||
2026 | ||||
2027 | // Reset errors count. | |||
2028 | errors_count_ = 0; | |||
2029 | ||||
2030 | // Now that we have two sockets open, we can try this again but this time | |||
2031 | // we should get two errors: one when opening a socket on eth0, another one | |||
2032 | // when opening a socket on eth1. | |||
2033 | ASSERT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2033; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2033 ; } } else gtest_label_testnothrow_2033 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2033, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2034 | EXPECT_EQ(2, errors_count_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("2", "errors_count_" , 2, errors_count_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2034, gtest_ar.failure_message()) = ::testing::Message(); | |||
2035 | } | |||
2036 | ||||
2037 | // Test that no exception is thrown when a port is already bound but skip open | |||
2038 | // flag is provided. | |||
2039 | TEST_F(IfaceMgrTest, openSockets4SkipOpen)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets4SkipOpen") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets4SkipOpen_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets4SkipOpen_Test() = default ; ~IfaceMgrTest_openSockets4SkipOpen_Test() override = default ; IfaceMgrTest_openSockets4SkipOpen_Test (const IfaceMgrTest_openSockets4SkipOpen_Test &) = delete; IfaceMgrTest_openSockets4SkipOpen_Test & operator=( const IfaceMgrTest_openSockets4SkipOpen_Test & ) = delete; IfaceMgrTest_openSockets4SkipOpen_Test (IfaceMgrTest_openSockets4SkipOpen_Test &&) noexcept = delete; IfaceMgrTest_openSockets4SkipOpen_Test & operator=( IfaceMgrTest_openSockets4SkipOpen_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets4SkipOpen_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4SkipOpen", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2039), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2039), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2039), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4SkipOpen_Test >); void IfaceMgrTest_openSockets4SkipOpen_Test::TestBody( ) { | |||
2040 | NakedIfaceMgr ifacemgr; | |||
2041 | ||||
2042 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2043 | ifacemgr.createIfaces(); | |||
2044 | ||||
2045 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
2046 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 2046, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
2047 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2047; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2047; } } else gtest_label_testnothrow_2047 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2047, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2048 | ||||
2049 | // Open socket on eth1. The openSockets4 should detect that this | |||
2050 | // socket has been already open and an attempt to open another socket | |||
2051 | // and bind to this address and port should fail. | |||
2052 | ASSERT_NO_THROW(ifacemgr.openSocket("eth1", IOAddress("192.0.2.3"),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth1", IOAddress("192.0.2.3"), DHCP4_SERVER_PORT ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2053; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2053 ; } } else gtest_label_testnothrow_2053 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2053, ("Expected: " "ifacemgr.openSocket(\"eth1\", IOAddress(\"192.0.2.3\"), DHCP4_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2053 | DHCP4_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth1", IOAddress("192.0.2.3"), DHCP4_SERVER_PORT ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2053; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2053 ; } } else gtest_label_testnothrow_2053 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2053, ("Expected: " "ifacemgr.openSocket(\"eth1\", IOAddress(\"192.0.2.3\"), DHCP4_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2054 | ||||
2055 | // The function doesn't throw an exception when it tries to open a socket | |||
2056 | // and bind it to the address in use but the skip open flag is provided. | |||
2057 | EXPECT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0, true))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0, true); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2057; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2057; } } else gtest_label_testnothrow_2057 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2057, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2058 | ||||
2059 | // Check that the other port is bound. | |||
2060 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("10.0.0.1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("10.0.0.1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2060, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"10.0.0.1\"))", "false", "true") .c_str()) = ::testing::Message(); | |||
2061 | } | |||
2062 | ||||
2063 | // This test verifies that the function correctly checks that the v4 socket is | |||
2064 | // open and bound to a specific address. | |||
2065 | TEST_F(IfaceMgrTest, hasOpenSocketForAddress4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("hasOpenSocketForAddress4") > 1, "test_name must not be empty" ); class IfaceMgrTest_hasOpenSocketForAddress4_Test : public IfaceMgrTest { public: IfaceMgrTest_hasOpenSocketForAddress4_Test() = default ; ~IfaceMgrTest_hasOpenSocketForAddress4_Test() override = default ; IfaceMgrTest_hasOpenSocketForAddress4_Test (const IfaceMgrTest_hasOpenSocketForAddress4_Test &) = delete; IfaceMgrTest_hasOpenSocketForAddress4_Test & operator=( const IfaceMgrTest_hasOpenSocketForAddress4_Test & ) = delete; IfaceMgrTest_hasOpenSocketForAddress4_Test (IfaceMgrTest_hasOpenSocketForAddress4_Test &&) noexcept = delete; IfaceMgrTest_hasOpenSocketForAddress4_Test & operator=( IfaceMgrTest_hasOpenSocketForAddress4_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_hasOpenSocketForAddress4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "hasOpenSocketForAddress4", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2065), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2065), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2065), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_hasOpenSocketForAddress4_Test >); void IfaceMgrTest_hasOpenSocketForAddress4_Test::TestBody () { | |||
2066 | NakedIfaceMgr ifacemgr; | |||
2067 | ||||
2068 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2069 | ifacemgr.createIfaces(); | |||
2070 | ||||
2071 | // Use the custom packet filter object. This object mimics the socket | |||
2072 | // opening operation - the real socket is not open. | |||
2073 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
2074 | ASSERT_TRUE(custom_packet_filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(custom_packet_filter) ) ; else return ::testing::internal::AssertHelper(::testing:: TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 2074, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
2075 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(custom_packet_filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(custom_packet_filter); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2075; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2075; } } else gtest_label_testnothrow_2075 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2075, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2076 | ||||
2077 | // Simulate opening sockets using the dummy packet filter. | |||
2078 | ASSERT_NO_THROW(ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2078; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2078; } } else gtest_label_testnothrow_2078 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2078, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2079 | ||||
2080 | // Expect that the sockets are open on both eth0 and eth1. | |||
2081 | ASSERT_EQ(1, ifacemgr.getIface("eth0")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"eth0\")->getSockets().size()" , 1, ifacemgr.getIface("eth0")->getSockets().size()))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2081, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2082 | ASSERT_EQ(1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(ETH0_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(ETH0_INDEX)->getSockets().size()))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2082, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2083 | ASSERT_EQ(1, ifacemgr.getIface("eth1")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"eth1\")->getSockets().size()" , 1, ifacemgr.getIface("eth1")->getSockets().size()))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2083, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2084 | ASSERT_EQ(1, ifacemgr.getIface(ETH1_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(ETH1_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(ETH1_INDEX)->getSockets().size()))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2084, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2085 | // Socket shouldn't have been opened on loopback. | |||
2086 | ASSERT_TRUE(ifacemgr.getIface("lo")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("lo" )->getSockets().empty())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2086, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"lo\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
2087 | ASSERT_TRUE(ifacemgr.getIface(LO_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface(LO_INDEX )->getSockets().empty())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2087, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(LO_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
2088 | ||||
2089 | // Check that there are sockets bound to addresses that we have | |||
2090 | // set for interfaces. | |||
2091 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("192.0.2.3")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("192.0.2.3")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2091, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"192.0.2.3\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
2092 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("10.0.0.1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("10.0.0.1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2092, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"10.0.0.1\"))", "false", "true") .c_str()) = ::testing::Message(); | |||
2093 | // Check that there is no socket for the address which is not | |||
2094 | // configured on any interface. | |||
2095 | EXPECT_FALSE(ifacemgr.hasOpenSocket(IOAddress("10.1.1.1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.hasOpenSocket (IOAddress("10.1.1.1"))))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2095, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"10.1.1.1\"))", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2096 | ||||
2097 | // Check that v4 sockets are open, but no v6 socket is open. | |||
2098 | EXPECT_TRUE(ifacemgr.hasOpenSocket(AF_INET))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (2))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2098, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.hasOpenSocket(2)" , "false", "true") .c_str()) = ::testing::Message(); | |||
2099 | EXPECT_FALSE(ifacemgr.hasOpenSocket(AF_INET6))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.hasOpenSocket (10)))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2099, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.hasOpenSocket(10)" , "true", "false") .c_str()) = ::testing::Message(); | |||
2100 | } | |||
2101 | ||||
2102 | // This test checks that the sockets are open and bound to link local addresses | |||
2103 | // only, if unicast addresses are not specified. | |||
2104 | TEST_F(IfaceMgrTest, openSockets6LinkLocal)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6LinkLocal") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6LinkLocal_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6LinkLocal_Test() = default ; ~IfaceMgrTest_openSockets6LinkLocal_Test() override = default ; IfaceMgrTest_openSockets6LinkLocal_Test (const IfaceMgrTest_openSockets6LinkLocal_Test &) = delete; IfaceMgrTest_openSockets6LinkLocal_Test & operator=( const IfaceMgrTest_openSockets6LinkLocal_Test & ) = delete; IfaceMgrTest_openSockets6LinkLocal_Test (IfaceMgrTest_openSockets6LinkLocal_Test &&) noexcept = delete; IfaceMgrTest_openSockets6LinkLocal_Test & operator=( IfaceMgrTest_openSockets6LinkLocal_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6LinkLocal_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6LinkLocal", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2104), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2104), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2104), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6LinkLocal_Test >); void IfaceMgrTest_openSockets6LinkLocal_Test::TestBody () { | |||
2105 | NakedIfaceMgr ifacemgr; | |||
2106 | ||||
2107 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2108 | ifacemgr.createIfaces(); | |||
2109 | ||||
2110 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2111 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2111, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2112 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2112 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2112 ; } } else gtest_label_testnothrow_2112 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2112, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2113 | ||||
2114 | // Simulate opening sockets using the dummy packet filter. | |||
2115 | bool success = false; | |||
2116 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2116; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2116; } } else gtest_label_testnothrow_2116 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2116, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2117 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2117, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2118 | ||||
2119 | // Check that the number of sockets is correct on each interface. | |||
2120 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
2121 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
2122 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 0); | |||
2123 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 0); | |||
2124 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
2125 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
2126 | ||||
2127 | // Sockets on eth0 should be bound to link-local and should not be bound | |||
2128 | // to global unicast address, even though this address is configured on | |||
2129 | // the eth0. | |||
2130 | EXPECT_TRUE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2130, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2131 | EXPECT_FALSE(ifacemgr.isBound("eth0", "2001:db8:1::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "2001:db8:1::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2131, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2132 | // Socket on eth1 should be bound to link local only. | |||
2133 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2133, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2134 | ||||
2135 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
2136 | #if defined OS_LINUX1 | |||
2137 | EXPECT_TRUE(ifacemgr.isBound("eth0", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2137 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2138 | EXPECT_TRUE(ifacemgr.isBound("eth1", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2138 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2139 | #endif | |||
2140 | } | |||
2141 | ||||
2142 | // This test checks that the sockets are open on the loopback interface | |||
2143 | // when the loopback is active and allowed. | |||
2144 | TEST_F(IfaceMgrTest, openSockets6Loopback)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6Loopback") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6Loopback_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6Loopback_Test() = default ; ~IfaceMgrTest_openSockets6Loopback_Test() override = default ; IfaceMgrTest_openSockets6Loopback_Test (const IfaceMgrTest_openSockets6Loopback_Test &) = delete; IfaceMgrTest_openSockets6Loopback_Test & operator=( const IfaceMgrTest_openSockets6Loopback_Test & ) = delete; IfaceMgrTest_openSockets6Loopback_Test (IfaceMgrTest_openSockets6Loopback_Test &&) noexcept = delete; IfaceMgrTest_openSockets6Loopback_Test & operator=( IfaceMgrTest_openSockets6Loopback_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6Loopback_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6Loopback", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2144), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2144), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2144), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6Loopback_Test >); void IfaceMgrTest_openSockets6Loopback_Test::TestBody( ) { | |||
2145 | NakedIfaceMgr ifacemgr; | |||
2146 | ||||
2147 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2148 | ifacemgr.createIfaces(); | |||
2149 | ||||
2150 | // Allow the loopback interface. | |||
2151 | ifacemgr.setAllowLoopBack(true); | |||
2152 | ||||
2153 | // Make the loopback interface active. | |||
2154 | ifacemgr.getIface("lo")->inactive6_ = false; | |||
2155 | ||||
2156 | // The loopback interface has no link-local (as for Linux but not BSD) | |||
2157 | // so add one. | |||
2158 | ifacemgr.getIface("lo")->addUnicast(IOAddress("::1")); | |||
2159 | ||||
2160 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2161 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2161, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2162 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2162 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2162 ; } } else gtest_label_testnothrow_2162 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2162, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2163 | ||||
2164 | // Simulate opening sockets using the dummy packet filter. | |||
2165 | bool success = false; | |||
2166 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2166; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2166; } } else gtest_label_testnothrow_2166 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2166, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2167 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2167, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2168 | ||||
2169 | // Check that the loopback interface has at least an open socket. | |||
2170 | EXPECT_EQ(1, ifacemgr.getIface("lo")->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(\"lo\")->getSockets().size()" , 1, ifacemgr.getIface("lo")->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2170, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2171 | EXPECT_EQ(1, ifacemgr.getIface(LO_INDEX)->getSockets().size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "ifacemgr.getIface(LO_INDEX)->getSockets().size()" , 1, ifacemgr.getIface(LO_INDEX)->getSockets().size()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2171, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2172 | ||||
2173 | // This socket should be bound to ::1 | |||
2174 | EXPECT_TRUE(ifacemgr.isBound("lo", "::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("lo" , "::1"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2174 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"lo\", \"::1\")", "false", "true") .c_str ()) = ::testing::Message(); | |||
2175 | } | |||
2176 | ||||
2177 | // This test checks that socket is not open on the interface which doesn't | |||
2178 | // have a link-local address. | |||
2179 | TEST_F(IfaceMgrTest, openSockets6NoLinkLocal)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6NoLinkLocal") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6NoLinkLocal_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6NoLinkLocal_Test() = default ; ~IfaceMgrTest_openSockets6NoLinkLocal_Test() override = default ; IfaceMgrTest_openSockets6NoLinkLocal_Test (const IfaceMgrTest_openSockets6NoLinkLocal_Test &) = delete; IfaceMgrTest_openSockets6NoLinkLocal_Test & operator=( const IfaceMgrTest_openSockets6NoLinkLocal_Test & ) = delete; IfaceMgrTest_openSockets6NoLinkLocal_Test (IfaceMgrTest_openSockets6NoLinkLocal_Test &&) noexcept = delete; IfaceMgrTest_openSockets6NoLinkLocal_Test & operator=( IfaceMgrTest_openSockets6NoLinkLocal_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6NoLinkLocal_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6NoLinkLocal", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2179), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2179), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2179), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6NoLinkLocal_Test >); void IfaceMgrTest_openSockets6NoLinkLocal_Test::TestBody () { | |||
2180 | NakedIfaceMgr ifacemgr; | |||
2181 | ||||
2182 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2183 | ifacemgr.createIfaces(); | |||
2184 | ||||
2185 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2186 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2186, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2187 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2187 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2187 ; } } else gtest_label_testnothrow_2187 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2187, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2188 | ||||
2189 | // Remove a link local address from eth0. If there is no link-local | |||
2190 | // address, the socket should not open. | |||
2191 | ASSERT_TRUE(ifacemgr.getIface("eth0")->switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth0" )-> delAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2192, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message() | |||
2192 | delAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth0" )-> delAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2192, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
2193 | ||||
2194 | // Simulate opening sockets using the dummy packet filter. | |||
2195 | bool success = false; | |||
2196 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2196; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2196; } } else gtest_label_testnothrow_2196 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2196, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2197 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2197, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2198 | ||||
2199 | // Check that the number of sockets is correct on each interface. | |||
2200 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
2201 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
2202 | // The third parameter specifies that the number of link-local | |||
2203 | // addresses for eth0 is equal to 0. | |||
2204 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 0, 0); | |||
2205 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 0, 0); | |||
2206 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0, 1); | |||
2207 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0, 1); | |||
2208 | ||||
2209 | // There should be no sockets open on eth0 because it neither has | |||
2210 | // link-local nor global unicast addresses. | |||
2211 | EXPECT_FALSE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef")))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2211, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
2212 | EXPECT_FALSE(ifacemgr.isBound("eth0", "2001:db8:1::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "2001:db8:1::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2212, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2213 | // Socket on eth1 should be bound to link local only. | |||
2214 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2214, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2215 | ||||
2216 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
2217 | #if defined OS_LINUX1 | |||
2218 | EXPECT_FALSE(ifacemgr.isBound("eth0", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "ff02::1:2")))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2218, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2219 | #endif | |||
2220 | } | |||
2221 | ||||
2222 | // This test checks that socket is open on the non-multicast-capable | |||
2223 | // interface. This socket simply doesn't join multicast group. | |||
2224 | TEST_F(IfaceMgrTest, openSockets6NotMulticast)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6NotMulticast") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6NotMulticast_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6NotMulticast_Test() = default ; ~IfaceMgrTest_openSockets6NotMulticast_Test() override = default ; IfaceMgrTest_openSockets6NotMulticast_Test (const IfaceMgrTest_openSockets6NotMulticast_Test &) = delete; IfaceMgrTest_openSockets6NotMulticast_Test & operator=( const IfaceMgrTest_openSockets6NotMulticast_Test & ) = delete; IfaceMgrTest_openSockets6NotMulticast_Test (IfaceMgrTest_openSockets6NotMulticast_Test &&) noexcept = delete; IfaceMgrTest_openSockets6NotMulticast_Test & operator=( IfaceMgrTest_openSockets6NotMulticast_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6NotMulticast_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6NotMulticast", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2224), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2224), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2224), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6NotMulticast_Test >); void IfaceMgrTest_openSockets6NotMulticast_Test::TestBody () { | |||
2225 | NakedIfaceMgr ifacemgr; | |||
2226 | ||||
2227 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2228 | ifacemgr.createIfaces(); | |||
2229 | ||||
2230 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2231 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2231, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2232 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2232 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2232 ; } } else gtest_label_testnothrow_2232 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2232, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2233 | ||||
2234 | // Make eth0 multicast-incapable. | |||
2235 | ifacemgr.getIface("eth0")->flag_multicast_ = false; | |||
2236 | ||||
2237 | // Simulate opening sockets using the dummy packet filter. | |||
2238 | bool success = false; | |||
2239 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2239; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2239; } } else gtest_label_testnothrow_2239 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2239, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2240 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2240, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2241 | ||||
2242 | // Check that the number of sockets is correct on each interface. | |||
2243 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
2244 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
2245 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 0); | |||
2246 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 0); | |||
2247 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
2248 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
2249 | ||||
2250 | // Sockets on eth0 should be bound to link-local and should not be bound | |||
2251 | // to global unicast address, even though this address is configured on | |||
2252 | // the eth0. | |||
2253 | EXPECT_TRUE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2253, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2254 | EXPECT_FALSE(ifacemgr.isBound("eth0", "2001:db8:1::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "2001:db8:1::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2254, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2255 | // The eth0 is not a multicast-capable interface, so the socket should | |||
2256 | // not be bound to the multicast address. | |||
2257 | EXPECT_FALSE(ifacemgr.isBound("eth0", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "ff02::1:2")))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2257, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2258 | // Socket on eth1 should be bound to link local only. | |||
2259 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2259, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2260 | ||||
2261 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
2262 | // on eth1. | |||
2263 | #if defined OS_LINUX1 | |||
2264 | EXPECT_TRUE(ifacemgr.isBound("eth1", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2264 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2265 | #endif | |||
2266 | } | |||
2267 | ||||
2268 | // This test checks that the sockets are opened and bound to link local | |||
2269 | // and unicast addresses which have been explicitly specified. | |||
2270 | TEST_F(IfaceMgrTest, openSockets6Unicast)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6Unicast") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6Unicast_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6Unicast_Test() = default; ~IfaceMgrTest_openSockets6Unicast_Test() override = default; IfaceMgrTest_openSockets6Unicast_Test (const IfaceMgrTest_openSockets6Unicast_Test &) = delete; IfaceMgrTest_openSockets6Unicast_Test & operator=( const IfaceMgrTest_openSockets6Unicast_Test & ) = delete; IfaceMgrTest_openSockets6Unicast_Test (IfaceMgrTest_openSockets6Unicast_Test &&) noexcept = delete; IfaceMgrTest_openSockets6Unicast_Test & operator=( IfaceMgrTest_openSockets6Unicast_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6Unicast_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6Unicast", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2270), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2270), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2270), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6Unicast_Test >); void IfaceMgrTest_openSockets6Unicast_Test::TestBody() { | |||
2271 | NakedIfaceMgr ifacemgr; | |||
2272 | ||||
2273 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2274 | ifacemgr.createIfaces(); | |||
2275 | ||||
2276 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2277 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2277, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2278 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2278 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2278 ; } } else gtest_label_testnothrow_2278 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2278, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2279 | ||||
2280 | // Configure the eth0 to open socket on the unicast address, apart | |||
2281 | // from link-local address. | |||
2282 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
2283 | ||||
2284 | // Simulate opening sockets using the dummy packet filter. | |||
2285 | bool success = false; | |||
2286 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2286; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2286; } } else gtest_label_testnothrow_2286 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2286, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2287 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2287, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2288 | ||||
2289 | // Check that we have correct number of sockets on each interface. | |||
2290 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
2291 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
2292 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 1); // one unicast address. | |||
2293 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 1); | |||
2294 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
2295 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
2296 | ||||
2297 | // eth0 should have two sockets, one bound to link-local, another one | |||
2298 | // bound to unicast address. | |||
2299 | EXPECT_TRUE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2299, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2300 | EXPECT_TRUE(ifacemgr.isBound("eth0", "2001:db8:1::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "2001:db8:1::1"))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2300, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2301 | // eth1 should have one socket, bound to link-local address. | |||
2302 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2302, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2303 | ||||
2304 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
2305 | #if defined OS_LINUX1 | |||
2306 | EXPECT_TRUE(ifacemgr.isBound("eth0", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2306 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2307 | EXPECT_TRUE(ifacemgr.isBound("eth1", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2307 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2308 | #endif | |||
2309 | } | |||
2310 | ||||
2311 | // This test checks that the socket is open and bound to a global unicast | |||
2312 | // address if the link-local address does not exist for the particular | |||
2313 | // interface. | |||
2314 | TEST_F(IfaceMgrTest, openSockets6UnicastOnly)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6UnicastOnly") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6UnicastOnly_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6UnicastOnly_Test() = default ; ~IfaceMgrTest_openSockets6UnicastOnly_Test() override = default ; IfaceMgrTest_openSockets6UnicastOnly_Test (const IfaceMgrTest_openSockets6UnicastOnly_Test &) = delete; IfaceMgrTest_openSockets6UnicastOnly_Test & operator=( const IfaceMgrTest_openSockets6UnicastOnly_Test & ) = delete; IfaceMgrTest_openSockets6UnicastOnly_Test (IfaceMgrTest_openSockets6UnicastOnly_Test &&) noexcept = delete; IfaceMgrTest_openSockets6UnicastOnly_Test & operator=( IfaceMgrTest_openSockets6UnicastOnly_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6UnicastOnly_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6UnicastOnly", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2314), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2314), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2314), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6UnicastOnly_Test >); void IfaceMgrTest_openSockets6UnicastOnly_Test::TestBody () { | |||
2315 | NakedIfaceMgr ifacemgr; | |||
2316 | ||||
2317 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2318 | ifacemgr.createIfaces(); | |||
2319 | ||||
2320 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2321 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2321, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2322 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2322 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2322 ; } } else gtest_label_testnothrow_2322 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2322, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2323 | ||||
2324 | // Configure the eth0 to open socket on the unicast address, apart | |||
2325 | // from link-local address. | |||
2326 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
2327 | // Explicitly remove the link-local address from eth0. | |||
2328 | ASSERT_TRUE(ifacemgr.getIface("eth0")->switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth0" )-> delAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2329, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message() | |||
2329 | delAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth0" )-> delAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2329, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
2330 | ||||
2331 | // Simulate opening sockets using the dummy packet filter. | |||
2332 | bool success = false; | |||
2333 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2333; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2333; } } else gtest_label_testnothrow_2333 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2333, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2334 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2334, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2335 | ||||
2336 | // Check that we have correct number of sockets on each interface. | |||
2337 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
2338 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
2339 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 1, 0); | |||
2340 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 1, 0); | |||
2341 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
2342 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
2343 | ||||
2344 | // The link-local address is not present on eth0. Therefore, no socket | |||
2345 | // must be bound to this address, nor to multicast address. | |||
2346 | EXPECT_FALSE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef")))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2346, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
2347 | EXPECT_FALSE(ifacemgr.isBound("eth0", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "ff02::1:2")))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2347, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2348 | // There should be one socket bound to unicast address. | |||
2349 | EXPECT_TRUE(ifacemgr.isBound("eth0", "2001:db8:1::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "2001:db8:1::1"))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2349, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2350 | // eth1 should have one socket, bound to link-local address. | |||
2351 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2351, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2352 | ||||
2353 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
2354 | #if defined OS_LINUX1 | |||
2355 | EXPECT_TRUE(ifacemgr.isBound("eth1", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2355 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2356 | #endif | |||
2357 | } | |||
2358 | ||||
2359 | // This test checks that no sockets are open for the interface which is down. | |||
2360 | TEST_F(IfaceMgrTest, openSockets6IfaceDown)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6IfaceDown") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6IfaceDown_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6IfaceDown_Test() = default ; ~IfaceMgrTest_openSockets6IfaceDown_Test() override = default ; IfaceMgrTest_openSockets6IfaceDown_Test (const IfaceMgrTest_openSockets6IfaceDown_Test &) = delete; IfaceMgrTest_openSockets6IfaceDown_Test & operator=( const IfaceMgrTest_openSockets6IfaceDown_Test & ) = delete; IfaceMgrTest_openSockets6IfaceDown_Test (IfaceMgrTest_openSockets6IfaceDown_Test &&) noexcept = delete; IfaceMgrTest_openSockets6IfaceDown_Test & operator=( IfaceMgrTest_openSockets6IfaceDown_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6IfaceDown_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6IfaceDown", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2360), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2360), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2360), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6IfaceDown_Test >); void IfaceMgrTest_openSockets6IfaceDown_Test::TestBody () { | |||
2361 | NakedIfaceMgr ifacemgr; | |||
2362 | ||||
2363 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2364 | ifacemgr.createIfaces(); | |||
2365 | ||||
2366 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2367 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2367, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2368 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2368 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2368 ; } } else gtest_label_testnothrow_2368 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2368, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2369 | ||||
2370 | // Configure the eth0 to open socket on the unicast address, apart | |||
2371 | // from link-local address. | |||
2372 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
2373 | ||||
2374 | // Boolean parameters specify that eth0 is: | |||
2375 | // - not a loopback | |||
2376 | // - is "down" (not up) | |||
2377 | // - is not running | |||
2378 | // - is active for both v4 and v6 | |||
2379 | ifacemgr.setIfaceFlags("eth0", false, false, false, false, false); | |||
2380 | ||||
2381 | // Install an error handler before trying to open sockets. This handler | |||
2382 | // should be called when the IfaceMgr fails to open socket on eth0. | |||
2383 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
2384 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
2385 | ||||
2386 | // Simulate opening sockets using the dummy packet filter. | |||
2387 | bool success = false; | |||
2388 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT,switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2389; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2389 ; } } else gtest_label_testnothrow_2389 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2389, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2389 | error_handler))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2389; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2389 ; } } else gtest_label_testnothrow_2389 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2389, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2390 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2390, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2391 | ||||
2392 | // Opening socket on the interface which is not configured, should | |||
2393 | // result in error. | |||
2394 | EXPECT_EQ(1, errors_count_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "errors_count_" , 1, errors_count_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2394, gtest_ar.failure_message()) = ::testing::Message(); | |||
2395 | ||||
2396 | // Check that we have correct number of sockets on each interface. | |||
2397 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
2398 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
2399 | // There should be no sockets on eth0 because interface is down. | |||
2400 | ASSERT_TRUE(ifacemgr.getIface("eth0")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth0" )->getSockets().empty())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2400, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
2401 | ASSERT_TRUE(ifacemgr.getIface(ETH0_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface(ETH0_INDEX )->getSockets().empty())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2401, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH0_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
2402 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
2403 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
2404 | ||||
2405 | // eth0 should have no sockets because the interface is down. | |||
2406 | EXPECT_FALSE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef")))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2406, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
2407 | EXPECT_FALSE(ifacemgr.isBound("eth0", "2001:db8:1::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "2001:db8:1::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2407, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2408 | EXPECT_FALSE(ifacemgr.isBound("eth0", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth0" , "ff02::1:2")))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2408, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2409 | // eth1 should have one socket, bound to link-local address. | |||
2410 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2410, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2411 | ||||
2412 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
2413 | #if defined OS_LINUX1 | |||
2414 | EXPECT_TRUE(ifacemgr.isBound("eth1", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2414 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2415 | #endif | |||
2416 | } | |||
2417 | ||||
2418 | // This test checks that no sockets are open for the interface which is | |||
2419 | // inactive. | |||
2420 | TEST_F(IfaceMgrTest, openSockets6IfaceInactive)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6IfaceInactive") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6IfaceInactive_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6IfaceInactive_Test () = default; ~IfaceMgrTest_openSockets6IfaceInactive_Test() override = default; IfaceMgrTest_openSockets6IfaceInactive_Test (const IfaceMgrTest_openSockets6IfaceInactive_Test &) = delete; IfaceMgrTest_openSockets6IfaceInactive_Test & operator=( const IfaceMgrTest_openSockets6IfaceInactive_Test &) = delete ; IfaceMgrTest_openSockets6IfaceInactive_Test (IfaceMgrTest_openSockets6IfaceInactive_Test &&) noexcept = delete; IfaceMgrTest_openSockets6IfaceInactive_Test & operator=( IfaceMgrTest_openSockets6IfaceInactive_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6IfaceInactive_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6IfaceInactive", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2420), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2420), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2420), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6IfaceInactive_Test >); void IfaceMgrTest_openSockets6IfaceInactive_Test::TestBody () { | |||
2421 | NakedIfaceMgr ifacemgr; | |||
2422 | ||||
2423 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2424 | ifacemgr.createIfaces(); | |||
2425 | ||||
2426 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2427 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2427, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2428 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2428 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2428 ; } } else gtest_label_testnothrow_2428 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2428, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2429 | ||||
2430 | // Configure the eth0 to open socket on the unicast address, apart | |||
2431 | // from link-local address. | |||
2432 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
2433 | ||||
2434 | // Boolean parameters specify that eth1 is: | |||
2435 | // - not a loopback | |||
2436 | // - is up | |||
2437 | // - is running | |||
2438 | // - is active for v4 | |||
2439 | // - is inactive for v6 | |||
2440 | ifacemgr.setIfaceFlags("eth1", false, true, true, false, true); | |||
2441 | ||||
2442 | // Simulate opening sockets using the dummy packet filter. | |||
2443 | bool success = false; | |||
2444 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2444; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2444; } } else gtest_label_testnothrow_2444 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2444, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2445 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2445, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2446 | ||||
2447 | // Check that we have correct number of sockets on each interface. | |||
2448 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
2449 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
2450 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 1); // one unicast address | |||
2451 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 1); | |||
2452 | // There should be no sockets on eth1 because interface is inactive | |||
2453 | ASSERT_TRUE(ifacemgr.getIface("eth1")->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface("eth1" )->getSockets().empty())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2453, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth1\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
2454 | ASSERT_TRUE(ifacemgr.getIface(ETH1_INDEX)->getSockets().empty())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.getIface(ETH1_INDEX )->getSockets().empty())) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2454, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH1_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
2455 | ||||
2456 | // eth0 should have one socket bound to a link-local address, another one | |||
2457 | // bound to unicast address. | |||
2458 | EXPECT_TRUE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2458, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2459 | EXPECT_TRUE(ifacemgr.isBound("eth0", "2001:db8:1::1"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "2001:db8:1::1"))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2459, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2460 | ||||
2461 | // eth1 shouldn't have a socket bound to link local address because | |||
2462 | // interface is inactive. | |||
2463 | EXPECT_FALSE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd")))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2463, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
2464 | EXPECT_FALSE(ifacemgr.isBound("eth1", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.isBound("eth1" , "ff02::1:2")))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2464, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2465 | ||||
2466 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
2467 | #if defined OS_LINUX1 | |||
2468 | EXPECT_TRUE(ifacemgr.isBound("eth0", ALL_DHCP_RELAY_AGENTS_AND_SERVERS))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "ff02::1:2"))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 2468 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2469 | #endif | |||
2470 | } | |||
2471 | ||||
2472 | // Test that the openSockets6 function does not throw if there are no interfaces | |||
2473 | // detected. This function is expected to return false. | |||
2474 | TEST_F(IfaceMgrTest, openSockets6NoIfaces)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6NoIfaces") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6NoIfaces_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6NoIfaces_Test() = default ; ~IfaceMgrTest_openSockets6NoIfaces_Test() override = default ; IfaceMgrTest_openSockets6NoIfaces_Test (const IfaceMgrTest_openSockets6NoIfaces_Test &) = delete; IfaceMgrTest_openSockets6NoIfaces_Test & operator=( const IfaceMgrTest_openSockets6NoIfaces_Test & ) = delete; IfaceMgrTest_openSockets6NoIfaces_Test (IfaceMgrTest_openSockets6NoIfaces_Test &&) noexcept = delete; IfaceMgrTest_openSockets6NoIfaces_Test & operator=( IfaceMgrTest_openSockets6NoIfaces_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6NoIfaces_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6NoIfaces", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2474), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2474), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2474), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6NoIfaces_Test >); void IfaceMgrTest_openSockets6NoIfaces_Test::TestBody( ) { | |||
2475 | NakedIfaceMgr ifacemgr; | |||
2476 | // Remove existing interfaces. | |||
2477 | ifacemgr.getIfacesLst().clear(); | |||
2478 | ||||
2479 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2480 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2480, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2481 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2481 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2481 ; } } else gtest_label_testnothrow_2481 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2481, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2482 | ||||
2483 | // This value indicates if at least one socket opens. There are no | |||
2484 | // interfaces, so it should be set to false. | |||
2485 | bool socket_open = false; | |||
2486 | ASSERT_NO_THROW(socket_open = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { socket_open = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2486; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2486; } } else gtest_label_testnothrow_2486 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2486, ("Expected: " "socket_open = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2487 | EXPECT_FALSE(socket_open)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(socket_open))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2487, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "socket_open", "true" , "false") .c_str()) = ::testing::Message(); | |||
2488 | } | |||
2489 | ||||
2490 | // Test that the external error handler is called when trying to bind a new | |||
2491 | // socket to the address and port being in use. The sockets on the other | |||
2492 | // interfaces should open just fine. | |||
2493 | TEST_F(IfaceMgrTest, openSockets6ErrorHandler)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6ErrorHandler") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6ErrorHandler_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6ErrorHandler_Test() = default ; ~IfaceMgrTest_openSockets6ErrorHandler_Test() override = default ; IfaceMgrTest_openSockets6ErrorHandler_Test (const IfaceMgrTest_openSockets6ErrorHandler_Test &) = delete; IfaceMgrTest_openSockets6ErrorHandler_Test & operator=( const IfaceMgrTest_openSockets6ErrorHandler_Test & ) = delete; IfaceMgrTest_openSockets6ErrorHandler_Test (IfaceMgrTest_openSockets6ErrorHandler_Test &&) noexcept = delete; IfaceMgrTest_openSockets6ErrorHandler_Test & operator=( IfaceMgrTest_openSockets6ErrorHandler_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6ErrorHandler_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6ErrorHandler", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2493), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2493), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2493), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6ErrorHandler_Test >); void IfaceMgrTest_openSockets6ErrorHandler_Test::TestBody () { | |||
2494 | NakedIfaceMgr ifacemgr; | |||
2495 | ||||
2496 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2497 | ifacemgr.createIfaces(); | |||
2498 | ||||
2499 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2500 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2500, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2501 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2501 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2501 ; } } else gtest_label_testnothrow_2501 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2501, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2502 | ||||
2503 | // Open multicast socket on eth0. | |||
2504 | ASSERT_NO_THROW(ifacemgr.openSocket("eth0",switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("fe80::3a60:77ff:fed5:cdef" ), DHCP6_SERVER_PORT, true); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2506 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2506 ; } } else gtest_label_testnothrow_2506 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2506, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"fe80::3a60:77ff:fed5:cdef\"), DHCP6_SERVER_PORT, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2505 | IOAddress("fe80::3a60:77ff:fed5:cdef"),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("fe80::3a60:77ff:fed5:cdef" ), DHCP6_SERVER_PORT, true); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2506 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2506 ; } } else gtest_label_testnothrow_2506 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2506, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"fe80::3a60:77ff:fed5:cdef\"), DHCP6_SERVER_PORT, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2506 | DHCP6_SERVER_PORT, true))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("fe80::3a60:77ff:fed5:cdef" ), DHCP6_SERVER_PORT, true); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2506 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2506 ; } } else gtest_label_testnothrow_2506 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2506, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"fe80::3a60:77ff:fed5:cdef\"), DHCP6_SERVER_PORT, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2507 | ||||
2508 | // Install an error handler before trying to open sockets. This handler | |||
2509 | // should be called when the IfaceMgr fails to open socket on eth0. | |||
2510 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
2511 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
2512 | // The openSockets6 should detect that a socket has been already | |||
2513 | // opened on eth0 and an attempt to open another socket and bind to | |||
2514 | // the same address and port should fail. | |||
2515 | ASSERT_NO_THROW(ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2515; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2515; } } else gtest_label_testnothrow_2515 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2515, ("Expected: " "ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2516 | // We expect that an error occurred when we tried to open a socket on | |||
2517 | // eth0, but the socket on eth1 should open just fine. | |||
2518 | EXPECT_EQ(1, errors_count_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "errors_count_" , 1, errors_count_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2518, gtest_ar.failure_message()) = ::testing::Message(); | |||
2519 | ||||
2520 | // Reset errors count. | |||
2521 | errors_count_ = 0; | |||
2522 | ||||
2523 | // Now that we have two sockets open, we can try this again but this time | |||
2524 | // we should get two errors: one when opening a socket on eth0, another one | |||
2525 | // when opening a socket on eth1. | |||
2526 | ASSERT_NO_THROW(ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2526; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2526; } } else gtest_label_testnothrow_2526 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2526, ("Expected: " "ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2527 | EXPECT_EQ(2, errors_count_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("2", "errors_count_" , 2, errors_count_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2527, gtest_ar.failure_message()) = ::testing::Message(); | |||
2528 | } | |||
2529 | ||||
2530 | // Test that no exception is thrown when a port is already bound but skip open | |||
2531 | // flag is provided. | |||
2532 | TEST_F(IfaceMgrTest, openSockets6SkipOpen)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("openSockets6SkipOpen") > 1, "test_name must not be empty" ); class IfaceMgrTest_openSockets6SkipOpen_Test : public IfaceMgrTest { public: IfaceMgrTest_openSockets6SkipOpen_Test() = default ; ~IfaceMgrTest_openSockets6SkipOpen_Test() override = default ; IfaceMgrTest_openSockets6SkipOpen_Test (const IfaceMgrTest_openSockets6SkipOpen_Test &) = delete; IfaceMgrTest_openSockets6SkipOpen_Test & operator=( const IfaceMgrTest_openSockets6SkipOpen_Test & ) = delete; IfaceMgrTest_openSockets6SkipOpen_Test (IfaceMgrTest_openSockets6SkipOpen_Test &&) noexcept = delete; IfaceMgrTest_openSockets6SkipOpen_Test & operator=( IfaceMgrTest_openSockets6SkipOpen_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_openSockets6SkipOpen_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6SkipOpen", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2532), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2532), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2532), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6SkipOpen_Test >); void IfaceMgrTest_openSockets6SkipOpen_Test::TestBody( ) { | |||
2533 | NakedIfaceMgr ifacemgr; | |||
2534 | ||||
2535 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2536 | ifacemgr.createIfaces(); | |||
2537 | ||||
2538 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2539 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2539, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2540 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2540 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2540 ; } } else gtest_label_testnothrow_2540 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2540, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2541 | ||||
2542 | // Open socket on eth0. The openSockets6 should detect that this | |||
2543 | // socket has been already open and an attempt to open another socket | |||
2544 | // and bind to this address and port should fail. | |||
2545 | ASSERT_NO_THROW(ifacemgr.openSocket("eth0",switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("fe80::3a60:77ff:fed5:cdef" ), DHCP6_SERVER_PORT, true); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2547 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2547 ; } } else gtest_label_testnothrow_2547 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2547, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"fe80::3a60:77ff:fed5:cdef\"), DHCP6_SERVER_PORT, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2546 | IOAddress("fe80::3a60:77ff:fed5:cdef"),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("fe80::3a60:77ff:fed5:cdef" ), DHCP6_SERVER_PORT, true); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2547 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2547 ; } } else gtest_label_testnothrow_2547 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2547, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"fe80::3a60:77ff:fed5:cdef\"), DHCP6_SERVER_PORT, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2547 | DHCP6_SERVER_PORT, true))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSocket("eth0", IOAddress("fe80::3a60:77ff:fed5:cdef" ), DHCP6_SERVER_PORT, true); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2547 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2547 ; } } else gtest_label_testnothrow_2547 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2547, ("Expected: " "ifacemgr.openSocket(\"eth0\", IOAddress(\"fe80::3a60:77ff:fed5:cdef\"), DHCP6_SERVER_PORT, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2548 | ||||
2549 | // The function doesn't throw an exception when it tries to open a socket | |||
2550 | // and bind it to the address in use but the skip open flag is provided. | |||
2551 | EXPECT_NO_THROW(ifacemgr.openSockets6(DHCP6_SERVER_PORT, 0, true))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.openSockets6(DHCP6_SERVER_PORT, 0, true); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2551; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2551; } } else gtest_label_testnothrow_2551 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2551, ("Expected: " "ifacemgr.openSockets6(DHCP6_SERVER_PORT, 0, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2552 | ||||
2553 | // Check that the other port is bound. | |||
2554 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2554, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2555 | } | |||
2556 | ||||
2557 | // This test verifies that the function correctly checks that the v6 socket is | |||
2558 | // open and bound to a specific address. | |||
2559 | TEST_F(IfaceMgrTest, hasOpenSocketForAddress6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("hasOpenSocketForAddress6") > 1, "test_name must not be empty" ); class IfaceMgrTest_hasOpenSocketForAddress6_Test : public IfaceMgrTest { public: IfaceMgrTest_hasOpenSocketForAddress6_Test() = default ; ~IfaceMgrTest_hasOpenSocketForAddress6_Test() override = default ; IfaceMgrTest_hasOpenSocketForAddress6_Test (const IfaceMgrTest_hasOpenSocketForAddress6_Test &) = delete; IfaceMgrTest_hasOpenSocketForAddress6_Test & operator=( const IfaceMgrTest_hasOpenSocketForAddress6_Test & ) = delete; IfaceMgrTest_hasOpenSocketForAddress6_Test (IfaceMgrTest_hasOpenSocketForAddress6_Test &&) noexcept = delete; IfaceMgrTest_hasOpenSocketForAddress6_Test & operator=( IfaceMgrTest_hasOpenSocketForAddress6_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_hasOpenSocketForAddress6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "hasOpenSocketForAddress6", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2559), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2559), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2559), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_hasOpenSocketForAddress6_Test >); void IfaceMgrTest_hasOpenSocketForAddress6_Test::TestBody () { | |||
2560 | NakedIfaceMgr ifacemgr; | |||
2561 | ||||
2562 | // Remove all real interfaces and create a set of dummy interfaces. | |||
2563 | ifacemgr.createIfaces(); | |||
2564 | ||||
2565 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
2566 | ASSERT_TRUE(filter)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(filter)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2566, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "filter", "false" , "true") .c_str()) = ::testing::Message(); | |||
2567 | ASSERT_NO_THROW(ifacemgr.setPacketFilter(filter))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr.setPacketFilter(filter); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2567 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2567 ; } } else gtest_label_testnothrow_2567 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2567, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2568 | ||||
2569 | // Simulate opening sockets using the dummy packet filter. | |||
2570 | bool success = false; | |||
2571 | ASSERT_NO_THROW(success = ifacemgr.openSockets6(DHCP6_SERVER_PORT))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { success = ifacemgr.openSockets6(DHCP6_SERVER_PORT); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2571; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2571; } } else gtest_label_testnothrow_2571 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 2571, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2572 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2572, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "success", "false", "true") .c_str()) = ::testing ::Message(); | |||
2573 | ||||
2574 | // Make sure that the sockets are bound as expected. | |||
2575 | ASSERT_TRUE(ifacemgr.isBound("eth0", "fe80::3a60:77ff:fed5:cdef"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth0" , "fe80::3a60:77ff:fed5:cdef"))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2575, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2576 | EXPECT_TRUE(ifacemgr.isBound("eth1", "fe80::3a60:77ff:fed5:abcd"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.isBound("eth1" , "fe80::3a60:77ff:fed5:abcd"))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2576, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
2577 | ||||
2578 | // There should be v6 sockets only, no v4 sockets. | |||
2579 | EXPECT_TRUE(ifacemgr.hasOpenSocket(AF_INET6))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (10))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2579, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.hasOpenSocket(10)" , "false", "true") .c_str()) = ::testing::Message(); | |||
2580 | EXPECT_FALSE(ifacemgr.hasOpenSocket(AF_INET))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.hasOpenSocket (2)))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2580, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.hasOpenSocket(2)" , "true", "false") .c_str()) = ::testing::Message(); | |||
2581 | ||||
2582 | // Check that there are sockets bound to the addresses we have configured | |||
2583 | // for interfaces. | |||
2584 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("fe80::3a60:77ff:fed5:cdef")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("fe80::3a60:77ff:fed5:cdef")))) ; else ::testing:: internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2584, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "ifacemgr.hasOpenSocket(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
2585 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("fe80::3a60:77ff:fed5:abcd")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("fe80::3a60:77ff:fed5:abcd")))) ; else ::testing:: internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2585, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "ifacemgr.hasOpenSocket(IOAddress(\"fe80::3a60:77ff:fed5:abcd\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
2586 | // Check that there is no socket bound to the address which hasn't been | |||
2587 | // configured on any interface. | |||
2588 | EXPECT_FALSE(ifacemgr.hasOpenSocket(IOAddress("fe80::3a60:77ff:feed:1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.hasOpenSocket (IOAddress("fe80::3a60:77ff:feed:1"))))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2588, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"fe80::3a60:77ff:feed:1\"))" , "true", "false") .c_str()) = ::testing::Message(); | |||
2589 | } | |||
2590 | ||||
2591 | // Test the Iface structure itself | |||
2592 | TEST_F(IfaceMgrTest, iface)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("iface") > 1, "test_name must not be empty" ); class IfaceMgrTest_iface_Test : public IfaceMgrTest { public : IfaceMgrTest_iface_Test() = default; ~IfaceMgrTest_iface_Test () override = default; IfaceMgrTest_iface_Test (const IfaceMgrTest_iface_Test &) = delete; IfaceMgrTest_iface_Test & operator=( const IfaceMgrTest_iface_Test &) = delete; IfaceMgrTest_iface_Test (IfaceMgrTest_iface_Test &&) noexcept = delete; IfaceMgrTest_iface_Test & operator=( IfaceMgrTest_iface_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const IfaceMgrTest_iface_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "iface", nullptr, nullptr, ::testing::internal::CodeLocation("iface_mgr_unittest.cc" , 2592), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("iface_mgr_unittest.cc", 2592), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("iface_mgr_unittest.cc", 2592), new ::testing::internal::TestFactoryImpl <IfaceMgrTest_iface_Test>); void IfaceMgrTest_iface_Test ::TestBody() { | |||
2593 | boost::scoped_ptr<Iface> iface; | |||
2594 | EXPECT_NO_THROW(iface.reset(new Iface("eth0",1)))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface.reset(new Iface("eth0",1)); } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2594 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2594 ; } } else gtest_label_testnothrow_2594 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2594, ("Expected: " "iface.reset(new Iface(\"eth0\",1))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2595 | ||||
2596 | EXPECT_EQ("eth0", iface->getName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"eth0\"" , "iface->getName()", "eth0", iface->getName()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2596, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2597 | EXPECT_EQ(1, iface->getIndex())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "iface->getIndex()" , 1, iface->getIndex()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2597, gtest_ar.failure_message()) = ::testing::Message(); | |||
2598 | EXPECT_EQ("eth0/1", iface->getFullName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"eth0/1\"" , "iface->getFullName()", "eth0/1", iface->getFullName( )))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2598, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2599 | ||||
2600 | // Let's make a copy of this address collection. | |||
2601 | Iface::AddressCollection addrs = iface->getAddresses(); | |||
2602 | ||||
2603 | EXPECT_EQ(0, addrs.size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "addrs.size()" , 0, addrs.size()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2603, gtest_ar.failure_message()) = ::testing::Message(); | |||
2604 | ||||
2605 | IOAddress addr1("192.0.2.6"); | |||
2606 | iface->addAddress(addr1); | |||
2607 | ||||
2608 | addrs = iface->getAddresses(); | |||
2609 | ASSERT_EQ(1, addrs.size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "addrs.size()" , 1, addrs.size()))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2609, gtest_ar.failure_message()) = ::testing::Message(); | |||
2610 | EXPECT_EQ("192.0.2.6", addrs.begin()->get().toText())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"192.0.2.6\"" , "addrs.begin()->get().toText()", "192.0.2.6", addrs.begin ()->get().toText()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2610, gtest_ar.failure_message()) = ::testing::Message(); | |||
2611 | ||||
2612 | // No such address, should return false. | |||
2613 | EXPECT_FALSE(iface->delAddress(IOAddress("192.0.8.9")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface->delAddress (IOAddress("192.0.8.9"))))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2613, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->delAddress(IOAddress(\"192.0.8.9\"))", "true", "false" ) .c_str()) = ::testing::Message(); | |||
2614 | ||||
2615 | // This address is present, delete it! | |||
2616 | EXPECT_TRUE(iface->delAddress(IOAddress("192.0.2.6")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface->delAddress( IOAddress("192.0.2.6")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2616, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->delAddress(IOAddress(\"192.0.2.6\"))", "false", "true" ) .c_str()) = ::testing::Message(); | |||
2617 | ||||
2618 | // Not really necessary, previous reference still points to the same | |||
2619 | // collection. Let's do it anyway, as test code may serve as example | |||
2620 | // usage code as well. | |||
2621 | addrs = iface->getAddresses(); | |||
2622 | ||||
2623 | EXPECT_EQ(0, addrs.size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "addrs.size()" , 0, addrs.size()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2623, gtest_ar.failure_message()) = ::testing::Message(); | |||
2624 | ||||
2625 | EXPECT_NO_THROW(iface.reset())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface.reset(); } else static_assert(true, ""); } catch (std:: exception const& e) { gtest_msg.value = "it throws "; gtest_msg .value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg .value += " with description \""; gtest_msg.value += e.what() ; gtest_msg.value += "\"."; goto gtest_label_testnothrow_2625 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2625 ; } } else gtest_label_testnothrow_2625 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2625, ("Expected: " "iface.reset()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
2626 | } | |||
2627 | ||||
2628 | TEST_F(IfaceMgrTest, iface_methods)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("iface_methods") > 1, "test_name must not be empty" ); class IfaceMgrTest_iface_methods_Test : public IfaceMgrTest { public: IfaceMgrTest_iface_methods_Test() = default; ~IfaceMgrTest_iface_methods_Test () override = default; IfaceMgrTest_iface_methods_Test (const IfaceMgrTest_iface_methods_Test &) = delete; IfaceMgrTest_iface_methods_Test & operator=( const IfaceMgrTest_iface_methods_Test & ) = delete; IfaceMgrTest_iface_methods_Test (IfaceMgrTest_iface_methods_Test &&) noexcept = delete; IfaceMgrTest_iface_methods_Test & operator=( IfaceMgrTest_iface_methods_Test &&) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_iface_methods_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "iface_methods", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 2628), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2628), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2628), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_iface_methods_Test >); void IfaceMgrTest_iface_methods_Test::TestBody() { | |||
2629 | Iface iface("foo", 1234); | |||
2630 | ||||
2631 | iface.setHWType(42); | |||
2632 | EXPECT_EQ(42, iface.getHWType())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("42", "iface.getHWType()" , 42, iface.getHWType()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2632, gtest_ar.failure_message()) = ::testing::Message(); | |||
2633 | ||||
2634 | ASSERT_LT(Iface::MAX_MAC_LEN + 10, 255)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLT("Iface::MAX_MAC_LEN + 10" , "255", Iface::MAX_MAC_LEN + 10, 255))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "iface_mgr_unittest.cc", 2634, gtest_ar.failure_message()) = ::testing::Message(); | |||
2635 | ||||
2636 | uint8_t mac[Iface::MAX_MAC_LEN+10]; | |||
2637 | for (uint8_t i = 0; i < Iface::MAX_MAC_LEN + 10; i++) { | |||
2638 | mac[i] = 255 - i; | |||
2639 | } | |||
2640 | ||||
2641 | EXPECT_EQ("foo", iface.getName())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"foo\"" , "iface.getName()", "foo", iface.getName()))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2641, gtest_ar.failure_message()) = ::testing::Message(); | |||
2642 | EXPECT_EQ(1234, iface.getIndex())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1234", "iface.getIndex()" , 1234, iface.getIndex()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2642, gtest_ar.failure_message()) = ::testing::Message(); | |||
2643 | ||||
2644 | // MAC is too long. Exception should be thrown and | |||
2645 | // MAC length should not be set. | |||
2646 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface.setMac(mac, Iface:: MAX_MAC_LEN + 1); } else static_assert(true, ""); } catch (OutOfRange const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< OutOfRange>::type> ::type, std::exception>::value, const ::testing::internal:: NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_2649 ; } catch (...) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2649; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2649; } } else gtest_label_testthrow_2649 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2649, gtest_msg. value.c_str()) = ::testing::Message() | |||
2647 | iface.setMac(mac, Iface::MAX_MAC_LEN + 1),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface.setMac(mac, Iface:: MAX_MAC_LEN + 1); } else static_assert(true, ""); } catch (OutOfRange const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< OutOfRange>::type> ::type, std::exception>::value, const ::testing::internal:: NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_2649 ; } catch (...) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2649; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2649; } } else gtest_label_testthrow_2649 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2649, gtest_msg. value.c_str()) = ::testing::Message() | |||
2648 | OutOfRangeswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface.setMac(mac, Iface:: MAX_MAC_LEN + 1); } else static_assert(true, ""); } catch (OutOfRange const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< OutOfRange>::type> ::type, std::exception>::value, const ::testing::internal:: NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_2649 ; } catch (...) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2649; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2649; } } else gtest_label_testthrow_2649 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2649, gtest_msg. value.c_str()) = ::testing::Message() | |||
2649 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface.setMac(mac, Iface:: MAX_MAC_LEN + 1); } else static_assert(true, ""); } catch (OutOfRange const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< OutOfRange>::type> ::type, std::exception>::value, const ::testing::internal:: NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_2649 ; } catch (...) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2649; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface.setMac(mac, Iface::MAX_MAC_LEN + 1)" " throws an exception of type " "OutOfRange" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2649; } } else gtest_label_testthrow_2649 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2649, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2650 | ||||
2651 | // MAC length should stay not set as exception was thrown. | |||
2652 | EXPECT_EQ(0, iface.getMacLen())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "iface.getMacLen()" , 0, iface.getMacLen()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2652, gtest_ar.failure_message()) = ::testing::Message(); | |||
2653 | ||||
2654 | // Setting maximum length MAC should be ok. | |||
2655 | iface.setMac(mac, Iface::MAX_MAC_LEN); | |||
2656 | ||||
2657 | // For some reason constants cannot be used directly in EXPECT_EQ | |||
2658 | // as this produces linking error. | |||
2659 | size_t len = Iface::MAX_MAC_LEN; | |||
2660 | EXPECT_EQ(len, iface.getMacLen())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("len", "iface.getMacLen()" , len, iface.getMacLen()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2660, gtest_ar.failure_message()) = ::testing::Message(); | |||
2661 | EXPECT_EQ(0, memcmp(mac, iface.getMac(), iface.getMacLen()))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "memcmp(mac, iface.getMac(), iface.getMacLen())" , 0, memcmp(mac, iface.getMac(), iface.getMacLen())))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2661, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2662 | } | |||
2663 | ||||
2664 | TEST_F(IfaceMgrTest, socketInfo)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("socketInfo") > 1, "test_name must not be empty" ); class IfaceMgrTest_socketInfo_Test : public IfaceMgrTest { public: IfaceMgrTest_socketInfo_Test() = default; ~IfaceMgrTest_socketInfo_Test () override = default; IfaceMgrTest_socketInfo_Test (const IfaceMgrTest_socketInfo_Test &) = delete; IfaceMgrTest_socketInfo_Test & operator =( const IfaceMgrTest_socketInfo_Test &) = delete; IfaceMgrTest_socketInfo_Test (IfaceMgrTest_socketInfo_Test &&) noexcept = delete; IfaceMgrTest_socketInfo_Test & operator=( IfaceMgrTest_socketInfo_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_socketInfo_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketInfo", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 2664), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2664), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2664), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketInfo_Test >); void IfaceMgrTest_socketInfo_Test::TestBody() { | |||
2665 | ||||
2666 | // Check that socketinfo for IPv4 socket is functional | |||
2667 | SocketInfo sock1(IOAddress("192.0.2.56"), DHCP4_SERVER_PORT + 7, 7); | |||
2668 | EXPECT_EQ(7, sock1.sockfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("7", "sock1.sockfd_" , 7, sock1.sockfd_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2668, gtest_ar.failure_message()) = ::testing::Message(); | |||
2669 | EXPECT_EQ(-1, sock1.fallbackfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("-1", "sock1.fallbackfd_" , -1, sock1.fallbackfd_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2669, gtest_ar.failure_message()) = ::testing::Message(); | |||
2670 | EXPECT_EQ("192.0.2.56", sock1.addr_.toText())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"192.0.2.56\"" , "sock1.addr_.toText()", "192.0.2.56", sock1.addr_.toText()) )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2670, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2671 | EXPECT_EQ(AF_INET, sock1.family_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("2", "sock1.family_" , 2, sock1.family_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2671, gtest_ar.failure_message()) = ::testing::Message(); | |||
2672 | EXPECT_EQ(DHCP4_SERVER_PORT + 7, sock1.port_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("DHCP4_SERVER_PORT + 7" , "sock1.port_", DHCP4_SERVER_PORT + 7, sock1.port_))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2672, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2673 | ||||
2674 | // Check that non-default value of the fallback socket descriptor is set | |||
2675 | SocketInfo sock2(IOAddress("192.0.2.53"), DHCP4_SERVER_PORT + 8, 8, 10); | |||
2676 | EXPECT_EQ(8, sock2.sockfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("8", "sock2.sockfd_" , 8, sock2.sockfd_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2676, gtest_ar.failure_message()) = ::testing::Message(); | |||
2677 | EXPECT_EQ(10, sock2.fallbackfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("10", "sock2.fallbackfd_" , 10, sock2.fallbackfd_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2677, gtest_ar.failure_message()) = ::testing::Message(); | |||
2678 | EXPECT_EQ("192.0.2.53", sock2.addr_.toText())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"192.0.2.53\"" , "sock2.addr_.toText()", "192.0.2.53", sock2.addr_.toText()) )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2678, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2679 | EXPECT_EQ(AF_INET, sock2.family_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("2", "sock2.family_" , 2, sock2.family_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2679, gtest_ar.failure_message()) = ::testing::Message(); | |||
2680 | EXPECT_EQ(DHCP4_SERVER_PORT + 8, sock2.port_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("DHCP4_SERVER_PORT + 8" , "sock2.port_", DHCP4_SERVER_PORT + 8, sock2.port_))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2680, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2681 | ||||
2682 | // Check that socketinfo for IPv6 socket is functional | |||
2683 | SocketInfo sock3(IOAddress("2001:db8:1::56"), DHCP4_SERVER_PORT + 9, 9); | |||
2684 | EXPECT_EQ(9, sock3.sockfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("9", "sock3.sockfd_" , 9, sock3.sockfd_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2684, gtest_ar.failure_message()) = ::testing::Message(); | |||
2685 | EXPECT_EQ(-1, sock3.fallbackfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("-1", "sock3.fallbackfd_" , -1, sock3.fallbackfd_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2685, gtest_ar.failure_message()) = ::testing::Message(); | |||
2686 | EXPECT_EQ("2001:db8:1::56", sock3.addr_.toText())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("\"2001:db8:1::56\"" , "sock3.addr_.toText()", "2001:db8:1::56", sock3.addr_.toText ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2686, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2687 | EXPECT_EQ(AF_INET6, sock3.family_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("10", "sock3.family_" , 10, sock3.family_))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2687, gtest_ar.failure_message()) = ::testing::Message(); | |||
2688 | EXPECT_EQ(DHCP4_SERVER_PORT + 9, sock3.port_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("DHCP4_SERVER_PORT + 9" , "sock3.port_", DHCP4_SERVER_PORT + 9, sock3.port_))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2688, gtest_ar.failure_message ()) = ::testing::Message(); | |||
2689 | ||||
2690 | // Now let's test if IfaceMgr handles socket info properly | |||
2691 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
2692 | IfacePtr loopback = ifacemgr->getIface(LOOPBACK_NAME); | |||
2693 | ASSERT_TRUE(loopback)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(loopback)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 2693, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "loopback", "false" , "true") .c_str()) = ::testing::Message(); | |||
2694 | loopback->addSocket(sock1); | |||
2695 | loopback->addSocket(sock2); | |||
2696 | loopback->addSocket(sock3); | |||
2697 | ||||
2698 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_REPLY, 123456)); | |||
2699 | ||||
2700 | // pkt6 does not have interface set yet | |||
2701 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2704; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2704; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2704; } } else gtest_label_testthrow_2704 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2704, gtest_msg. value.c_str()) = ::testing::Message() | |||
2702 | ifacemgr->getSocket(pkt6),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2704; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2704; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2704; } } else gtest_label_testthrow_2704 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2704, gtest_msg. value.c_str()) = ::testing::Message() | |||
2703 | IfaceNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2704; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2704; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2704; } } else gtest_label_testthrow_2704 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2704, gtest_msg. value.c_str()) = ::testing::Message() | |||
2704 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2704; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2704; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2704; } } else gtest_label_testthrow_2704 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2704, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2705 | ||||
2706 | // Try to send over non-existing interface | |||
2707 | pkt6->setIface("nosuchinterface45"); | |||
2708 | pkt6->setIndex(12345); | |||
2709 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2712; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2712; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2712; } } else gtest_label_testthrow_2712 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2712, gtest_msg. value.c_str()) = ::testing::Message() | |||
2710 | ifacemgr->getSocket(pkt6),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2712; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2712; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2712; } } else gtest_label_testthrow_2712 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2712, gtest_msg. value.c_str()) = ::testing::Message() | |||
2711 | IfaceNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2712; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2712; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2712; } } else gtest_label_testthrow_2712 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2712, gtest_msg. value.c_str()) = ::testing::Message() | |||
2712 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2712; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2712; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2712; } } else gtest_label_testthrow_2712 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2712, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2713 | ||||
2714 | // Index is now checked first | |||
2715 | pkt6->setIface(LOOPBACK_NAME); | |||
2716 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2719; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2719; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2719; } } else gtest_label_testthrow_2719 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2719, gtest_msg. value.c_str()) = ::testing::Message() | |||
2717 | ifacemgr->getSocket(pkt6),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2719; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2719; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2719; } } else gtest_label_testthrow_2719 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2719, gtest_msg. value.c_str()) = ::testing::Message() | |||
2718 | IfaceNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2719; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2719; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2719; } } else gtest_label_testthrow_2719 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2719, gtest_msg. value.c_str()) = ::testing::Message() | |||
2719 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2719; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2719; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2719; } } else gtest_label_testthrow_2719 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2719, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2720 | ||||
2721 | // This will work | |||
2722 | pkt6->setIndex(LOOPBACK_INDEX); | |||
2723 | EXPECT_EQ(9, ifacemgr->getSocket(pkt6))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("9", "ifacemgr->getSocket(pkt6)" , 9, ifacemgr->getSocket(pkt6)))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2723, gtest_ar.failure_message()) = ::testing::Message(); | |||
2724 | ||||
2725 | bool deleted = false; | |||
2726 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket (9);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2728; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2728 ; } } else gtest_label_testnothrow_2728 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2728, ("Expected: " "deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket(9);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2727 | deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket(9);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket (9);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2728; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2728 ; } } else gtest_label_testnothrow_2728 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2728, ("Expected: " "deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket(9);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2728 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket (9);; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_2728; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2728 ; } } else gtest_label_testnothrow_2728 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2728, ("Expected: " "deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket(9);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2729 | EXPECT_EQ(true, deleted)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("true", "deleted" , true, deleted))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 2729, gtest_ar.failure_message()) = ::testing::Message(); | |||
2730 | ||||
2731 | // It should throw again, there's no usable socket anymore | |||
2732 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2735; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2735; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2735; } } else gtest_label_testthrow_2735 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2735, gtest_msg. value.c_str()) = ::testing::Message() | |||
2733 | ifacemgr->getSocket(pkt6),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2735; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2735; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2735; } } else gtest_label_testthrow_2735 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2735, gtest_msg. value.c_str()) = ::testing::Message() | |||
2734 | SocketNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2735; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2735; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2735; } } else gtest_label_testthrow_2735 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2735, gtest_msg. value.c_str()) = ::testing::Message() | |||
2735 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt6 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2735; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2735; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt6)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2735; } } else gtest_label_testthrow_2735 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2735, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2736 | ||||
2737 | // Repeat for pkt4 | |||
2738 | Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 1)); | |||
2739 | ||||
2740 | // pkt4 does not have interface set yet. | |||
2741 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2744; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2744; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2744; } } else gtest_label_testthrow_2744 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2744, gtest_msg. value.c_str()) = ::testing::Message() | |||
2742 | ifacemgr->getSocket(pkt4),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2744; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2744; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2744; } } else gtest_label_testthrow_2744 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2744, gtest_msg. value.c_str()) = ::testing::Message() | |||
2743 | IfaceNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2744; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2744; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2744; } } else gtest_label_testthrow_2744 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2744, gtest_msg. value.c_str()) = ::testing::Message() | |||
2744 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2744; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2744; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2744; } } else gtest_label_testthrow_2744 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2744, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2745 | ||||
2746 | // Try to send over non-existing interface. | |||
2747 | pkt4->setIface("nosuchinterface45"); | |||
2748 | pkt4->setIndex(12345); | |||
2749 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2752; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2752; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2752; } } else gtest_label_testthrow_2752 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2752, gtest_msg. value.c_str()) = ::testing::Message() | |||
2750 | ifacemgr->getSocket(pkt4),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2752; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2752; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2752; } } else gtest_label_testthrow_2752 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2752, gtest_msg. value.c_str()) = ::testing::Message() | |||
2751 | IfaceNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2752; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2752; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2752; } } else gtest_label_testthrow_2752 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2752, gtest_msg. value.c_str()) = ::testing::Message() | |||
2752 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2752; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2752; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2752; } } else gtest_label_testthrow_2752 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2752, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2753 | ||||
2754 | // Index is now checked first. | |||
2755 | pkt4->setIface(LOOPBACK_NAME); | |||
2756 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2759; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2759; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2759; } } else gtest_label_testthrow_2759 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2759, gtest_msg. value.c_str()) = ::testing::Message() | |||
2757 | ifacemgr->getSocket(pkt4),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2759; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2759; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2759; } } else gtest_label_testthrow_2759 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2759, gtest_msg. value.c_str()) = ::testing::Message() | |||
2758 | IfaceNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2759; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2759; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2759; } } else gtest_label_testthrow_2759 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2759, gtest_msg. value.c_str()) = ::testing::Message() | |||
2759 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (IfaceNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< IfaceNotFound>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2759; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2759; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "IfaceNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2759; } } else gtest_label_testthrow_2759 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2759, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2760 | ||||
2761 | // Socket info is set, packet has well defined interface. It should work. | |||
2762 | pkt4->setIndex(LOOPBACK_INDEX); | |||
2763 | EXPECT_EQ(7, ifacemgr->getSocket(pkt4).sockfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("7", "ifacemgr->getSocket(pkt4).sockfd_" , 7, ifacemgr->getSocket(pkt4).sockfd_))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2763, gtest_ar.failure_message()) = ::testing::Message(); | |||
2764 | ||||
2765 | // Set the local address to check if the socket for this address will | |||
2766 | // be returned. | |||
2767 | pkt4->setLocalAddr(IOAddress("192.0.2.56")); | |||
2768 | EXPECT_EQ(7, ifacemgr->getSocket(pkt4).sockfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("7", "ifacemgr->getSocket(pkt4).sockfd_" , 7, ifacemgr->getSocket(pkt4).sockfd_))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2768, gtest_ar.failure_message()) = ::testing::Message(); | |||
2769 | ||||
2770 | // Modify the local address and expect that the other socket will be | |||
2771 | // returned. | |||
2772 | pkt4->setLocalAddr(IOAddress("192.0.2.53")); | |||
2773 | EXPECT_EQ(8, ifacemgr->getSocket(pkt4).sockfd_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("8", "ifacemgr->getSocket(pkt4).sockfd_" , 8, ifacemgr->getSocket(pkt4).sockfd_))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 2773, gtest_ar.failure_message()) = ::testing::Message(); | |||
2774 | ||||
2775 | EXPECT_NO_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr ->getIface(LOOPBACK_NAME)->delSocket(8);; } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2778; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2778; } } else gtest_label_testnothrow_2778 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2778, ("Expected: " "ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr->getIface(LOOPBACK_NAME)->delSocket(8);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2776 | ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr ->getIface(LOOPBACK_NAME)->delSocket(8);; } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2778; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2778; } } else gtest_label_testnothrow_2778 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2778, ("Expected: " "ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr->getIface(LOOPBACK_NAME)->delSocket(8);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2777 | ifacemgr->getIface(LOOPBACK_NAME)->delSocket(8);switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr ->getIface(LOOPBACK_NAME)->delSocket(8);; } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2778; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2778; } } else gtest_label_testnothrow_2778 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2778, ("Expected: " "ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr->getIface(LOOPBACK_NAME)->delSocket(8);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
2778 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr ->getIface(LOOPBACK_NAME)->delSocket(8);; } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_2778; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2778; } } else gtest_label_testnothrow_2778 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2778, ("Expected: " "ifacemgr->getIface(LOOPBACK_NAME)->delSocket(7); ifacemgr->getIface(LOOPBACK_NAME)->delSocket(8);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
2779 | ||||
2780 | // It should throw again, there's no usable socket anymore. | |||
2781 | EXPECT_THROW(switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2784; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2784; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2784; } } else gtest_label_testthrow_2784 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2784, gtest_msg. value.c_str()) = ::testing::Message() | |||
2782 | ifacemgr->getSocket(pkt4),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2784; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2784; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2784; } } else gtest_label_testthrow_2784 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2784, gtest_msg. value.c_str()) = ::testing::Message() | |||
2783 | SocketNotFoundswitch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2784; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2784; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2784; } } else gtest_label_testthrow_2784 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2784, gtest_msg. value.c_str()) = ::testing::Message() | |||
2784 | )switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->getSocket(pkt4 ); } else static_assert(true, ""); } catch (SocketNotFound const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketNotFound>::type>::type , std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_2784; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_2784; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->getSocket(pkt4)" " throws an exception of type " "SocketNotFound" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_2784; } } else gtest_label_testthrow_2784 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2784, gtest_msg. value.c_str()) = ::testing::Message(); | |||
2785 | } | |||
2786 | ||||
2787 | #if defined(OS_BSD) | |||
2788 | #include <net/if_dl.h> | |||
2789 | #endif | |||
2790 | ||||
2791 | #include <sys/socket.h> | |||
2792 | #include <net/if.h> | |||
2793 | #include <ifaddrs.h> | |||
2794 | ||||
2795 | /// @brief Checks the index of this interface | |||
2796 | /// @param iface Kea interface structure to be checked | |||
2797 | /// @param ifptr original structure returned by getifaddrs | |||
2798 | /// @return true if index is returned properly | |||
2799 | bool | |||
2800 | checkIfIndex(const Iface & iface, | |||
2801 | struct ifaddrs *& ifptr) { | |||
2802 | return (iface.getIndex() == if_nametoindex(ifptr->ifa_name)); | |||
2803 | } | |||
2804 | ||||
2805 | /// @brief Checks if the interface has proper flags set | |||
2806 | /// @param iface Kea interface structure to be checked | |||
2807 | /// @param ifptr original structure returned by getifaddrs | |||
2808 | /// @return true if flags are set properly | |||
2809 | bool | |||
2810 | checkIfFlags(const Iface & iface, | |||
2811 | struct ifaddrs *& ifptr) { | |||
2812 | bool flag_loopback_ = ifptr->ifa_flags & IFF_LOOPBACKIFF_LOOPBACK; | |||
2813 | bool flag_up_ = ifptr->ifa_flags & IFF_UPIFF_UP; | |||
2814 | bool flag_running_ = ifptr->ifa_flags & IFF_RUNNINGIFF_RUNNING; | |||
2815 | bool flag_multicast_ = ifptr->ifa_flags & IFF_MULTICASTIFF_MULTICAST; | |||
2816 | ||||
2817 | return ((iface.flag_loopback_ == flag_loopback_) && | |||
2818 | (iface.flag_up_ == flag_up_) && | |||
2819 | (iface.flag_running_ == flag_running_) && | |||
2820 | (iface.flag_multicast_ == flag_multicast_)); | |||
2821 | } | |||
2822 | ||||
2823 | /// @brief Checks if MAC Address/IP Addresses are properly well formed | |||
2824 | /// @param iface Kea interface structure to be checked | |||
2825 | /// @param ifptr original structure returned by getifaddrs | |||
2826 | /// @return true if addresses are returned properly | |||
2827 | bool | |||
2828 | checkIfAddrs(const Iface & iface, struct ifaddrs *& ifptr) { | |||
2829 | const unsigned char * p = 0; | |||
2830 | #if defined(OS_LINUX1) | |||
2831 | // Workaround for Linux ... | |||
2832 | if(ifptr->ifa_data != 0) { | |||
2833 | // We avoid localhost as it has no MAC Address | |||
2834 | if (!strncmp(iface.getName().c_str(), "lo", 2)) { | |||
2835 | return (true); | |||
2836 | } | |||
2837 | ||||
2838 | struct ifreq ifr; | |||
2839 | memset(& ifr.ifr_nameifr_ifrn.ifrn_name, 0, sizeof ifr.ifr_nameifr_ifrn.ifrn_name); | |||
2840 | strncpy(ifr.ifr_nameifr_ifrn.ifrn_name, iface.getName().c_str(), sizeof(ifr.ifr_nameifr_ifrn.ifrn_name) - 1); | |||
2841 | ||||
2842 | int s = -1; // Socket descriptor | |||
2843 | ||||
2844 | if ((s = socket(AF_INET2, SOCK_DGRAMSOCK_DGRAM, 0)) < 0) { | |||
2845 | isc_throw(Unexpected, "Cannot create AF_INET socket")do { std::ostringstream oss__; oss__ << "Cannot create AF_INET socket" ; throw Unexpected("iface_mgr_unittest.cc", 2845, oss__.str() .c_str()); } while (1); | |||
2846 | } | |||
2847 | ||||
2848 | if (ioctl(s, SIOCGIFHWADDR0x8927, & ifr) < 0) { | |||
2849 | close(s); | |||
2850 | isc_throw(Unexpected, "Cannot set SIOCGIFHWADDR flag")do { std::ostringstream oss__; oss__ << "Cannot set SIOCGIFHWADDR flag" ; throw Unexpected("iface_mgr_unittest.cc", 2850, oss__.str() .c_str()); } while (1); | |||
2851 | } | |||
2852 | ||||
2853 | p = reinterpret_cast<uint8_t *>(ifr.ifr_ifru.ifru_hwaddr.sa_data); | |||
2854 | ||||
2855 | close(s); | |||
2856 | ||||
2857 | /// @todo: Check MAC address length. For some interfaces it is | |||
2858 | /// different than 6. Some have 0, while some exotic ones (like | |||
2859 | /// infiniband) have 20. | |||
2860 | return (!memcmp(p, iface.getMac(), iface.getMacLen())); | |||
2861 | } | |||
2862 | #endif | |||
2863 | ||||
2864 | if(!ifptr->ifa_addr) { | |||
2865 | return (false); | |||
2866 | } | |||
2867 | ||||
2868 | switch(ifptr->ifa_addr->sa_family) { | |||
2869 | #if defined(OS_BSD) | |||
2870 | case AF_LINK: { | |||
2871 | // We avoid localhost as it has no MAC Address | |||
2872 | if (!strncmp(iface.getName().c_str(), "lo", 2)) { | |||
2873 | return (true); | |||
2874 | } | |||
2875 | ||||
2876 | struct sockaddr_dl * hwdata = | |||
2877 | reinterpret_cast<struct sockaddr_dl *>(ifptr->ifa_addr); | |||
2878 | p = reinterpret_cast<uint8_t *>(LLADDR(hwdata)); | |||
2879 | ||||
2880 | // Extract MAC address length | |||
2881 | if (hwdata->sdl_alen != iface.getMacLen()) { | |||
2882 | return (false); | |||
2883 | } | |||
2884 | ||||
2885 | return (!memcmp(p, iface.getMac(), hwdata->sdl_alen)); | |||
2886 | } | |||
2887 | #endif | |||
2888 | case AF_INET2: { | |||
2889 | struct sockaddr_in * v4data = | |||
2890 | reinterpret_cast<struct sockaddr_in *>(ifptr->ifa_addr); | |||
2891 | p = reinterpret_cast<uint8_t *>(& v4data->sin_addr); | |||
2892 | ||||
2893 | IOAddress addrv4 = IOAddress::fromBytes(AF_INET2, p); | |||
2894 | ||||
2895 | for (auto const& a :iface.getAddresses()) { | |||
2896 | if(a.get().isV4() && (a.get()) == addrv4) { | |||
2897 | return (true); | |||
2898 | } | |||
2899 | } | |||
2900 | ||||
2901 | return (false); | |||
2902 | } | |||
2903 | case AF_INET610: { | |||
2904 | struct sockaddr_in6 * v6data = | |||
2905 | reinterpret_cast<struct sockaddr_in6 *>(ifptr->ifa_addr); | |||
2906 | p = reinterpret_cast<uint8_t *>(& v6data->sin6_addr); | |||
2907 | ||||
2908 | IOAddress addrv6 = IOAddress::fromBytes(AF_INET610, p); | |||
2909 | ||||
2910 | for (auto const& a : iface.getAddresses()) { | |||
2911 | if (a.get().isV6() && (a.get() == addrv6)) { | |||
2912 | return (true); | |||
2913 | } | |||
2914 | } | |||
2915 | ||||
2916 | return (false); | |||
2917 | } | |||
2918 | default: | |||
2919 | return (true); | |||
2920 | } | |||
2921 | } | |||
2922 | ||||
2923 | /// This test checks that the IfaceMgr detects interfaces correctly and | |||
2924 | /// that detected interfaces have correct properties. | |||
2925 | TEST_F(IfaceMgrTest, detectIfaces)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("detectIfaces") > 1, "test_name must not be empty" ); class IfaceMgrTest_detectIfaces_Test : public IfaceMgrTest { public: IfaceMgrTest_detectIfaces_Test() = default; ~IfaceMgrTest_detectIfaces_Test () override = default; IfaceMgrTest_detectIfaces_Test (const IfaceMgrTest_detectIfaces_Test &) = delete; IfaceMgrTest_detectIfaces_Test & operator =( const IfaceMgrTest_detectIfaces_Test &) = delete; IfaceMgrTest_detectIfaces_Test (IfaceMgrTest_detectIfaces_Test &&) noexcept = delete ; IfaceMgrTest_detectIfaces_Test & operator=( IfaceMgrTest_detectIfaces_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_detectIfaces_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "detectIfaces", nullptr, nullptr, ::testing::internal::CodeLocation ("iface_mgr_unittest.cc", 2925), (::testing::internal::GetTypeId <IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2925), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2925), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_detectIfaces_Test >); void IfaceMgrTest_detectIfaces_Test::TestBody() { | |||
2926 | NakedIfaceMgr ifacemgr; | |||
2927 | ||||
2928 | // We are using struct ifaddrs as it is the only good portable one | |||
2929 | // ifreq and ioctls are far from portable. For BSD ifreq::ifa_flags field | |||
2930 | // is only a short which, nowadays, can be negative | |||
2931 | struct ifaddrs *iflist = 0, *ifptr = 0; | |||
2932 | ASSERT_EQ(0, getifaddrs(&iflist))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "getifaddrs(&iflist)" , 0, getifaddrs(&iflist)))) ; else return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 2932, gtest_ar.failure_message()) = ::testing::Message() | |||
2933 | << "Unit test failed to detect interfaces."; | |||
2934 | ||||
2935 | // Go over all interfaces detected by the unit test and see if they | |||
2936 | // match with the interfaces detected by IfaceMgr. | |||
2937 | for (ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) { | |||
2938 | // When more than one IPv4 address is assigned to the particular | |||
2939 | // physical interface, virtual interfaces may be created for each | |||
2940 | // additional IPv4 address. For example, when multiple addresses | |||
2941 | // are assigned to the eth0 interface, additional virtual interfaces | |||
2942 | // will be eth0:0, eth0:1 etc. This is the case on some Linux | |||
2943 | // distributions. The getifaddrs will return virtual interfaces, | |||
2944 | // with single address each, but the Netlink-based implementation | |||
2945 | // (used by IfaceMgr) will rather hold a list of physical interfaces | |||
2946 | // with multiple IPv4 addresses assigned. This means that the test | |||
2947 | // can't use a name of the interface returned by getifaddrs to match | |||
2948 | // with the interface name held by IfaceMgr. Instead, we use the | |||
2949 | // index of the interface because the virtual interfaces have the | |||
2950 | // same indexes as the physical interfaces. | |||
2951 | IfacePtr i = ifacemgr.getIface(if_nametoindex(ifptr->ifa_name)); | |||
2952 | ||||
2953 | // If the given interface was also detected by the IfaceMgr, | |||
2954 | // check that its properties are correct. | |||
2955 | if (i != NULL__null) { | |||
2956 | // Check if interface index is reported properly | |||
2957 | EXPECT_TRUE(checkIfIndex(*i, ifptr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(checkIfIndex(*i, ifptr ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2957, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "checkIfIndex(*i, ifptr)" , "false", "true") .c_str()) = ::testing::Message() | |||
2958 | << "Non-matching index of the detected interface " | |||
2959 | << i->getName(); | |||
2960 | ||||
2961 | // Check if flags are reported properly | |||
2962 | EXPECT_TRUE(checkIfFlags(*i, ifptr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(checkIfFlags(*i, ifptr ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2962, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "checkIfFlags(*i, ifptr)" , "false", "true") .c_str()) = ::testing::Message() | |||
2963 | << "Non-matching flags of the detected interface " | |||
2964 | << i->getName(); | |||
2965 | ||||
2966 | // Check if addresses are reported properly | |||
2967 | EXPECT_TRUE(checkIfAddrs(*i, ifptr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(checkIfAddrs(*i, ifptr ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 2967, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "checkIfAddrs(*i, ifptr)" , "false", "true") .c_str()) = ::testing::Message() | |||
2968 | << "Non-matching addresses on the detected interface " | |||
2969 | << i->getName(); | |||
2970 | ||||
2971 | } else { | |||
2972 | // The interface detected here seems to be missing in the | |||
2973 | // IfaceMgr. | |||
2974 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 2974, "Failed") = :: testing::Message() << "Interface " << ifptr->ifa_name | |||
2975 | << " not detected by the Interface Manager"; | |||
2976 | } | |||
2977 | } | |||
2978 | ||||
2979 | freeifaddrs(iflist); | |||
2980 | iflist = 0; | |||
2981 | } | |||
2982 | ||||
2983 | volatile bool callback_ok; | |||
2984 | volatile bool callback2_ok; | |||
2985 | ||||
2986 | void my_callback(int /* fd */) { | |||
2987 | callback_ok = true; | |||
2988 | } | |||
2989 | ||||
2990 | void my_callback2(int /* fd */) { | |||
2991 | callback2_ok = true; | |||
2992 | } | |||
2993 | ||||
2994 | // Tests if a single external socket and its callback can be passed and | |||
2995 | // it is supported properly by receive4() method. | |||
2996 | TEST_F(IfaceMgrTest, SingleExternalSocket4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("SingleExternalSocket4") > 1, "test_name must not be empty" ); class IfaceMgrTest_SingleExternalSocket4_Test : public IfaceMgrTest { public: IfaceMgrTest_SingleExternalSocket4_Test() = default ; ~IfaceMgrTest_SingleExternalSocket4_Test() override = default ; IfaceMgrTest_SingleExternalSocket4_Test (const IfaceMgrTest_SingleExternalSocket4_Test &) = delete; IfaceMgrTest_SingleExternalSocket4_Test & operator=( const IfaceMgrTest_SingleExternalSocket4_Test & ) = delete; IfaceMgrTest_SingleExternalSocket4_Test (IfaceMgrTest_SingleExternalSocket4_Test &&) noexcept = delete; IfaceMgrTest_SingleExternalSocket4_Test & operator=( IfaceMgrTest_SingleExternalSocket4_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_SingleExternalSocket4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "SingleExternalSocket4", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 2996), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 2996), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 2996), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_SingleExternalSocket4_Test >); void IfaceMgrTest_SingleExternalSocket4_Test::TestBody () { | |||
2997 | ||||
2998 | callback_ok = false; | |||
2999 | ||||
3000 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3001 | ||||
3002 | // Create pipe and register it as extra socket | |||
3003 | int pipefd[2]; | |||
3004 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3004, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3005 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], my_callback))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], my_callback); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3005; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3005; } } else gtest_label_testnothrow_3005 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3005, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3006 | ||||
3007 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(2); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3007 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3007 ; } } else gtest_label_testnothrow_3007 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3007, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3008 | ||||
3009 | Pkt4Ptr pkt4; | |||
3010 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3010 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3010 ; } } else gtest_label_testnothrow_3010 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3010, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3011 | ||||
3012 | // Our callback should not be called this time (there was no data) | |||
3013 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3013, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3014 | ||||
3015 | // IfaceMgr should not process control socket data as incoming packets | |||
3016 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3016, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
3017 | ||||
3018 | // Now, send some data over pipe (38 bytes) | |||
3019 | EXPECT_EQ(38, write(pipefd[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(pipefd[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(pipefd[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3019 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3020 | ||||
3021 | // ... and repeat | |||
3022 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3022 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3022 ; } } else gtest_label_testnothrow_3022 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3022, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3023 | ||||
3024 | // IfaceMgr should not process control socket data as incoming packets | |||
3025 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3025, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
3026 | ||||
3027 | // There was some data, so this time callback should be called | |||
3028 | EXPECT_TRUE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback_ok)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3028, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing ::Message(); | |||
3029 | ||||
3030 | // close both pipe ends | |||
3031 | close(pipefd[1]); | |||
3032 | close(pipefd[0]); | |||
3033 | ||||
3034 | ASSERT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3034 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3034 ; } } else gtest_label_testnothrow_3034 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3034, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3035 | } | |||
3036 | ||||
3037 | // Tests if multiple external sockets and their callbacks can be passed and | |||
3038 | // it is supported properly by receive4() method. | |||
3039 | TEST_F(IfaceMgrTest, MultipleExternalSockets4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("MultipleExternalSockets4") > 1, "test_name must not be empty" ); class IfaceMgrTest_MultipleExternalSockets4_Test : public IfaceMgrTest { public: IfaceMgrTest_MultipleExternalSockets4_Test() = default ; ~IfaceMgrTest_MultipleExternalSockets4_Test() override = default ; IfaceMgrTest_MultipleExternalSockets4_Test (const IfaceMgrTest_MultipleExternalSockets4_Test &) = delete; IfaceMgrTest_MultipleExternalSockets4_Test & operator=( const IfaceMgrTest_MultipleExternalSockets4_Test & ) = delete; IfaceMgrTest_MultipleExternalSockets4_Test (IfaceMgrTest_MultipleExternalSockets4_Test &&) noexcept = delete; IfaceMgrTest_MultipleExternalSockets4_Test & operator=( IfaceMgrTest_MultipleExternalSockets4_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_MultipleExternalSockets4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "MultipleExternalSockets4", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 3039), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 3039), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 3039), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_MultipleExternalSockets4_Test >); void IfaceMgrTest_MultipleExternalSockets4_Test::TestBody () { | |||
3040 | ||||
3041 | callback_ok = false; | |||
3042 | callback2_ok = false; | |||
3043 | ||||
3044 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3045 | ||||
3046 | // Create first pipe and register it as extra socket | |||
3047 | int pipefd[2]; | |||
3048 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3048, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3049 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], my_callback))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], my_callback); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3049; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3049; } } else gtest_label_testnothrow_3049 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3049, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3050 | ||||
3051 | // Let's create a second pipe and register it as well | |||
3052 | int secondpipe[2]; | |||
3053 | EXPECT_TRUE(pipe(secondpipe) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(secondpipe) == 0 )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3053, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(secondpipe) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3054 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0], my_callback2))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], my_callback2); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3054; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3054 ; } } else gtest_label_testnothrow_3054 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3054, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3055 | ||||
3056 | Pkt4Ptr pkt4; | |||
3057 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3057 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3057 ; } } else gtest_label_testnothrow_3057 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3057, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3058 | ||||
3059 | // Our callbacks should not be called this time (there was no data) | |||
3060 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3060, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3061 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3061, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3062 | ||||
3063 | // IfaceMgr should not process control socket data as incoming packets | |||
3064 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3064, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
3065 | ||||
3066 | // Now, send some data over the first pipe (38 bytes) | |||
3067 | EXPECT_EQ(38, write(pipefd[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(pipefd[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(pipefd[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3067 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3068 | ||||
3069 | // ... and repeat | |||
3070 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3070 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3070 ; } } else gtest_label_testnothrow_3070 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3070, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3071 | ||||
3072 | // IfaceMgr should not process control socket data as incoming packets | |||
3073 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3073, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
3074 | ||||
3075 | // There was some data, so this time callback should be called | |||
3076 | EXPECT_TRUE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback_ok)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3076, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing ::Message(); | |||
3077 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3077, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3078 | ||||
3079 | // Read the data sent, because our test callbacks are too dumb to actually | |||
3080 | // do it. We don't care about the content read, because we're testing | |||
3081 | // the callbacks, not pipes. | |||
3082 | char buf[80]; | |||
3083 | EXPECT_EQ(38, read(pipefd[0], buf, 80))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "read(pipefd[0], buf, 80)" , 38, read(pipefd[0], buf, 80)))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3083, gtest_ar.failure_message()) = ::testing::Message(); | |||
3084 | ||||
3085 | // Clear the status... | |||
3086 | callback_ok = false; | |||
3087 | callback2_ok = false; | |||
3088 | ||||
3089 | // And try again, using the second pipe | |||
3090 | EXPECT_EQ(38, write(secondpipe[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(secondpipe[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(secondpipe[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3090 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3091 | ||||
3092 | // ... and repeat | |||
3093 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3093 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3093 ; } } else gtest_label_testnothrow_3093 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3093, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3094 | ||||
3095 | // IfaceMgr should not process control socket data as incoming packets | |||
3096 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3096, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
3097 | ||||
3098 | // There was some data, so this time callback should be called | |||
3099 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3099, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3100 | EXPECT_TRUE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback2_ok)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3100, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "false" , "true") .c_str()) = ::testing::Message(); | |||
3101 | ||||
3102 | // close both pipe ends | |||
3103 | close(pipefd[1]); | |||
3104 | close(pipefd[0]); | |||
3105 | ||||
3106 | close(secondpipe[1]); | |||
3107 | close(secondpipe[0]); | |||
3108 | } | |||
3109 | ||||
3110 | // Tests if existing external socket can be deleted and that such deletion does | |||
3111 | // not affect any other existing sockets. Tests uses receive4() | |||
3112 | TEST_F(IfaceMgrTest, DeleteExternalSockets4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DeleteExternalSockets4") > 1, "test_name must not be empty" ); class IfaceMgrTest_DeleteExternalSockets4_Test : public IfaceMgrTest { public: IfaceMgrTest_DeleteExternalSockets4_Test() = default ; ~IfaceMgrTest_DeleteExternalSockets4_Test() override = default ; IfaceMgrTest_DeleteExternalSockets4_Test (const IfaceMgrTest_DeleteExternalSockets4_Test &) = delete; IfaceMgrTest_DeleteExternalSockets4_Test & operator=( const IfaceMgrTest_DeleteExternalSockets4_Test & ) = delete; IfaceMgrTest_DeleteExternalSockets4_Test (IfaceMgrTest_DeleteExternalSockets4_Test &&) noexcept = delete; IfaceMgrTest_DeleteExternalSockets4_Test & operator=( IfaceMgrTest_DeleteExternalSockets4_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_DeleteExternalSockets4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DeleteExternalSockets4", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 3112), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 3112), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 3112), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DeleteExternalSockets4_Test >); void IfaceMgrTest_DeleteExternalSockets4_Test::TestBody () { | |||
3113 | ||||
3114 | callback_ok = false; | |||
3115 | callback2_ok = false; | |||
3116 | ||||
3117 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3118 | ||||
3119 | // Create first pipe and register it as extra socket | |||
3120 | int pipefd[2]; | |||
3121 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3121, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3122 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], my_callback))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], my_callback); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3122; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3122; } } else gtest_label_testnothrow_3122 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3122, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3123 | ||||
3124 | // Let's create a second pipe and register it as well | |||
3125 | int secondpipe[2]; | |||
3126 | EXPECT_TRUE(pipe(secondpipe) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(secondpipe) == 0 )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3126, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(secondpipe) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3127 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0], my_callback2))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], my_callback2); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3127; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3127 ; } } else gtest_label_testnothrow_3127 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3127, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3128 | ||||
3129 | // Now delete the first session socket | |||
3130 | EXPECT_NO_THROW(ifacemgr->deleteExternalSocket(pipefd[0]))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->deleteExternalSocket(pipefd[0]); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3130; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3130; } } else gtest_label_testnothrow_3130 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3130, ("Expected: " "ifacemgr->deleteExternalSocket(pipefd[0])" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3131 | ||||
3132 | // Now check whether the second callback is still functional | |||
3133 | EXPECT_EQ(38, write(secondpipe[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(secondpipe[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(secondpipe[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3133 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3134 | ||||
3135 | // ... and repeat | |||
3136 | Pkt4Ptr pkt4; | |||
3137 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3137 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3137 ; } } else gtest_label_testnothrow_3137 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3137, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3138 | ||||
3139 | // IfaceMgr should not process control socket data as incoming packets | |||
3140 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3140, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing:: Message(); | |||
3141 | ||||
3142 | // There was some data, so this time callback should be called | |||
3143 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3143, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3144 | EXPECT_TRUE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback2_ok)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3144, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "false" , "true") .c_str()) = ::testing::Message(); | |||
3145 | ||||
3146 | // Let's reset the status | |||
3147 | callback_ok = false; | |||
3148 | callback2_ok = false; | |||
3149 | ||||
3150 | // Now let's send something over the first callback that was unregistered. | |||
3151 | // We should NOT receive any callback. | |||
3152 | EXPECT_EQ(38, write(pipefd[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(pipefd[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(pipefd[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3152 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3153 | ||||
3154 | // Now check that the first callback is NOT called. | |||
3155 | ASSERT_NO_THROW(pkt4 = ifacemgr->receive4(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt4 = ifacemgr->receive4(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3155 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3155 ; } } else gtest_label_testnothrow_3155 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3155, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3156 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3156, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3157 | ||||
3158 | // close both pipe ends | |||
3159 | close(pipefd[1]); | |||
3160 | close(pipefd[0]); | |||
3161 | ||||
3162 | close(secondpipe[1]); | |||
3163 | close(secondpipe[0]); | |||
3164 | } | |||
3165 | ||||
3166 | // Tests that an existing external socket that becomes invalid | |||
3167 | // is detected and purged, without affecting other sockets. | |||
3168 | // Tests uses receive4() without queuing. | |||
3169 | TEST_F(IfaceMgrTest, purgeExternalSockets4Direct)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("purgeExternalSockets4Direct") > 1 , "test_name must not be empty"); class IfaceMgrTest_purgeExternalSockets4Direct_Test : public IfaceMgrTest { public: IfaceMgrTest_purgeExternalSockets4Direct_Test () = default; ~IfaceMgrTest_purgeExternalSockets4Direct_Test( ) override = default; IfaceMgrTest_purgeExternalSockets4Direct_Test (const IfaceMgrTest_purgeExternalSockets4Direct_Test &) = delete; IfaceMgrTest_purgeExternalSockets4Direct_Test & operator =( const IfaceMgrTest_purgeExternalSockets4Direct_Test &) = delete; IfaceMgrTest_purgeExternalSockets4Direct_Test (IfaceMgrTest_purgeExternalSockets4Direct_Test &&) noexcept = delete; IfaceMgrTest_purgeExternalSockets4Direct_Test & operator=( IfaceMgrTest_purgeExternalSockets4Direct_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_purgeExternalSockets4Direct_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "purgeExternalSockets4Direct", nullptr, nullptr, ::testing:: internal::CodeLocation("iface_mgr_unittest.cc", 3169), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 3169), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 3169), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_purgeExternalSockets4Direct_Test >); void IfaceMgrTest_purgeExternalSockets4Direct_Test::TestBody () { | |||
3170 | purgeExternalSockets4Test(); | |||
3171 | } | |||
3172 | ||||
3173 | ||||
3174 | // Tests that an existing external socket that becomes invalid | |||
3175 | // is detected and purged, without affecting other sockets. | |||
3176 | // Tests uses receive4() with queuing. | |||
3177 | TEST_F(IfaceMgrTest, purgeExternalSockets4Indirect)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("purgeExternalSockets4Indirect") > 1, "test_name must not be empty"); class IfaceMgrTest_purgeExternalSockets4Indirect_Test : public IfaceMgrTest { public: IfaceMgrTest_purgeExternalSockets4Indirect_Test () = default; ~IfaceMgrTest_purgeExternalSockets4Indirect_Test () override = default; IfaceMgrTest_purgeExternalSockets4Indirect_Test (const IfaceMgrTest_purgeExternalSockets4Indirect_Test & ) = delete; IfaceMgrTest_purgeExternalSockets4Indirect_Test & operator=( const IfaceMgrTest_purgeExternalSockets4Indirect_Test &) = delete; IfaceMgrTest_purgeExternalSockets4Indirect_Test (IfaceMgrTest_purgeExternalSockets4Indirect_Test &&) noexcept = delete; IfaceMgrTest_purgeExternalSockets4Indirect_Test & operator=( IfaceMgrTest_purgeExternalSockets4Indirect_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_purgeExternalSockets4Indirect_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "purgeExternalSockets4Indirect", nullptr, nullptr, ::testing ::internal::CodeLocation("iface_mgr_unittest.cc", 3177), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 3177), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 3177), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_purgeExternalSockets4Indirect_Test >); void IfaceMgrTest_purgeExternalSockets4Indirect_Test:: TestBody() { | |||
3178 | purgeExternalSockets4Test(true); | |||
3179 | } | |||
3180 | ||||
3181 | // Tests if a single external socket and its callback can be passed and | |||
3182 | // it is supported properly by receive6() method. | |||
3183 | TEST_F(IfaceMgrTest, SingleExternalSocket6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("SingleExternalSocket6") > 1, "test_name must not be empty" ); class IfaceMgrTest_SingleExternalSocket6_Test : public IfaceMgrTest { public: IfaceMgrTest_SingleExternalSocket6_Test() = default ; ~IfaceMgrTest_SingleExternalSocket6_Test() override = default ; IfaceMgrTest_SingleExternalSocket6_Test (const IfaceMgrTest_SingleExternalSocket6_Test &) = delete; IfaceMgrTest_SingleExternalSocket6_Test & operator=( const IfaceMgrTest_SingleExternalSocket6_Test & ) = delete; IfaceMgrTest_SingleExternalSocket6_Test (IfaceMgrTest_SingleExternalSocket6_Test &&) noexcept = delete; IfaceMgrTest_SingleExternalSocket6_Test & operator=( IfaceMgrTest_SingleExternalSocket6_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_SingleExternalSocket6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "SingleExternalSocket6", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 3183), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 3183), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 3183), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_SingleExternalSocket6_Test >); void IfaceMgrTest_SingleExternalSocket6_Test::TestBody () { | |||
3184 | ||||
3185 | callback_ok = false; | |||
3186 | ||||
3187 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3188 | ||||
3189 | // Create pipe and register it as extra socket | |||
3190 | int pipefd[2]; | |||
3191 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3191, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3192 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], my_callback))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], my_callback); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3192; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3192; } } else gtest_label_testnothrow_3192 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3192, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3193 | ||||
3194 | Pkt6Ptr pkt6; | |||
3195 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3195 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3195 ; } } else gtest_label_testnothrow_3195 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3195, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3196 | ||||
3197 | // Our callback should not be called this time (there was no data) | |||
3198 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3198, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3199 | ||||
3200 | // IfaceMgr should not process control socket data as incoming packets | |||
3201 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3201, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
3202 | ||||
3203 | // Now, send some data over pipe (38 bytes) | |||
3204 | EXPECT_EQ(38, write(pipefd[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(pipefd[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(pipefd[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3204 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3205 | ||||
3206 | // ... and repeat | |||
3207 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3207 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3207 ; } } else gtest_label_testnothrow_3207 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3207, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3208 | ||||
3209 | // IfaceMgr should not process control socket data as incoming packets | |||
3210 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3210, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
3211 | ||||
3212 | // There was some data, so this time callback should be called | |||
3213 | EXPECT_TRUE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback_ok)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3213, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing ::Message(); | |||
3214 | ||||
3215 | // close both pipe ends | |||
3216 | close(pipefd[1]); | |||
3217 | close(pipefd[0]); | |||
3218 | } | |||
3219 | ||||
3220 | // Tests if multiple external sockets and their callbacks can be passed and | |||
3221 | // it is supported properly by receive6() method. | |||
3222 | TEST_F(IfaceMgrTest, MultipleExternalSockets6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("MultipleExternalSockets6") > 1, "test_name must not be empty" ); class IfaceMgrTest_MultipleExternalSockets6_Test : public IfaceMgrTest { public: IfaceMgrTest_MultipleExternalSockets6_Test() = default ; ~IfaceMgrTest_MultipleExternalSockets6_Test() override = default ; IfaceMgrTest_MultipleExternalSockets6_Test (const IfaceMgrTest_MultipleExternalSockets6_Test &) = delete; IfaceMgrTest_MultipleExternalSockets6_Test & operator=( const IfaceMgrTest_MultipleExternalSockets6_Test & ) = delete; IfaceMgrTest_MultipleExternalSockets6_Test (IfaceMgrTest_MultipleExternalSockets6_Test &&) noexcept = delete; IfaceMgrTest_MultipleExternalSockets6_Test & operator=( IfaceMgrTest_MultipleExternalSockets6_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_MultipleExternalSockets6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "MultipleExternalSockets6", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 3222), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 3222), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 3222), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_MultipleExternalSockets6_Test >); void IfaceMgrTest_MultipleExternalSockets6_Test::TestBody () { | |||
3223 | ||||
3224 | callback_ok = false; | |||
3225 | callback2_ok = false; | |||
3226 | ||||
3227 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3228 | ||||
3229 | // Create first pipe and register it as extra socket | |||
3230 | int pipefd[2]; | |||
3231 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3231, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3232 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], my_callback))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], my_callback); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3232; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3232; } } else gtest_label_testnothrow_3232 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3232, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3233 | ||||
3234 | // Let's create a second pipe and register it as well | |||
3235 | int secondpipe[2]; | |||
3236 | EXPECT_TRUE(pipe(secondpipe) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(secondpipe) == 0 )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3236, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(secondpipe) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3237 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0], my_callback2))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], my_callback2); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3237; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3237 ; } } else gtest_label_testnothrow_3237 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3237, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3238 | ||||
3239 | Pkt6Ptr pkt6; | |||
3240 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3240 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3240 ; } } else gtest_label_testnothrow_3240 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3240, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3241 | ||||
3242 | // Our callbacks should not be called this time (there was no data) | |||
3243 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3243, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3244 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3244, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3245 | ||||
3246 | // IfaceMgr should not process control socket data as incoming packets | |||
3247 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3247, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
3248 | ||||
3249 | // Now, send some data over the first pipe (38 bytes) | |||
3250 | EXPECT_EQ(38, write(pipefd[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(pipefd[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(pipefd[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3250 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3251 | ||||
3252 | // ... and repeat | |||
3253 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3253 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3253 ; } } else gtest_label_testnothrow_3253 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3253, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3254 | ||||
3255 | // IfaceMgr should not process control socket data as incoming packets | |||
3256 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3256, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
3257 | ||||
3258 | // There was some data, so this time callback should be called | |||
3259 | EXPECT_TRUE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback_ok)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3259, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing ::Message(); | |||
3260 | EXPECT_FALSE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback2_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3260, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3261 | ||||
3262 | // Read the data sent, because our test callbacks are too dumb to actually | |||
3263 | // do it. We don't care about the content read, because we're testing | |||
3264 | // the callbacks, not pipes. | |||
3265 | char buf[80]; | |||
3266 | EXPECT_EQ(38, read(pipefd[0], buf, 80))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "read(pipefd[0], buf, 80)" , 38, read(pipefd[0], buf, 80)))) ; else ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3266, gtest_ar.failure_message()) = ::testing::Message(); | |||
3267 | ||||
3268 | // Clear the status... | |||
3269 | callback_ok = false; | |||
3270 | callback2_ok = false; | |||
3271 | ||||
3272 | // And try again, using the second pipe | |||
3273 | EXPECT_EQ(38, write(secondpipe[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(secondpipe[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(secondpipe[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3273 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3274 | ||||
3275 | // ... and repeat | |||
3276 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3276 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3276 ; } } else gtest_label_testnothrow_3276 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3276, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3277 | ||||
3278 | // IfaceMgr should not process control socket data as incoming packets | |||
3279 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3279, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
3280 | ||||
3281 | // There was some data, so this time callback should be called | |||
3282 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3282, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3283 | EXPECT_TRUE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback2_ok)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3283, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "false" , "true") .c_str()) = ::testing::Message(); | |||
3284 | ||||
3285 | // close both pipe ends | |||
3286 | close(pipefd[1]); | |||
3287 | close(pipefd[0]); | |||
3288 | ||||
3289 | close(secondpipe[1]); | |||
3290 | close(secondpipe[0]); | |||
3291 | } | |||
3292 | ||||
3293 | // Tests if existing external socket can be deleted and that such deletion does | |||
3294 | // not affect any other existing sockets. Tests uses receive6() | |||
3295 | TEST_F(IfaceMgrTest, DeleteExternalSockets6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DeleteExternalSockets6") > 1, "test_name must not be empty" ); class IfaceMgrTest_DeleteExternalSockets6_Test : public IfaceMgrTest { public: IfaceMgrTest_DeleteExternalSockets6_Test() = default ; ~IfaceMgrTest_DeleteExternalSockets6_Test() override = default ; IfaceMgrTest_DeleteExternalSockets6_Test (const IfaceMgrTest_DeleteExternalSockets6_Test &) = delete; IfaceMgrTest_DeleteExternalSockets6_Test & operator=( const IfaceMgrTest_DeleteExternalSockets6_Test & ) = delete; IfaceMgrTest_DeleteExternalSockets6_Test (IfaceMgrTest_DeleteExternalSockets6_Test &&) noexcept = delete; IfaceMgrTest_DeleteExternalSockets6_Test & operator=( IfaceMgrTest_DeleteExternalSockets6_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_DeleteExternalSockets6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DeleteExternalSockets6", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 3295), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 3295), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 3295), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DeleteExternalSockets6_Test >); void IfaceMgrTest_DeleteExternalSockets6_Test::TestBody () { | |||
3296 | ||||
3297 | callback_ok = false; | |||
3298 | callback2_ok = false; | |||
3299 | ||||
3300 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3301 | ||||
3302 | // Create first pipe and register it as extra socket | |||
3303 | int pipefd[2]; | |||
3304 | EXPECT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3304, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(pipefd) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3305 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], my_callback))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], my_callback); } else static_assert(true, ""); } catch (std::exception const& e ) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing ::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3305; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3305; } } else gtest_label_testnothrow_3305 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3305, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3306 | ||||
3307 | // Let's create a second pipe and register it as well | |||
3308 | int secondpipe[2]; | |||
3309 | EXPECT_TRUE(pipe(secondpipe) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(secondpipe) == 0 )) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3309, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "pipe(secondpipe) == 0" , "false", "true") .c_str()) = ::testing::Message(); | |||
3310 | EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0], my_callback2))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], my_callback2); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3310; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3310 ; } } else gtest_label_testnothrow_3310 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3310, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3311 | ||||
3312 | // Now delete the first session socket | |||
3313 | EXPECT_NO_THROW(ifacemgr->deleteExternalSocket(pipefd[0]))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->deleteExternalSocket(pipefd[0]); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3313; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3313; } } else gtest_label_testnothrow_3313 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3313, ("Expected: " "ifacemgr->deleteExternalSocket(pipefd[0])" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3314 | ||||
3315 | // Now check whether the second callback is still functional | |||
3316 | EXPECT_EQ(38, write(secondpipe[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(secondpipe[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(secondpipe[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3316 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3317 | ||||
3318 | // ... and repeat | |||
3319 | Pkt6Ptr pkt6; | |||
3320 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3320 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3320 ; } } else gtest_label_testnothrow_3320 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3320, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3321 | ||||
3322 | // IfaceMgr should not process control socket data as incoming packets | |||
3323 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3323, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing:: Message(); | |||
3324 | ||||
3325 | // There was some data, so this time callback should be called | |||
3326 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3326, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3327 | EXPECT_TRUE(callback2_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(callback2_ok)) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3327, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback2_ok", "false" , "true") .c_str()) = ::testing::Message(); | |||
3328 | ||||
3329 | // Let's reset the status | |||
3330 | callback_ok = false; | |||
3331 | callback2_ok = false; | |||
3332 | ||||
3333 | // Now let's send something over the first callback that was unregistered. | |||
3334 | // We should NOT receive any callback. | |||
3335 | EXPECT_EQ(38, write(pipefd[1], "Hi, this is a message sent over a pipe", 38))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("38", "write(pipefd[1], \"Hi, this is a message sent over a pipe\", 38)" , 38, write(pipefd[1], "Hi, this is a message sent over a pipe" , 38)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3335 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3336 | ||||
3337 | // Now check that the first callback is NOT called. | |||
3338 | ASSERT_NO_THROW(pkt6 = ifacemgr->receive6(1))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { pkt6 = ifacemgr->receive6(1); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3338 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3338 ; } } else gtest_label_testnothrow_3338 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3338, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3339 | EXPECT_FALSE(callback_ok)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(callback_ok))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3339, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "true" , "false") .c_str()) = ::testing::Message(); | |||
3340 | ||||
3341 | // close both pipe ends | |||
3342 | close(pipefd[1]); | |||
3343 | close(pipefd[0]); | |||
3344 | ||||
3345 | close(secondpipe[1]); | |||
3346 | close(secondpipe[0]); | |||
3347 | } | |||
3348 | ||||
3349 | // Tests that an existing external socket that becomes invalid | |||
3350 | // is detected and purged, without affecting other sockets. | |||
3351 | // Tests uses receive6() without queuing. | |||
3352 | TEST_F(IfaceMgrTest, purgeExternalSockets6Direct)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("purgeExternalSockets6Direct") > 1 , "test_name must not be empty"); class IfaceMgrTest_purgeExternalSockets6Direct_Test : public IfaceMgrTest { public: IfaceMgrTest_purgeExternalSockets6Direct_Test () = default; ~IfaceMgrTest_purgeExternalSockets6Direct_Test( ) override = default; IfaceMgrTest_purgeExternalSockets6Direct_Test (const IfaceMgrTest_purgeExternalSockets6Direct_Test &) = delete; IfaceMgrTest_purgeExternalSockets6Direct_Test & operator =( const IfaceMgrTest_purgeExternalSockets6Direct_Test &) = delete; IfaceMgrTest_purgeExternalSockets6Direct_Test (IfaceMgrTest_purgeExternalSockets6Direct_Test &&) noexcept = delete; IfaceMgrTest_purgeExternalSockets6Direct_Test & operator=( IfaceMgrTest_purgeExternalSockets6Direct_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_purgeExternalSockets6Direct_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "purgeExternalSockets6Direct", nullptr, nullptr, ::testing:: internal::CodeLocation("iface_mgr_unittest.cc", 3352), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 3352), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 3352), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_purgeExternalSockets6Direct_Test >); void IfaceMgrTest_purgeExternalSockets6Direct_Test::TestBody () { | |||
3353 | purgeExternalSockets6Test(); | |||
3354 | } | |||
3355 | ||||
3356 | ||||
3357 | // Tests that an existing external socket that becomes invalid | |||
3358 | // is detected and purged, without affecting other sockets. | |||
3359 | // Tests uses receive6() with queuing. | |||
3360 | TEST_F(IfaceMgrTest, purgeExternalSockets6Indirect)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("purgeExternalSockets6Indirect") > 1, "test_name must not be empty"); class IfaceMgrTest_purgeExternalSockets6Indirect_Test : public IfaceMgrTest { public: IfaceMgrTest_purgeExternalSockets6Indirect_Test () = default; ~IfaceMgrTest_purgeExternalSockets6Indirect_Test () override = default; IfaceMgrTest_purgeExternalSockets6Indirect_Test (const IfaceMgrTest_purgeExternalSockets6Indirect_Test & ) = delete; IfaceMgrTest_purgeExternalSockets6Indirect_Test & operator=( const IfaceMgrTest_purgeExternalSockets6Indirect_Test &) = delete; IfaceMgrTest_purgeExternalSockets6Indirect_Test (IfaceMgrTest_purgeExternalSockets6Indirect_Test &&) noexcept = delete; IfaceMgrTest_purgeExternalSockets6Indirect_Test & operator=( IfaceMgrTest_purgeExternalSockets6Indirect_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_purgeExternalSockets6Indirect_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "purgeExternalSockets6Indirect", nullptr, nullptr, ::testing ::internal::CodeLocation("iface_mgr_unittest.cc", 3360), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 3360), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 3360), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_purgeExternalSockets6Indirect_Test >); void IfaceMgrTest_purgeExternalSockets6Indirect_Test:: TestBody() { | |||
3361 | purgeExternalSockets6Test(true); | |||
3362 | } | |||
3363 | ||||
3364 | // Test checks if the unicast sockets can be opened. | |||
3365 | // This test is now disabled, because there is no reliable way to test it. We | |||
3366 | // can't even use loopback, because openSockets() skips loopback interface | |||
3367 | // (as it should be, because DHCP server is not supposed to listen on loopback). | |||
3368 | TEST_F(IfaceMgrTest, DISABLED_openUnicastSockets)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DISABLED_openUnicastSockets") > 1 , "test_name must not be empty"); class IfaceMgrTest_DISABLED_openUnicastSockets_Test : public IfaceMgrTest { public: IfaceMgrTest_DISABLED_openUnicastSockets_Test () = default; ~IfaceMgrTest_DISABLED_openUnicastSockets_Test( ) override = default; IfaceMgrTest_DISABLED_openUnicastSockets_Test (const IfaceMgrTest_DISABLED_openUnicastSockets_Test &) = delete; IfaceMgrTest_DISABLED_openUnicastSockets_Test & operator =( const IfaceMgrTest_DISABLED_openUnicastSockets_Test &) = delete; IfaceMgrTest_DISABLED_openUnicastSockets_Test (IfaceMgrTest_DISABLED_openUnicastSockets_Test &&) noexcept = delete; IfaceMgrTest_DISABLED_openUnicastSockets_Test & operator=( IfaceMgrTest_DISABLED_openUnicastSockets_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_DISABLED_openUnicastSockets_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DISABLED_openUnicastSockets", nullptr, nullptr, ::testing:: internal::CodeLocation("iface_mgr_unittest.cc", 3368), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 3368), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 3368), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DISABLED_openUnicastSockets_Test >); void IfaceMgrTest_DISABLED_openUnicastSockets_Test::TestBody () { | |||
3369 | /// @todo Need to implement a test that is able to check whether we can open | |||
3370 | /// unicast sockets. There are 2 problems with it: | |||
3371 | /// 1. We need to have a non-link-local address on an interface that is | |||
3372 | /// up, running, IPv6 and multicast capable | |||
3373 | /// 2. We need that information on every OS that we run tests on. So far | |||
3374 | /// we are only supporting interface detection in Linux. | |||
3375 | /// | |||
3376 | /// To achieve this, we will probably need a pre-test setup, similar to what | |||
3377 | /// BIND9 is doing (i.e. configuring well known addresses on loopback). | |||
3378 | ||||
3379 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3380 | ||||
3381 | // Get the interface (todo: which interface) | |||
3382 | IfacePtr iface = ifacemgr->getIface("eth0"); | |||
3383 | ASSERT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "iface_mgr_unittest.cc", 3383, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false" , "true") .c_str()) = ::testing::Message(); | |||
3384 | iface->inactive6_ = false; | |||
3385 | ||||
3386 | // Tell the interface that it should bind to this global interface | |||
3387 | EXPECT_NO_THROW(iface->addUnicast(IOAddress("2001:db8::1")))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface->addUnicast(IOAddress("2001:db8::1")); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3387; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3387; } } else gtest_label_testnothrow_3387 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3387, ("Expected: " "iface->addUnicast(IOAddress(\"2001:db8::1\"))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3388 | ||||
3389 | // Tell IfaceMgr to open sockets. This should trigger at least 2 sockets | |||
3390 | // to open on eth0: link-local and global. On some systems (Linux), an | |||
3391 | // additional socket for multicast may be opened. | |||
3392 | EXPECT_TRUE(ifacemgr->openSockets6(PORT1))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->openSockets6 (PORT1))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3392 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->openSockets6(PORT1)", "false", "true") .c_str ()) = ::testing::Message(); | |||
3393 | ||||
3394 | const Iface::SocketCollection& sockets = iface->getSockets(); | |||
3395 | ASSERT_GE(2, sockets.size())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("2", "sockets.size()" , 2, sockets.size()))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3395, gtest_ar.failure_message()) = ::testing::Message(); | |||
3396 | ||||
3397 | // Global unicast should be first | |||
3398 | EXPECT_TRUE(getSocketByAddr(sockets, IOAddress("2001:db8::1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(getSocketByAddr(sockets , IOAddress("2001:db8::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3398, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "getSocketByAddr(sockets, IOAddress(\"2001:db8::1\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
3399 | EXPECT_TRUE(getSocketByAddr(sockets, IOAddress("figure-out-link-local-addr")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(getSocketByAddr(sockets , IOAddress("figure-out-link-local-addr")))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "iface_mgr_unittest.cc", 3399, ::testing::internal::GetBoolAssertionFailureMessage ( gtest_ar_, "getSocketByAddr(sockets, IOAddress(\"figure-out-link-local-addr\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
3400 | } | |||
3401 | ||||
3402 | // Checks if there is a protection against unicast duplicates. | |||
3403 | TEST_F(IfaceMgrTest, unicastDuplicates)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unicastDuplicates") > 1, "test_name must not be empty" ); class IfaceMgrTest_unicastDuplicates_Test : public IfaceMgrTest { public: IfaceMgrTest_unicastDuplicates_Test() = default; ~ IfaceMgrTest_unicastDuplicates_Test() override = default; IfaceMgrTest_unicastDuplicates_Test (const IfaceMgrTest_unicastDuplicates_Test &) = delete; IfaceMgrTest_unicastDuplicates_Test & operator=( const IfaceMgrTest_unicastDuplicates_Test & ) = delete; IfaceMgrTest_unicastDuplicates_Test (IfaceMgrTest_unicastDuplicates_Test &&) noexcept = delete; IfaceMgrTest_unicastDuplicates_Test & operator=( IfaceMgrTest_unicastDuplicates_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_unicastDuplicates_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unicastDuplicates", nullptr, nullptr, ::testing::internal:: CodeLocation("iface_mgr_unittest.cc", 3403), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 3403), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 3403), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unicastDuplicates_Test >); void IfaceMgrTest_unicastDuplicates_Test::TestBody() { | |||
3404 | NakedIfaceMgr ifacemgr; | |||
3405 | ||||
3406 | IfacePtr iface = ifacemgr.getIface(LOOPBACK_NAME); | |||
3407 | if (!iface) { | |||
3408 | cout << "Local loopback interface not found. Skipping test. " << endl; | |||
3409 | return; | |||
3410 | } | |||
3411 | ||||
3412 | // Tell the interface that it should bind to this global interface | |||
3413 | // It is the first attempt so it should succeed | |||
3414 | EXPECT_NO_THROW(iface->addUnicast(IOAddress("2001:db8::1")))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { iface->addUnicast(IOAddress("2001:db8::1")); } else static_assert (true, ""); } catch (std::exception const& e) { gtest_msg .value = "it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3414; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3414; } } else gtest_label_testnothrow_3414 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3414, ("Expected: " "iface->addUnicast(IOAddress(\"2001:db8::1\"))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3415 | ||||
3416 | // Tell the interface that it should bind to this global interface | |||
3417 | // It is the second attempt so it should fail | |||
3418 | EXPECT_THROW(iface->addUnicast(IOAddress("2001:db8::1")), BadValue)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { iface->addUnicast(IOAddress ("2001:db8::1")); } else static_assert(true, ""); } catch (BadValue const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< BadValue>::type> ::type, std::exception>::value, const ::testing::internal:: NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "iface->addUnicast(IOAddress(\"2001:db8::1\"))" " throws an exception of type " "BadValue" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_3418 ; } catch (...) { gtest_msg.value = "Expected: " "iface->addUnicast(IOAddress(\"2001:db8::1\"))" " throws an exception of type " "BadValue" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_3418; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "iface->addUnicast(IOAddress(\"2001:db8::1\"))" " throws an exception of type " "BadValue" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_3418; } } else gtest_label_testthrow_3418 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3418, gtest_msg. value.c_str()) = ::testing::Message(); | |||
3419 | } | |||
3420 | ||||
3421 | // This test requires addresses 2001:db8:15c::1/128 and fe80::1/64 to be | |||
3422 | // configured on loopback interface | |||
3423 | // | |||
3424 | // Useful commands: | |||
3425 | // ip a a 2001:db8:15c::1/128 dev lo | |||
3426 | // ip a a fe80::1/64 dev lo | |||
3427 | // | |||
3428 | // If you do not issue those commands before running this test, it will fail. | |||
3429 | TEST_F(IfaceMgrTest, DISABLED_getSocket)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DISABLED_getSocket") > 1, "test_name must not be empty" ); class IfaceMgrTest_DISABLED_getSocket_Test : public IfaceMgrTest { public: IfaceMgrTest_DISABLED_getSocket_Test() = default; ~ IfaceMgrTest_DISABLED_getSocket_Test() override = default; IfaceMgrTest_DISABLED_getSocket_Test (const IfaceMgrTest_DISABLED_getSocket_Test &) = delete; IfaceMgrTest_DISABLED_getSocket_Test & operator=( const IfaceMgrTest_DISABLED_getSocket_Test &) = delete; IfaceMgrTest_DISABLED_getSocket_Test (IfaceMgrTest_DISABLED_getSocket_Test &&) noexcept = delete; IfaceMgrTest_DISABLED_getSocket_Test & operator=( IfaceMgrTest_DISABLED_getSocket_Test && ) noexcept = delete; private: void TestBody() override; static ::testing::TestInfo* const test_info_ __attribute__((unused) ); }; ::testing::TestInfo* const IfaceMgrTest_DISABLED_getSocket_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DISABLED_getSocket", nullptr, nullptr, ::testing::internal ::CodeLocation("iface_mgr_unittest.cc", 3429), (::testing::internal ::GetTypeId<IfaceMgrTest>()), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetSetUpCaseOrSuite("iface_mgr_unittest.cc" , 3429), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("iface_mgr_unittest.cc", 3429), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DISABLED_getSocket_Test >); void IfaceMgrTest_DISABLED_getSocket_Test::TestBody() { | |||
3430 | // Testing socket operation in a portable way is tricky | |||
3431 | // without interface detection implemented. | |||
3432 | ||||
3433 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3434 | ||||
3435 | IOAddress lo_addr("::1"); | |||
3436 | IOAddress link_local("fe80::1"); | |||
3437 | IOAddress global("2001:db8:15c::1"); | |||
3438 | ||||
3439 | IOAddress dst_link_local("fe80::dead:beef"); | |||
3440 | IOAddress dst_global("2001:db8:15c::dead:beef"); | |||
3441 | ||||
3442 | // Bind loopback address | |||
3443 | int socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); | |||
3444 | EXPECT_GE(socket1, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket1", "0", socket1, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3444 , gtest_ar.failure_message()) = ::testing::Message(); // socket >= 0 | |||
3445 | ||||
3446 | // Bind link-local address | |||
3447 | int socket2 = ifacemgr->openSocket(LOOPBACK_NAME, link_local, 10547); | |||
3448 | EXPECT_GE(socket2, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket2", "0", socket2, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3448 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3449 | ||||
3450 | int socket3 = ifacemgr->openSocket(LOOPBACK_NAME, global, 10547); | |||
3451 | EXPECT_GE(socket3, 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("socket3", "0", socket3, 0))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3451 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3452 | ||||
3453 | // Let's make sure those sockets are unique | |||
3454 | EXPECT_NE(socket1, socket2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("socket1", "socket2" , socket1, socket2))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3454, gtest_ar.failure_message()) = ::testing::Message(); | |||
3455 | EXPECT_NE(socket2, socket3)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("socket2", "socket3" , socket2, socket3))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3455, gtest_ar.failure_message()) = ::testing::Message(); | |||
3456 | EXPECT_NE(socket3, socket1)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("socket3", "socket1" , socket3, socket1))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc" , 3456, gtest_ar.failure_message()) = ::testing::Message(); | |||
3457 | ||||
3458 | // Create a packet | |||
3459 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, 123)); | |||
3460 | pkt6->setIface(LOOPBACK_NAME); | |||
3461 | pkt6->setIndex(LOOPBACK_INDEX); | |||
3462 | ||||
3463 | // Check that packets sent to link-local will get socket bound to link local | |||
3464 | pkt6->setLocalAddr(global); | |||
3465 | pkt6->setRemoteAddr(dst_global); | |||
3466 | EXPECT_EQ(socket3, ifacemgr->getSocket(pkt6))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("socket3" , "ifacemgr->getSocket(pkt6)", socket3, ifacemgr->getSocket (pkt6)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3466 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3467 | ||||
3468 | // Check that packets sent to link-local will get socket bound to link local | |||
3469 | pkt6->setLocalAddr(link_local); | |||
3470 | pkt6->setRemoteAddr(dst_link_local); | |||
3471 | EXPECT_EQ(socket2, ifacemgr->getSocket(pkt6))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("socket2" , "ifacemgr->getSocket(pkt6)", socket2, ifacemgr->getSocket (pkt6)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "iface_mgr_unittest.cc", 3471 , gtest_ar.failure_message()) = ::testing::Message(); | |||
3472 | ||||
3473 | // Close sockets here because the following tests will want to | |||
3474 | // open sockets on the same ports. | |||
3475 | ifacemgr->closeSockets(); | |||
3476 | } | |||
3477 | ||||
3478 | // Verifies DHCPv4 behavior of configureDHCPPacketQueue() | |||
3479 | TEST_F(IfaceMgrTest, configureDHCPPacketQueueTest4)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("configureDHCPPacketQueueTest4") > 1, "test_name must not be empty"); class IfaceMgrTest_configureDHCPPacketQueueTest4_Test : public IfaceMgrTest { public: IfaceMgrTest_configureDHCPPacketQueueTest4_Test () = default; ~IfaceMgrTest_configureDHCPPacketQueueTest4_Test () override = default; IfaceMgrTest_configureDHCPPacketQueueTest4_Test (const IfaceMgrTest_configureDHCPPacketQueueTest4_Test & ) = delete; IfaceMgrTest_configureDHCPPacketQueueTest4_Test & operator=( const IfaceMgrTest_configureDHCPPacketQueueTest4_Test &) = delete; IfaceMgrTest_configureDHCPPacketQueueTest4_Test (IfaceMgrTest_configureDHCPPacketQueueTest4_Test &&) noexcept = delete; IfaceMgrTest_configureDHCPPacketQueueTest4_Test & operator=( IfaceMgrTest_configureDHCPPacketQueueTest4_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_configureDHCPPacketQueueTest4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "configureDHCPPacketQueueTest4", nullptr, nullptr, ::testing ::internal::CodeLocation("iface_mgr_unittest.cc", 3479), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 3479), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 3479), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_configureDHCPPacketQueueTest4_Test >); void IfaceMgrTest_configureDHCPPacketQueueTest4_Test:: TestBody() { | |||
3480 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3481 | ||||
3482 | // First let's make sure there is no queue and no thread. | |||
3483 | ASSERT_FALSE(ifacemgr->getPacketQueue4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue4 ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3483 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "true", "false") .c_str() ) = ::testing::Message(); | |||
3484 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3484 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3485 | ||||
3486 | bool queue_enabled = false; | |||
3487 | // Given an empty pointer, we should default to no queue. | |||
3488 | data::ConstElementPtr queue_control; | |||
3489 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3489; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3489 ; } } else gtest_label_testnothrow_3489 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3489, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3490 | EXPECT_FALSE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(queue_enabled))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3490, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "true", "false") .c_str()) = ::testing::Message(); | |||
3491 | EXPECT_FALSE(ifacemgr->getPacketQueue4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue4 ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3491, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue4()" , "true", "false") .c_str()) = ::testing::Message(); | |||
3492 | // configureDHCPPacketQueue() should never start the thread. | |||
3493 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3493 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3494 | ||||
3495 | // Verify that calling startDHCPReceiver with no queue, does NOT start the thread. | |||
3496 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(2); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3496 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3496 ; } } else gtest_label_testnothrow_3496 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3496, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3497 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3497 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3498 | ||||
3499 | // Now let's try with a populated queue control, but with enable-queue = false. | |||
3500 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, false); | |||
3501 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3501; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3501 ; } } else gtest_label_testnothrow_3501 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3501, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3502 | EXPECT_FALSE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(queue_enabled))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3502, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "true", "false") .c_str()) = ::testing::Message(); | |||
3503 | EXPECT_FALSE(ifacemgr->getPacketQueue4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue4 ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3503, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue4()" , "true", "false") .c_str()) = ::testing::Message(); | |||
3504 | // configureDHCPPacketQueue() should never start the thread. | |||
3505 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3505 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3506 | ||||
3507 | // Now let's enable the queue. | |||
3508 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, true); | |||
3509 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3509; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3509 ; } } else gtest_label_testnothrow_3509 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3509, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3510 | ASSERT_TRUE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(queue_enabled)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3510, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "false", "true") .c_str()) = ::testing::Message(); | |||
3511 | // Verify we have correctly created the queue. | |||
3512 | CHECK_QUEUE_INFO(ifacemgr->getPacketQueue4(), "{ \"capacity\": 500, \"queue-type\": \""{ std::ostringstream oss__; oss__ << "{ \"capacity\": 500, \"queue-type\": \"" << PacketQueueMgr4::DEFAULT_QUEUE_TYPE4 << "\", \"size\": 0 }" ; checkInfo(ifacemgr->getPacketQueue4(), oss__.str().c_str ()); } | |||
3513 | << PacketQueueMgr4::DEFAULT_QUEUE_TYPE4 << "\", \"size\": 0 }"){ std::ostringstream oss__; oss__ << "{ \"capacity\": 500, \"queue-type\": \"" << PacketQueueMgr4::DEFAULT_QUEUE_TYPE4 << "\", \"size\": 0 }" ; checkInfo(ifacemgr->getPacketQueue4(), oss__.str().c_str ()); }; | |||
3514 | // configureDHCPPacketQueue() should never start the thread. | |||
3515 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3515 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3516 | ||||
3517 | // Calling startDHCPReceiver with a queue, should start the thread. | |||
3518 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(2); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3518 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3518 ; } } else gtest_label_testnothrow_3518 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3518, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3519 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3519 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
3520 | ||||
3521 | // Verify that calling startDHCPReceiver when the thread is running, throws. | |||
3522 | ASSERT_THROW(ifacemgr->startDHCPReceiver(AF_INET), InvalidOperation)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver (2); } else static_assert(true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< InvalidOperation>:: type>::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->startDHCPReceiver(2)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_3522; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(2)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_3522; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(2)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_3522; } } else gtest_label_testthrow_3522 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3522, gtest_msg.value .c_str()) = ::testing::Message(); | |||
3523 | ||||
3524 | // Create a disabled config. | |||
3525 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, false); | |||
3526 | ||||
3527 | // Trying to reconfigure with a running thread should throw. | |||
3528 | ASSERT_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, queue_control),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr ->configureDHCPPacketQueue(2, queue_control); } else static_assert (true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< InvalidOperation>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_3529 ; } catch (...) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_3529; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_3529; } } else gtest_label_testthrow_3529 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3529, gtest_msg.value .c_str()) = ::testing::Message() | |||
3529 | InvalidOperation)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr ->configureDHCPPacketQueue(2, queue_control); } else static_assert (true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< InvalidOperation>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_3529 ; } catch (...) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_3529; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_3529; } } else gtest_label_testthrow_3529 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3529, gtest_msg.value .c_str()) = ::testing::Message(); | |||
3530 | ||||
3531 | // We should still have our queue and the thread should still be running. | |||
3532 | EXPECT_TRUE(ifacemgr->getPacketQueue4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->getPacketQueue4 ())) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3532, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue4()" , "false", "true") .c_str()) = ::testing::Message(); | |||
3533 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3533 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
3534 | ||||
3535 | // Now let's stop stop the thread. | |||
3536 | ASSERT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3536 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3536 ; } } else gtest_label_testnothrow_3536 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3536, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3537 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3537 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3538 | // Stopping the thread should not destroy the queue. | |||
3539 | ASSERT_TRUE(ifacemgr->getPacketQueue4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->getPacketQueue4 ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3539 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "false", "true") .c_str() ) = ::testing::Message(); | |||
3540 | ||||
3541 | // Reconfigure with the queue turned off. We should have neither queue nor thread. | |||
3542 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3542; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3542 ; } } else gtest_label_testnothrow_3542 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3542, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3543 | EXPECT_FALSE(ifacemgr->getPacketQueue4())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue4 ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3543, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue4()" , "true", "false") .c_str()) = ::testing::Message(); | |||
3544 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3544 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3545 | } | |||
3546 | ||||
3547 | // Verifies DHCPv6 behavior of configureDHCPPacketQueue() | |||
3548 | TEST_F(IfaceMgrTest, configureDHCPPacketQueueTest6)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("configureDHCPPacketQueueTest6") > 1, "test_name must not be empty"); class IfaceMgrTest_configureDHCPPacketQueueTest6_Test : public IfaceMgrTest { public: IfaceMgrTest_configureDHCPPacketQueueTest6_Test () = default; ~IfaceMgrTest_configureDHCPPacketQueueTest6_Test () override = default; IfaceMgrTest_configureDHCPPacketQueueTest6_Test (const IfaceMgrTest_configureDHCPPacketQueueTest6_Test & ) = delete; IfaceMgrTest_configureDHCPPacketQueueTest6_Test & operator=( const IfaceMgrTest_configureDHCPPacketQueueTest6_Test &) = delete; IfaceMgrTest_configureDHCPPacketQueueTest6_Test (IfaceMgrTest_configureDHCPPacketQueueTest6_Test &&) noexcept = delete; IfaceMgrTest_configureDHCPPacketQueueTest6_Test & operator=( IfaceMgrTest_configureDHCPPacketQueueTest6_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const IfaceMgrTest_configureDHCPPacketQueueTest6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "configureDHCPPacketQueueTest6", nullptr, nullptr, ::testing ::internal::CodeLocation("iface_mgr_unittest.cc", 3548), (::testing ::internal::GetTypeId<IfaceMgrTest>()), ::testing::internal ::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite( "iface_mgr_unittest.cc", 3548), ::testing::internal::SuiteApiResolver < IfaceMgrTest>::GetTearDownCaseOrSuite("iface_mgr_unittest.cc" , 3548), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_configureDHCPPacketQueueTest6_Test >); void IfaceMgrTest_configureDHCPPacketQueueTest6_Test:: TestBody() { | |||
3549 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
3550 | ||||
3551 | // First let's make sure there is no queue and no thread. | |||
3552 | ASSERT_FALSE(ifacemgr->getPacketQueue6())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue6 ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3552 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "true", "false") .c_str() ) = ::testing::Message(); | |||
3553 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3553 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3554 | ||||
3555 | bool queue_enabled = false; | |||
3556 | // Given an empty pointer, we should default to no queue. | |||
3557 | data::ConstElementPtr queue_control; | |||
3558 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3558; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3558 ; } } else gtest_label_testnothrow_3558 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3558, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3559 | EXPECT_FALSE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(queue_enabled))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3559, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "true", "false") .c_str()) = ::testing::Message(); | |||
3560 | EXPECT_FALSE(ifacemgr->getPacketQueue6())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue6 ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3560, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue6()" , "true", "false") .c_str()) = ::testing::Message(); | |||
3561 | // configureDHCPPacketQueue() should never start the thread. | |||
3562 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3562 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3563 | ||||
3564 | // Verify that calling startDHCPReceiver with no queue, does NOT start the thread. | |||
3565 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(2); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3565 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3565 ; } } else gtest_label_testnothrow_3565 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3565, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3566 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3566 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3567 | ||||
3568 | // Now let's try with a populated queue control, but with enable-queue = false. | |||
3569 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, false); | |||
3570 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET6, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3570; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3570 ; } } else gtest_label_testnothrow_3570 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3570, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3571 | EXPECT_FALSE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(queue_enabled))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "iface_mgr_unittest.cc", 3571, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "true", "false") .c_str()) = ::testing::Message(); | |||
3572 | EXPECT_FALSE(ifacemgr->getPacketQueue6())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue6 ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3572, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue6()" , "true", "false") .c_str()) = ::testing::Message(); | |||
3573 | // configureDHCPPacketQueue() should never start the thread. | |||
3574 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3574 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3575 | ||||
3576 | // Now let's enable the queue. | |||
3577 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, true); | |||
3578 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET6, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3578; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3578 ; } } else gtest_label_testnothrow_3578 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3578, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3579 | ASSERT_TRUE(queue_enabled)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(queue_enabled)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3579, ::testing::internal ::GetBoolAssertionFailureMessage( gtest_ar_, "queue_enabled", "false", "true") .c_str()) = ::testing::Message(); | |||
3580 | // Verify we have correctly created the queue. | |||
3581 | CHECK_QUEUE_INFO(ifacemgr->getPacketQueue6(), "{ \"capacity\": 500, \"queue-type\": \""{ std::ostringstream oss__; oss__ << "{ \"capacity\": 500, \"queue-type\": \"" << PacketQueueMgr6::DEFAULT_QUEUE_TYPE6 << "\", \"size\": 0 }" ; checkInfo(ifacemgr->getPacketQueue6(), oss__.str().c_str ()); } | |||
3582 | << PacketQueueMgr6::DEFAULT_QUEUE_TYPE6 << "\", \"size\": 0 }"){ std::ostringstream oss__; oss__ << "{ \"capacity\": 500, \"queue-type\": \"" << PacketQueueMgr6::DEFAULT_QUEUE_TYPE6 << "\", \"size\": 0 }" ; checkInfo(ifacemgr->getPacketQueue6(), oss__.str().c_str ()); }; | |||
3583 | // configureDHCPPacketQueue() should never start the thread. | |||
3584 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3584 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3585 | ||||
3586 | // Calling startDHCPReceiver with a queue, should start the thread. | |||
3587 | ASSERT_NO_THROW(ifacemgr->startDHCPReceiver(AF_INET6))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver(10); } else static_assert(true , ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName (typeid(e)); gtest_msg.value += " with description \""; gtest_msg .value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3587 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3587 ; } } else gtest_label_testnothrow_3587 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3587, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3588 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3588 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
3589 | ||||
3590 | // Verify that calling startDHCPReceiver when the thread is running, throws. | |||
3591 | ASSERT_THROW(ifacemgr->startDHCPReceiver(AF_INET6), InvalidOperation)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->startDHCPReceiver (10); } else static_assert(true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same<typename std::remove_cv <typename std::remove_reference< InvalidOperation>:: type>::type, std::exception>::value, const ::testing::internal ::NeverThrown&, const std::exception&>::type e) { gtest_msg .value = "Expected: " "ifacemgr->startDHCPReceiver(10)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testthrow_3591; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(10)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_3591; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->startDHCPReceiver(10)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_3591; } } else gtest_label_testthrow_3591 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3591, gtest_msg.value .c_str()) = ::testing::Message(); | |||
3592 | ||||
3593 | // Create a disabled config. | |||
3594 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, false); | |||
3595 | ||||
3596 | // Trying to reconfigure with a running thread should throw. | |||
3597 | ASSERT_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET6, queue_control),switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr ->configureDHCPPacketQueue(10, queue_control); } else static_assert (true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< InvalidOperation>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_3598 ; } catch (...) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_3598; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_3598; } } else gtest_label_testthrow_3598 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3598, gtest_msg.value .c_str()) = ::testing::Message() | |||
3598 | InvalidOperation)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr ->configureDHCPPacketQueue(10, queue_control); } else static_assert (true, ""); } catch (InvalidOperation const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< InvalidOperation>::type>::type, std::exception>::value , const ::testing::internal::NeverThrown&, const std::exception &>::type e) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_3598 ; } catch (...) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_3598; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " throws an exception of type " "InvalidOperation" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_3598; } } else gtest_label_testthrow_3598 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "iface_mgr_unittest.cc", 3598, gtest_msg.value .c_str()) = ::testing::Message(); | |||
3599 | ||||
3600 | // We should still have our queue and the thread should still be running. | |||
3601 | EXPECT_TRUE(ifacemgr->getPacketQueue6())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->getPacketQueue6 ())) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3601, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue6()" , "false", "true") .c_str()) = ::testing::Message(); | |||
3602 | ASSERT_TRUE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isDHCPReceiverRunning ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3602 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
3603 | ||||
3604 | // Now let's stop stop the thread. | |||
3605 | ASSERT_NO_THROW(ifacemgr->stopDHCPReceiver())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->stopDHCPReceiver(); } else static_assert(true, "" ); } catch (std::exception const& e) { gtest_msg.value = "it throws " ; gtest_msg.value += ::testing::internal::GetTypeName(typeid( e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testnothrow_3605 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3605 ; } } else gtest_label_testnothrow_3605 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3605, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
3606 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3606 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3607 | // Stopping the thread should not destroy the queue. | |||
3608 | ASSERT_TRUE(ifacemgr->getPacketQueue6())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->getPacketQueue6 ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3608 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "false", "true") .c_str() ) = ::testing::Message(); | |||
3609 | ||||
3610 | // Reconfigure with the queue turned off. We should have neither queue nor thread. | |||
3611 | ASSERT_NO_THROW(queue_enabled = ifacemgr->configureDHCPPacketQueue(AF_INET6, queue_control))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control ); } else static_assert(true, ""); } catch (std::exception const & e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_3611; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3611 ; } } else gtest_label_testnothrow_3611 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc" , 3611, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
3612 | EXPECT_FALSE(ifacemgr->getPacketQueue6())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->getPacketQueue6 ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "iface_mgr_unittest.cc", 3612, ::testing:: internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr->getPacketQueue6()" , "true", "false") .c_str()) = ::testing::Message(); | |||
3613 | ASSERT_FALSE(ifacemgr->isDHCPReceiverRunning())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isDHCPReceiverRunning ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "iface_mgr_unittest.cc", 3613 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
3614 | } | |||
3615 | ||||
3616 | } |
1 | // Components for manipulating sequences of characters -*- C++ -*- |
2 | |
3 | // Copyright (C) 1997-2024 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file bits/basic_string.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{string} |
28 | */ |
29 | |
30 | // |
31 | // ISO C++ 14882: 21 Strings library |
32 | // |
33 | |
34 | #ifndef _BASIC_STRING_H1 |
35 | #define _BASIC_STRING_H1 1 |
36 | |
37 | #pragma GCC system_header |
38 | |
39 | #include <ext/alloc_traits.h> |
40 | #include <debug/debug.h> |
41 | |
42 | #if __cplusplus202002L >= 201103L |
43 | #include <initializer_list> |
44 | #endif |
45 | |
46 | #if __cplusplus202002L >= 201703L |
47 | # include <string_view> |
48 | #endif |
49 | |
50 | #if __cplusplus202002L > 202302L |
51 | # include <charconv> |
52 | #endif |
53 | |
54 | #include <bits/version.h> |
55 | |
56 | #if ! _GLIBCXX_USE_CXX11_ABI1 |
57 | # include "cow_string.h" |
58 | #else |
59 | |
60 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
61 | { |
62 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
63 | _GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 { |
64 | |
65 | /** |
66 | * @class basic_string basic_string.h <string> |
67 | * @brief Managing sequences of characters and character-like objects. |
68 | * |
69 | * @ingroup strings |
70 | * @ingroup sequences |
71 | * @headerfile string |
72 | * @since C++98 |
73 | * |
74 | * @tparam _CharT Type of character |
75 | * @tparam _Traits Traits for character type, defaults to |
76 | * char_traits<_CharT>. |
77 | * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. |
78 | * |
79 | * Meets the requirements of a <a href="tables.html#65">container</a>, a |
80 | * <a href="tables.html#66">reversible container</a>, and a |
81 | * <a href="tables.html#67">sequence</a>. Of the |
82 | * <a href="tables.html#68">optional sequence requirements</a>, only |
83 | * @c push_back, @c at, and @c %array access are supported. |
84 | */ |
85 | template<typename _CharT, typename _Traits, typename _Alloc> |
86 | class basic_string |
87 | { |
88 | #if __cplusplus202002L >= 202002L |
89 | static_assert(is_same_v<_CharT, typename _Traits::char_type>); |
90 | static_assert(is_same_v<_CharT, typename _Alloc::value_type>); |
91 | using _Char_alloc_type = _Alloc; |
92 | #else |
93 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template |
94 | rebind<_CharT>::other _Char_alloc_type; |
95 | #endif |
96 | |
97 | typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; |
98 | |
99 | // Types: |
100 | public: |
101 | typedef _Traits traits_type; |
102 | typedef typename _Traits::char_type value_type; |
103 | typedef _Char_alloc_type allocator_type; |
104 | typedef typename _Alloc_traits::size_type size_type; |
105 | typedef typename _Alloc_traits::difference_type difference_type; |
106 | typedef typename _Alloc_traits::reference reference; |
107 | typedef typename _Alloc_traits::const_reference const_reference; |
108 | typedef typename _Alloc_traits::pointer pointer; |
109 | typedef typename _Alloc_traits::const_pointer const_pointer; |
110 | typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; |
111 | typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> |
112 | const_iterator; |
113 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
114 | typedef std::reverse_iterator<iterator> reverse_iterator; |
115 | |
116 | /// Value returned by various member functions when they fail. |
117 | static const size_type npos = static_cast<size_type>(-1); |
118 | |
119 | protected: |
120 | // type used for positions in insert, erase etc. |
121 | #if __cplusplus202002L < 201103L |
122 | typedef iterator __const_iterator; |
123 | #else |
124 | typedef const_iterator __const_iterator; |
125 | #endif |
126 | |
127 | private: |
128 | static _GLIBCXX20_CONSTEXPRconstexpr pointer |
129 | _S_allocate(_Char_alloc_type& __a, size_type __n) |
130 | { |
131 | pointer __p = _Alloc_traits::allocate(__a, __n); |
132 | #if __glibcxx_constexpr_string201907L >= 201907L |
133 | // std::char_traits begins the lifetime of characters, |
134 | // but custom traits might not, so do it here. |
135 | if constexpr (!is_same_v<_Traits, char_traits<_CharT>>) |
136 | if (std::__is_constant_evaluated()) |
137 | // Begin the lifetime of characters in allocated storage. |
138 | for (size_type __i = 0; __i < __n; ++__i) |
139 | std::construct_at(__builtin_addressof(__p[__i])); |
140 | #endif |
141 | return __p; |
142 | } |
143 | |
144 | #if __cplusplus202002L >= 201703L |
145 | // A helper type for avoiding boiler-plate. |
146 | typedef basic_string_view<_CharT, _Traits> __sv_type; |
147 | |
148 | template<typename _Tp, typename _Res> |
149 | using _If_sv = enable_if_t< |
150 | __and_<is_convertible<const _Tp&, __sv_type>, |
151 | __not_<is_convertible<const _Tp*, const basic_string*>>, |
152 | __not_<is_convertible<const _Tp&, const _CharT*>>>::value, |
153 | _Res>; |
154 | |
155 | // Allows an implicit conversion to __sv_type. |
156 | _GLIBCXX20_CONSTEXPRconstexpr |
157 | static __sv_type |
158 | _S_to_string_view(__sv_type __svt) noexcept |
159 | { return __svt; } |
160 | |
161 | // Wraps a string_view by explicit conversion and thus |
162 | // allows to add an internal constructor that does not |
163 | // participate in overload resolution when a string_view |
164 | // is provided. |
165 | struct __sv_wrapper |
166 | { |
167 | _GLIBCXX20_CONSTEXPRconstexpr explicit |
168 | __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { } |
169 | |
170 | __sv_type _M_sv; |
171 | }; |
172 | |
173 | /** |
174 | * @brief Only internally used: Construct string from a string view |
175 | * wrapper. |
176 | * @param __svw string view wrapper. |
177 | * @param __a Allocator to use. |
178 | */ |
179 | _GLIBCXX20_CONSTEXPRconstexpr |
180 | explicit |
181 | basic_string(__sv_wrapper __svw, const _Alloc& __a) |
182 | : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { } |
183 | #endif |
184 | |
185 | // Use empty-base optimization: http://www.cantrip.org/emptyopt.html |
186 | struct _Alloc_hider : allocator_type // TODO check __is_final |
187 | { |
188 | #if __cplusplus202002L < 201103L |
189 | _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc()) |
190 | : allocator_type(__a), _M_p(__dat) { } |
191 | #else |
192 | _GLIBCXX20_CONSTEXPRconstexpr |
193 | _Alloc_hider(pointer __dat, const _Alloc& __a) |
194 | : allocator_type(__a), _M_p(__dat) { } |
195 | |
196 | _GLIBCXX20_CONSTEXPRconstexpr |
197 | _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc()) |
198 | : allocator_type(std::move(__a)), _M_p(__dat) { } |
199 | #endif |
200 | |
201 | pointer _M_p; // The actual data. |
202 | }; |
203 | |
204 | _Alloc_hider _M_dataplus; |
205 | size_type _M_string_length; |
206 | |
207 | enum { _S_local_capacity = 15 / sizeof(_CharT) }; |
208 | |
209 | union |
210 | { |
211 | _CharT _M_local_buf[_S_local_capacity + 1]; |
212 | size_type _M_allocated_capacity; |
213 | }; |
214 | |
215 | _GLIBCXX20_CONSTEXPRconstexpr |
216 | void |
217 | _M_data(pointer __p) |
218 | { _M_dataplus._M_p = __p; } |
219 | |
220 | _GLIBCXX20_CONSTEXPRconstexpr |
221 | void |
222 | _M_length(size_type __length) |
223 | { _M_string_length = __length; } |
224 | |
225 | _GLIBCXX20_CONSTEXPRconstexpr |
226 | pointer |
227 | _M_data() const |
228 | { return _M_dataplus._M_p; } |
229 | |
230 | _GLIBCXX20_CONSTEXPRconstexpr |
231 | pointer |
232 | _M_local_data() |
233 | { |
234 | #if __cplusplus202002L >= 201103L |
235 | return std::pointer_traits<pointer>::pointer_to(*_M_local_buf); |
236 | #else |
237 | return pointer(_M_local_buf); |
238 | #endif |
239 | } |
240 | |
241 | _GLIBCXX20_CONSTEXPRconstexpr |
242 | const_pointer |
243 | _M_local_data() const |
244 | { |
245 | #if __cplusplus202002L >= 201103L |
246 | return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf); |
247 | #else |
248 | return const_pointer(_M_local_buf); |
249 | #endif |
250 | } |
251 | |
252 | _GLIBCXX20_CONSTEXPRconstexpr |
253 | void |
254 | _M_capacity(size_type __capacity) |
255 | { _M_allocated_capacity = __capacity; } |
256 | |
257 | _GLIBCXX20_CONSTEXPRconstexpr |
258 | void |
259 | _M_set_length(size_type __n) |
260 | { |
261 | _M_length(__n); |
262 | traits_type::assign(_M_data()[__n], _CharT()); |
263 | } |
264 | |
265 | _GLIBCXX20_CONSTEXPRconstexpr |
266 | bool |
267 | _M_is_local() const |
268 | { |
269 | if (_M_data() == _M_local_data()) |
270 | { |
271 | if (_M_string_length > _S_local_capacity) |
272 | __builtin_unreachable(); |
273 | return true; |
274 | } |
275 | return false; |
276 | } |
277 | |
278 | // Create & Destroy |
279 | _GLIBCXX20_CONSTEXPRconstexpr |
280 | pointer |
281 | _M_create(size_type&, size_type); |
282 | |
283 | _GLIBCXX20_CONSTEXPRconstexpr |
284 | void |
285 | _M_dispose() |
286 | { |
287 | if (!_M_is_local()) |
288 | _M_destroy(_M_allocated_capacity); |
289 | } |
290 | |
291 | _GLIBCXX20_CONSTEXPRconstexpr |
292 | void |
293 | _M_destroy(size_type __size) throw() |
294 | { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); } |
295 | |
296 | #if __cplusplus202002L < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
297 | // _M_construct_aux is used to implement the 21.3.1 para 15 which |
298 | // requires special behaviour if _InIterator is an integral type |
299 | template<typename _InIterator> |
300 | void |
301 | _M_construct_aux(_InIterator __beg, _InIterator __end, |
302 | std::__false_type) |
303 | { |
304 | typedef typename iterator_traits<_InIterator>::iterator_category _Tag; |
305 | _M_construct(__beg, __end, _Tag()); |
306 | } |
307 | |
308 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
309 | // 438. Ambiguity in the "do the right thing" clause |
310 | template<typename _Integer> |
311 | void |
312 | _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type) |
313 | { _M_construct_aux_2(static_cast<size_type>(__beg), __end); } |
314 | |
315 | void |
316 | _M_construct_aux_2(size_type __req, _CharT __c) |
317 | { _M_construct(__req, __c); } |
318 | #endif |
319 | |
320 | // For Input Iterators, used in istreambuf_iterators, etc. |
321 | template<typename _InIterator> |
322 | _GLIBCXX20_CONSTEXPRconstexpr |
323 | void |
324 | _M_construct(_InIterator __beg, _InIterator __end, |
325 | std::input_iterator_tag); |
326 | |
327 | // For forward_iterators up to random_access_iterators, used for |
328 | // string::iterator, _CharT*, etc. |
329 | template<typename _FwdIterator> |
330 | _GLIBCXX20_CONSTEXPRconstexpr |
331 | void |
332 | _M_construct(_FwdIterator __beg, _FwdIterator __end, |
333 | std::forward_iterator_tag); |
334 | |
335 | _GLIBCXX20_CONSTEXPRconstexpr |
336 | void |
337 | _M_construct(size_type __req, _CharT __c); |
338 | |
339 | _GLIBCXX20_CONSTEXPRconstexpr |
340 | allocator_type& |
341 | _M_get_allocator() |
342 | { return _M_dataplus; } |
343 | |
344 | _GLIBCXX20_CONSTEXPRconstexpr |
345 | const allocator_type& |
346 | _M_get_allocator() const |
347 | { return _M_dataplus; } |
348 | |
349 | // Ensure that _M_local_buf is the active member of the union. |
350 | __attribute__((__always_inline__)) |
351 | _GLIBCXX14_CONSTEXPRconstexpr |
352 | void |
353 | _M_init_local_buf() _GLIBCXX_NOEXCEPTnoexcept |
354 | { |
355 | #if __glibcxx_is_constant_evaluated201811L |
356 | if (std::is_constant_evaluated()) |
357 | for (size_type __i = 0; __i <= _S_local_capacity; ++__i) |
358 | _M_local_buf[__i] = _CharT(); |
359 | #endif |
360 | } |
361 | |
362 | __attribute__((__always_inline__)) |
363 | _GLIBCXX14_CONSTEXPRconstexpr |
364 | pointer |
365 | _M_use_local_data() _GLIBCXX_NOEXCEPTnoexcept |
366 | { |
367 | #if __cpp_lib_is_constant_evaluated201811L |
368 | _M_init_local_buf(); |
369 | #endif |
370 | return _M_local_data(); |
371 | } |
372 | |
373 | private: |
374 | |
375 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST |
376 | // The explicit instantiations in misc-inst.cc require this due to |
377 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063 |
378 | template<typename _Tp, bool _Requires = |
379 | !__are_same<_Tp, _CharT*>::__value |
380 | && !__are_same<_Tp, const _CharT*>::__value |
381 | && !__are_same<_Tp, iterator>::__value |
382 | && !__are_same<_Tp, const_iterator>::__value> |
383 | struct __enable_if_not_native_iterator |
384 | { typedef basic_string& __type; }; |
385 | template<typename _Tp> |
386 | struct __enable_if_not_native_iterator<_Tp, false> { }; |
387 | #endif |
388 | |
389 | _GLIBCXX20_CONSTEXPRconstexpr |
390 | size_type |
391 | _M_check(size_type __pos, const char* __s) const |
392 | { |
393 | if (__pos > this->size()) |
394 | __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "("%s: __pos (which is %zu) > " "this->size() (which is %zu)" ) |
395 | "this->size() (which is %zu)")("%s: __pos (which is %zu) > " "this->size() (which is %zu)" ), |
396 | __s, __pos, this->size()); |
397 | return __pos; |
398 | } |
399 | |
400 | _GLIBCXX20_CONSTEXPRconstexpr |
401 | void |
402 | _M_check_length(size_type __n1, size_type __n2, const char* __s) const |
403 | { |
404 | if (this->max_size() - (this->size() - __n1) < __n2) |
405 | __throw_length_error(__N(__s)(__s)); |
406 | } |
407 | |
408 | |
409 | // NB: _M_limit doesn't check for a bad __pos value. |
410 | _GLIBCXX20_CONSTEXPRconstexpr |
411 | size_type |
412 | _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPTnoexcept |
413 | { |
414 | const bool __testoff = __off < this->size() - __pos; |
415 | return __testoff ? __off : this->size() - __pos; |
416 | } |
417 | |
418 | // True if _Rep and source do not overlap. |
419 | bool |
420 | _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept |
421 | { |
422 | return (less<const _CharT*>()(__s, _M_data()) |
423 | || less<const _CharT*>()(_M_data() + this->size(), __s)); |
424 | } |
425 | |
426 | // When __n = 1 way faster than the general multichar |
427 | // traits_type::copy/move/assign. |
428 | _GLIBCXX20_CONSTEXPRconstexpr |
429 | static void |
430 | _S_copy(_CharT* __d, const _CharT* __s, size_type __n) |
431 | { |
432 | if (__n == 1) |
433 | traits_type::assign(*__d, *__s); |
434 | else |
435 | traits_type::copy(__d, __s, __n); |
436 | } |
437 | |
438 | _GLIBCXX20_CONSTEXPRconstexpr |
439 | static void |
440 | _S_move(_CharT* __d, const _CharT* __s, size_type __n) |
441 | { |
442 | if (__n == 1) |
443 | traits_type::assign(*__d, *__s); |
444 | else |
445 | traits_type::move(__d, __s, __n); |
446 | } |
447 | |
448 | _GLIBCXX20_CONSTEXPRconstexpr |
449 | static void |
450 | _S_assign(_CharT* __d, size_type __n, _CharT __c) |
451 | { |
452 | if (__n == 1) |
453 | traits_type::assign(*__d, __c); |
454 | else |
455 | traits_type::assign(__d, __n, __c); |
456 | } |
457 | |
458 | // _S_copy_chars is a separate template to permit specialization |
459 | // to optimize for the common case of pointers as iterators. |
460 | template<class _Iterator> |
461 | _GLIBCXX20_CONSTEXPRconstexpr |
462 | static void |
463 | _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) |
464 | { |
465 | for (; __k1 != __k2; ++__k1, (void)++__p) |
466 | traits_type::assign(*__p, *__k1); // These types are off. |
467 | } |
468 | |
469 | _GLIBCXX20_CONSTEXPRconstexpr |
470 | static void |
471 | _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPTnoexcept |
472 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
473 | |
474 | _GLIBCXX20_CONSTEXPRconstexpr |
475 | static void |
476 | _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) |
477 | _GLIBCXX_NOEXCEPTnoexcept |
478 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
479 | |
480 | _GLIBCXX20_CONSTEXPRconstexpr |
481 | static void |
482 | _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPTnoexcept |
483 | { _S_copy(__p, __k1, __k2 - __k1); } |
484 | |
485 | _GLIBCXX20_CONSTEXPRconstexpr |
486 | static void |
487 | _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) |
488 | _GLIBCXX_NOEXCEPTnoexcept |
489 | { _S_copy(__p, __k1, __k2 - __k1); } |
490 | |
491 | _GLIBCXX20_CONSTEXPRconstexpr |
492 | static int |
493 | _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPTnoexcept |
494 | { |
495 | const difference_type __d = difference_type(__n1 - __n2); |
496 | |
497 | if (__d > __gnu_cxx::__numeric_traits<int>::__max) |
498 | return __gnu_cxx::__numeric_traits<int>::__max; |
499 | else if (__d < __gnu_cxx::__numeric_traits<int>::__min) |
500 | return __gnu_cxx::__numeric_traits<int>::__min; |
501 | else |
502 | return int(__d); |
503 | } |
504 | |
505 | _GLIBCXX20_CONSTEXPRconstexpr |
506 | void |
507 | _M_assign(const basic_string&); |
508 | |
509 | _GLIBCXX20_CONSTEXPRconstexpr |
510 | void |
511 | _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, |
512 | size_type __len2); |
513 | |
514 | _GLIBCXX20_CONSTEXPRconstexpr |
515 | void |
516 | _M_erase(size_type __pos, size_type __n); |
517 | |
518 | public: |
519 | // Construct/copy/destroy: |
520 | // NB: We overload ctors in some cases instead of using default |
521 | // arguments, per 17.4.4.4 para. 2 item 2. |
522 | |
523 | /** |
524 | * @brief Default constructor creates an empty string. |
525 | */ |
526 | _GLIBCXX20_CONSTEXPRconstexpr |
527 | basic_string() |
528 | _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)noexcept(is_nothrow_default_constructible<_Alloc>::value ) |
529 | : _M_dataplus(_M_local_data()) |
530 | { |
531 | _M_init_local_buf(); |
532 | _M_set_length(0); |
533 | } |
534 | |
535 | /** |
536 | * @brief Construct an empty string using allocator @a a. |
537 | */ |
538 | _GLIBCXX20_CONSTEXPRconstexpr |
539 | explicit |
540 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPTnoexcept |
541 | : _M_dataplus(_M_local_data(), __a) |
542 | { |
543 | _M_init_local_buf(); |
544 | _M_set_length(0); |
545 | } |
546 | |
547 | /** |
548 | * @brief Construct string with copy of value of @a __str. |
549 | * @param __str Source string. |
550 | */ |
551 | _GLIBCXX20_CONSTEXPRconstexpr |
552 | basic_string(const basic_string& __str) |
553 | : _M_dataplus(_M_local_data(), |
554 | _Alloc_traits::_S_select_on_copy(__str._M_get_allocator())) |
555 | { |
556 | _M_construct(__str._M_data(), __str._M_data() + __str.length(), |
557 | std::forward_iterator_tag()); |
558 | } |
559 | |
560 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
561 | // 2583. no way to supply an allocator for basic_string(str, pos) |
562 | /** |
563 | * @brief Construct string as copy of a substring. |
564 | * @param __str Source string. |
565 | * @param __pos Index of first character to copy from. |
566 | * @param __a Allocator to use. |
567 | */ |
568 | _GLIBCXX20_CONSTEXPRconstexpr |
569 | basic_string(const basic_string& __str, size_type __pos, |
570 | const _Alloc& __a = _Alloc()) |
571 | : _M_dataplus(_M_local_data(), __a) |
572 | { |
573 | const _CharT* __start = __str._M_data() |
574 | + __str._M_check(__pos, "basic_string::basic_string"); |
575 | _M_construct(__start, __start + __str._M_limit(__pos, npos), |
576 | std::forward_iterator_tag()); |
577 | } |
578 | |
579 | /** |
580 | * @brief Construct string as copy of a substring. |
581 | * @param __str Source string. |
582 | * @param __pos Index of first character to copy from. |
583 | * @param __n Number of characters to copy. |
584 | */ |
585 | _GLIBCXX20_CONSTEXPRconstexpr |
586 | basic_string(const basic_string& __str, size_type __pos, |
587 | size_type __n) |
588 | : _M_dataplus(_M_local_data()) |
589 | { |
590 | const _CharT* __start = __str._M_data() |
591 | + __str._M_check(__pos, "basic_string::basic_string"); |
592 | _M_construct(__start, __start + __str._M_limit(__pos, __n), |
593 | std::forward_iterator_tag()); |
594 | } |
595 | |
596 | /** |
597 | * @brief Construct string as copy of a substring. |
598 | * @param __str Source string. |
599 | * @param __pos Index of first character to copy from. |
600 | * @param __n Number of characters to copy. |
601 | * @param __a Allocator to use. |
602 | */ |
603 | _GLIBCXX20_CONSTEXPRconstexpr |
604 | basic_string(const basic_string& __str, size_type __pos, |
605 | size_type __n, const _Alloc& __a) |
606 | : _M_dataplus(_M_local_data(), __a) |
607 | { |
608 | const _CharT* __start |
609 | = __str._M_data() + __str._M_check(__pos, "string::string"); |
610 | _M_construct(__start, __start + __str._M_limit(__pos, __n), |
611 | std::forward_iterator_tag()); |
612 | } |
613 | |
614 | /** |
615 | * @brief Construct string initialized by a character %array. |
616 | * @param __s Source character %array. |
617 | * @param __n Number of characters to copy. |
618 | * @param __a Allocator to use (default is default allocator). |
619 | * |
620 | * NB: @a __s must have at least @a __n characters, '\\0' |
621 | * has no special meaning. |
622 | */ |
623 | _GLIBCXX20_CONSTEXPRconstexpr |
624 | basic_string(const _CharT* __s, size_type __n, |
625 | const _Alloc& __a = _Alloc()) |
626 | : _M_dataplus(_M_local_data(), __a) |
627 | { |
628 | // NB: Not required, but considered best practice. |
629 | if (__s == 0 && __n > 0) |
630 | std::__throw_logic_error(__N("basic_string: "("basic_string: " "construction from null is not valid") |
631 | "construction from null is not valid")("basic_string: " "construction from null is not valid")); |
632 | _M_construct(__s, __s + __n, std::forward_iterator_tag()); |
633 | } |
634 | |
635 | /** |
636 | * @brief Construct string as copy of a C string. |
637 | * @param __s Source C string. |
638 | * @param __a Allocator to use (default is default allocator). |
639 | */ |
640 | #if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
641 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
642 | // 3076. basic_string CTAD ambiguity |
643 | template<typename = _RequireAllocator<_Alloc>> |
644 | #endif |
645 | _GLIBCXX20_CONSTEXPRconstexpr |
646 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) |
647 | : _M_dataplus(_M_local_data(), __a) |
648 | { |
649 | // NB: Not required, but considered best practice. |
650 | if (__s == 0) |
651 | std::__throw_logic_error(__N("basic_string: "("basic_string: " "construction from null is not valid") |
652 | "construction from null is not valid")("basic_string: " "construction from null is not valid")); |
653 | const _CharT* __end = __s + traits_type::length(__s); |
654 | _M_construct(__s, __end, forward_iterator_tag()); |
655 | } |
656 | |
657 | /** |
658 | * @brief Construct string as multiple characters. |
659 | * @param __n Number of characters. |
660 | * @param __c Character to use. |
661 | * @param __a Allocator to use (default is default allocator). |
662 | */ |
663 | #if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
664 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
665 | // 3076. basic_string CTAD ambiguity |
666 | template<typename = _RequireAllocator<_Alloc>> |
667 | #endif |
668 | _GLIBCXX20_CONSTEXPRconstexpr |
669 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) |
670 | : _M_dataplus(_M_local_data(), __a) |
671 | { _M_construct(__n, __c); } |
672 | |
673 | #if __cplusplus202002L >= 201103L |
674 | /** |
675 | * @brief Move construct string. |
676 | * @param __str Source string. |
677 | * |
678 | * The newly-created string contains the exact contents of @a __str. |
679 | * @a __str is a valid, but unspecified string. |
680 | */ |
681 | _GLIBCXX20_CONSTEXPRconstexpr |
682 | basic_string(basic_string&& __str) noexcept |
683 | : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) |
684 | { |
685 | if (__str._M_is_local()) |
686 | { |
687 | _M_init_local_buf(); |
688 | traits_type::copy(_M_local_buf, __str._M_local_buf, |
689 | __str.length() + 1); |
690 | } |
691 | else |
692 | { |
693 | _M_data(__str._M_data()); |
694 | _M_capacity(__str._M_allocated_capacity); |
695 | } |
696 | |
697 | // Must use _M_length() here not _M_set_length() because |
698 | // basic_stringbuf relies on writing into unallocated capacity so |
699 | // we mess up the contents if we put a '\0' in the string. |
700 | _M_length(__str.length()); |
701 | __str._M_data(__str._M_use_local_data()); |
702 | __str._M_set_length(0); |
703 | } |
704 | |
705 | /** |
706 | * @brief Construct string from an initializer %list. |
707 | * @param __l std::initializer_list of characters. |
708 | * @param __a Allocator to use (default is default allocator). |
709 | */ |
710 | _GLIBCXX20_CONSTEXPRconstexpr |
711 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) |
712 | : _M_dataplus(_M_local_data(), __a) |
713 | { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); } |
714 | |
715 | _GLIBCXX20_CONSTEXPRconstexpr |
716 | basic_string(const basic_string& __str, const _Alloc& __a) |
717 | : _M_dataplus(_M_local_data(), __a) |
718 | { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); } |
719 | |
720 | _GLIBCXX20_CONSTEXPRconstexpr |
721 | basic_string(basic_string&& __str, const _Alloc& __a) |
722 | noexcept(_Alloc_traits::_S_always_equal()) |
723 | : _M_dataplus(_M_local_data(), __a) |
724 | { |
725 | if (__str._M_is_local()) |
726 | { |
727 | _M_init_local_buf(); |
728 | traits_type::copy(_M_local_buf, __str._M_local_buf, |
729 | __str.length() + 1); |
730 | _M_length(__str.length()); |
731 | __str._M_set_length(0); |
732 | } |
733 | else if (_Alloc_traits::_S_always_equal() |
734 | || __str.get_allocator() == __a) |
735 | { |
736 | _M_data(__str._M_data()); |
737 | _M_length(__str.length()); |
738 | _M_capacity(__str._M_allocated_capacity); |
739 | __str._M_data(__str._M_use_local_data()); |
740 | __str._M_set_length(0); |
741 | } |
742 | else |
743 | _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); |
744 | } |
745 | #endif // C++11 |
746 | |
747 | #if __cplusplus202002L >= 202100L |
748 | basic_string(nullptr_t) = delete; |
749 | basic_string& operator=(nullptr_t) = delete; |
750 | #endif // C++23 |
751 | |
752 | /** |
753 | * @brief Construct string as copy of a range. |
754 | * @param __beg Start of range. |
755 | * @param __end End of range. |
756 | * @param __a Allocator to use (default is default allocator). |
757 | */ |
758 | #if __cplusplus202002L >= 201103L |
759 | template<typename _InputIterator, |
760 | typename = std::_RequireInputIter<_InputIterator>> |
761 | #else |
762 | template<typename _InputIterator> |
763 | #endif |
764 | _GLIBCXX20_CONSTEXPRconstexpr |
765 | basic_string(_InputIterator __beg, _InputIterator __end, |
766 | const _Alloc& __a = _Alloc()) |
767 | : _M_dataplus(_M_local_data(), __a), _M_string_length(0) |
768 | { |
769 | #if __cplusplus202002L >= 201103L |
770 | _M_construct(__beg, __end, std::__iterator_category(__beg)); |
771 | #else |
772 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
773 | _M_construct_aux(__beg, __end, _Integral()); |
774 | #endif |
775 | } |
776 | |
777 | #if __cplusplus202002L >= 201703L |
778 | /** |
779 | * @brief Construct string from a substring of a string_view. |
780 | * @param __t Source object convertible to string view. |
781 | * @param __pos The index of the first character to copy from __t. |
782 | * @param __n The number of characters to copy from __t. |
783 | * @param __a Allocator to use. |
784 | */ |
785 | template<typename _Tp, |
786 | typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> |
787 | _GLIBCXX20_CONSTEXPRconstexpr |
788 | basic_string(const _Tp& __t, size_type __pos, size_type __n, |
789 | const _Alloc& __a = _Alloc()) |
790 | : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } |
791 | |
792 | /** |
793 | * @brief Construct string from a string_view. |
794 | * @param __t Source object convertible to string view. |
795 | * @param __a Allocator to use (default is default allocator). |
796 | */ |
797 | template<typename _Tp, typename = _If_sv<_Tp, void>> |
798 | _GLIBCXX20_CONSTEXPRconstexpr |
799 | explicit |
800 | basic_string(const _Tp& __t, const _Alloc& __a = _Alloc()) |
801 | : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { } |
802 | #endif // C++17 |
803 | |
804 | /** |
805 | * @brief Destroy the string instance. |
806 | */ |
807 | _GLIBCXX20_CONSTEXPRconstexpr |
808 | ~basic_string() |
809 | { _M_dispose(); } |
810 | |
811 | /** |
812 | * @brief Assign the value of @a str to this string. |
813 | * @param __str Source string. |
814 | */ |
815 | _GLIBCXX20_CONSTEXPRconstexpr |
816 | basic_string& |
817 | operator=(const basic_string& __str) |
818 | { |
819 | return this->assign(__str); |
820 | } |
821 | |
822 | /** |
823 | * @brief Copy contents of @a s into this string. |
824 | * @param __s Source null-terminated string. |
825 | */ |
826 | _GLIBCXX20_CONSTEXPRconstexpr |
827 | basic_string& |
828 | operator=(const _CharT* __s) |
829 | { return this->assign(__s); } |
830 | |
831 | /** |
832 | * @brief Set value to string of length 1. |
833 | * @param __c Source character. |
834 | * |
835 | * Assigning to a character makes this string length 1 and |
836 | * (*this)[0] == @a c. |
837 | */ |
838 | _GLIBCXX20_CONSTEXPRconstexpr |
839 | basic_string& |
840 | operator=(_CharT __c) |
841 | { |
842 | this->assign(1, __c); |
843 | return *this; |
844 | } |
845 | |
846 | #if __cplusplus202002L >= 201103L |
847 | /** |
848 | * @brief Move assign the value of @a str to this string. |
849 | * @param __str Source string. |
850 | * |
851 | * The contents of @a str are moved into this string (without copying). |
852 | * @a str is a valid, but unspecified string. |
853 | */ |
854 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
855 | // 2063. Contradictory requirements for string move assignment |
856 | _GLIBCXX20_CONSTEXPRconstexpr |
857 | basic_string& |
858 | operator=(basic_string&& __str) |
859 | noexcept(_Alloc_traits::_S_nothrow_move()) |
860 | { |
861 | const bool __equal_allocs = _Alloc_traits::_S_always_equal() |
862 | || _M_get_allocator() == __str._M_get_allocator(); |
863 | if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign() |
864 | && !__equal_allocs) |
865 | { |
866 | // Destroy existing storage before replacing allocator. |
867 | _M_destroy(_M_allocated_capacity); |
868 | _M_data(_M_local_data()); |
869 | _M_set_length(0); |
870 | } |
871 | // Replace allocator if POCMA is true. |
872 | std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator()); |
873 | |
874 | if (__str._M_is_local()) |
875 | { |
876 | // We've always got room for a short string, just copy it |
877 | // (unless this is a self-move, because that would violate the |
878 | // char_traits::copy precondition that the ranges don't overlap). |
879 | if (__builtin_expect(std::__addressof(__str) != this, true)) |
880 | { |
881 | if (__str.size()) |
882 | this->_S_copy(_M_data(), __str._M_data(), __str.size()); |
883 | _M_set_length(__str.size()); |
884 | } |
885 | } |
886 | else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs) |
887 | { |
888 | // Just move the allocated pointer, our allocator can free it. |
889 | pointer __data = nullptr; |
890 | size_type __capacity; |
891 | if (!_M_is_local()) |
892 | { |
893 | if (__equal_allocs) |
894 | { |
895 | // __str can reuse our existing storage. |
896 | __data = _M_data(); |
897 | __capacity = _M_allocated_capacity; |
898 | } |
899 | else // __str can't use it, so free it. |
900 | _M_destroy(_M_allocated_capacity); |
901 | } |
902 | |
903 | _M_data(__str._M_data()); |
904 | _M_length(__str.length()); |
905 | _M_capacity(__str._M_allocated_capacity); |
906 | if (__data) |
907 | { |
908 | __str._M_data(__data); |
909 | __str._M_capacity(__capacity); |
910 | } |
911 | else |
912 | __str._M_data(__str._M_use_local_data()); |
913 | } |
914 | else // Need to do a deep copy |
915 | _M_assign(__str); |
916 | __str.clear(); |
917 | return *this; |
918 | } |
919 | |
920 | /** |
921 | * @brief Set value to string constructed from initializer %list. |
922 | * @param __l std::initializer_list. |
923 | */ |
924 | _GLIBCXX20_CONSTEXPRconstexpr |
925 | basic_string& |
926 | operator=(initializer_list<_CharT> __l) |
927 | { |
928 | this->assign(__l.begin(), __l.size()); |
929 | return *this; |
930 | } |
931 | #endif // C++11 |
932 | |
933 | #if __cplusplus202002L >= 201703L |
934 | /** |
935 | * @brief Set value to string constructed from a string_view. |
936 | * @param __svt An object convertible to string_view. |
937 | */ |
938 | template<typename _Tp> |
939 | _GLIBCXX20_CONSTEXPRconstexpr |
940 | _If_sv<_Tp, basic_string&> |
941 | operator=(const _Tp& __svt) |
942 | { return this->assign(__svt); } |
943 | |
944 | /** |
945 | * @brief Convert to a string_view. |
946 | * @return A string_view. |
947 | */ |
948 | _GLIBCXX20_CONSTEXPRconstexpr |
949 | operator __sv_type() const noexcept |
950 | { return __sv_type(data(), size()); } |
951 | #endif // C++17 |
952 | |
953 | // Iterators: |
954 | /** |
955 | * Returns a read/write iterator that points to the first character in |
956 | * the %string. |
957 | */ |
958 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
959 | iterator |
960 | begin() _GLIBCXX_NOEXCEPTnoexcept |
961 | { return iterator(_M_data()); } |
962 | |
963 | /** |
964 | * Returns a read-only (constant) iterator that points to the first |
965 | * character in the %string. |
966 | */ |
967 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
968 | const_iterator |
969 | begin() const _GLIBCXX_NOEXCEPTnoexcept |
970 | { return const_iterator(_M_data()); } |
971 | |
972 | /** |
973 | * Returns a read/write iterator that points one past the last |
974 | * character in the %string. |
975 | */ |
976 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
977 | iterator |
978 | end() _GLIBCXX_NOEXCEPTnoexcept |
979 | { return iterator(_M_data() + this->size()); } |
980 | |
981 | /** |
982 | * Returns a read-only (constant) iterator that points one past the |
983 | * last character in the %string. |
984 | */ |
985 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
986 | const_iterator |
987 | end() const _GLIBCXX_NOEXCEPTnoexcept |
988 | { return const_iterator(_M_data() + this->size()); } |
989 | |
990 | /** |
991 | * Returns a read/write reverse iterator that points to the last |
992 | * character in the %string. Iteration is done in reverse element |
993 | * order. |
994 | */ |
995 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
996 | reverse_iterator |
997 | rbegin() _GLIBCXX_NOEXCEPTnoexcept |
998 | { return reverse_iterator(this->end()); } |
999 | |
1000 | /** |
1001 | * Returns a read-only (constant) reverse iterator that points |
1002 | * to the last character in the %string. Iteration is done in |
1003 | * reverse element order. |
1004 | */ |
1005 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1006 | const_reverse_iterator |
1007 | rbegin() const _GLIBCXX_NOEXCEPTnoexcept |
1008 | { return const_reverse_iterator(this->end()); } |
1009 | |
1010 | /** |
1011 | * Returns a read/write reverse iterator that points to one before the |
1012 | * first character in the %string. Iteration is done in reverse |
1013 | * element order. |
1014 | */ |
1015 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1016 | reverse_iterator |
1017 | rend() _GLIBCXX_NOEXCEPTnoexcept |
1018 | { return reverse_iterator(this->begin()); } |
1019 | |
1020 | /** |
1021 | * Returns a read-only (constant) reverse iterator that points |
1022 | * to one before the first character in the %string. Iteration |
1023 | * is done in reverse element order. |
1024 | */ |
1025 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1026 | const_reverse_iterator |
1027 | rend() const _GLIBCXX_NOEXCEPTnoexcept |
1028 | { return const_reverse_iterator(this->begin()); } |
1029 | |
1030 | #if __cplusplus202002L >= 201103L |
1031 | /** |
1032 | * Returns a read-only (constant) iterator that points to the first |
1033 | * character in the %string. |
1034 | */ |
1035 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1036 | const_iterator |
1037 | cbegin() const noexcept |
1038 | { return const_iterator(this->_M_data()); } |
1039 | |
1040 | /** |
1041 | * Returns a read-only (constant) iterator that points one past the |
1042 | * last character in the %string. |
1043 | */ |
1044 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1045 | const_iterator |
1046 | cend() const noexcept |
1047 | { return const_iterator(this->_M_data() + this->size()); } |
1048 | |
1049 | /** |
1050 | * Returns a read-only (constant) reverse iterator that points |
1051 | * to the last character in the %string. Iteration is done in |
1052 | * reverse element order. |
1053 | */ |
1054 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1055 | const_reverse_iterator |
1056 | crbegin() const noexcept |
1057 | { return const_reverse_iterator(this->end()); } |
1058 | |
1059 | /** |
1060 | * Returns a read-only (constant) reverse iterator that points |
1061 | * to one before the first character in the %string. Iteration |
1062 | * is done in reverse element order. |
1063 | */ |
1064 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1065 | const_reverse_iterator |
1066 | crend() const noexcept |
1067 | { return const_reverse_iterator(this->begin()); } |
1068 | #endif |
1069 | |
1070 | public: |
1071 | // Capacity: |
1072 | /// Returns the number of characters in the string, not including any |
1073 | /// null-termination. |
1074 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1075 | size_type |
1076 | size() const _GLIBCXX_NOEXCEPTnoexcept |
1077 | { return _M_string_length; } |
1078 | |
1079 | /// Returns the number of characters in the string, not including any |
1080 | /// null-termination. |
1081 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1082 | size_type |
1083 | length() const _GLIBCXX_NOEXCEPTnoexcept |
1084 | { return _M_string_length; } |
1085 | |
1086 | /// Returns the size() of the largest possible %string. |
1087 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1088 | size_type |
1089 | max_size() const _GLIBCXX_NOEXCEPTnoexcept |
1090 | { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; } |
1091 | |
1092 | /** |
1093 | * @brief Resizes the %string to the specified number of characters. |
1094 | * @param __n Number of characters the %string should contain. |
1095 | * @param __c Character to fill any new elements. |
1096 | * |
1097 | * This function will %resize the %string to the specified |
1098 | * number of characters. If the number is smaller than the |
1099 | * %string's current size the %string is truncated, otherwise |
1100 | * the %string is extended and new elements are %set to @a __c. |
1101 | */ |
1102 | _GLIBCXX20_CONSTEXPRconstexpr |
1103 | void |
1104 | resize(size_type __n, _CharT __c); |
1105 | |
1106 | /** |
1107 | * @brief Resizes the %string to the specified number of characters. |
1108 | * @param __n Number of characters the %string should contain. |
1109 | * |
1110 | * This function will resize the %string to the specified length. If |
1111 | * the new size is smaller than the %string's current size the %string |
1112 | * is truncated, otherwise the %string is extended and new characters |
1113 | * are default-constructed. For basic types such as char, this means |
1114 | * setting them to 0. |
1115 | */ |
1116 | _GLIBCXX20_CONSTEXPRconstexpr |
1117 | void |
1118 | resize(size_type __n) |
1119 | { this->resize(__n, _CharT()); } |
1120 | |
1121 | #if __cplusplus202002L >= 201103L |
1122 | #pragma GCC diagnostic push |
1123 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
1124 | /// A non-binding request to reduce capacity() to size(). |
1125 | _GLIBCXX20_CONSTEXPRconstexpr |
1126 | void |
1127 | shrink_to_fit() noexcept |
1128 | { reserve(); } |
1129 | #pragma GCC diagnostic pop |
1130 | #endif |
1131 | |
1132 | #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23 |
1133 | /** Resize the string and call a function to fill it. |
1134 | * |
1135 | * @param __n The maximum size requested. |
1136 | * @param __op A callable object that writes characters to the string. |
1137 | * |
1138 | * This is a low-level function that is easy to misuse, be careful. |
1139 | * |
1140 | * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n` |
1141 | * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`, |
1142 | * and finally set the string length to `n2` (adding a null terminator |
1143 | * at the end). The function object `op` is allowed to write to the |
1144 | * extra capacity added by the initial reserve operation, which is not |
1145 | * allowed if you just call `str.reserve(n)` yourself. |
1146 | * |
1147 | * This can be used to efficiently fill a `string` buffer without the |
1148 | * overhead of zero-initializing characters that will be overwritten |
1149 | * anyway. |
1150 | * |
1151 | * The callable `op` must not access the string directly (only through |
1152 | * the pointer passed as its first argument), must not write more than |
1153 | * `n` characters to the string, must return a value no greater than `n`, |
1154 | * and must ensure that all characters up to the returned length are |
1155 | * valid after it returns (i.e. there must be no uninitialized values |
1156 | * left in the string after the call, because accessing them would |
1157 | * have undefined behaviour). If `op` exits by throwing an exception |
1158 | * the behaviour is undefined. |
1159 | * |
1160 | * @since C++23 |
1161 | */ |
1162 | template<typename _Operation> |
1163 | constexpr void |
1164 | resize_and_overwrite(size_type __n, _Operation __op); |
1165 | #endif |
1166 | |
1167 | #if __cplusplus202002L >= 201103L |
1168 | /// Non-standard version of resize_and_overwrite for C++11 and above. |
1169 | template<typename _Operation> |
1170 | _GLIBCXX20_CONSTEXPRconstexpr void |
1171 | __resize_and_overwrite(size_type __n, _Operation __op); |
1172 | #endif |
1173 | |
1174 | /** |
1175 | * Returns the total number of characters that the %string can hold |
1176 | * before needing to allocate more memory. |
1177 | */ |
1178 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1179 | size_type |
1180 | capacity() const _GLIBCXX_NOEXCEPTnoexcept |
1181 | { |
1182 | return _M_is_local() ? size_type(_S_local_capacity) |
1183 | : _M_allocated_capacity; |
1184 | } |
1185 | |
1186 | /** |
1187 | * @brief Attempt to preallocate enough memory for specified number of |
1188 | * characters. |
1189 | * @param __res_arg Number of characters required. |
1190 | * @throw std::length_error If @a __res_arg exceeds @c max_size(). |
1191 | * |
1192 | * This function attempts to reserve enough memory for the |
1193 | * %string to hold the specified number of characters. If the |
1194 | * number requested is more than max_size(), length_error is |
1195 | * thrown. |
1196 | * |
1197 | * The advantage of this function is that if optimal code is a |
1198 | * necessity and the user can determine the string length that will be |
1199 | * required, the user can reserve the memory in %advance, and thus |
1200 | * prevent a possible reallocation of memory and copying of %string |
1201 | * data. |
1202 | */ |
1203 | _GLIBCXX20_CONSTEXPRconstexpr |
1204 | void |
1205 | reserve(size_type __res_arg); |
1206 | |
1207 | /** |
1208 | * Equivalent to shrink_to_fit(). |
1209 | */ |
1210 | #if __cplusplus202002L > 201703L |
1211 | [[deprecated("use shrink_to_fit() instead")]] |
1212 | #endif |
1213 | _GLIBCXX20_CONSTEXPRconstexpr |
1214 | void |
1215 | reserve(); |
1216 | |
1217 | /** |
1218 | * Erases the string, making it empty. |
1219 | */ |
1220 | _GLIBCXX20_CONSTEXPRconstexpr |
1221 | void |
1222 | clear() _GLIBCXX_NOEXCEPTnoexcept |
1223 | { _M_set_length(0); } |
1224 | |
1225 | /** |
1226 | * Returns true if the %string is empty. Equivalent to |
1227 | * <code>*this == ""</code>. |
1228 | */ |
1229 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1230 | bool |
1231 | empty() const _GLIBCXX_NOEXCEPTnoexcept |
1232 | { return this->size() == 0; } |
1233 | |
1234 | // Element access: |
1235 | /** |
1236 | * @brief Subscript access to the data contained in the %string. |
1237 | * @param __pos The index of the character to access. |
1238 | * @return Read-only (constant) reference to the character. |
1239 | * |
1240 | * This operator allows for easy, array-style, data access. |
1241 | * Note that data access with this operator is unchecked and |
1242 | * out_of_range lookups are not defined. (For checked lookups |
1243 | * see at().) |
1244 | */ |
1245 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1246 | const_reference |
1247 | operator[] (size_type __pos) const _GLIBCXX_NOEXCEPTnoexcept |
1248 | { |
1249 | __glibcxx_assert(__pos <= size())do { if (std::__is_constant_evaluated()) if (__builtin_expect (!bool(__pos <= size()), false)) std::__glibcxx_assert_fail (); } while (false); |
1250 | return _M_data()[__pos]; |
1251 | } |
1252 | |
1253 | /** |
1254 | * @brief Subscript access to the data contained in the %string. |
1255 | * @param __pos The index of the character to access. |
1256 | * @return Read/write reference to the character. |
1257 | * |
1258 | * This operator allows for easy, array-style, data access. |
1259 | * Note that data access with this operator is unchecked and |
1260 | * out_of_range lookups are not defined. (For checked lookups |
1261 | * see at().) |
1262 | */ |
1263 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1264 | reference |
1265 | operator[](size_type __pos) |
1266 | { |
1267 | // Allow pos == size() both in C++98 mode, as v3 extension, |
1268 | // and in C++11 mode. |
1269 | __glibcxx_assert(__pos <= size())do { if (std::__is_constant_evaluated()) if (__builtin_expect (!bool(__pos <= size()), false)) std::__glibcxx_assert_fail (); } while (false); |
1270 | // In pedantic mode be strict in C++98 mode. |
1271 | _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); |
1272 | return _M_data()[__pos]; |
1273 | } |
1274 | |
1275 | /** |
1276 | * @brief Provides access to the data contained in the %string. |
1277 | * @param __n The index of the character to access. |
1278 | * @return Read-only (const) reference to the character. |
1279 | * @throw std::out_of_range If @a n is an invalid index. |
1280 | * |
1281 | * This function provides for safer data access. The parameter is |
1282 | * first checked that it is in the range of the string. The function |
1283 | * throws out_of_range if the check fails. |
1284 | */ |
1285 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1286 | const_reference |
1287 | at(size_type __n) const |
1288 | { |
1289 | if (__n >= this->size()) |
1290 | __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
1291 | "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
1292 | "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)"), |
1293 | __n, this->size()); |
1294 | return _M_data()[__n]; |
1295 | } |
1296 | |
1297 | /** |
1298 | * @brief Provides access to the data contained in the %string. |
1299 | * @param __n The index of the character to access. |
1300 | * @return Read/write reference to the character. |
1301 | * @throw std::out_of_range If @a n is an invalid index. |
1302 | * |
1303 | * This function provides for safer data access. The parameter is |
1304 | * first checked that it is in the range of the string. The function |
1305 | * throws out_of_range if the check fails. |
1306 | */ |
1307 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1308 | reference |
1309 | at(size_type __n) |
1310 | { |
1311 | if (__n >= size()) |
1312 | __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
1313 | "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
1314 | "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)"), |
1315 | __n, this->size()); |
1316 | return _M_data()[__n]; |
1317 | } |
1318 | |
1319 | #if __cplusplus202002L >= 201103L |
1320 | /** |
1321 | * Returns a read/write reference to the data at the first |
1322 | * element of the %string. |
1323 | */ |
1324 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1325 | reference |
1326 | front() noexcept |
1327 | { |
1328 | __glibcxx_assert(!empty())do { if (std::__is_constant_evaluated()) if (__builtin_expect (!bool(!empty()), false)) std::__glibcxx_assert_fail(); } while (false); |
1329 | return operator[](0); |
1330 | } |
1331 | |
1332 | /** |
1333 | * Returns a read-only (constant) reference to the data at the first |
1334 | * element of the %string. |
1335 | */ |
1336 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1337 | const_reference |
1338 | front() const noexcept |
1339 | { |
1340 | __glibcxx_assert(!empty())do { if (std::__is_constant_evaluated()) if (__builtin_expect (!bool(!empty()), false)) std::__glibcxx_assert_fail(); } while (false); |
1341 | return operator[](0); |
1342 | } |
1343 | |
1344 | /** |
1345 | * Returns a read/write reference to the data at the last |
1346 | * element of the %string. |
1347 | */ |
1348 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1349 | reference |
1350 | back() noexcept |
1351 | { |
1352 | __glibcxx_assert(!empty())do { if (std::__is_constant_evaluated()) if (__builtin_expect (!bool(!empty()), false)) std::__glibcxx_assert_fail(); } while (false); |
1353 | return operator[](this->size() - 1); |
1354 | } |
1355 | |
1356 | /** |
1357 | * Returns a read-only (constant) reference to the data at the |
1358 | * last element of the %string. |
1359 | */ |
1360 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
1361 | const_reference |
1362 | back() const noexcept |
1363 | { |
1364 | __glibcxx_assert(!empty())do { if (std::__is_constant_evaluated()) if (__builtin_expect (!bool(!empty()), false)) std::__glibcxx_assert_fail(); } while (false); |
1365 | return operator[](this->size() - 1); |
1366 | } |
1367 | #endif |
1368 | |
1369 | // Modifiers: |
1370 | /** |
1371 | * @brief Append a string to this string. |
1372 | * @param __str The string to append. |
1373 | * @return Reference to this string. |
1374 | */ |
1375 | _GLIBCXX20_CONSTEXPRconstexpr |
1376 | basic_string& |
1377 | operator+=(const basic_string& __str) |
1378 | { return this->append(__str); } |
1379 | |
1380 | /** |
1381 | * @brief Append a C string. |
1382 | * @param __s The C string to append. |
1383 | * @return Reference to this string. |
1384 | */ |
1385 | _GLIBCXX20_CONSTEXPRconstexpr |
1386 | basic_string& |
1387 | operator+=(const _CharT* __s) |
1388 | { return this->append(__s); } |
1389 | |
1390 | /** |
1391 | * @brief Append a character. |
1392 | * @param __c The character to append. |
1393 | * @return Reference to this string. |
1394 | */ |
1395 | _GLIBCXX20_CONSTEXPRconstexpr |
1396 | basic_string& |
1397 | operator+=(_CharT __c) |
1398 | { |
1399 | this->push_back(__c); |
1400 | return *this; |
1401 | } |
1402 | |
1403 | #if __cplusplus202002L >= 201103L |
1404 | /** |
1405 | * @brief Append an initializer_list of characters. |
1406 | * @param __l The initializer_list of characters to be appended. |
1407 | * @return Reference to this string. |
1408 | */ |
1409 | _GLIBCXX20_CONSTEXPRconstexpr |
1410 | basic_string& |
1411 | operator+=(initializer_list<_CharT> __l) |
1412 | { return this->append(__l.begin(), __l.size()); } |
1413 | #endif // C++11 |
1414 | |
1415 | #if __cplusplus202002L >= 201703L |
1416 | /** |
1417 | * @brief Append a string_view. |
1418 | * @param __svt An object convertible to string_view to be appended. |
1419 | * @return Reference to this string. |
1420 | */ |
1421 | template<typename _Tp> |
1422 | _GLIBCXX20_CONSTEXPRconstexpr |
1423 | _If_sv<_Tp, basic_string&> |
1424 | operator+=(const _Tp& __svt) |
1425 | { return this->append(__svt); } |
1426 | #endif // C++17 |
1427 | |
1428 | /** |
1429 | * @brief Append a string to this string. |
1430 | * @param __str The string to append. |
1431 | * @return Reference to this string. |
1432 | */ |
1433 | _GLIBCXX20_CONSTEXPRconstexpr |
1434 | basic_string& |
1435 | append(const basic_string& __str) |
1436 | { return this->append(__str._M_data(), __str.size()); } |
1437 | |
1438 | /** |
1439 | * @brief Append a substring. |
1440 | * @param __str The string to append. |
1441 | * @param __pos Index of the first character of str to append. |
1442 | * @param __n The number of characters to append. |
1443 | * @return Reference to this string. |
1444 | * @throw std::out_of_range if @a __pos is not a valid index. |
1445 | * |
1446 | * This function appends @a __n characters from @a __str |
1447 | * starting at @a __pos to this string. If @a __n is is larger |
1448 | * than the number of available characters in @a __str, the |
1449 | * remainder of @a __str is appended. |
1450 | */ |
1451 | _GLIBCXX20_CONSTEXPRconstexpr |
1452 | basic_string& |
1453 | append(const basic_string& __str, size_type __pos, size_type __n = npos) |
1454 | { return this->append(__str._M_data() |
1455 | + __str._M_check(__pos, "basic_string::append"), |
1456 | __str._M_limit(__pos, __n)); } |
1457 | |
1458 | /** |
1459 | * @brief Append a C substring. |
1460 | * @param __s The C string to append. |
1461 | * @param __n The number of characters to append. |
1462 | * @return Reference to this string. |
1463 | */ |
1464 | _GLIBCXX20_CONSTEXPRconstexpr |
1465 | basic_string& |
1466 | append(const _CharT* __s, size_type __n) |
1467 | { |
1468 | __glibcxx_requires_string_len(__s, __n); |
1469 | _M_check_length(size_type(0), __n, "basic_string::append"); |
1470 | return _M_append(__s, __n); |
1471 | } |
1472 | |
1473 | /** |
1474 | * @brief Append a C string. |
1475 | * @param __s The C string to append. |
1476 | * @return Reference to this string. |
1477 | */ |
1478 | _GLIBCXX20_CONSTEXPRconstexpr |
1479 | basic_string& |
1480 | append(const _CharT* __s) |
1481 | { |
1482 | __glibcxx_requires_string(__s); |
1483 | const size_type __n = traits_type::length(__s); |
1484 | _M_check_length(size_type(0), __n, "basic_string::append"); |
1485 | return _M_append(__s, __n); |
1486 | } |
1487 | |
1488 | /** |
1489 | * @brief Append multiple characters. |
1490 | * @param __n The number of characters to append. |
1491 | * @param __c The character to use. |
1492 | * @return Reference to this string. |
1493 | * |
1494 | * Appends __n copies of __c to this string. |
1495 | */ |
1496 | _GLIBCXX20_CONSTEXPRconstexpr |
1497 | basic_string& |
1498 | append(size_type __n, _CharT __c) |
1499 | { return _M_replace_aux(this->size(), size_type(0), __n, __c); } |
1500 | |
1501 | #if __cplusplus202002L >= 201103L |
1502 | /** |
1503 | * @brief Append an initializer_list of characters. |
1504 | * @param __l The initializer_list of characters to append. |
1505 | * @return Reference to this string. |
1506 | */ |
1507 | _GLIBCXX20_CONSTEXPRconstexpr |
1508 | basic_string& |
1509 | append(initializer_list<_CharT> __l) |
1510 | { return this->append(__l.begin(), __l.size()); } |
1511 | #endif // C++11 |
1512 | |
1513 | /** |
1514 | * @brief Append a range of characters. |
1515 | * @param __first Iterator referencing the first character to append. |
1516 | * @param __last Iterator marking the end of the range. |
1517 | * @return Reference to this string. |
1518 | * |
1519 | * Appends characters in the range [__first,__last) to this string. |
1520 | */ |
1521 | #if __cplusplus202002L >= 201103L |
1522 | template<class _InputIterator, |
1523 | typename = std::_RequireInputIter<_InputIterator>> |
1524 | _GLIBCXX20_CONSTEXPRconstexpr |
1525 | #else |
1526 | template<class _InputIterator> |
1527 | #endif |
1528 | basic_string& |
1529 | append(_InputIterator __first, _InputIterator __last) |
1530 | { return this->replace(end(), end(), __first, __last); } |
1531 | |
1532 | #if __cplusplus202002L >= 201703L |
1533 | /** |
1534 | * @brief Append a string_view. |
1535 | * @param __svt An object convertible to string_view to be appended. |
1536 | * @return Reference to this string. |
1537 | */ |
1538 | template<typename _Tp> |
1539 | _GLIBCXX20_CONSTEXPRconstexpr |
1540 | _If_sv<_Tp, basic_string&> |
1541 | append(const _Tp& __svt) |
1542 | { |
1543 | __sv_type __sv = __svt; |
1544 | return this->append(__sv.data(), __sv.size()); |
1545 | } |
1546 | |
1547 | /** |
1548 | * @brief Append a range of characters from a string_view. |
1549 | * @param __svt An object convertible to string_view to be appended from. |
1550 | * @param __pos The position in the string_view to append from. |
1551 | * @param __n The number of characters to append from the string_view. |
1552 | * @return Reference to this string. |
1553 | */ |
1554 | template<typename _Tp> |
1555 | _GLIBCXX20_CONSTEXPRconstexpr |
1556 | _If_sv<_Tp, basic_string&> |
1557 | append(const _Tp& __svt, size_type __pos, size_type __n = npos) |
1558 | { |
1559 | __sv_type __sv = __svt; |
1560 | return _M_append(__sv.data() |
1561 | + std::__sv_check(__sv.size(), __pos, "basic_string::append"), |
1562 | std::__sv_limit(__sv.size(), __pos, __n)); |
1563 | } |
1564 | #endif // C++17 |
1565 | |
1566 | /** |
1567 | * @brief Append a single character. |
1568 | * @param __c Character to append. |
1569 | */ |
1570 | _GLIBCXX20_CONSTEXPRconstexpr |
1571 | void |
1572 | push_back(_CharT __c) |
1573 | { |
1574 | const size_type __size = this->size(); |
1575 | if (__size + 1 > this->capacity()) |
1576 | this->_M_mutate(__size, size_type(0), 0, size_type(1)); |
1577 | traits_type::assign(this->_M_data()[__size], __c); |
1578 | this->_M_set_length(__size + 1); |
1579 | } |
1580 | |
1581 | /** |
1582 | * @brief Set value to contents of another string. |
1583 | * @param __str Source string to use. |
1584 | * @return Reference to this string. |
1585 | */ |
1586 | _GLIBCXX20_CONSTEXPRconstexpr |
1587 | basic_string& |
1588 | assign(const basic_string& __str) |
1589 | { |
1590 | #if __cplusplus202002L >= 201103L |
1591 | if (_Alloc_traits::_S_propagate_on_copy_assign()) |
1592 | { |
1593 | if (!_Alloc_traits::_S_always_equal() && !_M_is_local() |
1594 | && _M_get_allocator() != __str._M_get_allocator()) |
1595 | { |
1596 | // Propagating allocator cannot free existing storage so must |
1597 | // deallocate it before replacing current allocator. |
1598 | if (__str.size() <= _S_local_capacity) |
1599 | { |
1600 | _M_destroy(_M_allocated_capacity); |
1601 | _M_data(_M_use_local_data()); |
1602 | _M_set_length(0); |
1603 | } |
1604 | else |
1605 | { |
1606 | const auto __len = __str.size(); |
1607 | auto __alloc = __str._M_get_allocator(); |
1608 | // If this allocation throws there are no effects: |
1609 | auto __ptr = _S_allocate(__alloc, __len + 1); |
1610 | _M_destroy(_M_allocated_capacity); |
1611 | _M_data(__ptr); |
1612 | _M_capacity(__len); |
1613 | _M_set_length(__len); |
1614 | } |
1615 | } |
1616 | std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator()); |
1617 | } |
1618 | #endif |
1619 | this->_M_assign(__str); |
1620 | return *this; |
1621 | } |
1622 | |
1623 | #if __cplusplus202002L >= 201103L |
1624 | /** |
1625 | * @brief Set value to contents of another string. |
1626 | * @param __str Source string to use. |
1627 | * @return Reference to this string. |
1628 | * |
1629 | * This function sets this string to the exact contents of @a __str. |
1630 | * @a __str is a valid, but unspecified string. |
1631 | */ |
1632 | _GLIBCXX20_CONSTEXPRconstexpr |
1633 | basic_string& |
1634 | assign(basic_string&& __str) |
1635 | noexcept(_Alloc_traits::_S_nothrow_move()) |
1636 | { |
1637 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1638 | // 2063. Contradictory requirements for string move assignment |
1639 | return *this = std::move(__str); |
1640 | } |
1641 | #endif // C++11 |
1642 | |
1643 | /** |
1644 | * @brief Set value to a substring of a string. |
1645 | * @param __str The string to use. |
1646 | * @param __pos Index of the first character of str. |
1647 | * @param __n Number of characters to use. |
1648 | * @return Reference to this string. |
1649 | * @throw std::out_of_range if @a pos is not a valid index. |
1650 | * |
1651 | * This function sets this string to the substring of @a __str |
1652 | * consisting of @a __n characters at @a __pos. If @a __n is |
1653 | * is larger than the number of available characters in @a |
1654 | * __str, the remainder of @a __str is used. |
1655 | */ |
1656 | _GLIBCXX20_CONSTEXPRconstexpr |
1657 | basic_string& |
1658 | assign(const basic_string& __str, size_type __pos, size_type __n = npos) |
1659 | { return _M_replace(size_type(0), this->size(), __str._M_data() |
1660 | + __str._M_check(__pos, "basic_string::assign"), |
1661 | __str._M_limit(__pos, __n)); } |
1662 | |
1663 | /** |
1664 | * @brief Set value to a C substring. |
1665 | * @param __s The C string to use. |
1666 | * @param __n Number of characters to use. |
1667 | * @return Reference to this string. |
1668 | * |
1669 | * This function sets the value of this string to the first @a __n |
1670 | * characters of @a __s. If @a __n is is larger than the number of |
1671 | * available characters in @a __s, the remainder of @a __s is used. |
1672 | */ |
1673 | _GLIBCXX20_CONSTEXPRconstexpr |
1674 | basic_string& |
1675 | assign(const _CharT* __s, size_type __n) |
1676 | { |
1677 | __glibcxx_requires_string_len(__s, __n); |
1678 | return _M_replace(size_type(0), this->size(), __s, __n); |
1679 | } |
1680 | |
1681 | /** |
1682 | * @brief Set value to contents of a C string. |
1683 | * @param __s The C string to use. |
1684 | * @return Reference to this string. |
1685 | * |
1686 | * This function sets the value of this string to the value of @a __s. |
1687 | * The data is copied, so there is no dependence on @a __s once the |
1688 | * function returns. |
1689 | */ |
1690 | _GLIBCXX20_CONSTEXPRconstexpr |
1691 | basic_string& |
1692 | assign(const _CharT* __s) |
1693 | { |
1694 | __glibcxx_requires_string(__s); |
1695 | return _M_replace(size_type(0), this->size(), __s, |
1696 | traits_type::length(__s)); |
1697 | } |
1698 | |
1699 | /** |
1700 | * @brief Set value to multiple characters. |
1701 | * @param __n Length of the resulting string. |
1702 | * @param __c The character to use. |
1703 | * @return Reference to this string. |
1704 | * |
1705 | * This function sets the value of this string to @a __n copies of |
1706 | * character @a __c. |
1707 | */ |
1708 | _GLIBCXX20_CONSTEXPRconstexpr |
1709 | basic_string& |
1710 | assign(size_type __n, _CharT __c) |
1711 | { return _M_replace_aux(size_type(0), this->size(), __n, __c); } |
1712 | |
1713 | /** |
1714 | * @brief Set value to a range of characters. |
1715 | * @param __first Iterator referencing the first character to append. |
1716 | * @param __last Iterator marking the end of the range. |
1717 | * @return Reference to this string. |
1718 | * |
1719 | * Sets value of string to characters in the range [__first,__last). |
1720 | */ |
1721 | #if __cplusplus202002L >= 201103L |
1722 | #pragma GCC diagnostic push |
1723 | #pragma GCC diagnostic ignored "-Wc++17-extensions" |
1724 | template<class _InputIterator, |
1725 | typename = std::_RequireInputIter<_InputIterator>> |
1726 | _GLIBCXX20_CONSTEXPRconstexpr |
1727 | basic_string& |
1728 | assign(_InputIterator __first, _InputIterator __last) |
1729 | { |
1730 | #if __cplusplus202002L >= 202002L |
1731 | if constexpr (contiguous_iterator<_InputIterator> |
1732 | && is_same_v<iter_value_t<_InputIterator>, _CharT>) |
1733 | #else |
1734 | if constexpr (__is_one_of<_InputIterator, const_iterator, iterator, |
1735 | const _CharT*, _CharT*>::value) |
1736 | #endif |
1737 | { |
1738 | __glibcxx_requires_valid_range(__first, __last); |
1739 | return _M_replace(size_type(0), size(), |
1740 | std::__to_address(__first), __last - __first); |
1741 | } |
1742 | else |
1743 | return *this = basic_string(__first, __last, get_allocator()); |
1744 | } |
1745 | #pragma GCC diagnostic pop |
1746 | #else |
1747 | template<class _InputIterator> |
1748 | basic_string& |
1749 | assign(_InputIterator __first, _InputIterator __last) |
1750 | { return this->replace(begin(), end(), __first, __last); } |
1751 | #endif |
1752 | |
1753 | #if __cplusplus202002L >= 201103L |
1754 | /** |
1755 | * @brief Set value to an initializer_list of characters. |
1756 | * @param __l The initializer_list of characters to assign. |
1757 | * @return Reference to this string. |
1758 | */ |
1759 | _GLIBCXX20_CONSTEXPRconstexpr |
1760 | basic_string& |
1761 | assign(initializer_list<_CharT> __l) |
1762 | { |
1763 | // The initializer_list array cannot alias the characters in *this |
1764 | // so we don't need to use replace to that case. |
1765 | const size_type __n = __l.size(); |
1766 | if (__n > capacity()) |
1767 | *this = basic_string(__l.begin(), __l.end(), get_allocator()); |
1768 | else |
1769 | { |
1770 | if (__n) |
1771 | _S_copy(_M_data(), __l.begin(), __n); |
1772 | _M_set_length(__n); |
1773 | } |
1774 | return *this; |
1775 | } |
1776 | #endif // C++11 |
1777 | |
1778 | #if __cplusplus202002L >= 201703L |
1779 | /** |
1780 | * @brief Set value from a string_view. |
1781 | * @param __svt The source object convertible to string_view. |
1782 | * @return Reference to this string. |
1783 | */ |
1784 | template<typename _Tp> |
1785 | _GLIBCXX20_CONSTEXPRconstexpr |
1786 | _If_sv<_Tp, basic_string&> |
1787 | assign(const _Tp& __svt) |
1788 | { |
1789 | __sv_type __sv = __svt; |
1790 | return this->assign(__sv.data(), __sv.size()); |
1791 | } |
1792 | |
1793 | /** |
1794 | * @brief Set value from a range of characters in a string_view. |
1795 | * @param __svt The source object convertible to string_view. |
1796 | * @param __pos The position in the string_view to assign from. |
1797 | * @param __n The number of characters to assign. |
1798 | * @return Reference to this string. |
1799 | */ |
1800 | template<typename _Tp> |
1801 | _GLIBCXX20_CONSTEXPRconstexpr |
1802 | _If_sv<_Tp, basic_string&> |
1803 | assign(const _Tp& __svt, size_type __pos, size_type __n = npos) |
1804 | { |
1805 | __sv_type __sv = __svt; |
1806 | return _M_replace(size_type(0), this->size(), |
1807 | __sv.data() |
1808 | + std::__sv_check(__sv.size(), __pos, "basic_string::assign"), |
1809 | std::__sv_limit(__sv.size(), __pos, __n)); |
1810 | } |
1811 | #endif // C++17 |
1812 | |
1813 | #if __cplusplus202002L >= 201103L |
1814 | /** |
1815 | * @brief Insert multiple characters. |
1816 | * @param __p Const_iterator referencing location in string to |
1817 | * insert at. |
1818 | * @param __n Number of characters to insert |
1819 | * @param __c The character to insert. |
1820 | * @return Iterator referencing the first inserted char. |
1821 | * @throw std::length_error If new length exceeds @c max_size(). |
1822 | * |
1823 | * Inserts @a __n copies of character @a __c starting at the |
1824 | * position referenced by iterator @a __p. If adding |
1825 | * characters causes the length to exceed max_size(), |
1826 | * length_error is thrown. The value of the string doesn't |
1827 | * change if an error is thrown. |
1828 | */ |
1829 | _GLIBCXX20_CONSTEXPRconstexpr |
1830 | iterator |
1831 | insert(const_iterator __p, size_type __n, _CharT __c) |
1832 | { |
1833 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
1834 | const size_type __pos = __p - begin(); |
1835 | this->replace(__p, __p, __n, __c); |
1836 | return iterator(this->_M_data() + __pos); |
1837 | } |
1838 | #else |
1839 | /** |
1840 | * @brief Insert multiple characters. |
1841 | * @param __p Iterator referencing location in string to insert at. |
1842 | * @param __n Number of characters to insert |
1843 | * @param __c The character to insert. |
1844 | * @throw std::length_error If new length exceeds @c max_size(). |
1845 | * |
1846 | * Inserts @a __n copies of character @a __c starting at the |
1847 | * position referenced by iterator @a __p. If adding |
1848 | * characters causes the length to exceed max_size(), |
1849 | * length_error is thrown. The value of the string doesn't |
1850 | * change if an error is thrown. |
1851 | */ |
1852 | void |
1853 | insert(iterator __p, size_type __n, _CharT __c) |
1854 | { this->replace(__p, __p, __n, __c); } |
1855 | #endif |
1856 | |
1857 | #if __cplusplus202002L >= 201103L |
1858 | /** |
1859 | * @brief Insert a range of characters. |
1860 | * @param __p Const_iterator referencing location in string to |
1861 | * insert at. |
1862 | * @param __beg Start of range. |
1863 | * @param __end End of range. |
1864 | * @return Iterator referencing the first inserted char. |
1865 | * @throw std::length_error If new length exceeds @c max_size(). |
1866 | * |
1867 | * Inserts characters in range [beg,end). If adding characters |
1868 | * causes the length to exceed max_size(), length_error is |
1869 | * thrown. The value of the string doesn't change if an error |
1870 | * is thrown. |
1871 | */ |
1872 | template<class _InputIterator, |
1873 | typename = std::_RequireInputIter<_InputIterator>> |
1874 | _GLIBCXX20_CONSTEXPRconstexpr |
1875 | iterator |
1876 | insert(const_iterator __p, _InputIterator __beg, _InputIterator __end) |
1877 | { |
1878 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
1879 | const size_type __pos = __p - begin(); |
1880 | this->replace(__p, __p, __beg, __end); |
1881 | return iterator(this->_M_data() + __pos); |
1882 | } |
1883 | #else |
1884 | /** |
1885 | * @brief Insert a range of characters. |
1886 | * @param __p Iterator referencing location in string to insert at. |
1887 | * @param __beg Start of range. |
1888 | * @param __end End of range. |
1889 | * @throw std::length_error If new length exceeds @c max_size(). |
1890 | * |
1891 | * Inserts characters in range [__beg,__end). If adding |
1892 | * characters causes the length to exceed max_size(), |
1893 | * length_error is thrown. The value of the string doesn't |
1894 | * change if an error is thrown. |
1895 | */ |
1896 | template<class _InputIterator> |
1897 | void |
1898 | insert(iterator __p, _InputIterator __beg, _InputIterator __end) |
1899 | { this->replace(__p, __p, __beg, __end); } |
1900 | #endif |
1901 | |
1902 | #if __cplusplus202002L >= 201103L |
1903 | /** |
1904 | * @brief Insert an initializer_list of characters. |
1905 | * @param __p Iterator referencing location in string to insert at. |
1906 | * @param __l The initializer_list of characters to insert. |
1907 | * @throw std::length_error If new length exceeds @c max_size(). |
1908 | */ |
1909 | _GLIBCXX20_CONSTEXPRconstexpr |
1910 | iterator |
1911 | insert(const_iterator __p, initializer_list<_CharT> __l) |
1912 | { return this->insert(__p, __l.begin(), __l.end()); } |
1913 | |
1914 | #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
1915 | // See PR libstdc++/83328 |
1916 | void |
1917 | insert(iterator __p, initializer_list<_CharT> __l) |
1918 | { |
1919 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
1920 | this->insert(__p - begin(), __l.begin(), __l.size()); |
1921 | } |
1922 | #endif |
1923 | #endif // C++11 |
1924 | |
1925 | /** |
1926 | * @brief Insert value of a string. |
1927 | * @param __pos1 Position in string to insert at. |
1928 | * @param __str The string to insert. |
1929 | * @return Reference to this string. |
1930 | * @throw std::length_error If new length exceeds @c max_size(). |
1931 | * |
1932 | * Inserts value of @a __str starting at @a __pos1. If adding |
1933 | * characters causes the length to exceed max_size(), |
1934 | * length_error is thrown. The value of the string doesn't |
1935 | * change if an error is thrown. |
1936 | */ |
1937 | _GLIBCXX20_CONSTEXPRconstexpr |
1938 | basic_string& |
1939 | insert(size_type __pos1, const basic_string& __str) |
1940 | { return this->replace(__pos1, size_type(0), |
1941 | __str._M_data(), __str.size()); } |
1942 | |
1943 | /** |
1944 | * @brief Insert a substring. |
1945 | * @param __pos1 Position in string to insert at. |
1946 | * @param __str The string to insert. |
1947 | * @param __pos2 Start of characters in str to insert. |
1948 | * @param __n Number of characters to insert. |
1949 | * @return Reference to this string. |
1950 | * @throw std::length_error If new length exceeds @c max_size(). |
1951 | * @throw std::out_of_range If @a pos1 > size() or |
1952 | * @a __pos2 > @a str.size(). |
1953 | * |
1954 | * Starting at @a pos1, insert @a __n character of @a __str |
1955 | * beginning with @a __pos2. If adding characters causes the |
1956 | * length to exceed max_size(), length_error is thrown. If @a |
1957 | * __pos1 is beyond the end of this string or @a __pos2 is |
1958 | * beyond the end of @a __str, out_of_range is thrown. The |
1959 | * value of the string doesn't change if an error is thrown. |
1960 | */ |
1961 | _GLIBCXX20_CONSTEXPRconstexpr |
1962 | basic_string& |
1963 | insert(size_type __pos1, const basic_string& __str, |
1964 | size_type __pos2, size_type __n = npos) |
1965 | { return this->replace(__pos1, size_type(0), __str._M_data() |
1966 | + __str._M_check(__pos2, "basic_string::insert"), |
1967 | __str._M_limit(__pos2, __n)); } |
1968 | |
1969 | /** |
1970 | * @brief Insert a C substring. |
1971 | * @param __pos Position in string to insert at. |
1972 | * @param __s The C string to insert. |
1973 | * @param __n The number of characters to insert. |
1974 | * @return Reference to this string. |
1975 | * @throw std::length_error If new length exceeds @c max_size(). |
1976 | * @throw std::out_of_range If @a __pos is beyond the end of this |
1977 | * string. |
1978 | * |
1979 | * Inserts the first @a __n characters of @a __s starting at @a |
1980 | * __pos. If adding characters causes the length to exceed |
1981 | * max_size(), length_error is thrown. If @a __pos is beyond |
1982 | * end(), out_of_range is thrown. The value of the string |
1983 | * doesn't change if an error is thrown. |
1984 | */ |
1985 | _GLIBCXX20_CONSTEXPRconstexpr |
1986 | basic_string& |
1987 | insert(size_type __pos, const _CharT* __s, size_type __n) |
1988 | { return this->replace(__pos, size_type(0), __s, __n); } |
1989 | |
1990 | /** |
1991 | * @brief Insert a C string. |
1992 | * @param __pos Position in string to insert at. |
1993 | * @param __s The C string to insert. |
1994 | * @return Reference to this string. |
1995 | * @throw std::length_error If new length exceeds @c max_size(). |
1996 | * @throw std::out_of_range If @a pos is beyond the end of this |
1997 | * string. |
1998 | * |
1999 | * Inserts the first @a n characters of @a __s starting at @a __pos. If |
2000 | * adding characters causes the length to exceed max_size(), |
2001 | * length_error is thrown. If @a __pos is beyond end(), out_of_range is |
2002 | * thrown. The value of the string doesn't change if an error is |
2003 | * thrown. |
2004 | */ |
2005 | _GLIBCXX20_CONSTEXPRconstexpr |
2006 | basic_string& |
2007 | insert(size_type __pos, const _CharT* __s) |
2008 | { |
2009 | __glibcxx_requires_string(__s); |
2010 | return this->replace(__pos, size_type(0), __s, |
2011 | traits_type::length(__s)); |
2012 | } |
2013 | |
2014 | /** |
2015 | * @brief Insert multiple characters. |
2016 | * @param __pos Index in string to insert at. |
2017 | * @param __n Number of characters to insert |
2018 | * @param __c The character to insert. |
2019 | * @return Reference to this string. |
2020 | * @throw std::length_error If new length exceeds @c max_size(). |
2021 | * @throw std::out_of_range If @a __pos is beyond the end of this |
2022 | * string. |
2023 | * |
2024 | * Inserts @a __n copies of character @a __c starting at index |
2025 | * @a __pos. If adding characters causes the length to exceed |
2026 | * max_size(), length_error is thrown. If @a __pos > length(), |
2027 | * out_of_range is thrown. The value of the string doesn't |
2028 | * change if an error is thrown. |
2029 | */ |
2030 | _GLIBCXX20_CONSTEXPRconstexpr |
2031 | basic_string& |
2032 | insert(size_type __pos, size_type __n, _CharT __c) |
2033 | { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), |
2034 | size_type(0), __n, __c); } |
2035 | |
2036 | /** |
2037 | * @brief Insert one character. |
2038 | * @param __p Iterator referencing position in string to insert at. |
2039 | * @param __c The character to insert. |
2040 | * @return Iterator referencing newly inserted char. |
2041 | * @throw std::length_error If new length exceeds @c max_size(). |
2042 | * |
2043 | * Inserts character @a __c at position referenced by @a __p. |
2044 | * If adding character causes the length to exceed max_size(), |
2045 | * length_error is thrown. If @a __p is beyond end of string, |
2046 | * out_of_range is thrown. The value of the string doesn't |
2047 | * change if an error is thrown. |
2048 | */ |
2049 | _GLIBCXX20_CONSTEXPRconstexpr |
2050 | iterator |
2051 | insert(__const_iterator __p, _CharT __c) |
2052 | { |
2053 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
2054 | const size_type __pos = __p - begin(); |
2055 | _M_replace_aux(__pos, size_type(0), size_type(1), __c); |
2056 | return iterator(_M_data() + __pos); |
2057 | } |
2058 | |
2059 | #if __cplusplus202002L >= 201703L |
2060 | /** |
2061 | * @brief Insert a string_view. |
2062 | * @param __pos Position in string to insert at. |
2063 | * @param __svt The object convertible to string_view to insert. |
2064 | * @return Reference to this string. |
2065 | */ |
2066 | template<typename _Tp> |
2067 | _GLIBCXX20_CONSTEXPRconstexpr |
2068 | _If_sv<_Tp, basic_string&> |
2069 | insert(size_type __pos, const _Tp& __svt) |
2070 | { |
2071 | __sv_type __sv = __svt; |
2072 | return this->insert(__pos, __sv.data(), __sv.size()); |
2073 | } |
2074 | |
2075 | /** |
2076 | * @brief Insert a string_view. |
2077 | * @param __pos1 Position in string to insert at. |
2078 | * @param __svt The object convertible to string_view to insert from. |
2079 | * @param __pos2 Start of characters in str to insert. |
2080 | * @param __n The number of characters to insert. |
2081 | * @return Reference to this string. |
2082 | */ |
2083 | template<typename _Tp> |
2084 | _GLIBCXX20_CONSTEXPRconstexpr |
2085 | _If_sv<_Tp, basic_string&> |
2086 | insert(size_type __pos1, const _Tp& __svt, |
2087 | size_type __pos2, size_type __n = npos) |
2088 | { |
2089 | __sv_type __sv = __svt; |
2090 | return this->replace(__pos1, size_type(0), |
2091 | __sv.data() |
2092 | + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"), |
2093 | std::__sv_limit(__sv.size(), __pos2, __n)); |
2094 | } |
2095 | #endif // C++17 |
2096 | |
2097 | /** |
2098 | * @brief Remove characters. |
2099 | * @param __pos Index of first character to remove (default 0). |
2100 | * @param __n Number of characters to remove (default remainder). |
2101 | * @return Reference to this string. |
2102 | * @throw std::out_of_range If @a pos is beyond the end of this |
2103 | * string. |
2104 | * |
2105 | * Removes @a __n characters from this string starting at @a |
2106 | * __pos. The length of the string is reduced by @a __n. If |
2107 | * there are < @a __n characters to remove, the remainder of |
2108 | * the string is truncated. If @a __p is beyond end of string, |
2109 | * out_of_range is thrown. The value of the string doesn't |
2110 | * change if an error is thrown. |
2111 | */ |
2112 | _GLIBCXX20_CONSTEXPRconstexpr |
2113 | basic_string& |
2114 | erase(size_type __pos = 0, size_type __n = npos) |
2115 | { |
2116 | _M_check(__pos, "basic_string::erase"); |
2117 | if (__n == npos) |
2118 | this->_M_set_length(__pos); |
2119 | else if (__n != 0) |
2120 | this->_M_erase(__pos, _M_limit(__pos, __n)); |
2121 | return *this; |
2122 | } |
2123 | |
2124 | /** |
2125 | * @brief Remove one character. |
2126 | * @param __position Iterator referencing the character to remove. |
2127 | * @return iterator referencing same location after removal. |
2128 | * |
2129 | * Removes the character at @a __position from this string. The value |
2130 | * of the string doesn't change if an error is thrown. |
2131 | */ |
2132 | _GLIBCXX20_CONSTEXPRconstexpr |
2133 | iterator |
2134 | erase(__const_iterator __position) |
2135 | { |
2136 | _GLIBCXX_DEBUG_PEDASSERT(__position >= begin() |
2137 | && __position < end()); |
2138 | const size_type __pos = __position - begin(); |
2139 | this->_M_erase(__pos, size_type(1)); |
2140 | return iterator(_M_data() + __pos); |
2141 | } |
2142 | |
2143 | /** |
2144 | * @brief Remove a range of characters. |
2145 | * @param __first Iterator referencing the first character to remove. |
2146 | * @param __last Iterator referencing the end of the range. |
2147 | * @return Iterator referencing location of first after removal. |
2148 | * |
2149 | * Removes the characters in the range [first,last) from this string. |
2150 | * The value of the string doesn't change if an error is thrown. |
2151 | */ |
2152 | _GLIBCXX20_CONSTEXPRconstexpr |
2153 | iterator |
2154 | erase(__const_iterator __first, __const_iterator __last) |
2155 | { |
2156 | _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last |
2157 | && __last <= end()); |
2158 | const size_type __pos = __first - begin(); |
2159 | if (__last == end()) |
2160 | this->_M_set_length(__pos); |
2161 | else |
2162 | this->_M_erase(__pos, __last - __first); |
2163 | return iterator(this->_M_data() + __pos); |
2164 | } |
2165 | |
2166 | #if __cplusplus202002L >= 201103L |
2167 | /** |
2168 | * @brief Remove the last character. |
2169 | * |
2170 | * The string must be non-empty. |
2171 | */ |
2172 | _GLIBCXX20_CONSTEXPRconstexpr |
2173 | void |
2174 | pop_back() noexcept |
2175 | { |
2176 | __glibcxx_assert(!empty())do { if (std::__is_constant_evaluated()) if (__builtin_expect (!bool(!empty()), false)) std::__glibcxx_assert_fail(); } while (false); |
2177 | _M_erase(size() - 1, 1); |
2178 | } |
2179 | #endif // C++11 |
2180 | |
2181 | /** |
2182 | * @brief Replace characters with value from another string. |
2183 | * @param __pos Index of first character to replace. |
2184 | * @param __n Number of characters to be replaced. |
2185 | * @param __str String to insert. |
2186 | * @return Reference to this string. |
2187 | * @throw std::out_of_range If @a pos is beyond the end of this |
2188 | * string. |
2189 | * @throw std::length_error If new length exceeds @c max_size(). |
2190 | * |
2191 | * Removes the characters in the range [__pos,__pos+__n) from |
2192 | * this string. In place, the value of @a __str is inserted. |
2193 | * If @a __pos is beyond end of string, out_of_range is thrown. |
2194 | * If the length of the result exceeds max_size(), length_error |
2195 | * is thrown. The value of the string doesn't change if an |
2196 | * error is thrown. |
2197 | */ |
2198 | _GLIBCXX20_CONSTEXPRconstexpr |
2199 | basic_string& |
2200 | replace(size_type __pos, size_type __n, const basic_string& __str) |
2201 | { return this->replace(__pos, __n, __str._M_data(), __str.size()); } |
2202 | |
2203 | /** |
2204 | * @brief Replace characters with value from another string. |
2205 | * @param __pos1 Index of first character to replace. |
2206 | * @param __n1 Number of characters to be replaced. |
2207 | * @param __str String to insert. |
2208 | * @param __pos2 Index of first character of str to use. |
2209 | * @param __n2 Number of characters from str to use. |
2210 | * @return Reference to this string. |
2211 | * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > |
2212 | * __str.size(). |
2213 | * @throw std::length_error If new length exceeds @c max_size(). |
2214 | * |
2215 | * Removes the characters in the range [__pos1,__pos1 + n) from this |
2216 | * string. In place, the value of @a __str is inserted. If @a __pos is |
2217 | * beyond end of string, out_of_range is thrown. If the length of the |
2218 | * result exceeds max_size(), length_error is thrown. The value of the |
2219 | * string doesn't change if an error is thrown. |
2220 | */ |
2221 | _GLIBCXX20_CONSTEXPRconstexpr |
2222 | basic_string& |
2223 | replace(size_type __pos1, size_type __n1, const basic_string& __str, |
2224 | size_type __pos2, size_type __n2 = npos) |
2225 | { return this->replace(__pos1, __n1, __str._M_data() |
2226 | + __str._M_check(__pos2, "basic_string::replace"), |
2227 | __str._M_limit(__pos2, __n2)); } |
2228 | |
2229 | /** |
2230 | * @brief Replace characters with value of a C substring. |
2231 | * @param __pos Index of first character to replace. |
2232 | * @param __n1 Number of characters to be replaced. |
2233 | * @param __s C string to insert. |
2234 | * @param __n2 Number of characters from @a s to use. |
2235 | * @return Reference to this string. |
2236 | * @throw std::out_of_range If @a pos1 > size(). |
2237 | * @throw std::length_error If new length exceeds @c max_size(). |
2238 | * |
2239 | * Removes the characters in the range [__pos,__pos + __n1) |
2240 | * from this string. In place, the first @a __n2 characters of |
2241 | * @a __s are inserted, or all of @a __s if @a __n2 is too large. If |
2242 | * @a __pos is beyond end of string, out_of_range is thrown. If |
2243 | * the length of result exceeds max_size(), length_error is |
2244 | * thrown. The value of the string doesn't change if an error |
2245 | * is thrown. |
2246 | */ |
2247 | _GLIBCXX20_CONSTEXPRconstexpr |
2248 | basic_string& |
2249 | replace(size_type __pos, size_type __n1, const _CharT* __s, |
2250 | size_type __n2) |
2251 | { |
2252 | __glibcxx_requires_string_len(__s, __n2); |
2253 | return _M_replace(_M_check(__pos, "basic_string::replace"), |
2254 | _M_limit(__pos, __n1), __s, __n2); |
2255 | } |
2256 | |
2257 | /** |
2258 | * @brief Replace characters with value of a C string. |
2259 | * @param __pos Index of first character to replace. |
2260 | * @param __n1 Number of characters to be replaced. |
2261 | * @param __s C string to insert. |
2262 | * @return Reference to this string. |
2263 | * @throw std::out_of_range If @a pos > size(). |
2264 | * @throw std::length_error If new length exceeds @c max_size(). |
2265 | * |
2266 | * Removes the characters in the range [__pos,__pos + __n1) |
2267 | * from this string. In place, the characters of @a __s are |
2268 | * inserted. If @a __pos is beyond end of string, out_of_range |
2269 | * is thrown. If the length of result exceeds max_size(), |
2270 | * length_error is thrown. The value of the string doesn't |
2271 | * change if an error is thrown. |
2272 | */ |
2273 | _GLIBCXX20_CONSTEXPRconstexpr |
2274 | basic_string& |
2275 | replace(size_type __pos, size_type __n1, const _CharT* __s) |
2276 | { |
2277 | __glibcxx_requires_string(__s); |
2278 | return this->replace(__pos, __n1, __s, traits_type::length(__s)); |
2279 | } |
2280 | |
2281 | /** |
2282 | * @brief Replace characters with multiple characters. |
2283 | * @param __pos Index of first character to replace. |
2284 | * @param __n1 Number of characters to be replaced. |
2285 | * @param __n2 Number of characters to insert. |
2286 | * @param __c Character to insert. |
2287 | * @return Reference to this string. |
2288 | * @throw std::out_of_range If @a __pos > size(). |
2289 | * @throw std::length_error If new length exceeds @c max_size(). |
2290 | * |
2291 | * Removes the characters in the range [pos,pos + n1) from this |
2292 | * string. In place, @a __n2 copies of @a __c are inserted. |
2293 | * If @a __pos is beyond end of string, out_of_range is thrown. |
2294 | * If the length of result exceeds max_size(), length_error is |
2295 | * thrown. The value of the string doesn't change if an error |
2296 | * is thrown. |
2297 | */ |
2298 | _GLIBCXX20_CONSTEXPRconstexpr |
2299 | basic_string& |
2300 | replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) |
2301 | { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), |
2302 | _M_limit(__pos, __n1), __n2, __c); } |
2303 | |
2304 | /** |
2305 | * @brief Replace range of characters with string. |
2306 | * @param __i1 Iterator referencing start of range to replace. |
2307 | * @param __i2 Iterator referencing end of range to replace. |
2308 | * @param __str String value to insert. |
2309 | * @return Reference to this string. |
2310 | * @throw std::length_error If new length exceeds @c max_size(). |
2311 | * |
2312 | * Removes the characters in the range [__i1,__i2). In place, |
2313 | * the value of @a __str is inserted. If the length of result |
2314 | * exceeds max_size(), length_error is thrown. The value of |
2315 | * the string doesn't change if an error is thrown. |
2316 | */ |
2317 | _GLIBCXX20_CONSTEXPRconstexpr |
2318 | basic_string& |
2319 | replace(__const_iterator __i1, __const_iterator __i2, |
2320 | const basic_string& __str) |
2321 | { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } |
2322 | |
2323 | /** |
2324 | * @brief Replace range of characters with C substring. |
2325 | * @param __i1 Iterator referencing start of range to replace. |
2326 | * @param __i2 Iterator referencing end of range to replace. |
2327 | * @param __s C string value to insert. |
2328 | * @param __n Number of characters from s to insert. |
2329 | * @return Reference to this string. |
2330 | * @throw std::length_error If new length exceeds @c max_size(). |
2331 | * |
2332 | * Removes the characters in the range [__i1,__i2). In place, |
2333 | * the first @a __n characters of @a __s are inserted. If the |
2334 | * length of result exceeds max_size(), length_error is thrown. |
2335 | * The value of the string doesn't change if an error is |
2336 | * thrown. |
2337 | */ |
2338 | _GLIBCXX20_CONSTEXPRconstexpr |
2339 | basic_string& |
2340 | replace(__const_iterator __i1, __const_iterator __i2, |
2341 | const _CharT* __s, size_type __n) |
2342 | { |
2343 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2344 | && __i2 <= end()); |
2345 | return this->replace(__i1 - begin(), __i2 - __i1, __s, __n); |
2346 | } |
2347 | |
2348 | /** |
2349 | * @brief Replace range of characters with C string. |
2350 | * @param __i1 Iterator referencing start of range to replace. |
2351 | * @param __i2 Iterator referencing end of range to replace. |
2352 | * @param __s C string value to insert. |
2353 | * @return Reference to this string. |
2354 | * @throw std::length_error If new length exceeds @c max_size(). |
2355 | * |
2356 | * Removes the characters in the range [__i1,__i2). In place, |
2357 | * the characters of @a __s are inserted. If the length of |
2358 | * result exceeds max_size(), length_error is thrown. The |
2359 | * value of the string doesn't change if an error is thrown. |
2360 | */ |
2361 | _GLIBCXX20_CONSTEXPRconstexpr |
2362 | basic_string& |
2363 | replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s) |
2364 | { |
2365 | __glibcxx_requires_string(__s); |
2366 | return this->replace(__i1, __i2, __s, traits_type::length(__s)); |
2367 | } |
2368 | |
2369 | /** |
2370 | * @brief Replace range of characters with multiple characters |
2371 | * @param __i1 Iterator referencing start of range to replace. |
2372 | * @param __i2 Iterator referencing end of range to replace. |
2373 | * @param __n Number of characters to insert. |
2374 | * @param __c Character to insert. |
2375 | * @return Reference to this string. |
2376 | * @throw std::length_error If new length exceeds @c max_size(). |
2377 | * |
2378 | * Removes the characters in the range [__i1,__i2). In place, |
2379 | * @a __n copies of @a __c are inserted. If the length of |
2380 | * result exceeds max_size(), length_error is thrown. The |
2381 | * value of the string doesn't change if an error is thrown. |
2382 | */ |
2383 | _GLIBCXX20_CONSTEXPRconstexpr |
2384 | basic_string& |
2385 | replace(__const_iterator __i1, __const_iterator __i2, size_type __n, |
2386 | _CharT __c) |
2387 | { |
2388 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2389 | && __i2 <= end()); |
2390 | return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c); |
2391 | } |
2392 | |
2393 | /** |
2394 | * @brief Replace range of characters with range. |
2395 | * @param __i1 Iterator referencing start of range to replace. |
2396 | * @param __i2 Iterator referencing end of range to replace. |
2397 | * @param __k1 Iterator referencing start of range to insert. |
2398 | * @param __k2 Iterator referencing end of range to insert. |
2399 | * @return Reference to this string. |
2400 | * @throw std::length_error If new length exceeds @c max_size(). |
2401 | * |
2402 | * Removes the characters in the range [__i1,__i2). In place, |
2403 | * characters in the range [__k1,__k2) are inserted. If the |
2404 | * length of result exceeds max_size(), length_error is thrown. |
2405 | * The value of the string doesn't change if an error is |
2406 | * thrown. |
2407 | */ |
2408 | #if __cplusplus202002L >= 201103L |
2409 | template<class _InputIterator, |
2410 | typename = std::_RequireInputIter<_InputIterator>> |
2411 | _GLIBCXX20_CONSTEXPRconstexpr |
2412 | basic_string& |
2413 | replace(const_iterator __i1, const_iterator __i2, |
2414 | _InputIterator __k1, _InputIterator __k2) |
2415 | { |
2416 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2417 | && __i2 <= end()); |
2418 | __glibcxx_requires_valid_range(__k1, __k2); |
2419 | return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, |
2420 | std::__false_type()); |
2421 | } |
2422 | #else |
2423 | template<class _InputIterator> |
2424 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST |
2425 | typename __enable_if_not_native_iterator<_InputIterator>::__type |
2426 | #else |
2427 | basic_string& |
2428 | #endif |
2429 | replace(iterator __i1, iterator __i2, |
2430 | _InputIterator __k1, _InputIterator __k2) |
2431 | { |
2432 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2433 | && __i2 <= end()); |
2434 | __glibcxx_requires_valid_range(__k1, __k2); |
2435 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
2436 | return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); |
2437 | } |
2438 | #endif |
2439 | |
2440 | // Specializations for the common case of pointer and iterator: |
2441 | // useful to avoid the overhead of temporary buffering in _M_replace. |
2442 | _GLIBCXX20_CONSTEXPRconstexpr |
2443 | basic_string& |
2444 | replace(__const_iterator __i1, __const_iterator __i2, |
2445 | _CharT* __k1, _CharT* __k2) |
2446 | { |
2447 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2448 | && __i2 <= end()); |
2449 | __glibcxx_requires_valid_range(__k1, __k2); |
2450 | return this->replace(__i1 - begin(), __i2 - __i1, |
2451 | __k1, __k2 - __k1); |
2452 | } |
2453 | |
2454 | _GLIBCXX20_CONSTEXPRconstexpr |
2455 | basic_string& |
2456 | replace(__const_iterator __i1, __const_iterator __i2, |
2457 | const _CharT* __k1, const _CharT* __k2) |
2458 | { |
2459 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2460 | && __i2 <= end()); |
2461 | __glibcxx_requires_valid_range(__k1, __k2); |
2462 | return this->replace(__i1 - begin(), __i2 - __i1, |
2463 | __k1, __k2 - __k1); |
2464 | } |
2465 | |
2466 | _GLIBCXX20_CONSTEXPRconstexpr |
2467 | basic_string& |
2468 | replace(__const_iterator __i1, __const_iterator __i2, |
2469 | iterator __k1, iterator __k2) |
2470 | { |
2471 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2472 | && __i2 <= end()); |
2473 | __glibcxx_requires_valid_range(__k1, __k2); |
2474 | return this->replace(__i1 - begin(), __i2 - __i1, |
2475 | __k1.base(), __k2 - __k1); |
2476 | } |
2477 | |
2478 | _GLIBCXX20_CONSTEXPRconstexpr |
2479 | basic_string& |
2480 | replace(__const_iterator __i1, __const_iterator __i2, |
2481 | const_iterator __k1, const_iterator __k2) |
2482 | { |
2483 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
2484 | && __i2 <= end()); |
2485 | __glibcxx_requires_valid_range(__k1, __k2); |
2486 | return this->replace(__i1 - begin(), __i2 - __i1, |
2487 | __k1.base(), __k2 - __k1); |
2488 | } |
2489 | |
2490 | #if __cplusplus202002L >= 201103L |
2491 | /** |
2492 | * @brief Replace range of characters with initializer_list. |
2493 | * @param __i1 Iterator referencing start of range to replace. |
2494 | * @param __i2 Iterator referencing end of range to replace. |
2495 | * @param __l The initializer_list of characters to insert. |
2496 | * @return Reference to this string. |
2497 | * @throw std::length_error If new length exceeds @c max_size(). |
2498 | * |
2499 | * Removes the characters in the range [__i1,__i2). In place, |
2500 | * characters in the range [__k1,__k2) are inserted. If the |
2501 | * length of result exceeds max_size(), length_error is thrown. |
2502 | * The value of the string doesn't change if an error is |
2503 | * thrown. |
2504 | */ |
2505 | _GLIBCXX20_CONSTEXPRconstexpr |
2506 | basic_string& replace(const_iterator __i1, const_iterator __i2, |
2507 | initializer_list<_CharT> __l) |
2508 | { return this->replace(__i1, __i2, __l.begin(), __l.size()); } |
2509 | #endif // C++11 |
2510 | |
2511 | #if __cplusplus202002L >= 201703L |
2512 | /** |
2513 | * @brief Replace range of characters with string_view. |
2514 | * @param __pos The position to replace at. |
2515 | * @param __n The number of characters to replace. |
2516 | * @param __svt The object convertible to string_view to insert. |
2517 | * @return Reference to this string. |
2518 | */ |
2519 | template<typename _Tp> |
2520 | _GLIBCXX20_CONSTEXPRconstexpr |
2521 | _If_sv<_Tp, basic_string&> |
2522 | replace(size_type __pos, size_type __n, const _Tp& __svt) |
2523 | { |
2524 | __sv_type __sv = __svt; |
2525 | return this->replace(__pos, __n, __sv.data(), __sv.size()); |
2526 | } |
2527 | |
2528 | /** |
2529 | * @brief Replace range of characters with string_view. |
2530 | * @param __pos1 The position to replace at. |
2531 | * @param __n1 The number of characters to replace. |
2532 | * @param __svt The object convertible to string_view to insert from. |
2533 | * @param __pos2 The position in the string_view to insert from. |
2534 | * @param __n2 The number of characters to insert. |
2535 | * @return Reference to this string. |
2536 | */ |
2537 | template<typename _Tp> |
2538 | _GLIBCXX20_CONSTEXPRconstexpr |
2539 | _If_sv<_Tp, basic_string&> |
2540 | replace(size_type __pos1, size_type __n1, const _Tp& __svt, |
2541 | size_type __pos2, size_type __n2 = npos) |
2542 | { |
2543 | __sv_type __sv = __svt; |
2544 | return this->replace(__pos1, __n1, |
2545 | __sv.data() |
2546 | + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"), |
2547 | std::__sv_limit(__sv.size(), __pos2, __n2)); |
2548 | } |
2549 | |
2550 | /** |
2551 | * @brief Replace range of characters with string_view. |
2552 | * @param __i1 An iterator referencing the start position |
2553 | to replace at. |
2554 | * @param __i2 An iterator referencing the end position |
2555 | for the replace. |
2556 | * @param __svt The object convertible to string_view to insert from. |
2557 | * @return Reference to this string. |
2558 | */ |
2559 | template<typename _Tp> |
2560 | _GLIBCXX20_CONSTEXPRconstexpr |
2561 | _If_sv<_Tp, basic_string&> |
2562 | replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt) |
2563 | { |
2564 | __sv_type __sv = __svt; |
2565 | return this->replace(__i1 - begin(), __i2 - __i1, __sv); |
2566 | } |
2567 | #endif // C++17 |
2568 | |
2569 | private: |
2570 | template<class _Integer> |
2571 | _GLIBCXX20_CONSTEXPRconstexpr |
2572 | basic_string& |
2573 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, |
2574 | _Integer __n, _Integer __val, __true_type) |
2575 | { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); } |
2576 | |
2577 | template<class _InputIterator> |
2578 | _GLIBCXX20_CONSTEXPRconstexpr |
2579 | basic_string& |
2580 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, |
2581 | _InputIterator __k1, _InputIterator __k2, |
2582 | __false_type); |
2583 | |
2584 | _GLIBCXX20_CONSTEXPRconstexpr |
2585 | basic_string& |
2586 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, |
2587 | _CharT __c); |
2588 | |
2589 | __attribute__((__noinline__, __noclone__, __cold__)) void |
2590 | _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s, |
2591 | const size_type __len2, const size_type __how_much); |
2592 | |
2593 | _GLIBCXX20_CONSTEXPRconstexpr |
2594 | basic_string& |
2595 | _M_replace(size_type __pos, size_type __len1, const _CharT* __s, |
2596 | const size_type __len2); |
2597 | |
2598 | _GLIBCXX20_CONSTEXPRconstexpr |
2599 | basic_string& |
2600 | _M_append(const _CharT* __s, size_type __n); |
2601 | |
2602 | public: |
2603 | |
2604 | /** |
2605 | * @brief Copy substring into C string. |
2606 | * @param __s C string to copy value into. |
2607 | * @param __n Number of characters to copy. |
2608 | * @param __pos Index of first character to copy. |
2609 | * @return Number of characters actually copied |
2610 | * @throw std::out_of_range If __pos > size(). |
2611 | * |
2612 | * Copies up to @a __n characters starting at @a __pos into the |
2613 | * C string @a __s. If @a __pos is %greater than size(), |
2614 | * out_of_range is thrown. |
2615 | */ |
2616 | _GLIBCXX20_CONSTEXPRconstexpr |
2617 | size_type |
2618 | copy(_CharT* __s, size_type __n, size_type __pos = 0) const; |
2619 | |
2620 | /** |
2621 | * @brief Swap contents with another string. |
2622 | * @param __s String to swap with. |
2623 | * |
2624 | * Exchanges the contents of this string with that of @a __s in constant |
2625 | * time. |
2626 | */ |
2627 | _GLIBCXX20_CONSTEXPRconstexpr |
2628 | void |
2629 | swap(basic_string& __s) _GLIBCXX_NOEXCEPTnoexcept; |
2630 | |
2631 | // String operations: |
2632 | /** |
2633 | * @brief Return const pointer to null-terminated contents. |
2634 | * |
2635 | * This is a handle to internal data. Do not modify or dire things may |
2636 | * happen. |
2637 | */ |
2638 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2639 | const _CharT* |
2640 | c_str() const _GLIBCXX_NOEXCEPTnoexcept |
2641 | { return _M_data(); } |
2642 | |
2643 | /** |
2644 | * @brief Return const pointer to contents. |
2645 | * |
2646 | * This is a pointer to internal data. It is undefined to modify |
2647 | * the contents through the returned pointer. To get a pointer that |
2648 | * allows modifying the contents use @c &str[0] instead, |
2649 | * (or in C++17 the non-const @c str.data() overload). |
2650 | */ |
2651 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2652 | const _CharT* |
2653 | data() const _GLIBCXX_NOEXCEPTnoexcept |
2654 | { return _M_data(); } |
2655 | |
2656 | #if __cplusplus202002L >= 201703L |
2657 | /** |
2658 | * @brief Return non-const pointer to contents. |
2659 | * |
2660 | * This is a pointer to the character sequence held by the string. |
2661 | * Modifying the characters in the sequence is allowed. |
2662 | */ |
2663 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2664 | _CharT* |
2665 | data() noexcept |
2666 | { return _M_data(); } |
2667 | #endif |
2668 | |
2669 | /** |
2670 | * @brief Return copy of allocator used to construct this string. |
2671 | */ |
2672 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2673 | allocator_type |
2674 | get_allocator() const _GLIBCXX_NOEXCEPTnoexcept |
2675 | { return _M_get_allocator(); } |
2676 | |
2677 | /** |
2678 | * @brief Find position of a C substring. |
2679 | * @param __s C string to locate. |
2680 | * @param __pos Index of character to search from. |
2681 | * @param __n Number of characters from @a s to search for. |
2682 | * @return Index of start of first occurrence. |
2683 | * |
2684 | * Starting from @a __pos, searches forward for the first @a |
2685 | * __n characters in @a __s within this string. If found, |
2686 | * returns the index where it begins. If not found, returns |
2687 | * npos. |
2688 | */ |
2689 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2690 | size_type |
2691 | find(const _CharT* __s, size_type __pos, size_type __n) const |
2692 | _GLIBCXX_NOEXCEPTnoexcept; |
2693 | |
2694 | /** |
2695 | * @brief Find position of a string. |
2696 | * @param __str String to locate. |
2697 | * @param __pos Index of character to search from (default 0). |
2698 | * @return Index of start of first occurrence. |
2699 | * |
2700 | * Starting from @a __pos, searches forward for value of @a __str within |
2701 | * this string. If found, returns the index where it begins. If not |
2702 | * found, returns npos. |
2703 | */ |
2704 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2705 | size_type |
2706 | find(const basic_string& __str, size_type __pos = 0) const |
2707 | _GLIBCXX_NOEXCEPTnoexcept |
2708 | { return this->find(__str.data(), __pos, __str.size()); } |
2709 | |
2710 | #if __cplusplus202002L >= 201703L |
2711 | /** |
2712 | * @brief Find position of a string_view. |
2713 | * @param __svt The object convertible to string_view to locate. |
2714 | * @param __pos Index of character to search from (default 0). |
2715 | * @return Index of start of first occurrence. |
2716 | */ |
2717 | template<typename _Tp> |
2718 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2719 | _If_sv<_Tp, size_type> |
2720 | find(const _Tp& __svt, size_type __pos = 0) const |
2721 | noexcept(is_same<_Tp, __sv_type>::value) |
2722 | { |
2723 | __sv_type __sv = __svt; |
2724 | return this->find(__sv.data(), __pos, __sv.size()); |
2725 | } |
2726 | #endif // C++17 |
2727 | |
2728 | /** |
2729 | * @brief Find position of a C string. |
2730 | * @param __s C string to locate. |
2731 | * @param __pos Index of character to search from (default 0). |
2732 | * @return Index of start of first occurrence. |
2733 | * |
2734 | * Starting from @a __pos, searches forward for the value of @a |
2735 | * __s within this string. If found, returns the index where |
2736 | * it begins. If not found, returns npos. |
2737 | */ |
2738 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2739 | size_type |
2740 | find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept |
2741 | { |
2742 | __glibcxx_requires_string(__s); |
2743 | return this->find(__s, __pos, traits_type::length(__s)); |
2744 | } |
2745 | |
2746 | /** |
2747 | * @brief Find position of a character. |
2748 | * @param __c Character to locate. |
2749 | * @param __pos Index of character to search from (default 0). |
2750 | * @return Index of first occurrence. |
2751 | * |
2752 | * Starting from @a __pos, searches forward for @a __c within |
2753 | * this string. If found, returns the index where it was |
2754 | * found. If not found, returns npos. |
2755 | */ |
2756 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2757 | size_type |
2758 | find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept; |
2759 | |
2760 | /** |
2761 | * @brief Find last position of a string. |
2762 | * @param __str String to locate. |
2763 | * @param __pos Index of character to search back from (default end). |
2764 | * @return Index of start of last occurrence. |
2765 | * |
2766 | * Starting from @a __pos, searches backward for value of @a |
2767 | * __str within this string. If found, returns the index where |
2768 | * it begins. If not found, returns npos. |
2769 | */ |
2770 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2771 | size_type |
2772 | rfind(const basic_string& __str, size_type __pos = npos) const |
2773 | _GLIBCXX_NOEXCEPTnoexcept |
2774 | { return this->rfind(__str.data(), __pos, __str.size()); } |
2775 | |
2776 | #if __cplusplus202002L >= 201703L |
2777 | /** |
2778 | * @brief Find last position of a string_view. |
2779 | * @param __svt The object convertible to string_view to locate. |
2780 | * @param __pos Index of character to search back from (default end). |
2781 | * @return Index of start of last occurrence. |
2782 | */ |
2783 | template<typename _Tp> |
2784 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2785 | _If_sv<_Tp, size_type> |
2786 | rfind(const _Tp& __svt, size_type __pos = npos) const |
2787 | noexcept(is_same<_Tp, __sv_type>::value) |
2788 | { |
2789 | __sv_type __sv = __svt; |
2790 | return this->rfind(__sv.data(), __pos, __sv.size()); |
2791 | } |
2792 | #endif // C++17 |
2793 | |
2794 | /** |
2795 | * @brief Find last position of a C substring. |
2796 | * @param __s C string to locate. |
2797 | * @param __pos Index of character to search back from. |
2798 | * @param __n Number of characters from s to search for. |
2799 | * @return Index of start of last occurrence. |
2800 | * |
2801 | * Starting from @a __pos, searches backward for the first @a |
2802 | * __n characters in @a __s within this string. If found, |
2803 | * returns the index where it begins. If not found, returns |
2804 | * npos. |
2805 | */ |
2806 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2807 | size_type |
2808 | rfind(const _CharT* __s, size_type __pos, size_type __n) const |
2809 | _GLIBCXX_NOEXCEPTnoexcept; |
2810 | |
2811 | /** |
2812 | * @brief Find last position of a C string. |
2813 | * @param __s C string to locate. |
2814 | * @param __pos Index of character to start search at (default end). |
2815 | * @return Index of start of last occurrence. |
2816 | * |
2817 | * Starting from @a __pos, searches backward for the value of |
2818 | * @a __s within this string. If found, returns the index |
2819 | * where it begins. If not found, returns npos. |
2820 | */ |
2821 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2822 | size_type |
2823 | rfind(const _CharT* __s, size_type __pos = npos) const |
2824 | { |
2825 | __glibcxx_requires_string(__s); |
2826 | return this->rfind(__s, __pos, traits_type::length(__s)); |
2827 | } |
2828 | |
2829 | /** |
2830 | * @brief Find last position of a character. |
2831 | * @param __c Character to locate. |
2832 | * @param __pos Index of character to search back from (default end). |
2833 | * @return Index of last occurrence. |
2834 | * |
2835 | * Starting from @a __pos, searches backward for @a __c within |
2836 | * this string. If found, returns the index where it was |
2837 | * found. If not found, returns npos. |
2838 | */ |
2839 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2840 | size_type |
2841 | rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept; |
2842 | |
2843 | /** |
2844 | * @brief Find position of a character of string. |
2845 | * @param __str String containing characters to locate. |
2846 | * @param __pos Index of character to search from (default 0). |
2847 | * @return Index of first occurrence. |
2848 | * |
2849 | * Starting from @a __pos, searches forward for one of the |
2850 | * characters of @a __str within this string. If found, |
2851 | * returns the index where it was found. If not found, returns |
2852 | * npos. |
2853 | */ |
2854 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2855 | size_type |
2856 | find_first_of(const basic_string& __str, size_type __pos = 0) const |
2857 | _GLIBCXX_NOEXCEPTnoexcept |
2858 | { return this->find_first_of(__str.data(), __pos, __str.size()); } |
2859 | |
2860 | #if __cplusplus202002L >= 201703L |
2861 | /** |
2862 | * @brief Find position of a character of a string_view. |
2863 | * @param __svt An object convertible to string_view containing |
2864 | * characters to locate. |
2865 | * @param __pos Index of character to search from (default 0). |
2866 | * @return Index of first occurrence. |
2867 | */ |
2868 | template<typename _Tp> |
2869 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2870 | _If_sv<_Tp, size_type> |
2871 | find_first_of(const _Tp& __svt, size_type __pos = 0) const |
2872 | noexcept(is_same<_Tp, __sv_type>::value) |
2873 | { |
2874 | __sv_type __sv = __svt; |
2875 | return this->find_first_of(__sv.data(), __pos, __sv.size()); |
2876 | } |
2877 | #endif // C++17 |
2878 | |
2879 | /** |
2880 | * @brief Find position of a character of C substring. |
2881 | * @param __s String containing characters to locate. |
2882 | * @param __pos Index of character to search from. |
2883 | * @param __n Number of characters from s to search for. |
2884 | * @return Index of first occurrence. |
2885 | * |
2886 | * Starting from @a __pos, searches forward for one of the |
2887 | * first @a __n characters of @a __s within this string. If |
2888 | * found, returns the index where it was found. If not found, |
2889 | * returns npos. |
2890 | */ |
2891 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2892 | size_type |
2893 | find_first_of(const _CharT* __s, size_type __pos, size_type __n) const |
2894 | _GLIBCXX_NOEXCEPTnoexcept; |
2895 | |
2896 | /** |
2897 | * @brief Find position of a character of C string. |
2898 | * @param __s String containing characters to locate. |
2899 | * @param __pos Index of character to search from (default 0). |
2900 | * @return Index of first occurrence. |
2901 | * |
2902 | * Starting from @a __pos, searches forward for one of the |
2903 | * characters of @a __s within this string. If found, returns |
2904 | * the index where it was found. If not found, returns npos. |
2905 | */ |
2906 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2907 | size_type |
2908 | find_first_of(const _CharT* __s, size_type __pos = 0) const |
2909 | _GLIBCXX_NOEXCEPTnoexcept |
2910 | { |
2911 | __glibcxx_requires_string(__s); |
2912 | return this->find_first_of(__s, __pos, traits_type::length(__s)); |
2913 | } |
2914 | |
2915 | /** |
2916 | * @brief Find position of a character. |
2917 | * @param __c Character to locate. |
2918 | * @param __pos Index of character to search from (default 0). |
2919 | * @return Index of first occurrence. |
2920 | * |
2921 | * Starting from @a __pos, searches forward for the character |
2922 | * @a __c within this string. If found, returns the index |
2923 | * where it was found. If not found, returns npos. |
2924 | * |
2925 | * Note: equivalent to find(__c, __pos). |
2926 | */ |
2927 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2928 | size_type |
2929 | find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept |
2930 | { return this->find(__c, __pos); } |
2931 | |
2932 | /** |
2933 | * @brief Find last position of a character of string. |
2934 | * @param __str String containing characters to locate. |
2935 | * @param __pos Index of character to search back from (default end). |
2936 | * @return Index of last occurrence. |
2937 | * |
2938 | * Starting from @a __pos, searches backward for one of the |
2939 | * characters of @a __str within this string. If found, |
2940 | * returns the index where it was found. If not found, returns |
2941 | * npos. |
2942 | */ |
2943 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2944 | size_type |
2945 | find_last_of(const basic_string& __str, size_type __pos = npos) const |
2946 | _GLIBCXX_NOEXCEPTnoexcept |
2947 | { return this->find_last_of(__str.data(), __pos, __str.size()); } |
2948 | |
2949 | #if __cplusplus202002L >= 201703L |
2950 | /** |
2951 | * @brief Find last position of a character of string. |
2952 | * @param __svt An object convertible to string_view containing |
2953 | * characters to locate. |
2954 | * @param __pos Index of character to search back from (default end). |
2955 | * @return Index of last occurrence. |
2956 | */ |
2957 | template<typename _Tp> |
2958 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2959 | _If_sv<_Tp, size_type> |
2960 | find_last_of(const _Tp& __svt, size_type __pos = npos) const |
2961 | noexcept(is_same<_Tp, __sv_type>::value) |
2962 | { |
2963 | __sv_type __sv = __svt; |
2964 | return this->find_last_of(__sv.data(), __pos, __sv.size()); |
2965 | } |
2966 | #endif // C++17 |
2967 | |
2968 | /** |
2969 | * @brief Find last position of a character of C substring. |
2970 | * @param __s C string containing characters to locate. |
2971 | * @param __pos Index of character to search back from. |
2972 | * @param __n Number of characters from s to search for. |
2973 | * @return Index of last occurrence. |
2974 | * |
2975 | * Starting from @a __pos, searches backward for one of the |
2976 | * first @a __n characters of @a __s within this string. If |
2977 | * found, returns the index where it was found. If not found, |
2978 | * returns npos. |
2979 | */ |
2980 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2981 | size_type |
2982 | find_last_of(const _CharT* __s, size_type __pos, size_type __n) const |
2983 | _GLIBCXX_NOEXCEPTnoexcept; |
2984 | |
2985 | /** |
2986 | * @brief Find last position of a character of C string. |
2987 | * @param __s C string containing characters to locate. |
2988 | * @param __pos Index of character to search back from (default end). |
2989 | * @return Index of last occurrence. |
2990 | * |
2991 | * Starting from @a __pos, searches backward for one of the |
2992 | * characters of @a __s within this string. If found, returns |
2993 | * the index where it was found. If not found, returns npos. |
2994 | */ |
2995 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
2996 | size_type |
2997 | find_last_of(const _CharT* __s, size_type __pos = npos) const |
2998 | _GLIBCXX_NOEXCEPTnoexcept |
2999 | { |
3000 | __glibcxx_requires_string(__s); |
3001 | return this->find_last_of(__s, __pos, traits_type::length(__s)); |
3002 | } |
3003 | |
3004 | /** |
3005 | * @brief Find last position of a character. |
3006 | * @param __c Character to locate. |
3007 | * @param __pos Index of character to search back from (default end). |
3008 | * @return Index of last occurrence. |
3009 | * |
3010 | * Starting from @a __pos, searches backward for @a __c within |
3011 | * this string. If found, returns the index where it was |
3012 | * found. If not found, returns npos. |
3013 | * |
3014 | * Note: equivalent to rfind(__c, __pos). |
3015 | */ |
3016 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3017 | size_type |
3018 | find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept |
3019 | { return this->rfind(__c, __pos); } |
3020 | |
3021 | /** |
3022 | * @brief Find position of a character not in string. |
3023 | * @param __str String containing characters to avoid. |
3024 | * @param __pos Index of character to search from (default 0). |
3025 | * @return Index of first occurrence. |
3026 | * |
3027 | * Starting from @a __pos, searches forward for a character not contained |
3028 | * in @a __str within this string. If found, returns the index where it |
3029 | * was found. If not found, returns npos. |
3030 | */ |
3031 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3032 | size_type |
3033 | find_first_not_of(const basic_string& __str, size_type __pos = 0) const |
3034 | _GLIBCXX_NOEXCEPTnoexcept |
3035 | { return this->find_first_not_of(__str.data(), __pos, __str.size()); } |
3036 | |
3037 | #if __cplusplus202002L >= 201703L |
3038 | /** |
3039 | * @brief Find position of a character not in a string_view. |
3040 | * @param __svt A object convertible to string_view containing |
3041 | * characters to avoid. |
3042 | * @param __pos Index of character to search from (default 0). |
3043 | * @return Index of first occurrence. |
3044 | */ |
3045 | template<typename _Tp> |
3046 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3047 | _If_sv<_Tp, size_type> |
3048 | find_first_not_of(const _Tp& __svt, size_type __pos = 0) const |
3049 | noexcept(is_same<_Tp, __sv_type>::value) |
3050 | { |
3051 | __sv_type __sv = __svt; |
3052 | return this->find_first_not_of(__sv.data(), __pos, __sv.size()); |
3053 | } |
3054 | #endif // C++17 |
3055 | |
3056 | /** |
3057 | * @brief Find position of a character not in C substring. |
3058 | * @param __s C string containing characters to avoid. |
3059 | * @param __pos Index of character to search from. |
3060 | * @param __n Number of characters from __s to consider. |
3061 | * @return Index of first occurrence. |
3062 | * |
3063 | * Starting from @a __pos, searches forward for a character not |
3064 | * contained in the first @a __n characters of @a __s within |
3065 | * this string. If found, returns the index where it was |
3066 | * found. If not found, returns npos. |
3067 | */ |
3068 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3069 | size_type |
3070 | find_first_not_of(const _CharT* __s, size_type __pos, |
3071 | size_type __n) const _GLIBCXX_NOEXCEPTnoexcept; |
3072 | |
3073 | /** |
3074 | * @brief Find position of a character not in C string. |
3075 | * @param __s C string containing characters to avoid. |
3076 | * @param __pos Index of character to search from (default 0). |
3077 | * @return Index of first occurrence. |
3078 | * |
3079 | * Starting from @a __pos, searches forward for a character not |
3080 | * contained in @a __s within this string. If found, returns |
3081 | * the index where it was found. If not found, returns npos. |
3082 | */ |
3083 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3084 | size_type |
3085 | find_first_not_of(const _CharT* __s, size_type __pos = 0) const |
3086 | _GLIBCXX_NOEXCEPTnoexcept |
3087 | { |
3088 | __glibcxx_requires_string(__s); |
3089 | return this->find_first_not_of(__s, __pos, traits_type::length(__s)); |
3090 | } |
3091 | |
3092 | /** |
3093 | * @brief Find position of a different character. |
3094 | * @param __c Character to avoid. |
3095 | * @param __pos Index of character to search from (default 0). |
3096 | * @return Index of first occurrence. |
3097 | * |
3098 | * Starting from @a __pos, searches forward for a character |
3099 | * other than @a __c within this string. If found, returns the |
3100 | * index where it was found. If not found, returns npos. |
3101 | */ |
3102 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3103 | size_type |
3104 | find_first_not_of(_CharT __c, size_type __pos = 0) const |
3105 | _GLIBCXX_NOEXCEPTnoexcept; |
3106 | |
3107 | /** |
3108 | * @brief Find last position of a character not in string. |
3109 | * @param __str String containing characters to avoid. |
3110 | * @param __pos Index of character to search back from (default end). |
3111 | * @return Index of last occurrence. |
3112 | * |
3113 | * Starting from @a __pos, searches backward for a character |
3114 | * not contained in @a __str within this string. If found, |
3115 | * returns the index where it was found. If not found, returns |
3116 | * npos. |
3117 | */ |
3118 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3119 | size_type |
3120 | find_last_not_of(const basic_string& __str, size_type __pos = npos) const |
3121 | _GLIBCXX_NOEXCEPTnoexcept |
3122 | { return this->find_last_not_of(__str.data(), __pos, __str.size()); } |
3123 | |
3124 | #if __cplusplus202002L >= 201703L |
3125 | /** |
3126 | * @brief Find last position of a character not in a string_view. |
3127 | * @param __svt An object convertible to string_view containing |
3128 | * characters to avoid. |
3129 | * @param __pos Index of character to search back from (default end). |
3130 | * @return Index of last occurrence. |
3131 | */ |
3132 | template<typename _Tp> |
3133 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3134 | _If_sv<_Tp, size_type> |
3135 | find_last_not_of(const _Tp& __svt, size_type __pos = npos) const |
3136 | noexcept(is_same<_Tp, __sv_type>::value) |
3137 | { |
3138 | __sv_type __sv = __svt; |
3139 | return this->find_last_not_of(__sv.data(), __pos, __sv.size()); |
3140 | } |
3141 | #endif // C++17 |
3142 | |
3143 | /** |
3144 | * @brief Find last position of a character not in C substring. |
3145 | * @param __s C string containing characters to avoid. |
3146 | * @param __pos Index of character to search back from. |
3147 | * @param __n Number of characters from s to consider. |
3148 | * @return Index of last occurrence. |
3149 | * |
3150 | * Starting from @a __pos, searches backward for a character not |
3151 | * contained in the first @a __n characters of @a __s within this string. |
3152 | * If found, returns the index where it was found. If not found, |
3153 | * returns npos. |
3154 | */ |
3155 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3156 | size_type |
3157 | find_last_not_of(const _CharT* __s, size_type __pos, |
3158 | size_type __n) const _GLIBCXX_NOEXCEPTnoexcept; |
3159 | /** |
3160 | * @brief Find last position of a character not in C string. |
3161 | * @param __s C string containing characters to avoid. |
3162 | * @param __pos Index of character to search back from (default end). |
3163 | * @return Index of last occurrence. |
3164 | * |
3165 | * Starting from @a __pos, searches backward for a character |
3166 | * not contained in @a __s within this string. If found, |
3167 | * returns the index where it was found. If not found, returns |
3168 | * npos. |
3169 | */ |
3170 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3171 | size_type |
3172 | find_last_not_of(const _CharT* __s, size_type __pos = npos) const |
3173 | _GLIBCXX_NOEXCEPTnoexcept |
3174 | { |
3175 | __glibcxx_requires_string(__s); |
3176 | return this->find_last_not_of(__s, __pos, traits_type::length(__s)); |
3177 | } |
3178 | |
3179 | /** |
3180 | * @brief Find last position of a different character. |
3181 | * @param __c Character to avoid. |
3182 | * @param __pos Index of character to search back from (default end). |
3183 | * @return Index of last occurrence. |
3184 | * |
3185 | * Starting from @a __pos, searches backward for a character other than |
3186 | * @a __c within this string. If found, returns the index where it was |
3187 | * found. If not found, returns npos. |
3188 | */ |
3189 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3190 | size_type |
3191 | find_last_not_of(_CharT __c, size_type __pos = npos) const |
3192 | _GLIBCXX_NOEXCEPTnoexcept; |
3193 | |
3194 | /** |
3195 | * @brief Get a substring. |
3196 | * @param __pos Index of first character (default 0). |
3197 | * @param __n Number of characters in substring (default remainder). |
3198 | * @return The new string. |
3199 | * @throw std::out_of_range If __pos > size(). |
3200 | * |
3201 | * Construct and return a new string using the @a __n |
3202 | * characters starting at @a __pos. If the string is too |
3203 | * short, use the remainder of the characters. If @a __pos is |
3204 | * beyond the end of the string, out_of_range is thrown. |
3205 | */ |
3206 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3207 | basic_string |
3208 | substr(size_type __pos = 0, size_type __n = npos) const |
3209 | { return basic_string(*this, |
3210 | _M_check(__pos, "basic_string::substr"), __n); } |
3211 | |
3212 | /** |
3213 | * @brief Compare to a string. |
3214 | * @param __str String to compare against. |
3215 | * @return Integer < 0, 0, or > 0. |
3216 | * |
3217 | * Returns an integer < 0 if this string is ordered before @a |
3218 | * __str, 0 if their values are equivalent, or > 0 if this |
3219 | * string is ordered after @a __str. Determines the effective |
3220 | * length rlen of the strings to compare as the smallest of |
3221 | * size() and str.size(). The function then compares the two |
3222 | * strings by calling traits::compare(data(), str.data(),rlen). |
3223 | * If the result of the comparison is nonzero returns it, |
3224 | * otherwise the shorter one is ordered first. |
3225 | */ |
3226 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3227 | int |
3228 | compare(const basic_string& __str) const |
3229 | { |
3230 | const size_type __size = this->size(); |
3231 | const size_type __osize = __str.size(); |
3232 | const size_type __len = std::min(__size, __osize); |
3233 | |
3234 | int __r = traits_type::compare(_M_data(), __str.data(), __len); |
3235 | if (!__r) |
3236 | __r = _S_compare(__size, __osize); |
3237 | return __r; |
3238 | } |
3239 | |
3240 | #if __cplusplus202002L >= 201703L |
3241 | /** |
3242 | * @brief Compare to a string_view. |
3243 | * @param __svt An object convertible to string_view to compare against. |
3244 | * @return Integer < 0, 0, or > 0. |
3245 | */ |
3246 | template<typename _Tp> |
3247 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3248 | _If_sv<_Tp, int> |
3249 | compare(const _Tp& __svt) const |
3250 | noexcept(is_same<_Tp, __sv_type>::value) |
3251 | { |
3252 | __sv_type __sv = __svt; |
3253 | const size_type __size = this->size(); |
3254 | const size_type __osize = __sv.size(); |
3255 | const size_type __len = std::min(__size, __osize); |
3256 | |
3257 | int __r = traits_type::compare(_M_data(), __sv.data(), __len); |
3258 | if (!__r) |
3259 | __r = _S_compare(__size, __osize); |
3260 | return __r; |
3261 | } |
3262 | |
3263 | /** |
3264 | * @brief Compare to a string_view. |
3265 | * @param __pos A position in the string to start comparing from. |
3266 | * @param __n The number of characters to compare. |
3267 | * @param __svt An object convertible to string_view to compare |
3268 | * against. |
3269 | * @return Integer < 0, 0, or > 0. |
3270 | */ |
3271 | template<typename _Tp> |
3272 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3273 | _If_sv<_Tp, int> |
3274 | compare(size_type __pos, size_type __n, const _Tp& __svt) const |
3275 | noexcept(is_same<_Tp, __sv_type>::value) |
3276 | { |
3277 | __sv_type __sv = __svt; |
3278 | return __sv_type(*this).substr(__pos, __n).compare(__sv); |
3279 | } |
3280 | |
3281 | /** |
3282 | * @brief Compare to a string_view. |
3283 | * @param __pos1 A position in the string to start comparing from. |
3284 | * @param __n1 The number of characters to compare. |
3285 | * @param __svt An object convertible to string_view to compare |
3286 | * against. |
3287 | * @param __pos2 A position in the string_view to start comparing from. |
3288 | * @param __n2 The number of characters to compare. |
3289 | * @return Integer < 0, 0, or > 0. |
3290 | */ |
3291 | template<typename _Tp> |
3292 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3293 | _If_sv<_Tp, int> |
3294 | compare(size_type __pos1, size_type __n1, const _Tp& __svt, |
3295 | size_type __pos2, size_type __n2 = npos) const |
3296 | noexcept(is_same<_Tp, __sv_type>::value) |
3297 | { |
3298 | __sv_type __sv = __svt; |
3299 | return __sv_type(*this) |
3300 | .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); |
3301 | } |
3302 | #endif // C++17 |
3303 | |
3304 | /** |
3305 | * @brief Compare substring to a string. |
3306 | * @param __pos Index of first character of substring. |
3307 | * @param __n Number of characters in substring. |
3308 | * @param __str String to compare against. |
3309 | * @return Integer < 0, 0, or > 0. |
3310 | * |
3311 | * Form the substring of this string from the @a __n characters |
3312 | * starting at @a __pos. Returns an integer < 0 if the |
3313 | * substring is ordered before @a __str, 0 if their values are |
3314 | * equivalent, or > 0 if the substring is ordered after @a |
3315 | * __str. Determines the effective length rlen of the strings |
3316 | * to compare as the smallest of the length of the substring |
3317 | * and @a __str.size(). The function then compares the two |
3318 | * strings by calling |
3319 | * traits::compare(substring.data(),str.data(),rlen). If the |
3320 | * result of the comparison is nonzero returns it, otherwise |
3321 | * the shorter one is ordered first. |
3322 | */ |
3323 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3324 | int |
3325 | compare(size_type __pos, size_type __n, const basic_string& __str) const |
3326 | { |
3327 | _M_check(__pos, "basic_string::compare"); |
3328 | __n = _M_limit(__pos, __n); |
3329 | const size_type __osize = __str.size(); |
3330 | const size_type __len = std::min(__n, __osize); |
3331 | int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); |
3332 | if (!__r) |
3333 | __r = _S_compare(__n, __osize); |
3334 | return __r; |
3335 | } |
3336 | |
3337 | /** |
3338 | * @brief Compare substring to a substring. |
3339 | * @param __pos1 Index of first character of substring. |
3340 | * @param __n1 Number of characters in substring. |
3341 | * @param __str String to compare against. |
3342 | * @param __pos2 Index of first character of substring of str. |
3343 | * @param __n2 Number of characters in substring of str. |
3344 | * @return Integer < 0, 0, or > 0. |
3345 | * |
3346 | * Form the substring of this string from the @a __n1 |
3347 | * characters starting at @a __pos1. Form the substring of @a |
3348 | * __str from the @a __n2 characters starting at @a __pos2. |
3349 | * Returns an integer < 0 if this substring is ordered before |
3350 | * the substring of @a __str, 0 if their values are equivalent, |
3351 | * or > 0 if this substring is ordered after the substring of |
3352 | * @a __str. Determines the effective length rlen of the |
3353 | * strings to compare as the smallest of the lengths of the |
3354 | * substrings. The function then compares the two strings by |
3355 | * calling |
3356 | * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). |
3357 | * If the result of the comparison is nonzero returns it, |
3358 | * otherwise the shorter one is ordered first. |
3359 | */ |
3360 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3361 | int |
3362 | compare(size_type __pos1, size_type __n1, const basic_string& __str, |
3363 | size_type __pos2, size_type __n2 = npos) const |
3364 | { |
3365 | _M_check(__pos1, "basic_string::compare"); |
3366 | __str._M_check(__pos2, "basic_string::compare"); |
3367 | __n1 = _M_limit(__pos1, __n1); |
3368 | __n2 = __str._M_limit(__pos2, __n2); |
3369 | const size_type __len = std::min(__n1, __n2); |
3370 | int __r = traits_type::compare(_M_data() + __pos1, |
3371 | __str.data() + __pos2, __len); |
3372 | if (!__r) |
3373 | __r = _S_compare(__n1, __n2); |
3374 | return __r; |
3375 | } |
3376 | |
3377 | /** |
3378 | * @brief Compare to a C string. |
3379 | * @param __s C string to compare against. |
3380 | * @return Integer < 0, 0, or > 0. |
3381 | * |
3382 | * Returns an integer < 0 if this string is ordered before @a __s, 0 if |
3383 | * their values are equivalent, or > 0 if this string is ordered after |
3384 | * @a __s. Determines the effective length rlen of the strings to |
3385 | * compare as the smallest of size() and the length of a string |
3386 | * constructed from @a __s. The function then compares the two strings |
3387 | * by calling traits::compare(data(),s,rlen). If the result of the |
3388 | * comparison is nonzero returns it, otherwise the shorter one is |
3389 | * ordered first. |
3390 | */ |
3391 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3392 | int |
3393 | compare(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept |
3394 | { |
3395 | __glibcxx_requires_string(__s); |
3396 | const size_type __size = this->size(); |
3397 | const size_type __osize = traits_type::length(__s); |
3398 | const size_type __len = std::min(__size, __osize); |
3399 | int __r = traits_type::compare(_M_data(), __s, __len); |
3400 | if (!__r) |
3401 | __r = _S_compare(__size, __osize); |
3402 | return __r; |
3403 | } |
3404 | |
3405 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
3406 | // 5 String::compare specification questionable |
3407 | /** |
3408 | * @brief Compare substring to a C string. |
3409 | * @param __pos Index of first character of substring. |
3410 | * @param __n1 Number of characters in substring. |
3411 | * @param __s C string to compare against. |
3412 | * @return Integer < 0, 0, or > 0. |
3413 | * |
3414 | * Form the substring of this string from the @a __n1 |
3415 | * characters starting at @a pos. Returns an integer < 0 if |
3416 | * the substring is ordered before @a __s, 0 if their values |
3417 | * are equivalent, or > 0 if the substring is ordered after @a |
3418 | * __s. Determines the effective length rlen of the strings to |
3419 | * compare as the smallest of the length of the substring and |
3420 | * the length of a string constructed from @a __s. The |
3421 | * function then compares the two string by calling |
3422 | * traits::compare(substring.data(),__s,rlen). If the result of |
3423 | * the comparison is nonzero returns it, otherwise the shorter |
3424 | * one is ordered first. |
3425 | */ |
3426 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3427 | int |
3428 | compare(size_type __pos, size_type __n1, const _CharT* __s) const |
3429 | { |
3430 | __glibcxx_requires_string(__s); |
3431 | _M_check(__pos, "basic_string::compare"); |
3432 | __n1 = _M_limit(__pos, __n1); |
3433 | const size_type __osize = traits_type::length(__s); |
3434 | const size_type __len = std::min(__n1, __osize); |
3435 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
3436 | if (!__r) |
3437 | __r = _S_compare(__n1, __osize); |
3438 | return __r; |
3439 | } |
3440 | |
3441 | /** |
3442 | * @brief Compare substring against a character %array. |
3443 | * @param __pos Index of first character of substring. |
3444 | * @param __n1 Number of characters in substring. |
3445 | * @param __s character %array to compare against. |
3446 | * @param __n2 Number of characters of s. |
3447 | * @return Integer < 0, 0, or > 0. |
3448 | * |
3449 | * Form the substring of this string from the @a __n1 |
3450 | * characters starting at @a __pos. Form a string from the |
3451 | * first @a __n2 characters of @a __s. Returns an integer < 0 |
3452 | * if this substring is ordered before the string from @a __s, |
3453 | * 0 if their values are equivalent, or > 0 if this substring |
3454 | * is ordered after the string from @a __s. Determines the |
3455 | * effective length rlen of the strings to compare as the |
3456 | * smallest of the length of the substring and @a __n2. The |
3457 | * function then compares the two strings by calling |
3458 | * traits::compare(substring.data(),s,rlen). If the result of |
3459 | * the comparison is nonzero returns it, otherwise the shorter |
3460 | * one is ordered first. |
3461 | * |
3462 | * NB: s must have at least n2 characters, '\\0' has |
3463 | * no special meaning. |
3464 | */ |
3465 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3466 | int |
3467 | compare(size_type __pos, size_type __n1, const _CharT* __s, |
3468 | size_type __n2) const |
3469 | { |
3470 | __glibcxx_requires_string_len(__s, __n2); |
3471 | _M_check(__pos, "basic_string::compare"); |
3472 | __n1 = _M_limit(__pos, __n1); |
3473 | const size_type __len = std::min(__n1, __n2); |
3474 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
3475 | if (!__r) |
3476 | __r = _S_compare(__n1, __n2); |
3477 | return __r; |
3478 | } |
3479 | |
3480 | #if __cplusplus202002L >= 202002L |
3481 | [[nodiscard]] |
3482 | constexpr bool |
3483 | starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
3484 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
3485 | |
3486 | [[nodiscard]] |
3487 | constexpr bool |
3488 | starts_with(_CharT __x) const noexcept |
3489 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
3490 | |
3491 | [[nodiscard, __gnu__::__nonnull__]] |
3492 | constexpr bool |
3493 | starts_with(const _CharT* __x) const noexcept |
3494 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
3495 | |
3496 | [[nodiscard]] |
3497 | constexpr bool |
3498 | ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
3499 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
3500 | |
3501 | [[nodiscard]] |
3502 | constexpr bool |
3503 | ends_with(_CharT __x) const noexcept |
3504 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
3505 | |
3506 | [[nodiscard, __gnu__::__nonnull__]] |
3507 | constexpr bool |
3508 | ends_with(const _CharT* __x) const noexcept |
3509 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
3510 | #endif // C++20 |
3511 | |
3512 | #if __cplusplus202002L > 202002L |
3513 | [[nodiscard]] |
3514 | constexpr bool |
3515 | contains(basic_string_view<_CharT, _Traits> __x) const noexcept |
3516 | { return __sv_type(this->data(), this->size()).contains(__x); } |
3517 | |
3518 | [[nodiscard]] |
3519 | constexpr bool |
3520 | contains(_CharT __x) const noexcept |
3521 | { return __sv_type(this->data(), this->size()).contains(__x); } |
3522 | |
3523 | [[nodiscard, __gnu__::__nonnull__]] |
3524 | constexpr bool |
3525 | contains(const _CharT* __x) const noexcept |
3526 | { return __sv_type(this->data(), this->size()).contains(__x); } |
3527 | #endif // C++23 |
3528 | |
3529 | // Allow basic_stringbuf::__xfer_bufptrs to call _M_length: |
3530 | template<typename, typename, typename> friend class basic_stringbuf; |
3531 | }; |
3532 | _GLIBCXX_END_NAMESPACE_CXX11} |
3533 | _GLIBCXX_END_NAMESPACE_VERSION |
3534 | } // namespace std |
3535 | #endif // _GLIBCXX_USE_CXX11_ABI |
3536 | |
3537 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
3538 | { |
3539 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
3540 | |
3541 | #if __cpp_deduction_guides201703L >= 201606 |
3542 | _GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 { |
3543 | template<typename _InputIterator, typename _CharT |
3544 | = typename iterator_traits<_InputIterator>::value_type, |
3545 | typename _Allocator = allocator<_CharT>, |
3546 | typename = _RequireInputIter<_InputIterator>, |
3547 | typename = _RequireAllocator<_Allocator>> |
3548 | basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator()) |
3549 | -> basic_string<_CharT, char_traits<_CharT>, _Allocator>; |
3550 | |
3551 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
3552 | // 3075. basic_string needs deduction guides from basic_string_view |
3553 | template<typename _CharT, typename _Traits, |
3554 | typename _Allocator = allocator<_CharT>, |
3555 | typename = _RequireAllocator<_Allocator>> |
3556 | basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator()) |
3557 | -> basic_string<_CharT, _Traits, _Allocator>; |
3558 | |
3559 | template<typename _CharT, typename _Traits, |
3560 | typename _Allocator = allocator<_CharT>, |
3561 | typename = _RequireAllocator<_Allocator>> |
3562 | basic_string(basic_string_view<_CharT, _Traits>, |
3563 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, |
3564 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, |
3565 | const _Allocator& = _Allocator()) |
3566 | -> basic_string<_CharT, _Traits, _Allocator>; |
3567 | _GLIBCXX_END_NAMESPACE_CXX11} |
3568 | #endif |
3569 | |
3570 | template<typename _Str> |
3571 | _GLIBCXX20_CONSTEXPRconstexpr |
3572 | inline _Str |
3573 | __str_concat(typename _Str::value_type const* __lhs, |
3574 | typename _Str::size_type __lhs_len, |
3575 | typename _Str::value_type const* __rhs, |
3576 | typename _Str::size_type __rhs_len, |
3577 | typename _Str::allocator_type const& __a) |
3578 | { |
3579 | typedef typename _Str::allocator_type allocator_type; |
3580 | typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits; |
3581 | _Str __str(_Alloc_traits::_S_select_on_copy(__a)); |
3582 | __str.reserve(__lhs_len + __rhs_len); |
3583 | __str.append(__lhs, __lhs_len); |
3584 | __str.append(__rhs, __rhs_len); |
3585 | return __str; |
3586 | } |
3587 | |
3588 | // operator+ |
3589 | /** |
3590 | * @brief Concatenate two strings. |
3591 | * @param __lhs First string. |
3592 | * @param __rhs Last string. |
3593 | * @return New string with value of @a __lhs followed by @a __rhs. |
3594 | */ |
3595 | template<typename _CharT, typename _Traits, typename _Alloc> |
3596 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3597 | inline basic_string<_CharT, _Traits, _Alloc> |
3598 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3599 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3600 | { |
3601 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
3602 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), |
3603 | __rhs.c_str(), __rhs.size(), |
3604 | __lhs.get_allocator()); |
3605 | } |
3606 | |
3607 | /** |
3608 | * @brief Concatenate C string and string. |
3609 | * @param __lhs First string. |
3610 | * @param __rhs Last string. |
3611 | * @return New string with value of @a __lhs followed by @a __rhs. |
3612 | */ |
3613 | template<typename _CharT, typename _Traits, typename _Alloc> |
3614 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3615 | inline basic_string<_CharT,_Traits,_Alloc> |
3616 | operator+(const _CharT* __lhs, |
3617 | const basic_string<_CharT,_Traits,_Alloc>& __rhs) |
3618 | { |
3619 | __glibcxx_requires_string(__lhs); |
3620 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
3621 | return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs), |
3622 | __rhs.c_str(), __rhs.size(), |
3623 | __rhs.get_allocator()); |
3624 | } |
3625 | |
3626 | /** |
3627 | * @brief Concatenate character and string. |
3628 | * @param __lhs First string. |
3629 | * @param __rhs Last string. |
3630 | * @return New string with @a __lhs followed by @a __rhs. |
3631 | */ |
3632 | template<typename _CharT, typename _Traits, typename _Alloc> |
3633 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3634 | inline basic_string<_CharT,_Traits,_Alloc> |
3635 | operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs) |
3636 | { |
3637 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
3638 | return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1, |
3639 | __rhs.c_str(), __rhs.size(), |
3640 | __rhs.get_allocator()); |
3641 | } |
3642 | |
3643 | /** |
3644 | * @brief Concatenate string and C string. |
3645 | * @param __lhs First string. |
3646 | * @param __rhs Last string. |
3647 | * @return New string with @a __lhs followed by @a __rhs. |
3648 | */ |
3649 | template<typename _CharT, typename _Traits, typename _Alloc> |
3650 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3651 | inline basic_string<_CharT, _Traits, _Alloc> |
3652 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3653 | const _CharT* __rhs) |
3654 | { |
3655 | __glibcxx_requires_string(__rhs); |
3656 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
3657 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), |
3658 | __rhs, _Traits::length(__rhs), |
3659 | __lhs.get_allocator()); |
3660 | } |
3661 | /** |
3662 | * @brief Concatenate string and character. |
3663 | * @param __lhs First string. |
3664 | * @param __rhs Last string. |
3665 | * @return New string with @a __lhs followed by @a __rhs. |
3666 | */ |
3667 | template<typename _CharT, typename _Traits, typename _Alloc> |
3668 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3669 | inline basic_string<_CharT, _Traits, _Alloc> |
3670 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) |
3671 | { |
3672 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
3673 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), |
3674 | __builtin_addressof(__rhs), 1, |
3675 | __lhs.get_allocator()); |
3676 | } |
3677 | |
3678 | #if __cplusplus202002L >= 201103L |
3679 | template<typename _CharT, typename _Traits, typename _Alloc> |
3680 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3681 | inline basic_string<_CharT, _Traits, _Alloc> |
3682 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
3683 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3684 | { return std::move(__lhs.append(__rhs)); } |
3685 | |
3686 | template<typename _CharT, typename _Traits, typename _Alloc> |
3687 | _GLIBCXX20_CONSTEXPRconstexpr |
3688 | inline basic_string<_CharT, _Traits, _Alloc> |
3689 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3690 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
3691 | { return std::move(__rhs.insert(0, __lhs)); } |
3692 | |
3693 | template<typename _CharT, typename _Traits, typename _Alloc> |
3694 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3695 | inline basic_string<_CharT, _Traits, _Alloc> |
3696 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
3697 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
3698 | { |
3699 | #if _GLIBCXX_USE_CXX11_ABI1 |
3700 | using _Alloc_traits = allocator_traits<_Alloc>; |
3701 | bool __use_rhs = false; |
3702 | if _GLIBCXX17_CONSTEXPRconstexpr (typename _Alloc_traits::is_always_equal{}) |
3703 | __use_rhs = true; |
3704 | else if (__lhs.get_allocator() == __rhs.get_allocator()) |
3705 | __use_rhs = true; |
3706 | if (__use_rhs) |
3707 | #endif |
3708 | { |
3709 | const auto __size = __lhs.size() + __rhs.size(); |
3710 | if (__size > __lhs.capacity() && __size <= __rhs.capacity()) |
3711 | return std::move(__rhs.insert(0, __lhs)); |
3712 | } |
3713 | return std::move(__lhs.append(__rhs)); |
3714 | } |
3715 | |
3716 | template<typename _CharT, typename _Traits, typename _Alloc> |
3717 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3718 | inline basic_string<_CharT, _Traits, _Alloc> |
3719 | operator+(const _CharT* __lhs, |
3720 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
3721 | { return std::move(__rhs.insert(0, __lhs)); } |
3722 | |
3723 | template<typename _CharT, typename _Traits, typename _Alloc> |
3724 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3725 | inline basic_string<_CharT, _Traits, _Alloc> |
3726 | operator+(_CharT __lhs, |
3727 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
3728 | { return std::move(__rhs.insert(0, 1, __lhs)); } |
3729 | |
3730 | template<typename _CharT, typename _Traits, typename _Alloc> |
3731 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3732 | inline basic_string<_CharT, _Traits, _Alloc> |
3733 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
3734 | const _CharT* __rhs) |
3735 | { return std::move(__lhs.append(__rhs)); } |
3736 | |
3737 | template<typename _CharT, typename _Traits, typename _Alloc> |
3738 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3739 | inline basic_string<_CharT, _Traits, _Alloc> |
3740 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
3741 | _CharT __rhs) |
3742 | { return std::move(__lhs.append(1, __rhs)); } |
3743 | #endif |
3744 | |
3745 | // operator == |
3746 | /** |
3747 | * @brief Test equivalence of two strings. |
3748 | * @param __lhs First string. |
3749 | * @param __rhs Second string. |
3750 | * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. |
3751 | */ |
3752 | template<typename _CharT, typename _Traits, typename _Alloc> |
3753 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3754 | inline bool |
3755 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3756 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3757 | _GLIBCXX_NOEXCEPTnoexcept |
3758 | { |
3759 | return __lhs.size() == __rhs.size() |
3760 | && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()); |
3761 | } |
3762 | |
3763 | /** |
3764 | * @brief Test equivalence of string and C string. |
3765 | * @param __lhs String. |
3766 | * @param __rhs C string. |
3767 | * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. |
3768 | */ |
3769 | template<typename _CharT, typename _Traits, typename _Alloc> |
3770 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPRconstexpr |
3771 | inline bool |
3772 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3773 | const _CharT* __rhs) |
3774 | { |
3775 | return __lhs.size() == _Traits::length(__rhs) |
3776 | && !_Traits::compare(__lhs.data(), __rhs, __lhs.size()); |
3777 | } |
3778 | |
3779 | #if __cpp_lib_three_way_comparison201907L |
3780 | /** |
3781 | * @brief Three-way comparison of a string and a C string. |
3782 | * @param __lhs A string. |
3783 | * @param __rhs A null-terminated string. |
3784 | * @return A value indicating whether `__lhs` is less than, equal to, |
3785 | * greater than, or incomparable with `__rhs`. |
3786 | */ |
3787 | template<typename _CharT, typename _Traits, typename _Alloc> |
3788 | [[nodiscard]] |
3789 | constexpr auto |
3790 | operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3791 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept |
3792 | -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0)) |
3793 | { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); } |
3794 | |
3795 | /** |
3796 | * @brief Three-way comparison of a string and a C string. |
3797 | * @param __lhs A string. |
3798 | * @param __rhs A null-terminated string. |
3799 | * @return A value indicating whether `__lhs` is less than, equal to, |
3800 | * greater than, or incomparable with `__rhs`. |
3801 | */ |
3802 | template<typename _CharT, typename _Traits, typename _Alloc> |
3803 | [[nodiscard]] |
3804 | constexpr auto |
3805 | operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3806 | const _CharT* __rhs) noexcept |
3807 | -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0)) |
3808 | { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); } |
3809 | #else |
3810 | /** |
3811 | * @brief Test equivalence of C string and string. |
3812 | * @param __lhs C string. |
3813 | * @param __rhs String. |
3814 | * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise. |
3815 | */ |
3816 | template<typename _CharT, typename _Traits, typename _Alloc> |
3817 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3818 | inline bool |
3819 | operator==(const _CharT* __lhs, |
3820 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3821 | { return __rhs == __lhs; } |
3822 | |
3823 | // operator != |
3824 | /** |
3825 | * @brief Test difference of two strings. |
3826 | * @param __lhs First string. |
3827 | * @param __rhs Second string. |
3828 | * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. |
3829 | */ |
3830 | template<typename _CharT, typename _Traits, typename _Alloc> |
3831 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3832 | inline bool |
3833 | operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3834 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3835 | _GLIBCXX_NOEXCEPTnoexcept |
3836 | { return !(__lhs == __rhs); } |
3837 | |
3838 | /** |
3839 | * @brief Test difference of C string and string. |
3840 | * @param __lhs C string. |
3841 | * @param __rhs String. |
3842 | * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise. |
3843 | */ |
3844 | template<typename _CharT, typename _Traits, typename _Alloc> |
3845 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3846 | inline bool |
3847 | operator!=(const _CharT* __lhs, |
3848 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3849 | { return !(__rhs == __lhs); } |
3850 | |
3851 | /** |
3852 | * @brief Test difference of string and C string. |
3853 | * @param __lhs String. |
3854 | * @param __rhs C string. |
3855 | * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. |
3856 | */ |
3857 | template<typename _CharT, typename _Traits, typename _Alloc> |
3858 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3859 | inline bool |
3860 | operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3861 | const _CharT* __rhs) |
3862 | { return !(__lhs == __rhs); } |
3863 | |
3864 | // operator < |
3865 | /** |
3866 | * @brief Test if string precedes string. |
3867 | * @param __lhs First string. |
3868 | * @param __rhs Second string. |
3869 | * @return True if @a __lhs precedes @a __rhs. False otherwise. |
3870 | */ |
3871 | template<typename _CharT, typename _Traits, typename _Alloc> |
3872 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3873 | inline bool |
3874 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3875 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3876 | _GLIBCXX_NOEXCEPTnoexcept |
3877 | { return __lhs.compare(__rhs) < 0; } |
3878 | |
3879 | /** |
3880 | * @brief Test if string precedes C string. |
3881 | * @param __lhs String. |
3882 | * @param __rhs C string. |
3883 | * @return True if @a __lhs precedes @a __rhs. False otherwise. |
3884 | */ |
3885 | template<typename _CharT, typename _Traits, typename _Alloc> |
3886 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3887 | inline bool |
3888 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3889 | const _CharT* __rhs) |
3890 | { return __lhs.compare(__rhs) < 0; } |
3891 | |
3892 | /** |
3893 | * @brief Test if C string precedes string. |
3894 | * @param __lhs C string. |
3895 | * @param __rhs String. |
3896 | * @return True if @a __lhs precedes @a __rhs. False otherwise. |
3897 | */ |
3898 | template<typename _CharT, typename _Traits, typename _Alloc> |
3899 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3900 | inline bool |
3901 | operator<(const _CharT* __lhs, |
3902 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3903 | { return __rhs.compare(__lhs) > 0; } |
3904 | |
3905 | // operator > |
3906 | /** |
3907 | * @brief Test if string follows string. |
3908 | * @param __lhs First string. |
3909 | * @param __rhs Second string. |
3910 | * @return True if @a __lhs follows @a __rhs. False otherwise. |
3911 | */ |
3912 | template<typename _CharT, typename _Traits, typename _Alloc> |
3913 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3914 | inline bool |
3915 | operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3916 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3917 | _GLIBCXX_NOEXCEPTnoexcept |
3918 | { return __lhs.compare(__rhs) > 0; } |
3919 | |
3920 | /** |
3921 | * @brief Test if string follows C string. |
3922 | * @param __lhs String. |
3923 | * @param __rhs C string. |
3924 | * @return True if @a __lhs follows @a __rhs. False otherwise. |
3925 | */ |
3926 | template<typename _CharT, typename _Traits, typename _Alloc> |
3927 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3928 | inline bool |
3929 | operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3930 | const _CharT* __rhs) |
3931 | { return __lhs.compare(__rhs) > 0; } |
3932 | |
3933 | /** |
3934 | * @brief Test if C string follows string. |
3935 | * @param __lhs C string. |
3936 | * @param __rhs String. |
3937 | * @return True if @a __lhs follows @a __rhs. False otherwise. |
3938 | */ |
3939 | template<typename _CharT, typename _Traits, typename _Alloc> |
3940 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3941 | inline bool |
3942 | operator>(const _CharT* __lhs, |
3943 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3944 | { return __rhs.compare(__lhs) < 0; } |
3945 | |
3946 | // operator <= |
3947 | /** |
3948 | * @brief Test if string doesn't follow string. |
3949 | * @param __lhs First string. |
3950 | * @param __rhs Second string. |
3951 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. |
3952 | */ |
3953 | template<typename _CharT, typename _Traits, typename _Alloc> |
3954 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3955 | inline bool |
3956 | operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3957 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3958 | _GLIBCXX_NOEXCEPTnoexcept |
3959 | { return __lhs.compare(__rhs) <= 0; } |
3960 | |
3961 | /** |
3962 | * @brief Test if string doesn't follow C string. |
3963 | * @param __lhs String. |
3964 | * @param __rhs C string. |
3965 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. |
3966 | */ |
3967 | template<typename _CharT, typename _Traits, typename _Alloc> |
3968 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3969 | inline bool |
3970 | operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3971 | const _CharT* __rhs) |
3972 | { return __lhs.compare(__rhs) <= 0; } |
3973 | |
3974 | /** |
3975 | * @brief Test if C string doesn't follow string. |
3976 | * @param __lhs C string. |
3977 | * @param __rhs String. |
3978 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. |
3979 | */ |
3980 | template<typename _CharT, typename _Traits, typename _Alloc> |
3981 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3982 | inline bool |
3983 | operator<=(const _CharT* __lhs, |
3984 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3985 | { return __rhs.compare(__lhs) >= 0; } |
3986 | |
3987 | // operator >= |
3988 | /** |
3989 | * @brief Test if string doesn't precede string. |
3990 | * @param __lhs First string. |
3991 | * @param __rhs Second string. |
3992 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. |
3993 | */ |
3994 | template<typename _CharT, typename _Traits, typename _Alloc> |
3995 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
3996 | inline bool |
3997 | operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
3998 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
3999 | _GLIBCXX_NOEXCEPTnoexcept |
4000 | { return __lhs.compare(__rhs) >= 0; } |
4001 | |
4002 | /** |
4003 | * @brief Test if string doesn't precede C string. |
4004 | * @param __lhs String. |
4005 | * @param __rhs C string. |
4006 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. |
4007 | */ |
4008 | template<typename _CharT, typename _Traits, typename _Alloc> |
4009 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4010 | inline bool |
4011 | operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
4012 | const _CharT* __rhs) |
4013 | { return __lhs.compare(__rhs) >= 0; } |
4014 | |
4015 | /** |
4016 | * @brief Test if C string doesn't precede string. |
4017 | * @param __lhs C string. |
4018 | * @param __rhs String. |
4019 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. |
4020 | */ |
4021 | template<typename _CharT, typename _Traits, typename _Alloc> |
4022 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4023 | inline bool |
4024 | operator>=(const _CharT* __lhs, |
4025 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
4026 | { return __rhs.compare(__lhs) <= 0; } |
4027 | #endif // three-way comparison |
4028 | |
4029 | /** |
4030 | * @brief Swap contents of two strings. |
4031 | * @param __lhs First string. |
4032 | * @param __rhs Second string. |
4033 | * |
4034 | * Exchanges the contents of @a __lhs and @a __rhs in constant time. |
4035 | */ |
4036 | template<typename _CharT, typename _Traits, typename _Alloc> |
4037 | _GLIBCXX20_CONSTEXPRconstexpr |
4038 | inline void |
4039 | swap(basic_string<_CharT, _Traits, _Alloc>& __lhs, |
4040 | basic_string<_CharT, _Traits, _Alloc>& __rhs) |
4041 | _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))noexcept(noexcept(__lhs.swap(__rhs))) |
4042 | { __lhs.swap(__rhs); } |
4043 | |
4044 | |
4045 | /** |
4046 | * @brief Read stream into a string. |
4047 | * @param __is Input stream. |
4048 | * @param __str Buffer to store into. |
4049 | * @return Reference to the input stream. |
4050 | * |
4051 | * Stores characters from @a __is into @a __str until whitespace is |
4052 | * found, the end of the stream is encountered, or str.max_size() |
4053 | * is reached. If is.width() is non-zero, that is the limit on the |
4054 | * number of characters stored into @a __str. Any previous |
4055 | * contents of @a __str are erased. |
4056 | */ |
4057 | template<typename _CharT, typename _Traits, typename _Alloc> |
4058 | basic_istream<_CharT, _Traits>& |
4059 | operator>>(basic_istream<_CharT, _Traits>& __is, |
4060 | basic_string<_CharT, _Traits, _Alloc>& __str); |
4061 | |
4062 | template<> |
4063 | basic_istream<char>& |
4064 | operator>>(basic_istream<char>& __is, basic_string<char>& __str); |
4065 | |
4066 | /** |
4067 | * @brief Write string to a stream. |
4068 | * @param __os Output stream. |
4069 | * @param __str String to write out. |
4070 | * @return Reference to the output stream. |
4071 | * |
4072 | * Output characters of @a __str into os following the same rules as for |
4073 | * writing a C string. |
4074 | */ |
4075 | template<typename _CharT, typename _Traits, typename _Alloc> |
4076 | inline basic_ostream<_CharT, _Traits>& |
4077 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
4078 | const basic_string<_CharT, _Traits, _Alloc>& __str) |
4079 | { |
4080 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
4081 | // 586. string inserter not a formatted function |
4082 | return __ostream_insert(__os, __str.data(), __str.size()); |
4083 | } |
4084 | |
4085 | /** |
4086 | * @brief Read a line from stream into a string. |
4087 | * @param __is Input stream. |
4088 | * @param __str Buffer to store into. |
4089 | * @param __delim Character marking end of line. |
4090 | * @return Reference to the input stream. |
4091 | * |
4092 | * Stores characters from @a __is into @a __str until @a __delim is |
4093 | * found, the end of the stream is encountered, or str.max_size() |
4094 | * is reached. Any previous contents of @a __str are erased. If |
4095 | * @a __delim is encountered, it is extracted but not stored into |
4096 | * @a __str. |
4097 | */ |
4098 | template<typename _CharT, typename _Traits, typename _Alloc> |
4099 | basic_istream<_CharT, _Traits>& |
4100 | getline(basic_istream<_CharT, _Traits>& __is, |
4101 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); |
4102 | |
4103 | /** |
4104 | * @brief Read a line from stream into a string. |
4105 | * @param __is Input stream. |
4106 | * @param __str Buffer to store into. |
4107 | * @return Reference to the input stream. |
4108 | * |
4109 | * Stores characters from is into @a __str until '\n' is |
4110 | * found, the end of the stream is encountered, or str.max_size() |
4111 | * is reached. Any previous contents of @a __str are erased. If |
4112 | * end of line is encountered, it is extracted but not stored into |
4113 | * @a __str. |
4114 | */ |
4115 | template<typename _CharT, typename _Traits, typename _Alloc> |
4116 | inline basic_istream<_CharT, _Traits>& |
4117 | getline(basic_istream<_CharT, _Traits>& __is, |
4118 | basic_string<_CharT, _Traits, _Alloc>& __str) |
4119 | { return std::getline(__is, __str, __is.widen('\n')); } |
4120 | |
4121 | #if __cplusplus202002L >= 201103L |
4122 | /// Read a line from an rvalue stream into a string. |
4123 | template<typename _CharT, typename _Traits, typename _Alloc> |
4124 | inline basic_istream<_CharT, _Traits>& |
4125 | getline(basic_istream<_CharT, _Traits>&& __is, |
4126 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) |
4127 | { return std::getline(__is, __str, __delim); } |
4128 | |
4129 | /// Read a line from an rvalue stream into a string. |
4130 | template<typename _CharT, typename _Traits, typename _Alloc> |
4131 | inline basic_istream<_CharT, _Traits>& |
4132 | getline(basic_istream<_CharT, _Traits>&& __is, |
4133 | basic_string<_CharT, _Traits, _Alloc>& __str) |
4134 | { return std::getline(__is, __str); } |
4135 | #endif |
4136 | |
4137 | template<> |
4138 | basic_istream<char>& |
4139 | getline(basic_istream<char>& __in, basic_string<char>& __str, |
4140 | char __delim); |
4141 | |
4142 | #ifdef _GLIBCXX_USE_WCHAR_T1 |
4143 | template<> |
4144 | basic_istream<wchar_t>& |
4145 | getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str, |
4146 | wchar_t __delim); |
4147 | #endif |
4148 | |
4149 | _GLIBCXX_END_NAMESPACE_VERSION |
4150 | } // namespace |
4151 | |
4152 | #if __cplusplus202002L >= 201103L |
4153 | |
4154 | #include <ext/string_conversions.h> |
4155 | #include <bits/charconv.h> |
4156 | |
4157 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
4158 | { |
4159 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
4160 | _GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 { |
4161 | |
4162 | // 21.4 Numeric Conversions [string.conversions]. |
4163 | inline int |
4164 | stoi(const string& __str, size_t* __idx = 0, int __base = 10) |
4165 | { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(), |
4166 | __idx, __base); } |
4167 | |
4168 | inline long |
4169 | stol(const string& __str, size_t* __idx = 0, int __base = 10) |
4170 | { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(), |
4171 | __idx, __base); } |
4172 | |
4173 | inline unsigned long |
4174 | stoul(const string& __str, size_t* __idx = 0, int __base = 10) |
4175 | { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), |
4176 | __idx, __base); } |
4177 | |
4178 | #if _GLIBCXX_USE_C99_STDLIB1 |
4179 | inline long long |
4180 | stoll(const string& __str, size_t* __idx = 0, int __base = 10) |
4181 | { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(), |
4182 | __idx, __base); } |
4183 | |
4184 | inline unsigned long long |
4185 | stoull(const string& __str, size_t* __idx = 0, int __base = 10) |
4186 | { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(), |
4187 | __idx, __base); } |
4188 | #elif __LONG_WIDTH__64 == __LONG_LONG_WIDTH__ |
4189 | inline long long |
4190 | stoll(const string& __str, size_t* __idx = 0, int __base = 10) |
4191 | { return std::stol(__str, __idx, __base); } |
4192 | |
4193 | inline unsigned long long |
4194 | stoull(const string& __str, size_t* __idx = 0, int __base = 10) |
4195 | { return std::stoul(__str, __idx, __base); } |
4196 | #endif |
4197 | |
4198 | inline double |
4199 | stod(const string& __str, size_t* __idx = 0) |
4200 | { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); } |
4201 | |
4202 | #if _GLIBCXX_HAVE_STRTOF1 |
4203 | // NB: strtof vs strtod. |
4204 | inline float |
4205 | stof(const string& __str, size_t* __idx = 0) |
4206 | { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } |
4207 | #else |
4208 | inline float |
4209 | stof(const string& __str, size_t* __idx = 0) |
4210 | { |
4211 | double __d = std::stod(__str, __idx); |
4212 | if (__builtin_isfinite(__d) && __d != 0.0) |
4213 | { |
4214 | double __abs_d = __builtin_fabs(__d); |
4215 | if (__abs_d < __FLT_MIN__1.17549435e-38F || __abs_d > __FLT_MAX__3.40282347e+38F) |
4216 | { |
4217 | errno(*__errno_location ()) = ERANGE34; |
4218 | std::__throw_out_of_range("stof"); |
4219 | } |
4220 | } |
4221 | return __d; |
4222 | } |
4223 | #endif |
4224 | |
4225 | #if _GLIBCXX_HAVE_STRTOLD1 && ! _GLIBCXX_HAVE_BROKEN_STRTOLD |
4226 | inline long double |
4227 | stold(const string& __str, size_t* __idx = 0) |
4228 | { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); } |
4229 | #elif __DBL_MANT_DIG__53 == __LDBL_MANT_DIG__64 |
4230 | inline long double |
4231 | stold(const string& __str, size_t* __idx = 0) |
4232 | { return std::stod(__str, __idx); } |
4233 | #endif |
4234 | |
4235 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
4236 | // DR 1261. Insufficent overloads for to_string / to_wstring |
4237 | |
4238 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4239 | inline string |
4240 | to_string(int __val) |
4241 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_INT__4) <= 32 |
4242 | noexcept // any 32-bit value fits in the SSO buffer |
4243 | #endif |
4244 | { |
4245 | const bool __neg = __val < 0; |
4246 | const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val; |
4247 | const auto __len = __detail::__to_chars_len(__uval); |
4248 | string __str; |
4249 | __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) { |
4250 | __p[0] = '-'; |
4251 | __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval); |
4252 | return __n; |
4253 | }); |
4254 | return __str; |
4255 | } |
4256 | |
4257 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4258 | inline string |
4259 | to_string(unsigned __val) |
4260 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_INT__4) <= 32 |
4261 | noexcept // any 32-bit value fits in the SSO buffer |
4262 | #endif |
4263 | { |
4264 | const auto __len = __detail::__to_chars_len(__val); |
4265 | string __str; |
4266 | __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) { |
4267 | __detail::__to_chars_10_impl(__p, __n, __val); |
4268 | return __n; |
4269 | }); |
4270 | return __str; |
4271 | } |
4272 | |
4273 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4274 | inline string |
4275 | to_string(long __val) |
4276 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_LONG__8) <= 32 |
4277 | noexcept // any 32-bit value fits in the SSO buffer |
4278 | #endif |
4279 | { |
4280 | const bool __neg = __val < 0; |
4281 | const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val; |
4282 | const auto __len = __detail::__to_chars_len(__uval); |
4283 | string __str; |
4284 | __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) { |
4285 | __p[0] = '-'; |
4286 | __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval); |
4287 | return __n; |
4288 | }); |
4289 | return __str; |
4290 | } |
4291 | |
4292 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4293 | inline string |
4294 | to_string(unsigned long __val) |
4295 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_LONG__8) <= 32 |
4296 | noexcept // any 32-bit value fits in the SSO buffer |
4297 | #endif |
4298 | { |
4299 | const auto __len = __detail::__to_chars_len(__val); |
4300 | string __str; |
4301 | __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) { |
4302 | __detail::__to_chars_10_impl(__p, __n, __val); |
4303 | return __n; |
4304 | }); |
4305 | return __str; |
4306 | } |
4307 | |
4308 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4309 | inline string |
4310 | to_string(long long __val) |
4311 | { |
4312 | const bool __neg = __val < 0; |
4313 | const unsigned long long __uval |
4314 | = __neg ? (unsigned long long)~__val + 1ull : __val; |
4315 | const auto __len = __detail::__to_chars_len(__uval); |
4316 | string __str; |
4317 | __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) { |
4318 | __p[0] = '-'; |
4319 | __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval); |
4320 | return __n; |
4321 | }); |
4322 | return __str; |
4323 | } |
4324 | |
4325 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4326 | inline string |
4327 | to_string(unsigned long long __val) |
4328 | { |
4329 | const auto __len = __detail::__to_chars_len(__val); |
4330 | string __str; |
4331 | __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) { |
4332 | __detail::__to_chars_10_impl(__p, __n, __val); |
4333 | return __n; |
4334 | }); |
4335 | return __str; |
4336 | } |
4337 | |
4338 | #if __glibcxx_to_string >= 202306L // C++ >= 26 |
4339 | |
4340 | [[nodiscard]] |
4341 | inline string |
4342 | to_string(float __val) |
4343 | { |
4344 | string __str; |
4345 | size_t __len = 15; |
4346 | do { |
4347 | __str.resize_and_overwrite(__len, |
4348 | [__val, &__len] (char* __p, size_t __n) { |
4349 | auto [__end, __err] = std::to_chars(__p, __p + __n, __val); |
4350 | if (__err == errc{}) [[likely]] |
4351 | return __end - __p; |
4352 | __len *= 2; |
4353 | return __p - __p;; |
4354 | }); |
4355 | } while (__str.empty()); |
4356 | return __str; |
4357 | } |
4358 | |
4359 | [[nodiscard]] |
4360 | inline string |
4361 | to_string(double __val) |
4362 | { |
4363 | string __str; |
4364 | size_t __len = 15; |
4365 | do { |
4366 | __str.resize_and_overwrite(__len, |
4367 | [__val, &__len] (char* __p, size_t __n) { |
4368 | auto [__end, __err] = std::to_chars(__p, __p + __n, __val); |
4369 | if (__err == errc{}) [[likely]] |
4370 | return __end - __p; |
4371 | __len *= 2; |
4372 | return __p - __p;; |
4373 | }); |
4374 | } while (__str.empty()); |
4375 | return __str; |
4376 | } |
4377 | |
4378 | [[nodiscard]] |
4379 | inline string |
4380 | to_string(long double __val) |
4381 | { |
4382 | string __str; |
4383 | size_t __len = 15; |
4384 | do { |
4385 | __str.resize_and_overwrite(__len, |
4386 | [__val, &__len] (char* __p, size_t __n) { |
4387 | auto [__end, __err] = std::to_chars(__p, __p + __n, __val); |
4388 | if (__err == errc{}) [[likely]] |
4389 | return __end - __p; |
4390 | __len *= 2; |
4391 | return __p - __p;; |
4392 | }); |
4393 | } while (__str.empty()); |
4394 | return __str; |
4395 | } |
4396 | #elif _GLIBCXX_USE_C99_STDIO1 |
4397 | // NB: (v)snprintf vs sprintf. |
4398 | |
4399 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4400 | inline string |
4401 | to_string(float __val) |
4402 | { |
4403 | const int __n = |
4404 | __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; |
4405 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, |
4406 | "%f", __val); |
4407 | } |
4408 | |
4409 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4410 | inline string |
4411 | to_string(double __val) |
4412 | { |
4413 | const int __n = |
4414 | __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; |
4415 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, |
4416 | "%f", __val); |
4417 | } |
4418 | |
4419 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4420 | inline string |
4421 | to_string(long double __val) |
4422 | { |
4423 | const int __n = |
4424 | __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; |
4425 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, |
4426 | "%Lf", __val); |
4427 | } |
4428 | #endif // _GLIBCXX_USE_C99_STDIO |
4429 | |
4430 | #if defined(_GLIBCXX_USE_WCHAR_T1) && _GLIBCXX_USE_C99_WCHAR1 |
4431 | inline int |
4432 | stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) |
4433 | { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(), |
4434 | __idx, __base); } |
4435 | |
4436 | inline long |
4437 | stol(const wstring& __str, size_t* __idx = 0, int __base = 10) |
4438 | { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(), |
4439 | __idx, __base); } |
4440 | |
4441 | inline unsigned long |
4442 | stoul(const wstring& __str, size_t* __idx = 0, int __base = 10) |
4443 | { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(), |
4444 | __idx, __base); } |
4445 | |
4446 | inline long long |
4447 | stoll(const wstring& __str, size_t* __idx = 0, int __base = 10) |
4448 | { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(), |
4449 | __idx, __base); } |
4450 | |
4451 | inline unsigned long long |
4452 | stoull(const wstring& __str, size_t* __idx = 0, int __base = 10) |
4453 | { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(), |
4454 | __idx, __base); } |
4455 | |
4456 | // NB: wcstof vs wcstod. |
4457 | inline float |
4458 | stof(const wstring& __str, size_t* __idx = 0) |
4459 | { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); } |
4460 | |
4461 | inline double |
4462 | stod(const wstring& __str, size_t* __idx = 0) |
4463 | { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); } |
4464 | |
4465 | inline long double |
4466 | stold(const wstring& __str, size_t* __idx = 0) |
4467 | { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); } |
4468 | #endif |
4469 | |
4470 | #ifdef _GLIBCXX_USE_WCHAR_T1 |
4471 | #pragma GCC diagnostic push |
4472 | #pragma GCC diagnostic ignored "-Wc++17-extensions" |
4473 | _GLIBCXX20_CONSTEXPRconstexpr |
4474 | inline void |
4475 | __to_wstring_numeric(const char* __s, int __len, wchar_t* __wout) |
4476 | { |
4477 | // This condition is true if exec-charset and wide-exec-charset share the |
4478 | // same values for the ASCII subset or the EBCDIC invariant character set. |
4479 | if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-' |
4480 | && wchar_t('.') == L'.' && wchar_t('e') == L'e') |
4481 | { |
4482 | for (int __i = 0; __i < __len; ++__i) |
4483 | __wout[__i] = (wchar_t) __s[__i]; |
4484 | } |
4485 | else |
4486 | { |
4487 | wchar_t __wc[256]; |
4488 | for (int __i = '0'; __i <= '9'; ++__i) |
4489 | __wc[__i] = L'0' + __i; |
4490 | __wc['.'] = L'.'; |
4491 | __wc['+'] = L'+'; |
4492 | __wc['-'] = L'-'; |
4493 | __wc['a'] = L'a'; |
4494 | __wc['b'] = L'b'; |
4495 | __wc['c'] = L'c'; |
4496 | __wc['d'] = L'd'; |
4497 | __wc['e'] = L'e'; |
4498 | __wc['f'] = L'f'; |
4499 | __wc['n'] = L'n'; // for "nan" and "inf" |
4500 | __wc['p'] = L'p'; // for hexfloats "0x1p1" |
4501 | __wc['x'] = L'x'; |
4502 | __wc['A'] = L'A'; |
4503 | __wc['B'] = L'B'; |
4504 | __wc['C'] = L'C'; |
4505 | __wc['D'] = L'D'; |
4506 | __wc['E'] = L'E'; |
4507 | __wc['F'] = L'F'; |
4508 | __wc['N'] = L'N'; |
4509 | __wc['P'] = L'P'; |
4510 | __wc['X'] = L'X'; |
4511 | |
4512 | for (int __i = 0; __i < __len; ++__i) |
4513 | __wout[__i] = __wc[(int)__s[__i]]; |
4514 | } |
4515 | } |
4516 | |
4517 | #if __glibcxx_constexpr_string201907L >= 201907L |
4518 | constexpr |
4519 | #endif |
4520 | inline wstring |
4521 | #if __cplusplus202002L >= 201703L |
4522 | __to_wstring_numeric(string_view __s) |
4523 | #else |
4524 | __to_wstring_numeric(const string& __s) |
4525 | #endif |
4526 | { |
4527 | if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-' |
4528 | && wchar_t('.') == L'.' && wchar_t('e') == L'e') |
4529 | return wstring(__s.data(), __s.data() + __s.size()); |
4530 | else |
4531 | { |
4532 | wstring __ws; |
4533 | auto __f = __s.data(); |
4534 | __ws.__resize_and_overwrite(__s.size(), |
4535 | [__f] (wchar_t* __to, int __n) { |
4536 | std::__to_wstring_numeric(__f, __n, __to); |
4537 | return __n; |
4538 | }); |
4539 | return __ws; |
4540 | } |
4541 | } |
4542 | #pragma GCC diagnostic pop |
4543 | |
4544 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4545 | inline wstring |
4546 | to_wstring(int __val) |
4547 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4548 | |
4549 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4550 | inline wstring |
4551 | to_wstring(unsigned __val) |
4552 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4553 | |
4554 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4555 | inline wstring |
4556 | to_wstring(long __val) |
4557 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4558 | |
4559 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4560 | inline wstring |
4561 | to_wstring(unsigned long __val) |
4562 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4563 | |
4564 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4565 | inline wstring |
4566 | to_wstring(long long __val) |
4567 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4568 | |
4569 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4570 | inline wstring |
4571 | to_wstring(unsigned long long __val) |
4572 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4573 | |
4574 | #if __glibcxx_to_string || _GLIBCXX_USE_C99_STDIO1 |
4575 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4576 | inline wstring |
4577 | to_wstring(float __val) |
4578 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4579 | |
4580 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4581 | inline wstring |
4582 | to_wstring(double __val) |
4583 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4584 | |
4585 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
4586 | inline wstring |
4587 | to_wstring(long double __val) |
4588 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
4589 | #endif |
4590 | #endif // _GLIBCXX_USE_WCHAR_T |
4591 | |
4592 | _GLIBCXX_END_NAMESPACE_CXX11} |
4593 | _GLIBCXX_END_NAMESPACE_VERSION |
4594 | } // namespace |
4595 | |
4596 | #endif /* C++11 */ |
4597 | |
4598 | #if __cplusplus202002L >= 201103L |
4599 | |
4600 | #include <bits/functional_hash.h> |
4601 | |
4602 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
4603 | { |
4604 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
4605 | |
4606 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
4607 | // 3705. Hashability shouldn't depend on basic_string's allocator |
4608 | |
4609 | template<typename _CharT, typename _Alloc, |
4610 | typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>> |
4611 | struct __str_hash_base |
4612 | : public __hash_base<size_t, _StrT> |
4613 | { |
4614 | [[__nodiscard__]] |
4615 | size_t |
4616 | operator()(const _StrT& __s) const noexcept |
4617 | { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); } |
4618 | }; |
4619 | |
4620 | #ifndef _GLIBCXX_COMPATIBILITY_CXX0X |
4621 | /// std::hash specialization for string. |
4622 | template<typename _Alloc> |
4623 | struct hash<basic_string<char, char_traits<char>, _Alloc>> |
4624 | : public __str_hash_base<char, _Alloc> |
4625 | { }; |
4626 | |
4627 | /// std::hash specialization for wstring. |
4628 | template<typename _Alloc> |
4629 | struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>> |
4630 | : public __str_hash_base<wchar_t, _Alloc> |
4631 | { }; |
4632 | |
4633 | template<typename _Alloc> |
4634 | struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>, |
4635 | _Alloc>>> |
4636 | : std::false_type |
4637 | { }; |
4638 | #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */ |
4639 | |
4640 | #ifdef _GLIBCXX_USE_CHAR8_T1 |
4641 | /// std::hash specialization for u8string. |
4642 | template<typename _Alloc> |
4643 | struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>> |
4644 | : public __str_hash_base<char8_t, _Alloc> |
4645 | { }; |
4646 | #endif |
4647 | |
4648 | /// std::hash specialization for u16string. |
4649 | template<typename _Alloc> |
4650 | struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>> |
4651 | : public __str_hash_base<char16_t, _Alloc> |
4652 | { }; |
4653 | |
4654 | /// std::hash specialization for u32string. |
4655 | template<typename _Alloc> |
4656 | struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>> |
4657 | : public __str_hash_base<char32_t, _Alloc> |
4658 | { }; |
4659 | |
4660 | #if ! _GLIBCXX_INLINE_VERSION0 |
4661 | // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI. |
4662 | template<> struct __is_fast_hash<hash<string>> : std::false_type { }; |
4663 | template<> struct __is_fast_hash<hash<wstring>> : std::false_type { }; |
4664 | template<> struct __is_fast_hash<hash<u16string>> : std::false_type { }; |
4665 | template<> struct __is_fast_hash<hash<u32string>> : std::false_type { }; |
4666 | #ifdef _GLIBCXX_USE_CHAR8_T1 |
4667 | template<> struct __is_fast_hash<hash<u8string>> : std::false_type { }; |
4668 | #endif |
4669 | #else |
4670 | // For versioned namespace, assume every std::hash<basic_string<>> is slow. |
4671 | template<typename _CharT, typename _Traits, typename _Alloc> |
4672 | struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>> |
4673 | : std::false_type |
4674 | { }; |
4675 | #endif |
4676 | |
4677 | #ifdef __glibcxx_string_udls201304L // C++ >= 14 |
4678 | inline namespace literals |
4679 | { |
4680 | inline namespace string_literals |
4681 | { |
4682 | #pragma GCC diagnostic push |
4683 | #pragma GCC diagnostic ignored "-Wliteral-suffix" |
4684 | |
4685 | #if __glibcxx_constexpr_string201907L >= 201907L |
4686 | # define _GLIBCXX_STRING_CONSTEXPR constexpr |
4687 | #else |
4688 | # define _GLIBCXX_STRING_CONSTEXPR |
4689 | #endif |
4690 | |
4691 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
4692 | inline basic_string<char> |
4693 | operator""s(const char* __str, size_t __len) |
4694 | { return basic_string<char>{__str, __len}; } |
4695 | |
4696 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
4697 | inline basic_string<wchar_t> |
4698 | operator""s(const wchar_t* __str, size_t __len) |
4699 | { return basic_string<wchar_t>{__str, __len}; } |
4700 | |
4701 | #ifdef _GLIBCXX_USE_CHAR8_T1 |
4702 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
4703 | inline basic_string<char8_t> |
4704 | operator""s(const char8_t* __str, size_t __len) |
4705 | { return basic_string<char8_t>{__str, __len}; } |
4706 | #endif |
4707 | |
4708 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
4709 | inline basic_string<char16_t> |
4710 | operator""s(const char16_t* __str, size_t __len) |
4711 | { return basic_string<char16_t>{__str, __len}; } |
4712 | |
4713 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
4714 | inline basic_string<char32_t> |
4715 | operator""s(const char32_t* __str, size_t __len) |
4716 | { return basic_string<char32_t>{__str, __len}; } |
4717 | |
4718 | #undef _GLIBCXX_STRING_CONSTEXPR |
4719 | #pragma GCC diagnostic pop |
4720 | } // inline namespace string_literals |
4721 | } // inline namespace literals |
4722 | #endif // __glibcxx_string_udls |
4723 | |
4724 | #if __cplusplus202002L >= 201703L |
4725 | namespace __detail::__variant |
4726 | { |
4727 | template<typename> struct _Never_valueless_alt; // see <variant> |
4728 | |
4729 | // Provide the strong exception-safety guarantee when emplacing a |
4730 | // basic_string into a variant, but only if moving the string cannot throw. |
4731 | template<typename _Tp, typename _Traits, typename _Alloc> |
4732 | struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>> |
4733 | : __and_< |
4734 | is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>, |
4735 | is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>> |
4736 | >::type |
4737 | { }; |
4738 | } // namespace __detail::__variant |
4739 | #endif // C++17 |
4740 | |
4741 | _GLIBCXX_END_NAMESPACE_VERSION |
4742 | } // namespace std |
4743 | |
4744 | #endif // C++11 |
4745 | |
4746 | #endif /* _BASIC_STRING_H */ |
1 | // Copyright 2005, Google Inc. | |||
2 | // All rights reserved. | |||
3 | // | |||
4 | // Redistribution and use in source and binary forms, with or without | |||
5 | // modification, are permitted provided that the following conditions are | |||
6 | // met: | |||
7 | // | |||
8 | // * Redistributions of source code must retain the above copyright | |||
9 | // notice, this list of conditions and the following disclaimer. | |||
10 | // * Redistributions in binary form must reproduce the above | |||
11 | // copyright notice, this list of conditions and the following disclaimer | |||
12 | // in the documentation and/or other materials provided with the | |||
13 | // distribution. | |||
14 | // * Neither the name of Google Inc. nor the names of its | |||
15 | // contributors may be used to endorse or promote products derived from | |||
16 | // this software without specific prior written permission. | |||
17 | // | |||
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |||
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |||
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
29 | ||||
30 | // The Google C++ Testing and Mocking Framework (Google Test) | |||
31 | // | |||
32 | // This header file defines the public API for Google Test. It should be | |||
33 | // included by any test program that uses Google Test. | |||
34 | // | |||
35 | // IMPORTANT NOTE: Due to limitation of the C++ language, we have to | |||
36 | // leave some internal implementation details in this header file. | |||
37 | // They are clearly marked by comments like this: | |||
38 | // | |||
39 | // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
40 | // | |||
41 | // Such code is NOT meant to be used by a user directly, and is subject | |||
42 | // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user | |||
43 | // program! | |||
44 | // | |||
45 | // Acknowledgment: Google Test borrowed the idea of automatic test | |||
46 | // registration from Barthelemy Dagenais' (barthelemy@prologique.com) | |||
47 | // easyUnit framework. | |||
48 | ||||
49 | #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_ | |||
50 | #define GOOGLETEST_INCLUDE_GTEST_GTEST_H_ | |||
51 | ||||
52 | #include <cstddef> | |||
53 | #include <cstdint> | |||
54 | #include <iomanip> | |||
55 | #include <limits> | |||
56 | #include <memory> | |||
57 | #include <ostream> | |||
58 | #include <set> | |||
59 | #include <sstream> | |||
60 | #include <string> | |||
61 | #include <type_traits> | |||
62 | #include <vector> | |||
63 | ||||
64 | #include "gtest/gtest-assertion-result.h" | |||
65 | #include "gtest/gtest-death-test.h" | |||
66 | #include "gtest/gtest-matchers.h" | |||
67 | #include "gtest/gtest-message.h" | |||
68 | #include "gtest/gtest-param-test.h" | |||
69 | #include "gtest/gtest-printers.h" | |||
70 | #include "gtest/gtest-test-part.h" | |||
71 | #include "gtest/gtest-typed-test.h" | |||
72 | #include "gtest/gtest_pred_impl.h" | |||
73 | #include "gtest/gtest_prod.h" | |||
74 | #include "gtest/internal/gtest-internal.h" | |||
75 | #include "gtest/internal/gtest-string.h" | |||
76 | ||||
77 | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ | |||
78 | /* class A needs to have dll-interface to be used by clients of class B */) | |||
79 | ||||
80 | // Declares the flags. | |||
81 | ||||
82 | // This flag temporary enables the disabled tests. | |||
83 | GTEST_DECLARE_bool_(also_run_disabled_tests)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_also_run_disabled_tests; } static_assert(true , "no-op to require trailing semicolon"); | |||
84 | ||||
85 | // This flag brings the debugger on an assertion failure. | |||
86 | GTEST_DECLARE_bool_(break_on_failure)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_break_on_failure; } static_assert(true, "no-op to require trailing semicolon" ); | |||
87 | ||||
88 | // This flag controls whether Google Test catches all test-thrown exceptions | |||
89 | // and logs them as failures. | |||
90 | GTEST_DECLARE_bool_(catch_exceptions)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_catch_exceptions; } static_assert(true, "no-op to require trailing semicolon" ); | |||
91 | ||||
92 | // This flag enables using colors in terminal output. Available values are | |||
93 | // "yes" to enable colors, "no" (disable colors), or "auto" (the default) | |||
94 | // to let Google Test decide. | |||
95 | GTEST_DECLARE_string_(color)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_color; } static_assert(true, "no-op to require trailing semicolon" ); | |||
96 | ||||
97 | // This flag controls whether the test runner should continue execution past | |||
98 | // first failure. | |||
99 | GTEST_DECLARE_bool_(fail_fast)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_fail_fast; } static_assert(true, "no-op to require trailing semicolon" ); | |||
100 | ||||
101 | // This flag sets up the filter to select by name using a glob pattern | |||
102 | // the tests to run. If the filter is not given all tests are executed. | |||
103 | GTEST_DECLARE_string_(filter)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_filter; } static_assert(true, "no-op to require trailing semicolon" ); | |||
104 | ||||
105 | // This flag controls whether Google Test installs a signal handler that dumps | |||
106 | // debugging information when fatal signals are raised. | |||
107 | GTEST_DECLARE_bool_(install_failure_signal_handler)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_install_failure_signal_handler; } static_assert (true, "no-op to require trailing semicolon"); | |||
108 | ||||
109 | // This flag causes the Google Test to list tests. None of the tests listed | |||
110 | // are actually run if the flag is provided. | |||
111 | GTEST_DECLARE_bool_(list_tests)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_list_tests; } static_assert(true, "no-op to require trailing semicolon" ); | |||
112 | ||||
113 | // This flag controls whether Google Test emits a detailed XML report to a file | |||
114 | // in addition to its normal textual output. | |||
115 | GTEST_DECLARE_string_(output)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_output; } static_assert(true, "no-op to require trailing semicolon" ); | |||
116 | ||||
117 | // This flags control whether Google Test prints only test failures. | |||
118 | GTEST_DECLARE_bool_(brief)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_brief; } static_assert(true, "no-op to require trailing semicolon" ); | |||
119 | ||||
120 | // This flags control whether Google Test prints the elapsed time for each | |||
121 | // test. | |||
122 | GTEST_DECLARE_bool_(print_time)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_print_time; } static_assert(true, "no-op to require trailing semicolon" ); | |||
123 | ||||
124 | // This flags control whether Google Test prints UTF8 characters as text. | |||
125 | GTEST_DECLARE_bool_(print_utf8)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_print_utf8; } static_assert(true, "no-op to require trailing semicolon" ); | |||
126 | ||||
127 | // This flag specifies the random number seed. | |||
128 | GTEST_DECLARE_int32_(random_seed)namespace testing { __attribute__((visibility("default"))) extern std::int32_t FLAGS_gtest_random_seed; } static_assert(true, "no-op to require trailing semicolon" ); | |||
129 | ||||
130 | // This flag sets how many times the tests are repeated. The default value | |||
131 | // is 1. If the value is -1 the tests are repeating forever. | |||
132 | GTEST_DECLARE_int32_(repeat)namespace testing { __attribute__((visibility("default"))) extern std::int32_t FLAGS_gtest_repeat; } static_assert(true, "no-op to require trailing semicolon" ); | |||
133 | ||||
134 | // This flag controls whether Google Test Environments are recreated for each | |||
135 | // repeat of the tests. The default value is true. If set to false the global | |||
136 | // test Environment objects are only set up once, for the first iteration, and | |||
137 | // only torn down once, for the last. | |||
138 | GTEST_DECLARE_bool_(recreate_environments_when_repeating)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_recreate_environments_when_repeating; } static_assert (true, "no-op to require trailing semicolon"); | |||
139 | ||||
140 | // This flag controls whether Google Test includes Google Test internal | |||
141 | // stack frames in failure stack traces. | |||
142 | GTEST_DECLARE_bool_(show_internal_stack_frames)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_show_internal_stack_frames; } static_assert (true, "no-op to require trailing semicolon"); | |||
143 | ||||
144 | // When this flag is specified, tests' order is randomized on every iteration. | |||
145 | GTEST_DECLARE_bool_(shuffle)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_shuffle; } static_assert(true, "no-op to require trailing semicolon" ); | |||
146 | ||||
147 | // This flag specifies the maximum number of stack frames to be | |||
148 | // printed in a failure message. | |||
149 | GTEST_DECLARE_int32_(stack_trace_depth)namespace testing { __attribute__((visibility("default"))) extern std::int32_t FLAGS_gtest_stack_trace_depth; } static_assert( true, "no-op to require trailing semicolon"); | |||
150 | ||||
151 | // When this flag is specified, a failed assertion will throw an | |||
152 | // exception if exceptions are enabled, or exit the program with a | |||
153 | // non-zero code otherwise. For use with an external test framework. | |||
154 | GTEST_DECLARE_bool_(throw_on_failure)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_throw_on_failure; } static_assert(true, "no-op to require trailing semicolon" ); | |||
155 | ||||
156 | // When this flag is set with a "host:port" string, on supported | |||
157 | // platforms test results are streamed to the specified port on | |||
158 | // the specified host machine. | |||
159 | GTEST_DECLARE_string_(stream_result_to)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_stream_result_to; } static_assert( true, "no-op to require trailing semicolon"); | |||
160 | ||||
161 | #if GTEST_USE_OWN_FLAGFILE_FLAG_1 | |||
162 | GTEST_DECLARE_string_(flagfile)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_flagfile; } static_assert(true, "no-op to require trailing semicolon" ); | |||
163 | #endif // GTEST_USE_OWN_FLAGFILE_FLAG_ | |||
164 | ||||
165 | namespace testing { | |||
166 | ||||
167 | // Silence C4100 (unreferenced formal parameter) and 4805 | |||
168 | // unsafe mix of type 'const int' and type 'const bool' | |||
169 | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4805 4100) | |||
170 | ||||
171 | // The upper limit for valid stack trace depths. | |||
172 | const int kMaxStackTraceDepth = 100; | |||
173 | ||||
174 | namespace internal { | |||
175 | ||||
176 | class AssertHelper; | |||
177 | class DefaultGlobalTestPartResultReporter; | |||
178 | class ExecDeathTest; | |||
179 | class NoExecDeathTest; | |||
180 | class FinalSuccessChecker; | |||
181 | class GTestFlagSaver; | |||
182 | class StreamingListenerTest; | |||
183 | class TestResultAccessor; | |||
184 | class TestEventListenersAccessor; | |||
185 | class TestEventRepeater; | |||
186 | class UnitTestRecordPropertyTestHelper; | |||
187 | class WindowsDeathTest; | |||
188 | class FuchsiaDeathTest; | |||
189 | class UnitTestImpl* GetUnitTestImpl(); | |||
190 | void ReportFailureInUnknownLocation(TestPartResult::Type result_type, | |||
191 | const std::string& message); | |||
192 | std::set<std::string>* GetIgnoredParameterizedTestSuites(); | |||
193 | ||||
194 | // A base class that prevents subclasses from being copyable. | |||
195 | // We do this instead of using '= delete' so as to avoid triggering warnings | |||
196 | // inside user code regarding any of our declarations. | |||
197 | class GTestNonCopyable { | |||
198 | public: | |||
199 | GTestNonCopyable() = default; | |||
200 | GTestNonCopyable(const GTestNonCopyable&) = delete; | |||
201 | GTestNonCopyable& operator=(const GTestNonCopyable&) = delete; | |||
202 | ~GTestNonCopyable() = default; | |||
203 | }; | |||
204 | ||||
205 | } // namespace internal | |||
206 | ||||
207 | // The friend relationship of some of these classes is cyclic. | |||
208 | // If we don't forward declare them the compiler might confuse the classes | |||
209 | // in friendship clauses with same named classes on the scope. | |||
210 | class Test; | |||
211 | class TestSuite; | |||
212 | ||||
213 | // Old API is still available but deprecated | |||
214 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
215 | using TestCase = TestSuite; | |||
216 | #endif | |||
217 | class TestInfo; | |||
218 | class UnitTest; | |||
219 | ||||
220 | // The abstract class that all tests inherit from. | |||
221 | // | |||
222 | // In Google Test, a unit test program contains one or many TestSuites, and | |||
223 | // each TestSuite contains one or many Tests. | |||
224 | // | |||
225 | // When you define a test using the TEST macro, you don't need to | |||
226 | // explicitly derive from Test - the TEST macro automatically does | |||
227 | // this for you. | |||
228 | // | |||
229 | // The only time you derive from Test is when defining a test fixture | |||
230 | // to be used in a TEST_F. For example: | |||
231 | // | |||
232 | // class FooTest : public testing::Test { | |||
233 | // protected: | |||
234 | // void SetUp() override { ... } | |||
235 | // void TearDown() override { ... } | |||
236 | // ... | |||
237 | // }; | |||
238 | // | |||
239 | // TEST_F(FooTest, Bar) { ... } | |||
240 | // TEST_F(FooTest, Baz) { ... } | |||
241 | // | |||
242 | // Test is not copyable. | |||
243 | class GTEST_API___attribute__((visibility("default"))) Test { | |||
244 | public: | |||
245 | friend class TestInfo; | |||
246 | ||||
247 | // The d'tor is virtual as we intend to inherit from Test. | |||
248 | virtual ~Test(); | |||
249 | ||||
250 | // Sets up the stuff shared by all tests in this test suite. | |||
251 | // | |||
252 | // Google Test will call Foo::SetUpTestSuite() before running the first | |||
253 | // test in test suite Foo. Hence a sub-class can define its own | |||
254 | // SetUpTestSuite() method to shadow the one defined in the super | |||
255 | // class. | |||
256 | static void SetUpTestSuite() {} | |||
257 | ||||
258 | // Tears down the stuff shared by all tests in this test suite. | |||
259 | // | |||
260 | // Google Test will call Foo::TearDownTestSuite() after running the last | |||
261 | // test in test suite Foo. Hence a sub-class can define its own | |||
262 | // TearDownTestSuite() method to shadow the one defined in the super | |||
263 | // class. | |||
264 | static void TearDownTestSuite() {} | |||
265 | ||||
266 | // Legacy API is deprecated but still available. Use SetUpTestSuite and | |||
267 | // TearDownTestSuite instead. | |||
268 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
269 | static void TearDownTestCase() {} | |||
270 | static void SetUpTestCase() {} | |||
271 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
272 | ||||
273 | // Returns true if and only if the current test has a fatal failure. | |||
274 | static bool HasFatalFailure(); | |||
275 | ||||
276 | // Returns true if and only if the current test has a non-fatal failure. | |||
277 | static bool HasNonfatalFailure(); | |||
278 | ||||
279 | // Returns true if and only if the current test was skipped. | |||
280 | static bool IsSkipped(); | |||
281 | ||||
282 | // Returns true if and only if the current test has a (either fatal or | |||
283 | // non-fatal) failure. | |||
284 | static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } | |||
285 | ||||
286 | // Logs a property for the current test, test suite, or for the entire | |||
287 | // invocation of the test program when used outside of the context of a | |||
288 | // test suite. Only the last value for a given key is remembered. These | |||
289 | // are public static so they can be called from utility functions that are | |||
290 | // not members of the test fixture. Calls to RecordProperty made during | |||
291 | // lifespan of the test (from the moment its constructor starts to the | |||
292 | // moment its destructor finishes) will be output in XML as attributes of | |||
293 | // the <testcase> element. Properties recorded from fixture's | |||
294 | // SetUpTestSuite or TearDownTestSuite are logged as attributes of the | |||
295 | // corresponding <testsuite> element. Calls to RecordProperty made in the | |||
296 | // global context (before or after invocation of RUN_ALL_TESTS and from | |||
297 | // SetUp/TearDown method of Environment objects registered with Google | |||
298 | // Test) will be output as attributes of the <testsuites> element. | |||
299 | static void RecordProperty(const std::string& key, const std::string& value); | |||
300 | // We do not define a custom serialization except for values that can be | |||
301 | // converted to int64_t, but other values could be logged in this way. | |||
302 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | |||
303 | bool> = true> | |||
304 | static void RecordProperty(const std::string& key, const T& value) { | |||
305 | RecordProperty(key, (Message() << value).GetString()); | |||
306 | } | |||
307 | ||||
308 | protected: | |||
309 | // Creates a Test object. | |||
310 | Test(); | |||
311 | ||||
312 | // Sets up the test fixture. | |||
313 | virtual void SetUp(); | |||
314 | ||||
315 | // Tears down the test fixture. | |||
316 | virtual void TearDown(); | |||
317 | ||||
318 | private: | |||
319 | // Returns true if and only if the current test has the same fixture class | |||
320 | // as the first test in the current test suite. | |||
321 | static bool HasSameFixtureClass(); | |||
322 | ||||
323 | // Runs the test after the test fixture has been set up. | |||
324 | // | |||
325 | // A sub-class must implement this to define the test logic. | |||
326 | // | |||
327 | // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. | |||
328 | // Instead, use the TEST or TEST_F macro. | |||
329 | virtual void TestBody() = 0; | |||
330 | ||||
331 | // Sets up, executes, and tears down the test. | |||
332 | void Run(); | |||
333 | ||||
334 | // Deletes self. We deliberately pick an unusual name for this | |||
335 | // internal method to avoid clashing with names used in user TESTs. | |||
336 | void DeleteSelf_() { delete this; } | |||
337 | ||||
338 | const std::unique_ptr<GTEST_FLAG_SAVER_::testing::internal::GTestFlagSaver> gtest_flag_saver_; | |||
339 | ||||
340 | // Often a user misspells SetUp() as Setup() and spends a long time | |||
341 | // wondering why it is never called by Google Test. The declaration of | |||
342 | // the following method is solely for catching such an error at | |||
343 | // compile time: | |||
344 | // | |||
345 | // - The return type is deliberately chosen to be not void, so it | |||
346 | // will be a conflict if void Setup() is declared in the user's | |||
347 | // test fixture. | |||
348 | // | |||
349 | // - This method is private, so it will be another compiler error | |||
350 | // if the method is called from the user's test fixture. | |||
351 | // | |||
352 | // DO NOT OVERRIDE THIS FUNCTION. | |||
353 | // | |||
354 | // If you see an error about overriding the following function or | |||
355 | // about it being private, you have mis-spelled SetUp() as Setup(). | |||
356 | struct Setup_should_be_spelled_SetUp {}; | |||
357 | virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } | |||
358 | ||||
359 | // We disallow copying Tests. | |||
360 | Test(const Test&) = delete; | |||
361 | Test& operator=(const Test&) = delete; | |||
362 | }; | |||
363 | ||||
364 | typedef internal::TimeInMillis TimeInMillis; | |||
365 | ||||
366 | // A copyable object representing a user specified test property which can be | |||
367 | // output as a key/value string pair. | |||
368 | // | |||
369 | // Don't inherit from TestProperty as its destructor is not virtual. | |||
370 | class TestProperty { | |||
371 | public: | |||
372 | // C'tor. TestProperty does NOT have a default constructor. | |||
373 | // Always use this constructor (with parameters) to create a | |||
374 | // TestProperty object. | |||
375 | TestProperty(const std::string& a_key, const std::string& a_value) | |||
376 | : key_(a_key), value_(a_value) {} | |||
377 | ||||
378 | // Gets the user supplied key. | |||
379 | const char* key() const { return key_.c_str(); } | |||
380 | ||||
381 | // Gets the user supplied value. | |||
382 | const char* value() const { return value_.c_str(); } | |||
383 | ||||
384 | // Sets a new value, overriding the one supplied in the constructor. | |||
385 | void SetValue(const std::string& new_value) { value_ = new_value; } | |||
386 | ||||
387 | private: | |||
388 | // The key supplied by the user. | |||
389 | std::string key_; | |||
390 | // The value supplied by the user. | |||
391 | std::string value_; | |||
392 | }; | |||
393 | ||||
394 | // The result of a single Test. This includes a list of | |||
395 | // TestPartResults, a list of TestProperties, a count of how many | |||
396 | // death tests there are in the Test, and how much time it took to run | |||
397 | // the Test. | |||
398 | // | |||
399 | // TestResult is not copyable. | |||
400 | class GTEST_API___attribute__((visibility("default"))) TestResult { | |||
401 | public: | |||
402 | // Creates an empty TestResult. | |||
403 | TestResult(); | |||
404 | ||||
405 | // D'tor. Do not inherit from TestResult. | |||
406 | ~TestResult(); | |||
407 | ||||
408 | // Gets the number of all test parts. This is the sum of the number | |||
409 | // of successful test parts and the number of failed test parts. | |||
410 | int total_part_count() const; | |||
411 | ||||
412 | // Returns the number of the test properties. | |||
413 | int test_property_count() const; | |||
414 | ||||
415 | // Returns true if and only if the test passed (i.e. no test part failed). | |||
416 | bool Passed() const { return !Skipped() && !Failed(); } | |||
417 | ||||
418 | // Returns true if and only if the test was skipped. | |||
419 | bool Skipped() const; | |||
420 | ||||
421 | // Returns true if and only if the test failed. | |||
422 | bool Failed() const; | |||
423 | ||||
424 | // Returns true if and only if the test fatally failed. | |||
425 | bool HasFatalFailure() const; | |||
426 | ||||
427 | // Returns true if and only if the test has a non-fatal failure. | |||
428 | bool HasNonfatalFailure() const; | |||
429 | ||||
430 | // Returns the elapsed time, in milliseconds. | |||
431 | TimeInMillis elapsed_time() const { return elapsed_time_; } | |||
432 | ||||
433 | // Gets the time of the test case start, in ms from the start of the | |||
434 | // UNIX epoch. | |||
435 | TimeInMillis start_timestamp() const { return start_timestamp_; } | |||
436 | ||||
437 | // Returns the i-th test part result among all the results. i can range from 0 | |||
438 | // to total_part_count() - 1. If i is not in that range, aborts the program. | |||
439 | const TestPartResult& GetTestPartResult(int i) const; | |||
440 | ||||
441 | // Returns the i-th test property. i can range from 0 to | |||
442 | // test_property_count() - 1. If i is not in that range, aborts the | |||
443 | // program. | |||
444 | const TestProperty& GetTestProperty(int i) const; | |||
445 | ||||
446 | private: | |||
447 | friend class TestInfo; | |||
448 | friend class TestSuite; | |||
449 | friend class UnitTest; | |||
450 | friend class internal::DefaultGlobalTestPartResultReporter; | |||
451 | friend class internal::ExecDeathTest; | |||
452 | friend class internal::TestResultAccessor; | |||
453 | friend class internal::UnitTestImpl; | |||
454 | friend class internal::WindowsDeathTest; | |||
455 | friend class internal::FuchsiaDeathTest; | |||
456 | ||||
457 | // Gets the vector of TestPartResults. | |||
458 | const std::vector<TestPartResult>& test_part_results() const { | |||
459 | return test_part_results_; | |||
460 | } | |||
461 | ||||
462 | // Gets the vector of TestProperties. | |||
463 | const std::vector<TestProperty>& test_properties() const { | |||
464 | return test_properties_; | |||
465 | } | |||
466 | ||||
467 | // Sets the start time. | |||
468 | void set_start_timestamp(TimeInMillis start) { start_timestamp_ = start; } | |||
469 | ||||
470 | // Sets the elapsed time. | |||
471 | void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } | |||
472 | ||||
473 | // Adds a test property to the list. The property is validated and may add | |||
474 | // a non-fatal failure if invalid (e.g., if it conflicts with reserved | |||
475 | // key names). If a property is already recorded for the same key, the | |||
476 | // value will be updated, rather than storing multiple values for the same | |||
477 | // key. xml_element specifies the element for which the property is being | |||
478 | // recorded and is used for validation. | |||
479 | void RecordProperty(const std::string& xml_element, | |||
480 | const TestProperty& test_property); | |||
481 | ||||
482 | // Adds a failure if the key is a reserved attribute of Google Test | |||
483 | // testsuite tags. Returns true if the property is valid. | |||
484 | // FIXME: Validate attribute names are legal and human readable. | |||
485 | static bool ValidateTestProperty(const std::string& xml_element, | |||
486 | const TestProperty& test_property); | |||
487 | ||||
488 | // Adds a test part result to the list. | |||
489 | void AddTestPartResult(const TestPartResult& test_part_result); | |||
490 | ||||
491 | // Returns the death test count. | |||
492 | int death_test_count() const { return death_test_count_; } | |||
493 | ||||
494 | // Increments the death test count, returning the new count. | |||
495 | int increment_death_test_count() { return ++death_test_count_; } | |||
496 | ||||
497 | // Clears the test part results. | |||
498 | void ClearTestPartResults(); | |||
499 | ||||
500 | // Clears the object. | |||
501 | void Clear(); | |||
502 | ||||
503 | // Protects mutable state of the property vector and of owned | |||
504 | // properties, whose values may be updated. | |||
505 | internal::Mutex test_properties_mutex_; | |||
506 | ||||
507 | // The vector of TestPartResults | |||
508 | std::vector<TestPartResult> test_part_results_; | |||
509 | // The vector of TestProperties | |||
510 | std::vector<TestProperty> test_properties_; | |||
511 | // Running count of death tests. | |||
512 | int death_test_count_; | |||
513 | // The start time, in milliseconds since UNIX Epoch. | |||
514 | TimeInMillis start_timestamp_; | |||
515 | // The elapsed time, in milliseconds. | |||
516 | TimeInMillis elapsed_time_; | |||
517 | ||||
518 | // We disallow copying TestResult. | |||
519 | TestResult(const TestResult&) = delete; | |||
520 | TestResult& operator=(const TestResult&) = delete; | |||
521 | }; // class TestResult | |||
522 | ||||
523 | // A TestInfo object stores the following information about a test: | |||
524 | // | |||
525 | // Test suite name | |||
526 | // Test name | |||
527 | // Whether the test should be run | |||
528 | // A function pointer that creates the test object when invoked | |||
529 | // Test result | |||
530 | // | |||
531 | // The constructor of TestInfo registers itself with the UnitTest | |||
532 | // singleton such that the RUN_ALL_TESTS() macro knows which tests to | |||
533 | // run. | |||
534 | class GTEST_API___attribute__((visibility("default"))) TestInfo { | |||
535 | public: | |||
536 | // Destructs a TestInfo object. This function is not virtual, so | |||
537 | // don't inherit from TestInfo. | |||
538 | ~TestInfo(); | |||
539 | ||||
540 | // Returns the test suite name. | |||
541 | const char* test_suite_name() const { return test_suite_name_.c_str(); } | |||
542 | ||||
543 | // Legacy API is deprecated but still available | |||
544 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
545 | const char* test_case_name() const { return test_suite_name(); } | |||
546 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
547 | ||||
548 | // Returns the test name. | |||
549 | const char* name() const { return name_.c_str(); } | |||
550 | ||||
551 | // Returns the name of the parameter type, or NULL if this is not a typed | |||
552 | // or a type-parameterized test. | |||
553 | const char* type_param() const { | |||
554 | if (type_param_ != nullptr) return type_param_->c_str(); | |||
555 | return nullptr; | |||
556 | } | |||
557 | ||||
558 | // Returns the text representation of the value parameter, or NULL if this | |||
559 | // is not a value-parameterized test. | |||
560 | const char* value_param() const { | |||
561 | if (value_param_ != nullptr) return value_param_->c_str(); | |||
562 | return nullptr; | |||
563 | } | |||
564 | ||||
565 | // Returns the file name where this test is defined. | |||
566 | const char* file() const { return location_.file.c_str(); } | |||
567 | ||||
568 | // Returns the line where this test is defined. | |||
569 | int line() const { return location_.line; } | |||
570 | ||||
571 | // Return true if this test should not be run because it's in another shard. | |||
572 | bool is_in_another_shard() const { return is_in_another_shard_; } | |||
573 | ||||
574 | // Returns true if this test should run, that is if the test is not | |||
575 | // disabled (or it is disabled but the also_run_disabled_tests flag has | |||
576 | // been specified) and its full name matches the user-specified filter. | |||
577 | // | |||
578 | // Google Test allows the user to filter the tests by their full names. | |||
579 | // The full name of a test Bar in test suite Foo is defined as | |||
580 | // "Foo.Bar". Only the tests that match the filter will run. | |||
581 | // | |||
582 | // A filter is a colon-separated list of glob (not regex) patterns, | |||
583 | // optionally followed by a '-' and a colon-separated list of | |||
584 | // negative patterns (tests to exclude). A test is run if it | |||
585 | // matches one of the positive patterns and does not match any of | |||
586 | // the negative patterns. | |||
587 | // | |||
588 | // For example, *A*:Foo.* is a filter that matches any string that | |||
589 | // contains the character 'A' or starts with "Foo.". | |||
590 | bool should_run() const { return should_run_; } | |||
591 | ||||
592 | // Returns true if and only if this test will appear in the XML report. | |||
593 | bool is_reportable() const { | |||
594 | // The XML report includes tests matching the filter, excluding those | |||
595 | // run in other shards. | |||
596 | return matches_filter_ && !is_in_another_shard_; | |||
597 | } | |||
598 | ||||
599 | // Returns the result of the test. | |||
600 | const TestResult* result() const { return &result_; } | |||
601 | ||||
602 | private: | |||
603 | #ifdef GTEST_HAS_DEATH_TEST1 | |||
604 | friend class internal::DefaultDeathTestFactory; | |||
605 | #endif // GTEST_HAS_DEATH_TEST | |||
606 | friend class Test; | |||
607 | friend class TestSuite; | |||
608 | friend class internal::UnitTestImpl; | |||
609 | friend class internal::StreamingListenerTest; | |||
610 | friend TestInfo* internal::MakeAndRegisterTestInfo( | |||
611 | const char* test_suite_name, const char* name, const char* type_param, | |||
612 | const char* value_param, internal::CodeLocation code_location, | |||
613 | internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc, | |||
614 | internal::TearDownTestSuiteFunc tear_down_tc, | |||
615 | internal::TestFactoryBase* factory); | |||
616 | ||||
617 | // Constructs a TestInfo object. The newly constructed instance assumes | |||
618 | // ownership of the factory object. | |||
619 | TestInfo(const std::string& test_suite_name, const std::string& name, | |||
620 | const char* a_type_param, // NULL if not a type-parameterized test | |||
621 | const char* a_value_param, // NULL if not a value-parameterized test | |||
622 | internal::CodeLocation a_code_location, | |||
623 | internal::TypeId fixture_class_id, | |||
624 | internal::TestFactoryBase* factory); | |||
625 | ||||
626 | // Increments the number of death tests encountered in this test so | |||
627 | // far. | |||
628 | int increment_death_test_count() { | |||
629 | return result_.increment_death_test_count(); | |||
630 | } | |||
631 | ||||
632 | // Creates the test object, runs it, records its result, and then | |||
633 | // deletes it. | |||
634 | void Run(); | |||
635 | ||||
636 | // Skip and records the test result for this object. | |||
637 | void Skip(); | |||
638 | ||||
639 | static void ClearTestResult(TestInfo* test_info) { | |||
640 | test_info->result_.Clear(); | |||
641 | } | |||
642 | ||||
643 | // These fields are immutable properties of the test. | |||
644 | const std::string test_suite_name_; // test suite name | |||
645 | const std::string name_; // Test name | |||
646 | // Name of the parameter type, or NULL if this is not a typed or a | |||
647 | // type-parameterized test. | |||
648 | const std::unique_ptr<const ::std::string> type_param_; | |||
649 | // Text representation of the value parameter, or NULL if this is not a | |||
650 | // value-parameterized test. | |||
651 | const std::unique_ptr<const ::std::string> value_param_; | |||
652 | internal::CodeLocation location_; | |||
653 | const internal::TypeId fixture_class_id_; // ID of the test fixture class | |||
654 | bool should_run_; // True if and only if this test should run | |||
655 | bool is_disabled_; // True if and only if this test is disabled | |||
656 | bool matches_filter_; // True if this test matches the | |||
657 | // user-specified filter. | |||
658 | bool is_in_another_shard_; // Will be run in another shard. | |||
659 | internal::TestFactoryBase* const factory_; // The factory that creates | |||
660 | // the test object | |||
661 | ||||
662 | // This field is mutable and needs to be reset before running the | |||
663 | // test for the second time. | |||
664 | TestResult result_; | |||
665 | ||||
666 | TestInfo(const TestInfo&) = delete; | |||
667 | TestInfo& operator=(const TestInfo&) = delete; | |||
668 | }; | |||
669 | ||||
670 | // A test suite, which consists of a vector of TestInfos. | |||
671 | // | |||
672 | // TestSuite is not copyable. | |||
673 | class GTEST_API___attribute__((visibility("default"))) TestSuite { | |||
674 | public: | |||
675 | // Creates a TestSuite with the given name. | |||
676 | // | |||
677 | // TestSuite does NOT have a default constructor. Always use this | |||
678 | // constructor to create a TestSuite object. | |||
679 | // | |||
680 | // Arguments: | |||
681 | // | |||
682 | // name: name of the test suite | |||
683 | // a_type_param: the name of the test's type parameter, or NULL if | |||
684 | // this is not a type-parameterized test. | |||
685 | // set_up_tc: pointer to the function that sets up the test suite | |||
686 | // tear_down_tc: pointer to the function that tears down the test suite | |||
687 | TestSuite(const char* name, const char* a_type_param, | |||
688 | internal::SetUpTestSuiteFunc set_up_tc, | |||
689 | internal::TearDownTestSuiteFunc tear_down_tc); | |||
690 | ||||
691 | // Destructor of TestSuite. | |||
692 | virtual ~TestSuite(); | |||
693 | ||||
694 | // Gets the name of the TestSuite. | |||
695 | const char* name() const { return name_.c_str(); } | |||
696 | ||||
697 | // Returns the name of the parameter type, or NULL if this is not a | |||
698 | // type-parameterized test suite. | |||
699 | const char* type_param() const { | |||
700 | if (type_param_ != nullptr) return type_param_->c_str(); | |||
701 | return nullptr; | |||
702 | } | |||
703 | ||||
704 | // Returns true if any test in this test suite should run. | |||
705 | bool should_run() const { return should_run_; } | |||
706 | ||||
707 | // Gets the number of successful tests in this test suite. | |||
708 | int successful_test_count() const; | |||
709 | ||||
710 | // Gets the number of skipped tests in this test suite. | |||
711 | int skipped_test_count() const; | |||
712 | ||||
713 | // Gets the number of failed tests in this test suite. | |||
714 | int failed_test_count() const; | |||
715 | ||||
716 | // Gets the number of disabled tests that will be reported in the XML report. | |||
717 | int reportable_disabled_test_count() const; | |||
718 | ||||
719 | // Gets the number of disabled tests in this test suite. | |||
720 | int disabled_test_count() const; | |||
721 | ||||
722 | // Gets the number of tests to be printed in the XML report. | |||
723 | int reportable_test_count() const; | |||
724 | ||||
725 | // Get the number of tests in this test suite that should run. | |||
726 | int test_to_run_count() const; | |||
727 | ||||
728 | // Gets the number of all tests in this test suite. | |||
729 | int total_test_count() const; | |||
730 | ||||
731 | // Returns true if and only if the test suite passed. | |||
732 | bool Passed() const { return !Failed(); } | |||
733 | ||||
734 | // Returns true if and only if the test suite failed. | |||
735 | bool Failed() const { | |||
736 | return failed_test_count() > 0 || ad_hoc_test_result().Failed(); | |||
737 | } | |||
738 | ||||
739 | // Returns the elapsed time, in milliseconds. | |||
740 | TimeInMillis elapsed_time() const { return elapsed_time_; } | |||
741 | ||||
742 | // Gets the time of the test suite start, in ms from the start of the | |||
743 | // UNIX epoch. | |||
744 | TimeInMillis start_timestamp() const { return start_timestamp_; } | |||
745 | ||||
746 | // Returns the i-th test among all the tests. i can range from 0 to | |||
747 | // total_test_count() - 1. If i is not in that range, returns NULL. | |||
748 | const TestInfo* GetTestInfo(int i) const; | |||
749 | ||||
750 | // Returns the TestResult that holds test properties recorded during | |||
751 | // execution of SetUpTestSuite and TearDownTestSuite. | |||
752 | const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } | |||
753 | ||||
754 | private: | |||
755 | friend class Test; | |||
756 | friend class internal::UnitTestImpl; | |||
757 | ||||
758 | // Gets the (mutable) vector of TestInfos in this TestSuite. | |||
759 | std::vector<TestInfo*>& test_info_list() { return test_info_list_; } | |||
760 | ||||
761 | // Gets the (immutable) vector of TestInfos in this TestSuite. | |||
762 | const std::vector<TestInfo*>& test_info_list() const { | |||
763 | return test_info_list_; | |||
764 | } | |||
765 | ||||
766 | // Returns the i-th test among all the tests. i can range from 0 to | |||
767 | // total_test_count() - 1. If i is not in that range, returns NULL. | |||
768 | TestInfo* GetMutableTestInfo(int i); | |||
769 | ||||
770 | // Sets the should_run member. | |||
771 | void set_should_run(bool should) { should_run_ = should; } | |||
772 | ||||
773 | // Adds a TestInfo to this test suite. Will delete the TestInfo upon | |||
774 | // destruction of the TestSuite object. | |||
775 | void AddTestInfo(TestInfo* test_info); | |||
776 | ||||
777 | // Clears the results of all tests in this test suite. | |||
778 | void ClearResult(); | |||
779 | ||||
780 | // Clears the results of all tests in the given test suite. | |||
781 | static void ClearTestSuiteResult(TestSuite* test_suite) { | |||
782 | test_suite->ClearResult(); | |||
783 | } | |||
784 | ||||
785 | // Runs every test in this TestSuite. | |||
786 | void Run(); | |||
787 | ||||
788 | // Skips the execution of tests under this TestSuite | |||
789 | void Skip(); | |||
790 | ||||
791 | // Runs SetUpTestSuite() for this TestSuite. This wrapper is needed | |||
792 | // for catching exceptions thrown from SetUpTestSuite(). | |||
793 | void RunSetUpTestSuite() { | |||
794 | if (set_up_tc_ != nullptr) { | |||
795 | (*set_up_tc_)(); | |||
796 | } | |||
797 | } | |||
798 | ||||
799 | // Runs TearDownTestSuite() for this TestSuite. This wrapper is | |||
800 | // needed for catching exceptions thrown from TearDownTestSuite(). | |||
801 | void RunTearDownTestSuite() { | |||
802 | if (tear_down_tc_ != nullptr) { | |||
803 | (*tear_down_tc_)(); | |||
804 | } | |||
805 | } | |||
806 | ||||
807 | // Returns true if and only if test passed. | |||
808 | static bool TestPassed(const TestInfo* test_info) { | |||
809 | return test_info->should_run() && test_info->result()->Passed(); | |||
810 | } | |||
811 | ||||
812 | // Returns true if and only if test skipped. | |||
813 | static bool TestSkipped(const TestInfo* test_info) { | |||
814 | return test_info->should_run() && test_info->result()->Skipped(); | |||
815 | } | |||
816 | ||||
817 | // Returns true if and only if test failed. | |||
818 | static bool TestFailed(const TestInfo* test_info) { | |||
819 | return test_info->should_run() && test_info->result()->Failed(); | |||
820 | } | |||
821 | ||||
822 | // Returns true if and only if the test is disabled and will be reported in | |||
823 | // the XML report. | |||
824 | static bool TestReportableDisabled(const TestInfo* test_info) { | |||
825 | return test_info->is_reportable() && test_info->is_disabled_; | |||
826 | } | |||
827 | ||||
828 | // Returns true if and only if test is disabled. | |||
829 | static bool TestDisabled(const TestInfo* test_info) { | |||
830 | return test_info->is_disabled_; | |||
831 | } | |||
832 | ||||
833 | // Returns true if and only if this test will appear in the XML report. | |||
834 | static bool TestReportable(const TestInfo* test_info) { | |||
835 | return test_info->is_reportable(); | |||
836 | } | |||
837 | ||||
838 | // Returns true if the given test should run. | |||
839 | static bool ShouldRunTest(const TestInfo* test_info) { | |||
840 | return test_info->should_run(); | |||
841 | } | |||
842 | ||||
843 | // Shuffles the tests in this test suite. | |||
844 | void ShuffleTests(internal::Random* random); | |||
845 | ||||
846 | // Restores the test order to before the first shuffle. | |||
847 | void UnshuffleTests(); | |||
848 | ||||
849 | // Name of the test suite. | |||
850 | std::string name_; | |||
851 | // Name of the parameter type, or NULL if this is not a typed or a | |||
852 | // type-parameterized test. | |||
853 | const std::unique_ptr<const ::std::string> type_param_; | |||
854 | // The vector of TestInfos in their original order. It owns the | |||
855 | // elements in the vector. | |||
856 | std::vector<TestInfo*> test_info_list_; | |||
857 | // Provides a level of indirection for the test list to allow easy | |||
858 | // shuffling and restoring the test order. The i-th element in this | |||
859 | // vector is the index of the i-th test in the shuffled test list. | |||
860 | std::vector<int> test_indices_; | |||
861 | // Pointer to the function that sets up the test suite. | |||
862 | internal::SetUpTestSuiteFunc set_up_tc_; | |||
863 | // Pointer to the function that tears down the test suite. | |||
864 | internal::TearDownTestSuiteFunc tear_down_tc_; | |||
865 | // True if and only if any test in this test suite should run. | |||
866 | bool should_run_; | |||
867 | // The start time, in milliseconds since UNIX Epoch. | |||
868 | TimeInMillis start_timestamp_; | |||
869 | // Elapsed time, in milliseconds. | |||
870 | TimeInMillis elapsed_time_; | |||
871 | // Holds test properties recorded during execution of SetUpTestSuite and | |||
872 | // TearDownTestSuite. | |||
873 | TestResult ad_hoc_test_result_; | |||
874 | ||||
875 | // We disallow copying TestSuites. | |||
876 | TestSuite(const TestSuite&) = delete; | |||
877 | TestSuite& operator=(const TestSuite&) = delete; | |||
878 | }; | |||
879 | ||||
880 | // An Environment object is capable of setting up and tearing down an | |||
881 | // environment. You should subclass this to define your own | |||
882 | // environment(s). | |||
883 | // | |||
884 | // An Environment object does the set-up and tear-down in virtual | |||
885 | // methods SetUp() and TearDown() instead of the constructor and the | |||
886 | // destructor, as: | |||
887 | // | |||
888 | // 1. You cannot safely throw from a destructor. This is a problem | |||
889 | // as in some cases Google Test is used where exceptions are enabled, and | |||
890 | // we may want to implement ASSERT_* using exceptions where they are | |||
891 | // available. | |||
892 | // 2. You cannot use ASSERT_* directly in a constructor or | |||
893 | // destructor. | |||
894 | class Environment { | |||
895 | public: | |||
896 | // The d'tor is virtual as we need to subclass Environment. | |||
897 | virtual ~Environment() = default; | |||
898 | ||||
899 | // Override this to define how to set up the environment. | |||
900 | virtual void SetUp() {} | |||
901 | ||||
902 | // Override this to define how to tear down the environment. | |||
903 | virtual void TearDown() {} | |||
904 | ||||
905 | private: | |||
906 | // If you see an error about overriding the following function or | |||
907 | // about it being private, you have mis-spelled SetUp() as Setup(). | |||
908 | struct Setup_should_be_spelled_SetUp {}; | |||
909 | virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } | |||
910 | }; | |||
911 | ||||
912 | #if GTEST_HAS_EXCEPTIONS1 | |||
913 | ||||
914 | // Exception which can be thrown from TestEventListener::OnTestPartResult. | |||
915 | class GTEST_API___attribute__((visibility("default"))) AssertionException | |||
916 | : public internal::GoogleTestFailureException { | |||
917 | public: | |||
918 | explicit AssertionException(const TestPartResult& result) | |||
919 | : GoogleTestFailureException(result) {} | |||
920 | }; | |||
921 | ||||
922 | #endif // GTEST_HAS_EXCEPTIONS | |||
923 | ||||
924 | // The interface for tracing execution of tests. The methods are organized in | |||
925 | // the order the corresponding events are fired. | |||
926 | class TestEventListener { | |||
927 | public: | |||
928 | virtual ~TestEventListener() = default; | |||
929 | ||||
930 | // Fired before any test activity starts. | |||
931 | virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; | |||
932 | ||||
933 | // Fired before each iteration of tests starts. There may be more than | |||
934 | // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration | |||
935 | // index, starting from 0. | |||
936 | virtual void OnTestIterationStart(const UnitTest& unit_test, | |||
937 | int iteration) = 0; | |||
938 | ||||
939 | // Fired before environment set-up for each iteration of tests starts. | |||
940 | virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; | |||
941 | ||||
942 | // Fired after environment set-up for each iteration of tests ends. | |||
943 | virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; | |||
944 | ||||
945 | // Fired before the test suite starts. | |||
946 | virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {} | |||
947 | ||||
948 | // Legacy API is deprecated but still available | |||
949 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
950 | virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} | |||
951 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
952 | ||||
953 | // Fired before the test starts. | |||
954 | virtual void OnTestStart(const TestInfo& test_info) = 0; | |||
955 | ||||
956 | // Fired when a test is disabled | |||
957 | virtual void OnTestDisabled(const TestInfo& /*test_info*/) {} | |||
958 | ||||
959 | // Fired after a failed assertion or a SUCCEED() invocation. | |||
960 | // If you want to throw an exception from this function to skip to the next | |||
961 | // TEST, it must be AssertionException defined above, or inherited from it. | |||
962 | virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; | |||
963 | ||||
964 | // Fired after the test ends. | |||
965 | virtual void OnTestEnd(const TestInfo& test_info) = 0; | |||
966 | ||||
967 | // Fired after the test suite ends. | |||
968 | virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {} | |||
969 | ||||
970 | // Legacy API is deprecated but still available | |||
971 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
972 | virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} | |||
973 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
974 | ||||
975 | // Fired before environment tear-down for each iteration of tests starts. | |||
976 | virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; | |||
977 | ||||
978 | // Fired after environment tear-down for each iteration of tests ends. | |||
979 | virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; | |||
980 | ||||
981 | // Fired after each iteration of tests finishes. | |||
982 | virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration) = 0; | |||
983 | ||||
984 | // Fired after all test activities have ended. | |||
985 | virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; | |||
986 | }; | |||
987 | ||||
988 | // The convenience class for users who need to override just one or two | |||
989 | // methods and are not concerned that a possible change to a signature of | |||
990 | // the methods they override will not be caught during the build. For | |||
991 | // comments about each method please see the definition of TestEventListener | |||
992 | // above. | |||
993 | class EmptyTestEventListener : public TestEventListener { | |||
994 | public: | |||
995 | void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} | |||
996 | void OnTestIterationStart(const UnitTest& /*unit_test*/, | |||
997 | int /*iteration*/) override {} | |||
998 | void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} | |||
999 | void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} | |||
1000 | void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} | |||
1001 | // Legacy API is deprecated but still available | |||
1002 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1003 | void OnTestCaseStart(const TestCase& /*test_case*/) override {} | |||
1004 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1005 | ||||
1006 | void OnTestStart(const TestInfo& /*test_info*/) override {} | |||
1007 | void OnTestDisabled(const TestInfo& /*test_info*/) override {} | |||
1008 | void OnTestPartResult(const TestPartResult& /*test_part_result*/) override {} | |||
1009 | void OnTestEnd(const TestInfo& /*test_info*/) override {} | |||
1010 | void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} | |||
1011 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1012 | void OnTestCaseEnd(const TestCase& /*test_case*/) override {} | |||
1013 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1014 | ||||
1015 | void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} | |||
1016 | void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} | |||
1017 | void OnTestIterationEnd(const UnitTest& /*unit_test*/, | |||
1018 | int /*iteration*/) override {} | |||
1019 | void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} | |||
1020 | }; | |||
1021 | ||||
1022 | // TestEventListeners lets users add listeners to track events in Google Test. | |||
1023 | class GTEST_API___attribute__((visibility("default"))) TestEventListeners { | |||
1024 | public: | |||
1025 | TestEventListeners(); | |||
1026 | ~TestEventListeners(); | |||
1027 | ||||
1028 | // Appends an event listener to the end of the list. Google Test assumes | |||
1029 | // the ownership of the listener (i.e. it will delete the listener when | |||
1030 | // the test program finishes). | |||
1031 | void Append(TestEventListener* listener); | |||
1032 | ||||
1033 | // Removes the given event listener from the list and returns it. It then | |||
1034 | // becomes the caller's responsibility to delete the listener. Returns | |||
1035 | // NULL if the listener is not found in the list. | |||
1036 | TestEventListener* Release(TestEventListener* listener); | |||
1037 | ||||
1038 | // Returns the standard listener responsible for the default console | |||
1039 | // output. Can be removed from the listeners list to shut down default | |||
1040 | // console output. Note that removing this object from the listener list | |||
1041 | // with Release transfers its ownership to the caller and makes this | |||
1042 | // function return NULL the next time. | |||
1043 | TestEventListener* default_result_printer() const { | |||
1044 | return default_result_printer_; | |||
1045 | } | |||
1046 | ||||
1047 | // Returns the standard listener responsible for the default XML output | |||
1048 | // controlled by the --gtest_output=xml flag. Can be removed from the | |||
1049 | // listeners list by users who want to shut down the default XML output | |||
1050 | // controlled by this flag and substitute it with custom one. Note that | |||
1051 | // removing this object from the listener list with Release transfers its | |||
1052 | // ownership to the caller and makes this function return NULL the next | |||
1053 | // time. | |||
1054 | TestEventListener* default_xml_generator() const { | |||
1055 | return default_xml_generator_; | |||
1056 | } | |||
1057 | ||||
1058 | // Controls whether events will be forwarded by the repeater to the | |||
1059 | // listeners in the list. | |||
1060 | void SuppressEventForwarding(bool); | |||
1061 | ||||
1062 | private: | |||
1063 | friend class TestSuite; | |||
1064 | friend class TestInfo; | |||
1065 | friend class internal::DefaultGlobalTestPartResultReporter; | |||
1066 | friend class internal::NoExecDeathTest; | |||
1067 | friend class internal::TestEventListenersAccessor; | |||
1068 | friend class internal::UnitTestImpl; | |||
1069 | ||||
1070 | // Returns repeater that broadcasts the TestEventListener events to all | |||
1071 | // subscribers. | |||
1072 | TestEventListener* repeater(); | |||
1073 | ||||
1074 | // Sets the default_result_printer attribute to the provided listener. | |||
1075 | // The listener is also added to the listener list and previous | |||
1076 | // default_result_printer is removed from it and deleted. The listener can | |||
1077 | // also be NULL in which case it will not be added to the list. Does | |||
1078 | // nothing if the previous and the current listener objects are the same. | |||
1079 | void SetDefaultResultPrinter(TestEventListener* listener); | |||
1080 | ||||
1081 | // Sets the default_xml_generator attribute to the provided listener. The | |||
1082 | // listener is also added to the listener list and previous | |||
1083 | // default_xml_generator is removed from it and deleted. The listener can | |||
1084 | // also be NULL in which case it will not be added to the list. Does | |||
1085 | // nothing if the previous and the current listener objects are the same. | |||
1086 | void SetDefaultXmlGenerator(TestEventListener* listener); | |||
1087 | ||||
1088 | // Controls whether events will be forwarded by the repeater to the | |||
1089 | // listeners in the list. | |||
1090 | bool EventForwardingEnabled() const; | |||
1091 | ||||
1092 | // The actual list of listeners. | |||
1093 | internal::TestEventRepeater* repeater_; | |||
1094 | // Listener responsible for the standard result output. | |||
1095 | TestEventListener* default_result_printer_; | |||
1096 | // Listener responsible for the creation of the XML output file. | |||
1097 | TestEventListener* default_xml_generator_; | |||
1098 | ||||
1099 | // We disallow copying TestEventListeners. | |||
1100 | TestEventListeners(const TestEventListeners&) = delete; | |||
1101 | TestEventListeners& operator=(const TestEventListeners&) = delete; | |||
1102 | }; | |||
1103 | ||||
1104 | // A UnitTest consists of a vector of TestSuites. | |||
1105 | // | |||
1106 | // This is a singleton class. The only instance of UnitTest is | |||
1107 | // created when UnitTest::GetInstance() is first called. This | |||
1108 | // instance is never deleted. | |||
1109 | // | |||
1110 | // UnitTest is not copyable. | |||
1111 | // | |||
1112 | // This class is thread-safe as long as the methods are called | |||
1113 | // according to their specification. | |||
1114 | class GTEST_API___attribute__((visibility("default"))) UnitTest { | |||
1115 | public: | |||
1116 | // Gets the singleton UnitTest object. The first time this method | |||
1117 | // is called, a UnitTest object is constructed and returned. | |||
1118 | // Consecutive calls will return the same object. | |||
1119 | static UnitTest* GetInstance(); | |||
1120 | ||||
1121 | // Runs all tests in this UnitTest object and prints the result. | |||
1122 | // Returns 0 if successful, or 1 otherwise. | |||
1123 | // | |||
1124 | // This method can only be called from the main thread. | |||
1125 | // | |||
1126 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1127 | int Run() GTEST_MUST_USE_RESULT___attribute__((warn_unused_result)); | |||
1128 | ||||
1129 | // Returns the working directory when the first TEST() or TEST_F() | |||
1130 | // was executed. The UnitTest object owns the string. | |||
1131 | const char* original_working_dir() const; | |||
1132 | ||||
1133 | // Returns the TestSuite object for the test that's currently running, | |||
1134 | // or NULL if no test is running. | |||
1135 | const TestSuite* current_test_suite() const GTEST_LOCK_EXCLUDED_(mutex_); | |||
1136 | ||||
1137 | // Legacy API is still available but deprecated | |||
1138 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1139 | const TestCase* current_test_case() const GTEST_LOCK_EXCLUDED_(mutex_); | |||
1140 | #endif | |||
1141 | ||||
1142 | // Returns the TestInfo object for the test that's currently running, | |||
1143 | // or NULL if no test is running. | |||
1144 | const TestInfo* current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_); | |||
1145 | ||||
1146 | // Returns the random seed used at the start of the current test run. | |||
1147 | int random_seed() const; | |||
1148 | ||||
1149 | // Returns the ParameterizedTestSuiteRegistry object used to keep track of | |||
1150 | // value-parameterized tests and instantiate and register them. | |||
1151 | // | |||
1152 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1153 | internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() | |||
1154 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
1155 | ||||
1156 | // Gets the number of successful test suites. | |||
1157 | int successful_test_suite_count() const; | |||
1158 | ||||
1159 | // Gets the number of failed test suites. | |||
1160 | int failed_test_suite_count() const; | |||
1161 | ||||
1162 | // Gets the number of all test suites. | |||
1163 | int total_test_suite_count() const; | |||
1164 | ||||
1165 | // Gets the number of all test suites that contain at least one test | |||
1166 | // that should run. | |||
1167 | int test_suite_to_run_count() const; | |||
1168 | ||||
1169 | // Legacy API is deprecated but still available | |||
1170 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1171 | int successful_test_case_count() const; | |||
1172 | int failed_test_case_count() const; | |||
1173 | int total_test_case_count() const; | |||
1174 | int test_case_to_run_count() const; | |||
1175 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1176 | ||||
1177 | // Gets the number of successful tests. | |||
1178 | int successful_test_count() const; | |||
1179 | ||||
1180 | // Gets the number of skipped tests. | |||
1181 | int skipped_test_count() const; | |||
1182 | ||||
1183 | // Gets the number of failed tests. | |||
1184 | int failed_test_count() const; | |||
1185 | ||||
1186 | // Gets the number of disabled tests that will be reported in the XML report. | |||
1187 | int reportable_disabled_test_count() const; | |||
1188 | ||||
1189 | // Gets the number of disabled tests. | |||
1190 | int disabled_test_count() const; | |||
1191 | ||||
1192 | // Gets the number of tests to be printed in the XML report. | |||
1193 | int reportable_test_count() const; | |||
1194 | ||||
1195 | // Gets the number of all tests. | |||
1196 | int total_test_count() const; | |||
1197 | ||||
1198 | // Gets the number of tests that should run. | |||
1199 | int test_to_run_count() const; | |||
1200 | ||||
1201 | // Gets the time of the test program start, in ms from the start of the | |||
1202 | // UNIX epoch. | |||
1203 | TimeInMillis start_timestamp() const; | |||
1204 | ||||
1205 | // Gets the elapsed time, in milliseconds. | |||
1206 | TimeInMillis elapsed_time() const; | |||
1207 | ||||
1208 | // Returns true if and only if the unit test passed (i.e. all test suites | |||
1209 | // passed). | |||
1210 | bool Passed() const; | |||
1211 | ||||
1212 | // Returns true if and only if the unit test failed (i.e. some test suite | |||
1213 | // failed or something outside of all tests failed). | |||
1214 | bool Failed() const; | |||
1215 | ||||
1216 | // Gets the i-th test suite among all the test suites. i can range from 0 to | |||
1217 | // total_test_suite_count() - 1. If i is not in that range, returns NULL. | |||
1218 | const TestSuite* GetTestSuite(int i) const; | |||
1219 | ||||
1220 | // Legacy API is deprecated but still available | |||
1221 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1222 | const TestCase* GetTestCase(int i) const; | |||
1223 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
1224 | ||||
1225 | // Returns the TestResult containing information on test failures and | |||
1226 | // properties logged outside of individual test suites. | |||
1227 | const TestResult& ad_hoc_test_result() const; | |||
1228 | ||||
1229 | // Returns the list of event listeners that can be used to track events | |||
1230 | // inside Google Test. | |||
1231 | TestEventListeners& listeners(); | |||
1232 | ||||
1233 | private: | |||
1234 | // Registers and returns a global test environment. When a test | |||
1235 | // program is run, all global test environments will be set-up in | |||
1236 | // the order they were registered. After all tests in the program | |||
1237 | // have finished, all global test environments will be torn-down in | |||
1238 | // the *reverse* order they were registered. | |||
1239 | // | |||
1240 | // The UnitTest object takes ownership of the given environment. | |||
1241 | // | |||
1242 | // This method can only be called from the main thread. | |||
1243 | Environment* AddEnvironment(Environment* env); | |||
1244 | ||||
1245 | // Adds a TestPartResult to the current TestResult object. All | |||
1246 | // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) | |||
1247 | // eventually call this to report their results. The user code | |||
1248 | // should use the assertion macros instead of calling this directly. | |||
1249 | void AddTestPartResult(TestPartResult::Type result_type, | |||
1250 | const char* file_name, int line_number, | |||
1251 | const std::string& message, | |||
1252 | const std::string& os_stack_trace) | |||
1253 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
1254 | ||||
1255 | // Adds a TestProperty to the current TestResult object when invoked from | |||
1256 | // inside a test, to current TestSuite's ad_hoc_test_result_ when invoked | |||
1257 | // from SetUpTestSuite or TearDownTestSuite, or to the global property set | |||
1258 | // when invoked elsewhere. If the result already contains a property with | |||
1259 | // the same key, the value will be updated. | |||
1260 | void RecordProperty(const std::string& key, const std::string& value); | |||
1261 | ||||
1262 | // Gets the i-th test suite among all the test suites. i can range from 0 to | |||
1263 | // total_test_suite_count() - 1. If i is not in that range, returns NULL. | |||
1264 | TestSuite* GetMutableTestSuite(int i); | |||
1265 | ||||
1266 | // Accessors for the implementation object. | |||
1267 | internal::UnitTestImpl* impl() { return impl_; } | |||
1268 | const internal::UnitTestImpl* impl() const { return impl_; } | |||
1269 | ||||
1270 | // These classes and functions are friends as they need to access private | |||
1271 | // members of UnitTest. | |||
1272 | friend class ScopedTrace; | |||
1273 | friend class Test; | |||
1274 | friend class internal::AssertHelper; | |||
1275 | friend class internal::StreamingListenerTest; | |||
1276 | friend class internal::UnitTestRecordPropertyTestHelper; | |||
1277 | friend Environment* AddGlobalTestEnvironment(Environment* env); | |||
1278 | friend std::set<std::string>* internal::GetIgnoredParameterizedTestSuites(); | |||
1279 | friend internal::UnitTestImpl* internal::GetUnitTestImpl(); | |||
1280 | friend void internal::ReportFailureInUnknownLocation( | |||
1281 | TestPartResult::Type result_type, const std::string& message); | |||
1282 | ||||
1283 | // Creates an empty UnitTest. | |||
1284 | UnitTest(); | |||
1285 | ||||
1286 | // D'tor | |||
1287 | virtual ~UnitTest(); | |||
1288 | ||||
1289 | // Pushes a trace defined by SCOPED_TRACE() on to the per-thread | |||
1290 | // Google Test trace stack. | |||
1291 | void PushGTestTrace(const internal::TraceInfo& trace) | |||
1292 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
1293 | ||||
1294 | // Pops a trace from the per-thread Google Test trace stack. | |||
1295 | void PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_); | |||
1296 | ||||
1297 | // Protects mutable state in *impl_. This is mutable as some const | |||
1298 | // methods need to lock it too. | |||
1299 | mutable internal::Mutex mutex_; | |||
1300 | ||||
1301 | // Opaque implementation object. This field is never changed once | |||
1302 | // the object is constructed. We don't mark it as const here, as | |||
1303 | // doing so will cause a warning in the constructor of UnitTest. | |||
1304 | // Mutable state in *impl_ is protected by mutex_. | |||
1305 | internal::UnitTestImpl* impl_; | |||
1306 | ||||
1307 | // We disallow copying UnitTest. | |||
1308 | UnitTest(const UnitTest&) = delete; | |||
1309 | UnitTest& operator=(const UnitTest&) = delete; | |||
1310 | }; | |||
1311 | ||||
1312 | // A convenient wrapper for adding an environment for the test | |||
1313 | // program. | |||
1314 | // | |||
1315 | // You should call this before RUN_ALL_TESTS() is called, probably in | |||
1316 | // main(). If you use gtest_main, you need to call this before main() | |||
1317 | // starts for it to take effect. For example, you can define a global | |||
1318 | // variable like this: | |||
1319 | // | |||
1320 | // testing::Environment* const foo_env = | |||
1321 | // testing::AddGlobalTestEnvironment(new FooEnvironment); | |||
1322 | // | |||
1323 | // However, we strongly recommend you to write your own main() and | |||
1324 | // call AddGlobalTestEnvironment() there, as relying on initialization | |||
1325 | // of global variables makes the code harder to read and may cause | |||
1326 | // problems when you register multiple environments from different | |||
1327 | // translation units and the environments have dependencies among them | |||
1328 | // (remember that the compiler doesn't guarantee the order in which | |||
1329 | // global variables from different translation units are initialized). | |||
1330 | inline Environment* AddGlobalTestEnvironment(Environment* env) { | |||
1331 | return UnitTest::GetInstance()->AddEnvironment(env); | |||
1332 | } | |||
1333 | ||||
1334 | // Initializes Google Test. This must be called before calling | |||
1335 | // RUN_ALL_TESTS(). In particular, it parses a command line for the | |||
1336 | // flags that Google Test recognizes. Whenever a Google Test flag is | |||
1337 | // seen, it is removed from argv, and *argc is decremented. | |||
1338 | // | |||
1339 | // No value is returned. Instead, the Google Test flag variables are | |||
1340 | // updated. | |||
1341 | // | |||
1342 | // Calling the function for the second time has no user-visible effect. | |||
1343 | GTEST_API___attribute__((visibility("default"))) void InitGoogleTest(int* argc, char** argv); | |||
1344 | ||||
1345 | // This overloaded version can be used in Windows programs compiled in | |||
1346 | // UNICODE mode. | |||
1347 | GTEST_API___attribute__((visibility("default"))) void InitGoogleTest(int* argc, wchar_t** argv); | |||
1348 | ||||
1349 | // This overloaded version can be used on Arduino/embedded platforms where | |||
1350 | // there is no argc/argv. | |||
1351 | GTEST_API___attribute__((visibility("default"))) void InitGoogleTest(); | |||
1352 | ||||
1353 | namespace internal { | |||
1354 | ||||
1355 | // Separate the error generating code from the code path to reduce the stack | |||
1356 | // frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers | |||
1357 | // when calling EXPECT_* in a tight loop. | |||
1358 | template <typename T1, typename T2> | |||
1359 | AssertionResult CmpHelperEQFailure(const char* lhs_expression, | |||
1360 | const char* rhs_expression, const T1& lhs, | |||
1361 | const T2& rhs) { | |||
1362 | return EqFailure(lhs_expression, rhs_expression, | |||
1363 | FormatForComparisonFailureMessage(lhs, rhs), | |||
1364 | FormatForComparisonFailureMessage(rhs, lhs), false); | |||
1365 | } | |||
1366 | ||||
1367 | // This block of code defines operator==/!= | |||
1368 | // to block lexical scope lookup. | |||
1369 | // It prevents using invalid operator==/!= defined at namespace scope. | |||
1370 | struct faketype {}; | |||
1371 | inline bool operator==(faketype, faketype) { return true; } | |||
1372 | inline bool operator!=(faketype, faketype) { return false; } | |||
1373 | ||||
1374 | // The helper function for {ASSERT|EXPECT}_EQ. | |||
1375 | template <typename T1, typename T2> | |||
1376 | AssertionResult CmpHelperEQ(const char* lhs_expression, | |||
1377 | const char* rhs_expression, const T1& lhs, | |||
1378 | const T2& rhs) { | |||
1379 | if (lhs == rhs) { | |||
1380 | return AssertionSuccess(); | |||
1381 | } | |||
1382 | ||||
1383 | return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs); | |||
1384 | } | |||
1385 | ||||
1386 | class EqHelper { | |||
1387 | public: | |||
1388 | // This templatized version is for the general case. | |||
1389 | template < | |||
1390 | typename T1, typename T2, | |||
1391 | // Disable this overload for cases where one argument is a pointer | |||
1392 | // and the other is the null pointer constant. | |||
1393 | typename std::enable_if<!std::is_integral<T1>::value || | |||
1394 | !std::is_pointer<T2>::value>::type* = nullptr> | |||
1395 | static AssertionResult Compare(const char* lhs_expression, | |||
1396 | const char* rhs_expression, const T1& lhs, | |||
1397 | const T2& rhs) { | |||
1398 | return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); | |||
1399 | } | |||
1400 | ||||
1401 | // With this overloaded version, we allow anonymous enums to be used | |||
1402 | // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous | |||
1403 | // enums can be implicitly cast to BiggestInt. | |||
1404 | // | |||
1405 | // Even though its body looks the same as the above version, we | |||
1406 | // cannot merge the two, as it will make anonymous enums unhappy. | |||
1407 | static AssertionResult Compare(const char* lhs_expression, | |||
1408 | const char* rhs_expression, BiggestInt lhs, | |||
1409 | BiggestInt rhs) { | |||
1410 | return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); | |||
1411 | } | |||
1412 | ||||
1413 | template <typename T> | |||
1414 | static AssertionResult Compare( | |||
1415 | const char* lhs_expression, const char* rhs_expression, | |||
1416 | // Handle cases where '0' is used as a null pointer literal. | |||
1417 | std::nullptr_t /* lhs */, T* rhs) { | |||
1418 | // We already know that 'lhs' is a null pointer. | |||
1419 | return CmpHelperEQ(lhs_expression, rhs_expression, static_cast<T*>(nullptr), | |||
1420 | rhs); | |||
1421 | } | |||
1422 | }; | |||
1423 | ||||
1424 | // Separate the error generating code from the code path to reduce the stack | |||
1425 | // frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers | |||
1426 | // when calling EXPECT_OP in a tight loop. | |||
1427 | template <typename T1, typename T2> | |||
1428 | AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, | |||
1429 | const T1& val1, const T2& val2, | |||
1430 | const char* op) { | |||
1431 | return AssertionFailure() | |||
1432 | << "Expected: (" << expr1 << ") " << op << " (" << expr2 | |||
1433 | << "), actual: " << FormatForComparisonFailureMessage(val1, val2) | |||
1434 | << " vs " << FormatForComparisonFailureMessage(val2, val1); | |||
1435 | } | |||
1436 | ||||
1437 | // A macro for implementing the helper functions needed to implement | |||
1438 | // ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste | |||
1439 | // of similar code. | |||
1440 | // | |||
1441 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1442 | ||||
1443 | #define GTEST_IMPL_CMP_HELPER_(op_name, op) \ | |||
1444 | template <typename T1, typename T2> \ | |||
1445 | AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ | |||
1446 | const T1& val1, const T2& val2) { \ | |||
1447 | if (val1 op val2) { \ | |||
1448 | return AssertionSuccess(); \ | |||
1449 | } else { \ | |||
1450 | return CmpHelperOpFailure(expr1, expr2, val1, val2, #op); \ | |||
1451 | } \ | |||
1452 | } | |||
1453 | ||||
1454 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1455 | ||||
1456 | // Implements the helper function for {ASSERT|EXPECT}_NE | |||
1457 | GTEST_IMPL_CMP_HELPER_(NE, !=) | |||
| ||||
1458 | // Implements the helper function for {ASSERT|EXPECT}_LE | |||
1459 | GTEST_IMPL_CMP_HELPER_(LE, <=) | |||
1460 | // Implements the helper function for {ASSERT|EXPECT}_LT | |||
1461 | GTEST_IMPL_CMP_HELPER_(LT, <) | |||
1462 | // Implements the helper function for {ASSERT|EXPECT}_GE | |||
1463 | GTEST_IMPL_CMP_HELPER_(GE, >=) | |||
1464 | // Implements the helper function for {ASSERT|EXPECT}_GT | |||
1465 | GTEST_IMPL_CMP_HELPER_(GT, >) | |||
1466 | ||||
1467 | #undef GTEST_IMPL_CMP_HELPER_ | |||
1468 | ||||
1469 | // The helper function for {ASSERT|EXPECT}_STREQ. | |||
1470 | // | |||
1471 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1472 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTREQ(const char* s1_expression, | |||
1473 | const char* s2_expression, | |||
1474 | const char* s1, const char* s2); | |||
1475 | ||||
1476 | // The helper function for {ASSERT|EXPECT}_STRCASEEQ. | |||
1477 | // | |||
1478 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1479 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression, | |||
1480 | const char* s2_expression, | |||
1481 | const char* s1, const char* s2); | |||
1482 | ||||
1483 | // The helper function for {ASSERT|EXPECT}_STRNE. | |||
1484 | // | |||
1485 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1486 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRNE(const char* s1_expression, | |||
1487 | const char* s2_expression, | |||
1488 | const char* s1, const char* s2); | |||
1489 | ||||
1490 | // The helper function for {ASSERT|EXPECT}_STRCASENE. | |||
1491 | // | |||
1492 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1493 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRCASENE(const char* s1_expression, | |||
1494 | const char* s2_expression, | |||
1495 | const char* s1, const char* s2); | |||
1496 | ||||
1497 | // Helper function for *_STREQ on wide strings. | |||
1498 | // | |||
1499 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1500 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTREQ(const char* s1_expression, | |||
1501 | const char* s2_expression, | |||
1502 | const wchar_t* s1, const wchar_t* s2); | |||
1503 | ||||
1504 | // Helper function for *_STRNE on wide strings. | |||
1505 | // | |||
1506 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1507 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRNE(const char* s1_expression, | |||
1508 | const char* s2_expression, | |||
1509 | const wchar_t* s1, const wchar_t* s2); | |||
1510 | ||||
1511 | } // namespace internal | |||
1512 | ||||
1513 | // IsSubstring() and IsNotSubstring() are intended to be used as the | |||
1514 | // first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by | |||
1515 | // themselves. They check whether needle is a substring of haystack | |||
1516 | // (NULL is considered a substring of itself only), and return an | |||
1517 | // appropriate error message when they fail. | |||
1518 | // | |||
1519 | // The {needle,haystack}_expr arguments are the stringified | |||
1520 | // expressions that generated the two real arguments. | |||
1521 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
1522 | const char* haystack_expr, | |||
1523 | const char* needle, | |||
1524 | const char* haystack); | |||
1525 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
1526 | const char* haystack_expr, | |||
1527 | const wchar_t* needle, | |||
1528 | const wchar_t* haystack); | |||
1529 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
1530 | const char* haystack_expr, | |||
1531 | const char* needle, | |||
1532 | const char* haystack); | |||
1533 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
1534 | const char* haystack_expr, | |||
1535 | const wchar_t* needle, | |||
1536 | const wchar_t* haystack); | |||
1537 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
1538 | const char* haystack_expr, | |||
1539 | const ::std::string& needle, | |||
1540 | const ::std::string& haystack); | |||
1541 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
1542 | const char* haystack_expr, | |||
1543 | const ::std::string& needle, | |||
1544 | const ::std::string& haystack); | |||
1545 | ||||
1546 | #if GTEST_HAS_STD_WSTRING1 | |||
1547 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
1548 | const char* haystack_expr, | |||
1549 | const ::std::wstring& needle, | |||
1550 | const ::std::wstring& haystack); | |||
1551 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
1552 | const char* haystack_expr, | |||
1553 | const ::std::wstring& needle, | |||
1554 | const ::std::wstring& haystack); | |||
1555 | #endif // GTEST_HAS_STD_WSTRING | |||
1556 | ||||
1557 | namespace internal { | |||
1558 | ||||
1559 | // Helper template function for comparing floating-points. | |||
1560 | // | |||
1561 | // Template parameter: | |||
1562 | // | |||
1563 | // RawType: the raw floating-point type (either float or double) | |||
1564 | // | |||
1565 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1566 | template <typename RawType> | |||
1567 | AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression, | |||
1568 | const char* rhs_expression, | |||
1569 | RawType lhs_value, RawType rhs_value) { | |||
1570 | const FloatingPoint<RawType> lhs(lhs_value), rhs(rhs_value); | |||
1571 | ||||
1572 | if (lhs.AlmostEquals(rhs)) { | |||
1573 | return AssertionSuccess(); | |||
1574 | } | |||
1575 | ||||
1576 | ::std::stringstream lhs_ss; | |||
1577 | lhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) | |||
1578 | << lhs_value; | |||
1579 | ||||
1580 | ::std::stringstream rhs_ss; | |||
1581 | rhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) | |||
1582 | << rhs_value; | |||
1583 | ||||
1584 | return EqFailure(lhs_expression, rhs_expression, | |||
1585 | StringStreamToString(&lhs_ss), StringStreamToString(&rhs_ss), | |||
1586 | false); | |||
1587 | } | |||
1588 | ||||
1589 | // Helper function for implementing ASSERT_NEAR. | |||
1590 | // | |||
1591 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
1592 | GTEST_API___attribute__((visibility("default"))) AssertionResult DoubleNearPredFormat(const char* expr1, | |||
1593 | const char* expr2, | |||
1594 | const char* abs_error_expr, | |||
1595 | double val1, double val2, | |||
1596 | double abs_error); | |||
1597 | ||||
1598 | // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | |||
1599 | // A class that enables one to stream messages to assertion macros | |||
1600 | class GTEST_API___attribute__((visibility("default"))) AssertHelper { | |||
1601 | public: | |||
1602 | // Constructor. | |||
1603 | AssertHelper(TestPartResult::Type type, const char* file, int line, | |||
1604 | const char* message); | |||
1605 | ~AssertHelper(); | |||
1606 | ||||
1607 | // Message assignment is a semantic trick to enable assertion | |||
1608 | // streaming; see the GTEST_MESSAGE_ macro below. | |||
1609 | void operator=(const Message& message) const; | |||
1610 | ||||
1611 | private: | |||
1612 | // We put our data in a struct so that the size of the AssertHelper class can | |||
1613 | // be as small as possible. This is important because gcc is incapable of | |||
1614 | // re-using stack space even for temporary variables, so every EXPECT_EQ | |||
1615 | // reserves stack space for another AssertHelper. | |||
1616 | struct AssertHelperData { | |||
1617 | AssertHelperData(TestPartResult::Type t, const char* srcfile, int line_num, | |||
1618 | const char* msg) | |||
1619 | : type(t), file(srcfile), line(line_num), message(msg) {} | |||
1620 | ||||
1621 | TestPartResult::Type const type; | |||
1622 | const char* const file; | |||
1623 | int const line; | |||
1624 | std::string const message; | |||
1625 | ||||
1626 | private: | |||
1627 | AssertHelperData(const AssertHelperData&) = delete; | |||
1628 | AssertHelperData& operator=(const AssertHelperData&) = delete; | |||
1629 | }; | |||
1630 | ||||
1631 | AssertHelperData* const data_; | |||
1632 | ||||
1633 | AssertHelper(const AssertHelper&) = delete; | |||
1634 | AssertHelper& operator=(const AssertHelper&) = delete; | |||
1635 | }; | |||
1636 | ||||
1637 | } // namespace internal | |||
1638 | ||||
1639 | // The pure interface class that all value-parameterized tests inherit from. | |||
1640 | // A value-parameterized class must inherit from both ::testing::Test and | |||
1641 | // ::testing::WithParamInterface. In most cases that just means inheriting | |||
1642 | // from ::testing::TestWithParam, but more complicated test hierarchies | |||
1643 | // may need to inherit from Test and WithParamInterface at different levels. | |||
1644 | // | |||
1645 | // This interface has support for accessing the test parameter value via | |||
1646 | // the GetParam() method. | |||
1647 | // | |||
1648 | // Use it with one of the parameter generator defining functions, like Range(), | |||
1649 | // Values(), ValuesIn(), Bool(), Combine(), and ConvertGenerator<T>(). | |||
1650 | // | |||
1651 | // class FooTest : public ::testing::TestWithParam<int> { | |||
1652 | // protected: | |||
1653 | // FooTest() { | |||
1654 | // // Can use GetParam() here. | |||
1655 | // } | |||
1656 | // ~FooTest() override { | |||
1657 | // // Can use GetParam() here. | |||
1658 | // } | |||
1659 | // void SetUp() override { | |||
1660 | // // Can use GetParam() here. | |||
1661 | // } | |||
1662 | // void TearDown override { | |||
1663 | // // Can use GetParam() here. | |||
1664 | // } | |||
1665 | // }; | |||
1666 | // TEST_P(FooTest, DoesBar) { | |||
1667 | // // Can use GetParam() method here. | |||
1668 | // Foo foo; | |||
1669 | // ASSERT_TRUE(foo.DoesBar(GetParam())); | |||
1670 | // } | |||
1671 | // INSTANTIATE_TEST_SUITE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); | |||
1672 | ||||
1673 | template <typename T> | |||
1674 | class WithParamInterface { | |||
1675 | public: | |||
1676 | typedef T ParamType; | |||
1677 | virtual ~WithParamInterface() = default; | |||
1678 | ||||
1679 | // The current parameter value. Is also available in the test fixture's | |||
1680 | // constructor. | |||
1681 | static const ParamType& GetParam() { | |||
1682 | GTEST_CHECK_(parameter_ != nullptr)switch (0) case 0: default: if (::testing::internal::IsTrue(parameter_ != nullptr)) ; else ::testing::internal::GTestLog(::testing:: internal::GTEST_FATAL, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1682) .GetStream() << "Condition " "parameter_ != nullptr" " failed. " | |||
1683 | << "GetParam() can only be called inside a value-parameterized test " | |||
1684 | << "-- did you intend to write TEST_P instead of TEST_F?"; | |||
1685 | return *parameter_; | |||
1686 | } | |||
1687 | ||||
1688 | private: | |||
1689 | // Sets parameter value. The caller is responsible for making sure the value | |||
1690 | // remains alive and unchanged throughout the current test. | |||
1691 | static void SetParam(const ParamType* parameter) { parameter_ = parameter; } | |||
1692 | ||||
1693 | // Static value used for accessing parameter during a test lifetime. | |||
1694 | static const ParamType* parameter_; | |||
1695 | ||||
1696 | // TestClass must be a subclass of WithParamInterface<T> and Test. | |||
1697 | template <class TestClass> | |||
1698 | friend class internal::ParameterizedTestFactory; | |||
1699 | }; | |||
1700 | ||||
1701 | template <typename T> | |||
1702 | const T* WithParamInterface<T>::parameter_ = nullptr; | |||
1703 | ||||
1704 | // Most value-parameterized classes can ignore the existence of | |||
1705 | // WithParamInterface, and can just inherit from ::testing::TestWithParam. | |||
1706 | ||||
1707 | template <typename T> | |||
1708 | class TestWithParam : public Test, public WithParamInterface<T> {}; | |||
1709 | ||||
1710 | // Macros for indicating success/failure in test code. | |||
1711 | ||||
1712 | // Skips test in runtime. | |||
1713 | // Skipping test aborts current function. | |||
1714 | // Skipped tests are neither successful nor failed. | |||
1715 | #define GTEST_SKIP()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kSkip, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1715, "") = ::testing::Message() GTEST_SKIP_("")return ::testing::internal::AssertHelper(::testing::TestPartResult ::kSkip, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1715, "") = ::testing::Message() | |||
1716 | ||||
1717 | // ADD_FAILURE unconditionally adds a failure to the current test. | |||
1718 | // SUCCEED generates a success - it doesn't automatically make the | |||
1719 | // current test successful, as a test is only successful when it has | |||
1720 | // no failure. | |||
1721 | // | |||
1722 | // EXPECT_* verifies that a certain condition is satisfied. If not, | |||
1723 | // it behaves like ADD_FAILURE. In particular: | |||
1724 | // | |||
1725 | // EXPECT_TRUE verifies that a Boolean condition is true. | |||
1726 | // EXPECT_FALSE verifies that a Boolean condition is false. | |||
1727 | // | |||
1728 | // FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except | |||
1729 | // that they will also abort the current function on failure. People | |||
1730 | // usually want the fail-fast behavior of FAIL and ASSERT_*, but those | |||
1731 | // writing data-driven tests often find themselves using ADD_FAILURE | |||
1732 | // and EXPECT_* more. | |||
1733 | ||||
1734 | // Generates a nonfatal failure with a generic message. | |||
1735 | #define ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1735, "Failed") = ::testing::Message() GTEST_NONFATAL_FAILURE_("Failed")::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1735, "Failed") = ::testing::Message() | |||
1736 | ||||
1737 | // Generates a nonfatal failure at the given source file location with | |||
1738 | // a generic message. | |||
1739 | #define ADD_FAILURE_AT(file, line)::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, file, line, "Failed") = ::testing::Message( ) \ | |||
1740 | GTEST_MESSAGE_AT_(file, line, "Failed", \::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, file, line, "Failed") = ::testing::Message( ) | |||
1741 | ::testing::TestPartResult::kNonFatalFailure)::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, file, line, "Failed") = ::testing::Message( ) | |||
1742 | ||||
1743 | // Generates a fatal failure with a generic message. | |||
1744 | #define GTEST_FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1744, "Failed") = ::testing::Message() GTEST_FATAL_FAILURE_("Failed")return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1744, "Failed") = ::testing::Message() | |||
1745 | ||||
1746 | // Like GTEST_FAIL(), but at the given source file location. | |||
1747 | #define GTEST_FAIL_AT(file, line)return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, file, line, "Failed") = ::testing::Message() \ | |||
1748 | return GTEST_MESSAGE_AT_(file, line, "Failed", \::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, file, line, "Failed") = ::testing::Message() | |||
1749 | ::testing::TestPartResult::kFatalFailure)::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, file, line, "Failed") = ::testing::Message() | |||
1750 | ||||
1751 | // Define this macro to 1 to omit the definition of FAIL(), which is a | |||
1752 | // generic name and clashes with some other libraries. | |||
1753 | #if !(defined(GTEST_DONT_DEFINE_FAIL) && GTEST_DONT_DEFINE_FAIL) | |||
1754 | #define FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1754, "Failed") = ::testing::Message() GTEST_FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1754, "Failed") = ::testing::Message() | |||
1755 | #endif | |||
1756 | ||||
1757 | // Generates a success with a generic message. | |||
1758 | #define GTEST_SUCCEED()::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1758, "Succeeded") = ::testing::Message() GTEST_SUCCESS_("Succeeded")::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1758, "Succeeded") = ::testing::Message() | |||
1759 | ||||
1760 | // Define this macro to 1 to omit the definition of SUCCEED(), which | |||
1761 | // is a generic name and clashes with some other libraries. | |||
1762 | #if !(defined(GTEST_DONT_DEFINE_SUCCEED) && GTEST_DONT_DEFINE_SUCCEED) | |||
1763 | #define SUCCEED()::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1763, "Succeeded") = ::testing::Message() GTEST_SUCCEED()::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1763, "Succeeded") = ::testing::Message() | |||
1764 | #endif | |||
1765 | ||||
1766 | // Macros for testing exceptions. | |||
1767 | // | |||
1768 | // * {ASSERT|EXPECT}_THROW(statement, expected_exception): | |||
1769 | // Tests that the statement throws the expected exception. | |||
1770 | // * {ASSERT|EXPECT}_NO_THROW(statement): | |||
1771 | // Tests that the statement doesn't throw any exception. | |||
1772 | // * {ASSERT|EXPECT}_ANY_THROW(statement): | |||
1773 | // Tests that the statement throws an exception. | |||
1774 | ||||
1775 | #define EXPECT_THROW(statement, expected_exception)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); } catch (expected_exception const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< expected_exception>::type>::type, std::exception>:: value, const ::testing::internal::NeverThrown&, const std ::exception&>::type e) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1775; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1775 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1775 ; } } else gtest_label_testthrow_1775 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1775, gtest_msg.value.c_str()) = ::testing::Message() \ | |||
1776 | GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); } catch (expected_exception const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< expected_exception>::type>::type, std::exception>:: value, const ::testing::internal::NeverThrown&, const std ::exception&>::type e) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1776; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1776 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1776 ; } } else gtest_label_testthrow_1776 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1776, gtest_msg.value.c_str()) = ::testing::Message() | |||
1777 | #define EXPECT_NO_THROW(statement)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1777; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1777 ; } } else gtest_label_testnothrow_1777 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1777, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () \ | |||
1778 | GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1778; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1778 ; } } else gtest_label_testnothrow_1778 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1778, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
1779 | #define EXPECT_ANY_THROW(statement)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { bool gtest_caught_any = false; try { if (::testing::internal ::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (...) { gtest_caught_any = true; } if (!gtest_caught_any ) { goto gtest_label_testanythrow_1779; } } else gtest_label_testanythrow_1779 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1779, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() \ | |||
1780 | GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { bool gtest_caught_any = false; try { if (::testing::internal ::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (...) { gtest_caught_any = true; } if (!gtest_caught_any ) { goto gtest_label_testanythrow_1780; } } else gtest_label_testanythrow_1780 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1780, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() | |||
1781 | #define ASSERT_THROW(statement, expected_exception)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); } catch (expected_exception const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< expected_exception>::type>::type, std::exception>:: value, const ::testing::internal::NeverThrown&, const std ::exception&>::type e) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1781; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1781 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1781 ; } } else gtest_label_testthrow_1781 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1781, gtest_msg.value.c_str()) = ::testing::Message() \ | |||
1782 | GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); } catch (expected_exception const&) { gtest_caught_expected = true; } catch (typename std::conditional< std::is_same< typename std::remove_cv<typename std::remove_reference< expected_exception>::type>::type, std::exception>:: value, const ::testing::internal::NeverThrown&, const std ::exception&>::type e) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws "; gtest_msg.value += ::testing::internal ::GetTypeName(typeid(e)); gtest_msg.value += " with description \"" ; gtest_msg.value += e.what(); gtest_msg.value += "\"."; goto gtest_label_testthrow_1782; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1782 ; } if (!gtest_caught_expected) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws nothing."; goto gtest_label_testthrow_1782 ; } } else gtest_label_testthrow_1782 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1782, gtest_msg.value.c_str()) = ::testing::Message() | |||
1783 | #define ASSERT_NO_THROW(statement)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1783; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1783 ; } } else gtest_label_testnothrow_1783 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1783, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () \ | |||
1784 | GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (std::exception const& e) { gtest_msg.value = "it throws "; gtest_msg.value += ::testing::internal::GetTypeName(typeid(e)); gtest_msg.value += " with description \""; gtest_msg.value += e.what(); gtest_msg .value += "\"."; goto gtest_label_testnothrow_1784; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1784 ; } } else gtest_label_testnothrow_1784 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1784, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
1785 | #define ASSERT_ANY_THROW(statement)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { bool gtest_caught_any = false; try { if (::testing::internal ::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (...) { gtest_caught_any = true; } if (!gtest_caught_any ) { goto gtest_label_testanythrow_1785; } } else gtest_label_testanythrow_1785 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1785, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() \ | |||
1786 | GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { bool gtest_caught_any = false; try { if (::testing::internal ::AlwaysTrue()) { statement; } else static_assert(true, ""); } catch (...) { gtest_caught_any = true; } if (!gtest_caught_any ) { goto gtest_label_testanythrow_1786; } } else gtest_label_testanythrow_1786 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1786, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() | |||
1787 | ||||
1788 | // Boolean assertions. Condition can be either a Boolean expression or an | |||
1789 | // AssertionResult. For more information on how to use AssertionResult with | |||
1790 | // these macros see comments on that class. | |||
1791 | #define GTEST_EXPECT_TRUE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1791 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () \ | |||
1792 | GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1793 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "false", "true") .c_str()) = ::testing::Message () | |||
1793 | GTEST_NONFATAL_FAILURE_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1793 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "false", "true") .c_str()) = ::testing::Message () | |||
1794 | #define GTEST_EXPECT_FALSE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1794, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () \ | |||
1795 | GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1796, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
1796 | GTEST_NONFATAL_FAILURE_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1796, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
1797 | #define GTEST_ASSERT_TRUE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1797, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () \ | |||
1798 | GTEST_TEST_BOOLEAN_(condition, #condition, false, true, GTEST_FATAL_FAILURE_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1798, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "false", "true") .c_str()) = ::testing::Message () | |||
1799 | #define GTEST_ASSERT_FALSE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1799, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () \ | |||
1800 | GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1801, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
1801 | GTEST_FATAL_FAILURE_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1801, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
1802 | ||||
1803 | // Define these macros to 1 to omit the definition of the corresponding | |||
1804 | // EXPECT or ASSERT, which clashes with some users' own code. | |||
1805 | ||||
1806 | #if !(defined(GTEST_DONT_DEFINE_EXPECT_TRUE) && GTEST_DONT_DEFINE_EXPECT_TRUE) | |||
1807 | #define EXPECT_TRUE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1807 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () GTEST_EXPECT_TRUE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1807 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () | |||
1808 | #endif | |||
1809 | ||||
1810 | #if !(defined(GTEST_DONT_DEFINE_EXPECT_FALSE) && GTEST_DONT_DEFINE_EXPECT_FALSE) | |||
1811 | #define EXPECT_FALSE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1811, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () GTEST_EXPECT_FALSE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1811, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () | |||
1812 | #endif | |||
1813 | ||||
1814 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_TRUE) && GTEST_DONT_DEFINE_ASSERT_TRUE) | |||
1815 | #define ASSERT_TRUE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1815, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () GTEST_ASSERT_TRUE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(condition)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1815, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () | |||
1816 | #endif | |||
1817 | ||||
1818 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_FALSE) && GTEST_DONT_DEFINE_ASSERT_FALSE) | |||
1819 | #define ASSERT_FALSE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1819, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () GTEST_ASSERT_FALSE(condition)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(condition))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1819, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () | |||
1820 | #endif | |||
1821 | ||||
1822 | // Macros for testing equalities and inequalities. | |||
1823 | // | |||
1824 | // * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2 | |||
1825 | // * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 | |||
1826 | // * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 | |||
1827 | // * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 | |||
1828 | // * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 | |||
1829 | // * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 | |||
1830 | // | |||
1831 | // When they are not, Google Test prints both the tested expressions and | |||
1832 | // their actual values. The values must be compatible built-in types, | |||
1833 | // or you will get a compiler error. By "compatible" we mean that the | |||
1834 | // values can be compared by the respective operator. | |||
1835 | // | |||
1836 | // Note: | |||
1837 | // | |||
1838 | // 1. It is possible to make a user-defined type work with | |||
1839 | // {ASSERT|EXPECT}_??(), but that requires overloading the | |||
1840 | // comparison operators and is thus discouraged by the Google C++ | |||
1841 | // Usage Guide. Therefore, you are advised to use the | |||
1842 | // {ASSERT|EXPECT}_TRUE() macro to assert that two objects are | |||
1843 | // equal. | |||
1844 | // | |||
1845 | // 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on | |||
1846 | // pointers (in particular, C strings). Therefore, if you use it | |||
1847 | // with two C strings, you are testing how their locations in memory | |||
1848 | // are related, not how their content is related. To compare two C | |||
1849 | // strings by content, use {ASSERT|EXPECT}_STR*(). | |||
1850 | // | |||
1851 | // 3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to | |||
1852 | // {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you | |||
1853 | // what the actual value is when it fails, and similarly for the | |||
1854 | // other comparisons. | |||
1855 | // | |||
1856 | // 4. Do not depend on the order in which {ASSERT|EXPECT}_??() | |||
1857 | // evaluate their arguments, which is undefined. | |||
1858 | // | |||
1859 | // 5. These macros evaluate their arguments exactly once. | |||
1860 | // | |||
1861 | // Examples: | |||
1862 | // | |||
1863 | // EXPECT_NE(Foo(), 5); | |||
1864 | // EXPECT_EQ(a_pointer, NULL); | |||
1865 | // ASSERT_LT(i, array_size); | |||
1866 | // ASSERT_GT(records.size(), 0) << "There is no record left."; | |||
1867 | ||||
1868 | #define EXPECT_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("val1", "val2" , val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1868, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1869 | EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("val1", "val2" , val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1869, gtest_ar.failure_message()) = ::testing::Message() | |||
1870 | #define EXPECT_NE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1870, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1871 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1871, gtest_ar.failure_message()) = ::testing::Message() | |||
1872 | #define EXPECT_LE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1872, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1873 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1873, gtest_ar.failure_message()) = ::testing::Message() | |||
1874 | #define EXPECT_LT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLT("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1874, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1875 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLT("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1875, gtest_ar.failure_message()) = ::testing::Message() | |||
1876 | #define EXPECT_GE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1876, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1877 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1877, gtest_ar.failure_message()) = ::testing::Message() | |||
1878 | #define EXPECT_GT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGT("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1878, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1879 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGT("val1", "val2", val1, val2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1879, gtest_ar.failure_message()) = ::testing::Message() | |||
1880 | ||||
1881 | #define GTEST_ASSERT_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("val1", "val2" , val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1881, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1882 | ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("val1", "val2" , val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1882, gtest_ar.failure_message()) = ::testing::Message() | |||
1883 | #define GTEST_ASSERT_NE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1883, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1884 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1884, gtest_ar.failure_message()) = ::testing::Message() | |||
1885 | #define GTEST_ASSERT_LE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1885, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1886 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1886, gtest_ar.failure_message()) = ::testing::Message() | |||
1887 | #define GTEST_ASSERT_LT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1887, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1888 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1888, gtest_ar.failure_message()) = ::testing::Message() | |||
1889 | #define GTEST_ASSERT_GE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1889, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1890 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1890, gtest_ar.failure_message()) = ::testing::Message() | |||
1891 | #define GTEST_ASSERT_GT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1891, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1892 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1892, gtest_ar.failure_message()) = ::testing::Message() | |||
1893 | ||||
1894 | // Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of | |||
1895 | // ASSERT_XY(), which clashes with some users' own code. | |||
1896 | ||||
1897 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_EQ) && GTEST_DONT_DEFINE_ASSERT_EQ) | |||
1898 | #define ASSERT_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("val1", "val2" , val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1898, gtest_ar.failure_message()) = ::testing::Message() GTEST_ASSERT_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("val1", "val2" , val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1898, gtest_ar.failure_message()) = ::testing::Message() | |||
1899 | #endif | |||
1900 | ||||
1901 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_NE) && GTEST_DONT_DEFINE_ASSERT_NE) | |||
1902 | #define ASSERT_NE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1902, gtest_ar.failure_message()) = ::testing::Message() GTEST_ASSERT_NE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperNE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1902, gtest_ar.failure_message()) = ::testing::Message() | |||
1903 | #endif | |||
1904 | ||||
1905 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_LE) && GTEST_DONT_DEFINE_ASSERT_LE) | |||
1906 | #define ASSERT_LE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1906, gtest_ar.failure_message()) = ::testing::Message() GTEST_ASSERT_LE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1906, gtest_ar.failure_message()) = ::testing::Message() | |||
1907 | #endif | |||
1908 | ||||
1909 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_LT) && GTEST_DONT_DEFINE_ASSERT_LT) | |||
1910 | #define ASSERT_LT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1910, gtest_ar.failure_message()) = ::testing::Message() GTEST_ASSERT_LT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperLT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1910, gtest_ar.failure_message()) = ::testing::Message() | |||
1911 | #endif | |||
1912 | ||||
1913 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_GE) && GTEST_DONT_DEFINE_ASSERT_GE) | |||
1914 | #define ASSERT_GE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1914, gtest_ar.failure_message()) = ::testing::Message() GTEST_ASSERT_GE(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGE("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1914, gtest_ar.failure_message()) = ::testing::Message() | |||
1915 | #endif | |||
1916 | ||||
1917 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_GT) && GTEST_DONT_DEFINE_ASSERT_GT) | |||
1918 | #define ASSERT_GT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1918, gtest_ar.failure_message()) = ::testing::Message() GTEST_ASSERT_GT(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperGT("val1", "val2", val1, val2))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1918, gtest_ar.failure_message()) = ::testing::Message() | |||
1919 | #endif | |||
1920 | ||||
1921 | // C-string Comparisons. All tests treat NULL and any non-NULL string | |||
1922 | // as different. Two NULLs are equal. | |||
1923 | // | |||
1924 | // * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 | |||
1925 | // * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 | |||
1926 | // * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case | |||
1927 | // * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case | |||
1928 | // | |||
1929 | // For wide or narrow string objects, you can use the | |||
1930 | // {ASSERT|EXPECT}_??() macros. | |||
1931 | // | |||
1932 | // Don't depend on the order in which the arguments are evaluated, | |||
1933 | // which is undefined. | |||
1934 | // | |||
1935 | // These macros evaluate their arguments exactly once. | |||
1936 | ||||
1937 | #define EXPECT_STREQ(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTREQ("s1", "s2", s1 , s2))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1937, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1938 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTREQ("s1", "s2", s1 , s2))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1938, gtest_ar.failure_message()) = ::testing::Message() | |||
1939 | #define EXPECT_STRNE(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRNE("s1", "s2", s1 , s2))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1939, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1940 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRNE("s1", "s2", s1 , s2))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1940, gtest_ar.failure_message()) = ::testing::Message() | |||
1941 | #define EXPECT_STRCASEEQ(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASEEQ("s1", "s2" , s1, s2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1941, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1942 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASEEQ("s1", "s2" , s1, s2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1942, gtest_ar.failure_message()) = ::testing::Message() | |||
1943 | #define EXPECT_STRCASENE(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASENE("s1", "s2" , s1, s2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1943, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1944 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASENE("s1", "s2" , s1, s2))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1944, gtest_ar.failure_message()) = ::testing::Message() | |||
1945 | ||||
1946 | #define ASSERT_STREQ(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTREQ("s1", "s2", s1 , s2))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1946, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1947 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTREQ("s1", "s2", s1 , s2))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1947, gtest_ar.failure_message()) = ::testing::Message() | |||
1948 | #define ASSERT_STRNE(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRNE("s1", "s2", s1 , s2))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1948, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1949 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRNE("s1", "s2", s1 , s2))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1949, gtest_ar.failure_message()) = ::testing::Message() | |||
1950 | #define ASSERT_STRCASEEQ(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASEEQ("s1", "s2" , s1, s2))) ; else return ::testing::internal::AssertHelper(:: testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1950, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1951 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASEEQ("s1", "s2" , s1, s2))) ; else return ::testing::internal::AssertHelper(:: testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1951, gtest_ar.failure_message()) = ::testing::Message() | |||
1952 | #define ASSERT_STRCASENE(s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASENE("s1", "s2" , s1, s2))) ; else return ::testing::internal::AssertHelper(:: testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1952, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1953 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperSTRCASENE("s1", "s2" , s1, s2))) ; else return ::testing::internal::AssertHelper(:: testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1953, gtest_ar.failure_message()) = ::testing::Message() | |||
1954 | ||||
1955 | // Macros for comparing floating-point numbers. | |||
1956 | // | |||
1957 | // * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2): | |||
1958 | // Tests that two float values are almost equal. | |||
1959 | // * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2): | |||
1960 | // Tests that two double values are almost equal. | |||
1961 | // * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): | |||
1962 | // Tests that v1 and v2 are within the given distance to each other. | |||
1963 | // | |||
1964 | // Google Test uses ULP-based comparison to automatically pick a default | |||
1965 | // error bound that is appropriate for the operands. See the | |||
1966 | // FloatingPoint template class in gtest-internal.h if you are | |||
1967 | // interested in the implementation details. | |||
1968 | ||||
1969 | #define EXPECT_FLOAT_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< float>("val1", "val2", val1, val2))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1969, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1970 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< float>("val1", "val2", val1, val2))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1971, gtest_ar.failure_message()) = ::testing::Message() | |||
1971 | val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< float>("val1", "val2", val1, val2))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1971, gtest_ar.failure_message()) = ::testing::Message() | |||
1972 | ||||
1973 | #define EXPECT_DOUBLE_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< double>("val1", "val2", val1, val2))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1973, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1974 | EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< double>("val1", "val2", val1, val2))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1975, gtest_ar.failure_message()) = ::testing::Message() | |||
1975 | val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< double>("val1", "val2", val1, val2))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1975, gtest_ar.failure_message()) = ::testing::Message() | |||
1976 | ||||
1977 | #define ASSERT_FLOAT_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< float>("val1", "val2", val1, val2))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1977 , gtest_ar.failure_message()) = ::testing::Message() \ | |||
1978 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< float>("val1", "val2", val1, val2))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1979 , gtest_ar.failure_message()) = ::testing::Message() | |||
1979 | val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< float>("val1", "val2", val1, val2))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1979 , gtest_ar.failure_message()) = ::testing::Message() | |||
1980 | ||||
1981 | #define ASSERT_DOUBLE_EQ(val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< double>("val1", "val2", val1, val2))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1981 , gtest_ar.failure_message()) = ::testing::Message() \ | |||
1982 | ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< double>("val1", "val2", val1, val2))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1983 , gtest_ar.failure_message()) = ::testing::Message() | |||
1983 | val1, val2)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::CmpHelperFloatingPointEQ< double>("val1", "val2", val1, val2))) ; else return ::testing ::internal::AssertHelper(::testing::TestPartResult::kFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1983 , gtest_ar.failure_message()) = ::testing::Message() | |||
1984 | ||||
1985 | #define EXPECT_NEAR(val1, val2, abs_error)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::DoubleNearPredFormat("val1" , "val2", "abs_error", val1, val2, abs_error))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1985 , gtest_ar.failure_message()) = ::testing::Message() \ | |||
1986 | EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, val1, val2, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::DoubleNearPredFormat("val1" , "val2", "abs_error", val1, val2, abs_error))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1987 , gtest_ar.failure_message()) = ::testing::Message() | |||
1987 | abs_error)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::DoubleNearPredFormat("val1" , "val2", "abs_error", val1, val2, abs_error))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "/usr/src/googletest/googletest/include/gtest/gtest.h", 1987 , gtest_ar.failure_message()) = ::testing::Message() | |||
1988 | ||||
1989 | #define ASSERT_NEAR(val1, val2, abs_error)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::DoubleNearPredFormat("val1" , "val2", "abs_error", val1, val2, abs_error))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1989, gtest_ar.failure_message()) = ::testing::Message() \ | |||
1990 | ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, val1, val2, \switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::DoubleNearPredFormat("val1" , "val2", "abs_error", val1, val2, abs_error))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1991, gtest_ar.failure_message()) = ::testing::Message() | |||
1991 | abs_error)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::DoubleNearPredFormat("val1" , "val2", "abs_error", val1, val2, abs_error))) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1991, gtest_ar.failure_message()) = ::testing::Message() | |||
1992 | ||||
1993 | // These predicate format functions work on floating-point values, and | |||
1994 | // can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. | |||
1995 | // | |||
1996 | // EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); | |||
1997 | ||||
1998 | // Asserts that val1 is less than, or almost equal to, val2. Fails | |||
1999 | // otherwise. In particular, it fails if either val1 or val2 is NaN. | |||
2000 | GTEST_API___attribute__((visibility("default"))) AssertionResult FloatLE(const char* expr1, const char* expr2, | |||
2001 | float val1, float val2); | |||
2002 | GTEST_API___attribute__((visibility("default"))) AssertionResult DoubleLE(const char* expr1, const char* expr2, | |||
2003 | double val1, double val2); | |||
2004 | ||||
2005 | #ifdef GTEST_OS_WINDOWS | |||
2006 | ||||
2007 | // Macros that test for HRESULT failure and success, these are only useful | |||
2008 | // on Windows, and rely on Windows SDK macros and APIs to compile. | |||
2009 | // | |||
2010 | // * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) | |||
2011 | // | |||
2012 | // When expr unexpectedly fails or succeeds, Google Test prints the | |||
2013 | // expected result and the actual result with both a human-readable | |||
2014 | // string representation of the error, if available, as well as the | |||
2015 | // hex result code. | |||
2016 | #define EXPECT_HRESULT_SUCCEEDED(expr) \ | |||
2017 | EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::IsHRESULTSuccess("(expr)", ( expr)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2017, gtest_ar.failure_message()) = ::testing::Message() | |||
2018 | ||||
2019 | #define ASSERT_HRESULT_SUCCEEDED(expr) \ | |||
2020 | ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::IsHRESULTSuccess("(expr)", ( expr)))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2020, gtest_ar.failure_message()) = ::testing::Message() | |||
2021 | ||||
2022 | #define EXPECT_HRESULT_FAILED(expr) \ | |||
2023 | EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::IsHRESULTFailure("(expr)", ( expr)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2023, gtest_ar.failure_message()) = ::testing::Message() | |||
2024 | ||||
2025 | #define ASSERT_HRESULT_FAILED(expr) \ | |||
2026 | ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::IsHRESULTFailure("(expr)", ( expr)))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2026, gtest_ar.failure_message()) = ::testing::Message() | |||
2027 | ||||
2028 | #endif // GTEST_OS_WINDOWS | |||
2029 | ||||
2030 | // Macros that execute statement and check that it doesn't generate new fatal | |||
2031 | // failures in the current thread. | |||
2032 | // | |||
2033 | // * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); | |||
2034 | // | |||
2035 | // Examples: | |||
2036 | // | |||
2037 | // EXPECT_NO_FATAL_FAILURE(Process()); | |||
2038 | // ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; | |||
2039 | // | |||
2040 | #define ASSERT_NO_FATAL_FAILURE(statement)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { const ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker ; if (::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); if (gtest_fatal_failure_checker.has_new_fatal_failure ()) { goto gtest_label_testnofatal_2040; } } else gtest_label_testnofatal_2040 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2040, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() \ | |||
2041 | GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { const ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker ; if (::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); if (gtest_fatal_failure_checker.has_new_fatal_failure ()) { goto gtest_label_testnofatal_2041; } } else gtest_label_testnofatal_2041 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2041, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() | |||
2042 | #define EXPECT_NO_FATAL_FAILURE(statement)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { const ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker ; if (::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); if (gtest_fatal_failure_checker.has_new_fatal_failure ()) { goto gtest_label_testnofatal_2042; } } else gtest_label_testnofatal_2042 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2042, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() \ | |||
2043 | GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { const ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker ; if (::testing::internal::AlwaysTrue()) { statement; } else static_assert (true, ""); if (gtest_fatal_failure_checker.has_new_fatal_failure ()) { goto gtest_label_testnofatal_2043; } } else gtest_label_testnofatal_2043 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2043, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() | |||
2044 | ||||
2045 | // Causes a trace (including the given source file path and line number, | |||
2046 | // and the given message) to be included in every test failure message generated | |||
2047 | // by code in the scope of the lifetime of an instance of this class. The effect | |||
2048 | // is undone with the destruction of the instance. | |||
2049 | // | |||
2050 | // The message argument can be anything streamable to std::ostream. | |||
2051 | // | |||
2052 | // Example: | |||
2053 | // testing::ScopedTrace trace("file.cc", 123, "message"); | |||
2054 | // | |||
2055 | class GTEST_API___attribute__((visibility("default"))) ScopedTrace { | |||
2056 | public: | |||
2057 | // The c'tor pushes the given source file location and message onto | |||
2058 | // a trace stack maintained by Google Test. | |||
2059 | ||||
2060 | // Template version. Uses Message() to convert the values into strings. | |||
2061 | // Slow, but flexible. | |||
2062 | template <typename T> | |||
2063 | ScopedTrace(const char* file, int line, const T& message) { | |||
2064 | PushTrace(file, line, (Message() << message).GetString()); | |||
2065 | } | |||
2066 | ||||
2067 | // Optimize for some known types. | |||
2068 | ScopedTrace(const char* file, int line, const char* message) { | |||
2069 | PushTrace(file, line, message ? message : "(null)"); | |||
2070 | } | |||
2071 | ||||
2072 | ScopedTrace(const char* file, int line, const std::string& message) { | |||
2073 | PushTrace(file, line, message); | |||
2074 | } | |||
2075 | ||||
2076 | // The d'tor pops the info pushed by the c'tor. | |||
2077 | // | |||
2078 | // Note that the d'tor is not virtual in order to be efficient. | |||
2079 | // Don't inherit from ScopedTrace! | |||
2080 | ~ScopedTrace(); | |||
2081 | ||||
2082 | private: | |||
2083 | void PushTrace(const char* file, int line, std::string message); | |||
2084 | ||||
2085 | ScopedTrace(const ScopedTrace&) = delete; | |||
2086 | ScopedTrace& operator=(const ScopedTrace&) = delete; | |||
2087 | }; | |||
2088 | ||||
2089 | // Causes a trace (including the source file path, the current line | |||
2090 | // number, and the given message) to be included in every test failure | |||
2091 | // message generated by code in the current scope. The effect is | |||
2092 | // undone when the control leaves the current scope. | |||
2093 | // | |||
2094 | // The message argument can be anything streamable to std::ostream. | |||
2095 | // | |||
2096 | // In the implementation, we include the current line number as part | |||
2097 | // of the dummy variable name, thus allowing multiple SCOPED_TRACE()s | |||
2098 | // to appear in the same block - as long as they are on different | |||
2099 | // lines. | |||
2100 | // | |||
2101 | // Assuming that each thread maintains its own stack of traces. | |||
2102 | // Therefore, a SCOPED_TRACE() would (correctly) only affect the | |||
2103 | // assertions in its own thread. | |||
2104 | #define SCOPED_TRACE(message)const ::testing::ScopedTrace gtest_trace_2104( "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2104, (message)) \ | |||
2105 | const ::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)gtest_trace_2105( \ | |||
2106 | __FILE__"/usr/src/googletest/googletest/include/gtest/gtest.h", __LINE__2106, (message)) | |||
2107 | ||||
2108 | // Compile-time assertion for type equality. | |||
2109 | // StaticAssertTypeEq<type1, type2>() compiles if and only if type1 and type2 | |||
2110 | // are the same type. The value it returns is not interesting. | |||
2111 | // | |||
2112 | // Instead of making StaticAssertTypeEq a class template, we make it a | |||
2113 | // function template that invokes a helper class template. This | |||
2114 | // prevents a user from misusing StaticAssertTypeEq<T1, T2> by | |||
2115 | // defining objects of that type. | |||
2116 | // | |||
2117 | // CAVEAT: | |||
2118 | // | |||
2119 | // When used inside a method of a class template, | |||
2120 | // StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is | |||
2121 | // instantiated. For example, given: | |||
2122 | // | |||
2123 | // template <typename T> class Foo { | |||
2124 | // public: | |||
2125 | // void Bar() { testing::StaticAssertTypeEq<int, T>(); } | |||
2126 | // }; | |||
2127 | // | |||
2128 | // the code: | |||
2129 | // | |||
2130 | // void Test1() { Foo<bool> foo; } | |||
2131 | // | |||
2132 | // will NOT generate a compiler error, as Foo<bool>::Bar() is never | |||
2133 | // actually instantiated. Instead, you need: | |||
2134 | // | |||
2135 | // void Test2() { Foo<bool> foo; foo.Bar(); } | |||
2136 | // | |||
2137 | // to cause a compiler error. | |||
2138 | template <typename T1, typename T2> | |||
2139 | constexpr bool StaticAssertTypeEq() noexcept { | |||
2140 | static_assert(std::is_same<T1, T2>::value, "T1 and T2 are not the same type"); | |||
2141 | return true; | |||
2142 | } | |||
2143 | ||||
2144 | // Defines a test. | |||
2145 | // | |||
2146 | // The first parameter is the name of the test suite, and the second | |||
2147 | // parameter is the name of the test within the test suite. | |||
2148 | // | |||
2149 | // The convention is to end the test suite name with "Test". For | |||
2150 | // example, a test suite for the Foo class can be named FooTest. | |||
2151 | // | |||
2152 | // Test code should appear between braces after an invocation of | |||
2153 | // this macro. Example: | |||
2154 | // | |||
2155 | // TEST(FooTest, InitializesCorrectly) { | |||
2156 | // Foo foo; | |||
2157 | // EXPECT_TRUE(foo.StatusIsOK()); | |||
2158 | // } | |||
2159 | ||||
2160 | // Note that we call GetTestTypeId() instead of GetTypeId< | |||
2161 | // ::testing::Test>() here to get the type ID of testing::Test. This | |||
2162 | // is to work around a suspected linker bug when using Google Test as | |||
2163 | // a framework on Mac OS X. The bug causes GetTypeId< | |||
2164 | // ::testing::Test>() to return different values depending on whether | |||
2165 | // the call is from the Google Test framework itself or from user test | |||
2166 | // code. GetTestTypeId() is guaranteed to always return the same | |||
2167 | // value, as it always calls GetTypeId<>() from the Google Test | |||
2168 | // framework. | |||
2169 | #define GTEST_TEST(test_suite_name, test_name)static_assert(sizeof("test_suite_name") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_suite_name_test_name_Test : public ::testing::Test { public: test_suite_name_test_name_Test() = default; ~test_suite_name_test_name_Test () override = default; test_suite_name_test_name_Test (const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test & operator =( const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test (test_suite_name_test_name_Test &&) noexcept = delete ; test_suite_name_test_name_Test & operator=( test_suite_name_test_name_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const test_suite_name_test_name_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "test_suite_name" , "test_name", nullptr, nullptr, ::testing::internal::CodeLocation ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2169 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2169 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2169), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() \ | |||
2170 | GTEST_TEST_(test_suite_name, test_name, ::testing::Test, \static_assert(sizeof("test_suite_name") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_suite_name_test_name_Test : public ::testing::Test { public: test_suite_name_test_name_Test() = default; ~test_suite_name_test_name_Test () override = default; test_suite_name_test_name_Test (const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test & operator =( const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test (test_suite_name_test_name_Test &&) noexcept = delete ; test_suite_name_test_name_Test & operator=( test_suite_name_test_name_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const test_suite_name_test_name_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "test_suite_name" , "test_name", nullptr, nullptr, ::testing::internal::CodeLocation ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2171 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2171 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2171), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() | |||
2171 | ::testing::internal::GetTestTypeId())static_assert(sizeof("test_suite_name") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_suite_name_test_name_Test : public ::testing::Test { public: test_suite_name_test_name_Test() = default; ~test_suite_name_test_name_Test () override = default; test_suite_name_test_name_Test (const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test & operator =( const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test (test_suite_name_test_name_Test &&) noexcept = delete ; test_suite_name_test_name_Test & operator=( test_suite_name_test_name_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const test_suite_name_test_name_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "test_suite_name" , "test_name", nullptr, nullptr, ::testing::internal::CodeLocation ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2171 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2171 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2171), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() | |||
2172 | ||||
2173 | // Define this macro to 1 to omit the definition of TEST(), which | |||
2174 | // is a generic name and clashes with some other libraries. | |||
2175 | #if !(defined(GTEST_DONT_DEFINE_TEST) && GTEST_DONT_DEFINE_TEST) | |||
2176 | #define TEST(test_suite_name, test_name)static_assert(sizeof("test_suite_name") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_suite_name_test_name_Test : public ::testing::Test { public: test_suite_name_test_name_Test() = default; ~test_suite_name_test_name_Test () override = default; test_suite_name_test_name_Test (const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test & operator =( const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test (test_suite_name_test_name_Test &&) noexcept = delete ; test_suite_name_test_name_Test & operator=( test_suite_name_test_name_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const test_suite_name_test_name_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "test_suite_name" , "test_name", nullptr, nullptr, ::testing::internal::CodeLocation ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2176 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2176 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2176), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() GTEST_TEST(test_suite_name, test_name)static_assert(sizeof("test_suite_name") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_suite_name_test_name_Test : public ::testing::Test { public: test_suite_name_test_name_Test() = default; ~test_suite_name_test_name_Test () override = default; test_suite_name_test_name_Test (const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test & operator =( const test_suite_name_test_name_Test &) = delete; test_suite_name_test_name_Test (test_suite_name_test_name_Test &&) noexcept = delete ; test_suite_name_test_name_Test & operator=( test_suite_name_test_name_Test &&) noexcept = delete; private: void TestBody() override ; static ::testing::TestInfo* const test_info_ __attribute__( (unused)); }; ::testing::TestInfo* const test_suite_name_test_name_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "test_suite_name" , "test_name", nullptr, nullptr, ::testing::internal::CodeLocation ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2176 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2176 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2176), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() | |||
2177 | #endif | |||
2178 | ||||
2179 | // Defines a test that uses a test fixture. | |||
2180 | // | |||
2181 | // The first parameter is the name of the test fixture class, which | |||
2182 | // also doubles as the test suite name. The second parameter is the | |||
2183 | // name of the test within the test suite. | |||
2184 | // | |||
2185 | // A test fixture class must be declared earlier. The user should put | |||
2186 | // the test code between braces after using this macro. Example: | |||
2187 | // | |||
2188 | // class FooTest : public testing::Test { | |||
2189 | // protected: | |||
2190 | // void SetUp() override { b_.AddElement(3); } | |||
2191 | // | |||
2192 | // Foo a_; | |||
2193 | // Foo b_; | |||
2194 | // }; | |||
2195 | // | |||
2196 | // TEST_F(FooTest, InitializesCorrectly) { | |||
2197 | // EXPECT_TRUE(a_.StatusIsOK()); | |||
2198 | // } | |||
2199 | // | |||
2200 | // TEST_F(FooTest, ReturnsElementCountCorrectly) { | |||
2201 | // EXPECT_EQ(a_.size(), 0); | |||
2202 | // EXPECT_EQ(b_.size(), 1); | |||
2203 | // } | |||
2204 | #define GTEST_TEST_F(test_fixture, test_name)static_assert(sizeof("test_fixture") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_fixture_test_name_Test : public test_fixture { public : test_fixture_test_name_Test() = default; ~test_fixture_test_name_Test () override = default; test_fixture_test_name_Test (const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test & operator= ( const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test (test_fixture_test_name_Test &&) noexcept = delete; test_fixture_test_name_Test & operator=( test_fixture_test_name_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const test_fixture_test_name_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "test_fixture", "test_name" , nullptr, nullptr, ::testing::internal::CodeLocation("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2204), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2204), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2204), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() \ | |||
2205 | GTEST_TEST_(test_fixture, test_name, test_fixture, \static_assert(sizeof("test_fixture") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_fixture_test_name_Test : public test_fixture { public : test_fixture_test_name_Test() = default; ~test_fixture_test_name_Test () override = default; test_fixture_test_name_Test (const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test & operator= ( const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test (test_fixture_test_name_Test &&) noexcept = delete; test_fixture_test_name_Test & operator=( test_fixture_test_name_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const test_fixture_test_name_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "test_fixture", "test_name" , nullptr, nullptr, ::testing::internal::CodeLocation("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2206), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2206), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2206), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() | |||
2206 | ::testing::internal::GetTypeId<test_fixture>())static_assert(sizeof("test_fixture") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_fixture_test_name_Test : public test_fixture { public : test_fixture_test_name_Test() = default; ~test_fixture_test_name_Test () override = default; test_fixture_test_name_Test (const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test & operator= ( const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test (test_fixture_test_name_Test &&) noexcept = delete; test_fixture_test_name_Test & operator=( test_fixture_test_name_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const test_fixture_test_name_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "test_fixture", "test_name" , nullptr, nullptr, ::testing::internal::CodeLocation("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2206), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2206), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2206), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() | |||
2207 | #if !(defined(GTEST_DONT_DEFINE_TEST_F) && GTEST_DONT_DEFINE_TEST_F) | |||
2208 | #define TEST_F(test_fixture, test_name)static_assert(sizeof("test_fixture") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_fixture_test_name_Test : public test_fixture { public : test_fixture_test_name_Test() = default; ~test_fixture_test_name_Test () override = default; test_fixture_test_name_Test (const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test & operator= ( const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test (test_fixture_test_name_Test &&) noexcept = delete; test_fixture_test_name_Test & operator=( test_fixture_test_name_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const test_fixture_test_name_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "test_fixture", "test_name" , nullptr, nullptr, ::testing::internal::CodeLocation("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2208), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2208), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2208), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() GTEST_TEST_F(test_fixture, test_name)static_assert(sizeof("test_fixture") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("test_name") > 1, "test_name must not be empty" ); class test_fixture_test_name_Test : public test_fixture { public : test_fixture_test_name_Test() = default; ~test_fixture_test_name_Test () override = default; test_fixture_test_name_Test (const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test & operator= ( const test_fixture_test_name_Test &) = delete; test_fixture_test_name_Test (test_fixture_test_name_Test &&) noexcept = delete; test_fixture_test_name_Test & operator=( test_fixture_test_name_Test &&) noexcept = delete; private: void TestBody() override; static ::testing ::TestInfo* const test_info_ __attribute__((unused)); }; ::testing ::TestInfo* const test_fixture_test_name_Test::test_info_ = :: testing::internal::MakeAndRegisterTestInfo( "test_fixture", "test_name" , nullptr, nullptr, ::testing::internal::CodeLocation("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2208), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2208), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2208), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() | |||
2209 | #endif | |||
2210 | ||||
2211 | // Returns a path to a temporary directory, which should be writable. It is | |||
2212 | // implementation-dependent whether or not the path is terminated by the | |||
2213 | // directory-separator character. | |||
2214 | GTEST_API___attribute__((visibility("default"))) std::string TempDir(); | |||
2215 | ||||
2216 | // Returns a path to a directory that contains ancillary data files that might | |||
2217 | // be used by tests. It is implementation dependent whether or not the path is | |||
2218 | // terminated by the directory-separator character. The directory and the files | |||
2219 | // in it should be considered read-only. | |||
2220 | GTEST_API___attribute__((visibility("default"))) std::string SrcDir(); | |||
2221 | ||||
2222 | GTEST_DISABLE_MSC_WARNINGS_POP_() // 4805 4100 | |||
2223 | ||||
2224 | // Dynamically registers a test with the framework. | |||
2225 | // | |||
2226 | // This is an advanced API only to be used when the `TEST` macros are | |||
2227 | // insufficient. The macros should be preferred when possible, as they avoid | |||
2228 | // most of the complexity of calling this function. | |||
2229 | // | |||
2230 | // The `factory` argument is a factory callable (move-constructible) object or | |||
2231 | // function pointer that creates a new instance of the Test object. It | |||
2232 | // handles ownership to the caller. The signature of the callable is | |||
2233 | // `Fixture*()`, where `Fixture` is the test fixture class for the test. All | |||
2234 | // tests registered with the same `test_suite_name` must return the same | |||
2235 | // fixture type. This is checked at runtime. | |||
2236 | // | |||
2237 | // The framework will infer the fixture class from the factory and will call | |||
2238 | // the `SetUpTestSuite` and `TearDownTestSuite` for it. | |||
2239 | // | |||
2240 | // Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is | |||
2241 | // undefined. | |||
2242 | // | |||
2243 | // Use case example: | |||
2244 | // | |||
2245 | // class MyFixture : public ::testing::Test { | |||
2246 | // public: | |||
2247 | // // All of these optional, just like in regular macro usage. | |||
2248 | // static void SetUpTestSuite() { ... } | |||
2249 | // static void TearDownTestSuite() { ... } | |||
2250 | // void SetUp() override { ... } | |||
2251 | // void TearDown() override { ... } | |||
2252 | // }; | |||
2253 | // | |||
2254 | // class MyTest : public MyFixture { | |||
2255 | // public: | |||
2256 | // explicit MyTest(int data) : data_(data) {} | |||
2257 | // void TestBody() override { ... } | |||
2258 | // | |||
2259 | // private: | |||
2260 | // int data_; | |||
2261 | // }; | |||
2262 | // | |||
2263 | // void RegisterMyTests(const std::vector<int>& values) { | |||
2264 | // for (int v : values) { | |||
2265 | // ::testing::RegisterTest( | |||
2266 | // "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, | |||
2267 | // std::to_string(v).c_str(), | |||
2268 | // __FILE__, __LINE__, | |||
2269 | // // Important to use the fixture type as the return type here. | |||
2270 | // [=]() -> MyFixture* { return new MyTest(v); }); | |||
2271 | // } | |||
2272 | // } | |||
2273 | // ... | |||
2274 | // int main(int argc, char** argv) { | |||
2275 | // ::testing::InitGoogleTest(&argc, argv); | |||
2276 | // std::vector<int> values_to_test = LoadValuesFromConfig(); | |||
2277 | // RegisterMyTests(values_to_test); | |||
2278 | // ... | |||
2279 | // return RUN_ALL_TESTS(); | |||
2280 | // } | |||
2281 | // | |||
2282 | template <int&... ExplicitParameterBarrier, typename Factory> | |||
2283 | TestInfo* RegisterTest(const char* test_suite_name, const char* test_name, | |||
2284 | const char* type_param, const char* value_param, | |||
2285 | const char* file, int line, Factory factory) { | |||
2286 | using TestT = typename std::remove_pointer<decltype(factory())>::type; | |||
2287 | ||||
2288 | class FactoryImpl : public internal::TestFactoryBase { | |||
2289 | public: | |||
2290 | explicit FactoryImpl(Factory f) : factory_(std::move(f)) {} | |||
2291 | Test* CreateTest() override { return factory_(); } | |||
2292 | ||||
2293 | private: | |||
2294 | Factory factory_; | |||
2295 | }; | |||
2296 | ||||
2297 | return internal::MakeAndRegisterTestInfo( | |||
2298 | test_suite_name, test_name, type_param, value_param, | |||
2299 | internal::CodeLocation(file, line), internal::GetTypeId<TestT>(), | |||
2300 | internal::SuiteApiResolver<TestT>::GetSetUpCaseOrSuite(file, line), | |||
2301 | internal::SuiteApiResolver<TestT>::GetTearDownCaseOrSuite(file, line), | |||
2302 | new FactoryImpl{std::move(factory)}); | |||
2303 | } | |||
2304 | ||||
2305 | } // namespace testing | |||
2306 | ||||
2307 | // Use this function in main() to run all tests. It returns 0 if all | |||
2308 | // tests are successful, or 1 otherwise. | |||
2309 | // | |||
2310 | // RUN_ALL_TESTS() should be invoked after the command line has been | |||
2311 | // parsed by InitGoogleTest(). | |||
2312 | // | |||
2313 | // This function was formerly a macro; thus, it is in the global | |||
2314 | // namespace and has an all-caps name. | |||
2315 | int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT___attribute__((warn_unused_result)); | |||
2316 | ||||
2317 | inline int RUN_ALL_TESTS() { return ::testing::UnitTest::GetInstance()->Run(); } | |||
2318 | ||||
2319 | GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 | |||
2320 | ||||
2321 | #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_H_ |