| File: | usr/src/googletest/googletest/include/gtest/gtest.h |
| Warning: | line 1394, column 14 An undefined value may be read from 'errno' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | // Copyright (C) 2011-2026 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/env_var_wrapper.h> | |||
| 19 | #include <testutils/gtest_utils.h> | |||
| 20 | #include <testutils/log_utils.h> | |||
| 21 | #include <boost/scoped_ptr.hpp> | |||
| 22 | #include <gtest/gtest.h> | |||
| 23 | ||||
| 24 | #include <fcntl.h> | |||
| 25 | #include <fstream> | |||
| 26 | #include <functional> | |||
| 27 | #include <iostream> | |||
| 28 | #include <sstream> | |||
| 29 | #include <thread> | |||
| 30 | ||||
| 31 | #include <arpa/inet.h> | |||
| 32 | #include <unistd.h> | |||
| 33 | ||||
| 34 | using namespace std; | |||
| 35 | using namespace isc; | |||
| 36 | using namespace isc::asiolink; | |||
| 37 | using namespace isc::dhcp; | |||
| 38 | using namespace isc::dhcp::test; | |||
| 39 | using namespace isc::test; | |||
| 40 | using boost::scoped_ptr; | |||
| 41 | namespace ph = std::placeholders; | |||
| 42 | ||||
| 43 | namespace { | |||
| 44 | ||||
| 45 | // Note this is for the *real* loopback interface, *not* the fake one. | |||
| 46 | // So in tests using it you have LOOPBACK_NAME, LOOPBACK_INDEX and | |||
| 47 | // no "eth0" nor "eth1". In tests not using it you can have "lo", LO_INDEX, | |||
| 48 | // "eth0" or "eth1". | |||
| 49 | // Name of loopback interface detection. | |||
| 50 | const size_t BUF_SIZE = 32; | |||
| 51 | // Can be overwritten to "lo0" for instance on BSD systems. | |||
| 52 | char LOOPBACK_NAME[BUF_SIZE] = "lo"; | |||
| 53 | // In fact is never 0, 1 is by far the most likely. | |||
| 54 | uint32_t LOOPBACK_INDEX = 0; | |||
| 55 | ||||
| 56 | // Ports used during testing | |||
| 57 | const uint16_t PORT1 = 10547; // V6 socket | |||
| 58 | const uint16_t PORT2 = 10548; // V4 socket | |||
| 59 | ||||
| 60 | // On some systems measured duration of receive6() and receive4() appears to be | |||
| 61 | // shorter than select() timeout. This may be the case if different time | |||
| 62 | // resolutions are used by these functions. For such cases we set the | |||
| 63 | // tolerance to 0.01s. | |||
| 64 | const uint32_t TIMEOUT_TOLERANCE = 10000; | |||
| 65 | ||||
| 66 | // Macro for making select wait time arguments for receive functions | |||
| 67 | #define RECEIVE_WAIT_MS(m)0,(m*1000) 0,(m*1000) | |||
| 68 | ||||
| 69 | /// This test verifies that the socket read buffer can be used to | |||
| 70 | /// receive the data and that the data can be read from it. | |||
| 71 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceTest_readBuffer_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceTest", "readBuffer" , nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 71), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 71), :: testing::internal::SuiteApiResolver< ::testing::Test>:: GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 71), new ::testing::internal::TestFactoryImpl<IfaceTest_readBuffer_Test >); void IfaceTest_readBuffer_Test::TestBody() { | |||
| 72 | // Create fake interface object. | |||
| 73 | Iface iface("em0", 0); | |||
| 74 | // The size of read buffer should initially be 0 and the returned | |||
| 75 | // pointer should be NULL. | |||
| 76 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 76, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 77 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 77, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 78 | ||||
| 79 | // Let's resize the buffer. | |||
| 80 | iface.resizeReadBuffer(256); | |||
| 81 | // Check that the buffer has expected size. | |||
| 82 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 82, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 83 | // The returned pointer should now be non-NULL. | |||
| 84 | uint8_t* buf_ptr = iface.getReadBuffer(); | |||
| 85 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 85, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "buf_ptr == __null", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 86 | ||||
| 87 | // Use the pointer to set some data. | |||
| 88 | for (size_t i = 0; i < iface.getReadBufferSize(); ++i) { | |||
| 89 | buf_ptr[i] = i; | |||
| 90 | } | |||
| 91 | ||||
| 92 | // Get the pointer again and validate the data. | |||
| 93 | buf_ptr = iface.getReadBuffer(); | |||
| 94 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 94, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 95 | for (size_t i = 0; i < iface.getReadBufferSize(); ++i) { | |||
| 96 | // Use assert so as it fails on the first failure, no need | |||
| 97 | // to continue further checks. | |||
| 98 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 98, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 99 | } | |||
| 100 | } | |||
| 101 | ||||
| 102 | // Check that counting the number of active addresses on the interface | |||
| 103 | // works as expected. | |||
| 104 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceTest_countActive4_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceTest", "countActive4" , nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 104), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 104), :: testing::internal::SuiteApiResolver< ::testing::Test>:: GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 104), new ::testing::internal::TestFactoryImpl<IfaceTest_countActive4_Test >); void IfaceTest_countActive4_Test::TestBody() { | |||
| 105 | Iface iface("eth0", 0); | |||
| 106 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 106, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 107 | ||||
| 108 | iface.addAddress(IOAddress("192.168.0.2")); | |||
| 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 109, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 110 | ||||
| 111 | iface.addAddress(IOAddress("2001:db8:1::1")); | |||
| 112 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 112, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 113 | ||||
| 114 | iface.addAddress(IOAddress("192.168.0.3")); | |||
| 115 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 115, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 116 | ||||
| 117 | 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_117; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_117; } } else gtest_label_testnothrow_117 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 117, ("Expected: " "iface.setActive(IOAddress(\"192.168.0.2\"), false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 118 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 118, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 119 | ||||
| 120 | 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_120; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_120; } } else gtest_label_testnothrow_120 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 120, ("Expected: " "iface.setActive(IOAddress(\"192.168.0.3\"), false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 121 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 121, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 122 | } | |||
| 123 | ||||
| 124 | /// Mock object implementing PktFilter class. It is used by | |||
| 125 | /// IfaceMgrTest::setPacketFilter to verify that IfaceMgr::setPacketFilter | |||
| 126 | /// sets this object as a handler for opening sockets. This dummy | |||
| 127 | /// class simply records that openSocket function was called by | |||
| 128 | /// the IfaceMgr as expected. | |||
| 129 | /// | |||
| 130 | /// @todo This class currently doesn't verify that send/receive functions | |||
| 131 | /// were called. In order to test it, there is a need to supply dummy | |||
| 132 | /// function performing select() on certain sockets. The system select() | |||
| 133 | /// call will fail when dummy socket descriptor is provided and thus | |||
| 134 | /// TestPktFilter::receive will never be called. The appropriate extension | |||
| 135 | /// to IfaceMgr is planned along with implementation of other "Packet | |||
| 136 | /// Filters" such as these supporting Linux Packet Filtering and | |||
| 137 | /// Berkeley Packet Filtering. | |||
| 138 | class TestPktFilter : public PktFilter { | |||
| 139 | public: | |||
| 140 | ||||
| 141 | /// Constructor | |||
| 142 | TestPktFilter() | |||
| 143 | : open_socket_called_(false) { | |||
| 144 | } | |||
| 145 | ||||
| 146 | virtual bool isDirectResponseSupported() const { | |||
| 147 | return (false); | |||
| 148 | } | |||
| 149 | ||||
| 150 | virtual bool isSocketReceivedTimeSupported() const { | |||
| 151 | return (false); | |||
| 152 | } | |||
| 153 | ||||
| 154 | /// @brief Pretend to open a socket. | |||
| 155 | /// | |||
| 156 | /// This function doesn't open a real socket. It always returns the | |||
| 157 | /// same fake socket descriptor. It also records the fact that it has | |||
| 158 | /// been called in the public open_socket_called_ member. | |||
| 159 | /// As in the case of opening a real socket, this function will check | |||
| 160 | /// if there is another fake socket "bound" to the same address and port. | |||
| 161 | /// If there is, it will throw an exception. This allows to simulate the | |||
| 162 | /// conditions when one of the sockets can't be open because there is | |||
| 163 | /// a socket already open and test how IfaceMgr will handle it. | |||
| 164 | /// | |||
| 165 | /// @param iface An interface on which the socket is to be opened. | |||
| 166 | /// @param addr An address to which the socket is to be bound. | |||
| 167 | /// @param port A port to which the socket is to be bound. | |||
| 168 | virtual SocketInfo openSocket(Iface& iface, | |||
| 169 | const isc::asiolink::IOAddress& addr, | |||
| 170 | const uint16_t port, | |||
| 171 | const bool join_multicast, | |||
| 172 | const bool) { | |||
| 173 | // Check if there is any other socket bound to the specified address | |||
| 174 | // and port on this interface. | |||
| 175 | const Iface::SocketCollection& sockets = iface.getSockets(); | |||
| 176 | for (auto const& socket : sockets) { | |||
| 177 | if (((socket.addr_ == addr) || | |||
| 178 | ((socket.addr_ == IOAddress("::")) && join_multicast)) && | |||
| 179 | socket.port_ == port) { | |||
| 180 | isc_throw(SocketConfigError, "test socket bind error")do { std::ostringstream oss__; oss__ << "test socket bind error" ; throw SocketConfigError("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 180, oss__.str().c_str()); } while (1); | |||
| 181 | } | |||
| 182 | } | |||
| 183 | open_socket_called_ = true; | |||
| 184 | return (SocketInfo(addr, port, 255)); | |||
| 185 | } | |||
| 186 | ||||
| 187 | /// Does nothing | |||
| 188 | virtual Pkt4Ptr receive(Iface&, const SocketInfo&) { | |||
| 189 | return (Pkt4Ptr()); | |||
| 190 | } | |||
| 191 | ||||
| 192 | /// Does nothing | |||
| 193 | virtual int send(const Iface&, uint16_t, const Pkt4Ptr&) { | |||
| 194 | return (0); | |||
| 195 | } | |||
| 196 | ||||
| 197 | /// Holds the information whether openSocket was called on this | |||
| 198 | /// object after its creation. | |||
| 199 | bool open_socket_called_; | |||
| 200 | }; | |||
| 201 | ||||
| 202 | class NakedIfaceMgr: public IfaceMgr { | |||
| 203 | // "Naked" Interface Manager, exposes internal fields | |||
| 204 | public: | |||
| 205 | ||||
| 206 | /// @brief Constructor. | |||
| 207 | NakedIfaceMgr() { | |||
| 208 | loDetect(); | |||
| 209 | } | |||
| 210 | ||||
| 211 | /// @brief detects name of the loopback interface | |||
| 212 | /// | |||
| 213 | /// This method detects name of the loopback interface. | |||
| 214 | static void loDetect() { | |||
| 215 | // Poor man's interface detection. It will go away as soon as proper | |||
| 216 | // interface detection is implemented | |||
| 217 | if (if_nametoindex("lo") > 0) { | |||
| 218 | snprintf(LOOPBACK_NAME, BUF_SIZE - 1, "lo"); | |||
| 219 | } else if (if_nametoindex("lo0") > 0) { | |||
| 220 | snprintf(LOOPBACK_NAME, BUF_SIZE - 1, "lo0"); | |||
| 221 | } else { | |||
| 222 | cout << "Failed to detect loopback interface. Neither " | |||
| 223 | << "lo nor lo0 worked. I give up." << endl; | |||
| 224 | FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 224, "Failed") = ::testing::Message(); | |||
| 225 | } | |||
| 226 | LOOPBACK_INDEX = if_nametoindex(LOOPBACK_NAME); | |||
| 227 | } | |||
| 228 | ||||
| 229 | /// @brief Returns the collection of existing interfaces. | |||
| 230 | IfaceCollection& getIfacesLst() { return (ifaces_); } | |||
| 231 | ||||
| 232 | /// @brief This function creates fictitious interfaces with fictitious | |||
| 233 | /// addresses. | |||
| 234 | /// | |||
| 235 | /// These interfaces can be used in tests that don't actually try | |||
| 236 | /// to open the sockets on these interfaces. Some tests use mock | |||
| 237 | /// objects to mimic sockets being open. These interfaces are | |||
| 238 | /// suitable for such tests. | |||
| 239 | void createIfaces() { | |||
| 240 | ||||
| 241 | ifaces_.clear(); | |||
| 242 | ||||
| 243 | // local loopback | |||
| 244 | IfacePtr lo = createIface("lo", LO_INDEX); | |||
| 245 | lo->addAddress(IOAddress("127.0.0.1")); | |||
| 246 | lo->addAddress(IOAddress("::1")); | |||
| 247 | ifaces_.push_back(lo); | |||
| 248 | // eth0 | |||
| 249 | IfacePtr eth0 = createIface("eth0", ETH0_INDEX); | |||
| 250 | eth0->addAddress(IOAddress("10.0.0.1")); | |||
| 251 | eth0->addAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")); | |||
| 252 | eth0->addAddress(IOAddress("2001:db8:1::1")); | |||
| 253 | ifaces_.push_back(eth0); | |||
| 254 | // eth1 | |||
| 255 | IfacePtr eth1 = createIface("eth1", ETH1_INDEX); | |||
| 256 | eth1->addAddress(IOAddress("192.0.2.3")); | |||
| 257 | eth1->addAddress(IOAddress("fe80::3a60:77ff:fed5:abcd")); | |||
| 258 | ifaces_.push_back(eth1); | |||
| 259 | } | |||
| 260 | ||||
| 261 | /// @brief Create an object representing interface. | |||
| 262 | /// | |||
| 263 | /// Apart from creating an interface, this function also sets the | |||
| 264 | /// interface flags: | |||
| 265 | /// - loopback flag if interface name is "lo" | |||
| 266 | /// - up always true | |||
| 267 | /// - running always true | |||
| 268 | /// - inactive always to false | |||
| 269 | /// - multicast always to true | |||
| 270 | /// - broadcast always to false | |||
| 271 | /// | |||
| 272 | /// If one needs to modify the default flag settings, the setIfaceFlags | |||
| 273 | /// function should be used. | |||
| 274 | /// | |||
| 275 | /// @param name A name of the interface to be created. | |||
| 276 | /// @param ifindex An index of the interface to be created. | |||
| 277 | /// | |||
| 278 | /// @return An object representing interface. | |||
| 279 | static IfacePtr createIface(const std::string& name, const unsigned int ifindex) { | |||
| 280 | IfacePtr iface(new Iface(name, ifindex)); | |||
| 281 | if (name == "lo") { | |||
| 282 | iface->flag_loopback_ = true; | |||
| 283 | // Don't open sockets on loopback interface. | |||
| 284 | iface->inactive4_ = true; | |||
| 285 | iface->inactive6_ = true; | |||
| 286 | } else { | |||
| 287 | iface->inactive4_ = false; | |||
| 288 | iface->inactive6_ = false; | |||
| 289 | } | |||
| 290 | iface->flag_multicast_ = true; | |||
| 291 | // On BSD systems, the SO_BINDTODEVICE option is not supported. | |||
| 292 | // Therefore the IfaceMgr will throw an exception on attempt to | |||
| 293 | // open sockets on more than one broadcast-capable interface at | |||
| 294 | // the same time. In order to prevent this error, we mark all | |||
| 295 | // interfaces broadcast-incapable for unit testing. | |||
| 296 | iface->flag_broadcast_ = false; | |||
| 297 | iface->flag_up_ = true; | |||
| 298 | iface->flag_running_ = true; | |||
| 299 | return (iface); | |||
| 300 | } | |||
| 301 | ||||
| 302 | /// @brief Checks if the specified interface has a socket bound to a | |||
| 303 | /// specified address. | |||
| 304 | /// | |||
| 305 | /// @param iface_name A name of the interface. | |||
| 306 | /// @param addr An address to be checked for binding. | |||
| 307 | /// | |||
| 308 | /// @return true if there is a socket bound to the specified address. | |||
| 309 | bool isBound(const std::string& iface_name, const std::string& addr) { | |||
| 310 | IfacePtr iface = getIface(iface_name); | |||
| 311 | if (!iface) { | |||
| 312 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 312, "Failed") = ::testing::Message() << "the interface " << iface_name << " doesn't exist"; | |||
| 313 | return (false); | |||
| 314 | } | |||
| 315 | const Iface::SocketCollection& sockets = iface->getSockets(); | |||
| 316 | for (auto const& sock : sockets) { | |||
| 317 | if (sock.addr_ == IOAddress(addr)) { | |||
| 318 | return (true); | |||
| 319 | ||||
| 320 | } else if ((sock.addr_ == IOAddress("::")) && | |||
| 321 | (IOAddress(addr).isV6LinkLocal())) { | |||
| 322 | for (auto const& a : iface->getAddresses()) { | |||
| 323 | if (a.get() == IOAddress(addr)) { | |||
| 324 | return (true); | |||
| 325 | } | |||
| 326 | } | |||
| 327 | } | |||
| 328 | } | |||
| 329 | return (false); | |||
| 330 | } | |||
| 331 | ||||
| 332 | /// @brief Modify flags on the interface. | |||
| 333 | /// | |||
| 334 | /// @param name A name of the interface. | |||
| 335 | /// @param loopback A new value of the loopback flag. | |||
| 336 | /// @param up A new value of the up flag. | |||
| 337 | /// @param running A new value of the running flag. | |||
| 338 | /// @param inactive A new value of the inactive flag. | |||
| 339 | void setIfaceFlags(const std::string& name, const bool loopback, | |||
| 340 | const bool up, const bool running, | |||
| 341 | const bool inactive4, | |||
| 342 | const bool inactive6) { | |||
| 343 | for (auto const& iface : ifaces_) { | |||
| 344 | if (iface->getName() == name) { | |||
| 345 | iface->flag_loopback_ = loopback; | |||
| 346 | iface->flag_up_ = up; | |||
| 347 | iface->flag_running_ = running; | |||
| 348 | iface->inactive4_ = inactive4; | |||
| 349 | iface->inactive6_ = inactive6; | |||
| 350 | } | |||
| 351 | } | |||
| 352 | } | |||
| 353 | }; | |||
| 354 | ||||
| 355 | volatile bool callback_ok; | |||
| 356 | volatile bool callback2_ok; | |||
| 357 | ||||
| 358 | void my_callback(int /* fd */) { | |||
| 359 | callback_ok = true; | |||
| 360 | } | |||
| 361 | ||||
| 362 | void my_callback2(int /* fd */) { | |||
| 363 | callback2_ok = true; | |||
| 364 | } | |||
| 365 | ||||
| 366 | /// @brief A test fixture class for IfaceMgr. | |||
| 367 | /// | |||
| 368 | /// @todo Sockets being opened by IfaceMgr tests should be managed by | |||
| 369 | /// the test fixture. In particular, the class should close sockets after | |||
| 370 | /// each test. Current approach where test cases are responsible for | |||
| 371 | /// closing sockets is resource leak prone, especially in case of the | |||
| 372 | /// test failure path. | |||
| 373 | class IfaceMgrTest : public ::testing::Test { | |||
| 374 | public: | |||
| 375 | /// @brief Constructor. | |||
| 376 | IfaceMgrTest() | |||
| 377 | : errors_count_(0), kea_event_handler_type_("KEA_EVENT_HANDLER_TYPE") { | |||
| 378 | } | |||
| 379 | ||||
| 380 | ~IfaceMgrTest() { | |||
| 381 | } | |||
| 382 | ||||
| 383 | /// @brief Tests the number of IPv6 sockets on interface | |||
| 384 | /// | |||
| 385 | /// This function checks the expected number of open IPv6 sockets on the | |||
| 386 | /// specified interface. On non-Linux systems, sockets are bound to a | |||
| 387 | /// link-local address and the number of unicast addresses specified. | |||
| 388 | /// On Linux systems, there is one more socket bound to a ff02::1:2 | |||
| 389 | /// multicast address. | |||
| 390 | /// | |||
| 391 | /// @param iface An interface on which sockets are open. | |||
| 392 | /// @param unicast_num A number of unicast addresses bound. | |||
| 393 | /// @param link_local_num A number of link local addresses bound. | |||
| 394 | void checkSocketsCount6(const Iface& iface, const int unicast_num, | |||
| 395 | const int link_local_num = 1) { | |||
| 396 | // On local-loopback interface, there should be no sockets. | |||
| 397 | if (iface.flag_loopback_) { | |||
| 398 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 398, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface.getSockets().empty()", "false", "true") .c_str()) = :: testing::Message() | |||
| 399 | << "expected empty socket set on loopback interface " | |||
| 400 | << iface.getName(); | |||
| 401 | return; | |||
| 402 | } | |||
| 403 | #if defined OS_LINUX | |||
| 404 | // On Linux, for each link-local address there may be an | |||
| 405 | // additional socket opened and bound to ff02::1:2. This socket | |||
| 406 | // is only opened if the interface is multicast-capable. | |||
| 407 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 408, gtest_ar.failure_message()) = ::testing::Message() | |||
| 408 | + 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 408, gtest_ar.failure_message()) = ::testing::Message() | |||
| 409 | << "invalid number of sockets on interface " | |||
| 410 | << iface.getName(); | |||
| 411 | #else | |||
| 412 | // On non-Linux, there is no additional socket. | |||
| 413 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 413, gtest_ar.failure_message()) = ::testing::Message() | |||
| 414 | << "invalid number of sockets on interface " | |||
| 415 | << iface.getName(); | |||
| 416 | ||||
| 417 | #endif | |||
| 418 | } | |||
| 419 | ||||
| 420 | // Get the number of IPv4 or IPv6 sockets on the loopback interface | |||
| 421 | int getOpenSocketsCount(const Iface& iface, uint16_t family) const { | |||
| 422 | // Get all sockets. | |||
| 423 | Iface::SocketCollection sockets = iface.getSockets(); | |||
| 424 | ||||
| 425 | // Loop through sockets and try to find the ones which match the | |||
| 426 | // specified type. | |||
| 427 | int sockets_count = 0; | |||
| 428 | for (auto const& sock : sockets) { | |||
| 429 | // Match found, increase the counter. | |||
| 430 | if (sock.family_ == family) { | |||
| 431 | ++sockets_count; | |||
| 432 | } | |||
| 433 | } | |||
| 434 | return (sockets_count); | |||
| 435 | } | |||
| 436 | ||||
| 437 | /// @brief returns socket bound to a specific address (or NULL) | |||
| 438 | /// | |||
| 439 | /// A helper function, used to pick a socketinfo that is bound to a given | |||
| 440 | /// address. | |||
| 441 | /// | |||
| 442 | /// @param sockets sockets collection | |||
| 443 | /// @param addr address the socket is bound to | |||
| 444 | /// | |||
| 445 | /// @return socket info structure (or NULL) | |||
| 446 | const isc::dhcp::SocketInfo* | |||
| 447 | getSocketByAddr(const isc::dhcp::Iface::SocketCollection& sockets, | |||
| 448 | const IOAddress& addr) { | |||
| 449 | for (auto const& s : sockets) { | |||
| 450 | if (s.addr_ == addr) { | |||
| 451 | return (&s); | |||
| 452 | } | |||
| 453 | } | |||
| 454 | return (NULL__null); | |||
| 455 | } | |||
| 456 | ||||
| 457 | /// @brief Implements an IfaceMgr error handler. | |||
| 458 | /// | |||
| 459 | /// This function can be installed as an error handler for the | |||
| 460 | /// IfaceMgr::openSockets4 function. The error handler is invoked | |||
| 461 | /// when an attempt to open a particular socket fails for any reason. | |||
| 462 | /// Typically, the error handler will log a warning. When the error | |||
| 463 | /// handler returns, the openSockets4 function should continue opening | |||
| 464 | /// sockets on other interfaces. | |||
| 465 | /// | |||
| 466 | /// @param errmsg An error string indicating the reason for failure. | |||
| 467 | void ifaceMgrErrorHandler(const std::string&) { | |||
| 468 | // Increase the counter of invocations to this function. By checking | |||
| 469 | // this number, a test may check if the expected number of errors | |||
| 470 | // has occurred. | |||
| 471 | ++errors_count_; | |||
| 472 | } | |||
| 473 | ||||
| 474 | /// @brief Tests the ability to send and receive DHCPv6 packets | |||
| 475 | /// | |||
| 476 | /// This test calls @r IfaceMgr::configureDHCPPacketQueue, passing in the | |||
| 477 | /// given queue configuration. It then calls IfaceMgr::startDHCPReceiver | |||
| 478 | /// and verifies whether or not the receive thread has been started as | |||
| 479 | /// expected. Next it creates a generic DHCPv6 packet and sends it over | |||
| 480 | /// the loop back interface. It invokes IfaceMgr::receive6 to receive the | |||
| 481 | /// packet sent, and compares to the packets for equality. | |||
| 482 | /// | |||
| 483 | /// @param dhcp_queue_control dhcp-queue-control contents to use for the test | |||
| 484 | /// @param exp_queue_enabled flag that indicates if packet queuing is expected | |||
| 485 | /// to be enabled. | |||
| 486 | void sendReceive6Test(data::ConstElementPtr dhcp_queue_control, bool exp_queue_enabled) { | |||
| 487 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 488 | ||||
| 489 | // Testing socket operation in a portable way is tricky | |||
| 490 | // without interface detection implemented | |||
| 491 | // let's assume that every supported OS have lo interface | |||
| 492 | IOAddress lo_addr("::1"); | |||
| 493 | int socket1 = 0, socket2 = 0; | |||
| 494 | 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_497; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_497 ; } } else gtest_label_testnothrow_497 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 497, ("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() | |||
| 495 | 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_497; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_497 ; } } else gtest_label_testnothrow_497 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 497, ("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() | |||
| 496 | 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_497; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_497 ; } } else gtest_label_testnothrow_497 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 497, ("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() | |||
| 497 | )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_497; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_497 ; } } else gtest_label_testnothrow_497 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 497, ("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(); | |||
| 498 | ||||
| 499 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 499, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 500 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 500, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 501 | ||||
| 502 | // Configure packet queueing as desired. | |||
| 503 | bool queue_enabled = false; | |||
| 504 | 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_504; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_504 ; } } else gtest_label_testnothrow_504 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 504, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, dhcp_queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 505 | ||||
| 506 | // Verify that we have a queue only if we expected one. | |||
| 507 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 507, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 508 | ||||
| 509 | // Thread should only start when there is a packet queue. | |||
| 510 | 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_510 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_510 ; } } else gtest_label_testnothrow_510 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 510, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 511 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 511, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled == ifacemgr->isDHCPReceiverRunning()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 512 | ||||
| 513 | // If the thread is already running, trying to start it again should fail. | |||
| 514 | if (queue_enabled) { | |||
| 515 | 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_515; } 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_515; } 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_515; } } else gtest_label_testthrow_515 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 515, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 516 | // Should still have one running. | |||
| 517 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 517, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 518 | } | |||
| 519 | ||||
| 520 | // Let's build our DHCPv6 packet. | |||
| 521 | // prepare dummy payload | |||
| 522 | uint8_t data[128]; | |||
| 523 | for (uint8_t i = 0; i < 128; i++) { | |||
| 524 | data[i] = i; | |||
| 525 | } | |||
| 526 | ||||
| 527 | Pkt6Ptr sendPkt = Pkt6Ptr(new Pkt6(data, 128)); | |||
| 528 | sendPkt->repack(); | |||
| 529 | sendPkt->setRemotePort(10547); | |||
| 530 | sendPkt->setRemoteAddr(IOAddress("::1")); | |||
| 531 | sendPkt->setIndex(LOOPBACK_INDEX); | |||
| 532 | sendPkt->setIface(LOOPBACK_NAME); | |||
| 533 | ||||
| 534 | // Send the packet. | |||
| 535 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 535, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 536 | ||||
| 537 | // Now, let's try and receive it. | |||
| 538 | Pkt6Ptr rcvPkt; | |||
| 539 | rcvPkt = ifacemgr->receive6(10); | |||
| 540 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 540, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "rcvPkt", "false", "true") .c_str()) = ::testing::Message(); // received our own packet | |||
| 541 | ||||
| 542 | // let's check that we received what was sent | |||
| 543 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 543, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 544 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 545, gtest_ar.failure_message()) = ::testing::Message() | |||
| 545 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 545, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 546 | ||||
| 547 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 547, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 548 | ||||
| 549 | // since we opened 2 sockets on the same interface and none of them is multicast, | |||
| 550 | // none is preferred over the other for sending data, so we really should not | |||
| 551 | // assume the one or the other will always be chosen for sending data. Therefore | |||
| 552 | // we should accept both values as source ports. | |||
| 553 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 553, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "(rcvPkt->getRemotePort() == 10546) || (rcvPkt->getRemotePort() == 10547)" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 554 | ||||
| 555 | // Close the socket. Further we will test if errors are reported | |||
| 556 | // properly on attempt to use closed socket. | |||
| 557 | close(socket2); | |||
| 558 | ||||
| 559 | // @todo Closing the socket does NOT cause a read error out of the | |||
| 560 | // receiveDHCP<X>Packets() select. Apparently this is because the | |||
| 561 | // thread is already inside the select when the socket is closed, | |||
| 562 | // and (at least under Centos 7.5), this does not interrupt the | |||
| 563 | // select. For now, we'll only test this for direct receive. | |||
| 564 | if (!queue_enabled) { | |||
| 565 | EXPECT_THROW(ifacemgr->receive6(10), SocketFDError)switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { bool gtest_caught_expected = false; try { if ( ::testing::internal::AlwaysTrue()) { ifacemgr->receive6(10 ); } else static_assert(true, ""); } catch (SocketFDError const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketFDError>::type>::type, std::exception>::value, const ::testing::internal::NeverThrown &, const std::exception&>::type e) { gtest_msg.value = "Expected: " "ifacemgr->receive6(10)" " throws an exception of type " "SocketFDError" ".\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_565; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->receive6(10)" " throws an exception of type " "SocketFDError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_565; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->receive6(10)" " throws an exception of type " "SocketFDError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_565; } } else gtest_label_testthrow_565 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 565, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 566 | } | |||
| 567 | ||||
| 568 | // Verify write fails. | |||
| 569 | 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_569; } 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_569; } 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_569; } } else gtest_label_testthrow_569 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 569, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 570 | ||||
| 571 | // Stop the thread. This should be no harm/no foul if we're not | |||
| 572 | // queueuing. Either way, we should not have a thread afterwards. | |||
| 573 | 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_573 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_573 ; } } else gtest_label_testnothrow_573 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 573, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 574 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 574, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 575 | } | |||
| 576 | ||||
| 577 | /// @brief Tests the ability to send and receive DHCPv4 packets | |||
| 578 | /// | |||
| 579 | /// This test calls @r IfaceMgr::configureDHCPPacketQueue, passing in the | |||
| 580 | /// given queue configuration. It then calls IfaceMgr::startDHCPReceiver | |||
| 581 | /// and verifies whether or not the receive thread has been started as | |||
| 582 | /// expected. Next it creates a DISCOVER packet and sends it over | |||
| 583 | /// the loop back interface. It invokes IfaceMgr::receive4 to receive the | |||
| 584 | /// packet sent, and compares to the packets for equality. | |||
| 585 | /// | |||
| 586 | /// @param dhcp_queue_control dhcp-queue-control contents to use for the test | |||
| 587 | /// @param exp_queue_enabled flag that indicates if packet queuing is expected | |||
| 588 | /// to be enabled. | |||
| 589 | void sendReceive4Test(data::ConstElementPtr dhcp_queue_control, bool exp_queue_enabled) { | |||
| 590 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 591 | ||||
| 592 | // Testing socket operation in a portable way is tricky | |||
| 593 | // without interface detection implemented. | |||
| 594 | // Let's assume that every supported OS has lo interface | |||
| 595 | IOAddress lo_addr("127.0.0.1"); | |||
| 596 | int socket1 = 0; | |||
| 597 | 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_600; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_600 ; } } else gtest_label_testnothrow_600 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 600, ("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() | |||
| 598 | 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_600; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_600 ; } } else gtest_label_testnothrow_600 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 600, ("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() | |||
| 599 | 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_600; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_600 ; } } else gtest_label_testnothrow_600 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 600, ("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() | |||
| 600 | )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_600; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_600 ; } } else gtest_label_testnothrow_600 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 600, ("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(); | |||
| 601 | ||||
| 602 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 602, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 603 | ||||
| 604 | // Configure packet queueing as desired. | |||
| 605 | bool queue_enabled = false; | |||
| 606 | 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_606; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_606 ; } } else gtest_label_testnothrow_606 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 606, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, dhcp_queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 607 | ||||
| 608 | // Verify that we have a queue only if we expected one. | |||
| 609 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 609, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 610 | ||||
| 611 | // Thread should only start when there is a packet queue. | |||
| 612 | 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_612 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_612 ; } } else gtest_label_testnothrow_612 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 612, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 613 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 613, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled == ifacemgr->isDHCPReceiverRunning()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 614 | ||||
| 615 | // If the thread is already running, trying to start it again should fail. | |||
| 616 | if (queue_enabled) { | |||
| 617 | 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_617; } 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_617; } 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_617; } } else gtest_label_testthrow_617 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 617, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 618 | // Should still have one running. | |||
| 619 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 619, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 620 | } | |||
| 621 | ||||
| 622 | // Let's construct the packet to send. | |||
| 623 | boost::shared_ptr<Pkt4> sendPkt(new Pkt4(DHCPDISCOVER, 1234) ); | |||
| 624 | sendPkt->setLocalAddr(IOAddress("127.0.0.1")); | |||
| 625 | sendPkt->setLocalPort(DHCP4_SERVER_PORT + 10000 + 1); | |||
| 626 | sendPkt->setRemotePort(DHCP4_SERVER_PORT + 10000); | |||
| 627 | sendPkt->setRemoteAddr(IOAddress("127.0.0.1")); | |||
| 628 | sendPkt->setIndex(LOOPBACK_INDEX); | |||
| 629 | sendPkt->setIface(string(LOOPBACK_NAME)); | |||
| 630 | sendPkt->setHops(6); | |||
| 631 | sendPkt->setSecs(42); | |||
| 632 | sendPkt->setCiaddr(IOAddress("192.0.2.1")); | |||
| 633 | sendPkt->setSiaddr(IOAddress("192.0.2.2")); | |||
| 634 | sendPkt->setYiaddr(IOAddress("192.0.2.3")); | |||
| 635 | sendPkt->setGiaddr(IOAddress("192.0.2.4")); | |||
| 636 | ||||
| 637 | // Unpack() now checks if mandatory DHCP_MESSAGE_TYPE is present. | |||
| 638 | // Workarounds (creating DHCP Message Type Option by hand) are no longer | |||
| 639 | // needed as setDhcpType() is called in constructor. | |||
| 640 | ||||
| 641 | uint8_t sname[] = "That's just a string that will act as SNAME"; | |||
| 642 | sendPkt->setSname(sname, strlen((const char*)sname)); | |||
| 643 | uint8_t file[] = "/another/string/that/acts/as/a/file_name.txt"; | |||
| 644 | sendPkt->setFile(file, strlen((const char*)file)); | |||
| 645 | ||||
| 646 | 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_648 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_648 ; } } else gtest_label_testnothrow_648 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 648, ("Expected: " "sendPkt->pack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
| 647 | 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_648 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_648 ; } } else gtest_label_testnothrow_648 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 648, ("Expected: " "sendPkt->pack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
| 648 | )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_648 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_648 ; } } else gtest_label_testnothrow_648 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 648, ("Expected: " "sendPkt->pack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 649 | ||||
| 650 | // OK, Send the PACKET! | |||
| 651 | bool result = false; | |||
| 652 | 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_652 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_652 ; } } else gtest_label_testnothrow_652 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 652, ("Expected: " "result = ifacemgr->send(sendPkt)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 653 | EXPECT_TRUE(result)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(result)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 653, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "result", "false", "true") .c_str()) = ::testing::Message(); | |||
| 654 | ||||
| 655 | // Now let's try and receive it. | |||
| 656 | boost::shared_ptr<Pkt4> rcvPkt; | |||
| 657 | 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_657 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_657 ; } } else gtest_label_testnothrow_657 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 657, ("Expected: " "rcvPkt = ifacemgr->receive4(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 658 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 658, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "rcvPkt", "false", "true") .c_str()) = ::testing::Message(); // received our own packet | |||
| 659 | 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_661 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_661 ; } } else gtest_label_testnothrow_661 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 661, ("Expected: " "rcvPkt->unpack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
| 660 | 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_661 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_661 ; } } else gtest_label_testnothrow_661 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 661, ("Expected: " "rcvPkt->unpack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
| 661 | )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_661 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_661 ; } } else gtest_label_testnothrow_661 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 661, ("Expected: " "rcvPkt->unpack();" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 662 | ||||
| 663 | // let's check that we received what was sent | |||
| 664 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 664, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 665 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 665, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 666 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 666, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 667 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 667, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 668 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 668, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 669 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 669, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 670 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 670, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 671 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 671, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 672 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 672, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 673 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 673, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 674 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 674, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 675 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 675, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 676 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 676, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "sendPkt->getSname() == rcvPkt->getSname()", "false", "true") .c_str()) = ::testing::Message(); | |||
| 677 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 677, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "sendPkt->getFile() == rcvPkt->getFile()", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 678 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 678, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 679 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 679, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 680 | ||||
| 681 | // since we opened 2 sockets on the same interface and none of them is multicast, | |||
| 682 | // none is preferred over the other for sending data, so we really should not | |||
| 683 | // assume the one or the other will always be chosen for sending data. We should | |||
| 684 | // skip checking source port of sent address. | |||
| 685 | ||||
| 686 | // Close the socket. Further we will test if errors are reported | |||
| 687 | // properly on attempt to use closed socket. | |||
| 688 | close(socket1); | |||
| 689 | ||||
| 690 | // @todo Closing the socket does NOT cause a read error out of the | |||
| 691 | // receiveDHCP<X>Packets() select. Apparently this is because the | |||
| 692 | // thread is already inside the select when the socket is closed, | |||
| 693 | // and (at least under Centos 7.5), this does not interrupt the | |||
| 694 | // select. For now, we'll only test this for direct receive. | |||
| 695 | if (!queue_enabled) { | |||
| 696 | EXPECT_THROW(ifacemgr->receive4(10), SocketFDError)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 (SocketFDError const &) { gtest_caught_expected = true; } catch (typename std:: conditional< std::is_same<typename std::remove_cv<typename std::remove_reference< SocketFDError>::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 " "SocketFDError" ".\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_696; } catch (... ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(10)" " throws an exception of type " "SocketFDError" ".\n Actual: it throws a different type." ; goto gtest_label_testthrow_696; } if (!gtest_caught_expected ) { gtest_msg.value = "Expected: " "ifacemgr->receive4(10)" " throws an exception of type " "SocketFDError" ".\n Actual: it throws nothing." ; goto gtest_label_testthrow_696; } } else gtest_label_testthrow_696 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 696, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 697 | } | |||
| 698 | ||||
| 699 | // Verify write fails. | |||
| 700 | 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_700; } 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_700; } 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_700; } } else gtest_label_testthrow_700 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 700, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 701 | ||||
| 702 | // Stop the thread. This should be no harm/no foul if we're not | |||
| 703 | // queueuing. Either way, we should not have a thread afterwards. | |||
| 704 | 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_704 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_704 ; } } else gtest_label_testnothrow_704 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 704, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 705 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 705, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 706 | } | |||
| 707 | ||||
| 708 | /// @brief Verifies that IfaceMgr DHCPv4 receive calls detect and | |||
| 709 | /// ignore external sockets that have gone bad without affecting | |||
| 710 | /// affecting normal operations. It can be run with or without | |||
| 711 | /// packet queuing. | |||
| 712 | /// | |||
| 713 | /// @param use_queue determines if packet queuing is used or not. | |||
| 714 | void unusableExternalSockets4Test(bool use_queue = false) { | |||
| 715 | callback_ok = false; | |||
| 716 | callback2_ok = false; | |||
| 717 | ||||
| 718 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 719 | ||||
| 720 | if (use_queue) { | |||
| 721 | bool queue_enabled = false; | |||
| 722 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500); | |||
| 723 | 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_723; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_723 ; } } else gtest_label_testnothrow_723 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 723, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 724 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 724, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "false", "true") .c_str()) = ::testing::Message (); | |||
| 725 | ||||
| 726 | // Thread should only start when there is a packet queue. | |||
| 727 | 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_727 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_727 ; } } else gtest_label_testnothrow_727 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 727, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 728 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 728, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 729 | } | |||
| 730 | ||||
| 731 | // Create first pipe and register it as extra socket | |||
| 732 | int pipefd[2]; | |||
| 733 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 733, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 734 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 734, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 735 | 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], [&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_738; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_738; } } else gtest_label_testnothrow_738 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 738, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 736 | [&pipefd](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&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_738; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_738; } } else gtest_label_testnothrow_738 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 738, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 737 | 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], [&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_738; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_738; } } else gtest_label_testnothrow_738 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 738, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 738 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&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_738; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_738; } } else gtest_label_testnothrow_738 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 738, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 739 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 739, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 740 | ASSERT_FALSE(ifacemgr->isExternalSocketUnusable(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocketUnusable (pipefd[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 740, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(pipefd[0])", "true", "false") .c_str()) = ::testing::Message(); | |||
| 741 | ||||
| 742 | // Let's create a second pipe and register it as well | |||
| 743 | int secondpipe[2]; | |||
| 744 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 744, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(secondpipe) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 745 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 745, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 746 | 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], [&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_749; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_749; } } else gtest_label_testnothrow_749 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 749, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 747 | [&secondpipe](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&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_749; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_749; } } else gtest_label_testnothrow_749 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 749, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 748 | 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], [&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_749; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_749; } } else gtest_label_testnothrow_749 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 749, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 749 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&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_749; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_749; } } else gtest_label_testnothrow_749 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 749, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 750 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 750, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 751 | ASSERT_FALSE(ifacemgr->isExternalSocketUnusable(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocketUnusable (secondpipe[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 751, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(secondpipe[0])", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 752 | ||||
| 753 | // Verify a call with no data and normal external sockets works ok. | |||
| 754 | Pkt4Ptr pkt4; | |||
| 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, "../../../src/lib/dhcp/tests/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 | // No callback invocations and no DHCPv4 pkt. | |||
| 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 758, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 759 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 759, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 760, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 761 | ||||
| 762 | // Now close the first pipe. This should make it's external socket invalid. | |||
| 763 | close(pipefd[1]); | |||
| 764 | close(pipefd[0]); | |||
| 765 | ||||
| 766 | // We call receive4() which should detect and remove the invalid socket. | |||
| 767 | try { | |||
| 768 | pkt4 = ifacemgr->receive4(RECEIVE_WAIT_MS(10)0,(10*1000)); | |||
| 769 | } catch (const SocketFDError& ex) { | |||
| 770 | std::ostringstream err_msg; | |||
| 771 | err_msg << "unexpected state (closed) for fd: " << pipefd[0]; | |||
| 772 | EXPECT_EQ(err_msg.str(), ex.what())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("err_msg.str()" , "ex.what()", err_msg.str(), ex.what()))) ; else ::testing:: internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 772, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 773 | } catch (const std::exception& ex) { | |||
| 774 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 774, "Failed") = ::testing::Message() << "wrong exception thrown: " << ex.what(); | |||
| 775 | } | |||
| 776 | EXPECT_TRUE(ifacemgr->isExternalSocketUnusable(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isExternalSocketUnusable (pipefd[0]))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 776, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(pipefd[0])", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 777 | EXPECT_FALSE(ifacemgr->isExternalSocketUnusable(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocketUnusable (secondpipe[0])))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 777, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(secondpipe[0])", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 778 | ||||
| 779 | // No callback invocations and no DHCPv4 pkt. | |||
| 780 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 780, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 781 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 781, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 782 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 782, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 783 | ||||
| 784 | // Now check whether the second callback is still functional | |||
| 785 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 785, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 786 | ||||
| 787 | // Call receive4 again, this should work. | |||
| 788 | 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_788; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_788; } } else gtest_label_testnothrow_788 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 788, ("Expected: " "pkt4 = ifacemgr->receive4(0,(10*1000))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 789 | ||||
| 790 | // Should have callback2 data only. | |||
| 791 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 791, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 792 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 792, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 793 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 793, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 794 | ||||
| 795 | // Stop the thread. This should be no harm/no foul if we're not | |||
| 796 | // queueuing. Either way, we should not have a thread afterwards. | |||
| 797 | 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_797 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_797 ; } } else gtest_label_testnothrow_797 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 797, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 798 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 798, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 799 | } | |||
| 800 | ||||
| 801 | /// @brief Verifies that IfaceMgr DHCPv6 receive calls detect and | |||
| 802 | /// ignore external sockets that have gone bad without affecting | |||
| 803 | /// affecting normal operations. It can be run with or without | |||
| 804 | /// packet queuing. | |||
| 805 | /// | |||
| 806 | /// @param use_queue determines if packet queuing is used or not. | |||
| 807 | void unusableExternalSockets6Test(bool use_queue = false) { | |||
| 808 | callback_ok = false; | |||
| 809 | callback2_ok = false; | |||
| 810 | ||||
| 811 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 812 | ||||
| 813 | if (use_queue) { | |||
| 814 | bool queue_enabled = false; | |||
| 815 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500); | |||
| 816 | 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_816; } catch (... ) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_816 ; } } else gtest_label_testnothrow_816 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 816, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 817 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 817, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "false", "true") .c_str()) = ::testing::Message (); | |||
| 818 | ||||
| 819 | // Thread should only start when there is a packet queue. | |||
| 820 | 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_820 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_820 ; } } else gtest_label_testnothrow_820 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 820, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 821 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 821, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 822 | } | |||
| 823 | ||||
| 824 | // Create first pipe and register it as extra socket | |||
| 825 | int pipefd[2]; | |||
| 826 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 826, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 827 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 827, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 828 | 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], [&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_831; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_831; } } else gtest_label_testnothrow_831 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 831, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 829 | [&pipefd](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&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_831; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_831; } } else gtest_label_testnothrow_831 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 831, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 830 | 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], [&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_831; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_831; } } else gtest_label_testnothrow_831 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 831, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 831 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(pipefd[0], [&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_831; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_831; } } else gtest_label_testnothrow_831 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 831, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], [&pipefd](int fd) { callback_ok = (pipefd[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 832 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 832, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(pipefd[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 833 | ASSERT_FALSE(ifacemgr->isExternalSocketUnusable(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocketUnusable (pipefd[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 833, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(pipefd[0])", "true", "false") .c_str()) = ::testing::Message(); | |||
| 834 | ||||
| 835 | // Let's create a second pipe and register it as well | |||
| 836 | int secondpipe[2]; | |||
| 837 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 837, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(secondpipe) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 838 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 838, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 839 | 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], [&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_842; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_842; } } else gtest_label_testnothrow_842 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 842, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 840 | [&secondpipe](int fd) {switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&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_842; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_842; } } else gtest_label_testnothrow_842 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 842, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 841 | 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], [&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_842; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_842; } } else gtest_label_testnothrow_842 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 842, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 842 | }))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr->addExternalSocket(secondpipe[0], [&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_842; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_842; } } else gtest_label_testnothrow_842 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 842, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], [&secondpipe](int fd) { callback2_ok = (secondpipe[0] == fd); })" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 843 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 843, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocket(secondpipe[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 844 | ASSERT_FALSE(ifacemgr->isExternalSocketUnusable(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocketUnusable (secondpipe[0])))) ; else return ::testing::internal::AssertHelper (::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 844, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(secondpipe[0])", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 845 | ||||
| 846 | // Verify a call with no data and normal external sockets works ok. | |||
| 847 | Pkt6Ptr pkt6; | |||
| 848 | 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_848; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_848; } } else gtest_label_testnothrow_848 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 848, ("Expected: " "pkt6 = ifacemgr->receive6(0,(10*1000))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 849 | ||||
| 850 | // No callback invocations and no DHCPv6 pkt. | |||
| 851 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 851, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 852 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 852, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 853 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 853, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 854 | ||||
| 855 | // Now close the first pipe. This should make it's external socket invalid. | |||
| 856 | close(pipefd[1]); | |||
| 857 | close(pipefd[0]); | |||
| 858 | ||||
| 859 | // We call receive6() which should detect and remove the invalid socket. | |||
| 860 | try { | |||
| 861 | pkt6 = ifacemgr->receive6(RECEIVE_WAIT_MS(10)0,(10*1000)); | |||
| 862 | } catch (const SocketFDError& ex) { | |||
| 863 | std::ostringstream err_msg; | |||
| 864 | err_msg << "unexpected state (closed) for fd: " << pipefd[0]; | |||
| 865 | EXPECT_EQ(err_msg.str(), ex.what())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("err_msg.str()" , "ex.what()", err_msg.str(), ex.what()))) ; else ::testing:: internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 865, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 866 | } catch (const std::exception& ex) { | |||
| 867 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 867, "Failed") = ::testing::Message() << "wrong exception thrown: " << ex.what(); | |||
| 868 | } | |||
| 869 | EXPECT_TRUE(ifacemgr->isExternalSocketUnusable(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr->isExternalSocketUnusable (pipefd[0]))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 869, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(pipefd[0])", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 870 | EXPECT_FALSE(ifacemgr->isExternalSocketUnusable(secondpipe[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr->isExternalSocketUnusable (secondpipe[0])))) ; else ::testing::internal::AssertHelper(:: testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 870, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isExternalSocketUnusable(secondpipe[0])", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 871 | ||||
| 872 | // No callback invocations and no DHCPv6 pkt. | |||
| 873 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 873, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 874 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 874, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 875 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 875, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 876 | ||||
| 877 | // Now check whether the second callback is still functional | |||
| 878 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 878, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 879 | ||||
| 880 | // Call receive6 again, this should work. | |||
| 881 | 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_881; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_881; } } else gtest_label_testnothrow_881 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 881, ("Expected: " "pkt6 = ifacemgr->receive6(0,(10*1000))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 882 | ||||
| 883 | // Should have callback2 data only. | |||
| 884 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 884, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 885 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 885, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 886 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 886, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 887 | ||||
| 888 | // Stop the thread. This should be no harm/no foul if we're not | |||
| 889 | // queueuing. Either way, we should not have a thread afterwards. | |||
| 890 | 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_890 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_890 ; } } else gtest_label_testnothrow_890 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 890, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 891 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 891, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 892 | } | |||
| 893 | ||||
| 894 | /// @brief test receive timeout (v6). | |||
| 895 | void testReceiveTimeout6(); | |||
| 896 | ||||
| 897 | /// @brief test receive timeout (v4). | |||
| 898 | void testReceiveTimeout4(); | |||
| 899 | ||||
| 900 | /// @brief Tests single external socket (v4). | |||
| 901 | void testSingleExternalSocket4(); | |||
| 902 | ||||
| 903 | /// @brief Tests multiple external sockets (v4); | |||
| 904 | void testMultipleExternalSockets4(); | |||
| 905 | ||||
| 906 | /// @brief Tests if existing external socket can be deleted (v4). | |||
| 907 | void testDeleteExternalSockets4(); | |||
| 908 | ||||
| 909 | /// @brief Tests single external socket (v6). | |||
| 910 | void testSingleExternalSocket6(); | |||
| 911 | ||||
| 912 | /// @brief Tests multiple external sockets (v6); | |||
| 913 | void testMultipleExternalSockets6(); | |||
| 914 | ||||
| 915 | /// @brief Tests if existing external socket can be deleted (v6). | |||
| 916 | void testDeleteExternalSockets6(); | |||
| 917 | ||||
| 918 | /// @brief Holds the invocation counter for ifaceMgrErrorHandler. | |||
| 919 | int errors_count_; | |||
| 920 | ||||
| 921 | /// @brief RAII wrapper for KEA_EVENT_HANDLER_TYPE env variable. | |||
| 922 | EnvVarWrapper kea_event_handler_type_; | |||
| 923 | }; | |||
| 924 | ||||
| 925 | // We need some known interface to work reliably. Loopback interface is named | |||
| 926 | // lo on Linux and lo0 on BSD boxes. We need to find out which is available. | |||
| 927 | // This is not a real test, but rather a workaround that will go away when | |||
| 928 | // interface detection is implemented on all OSes. | |||
| 929 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceMgrTest_loDetect_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "loDetect" , nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 929), (::testing::internal::GetTypeId<IfaceMgrTest>() ), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 929), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 929), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_loDetect_Test >); void IfaceMgrTest_loDetect_Test::TestBody() { | |||
| 930 | NakedIfaceMgr::loDetect(); | |||
| 931 | } | |||
| 932 | ||||
| 933 | // Uncomment this test to create packet writer. It will | |||
| 934 | // write incoming DHCPv6 packets as C arrays. That is useful | |||
| 935 | // for generating test sequences based on actual traffic | |||
| 936 | // | |||
| 937 | // TODO: this potentially should be moved to a separate tool | |||
| 938 | // | |||
| 939 | ||||
| 940 | #if 0 | |||
| 941 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_dhcp6Sniffer_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "dhcp6Sniffer", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 941), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 941), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 941), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_dhcp6Sniffer_Test >); void IfaceMgrTest_dhcp6Sniffer_Test::TestBody() { | |||
| 942 | // Testing socket operation in a portable way is tricky | |||
| 943 | // without interface detection implemented | |||
| 944 | ||||
| 945 | static_cast<void>(remove("interfaces.txt")); | |||
| 946 | ||||
| 947 | ofstream interfaces("interfaces.txt", ios::ate); | |||
| 948 | interfaces << "eth0 fe80::21e:8cff:fe9b:7349"; | |||
| 949 | interfaces.close(); | |||
| 950 | ||||
| 951 | boost::scoped_ptr<NakedIfaceMgr> ifacemgr = new NakedIfaceMgr(); | |||
| 952 | ||||
| 953 | Pkt6Ptr pkt; | |||
| 954 | int cnt = 0; | |||
| 955 | cout << "---8X-----------------------------------------" << endl; | |||
| 956 | while (true) { | |||
| 957 | pkt.reset(ifacemgr->receive()); | |||
| 958 | ||||
| 959 | cout << "// this code is autogenerated. Do NOT edit." << endl; | |||
| 960 | cout << "// Received " << pkt->data_len_ << " bytes packet:" << endl; | |||
| 961 | cout << "Pkt6 *capture" << cnt++ << "() {" << endl; | |||
| 962 | cout << " Pkt6* pkt;" << endl; | |||
| 963 | cout << " pkt = new Pkt6(" << pkt->data_len_ << ");" << endl; | |||
| 964 | cout << " pkt->remote_port_ = " << pkt-> remote_port_ << ";" << endl; | |||
| 965 | cout << " pkt->remote_addr_ = IOAddress(\"" | |||
| 966 | << pkt->remote_addr_ << "\");" << endl; | |||
| 967 | cout << " pkt->local_port_ = " << pkt-> local_port_ << ";" << endl; | |||
| 968 | cout << " pkt->local_addr_ = IOAddress(\"" | |||
| 969 | << pkt->local_addr_ << "\");" << endl; | |||
| 970 | cout << " pkt->ifindex_ = " << pkt->ifindex_ << ";" << endl; | |||
| 971 | cout << " pkt->iface_ = \"" << pkt->iface_ << "\";" << endl; | |||
| 972 | ||||
| 973 | // TODO it is better to declare statically initialize the array | |||
| 974 | // and then memcpy it to packet. | |||
| 975 | for (int i=0; i< pkt->data_len_; i++) { | |||
| 976 | cout << " pkt->data_[" << i << "]=" | |||
| 977 | << (int)(unsigned char)pkt->data_[i] << "; "; | |||
| 978 | if (!(i%4)) | |||
| 979 | cout << endl; | |||
| 980 | } | |||
| 981 | cout << endl; | |||
| 982 | cout << " return (pkt);" << endl; | |||
| 983 | cout << "}" << endl << endl; | |||
| 984 | ||||
| 985 | pkt.reset(); | |||
| 986 | } | |||
| 987 | cout << "---8X-----------------------------------------" << endl; | |||
| 988 | ||||
| 989 | // Never happens. Infinite loop is infinite | |||
| 990 | } | |||
| 991 | #endif | |||
| 992 | ||||
| 993 | // This test verifies that creation of the IfaceMgr instance doesn't | |||
| 994 | // cause an exception. | |||
| 995 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceMgrTest_instance_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "instance" , nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 995), (::testing::internal::GetTypeId<IfaceMgrTest>() ), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 995), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 995), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_instance_Test >); void IfaceMgrTest_instance_Test::TestBody() { | |||
| 996 | 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_996 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_996 ; } } else gtest_label_testnothrow_996 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 996, ("Expected: " "IfaceMgr::instance()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 997 | } | |||
| 998 | ||||
| 999 | // Basic tests for Iface inner class. | |||
| 1000 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_ifaceClass_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "ifaceClass", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1000), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1000), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1000), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_ifaceClass_Test >); void IfaceMgrTest_ifaceClass_Test::TestBody() { | |||
| 1001 | ||||
| 1002 | IfacePtr iface(new Iface("eth5", 7)); | |||
| 1003 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1003, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1004 | ||||
| 1005 | EXPECT_THROW_MSG(iface.reset(new Iface("", 10)), BadValue,{ try { iface.reset(new Iface("", 10)); ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1006, "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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1006, gtest_ar .failure_message()) = ::testing::Message(); } catch (...) { :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1006, "Failed" ) = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } } | |||
| 1006 | "Interface name must not be empty"){ try { iface.reset(new Iface("", 10)); ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1006, "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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1006, gtest_ar .failure_message()) = ::testing::Message(); } catch (...) { :: testing::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1006, "Failed" ) = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } }; | |||
| 1007 | ||||
| 1008 | 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_1008; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1008; } } else gtest_label_testnothrow_1008 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1008, ("Expected: " "iface.reset(new Iface(\"big-index\", 66666))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1009 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1009, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1010 | } | |||
| 1011 | ||||
| 1012 | // This test checks the getIface by index method. | |||
| 1013 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_getIfaceByIndex_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "getIfaceByIndex", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1013), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1013), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1013), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_getIfaceByIndex_Test >); void IfaceMgrTest_getIfaceByIndex_Test::TestBody() { | |||
| 1014 | NakedIfaceMgr ifacemgr; | |||
| 1015 | ||||
| 1016 | // Create a set of fake interfaces. At the same time, remove the actual | |||
| 1017 | // interfaces that have been detected by the IfaceMgr. | |||
| 1018 | ifacemgr.createIfaces(); | |||
| 1019 | ||||
| 1020 | // Getting an unset index should throw. | |||
| 1021 | EXPECT_THROW_MSG(ifacemgr.getIface(UNSET_IFINDEX), BadValue, "interface index was not set"){ try { ifacemgr.getIface(UNSET_IFINDEX); ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1021, "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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1021, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1021, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } }; | |||
| 1022 | ||||
| 1023 | // Historically -1 was used as an unset value. Let's also check that it throws in case we didn't | |||
| 1024 | // migrate all code to UNSET_IFINDEX and in case the values diverge. | |||
| 1025 | EXPECT_THROW_MSG(ifacemgr.getIface(-1), BadValue, "interface index was not set"){ try { ifacemgr.getIface(-1); ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1025, "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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1025, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1025, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "BadValue"; } }; | |||
| 1026 | ||||
| 1027 | // Get the first interface defined. | |||
| 1028 | IfacePtr iface(ifacemgr.getIface(0)); | |||
| 1029 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1029, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1030 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1030, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1031 | ||||
| 1032 | // Attemt to get an undefined interface. | |||
| 1033 | iface = ifacemgr.getIface(3); | |||
| 1034 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1034, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1035 | ||||
| 1036 | // Check that we can go past INT_MAX. | |||
| 1037 | unsigned int int_max(numeric_limits<int>::max()); | |||
| 1038 | iface = ifacemgr.getIface(int_max); | |||
| 1039 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1039, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1040 | iface = ifacemgr.createIface("wlan0", int_max); | |||
| 1041 | ifacemgr.addInterface(iface); | |||
| 1042 | iface = ifacemgr.getIface(int_max); | |||
| 1043 | EXPECT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1043, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1044 | iface = ifacemgr.getIface(int_max + 1); | |||
| 1045 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1045, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1046 | iface = ifacemgr.createIface("wlan1", int_max + 1); | |||
| 1047 | ifacemgr.addInterface(iface); | |||
| 1048 | iface = ifacemgr.getIface(int_max + 1); | |||
| 1049 | EXPECT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1049, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1050 | } | |||
| 1051 | ||||
| 1052 | // This test checks the getIface by packet method. | |||
| 1053 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_getIfaceByPkt_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "getIfaceByPkt", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1053), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1053), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1053), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_getIfaceByPkt_Test >); void IfaceMgrTest_getIfaceByPkt_Test::TestBody() { | |||
| 1054 | NakedIfaceMgr ifacemgr; | |||
| 1055 | // Create a set of fake interfaces. At the same time, remove the actual | |||
| 1056 | // interfaces that have been detected by the IfaceMgr. | |||
| 1057 | ifacemgr.createIfaces(); | |||
| 1058 | ||||
| 1059 | // Try IPv4 packet by name. | |||
| 1060 | Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 1234)); | |||
| 1061 | IfacePtr iface = ifacemgr.getIface(pkt4); | |||
| 1062 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1062, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1063 | pkt4->setIface("eth0"); | |||
| 1064 | iface = ifacemgr.getIface(pkt4); | |||
| 1065 | EXPECT_TRUE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(iface)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1065, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1066 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1066, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt4->indexSet()", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 1067 | ||||
| 1068 | // Try IPv6 packet by index. | |||
| 1069 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_REPLY, 123456)); | |||
| 1070 | iface = ifacemgr.getIface(pkt6); | |||
| 1071 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1071, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1072 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1072, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1073 | pkt6->setIndex(ifacemgr.getIface("eth0")->getIndex() + 1); | |||
| 1074 | iface = ifacemgr.getIface(pkt6); | |||
| 1075 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1075, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1076 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1076, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt6->indexSet()", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1077 | ||||
| 1078 | // Index has precedence when both name and index are available. | |||
| 1079 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1079, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1080 | pkt6->setIface("eth0"); | |||
| 1081 | iface = ifacemgr.getIface(pkt6); | |||
| 1082 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1082, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1083 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1083, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1084 | ||||
| 1085 | // Not existing name fails. | |||
| 1086 | pkt4->setIface("eth2"); | |||
| 1087 | iface = ifacemgr.getIface(pkt4); | |||
| 1088 | EXPECT_FALSE(iface)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(iface))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1088, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1089 | ||||
| 1090 | // Not existing index fails. | |||
| 1091 | pkt6->setIndex(3); | |||
| 1092 | iface = ifacemgr.getIface(pkt6); | |||
| 1093 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1093, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1094 | ||||
| 1095 | // Test that resetting the index is verifiable. | |||
| 1096 | pkt4->resetIndex(); | |||
| 1097 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1097, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt4->indexSet()", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 1098 | pkt6->resetIndex(); | |||
| 1099 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1099, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt6->indexSet()", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 1100 | ||||
| 1101 | // Test that you can also reset the index via setIndex(). | |||
| 1102 | pkt4->setIndex(UNSET_IFINDEX); | |||
| 1103 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1103, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt4->indexSet()", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 1104 | pkt6->setIndex(UNSET_IFINDEX); | |||
| 1105 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1105, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt6->indexSet()", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 1106 | } | |||
| 1107 | ||||
| 1108 | // Test that the IPv4 address can be retrieved for the interface. | |||
| 1109 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_ifaceGetAddress_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "ifaceGetAddress", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1109), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1109), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1109), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_ifaceGetAddress_Test >); void IfaceMgrTest_ifaceGetAddress_Test::TestBody() { | |||
| 1110 | Iface iface("eth0", 0); | |||
| 1111 | ||||
| 1112 | IOAddress addr("::1"); | |||
| 1113 | // Initially, the Iface has no addresses assigned. | |||
| 1114 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1114, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface.getAddress4(addr)", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 1115 | // Add some addresses with IPv4 address in the middle. | |||
| 1116 | iface.addAddress(IOAddress("fe80::3a60:77ff:fed5:cdef")); | |||
| 1117 | iface.addAddress(IOAddress("10.1.2.3")); | |||
| 1118 | iface.addAddress(IOAddress("2001:db8:1::2")); | |||
| 1119 | // The v4 address should be returned. | |||
| 1120 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1120, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface.getAddress4(addr)", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1121 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1121, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1122 | // Delete the IPv4 address and leave only two IPv6 addresses. | |||
| 1123 | 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_1123; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1123; } } else gtest_label_testnothrow_1123 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1123, ("Expected: " "iface.delAddress(IOAddress(\"10.1.2.3\"))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1124 | // The IPv4 address should not be returned. | |||
| 1125 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1125, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface.getAddress4(addr)", "true", "false") .c_str()) = ::testing ::Message(); | |||
| 1126 | // Add a different IPv4 address at the end of the list. | |||
| 1127 | iface.addAddress(IOAddress("192.0.2.3")); | |||
| 1128 | // This new address should now be returned. | |||
| 1129 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1129, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface.getAddress4(addr)", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1130 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1130, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1131 | } | |||
| 1132 | ||||
| 1133 | // This test checks if it is possible to check that the specific address is | |||
| 1134 | // assigned to the interface. | |||
| 1135 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_ifaceHasAddress_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "ifaceHasAddress", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1135), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1135), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1135), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_ifaceHasAddress_Test >); void IfaceMgrTest_ifaceHasAddress_Test::TestBody() { | |||
| 1136 | IfaceMgrTestConfig config(true); | |||
| 1137 | ||||
| 1138 | IfacePtr iface = IfaceMgr::instance().getIface("eth0"); | |||
| 1139 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1139, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1140 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1140, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"10.0.0.1\"))", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 1141 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1141, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"10.0.0.2\"))", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 1142 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1142, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 1143 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1143, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"2001:db8:1::1\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 1144 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1144, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->hasAddress(IOAddress(\"2001:db8:1::2\"))", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 1145 | } | |||
| 1146 | ||||
| 1147 | // This test checks it is not allowed to add duplicate interfaces. | |||
| 1148 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_addInterface_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "addInterface", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1148), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1148), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1148), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_addInterface_Test >); void IfaceMgrTest_addInterface_Test::TestBody() { | |||
| 1149 | IfaceMgrTestConfig config(true); | |||
| 1150 | ||||
| 1151 | IfacePtr dup_name(new Iface("eth1", 123)); | |||
| 1152 | EXPECT_THROW_MSG(IfaceMgr::instance().addInterface(dup_name), Unexpected,{ try { IfaceMgr::instance().addInterface(dup_name); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1153, "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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1153, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1153, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } } | |||
| 1153 | "Can't add eth1/123 when eth1/2 already exists."){ try { IfaceMgr::instance().addInterface(dup_name); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1153, "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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1153, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1153, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } }; | |||
| 1154 | IfacePtr dup_index(new Iface("eth2", 2)); | |||
| 1155 | EXPECT_THROW_MSG(IfaceMgr::instance().addInterface(dup_index), Unexpected,{ try { IfaceMgr::instance().addInterface(dup_index); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1156, "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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1156, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1156, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } } | |||
| 1156 | "Can't add eth2/2 when eth1/2 already exists."){ try { IfaceMgr::instance().addInterface(dup_index); ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1156, "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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1156, gtest_ar.failure_message()) = ::testing::Message(); } catch (...) { ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1156, "Failed") = ::testing::Message() << "wrong exception type thrown, expected: " << "Unexpected"; } }; | |||
| 1157 | ||||
| 1158 | IfacePtr eth2(new Iface("eth2", 3)); | |||
| 1159 | 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_1159; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1159; } } else gtest_label_testnothrow_1159 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1159, ("Expected: " "IfaceMgr::instance().addInterface(eth2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1160 | } | |||
| 1161 | ||||
| 1162 | // TODO: Implement getPlainMac() test as soon as interface detection | |||
| 1163 | // is implemented. | |||
| 1164 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceMgrTest_getIface_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "getIface" , nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1164), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1164), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1164), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_getIface_Test >); void IfaceMgrTest_getIface_Test::TestBody() { | |||
| 1165 | ||||
| 1166 | cout << "Interface checks. Please ignore socket binding errors." << endl; | |||
| 1167 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1168 | ||||
| 1169 | // Interface name, ifindex | |||
| 1170 | IfacePtr iface1(new Iface("lo1", 100)); | |||
| 1171 | IfacePtr iface2(new Iface("eth9", 101)); | |||
| 1172 | IfacePtr iface3(new Iface("en99", 102)); | |||
| 1173 | IfacePtr iface4(new Iface("e1000g4", 103)); | |||
| 1174 | cout << "This test assumes that there are less than 100 network interfaces" | |||
| 1175 | << " in the tested system and there are no lo1, eth9, en99, e1000g4" | |||
| 1176 | << " or wifi15 interfaces present." << endl; | |||
| 1177 | ||||
| 1178 | // Note: real interfaces may be detected as well | |||
| 1179 | ifacemgr->getIfacesLst().push_back(iface1); | |||
| 1180 | ifacemgr->getIfacesLst().push_back(iface2); | |||
| 1181 | ifacemgr->getIfacesLst().push_back(iface3); | |||
| 1182 | ifacemgr->getIfacesLst().push_back(iface4); | |||
| 1183 | ||||
| 1184 | cout << "There are " << ifacemgr->getIfacesLst().size() | |||
| 1185 | << " interfaces." << endl; | |||
| 1186 | for (auto const& iface : ifacemgr->getIfacesLst()) { | |||
| 1187 | cout << " " << iface->getFullName() << endl; | |||
| 1188 | } | |||
| 1189 | ||||
| 1190 | // Check that interface can be retrieved by ifindex | |||
| 1191 | IfacePtr tmp = ifacemgr->getIface(102); | |||
| 1192 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1192, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "tmp", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1193 | ||||
| 1194 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1194, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1195 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1195, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1196 | ||||
| 1197 | // Check that interface can be retrieved by name | |||
| 1198 | tmp = ifacemgr->getIface("lo1"); | |||
| 1199 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1199, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "tmp", "false", "true") .c_str()) = ::testing::Message(); | |||
| 1200 | ||||
| 1201 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1201, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1202 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1202, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1203 | ||||
| 1204 | // Check that non-existing interfaces are not returned | |||
| 1205 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1205, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getIface(\"wifi15\")", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 1206 | } | |||
| 1207 | ||||
| 1208 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_clearIfaces_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "clearIfaces", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1208), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1208), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1208), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_clearIfaces_Test >); void IfaceMgrTest_clearIfaces_Test::TestBody() { | |||
| 1209 | NakedIfaceMgr ifacemgr; | |||
| 1210 | // Create a set of fake interfaces. At the same time, remove the actual | |||
| 1211 | // interfaces that have been detected by the IfaceMgr. | |||
| 1212 | ifacemgr.createIfaces(); | |||
| 1213 | ||||
| 1214 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1214, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1215 | ||||
| 1216 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 1217 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1217, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1218 | 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_1218; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1218; } } else gtest_label_testnothrow_1218 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1218, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1219 | ||||
| 1220 | 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_1220 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1220 ; } } else gtest_label_testnothrow_1220 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1220, ("Expected: " "ifacemgr.openSockets4()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 1221 | ||||
| 1222 | ifacemgr.clearIfaces(); | |||
| 1223 | ||||
| 1224 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1224, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1225 | } | |||
| 1226 | ||||
| 1227 | // Verify that we have the expected default DHCPv4 packet queue. | |||
| 1228 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_packetQueue4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "packetQueue4", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1228), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1228), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1228), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_packetQueue4_Test >); void IfaceMgrTest_packetQueue4_Test::TestBody() { | |||
| 1229 | NakedIfaceMgr ifacemgr; | |||
| 1230 | ||||
| 1231 | // Should not have a queue at start up. | |||
| 1232 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1232, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getPacketQueue4()", "true", "false") .c_str()) = :: testing::Message(); | |||
| 1233 | ||||
| 1234 | // Verify that we can create a queue with default factory. | |||
| 1235 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 2000); | |||
| 1236 | 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_1236; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1236 ; } } else gtest_label_testnothrow_1236 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1236, ("Expected: " "ifacemgr.getPacketQueueMgr4()->createPacketQueue(config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1237 | 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()); } | |||
| 1238 | << 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()); }; | |||
| 1239 | } | |||
| 1240 | ||||
| 1241 | // Verify that we have the expected default DHCPv6 packet queue. | |||
| 1242 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_packetQueue6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "packetQueue6", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1242), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1242), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1242), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_packetQueue6_Test >); void IfaceMgrTest_packetQueue6_Test::TestBody() { | |||
| 1243 | NakedIfaceMgr ifacemgr; | |||
| 1244 | ||||
| 1245 | // Should not have a queue at start up. | |||
| 1246 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1246, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getPacketQueue6()", "true", "false") .c_str()) = :: testing::Message(); | |||
| 1247 | ||||
| 1248 | // Verify that we can create a queue with default factory. | |||
| 1249 | data::ConstElementPtr config = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 2000); | |||
| 1250 | 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_1250; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1250 ; } } else gtest_label_testnothrow_1250 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1250, ("Expected: " "ifacemgr.getPacketQueueMgr6()->createPacketQueue(config)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1251 | 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()); } | |||
| 1252 | << 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()); }; | |||
| 1253 | } | |||
| 1254 | ||||
| 1255 | void IfaceMgrTest::testReceiveTimeout6() { | |||
| 1256 | using namespace boost::posix_time; | |||
| 1257 | std::cout << "Testing DHCPv6 packet reception timeouts." | |||
| 1258 | << " Test will block for a few seconds when waiting" | |||
| 1259 | << " for timeout to occur." << std::endl; | |||
| 1260 | ||||
| 1261 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1262 | // Open socket on the lo interface. | |||
| 1263 | IOAddress lo_addr("::1"); | |||
| 1264 | int socket1 = 0; | |||
| 1265 | 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_1267; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1267 ; } } else gtest_label_testnothrow_1267 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1267, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1266 | 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_1267; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1267 ; } } else gtest_label_testnothrow_1267 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1267, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1267 | )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_1267; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1267 ; } } else gtest_label_testnothrow_1267 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1267, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1268 | // Socket is open if result is non-negative. | |||
| 1269 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1269, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1270 | // Start receiver. | |||
| 1271 | 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_1271 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1271 ; } } else gtest_label_testnothrow_1271 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1271, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 1272 | ||||
| 1273 | // Remember when we call receive6(). | |||
| 1274 | ptime start_time = microsec_clock::universal_time(); | |||
| 1275 | // Call receive with timeout of 1s + 400000us = 1.4s. | |||
| 1276 | Pkt6Ptr pkt; | |||
| 1277 | 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_1277; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1277; } } else gtest_label_testnothrow_1277 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1277, ("Expected: " "pkt = ifacemgr->receive6(1, 400000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1278 | // Remember when call to receive6() ended. | |||
| 1279 | ptime stop_time = microsec_clock::universal_time(); | |||
| 1280 | // We did not send a packet to lo interface so we expect that | |||
| 1281 | // nothing has been received and timeout has been reached. | |||
| 1282 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1282, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1283 | // Calculate duration of call to receive6(). | |||
| 1284 | time_duration duration = stop_time - start_time; | |||
| 1285 | // We stop the clock when the call completes so it does not | |||
| 1286 | // precisely reflect the receive timeout. However the | |||
| 1287 | // uncertainty should be low enough to expect that measured | |||
| 1288 | // value is in the range <1.4s; 1.7s>. | |||
| 1289 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1290, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1290 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1290, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1291 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1291, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1292 | ||||
| 1293 | // Test timeout shorter than 1s. | |||
| 1294 | start_time = microsec_clock::universal_time(); | |||
| 1295 | 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_1295; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1295; } } else gtest_label_testnothrow_1295 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1295, ("Expected: " "pkt = ifacemgr->receive6(0, 500000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1296 | stop_time = microsec_clock::universal_time(); | |||
| 1297 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1297, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1298 | duration = stop_time - start_time; | |||
| 1299 | // Check if measured duration is within <0.5s; 0.8s>. | |||
| 1300 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1301, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1301 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1301, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1302 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1302, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1303 | ||||
| 1304 | // Test with invalid fractional timeout values. | |||
| 1305 | 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_1305; } 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_1305; } 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_1305; } } else gtest_label_testthrow_1305 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1305, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1306 | 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_1306; } 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_1306; } 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_1306; } } else gtest_label_testthrow_1306 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1306, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1307 | ||||
| 1308 | // Stop receiver. | |||
| 1309 | 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_1309 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1309 ; } } else gtest_label_testnothrow_1309 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1309, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 1310 | } | |||
| 1311 | ||||
| 1312 | TEST_F(IfaceMgrTest, receiveTimeout6Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("receiveTimeout6Select") > 1, "test_name must not be empty" ); class IfaceMgrTest_receiveTimeout6Select_Test : public IfaceMgrTest { public: IfaceMgrTest_receiveTimeout6Select_Test() = default ; ~IfaceMgrTest_receiveTimeout6Select_Test() override = default ; IfaceMgrTest_receiveTimeout6Select_Test (const IfaceMgrTest_receiveTimeout6Select_Test &) = delete; IfaceMgrTest_receiveTimeout6Select_Test & operator=( const IfaceMgrTest_receiveTimeout6Select_Test & ) = delete; IfaceMgrTest_receiveTimeout6Select_Test (IfaceMgrTest_receiveTimeout6Select_Test &&) noexcept = delete; IfaceMgrTest_receiveTimeout6Select_Test & operator=( IfaceMgrTest_receiveTimeout6Select_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_receiveTimeout6Select_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "receiveTimeout6Select", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1312), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1312), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1312), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_receiveTimeout6Select_Test >); void IfaceMgrTest_receiveTimeout6Select_Test::TestBody () { | |||
| 1313 | kea_event_handler_type_.setValue("select"); | |||
| 1314 | testReceiveTimeout6(); | |||
| 1315 | } | |||
| 1316 | ||||
| 1317 | TEST_F(IfaceMgrTest, receiveTimeout6Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("receiveTimeout6Poll") > 1, "test_name must not be empty" ); class IfaceMgrTest_receiveTimeout6Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_receiveTimeout6Poll_Test() = default; ~IfaceMgrTest_receiveTimeout6Poll_Test() override = default; IfaceMgrTest_receiveTimeout6Poll_Test (const IfaceMgrTest_receiveTimeout6Poll_Test &) = delete; IfaceMgrTest_receiveTimeout6Poll_Test & operator=( const IfaceMgrTest_receiveTimeout6Poll_Test & ) = delete; IfaceMgrTest_receiveTimeout6Poll_Test (IfaceMgrTest_receiveTimeout6Poll_Test &&) noexcept = delete; IfaceMgrTest_receiveTimeout6Poll_Test & operator=( IfaceMgrTest_receiveTimeout6Poll_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_receiveTimeout6Poll_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "receiveTimeout6Poll", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1317), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1317), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1317), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_receiveTimeout6Poll_Test >); void IfaceMgrTest_receiveTimeout6Poll_Test::TestBody() { | |||
| 1318 | kea_event_handler_type_.setValue("poll"); | |||
| 1319 | testReceiveTimeout6(); | |||
| 1320 | } | |||
| 1321 | ||||
| 1322 | void IfaceMgrTest::testReceiveTimeout4() { | |||
| 1323 | using namespace boost::posix_time; | |||
| 1324 | std::cout << "Testing DHCPv4 packet reception timeouts." | |||
| 1325 | << " Test will block for a few seconds when waiting" | |||
| 1326 | << " for timeout to occur." << std::endl; | |||
| 1327 | ||||
| 1328 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1329 | // Open socket on the lo interface. | |||
| 1330 | IOAddress lo_addr("127.0.0.1"); | |||
| 1331 | int socket1 = 0; | |||
| 1332 | 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_1334; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1334 ; } } else gtest_label_testnothrow_1334 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1334, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1333 | 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_1334; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1334 ; } } else gtest_label_testnothrow_1334 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1334, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1334 | )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_1334; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1334 ; } } else gtest_label_testnothrow_1334 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1334, ("Expected: " "socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10067)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1335 | // Socket is open if returned value is non-negative. | |||
| 1336 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1336, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1337 | ||||
| 1338 | // Start receiver. | |||
| 1339 | 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_1339 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1339 ; } } else gtest_label_testnothrow_1339 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1339, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 1340 | ||||
| 1341 | Pkt4Ptr pkt; | |||
| 1342 | // Remember when we call receive4(). | |||
| 1343 | ptime start_time = microsec_clock::universal_time(); | |||
| 1344 | // Call receive with timeout of 2s + 300000us = 2.3s. | |||
| 1345 | 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_1345; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1345; } } else gtest_label_testnothrow_1345 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1345, ("Expected: " "pkt = ifacemgr->receive4(2, 300000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1346 | // Remember when call to receive4() ended. | |||
| 1347 | ptime stop_time = microsec_clock::universal_time(); | |||
| 1348 | // We did not send a packet to lo interface so we expect that | |||
| 1349 | // nothing has been received and timeout has been reached. | |||
| 1350 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1350, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1351 | // Calculate duration of call to receive4(). | |||
| 1352 | time_duration duration = stop_time - start_time; | |||
| 1353 | // We stop the clock when the call completes so it does not | |||
| 1354 | // precisely reflect the receive timeout. However the | |||
| 1355 | // uncertainty should be low enough to expect that measured | |||
| 1356 | // value is in the range <2.3s; 2.6s>. | |||
| 1357 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1358, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1358 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1358, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1359 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1359, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1360 | ||||
| 1361 | // Test timeout shorter than 1s. | |||
| 1362 | start_time = microsec_clock::universal_time(); | |||
| 1363 | 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_1363; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1363; } } else gtest_label_testnothrow_1363 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1363, ("Expected: " "pkt = ifacemgr->receive4(0, 400000)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1364 | stop_time = microsec_clock::universal_time(); | |||
| 1365 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1365, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pkt", "true", "false") .c_str()) = ::testing::Message(); | |||
| 1366 | duration = stop_time - start_time; | |||
| 1367 | // Check if measured duration is within <0.4s; 0.7s>. | |||
| 1368 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1369, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1369 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1369, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1370 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1370, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1371 | ||||
| 1372 | // Test with invalid fractional timeout values. | |||
| 1373 | 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_1373; } 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_1373; } 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_1373; } } else gtest_label_testthrow_1373 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1373, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1374 | 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_1374; } 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_1374; } 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_1374; } } else gtest_label_testthrow_1374 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1374, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1375 | ||||
| 1376 | // Stop receiver. | |||
| 1377 | 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_1377 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1377 ; } } else gtest_label_testnothrow_1377 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1377, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 1378 | } | |||
| 1379 | ||||
| 1380 | TEST_F(IfaceMgrTest, receiveTimeout4Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("receiveTimeout4Select") > 1, "test_name must not be empty" ); class IfaceMgrTest_receiveTimeout4Select_Test : public IfaceMgrTest { public: IfaceMgrTest_receiveTimeout4Select_Test() = default ; ~IfaceMgrTest_receiveTimeout4Select_Test() override = default ; IfaceMgrTest_receiveTimeout4Select_Test (const IfaceMgrTest_receiveTimeout4Select_Test &) = delete; IfaceMgrTest_receiveTimeout4Select_Test & operator=( const IfaceMgrTest_receiveTimeout4Select_Test & ) = delete; IfaceMgrTest_receiveTimeout4Select_Test (IfaceMgrTest_receiveTimeout4Select_Test &&) noexcept = delete; IfaceMgrTest_receiveTimeout4Select_Test & operator=( IfaceMgrTest_receiveTimeout4Select_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_receiveTimeout4Select_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "receiveTimeout4Select", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1380), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1380), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1380), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_receiveTimeout4Select_Test >); void IfaceMgrTest_receiveTimeout4Select_Test::TestBody () { | |||
| 1381 | kea_event_handler_type_.setValue("select"); | |||
| 1382 | testReceiveTimeout4(); | |||
| 1383 | } | |||
| 1384 | ||||
| 1385 | TEST_F(IfaceMgrTest, receiveTimeout4Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("receiveTimeout4Poll") > 1, "test_name must not be empty" ); class IfaceMgrTest_receiveTimeout4Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_receiveTimeout4Poll_Test() = default; ~IfaceMgrTest_receiveTimeout4Poll_Test() override = default; IfaceMgrTest_receiveTimeout4Poll_Test (const IfaceMgrTest_receiveTimeout4Poll_Test &) = delete; IfaceMgrTest_receiveTimeout4Poll_Test & operator=( const IfaceMgrTest_receiveTimeout4Poll_Test & ) = delete; IfaceMgrTest_receiveTimeout4Poll_Test (IfaceMgrTest_receiveTimeout4Poll_Test &&) noexcept = delete; IfaceMgrTest_receiveTimeout4Poll_Test & operator=( IfaceMgrTest_receiveTimeout4Poll_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_receiveTimeout4Poll_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "receiveTimeout4Poll", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1385), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1385), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1385), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_receiveTimeout4Poll_Test >); void IfaceMgrTest_receiveTimeout4Poll_Test::TestBody() { | |||
| 1386 | kea_event_handler_type_.setValue("poll"); | |||
| 1387 | testReceiveTimeout4(); | |||
| 1388 | } | |||
| 1389 | ||||
| 1390 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_multipleSockets_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "multipleSockets", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1390), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1390), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1390), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_multipleSockets_Test >); void IfaceMgrTest_multipleSockets_Test::TestBody() { | |||
| 1391 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1392 | ||||
| 1393 | // Container for initialized socket descriptors | |||
| 1394 | std::list<uint16_t> init_sockets; | |||
| 1395 | ||||
| 1396 | // Create socket #1 | |||
| 1397 | int socket1 = 0; | |||
| 1398 | 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_1400; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1400 ; } } else gtest_label_testnothrow_1400 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1400, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| ||||
| 1399 | 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_1400; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1400 ; } } else gtest_label_testnothrow_1400 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1400, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1400 | )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_1400; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1400 ; } } else gtest_label_testnothrow_1400 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1400, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1401 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1401, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1402 | init_sockets.push_back(socket1); | |||
| 1403 | ||||
| 1404 | // Create socket #2 | |||
| 1405 | IOAddress lo_addr("127.0.0.1"); | |||
| 1406 | int socket2 = 0; | |||
| 1407 | 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_1409; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1409 ; } } else gtest_label_testnothrow_1409 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1409, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1408 | 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_1409; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1409 ; } } else gtest_label_testnothrow_1409 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1409, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1409 | )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_1409; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1409 ; } } else gtest_label_testnothrow_1409 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1409, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1410 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1410, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1411 | init_sockets.push_back(socket2); | |||
| 1412 | ||||
| 1413 | // Get loopback interface. If we don't find one we are unable to run | |||
| 1414 | // this test but we don't want to fail. | |||
| 1415 | IfacePtr iface_ptr = ifacemgr->getIface(LOOPBACK_NAME); | |||
| 1416 | if (iface_ptr == NULL__null) { | |||
| 1417 | cout << "Local loopback interface not found. Skipping test. " << endl; | |||
| 1418 | return; | |||
| 1419 | } | |||
| 1420 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1420, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1421 | // Once sockets have been successfully opened, they are supposed to | |||
| 1422 | // be on the list. Here we start to test if all expected sockets | |||
| 1423 | // are on the list and no other (unexpected) socket is there. | |||
| 1424 | Iface::SocketCollection sockets = iface_ptr->getSockets(); | |||
| 1425 | int matched_sockets = 0; | |||
| 1426 | for (std::list<uint16_t>::iterator init_sockets_it = | |||
| 1427 | init_sockets.begin(); | |||
| 1428 | init_sockets_it != init_sockets.end(); ++init_sockets_it) { | |||
| 1429 | // Set socket descriptors non blocking in order to be able | |||
| 1430 | // to call recv() on them without hang. | |||
| 1431 | int flags = fcntl(*init_sockets_it, F_GETFL3, 0); | |||
| 1432 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1432, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1433 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1433, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1434 | // recv() is expected to result in EWOULDBLOCK error on non-blocking | |||
| 1435 | // socket in case socket is valid but simply no data are coming in. | |||
| 1436 | char buf; | |||
| 1437 | recv(*init_sockets_it, &buf, 1, MSG_PEEKMSG_PEEK); | |||
| 1438 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1438, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1439 | // Apart from the ability to use the socket we want to make | |||
| 1440 | // sure that socket on the list is the one that we created. | |||
| 1441 | for (Iface::SocketCollection::const_iterator socket_it = | |||
| 1442 | sockets.begin(); socket_it != sockets.end(); ++socket_it) { | |||
| 1443 | if (*init_sockets_it == socket_it->sockfd_) { | |||
| 1444 | // This socket is the one that we created. | |||
| 1445 | ++matched_sockets; | |||
| 1446 | break; | |||
| 1447 | } | |||
| 1448 | } | |||
| 1449 | } | |||
| 1450 | // All created sockets have been matched if this condition works. | |||
| 1451 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1451, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1452 | ||||
| 1453 | // closeSockets() is the other function that we want to test. It | |||
| 1454 | // is supposed to close all sockets so as we will not be able to use | |||
| 1455 | // them anymore communication. | |||
| 1456 | ifacemgr->closeSockets(); | |||
| 1457 | ||||
| 1458 | // Closed sockets are supposed to be removed from the list | |||
| 1459 | sockets = iface_ptr->getSockets(); | |||
| 1460 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1460, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1461 | ||||
| 1462 | // We are still in possession of socket descriptors that we created | |||
| 1463 | // on the beginning of this test. We can use them to check whether | |||
| 1464 | // closeSockets() only removed them from the list or they have been | |||
| 1465 | // really closed. | |||
| 1466 | for (std::list<uint16_t>::const_iterator init_sockets_it = | |||
| 1467 | init_sockets.begin(); | |||
| 1468 | init_sockets_it != init_sockets.end(); ++init_sockets_it) { | |||
| 1469 | // recv() must result in error when using invalid socket. | |||
| 1470 | char buf; | |||
| 1471 | static_cast<void>(recv(*init_sockets_it, &buf, 1, MSG_PEEKMSG_PEEK)); | |||
| 1472 | // EWOULDBLOCK would mean that socket is valid/open but | |||
| 1473 | // simply no data is received so we have to check for | |||
| 1474 | // other errors. | |||
| 1475 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1475, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1476 | } | |||
| 1477 | } | |||
| 1478 | ||||
| 1479 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceMgrTest_sockets6_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "sockets6" , nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1479), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1479), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1479), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_sockets6_Test >); void IfaceMgrTest_sockets6_Test::TestBody() { | |||
| 1480 | // Testing socket operation in a portable way is tricky | |||
| 1481 | // without interface detection implemented. | |||
| 1482 | ||||
| 1483 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1484 | ||||
| 1485 | IOAddress lo_addr("::1"); | |||
| 1486 | ||||
| 1487 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, 123)); | |||
| 1488 | pkt6->setIface(LOOPBACK_NAME); | |||
| 1489 | ||||
| 1490 | // Bind multicast socket to port 10547 | |||
| 1491 | int socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); | |||
| 1492 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1492, gtest_ar.failure_message()) = ::testing::Message(); // socket >= 0 | |||
| 1493 | ||||
| 1494 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1494, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1495 | ||||
| 1496 | // Bind unicast socket to port 10548 | |||
| 1497 | int socket2 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10548); | |||
| 1498 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1498, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1499 | ||||
| 1500 | // Removed code for binding socket twice to the same address/port | |||
| 1501 | // as it caused problems on some platforms (e.g. Mac OS X) | |||
| 1502 | ||||
| 1503 | // Close sockets here because the following tests will want to | |||
| 1504 | // open sockets on the same ports. | |||
| 1505 | ifacemgr->closeSockets(); | |||
| 1506 | ||||
| 1507 | // Use address that is not assigned to LOOPBACK iface. | |||
| 1508 | IOAddress invalidAddr("::2"); | |||
| 1509 | 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_1512 ; } 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_1512; } 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_1512; } } else gtest_label_testthrow_1512 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1512, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1510 | 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_1512 ; } 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_1512; } 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_1512; } } else gtest_label_testthrow_1512 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1512, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1511 | 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_1512 ; } 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_1512; } 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_1512; } } else gtest_label_testthrow_1512 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1512, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1512 | )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_1512 ; } 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_1512; } 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_1512; } } else gtest_label_testthrow_1512 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1512, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1513 | ||||
| 1514 | // Use non-existing interface name. | |||
| 1515 | 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_1518 ; } 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_1518; } 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_1518; } } else gtest_label_testthrow_1518 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1518, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1516 | 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_1518 ; } 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_1518; } 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_1518; } } else gtest_label_testthrow_1518 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1518, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1517 | 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_1518 ; } 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_1518; } 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_1518; } } else gtest_label_testthrow_1518 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1518, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1518 | )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_1518 ; } 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_1518; } 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_1518; } } else gtest_label_testthrow_1518 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1518, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1519 | ||||
| 1520 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
| 1521 | // virtual destructor. | |||
| 1522 | } | |||
| 1523 | ||||
| 1524 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_socketsFromIface_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketsFromIface", nullptr, nullptr, ::testing::internal:: CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1524), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1524), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1524), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketsFromIface_Test >); void IfaceMgrTest_socketsFromIface_Test::TestBody() { | |||
| 1525 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1526 | ||||
| 1527 | // Open v6 socket on loopback interface and bind to port | |||
| 1528 | int socket1 = 0; | |||
| 1529 | 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_1531; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1531 ; } } else gtest_label_testnothrow_1531 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1531, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 10);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1530 | 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_1531; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1531 ; } } else gtest_label_testnothrow_1531 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1531, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 10);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1531 | )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_1531; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1531 ; } } else gtest_label_testnothrow_1531 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1531, ("Expected: " "socket1 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT1, 10);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1532 | // Socket descriptor must be non-negative integer | |||
| 1533 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1533, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1534 | close(socket1); | |||
| 1535 | ||||
| 1536 | // Open v4 socket on loopback interface and bind to different port | |||
| 1537 | int socket2 = 0; | |||
| 1538 | 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_1540; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1540 ; } } else gtest_label_testnothrow_1540 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1540, ("Expected: " "socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1539 | 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_1540; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1540 ; } } else gtest_label_testnothrow_1540 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1540, ("Expected: " "socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1540 | )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_1540; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1540 ; } } else gtest_label_testnothrow_1540 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1540, ("Expected: " "socket2 = ifacemgr->openSocketFromIface(LOOPBACK_NAME, PORT2, 2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1541 | // socket descriptor must be non-negative integer | |||
| 1542 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1542, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1543 | close(socket2); | |||
| 1544 | ||||
| 1545 | // Close sockets here because the following tests will want to | |||
| 1546 | // open sockets on the same ports. | |||
| 1547 | ifacemgr->closeSockets(); | |||
| 1548 | ||||
| 1549 | // Use invalid interface name. | |||
| 1550 | 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_1553 ; } 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_1553; } 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_1553; } } else gtest_label_testthrow_1553 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1553, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1551 | 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_1553 ; } 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_1553; } 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_1553; } } else gtest_label_testthrow_1553 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1553, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1552 | 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_1553 ; } 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_1553; } 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_1553; } } else gtest_label_testthrow_1553 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1553, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1553 | )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_1553 ; } 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_1553; } 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_1553; } } else gtest_label_testthrow_1553 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1553, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1554 | ||||
| 1555 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
| 1556 | // virtual destructor. | |||
| 1557 | } | |||
| 1558 | ||||
| 1559 | ||||
| 1560 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_socketsFromAddress_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketsFromAddress", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1560), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1560), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1560), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketsFromAddress_Test >); void IfaceMgrTest_socketsFromAddress_Test::TestBody() { | |||
| 1561 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1562 | ||||
| 1563 | // Open v6 socket on loopback interface and bind to port | |||
| 1564 | int socket1 = 0; | |||
| 1565 | IOAddress lo_addr6("::1"); | |||
| 1566 | 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_1568; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1568 ; } } else gtest_label_testnothrow_1568 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1568, ("Expected: " "socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1567 | 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_1568; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1568 ; } } else gtest_label_testnothrow_1568 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1568, ("Expected: " "socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1568 | )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_1568; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1568 ; } } else gtest_label_testnothrow_1568 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1568, ("Expected: " "socket1 = ifacemgr->openSocketFromAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1569 | // socket descriptor must be non-negative integer | |||
| 1570 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1570, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1571 | ||||
| 1572 | // Open v4 socket on loopback interface and bind to different port | |||
| 1573 | int socket2 = 0; | |||
| 1574 | IOAddress lo_addr("127.0.0.1"); | |||
| 1575 | 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_1577; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1577 ; } } else gtest_label_testnothrow_1577 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1577, ("Expected: " "socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1576 | 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_1577; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1577 ; } } else gtest_label_testnothrow_1577 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1577, ("Expected: " "socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1577 | )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_1577; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1577 ; } } else gtest_label_testnothrow_1577 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1577, ("Expected: " "socket2 = ifacemgr->openSocketFromAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1578 | // socket descriptor must be positive integer | |||
| 1579 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1579, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1580 | ||||
| 1581 | // Close sockets here because the following tests will want to | |||
| 1582 | // open sockets on the same ports. | |||
| 1583 | ifacemgr->closeSockets(); | |||
| 1584 | ||||
| 1585 | // Use non-existing address. | |||
| 1586 | IOAddress invalidAddr("1.2.3.4"); | |||
| 1587 | 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_1589 ; } 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_1589; } 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_1589; } } else gtest_label_testthrow_1589 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1589, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1588 | 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_1589 ; } 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_1589; } 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_1589; } } else gtest_label_testthrow_1589 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1589, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1589 | )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_1589 ; } 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_1589; } 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_1589; } } else gtest_label_testthrow_1589 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1589, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1590 | ||||
| 1591 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
| 1592 | // virtual destructor. | |||
| 1593 | } | |||
| 1594 | ||||
| 1595 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_socketsFromRemoteAddress_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketsFromRemoteAddress", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1595), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1595), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1595), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketsFromRemoteAddress_Test >); void IfaceMgrTest_socketsFromRemoteAddress_Test::TestBody () { | |||
| 1596 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1597 | ||||
| 1598 | // Open v6 socket to connect to remote address. | |||
| 1599 | // Loopback address is the only one that we know | |||
| 1600 | // so let's treat it as remote address. | |||
| 1601 | int socket1 = 0; | |||
| 1602 | IOAddress lo_addr6("::1"); | |||
| 1603 | 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_1605; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1605 ; } } else gtest_label_testnothrow_1605 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1605, ("Expected: " "socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1604 | 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_1605; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1605 ; } } else gtest_label_testnothrow_1605 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1605, ("Expected: " "socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1605 | )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_1605; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1605 ; } } else gtest_label_testnothrow_1605 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1605, ("Expected: " "socket1 = ifacemgr->openSocketFromRemoteAddress(lo_addr6, PORT1);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1606 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1606, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1607 | ||||
| 1608 | // Open v4 socket to connect to remote address. | |||
| 1609 | int socket2 = 0; | |||
| 1610 | IOAddress lo_addr("127.0.0.1"); | |||
| 1611 | 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_1613; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1613 ; } } else gtest_label_testnothrow_1613 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1613, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1612 | 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_1613; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1613 ; } } else gtest_label_testnothrow_1613 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1613, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 1613 | )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_1613; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1613 ; } } else gtest_label_testnothrow_1613 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1613, ("Expected: " "socket2 = ifacemgr->openSocketFromRemoteAddress(lo_addr, PORT2);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1614 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1614, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1615 | ||||
| 1616 | // Close sockets here because the following tests will want to | |||
| 1617 | // open sockets on the same ports. | |||
| 1618 | ifacemgr->closeSockets(); | |||
| 1619 | ||||
| 1620 | // There used to be a check here that verified the ability to open | |||
| 1621 | // suitable socket for sending broadcast request. However, | |||
| 1622 | // there is no guarantee for such test to work on all systems | |||
| 1623 | // because some systems may have no broadcast capable interfaces at all. | |||
| 1624 | // Thus, this check has been removed. | |||
| 1625 | ||||
| 1626 | // Do not call closeSockets() because it is called by IfaceMgr's | |||
| 1627 | // virtual destructor. | |||
| 1628 | } | |||
| 1629 | ||||
| 1630 | // TODO: disabled due to other naming on various systems | |||
| 1631 | // (lo in Linux, lo0 in BSD systems) | |||
| 1632 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_DISABLED_sockets6Mcast_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DISABLED_sockets6Mcast", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1632), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1632), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1632), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DISABLED_sockets6Mcast_Test >); void IfaceMgrTest_DISABLED_sockets6Mcast_Test::TestBody () { | |||
| 1633 | // testing socket operation in a portable way is tricky | |||
| 1634 | // without interface detection implemented | |||
| 1635 | ||||
| 1636 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1637 | ||||
| 1638 | IOAddress lo_addr("::1"); | |||
| 1639 | IOAddress mcastAddr("ff02::1:2"); | |||
| 1640 | ||||
| 1641 | // bind multicast socket to port 10547 | |||
| 1642 | int socket1 = ifacemgr->openSocket(LOOPBACK_NAME, mcastAddr, 10547); | |||
| 1643 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1643, gtest_ar.failure_message()) = ::testing::Message(); // socket > 0 | |||
| 1644 | ||||
| 1645 | // expect success. This address/port is already bound, but | |||
| 1646 | // we are using SO_REUSEADDR, so we can bind it twice | |||
| 1647 | int socket2 = ifacemgr->openSocket(LOOPBACK_NAME, mcastAddr, 10547); | |||
| 1648 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1648, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1649 | ||||
| 1650 | // there's no good way to test negative case here. | |||
| 1651 | // we would need non-multicast interface. We will be able | |||
| 1652 | // to iterate thru available interfaces and check if there | |||
| 1653 | // are interfaces without multicast-capable flag. | |||
| 1654 | ||||
| 1655 | close(socket1); | |||
| 1656 | close(socket2); | |||
| 1657 | } | |||
| 1658 | ||||
| 1659 | // Verifies that basic DHCPv6 packet send and receive operates | |||
| 1660 | // in either direct or indirect mode. | |||
| 1661 | // No poll version as it depends on select() behavior. | |||
| 1662 | TEST_F(IfaceMgrTest, sendReceive6Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("sendReceive6Select") > 1, "test_name must not be empty" ); class IfaceMgrTest_sendReceive6Select_Test : public IfaceMgrTest { public: IfaceMgrTest_sendReceive6Select_Test() = default; ~ IfaceMgrTest_sendReceive6Select_Test() override = default; IfaceMgrTest_sendReceive6Select_Test (const IfaceMgrTest_sendReceive6Select_Test &) = delete; IfaceMgrTest_sendReceive6Select_Test & operator=( const IfaceMgrTest_sendReceive6Select_Test &) = delete; IfaceMgrTest_sendReceive6Select_Test (IfaceMgrTest_sendReceive6Select_Test &&) noexcept = delete; IfaceMgrTest_sendReceive6Select_Test & operator=( IfaceMgrTest_sendReceive6Select_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_sendReceive6Select_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "sendReceive6Select", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1662), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1662), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1662), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_sendReceive6Select_Test >); void IfaceMgrTest_sendReceive6Select_Test::TestBody() { | |||
| 1663 | kea_event_handler_type_.setValue("select"); | |||
| 1664 | data::ElementPtr queue_control; | |||
| 1665 | ||||
| 1666 | // Given an empty pointer, queueing should be disabled. | |||
| 1667 | // This should do direct reception. | |||
| 1668 | sendReceive6Test(queue_control, false); | |||
| 1669 | ||||
| 1670 | // Now let's populate queue control. | |||
| 1671 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, false); | |||
| 1672 | // With queueing disabled, we should use direct reception. | |||
| 1673 | sendReceive6Test(queue_control, false); | |||
| 1674 | ||||
| 1675 | // Queuing enabled, indirection reception should work. | |||
| 1676 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, true); | |||
| 1677 | sendReceive6Test(queue_control, true); | |||
| 1678 | } | |||
| 1679 | ||||
| 1680 | // Verifies that basic DHCPv4 packet send and receive operates | |||
| 1681 | // in either direct or indirect mode. | |||
| 1682 | // No poll version as it depends on select() behavior. | |||
| 1683 | TEST_F(IfaceMgrTest, sendReceive4Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("sendReceive4Select") > 1, "test_name must not be empty" ); class IfaceMgrTest_sendReceive4Select_Test : public IfaceMgrTest { public: IfaceMgrTest_sendReceive4Select_Test() = default; ~ IfaceMgrTest_sendReceive4Select_Test() override = default; IfaceMgrTest_sendReceive4Select_Test (const IfaceMgrTest_sendReceive4Select_Test &) = delete; IfaceMgrTest_sendReceive4Select_Test & operator=( const IfaceMgrTest_sendReceive4Select_Test &) = delete; IfaceMgrTest_sendReceive4Select_Test (IfaceMgrTest_sendReceive4Select_Test &&) noexcept = delete; IfaceMgrTest_sendReceive4Select_Test & operator=( IfaceMgrTest_sendReceive4Select_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_sendReceive4Select_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "sendReceive4Select", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1683), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1683), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1683), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_sendReceive4Select_Test >); void IfaceMgrTest_sendReceive4Select_Test::TestBody() { | |||
| 1684 | kea_event_handler_type_.setValue("select"); | |||
| 1685 | data::ElementPtr queue_control; | |||
| 1686 | ||||
| 1687 | // Given an empty pointer, queueing should be disabled. | |||
| 1688 | // This should do direct reception. | |||
| 1689 | sendReceive4Test(queue_control, false); | |||
| 1690 | ||||
| 1691 | // Now let's populate queue control. | |||
| 1692 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, false); | |||
| 1693 | // With queueing disabled, we should use direct reception. | |||
| 1694 | sendReceive4Test(queue_control, false); | |||
| 1695 | ||||
| 1696 | // Queuing enabled, indirection reception should work. | |||
| 1697 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, true); | |||
| 1698 | sendReceive4Test(queue_control, true); | |||
| 1699 | } | |||
| 1700 | ||||
| 1701 | // Verifies that it is possible to set custom packet filter object | |||
| 1702 | // to handle sockets opening and send/receive operation. | |||
| 1703 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_setPacketFilter_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setPacketFilter", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1703), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1703), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1703), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setPacketFilter_Test >); void IfaceMgrTest_setPacketFilter_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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1707, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr", "false", "true") .c_str()) = ::testing::Message (); | |||
| 1708 | ||||
| 1709 | // Try to set NULL packet filter object and make sure it is rejected. | |||
| 1710 | boost::shared_ptr<TestPktFilter> custom_packet_filter; | |||
| 1711 | 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_1712; } 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_1712 ; } 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_1712; } } else gtest_label_testthrow_1712 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1712, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1712 | 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_1712; } 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_1712 ; } 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_1712; } } else gtest_label_testthrow_1712 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1712, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1713 | ||||
| 1714 | // Create valid object and check if it can be set. | |||
| 1715 | custom_packet_filter.reset(new TestPktFilter()); | |||
| 1716 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1716, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1717 | 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_1717; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1717; } } else gtest_label_testnothrow_1717 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1717, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1718 | ||||
| 1719 | // Try to open socket using IfaceMgr. It should call the openSocket() function | |||
| 1720 | // on the packet filter object we have set. | |||
| 1721 | IOAddress lo_addr("127.0.0.1"); | |||
| 1722 | int socket1 = 0; | |||
| 1723 | 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_1726; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1726 ; } } else gtest_label_testnothrow_1726 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1726, ("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() | |||
| 1724 | 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_1726; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1726 ; } } else gtest_label_testnothrow_1726 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1726, ("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() | |||
| 1725 | 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_1726; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1726 ; } } else gtest_label_testnothrow_1726 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1726, ("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() | |||
| 1726 | )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_1726; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1726 ; } } else gtest_label_testnothrow_1726 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1726, ("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(); | |||
| 1727 | ||||
| 1728 | // Check that openSocket function was called. | |||
| 1729 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1729, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter->open_socket_called_", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 1730 | // This function always returns fake socket descriptor equal to 255. | |||
| 1731 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1731, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1732 | ||||
| 1733 | // Replacing current packet filter object while there are IPv4 | |||
| 1734 | // sockets open is not allowed! | |||
| 1735 | 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_1736; } 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_1736; } 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_1736; } } else gtest_label_testthrow_1736 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1736, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1736 | 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_1736; } 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_1736; } 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_1736; } } else gtest_label_testthrow_1736 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1736, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1737 | ||||
| 1738 | // So, let's close the open sockets and retry. Now it should succeed. | |||
| 1739 | iface_mgr->closeSockets(); | |||
| 1740 | 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_1740; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1740; } } else gtest_label_testnothrow_1740 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1740, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1741 | } | |||
| 1742 | ||||
| 1743 | // This test checks that the default packet filter for DHCPv6 can be replaced | |||
| 1744 | // with the custom one. | |||
| 1745 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_setPacketFilter6_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setPacketFilter6", nullptr, nullptr, ::testing::internal:: CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1745), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1745), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1745), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setPacketFilter6_Test >); void IfaceMgrTest_setPacketFilter6_Test::TestBody() { | |||
| 1746 | // Create an instance of IfaceMgr. | |||
| 1747 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr(new NakedIfaceMgr()); | |||
| 1748 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1748, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr", "false", "true") .c_str()) = ::testing::Message (); | |||
| 1749 | ||||
| 1750 | // Try to set NULL packet filter object and make sure it is rejected. | |||
| 1751 | boost::shared_ptr<PktFilter6Stub> custom_packet_filter; | |||
| 1752 | 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_1753; } 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_1753 ; } 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_1753; } } else gtest_label_testthrow_1753 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1753, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1753 | 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_1753; } 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_1753 ; } 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_1753; } } else gtest_label_testthrow_1753 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1753, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1754 | ||||
| 1755 | // Create valid object and check if it can be set. | |||
| 1756 | custom_packet_filter.reset(new PktFilter6Stub()); | |||
| 1757 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1757, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1758 | 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_1758; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1758; } } else gtest_label_testnothrow_1758 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1758, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1759 | ||||
| 1760 | // Try to open socket using IfaceMgr. It should call the openSocket() | |||
| 1761 | // function on the packet filter object we have set. | |||
| 1762 | IOAddress lo_addr("::1"); | |||
| 1763 | int socket1 = 0; | |||
| 1764 | 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_1767; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1767 ; } } else gtest_label_testnothrow_1767 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1767, ("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() | |||
| 1765 | 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_1767; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1767 ; } } else gtest_label_testnothrow_1767 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1767, ("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() | |||
| 1766 | 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_1767; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1767 ; } } else gtest_label_testnothrow_1767 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1767, ("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() | |||
| 1767 | )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_1767; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1767 ; } } else gtest_label_testnothrow_1767 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1767, ("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(); | |||
| 1768 | // Check that openSocket function has been actually called on the packet | |||
| 1769 | // filter object. | |||
| 1770 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1770, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 1771 | // Also check that the returned socket descriptor has an expected value. | |||
| 1772 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1772, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1773 | ||||
| 1774 | // Replacing current packet filter object, while there are sockets open, | |||
| 1775 | // is not allowed! | |||
| 1776 | 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_1777; } 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_1777; } 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_1777; } } else gtest_label_testthrow_1777 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1777, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1777 | 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_1777; } 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_1777; } 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_1777; } } else gtest_label_testthrow_1777 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1777, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1778 | ||||
| 1779 | // So, let's close the sockets and retry. Now it should succeed. | |||
| 1780 | iface_mgr->closeSockets(); | |||
| 1781 | 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_1781; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1781; } } else gtest_label_testnothrow_1781 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1781, ("Expected: " "iface_mgr->setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1782 | } | |||
| 1783 | ||||
| 1784 | #if defined(OS_LINUX) || defined(OS_BSD) | |||
| 1785 | ||||
| 1786 | // This test is only supported on Linux and BSD systems. It checks | |||
| 1787 | // if it is possible to use the IfaceMgr to select the packet filter | |||
| 1788 | // object which can be used to send direct responses to the host | |||
| 1789 | // which doesn't have an address yet. | |||
| 1790 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_setMatchingPacketFilter_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setMatchingPacketFilter", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1790), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1790), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1790), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setMatchingPacketFilter_Test >); void IfaceMgrTest_setMatchingPacketFilter_Test::TestBody () { | |||
| 1791 | ||||
| 1792 | // Create an instance of IfaceMgr. | |||
| 1793 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr(new NakedIfaceMgr()); | |||
| 1794 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1794, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr", "false", "true") .c_str()) = ::testing::Message (); | |||
| 1795 | ||||
| 1796 | // Let IfaceMgr figure out which Packet Filter to use when | |||
| 1797 | // direct response capability is not desired. It should pick | |||
| 1798 | // PktFilterInet on Linux. | |||
| 1799 | 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_1799; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1799; } } else gtest_label_testnothrow_1799 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1799, ("Expected: " "iface_mgr->setMatchingPacketFilter(false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1800 | // The PktFilterInet is supposed to report lack of direct | |||
| 1801 | // response capability. | |||
| 1802 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1802, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr->isDirectResponseSupported()", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 1803 | ||||
| 1804 | // There is working implementation of direct responses on Linux | |||
| 1805 | // and BSD (using PktFilterLPF and PktFilterBPF. When direct | |||
| 1806 | // responses are desired the object of this class should be set. | |||
| 1807 | 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_1807; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1807; } } else gtest_label_testnothrow_1807 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1807, ("Expected: " "iface_mgr->setMatchingPacketFilter(true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1808 | // This object should report that direct responses are supported. | |||
| 1809 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1809, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr->isDirectResponseSupported()", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 1810 | } | |||
| 1811 | ||||
| 1812 | // This test checks that it is not possible to open two sockets: IP/UDP | |||
| 1813 | // and raw socket and bind to the same address and port. The | |||
| 1814 | // raw socket should be opened together with the fallback IP/UDP socket. | |||
| 1815 | // The fallback socket should fail to open when there is another IP/UDP | |||
| 1816 | // socket bound to the same address and port. Failing to open the fallback | |||
| 1817 | // socket should preclude the raw socket from being open. | |||
| 1818 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_checkPacketFilterRawSocket_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "checkPacketFilterRawSocket", nullptr, nullptr, ::testing:: internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1818), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1818), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1818), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_checkPacketFilterRawSocket_Test >); void IfaceMgrTest_checkPacketFilterRawSocket_Test::TestBody () { | |||
| 1819 | IOAddress lo_addr("127.0.0.1"); | |||
| 1820 | int socket1 = -1, socket2 = -1; | |||
| 1821 | // Create two instances of IfaceMgr. | |||
| 1822 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr1(new NakedIfaceMgr()); | |||
| 1823 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1823, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr1", "false", "true") .c_str()) = ::testing::Message (); | |||
| 1824 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr2(new NakedIfaceMgr()); | |||
| 1825 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1825, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr2", "false", "true") .c_str()) = ::testing::Message (); | |||
| 1826 | ||||
| 1827 | // Let IfaceMgr figure out which Packet Filter to use when | |||
| 1828 | // direct response capability is not desired. It should pick | |||
| 1829 | // PktFilterInet. | |||
| 1830 | 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_1830; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1830; } } else gtest_label_testnothrow_1830 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1830, ("Expected: " "iface_mgr1->setMatchingPacketFilter(false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1831 | // Let's open a loopback socket with handy unpriviliged port number | |||
| 1832 | socket1 = iface_mgr1->openSocket(LOOPBACK_NAME, lo_addr, | |||
| 1833 | DHCP4_SERVER_PORT + 10000); | |||
| 1834 | ||||
| 1835 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1835, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1836 | ||||
| 1837 | // Then the second use PkFilterLPF mode | |||
| 1838 | 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_1838; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1838; } } else gtest_label_testnothrow_1838 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1838, ("Expected: " "iface_mgr2->setMatchingPacketFilter(true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1839 | ||||
| 1840 | // The socket is open and bound. Another attempt to open socket and | |||
| 1841 | // bind to the same address and port should result in an exception. | |||
| 1842 | 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_1846; } 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_1846 ; } 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_1846 ; } } else gtest_label_testthrow_1846 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1846, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1843 | 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_1846; } 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_1846 ; } 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_1846 ; } } else gtest_label_testthrow_1846 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1846, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1844 | 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_1846; } 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_1846 ; } 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_1846 ; } } else gtest_label_testthrow_1846 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1846, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1845 | 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_1846; } 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_1846 ; } 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_1846 ; } } else gtest_label_testthrow_1846 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1846, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1846 | )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_1846; } 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_1846 ; } 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_1846 ; } } else gtest_label_testthrow_1846 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1846, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 1847 | // Surprisingly we managed to open another socket. We have to close it | |||
| 1848 | // to prevent resource leak. | |||
| 1849 | if (socket2 >= 0) { | |||
| 1850 | close(socket2); | |||
| 1851 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1851, "Failed") = ::testing::Message() << "Two sockets opened and bound to the same IP" | |||
| 1852 | " address and UDP port"; | |||
| 1853 | } | |||
| 1854 | ||||
| 1855 | if (socket1 >= 0) { | |||
| 1856 | close(socket1); | |||
| 1857 | } | |||
| 1858 | } | |||
| 1859 | ||||
| 1860 | #else | |||
| 1861 | ||||
| 1862 | // Note: This test will only run on non-Linux and non-BSD systems. | |||
| 1863 | // This test checks whether it is possible to use IfaceMgr to figure | |||
| 1864 | // out which Packet Filter object should be used when direct responses | |||
| 1865 | // to hosts, having no address assigned are desired or not desired. | |||
| 1866 | // Since direct responses aren't supported on systems other than Linux | |||
| 1867 | // and BSD the function under test should always set object of | |||
| 1868 | // PktFilterInet type as current Packet Filter. This object does not | |||
| 1869 | //support direct responses. Once implementation is added on systems | |||
| 1870 | // other than BSD and Linux the OS specific version of the test will | |||
| 1871 | // be removed. | |||
| 1872 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_setMatchingPacketFilter_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "setMatchingPacketFilter", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1872), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1872), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1872), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_setMatchingPacketFilter_Test >); void IfaceMgrTest_setMatchingPacketFilter_Test::TestBody () { | |||
| 1873 | ||||
| 1874 | // Create an instance of IfaceMgr. | |||
| 1875 | boost::scoped_ptr<NakedIfaceMgr> iface_mgr(new NakedIfaceMgr()); | |||
| 1876 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1876, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr", "false", "true") .c_str()) = ::testing::Message (); | |||
| 1877 | ||||
| 1878 | // Let IfaceMgr figure out which Packet Filter to use when | |||
| 1879 | // direct response capability is not desired. It should pick | |||
| 1880 | // PktFilterInet. | |||
| 1881 | 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_1881; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1881; } } else gtest_label_testnothrow_1881 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1881, ("Expected: " "iface_mgr->setMatchingPacketFilter(false)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1882 | // The PktFilterInet is supposed to report lack of direct | |||
| 1883 | // response capability. | |||
| 1884 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1884, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr->isDirectResponseSupported()", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 1885 | ||||
| 1886 | // On non-Linux systems, we are missing the direct traffic | |||
| 1887 | // implementation. Therefore, we expect that PktFilterInet | |||
| 1888 | // object will be set. | |||
| 1889 | 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_1889; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1889; } } else gtest_label_testnothrow_1889 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1889, ("Expected: " "iface_mgr->setMatchingPacketFilter(true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1890 | // This object should report lack of direct response capability. | |||
| 1891 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1891, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface_mgr->isDirectResponseSupported()", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 1892 | } | |||
| 1893 | ||||
| 1894 | #endif | |||
| 1895 | ||||
| 1896 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceMgrTest_socket4_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "socket4" , nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1896), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1896), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1896), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socket4_Test >); void IfaceMgrTest_socket4_Test::TestBody() { | |||
| 1897 | ||||
| 1898 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 1899 | ||||
| 1900 | // Let's assume that every supported OS have lo interface. | |||
| 1901 | IOAddress lo_addr("127.0.0.1"); | |||
| 1902 | // Use unprivileged port (it's convenient for running tests as non-root). | |||
| 1903 | int socket1 = 0; | |||
| 1904 | ||||
| 1905 | 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_1908; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1908 ; } } else gtest_label_testnothrow_1908 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1908, ("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() | |||
| 1906 | 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_1908; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1908 ; } } else gtest_label_testnothrow_1908 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1908, ("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() | |||
| 1907 | 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_1908; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1908 ; } } else gtest_label_testnothrow_1908 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1908, ("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() | |||
| 1908 | )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_1908; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1908 ; } } else gtest_label_testnothrow_1908 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1908, ("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(); | |||
| 1909 | ||||
| 1910 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1910, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1911 | ||||
| 1912 | Pkt4Ptr pkt(new Pkt4(DHCPDISCOVER, 1234)); | |||
| 1913 | pkt->setIface(LOOPBACK_NAME); | |||
| 1914 | pkt->setIndex(LOOPBACK_INDEX); | |||
| 1915 | ||||
| 1916 | // Expect that we get the socket that we just opened. | |||
| 1917 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1917, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1918 | ||||
| 1919 | close(socket1); | |||
| 1920 | } | |||
| 1921 | ||||
| 1922 | // This test verifies that IPv4 sockets are open on all interfaces (except | |||
| 1923 | // loopback), when interfaces are up, running and active (not disabled from | |||
| 1924 | // the DHCP configuration). | |||
| 1925 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_openSockets4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1925), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1925), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1925), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4_Test >); void IfaceMgrTest_openSockets4_Test::TestBody() { | |||
| 1926 | NakedIfaceMgr ifacemgr; | |||
| 1927 | ||||
| 1928 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 1929 | ifacemgr.createIfaces(); | |||
| 1930 | ||||
| 1931 | // Use the custom packet filter object. This object mimics the socket | |||
| 1932 | // opening operation - the real socket is not open. | |||
| 1933 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 1934 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1934, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1935 | 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_1935; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1935; } } else gtest_label_testnothrow_1935 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1935, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1936 | ||||
| 1937 | // Simulate opening sockets using the dummy packet filter. | |||
| 1938 | 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_1938; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1938; } } else gtest_label_testnothrow_1938 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1938, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1939 | ||||
| 1940 | // Expect that the sockets are open on both eth0 and eth1. | |||
| 1941 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1941, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1942 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1942, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1943 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1943, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1944 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1944, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1945 | // Socket shouldn't have been opened on loopback. | |||
| 1946 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1946, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"lo\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 1947 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1947, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(LO_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 1948 | } | |||
| 1949 | ||||
| 1950 | // This test verifies that IPv4 sockets are open on the loopback interface | |||
| 1951 | // when the loopback is active and allowed. | |||
| 1952 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets4Loopback_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4Loopback", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1952), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1952), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1952), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4Loopback_Test >); void IfaceMgrTest_openSockets4Loopback_Test::TestBody( ) { | |||
| 1953 | NakedIfaceMgr ifacemgr; | |||
| 1954 | ||||
| 1955 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 1956 | ifacemgr.createIfaces(); | |||
| 1957 | ||||
| 1958 | // Allow the loopback interface. | |||
| 1959 | ifacemgr.setAllowLoopBack(true); | |||
| 1960 | ||||
| 1961 | // Make the loopback interface active. | |||
| 1962 | ifacemgr.getIface("lo")->inactive4_ = false; | |||
| 1963 | ||||
| 1964 | // Use the custom packet filter object. This object mimics the socket | |||
| 1965 | // opening operation - the real socket is not open. | |||
| 1966 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 1967 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1967, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 1968 | 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_1968; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1968; } } else gtest_label_testnothrow_1968 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1968, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1969 | ||||
| 1970 | // Simulate opening sockets using the dummy packet filter. | |||
| 1971 | 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_1971; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1971; } } else gtest_label_testnothrow_1971 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1971, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 1972 | ||||
| 1973 | // Expect that the sockets are open on all interfaces. | |||
| 1974 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1974, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1975 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1975, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1976 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1976, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1977 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1977, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1978 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1978, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1979 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1979, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 1980 | } | |||
| 1981 | ||||
| 1982 | // This test verifies that the socket is not open on the interface which is | |||
| 1983 | // down, but sockets are open on all other non-loopback interfaces. | |||
| 1984 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets4IfaceDown_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4IfaceDown", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1984), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1984), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1984), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4IfaceDown_Test >); void IfaceMgrTest_openSockets4IfaceDown_Test::TestBody () { | |||
| 1985 | IfaceMgrTestConfig config(true); | |||
| 1986 | ||||
| 1987 | // Boolean parameters specify that eth0 is: | |||
| 1988 | // - not a loopback | |||
| 1989 | // - is "down" (not up) | |||
| 1990 | // - is not running | |||
| 1991 | // - is active (is not inactive) | |||
| 1992 | config.setIfaceFlags("eth0", FlagLoopback(false), FlagUp(false), | |||
| 1993 | FlagRunning(false), FlagInactive4(false), | |||
| 1994 | FlagInactive6(false)); | |||
| 1995 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 1995, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "IfaceMgr::instance().getIface(\"eth0\")->flag_up_", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 1996 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 1996, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "IfaceMgr::instance().getIface(ETH0_INDEX)->flag_up_", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 1997 | ||||
| 1998 | // Install an error handler before trying to open sockets. This handler | |||
| 1999 | // should be called when the IfaceMgr fails to open socket on an interface | |||
| 2000 | // on which the server is configured to listen. | |||
| 2001 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
| 2002 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
| 2003 | ||||
| 2004 | 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_2005; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2005 ; } } else gtest_label_testnothrow_2005 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2005, ("Expected: " "IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 2005 | 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_2005; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2005 ; } } else gtest_label_testnothrow_2005 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2005, ("Expected: " "IfaceMgr::instance().openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2006 | // Since the interface is down, an attempt to open a socket should result | |||
| 2007 | // in error. | |||
| 2008 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2008, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2009 | ||||
| 2010 | // There should be no socket on eth0 open, because interface was down. | |||
| 2011 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2011, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "IfaceMgr::instance().getIface(\"eth0\")->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2012 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2012, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "IfaceMgr::instance().getIface(ETH0_INDEX)->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2013 | ||||
| 2014 | // Expecting that the socket is open on eth1 because it was up, running | |||
| 2015 | // and active. | |||
| 2016 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2016, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2017 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2017, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2018 | // Same for eth1961. | |||
| 2019 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2019, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2020 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2020, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2021 | // Never open socket on loopback interface. | |||
| 2022 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2022, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "IfaceMgr::instance().getIface(\"lo\")->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2023 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2023, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "IfaceMgr::instance().getIface(LO_INDEX)->getSockets().empty()" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2024 | } | |||
| 2025 | ||||
| 2026 | // This test verifies that the socket is not open on the interface which is | |||
| 2027 | // disabled from the DHCP configuration, but sockets are open on all other | |||
| 2028 | // non-loopback interfaces. | |||
| 2029 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_openSockets4IfaceInactive_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4IfaceInactive", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2029), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2029), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2029), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4IfaceInactive_Test >); void IfaceMgrTest_openSockets4IfaceInactive_Test::TestBody () { | |||
| 2030 | NakedIfaceMgr ifacemgr; | |||
| 2031 | ||||
| 2032 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2033 | ifacemgr.createIfaces(); | |||
| 2034 | ||||
| 2035 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 2036 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2036, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 2037 | 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_2037; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2037; } } else gtest_label_testnothrow_2037 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2037, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2038 | ||||
| 2039 | // Boolean parameters specify that eth1 is: | |||
| 2040 | // - not a loopback | |||
| 2041 | // - is up | |||
| 2042 | // - is running | |||
| 2043 | // - is inactive | |||
| 2044 | ifacemgr.setIfaceFlags("eth1", false, true, true, true, false); | |||
| 2045 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2045, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth1\")->inactive4_", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2046 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2046, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH1_INDEX)->inactive4_", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2047 | 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_2047; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2047; } } else gtest_label_testnothrow_2047 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2047, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2048 | ||||
| 2049 | // The socket on eth0 should be open because interface is up, running and | |||
| 2050 | // active (not disabled through DHCP configuration, for example). | |||
| 2051 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2051, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2052 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2052, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2053 | // There should be no socket open on eth1 because it was marked inactive. | |||
| 2054 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2054, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth1\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2055 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2055, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH1_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2056 | // Sockets are not open on loopback interfaces too. | |||
| 2057 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2057, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"lo\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2058 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2058, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(LO_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2059 | } | |||
| 2060 | ||||
| 2061 | // Test that exception is thrown when trying to bind a new socket to the port | |||
| 2062 | // and address which is already in use by another socket. | |||
| 2063 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_openSockets4NoErrorHandler_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4NoErrorHandler", nullptr, nullptr, ::testing:: internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2063), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2063), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2063), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4NoErrorHandler_Test >); void IfaceMgrTest_openSockets4NoErrorHandler_Test::TestBody () { | |||
| 2064 | NakedIfaceMgr ifacemgr; | |||
| 2065 | ||||
| 2066 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2067 | ifacemgr.createIfaces(); | |||
| 2068 | ||||
| 2069 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 2070 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2070, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 2071 | 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_2071; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2071; } } else gtest_label_testnothrow_2071 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2071, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2072 | ||||
| 2073 | // Open socket on eth1. The openSockets4 should detect that this | |||
| 2074 | // socket has been already open and an attempt to open another socket | |||
| 2075 | // and bind to this address and port should fail. | |||
| 2076 | 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_2077; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2077 ; } } else gtest_label_testnothrow_2077 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2077, ("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() | |||
| 2077 | 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_2077; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2077 ; } } else gtest_label_testnothrow_2077 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2077, ("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(); | |||
| 2078 | ||||
| 2079 | // The function throws an exception when it tries to open a socket | |||
| 2080 | // and bind it to the address in use. | |||
| 2081 | 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_2082; } 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_2082 ; } 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_2082; } } else gtest_label_testthrow_2082 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2082, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2082 | 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_2082; } 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_2082 ; } 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_2082; } } else gtest_label_testthrow_2082 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2082, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2083 | } | |||
| 2084 | ||||
| 2085 | // Test that the external error handler is called when trying to bind a new | |||
| 2086 | // socket to the address and port being in use. The sockets on the other | |||
| 2087 | // interfaces should open just fine. | |||
| 2088 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSocket4ErrorHandler_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSocket4ErrorHandler", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2088), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2088), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2088), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSocket4ErrorHandler_Test >); void IfaceMgrTest_openSocket4ErrorHandler_Test::TestBody () { | |||
| 2089 | NakedIfaceMgr ifacemgr; | |||
| 2090 | ||||
| 2091 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2092 | ifacemgr.createIfaces(); | |||
| 2093 | ||||
| 2094 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 2095 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2095, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 2096 | 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_2096; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2096; } } else gtest_label_testnothrow_2096 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2096, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2097 | ||||
| 2098 | // Open socket on eth0. | |||
| 2099 | 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_2100; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2100 ; } } else gtest_label_testnothrow_2100 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2100, ("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() | |||
| 2100 | 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_2100; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2100 ; } } else gtest_label_testnothrow_2100 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2100, ("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(); | |||
| 2101 | ||||
| 2102 | // Install an error handler before trying to open sockets. This handler | |||
| 2103 | // should be called when the IfaceMgr fails to open socket on eth0. | |||
| 2104 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
| 2105 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
| 2106 | // The openSockets4 should detect that there is another socket already | |||
| 2107 | // open and bound to the same address and port. An attempt to open | |||
| 2108 | // another socket and bind to this address and port should fail. | |||
| 2109 | 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_2109; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2109 ; } } else gtest_label_testnothrow_2109 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2109, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2110 | // We expect that an error occurred when we tried to open a socket on | |||
| 2111 | // eth0, but the socket on eth1 should open just fine. | |||
| 2112 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2112, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2113 | ||||
| 2114 | // Reset errors count. | |||
| 2115 | errors_count_ = 0; | |||
| 2116 | ||||
| 2117 | // Now that we have two sockets open, we can try this again but this time | |||
| 2118 | // we should get two errors: one when opening a socket on eth0, another one | |||
| 2119 | // when opening a socket on eth1. | |||
| 2120 | 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_2120; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2120 ; } } else gtest_label_testnothrow_2120 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2120, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2121 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2121, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2122 | } | |||
| 2123 | ||||
| 2124 | // Test that no exception is thrown when a port is already bound but skip open | |||
| 2125 | // flag is provided. | |||
| 2126 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets4SkipOpen_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets4SkipOpen", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2126), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2126), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2126), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets4SkipOpen_Test >); void IfaceMgrTest_openSockets4SkipOpen_Test::TestBody( ) { | |||
| 2127 | NakedIfaceMgr ifacemgr; | |||
| 2128 | ||||
| 2129 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2130 | ifacemgr.createIfaces(); | |||
| 2131 | ||||
| 2132 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 2133 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2133, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 2134 | 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_2134; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2134; } } else gtest_label_testnothrow_2134 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2134, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2135 | ||||
| 2136 | // Open socket on eth1. The openSockets4 should detect that this | |||
| 2137 | // socket has been already open and an attempt to open another socket | |||
| 2138 | // and bind to this address and port should fail. | |||
| 2139 | 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_2140; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2140 ; } } else gtest_label_testnothrow_2140 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2140, ("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() | |||
| 2140 | 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_2140; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2140 ; } } else gtest_label_testnothrow_2140 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2140, ("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(); | |||
| 2141 | ||||
| 2142 | // The function doesn't throw an exception when it tries to open a socket | |||
| 2143 | // and bind it to the address in use but the skip open flag is provided. | |||
| 2144 | 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_2144; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2144; } } else gtest_label_testnothrow_2144 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2144, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2145 | ||||
| 2146 | // Check that the other port is bound. | |||
| 2147 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2147, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"10.0.0.1\"))", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2148 | } | |||
| 2149 | ||||
| 2150 | // This test verifies that the function correctly checks that the v4 socket is | |||
| 2151 | // open and bound to a specific address. | |||
| 2152 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_hasOpenSocketForAddress4_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "hasOpenSocketForAddress4", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2152), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2152), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2152), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_hasOpenSocketForAddress4_Test >); void IfaceMgrTest_hasOpenSocketForAddress4_Test::TestBody () { | |||
| 2153 | NakedIfaceMgr ifacemgr; | |||
| 2154 | ||||
| 2155 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2156 | ifacemgr.createIfaces(); | |||
| 2157 | ||||
| 2158 | // Use the custom packet filter object. This object mimics the socket | |||
| 2159 | // opening operation - the real socket is not open. | |||
| 2160 | boost::shared_ptr<TestPktFilter> custom_packet_filter(new TestPktFilter()); | |||
| 2161 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2161, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "custom_packet_filter", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 2162 | 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_2162; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2162; } } else gtest_label_testnothrow_2162 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2162, ("Expected: " "ifacemgr.setPacketFilter(custom_packet_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 | 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_2165; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2165; } } else gtest_label_testnothrow_2165 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2165, ("Expected: " "ifacemgr.openSockets4(DHCP4_SERVER_PORT, true, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2166 | ||||
| 2167 | // Expect that the sockets are open on both eth0 and eth1. | |||
| 2168 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2168, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2169 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2169, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2170 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2170, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2171 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2171, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2172 | // Socket shouldn't have been opened on loopback. | |||
| 2173 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2173, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"lo\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2174 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2174, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(LO_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2175 | ||||
| 2176 | // Check that there are sockets bound to addresses that we have | |||
| 2177 | // set for interfaces. | |||
| 2178 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2178, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"192.0.2.3\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2179 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2179, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"10.0.0.1\"))", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2180 | // Check that there is no socket for the address which is not | |||
| 2181 | // configured on any interface. | |||
| 2182 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2182, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"10.1.1.1\"))", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2183 | ||||
| 2184 | // Check that v4 sockets are open, but no v6 socket is open. | |||
| 2185 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2185, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(2)", "false", "true") .c_str()) = :: testing::Message(); | |||
| 2186 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2186, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(10)", "true", "false") .c_str()) = :: testing::Message(); | |||
| 2187 | } | |||
| 2188 | ||||
| 2189 | // This test checks that the sockets are open and bound to link local addresses | |||
| 2190 | // only, if unicast addresses are not specified. | |||
| 2191 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6LinkLocal_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6LinkLocal", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2191), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2191), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2191), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6LinkLocal_Test >); void IfaceMgrTest_openSockets6LinkLocal_Test::TestBody () { | |||
| 2192 | NakedIfaceMgr ifacemgr; | |||
| 2193 | ||||
| 2194 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2195 | ifacemgr.createIfaces(); | |||
| 2196 | ||||
| 2197 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2198 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2198, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2199 | 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_2199 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2199 ; } } else gtest_label_testnothrow_2199 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2199, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2200 | ||||
| 2201 | // Simulate opening sockets using the dummy packet filter. | |||
| 2202 | bool success = false; | |||
| 2203 | 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_2203; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2203; } } else gtest_label_testnothrow_2203 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2203, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2204 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2204, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2205 | ||||
| 2206 | // Check that the number of sockets is correct on each interface. | |||
| 2207 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
| 2208 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
| 2209 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 0); | |||
| 2210 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 0); | |||
| 2211 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
| 2212 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
| 2213 | ||||
| 2214 | // Sockets on eth0 should be bound to link-local and should not be bound | |||
| 2215 | // to global unicast address, even though this address is configured on | |||
| 2216 | // the eth0. | |||
| 2217 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2217, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2218 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2218, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2219 | // Socket on eth1 should be bound to link local only. | |||
| 2220 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2220, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2221 | ||||
| 2222 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
| 2223 | #if defined OS_LINUX | |||
| 2224 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2224, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2225 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2225, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2226 | #endif | |||
| 2227 | } | |||
| 2228 | ||||
| 2229 | // This test checks that the sockets are open on the loopback interface | |||
| 2230 | // when the loopback is active and allowed. | |||
| 2231 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6Loopback_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6Loopback", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2231), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2231), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2231), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6Loopback_Test >); void IfaceMgrTest_openSockets6Loopback_Test::TestBody( ) { | |||
| 2232 | NakedIfaceMgr ifacemgr; | |||
| 2233 | ||||
| 2234 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2235 | ifacemgr.createIfaces(); | |||
| 2236 | ||||
| 2237 | // Allow the loopback interface. | |||
| 2238 | ifacemgr.setAllowLoopBack(true); | |||
| 2239 | ||||
| 2240 | // Make the loopback interface active. | |||
| 2241 | ifacemgr.getIface("lo")->inactive6_ = false; | |||
| 2242 | ||||
| 2243 | // The loopback interface has no link-local (as for Linux but not BSD) | |||
| 2244 | // so add one. | |||
| 2245 | ifacemgr.getIface("lo")->addUnicast(IOAddress("::1")); | |||
| 2246 | ||||
| 2247 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2248 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2248, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2249 | 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_2249 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2249 ; } } else gtest_label_testnothrow_2249 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2249, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2250 | ||||
| 2251 | // Simulate opening sockets using the dummy packet filter. | |||
| 2252 | bool success = false; | |||
| 2253 | 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_2253; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2253; } } else gtest_label_testnothrow_2253 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2253, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2254 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2254, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2255 | ||||
| 2256 | // Check that the loopback interface has at least an open socket. | |||
| 2257 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2257, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2258 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2258, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2259 | ||||
| 2260 | // This socket should be bound to ::1 | |||
| 2261 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2261, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"lo\", \"::1\")", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 2262 | } | |||
| 2263 | ||||
| 2264 | // This test checks that socket is not open on the interface which doesn't | |||
| 2265 | // have a link-local address. | |||
| 2266 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6NoLinkLocal_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6NoLinkLocal", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2266), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2266), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2266), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6NoLinkLocal_Test >); void IfaceMgrTest_openSockets6NoLinkLocal_Test::TestBody () { | |||
| 2267 | NakedIfaceMgr ifacemgr; | |||
| 2268 | ||||
| 2269 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2270 | ifacemgr.createIfaces(); | |||
| 2271 | ||||
| 2272 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2273 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2273, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2274 | 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_2274 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2274 ; } } else gtest_label_testnothrow_2274 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2274, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2275 | ||||
| 2276 | // Remove a link local address from eth0. If there is no link-local | |||
| 2277 | // address, the socket should not open. | |||
| 2278 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2279, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message() | |||
| 2279 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2279, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2280 | ||||
| 2281 | // Simulate opening sockets using the dummy packet filter. | |||
| 2282 | bool success = false; | |||
| 2283 | 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_2283; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2283; } } else gtest_label_testnothrow_2283 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2283, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2284 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2284, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2285 | ||||
| 2286 | // Check that the number of sockets is correct on each interface. | |||
| 2287 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
| 2288 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
| 2289 | // The third parameter specifies that the number of link-local | |||
| 2290 | // addresses for eth0 is equal to 0. | |||
| 2291 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 0, 0); | |||
| 2292 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 0, 0); | |||
| 2293 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0, 1); | |||
| 2294 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0, 1); | |||
| 2295 | ||||
| 2296 | // There should be no sockets open on eth0 because it neither has | |||
| 2297 | // link-local nor global unicast addresses. | |||
| 2298 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2298, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
| 2299 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2299, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2300 | // Socket on eth1 should be bound to link local only. | |||
| 2301 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2301, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2302 | ||||
| 2303 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
| 2304 | #if defined OS_LINUX | |||
| 2305 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2305, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2306 | #endif | |||
| 2307 | } | |||
| 2308 | ||||
| 2309 | // This test checks that socket is open on the non-multicast-capable | |||
| 2310 | // interface. This socket simply doesn't join multicast group. | |||
| 2311 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6NotMulticast_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6NotMulticast", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2311), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2311), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2311), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6NotMulticast_Test >); void IfaceMgrTest_openSockets6NotMulticast_Test::TestBody () { | |||
| 2312 | NakedIfaceMgr ifacemgr; | |||
| 2313 | ||||
| 2314 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2315 | ifacemgr.createIfaces(); | |||
| 2316 | ||||
| 2317 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2318 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2318, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2319 | 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_2319 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2319 ; } } else gtest_label_testnothrow_2319 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2319, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2320 | ||||
| 2321 | // Make eth0 multicast-incapable. | |||
| 2322 | ifacemgr.getIface("eth0")->flag_multicast_ = false; | |||
| 2323 | ||||
| 2324 | // Simulate opening sockets using the dummy packet filter. | |||
| 2325 | bool success = false; | |||
| 2326 | 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_2326; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2326; } } else gtest_label_testnothrow_2326 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2326, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2327 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2327, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2328 | ||||
| 2329 | // Check that the number of sockets is correct on each interface. | |||
| 2330 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
| 2331 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
| 2332 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 0); | |||
| 2333 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 0); | |||
| 2334 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
| 2335 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
| 2336 | ||||
| 2337 | // Sockets on eth0 should be bound to link-local and should not be bound | |||
| 2338 | // to global unicast address, even though this address is configured on | |||
| 2339 | // the eth0. | |||
| 2340 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2340, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2341 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2341, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2342 | // The eth0 is not a multicast-capable interface, so the socket should | |||
| 2343 | // not be bound to the multicast address. | |||
| 2344 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2344, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2345 | // Socket on eth1 should be bound to link local only. | |||
| 2346 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2346, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2347 | ||||
| 2348 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
| 2349 | // on eth1. | |||
| 2350 | #if defined OS_LINUX | |||
| 2351 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2351, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2352 | #endif | |||
| 2353 | } | |||
| 2354 | ||||
| 2355 | // This test checks that the sockets are opened and bound to link local | |||
| 2356 | // and unicast addresses which have been explicitly specified. | |||
| 2357 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6Unicast_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6Unicast", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2357), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2357), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2357), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6Unicast_Test >); void IfaceMgrTest_openSockets6Unicast_Test::TestBody() { | |||
| 2358 | NakedIfaceMgr ifacemgr; | |||
| 2359 | ||||
| 2360 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2361 | ifacemgr.createIfaces(); | |||
| 2362 | ||||
| 2363 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2364 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2364, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2365 | 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_2365 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2365 ; } } else gtest_label_testnothrow_2365 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2365, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2366 | ||||
| 2367 | // Configure the eth0 to open socket on the unicast address, apart | |||
| 2368 | // from link-local address. | |||
| 2369 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
| 2370 | ||||
| 2371 | // Simulate opening sockets using the dummy packet filter. | |||
| 2372 | bool success = false; | |||
| 2373 | 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_2373; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2373; } } else gtest_label_testnothrow_2373 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2373, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2374 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2374, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2375 | ||||
| 2376 | // Check that we have correct number of sockets on each interface. | |||
| 2377 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
| 2378 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
| 2379 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 1); // one unicast address. | |||
| 2380 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 1); | |||
| 2381 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
| 2382 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
| 2383 | ||||
| 2384 | // eth0 should have two sockets, one bound to link-local, another one | |||
| 2385 | // bound to unicast address. | |||
| 2386 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2386, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2387 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2387, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2388 | // eth1 should have one socket, bound to link-local address. | |||
| 2389 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2389, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2390 | ||||
| 2391 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
| 2392 | #if defined OS_LINUX | |||
| 2393 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2393, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2394 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2394, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2395 | #endif | |||
| 2396 | } | |||
| 2397 | ||||
| 2398 | // This test checks that the socket is open and bound to a global unicast | |||
| 2399 | // address if the link-local address does not exist for the particular | |||
| 2400 | // interface. | |||
| 2401 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6UnicastOnly_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6UnicastOnly", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2401), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2401), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2401), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6UnicastOnly_Test >); void IfaceMgrTest_openSockets6UnicastOnly_Test::TestBody () { | |||
| 2402 | NakedIfaceMgr ifacemgr; | |||
| 2403 | ||||
| 2404 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2405 | ifacemgr.createIfaces(); | |||
| 2406 | ||||
| 2407 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2408 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2408, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2409 | 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_2409 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2409 ; } } else gtest_label_testnothrow_2409 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2409, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2410 | ||||
| 2411 | // Configure the eth0 to open socket on the unicast address, apart | |||
| 2412 | // from link-local address. | |||
| 2413 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
| 2414 | // Explicitly remove the link-local address from eth0. | |||
| 2415 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2416, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message() | |||
| 2416 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2416, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")-> delAddress(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2417 | ||||
| 2418 | // Simulate opening sockets using the dummy packet filter. | |||
| 2419 | bool success = false; | |||
| 2420 | 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_2420; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2420; } } else gtest_label_testnothrow_2420 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2420, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2421 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2421, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2422 | ||||
| 2423 | // Check that we have correct number of sockets on each interface. | |||
| 2424 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
| 2425 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
| 2426 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 1, 0); | |||
| 2427 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 1, 0); | |||
| 2428 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
| 2429 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
| 2430 | ||||
| 2431 | // The link-local address is not present on eth0. Therefore, no socket | |||
| 2432 | // must be bound to this address, nor to multicast address. | |||
| 2433 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2433, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
| 2434 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2434, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2435 | // There should be one socket bound to unicast address. | |||
| 2436 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2436, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2437 | // eth1 should have one socket, bound to link-local address. | |||
| 2438 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2438, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2439 | ||||
| 2440 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
| 2441 | #if defined OS_LINUX | |||
| 2442 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2442, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2443 | #endif | |||
| 2444 | } | |||
| 2445 | ||||
| 2446 | // This test checks that no sockets are open for the interface which is down. | |||
| 2447 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6IfaceDown_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6IfaceDown", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2447), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2447), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2447), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6IfaceDown_Test >); void IfaceMgrTest_openSockets6IfaceDown_Test::TestBody () { | |||
| 2448 | NakedIfaceMgr ifacemgr; | |||
| 2449 | ||||
| 2450 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2451 | ifacemgr.createIfaces(); | |||
| 2452 | ||||
| 2453 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2454 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2454, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2455 | 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_2455 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2455 ; } } else gtest_label_testnothrow_2455 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2455, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2456 | ||||
| 2457 | // Configure the eth0 to open socket on the unicast address, apart | |||
| 2458 | // from link-local address. | |||
| 2459 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
| 2460 | ||||
| 2461 | // Boolean parameters specify that eth0 is: | |||
| 2462 | // - not a loopback | |||
| 2463 | // - is "down" (not up) | |||
| 2464 | // - is not running | |||
| 2465 | // - is active for both v4 and v6 | |||
| 2466 | ifacemgr.setIfaceFlags("eth0", false, false, false, false, false); | |||
| 2467 | ||||
| 2468 | // Install an error handler before trying to open sockets. This handler | |||
| 2469 | // should be called when the IfaceMgr fails to open socket on eth0. | |||
| 2470 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
| 2471 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
| 2472 | ||||
| 2473 | // Simulate opening sockets using the dummy packet filter. | |||
| 2474 | bool success = false; | |||
| 2475 | 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_2476; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2476 ; } } else gtest_label_testnothrow_2476 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2476, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 2476 | 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_2476; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2476 ; } } else gtest_label_testnothrow_2476 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2476, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2477 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2477, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2478 | ||||
| 2479 | // Opening socket on the interface which is not configured, should | |||
| 2480 | // result in error. | |||
| 2481 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2481, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2482 | ||||
| 2483 | // Check that we have correct number of sockets on each interface. | |||
| 2484 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
| 2485 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
| 2486 | // There should be no sockets on eth0 because interface is down. | |||
| 2487 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2487, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth0\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2488 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2488, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH0_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2489 | checkSocketsCount6(*ifacemgr.getIface("eth1"), 0); | |||
| 2490 | checkSocketsCount6(*ifacemgr.getIface(ETH1_INDEX), 0); | |||
| 2491 | ||||
| 2492 | // eth0 should have no sockets because the interface is down. | |||
| 2493 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2493, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
| 2494 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2494, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2495 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2495, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2496 | // eth1 should have one socket, bound to link-local address. | |||
| 2497 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2497, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2498 | ||||
| 2499 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
| 2500 | #if defined OS_LINUX | |||
| 2501 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2501, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2502 | #endif | |||
| 2503 | } | |||
| 2504 | ||||
| 2505 | // This test checks that no sockets are open for the interface which is | |||
| 2506 | // inactive. | |||
| 2507 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_openSockets6IfaceInactive_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6IfaceInactive", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2507), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2507), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2507), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6IfaceInactive_Test >); void IfaceMgrTest_openSockets6IfaceInactive_Test::TestBody () { | |||
| 2508 | NakedIfaceMgr ifacemgr; | |||
| 2509 | ||||
| 2510 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2511 | ifacemgr.createIfaces(); | |||
| 2512 | ||||
| 2513 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2514 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2514, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2515 | 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_2515 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2515 ; } } else gtest_label_testnothrow_2515 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2515, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2516 | ||||
| 2517 | // Configure the eth0 to open socket on the unicast address, apart | |||
| 2518 | // from link-local address. | |||
| 2519 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
| 2520 | ||||
| 2521 | // Boolean parameters specify that eth1 is: | |||
| 2522 | // - not a loopback | |||
| 2523 | // - is up | |||
| 2524 | // - is running | |||
| 2525 | // - is active for v4 | |||
| 2526 | // - is inactive for v6 | |||
| 2527 | ifacemgr.setIfaceFlags("eth1", false, true, true, false, true); | |||
| 2528 | ||||
| 2529 | // Simulate opening sockets using the dummy packet filter. | |||
| 2530 | bool success = false; | |||
| 2531 | 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_2531; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2531; } } else gtest_label_testnothrow_2531 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2531, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2532 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2532, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2533 | ||||
| 2534 | // Check that we have correct number of sockets on each interface. | |||
| 2535 | checkSocketsCount6(*ifacemgr.getIface("lo"), 0); | |||
| 2536 | checkSocketsCount6(*ifacemgr.getIface(LO_INDEX), 0); | |||
| 2537 | checkSocketsCount6(*ifacemgr.getIface("eth0"), 1); // one unicast address | |||
| 2538 | checkSocketsCount6(*ifacemgr.getIface(ETH0_INDEX), 1); | |||
| 2539 | // There should be no sockets on eth1 because interface is inactive | |||
| 2540 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2540, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(\"eth1\")->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2541 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2541, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.getIface(ETH1_INDEX)->getSockets().empty()", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2542 | ||||
| 2543 | // eth0 should have one socket bound to a link-local address, another one | |||
| 2544 | // bound to unicast address. | |||
| 2545 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2545, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2546 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2546, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"2001:db8:1::1\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2547 | ||||
| 2548 | // eth1 shouldn't have a socket bound to link local address because | |||
| 2549 | // interface is inactive. | |||
| 2550 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2550, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "true", "false") .c_str()) = ::testing::Message(); | |||
| 2551 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2551, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"ff02::1:2\")", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2552 | ||||
| 2553 | // If we are on Linux, there is one more socket bound to ff02::1:2 | |||
| 2554 | #if defined OS_LINUX | |||
| 2555 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2555, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"ff02::1:2\")", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2556 | #endif | |||
| 2557 | } | |||
| 2558 | ||||
| 2559 | // Test that the openSockets6 function does not throw if there are no interfaces | |||
| 2560 | // detected. This function is expected to return false. | |||
| 2561 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6NoIfaces_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6NoIfaces", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2561), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2561), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2561), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6NoIfaces_Test >); void IfaceMgrTest_openSockets6NoIfaces_Test::TestBody( ) { | |||
| 2562 | NakedIfaceMgr ifacemgr; | |||
| 2563 | // Remove existing interfaces. | |||
| 2564 | ifacemgr.getIfacesLst().clear(); | |||
| 2565 | ||||
| 2566 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2567 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2567, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2568 | 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_2568 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2568 ; } } else gtest_label_testnothrow_2568 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2568, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2569 | ||||
| 2570 | // This value indicates if at least one socket opens. There are no | |||
| 2571 | // interfaces, so it should be set to false. | |||
| 2572 | bool socket_open = false; | |||
| 2573 | 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_2573; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2573; } } else gtest_label_testnothrow_2573 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2573, ("Expected: " "socket_open = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2574 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2574, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "socket_open", "true", "false") .c_str()) = ::testing::Message (); | |||
| 2575 | } | |||
| 2576 | ||||
| 2577 | // Test that the external error handler is called when trying to bind a new | |||
| 2578 | // socket to the address and port being in use. The sockets on the other | |||
| 2579 | // interfaces should open just fine. | |||
| 2580 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6ErrorHandler_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6ErrorHandler", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2580), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2580), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2580), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6ErrorHandler_Test >); void IfaceMgrTest_openSockets6ErrorHandler_Test::TestBody () { | |||
| 2581 | NakedIfaceMgr ifacemgr; | |||
| 2582 | ||||
| 2583 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2584 | ifacemgr.createIfaces(); | |||
| 2585 | ||||
| 2586 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2587 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2587, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2588 | 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_2588 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2588 ; } } else gtest_label_testnothrow_2588 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2588, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2589 | ||||
| 2590 | // Open multicast socket on eth0. | |||
| 2591 | 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_2593 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2593 ; } } else gtest_label_testnothrow_2593 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2593, ("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() | |||
| 2592 | 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_2593 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2593 ; } } else gtest_label_testnothrow_2593 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2593, ("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() | |||
| 2593 | 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_2593 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2593 ; } } else gtest_label_testnothrow_2593 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2593, ("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(); | |||
| 2594 | ||||
| 2595 | // Install an error handler before trying to open sockets. This handler | |||
| 2596 | // should be called when the IfaceMgr fails to open socket on eth0. | |||
| 2597 | isc::dhcp::IfaceMgrErrorMsgCallback error_handler = | |||
| 2598 | std::bind(&IfaceMgrTest::ifaceMgrErrorHandler, this, ph::_1); | |||
| 2599 | // The openSockets6 should detect that a socket has been already | |||
| 2600 | // opened on eth0 and an attempt to open another socket and bind to | |||
| 2601 | // the same address and port should fail. | |||
| 2602 | 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_2602; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2602; } } else gtest_label_testnothrow_2602 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2602, ("Expected: " "ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2603 | // We expect that an error occurred when we tried to open a socket on | |||
| 2604 | // eth0, but the socket on eth1 should open just fine. | |||
| 2605 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2605, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2606 | ||||
| 2607 | // Reset errors count. | |||
| 2608 | errors_count_ = 0; | |||
| 2609 | ||||
| 2610 | // Now that we have two sockets open, we can try this again but this time | |||
| 2611 | // we should get two errors: one when opening a socket on eth0, another one | |||
| 2612 | // when opening a socket on eth1. | |||
| 2613 | 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_2613; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2613; } } else gtest_label_testnothrow_2613 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2613, ("Expected: " "ifacemgr.openSockets6(DHCP6_SERVER_PORT, error_handler)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2614 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2614, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2615 | } | |||
| 2616 | ||||
| 2617 | // Test that no exception is thrown when a port is already bound but skip open | |||
| 2618 | // flag is provided. | |||
| 2619 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_openSockets6SkipOpen_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "openSockets6SkipOpen", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2619), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2619), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2619), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_openSockets6SkipOpen_Test >); void IfaceMgrTest_openSockets6SkipOpen_Test::TestBody( ) { | |||
| 2620 | NakedIfaceMgr ifacemgr; | |||
| 2621 | ||||
| 2622 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2623 | ifacemgr.createIfaces(); | |||
| 2624 | ||||
| 2625 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2626 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2626, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2627 | 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_2627 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2627 ; } } else gtest_label_testnothrow_2627 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2627, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2628 | ||||
| 2629 | // Open socket on eth0. The openSockets6 should detect that this | |||
| 2630 | // socket has been already open and an attempt to open another socket | |||
| 2631 | // and bind to this address and port should fail. | |||
| 2632 | 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_2634 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2634 ; } } else gtest_label_testnothrow_2634 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2634, ("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() | |||
| 2633 | 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_2634 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2634 ; } } else gtest_label_testnothrow_2634 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2634, ("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() | |||
| 2634 | 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_2634 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2634 ; } } else gtest_label_testnothrow_2634 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2634, ("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(); | |||
| 2635 | ||||
| 2636 | // The function doesn't throw an exception when it tries to open a socket | |||
| 2637 | // and bind it to the address in use but the skip open flag is provided. | |||
| 2638 | 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_2638; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2638; } } else gtest_label_testnothrow_2638 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2638, ("Expected: " "ifacemgr.openSockets6(DHCP6_SERVER_PORT, 0, true)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2639 | ||||
| 2640 | // Check that the other port is bound. | |||
| 2641 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2641, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2642 | } | |||
| 2643 | ||||
| 2644 | // This test verifies that the function correctly checks that the v6 socket is | |||
| 2645 | // open and bound to a specific address. | |||
| 2646 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_hasOpenSocketForAddress6_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "hasOpenSocketForAddress6", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2646), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2646), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2646), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_hasOpenSocketForAddress6_Test >); void IfaceMgrTest_hasOpenSocketForAddress6_Test::TestBody () { | |||
| 2647 | NakedIfaceMgr ifacemgr; | |||
| 2648 | ||||
| 2649 | // Remove all real interfaces and create a set of dummy interfaces. | |||
| 2650 | ifacemgr.createIfaces(); | |||
| 2651 | ||||
| 2652 | boost::shared_ptr<PktFilter6Stub> filter(new PktFilter6Stub()); | |||
| 2653 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2653, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "filter", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2654 | 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_2654 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2654 ; } } else gtest_label_testnothrow_2654 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2654, ("Expected: " "ifacemgr.setPacketFilter(filter)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2655 | ||||
| 2656 | ifacemgr.getIface("eth0")->addUnicast(IOAddress("2001:db8:1::1")); | |||
| 2657 | ifacemgr.getIface("eth1")->addAddress(IOAddress("3001:db8:1::1")); | |||
| 2658 | ||||
| 2659 | // Simulate opening sockets using the dummy packet filter. | |||
| 2660 | bool success = false; | |||
| 2661 | 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_2661; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2661; } } else gtest_label_testnothrow_2661 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2661, ("Expected: " "success = ifacemgr.openSockets6(DHCP6_SERVER_PORT)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2662 | EXPECT_TRUE(success)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(success)) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2662, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "success", "false", "true") .c_str()) = ::testing::Message(); | |||
| 2663 | ||||
| 2664 | // Make sure that the sockets are bound as expected. | |||
| 2665 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2665, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth0\", \"fe80::3a60:77ff:fed5:cdef\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2666 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2666, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.isBound(\"eth1\", \"fe80::3a60:77ff:fed5:abcd\")" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2667 | ||||
| 2668 | // There should be v6 sockets only, no v4 sockets. | |||
| 2669 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2669, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(10)", "false", "true") .c_str()) = :: testing::Message(); | |||
| 2670 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2670, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(2)", "true", "false") .c_str()) = :: testing::Message(); | |||
| 2671 | ||||
| 2672 | // Check that there are sockets bound to the addresses we have configured | |||
| 2673 | // for interfaces. | |||
| 2674 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2674, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.hasOpenSocket(IOAddress(\"fe80::3a60:77ff:fed5:cdef\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2675 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2675, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "ifacemgr.hasOpenSocket(IOAddress(\"fe80::3a60:77ff:fed5:abcd\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2676 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("2001:db8:1::1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("2001:db8:1::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2676, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"2001:db8:1::1\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2677 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("2001:db8:1::1"), true))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("2001:db8:1::1"), true))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2677, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"2001:db8:1::1\"), true)" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 2678 | if (ifacemgr.hasOpenSocket(IOAddress("::"))) { | |||
| 2679 | // On BSD binding is done using "::" when address is multicast which | |||
| 2680 | // makes the address match interface address. | |||
| 2681 | EXPECT_TRUE(ifacemgr.hasOpenSocket(IOAddress("3001:db8:1::1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr.hasOpenSocket (IOAddress("3001:db8:1::1")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2681, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"3001:db8:1::1\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 2682 | } else { | |||
| 2683 | // On Linux binding is done using link-local and all-servers address | |||
| 2684 | // when address is multicast. | |||
| 2685 | EXPECT_FALSE(ifacemgr.hasOpenSocket(IOAddress("3001:db8:1::1")))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.hasOpenSocket (IOAddress("3001:db8:1::1"))))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2685, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"3001:db8:1::1\"))", "true" , "false") .c_str()) = ::testing::Message(); | |||
| 2686 | } | |||
| 2687 | EXPECT_FALSE(ifacemgr.hasOpenSocket(IOAddress("3001:db8:1::1"), true))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr.hasOpenSocket (IOAddress("3001:db8:1::1"), true)))) ; else ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2687, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"3001:db8:1::1\"), true)" , "true", "false") .c_str()) = ::testing::Message(); | |||
| 2688 | // Check that there is no socket bound to the address which hasn't been | |||
| 2689 | // configured on any interface. | |||
| 2690 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2690, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr.hasOpenSocket(IOAddress(\"fe80::3a60:77ff:feed:1\"))" , "true", "false") .c_str()) = ::testing::Message(); | |||
| 2691 | } | |||
| 2692 | ||||
| 2693 | // Test the Iface structure itself | |||
| 2694 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::testing:: TestInfo* const IfaceMgrTest_iface_Test::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "IfaceMgrTest", "iface", nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2694), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2694), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2694), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_iface_Test >); void IfaceMgrTest_iface_Test::TestBody() { | |||
| 2695 | boost::scoped_ptr<Iface> iface; | |||
| 2696 | 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_2696 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2696 ; } } else gtest_label_testnothrow_2696 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2696, ("Expected: " "iface.reset(new Iface(\"eth0\",1))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2697 | ||||
| 2698 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2698, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2699 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2699, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2700 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2700, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2701 | ||||
| 2702 | // Let's make a copy of this address collection. | |||
| 2703 | Iface::AddressCollection addrs = iface->getAddresses(); | |||
| 2704 | ||||
| 2705 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2705, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2706 | ||||
| 2707 | IOAddress addr1("192.0.2.6"); | |||
| 2708 | iface->addAddress(addr1); | |||
| 2709 | ||||
| 2710 | addrs = iface->getAddresses(); | |||
| 2711 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2711, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2712 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2712, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2713 | ||||
| 2714 | // No such address, should return false. | |||
| 2715 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2715, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->delAddress(IOAddress(\"192.0.8.9\"))", "true", "false" ) .c_str()) = ::testing::Message(); | |||
| 2716 | ||||
| 2717 | // This address is present, delete it! | |||
| 2718 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2718, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface->delAddress(IOAddress(\"192.0.2.6\"))", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 2719 | ||||
| 2720 | // Not really necessary, previous reference still points to the same | |||
| 2721 | // collection. Let's do it anyway, as test code may serve as example | |||
| 2722 | // usage code as well. | |||
| 2723 | addrs = iface->getAddresses(); | |||
| 2724 | ||||
| 2725 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2725, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2726 | ||||
| 2727 | 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_2727 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2727 ; } } else gtest_label_testnothrow_2727 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2727, ("Expected: " "iface.reset()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 2728 | } | |||
| 2729 | ||||
| 2730 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_iface_methods_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "iface_methods", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2730), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2730), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2730), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_iface_methods_Test >); void IfaceMgrTest_iface_methods_Test::TestBody() { | |||
| 2731 | Iface iface("foo", 1234); | |||
| 2732 | ||||
| 2733 | iface.setHWType(42); | |||
| 2734 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2734, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2735 | ||||
| 2736 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2736, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 2737 | ||||
| 2738 | uint8_t mac[Iface::MAX_MAC_LEN+10]; | |||
| 2739 | for (uint8_t i = 0; i < Iface::MAX_MAC_LEN + 10; i++) { | |||
| 2740 | mac[i] = 255 - i; | |||
| 2741 | } | |||
| 2742 | ||||
| 2743 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2743, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 2744 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2744, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2745 | ||||
| 2746 | // MAC is too long. Exception should be thrown and | |||
| 2747 | // MAC length should not be set. | |||
| 2748 | 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_2751 ; } 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_2751; } 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_2751; } } else gtest_label_testthrow_2751 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2751, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2749 | 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_2751 ; } 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_2751; } 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_2751; } } else gtest_label_testthrow_2751 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2751, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2750 | 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_2751 ; } 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_2751; } 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_2751; } } else gtest_label_testthrow_2751 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2751, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2751 | )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_2751 ; } 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_2751; } 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_2751; } } else gtest_label_testthrow_2751 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2751, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2752 | ||||
| 2753 | // MAC length should stay not set as exception was thrown. | |||
| 2754 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2754, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2755 | ||||
| 2756 | // Setting maximum length MAC should be ok. | |||
| 2757 | iface.setMac(mac, Iface::MAX_MAC_LEN); | |||
| 2758 | ||||
| 2759 | // For some reason constants cannot be used directly in EXPECT_EQ | |||
| 2760 | // as this produces linking error. | |||
| 2761 | size_t len = Iface::MAX_MAC_LEN; | |||
| 2762 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2762, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2763 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2763, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2764 | } | |||
| 2765 | ||||
| 2766 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_socketInfo_Test:: test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "socketInfo", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2766), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2766), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2766), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_socketInfo_Test >); void IfaceMgrTest_socketInfo_Test::TestBody() { | |||
| 2767 | ||||
| 2768 | // Check that socketinfo for IPv4 socket is functional | |||
| 2769 | SocketInfo sock1(IOAddress("192.0.2.56"), DHCP4_SERVER_PORT + 7, 7); | |||
| 2770 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2770, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2771 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2771, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2772 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2772, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2773 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2773, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2774 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2774, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2775 | ||||
| 2776 | // Check that non-default value of the fallback socket descriptor is set | |||
| 2777 | SocketInfo sock2(IOAddress("192.0.2.53"), DHCP4_SERVER_PORT + 8, 8, 10); | |||
| 2778 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2778, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2779 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2779, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2780 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2780, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2781 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2781, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2782 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2782, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2783 | ||||
| 2784 | // Check that socketinfo for IPv6 socket is functional | |||
| 2785 | SocketInfo sock3(IOAddress("2001:db8:1::56"), DHCP4_SERVER_PORT + 9, 9); | |||
| 2786 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2786, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2787 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2787, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2788 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2788, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2789 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2789, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2790 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2790, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2791 | ||||
| 2792 | // Now let's test if IfaceMgr handles socket info properly | |||
| 2793 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 2794 | IfacePtr loopback = ifacemgr->getIface(LOOPBACK_NAME); | |||
| 2795 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2795, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "loopback", "false", "true") .c_str()) = ::testing::Message (); | |||
| 2796 | loopback->addSocket(sock1); | |||
| 2797 | loopback->addSocket(sock2); | |||
| 2798 | loopback->addSocket(sock3); | |||
| 2799 | ||||
| 2800 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_REPLY, 123456)); | |||
| 2801 | ||||
| 2802 | // pkt6 does not have interface set yet | |||
| 2803 | 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_2806; } 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_2806; } 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_2806; } } else gtest_label_testthrow_2806 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2806, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2804 | 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_2806; } 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_2806; } 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_2806; } } else gtest_label_testthrow_2806 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2806, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2805 | 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_2806; } 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_2806; } 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_2806; } } else gtest_label_testthrow_2806 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2806, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2806 | )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_2806; } 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_2806; } 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_2806; } } else gtest_label_testthrow_2806 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2806, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2807 | ||||
| 2808 | // Try to send over non-existing interface | |||
| 2809 | pkt6->setIface("nosuchinterface45"); | |||
| 2810 | pkt6->setIndex(12345); | |||
| 2811 | 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_2814; } 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_2814; } 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_2814; } } else gtest_label_testthrow_2814 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2814, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2812 | 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_2814; } 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_2814; } 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_2814; } } else gtest_label_testthrow_2814 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2814, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2813 | 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_2814; } 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_2814; } 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_2814; } } else gtest_label_testthrow_2814 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2814, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2814 | )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_2814; } 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_2814; } 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_2814; } } else gtest_label_testthrow_2814 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2814, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2815 | ||||
| 2816 | // Index is now checked first | |||
| 2817 | pkt6->setIface(LOOPBACK_NAME); | |||
| 2818 | 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_2821; } 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_2821; } 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_2821; } } else gtest_label_testthrow_2821 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2821, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2819 | 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_2821; } 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_2821; } 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_2821; } } else gtest_label_testthrow_2821 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2821, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2820 | 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_2821; } 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_2821; } 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_2821; } } else gtest_label_testthrow_2821 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2821, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2821 | )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_2821; } 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_2821; } 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_2821; } } else gtest_label_testthrow_2821 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2821, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2822 | ||||
| 2823 | // This will work | |||
| 2824 | pkt6->setIndex(LOOPBACK_INDEX); | |||
| 2825 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2825, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2826 | ||||
| 2827 | bool deleted = false; | |||
| 2828 | 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_2830; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2830 ; } } else gtest_label_testnothrow_2830 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2830, ("Expected: " "deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket(9);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 2829 | 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_2830; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2830 ; } } else gtest_label_testnothrow_2830 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2830, ("Expected: " "deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket(9);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message() | |||
| 2830 | )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_2830; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2830 ; } } else gtest_label_testnothrow_2830 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2830, ("Expected: " "deleted = ifacemgr->getIface(LOOPBACK_NAME)->delSocket(9);" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 2831 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2831, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 2832 | ||||
| 2833 | // It should throw again, there's no usable socket anymore | |||
| 2834 | 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_2837; } 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_2837; } 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_2837; } } else gtest_label_testthrow_2837 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2837, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2835 | 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_2837; } 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_2837; } 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_2837; } } else gtest_label_testthrow_2837 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2837, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2836 | 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_2837; } 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_2837; } 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_2837; } } else gtest_label_testthrow_2837 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2837, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2837 | )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_2837; } 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_2837; } 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_2837; } } else gtest_label_testthrow_2837 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2837, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2838 | ||||
| 2839 | // Repeat for pkt4 | |||
| 2840 | Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 1)); | |||
| 2841 | ||||
| 2842 | // pkt4 does not have interface set yet. | |||
| 2843 | 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_2846; } 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_2846; } 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_2846; } } else gtest_label_testthrow_2846 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2846, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2844 | 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_2846; } 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_2846; } 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_2846; } } else gtest_label_testthrow_2846 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2846, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2845 | 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_2846; } 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_2846; } 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_2846; } } else gtest_label_testthrow_2846 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2846, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2846 | )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_2846; } 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_2846; } 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_2846; } } else gtest_label_testthrow_2846 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2846, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2847 | ||||
| 2848 | // Try to send over non-existing interface. | |||
| 2849 | pkt4->setIface("nosuchinterface45"); | |||
| 2850 | pkt4->setIndex(12345); | |||
| 2851 | 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_2854; } 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_2854; } 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_2854; } } else gtest_label_testthrow_2854 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2854, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2852 | 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_2854; } 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_2854; } 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_2854; } } else gtest_label_testthrow_2854 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2854, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2853 | 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_2854; } 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_2854; } 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_2854; } } else gtest_label_testthrow_2854 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2854, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2854 | )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_2854; } 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_2854; } 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_2854; } } else gtest_label_testthrow_2854 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2854, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2855 | ||||
| 2856 | // Index is now checked first. | |||
| 2857 | pkt4->setIface(LOOPBACK_NAME); | |||
| 2858 | 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_2861; } 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_2861; } 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_2861; } } else gtest_label_testthrow_2861 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2861, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2859 | 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_2861; } 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_2861; } 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_2861; } } else gtest_label_testthrow_2861 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2861, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2860 | 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_2861; } 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_2861; } 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_2861; } } else gtest_label_testthrow_2861 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2861, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2861 | )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_2861; } 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_2861; } 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_2861; } } else gtest_label_testthrow_2861 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2861, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2862 | ||||
| 2863 | // Socket info is set, packet has well defined interface. It should work. | |||
| 2864 | pkt4->setIndex(LOOPBACK_INDEX); | |||
| 2865 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2865, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 2866 | ||||
| 2867 | // Set the local address to check if the socket for this address will | |||
| 2868 | // be returned. | |||
| 2869 | pkt4->setLocalAddr(IOAddress("192.0.2.56")); | |||
| 2870 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2870, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 2871 | ||||
| 2872 | // Modify the local address and expect that the other socket will be | |||
| 2873 | // returned. | |||
| 2874 | pkt4->setLocalAddr(IOAddress("192.0.2.53")); | |||
| 2875 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 2875, gtest_ar .failure_message()) = ::testing::Message(); | |||
| 2876 | ||||
| 2877 | 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_2880; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2880; } } else gtest_label_testnothrow_2880 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2880, ("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() | |||
| 2878 | 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_2880; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2880; } } else gtest_label_testnothrow_2880 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2880, ("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() | |||
| 2879 | 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_2880; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2880; } } else gtest_label_testnothrow_2880 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2880, ("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() | |||
| 2880 | )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_2880; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_2880; } } else gtest_label_testnothrow_2880 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2880, ("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(); | |||
| 2881 | ||||
| 2882 | // It should throw again, there's no usable socket anymore. | |||
| 2883 | 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_2886; } 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_2886; } 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_2886; } } else gtest_label_testthrow_2886 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2886, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2884 | 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_2886; } 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_2886; } 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_2886; } } else gtest_label_testthrow_2886 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2886, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2885 | 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_2886; } 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_2886; } 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_2886; } } else gtest_label_testthrow_2886 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2886, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 2886 | )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_2886; } 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_2886; } 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_2886; } } else gtest_label_testthrow_2886 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2886, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 2887 | } | |||
| 2888 | ||||
| 2889 | #if defined(OS_BSD) | |||
| 2890 | #include <net/if_dl.h> | |||
| 2891 | #endif | |||
| 2892 | ||||
| 2893 | #include <sys/socket.h> | |||
| 2894 | #include <net/if.h> | |||
| 2895 | #include <ifaddrs.h> | |||
| 2896 | ||||
| 2897 | /// @brief Checks the index of this interface | |||
| 2898 | /// @param iface Kea interface structure to be checked | |||
| 2899 | /// @param ifptr original structure returned by getifaddrs | |||
| 2900 | /// @return true if index is returned properly | |||
| 2901 | bool | |||
| 2902 | checkIfIndex(const Iface & iface, | |||
| 2903 | struct ifaddrs *& ifptr) { | |||
| 2904 | return (iface.getIndex() == if_nametoindex(ifptr->ifa_name)); | |||
| 2905 | } | |||
| 2906 | ||||
| 2907 | /// @brief Checks if the interface has proper flags set | |||
| 2908 | /// @param iface Kea interface structure to be checked | |||
| 2909 | /// @param ifptr original structure returned by getifaddrs | |||
| 2910 | /// @return true if flags are set properly | |||
| 2911 | bool | |||
| 2912 | checkIfFlags(const Iface & iface, | |||
| 2913 | struct ifaddrs *& ifptr) { | |||
| 2914 | bool flag_loopback_ = ifptr->ifa_flags & IFF_LOOPBACKIFF_LOOPBACK; | |||
| 2915 | bool flag_up_ = ifptr->ifa_flags & IFF_UPIFF_UP; | |||
| 2916 | bool flag_running_ = ifptr->ifa_flags & IFF_RUNNINGIFF_RUNNING; | |||
| 2917 | bool flag_multicast_ = ifptr->ifa_flags & IFF_MULTICASTIFF_MULTICAST; | |||
| 2918 | ||||
| 2919 | return ((iface.flag_loopback_ == flag_loopback_) && | |||
| 2920 | (iface.flag_up_ == flag_up_) && | |||
| 2921 | (iface.flag_running_ == flag_running_) && | |||
| 2922 | (iface.flag_multicast_ == flag_multicast_)); | |||
| 2923 | } | |||
| 2924 | ||||
| 2925 | /// @brief Checks if MAC Address/IP Addresses are properly well formed | |||
| 2926 | /// @param iface Kea interface structure to be checked | |||
| 2927 | /// @param ifptr original structure returned by getifaddrs | |||
| 2928 | /// @return true if addresses are returned properly | |||
| 2929 | bool | |||
| 2930 | checkIfAddrs(const Iface & iface, struct ifaddrs *& ifptr) { | |||
| 2931 | const unsigned char * p = 0; | |||
| 2932 | #if defined(OS_LINUX) | |||
| 2933 | // Workaround for Linux ... | |||
| 2934 | if(ifptr->ifa_data != 0) { | |||
| 2935 | // We avoid localhost as it has no MAC Address | |||
| 2936 | if (!strncmp(iface.getName().c_str(), "lo", 2)) { | |||
| 2937 | return (true); | |||
| 2938 | } | |||
| 2939 | ||||
| 2940 | struct ifreq ifr; | |||
| 2941 | memset(& ifr.ifr_nameifr_ifrn.ifrn_name, 0, sizeof ifr.ifr_nameifr_ifrn.ifrn_name); | |||
| 2942 | strncpy(ifr.ifr_nameifr_ifrn.ifrn_name, iface.getName().c_str(), sizeof(ifr.ifr_nameifr_ifrn.ifrn_name) - 1); | |||
| 2943 | ||||
| 2944 | int s = -1; // Socket descriptor | |||
| 2945 | ||||
| 2946 | if ((s = socket(AF_INET2, SOCK_DGRAMSOCK_DGRAM, 0)) < 0) { | |||
| 2947 | isc_throw(Unexpected, "Cannot create AF_INET socket")do { std::ostringstream oss__; oss__ << "Cannot create AF_INET socket" ; throw Unexpected("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2947, oss__.str().c_str()); } while (1); | |||
| 2948 | } | |||
| 2949 | ||||
| 2950 | if (ioctl(s, SIOCGIFHWADDR0x8927, & ifr) < 0) { | |||
| 2951 | close(s); | |||
| 2952 | isc_throw(Unexpected, "Cannot set SIOCGIFHWADDR flag")do { std::ostringstream oss__; oss__ << "Cannot set SIOCGIFHWADDR flag" ; throw Unexpected("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 2952, oss__.str().c_str()); } while (1); | |||
| 2953 | } | |||
| 2954 | ||||
| 2955 | p = reinterpret_cast<uint8_t *>(ifr.ifr_ifru.ifru_hwaddr.sa_data); | |||
| 2956 | ||||
| 2957 | close(s); | |||
| 2958 | ||||
| 2959 | /// @todo: Check MAC address length. For some interfaces it is | |||
| 2960 | /// different than 6. Some have 0, while some exotic ones (like | |||
| 2961 | /// infiniband) have 20. | |||
| 2962 | return (!memcmp(p, iface.getMac(), iface.getMacLen())); | |||
| 2963 | } | |||
| 2964 | #endif | |||
| 2965 | ||||
| 2966 | if(!ifptr->ifa_addr) { | |||
| 2967 | return (false); | |||
| 2968 | } | |||
| 2969 | ||||
| 2970 | switch(ifptr->ifa_addr->sa_family) { | |||
| 2971 | #if defined(OS_BSD) | |||
| 2972 | case AF_LINK: { | |||
| 2973 | // We avoid localhost as it has no MAC Address | |||
| 2974 | if (!strncmp(iface.getName().c_str(), "lo", 2)) { | |||
| 2975 | return (true); | |||
| 2976 | } | |||
| 2977 | ||||
| 2978 | struct sockaddr_dl * hwdata = | |||
| 2979 | reinterpret_cast<struct sockaddr_dl *>(ifptr->ifa_addr); | |||
| 2980 | p = reinterpret_cast<uint8_t *>(LLADDR(hwdata)); | |||
| 2981 | ||||
| 2982 | // Extract MAC address length | |||
| 2983 | if (hwdata->sdl_alen != iface.getMacLen()) { | |||
| 2984 | return (false); | |||
| 2985 | } | |||
| 2986 | ||||
| 2987 | return (!memcmp(p, iface.getMac(), hwdata->sdl_alen)); | |||
| 2988 | } | |||
| 2989 | #endif | |||
| 2990 | case AF_INET2: { | |||
| 2991 | struct sockaddr_in * v4data = | |||
| 2992 | reinterpret_cast<struct sockaddr_in *>(ifptr->ifa_addr); | |||
| 2993 | p = reinterpret_cast<uint8_t *>(& v4data->sin_addr); | |||
| 2994 | ||||
| 2995 | IOAddress addrv4 = IOAddress::fromBytes(AF_INET2, p); | |||
| 2996 | ||||
| 2997 | for (auto const& a :iface.getAddresses()) { | |||
| 2998 | if(a.get().isV4() && (a.get()) == addrv4) { | |||
| 2999 | return (true); | |||
| 3000 | } | |||
| 3001 | } | |||
| 3002 | ||||
| 3003 | return (false); | |||
| 3004 | } | |||
| 3005 | case AF_INET610: { | |||
| 3006 | struct sockaddr_in6 * v6data = | |||
| 3007 | reinterpret_cast<struct sockaddr_in6 *>(ifptr->ifa_addr); | |||
| 3008 | p = reinterpret_cast<uint8_t *>(& v6data->sin6_addr); | |||
| 3009 | ||||
| 3010 | IOAddress addrv6 = IOAddress::fromBytes(AF_INET610, p); | |||
| 3011 | ||||
| 3012 | for (auto const& a : iface.getAddresses()) { | |||
| 3013 | if (a.get().isV6() && (a.get() == addrv6)) { | |||
| 3014 | return (true); | |||
| 3015 | } | |||
| 3016 | } | |||
| 3017 | ||||
| 3018 | return (false); | |||
| 3019 | } | |||
| 3020 | default: | |||
| 3021 | return (true); | |||
| 3022 | } | |||
| 3023 | } | |||
| 3024 | ||||
| 3025 | /// This test checks that the IfaceMgr detects interfaces correctly and | |||
| 3026 | /// that detected interfaces have correct properties. | |||
| 3027 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_detectIfaces_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "detectIfaces", nullptr, nullptr, ::testing::internal::CodeLocation ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3027), ( ::testing::internal::GetTypeId<IfaceMgrTest>()), ::testing ::internal::SuiteApiResolver< IfaceMgrTest>::GetSetUpCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3027), :: testing::internal::SuiteApiResolver< IfaceMgrTest>::GetTearDownCaseOrSuite ("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3027), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_detectIfaces_Test >); void IfaceMgrTest_detectIfaces_Test::TestBody() { | |||
| 3028 | NakedIfaceMgr ifacemgr; | |||
| 3029 | ||||
| 3030 | // We are using struct ifaddrs as it is the only good portable one | |||
| 3031 | // ifreq and ioctls are far from portable. For BSD ifreq::ifa_flags field | |||
| 3032 | // is only a short which, nowadays, can be negative | |||
| 3033 | struct ifaddrs *iflist = 0, *ifptr = 0; | |||
| 3034 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3034, gtest_ar.failure_message()) = ::testing::Message() | |||
| 3035 | << "Unit test failed to detect interfaces."; | |||
| 3036 | ||||
| 3037 | // Go over all interfaces detected by the unit test and see if they | |||
| 3038 | // match with the interfaces detected by IfaceMgr. | |||
| 3039 | for (ifptr = iflist; ifptr != 0; ifptr = ifptr->ifa_next) { | |||
| 3040 | // When more than one IPv4 address is assigned to the particular | |||
| 3041 | // physical interface, virtual interfaces may be created for each | |||
| 3042 | // additional IPv4 address. For example, when multiple addresses | |||
| 3043 | // are assigned to the eth0 interface, additional virtual interfaces | |||
| 3044 | // will be eth0:0, eth0:1 etc. This is the case on some Linux | |||
| 3045 | // distributions. The getifaddrs will return virtual interfaces, | |||
| 3046 | // with single address each, but the Netlink-based implementation | |||
| 3047 | // (used by IfaceMgr) will rather hold a list of physical interfaces | |||
| 3048 | // with multiple IPv4 addresses assigned. This means that the test | |||
| 3049 | // can't use a name of the interface returned by getifaddrs to match | |||
| 3050 | // with the interface name held by IfaceMgr. Instead, we use the | |||
| 3051 | // index of the interface because the virtual interfaces have the | |||
| 3052 | // same indexes as the physical interfaces. | |||
| 3053 | IfacePtr i = ifacemgr.getIface(if_nametoindex(ifptr->ifa_name)); | |||
| 3054 | ||||
| 3055 | // If the given interface was also detected by the IfaceMgr, | |||
| 3056 | // check that its properties are correct. | |||
| 3057 | if (i != NULL__null) { | |||
| 3058 | // Check if interface index is reported properly | |||
| 3059 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3059, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "checkIfIndex(*i, ifptr)", "false", "true") .c_str()) = ::testing ::Message() | |||
| 3060 | << "Non-matching index of the detected interface " | |||
| 3061 | << i->getName(); | |||
| 3062 | ||||
| 3063 | // Check if flags are reported properly | |||
| 3064 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3064, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "checkIfFlags(*i, ifptr)", "false", "true") .c_str()) = ::testing ::Message() | |||
| 3065 | << "Non-matching flags of the detected interface " | |||
| 3066 | << i->getName(); | |||
| 3067 | ||||
| 3068 | // Check if addresses are reported properly | |||
| 3069 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3069, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "checkIfAddrs(*i, ifptr)", "false", "true") .c_str()) = ::testing ::Message() | |||
| 3070 | << "Non-matching addresses on the detected interface " | |||
| 3071 | << i->getName(); | |||
| 3072 | ||||
| 3073 | } else { | |||
| 3074 | // The interface detected here seems to be missing in the | |||
| 3075 | // IfaceMgr. | |||
| 3076 | ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3076, "Failed") = ::testing::Message() << "Interface " << ifptr->ifa_name | |||
| 3077 | << " not detected by the Interface Manager"; | |||
| 3078 | } | |||
| 3079 | } | |||
| 3080 | ||||
| 3081 | freeifaddrs(iflist); | |||
| 3082 | iflist = 0; | |||
| 3083 | } | |||
| 3084 | ||||
| 3085 | // Tests if a single external socket and its callback can be passed and | |||
| 3086 | // it is supported properly by receive4() method. | |||
| 3087 | void IfaceMgrTest::testSingleExternalSocket4() { | |||
| 3088 | callback_ok = false; | |||
| 3089 | ||||
| 3090 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3091 | ||||
| 3092 | // Create pipe and register it as extra socket | |||
| 3093 | int pipefd[2]; | |||
| 3094 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3094, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3095 | 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_3095; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3095; } } else gtest_label_testnothrow_3095 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3095, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3096 | ||||
| 3097 | 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_3097 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3097 ; } } else gtest_label_testnothrow_3097 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3097, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3098 | ||||
| 3099 | Pkt4Ptr pkt4; | |||
| 3100 | 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_3100 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3100 ; } } else gtest_label_testnothrow_3100 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3100, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3101 | ||||
| 3102 | // Our callback should not be called this time (there was no data) | |||
| 3103 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3103, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3104 | ||||
| 3105 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3106 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3106, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3107 | ||||
| 3108 | // Now, send some data over pipe (38 bytes) | |||
| 3109 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3109, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3110 | ||||
| 3111 | // ... and repeat | |||
| 3112 | 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_3112 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3112 ; } } else gtest_label_testnothrow_3112 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3112, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3113 | ||||
| 3114 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3115 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3115, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3116 | ||||
| 3117 | // There was some data, so this time callback should be called | |||
| 3118 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3118, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3119 | ||||
| 3120 | // close both pipe ends | |||
| 3121 | close(pipefd[1]); | |||
| 3122 | close(pipefd[0]); | |||
| 3123 | ||||
| 3124 | 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_3124 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3124 ; } } else gtest_label_testnothrow_3124 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3124, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3125 | } | |||
| 3126 | ||||
| 3127 | TEST_F(IfaceMgrTest, SingleExternalSocket4Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("SingleExternalSocket4Select") > 1 , "test_name must not be empty"); class IfaceMgrTest_SingleExternalSocket4Select_Test : public IfaceMgrTest { public: IfaceMgrTest_SingleExternalSocket4Select_Test () = default; ~IfaceMgrTest_SingleExternalSocket4Select_Test( ) override = default; IfaceMgrTest_SingleExternalSocket4Select_Test (const IfaceMgrTest_SingleExternalSocket4Select_Test &) = delete; IfaceMgrTest_SingleExternalSocket4Select_Test & operator =( const IfaceMgrTest_SingleExternalSocket4Select_Test &) = delete; IfaceMgrTest_SingleExternalSocket4Select_Test (IfaceMgrTest_SingleExternalSocket4Select_Test &&) noexcept = delete; IfaceMgrTest_SingleExternalSocket4Select_Test & operator=( IfaceMgrTest_SingleExternalSocket4Select_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_SingleExternalSocket4Select_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "SingleExternalSocket4Select", nullptr, nullptr, ::testing:: internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3127), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3127), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3127), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_SingleExternalSocket4Select_Test >); void IfaceMgrTest_SingleExternalSocket4Select_Test::TestBody () { | |||
| 3128 | kea_event_handler_type_.setValue("select"); | |||
| 3129 | testSingleExternalSocket4(); | |||
| 3130 | } | |||
| 3131 | ||||
| 3132 | TEST_F(IfaceMgrTest, SingleExternalSocket4Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("SingleExternalSocket4Poll") > 1, "test_name must not be empty" ); class IfaceMgrTest_SingleExternalSocket4Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_SingleExternalSocket4Poll_Test () = default; ~IfaceMgrTest_SingleExternalSocket4Poll_Test() override = default; IfaceMgrTest_SingleExternalSocket4Poll_Test (const IfaceMgrTest_SingleExternalSocket4Poll_Test &) = delete; IfaceMgrTest_SingleExternalSocket4Poll_Test & operator=( const IfaceMgrTest_SingleExternalSocket4Poll_Test &) = delete ; IfaceMgrTest_SingleExternalSocket4Poll_Test (IfaceMgrTest_SingleExternalSocket4Poll_Test &&) noexcept = delete; IfaceMgrTest_SingleExternalSocket4Poll_Test & operator=( IfaceMgrTest_SingleExternalSocket4Poll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_SingleExternalSocket4Poll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "SingleExternalSocket4Poll", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3132), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3132), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3132), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_SingleExternalSocket4Poll_Test >); void IfaceMgrTest_SingleExternalSocket4Poll_Test::TestBody () { | |||
| 3133 | kea_event_handler_type_.setValue("poll"); | |||
| 3134 | testSingleExternalSocket4(); | |||
| 3135 | } | |||
| 3136 | ||||
| 3137 | // Tests if multiple external sockets and their callbacks can be passed and | |||
| 3138 | // it is supported properly by receive4() method. | |||
| 3139 | void IfaceMgrTest::testMultipleExternalSockets4() { | |||
| 3140 | callback_ok = false; | |||
| 3141 | callback2_ok = false; | |||
| 3142 | ||||
| 3143 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3144 | ||||
| 3145 | // Create first pipe and register it as extra socket | |||
| 3146 | int pipefd[2]; | |||
| 3147 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3147, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3148 | 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_3148; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3148; } } else gtest_label_testnothrow_3148 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3148, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3149 | ||||
| 3150 | // Let's create a second pipe and register it as well | |||
| 3151 | int secondpipe[2]; | |||
| 3152 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3152, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(secondpipe) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3153 | 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_3153; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3153 ; } } else gtest_label_testnothrow_3153 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3153, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3154 | ||||
| 3155 | Pkt4Ptr pkt4; | |||
| 3156 | 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_3156 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3156 ; } } else gtest_label_testnothrow_3156 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3156, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3157 | ||||
| 3158 | // Our callbacks should not be called this time (there was no data) | |||
| 3159 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3159, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3160 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3160, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3161 | ||||
| 3162 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3163 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3163, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3164 | ||||
| 3165 | // Now, send some data over the first pipe (38 bytes) | |||
| 3166 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3166, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3167 | ||||
| 3168 | // ... and repeat | |||
| 3169 | 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_3169 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3169 ; } } else gtest_label_testnothrow_3169 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3169, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3170 | ||||
| 3171 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3172 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3172, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3173 | ||||
| 3174 | // There was some data, so this time callback should be called | |||
| 3175 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3175, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3176 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3176, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3177 | ||||
| 3178 | // Read the data sent, because our test callbacks are too dumb to actually | |||
| 3179 | // do it. We don't care about the content read, because we're testing | |||
| 3180 | // the callbacks, not pipes. | |||
| 3181 | char buf[80]; | |||
| 3182 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3182, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3183 | ||||
| 3184 | // Clear the status... | |||
| 3185 | callback_ok = false; | |||
| 3186 | callback2_ok = false; | |||
| 3187 | ||||
| 3188 | // And try again, using the second pipe | |||
| 3189 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3189, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3190 | ||||
| 3191 | // ... and repeat | |||
| 3192 | 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_3192 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3192 ; } } else gtest_label_testnothrow_3192 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3192, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3193 | ||||
| 3194 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3195 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3195, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3196 | ||||
| 3197 | // There was some data, so this time callback should be called | |||
| 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3198, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3199 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3199, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3200 | ||||
| 3201 | // close both pipe ends | |||
| 3202 | close(pipefd[1]); | |||
| 3203 | close(pipefd[0]); | |||
| 3204 | ||||
| 3205 | close(secondpipe[1]); | |||
| 3206 | close(secondpipe[0]); | |||
| 3207 | } | |||
| 3208 | ||||
| 3209 | TEST_F(IfaceMgrTest, MultipleExternalSockets4Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("MultipleExternalSockets4Select") > 1, "test_name must not be empty"); class IfaceMgrTest_MultipleExternalSockets4Select_Test : public IfaceMgrTest { public: IfaceMgrTest_MultipleExternalSockets4Select_Test () = default; ~IfaceMgrTest_MultipleExternalSockets4Select_Test () override = default; IfaceMgrTest_MultipleExternalSockets4Select_Test (const IfaceMgrTest_MultipleExternalSockets4Select_Test & ) = delete; IfaceMgrTest_MultipleExternalSockets4Select_Test & operator=( const IfaceMgrTest_MultipleExternalSockets4Select_Test &) = delete; IfaceMgrTest_MultipleExternalSockets4Select_Test (IfaceMgrTest_MultipleExternalSockets4Select_Test && ) noexcept = delete; IfaceMgrTest_MultipleExternalSockets4Select_Test & operator=( IfaceMgrTest_MultipleExternalSockets4Select_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_MultipleExternalSockets4Select_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "MultipleExternalSockets4Select", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3209), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3209), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3209), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_MultipleExternalSockets4Select_Test >); void IfaceMgrTest_MultipleExternalSockets4Select_Test:: TestBody() { | |||
| 3210 | kea_event_handler_type_.setValue("select"); | |||
| 3211 | testMultipleExternalSockets4(); | |||
| 3212 | } | |||
| 3213 | ||||
| 3214 | TEST_F(IfaceMgrTest, MultipleExternalSockets4Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("MultipleExternalSockets4Poll") > 1 , "test_name must not be empty"); class IfaceMgrTest_MultipleExternalSockets4Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_MultipleExternalSockets4Poll_Test () = default; ~IfaceMgrTest_MultipleExternalSockets4Poll_Test () override = default; IfaceMgrTest_MultipleExternalSockets4Poll_Test (const IfaceMgrTest_MultipleExternalSockets4Poll_Test &) = delete; IfaceMgrTest_MultipleExternalSockets4Poll_Test & operator=( const IfaceMgrTest_MultipleExternalSockets4Poll_Test &) = delete; IfaceMgrTest_MultipleExternalSockets4Poll_Test (IfaceMgrTest_MultipleExternalSockets4Poll_Test &&) noexcept = delete; IfaceMgrTest_MultipleExternalSockets4Poll_Test & operator=( IfaceMgrTest_MultipleExternalSockets4Poll_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_MultipleExternalSockets4Poll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "MultipleExternalSockets4Poll", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3214), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3214), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3214), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_MultipleExternalSockets4Poll_Test >); void IfaceMgrTest_MultipleExternalSockets4Poll_Test::TestBody () { | |||
| 3215 | kea_event_handler_type_.setValue("poll"); | |||
| 3216 | testMultipleExternalSockets4(); | |||
| 3217 | } | |||
| 3218 | ||||
| 3219 | // Tests if existing external socket can be deleted and that such deletion does | |||
| 3220 | // not affect any other existing sockets. Tests uses receive4() | |||
| 3221 | void IfaceMgrTest::testDeleteExternalSockets4() { | |||
| 3222 | callback_ok = false; | |||
| 3223 | callback2_ok = false; | |||
| 3224 | ||||
| 3225 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3226 | ||||
| 3227 | // Create first pipe and register it as extra socket | |||
| 3228 | int pipefd[2]; | |||
| 3229 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3229, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3230 | 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_3230; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3230; } } else gtest_label_testnothrow_3230 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3230, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3231 | ||||
| 3232 | // Let's create a second pipe and register it as well | |||
| 3233 | int secondpipe[2]; | |||
| 3234 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3234, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(secondpipe) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3235 | 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_3235; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3235 ; } } else gtest_label_testnothrow_3235 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3235, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3236 | ||||
| 3237 | // Now delete the first session socket | |||
| 3238 | 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_3238; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3238; } } else gtest_label_testnothrow_3238 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3238, ("Expected: " "ifacemgr->deleteExternalSocket(pipefd[0])" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3239 | ||||
| 3240 | // Now check whether the second callback is still functional | |||
| 3241 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3241, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3242 | ||||
| 3243 | // ... and repeat | |||
| 3244 | Pkt4Ptr pkt4; | |||
| 3245 | 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_3245 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3245 ; } } else gtest_label_testnothrow_3245 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3245, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3246 | ||||
| 3247 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3248 | EXPECT_FALSE(pkt4)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt4))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3248, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt4", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3249 | ||||
| 3250 | // There was some data, so this time callback should be called | |||
| 3251 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3251, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3252 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3252, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3253 | ||||
| 3254 | // Let's reset the status | |||
| 3255 | callback_ok = false; | |||
| 3256 | callback2_ok = false; | |||
| 3257 | ||||
| 3258 | // Now let's send something over the first callback that was unregistered. | |||
| 3259 | // We should NOT receive any callback. | |||
| 3260 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3260, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3261 | ||||
| 3262 | // Now check that the first callback is NOT called. | |||
| 3263 | 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_3263 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3263 ; } } else gtest_label_testnothrow_3263 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3263, ("Expected: " "pkt4 = ifacemgr->receive4(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3264 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3264, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3265 | ||||
| 3266 | // close both pipe ends | |||
| 3267 | close(pipefd[1]); | |||
| 3268 | close(pipefd[0]); | |||
| 3269 | ||||
| 3270 | close(secondpipe[1]); | |||
| 3271 | close(secondpipe[0]); | |||
| 3272 | } | |||
| 3273 | ||||
| 3274 | TEST_F(IfaceMgrTest, DeleteExternalSockets4Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DeleteExternalSockets4Select") > 1 , "test_name must not be empty"); class IfaceMgrTest_DeleteExternalSockets4Select_Test : public IfaceMgrTest { public: IfaceMgrTest_DeleteExternalSockets4Select_Test () = default; ~IfaceMgrTest_DeleteExternalSockets4Select_Test () override = default; IfaceMgrTest_DeleteExternalSockets4Select_Test (const IfaceMgrTest_DeleteExternalSockets4Select_Test &) = delete; IfaceMgrTest_DeleteExternalSockets4Select_Test & operator=( const IfaceMgrTest_DeleteExternalSockets4Select_Test &) = delete; IfaceMgrTest_DeleteExternalSockets4Select_Test (IfaceMgrTest_DeleteExternalSockets4Select_Test &&) noexcept = delete; IfaceMgrTest_DeleteExternalSockets4Select_Test & operator=( IfaceMgrTest_DeleteExternalSockets4Select_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_DeleteExternalSockets4Select_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DeleteExternalSockets4Select", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3274), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3274), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3274), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DeleteExternalSockets4Select_Test >); void IfaceMgrTest_DeleteExternalSockets4Select_Test::TestBody () { | |||
| 3275 | kea_event_handler_type_.setValue("select"); | |||
| 3276 | testDeleteExternalSockets4(); | |||
| 3277 | } | |||
| 3278 | ||||
| 3279 | TEST_F(IfaceMgrTest, DeleteExternalSockets4Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DeleteExternalSockets4Poll") > 1, "test_name must not be empty"); class IfaceMgrTest_DeleteExternalSockets4Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_DeleteExternalSockets4Poll_Test () = default; ~IfaceMgrTest_DeleteExternalSockets4Poll_Test() override = default; IfaceMgrTest_DeleteExternalSockets4Poll_Test (const IfaceMgrTest_DeleteExternalSockets4Poll_Test &) = delete; IfaceMgrTest_DeleteExternalSockets4Poll_Test & operator =( const IfaceMgrTest_DeleteExternalSockets4Poll_Test &) = delete; IfaceMgrTest_DeleteExternalSockets4Poll_Test (IfaceMgrTest_DeleteExternalSockets4Poll_Test &&) noexcept = delete; IfaceMgrTest_DeleteExternalSockets4Poll_Test & operator=( IfaceMgrTest_DeleteExternalSockets4Poll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_DeleteExternalSockets4Poll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DeleteExternalSockets4Poll", nullptr, nullptr, ::testing:: internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3279), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3279), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3279), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DeleteExternalSockets4Poll_Test >); void IfaceMgrTest_DeleteExternalSockets4Poll_Test::TestBody () { | |||
| 3280 | kea_event_handler_type_.setValue("poll"); | |||
| 3281 | testDeleteExternalSockets4(); | |||
| 3282 | } | |||
| 3283 | ||||
| 3284 | TEST_F(IfaceMgrTest, unusableExternalSockets4DirectSelect)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets4DirectSelect" ) > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets4DirectSelect_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets4DirectSelect_Test () = default; ~IfaceMgrTest_unusableExternalSockets4DirectSelect_Test () override = default; IfaceMgrTest_unusableExternalSockets4DirectSelect_Test (const IfaceMgrTest_unusableExternalSockets4DirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets4DirectSelect_Test & operator=( const IfaceMgrTest_unusableExternalSockets4DirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets4DirectSelect_Test (IfaceMgrTest_unusableExternalSockets4DirectSelect_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets4DirectSelect_Test & operator=( IfaceMgrTest_unusableExternalSockets4DirectSelect_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets4DirectSelect_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets4DirectSelect", nullptr, nullptr, :: testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3284), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3284), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3284), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets4DirectSelect_Test >); void IfaceMgrTest_unusableExternalSockets4DirectSelect_Test ::TestBody() { | |||
| 3285 | kea_event_handler_type_.setValue("select"); | |||
| 3286 | unusableExternalSockets4Test(); | |||
| 3287 | } | |||
| 3288 | ||||
| 3289 | TEST_F(IfaceMgrTest, unusableExternalSockets4DirectPoll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets4DirectPoll") > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets4DirectPoll_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets4DirectPoll_Test () = default; ~IfaceMgrTest_unusableExternalSockets4DirectPoll_Test () override = default; IfaceMgrTest_unusableExternalSockets4DirectPoll_Test (const IfaceMgrTest_unusableExternalSockets4DirectPoll_Test & ) = delete; IfaceMgrTest_unusableExternalSockets4DirectPoll_Test & operator=( const IfaceMgrTest_unusableExternalSockets4DirectPoll_Test &) = delete; IfaceMgrTest_unusableExternalSockets4DirectPoll_Test (IfaceMgrTest_unusableExternalSockets4DirectPoll_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets4DirectPoll_Test & operator=( IfaceMgrTest_unusableExternalSockets4DirectPoll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets4DirectPoll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets4DirectPoll", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3289), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3289), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3289), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets4DirectPoll_Test >); void IfaceMgrTest_unusableExternalSockets4DirectPoll_Test ::TestBody() { | |||
| 3290 | kea_event_handler_type_.setValue("poll"); | |||
| 3291 | unusableExternalSockets4Test(); | |||
| 3292 | } | |||
| 3293 | ||||
| 3294 | TEST_F(IfaceMgrTest, unusableExternalSockets4IndirectSelect)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets4IndirectSelect" ) > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test () = default; ~IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test () override = default; IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test (const IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test & operator=( const IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test (IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test & operator=( IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets4IndirectSelect", nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3294), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3294), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3294), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test >); void IfaceMgrTest_unusableExternalSockets4IndirectSelect_Test ::TestBody() { | |||
| 3295 | kea_event_handler_type_.setValue("select"); | |||
| 3296 | unusableExternalSockets4Test(true); | |||
| 3297 | } | |||
| 3298 | ||||
| 3299 | TEST_F(IfaceMgrTest, unusableExternalSockets4IndirectPoll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets4IndirectPoll" ) > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test () = default; ~IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test () override = default; IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test (const IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test &) = delete; IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test & operator=( const IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test &) = delete; IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test (IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test & operator=( IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets4IndirectPoll", nullptr, nullptr, :: testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3299), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3299), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3299), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test >); void IfaceMgrTest_unusableExternalSockets4IndirectPoll_Test ::TestBody() { | |||
| 3300 | kea_event_handler_type_.setValue("poll"); | |||
| 3301 | unusableExternalSockets4Test(true); | |||
| 3302 | } | |||
| 3303 | ||||
| 3304 | // Tests if a single external socket and its callback can be passed and | |||
| 3305 | // it is supported properly by receive6() method. | |||
| 3306 | void IfaceMgrTest::testSingleExternalSocket6() { | |||
| 3307 | callback_ok = false; | |||
| 3308 | ||||
| 3309 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3310 | ||||
| 3311 | // Create pipe and register it as extra socket | |||
| 3312 | int pipefd[2]; | |||
| 3313 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3313, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3314 | 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_3314; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3314; } } else gtest_label_testnothrow_3314 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3314, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3315 | ||||
| 3316 | Pkt6Ptr pkt6; | |||
| 3317 | 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_3317 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3317 ; } } else gtest_label_testnothrow_3317 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3317, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3318 | ||||
| 3319 | // Our callback should not be called this time (there was no data) | |||
| 3320 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3320, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3323, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3324 | ||||
| 3325 | // Now, send some data over pipe (38 bytes) | |||
| 3326 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3326, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3327 | ||||
| 3328 | // ... and repeat | |||
| 3329 | 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_3329 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3329 ; } } else gtest_label_testnothrow_3329 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3329, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3330 | ||||
| 3331 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3332 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3332, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3333 | ||||
| 3334 | // There was some data, so this time callback should be called | |||
| 3335 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3335, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3336 | ||||
| 3337 | // close both pipe ends | |||
| 3338 | close(pipefd[1]); | |||
| 3339 | close(pipefd[0]); | |||
| 3340 | } | |||
| 3341 | ||||
| 3342 | TEST_F(IfaceMgrTest, SingleExternalSocket6Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("SingleExternalSocket6Select") > 1 , "test_name must not be empty"); class IfaceMgrTest_SingleExternalSocket6Select_Test : public IfaceMgrTest { public: IfaceMgrTest_SingleExternalSocket6Select_Test () = default; ~IfaceMgrTest_SingleExternalSocket6Select_Test( ) override = default; IfaceMgrTest_SingleExternalSocket6Select_Test (const IfaceMgrTest_SingleExternalSocket6Select_Test &) = delete; IfaceMgrTest_SingleExternalSocket6Select_Test & operator =( const IfaceMgrTest_SingleExternalSocket6Select_Test &) = delete; IfaceMgrTest_SingleExternalSocket6Select_Test (IfaceMgrTest_SingleExternalSocket6Select_Test &&) noexcept = delete; IfaceMgrTest_SingleExternalSocket6Select_Test & operator=( IfaceMgrTest_SingleExternalSocket6Select_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_SingleExternalSocket6Select_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "SingleExternalSocket6Select", nullptr, nullptr, ::testing:: internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3342), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3342), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3342), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_SingleExternalSocket6Select_Test >); void IfaceMgrTest_SingleExternalSocket6Select_Test::TestBody () { | |||
| 3343 | kea_event_handler_type_.setValue("select"); | |||
| 3344 | testSingleExternalSocket6(); | |||
| 3345 | } | |||
| 3346 | ||||
| 3347 | TEST_F(IfaceMgrTest, SingleExternalSocket6Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("SingleExternalSocket6Poll") > 1, "test_name must not be empty" ); class IfaceMgrTest_SingleExternalSocket6Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_SingleExternalSocket6Poll_Test () = default; ~IfaceMgrTest_SingleExternalSocket6Poll_Test() override = default; IfaceMgrTest_SingleExternalSocket6Poll_Test (const IfaceMgrTest_SingleExternalSocket6Poll_Test &) = delete; IfaceMgrTest_SingleExternalSocket6Poll_Test & operator=( const IfaceMgrTest_SingleExternalSocket6Poll_Test &) = delete ; IfaceMgrTest_SingleExternalSocket6Poll_Test (IfaceMgrTest_SingleExternalSocket6Poll_Test &&) noexcept = delete; IfaceMgrTest_SingleExternalSocket6Poll_Test & operator=( IfaceMgrTest_SingleExternalSocket6Poll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_SingleExternalSocket6Poll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "SingleExternalSocket6Poll", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3347), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3347), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3347), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_SingleExternalSocket6Poll_Test >); void IfaceMgrTest_SingleExternalSocket6Poll_Test::TestBody () { | |||
| 3348 | kea_event_handler_type_.setValue("poll"); | |||
| 3349 | testSingleExternalSocket6(); | |||
| 3350 | } | |||
| 3351 | ||||
| 3352 | // Tests if multiple external sockets and their callbacks can be passed and | |||
| 3353 | // it is supported properly by receive6() method. | |||
| 3354 | void IfaceMgrTest::testMultipleExternalSockets6() { | |||
| 3355 | callback_ok = false; | |||
| 3356 | callback2_ok = false; | |||
| 3357 | ||||
| 3358 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3359 | ||||
| 3360 | // Create first pipe and register it as extra socket | |||
| 3361 | int pipefd[2]; | |||
| 3362 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3362, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3363 | 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_3363; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3363; } } else gtest_label_testnothrow_3363 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3363, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3364 | ||||
| 3365 | // Let's create a second pipe and register it as well | |||
| 3366 | int secondpipe[2]; | |||
| 3367 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3367, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(secondpipe) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3368 | 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_3368; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3368 ; } } else gtest_label_testnothrow_3368 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3368, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3369 | ||||
| 3370 | Pkt6Ptr pkt6; | |||
| 3371 | 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_3371 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3371 ; } } else gtest_label_testnothrow_3371 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3371, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3372 | ||||
| 3373 | // Our callbacks should not be called this time (there was no data) | |||
| 3374 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3374, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3375 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3375, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3376 | ||||
| 3377 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3378 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3378, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3379 | ||||
| 3380 | // Now, send some data over the first pipe (38 bytes) | |||
| 3381 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3381, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3382 | ||||
| 3383 | // ... and repeat | |||
| 3384 | 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_3384 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3384 ; } } else gtest_label_testnothrow_3384 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3384, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3385 | ||||
| 3386 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3387 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3387, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3388 | ||||
| 3389 | // There was some data, so this time callback should be called | |||
| 3390 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3390, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "callback_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3391 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3391, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3392 | ||||
| 3393 | // Read the data sent, because our test callbacks are too dumb to actually | |||
| 3394 | // do it. We don't care about the content read, because we're testing | |||
| 3395 | // the callbacks, not pipes. | |||
| 3396 | char buf[80]; | |||
| 3397 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3397, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3398 | ||||
| 3399 | // Clear the status... | |||
| 3400 | callback_ok = false; | |||
| 3401 | callback2_ok = false; | |||
| 3402 | ||||
| 3403 | // And try again, using the second pipe | |||
| 3404 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3404, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3405 | ||||
| 3406 | // ... and repeat | |||
| 3407 | 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_3407 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3407 ; } } else gtest_label_testnothrow_3407 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3407, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3408 | ||||
| 3409 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3410 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3410, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3411 | ||||
| 3412 | // There was some data, so this time callback should be called | |||
| 3413 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3413, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3414 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3414, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3415 | ||||
| 3416 | // close both pipe ends | |||
| 3417 | close(pipefd[1]); | |||
| 3418 | close(pipefd[0]); | |||
| 3419 | ||||
| 3420 | close(secondpipe[1]); | |||
| 3421 | close(secondpipe[0]); | |||
| 3422 | } | |||
| 3423 | ||||
| 3424 | TEST_F(IfaceMgrTest, MultipleExternalSockets6Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("MultipleExternalSockets6Select") > 1, "test_name must not be empty"); class IfaceMgrTest_MultipleExternalSockets6Select_Test : public IfaceMgrTest { public: IfaceMgrTest_MultipleExternalSockets6Select_Test () = default; ~IfaceMgrTest_MultipleExternalSockets6Select_Test () override = default; IfaceMgrTest_MultipleExternalSockets6Select_Test (const IfaceMgrTest_MultipleExternalSockets6Select_Test & ) = delete; IfaceMgrTest_MultipleExternalSockets6Select_Test & operator=( const IfaceMgrTest_MultipleExternalSockets6Select_Test &) = delete; IfaceMgrTest_MultipleExternalSockets6Select_Test (IfaceMgrTest_MultipleExternalSockets6Select_Test && ) noexcept = delete; IfaceMgrTest_MultipleExternalSockets6Select_Test & operator=( IfaceMgrTest_MultipleExternalSockets6Select_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_MultipleExternalSockets6Select_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "MultipleExternalSockets6Select", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3424), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3424), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3424), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_MultipleExternalSockets6Select_Test >); void IfaceMgrTest_MultipleExternalSockets6Select_Test:: TestBody() { | |||
| 3425 | kea_event_handler_type_.setValue("select"); | |||
| 3426 | testMultipleExternalSockets6(); | |||
| 3427 | } | |||
| 3428 | ||||
| 3429 | TEST_F(IfaceMgrTest, MultipleExternalSockets6Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("MultipleExternalSockets6Poll") > 1 , "test_name must not be empty"); class IfaceMgrTest_MultipleExternalSockets6Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_MultipleExternalSockets6Poll_Test () = default; ~IfaceMgrTest_MultipleExternalSockets6Poll_Test () override = default; IfaceMgrTest_MultipleExternalSockets6Poll_Test (const IfaceMgrTest_MultipleExternalSockets6Poll_Test &) = delete; IfaceMgrTest_MultipleExternalSockets6Poll_Test & operator=( const IfaceMgrTest_MultipleExternalSockets6Poll_Test &) = delete; IfaceMgrTest_MultipleExternalSockets6Poll_Test (IfaceMgrTest_MultipleExternalSockets6Poll_Test &&) noexcept = delete; IfaceMgrTest_MultipleExternalSockets6Poll_Test & operator=( IfaceMgrTest_MultipleExternalSockets6Poll_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_MultipleExternalSockets6Poll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "MultipleExternalSockets6Poll", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3429), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3429), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3429), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_MultipleExternalSockets6Poll_Test >); void IfaceMgrTest_MultipleExternalSockets6Poll_Test::TestBody () { | |||
| 3430 | kea_event_handler_type_.setValue("poll"); | |||
| 3431 | testMultipleExternalSockets6(); | |||
| 3432 | } | |||
| 3433 | ||||
| 3434 | // Tests if existing external socket can be deleted and that such deletion does | |||
| 3435 | // not affect any other existing sockets. Tests uses receive6() | |||
| 3436 | void IfaceMgrTest::testDeleteExternalSockets6() { | |||
| 3437 | callback_ok = false; | |||
| 3438 | callback2_ok = false; | |||
| 3439 | ||||
| 3440 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3441 | ||||
| 3442 | // Create first pipe and register it as extra socket | |||
| 3443 | int pipefd[2]; | |||
| 3444 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3444, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3445 | 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_3445; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3445; } } else gtest_label_testnothrow_3445 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3445, ("Expected: " "ifacemgr->addExternalSocket(pipefd[0], my_callback)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3446 | ||||
| 3447 | // Let's create a second pipe and register it as well | |||
| 3448 | int secondpipe[2]; | |||
| 3449 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3449, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(secondpipe) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3450 | 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_3450; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3450 ; } } else gtest_label_testnothrow_3450 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3450, ("Expected: " "ifacemgr->addExternalSocket(secondpipe[0], my_callback2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3451 | ||||
| 3452 | // Now delete the first session socket | |||
| 3453 | 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_3453; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3453; } } else gtest_label_testnothrow_3453 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3453, ("Expected: " "ifacemgr->deleteExternalSocket(pipefd[0])" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3454 | ||||
| 3455 | // Now check whether the second callback is still functional | |||
| 3456 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3456, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3457 | ||||
| 3458 | // ... and repeat | |||
| 3459 | Pkt6Ptr pkt6; | |||
| 3460 | 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_3460 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3460 ; } } else gtest_label_testnothrow_3460 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3460, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3461 | ||||
| 3462 | // IfaceMgr should not process control socket data as incoming packets | |||
| 3463 | EXPECT_FALSE(pkt6)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(pkt6))) ; else ::testing ::internal::AssertHelper(::testing::TestPartResult::kNonFatalFailure , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3463, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "pkt6", "true", "false") .c_str()) = ::testing::Message(); | |||
| 3464 | ||||
| 3465 | // There was some data, so this time callback should be called | |||
| 3466 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3466, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3467 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3467, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback2_ok", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3468 | ||||
| 3469 | // Let's reset the status | |||
| 3470 | callback_ok = false; | |||
| 3471 | callback2_ok = false; | |||
| 3472 | ||||
| 3473 | // Now let's send something over the first callback that was unregistered. | |||
| 3474 | // We should NOT receive any callback. | |||
| 3475 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3475, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3476 | ||||
| 3477 | // Now check that the first callback is NOT called. | |||
| 3478 | 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_3478 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3478 ; } } else gtest_label_testnothrow_3478 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3478, ("Expected: " "pkt6 = ifacemgr->receive6(1)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3479 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3479, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "callback_ok", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3480 | ||||
| 3481 | // close both pipe ends | |||
| 3482 | close(pipefd[1]); | |||
| 3483 | close(pipefd[0]); | |||
| 3484 | ||||
| 3485 | close(secondpipe[1]); | |||
| 3486 | close(secondpipe[0]); | |||
| 3487 | } | |||
| 3488 | ||||
| 3489 | TEST_F(IfaceMgrTest, DeleteExternalSockets6Select)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DeleteExternalSockets6Select") > 1 , "test_name must not be empty"); class IfaceMgrTest_DeleteExternalSockets6Select_Test : public IfaceMgrTest { public: IfaceMgrTest_DeleteExternalSockets6Select_Test () = default; ~IfaceMgrTest_DeleteExternalSockets6Select_Test () override = default; IfaceMgrTest_DeleteExternalSockets6Select_Test (const IfaceMgrTest_DeleteExternalSockets6Select_Test &) = delete; IfaceMgrTest_DeleteExternalSockets6Select_Test & operator=( const IfaceMgrTest_DeleteExternalSockets6Select_Test &) = delete; IfaceMgrTest_DeleteExternalSockets6Select_Test (IfaceMgrTest_DeleteExternalSockets6Select_Test &&) noexcept = delete; IfaceMgrTest_DeleteExternalSockets6Select_Test & operator=( IfaceMgrTest_DeleteExternalSockets6Select_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_DeleteExternalSockets6Select_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DeleteExternalSockets6Select", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3489), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3489), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3489), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DeleteExternalSockets6Select_Test >); void IfaceMgrTest_DeleteExternalSockets6Select_Test::TestBody () { | |||
| 3490 | kea_event_handler_type_.setValue("select"); | |||
| 3491 | testDeleteExternalSockets6(); | |||
| 3492 | } | |||
| 3493 | ||||
| 3494 | TEST_F(IfaceMgrTest, DeleteExternalSockets6Poll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("DeleteExternalSockets6Poll") > 1, "test_name must not be empty"); class IfaceMgrTest_DeleteExternalSockets6Poll_Test : public IfaceMgrTest { public: IfaceMgrTest_DeleteExternalSockets6Poll_Test () = default; ~IfaceMgrTest_DeleteExternalSockets6Poll_Test() override = default; IfaceMgrTest_DeleteExternalSockets6Poll_Test (const IfaceMgrTest_DeleteExternalSockets6Poll_Test &) = delete; IfaceMgrTest_DeleteExternalSockets6Poll_Test & operator =( const IfaceMgrTest_DeleteExternalSockets6Poll_Test &) = delete; IfaceMgrTest_DeleteExternalSockets6Poll_Test (IfaceMgrTest_DeleteExternalSockets6Poll_Test &&) noexcept = delete; IfaceMgrTest_DeleteExternalSockets6Poll_Test & operator=( IfaceMgrTest_DeleteExternalSockets6Poll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_DeleteExternalSockets6Poll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DeleteExternalSockets6Poll", nullptr, nullptr, ::testing:: internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3494), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3494), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3494), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DeleteExternalSockets6Poll_Test >); void IfaceMgrTest_DeleteExternalSockets6Poll_Test::TestBody () { | |||
| 3495 | kea_event_handler_type_.setValue("poll"); | |||
| 3496 | testDeleteExternalSockets6(); | |||
| 3497 | } | |||
| 3498 | ||||
| 3499 | // Tests that an existing external socket that becomes invalid | |||
| 3500 | // is detected and ignored, without affecting other sockets. | |||
| 3501 | // Tests uses receive6() without queuing. | |||
| 3502 | TEST_F(IfaceMgrTest, unusableExternalSockets6DirectSelect)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets6DirectSelect" ) > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets6DirectSelect_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets6DirectSelect_Test () = default; ~IfaceMgrTest_unusableExternalSockets6DirectSelect_Test () override = default; IfaceMgrTest_unusableExternalSockets6DirectSelect_Test (const IfaceMgrTest_unusableExternalSockets6DirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets6DirectSelect_Test & operator=( const IfaceMgrTest_unusableExternalSockets6DirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets6DirectSelect_Test (IfaceMgrTest_unusableExternalSockets6DirectSelect_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets6DirectSelect_Test & operator=( IfaceMgrTest_unusableExternalSockets6DirectSelect_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets6DirectSelect_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets6DirectSelect", nullptr, nullptr, :: testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3502), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3502), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3502), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets6DirectSelect_Test >); void IfaceMgrTest_unusableExternalSockets6DirectSelect_Test ::TestBody() { | |||
| 3503 | kea_event_handler_type_.setValue("select"); | |||
| 3504 | unusableExternalSockets6Test(); | |||
| 3505 | } | |||
| 3506 | ||||
| 3507 | TEST_F(IfaceMgrTest, unusableExternalSockets6DirectPoll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets6DirectPoll") > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets6DirectPoll_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets6DirectPoll_Test () = default; ~IfaceMgrTest_unusableExternalSockets6DirectPoll_Test () override = default; IfaceMgrTest_unusableExternalSockets6DirectPoll_Test (const IfaceMgrTest_unusableExternalSockets6DirectPoll_Test & ) = delete; IfaceMgrTest_unusableExternalSockets6DirectPoll_Test & operator=( const IfaceMgrTest_unusableExternalSockets6DirectPoll_Test &) = delete; IfaceMgrTest_unusableExternalSockets6DirectPoll_Test (IfaceMgrTest_unusableExternalSockets6DirectPoll_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets6DirectPoll_Test & operator=( IfaceMgrTest_unusableExternalSockets6DirectPoll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets6DirectPoll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets6DirectPoll", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3507), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3507), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3507), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets6DirectPoll_Test >); void IfaceMgrTest_unusableExternalSockets6DirectPoll_Test ::TestBody() { | |||
| 3508 | kea_event_handler_type_.setValue("poll"); | |||
| 3509 | unusableExternalSockets6Test(); | |||
| 3510 | } | |||
| 3511 | ||||
| 3512 | // Tests that an existing external socket that becomes invalid | |||
| 3513 | // is detected and ignored, without affecting other sockets. | |||
| 3514 | // Tests uses receive6() with queuing. | |||
| 3515 | TEST_F(IfaceMgrTest, unusableExternalSockets6IndirectSelect)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets6IndirectSelect" ) > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test () = default; ~IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test () override = default; IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test (const IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test & operator=( const IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test &) = delete; IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test (IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test & operator=( IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets6IndirectSelect", nullptr, nullptr, ::testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3515), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3515), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3515), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test >); void IfaceMgrTest_unusableExternalSockets6IndirectSelect_Test ::TestBody() { | |||
| 3516 | kea_event_handler_type_.setValue("select"); | |||
| 3517 | unusableExternalSockets6Test(true); | |||
| 3518 | } | |||
| 3519 | ||||
| 3520 | TEST_F(IfaceMgrTest, unusableExternalSockets6IndirectPoll)static_assert(sizeof("IfaceMgrTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("unusableExternalSockets6IndirectPoll" ) > 1, "test_name must not be empty"); class IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test : public IfaceMgrTest { public: IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test () = default; ~IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test () override = default; IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test (const IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test &) = delete; IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test & operator=( const IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test &) = delete; IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test (IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test && ) noexcept = delete; IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test & operator=( IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unusableExternalSockets6IndirectPoll", nullptr, nullptr, :: testing::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3520), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3520), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3520), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test >); void IfaceMgrTest_unusableExternalSockets6IndirectPoll_Test ::TestBody() { | |||
| 3521 | kea_event_handler_type_.setValue("poll"); | |||
| 3522 | unusableExternalSockets6Test(true); | |||
| 3523 | } | |||
| 3524 | ||||
| 3525 | /// @brief Test fixture for logs. | |||
| 3526 | class IfaceMgrLogTest : public LogContentTest { | |||
| 3527 | public: | |||
| 3528 | /// @brief Create the interface manager. | |||
| 3529 | IfaceMgrLogTest() { | |||
| 3530 | ifacemgr_.reset(new NakedIfaceMgr()); | |||
| 3531 | } | |||
| 3532 | ||||
| 3533 | /// @brief Destroy the interface manager. | |||
| 3534 | ~IfaceMgrLogTest() { | |||
| 3535 | ifacemgr_.reset(); | |||
| 3536 | } | |||
| 3537 | ||||
| 3538 | /// @brief Perform external socket operations in another thread. | |||
| 3539 | void testThread() { | |||
| 3540 | thread th([this]() { | |||
| 3541 | // Create pipe mainly to use a not fake fd. | |||
| 3542 | int pipefd[2]; | |||
| 3543 | ASSERT_TRUE(pipe(pipefd) == 0)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(pipe(pipefd) == 0)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3543, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "pipe(pipefd) == 0", "false", "true") .c_str()) = ::testing ::Message(); | |||
| 3544 | EXPECT_NO_THROW(ifacemgr_->addExternalSocket(pipefd[0], 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr_->addExternalSocket(pipefd[0], 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_3544; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3544; } } else gtest_label_testnothrow_3544 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3544, ("Expected: " "ifacemgr_->addExternalSocket(pipefd[0], 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3545 | EXPECT_TRUE(ifacemgr_->isExternalSocket(pipefd[0]))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr_->isExternalSocket (pipefd[0]))) ; else ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3545, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_->isExternalSocket(pipefd[0])", "false", "true" ) .c_str()) = ::testing::Message(); | |||
| 3546 | 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_3546; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3546; } } else gtest_label_testnothrow_3546 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3546, ("Expected: " "ifacemgr_->deleteExternalSocket(pipefd[0])" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3547 | EXPECT_NO_THROW(ifacemgr_->deleteAllExternalSockets())switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr_->deleteAllExternalSockets(); } 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_3547; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3547; } } else gtest_label_testnothrow_3547 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3547, ("Expected: " "ifacemgr_->deleteAllExternalSockets()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3548 | close(pipefd[1]); | |||
| 3549 | close(pipefd[0]); | |||
| 3550 | }); | |||
| 3551 | th.join(); | |||
| 3552 | } | |||
| 3553 | ||||
| 3554 | /// @brief The interface manager. | |||
| 3555 | boost::shared_ptr<NakedIfaceMgr> ifacemgr_; | |||
| 3556 | }; | |||
| 3557 | ||||
| 3558 | // Tests that external socket operations log errors when not in the main thread. | |||
| 3559 | TEST_F(IfaceMgrLogTest, externalSocketOtherThread)static_assert(sizeof("IfaceMgrLogTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("externalSocketOtherThread") > 1, "test_name must not be empty" ); class IfaceMgrLogTest_externalSocketOtherThread_Test : public IfaceMgrLogTest { public: IfaceMgrLogTest_externalSocketOtherThread_Test () = default; ~IfaceMgrLogTest_externalSocketOtherThread_Test () override = default; IfaceMgrLogTest_externalSocketOtherThread_Test (const IfaceMgrLogTest_externalSocketOtherThread_Test &) = delete; IfaceMgrLogTest_externalSocketOtherThread_Test & operator=( const IfaceMgrLogTest_externalSocketOtherThread_Test &) = delete; IfaceMgrLogTest_externalSocketOtherThread_Test (IfaceMgrLogTest_externalSocketOtherThread_Test &&) noexcept = delete; IfaceMgrLogTest_externalSocketOtherThread_Test & operator=( IfaceMgrLogTest_externalSocketOtherThread_Test && ) noexcept = delete; private: void TestBody() override; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrLogTest_externalSocketOtherThread_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrLogTest" , "externalSocketOtherThread", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3559), (::testing::internal::GetTypeId<IfaceMgrLogTest> ()), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3559), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3559), new ::testing::internal::TestFactoryImpl<IfaceMgrLogTest_externalSocketOtherThread_Test >); void IfaceMgrLogTest_externalSocketOtherThread_Test::TestBody () { | |||
| 3560 | ASSERT_TRUE(ifacemgr_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr_)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3560, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3561 | ASSERT_TRUE(ifacemgr_->getCheckThreadId())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr_->getCheckThreadId ())) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3561, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_->getCheckThreadId()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 3562 | ||||
| 3563 | testThread(); | |||
| 3564 | EXPECT_EQ(1, countFile("DHCP_ADD_EXTERNAL_SOCKET_BAD_THREAD"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "countFile(\"DHCP_ADD_EXTERNAL_SOCKET_BAD_THREAD\")" , 1, countFile("DHCP_ADD_EXTERNAL_SOCKET_BAD_THREAD")))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3564, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3565 | EXPECT_EQ(1, countFile("DHCP_DELETE_EXTERNAL_SOCKET_BAD_THREAD"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "countFile(\"DHCP_DELETE_EXTERNAL_SOCKET_BAD_THREAD\")" , 1, countFile("DHCP_DELETE_EXTERNAL_SOCKET_BAD_THREAD")))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3565, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3566 | EXPECT_EQ(1, countFile("DHCP_DELETE_ALL_EXTERNAL_SOCKETS_BAD_THREAD"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "countFile(\"DHCP_DELETE_ALL_EXTERNAL_SOCKETS_BAD_THREAD\")" , 1, countFile("DHCP_DELETE_ALL_EXTERNAL_SOCKETS_BAD_THREAD") ))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3566, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3567 | } | |||
| 3568 | ||||
| 3569 | // Tests that errors are logged only when enabled. | |||
| 3570 | TEST_F(IfaceMgrLogTest, externalSocketOtherThreadNoLog)static_assert(sizeof("IfaceMgrLogTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("externalSocketOtherThreadNoLog") > 1, "test_name must not be empty"); class IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test : public IfaceMgrLogTest { public: IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test () = default; ~IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test () override = default; IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test (const IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test & ) = delete; IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test & operator=( const IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test &) = delete; IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test (IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test && ) noexcept = delete; IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test & operator=( IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrLogTest" , "externalSocketOtherThreadNoLog", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3570), (::testing::internal::GetTypeId<IfaceMgrLogTest> ()), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3570), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3570), new ::testing::internal::TestFactoryImpl<IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test >); void IfaceMgrLogTest_externalSocketOtherThreadNoLog_Test ::TestBody() { | |||
| 3571 | ASSERT_TRUE(ifacemgr_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr_)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3571, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3572 | ifacemgr_->setCheckThreadId(false); | |||
| 3573 | ASSERT_FALSE(ifacemgr_->getCheckThreadId())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr_->getCheckThreadId ()))) ; else return ::testing::internal::AssertHelper(::testing ::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3573, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_->getCheckThreadId()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3574 | ||||
| 3575 | testThread(); | |||
| 3576 | EXPECT_EQ(0, countFile("DHCP"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "countFile(\"DHCP\")" , 0, countFile("DHCP")))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3576, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3577 | } | |||
| 3578 | ||||
| 3579 | // Tests that calling twice addExternalSocket on the same value warns. | |||
| 3580 | TEST_F(IfaceMgrLogTest, addExternalSocketAlreadyExists)static_assert(sizeof("IfaceMgrLogTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("addExternalSocketAlreadyExists") > 1, "test_name must not be empty"); class IfaceMgrLogTest_addExternalSocketAlreadyExists_Test : public IfaceMgrLogTest { public: IfaceMgrLogTest_addExternalSocketAlreadyExists_Test () = default; ~IfaceMgrLogTest_addExternalSocketAlreadyExists_Test () override = default; IfaceMgrLogTest_addExternalSocketAlreadyExists_Test (const IfaceMgrLogTest_addExternalSocketAlreadyExists_Test & ) = delete; IfaceMgrLogTest_addExternalSocketAlreadyExists_Test & operator=( const IfaceMgrLogTest_addExternalSocketAlreadyExists_Test &) = delete; IfaceMgrLogTest_addExternalSocketAlreadyExists_Test (IfaceMgrLogTest_addExternalSocketAlreadyExists_Test && ) noexcept = delete; IfaceMgrLogTest_addExternalSocketAlreadyExists_Test & operator=( IfaceMgrLogTest_addExternalSocketAlreadyExists_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrLogTest_addExternalSocketAlreadyExists_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrLogTest" , "addExternalSocketAlreadyExists", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3580), (::testing::internal::GetTypeId<IfaceMgrLogTest> ()), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3580), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3580), new ::testing::internal::TestFactoryImpl<IfaceMgrLogTest_addExternalSocketAlreadyExists_Test >); void IfaceMgrLogTest_addExternalSocketAlreadyExists_Test ::TestBody() { | |||
| 3581 | ASSERT_TRUE(ifacemgr_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr_)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3581, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3582 | ASSERT_NO_THROW(ifacemgr_->addExternalSocket(100, 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr_->addExternalSocket(100, 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_3582; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3582; } } else gtest_label_testnothrow_3582 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3582, ("Expected: " "ifacemgr_->addExternalSocket(100, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3583 | EXPECT_EQ(0, countFile("DHCP_ADD_EXTERNAL_SOCKET_ALREADY_EXISTS"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "countFile(\"DHCP_ADD_EXTERNAL_SOCKET_ALREADY_EXISTS\")" , 0, countFile("DHCP_ADD_EXTERNAL_SOCKET_ALREADY_EXISTS")))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3583, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3584 | ASSERT_NO_THROW(ifacemgr_->addExternalSocket(100, 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr_->addExternalSocket(100, 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_3584; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3584; } } else gtest_label_testnothrow_3584 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3584, ("Expected: " "ifacemgr_->addExternalSocket(100, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3585 | EXPECT_EQ(1, countFile("DHCP_ADD_EXTERNAL_SOCKET_ALREADY_EXISTS"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "countFile(\"DHCP_ADD_EXTERNAL_SOCKET_ALREADY_EXISTS\")" , 1, countFile("DHCP_ADD_EXTERNAL_SOCKET_ALREADY_EXISTS")))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3585, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3586 | } | |||
| 3587 | ||||
| 3588 | // Tests that calling twice deleteExternalSocket on the same value warns. | |||
| 3589 | TEST_F(IfaceMgrLogTest, deleteExternalSocketNotFound)static_assert(sizeof("IfaceMgrLogTest") > 1, "test_suite_name must not be empty" ); static_assert(sizeof("deleteExternalSocketNotFound") > 1 , "test_name must not be empty"); class IfaceMgrLogTest_deleteExternalSocketNotFound_Test : public IfaceMgrLogTest { public: IfaceMgrLogTest_deleteExternalSocketNotFound_Test () = default; ~IfaceMgrLogTest_deleteExternalSocketNotFound_Test () override = default; IfaceMgrLogTest_deleteExternalSocketNotFound_Test (const IfaceMgrLogTest_deleteExternalSocketNotFound_Test & ) = delete; IfaceMgrLogTest_deleteExternalSocketNotFound_Test & operator=( const IfaceMgrLogTest_deleteExternalSocketNotFound_Test &) = delete; IfaceMgrLogTest_deleteExternalSocketNotFound_Test (IfaceMgrLogTest_deleteExternalSocketNotFound_Test && ) noexcept = delete; IfaceMgrLogTest_deleteExternalSocketNotFound_Test & operator=( IfaceMgrLogTest_deleteExternalSocketNotFound_Test &&) noexcept = delete; private: void TestBody() override ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrLogTest_deleteExternalSocketNotFound_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrLogTest" , "deleteExternalSocketNotFound", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3589), (::testing::internal::GetTypeId<IfaceMgrLogTest> ()), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3589), ::testing::internal::SuiteApiResolver< IfaceMgrLogTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3589), new ::testing::internal::TestFactoryImpl<IfaceMgrLogTest_deleteExternalSocketNotFound_Test >); void IfaceMgrLogTest_deleteExternalSocketNotFound_Test ::TestBody() { | |||
| 3590 | ASSERT_TRUE(ifacemgr_)switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr_)) ; else return ::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3590, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3591 | ASSERT_NO_THROW(ifacemgr_->addExternalSocket(100, 0))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr_->addExternalSocket(100, 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_3591; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3591; } } else gtest_label_testnothrow_3591 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3591, ("Expected: " "ifacemgr_->addExternalSocket(100, 0)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3592 | EXPECT_TRUE(ifacemgr_->isExternalSocket(100))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(ifacemgr_->isExternalSocket (100))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3592, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_->isExternalSocket(100)", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 3593 | ASSERT_NO_THROW(ifacemgr_->deleteExternalSocket(100))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr_->deleteExternalSocket(100); } 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_3593; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3593; } } else gtest_label_testnothrow_3593 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3593, ("Expected: " "ifacemgr_->deleteExternalSocket(100)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3594 | EXPECT_EQ(0, countFile("DHCP_DELETE_EXTERNAL_SOCKET_NOT_FOUND"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("0", "countFile(\"DHCP_DELETE_EXTERNAL_SOCKET_NOT_FOUND\")" , 0, countFile("DHCP_DELETE_EXTERNAL_SOCKET_NOT_FOUND")))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3594, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3595 | EXPECT_FALSE(ifacemgr_->isExternalSocket(100))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(ifacemgr_->isExternalSocket (100)))) ; else ::testing::internal::AssertHelper(::testing:: TestPartResult::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3595, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr_->isExternalSocket(100)", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3596 | ASSERT_NO_THROW(ifacemgr_->deleteExternalSocket(100))switch (0) case 0: default: if (::testing::internal::TrueWithString gtest_msg{}) { try { if (::testing::internal::AlwaysTrue()) { ifacemgr_->deleteExternalSocket(100); } 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_3596; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3596; } } else gtest_label_testnothrow_3596 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3596, ("Expected: " "ifacemgr_->deleteExternalSocket(100)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3597 | EXPECT_EQ(1, countFile("DHCP_DELETE_EXTERNAL_SOCKET_NOT_FOUND"))switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal::EqHelper::Compare("1", "countFile(\"DHCP_DELETE_EXTERNAL_SOCKET_NOT_FOUND\")" , 1, countFile("DHCP_DELETE_EXTERNAL_SOCKET_NOT_FOUND")))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3597, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3598 | } | |||
| 3599 | ||||
| 3600 | // Test checks if the unicast sockets can be opened. | |||
| 3601 | // This test is now disabled, because there is no reliable way to test it. We | |||
| 3602 | // can't even use loopback, because openSockets() skips loopback interface | |||
| 3603 | // (as it should be, because DHCP server is not supposed to listen on loopback). | |||
| 3604 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_DISABLED_openUnicastSockets_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DISABLED_openUnicastSockets", nullptr, nullptr, ::testing:: internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3604), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3604), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3604), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DISABLED_openUnicastSockets_Test >); void IfaceMgrTest_DISABLED_openUnicastSockets_Test::TestBody () { | |||
| 3605 | /// @todo Need to implement a test that is able to check whether we can open | |||
| 3606 | /// unicast sockets. There are 2 problems with it: | |||
| 3607 | /// 1. We need to have a non-link-local address on an interface that is | |||
| 3608 | /// up, running, IPv6 and multicast capable | |||
| 3609 | /// 2. We need that information on every OS that we run tests on. So far | |||
| 3610 | /// we are only supporting interface detection in Linux. | |||
| 3611 | /// | |||
| 3612 | /// To achieve this, we will probably need a pre-test setup, similar to what | |||
| 3613 | /// BIND9 is doing (i.e. configuring well known addresses on loopback). | |||
| 3614 | ||||
| 3615 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3616 | ||||
| 3617 | // Get the interface (todo: which interface) | |||
| 3618 | IfacePtr iface = ifacemgr->getIface("eth0"); | |||
| 3619 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3619, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "iface", "false", "true") .c_str()) = ::testing::Message(); | |||
| 3620 | iface->inactive6_ = false; | |||
| 3621 | ||||
| 3622 | // Tell the interface that it should bind to this global interface | |||
| 3623 | 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_3623; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3623; } } else gtest_label_testnothrow_3623 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3623, ("Expected: " "iface->addUnicast(IOAddress(\"2001:db8::1\"))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3624 | ||||
| 3625 | // Tell IfaceMgr to open sockets. This should trigger at least 2 sockets | |||
| 3626 | // to open on eth0: link-local and global. On some systems (Linux), an | |||
| 3627 | // additional socket for multicast may be opened. | |||
| 3628 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3628, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->openSockets6(PORT1)", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 3629 | ||||
| 3630 | const Iface::SocketCollection& sockets = iface->getSockets(); | |||
| 3631 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3631, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3632 | ||||
| 3633 | // Global unicast should be first | |||
| 3634 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3634, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "getSocketByAddr(sockets, IOAddress(\"2001:db8::1\"))", "false" , "true") .c_str()) = ::testing::Message(); | |||
| 3635 | 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 , "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc", 3635, :: testing::internal::GetBoolAssertionFailureMessage( gtest_ar_, "getSocketByAddr(sockets, IOAddress(\"figure-out-link-local-addr\"))" , "false", "true") .c_str()) = ::testing::Message(); | |||
| 3636 | } | |||
| 3637 | ||||
| 3638 | // Checks if there is a protection against unicast duplicates. | |||
| 3639 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_unicastDuplicates_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "unicastDuplicates", nullptr, nullptr, ::testing::internal:: CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3639), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3639), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3639), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_unicastDuplicates_Test >); void IfaceMgrTest_unicastDuplicates_Test::TestBody() { | |||
| 3640 | NakedIfaceMgr ifacemgr; | |||
| 3641 | ||||
| 3642 | IfacePtr iface = ifacemgr.getIface(LOOPBACK_NAME); | |||
| 3643 | if (!iface) { | |||
| 3644 | cout << "Local loopback interface not found. Skipping test. " << endl; | |||
| 3645 | return; | |||
| 3646 | } | |||
| 3647 | ||||
| 3648 | // Tell the interface that it should bind to this global interface | |||
| 3649 | // It is the first attempt so it should succeed | |||
| 3650 | 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_3650; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3650; } } else gtest_label_testnothrow_3650 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3650, ("Expected: " "iface->addUnicast(IOAddress(\"2001:db8::1\"))" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3651 | ||||
| 3652 | // Tell the interface that it should bind to this global interface | |||
| 3653 | // It is the second attempt so it should fail | |||
| 3654 | 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_3654 ; } 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_3654; } 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_3654; } } else gtest_label_testthrow_3654 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3654, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 3655 | } | |||
| 3656 | ||||
| 3657 | // This test requires addresses 2001:db8:15c::1/128 and fe80::1/64 to be | |||
| 3658 | // configured on loopback interface | |||
| 3659 | // | |||
| 3660 | // Useful commands: | |||
| 3661 | // ip a a 2001:db8:15c::1/128 dev lo | |||
| 3662 | // ip a a fe80::1/64 dev lo | |||
| 3663 | // | |||
| 3664 | // If you do not issue those commands before running this test, it will fail. | |||
| 3665 | 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; [[maybe_unused ]] static ::testing::TestInfo* const test_info_; }; ::testing ::TestInfo* const IfaceMgrTest_DISABLED_getSocket_Test::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "DISABLED_getSocket", nullptr, nullptr, ::testing::internal ::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3665), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3665), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3665), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_DISABLED_getSocket_Test >); void IfaceMgrTest_DISABLED_getSocket_Test::TestBody() { | |||
| 3666 | // Testing socket operation in a portable way is tricky | |||
| 3667 | // without interface detection implemented. | |||
| 3668 | ||||
| 3669 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3670 | ||||
| 3671 | IOAddress lo_addr("::1"); | |||
| 3672 | IOAddress link_local("fe80::1"); | |||
| 3673 | IOAddress global("2001:db8:15c::1"); | |||
| 3674 | ||||
| 3675 | IOAddress dst_link_local("fe80::dead:beef"); | |||
| 3676 | IOAddress dst_global("2001:db8:15c::dead:beef"); | |||
| 3677 | ||||
| 3678 | // Bind loopback address | |||
| 3679 | int socket1 = ifacemgr->openSocket(LOOPBACK_NAME, lo_addr, 10547); | |||
| 3680 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3680, gtest_ar.failure_message()) = ::testing::Message(); // socket >= 0 | |||
| 3681 | ||||
| 3682 | // Bind link-local address | |||
| 3683 | int socket2 = ifacemgr->openSocket(LOOPBACK_NAME, link_local, 10547); | |||
| 3684 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3684, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3685 | ||||
| 3686 | int socket3 = ifacemgr->openSocket(LOOPBACK_NAME, global, 10547); | |||
| 3687 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3687, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3688 | ||||
| 3689 | // Let's make sure those sockets are unique | |||
| 3690 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3690, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3691 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3691, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3692 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3692, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3693 | ||||
| 3694 | // Create a packet | |||
| 3695 | Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, 123)); | |||
| 3696 | pkt6->setIface(LOOPBACK_NAME); | |||
| 3697 | pkt6->setIndex(LOOPBACK_INDEX); | |||
| 3698 | ||||
| 3699 | // Check that packets sent to link-local will get socket bound to link local | |||
| 3700 | pkt6->setLocalAddr(global); | |||
| 3701 | pkt6->setRemoteAddr(dst_global); | |||
| 3702 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3702, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3703 | ||||
| 3704 | // Check that packets sent to link-local will get socket bound to link local | |||
| 3705 | pkt6->setLocalAddr(link_local); | |||
| 3706 | pkt6->setRemoteAddr(dst_link_local); | |||
| 3707 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3707, gtest_ar.failure_message()) = ::testing::Message(); | |||
| 3708 | ||||
| 3709 | // Close sockets here because the following tests will want to | |||
| 3710 | // open sockets on the same ports. | |||
| 3711 | ifacemgr->closeSockets(); | |||
| 3712 | } | |||
| 3713 | ||||
| 3714 | // Verifies DHCPv4 behavior of configureDHCPPacketQueue() | |||
| 3715 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_configureDHCPPacketQueueTest4_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "configureDHCPPacketQueueTest4", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3715), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3715), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3715), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_configureDHCPPacketQueueTest4_Test >); void IfaceMgrTest_configureDHCPPacketQueueTest4_Test:: TestBody() { | |||
| 3716 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3717 | ||||
| 3718 | // First let's make sure there is no queue and no thread. | |||
| 3719 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3719, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3720 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3720, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3721 | ||||
| 3722 | bool queue_enabled = false; | |||
| 3723 | // Given an empty pointer, we should default to no queue. | |||
| 3724 | data::ConstElementPtr queue_control; | |||
| 3725 | 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_3725; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3725 ; } } else gtest_label_testnothrow_3725 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3725, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3726 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3726, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3727 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3727, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3728 | // configureDHCPPacketQueue() should never start the thread. | |||
| 3729 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3729, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3730 | ||||
| 3731 | // Verify that calling startDHCPReceiver with no queue, does NOT start the thread. | |||
| 3732 | 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_3732 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3732 ; } } else gtest_label_testnothrow_3732 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3732, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3733 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3733, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3734 | ||||
| 3735 | // Now let's try with a populated queue control, but with enable-queue = false. | |||
| 3736 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, false); | |||
| 3737 | 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_3737; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3737 ; } } else gtest_label_testnothrow_3737 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3737, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3738 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3738, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3739 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3739, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3740 | // configureDHCPPacketQueue() should never start the thread. | |||
| 3741 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3741, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3742 | ||||
| 3743 | // Now let's enable the queue. | |||
| 3744 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, true); | |||
| 3745 | 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_3745; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3745 ; } } else gtest_label_testnothrow_3745 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3745, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3746 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3746, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3747 | // Verify we have correctly created the queue. | |||
| 3748 | 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 ()); } | |||
| 3749 | << 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 ()); }; | |||
| 3750 | // configureDHCPPacketQueue() should never start the thread. | |||
| 3751 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3751, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3752 | ||||
| 3753 | // Calling startDHCPReceiver with a queue, should start the thread. | |||
| 3754 | 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_3754 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3754 ; } } else gtest_label_testnothrow_3754 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3754, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3755 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3755, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 3756 | ||||
| 3757 | // Verify that calling startDHCPReceiver when the thread is running, throws. | |||
| 3758 | 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_3758; } 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_3758; } 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_3758; } } else gtest_label_testthrow_3758 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3758, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 3759 | ||||
| 3760 | // Create a disabled config. | |||
| 3761 | queue_control = makeQueueConfig(PacketQueueMgr4::DEFAULT_QUEUE_TYPE4, 500, false); | |||
| 3762 | ||||
| 3763 | // Trying to reconfigure with a running thread should throw. | |||
| 3764 | 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_3765 ; } 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_3765; } 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_3765; } } else gtest_label_testthrow_3765 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3765, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 3765 | 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_3765 ; } 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_3765; } 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_3765; } } else gtest_label_testthrow_3765 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3765, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 3766 | ||||
| 3767 | // We should still have our queue and the thread should still be running. | |||
| 3768 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3768, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "false", "true") .c_str() ) = ::testing::Message(); | |||
| 3769 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3769, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 3770 | ||||
| 3771 | // Now let's stop stop the thread. | |||
| 3772 | 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_3772 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3772 ; } } else gtest_label_testnothrow_3772 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3772, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3773 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3773, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3774 | // Stopping the thread should not destroy the queue. | |||
| 3775 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3775, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "false", "true") .c_str() ) = ::testing::Message(); | |||
| 3776 | ||||
| 3777 | // Reconfigure with the queue turned off. We should have neither queue nor thread. | |||
| 3778 | 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_3778; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3778 ; } } else gtest_label_testnothrow_3778 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3778, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3779 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3779, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue4()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3780 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3780, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3781 | } | |||
| 3782 | ||||
| 3783 | // Verifies DHCPv6 behavior of configureDHCPPacketQueue() | |||
| 3784 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::testing::TestInfo* const IfaceMgrTest_configureDHCPPacketQueueTest6_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "IfaceMgrTest" , "configureDHCPPacketQueueTest6", nullptr, nullptr, ::testing ::internal::CodeLocation("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3784), (::testing::internal::GetTypeId<IfaceMgrTest>( )), ::testing::internal::SuiteApiResolver< IfaceMgrTest> ::GetSetUpCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3784), ::testing::internal::SuiteApiResolver< IfaceMgrTest >::GetTearDownCaseOrSuite("../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3784), new ::testing::internal::TestFactoryImpl<IfaceMgrTest_configureDHCPPacketQueueTest6_Test >); void IfaceMgrTest_configureDHCPPacketQueueTest6_Test:: TestBody() { | |||
| 3785 | scoped_ptr<NakedIfaceMgr> ifacemgr(new NakedIfaceMgr()); | |||
| 3786 | ||||
| 3787 | // First let's make sure there is no queue and no thread. | |||
| 3788 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3788, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3789 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3789, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3790 | ||||
| 3791 | bool queue_enabled = false; | |||
| 3792 | // Given an empty pointer, we should default to no queue. | |||
| 3793 | data::ConstElementPtr queue_control; | |||
| 3794 | 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_3794; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3794 ; } } else gtest_label_testnothrow_3794 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3794, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(2, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3795 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3795, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3796 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3796, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3797 | // configureDHCPPacketQueue() should never start the thread. | |||
| 3798 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3798, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3799 | ||||
| 3800 | // Verify that calling startDHCPReceiver with no queue, does NOT start the thread. | |||
| 3801 | 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_3801 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3801 ; } } else gtest_label_testnothrow_3801 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3801, ("Expected: " "ifacemgr->startDHCPReceiver(2)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3802 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3802, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3803 | ||||
| 3804 | // Now let's try with a populated queue control, but with enable-queue = false. | |||
| 3805 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, false); | |||
| 3806 | 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_3806; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3806 ; } } else gtest_label_testnothrow_3806 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3806, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3807 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3807, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "true", "false") .c_str()) = ::testing::Message (); | |||
| 3808 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3808, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3809 | // configureDHCPPacketQueue() should never start the thread. | |||
| 3810 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3810, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3811 | ||||
| 3812 | // Now let's enable the queue. | |||
| 3813 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, true); | |||
| 3814 | 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_3814; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3814 ; } } else gtest_label_testnothrow_3814 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3814, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3815 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3815, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "queue_enabled", "false", "true") .c_str()) = ::testing::Message (); | |||
| 3816 | // Verify we have correctly created the queue. | |||
| 3817 | 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 ()); } | |||
| 3818 | << 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 ()); }; | |||
| 3819 | // configureDHCPPacketQueue() should never start the thread. | |||
| 3820 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3820, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3821 | ||||
| 3822 | // Calling startDHCPReceiver with a queue, should start the thread. | |||
| 3823 | 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_3823 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3823 ; } } else gtest_label_testnothrow_3823 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3823, ("Expected: " "ifacemgr->startDHCPReceiver(10)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3824 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3824, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 3825 | ||||
| 3826 | // Verify that calling startDHCPReceiver when the thread is running, throws. | |||
| 3827 | 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_3827; } 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_3827; } 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_3827; } } else gtest_label_testthrow_3827 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3827, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 3828 | ||||
| 3829 | // Create a disabled config. | |||
| 3830 | queue_control = makeQueueConfig(PacketQueueMgr6::DEFAULT_QUEUE_TYPE6, 500, false); | |||
| 3831 | ||||
| 3832 | // Trying to reconfigure with a running thread should throw. | |||
| 3833 | 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_3834 ; } 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_3834; } 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_3834; } } else gtest_label_testthrow_3834 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3834, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 3834 | 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_3834 ; } 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_3834; } 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_3834; } } else gtest_label_testthrow_3834 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3834, gtest_msg.value.c_str()) = ::testing::Message(); | |||
| 3835 | ||||
| 3836 | // We should still have our queue and the thread should still be running. | |||
| 3837 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3837, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "false", "true") .c_str() ) = ::testing::Message(); | |||
| 3838 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3838, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "false", "true") .c_str ()) = ::testing::Message(); | |||
| 3839 | ||||
| 3840 | // Now let's stop stop the thread. | |||
| 3841 | 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_3841 ; } catch (...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3841 ; } } else gtest_label_testnothrow_3841 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3841, ("Expected: " "ifacemgr->stopDHCPReceiver()" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message (); | |||
| 3842 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3842, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3843 | // Stopping the thread should not destroy the queue. | |||
| 3844 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3844, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "false", "true") .c_str() ) = ::testing::Message(); | |||
| 3845 | ||||
| 3846 | // Reconfigure with the queue turned off. We should have neither queue nor thread. | |||
| 3847 | 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_3847; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_3847 ; } } else gtest_label_testnothrow_3847 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3847, ("Expected: " "queue_enabled = ifacemgr->configureDHCPPacketQueue(10, queue_control)" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value ) .c_str()) = ::testing::Message(); | |||
| 3848 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3848, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->getPacketQueue6()", "true", "false") .c_str() ) = ::testing::Message(); | |||
| 3849 | 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, "../../../src/lib/dhcp/tests/iface_mgr_unittest.cc" , 3849, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "ifacemgr->isDHCPReceiverRunning()", "true", "false") .c_str ()) = ::testing::Message(); | |||
| 3850 | } | |||
| 3851 | ||||
| 3852 | } |
| 1 | // Components for manipulating sequences of characters -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 1997-2025 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 | #ifdef _GLIBCXX_SYSHDR |
| 38 | #pragma GCC system_header |
| 39 | #endif |
| 40 | |
| 41 | #include <ext/alloc_traits.h> |
| 42 | #include <debug/debug.h> |
| 43 | |
| 44 | #if __cplusplus201703L >= 201103L |
| 45 | #include <initializer_list> |
| 46 | #endif |
| 47 | |
| 48 | #include <bits/version.h> |
| 49 | |
| 50 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 51 | # include <string_view> |
| 52 | #endif |
| 53 | |
| 54 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 55 | # include <bits/ranges_algobase.h> // ranges::copy |
| 56 | # include <bits/ranges_util.h> // ranges::subrange |
| 57 | #endif |
| 58 | |
| 59 | #if __cplusplus201703L > 202302L |
| 60 | # include <charconv> |
| 61 | #endif |
| 62 | |
| 63 | |
| 64 | #if ! _GLIBCXX_USE_CXX11_ABI1 |
| 65 | # include "cow_string.h" |
| 66 | #else |
| 67 | |
| 68 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
| 69 | { |
| 70 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 71 | _GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 { |
| 72 | |
| 73 | /** |
| 74 | * @class basic_string basic_string.h <string> |
| 75 | * @brief Managing sequences of characters and character-like objects. |
| 76 | * |
| 77 | * @ingroup strings |
| 78 | * @ingroup sequences |
| 79 | * @headerfile string |
| 80 | * @since C++98 |
| 81 | * |
| 82 | * @tparam _CharT Type of character |
| 83 | * @tparam _Traits Traits for character type, defaults to |
| 84 | * char_traits<_CharT>. |
| 85 | * @tparam _Alloc Allocator type, defaults to allocator<_CharT>. |
| 86 | * |
| 87 | * Meets the requirements of a <a href="tables.html#65">container</a>, a |
| 88 | * <a href="tables.html#66">reversible container</a>, and a |
| 89 | * <a href="tables.html#67">sequence</a>. Of the |
| 90 | * <a href="tables.html#68">optional sequence requirements</a>, only |
| 91 | * @c push_back, @c at, and @c %array access are supported. |
| 92 | */ |
| 93 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 94 | class basic_string |
| 95 | { |
| 96 | #if __cplusplus201703L >= 202002L |
| 97 | static_assert(is_trivially_copyable_v<_CharT> |
| 98 | && is_trivially_default_constructible_v<_CharT> |
| 99 | && is_standard_layout_v<_CharT>); |
| 100 | static_assert(is_same_v<_CharT, typename _Traits::char_type>); |
| 101 | static_assert(is_same_v<_CharT, typename _Alloc::value_type>); |
| 102 | using _Char_alloc_type = _Alloc; |
| 103 | #else |
| 104 | typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template |
| 105 | rebind<_CharT>::other _Char_alloc_type; |
| 106 | #endif |
| 107 | |
| 108 | typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; |
| 109 | |
| 110 | // Types: |
| 111 | public: |
| 112 | typedef _Traits traits_type; |
| 113 | typedef typename _Traits::char_type value_type; |
| 114 | typedef _Char_alloc_type allocator_type; |
| 115 | typedef typename _Alloc_traits::size_type size_type; |
| 116 | typedef typename _Alloc_traits::difference_type difference_type; |
| 117 | typedef typename _Alloc_traits::reference reference; |
| 118 | typedef typename _Alloc_traits::const_reference const_reference; |
| 119 | typedef typename _Alloc_traits::pointer pointer; |
| 120 | typedef typename _Alloc_traits::const_pointer const_pointer; |
| 121 | typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator; |
| 122 | typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string> |
| 123 | const_iterator; |
| 124 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 125 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 126 | |
| 127 | /// Value returned by various member functions when they fail. |
| 128 | static const size_type npos = static_cast<size_type>(-1); |
| 129 | |
| 130 | protected: |
| 131 | // type used for positions in insert, erase etc. |
| 132 | #if __cplusplus201703L < 201103L |
| 133 | typedef iterator __const_iterator; |
| 134 | #else |
| 135 | typedef const_iterator __const_iterator; |
| 136 | #endif |
| 137 | |
| 138 | private: |
| 139 | static _GLIBCXX20_CONSTEXPR pointer |
| 140 | _S_allocate(_Char_alloc_type& __a, size_type __n) |
| 141 | { |
| 142 | pointer __p = _Alloc_traits::allocate(__a, __n); |
| 143 | #if __glibcxx_constexpr_string201611L >= 201907L |
| 144 | // std::char_traits begins the lifetime of characters, |
| 145 | // but custom traits might not, so do it here. |
| 146 | if constexpr (!is_same_v<_Traits, char_traits<_CharT>>) |
| 147 | if (std::__is_constant_evaluated()) |
| 148 | // Begin the lifetime of characters in allocated storage. |
| 149 | for (size_type __i = 0; __i < __n; ++__i) |
| 150 | std::construct_at(__builtin_addressof(__p[__i])); |
| 151 | #endif |
| 152 | return __p; |
| 153 | } |
| 154 | |
| 155 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 156 | // A helper type for avoiding boiler-plate. |
| 157 | typedef basic_string_view<_CharT, _Traits> __sv_type; |
| 158 | |
| 159 | template<typename _Tp, typename _Res> |
| 160 | using _If_sv = enable_if_t< |
| 161 | __and_<is_convertible<const _Tp&, __sv_type>, |
| 162 | __not_<is_convertible<const _Tp*, const basic_string*>>, |
| 163 | __not_<is_convertible<const _Tp&, const _CharT*>>>::value, |
| 164 | _Res>; |
| 165 | |
| 166 | // Allows an implicit conversion to __sv_type. |
| 167 | _GLIBCXX20_CONSTEXPR |
| 168 | static __sv_type |
| 169 | _S_to_string_view(__sv_type __svt) noexcept |
| 170 | { return __svt; } |
| 171 | |
| 172 | // Wraps a string_view by explicit conversion and thus |
| 173 | // allows to add an internal constructor that does not |
| 174 | // participate in overload resolution when a string_view |
| 175 | // is provided. |
| 176 | struct __sv_wrapper |
| 177 | { |
| 178 | _GLIBCXX20_CONSTEXPR explicit |
| 179 | __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { } |
| 180 | |
| 181 | __sv_type _M_sv; |
| 182 | }; |
| 183 | |
| 184 | /** |
| 185 | * @brief Only internally used: Construct string from a string view |
| 186 | * wrapper. |
| 187 | * @param __svw string view wrapper. |
| 188 | * @param __a Allocator to use. |
| 189 | */ |
| 190 | _GLIBCXX20_CONSTEXPR |
| 191 | explicit |
| 192 | basic_string(__sv_wrapper __svw, const _Alloc& __a) |
| 193 | : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { } |
| 194 | #endif |
| 195 | |
| 196 | // Use empty-base optimization: http://www.cantrip.org/emptyopt.html |
| 197 | struct _Alloc_hider : allocator_type // TODO check __is_final |
| 198 | { |
| 199 | #if __cplusplus201703L < 201103L |
| 200 | _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc()) |
| 201 | : allocator_type(__a), _M_p(__dat) { } |
| 202 | #else |
| 203 | _GLIBCXX20_CONSTEXPR |
| 204 | _Alloc_hider(pointer __dat, const _Alloc& __a) |
| 205 | : allocator_type(__a), _M_p(__dat) { } |
| 206 | |
| 207 | _GLIBCXX20_CONSTEXPR |
| 208 | _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc()) |
| 209 | : allocator_type(std::move(__a)), _M_p(__dat) { } |
| 210 | #endif |
| 211 | |
| 212 | pointer _M_p; // The actual data. |
| 213 | }; |
| 214 | |
| 215 | _Alloc_hider _M_dataplus; |
| 216 | size_type _M_string_length; |
| 217 | |
| 218 | enum { _S_local_capacity = 15 / sizeof(_CharT) }; |
| 219 | |
| 220 | union |
| 221 | { |
| 222 | _CharT _M_local_buf[_S_local_capacity + 1]; |
| 223 | size_type _M_allocated_capacity; |
| 224 | }; |
| 225 | |
| 226 | _GLIBCXX20_CONSTEXPR |
| 227 | void |
| 228 | _M_data(pointer __p) |
| 229 | { _M_dataplus._M_p = __p; } |
| 230 | |
| 231 | _GLIBCXX20_CONSTEXPR |
| 232 | void |
| 233 | _M_length(size_type __length) |
| 234 | { _M_string_length = __length; } |
| 235 | |
| 236 | _GLIBCXX20_CONSTEXPR |
| 237 | pointer |
| 238 | _M_data() const |
| 239 | { return _M_dataplus._M_p; } |
| 240 | |
| 241 | _GLIBCXX20_CONSTEXPR |
| 242 | pointer |
| 243 | _M_local_data() |
| 244 | { |
| 245 | #if __cplusplus201703L >= 201103L |
| 246 | return std::pointer_traits<pointer>::pointer_to(*_M_local_buf); |
| 247 | #else |
| 248 | return pointer(_M_local_buf); |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | _GLIBCXX20_CONSTEXPR |
| 253 | const_pointer |
| 254 | _M_local_data() const |
| 255 | { |
| 256 | #if __cplusplus201703L >= 201103L |
| 257 | return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf); |
| 258 | #else |
| 259 | return const_pointer(_M_local_buf); |
| 260 | #endif |
| 261 | } |
| 262 | |
| 263 | _GLIBCXX20_CONSTEXPR |
| 264 | void |
| 265 | _M_capacity(size_type __capacity) |
| 266 | { _M_allocated_capacity = __capacity; } |
| 267 | |
| 268 | _GLIBCXX20_CONSTEXPR |
| 269 | void |
| 270 | _M_set_length(size_type __n) |
| 271 | { |
| 272 | _M_length(__n); |
| 273 | traits_type::assign(_M_data()[__n], _CharT()); |
| 274 | } |
| 275 | |
| 276 | _GLIBCXX20_CONSTEXPR |
| 277 | bool |
| 278 | _M_is_local() const |
| 279 | { |
| 280 | if (_M_data() == _M_local_data()) |
| 281 | { |
| 282 | if (_M_string_length > _S_local_capacity) |
| 283 | __builtin_unreachable(); |
| 284 | return true; |
| 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | // Create & Destroy |
| 290 | _GLIBCXX20_CONSTEXPR |
| 291 | pointer |
| 292 | _M_create(size_type&, size_type); |
| 293 | |
| 294 | _GLIBCXX20_CONSTEXPR |
| 295 | void |
| 296 | _M_dispose() |
| 297 | { |
| 298 | if (!_M_is_local()) |
| 299 | _M_destroy(_M_allocated_capacity); |
| 300 | } |
| 301 | |
| 302 | _GLIBCXX20_CONSTEXPR |
| 303 | void |
| 304 | _M_destroy(size_type __size) throw() |
| 305 | { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); } |
| 306 | |
| 307 | #if __cplusplus201703L < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
| 308 | // _M_construct_aux is used to implement the 21.3.1 para 15 which |
| 309 | // requires special behaviour if _InIterator is an integral type |
| 310 | template<typename _InIterator> |
| 311 | void |
| 312 | _M_construct_aux(_InIterator __beg, _InIterator __end, |
| 313 | std::__false_type) |
| 314 | { |
| 315 | typedef typename iterator_traits<_InIterator>::iterator_category _Tag; |
| 316 | _M_construct(__beg, __end, _Tag()); |
| 317 | } |
| 318 | |
| 319 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 320 | // 438. Ambiguity in the "do the right thing" clause |
| 321 | template<typename _Integer> |
| 322 | void |
| 323 | _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type) |
| 324 | { _M_construct_aux_2(static_cast<size_type>(__beg), __end); } |
| 325 | |
| 326 | void |
| 327 | _M_construct_aux_2(size_type __req, _CharT __c) |
| 328 | { _M_construct(__req, __c); } |
| 329 | #endif |
| 330 | |
| 331 | // For Input Iterators, used in istreambuf_iterators, etc. |
| 332 | template<typename _InIterator> |
| 333 | _GLIBCXX20_CONSTEXPR |
| 334 | void |
| 335 | _M_construct(_InIterator __beg, _InIterator __end, |
| 336 | std::input_iterator_tag); |
| 337 | |
| 338 | // For forward_iterators up to random_access_iterators, used for |
| 339 | // string::iterator, _CharT*, etc. |
| 340 | template<typename _FwdIterator> |
| 341 | _GLIBCXX20_CONSTEXPR |
| 342 | void |
| 343 | _M_construct(_FwdIterator __beg, _FwdIterator __end, |
| 344 | std::forward_iterator_tag); |
| 345 | |
| 346 | _GLIBCXX20_CONSTEXPR |
| 347 | void |
| 348 | _M_construct(size_type __req, _CharT __c); |
| 349 | |
| 350 | // Construct using block of memory of known size. |
| 351 | // If _Terminated is true assume that source is already 0 terminated. |
| 352 | template<bool _Terminated> |
| 353 | _GLIBCXX20_CONSTEXPR |
| 354 | void |
| 355 | _M_construct(const _CharT *__c, size_type __n); |
| 356 | |
| 357 | _GLIBCXX20_CONSTEXPR |
| 358 | allocator_type& |
| 359 | _M_get_allocator() |
| 360 | { return _M_dataplus; } |
| 361 | |
| 362 | _GLIBCXX20_CONSTEXPR |
| 363 | const allocator_type& |
| 364 | _M_get_allocator() const |
| 365 | { return _M_dataplus; } |
| 366 | |
| 367 | // Ensure that _M_local_buf is the active member of the union. |
| 368 | __attribute__((__always_inline__)) |
| 369 | _GLIBCXX14_CONSTEXPRconstexpr |
| 370 | void |
| 371 | _M_init_local_buf() _GLIBCXX_NOEXCEPTnoexcept |
| 372 | { |
| 373 | #if __glibcxx_is_constant_evaluated |
| 374 | if (std::is_constant_evaluated()) |
| 375 | for (size_type __i = 0; __i <= _S_local_capacity; ++__i) |
| 376 | _M_local_buf[__i] = _CharT(); |
| 377 | #endif |
| 378 | } |
| 379 | |
| 380 | __attribute__((__always_inline__)) |
| 381 | _GLIBCXX14_CONSTEXPRconstexpr |
| 382 | pointer |
| 383 | _M_use_local_data() _GLIBCXX_NOEXCEPTnoexcept |
| 384 | { |
| 385 | #if __cpp_lib_is_constant_evaluated |
| 386 | _M_init_local_buf(); |
| 387 | #endif |
| 388 | return _M_local_data(); |
| 389 | } |
| 390 | |
| 391 | private: |
| 392 | |
| 393 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST |
| 394 | // The explicit instantiations in misc-inst.cc require this due to |
| 395 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063 |
| 396 | template<typename _Tp, bool _Requires = |
| 397 | !__are_same<_Tp, _CharT*>::__value |
| 398 | && !__are_same<_Tp, const _CharT*>::__value |
| 399 | && !__are_same<_Tp, iterator>::__value |
| 400 | && !__are_same<_Tp, const_iterator>::__value> |
| 401 | struct __enable_if_not_native_iterator |
| 402 | { typedef basic_string& __type; }; |
| 403 | template<typename _Tp> |
| 404 | struct __enable_if_not_native_iterator<_Tp, false> { }; |
| 405 | #endif |
| 406 | |
| 407 | _GLIBCXX20_CONSTEXPR |
| 408 | size_type |
| 409 | _M_check(size_type __pos, const char* __s) const |
| 410 | { |
| 411 | if (__pos > this->size()) |
| 412 | __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "("%s: __pos (which is %zu) > " "this->size() (which is %zu)" ) |
| 413 | "this->size() (which is %zu)")("%s: __pos (which is %zu) > " "this->size() (which is %zu)" ), |
| 414 | __s, __pos, this->size()); |
| 415 | return __pos; |
| 416 | } |
| 417 | |
| 418 | _GLIBCXX20_CONSTEXPR |
| 419 | void |
| 420 | _M_check_length(size_type __n1, size_type __n2, const char* __s) const |
| 421 | { |
| 422 | if (this->max_size() - (this->size() - __n1) < __n2) |
| 423 | __throw_length_error(__N(__s)(__s)); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | // NB: _M_limit doesn't check for a bad __pos value. |
| 428 | _GLIBCXX20_CONSTEXPR |
| 429 | size_type |
| 430 | _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPTnoexcept |
| 431 | { |
| 432 | const bool __testoff = __off < this->size() - __pos; |
| 433 | return __testoff ? __off : this->size() - __pos; |
| 434 | } |
| 435 | |
| 436 | // True if _Rep and source do not overlap. |
| 437 | bool |
| 438 | _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept |
| 439 | { |
| 440 | return (less<const _CharT*>()(__s, _M_data()) |
| 441 | || less<const _CharT*>()(_M_data() + this->size(), __s)); |
| 442 | } |
| 443 | |
| 444 | // When __n = 1 way faster than the general multichar |
| 445 | // traits_type::copy/move/assign. |
| 446 | _GLIBCXX20_CONSTEXPR |
| 447 | static void |
| 448 | _S_copy(_CharT* __d, const _CharT* __s, size_type __n) |
| 449 | { |
| 450 | if (__n == 1) |
| 451 | traits_type::assign(*__d, *__s); |
| 452 | else |
| 453 | traits_type::copy(__d, __s, __n); |
| 454 | } |
| 455 | |
| 456 | _GLIBCXX20_CONSTEXPR |
| 457 | static void |
| 458 | _S_move(_CharT* __d, const _CharT* __s, size_type __n) |
| 459 | { |
| 460 | if (__n == 1) |
| 461 | traits_type::assign(*__d, *__s); |
| 462 | else |
| 463 | traits_type::move(__d, __s, __n); |
| 464 | } |
| 465 | |
| 466 | _GLIBCXX20_CONSTEXPR |
| 467 | static void |
| 468 | _S_assign(_CharT* __d, size_type __n, _CharT __c) |
| 469 | { |
| 470 | if (__n == 1) |
| 471 | traits_type::assign(*__d, __c); |
| 472 | else |
| 473 | traits_type::assign(__d, __n, __c); |
| 474 | } |
| 475 | |
| 476 | #pragma GCC diagnostic push |
| 477 | #pragma GCC diagnostic ignored "-Wc++17-extensions" |
| 478 | // _S_copy_chars is a separate template to permit specialization |
| 479 | // to optimize for the common case of pointers as iterators. |
| 480 | template<class _Iterator> |
| 481 | _GLIBCXX20_CONSTEXPR |
| 482 | static void |
| 483 | _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2) |
| 484 | { |
| 485 | #if __cplusplus201703L >= 201103L |
| 486 | using _IterBase = decltype(std::__niter_base(__k1)); |
| 487 | if constexpr (__or_<is_same<_IterBase, _CharT*>, |
| 488 | is_same<_IterBase, const _CharT*>>::value) |
| 489 | _S_copy(__p, std::__niter_base(__k1), __k2 - __k1); |
| 490 | #if __cpp_lib_concepts |
| 491 | else if constexpr (requires { |
| 492 | requires contiguous_iterator<_Iterator>; |
| 493 | { std::to_address(__k1) } |
| 494 | -> convertible_to<const _CharT*>; |
| 495 | }) |
| 496 | { |
| 497 | const auto __d = __k2 - __k1; |
| 498 | (void) (__k1 + __d); // See P3349R1 |
| 499 | _S_copy(__p, std::to_address(__k1), static_cast<size_type>(__d)); |
| 500 | } |
| 501 | #endif |
| 502 | else |
| 503 | #endif |
| 504 | for (; __k1 != __k2; ++__k1, (void)++__p) |
| 505 | traits_type::assign(*__p, static_cast<_CharT>(*__k1)); |
| 506 | } |
| 507 | #pragma GCC diagnostic pop |
| 508 | |
| 509 | #if __cplusplus201703L < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
| 510 | static void |
| 511 | _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) |
| 512 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
| 513 | |
| 514 | static void |
| 515 | _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2) |
| 516 | { _S_copy_chars(__p, __k1.base(), __k2.base()); } |
| 517 | |
| 518 | static void |
| 519 | _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) |
| 520 | { _S_copy(__p, __k1, __k2 - __k1); } |
| 521 | |
| 522 | static void |
| 523 | _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2) |
| 524 | { _S_copy(__p, __k1, __k2 - __k1); } |
| 525 | #endif |
| 526 | |
| 527 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 528 | // pre: __n == ranges::distance(__rg). __p+[0,__n) is a valid range. |
| 529 | template<typename _Rg> |
| 530 | static constexpr void |
| 531 | _S_copy_range(pointer __p, _Rg&& __rg, size_type __n) |
| 532 | { |
| 533 | if constexpr (requires { |
| 534 | requires ranges::contiguous_range<_Rg>; |
| 535 | { ranges::data(std::forward<_Rg>(__rg)) } |
| 536 | -> convertible_to<const _CharT*>; |
| 537 | }) |
| 538 | _S_copy(__p, ranges::data(std::forward<_Rg>(__rg)), __n); |
| 539 | else |
| 540 | { |
| 541 | auto __first = ranges::begin(__rg); |
| 542 | const auto __last = ranges::end(__rg); |
| 543 | for (; __first != __last; ++__first) |
| 544 | traits_type::assign(*__p++, static_cast<_CharT>(*__first)); |
| 545 | } |
| 546 | } |
| 547 | #endif |
| 548 | |
| 549 | _GLIBCXX20_CONSTEXPR |
| 550 | static int |
| 551 | _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPTnoexcept |
| 552 | { |
| 553 | const difference_type __d = difference_type(__n1 - __n2); |
| 554 | |
| 555 | if (__d > __gnu_cxx::__numeric_traits<int>::__max) |
| 556 | return __gnu_cxx::__numeric_traits<int>::__max; |
| 557 | else if (__d < __gnu_cxx::__numeric_traits<int>::__min) |
| 558 | return __gnu_cxx::__numeric_traits<int>::__min; |
| 559 | else |
| 560 | return int(__d); |
| 561 | } |
| 562 | |
| 563 | _GLIBCXX20_CONSTEXPR |
| 564 | void |
| 565 | _M_assign(const basic_string&); |
| 566 | |
| 567 | _GLIBCXX20_CONSTEXPR |
| 568 | void |
| 569 | _M_mutate(size_type __pos, size_type __len1, const _CharT* __s, |
| 570 | size_type __len2); |
| 571 | |
| 572 | _GLIBCXX20_CONSTEXPR |
| 573 | void |
| 574 | _M_erase(size_type __pos, size_type __n); |
| 575 | |
| 576 | public: |
| 577 | // Construct/copy/destroy: |
| 578 | // NB: We overload ctors in some cases instead of using default |
| 579 | // arguments, per 17.4.4.4 para. 2 item 2. |
| 580 | |
| 581 | /** |
| 582 | * @brief Default constructor creates an empty string. |
| 583 | */ |
| 584 | _GLIBCXX20_CONSTEXPR |
| 585 | basic_string() |
| 586 | _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)noexcept(is_nothrow_default_constructible<_Alloc>::value ) |
| 587 | #if __cpp_concepts && __glibcxx_type_trait_variable_templates201510L |
| 588 | requires is_default_constructible_v<_Alloc> |
| 589 | #endif |
| 590 | : _M_dataplus(_M_local_data()) |
| 591 | { |
| 592 | _M_init_local_buf(); |
| 593 | _M_set_length(0); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * @brief Construct an empty string using allocator @a a. |
| 598 | */ |
| 599 | _GLIBCXX20_CONSTEXPR |
| 600 | explicit |
| 601 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPTnoexcept |
| 602 | : _M_dataplus(_M_local_data(), __a) |
| 603 | { |
| 604 | _M_init_local_buf(); |
| 605 | _M_set_length(0); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * @brief Construct string with copy of value of @a __str. |
| 610 | * @param __str Source string. |
| 611 | */ |
| 612 | _GLIBCXX20_CONSTEXPR |
| 613 | basic_string(const basic_string& __str) |
| 614 | : _M_dataplus(_M_local_data(), |
| 615 | _Alloc_traits::_S_select_on_copy(__str._M_get_allocator())) |
| 616 | { |
| 617 | _M_construct<true>(__str._M_data(), __str.length()); |
| 618 | } |
| 619 | |
| 620 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 621 | // 2583. no way to supply an allocator for basic_string(str, pos) |
| 622 | /** |
| 623 | * @brief Construct string as copy of a substring. |
| 624 | * @param __str Source string. |
| 625 | * @param __pos Index of first character to copy from. |
| 626 | * @param __a Allocator to use. |
| 627 | */ |
| 628 | _GLIBCXX20_CONSTEXPR |
| 629 | basic_string(const basic_string& __str, size_type __pos, |
| 630 | const _Alloc& __a = _Alloc()) |
| 631 | : _M_dataplus(_M_local_data(), __a) |
| 632 | { |
| 633 | const _CharT* __start = __str._M_data() |
| 634 | + __str._M_check(__pos, "basic_string::basic_string"); |
| 635 | _M_construct(__start, __start + __str._M_limit(__pos, npos), |
| 636 | std::forward_iterator_tag()); |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * @brief Construct string as copy of a substring. |
| 641 | * @param __str Source string. |
| 642 | * @param __pos Index of first character to copy from. |
| 643 | * @param __n Number of characters to copy. |
| 644 | */ |
| 645 | _GLIBCXX20_CONSTEXPR |
| 646 | basic_string(const basic_string& __str, size_type __pos, |
| 647 | size_type __n) |
| 648 | : _M_dataplus(_M_local_data()) |
| 649 | { |
| 650 | const _CharT* __start = __str._M_data() |
| 651 | + __str._M_check(__pos, "basic_string::basic_string"); |
| 652 | _M_construct(__start, __start + __str._M_limit(__pos, __n), |
| 653 | std::forward_iterator_tag()); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * @brief Construct string as copy of a substring. |
| 658 | * @param __str Source string. |
| 659 | * @param __pos Index of first character to copy from. |
| 660 | * @param __n Number of characters to copy. |
| 661 | * @param __a Allocator to use. |
| 662 | */ |
| 663 | _GLIBCXX20_CONSTEXPR |
| 664 | basic_string(const basic_string& __str, size_type __pos, |
| 665 | size_type __n, const _Alloc& __a) |
| 666 | : _M_dataplus(_M_local_data(), __a) |
| 667 | { |
| 668 | const _CharT* __start |
| 669 | = __str._M_data() + __str._M_check(__pos, "string::string"); |
| 670 | _M_construct(__start, __start + __str._M_limit(__pos, __n), |
| 671 | std::forward_iterator_tag()); |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * @brief Construct string initialized by a character %array. |
| 676 | * @param __s Source character %array. |
| 677 | * @param __n Number of characters to copy. |
| 678 | * @param __a Allocator to use (default is default allocator). |
| 679 | * |
| 680 | * NB: @a __s must have at least @a __n characters, '\\0' |
| 681 | * has no special meaning. |
| 682 | */ |
| 683 | _GLIBCXX20_CONSTEXPR |
| 684 | basic_string(const _CharT* __s, size_type __n, |
| 685 | const _Alloc& __a = _Alloc()) |
| 686 | : _M_dataplus(_M_local_data(), __a) |
| 687 | { |
| 688 | // NB: Not required, but considered best practice. |
| 689 | if (__s == 0 && __n > 0) |
| 690 | std::__throw_logic_error(__N("basic_string: "("basic_string: " "construction from null is not valid") |
| 691 | "construction from null is not valid")("basic_string: " "construction from null is not valid")); |
| 692 | _M_construct(__s, __s + __n, std::forward_iterator_tag()); |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * @brief Construct string as copy of a C string. |
| 697 | * @param __s Source C string. |
| 698 | * @param __a Allocator to use (default is default allocator). |
| 699 | */ |
| 700 | #if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
| 701 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 702 | // 3076. basic_string CTAD ambiguity |
| 703 | template<typename = _RequireAllocator<_Alloc>> |
| 704 | #endif |
| 705 | _GLIBCXX20_CONSTEXPR |
| 706 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) |
| 707 | : _M_dataplus(_M_local_data(), __a) |
| 708 | { |
| 709 | // NB: Not required, but considered best practice. |
| 710 | if (__s == 0) |
| 711 | std::__throw_logic_error(__N("basic_string: "("basic_string: " "construction from null is not valid") |
| 712 | "construction from null is not valid")("basic_string: " "construction from null is not valid")); |
| 713 | const _CharT* __end = __s + traits_type::length(__s); |
| 714 | _M_construct(__s, __end, forward_iterator_tag()); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * @brief Construct string as multiple characters. |
| 719 | * @param __n Number of characters. |
| 720 | * @param __c Character to use. |
| 721 | * @param __a Allocator to use (default is default allocator). |
| 722 | */ |
| 723 | #if __cpp_deduction_guides201703L && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
| 724 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 725 | // 3076. basic_string CTAD ambiguity |
| 726 | template<typename = _RequireAllocator<_Alloc>> |
| 727 | #endif |
| 728 | _GLIBCXX20_CONSTEXPR |
| 729 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) |
| 730 | : _M_dataplus(_M_local_data(), __a) |
| 731 | { _M_construct(__n, __c); } |
| 732 | |
| 733 | #if __cplusplus201703L >= 201103L |
| 734 | /** |
| 735 | * @brief Move construct string. |
| 736 | * @param __str Source string. |
| 737 | * |
| 738 | * The newly-created string contains the exact contents of @a __str. |
| 739 | * @a __str is a valid, but unspecified string. |
| 740 | */ |
| 741 | _GLIBCXX20_CONSTEXPR |
| 742 | basic_string(basic_string&& __str) noexcept |
| 743 | : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) |
| 744 | { |
| 745 | if (__str._M_is_local()) |
| 746 | { |
| 747 | _M_init_local_buf(); |
| 748 | traits_type::copy(_M_local_buf, __str._M_local_buf, |
| 749 | __str.length() + 1); |
| 750 | } |
| 751 | else |
| 752 | { |
| 753 | _M_data(__str._M_data()); |
| 754 | _M_capacity(__str._M_allocated_capacity); |
| 755 | } |
| 756 | |
| 757 | // Must use _M_length() here not _M_set_length() because |
| 758 | // basic_stringbuf relies on writing into unallocated capacity so |
| 759 | // we mess up the contents if we put a '\0' in the string. |
| 760 | _M_length(__str.length()); |
| 761 | __str._M_data(__str._M_use_local_data()); |
| 762 | __str._M_set_length(0); |
| 763 | } |
| 764 | |
| 765 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 766 | /** |
| 767 | * @brief Construct a string from a range. |
| 768 | * @since C++23 |
| 769 | */ |
| 770 | template<__detail::__container_compatible_range<_CharT> _Rg> |
| 771 | constexpr |
| 772 | basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc()) |
| 773 | : basic_string(__a) |
| 774 | { |
| 775 | if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>) |
| 776 | { |
| 777 | const auto __n = static_cast<size_type>(ranges::distance(__rg)); |
| 778 | reserve(__n); |
| 779 | _S_copy_range(_M_data(), std::forward<_Rg>(__rg), __n); |
| 780 | _M_set_length(__n); |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | auto __first = ranges::begin(__rg); |
| 785 | const auto __last = ranges::end(__rg); |
| 786 | for (; __first != __last; ++__first) |
| 787 | push_back(*__first); |
| 788 | } |
| 789 | } |
| 790 | #endif |
| 791 | |
| 792 | /** |
| 793 | * @brief Construct string from an initializer %list. |
| 794 | * @param __l std::initializer_list of characters. |
| 795 | * @param __a Allocator to use (default is default allocator). |
| 796 | */ |
| 797 | _GLIBCXX20_CONSTEXPR |
| 798 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) |
| 799 | : _M_dataplus(_M_local_data(), __a) |
| 800 | { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); } |
| 801 | |
| 802 | _GLIBCXX20_CONSTEXPR |
| 803 | basic_string(const basic_string& __str, const _Alloc& __a) |
| 804 | : _M_dataplus(_M_local_data(), __a) |
| 805 | { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); } |
| 806 | |
| 807 | _GLIBCXX20_CONSTEXPR |
| 808 | basic_string(basic_string&& __str, const _Alloc& __a) |
| 809 | noexcept(_Alloc_traits::_S_always_equal()) |
| 810 | : _M_dataplus(_M_local_data(), __a) |
| 811 | { |
| 812 | if (__str._M_is_local()) |
| 813 | { |
| 814 | _M_init_local_buf(); |
| 815 | traits_type::copy(_M_local_buf, __str._M_local_buf, |
| 816 | __str.length() + 1); |
| 817 | _M_length(__str.length()); |
| 818 | __str._M_set_length(0); |
| 819 | } |
| 820 | else if (_Alloc_traits::_S_always_equal() |
| 821 | || __str.get_allocator() == __a) |
| 822 | { |
| 823 | _M_data(__str._M_data()); |
| 824 | _M_length(__str.length()); |
| 825 | _M_capacity(__str._M_allocated_capacity); |
| 826 | __str._M_data(__str._M_use_local_data()); |
| 827 | __str._M_set_length(0); |
| 828 | } |
| 829 | else |
| 830 | _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); |
| 831 | } |
| 832 | #endif // C++11 |
| 833 | |
| 834 | #if __cplusplus201703L >= 202100L |
| 835 | basic_string(nullptr_t) = delete; |
| 836 | basic_string& operator=(nullptr_t) = delete; |
| 837 | #endif // C++23 |
| 838 | |
| 839 | /** |
| 840 | * @brief Construct string as copy of a range. |
| 841 | * @param __beg Start of range. |
| 842 | * @param __end End of range. |
| 843 | * @param __a Allocator to use (default is default allocator). |
| 844 | */ |
| 845 | #if __cplusplus201703L >= 201103L |
| 846 | template<typename _InputIterator, |
| 847 | typename = std::_RequireInputIter<_InputIterator>> |
| 848 | #else |
| 849 | template<typename _InputIterator> |
| 850 | #endif |
| 851 | _GLIBCXX20_CONSTEXPR |
| 852 | basic_string(_InputIterator __beg, _InputIterator __end, |
| 853 | const _Alloc& __a = _Alloc()) |
| 854 | : _M_dataplus(_M_local_data(), __a), _M_string_length(0) |
| 855 | { |
| 856 | #if __cplusplus201703L >= 201103L |
| 857 | _M_construct(__beg, __end, std::__iterator_category(__beg)); |
| 858 | #else |
| 859 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
| 860 | _M_construct_aux(__beg, __end, _Integral()); |
| 861 | #endif |
| 862 | } |
| 863 | |
| 864 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 865 | /** |
| 866 | * @brief Construct string from a substring of a string_view. |
| 867 | * @param __t Source object convertible to string view. |
| 868 | * @param __pos The index of the first character to copy from __t. |
| 869 | * @param __n The number of characters to copy from __t. |
| 870 | * @param __a Allocator to use. |
| 871 | */ |
| 872 | template<typename _Tp, |
| 873 | typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> |
| 874 | _GLIBCXX20_CONSTEXPR |
| 875 | basic_string(const _Tp& __t, size_type __pos, size_type __n, |
| 876 | const _Alloc& __a = _Alloc()) |
| 877 | : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } |
| 878 | |
| 879 | /** |
| 880 | * @brief Construct string from a string_view. |
| 881 | * @param __t Source object convertible to string view. |
| 882 | * @param __a Allocator to use (default is default allocator). |
| 883 | */ |
| 884 | template<typename _Tp, typename = _If_sv<_Tp, void>> |
| 885 | _GLIBCXX20_CONSTEXPR |
| 886 | explicit |
| 887 | basic_string(const _Tp& __t, const _Alloc& __a = _Alloc()) |
| 888 | : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { } |
| 889 | #endif // C++17 |
| 890 | |
| 891 | /** |
| 892 | * @brief Destroy the string instance. |
| 893 | */ |
| 894 | _GLIBCXX20_CONSTEXPR |
| 895 | ~basic_string() |
| 896 | { _M_dispose(); } |
| 897 | |
| 898 | /** |
| 899 | * @brief Assign the value of @a str to this string. |
| 900 | * @param __str Source string. |
| 901 | */ |
| 902 | _GLIBCXX20_CONSTEXPR |
| 903 | basic_string& |
| 904 | operator=(const basic_string& __str) |
| 905 | { |
| 906 | return this->assign(__str); |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * @brief Copy contents of @a s into this string. |
| 911 | * @param __s Source null-terminated string. |
| 912 | */ |
| 913 | _GLIBCXX20_CONSTEXPR |
| 914 | basic_string& |
| 915 | operator=(const _CharT* __s) |
| 916 | { return this->assign(__s); } |
| 917 | |
| 918 | /** |
| 919 | * @brief Set value to string of length 1. |
| 920 | * @param __c Source character. |
| 921 | * |
| 922 | * Assigning to a character makes this string length 1 and |
| 923 | * (*this)[0] == @a c. |
| 924 | */ |
| 925 | _GLIBCXX20_CONSTEXPR |
| 926 | basic_string& |
| 927 | operator=(_CharT __c) |
| 928 | { |
| 929 | this->assign(1, __c); |
| 930 | return *this; |
| 931 | } |
| 932 | |
| 933 | #if __cplusplus201703L >= 201103L |
| 934 | /** |
| 935 | * @brief Move assign the value of @a str to this string. |
| 936 | * @param __str Source string. |
| 937 | * |
| 938 | * The contents of @a str are moved into this string (without copying). |
| 939 | * @a str is a valid, but unspecified string. |
| 940 | */ |
| 941 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 942 | // 2063. Contradictory requirements for string move assignment |
| 943 | _GLIBCXX20_CONSTEXPR |
| 944 | basic_string& |
| 945 | operator=(basic_string&& __str) |
| 946 | noexcept(_Alloc_traits::_S_nothrow_move()) |
| 947 | { |
| 948 | const bool __equal_allocs = _Alloc_traits::_S_always_equal() |
| 949 | || _M_get_allocator() == __str._M_get_allocator(); |
| 950 | if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign() |
| 951 | && !__equal_allocs) |
| 952 | { |
| 953 | // Destroy existing storage before replacing allocator. |
| 954 | _M_destroy(_M_allocated_capacity); |
| 955 | _M_data(_M_local_data()); |
| 956 | _M_set_length(0); |
| 957 | } |
| 958 | // Replace allocator if POCMA is true. |
| 959 | std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator()); |
| 960 | |
| 961 | if (__str._M_is_local()) |
| 962 | { |
| 963 | // We've always got room for a short string, just copy it |
| 964 | // (unless this is a self-move, because that would violate the |
| 965 | // char_traits::copy precondition that the ranges don't overlap). |
| 966 | if (__builtin_expect(std::__addressof(__str) != this, true)) |
| 967 | { |
| 968 | if (__str.size()) |
| 969 | this->_S_copy(_M_data(), __str._M_data(), __str.size()); |
| 970 | _M_set_length(__str.size()); |
| 971 | } |
| 972 | } |
| 973 | else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs) |
| 974 | { |
| 975 | // Just move the allocated pointer, our allocator can free it. |
| 976 | pointer __data = nullptr; |
| 977 | size_type __capacity; |
| 978 | if (!_M_is_local()) |
| 979 | { |
| 980 | if (__equal_allocs) |
| 981 | { |
| 982 | // __str can reuse our existing storage. |
| 983 | __data = _M_data(); |
| 984 | __capacity = _M_allocated_capacity; |
| 985 | } |
| 986 | else // __str can't use it, so free it. |
| 987 | _M_destroy(_M_allocated_capacity); |
| 988 | } |
| 989 | |
| 990 | _M_data(__str._M_data()); |
| 991 | _M_length(__str.length()); |
| 992 | _M_capacity(__str._M_allocated_capacity); |
| 993 | if (__data) |
| 994 | { |
| 995 | __str._M_data(__data); |
| 996 | __str._M_capacity(__capacity); |
| 997 | } |
| 998 | else |
| 999 | __str._M_data(__str._M_use_local_data()); |
| 1000 | } |
| 1001 | else // Need to do a deep copy |
| 1002 | _M_assign(__str); |
| 1003 | __str.clear(); |
| 1004 | return *this; |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * @brief Set value to string constructed from initializer %list. |
| 1009 | * @param __l std::initializer_list. |
| 1010 | */ |
| 1011 | _GLIBCXX20_CONSTEXPR |
| 1012 | basic_string& |
| 1013 | operator=(initializer_list<_CharT> __l) |
| 1014 | { |
| 1015 | this->assign(__l.begin(), __l.size()); |
| 1016 | return *this; |
| 1017 | } |
| 1018 | #endif // C++11 |
| 1019 | |
| 1020 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 1021 | /** |
| 1022 | * @brief Set value to string constructed from a string_view. |
| 1023 | * @param __svt An object convertible to string_view. |
| 1024 | */ |
| 1025 | template<typename _Tp> |
| 1026 | _GLIBCXX20_CONSTEXPR |
| 1027 | _If_sv<_Tp, basic_string&> |
| 1028 | operator=(const _Tp& __svt) |
| 1029 | { return this->assign(__svt); } |
| 1030 | |
| 1031 | /** |
| 1032 | * @brief Convert to a string_view. |
| 1033 | * @return A string_view. |
| 1034 | */ |
| 1035 | _GLIBCXX20_CONSTEXPR |
| 1036 | operator __sv_type() const noexcept |
| 1037 | { return __sv_type(data(), size()); } |
| 1038 | #endif // C++17 |
| 1039 | |
| 1040 | // Iterators: |
| 1041 | /** |
| 1042 | * Returns a read/write iterator that points to the first character in |
| 1043 | * the %string. |
| 1044 | */ |
| 1045 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1046 | iterator |
| 1047 | begin() _GLIBCXX_NOEXCEPTnoexcept |
| 1048 | { return iterator(_M_data()); } |
| 1049 | |
| 1050 | /** |
| 1051 | * Returns a read-only (constant) iterator that points to the first |
| 1052 | * character in the %string. |
| 1053 | */ |
| 1054 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1055 | const_iterator |
| 1056 | begin() const _GLIBCXX_NOEXCEPTnoexcept |
| 1057 | { return const_iterator(_M_data()); } |
| 1058 | |
| 1059 | /** |
| 1060 | * Returns a read/write iterator that points one past the last |
| 1061 | * character in the %string. |
| 1062 | */ |
| 1063 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1064 | iterator |
| 1065 | end() _GLIBCXX_NOEXCEPTnoexcept |
| 1066 | { return iterator(_M_data() + this->size()); } |
| 1067 | |
| 1068 | /** |
| 1069 | * Returns a read-only (constant) iterator that points one past the |
| 1070 | * last character in the %string. |
| 1071 | */ |
| 1072 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1073 | const_iterator |
| 1074 | end() const _GLIBCXX_NOEXCEPTnoexcept |
| 1075 | { return const_iterator(_M_data() + this->size()); } |
| 1076 | |
| 1077 | /** |
| 1078 | * Returns a read/write reverse iterator that points to the last |
| 1079 | * character in the %string. Iteration is done in reverse element |
| 1080 | * order. |
| 1081 | */ |
| 1082 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1083 | reverse_iterator |
| 1084 | rbegin() _GLIBCXX_NOEXCEPTnoexcept |
| 1085 | { return reverse_iterator(this->end()); } |
| 1086 | |
| 1087 | /** |
| 1088 | * Returns a read-only (constant) reverse iterator that points |
| 1089 | * to the last character in the %string. Iteration is done in |
| 1090 | * reverse element order. |
| 1091 | */ |
| 1092 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1093 | const_reverse_iterator |
| 1094 | rbegin() const _GLIBCXX_NOEXCEPTnoexcept |
| 1095 | { return const_reverse_iterator(this->end()); } |
| 1096 | |
| 1097 | /** |
| 1098 | * Returns a read/write reverse iterator that points to one before the |
| 1099 | * first character in the %string. Iteration is done in reverse |
| 1100 | * element order. |
| 1101 | */ |
| 1102 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1103 | reverse_iterator |
| 1104 | rend() _GLIBCXX_NOEXCEPTnoexcept |
| 1105 | { return reverse_iterator(this->begin()); } |
| 1106 | |
| 1107 | /** |
| 1108 | * Returns a read-only (constant) reverse iterator that points |
| 1109 | * to one before the first character in the %string. Iteration |
| 1110 | * is done in reverse element order. |
| 1111 | */ |
| 1112 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1113 | const_reverse_iterator |
| 1114 | rend() const _GLIBCXX_NOEXCEPTnoexcept |
| 1115 | { return const_reverse_iterator(this->begin()); } |
| 1116 | |
| 1117 | #if __cplusplus201703L >= 201103L |
| 1118 | /** |
| 1119 | * Returns a read-only (constant) iterator that points to the first |
| 1120 | * character in the %string. |
| 1121 | */ |
| 1122 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1123 | const_iterator |
| 1124 | cbegin() const noexcept |
| 1125 | { return const_iterator(this->_M_data()); } |
| 1126 | |
| 1127 | /** |
| 1128 | * Returns a read-only (constant) iterator that points one past the |
| 1129 | * last character in the %string. |
| 1130 | */ |
| 1131 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1132 | const_iterator |
| 1133 | cend() const noexcept |
| 1134 | { return const_iterator(this->_M_data() + this->size()); } |
| 1135 | |
| 1136 | /** |
| 1137 | * Returns a read-only (constant) reverse iterator that points |
| 1138 | * to the last character in the %string. Iteration is done in |
| 1139 | * reverse element order. |
| 1140 | */ |
| 1141 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1142 | const_reverse_iterator |
| 1143 | crbegin() const noexcept |
| 1144 | { return const_reverse_iterator(this->end()); } |
| 1145 | |
| 1146 | /** |
| 1147 | * Returns a read-only (constant) reverse iterator that points |
| 1148 | * to one before the first character in the %string. Iteration |
| 1149 | * is done in reverse element order. |
| 1150 | */ |
| 1151 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1152 | const_reverse_iterator |
| 1153 | crend() const noexcept |
| 1154 | { return const_reverse_iterator(this->begin()); } |
| 1155 | #endif |
| 1156 | |
| 1157 | public: |
| 1158 | // Capacity: |
| 1159 | /// Returns the number of characters in the string, not including any |
| 1160 | /// null-termination. |
| 1161 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1162 | size_type |
| 1163 | size() const _GLIBCXX_NOEXCEPTnoexcept |
| 1164 | { |
| 1165 | size_type __sz = _M_string_length; |
| 1166 | if (__sz > max_size ()) |
| 1167 | __builtin_unreachable (); |
| 1168 | return __sz; |
| 1169 | } |
| 1170 | |
| 1171 | /// Returns the number of characters in the string, not including any |
| 1172 | /// null-termination. |
| 1173 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1174 | size_type |
| 1175 | length() const _GLIBCXX_NOEXCEPTnoexcept |
| 1176 | { return size(); } |
| 1177 | |
| 1178 | /// Returns the size() of the largest possible %string. |
| 1179 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1180 | size_type |
| 1181 | max_size() const _GLIBCXX_NOEXCEPTnoexcept |
| 1182 | { |
| 1183 | const size_t __diffmax |
| 1184 | = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_CharT); |
| 1185 | const size_t __allocmax = _Alloc_traits::max_size(_M_get_allocator()); |
| 1186 | return (std::min)(__diffmax, __allocmax) - 1; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * @brief Resizes the %string to the specified number of characters. |
| 1191 | * @param __n Number of characters the %string should contain. |
| 1192 | * @param __c Character to fill any new elements. |
| 1193 | * |
| 1194 | * This function will %resize the %string to the specified |
| 1195 | * number of characters. If the number is smaller than the |
| 1196 | * %string's current size the %string is truncated, otherwise |
| 1197 | * the %string is extended and new elements are %set to @a __c. |
| 1198 | */ |
| 1199 | _GLIBCXX20_CONSTEXPR |
| 1200 | void |
| 1201 | resize(size_type __n, _CharT __c); |
| 1202 | |
| 1203 | /** |
| 1204 | * @brief Resizes the %string to the specified number of characters. |
| 1205 | * @param __n Number of characters the %string should contain. |
| 1206 | * |
| 1207 | * This function will resize the %string to the specified length. If |
| 1208 | * the new size is smaller than the %string's current size the %string |
| 1209 | * is truncated, otherwise the %string is extended and new characters |
| 1210 | * are default-constructed. For basic types such as char, this means |
| 1211 | * setting them to 0. |
| 1212 | */ |
| 1213 | _GLIBCXX20_CONSTEXPR |
| 1214 | void |
| 1215 | resize(size_type __n) |
| 1216 | { this->resize(__n, _CharT()); } |
| 1217 | |
| 1218 | #if __cplusplus201703L >= 201103L |
| 1219 | #pragma GCC diagnostic push |
| 1220 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 1221 | /// A non-binding request to reduce capacity() to size(). |
| 1222 | _GLIBCXX20_CONSTEXPR |
| 1223 | void |
| 1224 | shrink_to_fit() noexcept |
| 1225 | { reserve(); } |
| 1226 | #pragma GCC diagnostic pop |
| 1227 | #endif |
| 1228 | |
| 1229 | #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23 |
| 1230 | /** Resize the string and call a function to fill it. |
| 1231 | * |
| 1232 | * @param __n The maximum size requested. |
| 1233 | * @param __op A callable object that writes characters to the string. |
| 1234 | * |
| 1235 | * This is a low-level function that is easy to misuse, be careful. |
| 1236 | * |
| 1237 | * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n` |
| 1238 | * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`, |
| 1239 | * and finally set the string length to `n2` (adding a null terminator |
| 1240 | * at the end). The function object `op` is allowed to write to the |
| 1241 | * extra capacity added by the initial reserve operation, which is not |
| 1242 | * allowed if you just call `str.reserve(n)` yourself. |
| 1243 | * |
| 1244 | * This can be used to efficiently fill a `string` buffer without the |
| 1245 | * overhead of zero-initializing characters that will be overwritten |
| 1246 | * anyway. |
| 1247 | * |
| 1248 | * The callable `op` must not access the string directly (only through |
| 1249 | * the pointer passed as its first argument), must not write more than |
| 1250 | * `n` characters to the string, must return a value no greater than `n`, |
| 1251 | * and must ensure that all characters up to the returned length are |
| 1252 | * valid after it returns (i.e. there must be no uninitialized values |
| 1253 | * left in the string after the call, because accessing them would |
| 1254 | * have undefined behaviour). If `op` exits by throwing an exception |
| 1255 | * the behaviour is undefined. |
| 1256 | * |
| 1257 | * @since C++23 |
| 1258 | */ |
| 1259 | template<typename _Operation> |
| 1260 | constexpr void |
| 1261 | resize_and_overwrite(size_type __n, _Operation __op); |
| 1262 | #endif |
| 1263 | |
| 1264 | #if __cplusplus201703L >= 201103L |
| 1265 | /// Non-standard version of resize_and_overwrite for C++11 and above. |
| 1266 | template<typename _Operation> |
| 1267 | _GLIBCXX20_CONSTEXPR void |
| 1268 | __resize_and_overwrite(size_type __n, _Operation __op); |
| 1269 | #endif |
| 1270 | |
| 1271 | /** |
| 1272 | * Returns the total number of characters that the %string can hold |
| 1273 | * before needing to allocate more memory. |
| 1274 | */ |
| 1275 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1276 | size_type |
| 1277 | capacity() const _GLIBCXX_NOEXCEPTnoexcept |
| 1278 | { |
| 1279 | size_t __sz = _M_is_local() ? size_type(_S_local_capacity) |
| 1280 | : _M_allocated_capacity; |
| 1281 | if (__sz < _S_local_capacity || __sz > max_size ()) |
| 1282 | __builtin_unreachable (); |
| 1283 | return __sz; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * @brief Attempt to preallocate enough memory for specified number of |
| 1288 | * characters. |
| 1289 | * @param __res_arg Number of characters required. |
| 1290 | * @throw std::length_error If @a __res_arg exceeds @c max_size(). |
| 1291 | * |
| 1292 | * This function attempts to reserve enough memory for the |
| 1293 | * %string to hold the specified number of characters. If the |
| 1294 | * number requested is more than max_size(), length_error is |
| 1295 | * thrown. |
| 1296 | * |
| 1297 | * The advantage of this function is that if optimal code is a |
| 1298 | * necessity and the user can determine the string length that will be |
| 1299 | * required, the user can reserve the memory in %advance, and thus |
| 1300 | * prevent a possible reallocation of memory and copying of %string |
| 1301 | * data. |
| 1302 | */ |
| 1303 | _GLIBCXX20_CONSTEXPR |
| 1304 | void |
| 1305 | reserve(size_type __res_arg); |
| 1306 | |
| 1307 | /** |
| 1308 | * Equivalent to shrink_to_fit(). |
| 1309 | */ |
| 1310 | #if __cplusplus201703L > 201703L |
| 1311 | [[deprecated("use shrink_to_fit() instead")]] |
| 1312 | #endif |
| 1313 | _GLIBCXX20_CONSTEXPR |
| 1314 | void |
| 1315 | reserve(); |
| 1316 | |
| 1317 | /** |
| 1318 | * Erases the string, making it empty. |
| 1319 | */ |
| 1320 | _GLIBCXX20_CONSTEXPR |
| 1321 | void |
| 1322 | clear() _GLIBCXX_NOEXCEPTnoexcept |
| 1323 | { _M_set_length(0); } |
| 1324 | |
| 1325 | /** |
| 1326 | * Returns true if the %string is empty. Equivalent to |
| 1327 | * <code>*this == ""</code>. |
| 1328 | */ |
| 1329 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1330 | bool |
| 1331 | empty() const _GLIBCXX_NOEXCEPTnoexcept |
| 1332 | { return _M_string_length == 0; } |
| 1333 | |
| 1334 | // Element access: |
| 1335 | /** |
| 1336 | * @brief Subscript access to the data contained in the %string. |
| 1337 | * @param __pos The index of the character to access. |
| 1338 | * @return Read-only (constant) reference to the character. |
| 1339 | * |
| 1340 | * This operator allows for easy, array-style, data access. |
| 1341 | * Note that data access with this operator is unchecked and |
| 1342 | * out_of_range lookups are not defined. (For checked lookups |
| 1343 | * see at().) |
| 1344 | */ |
| 1345 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1346 | const_reference |
| 1347 | operator[] (size_type __pos) const _GLIBCXX_NOEXCEPTnoexcept |
| 1348 | { |
| 1349 | __glibcxx_assert(__pos <= size())do { if (__builtin_expect(!bool(__pos <= size()), false)) std ::__glibcxx_assert_fail("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/basic_string.h" , 1349, __PRETTY_FUNCTION__, "__pos <= size()"); } while ( false); |
| 1350 | return _M_data()[__pos]; |
| 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * @brief Subscript access to the data contained in the %string. |
| 1355 | * @param __pos The index of the character to access. |
| 1356 | * @return Read/write reference to the character. |
| 1357 | * |
| 1358 | * This operator allows for easy, array-style, data access. |
| 1359 | * Note that data access with this operator is unchecked and |
| 1360 | * out_of_range lookups are not defined. (For checked lookups |
| 1361 | * see at().) |
| 1362 | */ |
| 1363 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1364 | reference |
| 1365 | operator[](size_type __pos) |
| 1366 | { |
| 1367 | // Allow pos == size() both in C++98 mode, as v3 extension, |
| 1368 | // and in C++11 mode. |
| 1369 | __glibcxx_assert(__pos <= size())do { if (__builtin_expect(!bool(__pos <= size()), false)) std ::__glibcxx_assert_fail("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/basic_string.h" , 1369, __PRETTY_FUNCTION__, "__pos <= size()"); } while ( false); |
| 1370 | // In pedantic mode be strict in C++98 mode. |
| 1371 | _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size()); |
| 1372 | return _M_data()[__pos]; |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * @brief Provides access to the data contained in the %string. |
| 1377 | * @param __n The index of the character to access. |
| 1378 | * @return Read-only (const) reference to the character. |
| 1379 | * @throw std::out_of_range If @a n is an invalid index. |
| 1380 | * |
| 1381 | * This function provides for safer data access. The parameter is |
| 1382 | * first checked that it is in the range of the string. The function |
| 1383 | * throws out_of_range if the check fails. |
| 1384 | */ |
| 1385 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1386 | const_reference |
| 1387 | at(size_type __n) const |
| 1388 | { |
| 1389 | if (__n >= this->size()) |
| 1390 | __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
| 1391 | "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
| 1392 | "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)"), |
| 1393 | __n, this->size()); |
| 1394 | return _M_data()[__n]; |
| 1395 | } |
| 1396 | |
| 1397 | /** |
| 1398 | * @brief Provides access to the data contained in the %string. |
| 1399 | * @param __n The index of the character to access. |
| 1400 | * @return Read/write reference to the character. |
| 1401 | * @throw std::out_of_range If @a n is an invalid index. |
| 1402 | * |
| 1403 | * This function provides for safer data access. The parameter is |
| 1404 | * first checked that it is in the range of the string. The function |
| 1405 | * throws out_of_range if the check fails. |
| 1406 | */ |
| 1407 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1408 | reference |
| 1409 | at(size_type __n) |
| 1410 | { |
| 1411 | if (__n >= size()) |
| 1412 | __throw_out_of_range_fmt(__N("basic_string::at: __n "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
| 1413 | "(which is %zu) >= this->size() "("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)") |
| 1414 | "(which is %zu)")("basic_string::at: __n " "(which is %zu) >= this->size() " "(which is %zu)"), |
| 1415 | __n, this->size()); |
| 1416 | return _M_data()[__n]; |
| 1417 | } |
| 1418 | |
| 1419 | #if __cplusplus201703L >= 201103L |
| 1420 | /** |
| 1421 | * Returns a read/write reference to the data at the first |
| 1422 | * element of the %string. |
| 1423 | */ |
| 1424 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1425 | reference |
| 1426 | front() noexcept |
| 1427 | { |
| 1428 | __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail ("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/basic_string.h" , 1428, __PRETTY_FUNCTION__, "!empty()"); } while (false); |
| 1429 | return operator[](0); |
| 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * Returns a read-only (constant) reference to the data at the first |
| 1434 | * element of the %string. |
| 1435 | */ |
| 1436 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1437 | const_reference |
| 1438 | front() const noexcept |
| 1439 | { |
| 1440 | __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail ("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/basic_string.h" , 1440, __PRETTY_FUNCTION__, "!empty()"); } while (false); |
| 1441 | return operator[](0); |
| 1442 | } |
| 1443 | |
| 1444 | /** |
| 1445 | * Returns a read/write reference to the data at the last |
| 1446 | * element of the %string. |
| 1447 | */ |
| 1448 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1449 | reference |
| 1450 | back() noexcept |
| 1451 | { |
| 1452 | __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail ("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/basic_string.h" , 1452, __PRETTY_FUNCTION__, "!empty()"); } while (false); |
| 1453 | return operator[](this->size() - 1); |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * Returns a read-only (constant) reference to the data at the |
| 1458 | * last element of the %string. |
| 1459 | */ |
| 1460 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 1461 | const_reference |
| 1462 | back() const noexcept |
| 1463 | { |
| 1464 | __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail ("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/basic_string.h" , 1464, __PRETTY_FUNCTION__, "!empty()"); } while (false); |
| 1465 | return operator[](this->size() - 1); |
| 1466 | } |
| 1467 | #endif |
| 1468 | |
| 1469 | // Modifiers: |
| 1470 | /** |
| 1471 | * @brief Append a string to this string. |
| 1472 | * @param __str The string to append. |
| 1473 | * @return Reference to this string. |
| 1474 | */ |
| 1475 | _GLIBCXX20_CONSTEXPR |
| 1476 | basic_string& |
| 1477 | operator+=(const basic_string& __str) |
| 1478 | { return this->append(__str); } |
| 1479 | |
| 1480 | /** |
| 1481 | * @brief Append a C string. |
| 1482 | * @param __s The C string to append. |
| 1483 | * @return Reference to this string. |
| 1484 | */ |
| 1485 | _GLIBCXX20_CONSTEXPR |
| 1486 | basic_string& |
| 1487 | operator+=(const _CharT* __s) |
| 1488 | { return this->append(__s); } |
| 1489 | |
| 1490 | /** |
| 1491 | * @brief Append a character. |
| 1492 | * @param __c The character to append. |
| 1493 | * @return Reference to this string. |
| 1494 | */ |
| 1495 | _GLIBCXX20_CONSTEXPR |
| 1496 | basic_string& |
| 1497 | operator+=(_CharT __c) |
| 1498 | { |
| 1499 | this->push_back(__c); |
| 1500 | return *this; |
| 1501 | } |
| 1502 | |
| 1503 | #if __cplusplus201703L >= 201103L |
| 1504 | /** |
| 1505 | * @brief Append an initializer_list of characters. |
| 1506 | * @param __l The initializer_list of characters to be appended. |
| 1507 | * @return Reference to this string. |
| 1508 | */ |
| 1509 | _GLIBCXX20_CONSTEXPR |
| 1510 | basic_string& |
| 1511 | operator+=(initializer_list<_CharT> __l) |
| 1512 | { return this->append(__l.begin(), __l.size()); } |
| 1513 | #endif // C++11 |
| 1514 | |
| 1515 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 1516 | /** |
| 1517 | * @brief Append a string_view. |
| 1518 | * @param __svt An object convertible to string_view to be appended. |
| 1519 | * @return Reference to this string. |
| 1520 | */ |
| 1521 | template<typename _Tp> |
| 1522 | _GLIBCXX20_CONSTEXPR |
| 1523 | _If_sv<_Tp, basic_string&> |
| 1524 | operator+=(const _Tp& __svt) |
| 1525 | { return this->append(__svt); } |
| 1526 | #endif // C++17 |
| 1527 | |
| 1528 | /** |
| 1529 | * @brief Append a string to this string. |
| 1530 | * @param __str The string to append. |
| 1531 | * @return Reference to this string. |
| 1532 | */ |
| 1533 | _GLIBCXX20_CONSTEXPR |
| 1534 | basic_string& |
| 1535 | append(const basic_string& __str) |
| 1536 | { return this->append(__str._M_data(), __str.size()); } |
| 1537 | |
| 1538 | /** |
| 1539 | * @brief Append a substring. |
| 1540 | * @param __str The string to append. |
| 1541 | * @param __pos Index of the first character of str to append. |
| 1542 | * @param __n The number of characters to append. |
| 1543 | * @return Reference to this string. |
| 1544 | * @throw std::out_of_range if @a __pos is not a valid index. |
| 1545 | * |
| 1546 | * This function appends @a __n characters from @a __str |
| 1547 | * starting at @a __pos to this string. If @a __n is is larger |
| 1548 | * than the number of available characters in @a __str, the |
| 1549 | * remainder of @a __str is appended. |
| 1550 | */ |
| 1551 | _GLIBCXX20_CONSTEXPR |
| 1552 | basic_string& |
| 1553 | append(const basic_string& __str, size_type __pos, size_type __n = npos) |
| 1554 | { return this->append(__str._M_data() |
| 1555 | + __str._M_check(__pos, "basic_string::append"), |
| 1556 | __str._M_limit(__pos, __n)); } |
| 1557 | |
| 1558 | /** |
| 1559 | * @brief Append a C substring. |
| 1560 | * @param __s The C string to append. |
| 1561 | * @param __n The number of characters to append. |
| 1562 | * @return Reference to this string. |
| 1563 | */ |
| 1564 | _GLIBCXX20_CONSTEXPR |
| 1565 | basic_string& |
| 1566 | append(const _CharT* __s, size_type __n) |
| 1567 | { |
| 1568 | __glibcxx_requires_string_len(__s, __n); |
| 1569 | _M_check_length(size_type(0), __n, "basic_string::append"); |
| 1570 | return _M_append(__s, __n); |
| 1571 | } |
| 1572 | |
| 1573 | /** |
| 1574 | * @brief Append a C string. |
| 1575 | * @param __s The C string to append. |
| 1576 | * @return Reference to this string. |
| 1577 | */ |
| 1578 | _GLIBCXX20_CONSTEXPR |
| 1579 | basic_string& |
| 1580 | append(const _CharT* __s) |
| 1581 | { |
| 1582 | __glibcxx_requires_string(__s); |
| 1583 | const size_type __n = traits_type::length(__s); |
| 1584 | _M_check_length(size_type(0), __n, "basic_string::append"); |
| 1585 | return _M_append(__s, __n); |
| 1586 | } |
| 1587 | |
| 1588 | /** |
| 1589 | * @brief Append multiple characters. |
| 1590 | * @param __n The number of characters to append. |
| 1591 | * @param __c The character to use. |
| 1592 | * @return Reference to this string. |
| 1593 | * |
| 1594 | * Appends __n copies of __c to this string. |
| 1595 | */ |
| 1596 | _GLIBCXX20_CONSTEXPR |
| 1597 | basic_string& |
| 1598 | append(size_type __n, _CharT __c) |
| 1599 | { return _M_replace_aux(this->size(), size_type(0), __n, __c); } |
| 1600 | |
| 1601 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 1602 | /** |
| 1603 | * @brief Append a range to the string. |
| 1604 | * @param __rg A range of values that are convertible to `value_type`. |
| 1605 | * @since C++23 |
| 1606 | * |
| 1607 | * The range `__rg` is allowed to overlap with `*this`. |
| 1608 | */ |
| 1609 | template<__detail::__container_compatible_range<_CharT> _Rg> |
| 1610 | constexpr basic_string& |
| 1611 | append_range(_Rg&& __rg) |
| 1612 | { |
| 1613 | // N.B. __rg may overlap with *this, so we must copy from __rg before |
| 1614 | // existing elements or iterators referring to *this are invalidated. |
| 1615 | // e.g. in s.append_range(views::concat(s, str)), rg overlaps s. |
| 1616 | if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>) |
| 1617 | { |
| 1618 | const auto __len = size_type(ranges::distance(__rg)); |
| 1619 | |
| 1620 | // Don't care if this addition wraps around, we check it below: |
| 1621 | const size_type __newlen = size() + __len; |
| 1622 | |
| 1623 | if ((capacity() - size()) >= __len) |
| 1624 | _S_copy_range(_M_data() + size(), std::forward<_Rg>(__rg), |
| 1625 | __len); |
| 1626 | else |
| 1627 | { |
| 1628 | _M_check_length(0, __len, "basic_string::append_range"); |
| 1629 | basic_string __s(_M_get_allocator()); |
| 1630 | __s.reserve(__newlen); |
| 1631 | _S_copy_range(__s._M_data() + size(), std::forward<_Rg>(__rg), |
| 1632 | __len); |
| 1633 | _S_copy(__s._M_data(), _M_data(), size()); |
| 1634 | if (!_M_is_local()) |
| 1635 | _M_destroy(_M_allocated_capacity); |
| 1636 | _M_data(__s._M_data()); |
| 1637 | _M_capacity(__s._M_allocated_capacity); |
| 1638 | __s._M_data(__s._M_local_data()); |
| 1639 | __s._M_length(0); |
| 1640 | } |
| 1641 | _M_set_length(__newlen); // adds null-terminator |
| 1642 | } |
| 1643 | else |
| 1644 | { |
| 1645 | basic_string __s(from_range, std::forward<_Rg>(__rg), |
| 1646 | _M_get_allocator()); |
| 1647 | append(__s); |
| 1648 | } |
| 1649 | return *this; |
| 1650 | } |
| 1651 | #endif |
| 1652 | |
| 1653 | #if __cplusplus201703L >= 201103L |
| 1654 | /** |
| 1655 | * @brief Append an initializer_list of characters. |
| 1656 | * @param __l The initializer_list of characters to append. |
| 1657 | * @return Reference to this string. |
| 1658 | */ |
| 1659 | _GLIBCXX20_CONSTEXPR |
| 1660 | basic_string& |
| 1661 | append(initializer_list<_CharT> __l) |
| 1662 | { return this->append(__l.begin(), __l.size()); } |
| 1663 | #endif // C++11 |
| 1664 | |
| 1665 | /** |
| 1666 | * @brief Append a range of characters. |
| 1667 | * @param __first Iterator referencing the first character to append. |
| 1668 | * @param __last Iterator marking the end of the range. |
| 1669 | * @return Reference to this string. |
| 1670 | * |
| 1671 | * Appends characters in the range [__first,__last) to this string. |
| 1672 | */ |
| 1673 | #if __cplusplus201703L >= 201103L |
| 1674 | template<class _InputIterator, |
| 1675 | typename = std::_RequireInputIter<_InputIterator>> |
| 1676 | _GLIBCXX20_CONSTEXPR |
| 1677 | #else |
| 1678 | template<class _InputIterator> |
| 1679 | #endif |
| 1680 | basic_string& |
| 1681 | append(_InputIterator __first, _InputIterator __last) |
| 1682 | { return this->replace(end(), end(), __first, __last); } |
| 1683 | |
| 1684 | #ifdef __glibcxx_string_view201803L |
| 1685 | /** |
| 1686 | * @brief Append a string_view. |
| 1687 | * @param __svt An object convertible to string_view to be appended. |
| 1688 | * @return Reference to this string. |
| 1689 | */ |
| 1690 | template<typename _Tp> |
| 1691 | _GLIBCXX20_CONSTEXPR |
| 1692 | _If_sv<_Tp, basic_string&> |
| 1693 | append(const _Tp& __svt) |
| 1694 | { |
| 1695 | __sv_type __sv = __svt; |
| 1696 | return this->append(__sv.data(), __sv.size()); |
| 1697 | } |
| 1698 | |
| 1699 | /** |
| 1700 | * @brief Append a range of characters from a string_view. |
| 1701 | * @param __svt An object convertible to string_view to be appended from. |
| 1702 | * @param __pos The position in the string_view to append from. |
| 1703 | * @param __n The number of characters to append from the string_view. |
| 1704 | * @return Reference to this string. |
| 1705 | */ |
| 1706 | template<typename _Tp> |
| 1707 | _GLIBCXX20_CONSTEXPR |
| 1708 | _If_sv<_Tp, basic_string&> |
| 1709 | append(const _Tp& __svt, size_type __pos, size_type __n = npos) |
| 1710 | { |
| 1711 | __sv_type __sv = __svt; |
| 1712 | return _M_append(__sv.data() |
| 1713 | + std::__sv_check(__sv.size(), __pos, "basic_string::append"), |
| 1714 | std::__sv_limit(__sv.size(), __pos, __n)); |
| 1715 | } |
| 1716 | #endif // C++17 |
| 1717 | |
| 1718 | /** |
| 1719 | * @brief Append a single character. |
| 1720 | * @param __c Character to append. |
| 1721 | */ |
| 1722 | _GLIBCXX20_CONSTEXPR |
| 1723 | void |
| 1724 | push_back(_CharT __c) |
| 1725 | { |
| 1726 | const size_type __size = this->size(); |
| 1727 | if (__size + 1 > this->capacity()) |
| 1728 | this->_M_mutate(__size, size_type(0), 0, size_type(1)); |
| 1729 | traits_type::assign(this->_M_data()[__size], __c); |
| 1730 | this->_M_set_length(__size + 1); |
| 1731 | } |
| 1732 | |
| 1733 | /** |
| 1734 | * @brief Set value to contents of another string. |
| 1735 | * @param __str Source string to use. |
| 1736 | * @return Reference to this string. |
| 1737 | */ |
| 1738 | _GLIBCXX20_CONSTEXPR |
| 1739 | basic_string& |
| 1740 | assign(const basic_string& __str) |
| 1741 | { |
| 1742 | #if __cplusplus201703L >= 201103L |
| 1743 | if (_Alloc_traits::_S_propagate_on_copy_assign()) |
| 1744 | { |
| 1745 | if (!_Alloc_traits::_S_always_equal() && !_M_is_local() |
| 1746 | && _M_get_allocator() != __str._M_get_allocator()) |
| 1747 | { |
| 1748 | // Propagating allocator cannot free existing storage so must |
| 1749 | // deallocate it before replacing current allocator. |
| 1750 | if (__str.size() <= _S_local_capacity) |
| 1751 | { |
| 1752 | _M_destroy(_M_allocated_capacity); |
| 1753 | _M_data(_M_use_local_data()); |
| 1754 | _M_set_length(0); |
| 1755 | } |
| 1756 | else |
| 1757 | { |
| 1758 | const auto __len = __str.size(); |
| 1759 | auto __alloc = __str._M_get_allocator(); |
| 1760 | // If this allocation throws there are no effects: |
| 1761 | auto __ptr = _S_allocate(__alloc, __len + 1); |
| 1762 | _M_destroy(_M_allocated_capacity); |
| 1763 | _M_data(__ptr); |
| 1764 | _M_capacity(__len); |
| 1765 | _M_set_length(__len); |
| 1766 | } |
| 1767 | } |
| 1768 | std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator()); |
| 1769 | } |
| 1770 | #endif |
| 1771 | this->_M_assign(__str); |
| 1772 | return *this; |
| 1773 | } |
| 1774 | |
| 1775 | #if __cplusplus201703L >= 201103L |
| 1776 | /** |
| 1777 | * @brief Set value to contents of another string. |
| 1778 | * @param __str Source string to use. |
| 1779 | * @return Reference to this string. |
| 1780 | * |
| 1781 | * This function sets this string to the exact contents of @a __str. |
| 1782 | * @a __str is a valid, but unspecified string. |
| 1783 | */ |
| 1784 | _GLIBCXX20_CONSTEXPR |
| 1785 | basic_string& |
| 1786 | assign(basic_string&& __str) |
| 1787 | noexcept(_Alloc_traits::_S_nothrow_move()) |
| 1788 | { |
| 1789 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 1790 | // 2063. Contradictory requirements for string move assignment |
| 1791 | return *this = std::move(__str); |
| 1792 | } |
| 1793 | #endif // C++11 |
| 1794 | |
| 1795 | /** |
| 1796 | * @brief Set value to a substring of a string. |
| 1797 | * @param __str The string to use. |
| 1798 | * @param __pos Index of the first character of str. |
| 1799 | * @param __n Number of characters to use. |
| 1800 | * @return Reference to this string. |
| 1801 | * @throw std::out_of_range if @a pos is not a valid index. |
| 1802 | * |
| 1803 | * This function sets this string to the substring of @a __str |
| 1804 | * consisting of @a __n characters at @a __pos. If @a __n is |
| 1805 | * is larger than the number of available characters in @a |
| 1806 | * __str, the remainder of @a __str is used. |
| 1807 | */ |
| 1808 | _GLIBCXX20_CONSTEXPR |
| 1809 | basic_string& |
| 1810 | assign(const basic_string& __str, size_type __pos, size_type __n = npos) |
| 1811 | { return _M_replace(size_type(0), this->size(), __str._M_data() |
| 1812 | + __str._M_check(__pos, "basic_string::assign"), |
| 1813 | __str._M_limit(__pos, __n)); } |
| 1814 | |
| 1815 | /** |
| 1816 | * @brief Set value to a C substring. |
| 1817 | * @param __s The C string to use. |
| 1818 | * @param __n Number of characters to use. |
| 1819 | * @return Reference to this string. |
| 1820 | * |
| 1821 | * This function sets the value of this string to the first @a __n |
| 1822 | * characters of @a __s. If @a __n is is larger than the number of |
| 1823 | * available characters in @a __s, the remainder of @a __s is used. |
| 1824 | */ |
| 1825 | _GLIBCXX20_CONSTEXPR |
| 1826 | basic_string& |
| 1827 | assign(const _CharT* __s, size_type __n) |
| 1828 | { |
| 1829 | __glibcxx_requires_string_len(__s, __n); |
| 1830 | return _M_replace(size_type(0), this->size(), __s, __n); |
| 1831 | } |
| 1832 | |
| 1833 | /** |
| 1834 | * @brief Set value to contents of a C string. |
| 1835 | * @param __s The C string to use. |
| 1836 | * @return Reference to this string. |
| 1837 | * |
| 1838 | * This function sets the value of this string to the value of @a __s. |
| 1839 | * The data is copied, so there is no dependence on @a __s once the |
| 1840 | * function returns. |
| 1841 | */ |
| 1842 | _GLIBCXX20_CONSTEXPR |
| 1843 | basic_string& |
| 1844 | assign(const _CharT* __s) |
| 1845 | { |
| 1846 | __glibcxx_requires_string(__s); |
| 1847 | return _M_replace(size_type(0), this->size(), __s, |
| 1848 | traits_type::length(__s)); |
| 1849 | } |
| 1850 | |
| 1851 | /** |
| 1852 | * @brief Set value to multiple characters. |
| 1853 | * @param __n Length of the resulting string. |
| 1854 | * @param __c The character to use. |
| 1855 | * @return Reference to this string. |
| 1856 | * |
| 1857 | * This function sets the value of this string to @a __n copies of |
| 1858 | * character @a __c. |
| 1859 | */ |
| 1860 | _GLIBCXX20_CONSTEXPR |
| 1861 | basic_string& |
| 1862 | assign(size_type __n, _CharT __c) |
| 1863 | { return _M_replace_aux(size_type(0), this->size(), __n, __c); } |
| 1864 | |
| 1865 | /** |
| 1866 | * @brief Set value to a range of characters. |
| 1867 | * @param __first Iterator referencing the first character to append. |
| 1868 | * @param __last Iterator marking the end of the range. |
| 1869 | * @return Reference to this string. |
| 1870 | * |
| 1871 | * Sets value of string to characters in the range [__first,__last). |
| 1872 | */ |
| 1873 | #if __cplusplus201703L >= 201103L |
| 1874 | #pragma GCC diagnostic push |
| 1875 | #pragma GCC diagnostic ignored "-Wc++17-extensions" |
| 1876 | template<class _InputIterator, |
| 1877 | typename = std::_RequireInputIter<_InputIterator>> |
| 1878 | _GLIBCXX20_CONSTEXPR |
| 1879 | basic_string& |
| 1880 | assign(_InputIterator __first, _InputIterator __last) |
| 1881 | { |
| 1882 | using _IterTraits = iterator_traits<_InputIterator>; |
| 1883 | if constexpr (is_pointer<decltype(std::__niter_base(__first))>::value |
| 1884 | && is_same<typename _IterTraits::value_type, |
| 1885 | _CharT>::value) |
| 1886 | { |
| 1887 | __glibcxx_requires_valid_range(__first, __last); |
| 1888 | return _M_replace(size_type(0), size(), |
| 1889 | std::__niter_base(__first), __last - __first); |
| 1890 | } |
| 1891 | #if __cplusplus201703L >= 202002L |
| 1892 | else if constexpr (contiguous_iterator<_InputIterator> |
| 1893 | && is_same_v<iter_value_t<_InputIterator>, |
| 1894 | _CharT>) |
| 1895 | { |
| 1896 | __glibcxx_requires_valid_range(__first, __last); |
| 1897 | return _M_replace(size_type(0), size(), |
| 1898 | std::to_address(__first), __last - __first); |
| 1899 | } |
| 1900 | #endif |
| 1901 | else |
| 1902 | return *this = basic_string(__first, __last, get_allocator()); |
| 1903 | } |
| 1904 | #pragma GCC diagnostic pop |
| 1905 | #else |
| 1906 | template<class _InputIterator> |
| 1907 | basic_string& |
| 1908 | assign(_InputIterator __first, _InputIterator __last) |
| 1909 | { return this->replace(begin(), end(), __first, __last); } |
| 1910 | #endif |
| 1911 | |
| 1912 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 1913 | /** |
| 1914 | * @brief Assign a range to the string. |
| 1915 | * @param __rg A range of values that are convertible to `value_type`. |
| 1916 | * @since C++23 |
| 1917 | * |
| 1918 | * The range `__rg` is allowed to overlap with `*this`. |
| 1919 | */ |
| 1920 | template<__detail::__container_compatible_range<_CharT> _Rg> |
| 1921 | constexpr basic_string& |
| 1922 | assign_range(_Rg&& __rg) |
| 1923 | { |
| 1924 | basic_string __s(from_range, std::forward<_Rg>(__rg), |
| 1925 | _M_get_allocator()); |
| 1926 | assign(std::move(__s)); |
| 1927 | return *this; |
| 1928 | } |
| 1929 | #endif |
| 1930 | |
| 1931 | #if __cplusplus201703L >= 201103L |
| 1932 | /** |
| 1933 | * @brief Set value to an initializer_list of characters. |
| 1934 | * @param __l The initializer_list of characters to assign. |
| 1935 | * @return Reference to this string. |
| 1936 | */ |
| 1937 | _GLIBCXX20_CONSTEXPR |
| 1938 | basic_string& |
| 1939 | assign(initializer_list<_CharT> __l) |
| 1940 | { |
| 1941 | // The initializer_list array cannot alias the characters in *this |
| 1942 | // so we don't need to use replace to that case. |
| 1943 | const size_type __n = __l.size(); |
| 1944 | if (__n > capacity()) |
| 1945 | *this = basic_string(__l.begin(), __l.end(), get_allocator()); |
| 1946 | else |
| 1947 | { |
| 1948 | if (__n) |
| 1949 | _S_copy(_M_data(), __l.begin(), __n); |
| 1950 | _M_set_length(__n); |
| 1951 | } |
| 1952 | return *this; |
| 1953 | } |
| 1954 | #endif // C++11 |
| 1955 | |
| 1956 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 1957 | /** |
| 1958 | * @brief Set value from a string_view. |
| 1959 | * @param __svt The source object convertible to string_view. |
| 1960 | * @return Reference to this string. |
| 1961 | */ |
| 1962 | template<typename _Tp> |
| 1963 | _GLIBCXX20_CONSTEXPR |
| 1964 | _If_sv<_Tp, basic_string&> |
| 1965 | assign(const _Tp& __svt) |
| 1966 | { |
| 1967 | __sv_type __sv = __svt; |
| 1968 | return this->assign(__sv.data(), __sv.size()); |
| 1969 | } |
| 1970 | |
| 1971 | /** |
| 1972 | * @brief Set value from a range of characters in a string_view. |
| 1973 | * @param __svt The source object convertible to string_view. |
| 1974 | * @param __pos The position in the string_view to assign from. |
| 1975 | * @param __n The number of characters to assign. |
| 1976 | * @return Reference to this string. |
| 1977 | */ |
| 1978 | template<typename _Tp> |
| 1979 | _GLIBCXX20_CONSTEXPR |
| 1980 | _If_sv<_Tp, basic_string&> |
| 1981 | assign(const _Tp& __svt, size_type __pos, size_type __n = npos) |
| 1982 | { |
| 1983 | __sv_type __sv = __svt; |
| 1984 | return _M_replace(size_type(0), this->size(), |
| 1985 | __sv.data() |
| 1986 | + std::__sv_check(__sv.size(), __pos, "basic_string::assign"), |
| 1987 | std::__sv_limit(__sv.size(), __pos, __n)); |
| 1988 | } |
| 1989 | #endif // C++17 |
| 1990 | |
| 1991 | #if __cplusplus201703L >= 201103L |
| 1992 | /** |
| 1993 | * @brief Insert multiple characters. |
| 1994 | * @param __p Const_iterator referencing location in string to |
| 1995 | * insert at. |
| 1996 | * @param __n Number of characters to insert |
| 1997 | * @param __c The character to insert. |
| 1998 | * @return Iterator referencing the first inserted char. |
| 1999 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2000 | * |
| 2001 | * Inserts @a __n copies of character @a __c starting at the |
| 2002 | * position referenced by iterator @a __p. If adding |
| 2003 | * characters causes the length to exceed max_size(), |
| 2004 | * length_error is thrown. The value of the string doesn't |
| 2005 | * change if an error is thrown. |
| 2006 | */ |
| 2007 | _GLIBCXX20_CONSTEXPR |
| 2008 | iterator |
| 2009 | insert(const_iterator __p, size_type __n, _CharT __c) |
| 2010 | { |
| 2011 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
| 2012 | const size_type __pos = __p - begin(); |
| 2013 | this->replace(__p, __p, __n, __c); |
| 2014 | return iterator(this->_M_data() + __pos); |
| 2015 | } |
| 2016 | #else |
| 2017 | /** |
| 2018 | * @brief Insert multiple characters. |
| 2019 | * @param __p Iterator referencing location in string to insert at. |
| 2020 | * @param __n Number of characters to insert |
| 2021 | * @param __c The character to insert. |
| 2022 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2023 | * |
| 2024 | * Inserts @a __n copies of character @a __c starting at the |
| 2025 | * position referenced by iterator @a __p. If adding |
| 2026 | * characters causes the length to exceed max_size(), |
| 2027 | * length_error is thrown. The value of the string doesn't |
| 2028 | * change if an error is thrown. |
| 2029 | */ |
| 2030 | void |
| 2031 | insert(iterator __p, size_type __n, _CharT __c) |
| 2032 | { this->replace(__p, __p, __n, __c); } |
| 2033 | #endif |
| 2034 | |
| 2035 | #if __cplusplus201703L >= 201103L |
| 2036 | /** |
| 2037 | * @brief Insert a range of characters. |
| 2038 | * @param __p Const_iterator referencing location in string to |
| 2039 | * insert at. |
| 2040 | * @param __beg Start of range. |
| 2041 | * @param __end End of range. |
| 2042 | * @return Iterator referencing the first inserted char. |
| 2043 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2044 | * |
| 2045 | * Inserts characters in range [beg,end). If adding characters |
| 2046 | * causes the length to exceed max_size(), length_error is |
| 2047 | * thrown. The value of the string doesn't change if an error |
| 2048 | * is thrown. |
| 2049 | */ |
| 2050 | template<class _InputIterator, |
| 2051 | typename = std::_RequireInputIter<_InputIterator>> |
| 2052 | _GLIBCXX20_CONSTEXPR |
| 2053 | iterator |
| 2054 | insert(const_iterator __p, _InputIterator __beg, _InputIterator __end) |
| 2055 | { |
| 2056 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
| 2057 | const size_type __pos = __p - begin(); |
| 2058 | this->replace(__p, __p, __beg, __end); |
| 2059 | return iterator(this->_M_data() + __pos); |
| 2060 | } |
| 2061 | #else |
| 2062 | /** |
| 2063 | * @brief Insert a range of characters. |
| 2064 | * @param __p Iterator referencing location in string to insert at. |
| 2065 | * @param __beg Start of range. |
| 2066 | * @param __end End of range. |
| 2067 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2068 | * |
| 2069 | * Inserts characters in range [__beg,__end). If adding |
| 2070 | * characters causes the length to exceed max_size(), |
| 2071 | * length_error is thrown. The value of the string doesn't |
| 2072 | * change if an error is thrown. |
| 2073 | */ |
| 2074 | template<class _InputIterator> |
| 2075 | void |
| 2076 | insert(iterator __p, _InputIterator __beg, _InputIterator __end) |
| 2077 | { this->replace(__p, __p, __beg, __end); } |
| 2078 | #endif |
| 2079 | |
| 2080 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 2081 | /** |
| 2082 | * @brief Insert a range into the string. |
| 2083 | * @param __rg A range of values that are convertible to `value_type`. |
| 2084 | * @since C++23 |
| 2085 | * |
| 2086 | * The range `__rg` is allowed to overlap with `*this`. |
| 2087 | */ |
| 2088 | template<__detail::__container_compatible_range<_CharT> _Rg> |
| 2089 | constexpr iterator |
| 2090 | insert_range(const_iterator __p, _Rg&& __rg) |
| 2091 | { |
| 2092 | auto __pos = __p - cbegin(); |
| 2093 | |
| 2094 | if constexpr (ranges::forward_range<_Rg>) |
| 2095 | if (ranges::empty(__rg)) |
| 2096 | return begin() + __pos; |
| 2097 | |
| 2098 | |
| 2099 | if (__p == cend()) |
| 2100 | append_range(std::forward<_Rg>(__rg)); |
| 2101 | else |
| 2102 | { |
| 2103 | basic_string __s(from_range, std::forward<_Rg>(__rg), |
| 2104 | _M_get_allocator()); |
| 2105 | insert(__pos, __s); |
| 2106 | } |
| 2107 | return begin() + __pos; |
| 2108 | } |
| 2109 | #endif |
| 2110 | |
| 2111 | #if __cplusplus201703L >= 201103L |
| 2112 | /** |
| 2113 | * @brief Insert an initializer_list of characters. |
| 2114 | * @param __p Iterator referencing location in string to insert at. |
| 2115 | * @param __l The initializer_list of characters to insert. |
| 2116 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2117 | */ |
| 2118 | _GLIBCXX20_CONSTEXPR |
| 2119 | iterator |
| 2120 | insert(const_iterator __p, initializer_list<_CharT> __l) |
| 2121 | { return this->insert(__p, __l.begin(), __l.end()); } |
| 2122 | |
| 2123 | #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS |
| 2124 | // See PR libstdc++/83328 |
| 2125 | void |
| 2126 | insert(iterator __p, initializer_list<_CharT> __l) |
| 2127 | { |
| 2128 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
| 2129 | this->insert(__p - begin(), __l.begin(), __l.size()); |
| 2130 | } |
| 2131 | #endif |
| 2132 | #endif // C++11 |
| 2133 | |
| 2134 | /** |
| 2135 | * @brief Insert value of a string. |
| 2136 | * @param __pos1 Position in string to insert at. |
| 2137 | * @param __str The string to insert. |
| 2138 | * @return Reference to this string. |
| 2139 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2140 | * |
| 2141 | * Inserts value of @a __str starting at @a __pos1. If adding |
| 2142 | * characters causes the length to exceed max_size(), |
| 2143 | * length_error is thrown. The value of the string doesn't |
| 2144 | * change if an error is thrown. |
| 2145 | */ |
| 2146 | _GLIBCXX20_CONSTEXPR |
| 2147 | basic_string& |
| 2148 | insert(size_type __pos1, const basic_string& __str) |
| 2149 | { return this->replace(__pos1, size_type(0), |
| 2150 | __str._M_data(), __str.size()); } |
| 2151 | |
| 2152 | /** |
| 2153 | * @brief Insert a substring. |
| 2154 | * @param __pos1 Position in string to insert at. |
| 2155 | * @param __str The string to insert. |
| 2156 | * @param __pos2 Start of characters in str to insert. |
| 2157 | * @param __n Number of characters to insert. |
| 2158 | * @return Reference to this string. |
| 2159 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2160 | * @throw std::out_of_range If @a pos1 > size() or |
| 2161 | * @a __pos2 > @a str.size(). |
| 2162 | * |
| 2163 | * Starting at @a pos1, insert @a __n character of @a __str |
| 2164 | * beginning with @a __pos2. If adding characters causes the |
| 2165 | * length to exceed max_size(), length_error is thrown. If @a |
| 2166 | * __pos1 is beyond the end of this string or @a __pos2 is |
| 2167 | * beyond the end of @a __str, out_of_range is thrown. The |
| 2168 | * value of the string doesn't change if an error is thrown. |
| 2169 | */ |
| 2170 | _GLIBCXX20_CONSTEXPR |
| 2171 | basic_string& |
| 2172 | insert(size_type __pos1, const basic_string& __str, |
| 2173 | size_type __pos2, size_type __n = npos) |
| 2174 | { return this->replace(__pos1, size_type(0), __str._M_data() |
| 2175 | + __str._M_check(__pos2, "basic_string::insert"), |
| 2176 | __str._M_limit(__pos2, __n)); } |
| 2177 | |
| 2178 | /** |
| 2179 | * @brief Insert a C substring. |
| 2180 | * @param __pos Position in string to insert at. |
| 2181 | * @param __s The C string to insert. |
| 2182 | * @param __n The number of characters to insert. |
| 2183 | * @return Reference to this string. |
| 2184 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2185 | * @throw std::out_of_range If @a __pos is beyond the end of this |
| 2186 | * string. |
| 2187 | * |
| 2188 | * Inserts the first @a __n characters of @a __s starting at @a |
| 2189 | * __pos. If adding characters causes the length to exceed |
| 2190 | * max_size(), length_error is thrown. If @a __pos is beyond |
| 2191 | * end(), out_of_range is thrown. The value of the string |
| 2192 | * doesn't change if an error is thrown. |
| 2193 | */ |
| 2194 | _GLIBCXX20_CONSTEXPR |
| 2195 | basic_string& |
| 2196 | insert(size_type __pos, const _CharT* __s, size_type __n) |
| 2197 | { return this->replace(__pos, size_type(0), __s, __n); } |
| 2198 | |
| 2199 | /** |
| 2200 | * @brief Insert a C string. |
| 2201 | * @param __pos Position in string to insert at. |
| 2202 | * @param __s The C string to insert. |
| 2203 | * @return Reference to this string. |
| 2204 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2205 | * @throw std::out_of_range If @a pos is beyond the end of this |
| 2206 | * string. |
| 2207 | * |
| 2208 | * Inserts the first @a n characters of @a __s starting at @a __pos. If |
| 2209 | * adding characters causes the length to exceed max_size(), |
| 2210 | * length_error is thrown. If @a __pos is beyond end(), out_of_range is |
| 2211 | * thrown. The value of the string doesn't change if an error is |
| 2212 | * thrown. |
| 2213 | */ |
| 2214 | _GLIBCXX20_CONSTEXPR |
| 2215 | basic_string& |
| 2216 | insert(size_type __pos, const _CharT* __s) |
| 2217 | { |
| 2218 | __glibcxx_requires_string(__s); |
| 2219 | return this->replace(__pos, size_type(0), __s, |
| 2220 | traits_type::length(__s)); |
| 2221 | } |
| 2222 | |
| 2223 | /** |
| 2224 | * @brief Insert multiple characters. |
| 2225 | * @param __pos Index in string to insert at. |
| 2226 | * @param __n Number of characters to insert |
| 2227 | * @param __c The character to insert. |
| 2228 | * @return Reference to this string. |
| 2229 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2230 | * @throw std::out_of_range If @a __pos is beyond the end of this |
| 2231 | * string. |
| 2232 | * |
| 2233 | * Inserts @a __n copies of character @a __c starting at index |
| 2234 | * @a __pos. If adding characters causes the length to exceed |
| 2235 | * max_size(), length_error is thrown. If @a __pos > length(), |
| 2236 | * out_of_range is thrown. The value of the string doesn't |
| 2237 | * change if an error is thrown. |
| 2238 | */ |
| 2239 | _GLIBCXX20_CONSTEXPR |
| 2240 | basic_string& |
| 2241 | insert(size_type __pos, size_type __n, _CharT __c) |
| 2242 | { return _M_replace_aux(_M_check(__pos, "basic_string::insert"), |
| 2243 | size_type(0), __n, __c); } |
| 2244 | |
| 2245 | /** |
| 2246 | * @brief Insert one character. |
| 2247 | * @param __p Iterator referencing position in string to insert at. |
| 2248 | * @param __c The character to insert. |
| 2249 | * @return Iterator referencing newly inserted char. |
| 2250 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2251 | * |
| 2252 | * Inserts character @a __c at position referenced by @a __p. |
| 2253 | * If adding character causes the length to exceed max_size(), |
| 2254 | * length_error is thrown. If @a __p is beyond end of string, |
| 2255 | * out_of_range is thrown. The value of the string doesn't |
| 2256 | * change if an error is thrown. |
| 2257 | */ |
| 2258 | _GLIBCXX20_CONSTEXPR |
| 2259 | iterator |
| 2260 | insert(__const_iterator __p, _CharT __c) |
| 2261 | { |
| 2262 | _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end()); |
| 2263 | const size_type __pos = __p - begin(); |
| 2264 | _M_replace_aux(__pos, size_type(0), size_type(1), __c); |
| 2265 | return iterator(_M_data() + __pos); |
| 2266 | } |
| 2267 | |
| 2268 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 2269 | /** |
| 2270 | * @brief Insert a string_view. |
| 2271 | * @param __pos Position in string to insert at. |
| 2272 | * @param __svt The object convertible to string_view to insert. |
| 2273 | * @return Reference to this string. |
| 2274 | */ |
| 2275 | template<typename _Tp> |
| 2276 | _GLIBCXX20_CONSTEXPR |
| 2277 | _If_sv<_Tp, basic_string&> |
| 2278 | insert(size_type __pos, const _Tp& __svt) |
| 2279 | { |
| 2280 | __sv_type __sv = __svt; |
| 2281 | return this->insert(__pos, __sv.data(), __sv.size()); |
| 2282 | } |
| 2283 | |
| 2284 | /** |
| 2285 | * @brief Insert a string_view. |
| 2286 | * @param __pos1 Position in string to insert at. |
| 2287 | * @param __svt The object convertible to string_view to insert from. |
| 2288 | * @param __pos2 Start of characters in str to insert. |
| 2289 | * @param __n The number of characters to insert. |
| 2290 | * @return Reference to this string. |
| 2291 | */ |
| 2292 | template<typename _Tp> |
| 2293 | _GLIBCXX20_CONSTEXPR |
| 2294 | _If_sv<_Tp, basic_string&> |
| 2295 | insert(size_type __pos1, const _Tp& __svt, |
| 2296 | size_type __pos2, size_type __n = npos) |
| 2297 | { |
| 2298 | __sv_type __sv = __svt; |
| 2299 | return this->replace(__pos1, size_type(0), |
| 2300 | __sv.data() |
| 2301 | + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"), |
| 2302 | std::__sv_limit(__sv.size(), __pos2, __n)); |
| 2303 | } |
| 2304 | #endif // C++17 |
| 2305 | |
| 2306 | /** |
| 2307 | * @brief Remove characters. |
| 2308 | * @param __pos Index of first character to remove (default 0). |
| 2309 | * @param __n Number of characters to remove (default remainder). |
| 2310 | * @return Reference to this string. |
| 2311 | * @throw std::out_of_range If @a pos is beyond the end of this |
| 2312 | * string. |
| 2313 | * |
| 2314 | * Removes @a __n characters from this string starting at @a |
| 2315 | * __pos. The length of the string is reduced by @a __n. If |
| 2316 | * there are < @a __n characters to remove, the remainder of |
| 2317 | * the string is truncated. If @a __p is beyond end of string, |
| 2318 | * out_of_range is thrown. The value of the string doesn't |
| 2319 | * change if an error is thrown. |
| 2320 | */ |
| 2321 | _GLIBCXX20_CONSTEXPR |
| 2322 | basic_string& |
| 2323 | erase(size_type __pos = 0, size_type __n = npos) |
| 2324 | { |
| 2325 | _M_check(__pos, "basic_string::erase"); |
| 2326 | if (__n == npos) |
| 2327 | this->_M_set_length(__pos); |
| 2328 | else if (__n != 0) |
| 2329 | this->_M_erase(__pos, _M_limit(__pos, __n)); |
| 2330 | return *this; |
| 2331 | } |
| 2332 | |
| 2333 | /** |
| 2334 | * @brief Remove one character. |
| 2335 | * @param __position Iterator referencing the character to remove. |
| 2336 | * @return iterator referencing same location after removal. |
| 2337 | * |
| 2338 | * Removes the character at @a __position from this string. The value |
| 2339 | * of the string doesn't change if an error is thrown. |
| 2340 | */ |
| 2341 | _GLIBCXX20_CONSTEXPR |
| 2342 | iterator |
| 2343 | erase(__const_iterator __position) |
| 2344 | { |
| 2345 | _GLIBCXX_DEBUG_PEDASSERT(__position >= begin() |
| 2346 | && __position < end()); |
| 2347 | const size_type __pos = __position - begin(); |
| 2348 | this->_M_erase(__pos, size_type(1)); |
| 2349 | return iterator(_M_data() + __pos); |
| 2350 | } |
| 2351 | |
| 2352 | /** |
| 2353 | * @brief Remove a range of characters. |
| 2354 | * @param __first Iterator referencing the first character to remove. |
| 2355 | * @param __last Iterator referencing the end of the range. |
| 2356 | * @return Iterator referencing location of first after removal. |
| 2357 | * |
| 2358 | * Removes the characters in the range [first,last) from this string. |
| 2359 | * The value of the string doesn't change if an error is thrown. |
| 2360 | */ |
| 2361 | _GLIBCXX20_CONSTEXPR |
| 2362 | iterator |
| 2363 | erase(__const_iterator __first, __const_iterator __last) |
| 2364 | { |
| 2365 | _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last |
| 2366 | && __last <= end()); |
| 2367 | const size_type __pos = __first - begin(); |
| 2368 | if (__last == end()) |
| 2369 | this->_M_set_length(__pos); |
| 2370 | else |
| 2371 | this->_M_erase(__pos, __last - __first); |
| 2372 | return iterator(this->_M_data() + __pos); |
| 2373 | } |
| 2374 | |
| 2375 | #if __cplusplus201703L >= 201103L |
| 2376 | /** |
| 2377 | * @brief Remove the last character. |
| 2378 | * |
| 2379 | * The string must be non-empty. |
| 2380 | */ |
| 2381 | _GLIBCXX20_CONSTEXPR |
| 2382 | void |
| 2383 | pop_back() noexcept |
| 2384 | { |
| 2385 | __glibcxx_assert(!empty())do { if (__builtin_expect(!bool(!empty()), false)) std::__glibcxx_assert_fail ("/usr/bin/../lib/gcc/x86_64-redhat-linux/15/../../../../include/c++/15/bits/basic_string.h" , 2385, __PRETTY_FUNCTION__, "!empty()"); } while (false); |
| 2386 | _M_erase(size() - 1, 1); |
| 2387 | } |
| 2388 | #endif // C++11 |
| 2389 | |
| 2390 | /** |
| 2391 | * @brief Replace characters with value from another string. |
| 2392 | * @param __pos Index of first character to replace. |
| 2393 | * @param __n Number of characters to be replaced. |
| 2394 | * @param __str String to insert. |
| 2395 | * @return Reference to this string. |
| 2396 | * @throw std::out_of_range If @a pos is beyond the end of this |
| 2397 | * string. |
| 2398 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2399 | * |
| 2400 | * Removes the characters in the range [__pos,__pos+__n) from |
| 2401 | * this string. In place, the value of @a __str is inserted. |
| 2402 | * If @a __pos is beyond end of string, out_of_range is thrown. |
| 2403 | * If the length of the result exceeds max_size(), length_error |
| 2404 | * is thrown. The value of the string doesn't change if an |
| 2405 | * error is thrown. |
| 2406 | */ |
| 2407 | _GLIBCXX20_CONSTEXPR |
| 2408 | basic_string& |
| 2409 | replace(size_type __pos, size_type __n, const basic_string& __str) |
| 2410 | { return this->replace(__pos, __n, __str._M_data(), __str.size()); } |
| 2411 | |
| 2412 | /** |
| 2413 | * @brief Replace characters with value from another string. |
| 2414 | * @param __pos1 Index of first character to replace. |
| 2415 | * @param __n1 Number of characters to be replaced. |
| 2416 | * @param __str String to insert. |
| 2417 | * @param __pos2 Index of first character of str to use. |
| 2418 | * @param __n2 Number of characters from str to use. |
| 2419 | * @return Reference to this string. |
| 2420 | * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 > |
| 2421 | * __str.size(). |
| 2422 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2423 | * |
| 2424 | * Removes the characters in the range [__pos1,__pos1 + n) from this |
| 2425 | * string. In place, the value of @a __str is inserted. If @a __pos is |
| 2426 | * beyond end of string, out_of_range is thrown. If the length of the |
| 2427 | * result exceeds max_size(), length_error is thrown. The value of the |
| 2428 | * string doesn't change if an error is thrown. |
| 2429 | */ |
| 2430 | _GLIBCXX20_CONSTEXPR |
| 2431 | basic_string& |
| 2432 | replace(size_type __pos1, size_type __n1, const basic_string& __str, |
| 2433 | size_type __pos2, size_type __n2 = npos) |
| 2434 | { return this->replace(__pos1, __n1, __str._M_data() |
| 2435 | + __str._M_check(__pos2, "basic_string::replace"), |
| 2436 | __str._M_limit(__pos2, __n2)); } |
| 2437 | |
| 2438 | /** |
| 2439 | * @brief Replace characters with value of a C substring. |
| 2440 | * @param __pos Index of first character to replace. |
| 2441 | * @param __n1 Number of characters to be replaced. |
| 2442 | * @param __s C string to insert. |
| 2443 | * @param __n2 Number of characters from @a s to use. |
| 2444 | * @return Reference to this string. |
| 2445 | * @throw std::out_of_range If @a pos1 > size(). |
| 2446 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2447 | * |
| 2448 | * Removes the characters in the range [__pos,__pos + __n1) |
| 2449 | * from this string. In place, the first @a __n2 characters of |
| 2450 | * @a __s are inserted, or all of @a __s if @a __n2 is too large. If |
| 2451 | * @a __pos is beyond end of string, out_of_range is thrown. If |
| 2452 | * the length of result exceeds max_size(), length_error is |
| 2453 | * thrown. The value of the string doesn't change if an error |
| 2454 | * is thrown. |
| 2455 | */ |
| 2456 | _GLIBCXX20_CONSTEXPR |
| 2457 | basic_string& |
| 2458 | replace(size_type __pos, size_type __n1, const _CharT* __s, |
| 2459 | size_type __n2) |
| 2460 | { |
| 2461 | __glibcxx_requires_string_len(__s, __n2); |
| 2462 | return _M_replace(_M_check(__pos, "basic_string::replace"), |
| 2463 | _M_limit(__pos, __n1), __s, __n2); |
| 2464 | } |
| 2465 | |
| 2466 | /** |
| 2467 | * @brief Replace characters with value of a C string. |
| 2468 | * @param __pos Index of first character to replace. |
| 2469 | * @param __n1 Number of characters to be replaced. |
| 2470 | * @param __s C string to insert. |
| 2471 | * @return Reference to this string. |
| 2472 | * @throw std::out_of_range If @a pos > size(). |
| 2473 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2474 | * |
| 2475 | * Removes the characters in the range [__pos,__pos + __n1) |
| 2476 | * from this string. In place, the characters of @a __s are |
| 2477 | * inserted. If @a __pos is beyond end of string, out_of_range |
| 2478 | * is thrown. If the length of result exceeds max_size(), |
| 2479 | * length_error is thrown. The value of the string doesn't |
| 2480 | * change if an error is thrown. |
| 2481 | */ |
| 2482 | _GLIBCXX20_CONSTEXPR |
| 2483 | basic_string& |
| 2484 | replace(size_type __pos, size_type __n1, const _CharT* __s) |
| 2485 | { |
| 2486 | __glibcxx_requires_string(__s); |
| 2487 | return this->replace(__pos, __n1, __s, traits_type::length(__s)); |
| 2488 | } |
| 2489 | |
| 2490 | /** |
| 2491 | * @brief Replace characters with multiple characters. |
| 2492 | * @param __pos Index of first character to replace. |
| 2493 | * @param __n1 Number of characters to be replaced. |
| 2494 | * @param __n2 Number of characters to insert. |
| 2495 | * @param __c Character to insert. |
| 2496 | * @return Reference to this string. |
| 2497 | * @throw std::out_of_range If @a __pos > size(). |
| 2498 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2499 | * |
| 2500 | * Removes the characters in the range [pos,pos + n1) from this |
| 2501 | * string. In place, @a __n2 copies of @a __c are inserted. |
| 2502 | * If @a __pos is beyond end of string, out_of_range is thrown. |
| 2503 | * If the length of result exceeds max_size(), length_error is |
| 2504 | * thrown. The value of the string doesn't change if an error |
| 2505 | * is thrown. |
| 2506 | */ |
| 2507 | _GLIBCXX20_CONSTEXPR |
| 2508 | basic_string& |
| 2509 | replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c) |
| 2510 | { return _M_replace_aux(_M_check(__pos, "basic_string::replace"), |
| 2511 | _M_limit(__pos, __n1), __n2, __c); } |
| 2512 | |
| 2513 | /** |
| 2514 | * @brief Replace range of characters with string. |
| 2515 | * @param __i1 Iterator referencing start of range to replace. |
| 2516 | * @param __i2 Iterator referencing end of range to replace. |
| 2517 | * @param __str String value to insert. |
| 2518 | * @return Reference to this string. |
| 2519 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2520 | * |
| 2521 | * Removes the characters in the range [__i1,__i2). In place, |
| 2522 | * the value of @a __str is inserted. If the length of result |
| 2523 | * exceeds max_size(), length_error is thrown. The value of |
| 2524 | * the string doesn't change if an error is thrown. |
| 2525 | */ |
| 2526 | _GLIBCXX20_CONSTEXPR |
| 2527 | basic_string& |
| 2528 | replace(__const_iterator __i1, __const_iterator __i2, |
| 2529 | const basic_string& __str) |
| 2530 | { return this->replace(__i1, __i2, __str._M_data(), __str.size()); } |
| 2531 | |
| 2532 | /** |
| 2533 | * @brief Replace range of characters with C substring. |
| 2534 | * @param __i1 Iterator referencing start of range to replace. |
| 2535 | * @param __i2 Iterator referencing end of range to replace. |
| 2536 | * @param __s C string value to insert. |
| 2537 | * @param __n Number of characters from s to insert. |
| 2538 | * @return Reference to this string. |
| 2539 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2540 | * |
| 2541 | * Removes the characters in the range [__i1,__i2). In place, |
| 2542 | * the first @a __n characters of @a __s are inserted. If the |
| 2543 | * length of result exceeds max_size(), length_error is thrown. |
| 2544 | * The value of the string doesn't change if an error is |
| 2545 | * thrown. |
| 2546 | */ |
| 2547 | _GLIBCXX20_CONSTEXPR |
| 2548 | basic_string& |
| 2549 | replace(__const_iterator __i1, __const_iterator __i2, |
| 2550 | const _CharT* __s, size_type __n) |
| 2551 | { |
| 2552 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2553 | && __i2 <= end()); |
| 2554 | return this->replace(__i1 - begin(), __i2 - __i1, __s, __n); |
| 2555 | } |
| 2556 | |
| 2557 | /** |
| 2558 | * @brief Replace range of characters with C string. |
| 2559 | * @param __i1 Iterator referencing start of range to replace. |
| 2560 | * @param __i2 Iterator referencing end of range to replace. |
| 2561 | * @param __s C string value to insert. |
| 2562 | * @return Reference to this string. |
| 2563 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2564 | * |
| 2565 | * Removes the characters in the range [__i1,__i2). In place, |
| 2566 | * the characters of @a __s are inserted. If the length of |
| 2567 | * result exceeds max_size(), length_error is thrown. The |
| 2568 | * value of the string doesn't change if an error is thrown. |
| 2569 | */ |
| 2570 | _GLIBCXX20_CONSTEXPR |
| 2571 | basic_string& |
| 2572 | replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s) |
| 2573 | { |
| 2574 | __glibcxx_requires_string(__s); |
| 2575 | return this->replace(__i1, __i2, __s, traits_type::length(__s)); |
| 2576 | } |
| 2577 | |
| 2578 | /** |
| 2579 | * @brief Replace range of characters with multiple characters |
| 2580 | * @param __i1 Iterator referencing start of range to replace. |
| 2581 | * @param __i2 Iterator referencing end of range to replace. |
| 2582 | * @param __n Number of characters to insert. |
| 2583 | * @param __c Character to insert. |
| 2584 | * @return Reference to this string. |
| 2585 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2586 | * |
| 2587 | * Removes the characters in the range [__i1,__i2). In place, |
| 2588 | * @a __n copies of @a __c are inserted. If the length of |
| 2589 | * result exceeds max_size(), length_error is thrown. The |
| 2590 | * value of the string doesn't change if an error is thrown. |
| 2591 | */ |
| 2592 | _GLIBCXX20_CONSTEXPR |
| 2593 | basic_string& |
| 2594 | replace(__const_iterator __i1, __const_iterator __i2, size_type __n, |
| 2595 | _CharT __c) |
| 2596 | { |
| 2597 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2598 | && __i2 <= end()); |
| 2599 | return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c); |
| 2600 | } |
| 2601 | |
| 2602 | /** |
| 2603 | * @brief Replace range of characters with range. |
| 2604 | * @param __i1 Iterator referencing start of range to replace. |
| 2605 | * @param __i2 Iterator referencing end of range to replace. |
| 2606 | * @param __k1 Iterator referencing start of range to insert. |
| 2607 | * @param __k2 Iterator referencing end of range to insert. |
| 2608 | * @return Reference to this string. |
| 2609 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2610 | * |
| 2611 | * Removes the characters in the range [__i1,__i2). In place, |
| 2612 | * characters in the range [__k1,__k2) are inserted. If the |
| 2613 | * length of result exceeds max_size(), length_error is thrown. |
| 2614 | * The value of the string doesn't change if an error is |
| 2615 | * thrown. |
| 2616 | */ |
| 2617 | #if __cplusplus201703L >= 201103L |
| 2618 | template<class _InputIterator, |
| 2619 | typename = std::_RequireInputIter<_InputIterator>> |
| 2620 | _GLIBCXX20_CONSTEXPR |
| 2621 | basic_string& |
| 2622 | replace(const_iterator __i1, const_iterator __i2, |
| 2623 | _InputIterator __k1, _InputIterator __k2) |
| 2624 | { |
| 2625 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2626 | && __i2 <= end()); |
| 2627 | __glibcxx_requires_valid_range(__k1, __k2); |
| 2628 | return this->_M_replace_dispatch(__i1, __i2, __k1, __k2, |
| 2629 | std::__false_type()); |
| 2630 | } |
| 2631 | #else |
| 2632 | template<class _InputIterator> |
| 2633 | #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST |
| 2634 | typename __enable_if_not_native_iterator<_InputIterator>::__type |
| 2635 | #else |
| 2636 | basic_string& |
| 2637 | #endif |
| 2638 | replace(iterator __i1, iterator __i2, |
| 2639 | _InputIterator __k1, _InputIterator __k2) |
| 2640 | { |
| 2641 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2642 | && __i2 <= end()); |
| 2643 | __glibcxx_requires_valid_range(__k1, __k2); |
| 2644 | typedef typename std::__is_integer<_InputIterator>::__type _Integral; |
| 2645 | return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); |
| 2646 | } |
| 2647 | #endif |
| 2648 | |
| 2649 | // Specializations for the common case of pointer and iterator: |
| 2650 | // useful to avoid the overhead of temporary buffering in _M_replace. |
| 2651 | _GLIBCXX20_CONSTEXPR |
| 2652 | basic_string& |
| 2653 | replace(__const_iterator __i1, __const_iterator __i2, |
| 2654 | _CharT* __k1, _CharT* __k2) |
| 2655 | { |
| 2656 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2657 | && __i2 <= end()); |
| 2658 | __glibcxx_requires_valid_range(__k1, __k2); |
| 2659 | return this->replace(__i1 - begin(), __i2 - __i1, |
| 2660 | __k1, __k2 - __k1); |
| 2661 | } |
| 2662 | |
| 2663 | _GLIBCXX20_CONSTEXPR |
| 2664 | basic_string& |
| 2665 | replace(__const_iterator __i1, __const_iterator __i2, |
| 2666 | const _CharT* __k1, const _CharT* __k2) |
| 2667 | { |
| 2668 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2669 | && __i2 <= end()); |
| 2670 | __glibcxx_requires_valid_range(__k1, __k2); |
| 2671 | return this->replace(__i1 - begin(), __i2 - __i1, |
| 2672 | __k1, __k2 - __k1); |
| 2673 | } |
| 2674 | |
| 2675 | _GLIBCXX20_CONSTEXPR |
| 2676 | basic_string& |
| 2677 | replace(__const_iterator __i1, __const_iterator __i2, |
| 2678 | iterator __k1, iterator __k2) |
| 2679 | { |
| 2680 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2681 | && __i2 <= end()); |
| 2682 | __glibcxx_requires_valid_range(__k1, __k2); |
| 2683 | return this->replace(__i1 - begin(), __i2 - __i1, |
| 2684 | __k1.base(), __k2 - __k1); |
| 2685 | } |
| 2686 | |
| 2687 | _GLIBCXX20_CONSTEXPR |
| 2688 | basic_string& |
| 2689 | replace(__const_iterator __i1, __const_iterator __i2, |
| 2690 | const_iterator __k1, const_iterator __k2) |
| 2691 | { |
| 2692 | _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2 |
| 2693 | && __i2 <= end()); |
| 2694 | __glibcxx_requires_valid_range(__k1, __k2); |
| 2695 | return this->replace(__i1 - begin(), __i2 - __i1, |
| 2696 | __k1.base(), __k2 - __k1); |
| 2697 | } |
| 2698 | |
| 2699 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 2700 | /** |
| 2701 | * @brief Replace part of the string with a range. |
| 2702 | * @param __rg A range of values that are convertible to `value_type`. |
| 2703 | * @since C++23 |
| 2704 | * |
| 2705 | * The range `__rg` is allowed to overlap with `*this`. |
| 2706 | */ |
| 2707 | template<__detail::__container_compatible_range<_CharT> _Rg> |
| 2708 | constexpr basic_string& |
| 2709 | replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg) |
| 2710 | { |
| 2711 | if (__i1 == cend()) |
| 2712 | append_range(std::forward<_Rg>(__rg)); |
| 2713 | else |
| 2714 | { |
| 2715 | basic_string __s(from_range, std::forward<_Rg>(__rg), |
| 2716 | _M_get_allocator()); |
| 2717 | replace(__i1, __i2, __s); |
| 2718 | } |
| 2719 | return *this; |
| 2720 | } |
| 2721 | #endif |
| 2722 | |
| 2723 | #if __cplusplus201703L >= 201103L |
| 2724 | /** |
| 2725 | * @brief Replace range of characters with initializer_list. |
| 2726 | * @param __i1 Iterator referencing start of range to replace. |
| 2727 | * @param __i2 Iterator referencing end of range to replace. |
| 2728 | * @param __l The initializer_list of characters to insert. |
| 2729 | * @return Reference to this string. |
| 2730 | * @throw std::length_error If new length exceeds @c max_size(). |
| 2731 | * |
| 2732 | * Removes the characters in the range [__i1,__i2). In place, |
| 2733 | * characters in the range [__k1,__k2) are inserted. If the |
| 2734 | * length of result exceeds max_size(), length_error is thrown. |
| 2735 | * The value of the string doesn't change if an error is |
| 2736 | * thrown. |
| 2737 | */ |
| 2738 | _GLIBCXX20_CONSTEXPR |
| 2739 | basic_string& replace(const_iterator __i1, const_iterator __i2, |
| 2740 | initializer_list<_CharT> __l) |
| 2741 | { return this->replace(__i1, __i2, __l.begin(), __l.size()); } |
| 2742 | #endif // C++11 |
| 2743 | |
| 2744 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 2745 | /** |
| 2746 | * @brief Replace range of characters with string_view. |
| 2747 | * @param __pos The position to replace at. |
| 2748 | * @param __n The number of characters to replace. |
| 2749 | * @param __svt The object convertible to string_view to insert. |
| 2750 | * @return Reference to this string. |
| 2751 | */ |
| 2752 | template<typename _Tp> |
| 2753 | _GLIBCXX20_CONSTEXPR |
| 2754 | _If_sv<_Tp, basic_string&> |
| 2755 | replace(size_type __pos, size_type __n, const _Tp& __svt) |
| 2756 | { |
| 2757 | __sv_type __sv = __svt; |
| 2758 | return this->replace(__pos, __n, __sv.data(), __sv.size()); |
| 2759 | } |
| 2760 | |
| 2761 | /** |
| 2762 | * @brief Replace range of characters with string_view. |
| 2763 | * @param __pos1 The position to replace at. |
| 2764 | * @param __n1 The number of characters to replace. |
| 2765 | * @param __svt The object convertible to string_view to insert from. |
| 2766 | * @param __pos2 The position in the string_view to insert from. |
| 2767 | * @param __n2 The number of characters to insert. |
| 2768 | * @return Reference to this string. |
| 2769 | */ |
| 2770 | template<typename _Tp> |
| 2771 | _GLIBCXX20_CONSTEXPR |
| 2772 | _If_sv<_Tp, basic_string&> |
| 2773 | replace(size_type __pos1, size_type __n1, const _Tp& __svt, |
| 2774 | size_type __pos2, size_type __n2 = npos) |
| 2775 | { |
| 2776 | __sv_type __sv = __svt; |
| 2777 | return this->replace(__pos1, __n1, |
| 2778 | __sv.data() |
| 2779 | + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"), |
| 2780 | std::__sv_limit(__sv.size(), __pos2, __n2)); |
| 2781 | } |
| 2782 | |
| 2783 | /** |
| 2784 | * @brief Replace range of characters with string_view. |
| 2785 | * @param __i1 An iterator referencing the start position |
| 2786 | to replace at. |
| 2787 | * @param __i2 An iterator referencing the end position |
| 2788 | for the replace. |
| 2789 | * @param __svt The object convertible to string_view to insert from. |
| 2790 | * @return Reference to this string. |
| 2791 | */ |
| 2792 | template<typename _Tp> |
| 2793 | _GLIBCXX20_CONSTEXPR |
| 2794 | _If_sv<_Tp, basic_string&> |
| 2795 | replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt) |
| 2796 | { |
| 2797 | __sv_type __sv = __svt; |
| 2798 | return this->replace(__i1 - begin(), __i2 - __i1, __sv); |
| 2799 | } |
| 2800 | #endif // C++17 |
| 2801 | |
| 2802 | private: |
| 2803 | template<class _Integer> |
| 2804 | _GLIBCXX20_CONSTEXPR |
| 2805 | basic_string& |
| 2806 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, |
| 2807 | _Integer __n, _Integer __val, __true_type) |
| 2808 | { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); } |
| 2809 | |
| 2810 | template<class _InputIterator> |
| 2811 | _GLIBCXX20_CONSTEXPR |
| 2812 | basic_string& |
| 2813 | _M_replace_dispatch(const_iterator __i1, const_iterator __i2, |
| 2814 | _InputIterator __k1, _InputIterator __k2, |
| 2815 | __false_type); |
| 2816 | |
| 2817 | _GLIBCXX20_CONSTEXPR |
| 2818 | basic_string& |
| 2819 | _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2, |
| 2820 | _CharT __c); |
| 2821 | |
| 2822 | __attribute__((__noinline__, __noclone__, __cold__)) void |
| 2823 | _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s, |
| 2824 | const size_type __len2, const size_type __how_much); |
| 2825 | |
| 2826 | _GLIBCXX20_CONSTEXPR |
| 2827 | basic_string& |
| 2828 | _M_replace(size_type __pos, size_type __len1, const _CharT* __s, |
| 2829 | const size_type __len2); |
| 2830 | |
| 2831 | _GLIBCXX20_CONSTEXPR |
| 2832 | basic_string& |
| 2833 | _M_append(const _CharT* __s, size_type __n); |
| 2834 | |
| 2835 | public: |
| 2836 | |
| 2837 | /** |
| 2838 | * @brief Copy substring into C string. |
| 2839 | * @param __s C string to copy value into. |
| 2840 | * @param __n Number of characters to copy. |
| 2841 | * @param __pos Index of first character to copy. |
| 2842 | * @return Number of characters actually copied |
| 2843 | * @throw std::out_of_range If __pos > size(). |
| 2844 | * |
| 2845 | * Copies up to @a __n characters starting at @a __pos into the |
| 2846 | * C string @a __s. If @a __pos is %greater than size(), |
| 2847 | * out_of_range is thrown. |
| 2848 | */ |
| 2849 | _GLIBCXX20_CONSTEXPR |
| 2850 | size_type |
| 2851 | copy(_CharT* __s, size_type __n, size_type __pos = 0) const; |
| 2852 | |
| 2853 | /** |
| 2854 | * @brief Swap contents with another string. |
| 2855 | * @param __s String to swap with. |
| 2856 | * |
| 2857 | * Exchanges the contents of this string with that of @a __s in constant |
| 2858 | * time. |
| 2859 | */ |
| 2860 | _GLIBCXX20_CONSTEXPR |
| 2861 | void |
| 2862 | swap(basic_string& __s) _GLIBCXX_NOEXCEPTnoexcept; |
| 2863 | |
| 2864 | // String operations: |
| 2865 | /** |
| 2866 | * @brief Return const pointer to null-terminated contents. |
| 2867 | * |
| 2868 | * This is a handle to internal data. Do not modify or dire things may |
| 2869 | * happen. |
| 2870 | */ |
| 2871 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2872 | const _CharT* |
| 2873 | c_str() const _GLIBCXX_NOEXCEPTnoexcept |
| 2874 | { return _M_data(); } |
| 2875 | |
| 2876 | /** |
| 2877 | * @brief Return const pointer to contents. |
| 2878 | * |
| 2879 | * This is a pointer to internal data. It is undefined to modify |
| 2880 | * the contents through the returned pointer. To get a pointer that |
| 2881 | * allows modifying the contents use @c &str[0] instead, |
| 2882 | * (or in C++17 the non-const @c str.data() overload). |
| 2883 | */ |
| 2884 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2885 | const _CharT* |
| 2886 | data() const _GLIBCXX_NOEXCEPTnoexcept |
| 2887 | { return _M_data(); } |
| 2888 | |
| 2889 | #if __cplusplus201703L >= 201703L |
| 2890 | /** |
| 2891 | * @brief Return non-const pointer to contents. |
| 2892 | * |
| 2893 | * This is a pointer to the character sequence held by the string. |
| 2894 | * Modifying the characters in the sequence is allowed. |
| 2895 | */ |
| 2896 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2897 | _CharT* |
| 2898 | data() noexcept |
| 2899 | { return _M_data(); } |
| 2900 | #endif |
| 2901 | |
| 2902 | /** |
| 2903 | * @brief Return copy of allocator used to construct this string. |
| 2904 | */ |
| 2905 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2906 | allocator_type |
| 2907 | get_allocator() const _GLIBCXX_NOEXCEPTnoexcept |
| 2908 | { return _M_get_allocator(); } |
| 2909 | |
| 2910 | /** |
| 2911 | * @brief Find position of a C substring. |
| 2912 | * @param __s C string to locate. |
| 2913 | * @param __pos Index of character to search from. |
| 2914 | * @param __n Number of characters from @a s to search for. |
| 2915 | * @return Index of start of first occurrence. |
| 2916 | * |
| 2917 | * Starting from @a __pos, searches forward for the first @a |
| 2918 | * __n characters in @a __s within this string. If found, |
| 2919 | * returns the index where it begins. If not found, returns |
| 2920 | * npos. |
| 2921 | */ |
| 2922 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2923 | size_type |
| 2924 | find(const _CharT* __s, size_type __pos, size_type __n) const |
| 2925 | _GLIBCXX_NOEXCEPTnoexcept; |
| 2926 | |
| 2927 | /** |
| 2928 | * @brief Find position of a string. |
| 2929 | * @param __str String to locate. |
| 2930 | * @param __pos Index of character to search from (default 0). |
| 2931 | * @return Index of start of first occurrence. |
| 2932 | * |
| 2933 | * Starting from @a __pos, searches forward for value of @a __str within |
| 2934 | * this string. If found, returns the index where it begins. If not |
| 2935 | * found, returns npos. |
| 2936 | */ |
| 2937 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2938 | size_type |
| 2939 | find(const basic_string& __str, size_type __pos = 0) const |
| 2940 | _GLIBCXX_NOEXCEPTnoexcept |
| 2941 | { return this->find(__str.data(), __pos, __str.size()); } |
| 2942 | |
| 2943 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 2944 | /** |
| 2945 | * @brief Find position of a string_view. |
| 2946 | * @param __svt The object convertible to string_view to locate. |
| 2947 | * @param __pos Index of character to search from (default 0). |
| 2948 | * @return Index of start of first occurrence. |
| 2949 | */ |
| 2950 | template<typename _Tp> |
| 2951 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2952 | _If_sv<_Tp, size_type> |
| 2953 | find(const _Tp& __svt, size_type __pos = 0) const |
| 2954 | noexcept(is_same<_Tp, __sv_type>::value) |
| 2955 | { |
| 2956 | __sv_type __sv = __svt; |
| 2957 | return this->find(__sv.data(), __pos, __sv.size()); |
| 2958 | } |
| 2959 | #endif // C++17 |
| 2960 | |
| 2961 | /** |
| 2962 | * @brief Find position of a C string. |
| 2963 | * @param __s C string to locate. |
| 2964 | * @param __pos Index of character to search from (default 0). |
| 2965 | * @return Index of start of first occurrence. |
| 2966 | * |
| 2967 | * Starting from @a __pos, searches forward for the value of @a |
| 2968 | * __s within this string. If found, returns the index where |
| 2969 | * it begins. If not found, returns npos. |
| 2970 | */ |
| 2971 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2972 | size_type |
| 2973 | find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept |
| 2974 | { |
| 2975 | __glibcxx_requires_string(__s); |
| 2976 | return this->find(__s, __pos, traits_type::length(__s)); |
| 2977 | } |
| 2978 | |
| 2979 | /** |
| 2980 | * @brief Find position of a character. |
| 2981 | * @param __c Character to locate. |
| 2982 | * @param __pos Index of character to search from (default 0). |
| 2983 | * @return Index of first occurrence. |
| 2984 | * |
| 2985 | * Starting from @a __pos, searches forward for @a __c within |
| 2986 | * this string. If found, returns the index where it was |
| 2987 | * found. If not found, returns npos. |
| 2988 | */ |
| 2989 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 2990 | size_type |
| 2991 | find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept; |
| 2992 | |
| 2993 | /** |
| 2994 | * @brief Find last position of a string. |
| 2995 | * @param __str String to locate. |
| 2996 | * @param __pos Index of character to search back from (default end). |
| 2997 | * @return Index of start of last occurrence. |
| 2998 | * |
| 2999 | * Starting from @a __pos, searches backward for value of @a |
| 3000 | * __str within this string. If found, returns the index where |
| 3001 | * it begins. If not found, returns npos. |
| 3002 | */ |
| 3003 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3004 | size_type |
| 3005 | rfind(const basic_string& __str, size_type __pos = npos) const |
| 3006 | _GLIBCXX_NOEXCEPTnoexcept |
| 3007 | { return this->rfind(__str.data(), __pos, __str.size()); } |
| 3008 | |
| 3009 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 3010 | /** |
| 3011 | * @brief Find last position of a string_view. |
| 3012 | * @param __svt The object convertible to string_view to locate. |
| 3013 | * @param __pos Index of character to search back from (default end). |
| 3014 | * @return Index of start of last occurrence. |
| 3015 | */ |
| 3016 | template<typename _Tp> |
| 3017 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3018 | _If_sv<_Tp, size_type> |
| 3019 | rfind(const _Tp& __svt, size_type __pos = npos) const |
| 3020 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3021 | { |
| 3022 | __sv_type __sv = __svt; |
| 3023 | return this->rfind(__sv.data(), __pos, __sv.size()); |
| 3024 | } |
| 3025 | #endif // C++17 |
| 3026 | |
| 3027 | /** |
| 3028 | * @brief Find last position of a C substring. |
| 3029 | * @param __s C string to locate. |
| 3030 | * @param __pos Index of character to search back from. |
| 3031 | * @param __n Number of characters from s to search for. |
| 3032 | * @return Index of start of last occurrence. |
| 3033 | * |
| 3034 | * Starting from @a __pos, searches backward for the first @a |
| 3035 | * __n characters in @a __s within this string. If found, |
| 3036 | * returns the index where it begins. If not found, returns |
| 3037 | * npos. |
| 3038 | */ |
| 3039 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3040 | size_type |
| 3041 | rfind(const _CharT* __s, size_type __pos, size_type __n) const |
| 3042 | _GLIBCXX_NOEXCEPTnoexcept; |
| 3043 | |
| 3044 | /** |
| 3045 | * @brief Find last position of a C string. |
| 3046 | * @param __s C string to locate. |
| 3047 | * @param __pos Index of character to start search at (default end). |
| 3048 | * @return Index of start of last occurrence. |
| 3049 | * |
| 3050 | * Starting from @a __pos, searches backward for the value of |
| 3051 | * @a __s within this string. If found, returns the index |
| 3052 | * where it begins. If not found, returns npos. |
| 3053 | */ |
| 3054 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3055 | size_type |
| 3056 | rfind(const _CharT* __s, size_type __pos = npos) const |
| 3057 | { |
| 3058 | __glibcxx_requires_string(__s); |
| 3059 | return this->rfind(__s, __pos, traits_type::length(__s)); |
| 3060 | } |
| 3061 | |
| 3062 | /** |
| 3063 | * @brief Find last position of a character. |
| 3064 | * @param __c Character to locate. |
| 3065 | * @param __pos Index of character to search back from (default end). |
| 3066 | * @return Index of last occurrence. |
| 3067 | * |
| 3068 | * Starting from @a __pos, searches backward for @a __c within |
| 3069 | * this string. If found, returns the index where it was |
| 3070 | * found. If not found, returns npos. |
| 3071 | */ |
| 3072 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3073 | size_type |
| 3074 | rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept; |
| 3075 | |
| 3076 | /** |
| 3077 | * @brief Find position of a character of string. |
| 3078 | * @param __str String containing characters to locate. |
| 3079 | * @param __pos Index of character to search from (default 0). |
| 3080 | * @return Index of first occurrence. |
| 3081 | * |
| 3082 | * Starting from @a __pos, searches forward for one of the |
| 3083 | * characters of @a __str within this string. If found, |
| 3084 | * returns the index where it was found. If not found, returns |
| 3085 | * npos. |
| 3086 | */ |
| 3087 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3088 | size_type |
| 3089 | find_first_of(const basic_string& __str, size_type __pos = 0) const |
| 3090 | _GLIBCXX_NOEXCEPTnoexcept |
| 3091 | { return this->find_first_of(__str.data(), __pos, __str.size()); } |
| 3092 | |
| 3093 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 3094 | /** |
| 3095 | * @brief Find position of a character of a string_view. |
| 3096 | * @param __svt An object convertible to string_view containing |
| 3097 | * characters to locate. |
| 3098 | * @param __pos Index of character to search from (default 0). |
| 3099 | * @return Index of first occurrence. |
| 3100 | */ |
| 3101 | template<typename _Tp> |
| 3102 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3103 | _If_sv<_Tp, size_type> |
| 3104 | find_first_of(const _Tp& __svt, size_type __pos = 0) const |
| 3105 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3106 | { |
| 3107 | __sv_type __sv = __svt; |
| 3108 | return this->find_first_of(__sv.data(), __pos, __sv.size()); |
| 3109 | } |
| 3110 | #endif // C++17 |
| 3111 | |
| 3112 | /** |
| 3113 | * @brief Find position of a character of C substring. |
| 3114 | * @param __s String containing characters to locate. |
| 3115 | * @param __pos Index of character to search from. |
| 3116 | * @param __n Number of characters from s to search for. |
| 3117 | * @return Index of first occurrence. |
| 3118 | * |
| 3119 | * Starting from @a __pos, searches forward for one of the |
| 3120 | * first @a __n characters of @a __s within this string. If |
| 3121 | * found, returns the index where it was found. If not found, |
| 3122 | * returns npos. |
| 3123 | */ |
| 3124 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3125 | size_type |
| 3126 | find_first_of(const _CharT* __s, size_type __pos, size_type __n) const |
| 3127 | _GLIBCXX_NOEXCEPTnoexcept; |
| 3128 | |
| 3129 | /** |
| 3130 | * @brief Find position of a character of C string. |
| 3131 | * @param __s String containing characters to locate. |
| 3132 | * @param __pos Index of character to search from (default 0). |
| 3133 | * @return Index of first occurrence. |
| 3134 | * |
| 3135 | * Starting from @a __pos, searches forward for one of the |
| 3136 | * characters of @a __s within this string. If found, returns |
| 3137 | * the index where it was found. If not found, returns npos. |
| 3138 | */ |
| 3139 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3140 | size_type |
| 3141 | find_first_of(const _CharT* __s, size_type __pos = 0) const |
| 3142 | _GLIBCXX_NOEXCEPTnoexcept |
| 3143 | { |
| 3144 | __glibcxx_requires_string(__s); |
| 3145 | return this->find_first_of(__s, __pos, traits_type::length(__s)); |
| 3146 | } |
| 3147 | |
| 3148 | /** |
| 3149 | * @brief Find position of a character. |
| 3150 | * @param __c Character to locate. |
| 3151 | * @param __pos Index of character to search from (default 0). |
| 3152 | * @return Index of first occurrence. |
| 3153 | * |
| 3154 | * Starting from @a __pos, searches forward for the character |
| 3155 | * @a __c within this string. If found, returns the index |
| 3156 | * where it was found. If not found, returns npos. |
| 3157 | * |
| 3158 | * Note: equivalent to find(__c, __pos). |
| 3159 | */ |
| 3160 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3161 | size_type |
| 3162 | find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPTnoexcept |
| 3163 | { return this->find(__c, __pos); } |
| 3164 | |
| 3165 | /** |
| 3166 | * @brief Find last position of a character of string. |
| 3167 | * @param __str String containing characters to locate. |
| 3168 | * @param __pos Index of character to search back from (default end). |
| 3169 | * @return Index of last occurrence. |
| 3170 | * |
| 3171 | * Starting from @a __pos, searches backward for one of the |
| 3172 | * characters of @a __str within this string. If found, |
| 3173 | * returns the index where it was found. If not found, returns |
| 3174 | * npos. |
| 3175 | */ |
| 3176 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3177 | size_type |
| 3178 | find_last_of(const basic_string& __str, size_type __pos = npos) const |
| 3179 | _GLIBCXX_NOEXCEPTnoexcept |
| 3180 | { return this->find_last_of(__str.data(), __pos, __str.size()); } |
| 3181 | |
| 3182 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 3183 | /** |
| 3184 | * @brief Find last position of a character of string. |
| 3185 | * @param __svt An object convertible to string_view containing |
| 3186 | * characters to locate. |
| 3187 | * @param __pos Index of character to search back from (default end). |
| 3188 | * @return Index of last occurrence. |
| 3189 | */ |
| 3190 | template<typename _Tp> |
| 3191 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3192 | _If_sv<_Tp, size_type> |
| 3193 | find_last_of(const _Tp& __svt, size_type __pos = npos) const |
| 3194 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3195 | { |
| 3196 | __sv_type __sv = __svt; |
| 3197 | return this->find_last_of(__sv.data(), __pos, __sv.size()); |
| 3198 | } |
| 3199 | #endif // C++17 |
| 3200 | |
| 3201 | /** |
| 3202 | * @brief Find last position of a character of C substring. |
| 3203 | * @param __s C string containing characters to locate. |
| 3204 | * @param __pos Index of character to search back from. |
| 3205 | * @param __n Number of characters from s to search for. |
| 3206 | * @return Index of last occurrence. |
| 3207 | * |
| 3208 | * Starting from @a __pos, searches backward for one of the |
| 3209 | * first @a __n characters of @a __s within this string. If |
| 3210 | * found, returns the index where it was found. If not found, |
| 3211 | * returns npos. |
| 3212 | */ |
| 3213 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3214 | size_type |
| 3215 | find_last_of(const _CharT* __s, size_type __pos, size_type __n) const |
| 3216 | _GLIBCXX_NOEXCEPTnoexcept; |
| 3217 | |
| 3218 | /** |
| 3219 | * @brief Find last position of a character of C string. |
| 3220 | * @param __s C string containing characters to locate. |
| 3221 | * @param __pos Index of character to search back from (default end). |
| 3222 | * @return Index of last occurrence. |
| 3223 | * |
| 3224 | * Starting from @a __pos, searches backward for one of the |
| 3225 | * characters of @a __s within this string. If found, returns |
| 3226 | * the index where it was found. If not found, returns npos. |
| 3227 | */ |
| 3228 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3229 | size_type |
| 3230 | find_last_of(const _CharT* __s, size_type __pos = npos) const |
| 3231 | _GLIBCXX_NOEXCEPTnoexcept |
| 3232 | { |
| 3233 | __glibcxx_requires_string(__s); |
| 3234 | return this->find_last_of(__s, __pos, traits_type::length(__s)); |
| 3235 | } |
| 3236 | |
| 3237 | /** |
| 3238 | * @brief Find last position of a character. |
| 3239 | * @param __c Character to locate. |
| 3240 | * @param __pos Index of character to search back from (default end). |
| 3241 | * @return Index of last occurrence. |
| 3242 | * |
| 3243 | * Starting from @a __pos, searches backward for @a __c within |
| 3244 | * this string. If found, returns the index where it was |
| 3245 | * found. If not found, returns npos. |
| 3246 | * |
| 3247 | * Note: equivalent to rfind(__c, __pos). |
| 3248 | */ |
| 3249 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3250 | size_type |
| 3251 | find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPTnoexcept |
| 3252 | { return this->rfind(__c, __pos); } |
| 3253 | |
| 3254 | /** |
| 3255 | * @brief Find position of a character not in string. |
| 3256 | * @param __str String containing characters to avoid. |
| 3257 | * @param __pos Index of character to search from (default 0). |
| 3258 | * @return Index of first occurrence. |
| 3259 | * |
| 3260 | * Starting from @a __pos, searches forward for a character not contained |
| 3261 | * in @a __str within this string. If found, returns the index where it |
| 3262 | * was found. If not found, returns npos. |
| 3263 | */ |
| 3264 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3265 | size_type |
| 3266 | find_first_not_of(const basic_string& __str, size_type __pos = 0) const |
| 3267 | _GLIBCXX_NOEXCEPTnoexcept |
| 3268 | { return this->find_first_not_of(__str.data(), __pos, __str.size()); } |
| 3269 | |
| 3270 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 3271 | /** |
| 3272 | * @brief Find position of a character not in a string_view. |
| 3273 | * @param __svt A object convertible to string_view containing |
| 3274 | * characters to avoid. |
| 3275 | * @param __pos Index of character to search from (default 0). |
| 3276 | * @return Index of first occurrence. |
| 3277 | */ |
| 3278 | template<typename _Tp> |
| 3279 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3280 | _If_sv<_Tp, size_type> |
| 3281 | find_first_not_of(const _Tp& __svt, size_type __pos = 0) const |
| 3282 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3283 | { |
| 3284 | __sv_type __sv = __svt; |
| 3285 | return this->find_first_not_of(__sv.data(), __pos, __sv.size()); |
| 3286 | } |
| 3287 | #endif // C++17 |
| 3288 | |
| 3289 | /** |
| 3290 | * @brief Find position of a character not in C substring. |
| 3291 | * @param __s C string containing characters to avoid. |
| 3292 | * @param __pos Index of character to search from. |
| 3293 | * @param __n Number of characters from __s to consider. |
| 3294 | * @return Index of first occurrence. |
| 3295 | * |
| 3296 | * Starting from @a __pos, searches forward for a character not |
| 3297 | * contained in the first @a __n characters of @a __s within |
| 3298 | * this string. If found, returns the index where it was |
| 3299 | * found. If not found, returns npos. |
| 3300 | */ |
| 3301 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3302 | size_type |
| 3303 | find_first_not_of(const _CharT* __s, size_type __pos, |
| 3304 | size_type __n) const _GLIBCXX_NOEXCEPTnoexcept; |
| 3305 | |
| 3306 | /** |
| 3307 | * @brief Find position of a character not in C string. |
| 3308 | * @param __s C string containing characters to avoid. |
| 3309 | * @param __pos Index of character to search from (default 0). |
| 3310 | * @return Index of first occurrence. |
| 3311 | * |
| 3312 | * Starting from @a __pos, searches forward for a character not |
| 3313 | * contained in @a __s within this string. If found, returns |
| 3314 | * the index where it was found. If not found, returns npos. |
| 3315 | */ |
| 3316 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3317 | size_type |
| 3318 | find_first_not_of(const _CharT* __s, size_type __pos = 0) const |
| 3319 | _GLIBCXX_NOEXCEPTnoexcept |
| 3320 | { |
| 3321 | __glibcxx_requires_string(__s); |
| 3322 | return this->find_first_not_of(__s, __pos, traits_type::length(__s)); |
| 3323 | } |
| 3324 | |
| 3325 | /** |
| 3326 | * @brief Find position of a different character. |
| 3327 | * @param __c Character to avoid. |
| 3328 | * @param __pos Index of character to search from (default 0). |
| 3329 | * @return Index of first occurrence. |
| 3330 | * |
| 3331 | * Starting from @a __pos, searches forward for a character |
| 3332 | * other than @a __c within this string. If found, returns the |
| 3333 | * index where it was found. If not found, returns npos. |
| 3334 | */ |
| 3335 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3336 | size_type |
| 3337 | find_first_not_of(_CharT __c, size_type __pos = 0) const |
| 3338 | _GLIBCXX_NOEXCEPTnoexcept; |
| 3339 | |
| 3340 | /** |
| 3341 | * @brief Find last position of a character not in string. |
| 3342 | * @param __str String containing characters to avoid. |
| 3343 | * @param __pos Index of character to search back from (default end). |
| 3344 | * @return Index of last occurrence. |
| 3345 | * |
| 3346 | * Starting from @a __pos, searches backward for a character |
| 3347 | * not contained in @a __str within this string. If found, |
| 3348 | * returns the index where it was found. If not found, returns |
| 3349 | * npos. |
| 3350 | */ |
| 3351 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3352 | size_type |
| 3353 | find_last_not_of(const basic_string& __str, size_type __pos = npos) const |
| 3354 | _GLIBCXX_NOEXCEPTnoexcept |
| 3355 | { return this->find_last_not_of(__str.data(), __pos, __str.size()); } |
| 3356 | |
| 3357 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 3358 | /** |
| 3359 | * @brief Find last position of a character not in a string_view. |
| 3360 | * @param __svt An object convertible to string_view containing |
| 3361 | * characters to avoid. |
| 3362 | * @param __pos Index of character to search back from (default end). |
| 3363 | * @return Index of last occurrence. |
| 3364 | */ |
| 3365 | template<typename _Tp> |
| 3366 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3367 | _If_sv<_Tp, size_type> |
| 3368 | find_last_not_of(const _Tp& __svt, size_type __pos = npos) const |
| 3369 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3370 | { |
| 3371 | __sv_type __sv = __svt; |
| 3372 | return this->find_last_not_of(__sv.data(), __pos, __sv.size()); |
| 3373 | } |
| 3374 | #endif // C++17 |
| 3375 | |
| 3376 | /** |
| 3377 | * @brief Find last position of a character not in C substring. |
| 3378 | * @param __s C string containing characters to avoid. |
| 3379 | * @param __pos Index of character to search back from. |
| 3380 | * @param __n Number of characters from s to consider. |
| 3381 | * @return Index of last occurrence. |
| 3382 | * |
| 3383 | * Starting from @a __pos, searches backward for a character not |
| 3384 | * contained in the first @a __n characters of @a __s within this string. |
| 3385 | * If found, returns the index where it was found. If not found, |
| 3386 | * returns npos. |
| 3387 | */ |
| 3388 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3389 | size_type |
| 3390 | find_last_not_of(const _CharT* __s, size_type __pos, |
| 3391 | size_type __n) const _GLIBCXX_NOEXCEPTnoexcept; |
| 3392 | /** |
| 3393 | * @brief Find last position of a character not in C string. |
| 3394 | * @param __s C string containing characters to avoid. |
| 3395 | * @param __pos Index of character to search back from (default end). |
| 3396 | * @return Index of last occurrence. |
| 3397 | * |
| 3398 | * Starting from @a __pos, searches backward for a character |
| 3399 | * not contained in @a __s within this string. If found, |
| 3400 | * returns the index where it was found. If not found, returns |
| 3401 | * npos. |
| 3402 | */ |
| 3403 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3404 | size_type |
| 3405 | find_last_not_of(const _CharT* __s, size_type __pos = npos) const |
| 3406 | _GLIBCXX_NOEXCEPTnoexcept |
| 3407 | { |
| 3408 | __glibcxx_requires_string(__s); |
| 3409 | return this->find_last_not_of(__s, __pos, traits_type::length(__s)); |
| 3410 | } |
| 3411 | |
| 3412 | /** |
| 3413 | * @brief Find last position of a different character. |
| 3414 | * @param __c Character to avoid. |
| 3415 | * @param __pos Index of character to search back from (default end). |
| 3416 | * @return Index of last occurrence. |
| 3417 | * |
| 3418 | * Starting from @a __pos, searches backward for a character other than |
| 3419 | * @a __c within this string. If found, returns the index where it was |
| 3420 | * found. If not found, returns npos. |
| 3421 | */ |
| 3422 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3423 | size_type |
| 3424 | find_last_not_of(_CharT __c, size_type __pos = npos) const |
| 3425 | _GLIBCXX_NOEXCEPTnoexcept; |
| 3426 | |
| 3427 | /** |
| 3428 | * @brief Get a substring. |
| 3429 | * @param __pos Index of first character (default 0). |
| 3430 | * @param __n Number of characters in substring (default remainder). |
| 3431 | * @return The new string. |
| 3432 | * @throw std::out_of_range If __pos > size(). |
| 3433 | * |
| 3434 | * Construct and return a new string using the @a __n |
| 3435 | * characters starting at @a __pos. If the string is too |
| 3436 | * short, use the remainder of the characters. If @a __pos is |
| 3437 | * beyond the end of the string, out_of_range is thrown. |
| 3438 | */ |
| 3439 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3440 | basic_string |
| 3441 | substr(size_type __pos = 0, size_type __n = npos) const |
| 3442 | { return basic_string(*this, |
| 3443 | _M_check(__pos, "basic_string::substr"), __n); } |
| 3444 | |
| 3445 | /** |
| 3446 | * @brief Compare to a string. |
| 3447 | * @param __str String to compare against. |
| 3448 | * @return Integer < 0, 0, or > 0. |
| 3449 | * |
| 3450 | * Returns an integer < 0 if this string is ordered before @a |
| 3451 | * __str, 0 if their values are equivalent, or > 0 if this |
| 3452 | * string is ordered after @a __str. Determines the effective |
| 3453 | * length rlen of the strings to compare as the smallest of |
| 3454 | * size() and str.size(). The function then compares the two |
| 3455 | * strings by calling traits::compare(data(), str.data(),rlen). |
| 3456 | * If the result of the comparison is nonzero returns it, |
| 3457 | * otherwise the shorter one is ordered first. |
| 3458 | */ |
| 3459 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3460 | int |
| 3461 | compare(const basic_string& __str) const |
| 3462 | { |
| 3463 | const size_type __size = this->size(); |
| 3464 | const size_type __osize = __str.size(); |
| 3465 | const size_type __len = std::min(__size, __osize); |
| 3466 | |
| 3467 | int __r = traits_type::compare(_M_data(), __str.data(), __len); |
| 3468 | if (!__r) |
| 3469 | __r = _S_compare(__size, __osize); |
| 3470 | return __r; |
| 3471 | } |
| 3472 | |
| 3473 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 3474 | /** |
| 3475 | * @brief Compare to a string_view. |
| 3476 | * @param __svt An object convertible to string_view to compare against. |
| 3477 | * @return Integer < 0, 0, or > 0. |
| 3478 | */ |
| 3479 | template<typename _Tp> |
| 3480 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3481 | _If_sv<_Tp, int> |
| 3482 | compare(const _Tp& __svt) const |
| 3483 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3484 | { |
| 3485 | __sv_type __sv = __svt; |
| 3486 | const size_type __size = this->size(); |
| 3487 | const size_type __osize = __sv.size(); |
| 3488 | const size_type __len = std::min(__size, __osize); |
| 3489 | |
| 3490 | int __r = traits_type::compare(_M_data(), __sv.data(), __len); |
| 3491 | if (!__r) |
| 3492 | __r = _S_compare(__size, __osize); |
| 3493 | return __r; |
| 3494 | } |
| 3495 | |
| 3496 | /** |
| 3497 | * @brief Compare to a string_view. |
| 3498 | * @param __pos A position in the string to start comparing from. |
| 3499 | * @param __n The number of characters to compare. |
| 3500 | * @param __svt An object convertible to string_view to compare |
| 3501 | * against. |
| 3502 | * @return Integer < 0, 0, or > 0. |
| 3503 | */ |
| 3504 | template<typename _Tp> |
| 3505 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3506 | _If_sv<_Tp, int> |
| 3507 | compare(size_type __pos, size_type __n, const _Tp& __svt) const |
| 3508 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3509 | { |
| 3510 | __sv_type __sv = __svt; |
| 3511 | return __sv_type(*this).substr(__pos, __n).compare(__sv); |
| 3512 | } |
| 3513 | |
| 3514 | /** |
| 3515 | * @brief Compare to a string_view. |
| 3516 | * @param __pos1 A position in the string to start comparing from. |
| 3517 | * @param __n1 The number of characters to compare. |
| 3518 | * @param __svt An object convertible to string_view to compare |
| 3519 | * against. |
| 3520 | * @param __pos2 A position in the string_view to start comparing from. |
| 3521 | * @param __n2 The number of characters to compare. |
| 3522 | * @return Integer < 0, 0, or > 0. |
| 3523 | */ |
| 3524 | template<typename _Tp> |
| 3525 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3526 | _If_sv<_Tp, int> |
| 3527 | compare(size_type __pos1, size_type __n1, const _Tp& __svt, |
| 3528 | size_type __pos2, size_type __n2 = npos) const |
| 3529 | noexcept(is_same<_Tp, __sv_type>::value) |
| 3530 | { |
| 3531 | __sv_type __sv = __svt; |
| 3532 | return __sv_type(*this) |
| 3533 | .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2)); |
| 3534 | } |
| 3535 | #endif // C++17 |
| 3536 | |
| 3537 | /** |
| 3538 | * @brief Compare substring to a string. |
| 3539 | * @param __pos Index of first character of substring. |
| 3540 | * @param __n Number of characters in substring. |
| 3541 | * @param __str String to compare against. |
| 3542 | * @return Integer < 0, 0, or > 0. |
| 3543 | * |
| 3544 | * Form the substring of this string from the @a __n characters |
| 3545 | * starting at @a __pos. Returns an integer < 0 if the |
| 3546 | * substring is ordered before @a __str, 0 if their values are |
| 3547 | * equivalent, or > 0 if the substring is ordered after @a |
| 3548 | * __str. Determines the effective length rlen of the strings |
| 3549 | * to compare as the smallest of the length of the substring |
| 3550 | * and @a __str.size(). The function then compares the two |
| 3551 | * strings by calling |
| 3552 | * traits::compare(substring.data(),str.data(),rlen). If the |
| 3553 | * result of the comparison is nonzero returns it, otherwise |
| 3554 | * the shorter one is ordered first. |
| 3555 | */ |
| 3556 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3557 | int |
| 3558 | compare(size_type __pos, size_type __n, const basic_string& __str) const |
| 3559 | { |
| 3560 | _M_check(__pos, "basic_string::compare"); |
| 3561 | __n = _M_limit(__pos, __n); |
| 3562 | const size_type __osize = __str.size(); |
| 3563 | const size_type __len = std::min(__n, __osize); |
| 3564 | int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len); |
| 3565 | if (!__r) |
| 3566 | __r = _S_compare(__n, __osize); |
| 3567 | return __r; |
| 3568 | } |
| 3569 | |
| 3570 | /** |
| 3571 | * @brief Compare substring to a substring. |
| 3572 | * @param __pos1 Index of first character of substring. |
| 3573 | * @param __n1 Number of characters in substring. |
| 3574 | * @param __str String to compare against. |
| 3575 | * @param __pos2 Index of first character of substring of str. |
| 3576 | * @param __n2 Number of characters in substring of str. |
| 3577 | * @return Integer < 0, 0, or > 0. |
| 3578 | * |
| 3579 | * Form the substring of this string from the @a __n1 |
| 3580 | * characters starting at @a __pos1. Form the substring of @a |
| 3581 | * __str from the @a __n2 characters starting at @a __pos2. |
| 3582 | * Returns an integer < 0 if this substring is ordered before |
| 3583 | * the substring of @a __str, 0 if their values are equivalent, |
| 3584 | * or > 0 if this substring is ordered after the substring of |
| 3585 | * @a __str. Determines the effective length rlen of the |
| 3586 | * strings to compare as the smallest of the lengths of the |
| 3587 | * substrings. The function then compares the two strings by |
| 3588 | * calling |
| 3589 | * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen). |
| 3590 | * If the result of the comparison is nonzero returns it, |
| 3591 | * otherwise the shorter one is ordered first. |
| 3592 | */ |
| 3593 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3594 | int |
| 3595 | compare(size_type __pos1, size_type __n1, const basic_string& __str, |
| 3596 | size_type __pos2, size_type __n2 = npos) const |
| 3597 | { |
| 3598 | _M_check(__pos1, "basic_string::compare"); |
| 3599 | __str._M_check(__pos2, "basic_string::compare"); |
| 3600 | __n1 = _M_limit(__pos1, __n1); |
| 3601 | __n2 = __str._M_limit(__pos2, __n2); |
| 3602 | const size_type __len = std::min(__n1, __n2); |
| 3603 | int __r = traits_type::compare(_M_data() + __pos1, |
| 3604 | __str.data() + __pos2, __len); |
| 3605 | if (!__r) |
| 3606 | __r = _S_compare(__n1, __n2); |
| 3607 | return __r; |
| 3608 | } |
| 3609 | |
| 3610 | /** |
| 3611 | * @brief Compare to a C string. |
| 3612 | * @param __s C string to compare against. |
| 3613 | * @return Integer < 0, 0, or > 0. |
| 3614 | * |
| 3615 | * Returns an integer < 0 if this string is ordered before @a __s, 0 if |
| 3616 | * their values are equivalent, or > 0 if this string is ordered after |
| 3617 | * @a __s. Determines the effective length rlen of the strings to |
| 3618 | * compare as the smallest of size() and the length of a string |
| 3619 | * constructed from @a __s. The function then compares the two strings |
| 3620 | * by calling traits::compare(data(),s,rlen). If the result of the |
| 3621 | * comparison is nonzero returns it, otherwise the shorter one is |
| 3622 | * ordered first. |
| 3623 | */ |
| 3624 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3625 | int |
| 3626 | compare(const _CharT* __s) const _GLIBCXX_NOEXCEPTnoexcept |
| 3627 | { |
| 3628 | __glibcxx_requires_string(__s); |
| 3629 | const size_type __size = this->size(); |
| 3630 | const size_type __osize = traits_type::length(__s); |
| 3631 | const size_type __len = std::min(__size, __osize); |
| 3632 | int __r = traits_type::compare(_M_data(), __s, __len); |
| 3633 | if (!__r) |
| 3634 | __r = _S_compare(__size, __osize); |
| 3635 | return __r; |
| 3636 | } |
| 3637 | |
| 3638 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 3639 | // 5 String::compare specification questionable |
| 3640 | /** |
| 3641 | * @brief Compare substring to a C string. |
| 3642 | * @param __pos Index of first character of substring. |
| 3643 | * @param __n1 Number of characters in substring. |
| 3644 | * @param __s C string to compare against. |
| 3645 | * @return Integer < 0, 0, or > 0. |
| 3646 | * |
| 3647 | * Form the substring of this string from the @a __n1 |
| 3648 | * characters starting at @a pos. Returns an integer < 0 if |
| 3649 | * the substring is ordered before @a __s, 0 if their values |
| 3650 | * are equivalent, or > 0 if the substring is ordered after @a |
| 3651 | * __s. Determines the effective length rlen of the strings to |
| 3652 | * compare as the smallest of the length of the substring and |
| 3653 | * the length of a string constructed from @a __s. The |
| 3654 | * function then compares the two string by calling |
| 3655 | * traits::compare(substring.data(),__s,rlen). If the result of |
| 3656 | * the comparison is nonzero returns it, otherwise the shorter |
| 3657 | * one is ordered first. |
| 3658 | */ |
| 3659 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3660 | int |
| 3661 | compare(size_type __pos, size_type __n1, const _CharT* __s) const |
| 3662 | { |
| 3663 | __glibcxx_requires_string(__s); |
| 3664 | _M_check(__pos, "basic_string::compare"); |
| 3665 | __n1 = _M_limit(__pos, __n1); |
| 3666 | const size_type __osize = traits_type::length(__s); |
| 3667 | const size_type __len = std::min(__n1, __osize); |
| 3668 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
| 3669 | if (!__r) |
| 3670 | __r = _S_compare(__n1, __osize); |
| 3671 | return __r; |
| 3672 | } |
| 3673 | |
| 3674 | /** |
| 3675 | * @brief Compare substring against a character %array. |
| 3676 | * @param __pos Index of first character of substring. |
| 3677 | * @param __n1 Number of characters in substring. |
| 3678 | * @param __s character %array to compare against. |
| 3679 | * @param __n2 Number of characters of s. |
| 3680 | * @return Integer < 0, 0, or > 0. |
| 3681 | * |
| 3682 | * Form the substring of this string from the @a __n1 |
| 3683 | * characters starting at @a __pos. Form a string from the |
| 3684 | * first @a __n2 characters of @a __s. Returns an integer < 0 |
| 3685 | * if this substring is ordered before the string from @a __s, |
| 3686 | * 0 if their values are equivalent, or > 0 if this substring |
| 3687 | * is ordered after the string from @a __s. Determines the |
| 3688 | * effective length rlen of the strings to compare as the |
| 3689 | * smallest of the length of the substring and @a __n2. The |
| 3690 | * function then compares the two strings by calling |
| 3691 | * traits::compare(substring.data(),s,rlen). If the result of |
| 3692 | * the comparison is nonzero returns it, otherwise the shorter |
| 3693 | * one is ordered first. |
| 3694 | * |
| 3695 | * NB: s must have at least n2 characters, '\\0' has |
| 3696 | * no special meaning. |
| 3697 | */ |
| 3698 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3699 | int |
| 3700 | compare(size_type __pos, size_type __n1, const _CharT* __s, |
| 3701 | size_type __n2) const |
| 3702 | { |
| 3703 | __glibcxx_requires_string_len(__s, __n2); |
| 3704 | _M_check(__pos, "basic_string::compare"); |
| 3705 | __n1 = _M_limit(__pos, __n1); |
| 3706 | const size_type __len = std::min(__n1, __n2); |
| 3707 | int __r = traits_type::compare(_M_data() + __pos, __s, __len); |
| 3708 | if (!__r) |
| 3709 | __r = _S_compare(__n1, __n2); |
| 3710 | return __r; |
| 3711 | } |
| 3712 | |
| 3713 | #if __cplusplus201703L >= 202002L |
| 3714 | [[nodiscard]] |
| 3715 | constexpr bool |
| 3716 | starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
| 3717 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
| 3718 | |
| 3719 | [[nodiscard]] |
| 3720 | constexpr bool |
| 3721 | starts_with(_CharT __x) const noexcept |
| 3722 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
| 3723 | |
| 3724 | [[nodiscard, __gnu__::__nonnull__]] |
| 3725 | constexpr bool |
| 3726 | starts_with(const _CharT* __x) const noexcept |
| 3727 | { return __sv_type(this->data(), this->size()).starts_with(__x); } |
| 3728 | |
| 3729 | [[nodiscard]] |
| 3730 | constexpr bool |
| 3731 | ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept |
| 3732 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
| 3733 | |
| 3734 | [[nodiscard]] |
| 3735 | constexpr bool |
| 3736 | ends_with(_CharT __x) const noexcept |
| 3737 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
| 3738 | |
| 3739 | [[nodiscard, __gnu__::__nonnull__]] |
| 3740 | constexpr bool |
| 3741 | ends_with(const _CharT* __x) const noexcept |
| 3742 | { return __sv_type(this->data(), this->size()).ends_with(__x); } |
| 3743 | #endif // C++20 |
| 3744 | |
| 3745 | #if __cplusplus201703L > 202002L |
| 3746 | [[nodiscard]] |
| 3747 | constexpr bool |
| 3748 | contains(basic_string_view<_CharT, _Traits> __x) const noexcept |
| 3749 | { return __sv_type(this->data(), this->size()).contains(__x); } |
| 3750 | |
| 3751 | [[nodiscard]] |
| 3752 | constexpr bool |
| 3753 | contains(_CharT __x) const noexcept |
| 3754 | { return __sv_type(this->data(), this->size()).contains(__x); } |
| 3755 | |
| 3756 | [[nodiscard, __gnu__::__nonnull__]] |
| 3757 | constexpr bool |
| 3758 | contains(const _CharT* __x) const noexcept |
| 3759 | { return __sv_type(this->data(), this->size()).contains(__x); } |
| 3760 | #endif // C++23 |
| 3761 | |
| 3762 | // Allow basic_stringbuf::__xfer_bufptrs to call _M_length: |
| 3763 | template<typename, typename, typename> friend class basic_stringbuf; |
| 3764 | }; |
| 3765 | _GLIBCXX_END_NAMESPACE_CXX11} |
| 3766 | _GLIBCXX_END_NAMESPACE_VERSION |
| 3767 | } // namespace std |
| 3768 | #endif // _GLIBCXX_USE_CXX11_ABI |
| 3769 | |
| 3770 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
| 3771 | { |
| 3772 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 3773 | |
| 3774 | #if __cpp_deduction_guides201703L >= 201606 |
| 3775 | _GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 { |
| 3776 | template<typename _InputIterator, typename _CharT |
| 3777 | = typename iterator_traits<_InputIterator>::value_type, |
| 3778 | typename _Allocator = allocator<_CharT>, |
| 3779 | typename = _RequireInputIter<_InputIterator>, |
| 3780 | typename = _RequireAllocator<_Allocator>> |
| 3781 | basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator()) |
| 3782 | -> basic_string<_CharT, char_traits<_CharT>, _Allocator>; |
| 3783 | |
| 3784 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 3785 | // 3075. basic_string needs deduction guides from basic_string_view |
| 3786 | template<typename _CharT, typename _Traits, |
| 3787 | typename _Allocator = allocator<_CharT>, |
| 3788 | typename = _RequireAllocator<_Allocator>> |
| 3789 | basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator()) |
| 3790 | -> basic_string<_CharT, _Traits, _Allocator>; |
| 3791 | |
| 3792 | template<typename _CharT, typename _Traits, |
| 3793 | typename _Allocator = allocator<_CharT>, |
| 3794 | typename = _RequireAllocator<_Allocator>> |
| 3795 | basic_string(basic_string_view<_CharT, _Traits>, |
| 3796 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, |
| 3797 | typename basic_string<_CharT, _Traits, _Allocator>::size_type, |
| 3798 | const _Allocator& = _Allocator()) |
| 3799 | -> basic_string<_CharT, _Traits, _Allocator>; |
| 3800 | |
| 3801 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 3802 | template<ranges::input_range _Rg, |
| 3803 | typename _Allocator = allocator<ranges::range_value_t<_Rg>>> |
| 3804 | basic_string(from_range_t, _Rg&&, _Allocator = _Allocator()) |
| 3805 | -> basic_string<ranges::range_value_t<_Rg>, |
| 3806 | char_traits<ranges::range_value_t<_Rg>>, |
| 3807 | _Allocator>; |
| 3808 | #endif |
| 3809 | _GLIBCXX_END_NAMESPACE_CXX11} |
| 3810 | #endif |
| 3811 | |
| 3812 | template<typename _Str> |
| 3813 | _GLIBCXX20_CONSTEXPR |
| 3814 | inline _Str |
| 3815 | __str_concat(typename _Str::value_type const* __lhs, |
| 3816 | typename _Str::size_type __lhs_len, |
| 3817 | typename _Str::value_type const* __rhs, |
| 3818 | typename _Str::size_type __rhs_len, |
| 3819 | typename _Str::allocator_type const& __a) |
| 3820 | { |
| 3821 | typedef typename _Str::allocator_type allocator_type; |
| 3822 | typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits; |
| 3823 | _Str __str(_Alloc_traits::_S_select_on_copy(__a)); |
| 3824 | __str.reserve(__lhs_len + __rhs_len); |
| 3825 | __str.append(__lhs, __lhs_len); |
| 3826 | __str.append(__rhs, __rhs_len); |
| 3827 | return __str; |
| 3828 | } |
| 3829 | |
| 3830 | // operator+ |
| 3831 | /** |
| 3832 | * @brief Concatenate two strings. |
| 3833 | * @param __lhs First string. |
| 3834 | * @param __rhs Last string. |
| 3835 | * @return New string with value of @a __lhs followed by @a __rhs. |
| 3836 | */ |
| 3837 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3838 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3839 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3840 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 3841 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 3842 | { |
| 3843 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
| 3844 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), |
| 3845 | __rhs.c_str(), __rhs.size(), |
| 3846 | __lhs.get_allocator()); |
| 3847 | } |
| 3848 | |
| 3849 | /** |
| 3850 | * @brief Concatenate C string and string. |
| 3851 | * @param __lhs First string. |
| 3852 | * @param __rhs Last string. |
| 3853 | * @return New string with value of @a __lhs followed by @a __rhs. |
| 3854 | */ |
| 3855 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3856 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3857 | inline basic_string<_CharT,_Traits,_Alloc> |
| 3858 | operator+(const _CharT* __lhs, |
| 3859 | const basic_string<_CharT,_Traits,_Alloc>& __rhs) |
| 3860 | { |
| 3861 | __glibcxx_requires_string(__lhs); |
| 3862 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
| 3863 | return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs), |
| 3864 | __rhs.c_str(), __rhs.size(), |
| 3865 | __rhs.get_allocator()); |
| 3866 | } |
| 3867 | |
| 3868 | /** |
| 3869 | * @brief Concatenate character and string. |
| 3870 | * @param __lhs First string. |
| 3871 | * @param __rhs Last string. |
| 3872 | * @return New string with @a __lhs followed by @a __rhs. |
| 3873 | */ |
| 3874 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3875 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3876 | inline basic_string<_CharT,_Traits,_Alloc> |
| 3877 | operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs) |
| 3878 | { |
| 3879 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
| 3880 | return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1, |
| 3881 | __rhs.c_str(), __rhs.size(), |
| 3882 | __rhs.get_allocator()); |
| 3883 | } |
| 3884 | |
| 3885 | /** |
| 3886 | * @brief Concatenate string and C string. |
| 3887 | * @param __lhs First string. |
| 3888 | * @param __rhs Last string. |
| 3889 | * @return New string with @a __lhs followed by @a __rhs. |
| 3890 | */ |
| 3891 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3892 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3893 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3894 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 3895 | const _CharT* __rhs) |
| 3896 | { |
| 3897 | __glibcxx_requires_string(__rhs); |
| 3898 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
| 3899 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), |
| 3900 | __rhs, _Traits::length(__rhs), |
| 3901 | __lhs.get_allocator()); |
| 3902 | } |
| 3903 | /** |
| 3904 | * @brief Concatenate string and character. |
| 3905 | * @param __lhs First string. |
| 3906 | * @param __rhs Last string. |
| 3907 | * @return New string with @a __lhs followed by @a __rhs. |
| 3908 | */ |
| 3909 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3910 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3911 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3912 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) |
| 3913 | { |
| 3914 | typedef basic_string<_CharT, _Traits, _Alloc> _Str; |
| 3915 | return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), |
| 3916 | __builtin_addressof(__rhs), 1, |
| 3917 | __lhs.get_allocator()); |
| 3918 | } |
| 3919 | |
| 3920 | #if __cplusplus201703L >= 201103L |
| 3921 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3922 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3923 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3924 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
| 3925 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 3926 | { return std::move(__lhs.append(__rhs)); } |
| 3927 | |
| 3928 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3929 | _GLIBCXX20_CONSTEXPR |
| 3930 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3931 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 3932 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
| 3933 | { return std::move(__rhs.insert(0, __lhs)); } |
| 3934 | |
| 3935 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3936 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3937 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3938 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
| 3939 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
| 3940 | { |
| 3941 | #if _GLIBCXX_USE_CXX11_ABI1 |
| 3942 | using _Alloc_traits = allocator_traits<_Alloc>; |
| 3943 | bool __use_rhs = false; |
| 3944 | if _GLIBCXX17_CONSTEXPRconstexpr (typename _Alloc_traits::is_always_equal{}) |
| 3945 | __use_rhs = true; |
| 3946 | else if (__lhs.get_allocator() == __rhs.get_allocator()) |
| 3947 | __use_rhs = true; |
| 3948 | if (__use_rhs) |
| 3949 | #endif |
| 3950 | { |
| 3951 | const auto __size = __lhs.size() + __rhs.size(); |
| 3952 | if (__size > __lhs.capacity() && __size <= __rhs.capacity()) |
| 3953 | return std::move(__rhs.insert(0, __lhs)); |
| 3954 | } |
| 3955 | return std::move(__lhs.append(__rhs)); |
| 3956 | } |
| 3957 | |
| 3958 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3959 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3960 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3961 | operator+(const _CharT* __lhs, |
| 3962 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
| 3963 | { return std::move(__rhs.insert(0, __lhs)); } |
| 3964 | |
| 3965 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3966 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3967 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3968 | operator+(_CharT __lhs, |
| 3969 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
| 3970 | { return std::move(__rhs.insert(0, 1, __lhs)); } |
| 3971 | |
| 3972 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3973 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3974 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3975 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
| 3976 | const _CharT* __rhs) |
| 3977 | { return std::move(__lhs.append(__rhs)); } |
| 3978 | |
| 3979 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3980 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 3981 | inline basic_string<_CharT, _Traits, _Alloc> |
| 3982 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
| 3983 | _CharT __rhs) |
| 3984 | { return std::move(__lhs.append(1, __rhs)); } |
| 3985 | #endif |
| 3986 | |
| 3987 | #if __glibcxx_string_view201803L >= 202403L |
| 3988 | // const string & + string_view |
| 3989 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 3990 | [[nodiscard]] |
| 3991 | constexpr basic_string<_CharT, _Traits, _Alloc> |
| 3992 | operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 3993 | type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) |
| 3994 | { |
| 3995 | using _Str = basic_string<_CharT, _Traits, _Alloc>; |
| 3996 | return std::__str_concat<_Str>(__lhs.data(), __lhs.size(), |
| 3997 | __rhs.data(), __rhs.size(), |
| 3998 | __lhs.get_allocator()); |
| 3999 | } |
| 4000 | |
| 4001 | // string && + string_view |
| 4002 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4003 | [[nodiscard]] |
| 4004 | constexpr basic_string<_CharT, _Traits, _Alloc> |
| 4005 | operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs, |
| 4006 | type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) |
| 4007 | { |
| 4008 | return std::move(__lhs.append(__rhs)); |
| 4009 | } |
| 4010 | |
| 4011 | // string_view + const string & |
| 4012 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4013 | [[nodiscard]] |
| 4014 | constexpr basic_string<_CharT, _Traits, _Alloc> |
| 4015 | operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs, |
| 4016 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4017 | { |
| 4018 | using _Str = basic_string<_CharT, _Traits, _Alloc>; |
| 4019 | return std::__str_concat<_Str>(__lhs.data(), __lhs.size(), |
| 4020 | __rhs.data(), __rhs.size(), |
| 4021 | __rhs.get_allocator()); |
| 4022 | } |
| 4023 | |
| 4024 | // string_view + string && |
| 4025 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4026 | [[nodiscard]] |
| 4027 | constexpr basic_string<_CharT, _Traits, _Alloc> |
| 4028 | operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs, |
| 4029 | basic_string<_CharT, _Traits, _Alloc>&& __rhs) |
| 4030 | { |
| 4031 | return std::move(__rhs.insert(0, __lhs)); |
| 4032 | } |
| 4033 | #endif |
| 4034 | |
| 4035 | // operator == |
| 4036 | /** |
| 4037 | * @brief Test equivalence of two strings. |
| 4038 | * @param __lhs First string. |
| 4039 | * @param __rhs Second string. |
| 4040 | * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. |
| 4041 | */ |
| 4042 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4043 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 4044 | inline bool |
| 4045 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4046 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4047 | _GLIBCXX_NOEXCEPTnoexcept |
| 4048 | { |
| 4049 | return __lhs.size() == __rhs.size() |
| 4050 | && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()); |
| 4051 | } |
| 4052 | |
| 4053 | /** |
| 4054 | * @brief Test equivalence of string and C string. |
| 4055 | * @param __lhs String. |
| 4056 | * @param __rhs C string. |
| 4057 | * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise. |
| 4058 | */ |
| 4059 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4060 | _GLIBCXX_NODISCARD[[__nodiscard__]] _GLIBCXX20_CONSTEXPR |
| 4061 | inline bool |
| 4062 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4063 | const _CharT* __rhs) |
| 4064 | { |
| 4065 | return __lhs.size() == _Traits::length(__rhs) |
| 4066 | && !_Traits::compare(__lhs.data(), __rhs, __lhs.size()); |
| 4067 | } |
| 4068 | |
| 4069 | #if __cpp_lib_three_way_comparison |
| 4070 | /** |
| 4071 | * @brief Three-way comparison of a string and a C string. |
| 4072 | * @param __lhs A string. |
| 4073 | * @param __rhs A null-terminated string. |
| 4074 | * @return A value indicating whether `__lhs` is less than, equal to, |
| 4075 | * greater than, or incomparable with `__rhs`. |
| 4076 | */ |
| 4077 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4078 | [[nodiscard]] |
| 4079 | constexpr auto |
| 4080 | operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4081 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept |
| 4082 | -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0)) |
| 4083 | { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); } |
| 4084 | |
| 4085 | /** |
| 4086 | * @brief Three-way comparison of a string and a C string. |
| 4087 | * @param __lhs A string. |
| 4088 | * @param __rhs A null-terminated string. |
| 4089 | * @return A value indicating whether `__lhs` is less than, equal to, |
| 4090 | * greater than, or incomparable with `__rhs`. |
| 4091 | */ |
| 4092 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4093 | [[nodiscard]] |
| 4094 | constexpr auto |
| 4095 | operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4096 | const _CharT* __rhs) noexcept |
| 4097 | -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0)) |
| 4098 | { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); } |
| 4099 | #else |
| 4100 | /** |
| 4101 | * @brief Test equivalence of C string and string. |
| 4102 | * @param __lhs C string. |
| 4103 | * @param __rhs String. |
| 4104 | * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise. |
| 4105 | */ |
| 4106 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4107 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4108 | inline bool |
| 4109 | operator==(const _CharT* __lhs, |
| 4110 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4111 | { return __rhs == __lhs; } |
| 4112 | |
| 4113 | // operator != |
| 4114 | /** |
| 4115 | * @brief Test difference of two strings. |
| 4116 | * @param __lhs First string. |
| 4117 | * @param __rhs Second string. |
| 4118 | * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. |
| 4119 | */ |
| 4120 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4121 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4122 | inline bool |
| 4123 | operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4124 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4125 | _GLIBCXX_NOEXCEPTnoexcept |
| 4126 | { return !(__lhs == __rhs); } |
| 4127 | |
| 4128 | /** |
| 4129 | * @brief Test difference of C string and string. |
| 4130 | * @param __lhs C string. |
| 4131 | * @param __rhs String. |
| 4132 | * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise. |
| 4133 | */ |
| 4134 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4135 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4136 | inline bool |
| 4137 | operator!=(const _CharT* __lhs, |
| 4138 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4139 | { return !(__rhs == __lhs); } |
| 4140 | |
| 4141 | /** |
| 4142 | * @brief Test difference of string and C string. |
| 4143 | * @param __lhs String. |
| 4144 | * @param __rhs C string. |
| 4145 | * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise. |
| 4146 | */ |
| 4147 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4148 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4149 | inline bool |
| 4150 | operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4151 | const _CharT* __rhs) |
| 4152 | { return !(__lhs == __rhs); } |
| 4153 | |
| 4154 | // operator < |
| 4155 | /** |
| 4156 | * @brief Test if string precedes string. |
| 4157 | * @param __lhs First string. |
| 4158 | * @param __rhs Second string. |
| 4159 | * @return True if @a __lhs precedes @a __rhs. False otherwise. |
| 4160 | */ |
| 4161 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4162 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4163 | inline bool |
| 4164 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4165 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4166 | _GLIBCXX_NOEXCEPTnoexcept |
| 4167 | { return __lhs.compare(__rhs) < 0; } |
| 4168 | |
| 4169 | /** |
| 4170 | * @brief Test if string precedes C string. |
| 4171 | * @param __lhs String. |
| 4172 | * @param __rhs C string. |
| 4173 | * @return True if @a __lhs precedes @a __rhs. False otherwise. |
| 4174 | */ |
| 4175 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4176 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4177 | inline bool |
| 4178 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4179 | const _CharT* __rhs) |
| 4180 | { return __lhs.compare(__rhs) < 0; } |
| 4181 | |
| 4182 | /** |
| 4183 | * @brief Test if C string precedes string. |
| 4184 | * @param __lhs C string. |
| 4185 | * @param __rhs String. |
| 4186 | * @return True if @a __lhs precedes @a __rhs. False otherwise. |
| 4187 | */ |
| 4188 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4189 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4190 | inline bool |
| 4191 | operator<(const _CharT* __lhs, |
| 4192 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4193 | { return __rhs.compare(__lhs) > 0; } |
| 4194 | |
| 4195 | // operator > |
| 4196 | /** |
| 4197 | * @brief Test if string follows string. |
| 4198 | * @param __lhs First string. |
| 4199 | * @param __rhs Second string. |
| 4200 | * @return True if @a __lhs follows @a __rhs. False otherwise. |
| 4201 | */ |
| 4202 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4203 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4204 | inline bool |
| 4205 | operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4206 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4207 | _GLIBCXX_NOEXCEPTnoexcept |
| 4208 | { return __lhs.compare(__rhs) > 0; } |
| 4209 | |
| 4210 | /** |
| 4211 | * @brief Test if string follows C string. |
| 4212 | * @param __lhs String. |
| 4213 | * @param __rhs C string. |
| 4214 | * @return True if @a __lhs follows @a __rhs. False otherwise. |
| 4215 | */ |
| 4216 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4217 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4218 | inline bool |
| 4219 | operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4220 | const _CharT* __rhs) |
| 4221 | { return __lhs.compare(__rhs) > 0; } |
| 4222 | |
| 4223 | /** |
| 4224 | * @brief Test if C string follows string. |
| 4225 | * @param __lhs C string. |
| 4226 | * @param __rhs String. |
| 4227 | * @return True if @a __lhs follows @a __rhs. False otherwise. |
| 4228 | */ |
| 4229 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4230 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4231 | inline bool |
| 4232 | operator>(const _CharT* __lhs, |
| 4233 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4234 | { return __rhs.compare(__lhs) < 0; } |
| 4235 | |
| 4236 | // operator <= |
| 4237 | /** |
| 4238 | * @brief Test if string doesn't follow string. |
| 4239 | * @param __lhs First string. |
| 4240 | * @param __rhs Second string. |
| 4241 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. |
| 4242 | */ |
| 4243 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4244 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4245 | inline bool |
| 4246 | operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4247 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4248 | _GLIBCXX_NOEXCEPTnoexcept |
| 4249 | { return __lhs.compare(__rhs) <= 0; } |
| 4250 | |
| 4251 | /** |
| 4252 | * @brief Test if string doesn't follow C string. |
| 4253 | * @param __lhs String. |
| 4254 | * @param __rhs C string. |
| 4255 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. |
| 4256 | */ |
| 4257 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4258 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4259 | inline bool |
| 4260 | operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4261 | const _CharT* __rhs) |
| 4262 | { return __lhs.compare(__rhs) <= 0; } |
| 4263 | |
| 4264 | /** |
| 4265 | * @brief Test if C string doesn't follow string. |
| 4266 | * @param __lhs C string. |
| 4267 | * @param __rhs String. |
| 4268 | * @return True if @a __lhs doesn't follow @a __rhs. False otherwise. |
| 4269 | */ |
| 4270 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4271 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4272 | inline bool |
| 4273 | operator<=(const _CharT* __lhs, |
| 4274 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4275 | { return __rhs.compare(__lhs) >= 0; } |
| 4276 | |
| 4277 | // operator >= |
| 4278 | /** |
| 4279 | * @brief Test if string doesn't precede string. |
| 4280 | * @param __lhs First string. |
| 4281 | * @param __rhs Second string. |
| 4282 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. |
| 4283 | */ |
| 4284 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4285 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4286 | inline bool |
| 4287 | operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4288 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4289 | _GLIBCXX_NOEXCEPTnoexcept |
| 4290 | { return __lhs.compare(__rhs) >= 0; } |
| 4291 | |
| 4292 | /** |
| 4293 | * @brief Test if string doesn't precede C string. |
| 4294 | * @param __lhs String. |
| 4295 | * @param __rhs C string. |
| 4296 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. |
| 4297 | */ |
| 4298 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4299 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4300 | inline bool |
| 4301 | operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4302 | const _CharT* __rhs) |
| 4303 | { return __lhs.compare(__rhs) >= 0; } |
| 4304 | |
| 4305 | /** |
| 4306 | * @brief Test if C string doesn't precede string. |
| 4307 | * @param __lhs C string. |
| 4308 | * @param __rhs String. |
| 4309 | * @return True if @a __lhs doesn't precede @a __rhs. False otherwise. |
| 4310 | */ |
| 4311 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4312 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4313 | inline bool |
| 4314 | operator>=(const _CharT* __lhs, |
| 4315 | const basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4316 | { return __rhs.compare(__lhs) <= 0; } |
| 4317 | #endif // three-way comparison |
| 4318 | |
| 4319 | /** |
| 4320 | * @brief Swap contents of two strings. |
| 4321 | * @param __lhs First string. |
| 4322 | * @param __rhs Second string. |
| 4323 | * |
| 4324 | * Exchanges the contents of @a __lhs and @a __rhs in constant time. |
| 4325 | */ |
| 4326 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4327 | _GLIBCXX20_CONSTEXPR |
| 4328 | inline void |
| 4329 | swap(basic_string<_CharT, _Traits, _Alloc>& __lhs, |
| 4330 | basic_string<_CharT, _Traits, _Alloc>& __rhs) |
| 4331 | _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))noexcept(noexcept(__lhs.swap(__rhs))) |
| 4332 | { __lhs.swap(__rhs); } |
| 4333 | |
| 4334 | |
| 4335 | /** |
| 4336 | * @brief Read stream into a string. |
| 4337 | * @param __is Input stream. |
| 4338 | * @param __str Buffer to store into. |
| 4339 | * @return Reference to the input stream. |
| 4340 | * |
| 4341 | * Stores characters from @a __is into @a __str until whitespace is |
| 4342 | * found, the end of the stream is encountered, or str.max_size() |
| 4343 | * is reached. If is.width() is non-zero, that is the limit on the |
| 4344 | * number of characters stored into @a __str. Any previous |
| 4345 | * contents of @a __str are erased. |
| 4346 | */ |
| 4347 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4348 | basic_istream<_CharT, _Traits>& |
| 4349 | operator>>(basic_istream<_CharT, _Traits>& __is, |
| 4350 | basic_string<_CharT, _Traits, _Alloc>& __str); |
| 4351 | |
| 4352 | template<> |
| 4353 | basic_istream<char>& |
| 4354 | operator>>(basic_istream<char>& __is, basic_string<char>& __str); |
| 4355 | |
| 4356 | /** |
| 4357 | * @brief Write string to a stream. |
| 4358 | * @param __os Output stream. |
| 4359 | * @param __str String to write out. |
| 4360 | * @return Reference to the output stream. |
| 4361 | * |
| 4362 | * Output characters of @a __str into os following the same rules as for |
| 4363 | * writing a C string. |
| 4364 | */ |
| 4365 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4366 | inline basic_ostream<_CharT, _Traits>& |
| 4367 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
| 4368 | const basic_string<_CharT, _Traits, _Alloc>& __str) |
| 4369 | { |
| 4370 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 4371 | // 586. string inserter not a formatted function |
| 4372 | return __ostream_insert(__os, __str.data(), __str.size()); |
| 4373 | } |
| 4374 | |
| 4375 | /** |
| 4376 | * @brief Read a line from stream into a string. |
| 4377 | * @param __is Input stream. |
| 4378 | * @param __str Buffer to store into. |
| 4379 | * @param __delim Character marking end of line. |
| 4380 | * @return Reference to the input stream. |
| 4381 | * |
| 4382 | * Stores characters from @a __is into @a __str until @a __delim is |
| 4383 | * found, the end of the stream is encountered, or str.max_size() |
| 4384 | * is reached. Any previous contents of @a __str are erased. If |
| 4385 | * @a __delim is encountered, it is extracted but not stored into |
| 4386 | * @a __str. |
| 4387 | */ |
| 4388 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4389 | basic_istream<_CharT, _Traits>& |
| 4390 | getline(basic_istream<_CharT, _Traits>& __is, |
| 4391 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim); |
| 4392 | |
| 4393 | /** |
| 4394 | * @brief Read a line from stream into a string. |
| 4395 | * @param __is Input stream. |
| 4396 | * @param __str Buffer to store into. |
| 4397 | * @return Reference to the input stream. |
| 4398 | * |
| 4399 | * Stores characters from is into @a __str until '\n' is |
| 4400 | * found, the end of the stream is encountered, or str.max_size() |
| 4401 | * is reached. Any previous contents of @a __str are erased. If |
| 4402 | * end of line is encountered, it is extracted but not stored into |
| 4403 | * @a __str. |
| 4404 | */ |
| 4405 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4406 | inline basic_istream<_CharT, _Traits>& |
| 4407 | getline(basic_istream<_CharT, _Traits>& __is, |
| 4408 | basic_string<_CharT, _Traits, _Alloc>& __str) |
| 4409 | { return std::getline(__is, __str, __is.widen('\n')); } |
| 4410 | |
| 4411 | #if __cplusplus201703L >= 201103L |
| 4412 | /// Read a line from an rvalue stream into a string. |
| 4413 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4414 | inline basic_istream<_CharT, _Traits>& |
| 4415 | getline(basic_istream<_CharT, _Traits>&& __is, |
| 4416 | basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) |
| 4417 | { return std::getline(__is, __str, __delim); } |
| 4418 | |
| 4419 | /// Read a line from an rvalue stream into a string. |
| 4420 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4421 | inline basic_istream<_CharT, _Traits>& |
| 4422 | getline(basic_istream<_CharT, _Traits>&& __is, |
| 4423 | basic_string<_CharT, _Traits, _Alloc>& __str) |
| 4424 | { return std::getline(__is, __str); } |
| 4425 | #endif |
| 4426 | |
| 4427 | template<> |
| 4428 | basic_istream<char>& |
| 4429 | getline(basic_istream<char>& __in, basic_string<char>& __str, |
| 4430 | char __delim); |
| 4431 | |
| 4432 | #ifdef _GLIBCXX_USE_WCHAR_T1 |
| 4433 | template<> |
| 4434 | basic_istream<wchar_t>& |
| 4435 | getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str, |
| 4436 | wchar_t __delim); |
| 4437 | #endif |
| 4438 | |
| 4439 | _GLIBCXX_END_NAMESPACE_VERSION |
| 4440 | } // namespace |
| 4441 | |
| 4442 | #if __cplusplus201703L >= 201103L |
| 4443 | |
| 4444 | #include <ext/string_conversions.h> |
| 4445 | #include <bits/charconv.h> |
| 4446 | |
| 4447 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
| 4448 | { |
| 4449 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 4450 | _GLIBCXX_BEGIN_NAMESPACE_CXX11namespace __cxx11 { |
| 4451 | |
| 4452 | // 21.4 Numeric Conversions [string.conversions]. |
| 4453 | inline int |
| 4454 | stoi(const string& __str, size_t* __idx = 0, int __base = 10) |
| 4455 | { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(), |
| 4456 | __idx, __base); } |
| 4457 | |
| 4458 | inline long |
| 4459 | stol(const string& __str, size_t* __idx = 0, int __base = 10) |
| 4460 | { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(), |
| 4461 | __idx, __base); } |
| 4462 | |
| 4463 | inline unsigned long |
| 4464 | stoul(const string& __str, size_t* __idx = 0, int __base = 10) |
| 4465 | { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(), |
| 4466 | __idx, __base); } |
| 4467 | |
| 4468 | #if _GLIBCXX_USE_C99_STDLIB1 |
| 4469 | inline long long |
| 4470 | stoll(const string& __str, size_t* __idx = 0, int __base = 10) |
| 4471 | { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(), |
| 4472 | __idx, __base); } |
| 4473 | |
| 4474 | inline unsigned long long |
| 4475 | stoull(const string& __str, size_t* __idx = 0, int __base = 10) |
| 4476 | { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(), |
| 4477 | __idx, __base); } |
| 4478 | #elif __LONG_WIDTH__64 == __LONG_LONG_WIDTH__ |
| 4479 | inline long long |
| 4480 | stoll(const string& __str, size_t* __idx = 0, int __base = 10) |
| 4481 | { return std::stol(__str, __idx, __base); } |
| 4482 | |
| 4483 | inline unsigned long long |
| 4484 | stoull(const string& __str, size_t* __idx = 0, int __base = 10) |
| 4485 | { return std::stoul(__str, __idx, __base); } |
| 4486 | #endif |
| 4487 | |
| 4488 | inline double |
| 4489 | stod(const string& __str, size_t* __idx = 0) |
| 4490 | { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); } |
| 4491 | |
| 4492 | #if _GLIBCXX_HAVE_STRTOF1 |
| 4493 | // NB: strtof vs strtod. |
| 4494 | inline float |
| 4495 | stof(const string& __str, size_t* __idx = 0) |
| 4496 | { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } |
| 4497 | #else |
| 4498 | inline float |
| 4499 | stof(const string& __str, size_t* __idx = 0) |
| 4500 | { |
| 4501 | double __d = std::stod(__str, __idx); |
| 4502 | if (__builtin_isfinite(__d) && __d != 0.0) |
| 4503 | { |
| 4504 | double __abs_d = __builtin_fabs(__d); |
| 4505 | if (__abs_d < __FLT_MIN__1.17549435e-38F || __abs_d > __FLT_MAX__3.40282347e+38F) |
| 4506 | { |
| 4507 | errno(*__errno_location ()) = ERANGE34; |
| 4508 | std::__throw_out_of_range("stof"); |
| 4509 | } |
| 4510 | } |
| 4511 | return __d; |
| 4512 | } |
| 4513 | #endif |
| 4514 | |
| 4515 | #if _GLIBCXX_HAVE_STRTOLD1 && ! _GLIBCXX_HAVE_BROKEN_STRTOLD |
| 4516 | inline long double |
| 4517 | stold(const string& __str, size_t* __idx = 0) |
| 4518 | { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); } |
| 4519 | #elif __DBL_MANT_DIG__53 == __LDBL_MANT_DIG__64 |
| 4520 | inline long double |
| 4521 | stold(const string& __str, size_t* __idx = 0) |
| 4522 | { return std::stod(__str, __idx); } |
| 4523 | #endif |
| 4524 | |
| 4525 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 4526 | // DR 1261. Insufficent overloads for to_string / to_wstring |
| 4527 | |
| 4528 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4529 | inline string |
| 4530 | to_string(int __val) |
| 4531 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_INT__4) <= 32 |
| 4532 | noexcept // any 32-bit value fits in the SSO buffer |
| 4533 | #endif |
| 4534 | { |
| 4535 | const bool __neg = __val < 0; |
| 4536 | const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val; |
| 4537 | const auto __len = __detail::__to_chars_len(__uval); |
| 4538 | string __str; |
| 4539 | __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) { |
| 4540 | __p[0] = '-'; |
| 4541 | __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval); |
| 4542 | return __n; |
| 4543 | }); |
| 4544 | return __str; |
| 4545 | } |
| 4546 | |
| 4547 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4548 | inline string |
| 4549 | to_string(unsigned __val) |
| 4550 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_INT__4) <= 32 |
| 4551 | noexcept // any 32-bit value fits in the SSO buffer |
| 4552 | #endif |
| 4553 | { |
| 4554 | const auto __len = __detail::__to_chars_len(__val); |
| 4555 | string __str; |
| 4556 | __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) { |
| 4557 | __detail::__to_chars_10_impl(__p, __n, __val); |
| 4558 | return __n; |
| 4559 | }); |
| 4560 | return __str; |
| 4561 | } |
| 4562 | |
| 4563 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4564 | inline string |
| 4565 | to_string(long __val) |
| 4566 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_LONG__8) <= 32 |
| 4567 | noexcept // any 32-bit value fits in the SSO buffer |
| 4568 | #endif |
| 4569 | { |
| 4570 | const bool __neg = __val < 0; |
| 4571 | const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val; |
| 4572 | const auto __len = __detail::__to_chars_len(__uval); |
| 4573 | string __str; |
| 4574 | __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) { |
| 4575 | __p[0] = '-'; |
| 4576 | __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval); |
| 4577 | return __n; |
| 4578 | }); |
| 4579 | return __str; |
| 4580 | } |
| 4581 | |
| 4582 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4583 | inline string |
| 4584 | to_string(unsigned long __val) |
| 4585 | #if _GLIBCXX_USE_CXX11_ABI1 && (__CHAR_BIT__8 * __SIZEOF_LONG__8) <= 32 |
| 4586 | noexcept // any 32-bit value fits in the SSO buffer |
| 4587 | #endif |
| 4588 | { |
| 4589 | const auto __len = __detail::__to_chars_len(__val); |
| 4590 | string __str; |
| 4591 | __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) { |
| 4592 | __detail::__to_chars_10_impl(__p, __n, __val); |
| 4593 | return __n; |
| 4594 | }); |
| 4595 | return __str; |
| 4596 | } |
| 4597 | |
| 4598 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4599 | inline string |
| 4600 | to_string(long long __val) |
| 4601 | { |
| 4602 | const bool __neg = __val < 0; |
| 4603 | const unsigned long long __uval |
| 4604 | = __neg ? (unsigned long long)~__val + 1ull : __val; |
| 4605 | const auto __len = __detail::__to_chars_len(__uval); |
| 4606 | string __str; |
| 4607 | __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) { |
| 4608 | __p[0] = '-'; |
| 4609 | __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval); |
| 4610 | return __n; |
| 4611 | }); |
| 4612 | return __str; |
| 4613 | } |
| 4614 | |
| 4615 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4616 | inline string |
| 4617 | to_string(unsigned long long __val) |
| 4618 | { |
| 4619 | const auto __len = __detail::__to_chars_len(__val); |
| 4620 | string __str; |
| 4621 | __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) { |
| 4622 | __detail::__to_chars_10_impl(__p, __n, __val); |
| 4623 | return __n; |
| 4624 | }); |
| 4625 | return __str; |
| 4626 | } |
| 4627 | |
| 4628 | #if __glibcxx_to_string >= 202306L // C++ >= 26 |
| 4629 | |
| 4630 | [[nodiscard]] |
| 4631 | inline string |
| 4632 | to_string(float __val) |
| 4633 | { |
| 4634 | string __str; |
| 4635 | size_t __len = 15; |
| 4636 | do { |
| 4637 | __str.resize_and_overwrite(__len, |
| 4638 | [__val, &__len] (char* __p, size_t __n) { |
| 4639 | auto [__end, __err] = std::to_chars(__p, __p + __n, __val); |
| 4640 | if (__err == errc{}) [[likely]] |
| 4641 | return __end - __p; |
| 4642 | __len *= 2; |
| 4643 | return __p - __p;; |
| 4644 | }); |
| 4645 | } while (__str.empty()); |
| 4646 | return __str; |
| 4647 | } |
| 4648 | |
| 4649 | [[nodiscard]] |
| 4650 | inline string |
| 4651 | to_string(double __val) |
| 4652 | { |
| 4653 | string __str; |
| 4654 | size_t __len = 15; |
| 4655 | do { |
| 4656 | __str.resize_and_overwrite(__len, |
| 4657 | [__val, &__len] (char* __p, size_t __n) { |
| 4658 | auto [__end, __err] = std::to_chars(__p, __p + __n, __val); |
| 4659 | if (__err == errc{}) [[likely]] |
| 4660 | return __end - __p; |
| 4661 | __len *= 2; |
| 4662 | return __p - __p;; |
| 4663 | }); |
| 4664 | } while (__str.empty()); |
| 4665 | return __str; |
| 4666 | } |
| 4667 | |
| 4668 | [[nodiscard]] |
| 4669 | inline string |
| 4670 | to_string(long double __val) |
| 4671 | { |
| 4672 | string __str; |
| 4673 | size_t __len = 15; |
| 4674 | do { |
| 4675 | __str.resize_and_overwrite(__len, |
| 4676 | [__val, &__len] (char* __p, size_t __n) { |
| 4677 | auto [__end, __err] = std::to_chars(__p, __p + __n, __val); |
| 4678 | if (__err == errc{}) [[likely]] |
| 4679 | return __end - __p; |
| 4680 | __len *= 2; |
| 4681 | return __p - __p;; |
| 4682 | }); |
| 4683 | } while (__str.empty()); |
| 4684 | return __str; |
| 4685 | } |
| 4686 | #elif _GLIBCXX_USE_C99_STDIO1 |
| 4687 | #pragma GCC diagnostic push |
| 4688 | #pragma GCC diagnostic ignored "-Wsuggest-attribute=format" |
| 4689 | // NB: (v)snprintf vs sprintf. |
| 4690 | |
| 4691 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4692 | inline string |
| 4693 | to_string(float __val) |
| 4694 | { |
| 4695 | const int __n = |
| 4696 | __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20; |
| 4697 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, |
| 4698 | "%f", __val); |
| 4699 | } |
| 4700 | |
| 4701 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4702 | inline string |
| 4703 | to_string(double __val) |
| 4704 | { |
| 4705 | const int __n = |
| 4706 | __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20; |
| 4707 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, |
| 4708 | "%f", __val); |
| 4709 | } |
| 4710 | |
| 4711 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4712 | inline string |
| 4713 | to_string(long double __val) |
| 4714 | { |
| 4715 | const int __n = |
| 4716 | __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20; |
| 4717 | return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n, |
| 4718 | "%Lf", __val); |
| 4719 | } |
| 4720 | #pragma GCC diagnostic pop |
| 4721 | #endif // _GLIBCXX_USE_C99_STDIO |
| 4722 | |
| 4723 | #if defined(_GLIBCXX_USE_WCHAR_T1) && _GLIBCXX_USE_C99_WCHAR1 |
| 4724 | inline int |
| 4725 | stoi(const wstring& __str, size_t* __idx = 0, int __base = 10) |
| 4726 | { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(), |
| 4727 | __idx, __base); } |
| 4728 | |
| 4729 | inline long |
| 4730 | stol(const wstring& __str, size_t* __idx = 0, int __base = 10) |
| 4731 | { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(), |
| 4732 | __idx, __base); } |
| 4733 | |
| 4734 | inline unsigned long |
| 4735 | stoul(const wstring& __str, size_t* __idx = 0, int __base = 10) |
| 4736 | { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(), |
| 4737 | __idx, __base); } |
| 4738 | |
| 4739 | inline long long |
| 4740 | stoll(const wstring& __str, size_t* __idx = 0, int __base = 10) |
| 4741 | { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(), |
| 4742 | __idx, __base); } |
| 4743 | |
| 4744 | inline unsigned long long |
| 4745 | stoull(const wstring& __str, size_t* __idx = 0, int __base = 10) |
| 4746 | { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(), |
| 4747 | __idx, __base); } |
| 4748 | |
| 4749 | // NB: wcstof vs wcstod. |
| 4750 | inline float |
| 4751 | stof(const wstring& __str, size_t* __idx = 0) |
| 4752 | { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); } |
| 4753 | |
| 4754 | inline double |
| 4755 | stod(const wstring& __str, size_t* __idx = 0) |
| 4756 | { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); } |
| 4757 | |
| 4758 | inline long double |
| 4759 | stold(const wstring& __str, size_t* __idx = 0) |
| 4760 | { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); } |
| 4761 | #endif |
| 4762 | |
| 4763 | #ifdef _GLIBCXX_USE_WCHAR_T1 |
| 4764 | #pragma GCC diagnostic push |
| 4765 | #pragma GCC diagnostic ignored "-Wc++17-extensions" |
| 4766 | _GLIBCXX20_CONSTEXPR |
| 4767 | inline void |
| 4768 | __to_wstring_numeric(const char* __s, int __len, wchar_t* __wout) |
| 4769 | { |
| 4770 | // This condition is true if exec-charset and wide-exec-charset share the |
| 4771 | // same values for the ASCII subset or the EBCDIC invariant character set. |
| 4772 | if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-' |
| 4773 | && wchar_t('.') == L'.' && wchar_t('e') == L'e') |
| 4774 | { |
| 4775 | for (int __i = 0; __i < __len; ++__i) |
| 4776 | __wout[__i] = (wchar_t) __s[__i]; |
| 4777 | } |
| 4778 | else |
| 4779 | { |
| 4780 | wchar_t __wc[256]; |
| 4781 | for (int __i = '0'; __i <= '9'; ++__i) |
| 4782 | __wc[__i] = L'0' + __i; |
| 4783 | __wc['.'] = L'.'; |
| 4784 | __wc['+'] = L'+'; |
| 4785 | __wc['-'] = L'-'; |
| 4786 | __wc['a'] = L'a'; |
| 4787 | __wc['b'] = L'b'; |
| 4788 | __wc['c'] = L'c'; |
| 4789 | __wc['d'] = L'd'; |
| 4790 | __wc['e'] = L'e'; |
| 4791 | __wc['f'] = L'f'; |
| 4792 | __wc['i'] = L'i'; // for "inf" |
| 4793 | __wc['n'] = L'n'; // for "nan" and "inf" |
| 4794 | __wc['p'] = L'p'; // for hexfloats "0x1p1" |
| 4795 | __wc['x'] = L'x'; |
| 4796 | __wc['A'] = L'A'; |
| 4797 | __wc['B'] = L'B'; |
| 4798 | __wc['C'] = L'C'; |
| 4799 | __wc['D'] = L'D'; |
| 4800 | __wc['E'] = L'E'; |
| 4801 | __wc['F'] = L'F'; |
| 4802 | __wc['I'] = L'I'; |
| 4803 | __wc['N'] = L'N'; |
| 4804 | __wc['P'] = L'P'; |
| 4805 | __wc['X'] = L'X'; |
| 4806 | |
| 4807 | for (int __i = 0; __i < __len; ++__i) |
| 4808 | __wout[__i] = __wc[(int)__s[__i]]; |
| 4809 | } |
| 4810 | } |
| 4811 | |
| 4812 | #if __glibcxx_constexpr_string201611L >= 201907L |
| 4813 | constexpr |
| 4814 | #endif |
| 4815 | inline wstring |
| 4816 | #ifdef __glibcxx_string_view201803L // >= C++17 |
| 4817 | __to_wstring_numeric(string_view __s) |
| 4818 | #else |
| 4819 | __to_wstring_numeric(const string& __s) |
| 4820 | #endif |
| 4821 | { |
| 4822 | if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-' |
| 4823 | && wchar_t('.') == L'.' && wchar_t('e') == L'e') |
| 4824 | return wstring(__s.data(), __s.data() + __s.size()); |
| 4825 | else |
| 4826 | { |
| 4827 | wstring __ws; |
| 4828 | auto __f = __s.data(); |
| 4829 | __ws.__resize_and_overwrite(__s.size(), |
| 4830 | [__f] (wchar_t* __to, int __n) { |
| 4831 | std::__to_wstring_numeric(__f, __n, __to); |
| 4832 | return __n; |
| 4833 | }); |
| 4834 | return __ws; |
| 4835 | } |
| 4836 | } |
| 4837 | #pragma GCC diagnostic pop |
| 4838 | |
| 4839 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4840 | inline wstring |
| 4841 | to_wstring(int __val) |
| 4842 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4843 | |
| 4844 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4845 | inline wstring |
| 4846 | to_wstring(unsigned __val) |
| 4847 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4848 | |
| 4849 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4850 | inline wstring |
| 4851 | to_wstring(long __val) |
| 4852 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4853 | |
| 4854 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4855 | inline wstring |
| 4856 | to_wstring(unsigned long __val) |
| 4857 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4858 | |
| 4859 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4860 | inline wstring |
| 4861 | to_wstring(long long __val) |
| 4862 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4863 | |
| 4864 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4865 | inline wstring |
| 4866 | to_wstring(unsigned long long __val) |
| 4867 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4868 | |
| 4869 | #if __glibcxx_to_string || _GLIBCXX_USE_C99_STDIO1 |
| 4870 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4871 | inline wstring |
| 4872 | to_wstring(float __val) |
| 4873 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4874 | |
| 4875 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4876 | inline wstring |
| 4877 | to_wstring(double __val) |
| 4878 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4879 | |
| 4880 | _GLIBCXX_NODISCARD[[__nodiscard__]] |
| 4881 | inline wstring |
| 4882 | to_wstring(long double __val) |
| 4883 | { return std::__to_wstring_numeric(std::to_string(__val)); } |
| 4884 | #endif |
| 4885 | #endif // _GLIBCXX_USE_WCHAR_T |
| 4886 | |
| 4887 | _GLIBCXX_END_NAMESPACE_CXX11} |
| 4888 | _GLIBCXX_END_NAMESPACE_VERSION |
| 4889 | } // namespace |
| 4890 | |
| 4891 | #endif /* C++11 */ |
| 4892 | |
| 4893 | #if __cplusplus201703L >= 201103L |
| 4894 | |
| 4895 | #include <bits/functional_hash.h> |
| 4896 | |
| 4897 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
| 4898 | { |
| 4899 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 4900 | |
| 4901 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 4902 | // 3705. Hashability shouldn't depend on basic_string's allocator |
| 4903 | |
| 4904 | template<typename _CharT, typename _Alloc, |
| 4905 | typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>> |
| 4906 | struct __str_hash_base |
| 4907 | : public __hash_base<size_t, _StrT> |
| 4908 | { |
| 4909 | [[__nodiscard__]] |
| 4910 | size_t |
| 4911 | operator()(const _StrT& __s) const noexcept |
| 4912 | { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); } |
| 4913 | }; |
| 4914 | |
| 4915 | #ifndef _GLIBCXX_COMPATIBILITY_CXX0X |
| 4916 | /// std::hash specialization for string. |
| 4917 | template<typename _Alloc> |
| 4918 | struct hash<basic_string<char, char_traits<char>, _Alloc>> |
| 4919 | : public __str_hash_base<char, _Alloc> |
| 4920 | { }; |
| 4921 | |
| 4922 | /// std::hash specialization for wstring. |
| 4923 | template<typename _Alloc> |
| 4924 | struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>> |
| 4925 | : public __str_hash_base<wchar_t, _Alloc> |
| 4926 | { }; |
| 4927 | |
| 4928 | template<typename _Alloc> |
| 4929 | struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>, |
| 4930 | _Alloc>>> |
| 4931 | : std::false_type |
| 4932 | { }; |
| 4933 | #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */ |
| 4934 | |
| 4935 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 4936 | /// std::hash specialization for u8string. |
| 4937 | template<typename _Alloc> |
| 4938 | struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>> |
| 4939 | : public __str_hash_base<char8_t, _Alloc> |
| 4940 | { }; |
| 4941 | #endif |
| 4942 | |
| 4943 | /// std::hash specialization for u16string. |
| 4944 | template<typename _Alloc> |
| 4945 | struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>> |
| 4946 | : public __str_hash_base<char16_t, _Alloc> |
| 4947 | { }; |
| 4948 | |
| 4949 | /// std::hash specialization for u32string. |
| 4950 | template<typename _Alloc> |
| 4951 | struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>> |
| 4952 | : public __str_hash_base<char32_t, _Alloc> |
| 4953 | { }; |
| 4954 | |
| 4955 | #if ! _GLIBCXX_INLINE_VERSION0 |
| 4956 | // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI. |
| 4957 | template<> struct __is_fast_hash<hash<string>> : std::false_type { }; |
| 4958 | template<> struct __is_fast_hash<hash<wstring>> : std::false_type { }; |
| 4959 | template<> struct __is_fast_hash<hash<u16string>> : std::false_type { }; |
| 4960 | template<> struct __is_fast_hash<hash<u32string>> : std::false_type { }; |
| 4961 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 4962 | template<> struct __is_fast_hash<hash<u8string>> : std::false_type { }; |
| 4963 | #endif |
| 4964 | #else |
| 4965 | // For versioned namespace, assume every std::hash<basic_string<>> is slow. |
| 4966 | template<typename _CharT, typename _Traits, typename _Alloc> |
| 4967 | struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>> |
| 4968 | : std::false_type |
| 4969 | { }; |
| 4970 | #endif |
| 4971 | |
| 4972 | #ifdef __glibcxx_string_udls201304L // C++ >= 14 |
| 4973 | inline namespace literals |
| 4974 | { |
| 4975 | inline namespace string_literals |
| 4976 | { |
| 4977 | #pragma GCC diagnostic push |
| 4978 | #pragma GCC diagnostic ignored "-Wliteral-suffix" |
| 4979 | |
| 4980 | #if __glibcxx_constexpr_string201611L >= 201907L |
| 4981 | # define _GLIBCXX_STRING_CONSTEXPR constexpr |
| 4982 | #else |
| 4983 | # define _GLIBCXX_STRING_CONSTEXPR |
| 4984 | #endif |
| 4985 | |
| 4986 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
| 4987 | inline basic_string<char> |
| 4988 | operator""s(const char* __str, size_t __len) |
| 4989 | { return basic_string<char>{__str, __len}; } |
| 4990 | |
| 4991 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
| 4992 | inline basic_string<wchar_t> |
| 4993 | operator""s(const wchar_t* __str, size_t __len) |
| 4994 | { return basic_string<wchar_t>{__str, __len}; } |
| 4995 | |
| 4996 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 4997 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
| 4998 | inline basic_string<char8_t> |
| 4999 | operator""s(const char8_t* __str, size_t __len) |
| 5000 | { return basic_string<char8_t>{__str, __len}; } |
| 5001 | #endif |
| 5002 | |
| 5003 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
| 5004 | inline basic_string<char16_t> |
| 5005 | operator""s(const char16_t* __str, size_t __len) |
| 5006 | { return basic_string<char16_t>{__str, __len}; } |
| 5007 | |
| 5008 | _GLIBCXX_DEFAULT_ABI_TAG__attribute ((__abi_tag__ ("cxx11"))) _GLIBCXX_STRING_CONSTEXPR |
| 5009 | inline basic_string<char32_t> |
| 5010 | operator""s(const char32_t* __str, size_t __len) |
| 5011 | { return basic_string<char32_t>{__str, __len}; } |
| 5012 | |
| 5013 | #undef _GLIBCXX_STRING_CONSTEXPR |
| 5014 | #pragma GCC diagnostic pop |
| 5015 | } // inline namespace string_literals |
| 5016 | } // inline namespace literals |
| 5017 | #endif // __glibcxx_string_udls |
| 5018 | |
| 5019 | #ifdef __glibcxx_variant202102L // >= C++17 |
| 5020 | namespace __detail::__variant |
| 5021 | { |
| 5022 | template<typename> struct _Never_valueless_alt; // see <variant> |
| 5023 | |
| 5024 | // Provide the strong exception-safety guarantee when emplacing a |
| 5025 | // basic_string into a variant, but only if moving the string cannot throw. |
| 5026 | template<typename _Tp, typename _Traits, typename _Alloc> |
| 5027 | struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>> |
| 5028 | : __and_< |
| 5029 | is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>, |
| 5030 | is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>> |
| 5031 | >::type |
| 5032 | { }; |
| 5033 | } // namespace __detail::__variant |
| 5034 | #endif // C++17 |
| 5035 | |
| 5036 | _GLIBCXX_END_NAMESPACE_VERSION |
| 5037 | } // namespace std |
| 5038 | |
| 5039 | #endif // C++11 |
| 5040 | |
| 5041 | #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 <limits> | |||
| 55 | #include <memory> | |||
| 56 | #include <ostream> | |||
| 57 | #include <set> | |||
| 58 | #include <sstream> | |||
| 59 | #include <string> | |||
| 60 | #include <type_traits> | |||
| 61 | #include <vector> | |||
| 62 | ||||
| 63 | #include "gtest/gtest-assertion-result.h" // IWYU pragma: export | |||
| 64 | #include "gtest/gtest-death-test.h" // IWYU pragma: export | |||
| 65 | #include "gtest/gtest-matchers.h" // IWYU pragma: export | |||
| 66 | #include "gtest/gtest-message.h" // IWYU pragma: export | |||
| 67 | #include "gtest/gtest-param-test.h" // IWYU pragma: export | |||
| 68 | #include "gtest/gtest-printers.h" // IWYU pragma: export | |||
| 69 | #include "gtest/gtest-test-part.h" // IWYU pragma: export | |||
| 70 | #include "gtest/gtest-typed-test.h" // IWYU pragma: export | |||
| 71 | #include "gtest/gtest_pred_impl.h" // IWYU pragma: export | |||
| 72 | #include "gtest/gtest_prod.h" // IWYU pragma: export | |||
| 73 | #include "gtest/internal/gtest-internal.h" | |||
| 74 | #include "gtest/internal/gtest-string.h" | |||
| 75 | ||||
| 76 | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ | |||
| 77 | /* class A needs to have dll-interface to be used by clients of class B */) | |||
| 78 | ||||
| 79 | // Declares the flags. | |||
| 80 | ||||
| 81 | // This flag temporary enables the disabled tests. | |||
| 82 | 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"); | |||
| 83 | ||||
| 84 | // This flag brings the debugger on an assertion failure. | |||
| 85 | 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" ); | |||
| 86 | ||||
| 87 | // This flag controls whether Google Test catches all test-thrown exceptions | |||
| 88 | // and logs them as failures. | |||
| 89 | 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" ); | |||
| 90 | ||||
| 91 | // This flag enables using colors in terminal output. Available values are | |||
| 92 | // "yes" to enable colors, "no" (disable colors), or "auto" (the default) | |||
| 93 | // to let Google Test decide. | |||
| 94 | GTEST_DECLARE_string_(color)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_color; } static_assert(true, "no-op to require trailing semicolon" ); | |||
| 95 | ||||
| 96 | // This flag controls whether the test runner should continue execution past | |||
| 97 | // first failure. | |||
| 98 | 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" ); | |||
| 99 | ||||
| 100 | // This flag sets up the filter to select by name using a glob pattern | |||
| 101 | // the tests to run. If the filter is not given all tests are executed. | |||
| 102 | GTEST_DECLARE_string_(filter)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_filter; } static_assert(true, "no-op to require trailing semicolon" ); | |||
| 103 | ||||
| 104 | // This flag controls whether Google Test installs a signal handler that dumps | |||
| 105 | // debugging information when fatal signals are raised. | |||
| 106 | 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"); | |||
| 107 | ||||
| 108 | // This flag causes the Google Test to list tests. None of the tests listed | |||
| 109 | // are actually run if the flag is provided. | |||
| 110 | 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" ); | |||
| 111 | ||||
| 112 | // This flag controls whether Google Test emits a detailed XML report to a file | |||
| 113 | // in addition to its normal textual output. | |||
| 114 | GTEST_DECLARE_string_(output)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_output; } static_assert(true, "no-op to require trailing semicolon" ); | |||
| 115 | ||||
| 116 | // This flags control whether Google Test prints only test failures. | |||
| 117 | GTEST_DECLARE_bool_(brief)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_brief; } static_assert(true, "no-op to require trailing semicolon" ); | |||
| 118 | ||||
| 119 | // This flags control whether Google Test prints the elapsed time for each | |||
| 120 | // test. | |||
| 121 | 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" ); | |||
| 122 | ||||
| 123 | // This flags control whether Google Test prints UTF8 characters as text. | |||
| 124 | 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" ); | |||
| 125 | ||||
| 126 | // This flag specifies the random number seed. | |||
| 127 | 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" ); | |||
| 128 | ||||
| 129 | // This flag sets how many times the tests are repeated. The default value | |||
| 130 | // is 1. If the value is -1 the tests are repeating forever. | |||
| 131 | 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" ); | |||
| 132 | ||||
| 133 | // This flag controls whether Google Test Environments are recreated for each | |||
| 134 | // repeat of the tests. The default value is true. If set to false the global | |||
| 135 | // test Environment objects are only set up once, for the first iteration, and | |||
| 136 | // only torn down once, for the last. | |||
| 137 | 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"); | |||
| 138 | ||||
| 139 | // This flag controls whether Google Test includes Google Test internal | |||
| 140 | // stack frames in failure stack traces. | |||
| 141 | 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"); | |||
| 142 | ||||
| 143 | // When this flag is specified, tests' order is randomized on every iteration. | |||
| 144 | GTEST_DECLARE_bool_(shuffle)namespace testing { __attribute__((visibility("default"))) extern bool FLAGS_gtest_shuffle; } static_assert(true, "no-op to require trailing semicolon" ); | |||
| 145 | ||||
| 146 | // This flag specifies the maximum number of stack frames to be | |||
| 147 | // printed in a failure message. | |||
| 148 | 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"); | |||
| 149 | ||||
| 150 | // When this flag is specified, a failed assertion will throw an | |||
| 151 | // exception if exceptions are enabled, or exit the program with a | |||
| 152 | // non-zero code otherwise. For use with an external test framework. | |||
| 153 | 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" ); | |||
| 154 | ||||
| 155 | // When this flag is set with a "host:port" string, on supported | |||
| 156 | // platforms test results are streamed to the specified port on | |||
| 157 | // the specified host machine. | |||
| 158 | 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"); | |||
| 159 | ||||
| 160 | #if GTEST_USE_OWN_FLAGFILE_FLAG_1 | |||
| 161 | GTEST_DECLARE_string_(flagfile)namespace testing { __attribute__((visibility("default"))) extern ::std::string FLAGS_gtest_flagfile; } static_assert(true, "no-op to require trailing semicolon" ); | |||
| 162 | #endif // GTEST_USE_OWN_FLAGFILE_FLAG_ | |||
| 163 | ||||
| 164 | namespace testing { | |||
| 165 | ||||
| 166 | // Silence C4100 (unreferenced formal parameter) and 4805 | |||
| 167 | // unsafe mix of type 'const int' and type 'const bool' | |||
| 168 | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4805 4100) | |||
| 169 | ||||
| 170 | // The upper limit for valid stack trace depths. | |||
| 171 | const int kMaxStackTraceDepth = 100; | |||
| 172 | ||||
| 173 | namespace internal { | |||
| 174 | ||||
| 175 | class AssertHelper; | |||
| 176 | class DefaultGlobalTestPartResultReporter; | |||
| 177 | class ExecDeathTest; | |||
| 178 | class NoExecDeathTest; | |||
| 179 | class FinalSuccessChecker; | |||
| 180 | class GTestFlagSaver; | |||
| 181 | class StreamingListenerTest; | |||
| 182 | class TestResultAccessor; | |||
| 183 | class TestEventListenersAccessor; | |||
| 184 | class TestEventRepeater; | |||
| 185 | class UnitTestRecordPropertyTestHelper; | |||
| 186 | class WindowsDeathTest; | |||
| 187 | class FuchsiaDeathTest; | |||
| 188 | class UnitTestImpl* GetUnitTestImpl(); | |||
| 189 | void ReportFailureInUnknownLocation(TestPartResult::Type result_type, | |||
| 190 | const std::string& message); | |||
| 191 | std::set<std::string>* GetIgnoredParameterizedTestSuites(); | |||
| 192 | ||||
| 193 | // A base class that prevents subclasses from being copyable. | |||
| 194 | // We do this instead of using '= delete' so as to avoid triggering warnings | |||
| 195 | // inside user code regarding any of our declarations. | |||
| 196 | class GTestNonCopyable { | |||
| 197 | public: | |||
| 198 | GTestNonCopyable() = default; | |||
| 199 | GTestNonCopyable(const GTestNonCopyable&) = delete; | |||
| 200 | GTestNonCopyable& operator=(const GTestNonCopyable&) = delete; | |||
| 201 | ~GTestNonCopyable() = default; | |||
| 202 | }; | |||
| 203 | ||||
| 204 | } // namespace internal | |||
| 205 | ||||
| 206 | // The friend relationship of some of these classes is cyclic. | |||
| 207 | // If we don't forward declare them the compiler might confuse the classes | |||
| 208 | // in friendship clauses with same named classes on the scope. | |||
| 209 | class Test; | |||
| 210 | class TestSuite; | |||
| 211 | ||||
| 212 | // Old API is still available but deprecated | |||
| 213 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 214 | using TestCase = TestSuite; | |||
| 215 | #endif | |||
| 216 | class TestInfo; | |||
| 217 | class UnitTest; | |||
| 218 | ||||
| 219 | // The abstract class that all tests inherit from. | |||
| 220 | // | |||
| 221 | // In Google Test, a unit test program contains one or many TestSuites, and | |||
| 222 | // each TestSuite contains one or many Tests. | |||
| 223 | // | |||
| 224 | // When you define a test using the TEST macro, you don't need to | |||
| 225 | // explicitly derive from Test - the TEST macro automatically does | |||
| 226 | // this for you. | |||
| 227 | // | |||
| 228 | // The only time you derive from Test is when defining a test fixture | |||
| 229 | // to be used in a TEST_F. For example: | |||
| 230 | // | |||
| 231 | // class FooTest : public testing::Test { | |||
| 232 | // protected: | |||
| 233 | // void SetUp() override { ... } | |||
| 234 | // void TearDown() override { ... } | |||
| 235 | // ... | |||
| 236 | // }; | |||
| 237 | // | |||
| 238 | // TEST_F(FooTest, Bar) { ... } | |||
| 239 | // TEST_F(FooTest, Baz) { ... } | |||
| 240 | // | |||
| 241 | // Test is not copyable. | |||
| 242 | class GTEST_API___attribute__((visibility("default"))) Test { | |||
| 243 | public: | |||
| 244 | friend class TestInfo; | |||
| 245 | ||||
| 246 | // The d'tor is virtual as we intend to inherit from Test. | |||
| 247 | virtual ~Test(); | |||
| 248 | ||||
| 249 | // Sets up the stuff shared by all tests in this test suite. | |||
| 250 | // | |||
| 251 | // Google Test will call Foo::SetUpTestSuite() before running the first | |||
| 252 | // test in test suite Foo. Hence a sub-class can define its own | |||
| 253 | // SetUpTestSuite() method to shadow the one defined in the super | |||
| 254 | // class. | |||
| 255 | static void SetUpTestSuite() {} | |||
| 256 | ||||
| 257 | // Tears down the stuff shared by all tests in this test suite. | |||
| 258 | // | |||
| 259 | // Google Test will call Foo::TearDownTestSuite() after running the last | |||
| 260 | // test in test suite Foo. Hence a sub-class can define its own | |||
| 261 | // TearDownTestSuite() method to shadow the one defined in the super | |||
| 262 | // class. | |||
| 263 | static void TearDownTestSuite() {} | |||
| 264 | ||||
| 265 | // Legacy API is deprecated but still available. Use SetUpTestSuite and | |||
| 266 | // TearDownTestSuite instead. | |||
| 267 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 268 | static void TearDownTestCase() {} | |||
| 269 | static void SetUpTestCase() {} | |||
| 270 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 271 | ||||
| 272 | // Returns true if and only if the current test has a fatal failure. | |||
| 273 | static bool HasFatalFailure(); | |||
| 274 | ||||
| 275 | // Returns true if and only if the current test has a non-fatal failure. | |||
| 276 | static bool HasNonfatalFailure(); | |||
| 277 | ||||
| 278 | // Returns true if and only if the current test was skipped. | |||
| 279 | static bool IsSkipped(); | |||
| 280 | ||||
| 281 | // Returns true if and only if the current test has a (either fatal or | |||
| 282 | // non-fatal) failure. | |||
| 283 | static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } | |||
| 284 | ||||
| 285 | // Logs a property for the current test, test suite, or for the entire | |||
| 286 | // invocation of the test program when used outside of the context of a | |||
| 287 | // test suite. Only the last value for a given key is remembered. These | |||
| 288 | // are public static so they can be called from utility functions that are | |||
| 289 | // not members of the test fixture. Calls to RecordProperty made during | |||
| 290 | // lifespan of the test (from the moment its constructor starts to the | |||
| 291 | // moment its destructor finishes) will be output in XML as attributes of | |||
| 292 | // the <testcase> element. Properties recorded from fixture's | |||
| 293 | // SetUpTestSuite or TearDownTestSuite are logged as attributes of the | |||
| 294 | // corresponding <testsuite> element. Calls to RecordProperty made in the | |||
| 295 | // global context (before or after invocation of RUN_ALL_TESTS and from | |||
| 296 | // SetUp/TearDown method of Environment objects registered with Google | |||
| 297 | // Test) will be output as attributes of the <testsuites> element. | |||
| 298 | static void RecordProperty(const std::string& key, const std::string& value); | |||
| 299 | // We do not define a custom serialization except for values that can be | |||
| 300 | // converted to int64_t, but other values could be logged in this way. | |||
| 301 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | |||
| 302 | bool> = true> | |||
| 303 | static void RecordProperty(const std::string& key, const T& value) { | |||
| 304 | RecordProperty(key, (Message() << value).GetString()); | |||
| 305 | } | |||
| 306 | ||||
| 307 | protected: | |||
| 308 | // Creates a Test object. | |||
| 309 | Test(); | |||
| 310 | ||||
| 311 | // Sets up the test fixture. | |||
| 312 | virtual void SetUp(); | |||
| 313 | ||||
| 314 | // Tears down the test fixture. | |||
| 315 | virtual void TearDown(); | |||
| 316 | ||||
| 317 | private: | |||
| 318 | // Returns true if and only if the current test has the same fixture class | |||
| 319 | // as the first test in the current test suite. | |||
| 320 | static bool HasSameFixtureClass(); | |||
| 321 | ||||
| 322 | // Runs the test after the test fixture has been set up. | |||
| 323 | // | |||
| 324 | // A sub-class must implement this to define the test logic. | |||
| 325 | // | |||
| 326 | // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. | |||
| 327 | // Instead, use the TEST or TEST_F macro. | |||
| 328 | virtual void TestBody() = 0; | |||
| 329 | ||||
| 330 | // Sets up, executes, and tears down the test. | |||
| 331 | void Run(); | |||
| 332 | ||||
| 333 | // Deletes self. We deliberately pick an unusual name for this | |||
| 334 | // internal method to avoid clashing with names used in user TESTs. | |||
| 335 | void DeleteSelf_() { delete this; } | |||
| 336 | ||||
| 337 | const std::unique_ptr<GTEST_FLAG_SAVER_::testing::internal::GTestFlagSaver> gtest_flag_saver_; | |||
| 338 | ||||
| 339 | // Often a user misspells SetUp() as Setup() and spends a long time | |||
| 340 | // wondering why it is never called by Google Test. The declaration of | |||
| 341 | // the following method is solely for catching such an error at | |||
| 342 | // compile time: | |||
| 343 | // | |||
| 344 | // - The return type is deliberately chosen to be not void, so it | |||
| 345 | // will be a conflict if void Setup() is declared in the user's | |||
| 346 | // test fixture. | |||
| 347 | // | |||
| 348 | // - This method is private, so it will be another compiler error | |||
| 349 | // if the method is called from the user's test fixture. | |||
| 350 | // | |||
| 351 | // DO NOT OVERRIDE THIS FUNCTION. | |||
| 352 | // | |||
| 353 | // If you see an error about overriding the following function or | |||
| 354 | // about it being private, you have mis-spelled SetUp() as Setup(). | |||
| 355 | struct Setup_should_be_spelled_SetUp {}; | |||
| 356 | virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } | |||
| 357 | ||||
| 358 | // We disallow copying Tests. | |||
| 359 | Test(const Test&) = delete; | |||
| 360 | Test& operator=(const Test&) = delete; | |||
| 361 | }; | |||
| 362 | ||||
| 363 | typedef internal::TimeInMillis TimeInMillis; | |||
| 364 | ||||
| 365 | // A copyable object representing a user specified test property which can be | |||
| 366 | // output as a key/value string pair. | |||
| 367 | // | |||
| 368 | // Don't inherit from TestProperty as its destructor is not virtual. | |||
| 369 | class TestProperty { | |||
| 370 | public: | |||
| 371 | // C'tor. TestProperty does NOT have a default constructor. | |||
| 372 | // Always use this constructor (with parameters) to create a | |||
| 373 | // TestProperty object. | |||
| 374 | TestProperty(const std::string& a_key, const std::string& a_value) | |||
| 375 | : key_(a_key), value_(a_value) {} | |||
| 376 | ||||
| 377 | // Gets the user supplied key. | |||
| 378 | const char* key() const { return key_.c_str(); } | |||
| 379 | ||||
| 380 | // Gets the user supplied value. | |||
| 381 | const char* value() const { return value_.c_str(); } | |||
| 382 | ||||
| 383 | // Sets a new value, overriding the one supplied in the constructor. | |||
| 384 | void SetValue(const std::string& new_value) { value_ = new_value; } | |||
| 385 | ||||
| 386 | private: | |||
| 387 | // The key supplied by the user. | |||
| 388 | std::string key_; | |||
| 389 | // The value supplied by the user. | |||
| 390 | std::string value_; | |||
| 391 | }; | |||
| 392 | ||||
| 393 | // The result of a single Test. This includes a list of | |||
| 394 | // TestPartResults, a list of TestProperties, a count of how many | |||
| 395 | // death tests there are in the Test, and how much time it took to run | |||
| 396 | // the Test. | |||
| 397 | // | |||
| 398 | // TestResult is not copyable. | |||
| 399 | class GTEST_API___attribute__((visibility("default"))) TestResult { | |||
| 400 | public: | |||
| 401 | // Creates an empty TestResult. | |||
| 402 | TestResult(); | |||
| 403 | ||||
| 404 | // D'tor. Do not inherit from TestResult. | |||
| 405 | ~TestResult(); | |||
| 406 | ||||
| 407 | // Gets the number of all test parts. This is the sum of the number | |||
| 408 | // of successful test parts and the number of failed test parts. | |||
| 409 | int total_part_count() const; | |||
| 410 | ||||
| 411 | // Returns the number of the test properties. | |||
| 412 | int test_property_count() const; | |||
| 413 | ||||
| 414 | // Returns true if and only if the test passed (i.e. no test part failed). | |||
| 415 | bool Passed() const { return !Skipped() && !Failed(); } | |||
| 416 | ||||
| 417 | // Returns true if and only if the test was skipped. | |||
| 418 | bool Skipped() const; | |||
| 419 | ||||
| 420 | // Returns true if and only if the test failed. | |||
| 421 | bool Failed() const; | |||
| 422 | ||||
| 423 | // Returns true if and only if the test fatally failed. | |||
| 424 | bool HasFatalFailure() const; | |||
| 425 | ||||
| 426 | // Returns true if and only if the test has a non-fatal failure. | |||
| 427 | bool HasNonfatalFailure() const; | |||
| 428 | ||||
| 429 | // Returns the elapsed time, in milliseconds. | |||
| 430 | TimeInMillis elapsed_time() const { return elapsed_time_; } | |||
| 431 | ||||
| 432 | // Gets the time of the test case start, in ms from the start of the | |||
| 433 | // UNIX epoch. | |||
| 434 | TimeInMillis start_timestamp() const { return start_timestamp_; } | |||
| 435 | ||||
| 436 | // Returns the i-th test part result among all the results. i can range from 0 | |||
| 437 | // to total_part_count() - 1. If i is not in that range, aborts the program. | |||
| 438 | const TestPartResult& GetTestPartResult(int i) const; | |||
| 439 | ||||
| 440 | // Returns the i-th test property. i can range from 0 to | |||
| 441 | // test_property_count() - 1. If i is not in that range, aborts the | |||
| 442 | // program. | |||
| 443 | const TestProperty& GetTestProperty(int i) const; | |||
| 444 | ||||
| 445 | private: | |||
| 446 | friend class TestInfo; | |||
| 447 | friend class TestSuite; | |||
| 448 | friend class UnitTest; | |||
| 449 | friend class internal::DefaultGlobalTestPartResultReporter; | |||
| 450 | friend class internal::ExecDeathTest; | |||
| 451 | friend class internal::TestResultAccessor; | |||
| 452 | friend class internal::UnitTestImpl; | |||
| 453 | friend class internal::WindowsDeathTest; | |||
| 454 | friend class internal::FuchsiaDeathTest; | |||
| 455 | ||||
| 456 | // Gets the vector of TestPartResults. | |||
| 457 | const std::vector<TestPartResult>& test_part_results() const { | |||
| 458 | return test_part_results_; | |||
| 459 | } | |||
| 460 | ||||
| 461 | // Gets the vector of TestProperties. | |||
| 462 | const std::vector<TestProperty>& test_properties() const { | |||
| 463 | return test_properties_; | |||
| 464 | } | |||
| 465 | ||||
| 466 | // Sets the start time. | |||
| 467 | void set_start_timestamp(TimeInMillis start) { start_timestamp_ = start; } | |||
| 468 | ||||
| 469 | // Sets the elapsed time. | |||
| 470 | void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } | |||
| 471 | ||||
| 472 | // Adds a test property to the list. The property is validated and may add | |||
| 473 | // a non-fatal failure if invalid (e.g., if it conflicts with reserved | |||
| 474 | // key names). If a property is already recorded for the same key, the | |||
| 475 | // value will be updated, rather than storing multiple values for the same | |||
| 476 | // key. xml_element specifies the element for which the property is being | |||
| 477 | // recorded and is used for validation. | |||
| 478 | void RecordProperty(const std::string& xml_element, | |||
| 479 | const TestProperty& test_property); | |||
| 480 | ||||
| 481 | // Adds a failure if the key is a reserved attribute of Google Test | |||
| 482 | // testsuite tags. Returns true if the property is valid. | |||
| 483 | // FIXME: Validate attribute names are legal and human readable. | |||
| 484 | static bool ValidateTestProperty(const std::string& xml_element, | |||
| 485 | const TestProperty& test_property); | |||
| 486 | ||||
| 487 | // Adds a test part result to the list. | |||
| 488 | void AddTestPartResult(const TestPartResult& test_part_result); | |||
| 489 | ||||
| 490 | // Returns the death test count. | |||
| 491 | int death_test_count() const { return death_test_count_; } | |||
| 492 | ||||
| 493 | // Increments the death test count, returning the new count. | |||
| 494 | int increment_death_test_count() { return ++death_test_count_; } | |||
| 495 | ||||
| 496 | // Clears the test part results. | |||
| 497 | void ClearTestPartResults(); | |||
| 498 | ||||
| 499 | // Clears the object. | |||
| 500 | void Clear(); | |||
| 501 | ||||
| 502 | // Protects mutable state of the property vector and of owned | |||
| 503 | // properties, whose values may be updated. | |||
| 504 | internal::Mutex test_properties_mutex_; | |||
| 505 | ||||
| 506 | // The vector of TestPartResults | |||
| 507 | std::vector<TestPartResult> test_part_results_; | |||
| 508 | // The vector of TestProperties | |||
| 509 | std::vector<TestProperty> test_properties_; | |||
| 510 | // Running count of death tests. | |||
| 511 | int death_test_count_; | |||
| 512 | // The start time, in milliseconds since UNIX Epoch. | |||
| 513 | TimeInMillis start_timestamp_; | |||
| 514 | // The elapsed time, in milliseconds. | |||
| 515 | TimeInMillis elapsed_time_; | |||
| 516 | ||||
| 517 | // We disallow copying TestResult. | |||
| 518 | TestResult(const TestResult&) = delete; | |||
| 519 | TestResult& operator=(const TestResult&) = delete; | |||
| 520 | }; // class TestResult | |||
| 521 | ||||
| 522 | // A TestInfo object stores the following information about a test: | |||
| 523 | // | |||
| 524 | // Test suite name | |||
| 525 | // Test name | |||
| 526 | // Whether the test should be run | |||
| 527 | // A function pointer that creates the test object when invoked | |||
| 528 | // Test result | |||
| 529 | // | |||
| 530 | // The constructor of TestInfo registers itself with the UnitTest | |||
| 531 | // singleton such that the RUN_ALL_TESTS() macro knows which tests to | |||
| 532 | // run. | |||
| 533 | class GTEST_API___attribute__((visibility("default"))) TestInfo { | |||
| 534 | public: | |||
| 535 | // Destructs a TestInfo object. This function is not virtual, so | |||
| 536 | // don't inherit from TestInfo. | |||
| 537 | ~TestInfo(); | |||
| 538 | ||||
| 539 | // Returns the test suite name. | |||
| 540 | const char* test_suite_name() const { return test_suite_name_.c_str(); } | |||
| 541 | ||||
| 542 | // Legacy API is deprecated but still available | |||
| 543 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 544 | const char* test_case_name() const { return test_suite_name(); } | |||
| 545 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 546 | ||||
| 547 | // Returns the test name. | |||
| 548 | const char* name() const { return name_.c_str(); } | |||
| 549 | ||||
| 550 | // Returns the name of the parameter type, or NULL if this is not a typed | |||
| 551 | // or a type-parameterized test. | |||
| 552 | const char* type_param() const { | |||
| 553 | if (type_param_ != nullptr) return type_param_->c_str(); | |||
| 554 | return nullptr; | |||
| 555 | } | |||
| 556 | ||||
| 557 | // Returns the text representation of the value parameter, or NULL if this | |||
| 558 | // is not a value-parameterized test. | |||
| 559 | const char* value_param() const { | |||
| 560 | if (value_param_ != nullptr) return value_param_->c_str(); | |||
| 561 | return nullptr; | |||
| 562 | } | |||
| 563 | ||||
| 564 | // Returns the file name where this test is defined. | |||
| 565 | const char* file() const { return location_.file.c_str(); } | |||
| 566 | ||||
| 567 | // Returns the line where this test is defined. | |||
| 568 | int line() const { return location_.line; } | |||
| 569 | ||||
| 570 | // Return true if this test should not be run because it's in another shard. | |||
| 571 | bool is_in_another_shard() const { return is_in_another_shard_; } | |||
| 572 | ||||
| 573 | // Returns true if this test should run, that is if the test is not | |||
| 574 | // disabled (or it is disabled but the also_run_disabled_tests flag has | |||
| 575 | // been specified) and its full name matches the user-specified filter. | |||
| 576 | // | |||
| 577 | // Google Test allows the user to filter the tests by their full names. | |||
| 578 | // The full name of a test Bar in test suite Foo is defined as | |||
| 579 | // "Foo.Bar". Only the tests that match the filter will run. | |||
| 580 | // | |||
| 581 | // A filter is a colon-separated list of glob (not regex) patterns, | |||
| 582 | // optionally followed by a '-' and a colon-separated list of | |||
| 583 | // negative patterns (tests to exclude). A test is run if it | |||
| 584 | // matches one of the positive patterns and does not match any of | |||
| 585 | // the negative patterns. | |||
| 586 | // | |||
| 587 | // For example, *A*:Foo.* is a filter that matches any string that | |||
| 588 | // contains the character 'A' or starts with "Foo.". | |||
| 589 | bool should_run() const { return should_run_; } | |||
| 590 | ||||
| 591 | // Returns true if and only if this test will appear in the XML report. | |||
| 592 | bool is_reportable() const { | |||
| 593 | // The XML report includes tests matching the filter, excluding those | |||
| 594 | // run in other shards. | |||
| 595 | return matches_filter_ && !is_in_another_shard_; | |||
| 596 | } | |||
| 597 | ||||
| 598 | // Returns the result of the test. | |||
| 599 | const TestResult* result() const { return &result_; } | |||
| 600 | ||||
| 601 | private: | |||
| 602 | #ifdef GTEST_HAS_DEATH_TEST1 | |||
| 603 | friend class internal::DefaultDeathTestFactory; | |||
| 604 | #endif // GTEST_HAS_DEATH_TEST | |||
| 605 | friend class Test; | |||
| 606 | friend class TestSuite; | |||
| 607 | friend class internal::UnitTestImpl; | |||
| 608 | friend class internal::StreamingListenerTest; | |||
| 609 | friend TestInfo* internal::MakeAndRegisterTestInfo( | |||
| 610 | std::string test_suite_name, const char* name, const char* type_param, | |||
| 611 | const char* value_param, internal::CodeLocation code_location, | |||
| 612 | internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc, | |||
| 613 | internal::TearDownTestSuiteFunc tear_down_tc, | |||
| 614 | internal::TestFactoryBase* factory); | |||
| 615 | ||||
| 616 | // Constructs a TestInfo object. The newly constructed instance assumes | |||
| 617 | // ownership of the factory object. | |||
| 618 | TestInfo(std::string test_suite_name, std::string name, | |||
| 619 | const char* a_type_param, // NULL if not a type-parameterized test | |||
| 620 | const char* a_value_param, // NULL if not a value-parameterized test | |||
| 621 | internal::CodeLocation a_code_location, | |||
| 622 | internal::TypeId fixture_class_id, | |||
| 623 | internal::TestFactoryBase* factory); | |||
| 624 | ||||
| 625 | // Increments the number of death tests encountered in this test so | |||
| 626 | // far. | |||
| 627 | int increment_death_test_count() { | |||
| 628 | return result_.increment_death_test_count(); | |||
| 629 | } | |||
| 630 | ||||
| 631 | // Creates the test object, runs it, records its result, and then | |||
| 632 | // deletes it. | |||
| 633 | void Run(); | |||
| 634 | ||||
| 635 | // Skip and records the test result for this object. | |||
| 636 | void Skip(); | |||
| 637 | ||||
| 638 | static void ClearTestResult(TestInfo* test_info) { | |||
| 639 | test_info->result_.Clear(); | |||
| 640 | } | |||
| 641 | ||||
| 642 | // These fields are immutable properties of the test. | |||
| 643 | const std::string test_suite_name_; // test suite name | |||
| 644 | const std::string name_; // Test name | |||
| 645 | // Name of the parameter type, or NULL if this is not a typed or a | |||
| 646 | // type-parameterized test. | |||
| 647 | const std::unique_ptr<const ::std::string> type_param_; | |||
| 648 | // Text representation of the value parameter, or NULL if this is not a | |||
| 649 | // value-parameterized test. | |||
| 650 | const std::unique_ptr<const ::std::string> value_param_; | |||
| 651 | internal::CodeLocation location_; | |||
| 652 | const internal::TypeId fixture_class_id_; // ID of the test fixture class | |||
| 653 | bool should_run_; // True if and only if this test should run | |||
| 654 | bool is_disabled_; // True if and only if this test is disabled | |||
| 655 | bool matches_filter_; // True if this test matches the | |||
| 656 | // user-specified filter. | |||
| 657 | bool is_in_another_shard_; // Will be run in another shard. | |||
| 658 | internal::TestFactoryBase* const factory_; // The factory that creates | |||
| 659 | // the test object | |||
| 660 | ||||
| 661 | // This field is mutable and needs to be reset before running the | |||
| 662 | // test for the second time. | |||
| 663 | TestResult result_; | |||
| 664 | ||||
| 665 | TestInfo(const TestInfo&) = delete; | |||
| 666 | TestInfo& operator=(const TestInfo&) = delete; | |||
| 667 | }; | |||
| 668 | ||||
| 669 | // A test suite, which consists of a vector of TestInfos. | |||
| 670 | // | |||
| 671 | // TestSuite is not copyable. | |||
| 672 | class GTEST_API___attribute__((visibility("default"))) TestSuite { | |||
| 673 | public: | |||
| 674 | // Creates a TestSuite with the given name. | |||
| 675 | // | |||
| 676 | // TestSuite does NOT have a default constructor. Always use this | |||
| 677 | // constructor to create a TestSuite object. | |||
| 678 | // | |||
| 679 | // Arguments: | |||
| 680 | // | |||
| 681 | // name: name of the test suite | |||
| 682 | // a_type_param: the name of the test's type parameter, or NULL if | |||
| 683 | // this is not a type-parameterized test. | |||
| 684 | // set_up_tc: pointer to the function that sets up the test suite | |||
| 685 | // tear_down_tc: pointer to the function that tears down the test suite | |||
| 686 | TestSuite(const std::string& name, const char* a_type_param, | |||
| 687 | internal::SetUpTestSuiteFunc set_up_tc, | |||
| 688 | internal::TearDownTestSuiteFunc tear_down_tc); | |||
| 689 | ||||
| 690 | // Destructor of TestSuite. | |||
| 691 | virtual ~TestSuite(); | |||
| 692 | ||||
| 693 | // Gets the name of the TestSuite. | |||
| 694 | const char* name() const { return name_.c_str(); } | |||
| 695 | ||||
| 696 | // Returns the name of the parameter type, or NULL if this is not a | |||
| 697 | // type-parameterized test suite. | |||
| 698 | const char* type_param() const { | |||
| 699 | if (type_param_ != nullptr) return type_param_->c_str(); | |||
| 700 | return nullptr; | |||
| 701 | } | |||
| 702 | ||||
| 703 | // Returns true if any test in this test suite should run. | |||
| 704 | bool should_run() const { return should_run_; } | |||
| 705 | ||||
| 706 | // Gets the number of successful tests in this test suite. | |||
| 707 | int successful_test_count() const; | |||
| 708 | ||||
| 709 | // Gets the number of skipped tests in this test suite. | |||
| 710 | int skipped_test_count() const; | |||
| 711 | ||||
| 712 | // Gets the number of failed tests in this test suite. | |||
| 713 | int failed_test_count() const; | |||
| 714 | ||||
| 715 | // Gets the number of disabled tests that will be reported in the XML report. | |||
| 716 | int reportable_disabled_test_count() const; | |||
| 717 | ||||
| 718 | // Gets the number of disabled tests in this test suite. | |||
| 719 | int disabled_test_count() const; | |||
| 720 | ||||
| 721 | // Gets the number of tests to be printed in the XML report. | |||
| 722 | int reportable_test_count() const; | |||
| 723 | ||||
| 724 | // Get the number of tests in this test suite that should run. | |||
| 725 | int test_to_run_count() const; | |||
| 726 | ||||
| 727 | // Gets the number of all tests in this test suite. | |||
| 728 | int total_test_count() const; | |||
| 729 | ||||
| 730 | // Returns true if and only if the test suite passed. | |||
| 731 | bool Passed() const { return !Failed(); } | |||
| 732 | ||||
| 733 | // Returns true if and only if the test suite failed. | |||
| 734 | bool Failed() const { | |||
| 735 | return failed_test_count() > 0 || ad_hoc_test_result().Failed(); | |||
| 736 | } | |||
| 737 | ||||
| 738 | // Returns the elapsed time, in milliseconds. | |||
| 739 | TimeInMillis elapsed_time() const { return elapsed_time_; } | |||
| 740 | ||||
| 741 | // Gets the time of the test suite start, in ms from the start of the | |||
| 742 | // UNIX epoch. | |||
| 743 | TimeInMillis start_timestamp() const { return start_timestamp_; } | |||
| 744 | ||||
| 745 | // Returns the i-th test among all the tests. i can range from 0 to | |||
| 746 | // total_test_count() - 1. If i is not in that range, returns NULL. | |||
| 747 | const TestInfo* GetTestInfo(int i) const; | |||
| 748 | ||||
| 749 | // Returns the TestResult that holds test properties recorded during | |||
| 750 | // execution of SetUpTestSuite and TearDownTestSuite. | |||
| 751 | const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } | |||
| 752 | ||||
| 753 | private: | |||
| 754 | friend class Test; | |||
| 755 | friend class internal::UnitTestImpl; | |||
| 756 | ||||
| 757 | // Gets the (mutable) vector of TestInfos in this TestSuite. | |||
| 758 | std::vector<TestInfo*>& test_info_list() { return test_info_list_; } | |||
| 759 | ||||
| 760 | // Gets the (immutable) vector of TestInfos in this TestSuite. | |||
| 761 | const std::vector<TestInfo*>& test_info_list() const { | |||
| 762 | return test_info_list_; | |||
| 763 | } | |||
| 764 | ||||
| 765 | // Returns the i-th test among all the tests. i can range from 0 to | |||
| 766 | // total_test_count() - 1. If i is not in that range, returns NULL. | |||
| 767 | TestInfo* GetMutableTestInfo(int i); | |||
| 768 | ||||
| 769 | // Sets the should_run member. | |||
| 770 | void set_should_run(bool should) { should_run_ = should; } | |||
| 771 | ||||
| 772 | // Adds a TestInfo to this test suite. Will delete the TestInfo upon | |||
| 773 | // destruction of the TestSuite object. | |||
| 774 | void AddTestInfo(TestInfo* test_info); | |||
| 775 | ||||
| 776 | // Clears the results of all tests in this test suite. | |||
| 777 | void ClearResult(); | |||
| 778 | ||||
| 779 | // Clears the results of all tests in the given test suite. | |||
| 780 | static void ClearTestSuiteResult(TestSuite* test_suite) { | |||
| 781 | test_suite->ClearResult(); | |||
| 782 | } | |||
| 783 | ||||
| 784 | // Runs every test in this TestSuite. | |||
| 785 | void Run(); | |||
| 786 | ||||
| 787 | // Skips the execution of tests under this TestSuite | |||
| 788 | void Skip(); | |||
| 789 | ||||
| 790 | // Runs SetUpTestSuite() for this TestSuite. This wrapper is needed | |||
| 791 | // for catching exceptions thrown from SetUpTestSuite(). | |||
| 792 | void RunSetUpTestSuite() { | |||
| 793 | if (set_up_tc_ != nullptr) { | |||
| 794 | (*set_up_tc_)(); | |||
| 795 | } | |||
| 796 | } | |||
| 797 | ||||
| 798 | // Runs TearDownTestSuite() for this TestSuite. This wrapper is | |||
| 799 | // needed for catching exceptions thrown from TearDownTestSuite(). | |||
| 800 | void RunTearDownTestSuite() { | |||
| 801 | if (tear_down_tc_ != nullptr) { | |||
| 802 | (*tear_down_tc_)(); | |||
| 803 | } | |||
| 804 | } | |||
| 805 | ||||
| 806 | // Returns true if and only if test passed. | |||
| 807 | static bool TestPassed(const TestInfo* test_info) { | |||
| 808 | return test_info->should_run() && test_info->result()->Passed(); | |||
| 809 | } | |||
| 810 | ||||
| 811 | // Returns true if and only if test skipped. | |||
| 812 | static bool TestSkipped(const TestInfo* test_info) { | |||
| 813 | return test_info->should_run() && test_info->result()->Skipped(); | |||
| 814 | } | |||
| 815 | ||||
| 816 | // Returns true if and only if test failed. | |||
| 817 | static bool TestFailed(const TestInfo* test_info) { | |||
| 818 | return test_info->should_run() && test_info->result()->Failed(); | |||
| 819 | } | |||
| 820 | ||||
| 821 | // Returns true if and only if the test is disabled and will be reported in | |||
| 822 | // the XML report. | |||
| 823 | static bool TestReportableDisabled(const TestInfo* test_info) { | |||
| 824 | return test_info->is_reportable() && test_info->is_disabled_; | |||
| 825 | } | |||
| 826 | ||||
| 827 | // Returns true if and only if test is disabled. | |||
| 828 | static bool TestDisabled(const TestInfo* test_info) { | |||
| 829 | return test_info->is_disabled_; | |||
| 830 | } | |||
| 831 | ||||
| 832 | // Returns true if and only if this test will appear in the XML report. | |||
| 833 | static bool TestReportable(const TestInfo* test_info) { | |||
| 834 | return test_info->is_reportable(); | |||
| 835 | } | |||
| 836 | ||||
| 837 | // Returns true if the given test should run. | |||
| 838 | static bool ShouldRunTest(const TestInfo* test_info) { | |||
| 839 | return test_info->should_run(); | |||
| 840 | } | |||
| 841 | ||||
| 842 | // Shuffles the tests in this test suite. | |||
| 843 | void ShuffleTests(internal::Random* random); | |||
| 844 | ||||
| 845 | // Restores the test order to before the first shuffle. | |||
| 846 | void UnshuffleTests(); | |||
| 847 | ||||
| 848 | // Name of the test suite. | |||
| 849 | std::string name_; | |||
| 850 | // Name of the parameter type, or NULL if this is not a typed or a | |||
| 851 | // type-parameterized test. | |||
| 852 | const std::unique_ptr<const ::std::string> type_param_; | |||
| 853 | // The vector of TestInfos in their original order. It owns the | |||
| 854 | // elements in the vector. | |||
| 855 | std::vector<TestInfo*> test_info_list_; | |||
| 856 | // Provides a level of indirection for the test list to allow easy | |||
| 857 | // shuffling and restoring the test order. The i-th element in this | |||
| 858 | // vector is the index of the i-th test in the shuffled test list. | |||
| 859 | std::vector<int> test_indices_; | |||
| 860 | // Pointer to the function that sets up the test suite. | |||
| 861 | internal::SetUpTestSuiteFunc set_up_tc_; | |||
| 862 | // Pointer to the function that tears down the test suite. | |||
| 863 | internal::TearDownTestSuiteFunc tear_down_tc_; | |||
| 864 | // True if and only if any test in this test suite should run. | |||
| 865 | bool should_run_; | |||
| 866 | // The start time, in milliseconds since UNIX Epoch. | |||
| 867 | TimeInMillis start_timestamp_; | |||
| 868 | // Elapsed time, in milliseconds. | |||
| 869 | TimeInMillis elapsed_time_; | |||
| 870 | // Holds test properties recorded during execution of SetUpTestSuite and | |||
| 871 | // TearDownTestSuite. | |||
| 872 | TestResult ad_hoc_test_result_; | |||
| 873 | ||||
| 874 | // We disallow copying TestSuites. | |||
| 875 | TestSuite(const TestSuite&) = delete; | |||
| 876 | TestSuite& operator=(const TestSuite&) = delete; | |||
| 877 | }; | |||
| 878 | ||||
| 879 | // An Environment object is capable of setting up and tearing down an | |||
| 880 | // environment. You should subclass this to define your own | |||
| 881 | // environment(s). | |||
| 882 | // | |||
| 883 | // An Environment object does the set-up and tear-down in virtual | |||
| 884 | // methods SetUp() and TearDown() instead of the constructor and the | |||
| 885 | // destructor, as: | |||
| 886 | // | |||
| 887 | // 1. You cannot safely throw from a destructor. This is a problem | |||
| 888 | // as in some cases Google Test is used where exceptions are enabled, and | |||
| 889 | // we may want to implement ASSERT_* using exceptions where they are | |||
| 890 | // available. | |||
| 891 | // 2. You cannot use ASSERT_* directly in a constructor or | |||
| 892 | // destructor. | |||
| 893 | class Environment { | |||
| 894 | public: | |||
| 895 | // The d'tor is virtual as we need to subclass Environment. | |||
| 896 | virtual ~Environment() = default; | |||
| 897 | ||||
| 898 | // Override this to define how to set up the environment. | |||
| 899 | virtual void SetUp() {} | |||
| 900 | ||||
| 901 | // Override this to define how to tear down the environment. | |||
| 902 | virtual void TearDown() {} | |||
| 903 | ||||
| 904 | private: | |||
| 905 | // If you see an error about overriding the following function or | |||
| 906 | // about it being private, you have mis-spelled SetUp() as Setup(). | |||
| 907 | struct Setup_should_be_spelled_SetUp {}; | |||
| 908 | virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } | |||
| 909 | }; | |||
| 910 | ||||
| 911 | #if GTEST_HAS_EXCEPTIONS1 | |||
| 912 | ||||
| 913 | // Exception which can be thrown from TestEventListener::OnTestPartResult. | |||
| 914 | class GTEST_API___attribute__((visibility("default"))) AssertionException | |||
| 915 | : public internal::GoogleTestFailureException { | |||
| 916 | public: | |||
| 917 | explicit AssertionException(const TestPartResult& result) | |||
| 918 | : GoogleTestFailureException(result) {} | |||
| 919 | }; | |||
| 920 | ||||
| 921 | #endif // GTEST_HAS_EXCEPTIONS | |||
| 922 | ||||
| 923 | // The interface for tracing execution of tests. The methods are organized in | |||
| 924 | // the order the corresponding events are fired. | |||
| 925 | class TestEventListener { | |||
| 926 | public: | |||
| 927 | virtual ~TestEventListener() = default; | |||
| 928 | ||||
| 929 | // Fired before any test activity starts. | |||
| 930 | virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; | |||
| 931 | ||||
| 932 | // Fired before each iteration of tests starts. There may be more than | |||
| 933 | // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration | |||
| 934 | // index, starting from 0. | |||
| 935 | virtual void OnTestIterationStart(const UnitTest& unit_test, | |||
| 936 | int iteration) = 0; | |||
| 937 | ||||
| 938 | // Fired before environment set-up for each iteration of tests starts. | |||
| 939 | virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; | |||
| 940 | ||||
| 941 | // Fired after environment set-up for each iteration of tests ends. | |||
| 942 | virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; | |||
| 943 | ||||
| 944 | // Fired before the test suite starts. | |||
| 945 | virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {} | |||
| 946 | ||||
| 947 | // Legacy API is deprecated but still available | |||
| 948 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 949 | virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} | |||
| 950 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 951 | ||||
| 952 | // Fired before the test starts. | |||
| 953 | virtual void OnTestStart(const TestInfo& test_info) = 0; | |||
| 954 | ||||
| 955 | // Fired when a test is disabled | |||
| 956 | virtual void OnTestDisabled(const TestInfo& /*test_info*/) {} | |||
| 957 | ||||
| 958 | // Fired after a failed assertion or a SUCCEED() invocation. | |||
| 959 | // If you want to throw an exception from this function to skip to the next | |||
| 960 | // TEST, it must be AssertionException defined above, or inherited from it. | |||
| 961 | virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; | |||
| 962 | ||||
| 963 | // Fired after the test ends. | |||
| 964 | virtual void OnTestEnd(const TestInfo& test_info) = 0; | |||
| 965 | ||||
| 966 | // Fired after the test suite ends. | |||
| 967 | virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {} | |||
| 968 | ||||
| 969 | // Legacy API is deprecated but still available | |||
| 970 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 971 | virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} | |||
| 972 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 973 | ||||
| 974 | // Fired before environment tear-down for each iteration of tests starts. | |||
| 975 | virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; | |||
| 976 | ||||
| 977 | // Fired after environment tear-down for each iteration of tests ends. | |||
| 978 | virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; | |||
| 979 | ||||
| 980 | // Fired after each iteration of tests finishes. | |||
| 981 | virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration) = 0; | |||
| 982 | ||||
| 983 | // Fired after all test activities have ended. | |||
| 984 | virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; | |||
| 985 | }; | |||
| 986 | ||||
| 987 | // The convenience class for users who need to override just one or two | |||
| 988 | // methods and are not concerned that a possible change to a signature of | |||
| 989 | // the methods they override will not be caught during the build. For | |||
| 990 | // comments about each method please see the definition of TestEventListener | |||
| 991 | // above. | |||
| 992 | class EmptyTestEventListener : public TestEventListener { | |||
| 993 | public: | |||
| 994 | void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} | |||
| 995 | void OnTestIterationStart(const UnitTest& /*unit_test*/, | |||
| 996 | int /*iteration*/) override {} | |||
| 997 | void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} | |||
| 998 | void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} | |||
| 999 | void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} | |||
| 1000 | // Legacy API is deprecated but still available | |||
| 1001 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1002 | void OnTestCaseStart(const TestCase& /*test_case*/) override {} | |||
| 1003 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1004 | ||||
| 1005 | void OnTestStart(const TestInfo& /*test_info*/) override {} | |||
| 1006 | void OnTestDisabled(const TestInfo& /*test_info*/) override {} | |||
| 1007 | void OnTestPartResult(const TestPartResult& /*test_part_result*/) override {} | |||
| 1008 | void OnTestEnd(const TestInfo& /*test_info*/) override {} | |||
| 1009 | void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} | |||
| 1010 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1011 | void OnTestCaseEnd(const TestCase& /*test_case*/) override {} | |||
| 1012 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1013 | ||||
| 1014 | void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} | |||
| 1015 | void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} | |||
| 1016 | void OnTestIterationEnd(const UnitTest& /*unit_test*/, | |||
| 1017 | int /*iteration*/) override {} | |||
| 1018 | void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} | |||
| 1019 | }; | |||
| 1020 | ||||
| 1021 | // TestEventListeners lets users add listeners to track events in Google Test. | |||
| 1022 | class GTEST_API___attribute__((visibility("default"))) TestEventListeners { | |||
| 1023 | public: | |||
| 1024 | TestEventListeners(); | |||
| 1025 | ~TestEventListeners(); | |||
| 1026 | ||||
| 1027 | // Appends an event listener to the end of the list. Google Test assumes | |||
| 1028 | // the ownership of the listener (i.e. it will delete the listener when | |||
| 1029 | // the test program finishes). | |||
| 1030 | void Append(TestEventListener* listener); | |||
| 1031 | ||||
| 1032 | // Removes the given event listener from the list and returns it. It then | |||
| 1033 | // becomes the caller's responsibility to delete the listener. Returns | |||
| 1034 | // NULL if the listener is not found in the list. | |||
| 1035 | TestEventListener* Release(TestEventListener* listener); | |||
| 1036 | ||||
| 1037 | // Returns the standard listener responsible for the default console | |||
| 1038 | // output. Can be removed from the listeners list to shut down default | |||
| 1039 | // console output. Note that removing this object from the listener list | |||
| 1040 | // with Release transfers its ownership to the caller and makes this | |||
| 1041 | // function return NULL the next time. | |||
| 1042 | TestEventListener* default_result_printer() const { | |||
| 1043 | return default_result_printer_; | |||
| 1044 | } | |||
| 1045 | ||||
| 1046 | // Returns the standard listener responsible for the default XML output | |||
| 1047 | // controlled by the --gtest_output=xml flag. Can be removed from the | |||
| 1048 | // listeners list by users who want to shut down the default XML output | |||
| 1049 | // controlled by this flag and substitute it with custom one. Note that | |||
| 1050 | // removing this object from the listener list with Release transfers its | |||
| 1051 | // ownership to the caller and makes this function return NULL the next | |||
| 1052 | // time. | |||
| 1053 | TestEventListener* default_xml_generator() const { | |||
| 1054 | return default_xml_generator_; | |||
| 1055 | } | |||
| 1056 | ||||
| 1057 | // Controls whether events will be forwarded by the repeater to the | |||
| 1058 | // listeners in the list. | |||
| 1059 | void SuppressEventForwarding(bool); | |||
| 1060 | ||||
| 1061 | private: | |||
| 1062 | friend class TestSuite; | |||
| 1063 | friend class TestInfo; | |||
| 1064 | friend class internal::DefaultGlobalTestPartResultReporter; | |||
| 1065 | friend class internal::NoExecDeathTest; | |||
| 1066 | friend class internal::TestEventListenersAccessor; | |||
| 1067 | friend class internal::UnitTestImpl; | |||
| 1068 | ||||
| 1069 | // Returns repeater that broadcasts the TestEventListener events to all | |||
| 1070 | // subscribers. | |||
| 1071 | TestEventListener* repeater(); | |||
| 1072 | ||||
| 1073 | // Sets the default_result_printer attribute to the provided listener. | |||
| 1074 | // The listener is also added to the listener list and previous | |||
| 1075 | // default_result_printer is removed from it and deleted. The listener can | |||
| 1076 | // also be NULL in which case it will not be added to the list. Does | |||
| 1077 | // nothing if the previous and the current listener objects are the same. | |||
| 1078 | void SetDefaultResultPrinter(TestEventListener* listener); | |||
| 1079 | ||||
| 1080 | // Sets the default_xml_generator attribute to the provided listener. The | |||
| 1081 | // listener is also added to the listener list and previous | |||
| 1082 | // default_xml_generator is removed from it and deleted. The listener can | |||
| 1083 | // also be NULL in which case it will not be added to the list. Does | |||
| 1084 | // nothing if the previous and the current listener objects are the same. | |||
| 1085 | void SetDefaultXmlGenerator(TestEventListener* listener); | |||
| 1086 | ||||
| 1087 | // Controls whether events will be forwarded by the repeater to the | |||
| 1088 | // listeners in the list. | |||
| 1089 | bool EventForwardingEnabled() const; | |||
| 1090 | ||||
| 1091 | // The actual list of listeners. | |||
| 1092 | internal::TestEventRepeater* repeater_; | |||
| 1093 | // Listener responsible for the standard result output. | |||
| 1094 | TestEventListener* default_result_printer_; | |||
| 1095 | // Listener responsible for the creation of the XML output file. | |||
| 1096 | TestEventListener* default_xml_generator_; | |||
| 1097 | ||||
| 1098 | // We disallow copying TestEventListeners. | |||
| 1099 | TestEventListeners(const TestEventListeners&) = delete; | |||
| 1100 | TestEventListeners& operator=(const TestEventListeners&) = delete; | |||
| 1101 | }; | |||
| 1102 | ||||
| 1103 | // A UnitTest consists of a vector of TestSuites. | |||
| 1104 | // | |||
| 1105 | // This is a singleton class. The only instance of UnitTest is | |||
| 1106 | // created when UnitTest::GetInstance() is first called. This | |||
| 1107 | // instance is never deleted. | |||
| 1108 | // | |||
| 1109 | // UnitTest is not copyable. | |||
| 1110 | // | |||
| 1111 | // This class is thread-safe as long as the methods are called | |||
| 1112 | // according to their specification. | |||
| 1113 | class GTEST_API___attribute__((visibility("default"))) UnitTest { | |||
| 1114 | public: | |||
| 1115 | // Gets the singleton UnitTest object. The first time this method | |||
| 1116 | // is called, a UnitTest object is constructed and returned. | |||
| 1117 | // Consecutive calls will return the same object. | |||
| 1118 | static UnitTest* GetInstance(); | |||
| 1119 | ||||
| 1120 | // Runs all tests in this UnitTest object and prints the result. | |||
| 1121 | // Returns 0 if successful, or 1 otherwise. | |||
| 1122 | // | |||
| 1123 | // This method can only be called from the main thread. | |||
| 1124 | // | |||
| 1125 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1126 | int Run() GTEST_MUST_USE_RESULT___attribute__((warn_unused_result)); | |||
| 1127 | ||||
| 1128 | // Returns the working directory when the first TEST() or TEST_F() | |||
| 1129 | // was executed. The UnitTest object owns the string. | |||
| 1130 | const char* original_working_dir() const; | |||
| 1131 | ||||
| 1132 | // Returns the TestSuite object for the test that's currently running, | |||
| 1133 | // or NULL if no test is running. | |||
| 1134 | const TestSuite* current_test_suite() const GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1135 | ||||
| 1136 | // Legacy API is still available but deprecated | |||
| 1137 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1138 | const TestCase* current_test_case() const GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1139 | #endif | |||
| 1140 | ||||
| 1141 | // Returns the TestInfo object for the test that's currently running, | |||
| 1142 | // or NULL if no test is running. | |||
| 1143 | const TestInfo* current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1144 | ||||
| 1145 | // Returns the random seed used at the start of the current test run. | |||
| 1146 | int random_seed() const; | |||
| 1147 | ||||
| 1148 | // Returns the ParameterizedTestSuiteRegistry object used to keep track of | |||
| 1149 | // value-parameterized tests and instantiate and register them. | |||
| 1150 | // | |||
| 1151 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1152 | internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() | |||
| 1153 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1154 | ||||
| 1155 | // Gets the number of successful test suites. | |||
| 1156 | int successful_test_suite_count() const; | |||
| 1157 | ||||
| 1158 | // Gets the number of failed test suites. | |||
| 1159 | int failed_test_suite_count() const; | |||
| 1160 | ||||
| 1161 | // Gets the number of all test suites. | |||
| 1162 | int total_test_suite_count() const; | |||
| 1163 | ||||
| 1164 | // Gets the number of all test suites that contain at least one test | |||
| 1165 | // that should run. | |||
| 1166 | int test_suite_to_run_count() const; | |||
| 1167 | ||||
| 1168 | // Legacy API is deprecated but still available | |||
| 1169 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1170 | int successful_test_case_count() const; | |||
| 1171 | int failed_test_case_count() const; | |||
| 1172 | int total_test_case_count() const; | |||
| 1173 | int test_case_to_run_count() const; | |||
| 1174 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1175 | ||||
| 1176 | // Gets the number of successful tests. | |||
| 1177 | int successful_test_count() const; | |||
| 1178 | ||||
| 1179 | // Gets the number of skipped tests. | |||
| 1180 | int skipped_test_count() const; | |||
| 1181 | ||||
| 1182 | // Gets the number of failed tests. | |||
| 1183 | int failed_test_count() const; | |||
| 1184 | ||||
| 1185 | // Gets the number of disabled tests that will be reported in the XML report. | |||
| 1186 | int reportable_disabled_test_count() const; | |||
| 1187 | ||||
| 1188 | // Gets the number of disabled tests. | |||
| 1189 | int disabled_test_count() const; | |||
| 1190 | ||||
| 1191 | // Gets the number of tests to be printed in the XML report. | |||
| 1192 | int reportable_test_count() const; | |||
| 1193 | ||||
| 1194 | // Gets the number of all tests. | |||
| 1195 | int total_test_count() const; | |||
| 1196 | ||||
| 1197 | // Gets the number of tests that should run. | |||
| 1198 | int test_to_run_count() const; | |||
| 1199 | ||||
| 1200 | // Gets the time of the test program start, in ms from the start of the | |||
| 1201 | // UNIX epoch. | |||
| 1202 | TimeInMillis start_timestamp() const; | |||
| 1203 | ||||
| 1204 | // Gets the elapsed time, in milliseconds. | |||
| 1205 | TimeInMillis elapsed_time() const; | |||
| 1206 | ||||
| 1207 | // Returns true if and only if the unit test passed (i.e. all test suites | |||
| 1208 | // passed). | |||
| 1209 | bool Passed() const; | |||
| 1210 | ||||
| 1211 | // Returns true if and only if the unit test failed (i.e. some test suite | |||
| 1212 | // failed or something outside of all tests failed). | |||
| 1213 | bool Failed() const; | |||
| 1214 | ||||
| 1215 | // Gets the i-th test suite among all the test suites. i can range from 0 to | |||
| 1216 | // total_test_suite_count() - 1. If i is not in that range, returns NULL. | |||
| 1217 | const TestSuite* GetTestSuite(int i) const; | |||
| 1218 | ||||
| 1219 | // Legacy API is deprecated but still available | |||
| 1220 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1221 | const TestCase* GetTestCase(int i) const; | |||
| 1222 | #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | |||
| 1223 | ||||
| 1224 | // Returns the TestResult containing information on test failures and | |||
| 1225 | // properties logged outside of individual test suites. | |||
| 1226 | const TestResult& ad_hoc_test_result() const; | |||
| 1227 | ||||
| 1228 | // Returns the list of event listeners that can be used to track events | |||
| 1229 | // inside Google Test. | |||
| 1230 | TestEventListeners& listeners(); | |||
| 1231 | ||||
| 1232 | private: | |||
| 1233 | // Registers and returns a global test environment. When a test | |||
| 1234 | // program is run, all global test environments will be set-up in | |||
| 1235 | // the order they were registered. After all tests in the program | |||
| 1236 | // have finished, all global test environments will be torn-down in | |||
| 1237 | // the *reverse* order they were registered. | |||
| 1238 | // | |||
| 1239 | // The UnitTest object takes ownership of the given environment. | |||
| 1240 | // | |||
| 1241 | // This method can only be called from the main thread. | |||
| 1242 | Environment* AddEnvironment(Environment* env); | |||
| 1243 | ||||
| 1244 | // Adds a TestPartResult to the current TestResult object. All | |||
| 1245 | // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) | |||
| 1246 | // eventually call this to report their results. The user code | |||
| 1247 | // should use the assertion macros instead of calling this directly. | |||
| 1248 | void AddTestPartResult(TestPartResult::Type result_type, | |||
| 1249 | const char* file_name, int line_number, | |||
| 1250 | const std::string& message, | |||
| 1251 | const std::string& os_stack_trace) | |||
| 1252 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1253 | ||||
| 1254 | // Adds a TestProperty to the current TestResult object when invoked from | |||
| 1255 | // inside a test, to current TestSuite's ad_hoc_test_result_ when invoked | |||
| 1256 | // from SetUpTestSuite or TearDownTestSuite, or to the global property set | |||
| 1257 | // when invoked elsewhere. If the result already contains a property with | |||
| 1258 | // the same key, the value will be updated. | |||
| 1259 | void RecordProperty(const std::string& key, const std::string& value); | |||
| 1260 | ||||
| 1261 | // Gets the i-th test suite among all the test suites. i can range from 0 to | |||
| 1262 | // total_test_suite_count() - 1. If i is not in that range, returns NULL. | |||
| 1263 | TestSuite* GetMutableTestSuite(int i); | |||
| 1264 | ||||
| 1265 | // Invokes OsStackTrackGetterInterface::UponLeavingGTest. UponLeavingGTest() | |||
| 1266 | // should be called immediately before Google Test calls user code. It saves | |||
| 1267 | // some information about the current stack that CurrentStackTrace() will use | |||
| 1268 | // to find and hide Google Test stack frames. | |||
| 1269 | void UponLeavingGTest(); | |||
| 1270 | ||||
| 1271 | // Sets the TestSuite object for the test that's currently running. | |||
| 1272 | void set_current_test_suite(TestSuite* a_current_test_suite) | |||
| 1273 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1274 | ||||
| 1275 | // Sets the TestInfo object for the test that's currently running. | |||
| 1276 | void set_current_test_info(TestInfo* a_current_test_info) | |||
| 1277 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1278 | ||||
| 1279 | // Accessors for the implementation object. | |||
| 1280 | internal::UnitTestImpl* impl() { return impl_; } | |||
| 1281 | const internal::UnitTestImpl* impl() const { return impl_; } | |||
| 1282 | ||||
| 1283 | // These classes and functions are friends as they need to access private | |||
| 1284 | // members of UnitTest. | |||
| 1285 | friend class ScopedTrace; | |||
| 1286 | friend class Test; | |||
| 1287 | friend class TestInfo; | |||
| 1288 | friend class TestSuite; | |||
| 1289 | friend class internal::AssertHelper; | |||
| 1290 | friend class internal::StreamingListenerTest; | |||
| 1291 | friend class internal::UnitTestRecordPropertyTestHelper; | |||
| 1292 | friend Environment* AddGlobalTestEnvironment(Environment* env); | |||
| 1293 | friend std::set<std::string>* internal::GetIgnoredParameterizedTestSuites(); | |||
| 1294 | friend internal::UnitTestImpl* internal::GetUnitTestImpl(); | |||
| 1295 | friend void internal::ReportFailureInUnknownLocation( | |||
| 1296 | TestPartResult::Type result_type, const std::string& message); | |||
| 1297 | ||||
| 1298 | // Creates an empty UnitTest. | |||
| 1299 | UnitTest(); | |||
| 1300 | ||||
| 1301 | // D'tor | |||
| 1302 | virtual ~UnitTest(); | |||
| 1303 | ||||
| 1304 | // Pushes a trace defined by SCOPED_TRACE() on to the per-thread | |||
| 1305 | // Google Test trace stack. | |||
| 1306 | void PushGTestTrace(const internal::TraceInfo& trace) | |||
| 1307 | GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1308 | ||||
| 1309 | // Pops a trace from the per-thread Google Test trace stack. | |||
| 1310 | void PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_); | |||
| 1311 | ||||
| 1312 | // Protects mutable state in *impl_. This is mutable as some const | |||
| 1313 | // methods need to lock it too. | |||
| 1314 | mutable internal::Mutex mutex_; | |||
| 1315 | ||||
| 1316 | // Opaque implementation object. This field is never changed once | |||
| 1317 | // the object is constructed. We don't mark it as const here, as | |||
| 1318 | // doing so will cause a warning in the constructor of UnitTest. | |||
| 1319 | // Mutable state in *impl_ is protected by mutex_. | |||
| 1320 | internal::UnitTestImpl* impl_; | |||
| 1321 | ||||
| 1322 | // We disallow copying UnitTest. | |||
| 1323 | UnitTest(const UnitTest&) = delete; | |||
| 1324 | UnitTest& operator=(const UnitTest&) = delete; | |||
| 1325 | }; | |||
| 1326 | ||||
| 1327 | // A convenient wrapper for adding an environment for the test | |||
| 1328 | // program. | |||
| 1329 | // | |||
| 1330 | // You should call this before RUN_ALL_TESTS() is called, probably in | |||
| 1331 | // main(). If you use gtest_main, you need to call this before main() | |||
| 1332 | // starts for it to take effect. For example, you can define a global | |||
| 1333 | // variable like this: | |||
| 1334 | // | |||
| 1335 | // testing::Environment* const foo_env = | |||
| 1336 | // testing::AddGlobalTestEnvironment(new FooEnvironment); | |||
| 1337 | // | |||
| 1338 | // However, we strongly recommend you to write your own main() and | |||
| 1339 | // call AddGlobalTestEnvironment() there, as relying on initialization | |||
| 1340 | // of global variables makes the code harder to read and may cause | |||
| 1341 | // problems when you register multiple environments from different | |||
| 1342 | // translation units and the environments have dependencies among them | |||
| 1343 | // (remember that the compiler doesn't guarantee the order in which | |||
| 1344 | // global variables from different translation units are initialized). | |||
| 1345 | inline Environment* AddGlobalTestEnvironment(Environment* env) { | |||
| 1346 | return UnitTest::GetInstance()->AddEnvironment(env); | |||
| 1347 | } | |||
| 1348 | ||||
| 1349 | // Initializes Google Test. This must be called before calling | |||
| 1350 | // RUN_ALL_TESTS(). In particular, it parses a command line for the | |||
| 1351 | // flags that Google Test recognizes. Whenever a Google Test flag is | |||
| 1352 | // seen, it is removed from argv, and *argc is decremented. | |||
| 1353 | // | |||
| 1354 | // No value is returned. Instead, the Google Test flag variables are | |||
| 1355 | // updated. | |||
| 1356 | // | |||
| 1357 | // Calling the function for the second time has no user-visible effect. | |||
| 1358 | GTEST_API___attribute__((visibility("default"))) void InitGoogleTest(int* argc, char** argv); | |||
| 1359 | ||||
| 1360 | // This overloaded version can be used in Windows programs compiled in | |||
| 1361 | // UNICODE mode. | |||
| 1362 | GTEST_API___attribute__((visibility("default"))) void InitGoogleTest(int* argc, wchar_t** argv); | |||
| 1363 | ||||
| 1364 | // This overloaded version can be used on Arduino/embedded platforms where | |||
| 1365 | // there is no argc/argv. | |||
| 1366 | GTEST_API___attribute__((visibility("default"))) void InitGoogleTest(); | |||
| 1367 | ||||
| 1368 | namespace internal { | |||
| 1369 | ||||
| 1370 | // Separate the error generating code from the code path to reduce the stack | |||
| 1371 | // frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers | |||
| 1372 | // when calling EXPECT_* in a tight loop. | |||
| 1373 | template <typename T1, typename T2> | |||
| 1374 | AssertionResult CmpHelperEQFailure(const char* lhs_expression, | |||
| 1375 | const char* rhs_expression, const T1& lhs, | |||
| 1376 | const T2& rhs) { | |||
| 1377 | return EqFailure(lhs_expression, rhs_expression, | |||
| 1378 | FormatForComparisonFailureMessage(lhs, rhs), | |||
| 1379 | FormatForComparisonFailureMessage(rhs, lhs), false); | |||
| 1380 | } | |||
| 1381 | ||||
| 1382 | // This block of code defines operator==/!= | |||
| 1383 | // to block lexical scope lookup. | |||
| 1384 | // It prevents using invalid operator==/!= defined at namespace scope. | |||
| 1385 | struct faketype {}; | |||
| 1386 | inline bool operator==(faketype, faketype) { return true; } | |||
| 1387 | inline bool operator!=(faketype, faketype) { return false; } | |||
| 1388 | ||||
| 1389 | // The helper function for {ASSERT|EXPECT}_EQ. | |||
| 1390 | template <typename T1, typename T2> | |||
| 1391 | AssertionResult CmpHelperEQ(const char* lhs_expression, | |||
| 1392 | const char* rhs_expression, const T1& lhs, | |||
| 1393 | const T2& rhs) { | |||
| 1394 | if (lhs == rhs) { | |||
| ||||
| 1395 | return AssertionSuccess(); | |||
| 1396 | } | |||
| 1397 | ||||
| 1398 | return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs); | |||
| 1399 | } | |||
| 1400 | ||||
| 1401 | class EqHelper { | |||
| 1402 | public: | |||
| 1403 | // This templatized version is for the general case. | |||
| 1404 | template < | |||
| 1405 | typename T1, typename T2, | |||
| 1406 | // Disable this overload for cases where one argument is a pointer | |||
| 1407 | // and the other is the null pointer constant. | |||
| 1408 | typename std::enable_if<!std::is_integral<T1>::value || | |||
| 1409 | !std::is_pointer<T2>::value>::type* = nullptr> | |||
| 1410 | static AssertionResult Compare(const char* lhs_expression, | |||
| 1411 | const char* rhs_expression, const T1& lhs, | |||
| 1412 | const T2& rhs) { | |||
| 1413 | return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); | |||
| 1414 | } | |||
| 1415 | ||||
| 1416 | // With this overloaded version, we allow anonymous enums to be used | |||
| 1417 | // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous | |||
| 1418 | // enums can be implicitly cast to BiggestInt. | |||
| 1419 | // | |||
| 1420 | // Even though its body looks the same as the above version, we | |||
| 1421 | // cannot merge the two, as it will make anonymous enums unhappy. | |||
| 1422 | static AssertionResult Compare(const char* lhs_expression, | |||
| 1423 | const char* rhs_expression, BiggestInt lhs, | |||
| 1424 | BiggestInt rhs) { | |||
| 1425 | return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); | |||
| 1426 | } | |||
| 1427 | ||||
| 1428 | template <typename T> | |||
| 1429 | static AssertionResult Compare( | |||
| 1430 | const char* lhs_expression, const char* rhs_expression, | |||
| 1431 | // Handle cases where '0' is used as a null pointer literal. | |||
| 1432 | std::nullptr_t /* lhs */, T* rhs) { | |||
| 1433 | // We already know that 'lhs' is a null pointer. | |||
| 1434 | return CmpHelperEQ(lhs_expression, rhs_expression, static_cast<T*>(nullptr), | |||
| 1435 | rhs); | |||
| 1436 | } | |||
| 1437 | }; | |||
| 1438 | ||||
| 1439 | // Separate the error generating code from the code path to reduce the stack | |||
| 1440 | // frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers | |||
| 1441 | // when calling EXPECT_OP in a tight loop. | |||
| 1442 | template <typename T1, typename T2> | |||
| 1443 | AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, | |||
| 1444 | const T1& val1, const T2& val2, | |||
| 1445 | const char* op) { | |||
| 1446 | return AssertionFailure() | |||
| 1447 | << "Expected: (" << expr1 << ") " << op << " (" << expr2 | |||
| 1448 | << "), actual: " << FormatForComparisonFailureMessage(val1, val2) | |||
| 1449 | << " vs " << FormatForComparisonFailureMessage(val2, val1); | |||
| 1450 | } | |||
| 1451 | ||||
| 1452 | // A macro for implementing the helper functions needed to implement | |||
| 1453 | // ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste | |||
| 1454 | // of similar code. | |||
| 1455 | // | |||
| 1456 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1457 | ||||
| 1458 | #define GTEST_IMPL_CMP_HELPER_(op_name, op) \ | |||
| 1459 | template <typename T1, typename T2> \ | |||
| 1460 | AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ | |||
| 1461 | const T1& val1, const T2& val2) { \ | |||
| 1462 | if (val1 op val2) { \ | |||
| 1463 | return AssertionSuccess(); \ | |||
| 1464 | } else { \ | |||
| 1465 | return CmpHelperOpFailure(expr1, expr2, val1, val2, #op); \ | |||
| 1466 | } \ | |||
| 1467 | } | |||
| 1468 | ||||
| 1469 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1470 | ||||
| 1471 | // Implements the helper function for {ASSERT|EXPECT}_NE | |||
| 1472 | GTEST_IMPL_CMP_HELPER_(NE, !=) | |||
| 1473 | // Implements the helper function for {ASSERT|EXPECT}_LE | |||
| 1474 | GTEST_IMPL_CMP_HELPER_(LE, <=) | |||
| 1475 | // Implements the helper function for {ASSERT|EXPECT}_LT | |||
| 1476 | GTEST_IMPL_CMP_HELPER_(LT, <) | |||
| 1477 | // Implements the helper function for {ASSERT|EXPECT}_GE | |||
| 1478 | GTEST_IMPL_CMP_HELPER_(GE, >=) | |||
| 1479 | // Implements the helper function for {ASSERT|EXPECT}_GT | |||
| 1480 | GTEST_IMPL_CMP_HELPER_(GT, >) | |||
| 1481 | ||||
| 1482 | #undef GTEST_IMPL_CMP_HELPER_ | |||
| 1483 | ||||
| 1484 | // The helper function for {ASSERT|EXPECT}_STREQ. | |||
| 1485 | // | |||
| 1486 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1487 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTREQ(const char* s1_expression, | |||
| 1488 | const char* s2_expression, | |||
| 1489 | const char* s1, const char* s2); | |||
| 1490 | ||||
| 1491 | // The helper function for {ASSERT|EXPECT}_STRCASEEQ. | |||
| 1492 | // | |||
| 1493 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1494 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression, | |||
| 1495 | const char* s2_expression, | |||
| 1496 | const char* s1, const char* s2); | |||
| 1497 | ||||
| 1498 | // The helper function for {ASSERT|EXPECT}_STRNE. | |||
| 1499 | // | |||
| 1500 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1501 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRNE(const char* s1_expression, | |||
| 1502 | const char* s2_expression, | |||
| 1503 | const char* s1, const char* s2); | |||
| 1504 | ||||
| 1505 | // The helper function for {ASSERT|EXPECT}_STRCASENE. | |||
| 1506 | // | |||
| 1507 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1508 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRCASENE(const char* s1_expression, | |||
| 1509 | const char* s2_expression, | |||
| 1510 | const char* s1, const char* s2); | |||
| 1511 | ||||
| 1512 | // Helper function for *_STREQ on wide strings. | |||
| 1513 | // | |||
| 1514 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1515 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTREQ(const char* s1_expression, | |||
| 1516 | const char* s2_expression, | |||
| 1517 | const wchar_t* s1, const wchar_t* s2); | |||
| 1518 | ||||
| 1519 | // Helper function for *_STRNE on wide strings. | |||
| 1520 | // | |||
| 1521 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1522 | GTEST_API___attribute__((visibility("default"))) AssertionResult CmpHelperSTRNE(const char* s1_expression, | |||
| 1523 | const char* s2_expression, | |||
| 1524 | const wchar_t* s1, const wchar_t* s2); | |||
| 1525 | ||||
| 1526 | } // namespace internal | |||
| 1527 | ||||
| 1528 | // IsSubstring() and IsNotSubstring() are intended to be used as the | |||
| 1529 | // first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by | |||
| 1530 | // themselves. They check whether needle is a substring of haystack | |||
| 1531 | // (NULL is considered a substring of itself only), and return an | |||
| 1532 | // appropriate error message when they fail. | |||
| 1533 | // | |||
| 1534 | // The {needle,haystack}_expr arguments are the stringified | |||
| 1535 | // expressions that generated the two real arguments. | |||
| 1536 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
| 1537 | const char* haystack_expr, | |||
| 1538 | const char* needle, | |||
| 1539 | const char* haystack); | |||
| 1540 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
| 1541 | const char* haystack_expr, | |||
| 1542 | const wchar_t* needle, | |||
| 1543 | const wchar_t* haystack); | |||
| 1544 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
| 1545 | const char* haystack_expr, | |||
| 1546 | const char* needle, | |||
| 1547 | const char* haystack); | |||
| 1548 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
| 1549 | const char* haystack_expr, | |||
| 1550 | const wchar_t* needle, | |||
| 1551 | const wchar_t* haystack); | |||
| 1552 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
| 1553 | const char* haystack_expr, | |||
| 1554 | const ::std::string& needle, | |||
| 1555 | const ::std::string& haystack); | |||
| 1556 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
| 1557 | const char* haystack_expr, | |||
| 1558 | const ::std::string& needle, | |||
| 1559 | const ::std::string& haystack); | |||
| 1560 | ||||
| 1561 | #if GTEST_HAS_STD_WSTRING1 | |||
| 1562 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsSubstring(const char* needle_expr, | |||
| 1563 | const char* haystack_expr, | |||
| 1564 | const ::std::wstring& needle, | |||
| 1565 | const ::std::wstring& haystack); | |||
| 1566 | GTEST_API___attribute__((visibility("default"))) AssertionResult IsNotSubstring(const char* needle_expr, | |||
| 1567 | const char* haystack_expr, | |||
| 1568 | const ::std::wstring& needle, | |||
| 1569 | const ::std::wstring& haystack); | |||
| 1570 | #endif // GTEST_HAS_STD_WSTRING | |||
| 1571 | ||||
| 1572 | namespace internal { | |||
| 1573 | ||||
| 1574 | // Helper template function for comparing floating-points. | |||
| 1575 | // | |||
| 1576 | // Template parameter: | |||
| 1577 | // | |||
| 1578 | // RawType: the raw floating-point type (either float or double) | |||
| 1579 | // | |||
| 1580 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1581 | template <typename RawType> | |||
| 1582 | AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression, | |||
| 1583 | const char* rhs_expression, | |||
| 1584 | RawType lhs_value, RawType rhs_value) { | |||
| 1585 | const FloatingPoint<RawType> lhs(lhs_value), rhs(rhs_value); | |||
| 1586 | ||||
| 1587 | if (lhs.AlmostEquals(rhs)) { | |||
| 1588 | return AssertionSuccess(); | |||
| 1589 | } | |||
| 1590 | ||||
| 1591 | ::std::stringstream lhs_ss; | |||
| 1592 | lhs_ss.precision(std::numeric_limits<RawType>::digits10 + 2); | |||
| 1593 | lhs_ss << lhs_value; | |||
| 1594 | ||||
| 1595 | ::std::stringstream rhs_ss; | |||
| 1596 | rhs_ss.precision(std::numeric_limits<RawType>::digits10 + 2); | |||
| 1597 | rhs_ss << rhs_value; | |||
| 1598 | ||||
| 1599 | return EqFailure(lhs_expression, rhs_expression, | |||
| 1600 | StringStreamToString(&lhs_ss), StringStreamToString(&rhs_ss), | |||
| 1601 | false); | |||
| 1602 | } | |||
| 1603 | ||||
| 1604 | // Helper function for implementing ASSERT_NEAR. | |||
| 1605 | // | |||
| 1606 | // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | |||
| 1607 | GTEST_API___attribute__((visibility("default"))) AssertionResult DoubleNearPredFormat(const char* expr1, | |||
| 1608 | const char* expr2, | |||
| 1609 | const char* abs_error_expr, | |||
| 1610 | double val1, double val2, | |||
| 1611 | double abs_error); | |||
| 1612 | ||||
| 1613 | // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | |||
| 1614 | // A class that enables one to stream messages to assertion macros | |||
| 1615 | class GTEST_API___attribute__((visibility("default"))) AssertHelper { | |||
| 1616 | public: | |||
| 1617 | // Constructor. | |||
| 1618 | AssertHelper(TestPartResult::Type type, const char* file, int line, | |||
| 1619 | const char* message); | |||
| 1620 | ~AssertHelper(); | |||
| 1621 | ||||
| 1622 | // Message assignment is a semantic trick to enable assertion | |||
| 1623 | // streaming; see the GTEST_MESSAGE_ macro below. | |||
| 1624 | void operator=(const Message& message) const; | |||
| 1625 | ||||
| 1626 | private: | |||
| 1627 | // We put our data in a struct so that the size of the AssertHelper class can | |||
| 1628 | // be as small as possible. This is important because gcc is incapable of | |||
| 1629 | // re-using stack space even for temporary variables, so every EXPECT_EQ | |||
| 1630 | // reserves stack space for another AssertHelper. | |||
| 1631 | struct AssertHelperData { | |||
| 1632 | AssertHelperData(TestPartResult::Type t, const char* srcfile, int line_num, | |||
| 1633 | const char* msg) | |||
| 1634 | : type(t), file(srcfile), line(line_num), message(msg) {} | |||
| 1635 | ||||
| 1636 | TestPartResult::Type const type; | |||
| 1637 | const char* const file; | |||
| 1638 | int const line; | |||
| 1639 | std::string const message; | |||
| 1640 | ||||
| 1641 | private: | |||
| 1642 | AssertHelperData(const AssertHelperData&) = delete; | |||
| 1643 | AssertHelperData& operator=(const AssertHelperData&) = delete; | |||
| 1644 | }; | |||
| 1645 | ||||
| 1646 | AssertHelperData* const data_; | |||
| 1647 | ||||
| 1648 | AssertHelper(const AssertHelper&) = delete; | |||
| 1649 | AssertHelper& operator=(const AssertHelper&) = delete; | |||
| 1650 | }; | |||
| 1651 | ||||
| 1652 | } // namespace internal | |||
| 1653 | ||||
| 1654 | // The pure interface class that all value-parameterized tests inherit from. | |||
| 1655 | // A value-parameterized class must inherit from both ::testing::Test and | |||
| 1656 | // ::testing::WithParamInterface. In most cases that just means inheriting | |||
| 1657 | // from ::testing::TestWithParam, but more complicated test hierarchies | |||
| 1658 | // may need to inherit from Test and WithParamInterface at different levels. | |||
| 1659 | // | |||
| 1660 | // This interface has support for accessing the test parameter value via | |||
| 1661 | // the GetParam() method. | |||
| 1662 | // | |||
| 1663 | // Use it with one of the parameter generator defining functions, like Range(), | |||
| 1664 | // Values(), ValuesIn(), Bool(), Combine(), and ConvertGenerator<T>(). | |||
| 1665 | // | |||
| 1666 | // class FooTest : public ::testing::TestWithParam<int> { | |||
| 1667 | // protected: | |||
| 1668 | // FooTest() { | |||
| 1669 | // // Can use GetParam() here. | |||
| 1670 | // } | |||
| 1671 | // ~FooTest() override { | |||
| 1672 | // // Can use GetParam() here. | |||
| 1673 | // } | |||
| 1674 | // void SetUp() override { | |||
| 1675 | // // Can use GetParam() here. | |||
| 1676 | // } | |||
| 1677 | // void TearDown override { | |||
| 1678 | // // Can use GetParam() here. | |||
| 1679 | // } | |||
| 1680 | // }; | |||
| 1681 | // TEST_P(FooTest, DoesBar) { | |||
| 1682 | // // Can use GetParam() method here. | |||
| 1683 | // Foo foo; | |||
| 1684 | // ASSERT_TRUE(foo.DoesBar(GetParam())); | |||
| 1685 | // } | |||
| 1686 | // INSTANTIATE_TEST_SUITE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); | |||
| 1687 | ||||
| 1688 | template <typename T> | |||
| 1689 | class WithParamInterface { | |||
| 1690 | public: | |||
| 1691 | typedef T ParamType; | |||
| 1692 | virtual ~WithParamInterface() = default; | |||
| 1693 | ||||
| 1694 | // The current parameter value. Is also available in the test fixture's | |||
| 1695 | // constructor. | |||
| 1696 | static const ParamType& GetParam() { | |||
| 1697 | 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" , 1697) .GetStream() << "Condition " "parameter_ != nullptr" " failed. " | |||
| 1698 | << "GetParam() can only be called inside a value-parameterized test " | |||
| 1699 | << "-- did you intend to write TEST_P instead of TEST_F?"; | |||
| 1700 | return *parameter_; | |||
| 1701 | } | |||
| 1702 | ||||
| 1703 | private: | |||
| 1704 | // Sets parameter value. The caller is responsible for making sure the value | |||
| 1705 | // remains alive and unchanged throughout the current test. | |||
| 1706 | static void SetParam(const ParamType* parameter) { parameter_ = parameter; } | |||
| 1707 | ||||
| 1708 | // Static value used for accessing parameter during a test lifetime. | |||
| 1709 | static const ParamType* parameter_; | |||
| 1710 | ||||
| 1711 | // TestClass must be a subclass of WithParamInterface<T> and Test. | |||
| 1712 | template <class TestClass> | |||
| 1713 | friend class internal::ParameterizedTestFactory; | |||
| 1714 | }; | |||
| 1715 | ||||
| 1716 | template <typename T> | |||
| 1717 | const T* WithParamInterface<T>::parameter_ = nullptr; | |||
| 1718 | ||||
| 1719 | // Most value-parameterized classes can ignore the existence of | |||
| 1720 | // WithParamInterface, and can just inherit from ::testing::TestWithParam. | |||
| 1721 | ||||
| 1722 | template <typename T> | |||
| 1723 | class TestWithParam : public Test, public WithParamInterface<T> {}; | |||
| 1724 | ||||
| 1725 | // Macros for indicating success/failure in test code. | |||
| 1726 | ||||
| 1727 | // Skips test in runtime. | |||
| 1728 | // Skipping test aborts current function. | |||
| 1729 | // Skipped tests are neither successful nor failed. | |||
| 1730 | #define GTEST_SKIP()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kSkip, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1730, "") = ::testing::Message() GTEST_SKIP_("")return ::testing::internal::AssertHelper(::testing::TestPartResult ::kSkip, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1730, "") = ::testing::Message() | |||
| 1731 | ||||
| 1732 | // ADD_FAILURE unconditionally adds a failure to the current test. | |||
| 1733 | // SUCCEED generates a success - it doesn't automatically make the | |||
| 1734 | // current test successful, as a test is only successful when it has | |||
| 1735 | // no failure. | |||
| 1736 | // | |||
| 1737 | // EXPECT_* verifies that a certain condition is satisfied. If not, | |||
| 1738 | // it behaves like ADD_FAILURE. In particular: | |||
| 1739 | // | |||
| 1740 | // EXPECT_TRUE verifies that a Boolean condition is true. | |||
| 1741 | // EXPECT_FALSE verifies that a Boolean condition is false. | |||
| 1742 | // | |||
| 1743 | // FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except | |||
| 1744 | // that they will also abort the current function on failure. People | |||
| 1745 | // usually want the fail-fast behavior of FAIL and ASSERT_*, but those | |||
| 1746 | // writing data-driven tests often find themselves using ADD_FAILURE | |||
| 1747 | // and EXPECT_* more. | |||
| 1748 | ||||
| 1749 | // Generates a nonfatal failure with a generic message. | |||
| 1750 | #define ADD_FAILURE()::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1750, "Failed") = ::testing::Message() GTEST_NONFATAL_FAILURE_("Failed")::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1750, "Failed") = ::testing::Message() | |||
| 1751 | ||||
| 1752 | // Generates a nonfatal failure at the given source file location with | |||
| 1753 | // a generic message. | |||
| 1754 | #define ADD_FAILURE_AT(file, line)::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, file, line, "Failed") = ::testing::Message( ) \ | |||
| 1755 | GTEST_MESSAGE_AT_(file, line, "Failed", \::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, file, line, "Failed") = ::testing::Message( ) | |||
| 1756 | ::testing::TestPartResult::kNonFatalFailure)::testing::internal::AssertHelper(::testing::TestPartResult:: kNonFatalFailure, file, line, "Failed") = ::testing::Message( ) | |||
| 1757 | ||||
| 1758 | // Generates a fatal failure with a generic message. | |||
| 1759 | #define GTEST_FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1759, "Failed") = ::testing::Message() GTEST_FATAL_FAILURE_("Failed")return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1759, "Failed") = ::testing::Message() | |||
| 1760 | ||||
| 1761 | // Like GTEST_FAIL(), but at the given source file location. | |||
| 1762 | #define GTEST_FAIL_AT(file, line)return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, file, line, "Failed") = ::testing::Message() \ | |||
| 1763 | return GTEST_MESSAGE_AT_(file, line, "Failed", \::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, file, line, "Failed") = ::testing::Message() | |||
| 1764 | ::testing::TestPartResult::kFatalFailure)::testing::internal::AssertHelper(::testing::TestPartResult:: kFatalFailure, file, line, "Failed") = ::testing::Message() | |||
| 1765 | ||||
| 1766 | // Define this macro to 1 to omit the definition of FAIL(), which is a | |||
| 1767 | // generic name and clashes with some other libraries. | |||
| 1768 | #if !(defined(GTEST_DONT_DEFINE_FAIL) && GTEST_DONT_DEFINE_FAIL) | |||
| 1769 | #define FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1769, "Failed") = ::testing::Message() GTEST_FAIL()return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1769, "Failed") = ::testing::Message() | |||
| 1770 | #define FAIL_AT(file, line)return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, file, line, "Failed") = ::testing::Message() GTEST_FAIL_AT(file, line)return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, file, line, "Failed") = ::testing::Message() | |||
| 1771 | #endif | |||
| 1772 | ||||
| 1773 | // Generates a success with a generic message. | |||
| 1774 | #define GTEST_SUCCEED()::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1774, "Succeeded") = ::testing::Message() GTEST_SUCCESS_("Succeeded")::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1774, "Succeeded") = ::testing::Message() | |||
| 1775 | ||||
| 1776 | // Define this macro to 1 to omit the definition of SUCCEED(), which | |||
| 1777 | // is a generic name and clashes with some other libraries. | |||
| 1778 | #if !(defined(GTEST_DONT_DEFINE_SUCCEED) && GTEST_DONT_DEFINE_SUCCEED) | |||
| 1779 | #define SUCCEED()::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1779, "Succeeded") = ::testing::Message() GTEST_SUCCEED()::testing::internal::AssertHelper(::testing::TestPartResult:: kSuccess, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1779, "Succeeded") = ::testing::Message() | |||
| 1780 | #endif | |||
| 1781 | ||||
| 1782 | // Macros for testing exceptions. | |||
| 1783 | // | |||
| 1784 | // * {ASSERT|EXPECT}_THROW(statement, expected_exception): | |||
| 1785 | // Tests that the statement throws the expected exception. | |||
| 1786 | // * {ASSERT|EXPECT}_NO_THROW(statement): | |||
| 1787 | // Tests that the statement doesn't throw any exception. | |||
| 1788 | // * {ASSERT|EXPECT}_ANY_THROW(statement): | |||
| 1789 | // Tests that the statement throws an exception. | |||
| 1790 | ||||
| 1791 | #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_1791; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1791 ; } 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_1791 ; } } else gtest_label_testthrow_1791 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1791, gtest_msg.value.c_str()) = ::testing::Message() \ | |||
| 1792 | 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_1792; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1792 ; } 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_1792 ; } } else gtest_label_testthrow_1792 : ::testing::internal:: AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1792, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1793 | #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_1793; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1793 ; } } else gtest_label_testnothrow_1793 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1793, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () \ | |||
| 1794 | 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_1794; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1794 ; } } else gtest_label_testnothrow_1794 : ::testing::internal ::AssertHelper(::testing::TestPartResult::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1794, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
| 1795 | #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_1795; } } else gtest_label_testanythrow_1795 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1795, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() \ | |||
| 1796 | 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_1796; } } else gtest_label_testanythrow_1796 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1796, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() | |||
| 1797 | #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_1797; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1797 ; } 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_1797 ; } } else gtest_label_testthrow_1797 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1797, gtest_msg.value.c_str()) = ::testing::Message() \ | |||
| 1798 | 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_1798; } catch (...) { gtest_msg.value = "Expected: " "statement" " throws an exception of type " "expected_exception" ".\n Actual: it throws a different type."; goto gtest_label_testthrow_1798 ; } 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_1798 ; } } else gtest_label_testthrow_1798 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1798, gtest_msg.value.c_str()) = ::testing::Message() | |||
| 1799 | #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_1799; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1799 ; } } else gtest_label_testnothrow_1799 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1799, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () \ | |||
| 1800 | 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_1800; } catch ( ...) { gtest_msg.value = "it throws."; goto gtest_label_testnothrow_1800 ; } } else gtest_label_testnothrow_1800 : return ::testing::internal ::AssertHelper(::testing::TestPartResult::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1800, ("Expected: " "statement" " doesn't throw an exception.\n" " Actual: " + gtest_msg.value) .c_str()) = ::testing::Message () | |||
| 1801 | #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_1801; } } else gtest_label_testanythrow_1801 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1801, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() \ | |||
| 1802 | 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_1802; } } else gtest_label_testanythrow_1802 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 1802, "Expected: " "statement" " throws an exception.\n" " Actual: it doesn't." ) = ::testing::Message() | |||
| 1803 | ||||
| 1804 | // Boolean assertions. Condition can be either a Boolean expression or an | |||
| 1805 | // AssertionResult. For more information on how to use AssertionResult with | |||
| 1806 | // these macros see comments on that class. | |||
| 1807 | #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", 1807 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () \ | |||
| 1808 | 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", 1809 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "false", "true") .c_str()) = ::testing::Message () | |||
| 1809 | 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", 1809 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "false", "true") .c_str()) = ::testing::Message () | |||
| 1810 | #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" , 1810, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () \ | |||
| 1811 | 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" , 1812, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
| 1812 | 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" , 1812, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
| 1813 | #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" , 1813, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () \ | |||
| 1814 | 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" , 1814, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "false", "true") .c_str()) = ::testing::Message () | |||
| 1815 | #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" , 1815, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () \ | |||
| 1816 | 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" , 1817, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
| 1817 | 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" , 1817, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , #condition, "true", "false") .c_str()) = ::testing::Message () | |||
| 1818 | ||||
| 1819 | // Define these macros to 1 to omit the definition of the corresponding | |||
| 1820 | // EXPECT or ASSERT, which clashes with some users' own code. | |||
| 1821 | ||||
| 1822 | #if !(defined(GTEST_DONT_DEFINE_EXPECT_TRUE) && GTEST_DONT_DEFINE_EXPECT_TRUE) | |||
| 1823 | #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", 1823 , ::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", 1823 , ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () | |||
| 1824 | #endif | |||
| 1825 | ||||
| 1826 | #if !(defined(GTEST_DONT_DEFINE_EXPECT_FALSE) && GTEST_DONT_DEFINE_EXPECT_FALSE) | |||
| 1827 | #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" , 1827, ::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" , 1827, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () | |||
| 1828 | #endif | |||
| 1829 | ||||
| 1830 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_TRUE) && GTEST_DONT_DEFINE_ASSERT_TRUE) | |||
| 1831 | #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" , 1831, ::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" , 1831, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "false", "true") .c_str()) = ::testing::Message () | |||
| 1832 | #endif | |||
| 1833 | ||||
| 1834 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_FALSE) && GTEST_DONT_DEFINE_ASSERT_FALSE) | |||
| 1835 | #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" , 1835, ::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" , 1835, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "condition", "true", "false") .c_str()) = ::testing::Message () | |||
| 1836 | #endif | |||
| 1837 | ||||
| 1838 | // Macros for testing equalities and inequalities. | |||
| 1839 | // | |||
| 1840 | // * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2 | |||
| 1841 | // * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 | |||
| 1842 | // * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 | |||
| 1843 | // * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 | |||
| 1844 | // * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 | |||
| 1845 | // * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 | |||
| 1846 | // | |||
| 1847 | // When they are not, Google Test prints both the tested expressions and | |||
| 1848 | // their actual values. The values must be compatible built-in types, | |||
| 1849 | // or you will get a compiler error. By "compatible" we mean that the | |||
| 1850 | // values can be compared by the respective operator. | |||
| 1851 | // | |||
| 1852 | // Note: | |||
| 1853 | // | |||
| 1854 | // 1. It is possible to make a user-defined type work with | |||
| 1855 | // {ASSERT|EXPECT}_??(), but that requires overloading the | |||
| 1856 | // comparison operators and is thus discouraged by the Google C++ | |||
| 1857 | // Usage Guide. Therefore, you are advised to use the | |||
| 1858 | // {ASSERT|EXPECT}_TRUE() macro to assert that two objects are | |||
| 1859 | // equal. | |||
| 1860 | // | |||
| 1861 | // 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on | |||
| 1862 | // pointers (in particular, C strings). Therefore, if you use it | |||
| 1863 | // with two C strings, you are testing how their locations in memory | |||
| 1864 | // are related, not how their content is related. To compare two C | |||
| 1865 | // strings by content, use {ASSERT|EXPECT}_STR*(). | |||
| 1866 | // | |||
| 1867 | // 3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to | |||
| 1868 | // {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you | |||
| 1869 | // what the actual value is when it fails, and similarly for the | |||
| 1870 | // other comparisons. | |||
| 1871 | // | |||
| 1872 | // 4. Do not depend on the order in which {ASSERT|EXPECT}_??() | |||
| 1873 | // evaluate their arguments, which is undefined. | |||
| 1874 | // | |||
| 1875 | // 5. These macros evaluate their arguments exactly once. | |||
| 1876 | // | |||
| 1877 | // Examples: | |||
| 1878 | // | |||
| 1879 | // EXPECT_NE(Foo(), 5); | |||
| 1880 | // EXPECT_EQ(a_pointer, NULL); | |||
| 1881 | // ASSERT_LT(i, array_size); | |||
| 1882 | // ASSERT_GT(records.size(), 0) << "There is no record left."; | |||
| 1883 | ||||
| 1884 | #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" , 1884, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1885 | 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" , 1885, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1886 | #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" , 1886, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1887 | 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" , 1887, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1888 | #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" , 1888, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1889 | 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" , 1889, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1890 | #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" , 1890, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1891 | 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" , 1891, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1892 | #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" , 1892, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1893 | 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" , 1893, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1894 | #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" , 1894, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1895 | 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" , 1895, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1896 | ||||
| 1897 | #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" , 1897, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1898 | 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" , 1898, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1899 | #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" , 1899, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1900 | 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" , 1900, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1901 | #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" , 1901, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1902 | 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" , 1902, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1903 | #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" , 1903, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1904 | 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" , 1904, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1905 | #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" , 1905, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1906 | 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" , 1906, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1907 | #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" , 1907, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1908 | 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" , 1908, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1909 | ||||
| 1910 | // Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of | |||
| 1911 | // ASSERT_XY(), which clashes with some users' own code. | |||
| 1912 | ||||
| 1913 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_EQ) && GTEST_DONT_DEFINE_ASSERT_EQ) | |||
| 1914 | #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" , 1914, 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" , 1914, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1915 | #endif | |||
| 1916 | ||||
| 1917 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_NE) && GTEST_DONT_DEFINE_ASSERT_NE) | |||
| 1918 | #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" , 1918, 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" , 1918, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1919 | #endif | |||
| 1920 | ||||
| 1921 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_LE) && GTEST_DONT_DEFINE_ASSERT_LE) | |||
| 1922 | #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" , 1922, 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" , 1922, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1923 | #endif | |||
| 1924 | ||||
| 1925 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_LT) && GTEST_DONT_DEFINE_ASSERT_LT) | |||
| 1926 | #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" , 1926, 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" , 1926, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1927 | #endif | |||
| 1928 | ||||
| 1929 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_GE) && GTEST_DONT_DEFINE_ASSERT_GE) | |||
| 1930 | #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" , 1930, 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" , 1930, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1931 | #endif | |||
| 1932 | ||||
| 1933 | #if !(defined(GTEST_DONT_DEFINE_ASSERT_GT) && GTEST_DONT_DEFINE_ASSERT_GT) | |||
| 1934 | #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" , 1934, 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" , 1934, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1935 | #endif | |||
| 1936 | ||||
| 1937 | // C-string Comparisons. All tests treat NULL and any non-NULL string | |||
| 1938 | // as different. Two NULLs are equal. | |||
| 1939 | // | |||
| 1940 | // * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 | |||
| 1941 | // * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 | |||
| 1942 | // * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case | |||
| 1943 | // * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case | |||
| 1944 | // | |||
| 1945 | // For wide or narrow string objects, you can use the | |||
| 1946 | // {ASSERT|EXPECT}_??() macros. | |||
| 1947 | // | |||
| 1948 | // Don't depend on the order in which the arguments are evaluated, | |||
| 1949 | // which is undefined. | |||
| 1950 | // | |||
| 1951 | // These macros evaluate their arguments exactly once. | |||
| 1952 | ||||
| 1953 | #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" , 1953, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1954 | 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" , 1954, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1955 | #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" , 1955, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1956 | 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" , 1956, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1957 | #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" , 1957, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1958 | 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" , 1958, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1959 | #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" , 1959, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1960 | 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" , 1960, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1961 | ||||
| 1962 | #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" , 1962, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1963 | 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" , 1963, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1964 | #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" , 1964, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1965 | 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" , 1965, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1966 | #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" , 1966, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1967 | 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" , 1967, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1968 | #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" , 1968, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1969 | 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" , 1969, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1970 | ||||
| 1971 | // Macros for comparing floating-point numbers. | |||
| 1972 | // | |||
| 1973 | // * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2): | |||
| 1974 | // Tests that two float values are almost equal. | |||
| 1975 | // * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2): | |||
| 1976 | // Tests that two double values are almost equal. | |||
| 1977 | // * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): | |||
| 1978 | // Tests that v1 and v2 are within the given distance to each other. | |||
| 1979 | // | |||
| 1980 | // Google Test uses ULP-based comparison to automatically pick a default | |||
| 1981 | // error bound that is appropriate for the operands. See the | |||
| 1982 | // FloatingPoint template class in gtest-internal.h if you are | |||
| 1983 | // interested in the implementation details. | |||
| 1984 | ||||
| 1985 | #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" , 1985, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1986 | 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" , 1987, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1987 | 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" , 1987, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1988 | ||||
| 1989 | #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" , 1989, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1990 | 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" , 1991, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1991 | 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" , 1991, gtest_ar.failure_message()) = ::testing::Message() | |||
| 1992 | ||||
| 1993 | #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", 1993 , gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1994 | 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", 1995 , gtest_ar.failure_message()) = ::testing::Message() | |||
| 1995 | 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", 1995 , gtest_ar.failure_message()) = ::testing::Message() | |||
| 1996 | ||||
| 1997 | #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", 1997 , gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 1998 | 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", 1999 , gtest_ar.failure_message()) = ::testing::Message() | |||
| 1999 | 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", 1999 , gtest_ar.failure_message()) = ::testing::Message() | |||
| 2000 | ||||
| 2001 | #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", 2001 , gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 2002 | 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", 2003 , gtest_ar.failure_message()) = ::testing::Message() | |||
| 2003 | 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", 2003 , gtest_ar.failure_message()) = ::testing::Message() | |||
| 2004 | ||||
| 2005 | #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" , 2005, gtest_ar.failure_message()) = ::testing::Message() \ | |||
| 2006 | 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" , 2007, gtest_ar.failure_message()) = ::testing::Message() | |||
| 2007 | 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" , 2007, gtest_ar.failure_message()) = ::testing::Message() | |||
| 2008 | ||||
| 2009 | // These predicate format functions work on floating-point values, and | |||
| 2010 | // can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. | |||
| 2011 | // | |||
| 2012 | // EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); | |||
| 2013 | ||||
| 2014 | // Asserts that val1 is less than, or almost equal to, val2. Fails | |||
| 2015 | // otherwise. In particular, it fails if either val1 or val2 is NaN. | |||
| 2016 | GTEST_API___attribute__((visibility("default"))) AssertionResult FloatLE(const char* expr1, const char* expr2, | |||
| 2017 | float val1, float val2); | |||
| 2018 | GTEST_API___attribute__((visibility("default"))) AssertionResult DoubleLE(const char* expr1, const char* expr2, | |||
| 2019 | double val1, double val2); | |||
| 2020 | ||||
| 2021 | #ifdef GTEST_OS_WINDOWS | |||
| 2022 | ||||
| 2023 | // Macros that test for HRESULT failure and success, these are only useful | |||
| 2024 | // on Windows, and rely on Windows SDK macros and APIs to compile. | |||
| 2025 | // | |||
| 2026 | // * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) | |||
| 2027 | // | |||
| 2028 | // When expr unexpectedly fails or succeeds, Google Test prints the | |||
| 2029 | // expected result and the actual result with both a human-readable | |||
| 2030 | // string representation of the error, if available, as well as the | |||
| 2031 | // hex result code. | |||
| 2032 | #define EXPECT_HRESULT_SUCCEEDED(expr) \ | |||
| 2033 | 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" , 2033, gtest_ar.failure_message()) = ::testing::Message() | |||
| 2034 | ||||
| 2035 | #define ASSERT_HRESULT_SUCCEEDED(expr) \ | |||
| 2036 | 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" , 2036, gtest_ar.failure_message()) = ::testing::Message() | |||
| 2037 | ||||
| 2038 | #define EXPECT_HRESULT_FAILED(expr) \ | |||
| 2039 | 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" , 2039, gtest_ar.failure_message()) = ::testing::Message() | |||
| 2040 | ||||
| 2041 | #define ASSERT_HRESULT_FAILED(expr) \ | |||
| 2042 | 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" , 2042, gtest_ar.failure_message()) = ::testing::Message() | |||
| 2043 | ||||
| 2044 | #endif // GTEST_OS_WINDOWS | |||
| 2045 | ||||
| 2046 | // Macros that execute statement and check that it doesn't generate new fatal | |||
| 2047 | // failures in the current thread. | |||
| 2048 | // | |||
| 2049 | // * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); | |||
| 2050 | // | |||
| 2051 | // Examples: | |||
| 2052 | // | |||
| 2053 | // EXPECT_NO_FATAL_FAILURE(Process()); | |||
| 2054 | // ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; | |||
| 2055 | // | |||
| 2056 | #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_2056; } } else gtest_label_testnofatal_2056 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2056, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() \ | |||
| 2057 | 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_2057; } } else gtest_label_testnofatal_2057 : return ::testing::internal::AssertHelper(::testing::TestPartResult ::kFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2057, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() | |||
| 2058 | #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_2058; } } else gtest_label_testnofatal_2058 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2058, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() \ | |||
| 2059 | 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_2059; } } else gtest_label_testnofatal_2059 : ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2059, "Expected: " "statement" " doesn't generate new fatal " "failures in the current thread.\n" " Actual: it does.") = :: testing::Message() | |||
| 2060 | ||||
| 2061 | // Causes a trace (including the given source file path and line number, | |||
| 2062 | // and the given message) to be included in every test failure message generated | |||
| 2063 | // by code in the scope of the lifetime of an instance of this class. The effect | |||
| 2064 | // is undone with the destruction of the instance. | |||
| 2065 | // | |||
| 2066 | // The message argument can be anything streamable to std::ostream. | |||
| 2067 | // | |||
| 2068 | // Example: | |||
| 2069 | // testing::ScopedTrace trace("file.cc", 123, "message"); | |||
| 2070 | // | |||
| 2071 | class GTEST_API___attribute__((visibility("default"))) ScopedTrace { | |||
| 2072 | public: | |||
| 2073 | // The c'tor pushes the given source file location and message onto | |||
| 2074 | // a trace stack maintained by Google Test. | |||
| 2075 | ||||
| 2076 | // Template version. Uses Message() to convert the values into strings. | |||
| 2077 | // Slow, but flexible. | |||
| 2078 | template <typename T> | |||
| 2079 | ScopedTrace(const char* file, int line, const T& message) { | |||
| 2080 | PushTrace(file, line, (Message() << message).GetString()); | |||
| 2081 | } | |||
| 2082 | ||||
| 2083 | // Optimize for some known types. | |||
| 2084 | ScopedTrace(const char* file, int line, const char* message) { | |||
| 2085 | PushTrace(file, line, message ? message : "(null)"); | |||
| 2086 | } | |||
| 2087 | ||||
| 2088 | ScopedTrace(const char* file, int line, const std::string& message) { | |||
| 2089 | PushTrace(file, line, message); | |||
| 2090 | } | |||
| 2091 | ||||
| 2092 | // The d'tor pops the info pushed by the c'tor. | |||
| 2093 | // | |||
| 2094 | // Note that the d'tor is not virtual in order to be efficient. | |||
| 2095 | // Don't inherit from ScopedTrace! | |||
| 2096 | ~ScopedTrace(); | |||
| 2097 | ||||
| 2098 | private: | |||
| 2099 | void PushTrace(const char* file, int line, std::string message); | |||
| 2100 | ||||
| 2101 | ScopedTrace(const ScopedTrace&) = delete; | |||
| 2102 | ScopedTrace& operator=(const ScopedTrace&) = delete; | |||
| 2103 | }; | |||
| 2104 | ||||
| 2105 | // Causes a trace (including the source file path, the current line | |||
| 2106 | // number, and the given message) to be included in every test failure | |||
| 2107 | // message generated by code in the current scope. The effect is | |||
| 2108 | // undone when the control leaves the current scope. | |||
| 2109 | // | |||
| 2110 | // The message argument can be anything streamable to std::ostream. | |||
| 2111 | // | |||
| 2112 | // In the implementation, we include the current line number as part | |||
| 2113 | // of the dummy variable name, thus allowing multiple SCOPED_TRACE()s | |||
| 2114 | // to appear in the same block - as long as they are on different | |||
| 2115 | // lines. | |||
| 2116 | // | |||
| 2117 | // Assuming that each thread maintains its own stack of traces. | |||
| 2118 | // Therefore, a SCOPED_TRACE() would (correctly) only affect the | |||
| 2119 | // assertions in its own thread. | |||
| 2120 | #define SCOPED_TRACE(message)const ::testing::ScopedTrace gtest_trace_2120( "/usr/src/googletest/googletest/include/gtest/gtest.h" , 2120, (message)) \ | |||
| 2121 | const ::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)gtest_trace_2121( \ | |||
| 2122 | __FILE__"/usr/src/googletest/googletest/include/gtest/gtest.h", __LINE__2122, (message)) | |||
| 2123 | ||||
| 2124 | // Compile-time assertion for type equality. | |||
| 2125 | // StaticAssertTypeEq<type1, type2>() compiles if and only if type1 and type2 | |||
| 2126 | // are the same type. The value it returns is not interesting. | |||
| 2127 | // | |||
| 2128 | // Instead of making StaticAssertTypeEq a class template, we make it a | |||
| 2129 | // function template that invokes a helper class template. This | |||
| 2130 | // prevents a user from misusing StaticAssertTypeEq<T1, T2> by | |||
| 2131 | // defining objects of that type. | |||
| 2132 | // | |||
| 2133 | // CAVEAT: | |||
| 2134 | // | |||
| 2135 | // When used inside a method of a class template, | |||
| 2136 | // StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is | |||
| 2137 | // instantiated. For example, given: | |||
| 2138 | // | |||
| 2139 | // template <typename T> class Foo { | |||
| 2140 | // public: | |||
| 2141 | // void Bar() { testing::StaticAssertTypeEq<int, T>(); } | |||
| 2142 | // }; | |||
| 2143 | // | |||
| 2144 | // the code: | |||
| 2145 | // | |||
| 2146 | // void Test1() { Foo<bool> foo; } | |||
| 2147 | // | |||
| 2148 | // will NOT generate a compiler error, as Foo<bool>::Bar() is never | |||
| 2149 | // actually instantiated. Instead, you need: | |||
| 2150 | // | |||
| 2151 | // void Test2() { Foo<bool> foo; foo.Bar(); } | |||
| 2152 | // | |||
| 2153 | // to cause a compiler error. | |||
| 2154 | template <typename T1, typename T2> | |||
| 2155 | constexpr bool StaticAssertTypeEq() noexcept { | |||
| 2156 | static_assert(std::is_same<T1, T2>::value, "T1 and T2 are not the same type"); | |||
| 2157 | return true; | |||
| 2158 | } | |||
| 2159 | ||||
| 2160 | // Defines a test. | |||
| 2161 | // | |||
| 2162 | // The first parameter is the name of the test suite, and the second | |||
| 2163 | // parameter is the name of the test within the test suite. | |||
| 2164 | // | |||
| 2165 | // The convention is to end the test suite name with "Test". For | |||
| 2166 | // example, a test suite for the Foo class can be named FooTest. | |||
| 2167 | // | |||
| 2168 | // Test code should appear between braces after an invocation of | |||
| 2169 | // this macro. Example: | |||
| 2170 | // | |||
| 2171 | // TEST(FooTest, InitializesCorrectly) { | |||
| 2172 | // Foo foo; | |||
| 2173 | // EXPECT_TRUE(foo.StatusIsOK()); | |||
| 2174 | // } | |||
| 2175 | ||||
| 2176 | // Note that we call GetTestTypeId() instead of GetTypeId< | |||
| 2177 | // ::testing::Test>() here to get the type ID of testing::Test. This | |||
| 2178 | // is to work around a suspected linker bug when using Google Test as | |||
| 2179 | // a framework on Mac OS X. The bug causes GetTypeId< | |||
| 2180 | // ::testing::Test>() to return different values depending on whether | |||
| 2181 | // the call is from the Google Test framework itself or from user test | |||
| 2182 | // code. GetTestTypeId() is guaranteed to always return the same | |||
| 2183 | // value, as it always calls GetTypeId<>() from the Google Test | |||
| 2184 | // framework. | |||
| 2185 | #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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::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", 2185 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2185 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2185), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() \ | |||
| 2186 | 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::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", 2187 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2187 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2187), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() | |||
| 2187 | ::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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::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", 2187 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2187 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2187), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() | |||
| 2188 | ||||
| 2189 | // Define this macro to 1 to omit the definition of TEST(), which | |||
| 2190 | // is a generic name and clashes with some other libraries. | |||
| 2191 | #if !(defined(GTEST_DONT_DEFINE_TEST) && GTEST_DONT_DEFINE_TEST) | |||
| 2192 | #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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::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", 2192 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2192 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2192), 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 ; [[maybe_unused]] static ::testing::TestInfo* const test_info_ ; }; ::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", 2192 ), (::testing::internal::GetTestTypeId()), ::testing::internal ::SuiteApiResolver< ::testing::Test>::GetSetUpCaseOrSuite ("/usr/src/googletest/googletest/include/gtest/gtest.h", 2192 ), ::testing::internal::SuiteApiResolver< ::testing::Test> ::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2192), new ::testing::internal::TestFactoryImpl<test_suite_name_test_name_Test >); void test_suite_name_test_name_Test::TestBody() | |||
| 2193 | #endif | |||
| 2194 | ||||
| 2195 | // Defines a test that uses a test fixture. | |||
| 2196 | // | |||
| 2197 | // The first parameter is the name of the test fixture class, which | |||
| 2198 | // also doubles as the test suite name. The second parameter is the | |||
| 2199 | // name of the test within the test suite. | |||
| 2200 | // | |||
| 2201 | // A test fixture class must be declared earlier. The user should put | |||
| 2202 | // the test code between braces after using this macro. Example: | |||
| 2203 | // | |||
| 2204 | // class FooTest : public testing::Test { | |||
| 2205 | // protected: | |||
| 2206 | // void SetUp() override { b_.AddElement(3); } | |||
| 2207 | // | |||
| 2208 | // Foo a_; | |||
| 2209 | // Foo b_; | |||
| 2210 | // }; | |||
| 2211 | // | |||
| 2212 | // TEST_F(FooTest, InitializesCorrectly) { | |||
| 2213 | // EXPECT_TRUE(a_.StatusIsOK()); | |||
| 2214 | // } | |||
| 2215 | // | |||
| 2216 | // TEST_F(FooTest, ReturnsElementCountCorrectly) { | |||
| 2217 | // EXPECT_EQ(a_.size(), 0); | |||
| 2218 | // EXPECT_EQ(b_.size(), 1); | |||
| 2219 | // } | |||
| 2220 | #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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::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" , 2220), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2220), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2220), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() \ | |||
| 2221 | 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::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" , 2222), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2222), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2222), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() | |||
| 2222 | ::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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::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" , 2222), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2222), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2222), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() | |||
| 2223 | #if !(defined(GTEST_DONT_DEFINE_TEST_F) && GTEST_DONT_DEFINE_TEST_F) | |||
| 2224 | #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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::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" , 2224), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2224), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2224), 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; [[maybe_unused] ] static ::testing::TestInfo* const test_info_; }; ::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" , 2224), (::testing::internal::GetTypeId<test_fixture>( )), ::testing::internal::SuiteApiResolver< test_fixture> ::GetSetUpCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2224), ::testing::internal::SuiteApiResolver< test_fixture >::GetTearDownCaseOrSuite("/usr/src/googletest/googletest/include/gtest/gtest.h" , 2224), new ::testing::internal::TestFactoryImpl<test_fixture_test_name_Test >); void test_fixture_test_name_Test::TestBody() | |||
| 2225 | #endif | |||
| 2226 | ||||
| 2227 | // Returns a path to a temporary directory, which should be writable. It is | |||
| 2228 | // implementation-dependent whether or not the path is terminated by the | |||
| 2229 | // directory-separator character. | |||
| 2230 | GTEST_API___attribute__((visibility("default"))) std::string TempDir(); | |||
| 2231 | ||||
| 2232 | // Returns a path to a directory that contains ancillary data files that might | |||
| 2233 | // be used by tests. It is implementation dependent whether or not the path is | |||
| 2234 | // terminated by the directory-separator character. The directory and the files | |||
| 2235 | // in it should be considered read-only. | |||
| 2236 | GTEST_API___attribute__((visibility("default"))) std::string SrcDir(); | |||
| 2237 | ||||
| 2238 | GTEST_DISABLE_MSC_WARNINGS_POP_() // 4805 4100 | |||
| 2239 | ||||
| 2240 | // Dynamically registers a test with the framework. | |||
| 2241 | // | |||
| 2242 | // This is an advanced API only to be used when the `TEST` macros are | |||
| 2243 | // insufficient. The macros should be preferred when possible, as they avoid | |||
| 2244 | // most of the complexity of calling this function. | |||
| 2245 | // | |||
| 2246 | // The `factory` argument is a factory callable (move-constructible) object or | |||
| 2247 | // function pointer that creates a new instance of the Test object. It | |||
| 2248 | // handles ownership to the caller. The signature of the callable is | |||
| 2249 | // `Fixture*()`, where `Fixture` is the test fixture class for the test. All | |||
| 2250 | // tests registered with the same `test_suite_name` must return the same | |||
| 2251 | // fixture type. This is checked at runtime. | |||
| 2252 | // | |||
| 2253 | // The framework will infer the fixture class from the factory and will call | |||
| 2254 | // the `SetUpTestSuite` and `TearDownTestSuite` for it. | |||
| 2255 | // | |||
| 2256 | // Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is | |||
| 2257 | // undefined. | |||
| 2258 | // | |||
| 2259 | // Use case example: | |||
| 2260 | // | |||
| 2261 | // class MyFixture : public ::testing::Test { | |||
| 2262 | // public: | |||
| 2263 | // // All of these optional, just like in regular macro usage. | |||
| 2264 | // static void SetUpTestSuite() { ... } | |||
| 2265 | // static void TearDownTestSuite() { ... } | |||
| 2266 | // void SetUp() override { ... } | |||
| 2267 | // void TearDown() override { ... } | |||
| 2268 | // }; | |||
| 2269 | // | |||
| 2270 | // class MyTest : public MyFixture { | |||
| 2271 | // public: | |||
| 2272 | // explicit MyTest(int data) : data_(data) {} | |||
| 2273 | // void TestBody() override { ... } | |||
| 2274 | // | |||
| 2275 | // private: | |||
| 2276 | // int data_; | |||
| 2277 | // }; | |||
| 2278 | // | |||
| 2279 | // void RegisterMyTests(const std::vector<int>& values) { | |||
| 2280 | // for (int v : values) { | |||
| 2281 | // ::testing::RegisterTest( | |||
| 2282 | // "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, | |||
| 2283 | // std::to_string(v).c_str(), | |||
| 2284 | // __FILE__, __LINE__, | |||
| 2285 | // // Important to use the fixture type as the return type here. | |||
| 2286 | // [=]() -> MyFixture* { return new MyTest(v); }); | |||
| 2287 | // } | |||
| 2288 | // } | |||
| 2289 | // ... | |||
| 2290 | // int main(int argc, char** argv) { | |||
| 2291 | // ::testing::InitGoogleTest(&argc, argv); | |||
| 2292 | // std::vector<int> values_to_test = LoadValuesFromConfig(); | |||
| 2293 | // RegisterMyTests(values_to_test); | |||
| 2294 | // ... | |||
| 2295 | // return RUN_ALL_TESTS(); | |||
| 2296 | // } | |||
| 2297 | // | |||
| 2298 | template <int&... ExplicitParameterBarrier, typename Factory> | |||
| 2299 | TestInfo* RegisterTest(const char* test_suite_name, const char* test_name, | |||
| 2300 | const char* type_param, const char* value_param, | |||
| 2301 | const char* file, int line, Factory factory) { | |||
| 2302 | using TestT = typename std::remove_pointer<decltype(factory())>::type; | |||
| 2303 | ||||
| 2304 | class FactoryImpl : public internal::TestFactoryBase { | |||
| 2305 | public: | |||
| 2306 | explicit FactoryImpl(Factory f) : factory_(std::move(f)) {} | |||
| 2307 | Test* CreateTest() override { return factory_(); } | |||
| 2308 | ||||
| 2309 | private: | |||
| 2310 | Factory factory_; | |||
| 2311 | }; | |||
| 2312 | ||||
| 2313 | return internal::MakeAndRegisterTestInfo( | |||
| 2314 | test_suite_name, test_name, type_param, value_param, | |||
| 2315 | internal::CodeLocation(file, line), internal::GetTypeId<TestT>(), | |||
| 2316 | internal::SuiteApiResolver<TestT>::GetSetUpCaseOrSuite(file, line), | |||
| 2317 | internal::SuiteApiResolver<TestT>::GetTearDownCaseOrSuite(file, line), | |||
| 2318 | new FactoryImpl{std::move(factory)}); | |||
| 2319 | } | |||
| 2320 | ||||
| 2321 | } // namespace testing | |||
| 2322 | ||||
| 2323 | // Use this function in main() to run all tests. It returns 0 if all | |||
| 2324 | // tests are successful, or 1 otherwise. | |||
| 2325 | // | |||
| 2326 | // RUN_ALL_TESTS() should be invoked after the command line has been | |||
| 2327 | // parsed by InitGoogleTest(). RUN_ALL_TESTS will tear down and delete any | |||
| 2328 | // installed environments and should only be called once per binary. | |||
| 2329 | // | |||
| 2330 | // This function was formerly a macro; thus, it is in the global | |||
| 2331 | // namespace and has an all-caps name. | |||
| 2332 | int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT___attribute__((warn_unused_result)); | |||
| 2333 | ||||
| 2334 | inline int RUN_ALL_TESTS() { return ::testing::UnitTest::GetInstance()->Run(); } | |||
| 2335 | ||||
| 2336 | GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 | |||
| 2337 | ||||
| 2338 | #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_H_ |