Back to index

lightning-sunbird  0.9+nobinonly
TestIDN.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is i-DNS.net International, Inc. code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * i-DNS.net International.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s): darin@netscape.com,
00022  *                 gagan@netscape.com,
00023  *                 william.tan@i-dns.net
00024  *
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "TestCommon.h"
00041 #include <stdio.h>
00042 #include "nsIIDNService.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsIServiceManager.h"
00045 #include "nsNetCID.h"
00046 #include "nsString.h"
00047 
00048 int main(int argc, char **argv) {
00049     if (test_common_init(&argc, &argv) != 0)
00050         return -1;
00051 
00052     // Test case from RFC 3492 (7.1 - Simplified Chinese)
00053     const char plain[] =
00054          "\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";
00055     const char encoded[] = "xn--ihqwcrb4cv8a8dqg056pqjye";
00056 
00057     nsCOMPtr<nsIIDNService> converter = do_GetService(NS_IDNSERVICE_CONTRACTID);
00058     NS_ASSERTION(converter, "idnSDK not installed!");
00059     if (converter) {
00060         nsCAutoString buf;
00061         nsresult rv = converter->ConvertUTF8toACE(NS_LITERAL_CSTRING(plain), buf);
00062         NS_ASSERTION(NS_SUCCEEDED(rv), "error ConvertUTF8toACE");
00063         NS_ASSERTION(buf.Equals(NS_LITERAL_CSTRING(encoded), nsCaseInsensitiveCStringComparator()), 
00064                      "encode result incorrect");
00065         printf("encoded = %s\n", buf.get());
00066 
00067         buf.Truncate();
00068         rv = converter->ConvertACEtoUTF8(NS_LITERAL_CSTRING(encoded), buf);
00069         NS_ASSERTION(NS_SUCCEEDED(rv), "error ConvertACEtoUTF8");
00070         NS_ASSERTION(buf.Equals(NS_LITERAL_CSTRING(plain), nsCaseInsensitiveCStringComparator()), 
00071                      "decode result incorrect");
00072         printf("decoded = ");
00073         NS_ConvertUTF8toUCS2 u(buf);
00074         for (int i = 0; u[i]; i++) {
00075           printf("U+%.4X ", u[i]);
00076         }
00077         printf("\n");
00078 
00079         PRBool isAce;
00080         rv = converter->IsACE(NS_LITERAL_CSTRING("www.xn--ihqwcrb4cv8a8dqg056pqjye.com"), &isAce);
00081         NS_ASSERTION(NS_SUCCEEDED(rv), "error IsACE");
00082         NS_ASSERTION(isAce, "IsACE incorrect result");
00083     }
00084     return 0;
00085 }