PageRenderTime 43ms CodeModel.GetById 31ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/tests/llsdmessage_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 158 lines | 104 code | 8 blank | 46 comment | 2 complexity | e33fa23aa9be34a5780c312c19f7f3d4 MD5 | raw file
  1/**
  2 * @file   llsdmessage_test.cpp
  3 * @author Nat Goodspeed
  4 * @date   2008-12-22
  5 * @brief  Test of llsdmessage.h
  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#if LL_WINDOWS
 30#pragma warning (disable : 4675) // "resolved by ADL" -- just as I want!
 31#endif
 32
 33// Precompiled header
 34#include "linden_common.h"
 35// associated header
 36#include "llsdmessage.h"
 37// STL headers
 38#include <iostream>
 39// std headers
 40#include <stdexcept>
 41#include <typeinfo>
 42// external library headers
 43// other Linden headers
 44#include "../test/lltut.h"
 45#include "llsdserialize.h"
 46#include "llevents.h"
 47#include "stringize.h"
 48#include "llhost.h"
 49#include "tests/networkio.h"
 50#include "tests/commtest.h"
 51
 52/*****************************************************************************
 53*   TUT
 54*****************************************************************************/
 55namespace tut
 56{
 57    struct llsdmessage_data: public commtest_data
 58    {
 59        LLEventPump& httpPump;
 60
 61        llsdmessage_data():
 62            httpPump(pumps.obtain("LLHTTPClient"))
 63        {
 64            LLCurl::initClass();
 65            LLSDMessage::link();
 66        }
 67    };
 68    typedef test_group<llsdmessage_data> llsdmessage_group;
 69    typedef llsdmessage_group::object llsdmessage_object;
 70    llsdmessage_group llsdmgr("llsdmessage");
 71
 72    template<> template<>
 73    void llsdmessage_object::test<1>()
 74    {
 75        bool threw = false;
 76        // This should fail...
 77        try
 78        {
 79            LLSDMessage localListener;
 80        }
 81        catch (const LLEventPump::DupPumpName&)
 82        {
 83            threw = true;
 84        }
 85        catch (const std::runtime_error& ex)
 86        {
 87            // This clause is because on Linux, on the viewer side, for this
 88            // one test program (though not others!), the
 89            // LLEventPump::DupPumpName exception isn't caught by the clause
 90            // above. Warn the user...
 91            std::cerr << "Failed to catch " << typeid(ex).name() << std::endl;
 92            // But if the expected exception was thrown, allow the test to
 93            // succeed anyway. Not sure how else to handle this odd case.
 94            if (std::string(typeid(ex).name()) == typeid(LLEventPump::DupPumpName).name())
 95            {
 96                threw = true;
 97            }
 98            else
 99            {
100                // We don't even recognize this exception. Let it propagate
101                // out to TUT to fail the test.
102                throw;
103            }
104        }
105        catch (...)
106        {
107            std::cerr << "Utterly failed to catch expected exception!" << std::endl;
108            // This case is full of fail. We HAVE to address it.
109            throw;
110        }
111        ensure("second LLSDMessage should throw", threw);
112    }
113
114    template<> template<>
115    void llsdmessage_object::test<2>()
116    {
117        LLSD request, body;
118        body["data"] = "yes";
119        request["payload"] = body;
120        request["reply"] = replyPump.getName();
121        request["error"] = errorPump.getName();
122        bool threw = false;
123        try
124        {
125            httpPump.post(request);
126        }
127        catch (const LLSDMessage::ArgError&)
128        {
129            threw = true;
130        }
131        ensure("missing URL", threw);
132    }
133
134    template<> template<>
135    void llsdmessage_object::test<3>()
136    {
137        LLSD request, body;
138        body["data"] = "yes";
139        request["url"] = server + "got-message";
140        request["payload"] = body;
141        request["reply"] = replyPump.getName();
142        request["error"] = errorPump.getName();
143        httpPump.post(request);
144        ensure("got response", netio.pump());
145        ensure("success response", success);
146        ensure_equals(result.asString(), "success");
147
148        body["status"] = 499;
149        body["reason"] = "custom error message";
150        request["url"] = server + "fail";
151        request["payload"] = body;
152        httpPump.post(request);
153        ensure("got response", netio.pump());
154        ensure("failure response", ! success);
155        ensure_equals(result["status"].asInteger(), body["status"].asInteger());
156        ensure_equals(result["reason"].asString(),  body["reason"].asString());
157    }
158} // namespace tut