option_vendor_class_unittest.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 <exceptions/exceptions.h>
  16. #include <dhcp/option_vendor_class.h>
  17. #include <util/buffer.h>
  18. #include <gtest/gtest.h>
  19. using namespace isc;
  20. using namespace isc::dhcp;
  21. using namespace isc::util;
  22. namespace {
  23. // This test checks that the DHCPv4 option constructor sets the default
  24. // properties to the expected values. This constructor should add an
  25. // empty opaque data tuple (it is essentially the same as adding a 1-byte
  26. // long field which carries a value of 0).
  27. TEST(OptionVendorClass, constructor4) {
  28. OptionVendorClass vendor_class(Option::V4, 1234);
  29. EXPECT_EQ(1234, vendor_class.getVendorId());
  30. // Option length is 1 byte for header + 1 byte for option size +
  31. // 4 bytes of enterprise id + 1 byte for opaque data.
  32. EXPECT_EQ(7, vendor_class.len());
  33. // There should be one empty tuple.
  34. ASSERT_EQ(1, vendor_class.getTuplesNum());
  35. EXPECT_EQ(0, vendor_class.getTuple(0).getLength());
  36. }
  37. // This test checks that the DHCPv6 option constructor sets the default
  38. // properties to the expected values.
  39. TEST(OptionVendorClass, constructor6) {
  40. OptionVendorClass vendor_class(Option::V6, 2345);
  41. EXPECT_EQ(2345, vendor_class.getVendorId());
  42. // Option length is 2 bytes for option code + 2 bytes for option size +
  43. // 4 bytes of enterprise id.
  44. EXPECT_EQ(8, vendor_class.len());
  45. // There should be no tuples.
  46. EXPECT_EQ(0, vendor_class.getTuplesNum());
  47. }
  48. // This test verifies that it is possible to append the opaque data tuple
  49. // to the option and then retrieve it.
  50. TEST(OptionVendorClass, addTuple) {
  51. OptionVendorClass vendor_class(Option::V6, 2345);
  52. // Initially there should be no tuples (for DHCPv6).
  53. ASSERT_EQ(0, vendor_class.getTuplesNum());
  54. // Create a new tuple and add it to the option.
  55. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  56. tuple = "xyz";
  57. vendor_class.addTuple(tuple);
  58. // The option should now hold one tuple.
  59. ASSERT_EQ(1, vendor_class.getTuplesNum());
  60. EXPECT_EQ("xyz", vendor_class.getTuple(0).getText());
  61. // Add another tuple.
  62. tuple = "abc";
  63. vendor_class.addTuple(tuple);
  64. // The option should now hold exactly two tuples in the order in which
  65. // they were added.
  66. ASSERT_EQ(2, vendor_class.getTuplesNum());
  67. EXPECT_EQ("xyz", vendor_class.getTuple(0).getText());
  68. EXPECT_EQ("abc", vendor_class.getTuple(1).getText());
  69. // Check that hasTuple correctly identifies existing tuples.
  70. EXPECT_TRUE(vendor_class.hasTuple("xyz"));
  71. EXPECT_TRUE(vendor_class.hasTuple("abc"));
  72. EXPECT_FALSE(vendor_class.hasTuple("other"));
  73. // Attempt to add the tuple with 1 byte long length field should fail
  74. // for DHCPv6 option.
  75. OpaqueDataTuple tuple2(OpaqueDataTuple::LENGTH_1_BYTE);
  76. EXPECT_THROW(vendor_class.addTuple(tuple2), isc::BadValue);
  77. }
  78. // This test checks that it is possible to replace existing tuple.
  79. TEST(OptionVendorClass, setTuple) {
  80. OptionVendorClass vendor_class(Option::V4, 1234);
  81. // The DHCPv4 option should carry one empty tuple.
  82. ASSERT_EQ(1, vendor_class.getTuplesNum());
  83. ASSERT_TRUE(vendor_class.getTuple(0).getText().empty());
  84. // Replace the empty tuple with non-empty one.
  85. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  86. tuple = "xyz";
  87. ASSERT_NO_THROW(vendor_class.setTuple(0, tuple));
  88. // There should be one tuple with updated data.
  89. ASSERT_EQ(1, vendor_class.getTuplesNum());
  90. EXPECT_EQ("xyz", vendor_class.getTuple(0).getText());
  91. // Add another one.
  92. tuple = "abc";
  93. vendor_class.addTuple(tuple);
  94. ASSERT_EQ(2, vendor_class.getTuplesNum());
  95. ASSERT_EQ("abc", vendor_class.getTuple(1).getText());
  96. // Try to replace them with new tuples.
  97. tuple = "new_xyz";
  98. ASSERT_NO_THROW(vendor_class.setTuple(0, tuple));
  99. ASSERT_EQ(2, vendor_class.getTuplesNum());
  100. EXPECT_EQ("new_xyz", vendor_class.getTuple(0).getText());
  101. tuple = "new_abc";
  102. ASSERT_NO_THROW(vendor_class.setTuple(1, tuple));
  103. ASSERT_EQ(2, vendor_class.getTuplesNum());
  104. EXPECT_EQ("new_abc", vendor_class.getTuple(1).getText());
  105. // For out of range position, exception should be thrown.
  106. tuple = "foo";
  107. EXPECT_THROW(vendor_class.setTuple(2, tuple), isc::OutOfRange);
  108. // Attempt to add the tuple with 2 byte long length field should fail
  109. // for DHCPv4 option.
  110. OpaqueDataTuple tuple2(OpaqueDataTuple::LENGTH_2_BYTES);
  111. EXPECT_THROW(vendor_class.addTuple(tuple2), isc::BadValue);
  112. }
  113. // Check that the returned length of the DHCPv4 option is correct.
  114. TEST(OptionVendorClass, len4) {
  115. OptionVendorClass vendor_class(Option::V4, 1234);
  116. ASSERT_EQ(7, vendor_class.len());
  117. // Replace the default empty tuple.
  118. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  119. tuple = "xyz";
  120. ASSERT_NO_THROW(vendor_class.setTuple(0, tuple));
  121. // The total length should get increased by the size of 'xyz'.
  122. EXPECT_EQ(10, vendor_class.len());
  123. // Add another tuple.
  124. tuple = "abc";
  125. vendor_class.addTuple(tuple);
  126. // The total size now grows by the additional enterprise id and the
  127. // 1 byte of the tuple length field and 3 bytes of 'abc'.
  128. EXPECT_EQ(18, vendor_class.len());
  129. }
  130. // Check that the returned length of the DHCPv6 option is correct.
  131. TEST(OptionVendorClass, len6) {
  132. OptionVendorClass vendor_class(Option::V6, 1234);
  133. ASSERT_EQ(8, vendor_class.len());
  134. // Add first tuple.
  135. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  136. tuple = "xyz";
  137. ASSERT_NO_THROW(vendor_class.addTuple(tuple));
  138. // The total length grows by 2 bytes of the length field and 3 bytes
  139. // consumed by 'xyz'.
  140. EXPECT_EQ(13, vendor_class.len());
  141. // Add another tuple and check that the total size gets increased.
  142. tuple = "abc";
  143. vendor_class.addTuple(tuple);
  144. EXPECT_EQ(18, vendor_class.len());
  145. }
  146. // Check that the option is rendered to the buffer in wire format.
  147. TEST(OptionVendorClass, pack4) {
  148. OptionVendorClass vendor_class(Option::V4, 1234);
  149. ASSERT_EQ(1, vendor_class.getTuplesNum());
  150. // By default, there is an empty tuple in the option. Let's replace
  151. // it with the tuple with some data.
  152. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  153. tuple = "Hello world";
  154. vendor_class.setTuple(0, tuple);
  155. // And add another tuple so as resulting option is a bit more complex.
  156. tuple = "foo";
  157. vendor_class.addTuple(tuple);
  158. // Render the data to the buffer.
  159. OutputBuffer buf(10);
  160. ASSERT_NO_THROW(vendor_class.pack(buf));
  161. ASSERT_EQ(26, buf.getLength());
  162. // Prepare reference data.
  163. const uint8_t ref[] = {
  164. 0x7C, 0x18, // option 124, length 24
  165. 0, 0, 0x4, 0xD2, // enterprise id 1234
  166. 0x0B, // tuple length is 11
  167. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  168. 0x77, 0x6F, 0x72, 0x6C, 0x64, // world
  169. 0, 0, 0x4, 0xD2, // enterprise id 1234
  170. 3, // tuple length is 3
  171. 0x66, 0x6F, 0x6F // foo
  172. };
  173. // Compare the buffer with reference data.
  174. EXPECT_EQ(0, memcmp(static_cast<const void*>(ref),
  175. static_cast<const void*>(buf.getData()), 26));
  176. }
  177. // Check that the DHCPv6 option is rendered to the buffer in wire format.
  178. TEST(OptionVendorClass, pack6) {
  179. OptionVendorClass vendor_class(Option::V6, 1234);
  180. ASSERT_EQ(0, vendor_class.getTuplesNum());
  181. // Add tuple.
  182. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  183. tuple = "Hello world";
  184. vendor_class.addTuple(tuple);
  185. // And add another tuple so as resulting option is a bit more complex.
  186. tuple = "foo";
  187. vendor_class.addTuple(tuple);
  188. // Render the data to the buffer.
  189. OutputBuffer buf(10);
  190. ASSERT_NO_THROW(vendor_class.pack(buf));
  191. ASSERT_EQ(26, buf.getLength());
  192. // Prepare reference data.
  193. const uint8_t ref[] = {
  194. 0x00, 0x10, 0x00, 0x16, // option 16, length 22
  195. 0x00, 0x00, 0x04, 0xD2, // enterprise id 1234
  196. 0x00, 0x0B, // tuple length is 11
  197. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  198. 0x77, 0x6F, 0x72, 0x6C, 0x64, // world
  199. 0x00, 0x03, // tuple length is 3
  200. 0x66, 0x6F, 0x6F // foo
  201. };
  202. // Compare the buffer with reference data.
  203. EXPECT_EQ(0, memcmp(static_cast<const void*>(ref),
  204. static_cast<const void*>(buf.getData()),
  205. buf.getLength()));
  206. }
  207. // This function checks that the DHCPv4 option with two opaque data tuples
  208. // is parsed correctly.
  209. TEST(OptionVendorClass, unpack4) {
  210. // Prepare data to decode.
  211. const uint8_t buf_data[] = {
  212. 0, 0, 0x4, 0xD2, // enterprise id 1234
  213. 0x0B, // tuple length is 11
  214. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  215. 0x77, 0x6F, 0x72, 0x6C, 0x64, // world
  216. 0, 0, 0x4, 0xD2, // enterprise id 1234
  217. 3, // tuple length is 3
  218. 0x66, 0x6F, 0x6F // foo
  219. };
  220. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  221. OptionVendorClassPtr vendor_class;
  222. ASSERT_NO_THROW(
  223. vendor_class = OptionVendorClassPtr(new OptionVendorClass(Option::V4,
  224. buf.begin(),
  225. buf.end()));
  226. );
  227. EXPECT_EQ(DHO_VIVCO_SUBOPTIONS, vendor_class->getType());
  228. EXPECT_EQ(1234, vendor_class->getVendorId());
  229. ASSERT_EQ(2, vendor_class->getTuplesNum());
  230. EXPECT_EQ("Hello world", vendor_class->getTuple(0).getText());
  231. EXPECT_EQ("foo", vendor_class->getTuple(1).getText());
  232. }
  233. // This function checks that the DHCPv4 option with two opaque data tuples
  234. // is parsed correctly.
  235. TEST(OptionVendorClass, unpack6) {
  236. // Prepare data to decode.
  237. const uint8_t buf_data[] = {
  238. 0, 0, 0x4, 0xD2, // enterprise id 1234
  239. 0x00, 0x0B, // tuple length is 11
  240. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  241. 0x77, 0x6F, 0x72, 0x6C, 0x64, // world
  242. 0x00, 0x03, // tuple length is 3
  243. 0x66, 0x6F, 0x6F // foo
  244. };
  245. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  246. OptionVendorClassPtr vendor_class;
  247. ASSERT_NO_THROW(
  248. vendor_class = OptionVendorClassPtr(new OptionVendorClass(Option::V6,
  249. buf.begin(),
  250. buf.end()));
  251. );
  252. EXPECT_EQ(D6O_VENDOR_CLASS, vendor_class->getType());
  253. EXPECT_EQ(1234, vendor_class->getVendorId());
  254. ASSERT_EQ(2, vendor_class->getTuplesNum());
  255. EXPECT_EQ("Hello world", vendor_class->getTuple(0).getText());
  256. EXPECT_EQ("foo", vendor_class->getTuple(1).getText());
  257. }
  258. // This test checks that the DHCPv4 option with opaque data of size 0
  259. // is correctly parsed.
  260. TEST(OptionVendorClass, unpack4EmptyTuple) {
  261. // Prepare data to decode.
  262. const uint8_t buf_data[] = {
  263. 0, 0, 0x4, 0xD2, // enterprise id 1234
  264. 0x00, // tuple length is 0
  265. };
  266. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  267. OptionVendorClassPtr vendor_class;
  268. ASSERT_NO_THROW(
  269. vendor_class = OptionVendorClassPtr(new OptionVendorClass(Option::V4,
  270. buf.begin(),
  271. buf.end()));
  272. );
  273. EXPECT_EQ(DHO_VIVCO_SUBOPTIONS, vendor_class->getType());
  274. EXPECT_EQ(1234, vendor_class->getVendorId());
  275. ASSERT_EQ(1, vendor_class->getTuplesNum());
  276. EXPECT_TRUE(vendor_class->getTuple(0).getText().empty());
  277. }
  278. // This test checks that the DHCPv6 option with opaque data of size 0
  279. // is correctly parsed.
  280. TEST(OptionVendorClass, unpack6EmptyTuple) {
  281. // Prepare data to decode.
  282. const uint8_t buf_data[] = {
  283. 0, 0, 0x4, 0xD2, // enterprise id 1234
  284. 0x00, 0x00 // tuple length is 0
  285. };
  286. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  287. OptionVendorClassPtr vendor_class;
  288. ASSERT_NO_THROW(
  289. vendor_class = OptionVendorClassPtr(new OptionVendorClass(Option::V6,
  290. buf.begin(),
  291. buf.end()));
  292. );
  293. EXPECT_EQ(D6O_VENDOR_CLASS, vendor_class->getType());
  294. EXPECT_EQ(1234, vendor_class->getVendorId());
  295. ASSERT_EQ(1, vendor_class->getTuplesNum());
  296. EXPECT_TRUE(vendor_class->getTuple(0).getText().empty());
  297. }
  298. // This test checks that exception is thrown when parsing truncated DHCPv4
  299. // V-I Vendor Class option.
  300. TEST(OptionVendorClass, unpack4Truncated) {
  301. // Prepare data to decode.
  302. const uint8_t buf_data[] = {
  303. 0, 0, 0x4, 0xD2, // enterprise id 1234
  304. 0x0B, // tuple length is 11
  305. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  306. 0x77, 0x6F, 0x72, 0x6C, 0x64, // world
  307. 0, 0, 0x4, 0xD2, // enterprise id 1234
  308. };
  309. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  310. EXPECT_THROW(OptionVendorClass (Option::V4, buf.begin(), buf.end()),
  311. isc::OutOfRange);
  312. }
  313. // This test checks that exception is thrown when parsing truncated DHCPv6
  314. // Vendor Class option.
  315. TEST(OptionVendorClass, unpack6Truncated) {
  316. // Prepare data to decode.
  317. const uint8_t buf_data[] = {
  318. 0, 0, 0x4, 0xD2, // enterprise id 1234
  319. 0x00, 0x0B, // tuple length is 11
  320. 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // Hello<space>
  321. 0x77, 0x6F, 0x72, 0x6C // worl (truncated d!)
  322. };
  323. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  324. EXPECT_THROW(OptionVendorClass (Option::V6, buf.begin(), buf.end()),
  325. isc::dhcp::OpaqueDataTupleError);
  326. }
  327. // This test checks that exception is thrown when parsing DHCPv4 V-I Vendor
  328. // Class option which doesn't have opaque data length. This test is different
  329. // from the corresponding test for v6 in that, the v4 test expects that
  330. // exception is thrown when parsing DHCPv4 option without data-len field
  331. // (has no tuples), whereas for DHCPv6 option it is perfectly ok that
  332. // option has no tuples (see class constructor).
  333. TEST(OptionVendorClass, unpack4NoTuple) {
  334. // Prepare data to decode.
  335. const uint8_t buf_data[] = {
  336. 0, 0, 0x4, 0xD2 // enterprise id 1234
  337. };
  338. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  339. ASSERT_THROW(OptionVendorClass (Option::V4, buf.begin(), buf.end()),
  340. isc::OutOfRange);
  341. }
  342. // This test checks that the DHCPv6 Vendor Class option containing no opaque
  343. // data is parsed correctly. This test is different from the corresponding
  344. // test for v4 in that, the v6 test checks that the option parsing succeeds
  345. // when option has no opaque data tuples, whereas the v4 test expects that
  346. // parsing fails for DHCPv4 option which doesn't have opaque-data (see
  347. // class constructor).
  348. TEST(OptionVendorClass, unpack6NoTuple) {
  349. // Prepare data to decode.
  350. const uint8_t buf_data[] = {
  351. 0, 0, 0x4, 0xD2 // enterprise id 1234
  352. };
  353. OptionBuffer buf(buf_data, buf_data + sizeof(buf_data));
  354. OptionVendorClassPtr vendor_class;
  355. ASSERT_NO_THROW(
  356. vendor_class = OptionVendorClassPtr(new OptionVendorClass(Option::V6,
  357. buf.begin(),
  358. buf.end()));
  359. );
  360. EXPECT_EQ(D6O_VENDOR_CLASS, vendor_class->getType());
  361. EXPECT_EQ(1234, vendor_class->getVendorId());
  362. EXPECT_EQ(0, vendor_class->getTuplesNum());
  363. }
  364. // Verifies correctness of the text representation of the DHCPv4 option.
  365. TEST(OptionVendorClass, toText4) {
  366. OptionVendorClass vendor_class(Option::V4, 1234);
  367. ASSERT_EQ(1, vendor_class.getTuplesNum());
  368. // By default, there is an empty tuple in the option. Let's replace
  369. // it with the tuple with some data.
  370. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_1_BYTE);
  371. tuple = "Hello world";
  372. vendor_class.setTuple(0, tuple);
  373. // And add another tuple so as resulting option is a bit more complex.
  374. tuple = "foo";
  375. vendor_class.addTuple(tuple);
  376. // Check that the text representation of the option is as expected.
  377. EXPECT_EQ("type=124, len=24, enterprise id=0x4d2,"
  378. " data-len0=11, vendor-class-data0='Hello world',"
  379. " enterprise id=0x4d2, data-len1=3, vendor-class-data1='foo'",
  380. vendor_class.toText());
  381. // Check that indentation works.
  382. EXPECT_EQ(" type=124, len=24, enterprise id=0x4d2,"
  383. " data-len0=11, vendor-class-data0='Hello world',"
  384. " enterprise id=0x4d2, data-len1=3, vendor-class-data1='foo'",
  385. vendor_class.toText(3));
  386. }
  387. // Verifies correctness of the text representation of the DHCPv4 option.
  388. TEST(OptionVendorClass, toText6) {
  389. OptionVendorClass vendor_class(Option::V6, 1234);
  390. ASSERT_EQ(0, vendor_class.getTuplesNum());
  391. // By default, there is an empty tuple in the option. Let's replace
  392. // it with the tuple with some data.
  393. OpaqueDataTuple tuple(OpaqueDataTuple::LENGTH_2_BYTES);
  394. tuple = "Hello world";
  395. vendor_class.addTuple(tuple);
  396. // And add another tuple so as resulting option is a bit more complex.
  397. tuple = "foo";
  398. vendor_class.addTuple(tuple);
  399. // Check that the text representation of the option is as expected.
  400. EXPECT_EQ("type=16, len=22, enterprise id=0x4d2,"
  401. " data-len0=11, vendor-class-data0='Hello world',"
  402. " data-len1=3, vendor-class-data1='foo'",
  403. vendor_class.toText());
  404. // Check that indentation works.
  405. EXPECT_EQ(" type=16, len=22, enterprise id=0x4d2,"
  406. " data-len0=11, vendor-class-data0='Hello world',"
  407. " data-len1=3, vendor-class-data1='foo'",
  408. vendor_class.toText(2));
  409. }
  410. } // end of anonymous namespace