#include <folly/io/IOBuf.h>
#include <folly/io/async/test/AsyncSocketTest.h>
#include <folly/io/async/test/Util.h>
+#include <folly/test/SocketAddressTestHelper.h>
#include <gtest/gtest.h>
#include <boost/scoped_array.hpp>
// Hopefully this IP will be routable but unresponsive.
// (Alternatively, we could try listening on a local raw socket, but that
// normally requires root privileges.)
- folly::SocketAddress addr("8.8.8.8", 65535);
+ auto host =
+ SocketAddressTestHelper::isIPv6Enabled() ?
+ SocketAddressTestHelper::kGooglePublicDnsAAddrIPv6 :
+ SocketAddressTestHelper::isIPv4Enabled() ?
+ SocketAddressTestHelper::kGooglePublicDnsAAddrIPv4 :
+ nullptr;
+ SocketAddress addr(host, 65535);
ConnCallback cb;
socket->connect(&cb, addr, 1); // also set a ridiculously small timeout
#include <gtest/gtest.h>
#include <iostream>
#include <sstream>
+#include <folly/test/SocketAddressTestHelper.h>
using namespace boost;
using std::string;
using std::cerr;
using std::endl;
using folly::SocketAddress;
+using folly::SocketAddressTestHelper;
TEST(SocketAddress, Size) {
SocketAddress addr;
EXPECT_EQ(addr.getPort(), 80);
// Call setFromLocalIpPort() with an IP and port.
- addr.setFromLocalIpPort("127.0.0.1:4321");
- EXPECT_EQ(addr.getAddressStr(), "127.0.0.1");
- EXPECT_EQ(addr.getPort(), 4321);
+ if (SocketAddressTestHelper::isIPv4Enabled()) {
+ addr.setFromLocalIpPort("127.0.0.1:4321");
+ EXPECT_EQ(addr.getAddressStr(), "127.0.0.1");
+ EXPECT_EQ(addr.getPort(), 4321);
+ }
+ if (SocketAddressTestHelper::isIPv6Enabled()) {
+ addr.setFromLocalIpPort("::1:4321");
+ EXPECT_EQ(addr.getAddressStr(), "::1");
+ EXPECT_EQ(addr.getPort(), 4321);
+ }
// setFromIpPort() without an address should fail
EXPECT_THROW(addr.setFromIpPort("4321"), std::invalid_argument);
--- /dev/null
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/test/SocketAddressTestHelper.h>
+
+#include <sys/socket.h>
+#include <netdb.h>
+#include <glog/logging.h>
+
+namespace folly {
+
+constexpr const char* SocketAddressTestHelper::kLoopbackAddrIPv4;
+constexpr const char* SocketAddressTestHelper::kLoopbackAddrIPv6;
+constexpr const char* SocketAddressTestHelper::kGooglePublicDnsAName;
+constexpr const char* SocketAddressTestHelper::kGooglePublicDnsBName;
+constexpr const char* SocketAddressTestHelper::kGooglePublicDnsAAddrIPv4;
+constexpr const char* SocketAddressTestHelper::kGooglePublicDnsBAddrIPv4;
+constexpr const char* SocketAddressTestHelper::kGooglePublicDnsAAddrIPv6;
+constexpr const char* SocketAddressTestHelper::kGooglePublicDnsBAddrIPv6;
+
+bool SocketAddressTestHelper::isIPv4Enabled() {
+ return isFamilyOfAddrEnabled(kLoopbackAddrIPv4);
+}
+
+bool SocketAddressTestHelper::isIPv6Enabled() {
+ return isFamilyOfAddrEnabled(kLoopbackAddrIPv6);
+}
+
+bool SocketAddressTestHelper::isFamilyOfAddrEnabled(const char* addr) {
+ struct addrinfo hints {};
+ hints.ai_flags = AI_ADDRCONFIG;
+
+ struct addrinfo* resultsp = nullptr;
+ int err = getaddrinfo(addr, nullptr, &hints, &resultsp);
+ freeaddrinfo(resultsp);
+ return !err;
+}
+
+}
--- /dev/null
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+namespace folly {
+
+class SocketAddressTestHelper {
+ public:
+
+ static constexpr const char* kLoopbackAddrIPv4 = "127.0.0.1";
+ static constexpr const char* kLoopbackAddrIPv6 = "::1";
+
+ // https://developers.google.com/speed/public-dns/docs/using?hl=en
+ static constexpr const char* kGooglePublicDnsAName =
+ "google-public-dns-a.google.com";
+ static constexpr const char* kGooglePublicDnsBName =
+ "google-public-dns-b.google.com";
+ static constexpr const char* kGooglePublicDnsAAddrIPv4 = "8.8.8.8";
+ static constexpr const char* kGooglePublicDnsBAddrIPv4 = "8.8.4.4";
+ static constexpr const char* kGooglePublicDnsAAddrIPv6 =
+ "2001:4860:4860::8888";
+ static constexpr const char* kGooglePublicDnsBAddrIPv6 =
+ "2001:4860:4860::8844";
+
+ static bool isIPv4Enabled();
+ static bool isIPv6Enabled();
+
+ private:
+
+ static bool isFamilyOfAddrEnabled(const char* addr);
+};
+
+}