10c01d18094100db92d38daa923c95661512db203John McCall// Copyright 2014 The Chromium Authors. All rights reserved.
20c01d18094100db92d38daa923c95661512db203John McCall// Use of this source code is governed by a BSD-style license that can be
30c01d18094100db92d38daa923c95661512db203John McCall// found in the LICENSE file.
40c01d18094100db92d38daa923c95661512db203John McCall
50c01d18094100db92d38daa923c95661512db203John McCall#include <limits>
60c01d18094100db92d38daa923c95661512db203John McCall
70c01d18094100db92d38daa923c95661512db203John McCall#include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
80c01d18094100db92d38daa923c95661512db203John McCall#include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
90c01d18094100db92d38daa923c95661512db203John McCall#include "testing/gtest/include/gtest/gtest.h"
100c01d18094100db92d38daa923c95661512db203John McCall
110c01d18094100db92d38daa923c95661512db203John McCallnamespace mojo {
120c01d18094100db92d38daa923c95661512db203John McCallnamespace test {
130c01d18094100db92d38daa923c95661512db203John McCallnamespace {
140c01d18094100db92d38daa923c95661512db203John McCall
150c01d18094100db92d38daa923c95661512db203John McCallbool IsZero(void* p_buf, size_t size) {
160c01d18094100db92d38daa923c95661512db203John McCall  char* buf = reinterpret_cast<char*>(p_buf);
170c01d18094100db92d38daa923c95661512db203John McCall  for (size_t i = 0; i < size; ++i) {
180c01d18094100db92d38daa923c95661512db203John McCall    if (buf[i] != 0)
190c01d18094100db92d38daa923c95661512db203John McCall      return false;
200c01d18094100db92d38daa923c95661512db203John McCall  }
210c01d18094100db92d38daa923c95661512db203John McCall  return true;
220c01d18094100db92d38daa923c95661512db203John McCall}
23161755a09898c95d21bfff33707da9ca41cd53c5John McCall
2430a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth// Tests that FixedBuffer allocates memory aligned to 8 byte boundaries.
2530a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler CarruthTEST(FixedBufferTest, Alignment) {
260c01d18094100db92d38daa923c95661512db203John McCall  internal::FixedBuffer buf(internal::Align(10) * 2);
270c01d18094100db92d38daa923c95661512db203John McCall  ASSERT_EQ(buf.size(), 16u * 2);
280c01d18094100db92d38daa923c95661512db203John McCall
290c01d18094100db92d38daa923c95661512db203John McCall  void* a = buf.Allocate(10);
300c01d18094100db92d38daa923c95661512db203John McCall  ASSERT_TRUE(a);
310c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_TRUE(IsZero(a, 10));
320c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8);
330c01d18094100db92d38daa923c95661512db203John McCall
340c01d18094100db92d38daa923c95661512db203John McCall  void* b = buf.Allocate(10);
350c01d18094100db92d38daa923c95661512db203John McCall  ASSERT_TRUE(b);
360c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_TRUE(IsZero(b, 10));
370c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8);
380c01d18094100db92d38daa923c95661512db203John McCall
390c01d18094100db92d38daa923c95661512db203John McCall  // Any more allocations would result in an assert, but we can't test that.
400c01d18094100db92d38daa923c95661512db203John McCall}
410c01d18094100db92d38daa923c95661512db203John McCall
420c01d18094100db92d38daa923c95661512db203John McCall// Tests that FixedBuffer::Leak passes ownership to the caller.
430c01d18094100db92d38daa923c95661512db203John McCallTEST(FixedBufferTest, Leak) {
440c01d18094100db92d38daa923c95661512db203John McCall  void* ptr = NULL;
450c01d18094100db92d38daa923c95661512db203John McCall  void* buf_ptr = NULL;
46161755a09898c95d21bfff33707da9ca41cd53c5John McCall  {
470c01d18094100db92d38daa923c95661512db203John McCall    internal::FixedBuffer buf(8);
480c01d18094100db92d38daa923c95661512db203John McCall    ASSERT_EQ(8u, buf.size());
490c01d18094100db92d38daa923c95661512db203John McCall
500c01d18094100db92d38daa923c95661512db203John McCall    ptr = buf.Allocate(8);
510c01d18094100db92d38daa923c95661512db203John McCall    ASSERT_TRUE(ptr);
520c01d18094100db92d38daa923c95661512db203John McCall    buf_ptr = buf.Leak();
530c01d18094100db92d38daa923c95661512db203John McCall
54161755a09898c95d21bfff33707da9ca41cd53c5John McCall    // The buffer should point to the first element allocated.
550c01d18094100db92d38daa923c95661512db203John McCall    // TODO(mpcomplete): Is this a reasonable expectation?
560c01d18094100db92d38daa923c95661512db203John McCall    EXPECT_EQ(ptr, buf_ptr);
570c01d18094100db92d38daa923c95661512db203John McCall
580c01d18094100db92d38daa923c95661512db203John McCall    // The FixedBuffer should be empty now.
590c01d18094100db92d38daa923c95661512db203John McCall    EXPECT_EQ(0u, buf.size());
600c01d18094100db92d38daa923c95661512db203John McCall    EXPECT_FALSE(buf.Leak());
610c01d18094100db92d38daa923c95661512db203John McCall  }
620c01d18094100db92d38daa923c95661512db203John McCall
630c01d18094100db92d38daa923c95661512db203John McCall  // Since we called Leak, ptr is still writable after FixedBuffer went out of
640c01d18094100db92d38daa923c95661512db203John McCall  // scope.
650c01d18094100db92d38daa923c95661512db203John McCall  memset(ptr, 1, 8);
660c01d18094100db92d38daa923c95661512db203John McCall  free(buf_ptr);
670c01d18094100db92d38daa923c95661512db203John McCall}
680c01d18094100db92d38daa923c95661512db203John McCall
690c01d18094100db92d38daa923c95661512db203John McCall#ifdef NDEBUG
700c01d18094100db92d38daa923c95661512db203John McCallTEST(FixedBufferTest, TooBig) {
710c01d18094100db92d38daa923c95661512db203John McCall  internal::FixedBuffer buf(24);
720c01d18094100db92d38daa923c95661512db203John McCall
730c01d18094100db92d38daa923c95661512db203John McCall  // A little bit too large.
740c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_EQ(reinterpret_cast<void*>(0), buf.Allocate(32));
750c01d18094100db92d38daa923c95661512db203John McCall
760c01d18094100db92d38daa923c95661512db203John McCall  // Move the cursor forward.
770c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_NE(reinterpret_cast<void*>(0), buf.Allocate(16));
780c01d18094100db92d38daa923c95661512db203John McCall
790c01d18094100db92d38daa923c95661512db203John McCall  // A lot too large.
800c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_EQ(reinterpret_cast<void*>(0),
810c01d18094100db92d38daa923c95661512db203John McCall            buf.Allocate(std::numeric_limits<size_t>::max() - 1024u));
820c01d18094100db92d38daa923c95661512db203John McCall
830c01d18094100db92d38daa923c95661512db203John McCall  // A lot too large, leading to possible integer overflow.
840c01d18094100db92d38daa923c95661512db203John McCall  EXPECT_EQ(reinterpret_cast<void*>(0),
850c01d18094100db92d38daa923c95661512db203John McCall            buf.Allocate(std::numeric_limits<size_t>::max() - 8u));
860c01d18094100db92d38daa923c95661512db203John McCall}
87161755a09898c95d21bfff33707da9ca41cd53c5John McCall#endif
88161755a09898c95d21bfff33707da9ca41cd53c5John McCall
89161755a09898c95d21bfff33707da9ca41cd53c5John McCall}  // namespace
90161755a09898c95d21bfff33707da9ca41cd53c5John McCall}  // namespace test
91161755a09898c95d21bfff33707da9ca41cd53c5John McCall}  // namespace mojo
920c01d18094100db92d38daa923c95661512db203John McCall