1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/edk/embedder/simple_platform_shared_buffer.h"
#include <limits>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace embedder {
namespace {
TEST(SimplePlatformSharedBufferTest, Basic) {
const size_t kNumInts = 100;
const size_t kNumBytes = kNumInts * sizeof(int);
// A fudge so that we're not just writing zero bytes 75% of the time.
const int kFudge = 1234567890;
// Make some memory.
scoped_refptr<SimplePlatformSharedBuffer> buffer(
SimplePlatformSharedBuffer::Create(kNumBytes));
ASSERT_TRUE(buffer);
// Map it all, scribble some stuff, and then unmap it.
{
EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes));
scoped_ptr<PlatformSharedBufferMapping> mapping(buffer->Map(0, kNumBytes));
ASSERT_TRUE(mapping);
ASSERT_TRUE(mapping->GetBase());
int* stuff = static_cast<int*>(mapping->GetBase());
for (size_t i = 0; i < kNumInts; i++)
stuff[i] = static_cast<int>(i) + kFudge;
}
// Map it all again, check that our scribbling is still there, then do a
// partial mapping and scribble on that, check that everything is coherent,
// unmap the first mapping, scribble on some of the second mapping, and then
// unmap it.
{
ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes));
// Use |MapNoCheck()| this time.
scoped_ptr<PlatformSharedBufferMapping> mapping1(
buffer->MapNoCheck(0, kNumBytes));
ASSERT_TRUE(mapping1);
ASSERT_TRUE(mapping1->GetBase());
int* stuff1 = static_cast<int*>(mapping1->GetBase());
for (size_t i = 0; i < kNumInts; i++)
EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i;
scoped_ptr<PlatformSharedBufferMapping> mapping2(
buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int)));
ASSERT_TRUE(mapping2);
ASSERT_TRUE(mapping2->GetBase());
int* stuff2 = static_cast<int*>(mapping2->GetBase());
EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]);
EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]);
stuff2[0] = 123;
stuff2[1] = 456;
EXPECT_EQ(123, stuff1[kNumInts / 2]);
EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]);
mapping1.reset();
EXPECT_EQ(123, stuff2[0]);
EXPECT_EQ(456, stuff2[1]);
stuff2[1] = 789;
}
// Do another partial mapping and check that everything is the way we expect
// it to be.
{
EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int)));
scoped_ptr<PlatformSharedBufferMapping> mapping(
buffer->Map(sizeof(int), kNumBytes - sizeof(int)));
ASSERT_TRUE(mapping);
ASSERT_TRUE(mapping->GetBase());
int* stuff = static_cast<int*>(mapping->GetBase());
for (size_t j = 0; j < kNumInts - 1; j++) {
int i = static_cast<int>(j) + 1;
if (i == kNumInts / 2) {
EXPECT_EQ(123, stuff[j]);
} else if (i == kNumInts / 2 + 1) {
EXPECT_EQ(789, stuff[j]);
} else {
EXPECT_EQ(i + kFudge, stuff[j]) << i;
}
}
}
}
// TODO(vtl): Bigger buffers.
TEST(SimplePlatformSharedBufferTest, InvalidMappings) {
scoped_refptr<SimplePlatformSharedBuffer> buffer(
SimplePlatformSharedBuffer::Create(100));
ASSERT_TRUE(buffer);
// Zero length not allowed.
EXPECT_FALSE(buffer->Map(0, 0));
EXPECT_FALSE(buffer->IsValidMap(0, 0));
// Okay:
EXPECT_TRUE(buffer->Map(0, 100));
EXPECT_TRUE(buffer->IsValidMap(0, 100));
// Offset + length too big.
EXPECT_FALSE(buffer->Map(0, 101));
EXPECT_FALSE(buffer->IsValidMap(0, 101));
EXPECT_FALSE(buffer->Map(1, 100));
EXPECT_FALSE(buffer->IsValidMap(1, 100));
// Okay:
EXPECT_TRUE(buffer->Map(50, 50));
EXPECT_TRUE(buffer->IsValidMap(50, 50));
// Offset + length too big.
EXPECT_FALSE(buffer->Map(50, 51));
EXPECT_FALSE(buffer->IsValidMap(50, 51));
EXPECT_FALSE(buffer->Map(51, 50));
EXPECT_FALSE(buffer->IsValidMap(51, 50));
}
TEST(SimplePlatformSharedBufferTest, TooBig) {
// If |size_t| is 32-bit, it's quite possible/likely that |Create()| succeeds
// (since it only involves creating a 4 GB file).
const size_t kMaxSizeT = std::numeric_limits<size_t>::max();
scoped_refptr<SimplePlatformSharedBuffer> buffer(
SimplePlatformSharedBuffer::Create(kMaxSizeT));
// But, assuming |sizeof(size_t) == sizeof(void*)|, mapping all of it should
// always fail.
if (buffer)
EXPECT_FALSE(buffer->Map(0, kMaxSizeT));
}
// Tests that separate mappings get distinct addresses.
// Note: It's not inconceivable that the OS could ref-count identical mappings
// and reuse the same address, in which case we'd have to be more careful about
// using the address as the key for unmapping.
TEST(SimplePlatformSharedBufferTest, MappingsDistinct) {
scoped_refptr<SimplePlatformSharedBuffer> buffer(
SimplePlatformSharedBuffer::Create(100));
scoped_ptr<PlatformSharedBufferMapping> mapping1(buffer->Map(0, 100));
scoped_ptr<PlatformSharedBufferMapping> mapping2(buffer->Map(0, 100));
EXPECT_NE(mapping1->GetBase(), mapping2->GetBase());
}
TEST(SimplePlatformSharedBufferTest, BufferZeroInitialized) {
static const size_t kSizes[] = {10, 100, 1000, 10000, 100000};
for (size_t i = 0; i < arraysize(kSizes); i++) {
scoped_refptr<SimplePlatformSharedBuffer> buffer(
SimplePlatformSharedBuffer::Create(kSizes[i]));
scoped_ptr<PlatformSharedBufferMapping> mapping(buffer->Map(0, kSizes[i]));
for (size_t j = 0; j < kSizes[i]; j++) {
// "Assert" instead of "expect" so we don't spam the output with thousands
// of failures if we fail.
ASSERT_EQ('\0', static_cast<char*>(mapping->GetBase())[j])
<< "size " << kSizes[i] << ", offset " << j;
}
}
}
TEST(SimplePlatformSharedBufferTest, MappingsOutliveBuffer) {
scoped_ptr<PlatformSharedBufferMapping> mapping1;
scoped_ptr<PlatformSharedBufferMapping> mapping2;
{
scoped_refptr<SimplePlatformSharedBuffer> buffer(
SimplePlatformSharedBuffer::Create(100));
mapping1 = buffer->Map(0, 100).Pass();
mapping2 = buffer->Map(50, 50).Pass();
static_cast<char*>(mapping1->GetBase())[50] = 'x';
}
EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]);
static_cast<char*>(mapping2->GetBase())[1] = 'y';
EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]);
}
} // namespace
} // namespace embedder
} // namespace mojo
|