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