libdhcp++_unittest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright (C) 2011-2012 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 <iostream>
  16. #include <sstream>
  17. #include <arpa/inet.h>
  18. #include <gtest/gtest.h>
  19. #include <util/buffer.h>
  20. #include <dhcp/libdhcp++.h>
  21. #include "config.h"
  22. using namespace std;
  23. using namespace isc;
  24. using namespace isc::dhcp;
  25. using namespace isc::util;
  26. namespace {
  27. class LibDhcpTest : public ::testing::Test {
  28. public:
  29. LibDhcpTest() {
  30. }
  31. };
  32. static const uint8_t packed[] = {
  33. 0, 12, 0, 5, 100, 101, 102, 103, 104, // opt1 (9 bytes)
  34. 0, 13, 0, 3, 105, 106, 107, // opt2 (7 bytes)
  35. 0, 14, 0, 2, 108, 109, // opt3 (6 bytes)
  36. 1, 0, 0, 4, 110, 111, 112, 113, // opt4 (8 bytes)
  37. 1, 1, 0, 1, 114 // opt5 (5 bytes)
  38. };
  39. TEST(LibDhcpTest, packOptions6) {
  40. OptionBuffer buf(512);
  41. isc::dhcp::Option::OptionCollection opts; // list of options
  42. // generate content for options
  43. for (int i = 0; i < 64; i++) {
  44. buf[i]=i+100;
  45. }
  46. OptionPtr opt1(new Option(Option::V6, 12, buf.begin() + 0, buf.begin() + 5));
  47. OptionPtr opt2(new Option(Option::V6, 13, buf.begin() + 5, buf.begin() + 8));
  48. OptionPtr opt3(new Option(Option::V6, 14, buf.begin() + 8, buf.begin() + 10));
  49. OptionPtr opt4(new Option(Option::V6,256, buf.begin() + 10,buf.begin() + 14));
  50. OptionPtr opt5(new Option(Option::V6,257, buf.begin() + 14,buf.begin() + 15));
  51. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt1));
  52. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt2));
  53. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt3));
  54. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt4));
  55. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt5));
  56. OutputBuffer assembled(512);
  57. EXPECT_NO_THROW ({
  58. LibDHCP::packOptions6(assembled, opts);
  59. });
  60. EXPECT_EQ(35, assembled.getLength()); // options should take 35 bytes
  61. EXPECT_EQ(0, memcmp(assembled.getData(), packed, 35) );
  62. }
  63. TEST(LibDhcpTest, unpackOptions6) {
  64. // just couple of random options
  65. // Option is used as a simple option implementation
  66. // More advanced uses are validated in tests dedicated for
  67. // specific derived classes.
  68. isc::dhcp::Option::OptionCollection options; // list of options
  69. OptionBuffer buf(512);
  70. memcpy(&buf[0], packed, 35);
  71. EXPECT_NO_THROW ({
  72. LibDHCP::unpackOptions6(OptionBuffer(buf.begin(), buf.begin()+35), options);
  73. });
  74. EXPECT_EQ(options.size(), 5); // there should be 5 options
  75. isc::dhcp::Option::OptionCollection::const_iterator x = options.find(12);
  76. ASSERT_FALSE(x == options.end()); // option 1 should exist
  77. EXPECT_EQ(12, x->second->getType()); // this should be option 12
  78. ASSERT_EQ(9, x->second->len()); // it should be of length 9
  79. EXPECT_EQ(0, memcmp(&x->second->getData()[0], packed+4, 5)); // data len=5
  80. x = options.find(13);
  81. ASSERT_FALSE(x == options.end()); // option 13 should exist
  82. EXPECT_EQ(13, x->second->getType()); // this should be option 13
  83. ASSERT_EQ(7, x->second->len()); // it should be of length 7
  84. EXPECT_EQ(0, memcmp(&x->second->getData()[0], packed+13, 3)); // data len=3
  85. x = options.find(14);
  86. ASSERT_FALSE(x == options.end()); // option 3 should exist
  87. EXPECT_EQ(14, x->second->getType()); // this should be option 14
  88. ASSERT_EQ(6, x->second->len()); // it should be of length 6
  89. EXPECT_EQ(0, memcmp(&x->second->getData()[0], packed+20, 2)); // data len=2
  90. x = options.find(256);
  91. ASSERT_FALSE(x == options.end()); // option 256 should exist
  92. EXPECT_EQ(256, x->second->getType()); // this should be option 256
  93. ASSERT_EQ(8, x->second->len()); // it should be of length 7
  94. EXPECT_EQ(0, memcmp(&x->second->getData()[0], packed+26, 4)); // data len=4
  95. x = options.find(257);
  96. ASSERT_FALSE(x == options.end()); // option 257 should exist
  97. EXPECT_EQ(257, x->second->getType()); // this should be option 257
  98. ASSERT_EQ(5, x->second->len()); // it should be of length 5
  99. EXPECT_EQ(0, memcmp(&x->second->getData()[0], packed+34, 1)); // data len=1
  100. x = options.find(0);
  101. EXPECT_TRUE(x == options.end()); // option 0 not found
  102. x = options.find(1); // 1 is htons(256) on little endians. Worth checking
  103. EXPECT_TRUE(x == options.end()); // option 1 not found
  104. x = options.find(2);
  105. EXPECT_TRUE(x == options.end()); // option 2 not found
  106. x = options.find(32000);
  107. EXPECT_TRUE(x == options.end()); // option 32000 not found
  108. }
  109. static uint8_t v4Opts[] = {
  110. 12, 3, 0, 1, 2,
  111. 13, 3, 10, 11, 12,
  112. 14, 3, 20, 21, 22,
  113. 254, 3, 30, 31, 32,
  114. 128, 3, 40, 41, 42
  115. };
  116. TEST(LibDhcpTest, packOptions4) {
  117. vector<uint8_t> payload[5];
  118. for (int i = 0; i < 5; i++) {
  119. payload[i].resize(3);
  120. payload[i][0] = i*10;
  121. payload[i][1] = i*10+1;
  122. payload[i][2] = i*10+2;
  123. }
  124. OptionPtr opt1(new Option(Option::V4, 12, payload[0]));
  125. OptionPtr opt2(new Option(Option::V4, 13, payload[1]));
  126. OptionPtr opt3(new Option(Option::V4, 14, payload[2]));
  127. OptionPtr opt4(new Option(Option::V4,254, payload[3]));
  128. OptionPtr opt5(new Option(Option::V4,128, payload[4]));
  129. isc::dhcp::Option::OptionCollection opts; // list of options
  130. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt1));
  131. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt2));
  132. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt3));
  133. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt4));
  134. opts.insert(pair<int, OptionPtr >(opt1->getType(), opt5));
  135. vector<uint8_t> expVect(v4Opts, v4Opts + sizeof(v4Opts));
  136. OutputBuffer buf(100);
  137. EXPECT_NO_THROW (
  138. LibDHCP::packOptions(buf, opts);
  139. );
  140. ASSERT_EQ(buf.getLength(), sizeof(v4Opts));
  141. EXPECT_EQ(0, memcmp(v4Opts, buf.getData(), sizeof(v4Opts)));
  142. }
  143. TEST(LibDhcpTest, unpackOptions4) {
  144. vector<uint8_t> packed(v4Opts, v4Opts + sizeof(v4Opts));
  145. isc::dhcp::Option::OptionCollection options; // list of options
  146. ASSERT_NO_THROW(
  147. LibDHCP::unpackOptions4(packed, options);
  148. );
  149. isc::dhcp::Option::OptionCollection::const_iterator x = options.find(12);
  150. ASSERT_FALSE(x == options.end()); // option 1 should exist
  151. EXPECT_EQ(12, x->second->getType()); // this should be option 12
  152. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  153. EXPECT_EQ(5, x->second->len()); // total option length 5
  154. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+2, 3)); // data len=3
  155. x = options.find(13);
  156. ASSERT_FALSE(x == options.end()); // option 1 should exist
  157. EXPECT_EQ(13, x->second->getType()); // this should be option 13
  158. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  159. EXPECT_EQ(5, x->second->len()); // total option length 5
  160. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+7, 3)); // data len=3
  161. x = options.find(14);
  162. ASSERT_FALSE(x == options.end()); // option 3 should exist
  163. EXPECT_EQ(14, x->second->getType()); // this should be option 14
  164. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  165. EXPECT_EQ(5, x->second->len()); // total option length 5
  166. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+12, 3)); // data len=3
  167. x = options.find(254);
  168. ASSERT_FALSE(x == options.end()); // option 3 should exist
  169. EXPECT_EQ(254, x->second->getType()); // this should be option 254
  170. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  171. EXPECT_EQ(5, x->second->len()); // total option length 5
  172. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+17, 3)); // data len=3
  173. x = options.find(128);
  174. ASSERT_FALSE(x == options.end()); // option 3 should exist
  175. EXPECT_EQ(128, x->second->getType()); // this should be option 254
  176. ASSERT_EQ(3, x->second->getData().size()); // it should be of length 3
  177. EXPECT_EQ(5, x->second->len()); // total option length 5
  178. EXPECT_EQ(0, memcmp(&x->second->getData()[0], v4Opts+22, 3)); // data len=3
  179. x = options.find(0);
  180. EXPECT_TRUE(x == options.end()); // option 0 not found
  181. x = options.find(1);
  182. EXPECT_TRUE(x == options.end()); // option 1 not found
  183. x = options.find(2);
  184. EXPECT_TRUE(x == options.end()); // option 2 not found
  185. }
  186. }