TestIDN.cpp 2.0 KB

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