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.
4
5 #ifdef __cplusplus
6 #error "This file should be compiled as C, not C++."
7 #endif
8
9 #include <stddef.h>
10 #include <string.h>
11
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"
17
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
23
24 // Poor man's gtest.
25 #define EXPECT_EQ(a, b) \
26 do { \
27 if ((a) != (b)) \
28 return FAILURE(STRINGIFY(a) " != " STRINGIFY(b) " (expected ==)"); \
29 } while (0)
30 #define EXPECT_NE(a, b) \
31 do { \
32 if ((a) == (b)) \
33 return FAILURE(STRINGIFY(a) " == " STRINGIFY(b) " (expected !=)"); \
34 } while (0)
35
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).
MinimalCTest(void)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.)
43 MojoTimeTicks ticks;
44 MojoHandle handle0, handle1;
45 MojoHandleSignals signals;
46 const char kHello[] = "hello";
47 char buffer[200] = {0};
48 uint32_t num_bytes;
49
50 ticks = MojoGetTimeTicksNow();
51 EXPECT_NE(ticks, 0);
52
53 handle0 = MOJO_HANDLE_INVALID;
54 EXPECT_NE(MOJO_RESULT_OK, MojoClose(handle0));
55
56 EXPECT_EQ(
57 MOJO_RESULT_INVALID_ARGUMENT,
58 MojoWait(handle0, ~MOJO_HANDLE_SIGNAL_NONE, MOJO_DEADLINE_INDEFINITE));
59
60 handle1 = MOJO_HANDLE_INVALID;
61 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(NULL, &handle0, &handle1));
62
63 signals = MOJO_HANDLE_SIGNAL_READABLE;
64 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED,
65 MojoWaitMany(&handle0, &signals, 1, 1));
66
67 EXPECT_EQ(MOJO_RESULT_OK,
68 MojoWriteMessage(handle0,
69 kHello,
70 (uint32_t)sizeof(kHello),
71 NULL,
72 0u,
73 MOJO_WRITE_DATA_FLAG_NONE));
74
75 EXPECT_EQ(
76 MOJO_RESULT_OK,
77 MojoWait(handle1, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE));
78
79 num_bytes = (uint32_t)sizeof(buffer);
80 EXPECT_EQ(MOJO_RESULT_OK,
81 MojoReadMessage(handle1,
82 buffer,
83 &num_bytes,
84 NULL,
85 NULL,
86 MOJO_READ_MESSAGE_FLAG_NONE));
87 EXPECT_EQ((uint32_t)sizeof(kHello), num_bytes);
88 EXPECT_EQ(0, memcmp(buffer, kHello, sizeof(kHello)));
89
90 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle0));
91 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle1));
92
93 // TODO(vtl): data pipe
94
95 return NULL;
96 }
97