opaque_data_tuple_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <dhcp/opaque_data_tuple.h>
  16. #include <util/buffer.h>
  17. #include <gtest/gtest.h>
  18. #include <algorithm>
  19. #include <sstream>
  20. #include <vector>
  21. using namespace isc;
  22. using namespace isc::dhcp;
  23. using namespace isc::util;
  24. namespace {
  25. // This test checks that when the default constructor is called, the data buffer
  26. // is empty.
  27. TEST(OpaqueDataTuple, constructor) {
  28. OpaqueDataTuple tuple;
  29. // There should be no data in the tuple.
  30. EXPECT_EQ(0, tuple.getLength());
  31. EXPECT_TRUE(tuple.getData().empty());
  32. EXPECT_TRUE(tuple.getText().empty());
  33. }
  34. // Test that the constructor which takes the buffer as argument parses the
  35. // wire data.
  36. TEST(OpaqueDataTuple, constructorParse1Byte) {
  37. const char wire_data[] = {
  38. 0x0B, // Length is 11
  39. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  40. 0x77, 0x6F, 0x72, 0x6C, 0x64 // world
  41. };
  42. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE, wire_data,
  43. wire_data + sizeof(wire_data));
  44. EXPECT_EQ(11, tuple.getLength());
  45. EXPECT_EQ("Hello world", tuple.getText());
  46. }
  47. // Test that the constructor which takes the buffer as argument parses the
  48. // wire data.
  49. TEST(OpaqueDataTuple, constructorParse2Bytes) {
  50. const char wire_data[] = {
  51. 0x00, 0x0B, // Length is 11
  52. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  53. 0x77, 0x6F, 0x72, 0x6C, 0x64 // world
  54. };
  55. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES, wire_data,
  56. wire_data + sizeof(wire_data));
  57. EXPECT_EQ(11, tuple.getLength());
  58. EXPECT_EQ("Hello world", tuple.getText());
  59. }
  60. // This test checks that it is possible to set the tuple data using raw buffer.
  61. TEST(OpaqueDataTuple, assignData) {
  62. OpaqueDataTuple tuple;
  63. // Initially the tuple buffer should be empty.
  64. OpaqueDataTuple::Buffer buf = tuple.getData();
  65. ASSERT_TRUE(buf.empty());
  66. // Prepare some input data and assign to the tuple.
  67. const uint8_t data1[] = {
  68. 0xCA, 0xFE, 0xBE, 0xEF
  69. };
  70. tuple.assign(data1, sizeof(data1));
  71. // Tuple should now hold the data we assigned.
  72. ASSERT_EQ(sizeof(data1), tuple.getLength());
  73. buf = tuple.getData();
  74. EXPECT_TRUE(std::equal(buf.begin(), buf.end(), data1));
  75. // Prepare the other set of data and assign to the tuple.
  76. const uint8_t data2[] = {
  77. 1, 2, 3, 4, 5, 6
  78. };
  79. tuple.assign(data2, sizeof(data2));
  80. // The new data should have replaced the old data.
  81. ASSERT_EQ(sizeof(data2), tuple.getLength());
  82. buf = tuple.getData();
  83. EXPECT_TRUE(std::equal(buf.begin(), buf.end(), data2));
  84. }
  85. // This test checks thet it is possible to append the data to the tuple using
  86. // raw buffer.
  87. TEST(OpaqueDataTuple, appendData) {
  88. OpaqueDataTuple tuple;
  89. // Initially the tuple buffer should be empty.
  90. OpaqueDataTuple::Buffer buf = tuple.getData();
  91. ASSERT_TRUE(buf.empty());
  92. // Prepare some input data and append to the empty tuple.
  93. const uint8_t data1[] = {
  94. 0xCA, 0xFE, 0xBE, 0xEF
  95. };
  96. tuple.append(data1, sizeof(data1));
  97. // The tuple should now hold only the data we appended.
  98. ASSERT_EQ(sizeof(data1), tuple.getLength());
  99. buf = tuple.getData();
  100. EXPECT_TRUE(std::equal(buf.begin(), buf.end(), data1));
  101. // Prepare the new set of data and append.
  102. const uint8_t data2[] = {
  103. 1, 2, 3, 4, 5, 6
  104. };
  105. tuple.append(data2, sizeof(data2));
  106. // We expect that the tuple now has both sets of data we appended. In order
  107. // to verify that, we have to concatenate the input data1 and data2.
  108. std::vector<uint8_t> data12(data1, data1 + sizeof(data1));
  109. data12.insert(data12.end(), data2, data2 + sizeof(data2));
  110. // The size of the tuple should be a sum of data1 and data2 lengths.
  111. ASSERT_EQ(sizeof(data1) + sizeof(data2), tuple.getLength());
  112. buf = tuple.getData();
  113. EXPECT_TRUE(std::equal(buf.begin(), buf.end(), data12.begin()));
  114. }
  115. // This test checks that it is possible to assign the string to the tuple.
  116. TEST(OpaqueDataTuple, assignString) {
  117. OpaqueDataTuple tuple;
  118. // Initially, the tuple should be empty.
  119. ASSERT_EQ(0, tuple.getLength());
  120. // Assign some string data.
  121. tuple.assign("Some string");
  122. // Verify that the data has been assigned.
  123. EXPECT_EQ(11, tuple.getLength());
  124. EXPECT_EQ("Some string", tuple.getText());
  125. // Assign some other string.
  126. tuple.assign("Different string");
  127. // The new string should have replaced the old string.
  128. EXPECT_EQ(16, tuple.getLength());
  129. EXPECT_EQ("Different string", tuple.getText());
  130. }
  131. // This test checks that it is possible to append the string to the tuple.
  132. TEST(OpaqueDataTuple, appendString) {
  133. OpaqueDataTuple tuple;
  134. // Initially the tuple should be empty.
  135. ASSERT_EQ(0, tuple.getLength());
  136. // Append the string to it.
  137. tuple.append("First part");
  138. ASSERT_EQ(10, tuple.getLength());
  139. ASSERT_EQ("First part", tuple.getText());
  140. // Now append the other string.
  141. tuple.append(" and second part");
  142. EXPECT_EQ(26, tuple.getLength());
  143. // The resulting data in the tuple should be a concatenation of both
  144. // strings.
  145. EXPECT_EQ("First part and second part", tuple.getText());
  146. }
  147. // This test checks that equals function correctly checks that the tuple
  148. // holds a given string but it doesn't hold the other.
  149. TEST(OpaqueDataTuple, equals) {
  150. OpaqueDataTuple tuple;
  151. // Tuple is supposed to be empty so it is not equal xyz.
  152. EXPECT_FALSE(tuple.equals("xyz"));
  153. // Assign xyz.
  154. tuple = "xyz";
  155. // The tuple should be equal xyz, but not abc.
  156. EXPECT_FALSE(tuple.equals("abc"));
  157. EXPECT_TRUE(tuple.equals("xyz"));
  158. // Assign abc to the tuple.
  159. tuple = "abc";
  160. // It should be now opposite.
  161. EXPECT_TRUE(tuple.equals("abc"));
  162. EXPECT_FALSE(tuple.equals("xyz"));
  163. }
  164. // This test checks that the conversion of the data in the tuple to the string
  165. // is performed correctly.
  166. TEST(OpaqueDataTuple, getText) {
  167. OpaqueDataTuple tuple;
  168. // Initially the tuple should be empty.
  169. ASSERT_TRUE(tuple.getText().empty());
  170. // ASCII representation of 'Hello world'.
  171. const char as_ascii[] = {
  172. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  173. 0x77, 0x6F, 0x72, 0x6C, 0x64 // world
  174. };
  175. // Assign it to the tuple.
  176. tuple.assign(as_ascii, sizeof(as_ascii));
  177. // Conversion to string should give as the original text.
  178. EXPECT_EQ("Hello world", tuple.getText());
  179. }
  180. // This test verifies the behavior of (in)equality and assignment operators.
  181. TEST(OpaqueDataTuple, operators) {
  182. OpaqueDataTuple tuple;
  183. // Tuple should be empty initially.
  184. ASSERT_EQ(0, tuple.getLength());
  185. // Check assignment.
  186. tuple = "Hello World";
  187. EXPECT_EQ(11, tuple.getLength());
  188. EXPECT_TRUE(tuple == "Hello World");
  189. EXPECT_TRUE(tuple != "Something else");
  190. // Assign something else to make sure it affects the tuple.
  191. tuple = "Something else";
  192. EXPECT_EQ(14, tuple.getLength());
  193. EXPECT_TRUE(tuple == "Something else");
  194. EXPECT_TRUE(tuple != "Hello World");
  195. }
  196. // This test verifies that the tuple is inserted in the textual format to the
  197. // output stream.
  198. TEST(OpaqueDataTuple, operatorOutputStream) {
  199. OpaqueDataTuple tuple;
  200. // The tuple should be empty initially.
  201. ASSERT_EQ(0, tuple.getLength());
  202. // The tuple is empty, so assigning its content to the output stream should
  203. // be no-op and result in the same text in the stream.
  204. std::ostringstream s;
  205. s << "Some text";
  206. s << tuple;
  207. EXPECT_EQ("Some text", s.str());
  208. // Now, let's assign some text to the tuple and call operator again.
  209. // The new text should be added to the stream.
  210. tuple = " and some other text";
  211. s << tuple;
  212. EXPECT_EQ(s.str(), "Some text and some other text");
  213. }
  214. // This test verifies that the value of the tuple can be initialized from the
  215. // input stream.
  216. TEST(OpaqueDataTuple, operatorInputStream) {
  217. OpaqueDataTuple tuple;
  218. // The tuple should be empty initially.
  219. ASSERT_EQ(0, tuple.getLength());
  220. // The input stream has some text. This text should be appended to the
  221. // tuple.
  222. std::istringstream s;
  223. s.str("Some text");
  224. s >> tuple;
  225. EXPECT_EQ("Some text", tuple.getText());
  226. // Now, let's assign some other text to the stream. This new text should be
  227. // assigned to the tuple.
  228. s.str("And some other");
  229. s >> tuple;
  230. EXPECT_EQ("And some other", tuple.getText());
  231. }
  232. // This test checks that the tuple is correctly encoded in the wire format when
  233. // the size of the length field is 1 byte.
  234. TEST(OpaqueDataTuple, pack1Byte) {
  235. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  236. // Initially, the tuple should be empty.
  237. ASSERT_EQ(0, tuple.getLength());
  238. // The empty data doesn't make much sense, so the pack() should not
  239. // allow it.
  240. OutputBuffer out_buf(10);
  241. EXPECT_THROW(tuple.pack(out_buf), OpaqueDataTupleError);
  242. // Set the data for tuple.
  243. std::vector<uint8_t> data;
  244. for (int i = 0; i < 100; ++i) {
  245. data.push_back(i);
  246. }
  247. tuple.assign(data.begin(), data.size());
  248. // The pack should now succeed.
  249. ASSERT_NO_THROW(tuple.pack(out_buf));
  250. // The rendered buffer should be 101 bytes long - 1 byte for length,
  251. // 100 bytes for the actual data.
  252. ASSERT_EQ(101, out_buf.getLength());
  253. // Get the rendered data into the vector for convenience.
  254. std::vector<uint8_t>
  255. render_data(static_cast<const uint8_t*>(out_buf.getData()),
  256. static_cast<const uint8_t*>(out_buf.getData()) + 101);
  257. // The first byte is a length byte. It should hold the length of 100.
  258. EXPECT_EQ(100, render_data[0]);
  259. // Verify that the rendered data is correct.
  260. EXPECT_TRUE(std::equal(render_data.begin() + 1, render_data.end(),
  261. data.begin()));
  262. // Reset the output buffer for another test.
  263. out_buf.clear();
  264. // Fill in the tuple buffer so as it reaches maximum allowed length. The
  265. // maximum length is 255 when the size of the length field is one byte.
  266. for (int i = 100; i < 255; ++i) {
  267. data.push_back(i);
  268. }
  269. ASSERT_EQ(255, data.size());
  270. tuple.assign(data.begin(), data.size());
  271. // The pack() should be successful again.
  272. ASSERT_NO_THROW(tuple.pack(out_buf));
  273. // The rendered buffer should be 256 bytes long. The first byte holds the
  274. // opaque data length, the remaining bytes hold the actual data.
  275. ASSERT_EQ(256, out_buf.getLength());
  276. // Check that the data is correct.
  277. render_data.assign(static_cast<const uint8_t*>(out_buf.getData()),
  278. static_cast<const uint8_t*>(out_buf.getData()) + 256);
  279. EXPECT_EQ(255, render_data[0]);
  280. EXPECT_TRUE(std::equal(render_data.begin() + 1, render_data.end(),
  281. data.begin()));
  282. // Clear output buffer for another test.
  283. out_buf.clear();
  284. // Add one more value to the tuple. Now, the resulting buffer should exceed
  285. // the maximum length. An attempt to pack() should fail.
  286. data.push_back(255);
  287. tuple.assign(data.begin(), data.size());
  288. EXPECT_THROW(tuple.pack(out_buf), OpaqueDataTupleError);
  289. }
  290. // This test checks that the tuple is correctly encoded in the wire format when
  291. // the size of the length field is 2 bytes.
  292. TEST(OpaqueDataTuple, pack2Bytes) {
  293. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  294. // Initially, the tuple should be empty.
  295. ASSERT_EQ(0, tuple.getLength());
  296. // The empty data doesn't make much sense, so the pack() should not
  297. // allow it.
  298. OutputBuffer out_buf(10);
  299. EXPECT_THROW(tuple.pack(out_buf), OpaqueDataTupleError);
  300. // Set the data for tuple.
  301. std::vector<uint8_t> data;
  302. for (int i = 0; i < 512; ++i) {
  303. data.push_back(i);
  304. }
  305. tuple.assign(data.begin(), data.size());
  306. // The pack should now succeed.
  307. ASSERT_NO_THROW(tuple.pack(out_buf));
  308. // The rendered buffer should be 514 bytes long - 2 bytes for length,
  309. // 512 bytes for the actual data.
  310. ASSERT_EQ(514, out_buf.getLength());
  311. // Get the rendered data into the vector for convenience.
  312. std::vector<uint8_t>
  313. render_data(static_cast<const uint8_t*>(out_buf.getData()),
  314. static_cast<const uint8_t*>(out_buf.getData()) + 514);
  315. // The first two bytes hold the length of 512.
  316. uint16_t len = (render_data[0] << 8) + render_data[1];
  317. EXPECT_EQ(512, len);
  318. // Verify that the rendered data is correct.
  319. EXPECT_TRUE(std::equal(render_data.begin() + 2, render_data.end(),
  320. data.begin()));
  321. // Without clearing the output buffer, try to do it again. The pack should
  322. // append the data to the current buffer.
  323. ASSERT_NO_THROW(tuple.pack(out_buf));
  324. EXPECT_EQ(1028, out_buf.getLength());
  325. }
  326. // This test verifies that the tuple is decoded from the wire format.
  327. TEST(OpaqueDataTuple, unpack1Byte) {
  328. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  329. const char wire_data[] = {
  330. 0x0B, // Length is 11
  331. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  332. 0x77, 0x6F, 0x72, 0x6C, 0x64 // world
  333. };
  334. ASSERT_NO_THROW(tuple.unpack(wire_data, wire_data + sizeof(wire_data)));
  335. EXPECT_EQ(11, tuple.getLength());
  336. EXPECT_EQ("Hello world", tuple.getText());
  337. }
  338. // This test verifies that the tuple having a length of 0, is decoded from
  339. // the wire format.
  340. TEST(OpaqueDataTuple, unpack1ByteZeroLength) {
  341. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  342. tuple = "Hello world";
  343. ASSERT_NE(tuple.getLength(), 0);
  344. const char wire_data[] = {
  345. 0
  346. };
  347. ASSERT_NO_THROW(tuple.unpack(wire_data, wire_data + sizeof(wire_data)));
  348. EXPECT_EQ(0, tuple.getLength());
  349. }
  350. // This test verfifies that exception is thrown if the empty buffer is being
  351. // parsed.
  352. TEST(OpaqueDataTuple, unpack1ByteEmptyBuffer) {
  353. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  354. const char wire_data[] = {
  355. 1, 2, 3
  356. };
  357. EXPECT_THROW(tuple.unpack(wire_data, wire_data), OpaqueDataTupleError);
  358. }
  359. // This test verifies that exception if thrown when parsing truncated buffer.
  360. TEST(OpaqueDataTuple, unpack1ByteTruncatedBuffer) {
  361. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  362. const char wire_data[] = {
  363. 10, 2, 3
  364. };
  365. EXPECT_THROW(tuple.unpack(wire_data, wire_data + sizeof(wire_data)),
  366. OpaqueDataTupleError);
  367. }
  368. // This test verifies that the tuple is decoded from the wire format.
  369. TEST(OpaqueDataTuple, unpack2Byte) {
  370. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  371. std::vector<uint8_t> wire_data;
  372. // Set tuple length to 400 (0x190).
  373. wire_data.push_back(1);
  374. wire_data.push_back(0x90);
  375. // Fill in the buffer with some data.
  376. for (int i = 0; i < 400; ++i) {
  377. wire_data.push_back(i);
  378. }
  379. // The unpack shoud succeed.
  380. ASSERT_NO_THROW(tuple.unpack(wire_data.begin(), wire_data.end()));
  381. // The decoded length should be 400.
  382. ASSERT_EQ(400, tuple.getLength());
  383. // And the data should match.
  384. EXPECT_TRUE(std::equal(wire_data.begin() + 2, wire_data.end(),
  385. tuple.getData().begin()));
  386. }
  387. // This test verifies that the tuple having a length of 0, is decoded from
  388. // the wire format.
  389. TEST(OpaqueDataTuple, unpack2ByteZeroLength) {
  390. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  391. // Set some data for the tuple.
  392. tuple = "Hello world";
  393. ASSERT_NE(tuple.getLength(), 0);
  394. // The buffer holds just a length field with the value of 0.
  395. const char wire_data[] = {
  396. 0, 0
  397. };
  398. // The empty tuple should be successfully decoded.
  399. ASSERT_NO_THROW(tuple.unpack(wire_data, wire_data + sizeof(wire_data)));
  400. // The data should be replaced with an empty buffer.
  401. EXPECT_EQ(0, tuple.getLength());
  402. }
  403. // This test verifies that exception is thrown if the empty buffer is being
  404. // parsed.
  405. TEST(OpaqueDataTuple, unpack2ByteEmptyBuffer) {
  406. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  407. // Initialize the input buffer with some data, just to avoid initializing
  408. // empty array.
  409. const char wire_data[] = {
  410. 1, 2, 3
  411. };
  412. // Pass empty buffer (first iterator equal to second iterator).
  413. // This should not be accepted.
  414. EXPECT_THROW(tuple.unpack(wire_data, wire_data), OpaqueDataTupleError);
  415. }
  416. // This test verifies that exception if thrown when parsing truncated buffer.
  417. TEST(OpaqueDataTuple, unpack2ByteTruncatedBuffer) {
  418. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  419. // Specify the data with the length of 10, but limit the buffer size to
  420. // 2 bytes.
  421. const char wire_data[] = {
  422. 0, 10, 2, 3
  423. };
  424. // This should fail because the buffer is truncated.
  425. EXPECT_THROW(tuple.unpack(wire_data, wire_data + sizeof(wire_data)),
  426. OpaqueDataTupleError);
  427. }
  428. } // anonymous namespace