chromium/mojo/public/cpp/bindings/tests/validation_context_unittest.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>
#include <stdint.h>

#include <limits>

#include "mojo/public/cpp/bindings/lib/serialization_util.h"
#include "mojo/public/cpp/bindings/lib/validation_context.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace test {
namespace {

Handle_Data;
AssociatedEndpointHandle_Data;

const void* ToPtr(uintptr_t ptr) {}

#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
TEST(ValidationContextTest, ConstructorRangeOverflow) {
  {
    // Test memory range overflow.
    internal::ValidationContext context(
        ToPtr(std::numeric_limits<uintptr_t>::max() - 3000), 5000, 0, 0);

    EXPECT_FALSE(context.IsValidRange(
        ToPtr(std::numeric_limits<uintptr_t>::max() - 3000), 1));
    EXPECT_FALSE(context.ClaimMemory(
        ToPtr(std::numeric_limits<uintptr_t>::max() - 3000), 1));
  }

  if (sizeof(size_t) <= sizeof(uint32_t))
    return;

  {
    // Test handle index range overflow.
    size_t num_handles =
        static_cast<size_t>(std::numeric_limits<uint32_t>::max()) + 5;
    internal::ValidationContext context(ToPtr(0), 0, num_handles, 0);

    EXPECT_FALSE(context.ClaimHandle(Handle_Data(0)));
    EXPECT_FALSE(context.ClaimHandle(
        Handle_Data(std::numeric_limits<uint32_t>::max() - 1)));

    EXPECT_TRUE(context.ClaimHandle(
        Handle_Data(internal::kEncodedInvalidHandleValue)));
  }

  {
    size_t num_associated_endpoint_handles =
        static_cast<size_t>(std::numeric_limits<uint32_t>::max()) + 5;
    internal::ValidationContext context(ToPtr(0), 0, 0,
                                        num_associated_endpoint_handles);

    EXPECT_FALSE(context.ClaimAssociatedEndpointHandle(
        AssociatedEndpointHandle_Data(0)));
    EXPECT_FALSE(
        context.ClaimAssociatedEndpointHandle(AssociatedEndpointHandle_Data(
            std::numeric_limits<uint32_t>::max() - 1)));

    EXPECT_TRUE(context.ClaimAssociatedEndpointHandle(
        AssociatedEndpointHandle_Data(internal::kEncodedInvalidHandleValue)));
  }
}
#endif

TEST(ValidationContextTest, IsValidRange) {}

TEST(ValidationContextTest, ClaimHandle) {}

TEST(ValidationContextTest, ClaimAssociatedEndpointHandle) {}

TEST(ValidationContextTest, ClaimMemory) {}

}  // namespace
}  // namespace test
}  // namespace mojo