This source file includes following definitions.
- RevalidateOptions
- TEST
- TEST
#include "mojo/system/data_pipe.h"
#include <limits>
#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace system {
namespace {
const uint32_t kSizeOfOptions =
static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions));
void RevalidateOptions(const MojoCreateDataPipeOptions& validated_options) {
EXPECT_EQ(kSizeOfOptions, validated_options.struct_size);
EXPECT_GT(validated_options.element_num_bytes, 0u);
EXPECT_GT(validated_options.capacity_num_bytes, 0u);
EXPECT_EQ(0u,
validated_options.capacity_num_bytes %
validated_options.element_num_bytes);
MojoCreateDataPipeOptions revalidated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
DataPipe::ValidateOptions(&validated_options,
&revalidated_options));
EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size);
EXPECT_EQ(validated_options.element_num_bytes,
revalidated_options.element_num_bytes);
EXPECT_EQ(validated_options.capacity_num_bytes,
revalidated_options.capacity_num_bytes);
EXPECT_EQ(validated_options.flags, revalidated_options.flags);
}
TEST(DataPipeTest, ValidateOptionsValidInputs) {
{
MojoCreateDataPipeOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
DataPipe::ValidateOptions(NULL, &validated_options));
RevalidateOptions(validated_options);
}
MojoCreateDataPipeOptionsFlags flags_values[] = {
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD
};
for (size_t i = 0; i < arraysize(flags_values); i++) {
const MojoCreateDataPipeOptionsFlags flags = flags_values[i];
for (uint32_t capacity = 1; capacity <= 100 * 1000 * 1000; capacity *= 10) {
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
flags,
1,
capacity
};
MojoCreateDataPipeOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
DataPipe::ValidateOptions(&options, &validated_options))
<< capacity;
RevalidateOptions(validated_options);
}
for (uint32_t size = 1; size < 100; size++) {
for (uint32_t elements = 1; elements <= 1000 * 1000; elements *= 10) {
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
flags,
size,
size * elements
};
MojoCreateDataPipeOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
DataPipe::ValidateOptions(&options, &validated_options))
<< size << ", " << elements;
RevalidateOptions(validated_options);
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
flags,
size,
0
};
MojoCreateDataPipeOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
DataPipe::ValidateOptions(&options, &validated_options))
<< size;
RevalidateOptions(validated_options);
}
}
for (uint32_t size = 100; size <= 100 * 1000; size *= 10) {
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
flags,
size,
1000 * size
};
MojoCreateDataPipeOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
DataPipe::ValidateOptions(&options, &validated_options))
<< size;
RevalidateOptions(validated_options);
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
flags,
size,
0
};
MojoCreateDataPipeOptions validated_options = { 0 };
EXPECT_EQ(MOJO_RESULT_OK,
DataPipe::ValidateOptions(&options, &validated_options))
<< size;
RevalidateOptions(validated_options);
}
}
}
}
TEST(DataPipeTest, ValidateOptionsInvalidInputs) {
for (uint32_t struct_size = 0; struct_size < kSizeOfOptions; struct_size++) {
MojoCreateDataPipeOptions options = {
struct_size,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
1,
1000
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
DataPipe::ValidateOptions(&options, &unused));
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
0,
1000
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
DataPipe::ValidateOptions(&options, &unused));
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
std::numeric_limits<uint32_t>::max(),
std::numeric_limits<uint32_t>::max()
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
DataPipe::ValidateOptions(&options, &unused));
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
std::numeric_limits<uint32_t>::max() - 1000,
std::numeric_limits<uint32_t>::max() - 1000
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
DataPipe::ValidateOptions(&options, &unused));
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
2,
1
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
DataPipe::ValidateOptions(&options, &unused));
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
2,
111
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
DataPipe::ValidateOptions(&options, &unused));
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
5,
104
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
DataPipe::ValidateOptions(&options, &unused));
}
{
MojoCreateDataPipeOptions options = {
kSizeOfOptions,
MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
8,
0xffff0000
};
MojoCreateDataPipeOptions unused = { 0 };
EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
DataPipe::ValidateOptions(&options, &unused));
}
}
}
}
}