netwerk/test/TestDNS.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/TestDNS.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "TestCommon.h"
     1.9 +#include <stdio.h>
    1.10 +#include <stdlib.h>
    1.11 +#include "nsIServiceManager.h"
    1.12 +#include "nsPIDNSService.h"
    1.13 +#include "nsIDNSListener.h"
    1.14 +#include "nsIDNSRecord.h"
    1.15 +#include "nsICancelable.h"
    1.16 +#include "nsCOMPtr.h"
    1.17 +#include "nsStringAPI.h"
    1.18 +#include "nsNetCID.h"
    1.19 +#include "prinrval.h"
    1.20 +#include "prthread.h"
    1.21 +#include "prnetdb.h"
    1.22 +#include "nsXPCOM.h"
    1.23 +#include "nsServiceManagerUtils.h"
    1.24 +
    1.25 +class myDNSListener : public nsIDNSListener
    1.26 +{
    1.27 +public:
    1.28 +    NS_DECL_THREADSAFE_ISUPPORTS
    1.29 +
    1.30 +    myDNSListener(const char *host, int32_t index)
    1.31 +        : mHost(host)
    1.32 +        , mIndex(index) {}
    1.33 +    virtual ~myDNSListener() {}
    1.34 +
    1.35 +    NS_IMETHOD OnLookupComplete(nsICancelable *request,
    1.36 +                                nsIDNSRecord  *rec,
    1.37 +                                nsresult       status)
    1.38 +    {
    1.39 +        printf("%d: OnLookupComplete called [host=%s status=%x rec=%p]\n",
    1.40 +            mIndex, mHost.get(), static_cast<uint32_t>(status), (void*)rec);
    1.41 +
    1.42 +        if (NS_SUCCEEDED(status)) {
    1.43 +            nsAutoCString buf;
    1.44 +
    1.45 +            rec->GetCanonicalName(buf);
    1.46 +            printf("%d: canonname=%s\n", mIndex, buf.get());
    1.47 +
    1.48 +            bool hasMore;
    1.49 +            while (NS_SUCCEEDED(rec->HasMore(&hasMore)) && hasMore) {
    1.50 +                rec->GetNextAddrAsString(buf);
    1.51 +                printf("%d: => %s\n", mIndex, buf.get());
    1.52 +            }
    1.53 +        }
    1.54 +
    1.55 +        return NS_OK;
    1.56 +    }
    1.57 +
    1.58 +private:
    1.59 +    nsCString mHost;
    1.60 +    int32_t   mIndex;
    1.61 +};
    1.62 +
    1.63 +NS_IMPL_ISUPPORTS(myDNSListener, nsIDNSListener)
    1.64 +
    1.65 +static bool IsAscii(const char *s)
    1.66 +{
    1.67 +  for (; *s; ++s) {
    1.68 +    if (*s & 0x80)
    1.69 +      return false;
    1.70 +  }
    1.71 +
    1.72 +  return true;
    1.73 +}
    1.74 +
    1.75 +int main(int argc, char **argv)
    1.76 +{
    1.77 +    if (test_common_init(&argc, &argv) != 0)
    1.78 +        return -1;
    1.79 +
    1.80 +    int sleepLen = 10; // default: 10 seconds
    1.81 +
    1.82 +    if (argc == 1) {
    1.83 +        printf("usage: TestDNS [-N] hostname1 [hostname2 ...]\n");
    1.84 +        return -1;
    1.85 +    }
    1.86 +
    1.87 +    {
    1.88 +        nsCOMPtr<nsIServiceManager> servMan;
    1.89 +        NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
    1.90 +
    1.91 +        nsCOMPtr<nsPIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
    1.92 +        if (!dns)
    1.93 +            return -1;
    1.94 +
    1.95 +        if (argv[1][0] == '-') {
    1.96 +            sleepLen = atoi(argv[1]+1);
    1.97 +            argv++;
    1.98 +            argc--;
    1.99 +        }
   1.100 +
   1.101 +        for (int j=0; j<2; ++j) {
   1.102 +            for (int i=1; i<argc; ++i) {
   1.103 +                // assume non-ASCII input is given in the native charset 
   1.104 +                nsAutoCString hostBuf;
   1.105 +                if (IsAscii(argv[i]))
   1.106 +                    hostBuf.Assign(argv[i]);
   1.107 +                else
   1.108 +                    hostBuf = NS_ConvertUTF16toUTF8(NS_ConvertASCIItoUTF16(argv[i]));
   1.109 +
   1.110 +                nsCOMPtr<nsIDNSListener> listener = new myDNSListener(argv[i], i);
   1.111 +
   1.112 +                nsCOMPtr<nsICancelable> req;
   1.113 +                nsresult rv = dns->AsyncResolve(hostBuf,
   1.114 +                                                nsIDNSService::RESOLVE_CANONICAL_NAME,
   1.115 +                                                listener, nullptr, getter_AddRefs(req));
   1.116 +                if (NS_FAILED(rv))
   1.117 +                    printf("### AsyncResolve failed [rv=%x]\n",
   1.118 +                           static_cast<uint32_t>(rv));
   1.119 +            }
   1.120 +
   1.121 +            printf("main thread sleeping for %d seconds...\n", sleepLen);
   1.122 +            PR_Sleep(PR_SecondsToInterval(sleepLen));
   1.123 +        }
   1.124 +
   1.125 +        printf("shutting down main thread...\n");
   1.126 +        dns->Shutdown();
   1.127 +    }
   1.128 +
   1.129 +    NS_ShutdownXPCOM(nullptr);
   1.130 +    return 0;
   1.131 +}

mercurial