PageRenderTime 626ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "lltemplatemessagedispatcher.h"
  27. #include "lltut.h"
  28. #include "llhttpnode.h"
  29. #include "llhost.h"
  30. #include "message.h"
  31. #include "llsd.h"
  32. #include "llhost.cpp" // Needed for copy operator
  33. #include "net.cpp" // Needed by LLHost.
  34. LLMessageSystem * gMessageSystem = NULL;
  35. // sensor test doubles
  36. bool gClearRecvWasCalled = false;
  37. void LLMessageSystem::clearReceiveState(void)
  38. {
  39. gClearRecvWasCalled = true;
  40. }
  41. char gUdpDispatchedData[MAX_BUFFER_SIZE];
  42. bool gUdpDispatchWasCalled = false;
  43. BOOL LLTemplateMessageReader::readMessage(const U8* data,class LLHost const &)
  44. {
  45. gUdpDispatchWasCalled = true;
  46. strcpy(gUdpDispatchedData, reinterpret_cast<const char*>(data));
  47. return true;
  48. }
  49. BOOL gValidateMessage = FALSE;
  50. BOOL LLTemplateMessageReader::validateMessage(const U8*, S32 buffer_size, LLHost const &sender, bool trusted)
  51. {
  52. return gValidateMessage;
  53. }
  54. LLHost host;
  55. const LLHost& LLMessageSystem::getSender() const
  56. {
  57. return host;
  58. }
  59. const char* gBinaryTemplateData = "BINARYTEMPLATEDATA";
  60. void fillVector(std::vector<U8>& vector_data, const char* data)
  61. {
  62. vector_data.resize(strlen(data) + 1);
  63. strcpy(reinterpret_cast<char*>(&vector_data[0]), data);
  64. }
  65. namespace tut
  66. {
  67. static LLTemplateMessageReader::message_template_number_map_t numberMap;
  68. struct LLTemplateMessageDispatcherData
  69. {
  70. LLTemplateMessageDispatcherData()
  71. {
  72. mMessageName = "MessageName";
  73. gUdpDispatchWasCalled = false;
  74. gClearRecvWasCalled = false;
  75. gValidateMessage = FALSE;
  76. mMessage["body"]["binary-template-data"] = std::vector<U8>();
  77. }
  78. LLSD mMessage;
  79. LLHTTPNode::ResponsePtr mResponsePtr;
  80. std::string mMessageName;
  81. };
  82. typedef test_group<LLTemplateMessageDispatcherData> factory;
  83. typedef factory::object object;
  84. }
  85. namespace
  86. {
  87. tut::factory tf("LLTemplateMessageDispatcher");
  88. }
  89. namespace tut
  90. {
  91. // does an empty message stop processing?
  92. template<> template<>
  93. void object::test<1>()
  94. {
  95. LLTemplateMessageReader* pReader = NULL;
  96. LLTemplateMessageDispatcher t(*pReader);
  97. t.dispatch(mMessageName, mMessage, mResponsePtr);
  98. ensure(! gUdpDispatchWasCalled);
  99. ensure(! gClearRecvWasCalled);
  100. }
  101. // does the disaptch invoke the udp send method?
  102. template<> template<>
  103. void object::test<2>()
  104. {
  105. LLTemplateMessageReader* pReader = NULL;
  106. LLTemplateMessageDispatcher t(*pReader);
  107. gValidateMessage = TRUE;
  108. std::vector<U8> vector_data;
  109. fillVector(vector_data, gBinaryTemplateData);
  110. mMessage["body"]["binary-template-data"] = vector_data;
  111. t.dispatch(mMessageName, mMessage, mResponsePtr);
  112. ensure("udp dispatch was called", gUdpDispatchWasCalled);
  113. }
  114. // what if the message wasn't valid? We would hope the message gets cleared!
  115. template<> template<>
  116. void object::test<3>()
  117. {
  118. LLTemplateMessageReader* pReader = NULL;
  119. LLTemplateMessageDispatcher t(*pReader);
  120. std::vector<U8> vector_data;
  121. fillVector(vector_data, gBinaryTemplateData);
  122. mMessage["body"]["binary-template-data"] = vector_data;
  123. gValidateMessage = FALSE;
  124. t.dispatch(mMessageName, mMessage, mResponsePtr);
  125. ensure("clear received message was called", gClearRecvWasCalled);
  126. }
  127. // is the binary data passed through correctly?
  128. template<> template<>
  129. void object::test<4>()
  130. {
  131. LLTemplateMessageReader* pReader = NULL;
  132. LLTemplateMessageDispatcher t(*pReader);
  133. gValidateMessage = TRUE;
  134. std::vector<U8> vector_data;
  135. fillVector(vector_data, gBinaryTemplateData);
  136. mMessage["body"]["binary-template-data"] = vector_data;
  137. t.dispatch(mMessageName, mMessage, mResponsePtr);
  138. ensure("data couriered correctly", strcmp(gBinaryTemplateData, gUdpDispatchedData) == 0);
  139. }
  140. }