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 <stdint.h> 11 #include <string.h> 12 13 // Include all the header files that are meant to be compilable as C. Start with 14 // core.h, since it's the most important one. 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 // Makeshift 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). 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(MOJO_RESULT_INVALID_ARGUMENT, 57 MojoWait(handle0, ~MOJO_HANDLE_SIGNAL_NONE, 58 MOJO_DEADLINE_INDEFINITE, NULL)); 59 60 handle1 = MOJO_HANDLE_INVALID; 61 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(NULL, &handle0, &handle1)); 62 63 signals = MOJO_HANDLE_SIGNAL_READABLE; 64 uint32_t result_index = 123; 65 struct MojoHandleSignalsState states[1]; 66 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, 67 MojoWaitMany(&handle0, &signals, 1, 1, &result_index, states)); 68 69 // "Deadline exceeded" doesn't apply to a single handle, so this should leave 70 // |result_index| untouched. 71 EXPECT_EQ(123u, result_index); 72 EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, states[0].satisfied_signals); 73 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | 74 MOJO_HANDLE_SIGNAL_PEER_CLOSED, 75 states[0].satisfiable_signals); 76 77 EXPECT_EQ(MOJO_RESULT_OK, 78 MojoWriteMessage(handle0, kHello, (uint32_t)sizeof(kHello), NULL, 79 0u, MOJO_WRITE_DATA_FLAG_NONE)); 80 81 struct MojoHandleSignalsState state; 82 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(handle1, MOJO_HANDLE_SIGNAL_READABLE, 83 MOJO_DEADLINE_INDEFINITE, &state)); 84 85 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, 86 state.satisfied_signals); 87 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | 88 MOJO_HANDLE_SIGNAL_PEER_CLOSED, 89 state.satisfiable_signals); 90 91 num_bytes = (uint32_t)sizeof(buffer); 92 EXPECT_EQ(MOJO_RESULT_OK, MojoReadMessage(handle1, buffer, &num_bytes, NULL, 93 NULL, MOJO_READ_MESSAGE_FLAG_NONE)); 94 EXPECT_EQ((uint32_t)sizeof(kHello), num_bytes); 95 EXPECT_EQ(0, memcmp(buffer, kHello, sizeof(kHello))); 96 97 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle0)); 98 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handle1)); 99 100 // TODO(vtl): data pipe 101 102 return NULL; 103 } 104