header_flags_unittest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <cstddef>
  15. #include <stdint.h>
  16. #include <gtest/gtest.h>
  17. #include "../option_info.h"
  18. #include "../header_flags.h"
  19. using namespace isc::badpacket;
  20. // Test fixture class
  21. class HeaderFlagsTest : public ::testing::Test {
  22. public:
  23. HeaderFlagsTest() {}
  24. /// \brief Checks that all options are zero
  25. ///
  26. /// Checks that all flags options are set to zero.
  27. ///
  28. /// \param flags Flags structure to check.
  29. /// \param except Index not to check. (This allows options not being tested
  30. /// to be checked to see that they are at the default value.) As all
  31. /// index values are positive, a negative value means check
  32. /// everything.
  33. void checkZero(const HeaderFlags& flags, int except = -1) {
  34. // Check individual fields
  35. for (int i = OptionInfo::FLAGS_START; i < OptionInfo::FLAGS_END; ++i) {
  36. if (i != except) {
  37. EXPECT_EQ(0, flags.get(i));
  38. }
  39. }
  40. // ... and check the composite
  41. if (except == -1) {
  42. EXPECT_EQ(0, flags.getValue());
  43. } else {
  44. EXPECT_NE(0, flags.getValue());
  45. }
  46. }
  47. /// \brief Check Option
  48. ///
  49. /// Checks that an option will only set the appropriate bits in the flags
  50. /// field.
  51. ///
  52. /// \param index Index of the flags field to check.
  53. /// \param maxval Maximum value of the header field.
  54. void checkOption(int index, uint32_t maxval) {
  55. // Create header flags and check initialized properly.
  56. HeaderFlags flags;
  57. checkZero(flags);
  58. // Check we can set field to maximum.
  59. flags.set(index, maxval);
  60. EXPECT_EQ(maxval, flags.get(index));
  61. checkZero(flags, index);
  62. // Check we can reset it to zero.
  63. flags.set(index, 0);
  64. checkZero(flags);
  65. }
  66. };
  67. // Set of tests to check that setting a bit only sets that bit and nothing
  68. // else.
  69. TEST_F(HeaderFlagsTest, fields) {
  70. checkOption(OptionInfo::QR, 1);
  71. checkOption(OptionInfo::OP, 15);
  72. checkOption(OptionInfo::AA, 1);
  73. checkOption(OptionInfo::TC, 1);
  74. checkOption(OptionInfo::RD, 1);
  75. checkOption(OptionInfo::RA, 1);
  76. checkOption(OptionInfo::Z, 1);
  77. checkOption(OptionInfo::AD, 1);
  78. checkOption(OptionInfo::CD, 1);
  79. checkOption(OptionInfo::RC, 15);
  80. }
  81. // Check that the correct bits are set
  82. TEST_F(HeaderFlagsTest, bitValues) {
  83. HeaderFlags flags;
  84. checkZero(flags);
  85. flags.set(OptionInfo::QR, 1);
  86. EXPECT_EQ(0x8000, flags.getValue());
  87. flags.set(OptionInfo::QR, 0);
  88. flags.set(OptionInfo::OP, 15);
  89. EXPECT_EQ(0x7800, flags.getValue());
  90. flags.set(OptionInfo::OP, 0);
  91. flags.set(OptionInfo::AA, 1);
  92. EXPECT_EQ(0x0400, flags.getValue());
  93. flags.set(OptionInfo::AA, 0);
  94. flags.set(OptionInfo::TC, 1);
  95. EXPECT_EQ(0x0200, flags.getValue());
  96. flags.set(OptionInfo::TC, 0);
  97. flags.set(OptionInfo::RD, 1);
  98. EXPECT_EQ(0x0100, flags.getValue());
  99. flags.set(OptionInfo::RD, 0);
  100. flags.set(OptionInfo::RA, 1);
  101. EXPECT_EQ(0x0080, flags.getValue());
  102. flags.set(OptionInfo::RA, 0);
  103. flags.set(OptionInfo::Z, 1);
  104. EXPECT_EQ(0x0040, flags.getValue());
  105. flags.set(OptionInfo::Z, 0);
  106. flags.set(OptionInfo::AD, 1);
  107. EXPECT_EQ(0x0020, flags.getValue());
  108. flags.set(OptionInfo::AD, 0);
  109. flags.set(OptionInfo::CD, 1);
  110. EXPECT_EQ(0x0010, flags.getValue());
  111. flags.set(OptionInfo::CD, 0);
  112. flags.set(OptionInfo::RC, 15);
  113. EXPECT_EQ(0x000F, flags.getValue());
  114. }