From: tho Date: Tue, 10 Feb 2009 12:57:42 +0000 (+0000) Subject: Socket/Protocols/Raw: added MACAddress input streaming X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=e9da01a4341eca44e058ecb12ea77c173a8b6329;p=senf.git Socket/Protocols/Raw: added MACAddress input streaming git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1113 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Socket/Protocols/INet/INet4Address.test.cc b/Socket/Protocols/INet/INet4Address.test.cc index 944827f..7ff1539 100644 --- a/Socket/Protocols/INet/INet4Address.test.cc +++ b/Socket/Protocols/INet/INet4Address.test.cc @@ -67,6 +67,9 @@ BOOST_AUTO_UNIT_TEST(inet4Address) std::stringstream str; str << addr; BOOST_CHECK_EQUAL( str.str(), "128.129.130.131" ); + + str >> addr; + BOOST_CHECK_EQUAL(addr, senf::INet4Address::from_string("128.129.130.131") ); } BOOST_AUTO_UNIT_TEST(inet4Network) diff --git a/Socket/Protocols/INet/INet6Address.test.cc b/Socket/Protocols/INet/INet6Address.test.cc index 28bba52..4932467 100644 --- a/Socket/Protocols/INet/INet6Address.test.cc +++ b/Socket/Protocols/INet/INet6Address.test.cc @@ -129,6 +129,13 @@ BOOST_AUTO_UNIT_TEST(inet6Address) BOOST_CHECK( INet6Address::Loopback ); BOOST_CHECK( ! INet6Address::None ); + + std::stringstream str; + str << addr; + BOOST_CHECK_EQUAL( str.str(), "2001:dead:beef::1002:3004"); + + str >> addr; + BOOST_CHECK_EQUAL(addr, INet6Address::from_string("2001:dead:beef::1002:3004")); } { diff --git a/Socket/Protocols/Raw/MACAddress.cc b/Socket/Protocols/Raw/MACAddress.cc index 22b0ca9..6c36a47 100644 --- a/Socket/Protocols/Raw/MACAddress.cc +++ b/Socket/Protocols/Raw/MACAddress.cc @@ -128,6 +128,20 @@ prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & ma return os; } +prefix_ std::istream & senf::operator>>(std::istream & is, MACAddress & mac) +{ + std::string s; + if (!(is >> s)) + return is; + try { + mac = MACAddress::from_string(s); + } + catch (AddressException &) { + is.setstate(std::ios::failbit); + } + return is; +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "MACAddress.mpp" diff --git a/Socket/Protocols/Raw/MACAddress.hh b/Socket/Protocols/Raw/MACAddress.hh index c62c1df..00030c6 100644 --- a/Socket/Protocols/Raw/MACAddress.hh +++ b/Socket/Protocols/Raw/MACAddress.hh @@ -134,10 +134,16 @@ namespace senf { }; - /** \brief Write MAC address + /** \brief Output MAC instance as it's string representation \related MACAddress */ std::ostream & operator<<(std::ostream & os, MACAddress const & mac); + /** \brief Try to initialize MACAddress instance from a string representation + sets std::ios::failbit on the stream if an error occurred + \see MACAddress from_string() + \related MACAddress + */ + std::istream & operator>>(std::istream & os, MACAddress & mac); } diff --git a/Socket/Protocols/Raw/MACAddress.test.cc b/Socket/Protocols/Raw/MACAddress.test.cc index 683139d..0550c1a 100644 --- a/Socket/Protocols/Raw/MACAddress.test.cc +++ b/Socket/Protocols/Raw/MACAddress.test.cc @@ -28,8 +28,8 @@ // Custom includes #include -#include #include "MACAddress.hh" +#include "../../../Utils/String.hh" #include "../../../Utils/auto_unit_test.hh" #include @@ -39,8 +39,11 @@ BOOST_AUTO_UNIT_TEST(macAddress) { + using senf::MACAddress; + using senf::AddressSyntaxException; + std::string test ("A1-b2-C3:d4:E5:f6"); - senf::MACAddress mac (senf::MACAddress::from_string(test)); + MACAddress mac (MACAddress::from_string(test)); BOOST_CHECK_EQUAL( mac[0], 0xA1u ); BOOST_CHECK_EQUAL( mac[1], 0xB2u ); @@ -52,7 +55,9 @@ BOOST_AUTO_UNIT_TEST(macAddress) std::stringstream str; str << mac; BOOST_CHECK_EQUAL( str.str(), "a1:b2:c3:d4:e5:f6" ); - BOOST_CHECK_EQUAL (mac.toString(), "a1:b2:c3:d4:e5:f6"); + + str >> mac; + BOOST_CHECK_EQUAL(mac, MACAddress::from_string(test)); BOOST_CHECK( ! mac.local() ); BOOST_CHECK( mac.multicast() ); @@ -62,30 +67,27 @@ BOOST_AUTO_UNIT_TEST(macAddress) BOOST_CHECK_EQUAL( mac.nic(), 0xd4e5f6u ); BOOST_CHECK_EQUAL( mac.eui64(), 0xa1b2c3fffed4e5f6llu ); - senf::MACAddress mac2; + MACAddress mac2; BOOST_CHECK( ! mac2 ); - mac2 = senf::MACAddress::from_string("ff:ff:ff:ff:ff:ff"); + mac2 = MACAddress::from_string("ff:ff:ff:ff:ff:ff"); BOOST_CHECK( mac2.broadcast() ); - BOOST_CHECK_EQUAL( mac2, senf::MACAddress::Broadcast ); + BOOST_CHECK_EQUAL( mac2, MACAddress::Broadcast ); char data[] = { 0x01,0x02,0x03,0x04,0x05,0x06 }; - mac2 = senf::MACAddress::from_data(data); - BOOST_CHECK_EQUAL( boost::lexical_cast(mac2), "01:02:03:04:05:06" ); + mac2 = MACAddress::from_data(data); + BOOST_CHECK_EQUAL( senf::str(mac2), "01:02:03:04:05:06" ); BOOST_CHECK( mac != mac2 ); mac2 = mac; BOOST_CHECK( mac == mac2 ); - BOOST_CHECK_EQUAL( boost::lexical_cast(mac2), "a1:b2:c3:d4:e5:f6" ); + BOOST_CHECK_EQUAL( senf::str(mac2), "a1:b2:c3:d4:e5:f6" ); - BOOST_CHECK_THROW( senf::MACAddress::from_string("1:2:3:4:5:6"), - senf::AddressSyntaxException ); - BOOST_CHECK_THROW( senf::MACAddress::from_string("01:02:03:04:05"), - senf::AddressSyntaxException ); - BOOST_CHECK_THROW( senf::MACAddress::from_string("01:02:03:04:05:z6"), - senf::AddressSyntaxException ); + BOOST_CHECK_THROW( MACAddress::from_string("1:2:3:4:5:6"), AddressSyntaxException ); + BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05"), AddressSyntaxException ); + BOOST_CHECK_THROW( MACAddress::from_string("01:02:03:04:05:z6"), AddressSyntaxException ); - BOOST_CHECK_EQUAL( mac, senf::MACAddress::from_eui64(0xa1b2c3fffed4e5f6llu) ); - BOOST_CHECK_THROW( senf::MACAddress::from_eui64(0u), senf::AddressSyntaxException ); + BOOST_CHECK_EQUAL( mac, MACAddress::from_eui64(0xa1b2c3fffed4e5f6llu) ); + BOOST_CHECK_THROW( MACAddress::from_eui64(0u), AddressSyntaxException ); - BOOST_CHECK_EQUAL( senf::MACAddress(0x1a2b3c4d5e6fULL).uint64(), 0x1a2b3c4d5e6fULL); + BOOST_CHECK_EQUAL( MACAddress(0x1a2b3c4d5e6fULL).uint64(), 0x1a2b3c4d5e6fULL); } ///////////////////////////////cc.e////////////////////////////////////////