netwerk/test/TestIDN.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/TestIDN.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,52 @@
     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 "nsIIDNService.h"
    1.11 +#include "nsCOMPtr.h"
    1.12 +#include "nsIServiceManager.h"
    1.13 +#include "nsServiceManagerUtils.h"
    1.14 +#include "nsNetCID.h"
    1.15 +#include "nsStringAPI.h"
    1.16 +
    1.17 +int main(int argc, char **argv) {
    1.18 +    if (test_common_init(&argc, &argv) != 0)
    1.19 +        return -1;
    1.20 +
    1.21 +    // Test case from RFC 3492 (7.1 - Simplified Chinese)
    1.22 +    const char plain[] =
    1.23 +         "\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";
    1.24 +    const char encoded[] = "xn--ihqwcrb4cv8a8dqg056pqjye";
    1.25 +
    1.26 +    nsCOMPtr<nsIIDNService> converter = do_GetService(NS_IDNSERVICE_CONTRACTID);
    1.27 +    NS_ASSERTION(converter, "idnSDK not installed!");
    1.28 +    if (converter) {
    1.29 +        nsAutoCString buf;
    1.30 +        nsresult rv = converter->ConvertUTF8toACE(NS_LITERAL_CSTRING(plain), buf);
    1.31 +        NS_ASSERTION(NS_SUCCEEDED(rv), "error ConvertUTF8toACE");
    1.32 +        NS_ASSERTION(buf.Equals(NS_LITERAL_CSTRING(encoded)), 
    1.33 +                     "encode result incorrect");
    1.34 +        printf("encoded = %s\n", buf.get());
    1.35 +
    1.36 +        buf.Truncate();
    1.37 +        rv = converter->ConvertACEtoUTF8(NS_LITERAL_CSTRING(encoded), buf);
    1.38 +        NS_ASSERTION(NS_SUCCEEDED(rv), "error ConvertACEtoUTF8");
    1.39 +        NS_ASSERTION(buf.Equals(NS_LITERAL_CSTRING(plain)), 
    1.40 +                     "decode result incorrect");
    1.41 +        printf("decoded = ");
    1.42 +        NS_ConvertUTF8toUTF16 utf(buf);
    1.43 +        const char16_t *u = utf.get();
    1.44 +        for (int i = 0; u[i]; i++) {
    1.45 +          printf("U+%.4X ", u[i]);
    1.46 +        }
    1.47 +        printf("\n");
    1.48 +
    1.49 +        bool isAce;
    1.50 +        rv = converter->IsACE(NS_LITERAL_CSTRING("www.xn--ihqwcrb4cv8a8dqg056pqjye.com"), &isAce);
    1.51 +        NS_ASSERTION(NS_SUCCEEDED(rv), "error IsACE");
    1.52 +        NS_ASSERTION(isAce, "IsACE incorrect result");
    1.53 +    }
    1.54 +    return 0;
    1.55 +}

mercurial