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