asiolink_utilities_unittest.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /// \brief Test of asiolink utilties
  15. ///
  16. /// Tests the fuctionality of the asiolink utilities code by comparing them
  17. /// with the equivalent methods in isc::dns::[Input/Output]Buffer.
  18. #include <cstddef>
  19. #include <gtest/gtest.h>
  20. #include <dns/buffer.h>
  21. #include <asiolink/asiolink_utilities.h>
  22. using namespace asiolink;
  23. using namespace isc::dns;
  24. TEST(asioutil, readUint16) {
  25. // Reference buffer
  26. uint8_t data[2];
  27. isc::dns::InputBuffer buffer(data, sizeof(data));
  28. // Avoid possible compiler warnings by only setting uint8_t variables to
  29. // uint8_t values.
  30. uint8_t i8 = 0;
  31. uint8_t j8 = 0;
  32. for (int i = 0; i < (2 << 8); ++i, ++i8) {
  33. for (int j = 0; j < (2 << 8); ++j, ++j8) {
  34. data[0] = i8;
  35. data[1] = j8;
  36. buffer.setPosition(0);
  37. EXPECT_EQ(buffer.readUint16(), readUint16(data));
  38. }
  39. }
  40. }
  41. TEST(asioutil, writeUint16) {
  42. // Reference buffer
  43. isc::dns::OutputBuffer buffer(2);
  44. uint8_t test[2];
  45. // Avoid possible compiler warnings by only setting uint16_t variables to
  46. // uint16_t values.
  47. uint16_t i16 = 0;
  48. for (uint32_t i = 0; i < (2 << 16); ++i, ++i16) {
  49. // Write the reference data
  50. buffer.clear();
  51. buffer.writeUint16(i16);
  52. // ... and the test data
  53. writeUint16(i16, test);
  54. // ... and compare
  55. const uint8_t* ref = static_cast<const uint8_t*>(buffer.getData());
  56. EXPECT_EQ(ref[0], test[0]);
  57. EXPECT_EQ(ref[1], test[1]);
  58. }
  59. }