PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/tests/llcapabilitylistener_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 271 lines | 181 code | 15 blank | 75 comment | 2 complexity | 1ecbb26098d63c17ef55d3b280fef418 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llcapabilitylistener_test.cpp
  3. * @author Nat Goodspeed
  4. * @date 2008-12-31
  5. * @brief Test for llcapabilitylistener.cpp.
  6. *
  7. * $LicenseInfo:firstyear=2008&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. // Own header
  31. #include "../llcapabilitylistener.h"
  32. // STL headers
  33. #include <stdexcept>
  34. #include <map>
  35. #include <vector>
  36. // std headers
  37. // external library headers
  38. #include "boost/bind.hpp"
  39. // other Linden headers
  40. #include "../test/lltut.h"
  41. #include "../llcapabilityprovider.h"
  42. #include "lluuid.h"
  43. #include "tests/networkio.h"
  44. #include "tests/commtest.h"
  45. #include "tests/wrapllerrs.h"
  46. #include "message.h"
  47. #include "stringize.h"
  48. #if defined(LL_WINDOWS)
  49. #pragma warning(disable: 4355) // using 'this' in base-class ctor initializer expr
  50. #endif
  51. /*****************************************************************************
  52. * TestCapabilityProvider
  53. *****************************************************************************/
  54. struct TestCapabilityProvider: public LLCapabilityProvider
  55. {
  56. TestCapabilityProvider(const LLHost& host):
  57. mHost(host)
  58. {}
  59. std::string getCapability(const std::string& cap) const
  60. {
  61. CapMap::const_iterator found = mCaps.find(cap);
  62. if (found != mCaps.end())
  63. return found->second;
  64. // normal LLViewerRegion lookup failure mode
  65. return "";
  66. }
  67. void setCapability(const std::string& cap, const std::string& url)
  68. {
  69. mCaps[cap] = url;
  70. }
  71. const LLHost& getHost() const { return mHost; }
  72. std::string getDescription() const { return "TestCapabilityProvider"; }
  73. LLHost mHost;
  74. typedef std::map<std::string, std::string> CapMap;
  75. CapMap mCaps;
  76. };
  77. /*****************************************************************************
  78. * Dummy LLMessageSystem methods
  79. *****************************************************************************/
  80. /*==========================================================================*|
  81. // This doesn't work because we're already linking in llmessage.a, and we get
  82. // duplicate-symbol errors from the linker. Perhaps if I wanted to go through
  83. // the exercise of providing dummy versions of every single symbol defined in
  84. // message.o -- maybe some day.
  85. typedef std::vector< std::pair<std::string, std::string> > StringPairVector;
  86. StringPairVector call_history;
  87. S32 LLMessageSystem::sendReliable(const LLHost& host)
  88. {
  89. call_history.push_back(StringPairVector::value_type("sendReliable", stringize(host)));
  90. return 0;
  91. }
  92. |*==========================================================================*/
  93. /*****************************************************************************
  94. * TUT
  95. *****************************************************************************/
  96. namespace tut
  97. {
  98. struct llcapears_data: public commtest_data
  99. {
  100. TestCapabilityProvider provider;
  101. LLCapabilityListener regionListener;
  102. LLEventPump& regionPump;
  103. llcapears_data():
  104. provider(host),
  105. regionListener("testCapabilityListener", NULL, provider, LLUUID(), LLUUID()),
  106. regionPump(regionListener.getCapAPI())
  107. {
  108. LLCurl::initClass();
  109. provider.setCapability("good", server + "capability-test");
  110. provider.setCapability("fail", server + "fail");
  111. }
  112. };
  113. typedef test_group<llcapears_data> llcapears_group;
  114. typedef llcapears_group::object llcapears_object;
  115. llcapears_group llsdmgr("llcapabilitylistener");
  116. template<> template<>
  117. void llcapears_object::test<1>()
  118. {
  119. LLSD request, body;
  120. body["data"] = "yes";
  121. request["payload"] = body;
  122. request["reply"] = replyPump.getName();
  123. request["error"] = errorPump.getName();
  124. std::string threw;
  125. try
  126. {
  127. WrapLL_ERRS capture;
  128. regionPump.post(request);
  129. }
  130. catch (const WrapLL_ERRS::FatalException& e)
  131. {
  132. threw = e.what();
  133. }
  134. ensure_contains("missing capability name", threw, "without 'message' key");
  135. }
  136. template<> template<>
  137. void llcapears_object::test<2>()
  138. {
  139. LLSD request, body;
  140. body["data"] = "yes";
  141. request["message"] = "good";
  142. request["payload"] = body;
  143. request["reply"] = replyPump.getName();
  144. request["error"] = errorPump.getName();
  145. regionPump.post(request);
  146. ensure("got response", netio.pump());
  147. ensure("success response", success);
  148. ensure_equals(result.asString(), "success");
  149. body["status"] = 499;
  150. body["reason"] = "custom error message";
  151. request["message"] = "fail";
  152. request["payload"] = body;
  153. regionPump.post(request);
  154. ensure("got response", netio.pump());
  155. ensure("failure response", ! success);
  156. ensure_equals(result["status"].asInteger(), body["status"].asInteger());
  157. ensure_equals(result["reason"].asString(), body["reason"].asString());
  158. }
  159. template<> template<>
  160. void llcapears_object::test<3>()
  161. {
  162. LLSD request, body;
  163. body["data"] = "yes";
  164. request["message"] = "unknown";
  165. request["payload"] = body;
  166. request["reply"] = replyPump.getName();
  167. request["error"] = errorPump.getName();
  168. std::string threw;
  169. try
  170. {
  171. WrapLL_ERRS capture;
  172. regionPump.post(request);
  173. }
  174. catch (const WrapLL_ERRS::FatalException& e)
  175. {
  176. threw = e.what();
  177. }
  178. ensure_contains("bad capability name", threw, "unsupported capability");
  179. }
  180. struct TestMapper: public LLCapabilityListener::CapabilityMapper
  181. {
  182. // Instantiator gets to specify whether mapper expects a reply.
  183. // I'd really like to be able to test CapabilityMapper::buildMessage()
  184. // functionality, too, but -- even though LLCapabilityListener accepts
  185. // the LLMessageSystem* that it passes to CapabilityMapper --
  186. // LLMessageSystem::sendReliable(const LLHost&) isn't virtual, so it's
  187. // not helpful to pass a subclass instance. I suspect that making any
  188. // LLMessageSystem methods virtual would provoke howls of outrage,
  189. // given how heavily it's used. Nor can I just provide a local
  190. // definition of LLMessageSystem::sendReliable(const LLHost&) because
  191. // we're already linking in the rest of message.o via llmessage.a, and
  192. // that produces duplicate-symbol link errors.
  193. TestMapper(const std::string& replyMessage = std::string()):
  194. LLCapabilityListener::CapabilityMapper("test", replyMessage)
  195. {}
  196. virtual void buildMessage(LLMessageSystem* msg,
  197. const LLUUID& agentID,
  198. const LLUUID& sessionID,
  199. const std::string& capabilityName,
  200. const LLSD& payload) const
  201. {
  202. msg->newMessageFast(_PREHASH_SetStartLocationRequest);
  203. msg->nextBlockFast( _PREHASH_AgentData);
  204. msg->addUUIDFast(_PREHASH_AgentID, agentID);
  205. msg->addUUIDFast(_PREHASH_SessionID, sessionID);
  206. msg->nextBlockFast( _PREHASH_StartLocationData);
  207. // corrected by sim
  208. msg->addStringFast(_PREHASH_SimName, "");
  209. msg->addU32Fast(_PREHASH_LocationID, payload["HomeLocation"]["LocationId"].asInteger());
  210. /*==========================================================================*|
  211. msg->addVector3Fast(_PREHASH_LocationPos,
  212. ll_vector3_from_sdmap(payload["HomeLocation"]["LocationPos"]));
  213. msg->addVector3Fast(_PREHASH_LocationLookAt,
  214. ll_vector3_from_sdmap(payload["HomeLocation"]["LocationLookAt"]));
  215. |*==========================================================================*/
  216. }
  217. };
  218. template<> template<>
  219. void llcapears_object::test<4>()
  220. {
  221. TestMapper testMapper("WantReply");
  222. LLSD request, body;
  223. body["data"] = "yes";
  224. request["message"] = "test";
  225. request["payload"] = body;
  226. request["reply"] = replyPump.getName();
  227. request["error"] = errorPump.getName();
  228. std::string threw;
  229. try
  230. {
  231. WrapLL_ERRS capture;
  232. regionPump.post(request);
  233. }
  234. catch (const WrapLL_ERRS::FatalException& e)
  235. {
  236. threw = e.what();
  237. }
  238. ensure_contains("capability mapper wants reply", threw, "unimplemented support for reply message");
  239. }
  240. template<> template<>
  241. void llcapears_object::test<5>()
  242. {
  243. TestMapper testMapper;
  244. std::string threw;
  245. try
  246. {
  247. TestMapper testMapper2;
  248. }
  249. catch (const std::runtime_error& e)
  250. {
  251. threw = e.what();
  252. }
  253. ensure_contains("no dup cap mapper", threw, "DupCapMapper");
  254. }
  255. }