| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" | 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/sys_info.h" |
| 11 #include "mojo/public/cpp/system/macros.h" | 12 #include "mojo/public/cpp/system/macros.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 15 #if defined(OS_WIN) |
| 16 #include "base/win/windows_version.h" |
| 17 #endif |
| 18 |
| 14 namespace mojo { | 19 namespace mojo { |
| 15 namespace edk { | 20 namespace edk { |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 18 TEST(SimplePlatformSharedBufferTest, Basic) { | 23 TEST(SimplePlatformSharedBufferTest, Basic) { |
| 19 const size_t kNumInts = 100; | 24 const size_t kNumInts = 100; |
| 20 const size_t kNumBytes = kNumInts * sizeof(int); | 25 const size_t kNumBytes = kNumInts * sizeof(int); |
| 21 // A fudge so that we're not just writing zero bytes 75% of the time. | 26 // A fudge so that we're not just writing zero bytes 75% of the time. |
| 22 const int kFudge = 1234567890; | 27 const int kFudge = 1234567890; |
| 23 | 28 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 // Offset + length too big. | 126 // Offset + length too big. |
| 122 EXPECT_FALSE(buffer->Map(50, 51)); | 127 EXPECT_FALSE(buffer->Map(50, 51)); |
| 123 EXPECT_FALSE(buffer->IsValidMap(50, 51)); | 128 EXPECT_FALSE(buffer->IsValidMap(50, 51)); |
| 124 EXPECT_FALSE(buffer->Map(51, 50)); | 129 EXPECT_FALSE(buffer->Map(51, 50)); |
| 125 EXPECT_FALSE(buffer->IsValidMap(51, 50)); | 130 EXPECT_FALSE(buffer->IsValidMap(51, 50)); |
| 126 } | 131 } |
| 127 | 132 |
| 128 TEST(SimplePlatformSharedBufferTest, TooBig) { | 133 TEST(SimplePlatformSharedBufferTest, TooBig) { |
| 129 // If |size_t| is 32-bit, it's quite possible/likely that |Create()| succeeds | 134 // If |size_t| is 32-bit, it's quite possible/likely that |Create()| succeeds |
| 130 // (since it only involves creating a 4 GB file). | 135 // (since it only involves creating a 4 GB file). |
| 131 const size_t kMaxSizeT = std::numeric_limits<size_t>::max(); | 136 size_t max_size = std::numeric_limits<size_t>::max(); |
| 137 if (max_size > static_cast<size_t>(base::SysInfo::AmountOfVirtualMemory())) |
| 138 max_size = static_cast<size_t>(base::SysInfo::AmountOfVirtualMemory()); |
| 132 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 139 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
| 133 SimplePlatformSharedBuffer::Create(kMaxSizeT)); | 140 SimplePlatformSharedBuffer::Create(max_size)); |
| 134 // But, assuming |sizeof(size_t) == sizeof(void*)|, mapping all of it should | 141 // But, assuming |sizeof(size_t) == sizeof(void*)|, mapping all of it should |
| 135 // always fail. | 142 // always fail. |
| 136 if (buffer) | 143 if (buffer) |
| 137 EXPECT_FALSE(buffer->Map(0, kMaxSizeT)); | 144 EXPECT_FALSE(buffer->Map(0, max_size)); |
| 138 } | 145 } |
| 139 | 146 |
| 140 // Tests that separate mappings get distinct addresses. | 147 // Tests that separate mappings get distinct addresses. |
| 141 // Note: It's not inconceivable that the OS could ref-count identical mappings | 148 // Note: It's not inconceivable that the OS could ref-count identical mappings |
| 142 // and reuse the same address, in which case we'd have to be more careful about | 149 // and reuse the same address, in which case we'd have to be more careful about |
| 143 // using the address as the key for unmapping. | 150 // using the address as the key for unmapping. |
| 144 TEST(SimplePlatformSharedBufferTest, MappingsDistinct) { | 151 TEST(SimplePlatformSharedBufferTest, MappingsDistinct) { |
| 145 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 152 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
| 146 SimplePlatformSharedBuffer::Create(100)); | 153 SimplePlatformSharedBuffer::Create(100)); |
| 147 scoped_ptr<PlatformSharedBufferMapping> mapping1(buffer->Map(0, 100)); | 154 scoped_ptr<PlatformSharedBufferMapping> mapping1(buffer->Map(0, 100)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 178 | 185 |
| 179 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); | 186 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); |
| 180 | 187 |
| 181 static_cast<char*>(mapping2->GetBase())[1] = 'y'; | 188 static_cast<char*>(mapping2->GetBase())[1] = 'y'; |
| 182 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); | 189 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); |
| 183 } | 190 } |
| 184 | 191 |
| 185 } // namespace | 192 } // namespace |
| 186 } // namespace edk | 193 } // namespace edk |
| 187 } // namespace mojo | 194 } // namespace mojo |
| OLD | NEW |