| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <limits> | |
| 6 | |
| 7 #include "gtest/gtest.h" | |
| 8 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | |
| 9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 namespace test { | |
| 13 namespace { | |
| 14 | |
| 15 bool IsZero(void* p_buf, size_t size) { | |
| 16 char* buf = reinterpret_cast<char*>(p_buf); | |
| 17 for (size_t i = 0; i < size; ++i) { | |
| 18 if (buf[i] != 0) | |
| 19 return false; | |
| 20 } | |
| 21 return true; | |
| 22 } | |
| 23 | |
| 24 // Tests that you can create a FixedBuffer whose underlying buffer size is not | |
| 25 // a multiple of 8. | |
| 26 TEST(FixedBufferTest, UnAlignedBufferSized) { | |
| 27 char char_buf[10] = {}; | |
| 28 internal::FixedBuffer fixed_buf; | |
| 29 fixed_buf.Initialize(char_buf, sizeof(char_buf)); | |
| 30 } | |
| 31 | |
| 32 // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries. | |
| 33 TEST(FixedBufferTest, Alignment) { | |
| 34 internal::FixedBufferForTesting buf(internal::Align(10) * 2); | |
| 35 ASSERT_EQ(buf.size(), 16u * 2); | |
| 36 | |
| 37 void* a = buf.Allocate(10); | |
| 38 ASSERT_TRUE(a); | |
| 39 EXPECT_TRUE(IsZero(a, 10)); | |
| 40 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8); | |
| 41 | |
| 42 void* b = buf.Allocate(10); | |
| 43 ASSERT_TRUE(b); | |
| 44 EXPECT_TRUE(IsZero(b, 10)); | |
| 45 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8); | |
| 46 | |
| 47 // Any more allocations would result in an assert, but we can't test that. | |
| 48 } | |
| 49 | |
| 50 // Tests that FixedBufferForTesting::Leak passes ownership to the caller. | |
| 51 TEST(FixedBufferTest, Leak) { | |
| 52 void* ptr = nullptr; | |
| 53 void* buf_ptr = nullptr; | |
| 54 { | |
| 55 internal::FixedBufferForTesting buf(8); | |
| 56 ASSERT_EQ(8u, buf.size()); | |
| 57 | |
| 58 ptr = buf.Allocate(8); | |
| 59 ASSERT_TRUE(ptr); | |
| 60 buf_ptr = buf.Leak(); | |
| 61 | |
| 62 // The buffer should point to the first element allocated. | |
| 63 // TODO(mpcomplete): Is this a reasonable expectation? | |
| 64 EXPECT_EQ(ptr, buf_ptr); | |
| 65 | |
| 66 // The FixedBufferForTesting should be empty now. | |
| 67 EXPECT_EQ(0u, buf.size()); | |
| 68 EXPECT_FALSE(buf.Leak()); | |
| 69 } | |
| 70 | |
| 71 // Since we called Leak, ptr is still writable after FixedBufferForTesting | |
| 72 // went out of scope. | |
| 73 memset(ptr, 1, 8); | |
| 74 free(buf_ptr); | |
| 75 } | |
| 76 | |
| 77 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | |
| 78 TEST(FixedBufferTest, TooBig) { | |
| 79 internal::FixedBufferForTesting buf(24); | |
| 80 | |
| 81 // A little bit too large. | |
| 82 EXPECT_EQ(reinterpret_cast<void*>(0), buf.Allocate(32)); | |
| 83 | |
| 84 // Move the cursor forward. | |
| 85 EXPECT_NE(reinterpret_cast<void*>(0), buf.Allocate(16)); | |
| 86 | |
| 87 // A lot too large. | |
| 88 EXPECT_EQ(reinterpret_cast<void*>(0), | |
| 89 buf.Allocate(std::numeric_limits<size_t>::max() - 1024u)); | |
| 90 | |
| 91 // A lot too large, leading to possible integer overflow. | |
| 92 EXPECT_EQ(reinterpret_cast<void*>(0), | |
| 93 buf.Allocate(std::numeric_limits<size_t>::max() - 8u)); | |
| 94 } | |
| 95 #endif | |
| 96 | |
| 97 } // namespace | |
| 98 } // namespace test | |
| 99 } // namespace mojo | |
| OLD | NEW |