1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #error "This file should be compiled as C, not C++."
12 // Include all the header files that are meant to be compilable as C. Start with
13 // core.h, since it's the most important one.
14 #include "mojo/public/c/environment/async_waiter.h"
15 #include "mojo/public/c/system/core.h"
16 #include "mojo/public/c/system/macros.h"
18 // The joys of the C preprocessor....
19 #define STRINGIFY(x) #x
20 #define STRINGIFY2(x) STRINGIFY(x)
21 #define FAILURE(message) \
22 __FILE__ "(" STRINGIFY2(__LINE__) "): Failure: " message
25 #define EXPECT_EQ(a, b) \
28 return FAILURE(STRINGIFY(a) " != " STRINGIFY(b) " (expected ==)"); \
30 #define EXPECT_NE(a, b) \
33 return FAILURE(STRINGIFY(a) " == " STRINGIFY(b) " (expected !=)"); \
36 // This function exists mainly to be compiled and linked. We do some cursory
37 // checks and call it from a unit test, to make sure that link problems aren't
38 // missed due to deadstripping. Returns null on success and a string on failure
39 // (describing the failure).
40 const char* MinimalCTest(void) {
41 // MSVS before 2013 *really* only supports C90: All variables must be declared
42 // at the top. (MSVS 2013 is more reasonable.)
44 MojoHandle handle0, handle1;
45 MojoWaitFlags wait_flags;
46 const char kHello[] = "hello";
47 char buffer[200] = { 0 };
50 ticks = MojoGetTimeTicksNow();
53 handle0 = MOJO_HANDLE_INVALID;
54 EXPECT_NE(MOJO_RESULT_OK, MojoClose(handle0));
56 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
57 MojoWait(handle0, MOJO_WAIT_FLAG_EVERYTHING,
58 MOJO_DEADLINE_INDEFINITE));
60 handle1 = MOJO_HANDLE_INVALID;
61 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(&handle0, &handle1));
63 wait_flags = MOJO_WAIT_FLAG_READABLE;
64 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
65 MojoWaitMany(&handle0, &wait_flags, 1, 1));
67 EXPECT_EQ(MOJO_RESULT_OK,
68 MojoWriteMessage(handle0, kHello, (uint32_t) sizeof(kHello), NULL,
69 0u, MOJO_WRITE_DATA_FLAG_NONE));
71 EXPECT_EQ(MOJO_RESULT_OK,
72 MojoWait(handle1, MOJO_WAIT_FLAG_READABLE,
73 MOJO_DEADLINE_INDEFINITE));
75 num_bytes = (uint32_t) sizeof(buffer);
76 EXPECT_EQ(MOJO_RESULT_OK,
77 MojoReadMessage(handle1, buffer, &num_bytes, NULL, NULL,
78 MOJO_READ_MESSAGE_FLAG_NONE));
79 EXPECT_EQ((uint32_t) sizeof(kHello), num_bytes);
80 EXPECT_EQ(0, memcmp(buffer, kHello, sizeof(kHello)));
82 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle0));
83 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle1));
85 // TODO(vtl): data pipe