memory_segment_common_unittest.cc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2013 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 <util/memory_segment.h>
  15. #include <exceptions/exceptions.h>
  16. #include <gtest/gtest.h>
  17. #include <cstring>
  18. #include <stdint.h>
  19. namespace isc {
  20. namespace util {
  21. namespace test {
  22. void
  23. checkSegmentNamedAddress(MemorySegment& segment, bool out_of_segment_ok) {
  24. // NULL name is not allowed.
  25. EXPECT_THROW(segment.getNamedAddress(NULL), InvalidParameter);
  26. // If the name does not exist, NULL should be returned.
  27. EXPECT_EQ(static_cast<void*>(NULL),
  28. segment.getNamedAddress("test address"));
  29. // Now set it
  30. void* ptr32 = segment.allocate(sizeof(uint32_t));
  31. const uint32_t test_val = 42;
  32. *static_cast<uint32_t*>(ptr32) = test_val;
  33. EXPECT_FALSE(segment.setNamedAddress("test address", ptr32));
  34. // NULL name isn't allowed.
  35. EXPECT_THROW(segment.setNamedAddress(NULL, ptr32), InvalidParameter);
  36. // we can now get it; the stored value should be intact.
  37. EXPECT_EQ(ptr32, segment.getNamedAddress("test address"));
  38. EXPECT_EQ(test_val, *static_cast<const uint32_t*>(ptr32));
  39. // Override it.
  40. void* ptr16 = segment.allocate(sizeof(uint16_t));
  41. const uint16_t test_val16 = 4200;
  42. *static_cast<uint16_t*>(ptr16) = test_val16;
  43. EXPECT_FALSE(segment.setNamedAddress("test address", ptr16));
  44. EXPECT_EQ(ptr16, segment.getNamedAddress("test address"));
  45. EXPECT_EQ(test_val16, *static_cast<const uint16_t*>(ptr16));
  46. // Clear it. Then we won't be able to find it any more.
  47. EXPECT_TRUE(segment.clearNamedAddress("test address"));
  48. EXPECT_EQ(static_cast<void*>(NULL),
  49. segment.getNamedAddress("test address"));
  50. // duplicate attempt of clear will result in false as it doesn't exist.
  51. EXPECT_FALSE(segment.clearNamedAddress("test address"));
  52. // Setting NULL is okay.
  53. EXPECT_FALSE(segment.setNamedAddress("null address", NULL));
  54. EXPECT_EQ(static_cast<void*>(NULL),
  55. segment.getNamedAddress("null address"));
  56. // If the underlying implementation performs explicit check against
  57. // out-of-segment address, confirm the behavior.
  58. if (!out_of_segment_ok) {
  59. uint8_t ch = 'A';
  60. EXPECT_THROW(segment.setNamedAddress("local address", &ch),
  61. MemorySegmentError);
  62. }
  63. // clean them up all
  64. segment.deallocate(ptr32, sizeof(uint32_t));
  65. EXPECT_FALSE(segment.allMemoryDeallocated()); // not fully deallocated
  66. segment.deallocate(ptr16, sizeof(uint16_t)); // not yet
  67. EXPECT_FALSE(segment.allMemoryDeallocated());
  68. EXPECT_TRUE(segment.clearNamedAddress("null address"));
  69. // null name isn't allowed:
  70. EXPECT_THROW(segment.clearNamedAddress(NULL), InvalidParameter);
  71. EXPECT_TRUE(segment.allMemoryDeallocated()); // now everything is gone
  72. }
  73. }
  74. }
  75. }