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

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

mercurial