| 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/embedder/simple_platform_shared_buffer.h" | 5 #include "mojo/embedder/simple_platform_shared_buffer.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace mojo { | 14 namespace mojo { |
| 15 namespace embedder { | 15 namespace embedder { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 TEST(SimplePlatformSharedBufferTest, Basic) { | 18 TEST(SimplePlatformSharedBufferTest, Basic) { |
| 19 const size_t kNumInts = 100; | 19 const size_t kNumInts = 100; |
| 20 const size_t kNumBytes = kNumInts * sizeof(int); | 20 const size_t kNumBytes = kNumInts * sizeof(int); |
| 21 // A fudge so that we're not just writing zero bytes 75% of the time. | 21 // A fudge so that we're not just writing zero bytes 75% of the time. |
| 22 const int kFudge = 1234567890; | 22 const int kFudge = 1234567890; |
| 23 | 23 |
| 24 // Make some memory. | 24 // Make some memory. |
| 25 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 25 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
| 26 SimplePlatformSharedBuffer::Create(kNumBytes)); | 26 SimplePlatformSharedBuffer::Create(kNumBytes)); |
| 27 ASSERT_TRUE(buffer); | 27 ASSERT_TRUE(buffer.get()); |
| 28 | 28 |
| 29 // Map it all, scribble some stuff, and then unmap it. | 29 // Map it all, scribble some stuff, and then unmap it. |
| 30 { | 30 { |
| 31 EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes)); | 31 EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes)); |
| 32 scoped_ptr<PlatformSharedBufferMapping> mapping(buffer->Map(0, kNumBytes)); | 32 scoped_ptr<PlatformSharedBufferMapping> mapping(buffer->Map(0, kNumBytes)); |
| 33 ASSERT_TRUE(mapping); | 33 ASSERT_TRUE(mapping); |
| 34 ASSERT_TRUE(mapping->GetBase()); | 34 ASSERT_TRUE(mapping->GetBase()); |
| 35 int* stuff = static_cast<int*>(mapping->GetBase()); | 35 int* stuff = static_cast<int*>(mapping->GetBase()); |
| 36 for (size_t i = 0; i < kNumInts; i++) | 36 for (size_t i = 0; i < kNumInts; i++) |
| 37 stuff[i] = static_cast<int>(i) + kFudge; | 37 stuff[i] = static_cast<int>(i) + kFudge; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 // TODO(vtl): Bigger buffers. | 98 // TODO(vtl): Bigger buffers. |
| 99 | 99 |
| 100 TEST(SimplePlatformSharedBufferTest, InvalidMappings) { | 100 TEST(SimplePlatformSharedBufferTest, InvalidMappings) { |
| 101 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 101 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
| 102 SimplePlatformSharedBuffer::Create(100)); | 102 SimplePlatformSharedBuffer::Create(100)); |
| 103 ASSERT_TRUE(buffer); | 103 ASSERT_TRUE(buffer.get()); |
| 104 | 104 |
| 105 // Zero length not allowed. | 105 // Zero length not allowed. |
| 106 EXPECT_FALSE(buffer->Map(0, 0)); | 106 EXPECT_FALSE(buffer->Map(0, 0)); |
| 107 EXPECT_FALSE(buffer->IsValidMap(0, 0)); | 107 EXPECT_FALSE(buffer->IsValidMap(0, 0)); |
| 108 | 108 |
| 109 // Okay: | 109 // Okay: |
| 110 EXPECT_TRUE(buffer->Map(0, 100)); | 110 EXPECT_TRUE(buffer->Map(0, 100)); |
| 111 EXPECT_TRUE(buffer->IsValidMap(0, 100)); | 111 EXPECT_TRUE(buffer->IsValidMap(0, 100)); |
| 112 // Offset + length too big. | 112 // Offset + length too big. |
| 113 EXPECT_FALSE(buffer->Map(0, 101)); | 113 EXPECT_FALSE(buffer->Map(0, 101)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 126 } | 126 } |
| 127 | 127 |
| 128 TEST(SimplePlatformSharedBufferTest, TooBig) { | 128 TEST(SimplePlatformSharedBufferTest, TooBig) { |
| 129 // If |size_t| is 32-bit, it's quite possible/likely that |Create()| succeeds | 129 // If |size_t| is 32-bit, it's quite possible/likely that |Create()| succeeds |
| 130 // (since it only involves creating a 4 GB file). | 130 // (since it only involves creating a 4 GB file). |
| 131 const size_t kMaxSizeT = std::numeric_limits<size_t>::max(); | 131 const size_t kMaxSizeT = std::numeric_limits<size_t>::max(); |
| 132 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 132 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
| 133 SimplePlatformSharedBuffer::Create(kMaxSizeT)); | 133 SimplePlatformSharedBuffer::Create(kMaxSizeT)); |
| 134 // But, assuming |sizeof(size_t) == sizeof(void*)|, mapping all of it should | 134 // But, assuming |sizeof(size_t) == sizeof(void*)|, mapping all of it should |
| 135 // always fail. | 135 // always fail. |
| 136 if (buffer) | 136 if (buffer.get()) |
| 137 EXPECT_FALSE(buffer->Map(0, kMaxSizeT)); | 137 EXPECT_FALSE(buffer->Map(0, kMaxSizeT)); |
| 138 } | 138 } |
| 139 | 139 |
| 140 // Tests that separate mappings get distinct addresses. | 140 // Tests that separate mappings get distinct addresses. |
| 141 // Note: It's not inconceivable that the OS could ref-count identical mappings | 141 // 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 | 142 // 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. | 143 // using the address as the key for unmapping. |
| 144 TEST(SimplePlatformSharedBufferTest, MappingsDistinct) { | 144 TEST(SimplePlatformSharedBufferTest, MappingsDistinct) { |
| 145 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 145 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
| 146 SimplePlatformSharedBuffer::Create(100)); | 146 SimplePlatformSharedBuffer::Create(100)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 178 |
| 179 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); | 179 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); |
| 180 | 180 |
| 181 static_cast<char*>(mapping2->GetBase())[1] = 'y'; | 181 static_cast<char*>(mapping2->GetBase())[1] = 'y'; |
| 182 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); | 182 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); |
| 183 } | 183 } |
| 184 | 184 |
| 185 } // namespace | 185 } // namespace |
| 186 } // namespace embedder | 186 } // namespace embedder |
| 187 } // namespace mojo | 187 } // namespace mojo |
| OLD | NEW |