netwerk/test/TestIDN.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 "nsIIDNService.h"
michael@0 8 #include "nsCOMPtr.h"
michael@0 9 #include "nsIServiceManager.h"
michael@0 10 #include "nsServiceManagerUtils.h"
michael@0 11 #include "nsNetCID.h"
michael@0 12 #include "nsStringAPI.h"
michael@0 13
michael@0 14 int main(int argc, char **argv) {
michael@0 15 if (test_common_init(&argc, &argv) != 0)
michael@0 16 return -1;
michael@0 17
michael@0 18 // Test case from RFC 3492 (7.1 - Simplified Chinese)
michael@0 19 const char plain[] =
michael@0 20 "\xE4\xBB\x96\xE4\xBB\xAC\xE4\xB8\xBA\xE4\xBB\x80\xE4\xB9\x88\xE4\xB8\x8D\xE8\xAF\xB4\xE4\xB8\xAD\xE6\x96\x87";
michael@0 21 const char encoded[] = "xn--ihqwcrb4cv8a8dqg056pqjye";
michael@0 22
michael@0 23 nsCOMPtr<nsIIDNService> converter = do_GetService(NS_IDNSERVICE_CONTRACTID);
michael@0 24 NS_ASSERTION(converter, "idnSDK not installed!");
michael@0 25 if (converter) {
michael@0 26 nsAutoCString buf;
michael@0 27 nsresult rv = converter->ConvertUTF8toACE(NS_LITERAL_CSTRING(plain), buf);
michael@0 28 NS_ASSERTION(NS_SUCCEEDED(rv), "error ConvertUTF8toACE");
michael@0 29 NS_ASSERTION(buf.Equals(NS_LITERAL_CSTRING(encoded)),
michael@0 30 "encode result incorrect");
michael@0 31 printf("encoded = %s\n", buf.get());
michael@0 32
michael@0 33 buf.Truncate();
michael@0 34 rv = converter->ConvertACEtoUTF8(NS_LITERAL_CSTRING(encoded), buf);
michael@0 35 NS_ASSERTION(NS_SUCCEEDED(rv), "error ConvertACEtoUTF8");
michael@0 36 NS_ASSERTION(buf.Equals(NS_LITERAL_CSTRING(plain)),
michael@0 37 "decode result incorrect");
michael@0 38 printf("decoded = ");
michael@0 39 NS_ConvertUTF8toUTF16 utf(buf);
michael@0 40 const char16_t *u = utf.get();
michael@0 41 for (int i = 0; u[i]; i++) {
michael@0 42 printf("U+%.4X ", u[i]);
michael@0 43 }
michael@0 44 printf("\n");
michael@0 45
michael@0 46 bool isAce;
michael@0 47 rv = converter->IsACE(NS_LITERAL_CSTRING("www.xn--ihqwcrb4cv8a8dqg056pqjye.com"), &isAce);
michael@0 48 NS_ASSERTION(NS_SUCCEEDED(rv), "error IsACE");
michael@0 49 NS_ASSERTION(isAce, "IsACE incorrect result");
michael@0 50 }
michael@0 51 return 0;
michael@0 52 }

mercurial