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

游戏引擎

开发平台:

C++ Builder

  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=viewergpl$
  8.  * 
  9.  * Copyright (c) 2009-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. // associated header
  37. #include "../llxmlrpclistener.h"
  38. // STL headers
  39. #include <iomanip>
  40. // std headers
  41. // external library headers
  42. // other Linden headers
  43. #include "../test/lltut.h"
  44. #include "../llxmlrpctransaction.h"
  45. #include "llevents.h"
  46. #include "lleventfilter.h"
  47. #include "llsd.h"
  48. #include "llcontrol.h"
  49. #include "tests/wrapllerrs.h"
  50. LLControlGroup gSavedSettings("Global");
  51. /*****************************************************************************
  52. *   TUT
  53. *****************************************************************************/
  54. namespace tut
  55. {
  56.     struct data
  57.     {
  58.         data():
  59.             pumps(LLEventPumps::instance()),
  60.             uri("http://127.0.0.1:8000")
  61.         {
  62.             // These variables are required by machinery used by
  63.             // LLXMLRPCTransaction. The values reflect reality for this test
  64.             // executable; hopefully these values are correct.
  65.             gSavedSettings.declareBOOL("BrowserProxyEnabled", FALSE, "", FALSE); // don't persist
  66.             gSavedSettings.declareBOOL("NoVerifySSLCert", TRUE, "", FALSE); // don't persist
  67.         }
  68.         // LLEventPump listener signature
  69.         bool captureReply(const LLSD& r)
  70.         {
  71.             reply = r;
  72.             return false;
  73.         }
  74.         LLSD reply;
  75.         LLEventPumps& pumps;
  76.         std::string uri;
  77.     };
  78.     typedef test_group<data> llxmlrpclistener_group;
  79.     typedef llxmlrpclistener_group::object object;
  80.     llxmlrpclistener_group llxmlrpclistenergrp("llxmlrpclistener");
  81.     template<> template<>
  82.     void object::test<1>()
  83.     {
  84.         set_test_name("request validation");
  85.         WrapLL_ERRS capture;
  86.         LLSD request;
  87.         request["uri"] = uri;
  88.         std::string threw;
  89.         try
  90.         {
  91.             pumps.obtain("LLXMLRPCTransaction").post(request);
  92.         }
  93.         catch (const WrapLL_ERRS::FatalException& e)
  94.         {
  95.             threw = e.what();
  96.         }
  97.         ensure_contains("threw exception", threw, "missing params");
  98.         ensure_contains("identified missing", threw, "method");
  99.         ensure_contains("identified missing", threw, "reply");
  100.     }
  101.     template<> template<>
  102.     void object::test<2>()
  103.     {
  104.         set_test_name("param types validation");
  105.         WrapLL_ERRS capture;
  106.         LLSD request;
  107.         request["uri"] = uri;
  108.         request["method"] = "hello";
  109.         request["reply"] = "reply";
  110.         LLSD& params(request["params"]);
  111.         params["who"]["specifically"] = "world"; // LLXMLRPCListener only handles scalar params
  112.         std::string threw;
  113.         try
  114.         {
  115.             pumps.obtain("LLXMLRPCTransaction").post(request);
  116.         }
  117.         catch (const WrapLL_ERRS::FatalException& e)
  118.         {
  119.             threw = e.what();
  120.         }
  121.         ensure_contains("threw exception", threw, "unknown type");
  122.     }
  123.     template<> template<>
  124.     void object::test<3>()
  125.     {
  126.         set_test_name("success case");
  127.         LLSD request;
  128.         request["uri"] = uri;
  129.         request["method"] = "hello";
  130.         request["reply"] = "reply";
  131.         LLSD& params(request["params"]);
  132.         params["who"] = "world";
  133.         // Set up a timeout filter so we don't spin forever waiting.
  134.         LLEventTimeout watchdog;
  135.         // Connect the timeout filter to the reply pump.
  136.         LLTempBoundListener temp(
  137.             pumps.obtain("reply").
  138.             listen("watchdog", boost::bind(&LLEventTimeout::post, boost::ref(watchdog), _1)));
  139.         // Now connect our target listener to the timeout filter.
  140.         watchdog.listen("captureReply", boost::bind(&data::captureReply, this, _1));
  141.         // Kick off the request...
  142.         reply.clear();
  143.         pumps.obtain("LLXMLRPCTransaction").post(request);
  144.         // Set the timer
  145.         F32 timeout(10);
  146.         watchdog.eventAfter(timeout, LLSD().insert("timeout", 0));
  147.         // and pump "mainloop" until we get something, whether from
  148.         // LLXMLRPCListener or from the watchdog filter.
  149.         LLTimer timer;
  150.         F32 start = timer.getElapsedTimeF32();
  151.         LLEventPump& mainloop(pumps.obtain("mainloop"));
  152.         while (reply.isUndefined())
  153.         {
  154.             mainloop.post(LLSD());
  155.         }
  156.         ensure("timeout works", (timer.getElapsedTimeF32() - start) < (timeout + 1));
  157.         ensure_equals(reply["responses"]["hi_there"].asString(), "Hello, world!");
  158.     }
  159.     template<> template<>
  160.     void object::test<4>()
  161.     {
  162.         set_test_name("bogus method");
  163.         LLSD request;
  164.         request["uri"] = uri;
  165.         request["method"] = "goodbye";
  166.         request["reply"] = "reply";
  167.         LLSD& params(request["params"]);
  168.         params["who"] = "world";
  169.         // Set up a timeout filter so we don't spin forever waiting.
  170.         LLEventTimeout watchdog;
  171.         // Connect the timeout filter to the reply pump.
  172.         LLTempBoundListener temp(
  173.             pumps.obtain("reply").
  174.             listen("watchdog", boost::bind(&LLEventTimeout::post, boost::ref(watchdog), _1)));
  175.         // Now connect our target listener to the timeout filter.
  176.         watchdog.listen("captureReply", boost::bind(&data::captureReply, this, _1));
  177.         // Kick off the request...
  178.         reply.clear();
  179.         pumps.obtain("LLXMLRPCTransaction").post(request);
  180.         // Set the timer
  181.         F32 timeout(10);
  182.         watchdog.eventAfter(timeout, LLSD().insert("timeout", 0));
  183.         // and pump "mainloop" until we get something, whether from
  184.         // LLXMLRPCListener or from the watchdog filter.
  185.         LLTimer timer;
  186.         F32 start = timer.getElapsedTimeF32();
  187.         LLEventPump& mainloop(pumps.obtain("mainloop"));
  188.         while (reply.isUndefined())
  189.         {
  190.             mainloop.post(LLSD());
  191.         }
  192.         ensure("timeout works", (timer.getElapsedTimeF32() - start) < (timeout + 1));
  193.         ensure_equals("XMLRPC error", reply["status"].asString(), "XMLRPCError");
  194.     }
  195.     template<> template<>
  196.     void object::test<5>()
  197.     {
  198.         set_test_name("bad type");
  199.         LLSD request;
  200.         request["uri"] = uri;
  201.         request["method"] = "getdict";
  202.         request["reply"] = "reply";
  203.         (void)request["params"];
  204.         // Set up a timeout filter so we don't spin forever waiting.
  205.         LLEventTimeout watchdog;
  206.         // Connect the timeout filter to the reply pump.
  207.         LLTempBoundListener temp(
  208.             pumps.obtain("reply").
  209.             listen("watchdog", boost::bind(&LLEventTimeout::post, boost::ref(watchdog), _1)));
  210.         // Now connect our target listener to the timeout filter.
  211.         watchdog.listen("captureReply", boost::bind(&data::captureReply, this, _1));
  212.         // Kick off the request...
  213.         reply.clear();
  214.         pumps.obtain("LLXMLRPCTransaction").post(request);
  215.         // Set the timer
  216.         F32 timeout(10);
  217.         watchdog.eventAfter(timeout, LLSD().insert("timeout", 0));
  218.         // and pump "mainloop" until we get something, whether from
  219.         // LLXMLRPCListener or from the watchdog filter.
  220.         LLTimer timer;
  221.         F32 start = timer.getElapsedTimeF32();
  222.         LLEventPump& mainloop(pumps.obtain("mainloop"));
  223.         while (reply.isUndefined())
  224.         {
  225.             mainloop.post(LLSD());
  226.         }
  227.         ensure("timeout works", (timer.getElapsedTimeF32() - start) < (timeout + 1));
  228.         ensure_equals(reply["status"].asString(), "BadType");
  229.         ensure_contains("bad type", reply["responses"]["nested_dict"].asString(), "bad XMLRPC type");
  230.     }
  231. } // namespace tut
  232. /*****************************************************************************
  233. *   Resolve link errors: use real machinery here, since we intend to exchange
  234. *   actual XML with a peer process.
  235. *****************************************************************************/
  236. // Including llxmlrpctransaction.cpp drags in the static LLXMLRPCListener
  237. // instantiated there. That's why it works to post requests to the LLEventPump
  238. // named "LLXMLRPCTransaction".
  239. #include "../llxmlrpctransaction.cpp"
  240. #include "llcontrol.cpp"
  241. #include "llxmltree.cpp"
  242. #include "llxmlparser.cpp"