OLD | NEW |
| (Empty) |
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 all the header files that are meant to be compilable as C. | |
10 #include <mojo/environment/async_waiter.h> | |
11 #include <mojo/environment/logger.h> | |
12 #include <mojo/macros.h> | |
13 #include <mojo/result.h> | |
14 #include <mojo/system/buffer.h> | |
15 #include <mojo/system/data_pipe.h> | |
16 #include <mojo/system/handle.h> | |
17 #include <mojo/system/main.h> | |
18 #include <mojo/system/message_pipe.h> | |
19 #include <mojo/system/time.h> | |
20 #include <mojo/system/wait.h> | |
21 #include <mojo/system/wait_set.h> | |
22 #include <stddef.h> | |
23 #include <string.h> | |
24 | |
25 // The joys of the C preprocessor.... | |
26 #define STRINGIFY(x) #x | |
27 #define STRINGIFY2(x) STRINGIFY(x) | |
28 #define FAILURE(message) \ | |
29 __FILE__ "(" STRINGIFY2(__LINE__) "): Failure: " message | |
30 | |
31 // Poor man's gtest. | |
32 #define EXPECT_EQ(a, b) \ | |
33 do { \ | |
34 if ((a) != (b)) \ | |
35 return FAILURE(STRINGIFY(a) " != " STRINGIFY(b) " (expected ==)"); \ | |
36 } while (0) | |
37 #define EXPECT_NE(a, b) \ | |
38 do { \ | |
39 if ((a) == (b)) \ | |
40 return FAILURE(STRINGIFY(a) " == " STRINGIFY(b) " (expected !=)"); \ | |
41 } while (0) | |
42 | |
43 // This function exists mainly to be compiled and linked. We do some cursory | |
44 // checks and call it from a unit test, to make sure that link problems aren't | |
45 // missed due to deadstripping. Returns null on success and a string on failure | |
46 // (describing the failure). | |
47 const char* MinimalCTest(void) { | |
48 // MSVS before 2013 *really* only supports C90: All variables must be declared | |
49 // at the top. (MSVS 2013 is more reasonable.) | |
50 MojoTimeTicks ticks; | |
51 MojoHandle handle0, handle1; | |
52 MojoHandleSignals signals; | |
53 const char kHello[] = "hello"; | |
54 char buffer[200] = {0}; | |
55 uint32_t num_bytes; | |
56 | |
57 ticks = MojoGetTimeTicksNow(); | |
58 EXPECT_NE(ticks, 0); | |
59 | |
60 handle0 = MOJO_HANDLE_INVALID; | |
61 EXPECT_NE(MOJO_RESULT_OK, MojoClose(handle0)); | |
62 | |
63 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | |
64 MojoWait(handle0, ~MOJO_HANDLE_SIGNAL_NONE, | |
65 MOJO_DEADLINE_INDEFINITE, NULL)); | |
66 | |
67 handle1 = MOJO_HANDLE_INVALID; | |
68 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(NULL, &handle0, &handle1)); | |
69 | |
70 signals = MOJO_HANDLE_SIGNAL_READABLE; | |
71 uint32_t result_index = 123; | |
72 struct MojoHandleSignalsState states[1]; | |
73 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, | |
74 MojoWaitMany(&handle0, &signals, 1, 1, &result_index, states)); | |
75 | |
76 // "Deadline exceeded" doesn't apply to a single handle, so this should leave | |
77 // |result_index| untouched. | |
78 EXPECT_EQ(123u, result_index); | |
79 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, states[0].satisfied_signals); | |
80 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | | |
81 MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
82 states[0].satisfiable_signals); | |
83 | |
84 EXPECT_EQ(MOJO_RESULT_OK, | |
85 MojoWriteMessage(handle0, kHello, (uint32_t)sizeof(kHello), NULL, | |
86 0u, MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
87 | |
88 struct MojoHandleSignalsState state; | |
89 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(handle1, MOJO_HANDLE_SIGNAL_READABLE, | |
90 MOJO_DEADLINE_INDEFINITE, &state)); | |
91 | |
92 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, | |
93 state.satisfied_signals); | |
94 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | | |
95 MOJO_HANDLE_SIGNAL_PEER_CLOSED, | |
96 state.satisfiable_signals); | |
97 | |
98 num_bytes = (uint32_t)sizeof(buffer); | |
99 EXPECT_EQ(MOJO_RESULT_OK, MojoReadMessage(handle1, buffer, &num_bytes, NULL, | |
100 NULL, MOJO_READ_MESSAGE_FLAG_NONE)); | |
101 EXPECT_EQ((uint32_t)sizeof(kHello), num_bytes); | |
102 EXPECT_EQ(0, memcmp(buffer, kHello, sizeof(kHello))); | |
103 | |
104 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle0)); | |
105 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle1)); | |
106 | |
107 // TODO(vtl): data pipe | |
108 | |
109 return NULL; | |
110 } | |
OLD | NEW |