This source file includes following definitions.
- RevalidateOptions
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "mojo/system/shared_buffer_dispatcher.h"
#include <limits>
#include "base/memory/ref_counted.h"
#include "mojo/system/dispatcher.h"
#include "mojo/system/raw_shared_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace system {
namespace {
const uint32_t kSizeOfOptions =
static_cast<uint32_t>(sizeof(MojoCreateSharedBufferOptions));
void RevalidateOptions(const MojoCreateSharedBufferOptions& validated_options) {
EXPECT_EQ(kSizeOfOptions, validated_options.struct_size);
MojoCreateSharedBufferOptions revalidated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::ValidateOptions(&validated_options,
&revalidated_options));
EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size);
EXPECT_EQ(validated_options.flags, revalidated_options.flags);
}
TEST(SharedBufferDispatcherTest, ValidateOptionsValidInputs) {
{
MojoCreateSharedBufferOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::ValidateOptions(NULL,
&validated_options));
RevalidateOptions(validated_options);
}
MojoCreateSharedBufferOptionsFlags flags_values[] = {
MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE
};
for (size_t i = 0; i < arraysize(flags_values); i++) {
const MojoCreateSharedBufferOptionsFlags flags = flags_values[i];
for (uint32_t capacity = 1; capacity <= 100 * 1000 * 1000; capacity *= 10) {
MojoCreateSharedBufferOptions options = {
kSizeOfOptions,
flags
};
MojoCreateSharedBufferOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::ValidateOptions(&options,
&validated_options))
<< capacity;
RevalidateOptions(validated_options);
}
}
}
TEST(SharedBufferDispatcherTest, ValidateOptionsInvalidInputs) {
for (uint32_t struct_size = 0; struct_size < kSizeOfOptions; struct_size++) {
MojoCreateSharedBufferOptions options = {
struct_size,
MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE
};
MojoCreateSharedBufferOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
SharedBufferDispatcher::ValidateOptions(&options, &unused));
}
}
TEST(SharedBufferDispatcherTest, CreateAndMapBuffer) {
MojoCreateSharedBufferOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::ValidateOptions(NULL,
&validated_options));
scoped_refptr<SharedBufferDispatcher> dispatcher;
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::Create(validated_options, 100,
&dispatcher));
ASSERT_TRUE(dispatcher);
EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher->GetType());
scoped_ptr<RawSharedBufferMapping> mapping1;
EXPECT_EQ(MOJO_RESULT_OK,
dispatcher->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE,
&mapping1));
ASSERT_TRUE(mapping1);
ASSERT_TRUE(mapping1->base());
EXPECT_EQ(100u, mapping1->length());
static_cast<char*>(mapping1->base())[50] = 'x';
scoped_ptr<RawSharedBufferMapping> mapping2;
EXPECT_EQ(MOJO_RESULT_OK,
dispatcher->MapBuffer(50, 50, MOJO_MAP_BUFFER_FLAG_NONE,
&mapping2));
ASSERT_TRUE(mapping2);
ASSERT_TRUE(mapping2->base());
EXPECT_EQ(50u, mapping2->length());
EXPECT_EQ('x', static_cast<char*>(mapping2->base())[0]);
EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
static_cast<char*>(mapping2->base())[1] = 'y';
EXPECT_EQ('y', static_cast<char*>(mapping1->base())[51]);
}
TEST(SharedBufferDispatcher, DuplicateBufferHandle) {
MojoCreateSharedBufferOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::ValidateOptions(NULL,
&validated_options));
scoped_refptr<SharedBufferDispatcher> dispatcher1;
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::Create(validated_options, 100,
&dispatcher1));
scoped_ptr<RawSharedBufferMapping> mapping;
EXPECT_EQ(MOJO_RESULT_OK,
dispatcher1->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE,
&mapping));
static_cast<char*>(mapping->base())[0] = 'x';
mapping.reset();
scoped_refptr<Dispatcher> dispatcher2;
EXPECT_EQ(MOJO_RESULT_OK,
dispatcher1->DuplicateBufferHandle(NULL, &dispatcher2));
ASSERT_TRUE(dispatcher2);
EXPECT_EQ(Dispatcher::kTypeSharedBuffer, dispatcher2->GetType());
EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->Close());
EXPECT_EQ(MOJO_RESULT_OK,
dispatcher2->MapBuffer(0, 100, MOJO_MAP_BUFFER_FLAG_NONE,
&mapping));
EXPECT_EQ('x', static_cast<char*>(mapping->base())[0]);
EXPECT_EQ(MOJO_RESULT_OK, dispatcher2->Close());
}
TEST(SharedBufferDispatcherTest, CreateInvalidNumBytes) {
MojoCreateSharedBufferOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::ValidateOptions(NULL,
&validated_options));
scoped_refptr<SharedBufferDispatcher> dispatcher;
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
SharedBufferDispatcher::Create(validated_options,
std::numeric_limits<uint64_t>::max(),
&dispatcher));
EXPECT_FALSE(dispatcher);
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
SharedBufferDispatcher::Create(validated_options, 0, &dispatcher));
EXPECT_FALSE(dispatcher);
}
TEST(SharedBufferDispatcherTest, MapBufferInvalidArguments) {
MojoCreateSharedBufferOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::ValidateOptions(NULL,
&validated_options));
scoped_refptr<SharedBufferDispatcher> dispatcher;
EXPECT_EQ(MOJO_RESULT_OK,
SharedBufferDispatcher::Create(validated_options, 100,
&dispatcher));
scoped_ptr<RawSharedBufferMapping> mapping;
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
dispatcher->MapBuffer(0, 101, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
EXPECT_FALSE(mapping);
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
dispatcher->MapBuffer(1, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
EXPECT_FALSE(mapping);
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping));
EXPECT_FALSE(mapping);
EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
}
}
}
}