option6_iaaddr_unittest.cc 2.9 KB

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