PageRenderTime 39ms CodeModel.GetById 2ms app.highlight 32ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmessage/tests/lltemplatemessagedispatcher_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 159 lines | 110 code | 19 blank | 30 comment | 1 complexity | 6c7adb9f0c5db8737eff3cea237122ce MD5 | raw file
  1/**
  2 * @file lltrustedmessageservice_test.cpp
  3 * @brief LLTrustedMessageService unit tests
  4 *
  5 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "lltemplatemessagedispatcher.h"
 28#include "lltut.h"
 29
 30#include "llhttpnode.h"
 31#include "llhost.h"
 32#include "message.h"
 33#include "llsd.h"
 34
 35#include "llhost.cpp" // Needed for copy operator
 36#include "net.cpp" // Needed by LLHost.
 37
 38LLMessageSystem * gMessageSystem = NULL;
 39
 40// sensor test doubles
 41bool gClearRecvWasCalled = false;
 42void LLMessageSystem::clearReceiveState(void) 
 43{ 
 44	gClearRecvWasCalled = true; 
 45}
 46
 47char gUdpDispatchedData[MAX_BUFFER_SIZE];
 48bool gUdpDispatchWasCalled = false;
 49BOOL LLTemplateMessageReader::readMessage(const U8* data,class LLHost const &) 
 50{ 
 51	gUdpDispatchWasCalled = true;
 52	strcpy(gUdpDispatchedData, reinterpret_cast<const char*>(data));
 53	return  true;
 54}
 55
 56BOOL gValidateMessage = FALSE;
 57BOOL LLTemplateMessageReader::validateMessage(const U8*, S32 buffer_size, LLHost const &sender, bool trusted) 
 58{ 
 59	return gValidateMessage;
 60}
 61
 62LLHost host;
 63const LLHost& LLMessageSystem::getSender() const 
 64{ 
 65	return host; 
 66}
 67
 68const char* gBinaryTemplateData = "BINARYTEMPLATEDATA";
 69void fillVector(std::vector<U8>& vector_data, const char* data)
 70{
 71	vector_data.resize(strlen(data) + 1);
 72	strcpy(reinterpret_cast<char*>(&vector_data[0]), data);
 73}
 74
 75namespace tut
 76{
 77		static LLTemplateMessageReader::message_template_number_map_t numberMap;
 78
 79		struct LLTemplateMessageDispatcherData
 80		{
 81			LLTemplateMessageDispatcherData()
 82			{
 83				mMessageName = "MessageName";
 84				gUdpDispatchWasCalled = false;
 85				gClearRecvWasCalled = false;
 86				gValidateMessage = FALSE;
 87				mMessage["body"]["binary-template-data"] = std::vector<U8>();
 88			}
 89
 90			LLSD mMessage;
 91			LLHTTPNode::ResponsePtr mResponsePtr;
 92			std::string mMessageName;
 93		};
 94
 95	typedef test_group<LLTemplateMessageDispatcherData> factory;
 96	typedef factory::object object;
 97}
 98
 99namespace
100{
101	tut::factory tf("LLTemplateMessageDispatcher");
102}
103
104namespace tut
105{
106	// does an empty message stop processing?
107	template<> template<>
108	void object::test<1>()
109	{
110		LLTemplateMessageReader* pReader = NULL;
111		LLTemplateMessageDispatcher t(*pReader);
112		t.dispatch(mMessageName, mMessage, mResponsePtr);
113		ensure(! gUdpDispatchWasCalled);
114		ensure(! gClearRecvWasCalled);
115	}
116
117	// does the disaptch invoke the udp send method?
118	template<> template<>
119	void object::test<2>()
120	{
121		LLTemplateMessageReader* pReader = NULL;
122		LLTemplateMessageDispatcher t(*pReader);
123		gValidateMessage = TRUE;
124		std::vector<U8> vector_data;
125		fillVector(vector_data, gBinaryTemplateData);		
126		mMessage["body"]["binary-template-data"] = vector_data;
127		t.dispatch(mMessageName, mMessage, mResponsePtr);
128		ensure("udp dispatch was called", gUdpDispatchWasCalled);
129	}
130
131	// what if the message wasn't valid? We would hope the message gets cleared!
132	template<> template<>
133	void object::test<3>()
134	{
135		LLTemplateMessageReader* pReader = NULL;
136		LLTemplateMessageDispatcher t(*pReader);
137		std::vector<U8> vector_data;
138		fillVector(vector_data, gBinaryTemplateData);
139		mMessage["body"]["binary-template-data"] = vector_data;
140		gValidateMessage = FALSE;
141		t.dispatch(mMessageName, mMessage, mResponsePtr);
142		ensure("clear received message was called", gClearRecvWasCalled);
143	}
144
145	// is the binary data passed through correctly?
146	template<> template<>
147	void object::test<4>()
148	{
149		LLTemplateMessageReader* pReader = NULL;
150		LLTemplateMessageDispatcher t(*pReader);
151		gValidateMessage = TRUE;
152		std::vector<U8> vector_data;
153		fillVector(vector_data, gBinaryTemplateData);
154		mMessage["body"]["binary-template-data"] = vector_data;
155		t.dispatch(mMessageName, mMessage, mResponsePtr);
156		ensure("data couriered correctly", strcmp(gBinaryTemplateData, gUdpDispatchedData) == 0);
157	}
158}
159