Bug 1200763 - Remove hard-coded STUN IP address from ice_unittest, and do a DNS lookup instead. r=drno
authorByron Campen [:bwc] <docfaraday@gmail.com>
Fri, 04 Sep 2015 10:21:12 -0500
changeset 293956 5e54b1671de465cac7813bf20afdfa0ab46647d3
parent 293955 08960e6b65c3ea3dff06124660541652467eb3ed
child 293957 d56cd50e14618db3dac3a35d81003ec49eb6239e
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdrno
bugs1200763
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1200763 - Remove hard-coded STUN IP address from ice_unittest, and do a DNS lookup instead. r=drno
media/mtransport/nriceresolver.cpp
media/mtransport/nriceresolver.h
media/mtransport/test/ice_unittest.cpp
media/mtransport/third_party/nICEr/src/net/nr_resolver.h
--- a/media/mtransport/nriceresolver.cpp
+++ b/media/mtransport/nriceresolver.cpp
@@ -149,17 +149,17 @@ int NrIceResolver::resolve(nr_resolver_r
   int _status;
   MOZ_ASSERT(allocated_resolvers_ > 0);
   ASSERT_ON_THREAD(sts_thread_);
   nsRefPtr<PendingResolution> pr;
   uint32_t resolve_flags = 0;
 
   if (resource->transport_protocol != IPPROTO_UDP &&
       resource->transport_protocol != IPPROTO_TCP) {
-    MOZ_MTLOG(ML_ERROR, "Only UDP and TCP are is supported.");
+    MOZ_MTLOG(ML_ERROR, "Only UDP and TCP are supported.");
     ABORT(R_NOT_FOUND);
   }
   pr = new PendingResolution(sts_thread_,
                              resource->port? resource->port : 3478,
                              resource->transport_protocol ?
                              resource->transport_protocol :
                              IPPROTO_UDP,
                              cb, cb_arg);
--- a/media/mtransport/nriceresolver.h
+++ b/media/mtransport/nriceresolver.h
@@ -66,29 +66,29 @@ class NrIceResolver
  public:
   NrIceResolver();
 
   nsresult Init();
   nr_resolver *AllocateResolver();
   void DestroyResolver();
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceResolver)
 
+  int resolve(nr_resolver_resource *resource,
+              int (*cb)(void *cb_arg, nr_transport_addr *addr),
+              void *cb_arg, void **handle);
+
  private:
   // Implementations of vtbl functions
   static int destroy(void **objp);
   static int resolve(void *obj, nr_resolver_resource *resource,
                      int (*cb)(void *cb_arg, nr_transport_addr *addr),
                      void *cb_arg, void **handle);
   static void resolve_cb(NR_SOCKET s, int how, void *cb_arg);
   static int cancel(void *obj, void *handle);
 
-  int resolve(nr_resolver_resource *resource,
-              int (*cb)(void *cb_arg, nr_transport_addr *addr),
-              void *cb_arg, void **handle);
-
   class PendingResolution : public nsIDNSListener
   {
    public:
     PendingResolution(nsIEventTarget *thread,
                       uint16_t port,
                       int transport,
                       int (*cb)(void *cb_arg, nr_transport_addr *addr),
                       void *cb_arg) :
--- a/media/mtransport/test/ice_unittest.cpp
+++ b/media/mtransport/test/ice_unittest.cpp
@@ -45,44 +45,44 @@
 #include "mozilla/net/DNS.h"
 
 #include "ice_ctx.h"
 #include "ice_peer_ctx.h"
 #include "ice_media_stream.h"
 
 extern "C" {
 #include "r_data.h"
+#include "util.h"
 }
 
 #define GTEST_HAS_RTTI 0
 #include "gtest/gtest.h"
 #include "gtest_utils.h"
 
 using namespace mozilla;
 MtransportTestUtils *test_utils;
 
 bool stream_added = false;
 
 static unsigned int kDefaultTimeout = 7000;
 
 //TODO(nils@mozilla.com): This should get replaced with some non-external
 //solution like discussed in bug 860775.
-const std::string kDefaultStunServerAddress((char *)"52.27.56.60");
 const std::string kDefaultStunServerHostname(
     (char *)"global.stun.twilio.com");
 const std::string kBogusStunServerHostname(
     (char *)"stun-server-nonexistent.invalid");
 const uint16_t kDefaultStunServerPort=3478;
 const std::string kBogusIceCandidate(
     (char *)"candidate:0 2 UDP 2113601790 192.168.178.20 50769 typ");
 
 const std::string kUnreachableHostIceCandidate(
     (char *)"candidate:0 1 UDP 2113601790 192.168.178.20 50769 typ host");
 
-std::string g_stun_server_address(kDefaultStunServerAddress);
+std::string g_stun_server_address;
 std::string g_stun_server_hostname(kDefaultStunServerHostname);
 std::string g_turn_server;
 std::string g_turn_user;
 std::string g_turn_password;
 
 namespace {
 
 enum TrickleMode { TRICKLE_NONE, TRICKLE_SIMULATE, TRICKLE_REAL };
@@ -3055,27 +3055,70 @@ static std::string get_environment(const
   char *value = getenv(name);
 
   if (!value)
     return "";
 
   return value;
 }
 
+// DNS resolution helper code
+static std::string
+Resolve(const std::string& fqdn, int address_family)
+{
+  struct addrinfo hints;
+  memset(&hints, 0, sizeof(hints));
+  hints.ai_family = address_family;
+  hints.ai_protocol = IPPROTO_UDP;
+  struct addrinfo *res;
+  int err = getaddrinfo(fqdn.c_str(), nullptr, &hints, &res);
+  if (err) {
+    std::cerr << "Error in getaddrinfo: " << err << std::endl;
+    return "";
+  }
+
+  char str_addr[64] = {0};
+  switch (res->ai_family) {
+    case AF_INET:
+      inet_ntop(
+          AF_INET,
+          &reinterpret_cast<struct sockaddr_in*>(res->ai_addr)->sin_addr,
+          str_addr,
+          sizeof(str_addr));
+    case AF_INET6:
+      inet_ntop(
+          AF_INET6,
+          &reinterpret_cast<struct sockaddr_in6*>(res->ai_addr)->sin6_addr,
+          str_addr,
+          sizeof(str_addr));
+    default:
+      std::cerr << "Got unexpected address family in DNS lookup: "
+                << res->ai_family << std::endl;
+      return "";
+  }
+
+  if (!strlen(str_addr)) {
+    std::cerr << "inet_ntop failed" << std::endl;
+  }
+
+  return str_addr;
+}
+
 int main(int argc, char **argv)
 {
 #ifdef ANDROID
   // This test can cause intermittent oranges on the builders on Linux
   CHECK_ENVIRONMENT_FLAG("MOZ_WEBRTC_TESTS")
 #endif
 
   g_turn_server = get_environment("TURN_SERVER_ADDRESS");
   g_turn_user = get_environment("TURN_SERVER_USER");
   g_turn_password = get_environment("TURN_SERVER_PASSWORD");
 
+
   if (g_turn_server.empty() ||
       g_turn_user.empty(),
       g_turn_password.empty()) {
     printf(
         "Set TURN_SERVER_ADDRESS, TURN_SERVER_USER, and TURN_SERVER_PASSWORD\n"
         "environment variables to run this test\n");
     g_turn_server="";
   }
@@ -3099,16 +3142,22 @@ int main(int argc, char **argv)
     g_stun_server_hostname = "";
     g_turn_server = "";
   }
 
   test_utils = new MtransportTestUtils();
   NSS_NoDB_Init(nullptr);
   NSS_SetDomesticPolicy();
 
+  // If only a STUN server FQDN was provided, look up its IP address for the
+  // address-only tests.
+  if (g_stun_server_address.empty() && !g_stun_server_hostname.empty()) {
+    g_stun_server_address = Resolve(g_stun_server_hostname, AF_INET);
+  }
+
   // Start the tests
   ::testing::InitGoogleTest(&argc, argv);
 
   ::testing::TestEventListeners& listeners =
         ::testing::UnitTest::GetInstance()->listeners();
   // Adds a listener to the end.  Google Test takes the ownership.
 
   listeners.Append(new test::RingbufferDumper(test_utils));
--- a/media/mtransport/third_party/nICEr/src/net/nr_resolver.h
+++ b/media/mtransport/third_party/nICEr/src/net/nr_resolver.h
@@ -37,17 +37,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 #define _nr_resolver_h
 
 #include "transport_addr.h"
 
 #define NR_RESOLVE_PROTOCOL_STUN 1
 #define NR_RESOLVE_PROTOCOL_TURN 2
 
 typedef struct nr_resolver_resource_ {
-  char *domain_name;
+  const char *domain_name;
   UINT2 port;
   int stun_turn;
   UCHAR transport_protocol;
   UCHAR address_family;
 } nr_resolver_resource;
 
 typedef struct nr_resolver_vtbl_ {
   int (*destroy)(void **obj);