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