llcapabilitylistener_test.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:10k
源码类别:

游戏引擎

开发平台:

C++ Builder

  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=viewergpl$
  8.  * 
  9.  * Copyright (c) 2008-2010, Linden Research, Inc.
  10.  * 
  11.  * Second Life Viewer Source Code
  12.  * The source code in this file ("Source Code") is provided by Linden Lab
  13.  * to you under the terms of the GNU General Public License, version 2.0
  14.  * ("GPL"), unless you have obtained a separate licensing agreement
  15.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  16.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18.  * 
  19.  * There are special exceptions to the terms and conditions of the GPL as
  20.  * it is applied to this Source Code. View the full text of the exception
  21.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  22.  * online at
  23.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24.  * 
  25.  * By copying, modifying or distributing this software, you acknowledge
  26.  * that you have read and understood your obligations described above,
  27.  * and agree to abide by those obligations.
  28.  * 
  29.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31.  * COMPLETENESS OR PERFORMANCE.
  32.  * $/LicenseInfo$
  33.  */
  34. // Precompiled header
  35. #include "../llviewerprecompiledheaders.h"
  36. // Own header
  37. #include "../llcapabilitylistener.h"
  38. // STL headers
  39. #include <stdexcept>
  40. #include <map>
  41. #include <vector>
  42. // std headers
  43. // external library headers
  44. #include "boost/bind.hpp"
  45. // other Linden headers
  46. #include "../test/lltut.h"
  47. #include "../llcapabilityprovider.h"
  48. #include "lluuid.h"
  49. #include "tests/networkio.h"
  50. #include "tests/commtest.h"
  51. #include "tests/wrapllerrs.h"
  52. #include "message.h"
  53. #include "stringize.h"
  54. #if defined(LL_WINDOWS)
  55. #pragma warning(disable: 4355)      // using 'this' in base-class ctor initializer expr
  56. #endif
  57. /*****************************************************************************
  58. *   TestCapabilityProvider
  59. *****************************************************************************/
  60. struct TestCapabilityProvider: public LLCapabilityProvider
  61. {
  62.     TestCapabilityProvider(const LLHost& host):
  63.         mHost(host)
  64.     {}
  65.     std::string getCapability(const std::string& cap) const
  66.     {
  67.         CapMap::const_iterator found = mCaps.find(cap);
  68.         if (found != mCaps.end())
  69.             return found->second;
  70.         // normal LLViewerRegion lookup failure mode
  71.         return "";
  72.     }
  73.     void setCapability(const std::string& cap, const std::string& url)
  74.     {
  75.         mCaps[cap] = url;
  76.     }
  77.     LLHost getHost() const { return mHost; }
  78.     std::string getDescription() const { return "TestCapabilityProvider"; }
  79.     LLHost mHost;
  80.     typedef std::map<std::string, std::string> CapMap;
  81.     CapMap mCaps;
  82. };
  83. /*****************************************************************************
  84. *   Dummy LLMessageSystem methods
  85. *****************************************************************************/
  86. /*==========================================================================*|
  87. // This doesn't work because we're already linking in llmessage.a, and we get
  88. // duplicate-symbol errors from the linker. Perhaps if I wanted to go through
  89. // the exercise of providing dummy versions of every single symbol defined in
  90. // message.o -- maybe some day.
  91. typedef std::vector< std::pair<std::string, std::string> > StringPairVector;
  92. StringPairVector call_history;
  93. S32 LLMessageSystem::sendReliable(const LLHost& host)
  94. {
  95.     call_history.push_back(StringPairVector::value_type("sendReliable", stringize(host)));
  96.     return 0;
  97. }
  98. |*==========================================================================*/
  99. /*****************************************************************************
  100. *   TUT
  101. *****************************************************************************/
  102. namespace tut
  103. {
  104.     struct llcapears_data: public commtest_data
  105.     {
  106.         TestCapabilityProvider provider;
  107.         LLCapabilityListener regionListener;
  108.         LLEventPump& regionPump;
  109.         llcapears_data():
  110.             provider(host),
  111.             regionListener("testCapabilityListener", NULL, provider, LLUUID(), LLUUID()),
  112.             regionPump(regionListener.getCapAPI())
  113.         {
  114.             provider.setCapability("good", server + "capability-test");
  115.             provider.setCapability("fail", server + "fail");
  116.         }
  117.     };
  118.     typedef test_group<llcapears_data> llcapears_group;
  119.     typedef llcapears_group::object llcapears_object;
  120.     llcapears_group llsdmgr("llcapabilitylistener");
  121.     template<> template<>
  122.     void llcapears_object::test<1>()
  123.     {
  124.         LLSD request, body;
  125.         body["data"] = "yes";
  126.         request["payload"] = body;
  127.         request["reply"] = replyPump.getName();
  128.         request["error"] = errorPump.getName();
  129.         std::string threw;
  130.         try
  131.         {
  132.             WrapLL_ERRS capture;
  133.             regionPump.post(request);
  134.         }
  135.         catch (const WrapLL_ERRS::FatalException& e)
  136.         {
  137.             threw = e.what();
  138.         }
  139.         ensure_contains("missing capability name", threw, "without 'message' key");
  140.     }
  141.     template<> template<>
  142.     void llcapears_object::test<2>()
  143.     {
  144.         LLSD request, body;
  145.         body["data"] = "yes";
  146.         request["message"] = "good";
  147.         request["payload"] = body;
  148.         request["reply"] = replyPump.getName();
  149.         request["error"] = errorPump.getName();
  150.         regionPump.post(request);
  151.         ensure("got response", netio.pump());
  152.         ensure("success response", success);
  153.         ensure_equals(result.asString(), "success");
  154.         body["status"] = 499;
  155.         body["reason"] = "custom error message";
  156.         request["message"] = "fail";
  157.         request["payload"] = body;
  158.         regionPump.post(request);
  159.         ensure("got response", netio.pump());
  160.         ensure("failure response", ! success);
  161.         ensure_equals(result["status"].asInteger(), body["status"].asInteger());
  162.         ensure_equals(result["reason"].asString(),  body["reason"].asString());
  163.     }
  164.     template<> template<>
  165.     void llcapears_object::test<3>()
  166.     {
  167.         LLSD request, body;
  168.         body["data"] = "yes";
  169.         request["message"] = "unknown";
  170.         request["payload"] = body;
  171.         request["reply"] = replyPump.getName();
  172.         request["error"] = errorPump.getName();
  173.         std::string threw;
  174.         try
  175.         {
  176.             WrapLL_ERRS capture;
  177.             regionPump.post(request);
  178.         }
  179.         catch (const WrapLL_ERRS::FatalException& e)
  180.         {
  181.             threw = e.what();
  182.         }
  183.         ensure_contains("bad capability name", threw, "unsupported capability");
  184.     }
  185.     struct TestMapper: public LLCapabilityListener::CapabilityMapper
  186.     {
  187.         // Instantiator gets to specify whether mapper expects a reply.
  188.         // I'd really like to be able to test CapabilityMapper::buildMessage()
  189.         // functionality, too, but -- even though LLCapabilityListener accepts
  190.         // the LLMessageSystem* that it passes to CapabilityMapper --
  191.         // LLMessageSystem::sendReliable(const LLHost&) isn't virtual, so it's
  192.         // not helpful to pass a subclass instance. I suspect that making any
  193.         // LLMessageSystem methods virtual would provoke howls of outrage,
  194.         // given how heavily it's used. Nor can I just provide a local
  195.         // definition of LLMessageSystem::sendReliable(const LLHost&) because
  196.         // we're already linking in the rest of message.o via llmessage.a, and
  197.         // that produces duplicate-symbol link errors.
  198.         TestMapper(const std::string& replyMessage = std::string()):
  199.             LLCapabilityListener::CapabilityMapper("test", replyMessage)
  200.         {}
  201.         virtual void buildMessage(LLMessageSystem* msg,
  202.                                   const LLUUID& agentID,
  203.                                   const LLUUID& sessionID,
  204.                                   const std::string& capabilityName,
  205.                                   const LLSD& payload) const
  206.         {
  207.             msg->newMessageFast(_PREHASH_SetStartLocationRequest);
  208.             msg->nextBlockFast( _PREHASH_AgentData);
  209.             msg->addUUIDFast(_PREHASH_AgentID, agentID);
  210.             msg->addUUIDFast(_PREHASH_SessionID, sessionID);
  211.             msg->nextBlockFast( _PREHASH_StartLocationData);
  212.             // corrected by sim
  213.             msg->addStringFast(_PREHASH_SimName, "");
  214.             msg->addU32Fast(_PREHASH_LocationID, payload["HomeLocation"]["LocationId"].asInteger());
  215. /*==========================================================================*|
  216.             msg->addVector3Fast(_PREHASH_LocationPos,
  217.                                 ll_vector3_from_sdmap(payload["HomeLocation"]["LocationPos"]));
  218.             msg->addVector3Fast(_PREHASH_LocationLookAt,
  219.                                 ll_vector3_from_sdmap(payload["HomeLocation"]["LocationLookAt"]));
  220. |*==========================================================================*/
  221.         }
  222.     };
  223.     template<> template<>
  224.     void llcapears_object::test<4>()
  225.     {
  226.         TestMapper testMapper("WantReply");
  227.         LLSD request, body;
  228.         body["data"] = "yes";
  229.         request["message"] = "test";
  230.         request["payload"] = body;
  231.         request["reply"] = replyPump.getName();
  232.         request["error"] = errorPump.getName();
  233.         std::string threw;
  234.         try
  235.         {
  236.             WrapLL_ERRS capture;
  237.             regionPump.post(request);
  238.         }
  239.         catch (const WrapLL_ERRS::FatalException& e)
  240.         {
  241.             threw = e.what();
  242.         }
  243.         ensure_contains("capability mapper wants reply", threw, "unimplemented support for reply message");
  244.     }
  245.     template<> template<>
  246.     void llcapears_object::test<5>()
  247.     {
  248.         TestMapper testMapper;
  249.         std::string threw;
  250.         try
  251.         {
  252.             TestMapper testMapper2;
  253.         }
  254.         catch (const std::runtime_error& e)
  255.         {
  256.             threw = e.what();
  257.         }
  258.         ensure_contains("no dup cap mapper", threw, "DupCapMapper");
  259.     }
  260. }