예제 #1
0
TEST(IPAddress, fromBinaryV6) {
  for (auto& tc : provideBinary16Bytes) {
    SCOPED_TRACE(tc.first);
    ByteRange bytes(&tc.second.front(), tc.second.size());

    auto fromBin = IPAddressV6::fromBinary(bytes);
    IPAddressV6 fromStr(tc.first);
    EXPECT_EQ(fromStr, fromBin);

    IPAddressV6 addr2("::0");
    addr2 = IPAddressV6::fromBinary(bytes);
    EXPECT_EQ(fromStr, addr2);

    IPAddress genericAddr = IPAddress::fromBinary(bytes);
    ASSERT_TRUE(genericAddr.isV6());
    EXPECT_EQ(fromStr, genericAddr.asV6());
    EXPECT_EQ(ByteRange(genericAddr.bytes(), genericAddr.byteCount()), bytes);
  }

  uint8_t data[20];
  EXPECT_THROW(IPAddressV6::fromBinary(ByteRange(data, 3)),
               IPAddressFormatException);
  EXPECT_THROW(IPAddressV6::fromBinary(ByteRange(data, 4)),
               IPAddressFormatException);
  EXPECT_THROW(IPAddressV6::fromBinary(ByteRange(data, 20)),
               IPAddressFormatException);
}
예제 #2
0
TEST(IPAddress, fromBinaryV4) {
  for (auto& tc : provideToLong) {
    SCOPED_TRACE(tc.first);
    union {
      uint8_t u8[4];
      uint32_t u32;
    } data;
    data.u32 = Endian::big(tc.second);
    ByteRange bytes(data.u8, 4);

    auto fromBin = IPAddressV4::fromBinary(bytes);
    IPAddressV4 fromStr(tc.first);
    EXPECT_EQ(fromStr, fromBin);

    IPAddressV4 addr2("0.0.0.0");
    addr2 = IPAddressV4::fromBinary(bytes);
    EXPECT_EQ(fromStr, addr2);

    IPAddress genericAddr = IPAddress::fromBinary(bytes);
    ASSERT_TRUE(genericAddr.isV4());
    EXPECT_EQ(fromStr, genericAddr.asV4());
    EXPECT_EQ(ByteRange(genericAddr.bytes(), genericAddr.byteCount()), bytes);
  }

  uint8_t data[20];
  EXPECT_THROW(IPAddressV4::fromBinary(ByteRange(data, 3)),
               IPAddressFormatException);
  EXPECT_THROW(IPAddressV4::fromBinary(ByteRange(data, 16)),
               IPAddressFormatException);
  EXPECT_THROW(IPAddressV4::fromBinary(ByteRange(data, 20)),
               IPAddressFormatException);
}