PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/tests/llxmlrpclistener_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 250 lines | 171 code | 11 blank | 68 comment | 3 complexity | 22d96e1f1e268f11857de5b23553d90e MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * @file llxmlrpclistener_test.cpp
  3. * @author Nat Goodspeed
  4. * @date 2009-03-20
  5. * @brief Test for llxmlrpclistener.
  6. *
  7. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. // Precompiled header
  29. #include "../llviewerprecompiledheaders.h"
  30. // associated header
  31. #include "../llxmlrpclistener.h"
  32. // STL headers
  33. #include <iomanip>
  34. // std headers
  35. // external library headers
  36. // other Linden headers
  37. #include "../test/lltut.h"
  38. #include "../llxmlrpctransaction.h"
  39. #include "llevents.h"
  40. #include "lleventfilter.h"
  41. #include "llsd.h"
  42. #include "llhost.h"
  43. #include "llcontrol.h"
  44. #include "tests/wrapllerrs.h"
  45. #include "tests/commtest.h"
  46. LLControlGroup gSavedSettings("Global");
  47. /*****************************************************************************
  48. * TUT
  49. *****************************************************************************/
  50. namespace tut
  51. {
  52. struct data
  53. {
  54. data():
  55. pumps(LLEventPumps::instance()),
  56. uri(std::string("http://") +
  57. LLHost("127.0.0.1", commtest_data::getport("PORT")).getString())
  58. {
  59. // These variables are required by machinery used by
  60. // LLXMLRPCTransaction. The values reflect reality for this test
  61. // executable; hopefully these values are correct.
  62. gSavedSettings.declareBOOL("BrowserProxyEnabled", FALSE, "", FALSE); // don't persist
  63. gSavedSettings.declareBOOL("NoVerifySSLCert", TRUE, "", FALSE); // don't persist
  64. }
  65. // LLEventPump listener signature
  66. bool captureReply(const LLSD& r)
  67. {
  68. reply = r;
  69. return false;
  70. }
  71. LLSD reply;
  72. LLEventPumps& pumps;
  73. std::string uri;
  74. };
  75. typedef test_group<data> llxmlrpclistener_group;
  76. typedef llxmlrpclistener_group::object object;
  77. llxmlrpclistener_group llxmlrpclistenergrp("llxmlrpclistener");
  78. template<> template<>
  79. void object::test<1>()
  80. {
  81. set_test_name("request validation");
  82. WrapLL_ERRS capture;
  83. LLSD request;
  84. request["uri"] = uri;
  85. std::string threw;
  86. try
  87. {
  88. pumps.obtain("LLXMLRPCTransaction").post(request);
  89. }
  90. catch (const WrapLL_ERRS::FatalException& e)
  91. {
  92. threw = e.what();
  93. }
  94. ensure_contains("threw exception", threw, "missing params");
  95. ensure_contains("identified missing", threw, "method");
  96. ensure_contains("identified missing", threw, "reply");
  97. }
  98. template<> template<>
  99. void object::test<2>()
  100. {
  101. set_test_name("param types validation");
  102. WrapLL_ERRS capture;
  103. LLSD request;
  104. request["uri"] = uri;
  105. request["method"] = "hello";
  106. request["reply"] = "reply";
  107. LLSD& params(request["params"]);
  108. params["who"]["specifically"] = "world"; // LLXMLRPCListener only handles scalar params
  109. std::string threw;
  110. try
  111. {
  112. pumps.obtain("LLXMLRPCTransaction").post(request);
  113. }
  114. catch (const WrapLL_ERRS::FatalException& e)
  115. {
  116. threw = e.what();
  117. }
  118. ensure_contains("threw exception", threw, "unknown type");
  119. }
  120. template<> template<>
  121. void object::test<3>()
  122. {
  123. set_test_name("success case");
  124. LLSD request;
  125. request["uri"] = uri;
  126. request["method"] = "hello";
  127. request["reply"] = "reply";
  128. LLSD& params(request["params"]);
  129. params["who"] = "world";
  130. // Set up a timeout filter so we don't spin forever waiting.
  131. LLEventTimeout watchdog;
  132. // Connect the timeout filter to the reply pump.
  133. LLTempBoundListener temp(
  134. pumps.obtain("reply").
  135. listen("watchdog", boost::bind(&LLEventTimeout::post, boost::ref(watchdog), _1)));
  136. // Now connect our target listener to the timeout filter.
  137. watchdog.listen("captureReply", boost::bind(&data::captureReply, this, _1));
  138. // Kick off the request...
  139. reply.clear();
  140. pumps.obtain("LLXMLRPCTransaction").post(request);
  141. // Set the timer
  142. F32 timeout(10);
  143. watchdog.eventAfter(timeout, LLSD().with("timeout", 0));
  144. // and pump "mainloop" until we get something, whether from
  145. // LLXMLRPCListener or from the watchdog filter.
  146. LLTimer timer;
  147. F32 start = timer.getElapsedTimeF32();
  148. LLEventPump& mainloop(pumps.obtain("mainloop"));
  149. while (reply.isUndefined())
  150. {
  151. mainloop.post(LLSD());
  152. }
  153. ensure("timeout works", (timer.getElapsedTimeF32() - start) < (timeout + 1));
  154. ensure_equals(reply["responses"]["hi_there"].asString(), "Hello, world!");
  155. }
  156. template<> template<>
  157. void object::test<4>()
  158. {
  159. set_test_name("bogus method");
  160. LLSD request;
  161. request["uri"] = uri;
  162. request["method"] = "goodbye";
  163. request["reply"] = "reply";
  164. LLSD& params(request["params"]);
  165. params["who"] = "world";
  166. // Set up a timeout filter so we don't spin forever waiting.
  167. LLEventTimeout watchdog;
  168. // Connect the timeout filter to the reply pump.
  169. LLTempBoundListener temp(
  170. pumps.obtain("reply").
  171. listen("watchdog", boost::bind(&LLEventTimeout::post, boost::ref(watchdog), _1)));
  172. // Now connect our target listener to the timeout filter.
  173. watchdog.listen("captureReply", boost::bind(&data::captureReply, this, _1));
  174. // Kick off the request...
  175. reply.clear();
  176. pumps.obtain("LLXMLRPCTransaction").post(request);
  177. // Set the timer
  178. F32 timeout(10);
  179. watchdog.eventAfter(timeout, LLSD().with("timeout", 0));
  180. // and pump "mainloop" until we get something, whether from
  181. // LLXMLRPCListener or from the watchdog filter.
  182. LLTimer timer;
  183. F32 start = timer.getElapsedTimeF32();
  184. LLEventPump& mainloop(pumps.obtain("mainloop"));
  185. while (reply.isUndefined())
  186. {
  187. mainloop.post(LLSD());
  188. }
  189. ensure("timeout works", (timer.getElapsedTimeF32() - start) < (timeout + 1));
  190. ensure_equals("XMLRPC error", reply["status"].asString(), "XMLRPCError");
  191. }
  192. template<> template<>
  193. void object::test<5>()
  194. {
  195. set_test_name("bad type");
  196. LLSD request;
  197. request["uri"] = uri;
  198. request["method"] = "getdict";
  199. request["reply"] = "reply";
  200. (void)request["params"];
  201. // Set up a timeout filter so we don't spin forever waiting.
  202. LLEventTimeout watchdog;
  203. // Connect the timeout filter to the reply pump.
  204. LLTempBoundListener temp(
  205. pumps.obtain("reply").
  206. listen("watchdog", boost::bind(&LLEventTimeout::post, boost::ref(watchdog), _1)));
  207. // Now connect our target listener to the timeout filter.
  208. watchdog.listen("captureReply", boost::bind(&data::captureReply, this, _1));
  209. // Kick off the request...
  210. reply.clear();
  211. pumps.obtain("LLXMLRPCTransaction").post(request);
  212. // Set the timer
  213. F32 timeout(10);
  214. watchdog.eventAfter(timeout, LLSD().with("timeout", 0));
  215. // and pump "mainloop" until we get something, whether from
  216. // LLXMLRPCListener or from the watchdog filter.
  217. LLTimer timer;
  218. F32 start = timer.getElapsedTimeF32();
  219. LLEventPump& mainloop(pumps.obtain("mainloop"));
  220. while (reply.isUndefined())
  221. {
  222. mainloop.post(LLSD());
  223. }
  224. ensure("timeout works", (timer.getElapsedTimeF32() - start) < (timeout + 1));
  225. ensure_equals(reply["status"].asString(), "BadType");
  226. ensure_contains("bad type", reply["responses"]["nested_dict"].asString(), "bad XMLRPC type");
  227. }
  228. } // namespace tut
  229. /*****************************************************************************
  230. * Resolve link errors: use real machinery here, since we intend to exchange
  231. * actual XML with a peer process.
  232. *****************************************************************************/
  233. // Including llxmlrpctransaction.cpp drags in the static LLXMLRPCListener
  234. // instantiated there. That's why it works to post requests to the LLEventPump
  235. // named "LLXMLRPCTransaction".
  236. #include "../llxmlrpctransaction.cpp"
  237. #include "llcontrol.cpp"
  238. #include "llxmltree.cpp"
  239. #include "llxmlparser.cpp"