PropertiesTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "TestCommon.h"
  6. #include "mozilla/Sprintf.h"
  7. #include "nsXPCOM.h"
  8. #include "nsStringAPI.h"
  9. #include "nsIPersistentProperties2.h"
  10. #include "nsIServiceManager.h"
  11. #include "nsIComponentRegistrar.h"
  12. #include "nsIURL.h"
  13. #include "nsNetCID.h"
  14. #include "nsIChannel.h"
  15. #include "nsIComponentManager.h"
  16. #include <stdio.h>
  17. #include "nsComponentManagerUtils.h"
  18. #include "nsServiceManagerUtils.h"
  19. #include "nsISimpleEnumerator.h"
  20. #include "nsIScriptSecurityManager.h"
  21. #include "nsILoadInfo.h"
  22. #include "nsNetUtil.h"
  23. #define TEST_URL "resource:/res/test.properties"
  24. static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
  25. /***************************************************************************/
  26. int
  27. main(int argc, char* argv[])
  28. {
  29. if (test_common_init(&argc, &argv) != 0)
  30. return -1;
  31. nsresult ret;
  32. nsCOMPtr<nsIServiceManager> servMan;
  33. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  34. nsIInputStream* in = nullptr;
  35. nsCOMPtr<nsIScriptSecurityManager> secman =
  36. do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &ret);
  37. if (NS_FAILED(ret)) return 1;
  38. nsCOMPtr<nsIPrincipal> systemPrincipal;
  39. ret = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
  40. if (NS_FAILED(ret)) return 1;
  41. nsCOMPtr<nsIURI> uri;
  42. ret = NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING(TEST_URL));
  43. if (NS_FAILED(ret)) return 1;
  44. nsIChannel *channel = nullptr;
  45. ret = NS_NewChannel(&channel,
  46. uri,
  47. systemPrincipal,
  48. nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
  49. nsIContentPolicy::TYPE_OTHER);
  50. if (NS_FAILED(ret)) return 1;
  51. ret = channel->Open2(&in);
  52. if (NS_FAILED(ret)) return 1;
  53. nsIPersistentProperties* props;
  54. ret = CallCreateInstance(kPersistentPropertiesCID, &props);
  55. if (NS_FAILED(ret) || (!props)) {
  56. printf("create nsIPersistentProperties failed\n");
  57. return 1;
  58. }
  59. ret = props->Load(in);
  60. if (NS_FAILED(ret)) {
  61. printf("cannot load properties\n");
  62. return 1;
  63. }
  64. int i = 1;
  65. while (true) {
  66. char name[16];
  67. name[0] = 0;
  68. SprintfLiteral(name, "%d", i);
  69. nsAutoString v;
  70. ret = props->GetStringProperty(nsDependentCString(name), v);
  71. if (NS_FAILED(ret) || (!v.Length())) {
  72. break;
  73. }
  74. printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
  75. i++;
  76. }
  77. nsCOMPtr<nsISimpleEnumerator> propEnum;
  78. ret = props->Enumerate(getter_AddRefs(propEnum));
  79. if (NS_FAILED(ret)) {
  80. printf("cannot enumerate properties\n");
  81. return 1;
  82. }
  83. printf("\nKey\tValue\n");
  84. printf( "---\t-----\n");
  85. bool hasMore;
  86. while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
  87. hasMore) {
  88. nsCOMPtr<nsISupports> sup;
  89. ret = propEnum->GetNext(getter_AddRefs(sup));
  90. nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
  91. if (NS_FAILED(ret)) {
  92. printf("failed to get current item\n");
  93. return 1;
  94. }
  95. nsAutoCString key;
  96. nsAutoString value;
  97. ret = propElem->GetKey(key);
  98. if (NS_FAILED(ret)) {
  99. printf("failed to get current element's key\n");
  100. return 1;
  101. }
  102. ret = propElem->GetValue(value);
  103. if (NS_FAILED(ret)) {
  104. printf("failed to get current element's value\n");
  105. return 1;
  106. }
  107. printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
  108. }
  109. return 0;
  110. }