option6_iaaddr_unittest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2011 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 "dhcp/dhcp6.h"
  20. #include "dhcp/option.h"
  21. #include "dhcp/option6_iaaddr.h"
  22. using namespace std;
  23. using namespace isc;
  24. using namespace isc::dhcp;
  25. namespace {
  26. class Option6IAAddrTest : public ::testing::Test {
  27. public:
  28. Option6IAAddrTest() {
  29. }
  30. };
  31. TEST_F(Option6IAAddrTest, basic) {
  32. boost::shared_array<uint8_t> simple_buf(new uint8_t[128]);
  33. for (int i = 0; i < 128; i++)
  34. simple_buf[i] = 0;
  35. simple_buf[0] = 0x20;
  36. simple_buf[1] = 0x01;
  37. simple_buf[2] = 0x0d;
  38. simple_buf[3] = 0xb8;
  39. simple_buf[4] = 0x00;
  40. simple_buf[5] = 0x01;
  41. simple_buf[12] = 0xde;
  42. simple_buf[13] = 0xad;
  43. simple_buf[14] = 0xbe;
  44. simple_buf[15] = 0xef; // 2001:db8:1::dead:beef
  45. simple_buf[16] = 0x00;
  46. simple_buf[17] = 0x00;
  47. simple_buf[18] = 0x03;
  48. simple_buf[19] = 0xe8; // 1000
  49. simple_buf[20] = 0xb2;
  50. simple_buf[21] = 0xd0;
  51. simple_buf[22] = 0x5e;
  52. simple_buf[23] = 0x00; // 3,000,000,000
  53. // create an option (unpack content)
  54. Option6IAAddr* opt = new Option6IAAddr(D6O_IAADDR,
  55. simple_buf,
  56. 128,
  57. 0,
  58. 24);
  59. // pack this option again in the same buffer, but in
  60. // different place
  61. int offset = opt->pack(simple_buf, 128, 50);
  62. EXPECT_EQ(78, offset);
  63. // 4 bytes header + 4 bytes content
  64. EXPECT_EQ("2001:db8:1::dead:beef", opt->getAddress().toText());
  65. EXPECT_EQ(1000, opt->getPreferred());
  66. EXPECT_EQ(3000000000U, opt->getValid());
  67. EXPECT_EQ(D6O_IAADDR, opt->getType());
  68. EXPECT_EQ(Option::OPTION6_HDR_LEN + Option6IAAddr::OPTION6_IAADDR_LEN,
  69. opt->len());
  70. // check if pack worked properly:
  71. // if option type is correct
  72. EXPECT_EQ(D6O_IAADDR, simple_buf[50]*256 + simple_buf[51]);
  73. // if option length is correct
  74. EXPECT_EQ(24, simple_buf[52]*256 + simple_buf[53]);
  75. // if option content is correct
  76. EXPECT_EQ(0, memcmp(&simple_buf[0], &simple_buf[54],24));
  77. EXPECT_NO_THROW(
  78. delete opt;
  79. );
  80. }
  81. }