PageRenderTime 34ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/tests/llareslistener_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 217 lines | 154 code | 12 blank | 51 comment | 2 complexity | 135574428e6ea116da1eb99d9d1f82b3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llareslistener_test.cpp
  3. * @author Mark Palange
  4. * @date 2009-02-26
  5. * @brief Tests of llareslistener.h.
  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. #if LL_WINDOWS
  29. #pragma warning (disable : 4355) // 'this' used in initializer list: yes, intentionally
  30. #endif
  31. // Precompiled header
  32. #include "linden_common.h"
  33. // associated header
  34. #include "../llareslistener.h"
  35. // STL headers
  36. #include <iostream>
  37. // std headers
  38. // external library headers
  39. #include <boost/bind.hpp>
  40. // other Linden headers
  41. #include "llsd.h"
  42. #include "llares.h"
  43. #include "../test/lltut.h"
  44. #include "llevents.h"
  45. #include "tests/wrapllerrs.h"
  46. /*****************************************************************************
  47. * Dummy stuff
  48. *****************************************************************************/
  49. LLAres::LLAres():
  50. // Simulate this much of the real LLAres constructor: we need an
  51. // LLAresListener instance.
  52. mListener(new LLAresListener("LLAres", this))
  53. {}
  54. LLAres::~LLAres() {}
  55. void LLAres::rewriteURI(const std::string &uri,
  56. LLAres::UriRewriteResponder *resp)
  57. {
  58. // This is the only LLAres method I chose to implement.
  59. // The effect is that LLAres returns immediately with
  60. // a result that is equal to the input uri.
  61. std::vector<std::string> result;
  62. result.push_back(uri);
  63. resp->rewriteResult(result);
  64. }
  65. LLAres::QueryResponder::~QueryResponder() {}
  66. void LLAres::QueryResponder::queryError(int) {}
  67. void LLAres::QueryResponder::queryResult(char const*, size_t) {}
  68. LLQueryResponder::LLQueryResponder() {}
  69. void LLQueryResponder::queryResult(char const*, size_t) {}
  70. void LLQueryResponder::querySuccess() {}
  71. void LLAres::UriRewriteResponder::queryError(int) {}
  72. void LLAres::UriRewriteResponder::querySuccess() {}
  73. void LLAres::UriRewriteResponder::rewriteResult(const std::vector<std::string>& uris) {}
  74. /*****************************************************************************
  75. * TUT
  76. *****************************************************************************/
  77. namespace tut
  78. {
  79. struct data
  80. {
  81. LLAres dummyAres;
  82. };
  83. typedef test_group<data> llareslistener_group;
  84. typedef llareslistener_group::object object;
  85. llareslistener_group llareslistenergrp("llareslistener");
  86. struct ResponseCallback
  87. {
  88. std::vector<std::string> mURIs;
  89. bool operator()(const LLSD& response)
  90. {
  91. mURIs.clear();
  92. for (LLSD::array_const_iterator ri(response.beginArray()), rend(response.endArray());
  93. ri != rend; ++ri)
  94. {
  95. mURIs.push_back(*ri);
  96. }
  97. return false;
  98. }
  99. };
  100. template<> template<>
  101. void object::test<1>()
  102. {
  103. set_test_name("test event");
  104. // Tests the success and failure cases, since they both use
  105. // the same code paths in the LLAres responder.
  106. ResponseCallback response;
  107. std::string pumpname("trigger");
  108. // Since we're asking LLEventPumps to obtain() the pump by the desired
  109. // name, it will persist beyond the current scope, so ensure we
  110. // disconnect from it when 'response' goes away.
  111. LLTempBoundListener temp(
  112. LLEventPumps::instance().obtain(pumpname).listen("rewriteURIresponse",
  113. boost::bind(&ResponseCallback::operator(), &response, _1)));
  114. // Now build an LLSD request that will direct its response events to
  115. // that pump.
  116. const std::string testURI("login.bar.com");
  117. LLSD request;
  118. request["op"] = "rewriteURI";
  119. request["uri"] = testURI;
  120. request["reply"] = pumpname;
  121. LLEventPumps::instance().obtain("LLAres").post(request);
  122. ensure_equals(response.mURIs.size(), 1);
  123. ensure_equals(response.mURIs.front(), testURI);
  124. }
  125. template<> template<>
  126. void object::test<2>()
  127. {
  128. set_test_name("bad op");
  129. WrapLL_ERRS capture;
  130. LLSD request;
  131. request["op"] = "foo";
  132. std::string threw;
  133. try
  134. {
  135. LLEventPumps::instance().obtain("LLAres").post(request);
  136. }
  137. catch (const WrapLL_ERRS::FatalException& e)
  138. {
  139. threw = e.what();
  140. }
  141. ensure_contains("LLAresListener bad op", threw, "bad");
  142. }
  143. template<> template<>
  144. void object::test<3>()
  145. {
  146. set_test_name("bad rewriteURI request");
  147. WrapLL_ERRS capture;
  148. LLSD request;
  149. request["op"] = "rewriteURI";
  150. std::string threw;
  151. try
  152. {
  153. LLEventPumps::instance().obtain("LLAres").post(request);
  154. }
  155. catch (const WrapLL_ERRS::FatalException& e)
  156. {
  157. threw = e.what();
  158. }
  159. ensure_contains("LLAresListener bad req", threw, "missing");
  160. ensure_contains("LLAresListener bad req", threw, "reply");
  161. ensure_contains("LLAresListener bad req", threw, "uri");
  162. }
  163. template<> template<>
  164. void object::test<4>()
  165. {
  166. set_test_name("bad rewriteURI request");
  167. WrapLL_ERRS capture;
  168. LLSD request;
  169. request["op"] = "rewriteURI";
  170. request["reply"] = "nonexistent";
  171. std::string threw;
  172. try
  173. {
  174. LLEventPumps::instance().obtain("LLAres").post(request);
  175. }
  176. catch (const WrapLL_ERRS::FatalException& e)
  177. {
  178. threw = e.what();
  179. }
  180. ensure_contains("LLAresListener bad req", threw, "missing");
  181. ensure_contains("LLAresListener bad req", threw, "uri");
  182. ensure_does_not_contain("LLAresListener bad req", threw, "reply");
  183. }
  184. template<> template<>
  185. void object::test<5>()
  186. {
  187. set_test_name("bad rewriteURI request");
  188. WrapLL_ERRS capture;
  189. LLSD request;
  190. request["op"] = "rewriteURI";
  191. request["uri"] = "foo.bar.com";
  192. std::string threw;
  193. try
  194. {
  195. LLEventPumps::instance().obtain("LLAres").post(request);
  196. }
  197. catch (const WrapLL_ERRS::FatalException& e)
  198. {
  199. threw = e.what();
  200. }
  201. ensure_contains("LLAresListener bad req", threw, "missing");
  202. ensure_contains("LLAresListener bad req", threw, "reply");
  203. ensure_does_not_contain("LLAresListener bad req", threw, "uri");
  204. }
  205. }