|
@@ -287,12 +287,14 @@ void
|
|
|
checkNamedData(const std::string& name, const std::vector<uint8_t>& data,
|
|
|
MemorySegment& sgmt, bool delete_after_check = false)
|
|
|
{
|
|
|
- void* dp = sgmt.getNamedAddress(name.c_str());
|
|
|
- ASSERT_TRUE(dp);
|
|
|
- EXPECT_EQ(0, std::memcmp(dp, &data[0], data.size()));
|
|
|
+ const MemorySegment::NamedAddressResult result =
|
|
|
+ sgmt.getNamedAddress(name.c_str());
|
|
|
+ ASSERT_TRUE(result.first);
|
|
|
+ ASSERT_TRUE(result.second);
|
|
|
+ EXPECT_EQ(0, std::memcmp(result.second, &data[0], data.size()));
|
|
|
|
|
|
if (delete_after_check) {
|
|
|
- sgmt.deallocate(dp, data.size());
|
|
|
+ sgmt.deallocate(result.second, data.size());
|
|
|
sgmt.clearNamedAddress(name.c_str());
|
|
|
}
|
|
|
}
|
|
@@ -309,10 +311,10 @@ TEST_F(MemorySegmentMappedTest, namedAddress) {
|
|
|
segment_.reset(); // close it before opening another one
|
|
|
|
|
|
segment_.reset(new MemorySegmentMapped(mapped_file));
|
|
|
- EXPECT_NE(static_cast<void*>(NULL),
|
|
|
- segment_->getNamedAddress("test address"));
|
|
|
- EXPECT_EQ(test_val16, *static_cast<const uint16_t*>(
|
|
|
- segment_->getNamedAddress("test address")));
|
|
|
+ const MemorySegment::NamedAddressResult result =
|
|
|
+ segment_->getNamedAddress("test address");
|
|
|
+ ASSERT_TRUE(result.first);
|
|
|
+ EXPECT_EQ(test_val16, *static_cast<const uint16_t*>(result.second));
|
|
|
|
|
|
// try to set an unusually long name. We re-create the file so
|
|
|
// creating the name would cause allocation failure and trigger internal
|
|
@@ -323,7 +325,7 @@ TEST_F(MemorySegmentMappedTest, namedAddress) {
|
|
|
const std::string long_name(1025, 'x'); // definitely larger than segment
|
|
|
// setNamedAddress should return true, indicating segment has grown.
|
|
|
EXPECT_TRUE(segment_->setNamedAddress(long_name.c_str(), NULL));
|
|
|
- EXPECT_EQ(static_cast<void*>(NULL),
|
|
|
+ EXPECT_EQ(MemorySegment::NamedAddressResult(true, NULL),
|
|
|
segment_->getNamedAddress(long_name.c_str()));
|
|
|
|
|
|
// Check contents pointed by named addresses survive growing and
|
|
@@ -410,10 +412,12 @@ TEST_F(MemorySegmentMappedTest, multiProcess) {
|
|
|
EXPECT_EQ(0, from_parent);
|
|
|
|
|
|
MemorySegmentMapped sgmt(mapped_file);
|
|
|
- void* ptr_child = sgmt.getNamedAddress("test address");
|
|
|
- EXPECT_TRUE(ptr_child);
|
|
|
- if (ptr_child) {
|
|
|
- const uint32_t val = *static_cast<const uint32_t*>(ptr_child);
|
|
|
+ const MemorySegment::NamedAddressResult result =
|
|
|
+ sgmt.getNamedAddress("test address");
|
|
|
+ ASSERT_TRUE(result.first);
|
|
|
+ EXPECT_TRUE(result.second);
|
|
|
+ if (result.second) {
|
|
|
+ const uint32_t val = *static_cast<const uint32_t*>(result.second);
|
|
|
EXPECT_EQ(424242, val);
|
|
|
// tell the parent whether it succeeded. 0 means it did,
|
|
|
// 0xff means it failed.
|
|
@@ -425,9 +429,11 @@ TEST_F(MemorySegmentMappedTest, multiProcess) {
|
|
|
// parent: open another read-only segment, then tell the child to open
|
|
|
// its own segment.
|
|
|
segment_.reset(new MemorySegmentMapped(mapped_file));
|
|
|
- ptr = segment_->getNamedAddress("test address");
|
|
|
- ASSERT_TRUE(ptr);
|
|
|
- EXPECT_EQ(424242, *static_cast<const uint32_t*>(ptr));
|
|
|
+ const MemorySegment::NamedAddressResult result =
|
|
|
+ segment_->getNamedAddress("test address");
|
|
|
+ ASSERT_TRUE(result.first);
|
|
|
+ ASSERT_TRUE(result.second);
|
|
|
+ EXPECT_EQ(424242, *static_cast<const uint32_t*>(result.second));
|
|
|
const char some_data = 0;
|
|
|
EXPECT_EQ(1, write(pipe_to_child.getWriteFD(), &some_data,
|
|
|
sizeof(some_data)));
|
|
@@ -477,9 +483,11 @@ TEST_F(MemorySegmentMappedTest, violateReadOnly) {
|
|
|
if (!isc::util::unittests::runningOnValgrind()) {
|
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
|
|
MemorySegmentMapped segment_ro(mapped_file);
|
|
|
- EXPECT_TRUE(segment_ro.getNamedAddress("test address"));
|
|
|
- *static_cast<uint32_t*>(
|
|
|
- segment_ro.getNamedAddress("test address")) = 0;
|
|
|
+ const MemorySegment::NamedAddressResult result =
|
|
|
+ segment_ro.getNamedAddress("test address");
|
|
|
+ ASSERT_TRUE(result.first);
|
|
|
+ ASSERT_TRUE(result.second);
|
|
|
+ *static_cast<uint32_t*>(result.second) = 0;
|
|
|
}, "");
|
|
|
}
|
|
|
|
|
@@ -487,10 +495,12 @@ TEST_F(MemorySegmentMappedTest, violateReadOnly) {
|
|
|
// attempts are prohibited. When detectable it must result in an
|
|
|
// exception.
|
|
|
MemorySegmentMapped segment_ro(mapped_file);
|
|
|
- ptr = segment_ro.getNamedAddress("test address");
|
|
|
- EXPECT_NE(static_cast<void*>(NULL), ptr);
|
|
|
+ const MemorySegment::NamedAddressResult result =
|
|
|
+ segment_ro.getNamedAddress("test address");
|
|
|
+ ASSERT_TRUE(result.first);
|
|
|
+ EXPECT_NE(static_cast<void*>(NULL), result.second);
|
|
|
|
|
|
- EXPECT_THROW(segment_ro.deallocate(ptr, 4), MemorySegmentError);
|
|
|
+ EXPECT_THROW(segment_ro.deallocate(result.second, 4), MemorySegmentError);
|
|
|
|
|
|
EXPECT_THROW(segment_ro.allocate(16), MemorySegmentError);
|
|
|
// allocation that would otherwise require growing the segment; permission
|