PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/test/lltemplatemessagebuilder_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 971 lines | 790 code | 83 blank | 98 comment | 3 complexity | f1b7ead1fc25863e91c39b463891db91 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltemplatemessagebuilder_tut.cpp
  3. * @date 2007-04
  4. * @brief Tests for building messages.
  5. *
  6. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include <tut/tut.hpp>
  28. #include "linden_common.h"
  29. #include "lltut.h"
  30. #include "llapr.h"
  31. #include "llmessagetemplate.h"
  32. #include "llmath.h"
  33. #include "llquaternion.h"
  34. #include "lltemplatemessagebuilder.h"
  35. #include "lltemplatemessagereader.h"
  36. #include "llversionserver.h"
  37. #include "message_prehash.h"
  38. #include "u64.h"
  39. #include "v3dmath.h"
  40. #include "v3math.h"
  41. #include "v4math.h"
  42. namespace tut
  43. {
  44. static LLTemplateMessageBuilder::message_template_name_map_t nameMap;
  45. static LLTemplateMessageReader::message_template_number_map_t numberMap;
  46. struct LLTemplateMessageBuilderTestData
  47. {
  48. static LLMessageTemplate defaultTemplate()
  49. {
  50. static bool init = false;
  51. if(! init)
  52. {
  53. ll_init_apr();
  54. const F32 circuit_heartbeat_interval=5;
  55. const F32 circuit_timeout=100;
  56. start_messaging_system("notafile", 13035,
  57. LL_VERSION_MAJOR,
  58. LL_VERSION_MINOR,
  59. LL_VERSION_PATCH,
  60. FALSE,
  61. "notasharedsecret",
  62. NULL,
  63. false,
  64. circuit_heartbeat_interval,
  65. circuit_timeout);
  66. //init_prehash_data();
  67. init = true;
  68. }
  69. return LLMessageTemplate(_PREHASH_TestMessage, 1, MFT_HIGH);
  70. }
  71. static LLMessageBlock* defaultBlock(const EMsgVariableType type = MVT_NULL, const S32 size = 0, EMsgBlockType block = MBT_VARIABLE)
  72. {
  73. return createBlock(const_cast<char*>(_PREHASH_Test0), type, size, block);
  74. }
  75. static LLMessageBlock* createBlock(char* name, const EMsgVariableType type = MVT_NULL, const S32 size = 0, EMsgBlockType block = MBT_VARIABLE)
  76. {
  77. LLMessageBlock* result = new LLMessageBlock(name, block);
  78. if(type != MVT_NULL)
  79. {
  80. result->addVariable(const_cast<char*>(_PREHASH_Test0), type, size);
  81. }
  82. return result;
  83. }
  84. static LLTemplateMessageBuilder* defaultBuilder(LLMessageTemplate& messageTemplate, char* name = const_cast<char*>(_PREHASH_Test0))
  85. {
  86. nameMap[_PREHASH_TestMessage] = &messageTemplate;
  87. LLTemplateMessageBuilder* builder = new LLTemplateMessageBuilder(nameMap);
  88. builder->newMessage(_PREHASH_TestMessage);
  89. builder->nextBlock(name);
  90. return builder;
  91. }
  92. /** Takes ownership of builder */
  93. static LLTemplateMessageReader* setReader(
  94. LLMessageTemplate& messageTemplate,
  95. LLTemplateMessageBuilder* builder,
  96. U8 offset = 0)
  97. {
  98. numberMap[1] = &messageTemplate;
  99. const U32 bufferSize = 1024;
  100. U8 buffer[bufferSize];
  101. // zero out the packet ID field
  102. memset(buffer, 0, LL_PACKET_ID_SIZE);
  103. U32 builtSize = builder->buildMessage(buffer, bufferSize, offset);
  104. delete builder;
  105. LLTemplateMessageReader* reader = new LLTemplateMessageReader(numberMap);
  106. reader->validateMessage(buffer, builtSize, LLHost());
  107. reader->readMessage(buffer, LLHost());
  108. return reader;
  109. }
  110. };
  111. typedef test_group<LLTemplateMessageBuilderTestData> LLTemplateMessageBuilderTestGroup;
  112. typedef LLTemplateMessageBuilderTestGroup::object LLTemplateMessageBuilderTestObject;
  113. LLTemplateMessageBuilderTestGroup templateMessageBuilderTestGroup("LLTemplateMessageBuilder");
  114. template<> template<>
  115. void LLTemplateMessageBuilderTestObject::test<1>()
  116. // construction and test of undefined
  117. {
  118. LLMessageTemplate messageTemplate = defaultTemplate();
  119. messageTemplate.addBlock(defaultBlock());
  120. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  121. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  122. delete reader;
  123. }
  124. template<> template<>
  125. void LLTemplateMessageBuilderTestObject::test<2>()
  126. // BOOL
  127. {
  128. LLMessageTemplate messageTemplate = defaultTemplate();
  129. messageTemplate.addBlock(defaultBlock(MVT_BOOL, 1));
  130. BOOL outValue, inValue = TRUE;
  131. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  132. builder->addBOOL(_PREHASH_Test0, inValue);
  133. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  134. reader->getBOOL(_PREHASH_Test0, _PREHASH_Test0, outValue);
  135. ensure_equals("Ensure BOOL", inValue, outValue);
  136. delete reader;
  137. }
  138. template<> template<>
  139. void LLTemplateMessageBuilderTestObject::test<3>()
  140. // U8
  141. {
  142. LLMessageTemplate messageTemplate = defaultTemplate();
  143. messageTemplate.addBlock(defaultBlock(MVT_U8, 1));
  144. U8 outValue, inValue = 2;
  145. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  146. builder->addU8(_PREHASH_Test0, inValue);
  147. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  148. reader->getU8(_PREHASH_Test0, _PREHASH_Test0, outValue);
  149. ensure_equals("Ensure U8", inValue, outValue);
  150. delete reader;
  151. }
  152. template<> template<>
  153. void LLTemplateMessageBuilderTestObject::test<4>()
  154. // S16
  155. {
  156. LLMessageTemplate messageTemplate = defaultTemplate();
  157. messageTemplate.addBlock(defaultBlock(MVT_S16, 2));
  158. S16 outValue, inValue = 90;
  159. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  160. builder->addS16(_PREHASH_Test0, inValue);
  161. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  162. reader->getS16(_PREHASH_Test0, _PREHASH_Test0, outValue);
  163. ensure_equals("Ensure S16", inValue, outValue);
  164. delete reader;
  165. }
  166. template<> template<>
  167. void LLTemplateMessageBuilderTestObject::test<5>()
  168. // U16
  169. {
  170. LLMessageTemplate messageTemplate = defaultTemplate();
  171. messageTemplate.addBlock(defaultBlock(MVT_U16, 2));
  172. U16 outValue, inValue = 3;
  173. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  174. builder->addU16(_PREHASH_Test0, inValue);
  175. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  176. reader->getU16(_PREHASH_Test0, _PREHASH_Test0, outValue);
  177. ensure_equals("Ensure U16", inValue, outValue);
  178. delete reader;
  179. }
  180. template<> template<>
  181. void LLTemplateMessageBuilderTestObject::test<6>()
  182. // S32
  183. {
  184. LLMessageTemplate messageTemplate = defaultTemplate();
  185. messageTemplate.addBlock(defaultBlock(MVT_S32, 4));
  186. S32 outValue, inValue = 44;
  187. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  188. builder->addS32(_PREHASH_Test0, inValue);
  189. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  190. reader->getS32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  191. ensure_equals("Ensure S32", inValue, outValue);
  192. delete reader;
  193. }
  194. template<> template<>
  195. void LLTemplateMessageBuilderTestObject::test<7>()
  196. // F32
  197. {
  198. LLMessageTemplate messageTemplate = defaultTemplate();
  199. messageTemplate.addBlock(defaultBlock(MVT_F32, 4));
  200. F32 outValue, inValue = 121.44f;
  201. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  202. builder->addF32(_PREHASH_Test0, inValue);
  203. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  204. reader->getF32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  205. ensure_equals("Ensure F32", inValue, outValue);
  206. delete reader;
  207. }
  208. template<> template<>
  209. void LLTemplateMessageBuilderTestObject::test<8>()
  210. // U32
  211. {
  212. LLMessageTemplate messageTemplate = defaultTemplate();
  213. messageTemplate.addBlock(defaultBlock(MVT_U32, 4));
  214. U32 outValue, inValue = 88;
  215. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  216. builder->addU32(_PREHASH_Test0, inValue);
  217. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  218. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  219. ensure_equals("Ensure U32", inValue, outValue);
  220. delete reader;
  221. }
  222. template<> template<>
  223. void LLTemplateMessageBuilderTestObject::test<9>()
  224. // U64
  225. {
  226. LLMessageTemplate messageTemplate = defaultTemplate();
  227. messageTemplate.addBlock(defaultBlock(MVT_U64, 8));
  228. U64 outValue, inValue = 121;
  229. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  230. builder->addU64(_PREHASH_Test0, inValue);
  231. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  232. reader->getU64(_PREHASH_Test0, _PREHASH_Test0, outValue);
  233. ensure_equals("Ensure U64", inValue, outValue);
  234. delete reader;
  235. }
  236. template<> template<>
  237. void LLTemplateMessageBuilderTestObject::test<10>()
  238. // F64
  239. {
  240. LLMessageTemplate messageTemplate = defaultTemplate();
  241. messageTemplate.addBlock(defaultBlock(MVT_F64, 8));
  242. F64 outValue, inValue = 3232143.33;
  243. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  244. builder->addF64(_PREHASH_Test0, inValue);
  245. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  246. reader->getF64(_PREHASH_Test0, _PREHASH_Test0, outValue);
  247. ensure_equals("Ensure F64", inValue, outValue);
  248. delete reader;
  249. }
  250. template<> template<>
  251. void LLTemplateMessageBuilderTestObject::test<11>()
  252. // Vector3
  253. {
  254. LLMessageTemplate messageTemplate = defaultTemplate();
  255. messageTemplate.addBlock(defaultBlock(MVT_LLVector3, 12));
  256. LLVector3 outValue, inValue = LLVector3(1,2,3);
  257. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  258. builder->addVector3(_PREHASH_Test0, inValue);
  259. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  260. reader->getVector3(_PREHASH_Test0, _PREHASH_Test0, outValue);
  261. ensure_equals("Ensure LLVector3", inValue, outValue);
  262. delete reader;
  263. }
  264. template<> template<>
  265. void LLTemplateMessageBuilderTestObject::test<12>()
  266. // Vector4
  267. {
  268. LLMessageTemplate messageTemplate = defaultTemplate();
  269. messageTemplate.addBlock(defaultBlock(MVT_LLVector4, 16));
  270. LLVector4 outValue, inValue = LLVector4(1,2,3,4);
  271. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  272. builder->addVector4(_PREHASH_Test0, inValue);
  273. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  274. reader->getVector4(_PREHASH_Test0, _PREHASH_Test0, outValue);
  275. ensure_equals("Ensure LLVector4", inValue, outValue);
  276. delete reader;
  277. }
  278. template<> template<>
  279. void LLTemplateMessageBuilderTestObject::test<13>()
  280. // Vector3d
  281. {
  282. LLMessageTemplate messageTemplate = defaultTemplate();
  283. messageTemplate.addBlock(defaultBlock(MVT_LLVector3d, 24));
  284. LLVector3d outValue, inValue = LLVector3d(1,2,3);
  285. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  286. builder->addVector3d(_PREHASH_Test0, inValue);
  287. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  288. reader->getVector3d(_PREHASH_Test0, _PREHASH_Test0, outValue);
  289. ensure_equals("Ensure LLVector3d", inValue, outValue);
  290. delete reader;
  291. }
  292. template<> template<>
  293. void LLTemplateMessageBuilderTestObject::test<14>()
  294. // Quaternion
  295. {
  296. LLMessageTemplate messageTemplate = defaultTemplate();
  297. messageTemplate.addBlock(defaultBlock(MVT_LLQuaternion, 12));
  298. LLQuaternion outValue, inValue = LLQuaternion(1,2,3,0);
  299. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  300. builder->addQuat(_PREHASH_Test0, inValue);
  301. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  302. reader->getQuat(_PREHASH_Test0, _PREHASH_Test0, outValue);
  303. ensure_equals("Ensure LLQuaternion", inValue, outValue);
  304. delete reader;
  305. }
  306. template<> template<>
  307. void LLTemplateMessageBuilderTestObject::test<15>()
  308. // UUID
  309. {
  310. LLMessageTemplate messageTemplate = defaultTemplate();
  311. messageTemplate.addBlock(defaultBlock(MVT_LLUUID, 16));
  312. LLUUID outValue, inValue;
  313. inValue.generate();
  314. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  315. builder->addUUID(_PREHASH_Test0, inValue);
  316. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  317. reader->getUUID(_PREHASH_Test0, _PREHASH_Test0, outValue);
  318. ensure_equals("Ensure UUID", inValue, outValue);
  319. delete reader;
  320. }
  321. template<> template<>
  322. void LLTemplateMessageBuilderTestObject::test<16>()
  323. // IPAddr
  324. {
  325. LLMessageTemplate messageTemplate = defaultTemplate();
  326. messageTemplate.addBlock(defaultBlock(MVT_IP_ADDR, 4));
  327. U32 outValue, inValue = 12344556;
  328. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  329. builder->addIPAddr(_PREHASH_Test0, inValue);
  330. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  331. reader->getIPAddr(_PREHASH_Test0, _PREHASH_Test0, outValue);
  332. ensure_equals("Ensure IPAddr", inValue, outValue);
  333. delete reader;
  334. }
  335. template<> template<>
  336. void LLTemplateMessageBuilderTestObject::test<17>()
  337. // IPPort
  338. {
  339. LLMessageTemplate messageTemplate = defaultTemplate();
  340. messageTemplate.addBlock(defaultBlock(MVT_IP_PORT, 2));
  341. U16 outValue, inValue = 80;
  342. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  343. builder->addIPPort(_PREHASH_Test0, inValue);
  344. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  345. reader->getIPPort(_PREHASH_Test0, _PREHASH_Test0, outValue);
  346. ensure_equals("Ensure IPPort", inValue, outValue);
  347. delete reader;
  348. }
  349. template<> template<>
  350. void LLTemplateMessageBuilderTestObject::test<18>()
  351. // String
  352. {
  353. LLMessageTemplate messageTemplate = defaultTemplate();
  354. messageTemplate.addBlock(defaultBlock(MVT_VARIABLE, 1));
  355. std::string outValue, inValue = "testing";
  356. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  357. builder->addString(_PREHASH_Test0, inValue.c_str());
  358. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  359. char buffer[MAX_STRING];
  360. reader->getString(_PREHASH_Test0, _PREHASH_Test0, MAX_STRING, buffer);
  361. outValue = buffer;
  362. ensure_equals("Ensure String", inValue, outValue);
  363. delete reader;
  364. }
  365. template<> template<>
  366. void LLTemplateMessageBuilderTestObject::test<19>()
  367. // block name !-> binary order
  368. {
  369. U8 buffer1[MAX_BUFFER_SIZE];
  370. memset(buffer1, 0, MAX_BUFFER_SIZE);
  371. U8 buffer2[MAX_BUFFER_SIZE];
  372. memset(buffer2, 0, MAX_BUFFER_SIZE);
  373. U32 bufferSize1, bufferSize2;
  374. // build template: Test0 before Test1
  375. LLMessageTemplate messageTemplate = defaultTemplate();
  376. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4, MBT_SINGLE));
  377. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4, MBT_SINGLE));
  378. // build message: 1st declared block var == 0xaaaa, 2nd declared block var == 0xbbbb
  379. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate, const_cast<char*>(_PREHASH_Test0));
  380. builder->addU32(_PREHASH_Test0, 0xaaaa);
  381. builder->nextBlock(_PREHASH_Test1);
  382. builder->addU32(_PREHASH_Test0, 0xbbbb);
  383. bufferSize1 = builder->buildMessage(buffer1, MAX_BUFFER_SIZE, 0);
  384. delete builder;
  385. // build template: Test1 before Test0
  386. messageTemplate = defaultTemplate();
  387. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4, MBT_SINGLE));
  388. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4, MBT_SINGLE));
  389. // build message: 1st declared block var == 0xaaaa, 2nd declared block var == 0xbbbb
  390. builder = defaultBuilder(messageTemplate, const_cast<char*>(_PREHASH_Test1));
  391. builder->addU32(_PREHASH_Test0, 0xaaaa);
  392. builder->nextBlock(_PREHASH_Test0);
  393. builder->addU32(_PREHASH_Test0, 0xbbbb);
  394. bufferSize2 = builder->buildMessage(buffer2, MAX_BUFFER_SIZE, 0);
  395. delete builder;
  396. ensure_equals("Ensure Buffer Sizes Equal", bufferSize1, bufferSize2);
  397. ensure_equals("Ensure Buffer Contents Equal", memcmp(buffer1, buffer2, bufferSize1), 0);
  398. }
  399. template<> template<>
  400. void LLTemplateMessageBuilderTestObject::test<20>()
  401. // block build order !-> binary order
  402. {
  403. U8 buffer1[MAX_BUFFER_SIZE];
  404. memset(buffer1, 0, MAX_BUFFER_SIZE);
  405. U8 buffer2[MAX_BUFFER_SIZE];
  406. memset(buffer2, 0, MAX_BUFFER_SIZE);
  407. U32 bufferSize1, bufferSize2;
  408. // build template: Test0 before Test1
  409. LLMessageTemplate messageTemplate = defaultTemplate();
  410. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4, MBT_SINGLE));
  411. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4, MBT_SINGLE));
  412. // build message: 1st declared block var == 0xaaaa, 2nd declared block var == 0xbbbb
  413. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate, const_cast<char*>(_PREHASH_Test0));
  414. builder->addU32(_PREHASH_Test0, 0xaaaa);
  415. builder->nextBlock(_PREHASH_Test1);
  416. builder->addU32(_PREHASH_Test0, 0xbbbb);
  417. bufferSize1 = builder->buildMessage(buffer1, MAX_BUFFER_SIZE, 0);
  418. delete builder;
  419. // build message: 1st declared block var == 0xaaaa, 2nd declared block var == 0xbbbb
  420. builder = defaultBuilder(messageTemplate, const_cast<char*>(_PREHASH_Test1));
  421. builder->addU32(_PREHASH_Test0, 0xbbbb);
  422. builder->nextBlock(_PREHASH_Test0);
  423. builder->addU32(_PREHASH_Test0, 0xaaaa);
  424. bufferSize2 = builder->buildMessage(buffer2, MAX_BUFFER_SIZE, 0);
  425. delete builder;
  426. ensure_equals("Ensure Buffer Sizes Equal", bufferSize1, bufferSize2);
  427. ensure_equals("Ensure Buffer Contents Equal", memcmp(buffer1, buffer2, bufferSize1), 0);
  428. }
  429. template<> template<>
  430. void LLTemplateMessageBuilderTestObject::test<21>()
  431. // block appended in declaration -> data appended in binary
  432. {
  433. U8 buffer1[MAX_BUFFER_SIZE];
  434. memset(buffer1, 0, MAX_BUFFER_SIZE);
  435. U8 buffer2[MAX_BUFFER_SIZE];
  436. memset(buffer2, 0, MAX_BUFFER_SIZE);
  437. U32 bufferSize1, bufferSize2;
  438. // Build template: Test0 only
  439. LLMessageTemplate messageTemplate = defaultTemplate();
  440. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4, MBT_SINGLE));
  441. // Build message
  442. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate, const_cast<char*>(_PREHASH_Test0));
  443. builder->addU32(_PREHASH_Test0, 0xaaaa);
  444. bufferSize1 = builder->buildMessage(buffer1, MAX_BUFFER_SIZE, 0);
  445. delete builder;
  446. // Build template: Test0 before Test1
  447. messageTemplate = defaultTemplate();
  448. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4, MBT_SINGLE));
  449. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4, MBT_SINGLE));
  450. // Build message
  451. builder = defaultBuilder(messageTemplate, const_cast<char*>(_PREHASH_Test0));
  452. builder->addU32(_PREHASH_Test0, 0xaaaa);
  453. builder->nextBlock(_PREHASH_Test1);
  454. builder->addU32(_PREHASH_Test0, 0xbbbb);
  455. bufferSize2 = builder->buildMessage(buffer2, MAX_BUFFER_SIZE, 0);
  456. delete builder;
  457. ensure_not_equals("Ensure Buffer Sizes Not Equal", bufferSize1, bufferSize2);
  458. ensure_equals("Ensure Buffer Prefix Equal", memcmp(buffer1, buffer2, bufferSize1), 0);
  459. ensure_not_equals("Ensure Buffer Contents Not Equal", memcmp(buffer1, buffer2, bufferSize2), 0);
  460. }
  461. template<> template<>
  462. void LLTemplateMessageBuilderTestObject::test<22>()
  463. // repeated penultimate block (crashes when data in LLDynamicArrayIndexed)
  464. {
  465. U32 inTest00 = 0, inTest01 = 1, inTest1 = 2;
  466. U32 outTest00, outTest01, outTest1;
  467. LLMessageTemplate messageTemplate = defaultTemplate();
  468. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4));
  469. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4));
  470. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  471. builder->addU32(_PREHASH_Test0, inTest00);
  472. builder->nextBlock(_PREHASH_Test0);
  473. builder->addU32(_PREHASH_Test0, inTest01);
  474. builder->nextBlock(_PREHASH_Test1);
  475. builder->addU32(_PREHASH_Test0, inTest1);
  476. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  477. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outTest00, 0);
  478. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outTest01, 1);
  479. reader->getU32(_PREHASH_Test1, _PREHASH_Test0, outTest1);
  480. ensure_equals("Ensure Test0[0]", inTest00, outTest00);
  481. ensure_equals("Ensure Test0[1]", inTest01, outTest01);
  482. ensure_equals("Ensure Test1", inTest1, outTest1);
  483. delete reader;
  484. }
  485. template<> template<>
  486. void LLTemplateMessageBuilderTestObject::test<23>()
  487. // variable repeated block name never accessed
  488. {
  489. U32 inTest = 1, outTest;
  490. LLMessageTemplate messageTemplate = defaultTemplate();
  491. messageTemplate.addBlock(
  492. createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4, MBT_SINGLE));
  493. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4));
  494. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  495. builder->addU32(_PREHASH_Test0, inTest);
  496. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  497. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outTest);
  498. S32 blockCount = reader->getNumberOfBlocks(const_cast<char*>(_PREHASH_Test1));
  499. ensure_equals("Ensure block count", blockCount, 0);
  500. ensure_equals("Ensure Test0", inTest, outTest);
  501. delete reader;
  502. }
  503. template<> template<>
  504. void LLTemplateMessageBuilderTestObject::test<24>()
  505. // forwarding message
  506. {
  507. // build template
  508. LLMessageTemplate messageTemplate = defaultTemplate();
  509. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test0), MVT_U32, 4));
  510. // build message
  511. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  512. builder->addU32(_PREHASH_Test0, 42);
  513. // read message
  514. LLTemplateMessageReader* reader = setReader(messageTemplate, builder);
  515. // forward message
  516. builder = defaultBuilder(messageTemplate);
  517. builder->newMessage(_PREHASH_TestMessage);
  518. reader->copyToBuilder(*builder);
  519. U8 buffer[MAX_BUFFER_SIZE];
  520. builder->buildMessage(buffer, MAX_BUFFER_SIZE, 0);
  521. delete builder;
  522. delete reader;
  523. }
  524. template<> template<>
  525. void LLTemplateMessageBuilderTestObject::test<25>()
  526. // non-zero offset with undefined
  527. {
  528. LLMessageTemplate messageTemplate = defaultTemplate();
  529. messageTemplate.addBlock(defaultBlock());
  530. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  531. LLTemplateMessageReader* reader = setReader(
  532. messageTemplate, builder, 10);
  533. delete reader;
  534. }
  535. template<> template<>
  536. void LLTemplateMessageBuilderTestObject::test<26>()
  537. // non-zero offset with BOOL
  538. {
  539. LLMessageTemplate messageTemplate = defaultTemplate();
  540. messageTemplate.addBlock(defaultBlock(MVT_BOOL, 1));
  541. BOOL outValue, inValue = TRUE;
  542. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  543. builder->addBOOL(_PREHASH_Test0, inValue);
  544. LLTemplateMessageReader* reader = setReader(
  545. messageTemplate, builder, 1);
  546. reader->getBOOL(_PREHASH_Test0, _PREHASH_Test0, outValue);
  547. ensure_equals("Ensure BOOL", inValue, outValue);
  548. delete reader;
  549. }
  550. template<> template<>
  551. void LLTemplateMessageBuilderTestObject::test<27>()
  552. // non-zero offset with U8
  553. {
  554. LLMessageTemplate messageTemplate = defaultTemplate();
  555. messageTemplate.addBlock(defaultBlock(MVT_U8, 1));
  556. U8 outValue, inValue = 2;
  557. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  558. builder->addU8(_PREHASH_Test0, inValue);
  559. LLTemplateMessageReader* reader = setReader(
  560. messageTemplate, builder, 255);
  561. reader->getU8(_PREHASH_Test0, _PREHASH_Test0, outValue);
  562. ensure_equals("Ensure U8", inValue, outValue);
  563. delete reader;
  564. }
  565. template<> template<>
  566. void LLTemplateMessageBuilderTestObject::test<28>()
  567. // non-zero offset with S16
  568. {
  569. LLMessageTemplate messageTemplate = defaultTemplate();
  570. messageTemplate.addBlock(defaultBlock(MVT_S16, 2));
  571. S16 outValue, inValue = 90;
  572. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  573. builder->addS16(_PREHASH_Test0, inValue);
  574. LLTemplateMessageReader* reader = setReader(
  575. messageTemplate, builder, 2);
  576. reader->getS16(_PREHASH_Test0, _PREHASH_Test0, outValue);
  577. ensure_equals("Ensure S16", inValue, outValue);
  578. delete reader;
  579. }
  580. template<> template<>
  581. void LLTemplateMessageBuilderTestObject::test<29>()
  582. // non-zero offset with U16
  583. {
  584. LLMessageTemplate messageTemplate = defaultTemplate();
  585. messageTemplate.addBlock(defaultBlock(MVT_U16, 2));
  586. U16 outValue, inValue = 3;
  587. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  588. builder->addU16(_PREHASH_Test0, inValue);
  589. LLTemplateMessageReader* reader = setReader(
  590. messageTemplate, builder, 4);
  591. reader->getU16(_PREHASH_Test0, _PREHASH_Test0, outValue);
  592. ensure_equals("Ensure U16", inValue, outValue);
  593. delete reader;
  594. }
  595. template<> template<>
  596. void LLTemplateMessageBuilderTestObject::test<30>()
  597. // non-zero offset with S32
  598. {
  599. LLMessageTemplate messageTemplate = defaultTemplate();
  600. messageTemplate.addBlock(defaultBlock(MVT_S32, 4));
  601. S32 outValue, inValue = 44;
  602. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  603. builder->addS32(_PREHASH_Test0, inValue);
  604. LLTemplateMessageReader* reader = setReader(
  605. messageTemplate, builder, 4);
  606. reader->getS32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  607. ensure_equals("Ensure S32", inValue, outValue);
  608. delete reader;
  609. }
  610. template<> template<>
  611. void LLTemplateMessageBuilderTestObject::test<31>()
  612. // non-zero offset with F32
  613. {
  614. LLMessageTemplate messageTemplate = defaultTemplate();
  615. messageTemplate.addBlock(defaultBlock(MVT_F32, 4));
  616. F32 outValue, inValue = 121.44f;
  617. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  618. builder->addF32(_PREHASH_Test0, inValue);
  619. LLTemplateMessageReader* reader = setReader(
  620. messageTemplate, builder, 16);
  621. reader->getF32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  622. ensure_equals("Ensure F32", inValue, outValue);
  623. delete reader;
  624. }
  625. template<> template<>
  626. void LLTemplateMessageBuilderTestObject::test<32>()
  627. // non-zero offset with U32
  628. {
  629. LLMessageTemplate messageTemplate = defaultTemplate();
  630. messageTemplate.addBlock(defaultBlock(MVT_U32, 4));
  631. U32 outValue, inValue = 88;
  632. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  633. builder->addU32(_PREHASH_Test0, inValue);
  634. LLTemplateMessageReader* reader = setReader(
  635. messageTemplate, builder, 127);
  636. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  637. ensure_equals("Ensure U32", inValue, outValue);
  638. delete reader;
  639. }
  640. template<> template<>
  641. void LLTemplateMessageBuilderTestObject::test<33>()
  642. // non-zero offset with U64
  643. {
  644. LLMessageTemplate messageTemplate = defaultTemplate();
  645. messageTemplate.addBlock(defaultBlock(MVT_U64, 8));
  646. U64 outValue, inValue = 121;
  647. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  648. builder->addU64(_PREHASH_Test0, inValue);
  649. LLTemplateMessageReader* reader = setReader(
  650. messageTemplate, builder, 32);
  651. reader->getU64(_PREHASH_Test0, _PREHASH_Test0, outValue);
  652. ensure_equals("Ensure U64", inValue, outValue);
  653. delete reader;
  654. }
  655. template<> template<>
  656. void LLTemplateMessageBuilderTestObject::test<34>()
  657. // non-zero offset with F64
  658. {
  659. LLMessageTemplate messageTemplate = defaultTemplate();
  660. messageTemplate.addBlock(defaultBlock(MVT_F64, 8));
  661. F64 outValue, inValue = 3232143.33;
  662. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  663. builder->addF64(_PREHASH_Test0, inValue);
  664. LLTemplateMessageReader* reader = setReader(
  665. messageTemplate, builder, 128);
  666. reader->getF64(_PREHASH_Test0, _PREHASH_Test0, outValue);
  667. ensure_equals("Ensure F64", inValue, outValue);
  668. delete reader;
  669. }
  670. template<> template<>
  671. void LLTemplateMessageBuilderTestObject::test<35>()
  672. // non-zero offset with Vector3
  673. {
  674. LLMessageTemplate messageTemplate = defaultTemplate();
  675. messageTemplate.addBlock(defaultBlock(MVT_LLVector3, 12));
  676. LLVector3 outValue, inValue = LLVector3(1,2,3);
  677. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  678. builder->addVector3(_PREHASH_Test0, inValue);
  679. LLTemplateMessageReader* reader = setReader(
  680. messageTemplate, builder, 63);
  681. reader->getVector3(_PREHASH_Test0, _PREHASH_Test0, outValue);
  682. ensure_equals("Ensure LLVector3", inValue, outValue);
  683. delete reader;
  684. }
  685. template<> template<>
  686. void LLTemplateMessageBuilderTestObject::test<36>()
  687. // non-zero offset with Vector4
  688. {
  689. LLMessageTemplate messageTemplate = defaultTemplate();
  690. messageTemplate.addBlock(defaultBlock(MVT_LLVector4, 16));
  691. LLVector4 outValue, inValue = LLVector4(1,2,3,4);
  692. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  693. builder->addVector4(_PREHASH_Test0, inValue);
  694. LLTemplateMessageReader* reader = setReader(
  695. messageTemplate, builder, 64);
  696. reader->getVector4(_PREHASH_Test0, _PREHASH_Test0, outValue);
  697. ensure_equals("Ensure LLVector4", inValue, outValue);
  698. delete reader;
  699. }
  700. template<> template<>
  701. void LLTemplateMessageBuilderTestObject::test<37>()
  702. // non-zero offset with Vector3d
  703. {
  704. LLMessageTemplate messageTemplate = defaultTemplate();
  705. messageTemplate.addBlock(defaultBlock(MVT_LLVector3d, 24));
  706. LLVector3d outValue, inValue = LLVector3d(1,2,3);
  707. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  708. builder->addVector3d(_PREHASH_Test0, inValue);
  709. LLTemplateMessageReader* reader = setReader(
  710. messageTemplate, builder, 64);
  711. reader->getVector3d(_PREHASH_Test0, _PREHASH_Test0, outValue);
  712. ensure_equals("Ensure LLVector3d", inValue, outValue);
  713. delete reader;
  714. }
  715. template<> template<>
  716. void LLTemplateMessageBuilderTestObject::test<38>()
  717. // non-zero offset with Quaternion
  718. {
  719. LLMessageTemplate messageTemplate = defaultTemplate();
  720. messageTemplate.addBlock(defaultBlock(MVT_LLQuaternion, 12));
  721. LLQuaternion outValue, inValue = LLQuaternion(1,2,3,0);
  722. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  723. builder->addQuat(_PREHASH_Test0, inValue);
  724. LLTemplateMessageReader* reader = setReader(
  725. messageTemplate, builder, 12);
  726. reader->getQuat(_PREHASH_Test0, _PREHASH_Test0, outValue);
  727. ensure_equals("Ensure LLQuaternion", inValue, outValue);
  728. delete reader;
  729. }
  730. template<> template<>
  731. void LLTemplateMessageBuilderTestObject::test<39>()
  732. // non-zero offset with UUID
  733. {
  734. LLMessageTemplate messageTemplate = defaultTemplate();
  735. messageTemplate.addBlock(defaultBlock(MVT_LLUUID, 16));
  736. LLUUID outValue, inValue;
  737. inValue.generate();
  738. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  739. builder->addUUID(_PREHASH_Test0, inValue);
  740. LLTemplateMessageReader* reader = setReader(
  741. messageTemplate, builder, 31);
  742. reader->getUUID(_PREHASH_Test0, _PREHASH_Test0, outValue);
  743. ensure_equals("Ensure UUID", inValue, outValue);
  744. delete reader;
  745. }
  746. template<> template<>
  747. void LLTemplateMessageBuilderTestObject::test<40>()
  748. // non-zero offset with IPAddr
  749. {
  750. LLMessageTemplate messageTemplate = defaultTemplate();
  751. messageTemplate.addBlock(defaultBlock(MVT_IP_ADDR, 4));
  752. U32 outValue, inValue = 12344556;
  753. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  754. builder->addIPAddr(_PREHASH_Test0, inValue);
  755. LLTemplateMessageReader* reader = setReader(
  756. messageTemplate, builder, 32);
  757. reader->getIPAddr(_PREHASH_Test0, _PREHASH_Test0, outValue);
  758. ensure_equals("Ensure IPAddr", inValue, outValue);
  759. delete reader;
  760. }
  761. template<> template<>
  762. void LLTemplateMessageBuilderTestObject::test<41>()
  763. // non-zero offset with IPPort
  764. {
  765. LLMessageTemplate messageTemplate = defaultTemplate();
  766. messageTemplate.addBlock(defaultBlock(MVT_IP_PORT, 2));
  767. U16 outValue, inValue = 80;
  768. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  769. builder->addIPPort(_PREHASH_Test0, inValue);
  770. LLTemplateMessageReader* reader = setReader(
  771. messageTemplate, builder, 6);
  772. reader->getIPPort(_PREHASH_Test0, _PREHASH_Test0, outValue);
  773. ensure_equals("Ensure IPPort", inValue, outValue);
  774. delete reader;
  775. }
  776. template<> template<>
  777. void LLTemplateMessageBuilderTestObject::test<42>()
  778. // non-zero offset with String
  779. {
  780. LLMessageTemplate messageTemplate = defaultTemplate();
  781. messageTemplate.addBlock(defaultBlock(MVT_VARIABLE, 1));
  782. std::string outValue, inValue = "testing";
  783. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  784. builder->addString(_PREHASH_Test0, inValue.c_str());
  785. LLTemplateMessageReader* reader = setReader(
  786. messageTemplate, builder, 255);
  787. char buffer[MAX_STRING];
  788. reader->getString(_PREHASH_Test0, _PREHASH_Test0, MAX_STRING, buffer);
  789. outValue = buffer;
  790. ensure_equals("Ensure String", inValue, outValue);
  791. delete reader;
  792. }
  793. template<> template<>
  794. void LLTemplateMessageBuilderTestObject::test<43>()
  795. // read past end of message -> default values (forward compatibility)
  796. {
  797. // build message with single block
  798. LLMessageTemplate messageTemplate = defaultTemplate();
  799. messageTemplate.addBlock(defaultBlock(MVT_U32, 4, MBT_SINGLE));
  800. U32 outValue, outValue2, inValue = 0xbbbbbbbb;
  801. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  802. builder->addU32(_PREHASH_Test0, inValue);
  803. const U32 bufferSize = 1024;
  804. U8 buffer[bufferSize];
  805. memset(buffer, 0xaa, bufferSize);
  806. memset(buffer, 0, LL_PACKET_ID_SIZE);
  807. U32 builtSize = builder->buildMessage(buffer, bufferSize, 0);
  808. delete builder;
  809. // add block to reader template
  810. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4, MBT_SINGLE));
  811. // read message value and default value
  812. numberMap[1] = &messageTemplate;
  813. LLTemplateMessageReader* reader =
  814. new LLTemplateMessageReader(numberMap);
  815. reader->validateMessage(buffer, builtSize, LLHost());
  816. reader->readMessage(buffer, LLHost());
  817. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  818. reader->getU32(_PREHASH_Test1, _PREHASH_Test0, outValue2);
  819. ensure_equals("Ensure present value ", outValue, inValue);
  820. ensure_equals("Ensure default value ", outValue2, 0);
  821. delete reader;
  822. }
  823. template<> template<>
  824. void LLTemplateMessageBuilderTestObject::test<44>()
  825. // read variable block past end of message -> 0 repeats
  826. {
  827. // build message with single block
  828. LLMessageTemplate messageTemplate = defaultTemplate();
  829. messageTemplate.addBlock(defaultBlock(MVT_U32, 4, MBT_SINGLE));
  830. U32 outValue, outValue2, inValue = 0xbbbbbbbb;
  831. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  832. builder->addU32(_PREHASH_Test0, inValue);
  833. const U32 bufferSize = 1024;
  834. U8 buffer[bufferSize];
  835. memset(buffer, 0xaa, bufferSize);
  836. memset(buffer, 0, LL_PACKET_ID_SIZE);
  837. U32 builtSize = builder->buildMessage(buffer, bufferSize, 0);
  838. delete builder;
  839. // add variable block to reader template
  840. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_U32, 4));
  841. // read message value and check block repeat count
  842. numberMap[1] = &messageTemplate;
  843. LLTemplateMessageReader* reader =
  844. new LLTemplateMessageReader(numberMap);
  845. reader->validateMessage(buffer, builtSize, LLHost());
  846. reader->readMessage(buffer, LLHost());
  847. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  848. outValue2 = reader->getNumberOfBlocks(_PREHASH_Test1);
  849. ensure_equals("Ensure present value ", outValue, inValue);
  850. ensure_equals("Ensure 0 repeats ", outValue2, 0);
  851. delete reader;
  852. }
  853. template<> template<>
  854. void LLTemplateMessageBuilderTestObject::test<45>()
  855. // read variable length data past end of message -> 0 length
  856. {
  857. // build message with single block
  858. LLMessageTemplate messageTemplate = defaultTemplate();
  859. messageTemplate.addBlock(defaultBlock(MVT_U32, 4, MBT_SINGLE));
  860. U32 outValue, outValue2, inValue = 0xbbbbbbbb;
  861. LLTemplateMessageBuilder* builder = defaultBuilder(messageTemplate);
  862. builder->addU32(_PREHASH_Test0, inValue);
  863. const U32 bufferSize = 1024;
  864. U8 buffer[bufferSize];
  865. memset(buffer, 0xaa, bufferSize);
  866. memset(buffer, 0, LL_PACKET_ID_SIZE);
  867. U32 builtSize = builder->buildMessage(buffer, bufferSize, 0);
  868. delete builder;
  869. // add variable block to reader template
  870. messageTemplate.addBlock(createBlock(const_cast<char*>(_PREHASH_Test1), MVT_VARIABLE, 4,
  871. MBT_SINGLE));
  872. // read message value and default string
  873. numberMap[1] = &messageTemplate;
  874. LLTemplateMessageReader* reader =
  875. new LLTemplateMessageReader(numberMap);
  876. reader->validateMessage(buffer, builtSize, LLHost());
  877. reader->readMessage(buffer, LLHost());
  878. reader->getU32(_PREHASH_Test0, _PREHASH_Test0, outValue);
  879. char outBuffer[bufferSize];
  880. memset(buffer, 0xcc, bufferSize);
  881. reader->getString(_PREHASH_Test1, _PREHASH_Test0, bufferSize,
  882. outBuffer);
  883. outValue2 = reader->getNumberOfBlocks(_PREHASH_Test1);
  884. ensure_equals("Ensure present value ", outValue, inValue);
  885. ensure_equals("Ensure unchanged buffer ", strlen(outBuffer), 0);
  886. delete reader;
  887. }
  888. }