This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "mojo/public/c/system/core.h"
#include <string.h>
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace {
TEST(CoreTest, GetTimeTicksNow) {
const MojoTimeTicks start = MojoGetTimeTicksNow();
EXPECT_NE(static_cast<MojoTimeTicks>(0), start)
<< "MojoGetTimeTicksNow should return nonzero value";
}
TEST(CoreTest, InvalidHandle) {
MojoHandle h0, h1;
MojoWaitFlags wf;
char buffer[10] = { 0 };
uint32_t buffer_size;
void* write_pointer;
const void* read_pointer;
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(MOJO_HANDLE_INVALID));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoWait(MOJO_HANDLE_INVALID, MOJO_WAIT_FLAG_EVERYTHING, 1000000));
h0 = MOJO_HANDLE_INVALID;
wf = MOJO_WAIT_FLAG_EVERYTHING;
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoWaitMany(&h0, &wf, 1, MOJO_DEADLINE_INDEFINITE));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoWriteMessage(h0, buffer, 3, NULL, 0,
MOJO_WRITE_MESSAGE_FLAG_NONE));
buffer_size = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoReadMessage(h0, buffer, &buffer_size, NULL, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
buffer_size = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoWriteData(h0, buffer, &buffer_size, MOJO_WRITE_DATA_FLAG_NONE));
write_pointer = NULL;
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoBeginWriteData(h0, &write_pointer, &buffer_size,
MOJO_WRITE_DATA_FLAG_NONE));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoEndWriteData(h0, 1));
buffer_size = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoReadData(h0, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE));
read_pointer = NULL;
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoBeginReadData(h0, &read_pointer, &buffer_size,
MOJO_READ_DATA_FLAG_NONE));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoEndReadData(h0, 1));
h1 = MOJO_HANDLE_INVALID;
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoDuplicateBufferHandle(h0, NULL, &h1));
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
MojoMapBuffer(h0, 0, 1, &write_pointer, MOJO_MAP_BUFFER_FLAG_NONE));
}
TEST(CoreTest, BasicMessagePipe) {
MojoHandle h0, h1;
MojoWaitFlags wf;
char buffer[10] = { 0 };
uint32_t buffer_size;
h0 = MOJO_HANDLE_INVALID;
h1 = MOJO_HANDLE_INVALID;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(&h0, &h1));
EXPECT_NE(h0, MOJO_HANDLE_INVALID);
EXPECT_NE(h1, MOJO_HANDLE_INVALID);
EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
MojoWait(h0, MOJO_WAIT_FLAG_READABLE, 0));
EXPECT_EQ(MOJO_RESULT_OK, MojoWait(h0, MOJO_WAIT_FLAG_WRITABLE, 0));
buffer_size = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
MojoReadMessage(h0, buffer, &buffer_size, NULL, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
static const char kHello[] = "hello";
buffer_size = static_cast<uint32_t>(sizeof(kHello));
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(h1, kHello, buffer_size, NULL, 0,
MOJO_WRITE_MESSAGE_FLAG_NONE));
wf = MOJO_WAIT_FLAG_READABLE;
EXPECT_EQ(MOJO_RESULT_OK,
MojoWaitMany(&h0, &wf, 1, MOJO_DEADLINE_INDEFINITE));
buffer_size = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(h0, buffer, &buffer_size, NULL, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(static_cast<uint32_t>(sizeof(kHello)), buffer_size);
EXPECT_STREQ(kHello, buffer);
EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
MojoWait(h0, MOJO_WAIT_FLAG_READABLE, 10));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0));
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoWait(h1, MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE,
1000));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
}
TEST(CoreTest, BasicDataPipe) {
MojoHandle hp, hc;
MojoWaitFlags wf;
char buffer[20] = { 0 };
uint32_t buffer_size;
void* write_pointer;
const void* read_pointer;
hp = MOJO_HANDLE_INVALID;
hc = MOJO_HANDLE_INVALID;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateDataPipe(NULL, &hp, &hc));
EXPECT_NE(hp, MOJO_HANDLE_INVALID);
EXPECT_NE(hc, MOJO_HANDLE_INVALID);
EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
MojoWait(hc, MOJO_WAIT_FLAG_READABLE, 0));
EXPECT_EQ(MOJO_RESULT_OK, MojoWait(hp, MOJO_WAIT_FLAG_WRITABLE, 0));
buffer_size = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
MojoReadData(hc, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE));
read_pointer = NULL;
EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
MojoBeginReadData(hc, &read_pointer, &buffer_size,
MOJO_READ_DATA_FLAG_NONE));
static const char kHello[] = "hello ";
buffer_size = static_cast<uint32_t>(strlen(kHello));
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteData(hp, kHello, &buffer_size,
MOJO_WRITE_MESSAGE_FLAG_NONE));
wf = MOJO_WAIT_FLAG_READABLE;
EXPECT_EQ(MOJO_RESULT_OK,
MojoWaitMany(&hc, &wf, 1, MOJO_DEADLINE_INDEFINITE));
EXPECT_EQ(MOJO_RESULT_OK,
MojoBeginWriteData(hp, &write_pointer, &buffer_size,
MOJO_WRITE_DATA_FLAG_NONE));
static const char kWorld[] = "world";
ASSERT_GE(buffer_size, sizeof(kWorld));
memcpy(write_pointer, kWorld, sizeof(kWorld));
EXPECT_EQ(MOJO_RESULT_OK,
MojoEndWriteData(hp, static_cast<uint32_t>(sizeof(kWorld))));
memset(buffer, 0, sizeof(buffer));
buffer_size = 1;
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadData(hc, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(hp));
EXPECT_EQ(MOJO_RESULT_OK, MojoWait(hc, MOJO_WAIT_FLAG_READABLE, 0));
read_pointer = NULL;
EXPECT_EQ(MOJO_RESULT_OK,
MojoBeginReadData(hc, &read_pointer, &buffer_size,
MOJO_READ_DATA_FLAG_NONE));
ASSERT_LE(buffer_size, sizeof(buffer) - 1);
memcpy(&buffer[1], read_pointer, buffer_size);
EXPECT_EQ(MOJO_RESULT_OK, MojoEndReadData(hc, buffer_size));
EXPECT_STREQ("hello world", buffer);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
MojoWait(hc, MOJO_WAIT_FLAG_READABLE, 1000));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(hc));
}
TEST(CoreTest, BasicSharedBuffer) {
MojoHandle h0, h1;
void* pointer;
h0 = MOJO_HANDLE_INVALID;
EXPECT_EQ(MOJO_RESULT_OK, MojoCreateSharedBuffer(NULL, 100, &h0));
EXPECT_NE(h0, MOJO_HANDLE_INVALID);
pointer = NULL;
EXPECT_EQ(MOJO_RESULT_OK,
MojoMapBuffer(h0, 0, 100, &pointer, MOJO_MAP_BUFFER_FLAG_NONE));
ASSERT_TRUE(pointer);
static_cast<char*>(pointer)[50] = 'x';
h1 = MOJO_HANDLE_INVALID;
EXPECT_EQ(MOJO_RESULT_OK, MojoDuplicateBufferHandle(h0, NULL, &h1));
EXPECT_NE(h1, MOJO_HANDLE_INVALID);
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0));
static_cast<char*>(pointer)[51] = 'y';
EXPECT_EQ(MOJO_RESULT_OK, MojoUnmapBuffer(pointer));
pointer = NULL;
EXPECT_EQ(MOJO_RESULT_OK,
MojoMapBuffer(h1, 50, 50, &pointer, MOJO_MAP_BUFFER_FLAG_NONE));
ASSERT_TRUE(pointer);
EXPECT_EQ('x', static_cast<char*>(pointer)[0]);
EXPECT_EQ('y', static_cast<char*>(pointer)[1]);
EXPECT_EQ(MOJO_RESULT_OK, MojoUnmapBuffer(pointer));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
}
extern "C" const char* MinimalCTest(void);
TEST(CoreTest, MinimalCTest) {
const char* failure = MinimalCTest();
EXPECT_TRUE(failure == NULL) << failure;
}
}
}