netwerk/test/TestDNS.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "TestCommon.h"
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 #include "nsIServiceManager.h"
     9 #include "nsPIDNSService.h"
    10 #include "nsIDNSListener.h"
    11 #include "nsIDNSRecord.h"
    12 #include "nsICancelable.h"
    13 #include "nsCOMPtr.h"
    14 #include "nsStringAPI.h"
    15 #include "nsNetCID.h"
    16 #include "prinrval.h"
    17 #include "prthread.h"
    18 #include "prnetdb.h"
    19 #include "nsXPCOM.h"
    20 #include "nsServiceManagerUtils.h"
    22 class myDNSListener : public nsIDNSListener
    23 {
    24 public:
    25     NS_DECL_THREADSAFE_ISUPPORTS
    27     myDNSListener(const char *host, int32_t index)
    28         : mHost(host)
    29         , mIndex(index) {}
    30     virtual ~myDNSListener() {}
    32     NS_IMETHOD OnLookupComplete(nsICancelable *request,
    33                                 nsIDNSRecord  *rec,
    34                                 nsresult       status)
    35     {
    36         printf("%d: OnLookupComplete called [host=%s status=%x rec=%p]\n",
    37             mIndex, mHost.get(), static_cast<uint32_t>(status), (void*)rec);
    39         if (NS_SUCCEEDED(status)) {
    40             nsAutoCString buf;
    42             rec->GetCanonicalName(buf);
    43             printf("%d: canonname=%s\n", mIndex, buf.get());
    45             bool hasMore;
    46             while (NS_SUCCEEDED(rec->HasMore(&hasMore)) && hasMore) {
    47                 rec->GetNextAddrAsString(buf);
    48                 printf("%d: => %s\n", mIndex, buf.get());
    49             }
    50         }
    52         return NS_OK;
    53     }
    55 private:
    56     nsCString mHost;
    57     int32_t   mIndex;
    58 };
    60 NS_IMPL_ISUPPORTS(myDNSListener, nsIDNSListener)
    62 static bool IsAscii(const char *s)
    63 {
    64   for (; *s; ++s) {
    65     if (*s & 0x80)
    66       return false;
    67   }
    69   return true;
    70 }
    72 int main(int argc, char **argv)
    73 {
    74     if (test_common_init(&argc, &argv) != 0)
    75         return -1;
    77     int sleepLen = 10; // default: 10 seconds
    79     if (argc == 1) {
    80         printf("usage: TestDNS [-N] hostname1 [hostname2 ...]\n");
    81         return -1;
    82     }
    84     {
    85         nsCOMPtr<nsIServiceManager> servMan;
    86         NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
    88         nsCOMPtr<nsPIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
    89         if (!dns)
    90             return -1;
    92         if (argv[1][0] == '-') {
    93             sleepLen = atoi(argv[1]+1);
    94             argv++;
    95             argc--;
    96         }
    98         for (int j=0; j<2; ++j) {
    99             for (int i=1; i<argc; ++i) {
   100                 // assume non-ASCII input is given in the native charset 
   101                 nsAutoCString hostBuf;
   102                 if (IsAscii(argv[i]))
   103                     hostBuf.Assign(argv[i]);
   104                 else
   105                     hostBuf = NS_ConvertUTF16toUTF8(NS_ConvertASCIItoUTF16(argv[i]));
   107                 nsCOMPtr<nsIDNSListener> listener = new myDNSListener(argv[i], i);
   109                 nsCOMPtr<nsICancelable> req;
   110                 nsresult rv = dns->AsyncResolve(hostBuf,
   111                                                 nsIDNSService::RESOLVE_CANONICAL_NAME,
   112                                                 listener, nullptr, getter_AddRefs(req));
   113                 if (NS_FAILED(rv))
   114                     printf("### AsyncResolve failed [rv=%x]\n",
   115                            static_cast<uint32_t>(rv));
   116             }
   118             printf("main thread sleeping for %d seconds...\n", sleepLen);
   119             PR_Sleep(PR_SecondsToInterval(sleepLen));
   120         }
   122         printf("shutting down main thread...\n");
   123         dns->Shutdown();
   124     }
   126     NS_ShutdownXPCOM(nullptr);
   127     return 0;
   128 }

mercurial