diff options
Diffstat (limited to 'mojo/public/cpp/system')
-rw-r--r-- | mojo/public/cpp/system/BUILD.gn | 19 | ||||
-rw-r--r-- | mojo/public/cpp/system/buffer.h | 112 | ||||
-rw-r--r-- | mojo/public/cpp/system/core.h | 15 | ||||
-rw-r--r-- | mojo/public/cpp/system/data_pipe.h | 141 | ||||
-rw-r--r-- | mojo/public/cpp/system/functions.h | 20 | ||||
-rw-r--r-- | mojo/public/cpp/system/handle.h | 305 | ||||
-rw-r--r-- | mojo/public/cpp/system/macros.h | 45 | ||||
-rw-r--r-- | mojo/public/cpp/system/message_pipe.h | 101 | ||||
-rw-r--r-- | mojo/public/cpp/system/tests/BUILD.gn | 24 | ||||
-rw-r--r-- | mojo/public/cpp/system/tests/core_unittest.cc | 495 | ||||
-rw-r--r-- | mojo/public/cpp/system/tests/macros_unittest.cc | 128 |
11 files changed, 0 insertions, 1405 deletions
diff --git a/mojo/public/cpp/system/BUILD.gn b/mojo/public/cpp/system/BUILD.gn deleted file mode 100644 index e120462..0000000 --- a/mojo/public/cpp/system/BUILD.gn +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2014 The Chromium Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -import("../../mojo_sdk.gni") - -mojo_sdk_source_set("system") { - sources = [ - "buffer.h", - "core.h", - "data_pipe.h", - "functions.h", - "handle.h", - "macros.h", - "message_pipe.h", - ] - - mojo_sdk_public_deps = [ "mojo/public/c/system" ] -} diff --git a/mojo/public/cpp/system/buffer.h b/mojo/public/cpp/system/buffer.h deleted file mode 100644 index 6817297..0000000 --- a/mojo/public/cpp/system/buffer.h +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ -#define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ - -#include <assert.h> - -#include "mojo/public/c/system/buffer.h" -#include "mojo/public/cpp/system/handle.h" -#include "mojo/public/cpp/system/macros.h" - -namespace mojo { - -// SharedBufferHandle ---------------------------------------------------------- - -class SharedBufferHandle : public Handle { - public: - SharedBufferHandle() {} - explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} - - // Copying and assignment allowed. -}; - -static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), - "Bad size for C++ SharedBufferHandle"); - -typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; -static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), - "Bad size for C++ ScopedSharedBufferHandle"); - -inline MojoResult CreateSharedBuffer( - const MojoCreateSharedBufferOptions* options, - uint64_t num_bytes, - ScopedSharedBufferHandle* shared_buffer) { - assert(shared_buffer); - SharedBufferHandle handle; - MojoResult rv = - MojoCreateSharedBuffer(options, num_bytes, handle.mutable_value()); - // Reset even on failure (reduces the chances that a "stale"/incorrect handle - // will be used). - shared_buffer->reset(handle); - return rv; -} - -// TODO(vtl): This (and also the functions below) are templatized to allow for -// future/other buffer types. A bit "safer" would be to overload this function -// manually. (The template enforces that the in and out handles to be of the -// same type.) -template <class BufferHandleType> -inline MojoResult DuplicateBuffer( - BufferHandleType buffer, - const MojoDuplicateBufferHandleOptions* options, - ScopedHandleBase<BufferHandleType>* new_buffer) { - assert(new_buffer); - BufferHandleType handle; - MojoResult rv = MojoDuplicateBufferHandle( - buffer.value(), options, handle.mutable_value()); - // Reset even on failure (reduces the chances that a "stale"/incorrect handle - // will be used). - new_buffer->reset(handle); - return rv; -} - -template <class BufferHandleType> -inline MojoResult MapBuffer(BufferHandleType buffer, - uint64_t offset, - uint64_t num_bytes, - void** pointer, - MojoMapBufferFlags flags) { - assert(buffer.is_valid()); - return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags); -} - -inline MojoResult UnmapBuffer(void* pointer) { - assert(pointer); - return MojoUnmapBuffer(pointer); -} - -// A wrapper class that automatically creates a shared buffer and owns the -// handle. -class SharedBuffer { - public: - explicit SharedBuffer(uint64_t num_bytes); - SharedBuffer(uint64_t num_bytes, - const MojoCreateSharedBufferOptions& options); - ~SharedBuffer(); - - ScopedSharedBufferHandle handle; -}; - -inline SharedBuffer::SharedBuffer(uint64_t num_bytes) { - MojoResult result = CreateSharedBuffer(nullptr, num_bytes, &handle); - MOJO_ALLOW_UNUSED_LOCAL(result); - assert(result == MOJO_RESULT_OK); -} - -inline SharedBuffer::SharedBuffer( - uint64_t num_bytes, - const MojoCreateSharedBufferOptions& options) { - MojoResult result = CreateSharedBuffer(&options, num_bytes, &handle); - MOJO_ALLOW_UNUSED_LOCAL(result); - assert(result == MOJO_RESULT_OK); -} - -inline SharedBuffer::~SharedBuffer() { -} - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ diff --git a/mojo/public/cpp/system/core.h b/mojo/public/cpp/system/core.h deleted file mode 100644 index b08a5a6..0000000 --- a/mojo/public/cpp/system/core.h +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_SYSTEM_CORE_H_ -#define MOJO_PUBLIC_CPP_SYSTEM_CORE_H_ - -#include "mojo/public/cpp/system/buffer.h" -#include "mojo/public/cpp/system/data_pipe.h" -#include "mojo/public/cpp/system/functions.h" -#include "mojo/public/cpp/system/handle.h" -#include "mojo/public/cpp/system/macros.h" -#include "mojo/public/cpp/system/message_pipe.h" - -#endif // MOJO_PUBLIC_CPP_SYSTEM_CORE_H_ diff --git a/mojo/public/cpp/system/data_pipe.h b/mojo/public/cpp/system/data_pipe.h deleted file mode 100644 index 5d3396c..0000000 --- a/mojo/public/cpp/system/data_pipe.h +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ -#define MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ - -#include <assert.h> - -#include "mojo/public/c/system/data_pipe.h" -#include "mojo/public/cpp/system/handle.h" -#include "mojo/public/cpp/system/macros.h" - -namespace mojo { - -// DataPipeProducerHandle and DataPipeConsumerHandle --------------------------- - -class DataPipeProducerHandle : public Handle { - public: - DataPipeProducerHandle() {} - explicit DataPipeProducerHandle(MojoHandle value) : Handle(value) {} - - // Copying and assignment allowed. -}; - -static_assert(sizeof(DataPipeProducerHandle) == sizeof(Handle), - "Bad size for C++ DataPipeProducerHandle"); - -typedef ScopedHandleBase<DataPipeProducerHandle> ScopedDataPipeProducerHandle; -static_assert(sizeof(ScopedDataPipeProducerHandle) == - sizeof(DataPipeProducerHandle), - "Bad size for C++ ScopedDataPipeProducerHandle"); - -class DataPipeConsumerHandle : public Handle { - public: - DataPipeConsumerHandle() {} - explicit DataPipeConsumerHandle(MojoHandle value) : Handle(value) {} - - // Copying and assignment allowed. -}; - -static_assert(sizeof(DataPipeConsumerHandle) == sizeof(Handle), - "Bad size for C++ DataPipeConsumerHandle"); - -typedef ScopedHandleBase<DataPipeConsumerHandle> ScopedDataPipeConsumerHandle; -static_assert(sizeof(ScopedDataPipeConsumerHandle) == - sizeof(DataPipeConsumerHandle), - "Bad size for C++ ScopedDataPipeConsumerHandle"); - -inline MojoResult CreateDataPipe( - const MojoCreateDataPipeOptions* options, - ScopedDataPipeProducerHandle* data_pipe_producer, - ScopedDataPipeConsumerHandle* data_pipe_consumer) { - assert(data_pipe_producer); - assert(data_pipe_consumer); - DataPipeProducerHandle producer_handle; - DataPipeConsumerHandle consumer_handle; - MojoResult rv = MojoCreateDataPipe(options, - producer_handle.mutable_value(), - consumer_handle.mutable_value()); - // Reset even on failure (reduces the chances that a "stale"/incorrect handle - // will be used). - data_pipe_producer->reset(producer_handle); - data_pipe_consumer->reset(consumer_handle); - return rv; -} - -inline MojoResult WriteDataRaw(DataPipeProducerHandle data_pipe_producer, - const void* elements, - uint32_t* num_bytes, - MojoWriteDataFlags flags) { - return MojoWriteData(data_pipe_producer.value(), elements, num_bytes, flags); -} - -inline MojoResult BeginWriteDataRaw(DataPipeProducerHandle data_pipe_producer, - void** buffer, - uint32_t* buffer_num_bytes, - MojoWriteDataFlags flags) { - return MojoBeginWriteData( - data_pipe_producer.value(), buffer, buffer_num_bytes, flags); -} - -inline MojoResult EndWriteDataRaw(DataPipeProducerHandle data_pipe_producer, - uint32_t num_bytes_written) { - return MojoEndWriteData(data_pipe_producer.value(), num_bytes_written); -} - -inline MojoResult ReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, - void* elements, - uint32_t* num_bytes, - MojoReadDataFlags flags) { - return MojoReadData(data_pipe_consumer.value(), elements, num_bytes, flags); -} - -inline MojoResult BeginReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, - const void** buffer, - uint32_t* buffer_num_bytes, - MojoReadDataFlags flags) { - return MojoBeginReadData( - data_pipe_consumer.value(), buffer, buffer_num_bytes, flags); -} - -inline MojoResult EndReadDataRaw(DataPipeConsumerHandle data_pipe_consumer, - uint32_t num_bytes_read) { - return MojoEndReadData(data_pipe_consumer.value(), num_bytes_read); -} - -// A wrapper class that automatically creates a data pipe and owns both handles. -// TODO(vtl): Make an even more friendly version? (Maybe templatized for a -// particular type instead of some "element"? Maybe functions that take -// vectors?) -class DataPipe { - public: - DataPipe(); - explicit DataPipe(const MojoCreateDataPipeOptions& options); - ~DataPipe(); - - ScopedDataPipeProducerHandle producer_handle; - ScopedDataPipeConsumerHandle consumer_handle; -}; - -inline DataPipe::DataPipe() { - MojoResult result = - CreateDataPipe(nullptr, &producer_handle, &consumer_handle); - MOJO_ALLOW_UNUSED_LOCAL(result); - assert(result == MOJO_RESULT_OK); -} - -inline DataPipe::DataPipe(const MojoCreateDataPipeOptions& options) { - MojoResult result = - CreateDataPipe(&options, &producer_handle, &consumer_handle); - MOJO_ALLOW_UNUSED_LOCAL(result); - assert(result == MOJO_RESULT_OK); -} - -inline DataPipe::~DataPipe() { -} - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_SYSTEM_DATA_PIPE_H_ diff --git a/mojo/public/cpp/system/functions.h b/mojo/public/cpp/system/functions.h deleted file mode 100644 index d73d27a..0000000 --- a/mojo/public/cpp/system/functions.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_SYSTEM_FUNCTIONS_H_ -#define MOJO_PUBLIC_CPP_SYSTEM_FUNCTIONS_H_ - -#include "mojo/public/c/system/functions.h" - -namespace mojo { - -// Standalone functions -------------------------------------------------------- - -inline MojoTimeTicks GetTimeTicksNow() { - return MojoGetTimeTicksNow(); -} - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_SYSTEM_FUNCTIONS_H_ diff --git a/mojo/public/cpp/system/handle.h b/mojo/public/cpp/system/handle.h deleted file mode 100644 index 0c5adc7..0000000 --- a/mojo/public/cpp/system/handle.h +++ /dev/null @@ -1,305 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ -#define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ - -#include <assert.h> -#include <limits> - -#include "mojo/public/c/system/functions.h" -#include "mojo/public/c/system/types.h" -#include "mojo/public/cpp/system/macros.h" - -namespace mojo { - -// OVERVIEW -// -// |Handle| and |...Handle|: -// -// |Handle| is a simple, copyable wrapper for the C type |MojoHandle| (which is -// just an integer). Its purpose is to increase type-safety, not provide -// lifetime management. For the same purpose, we have trivial *subclasses* of -// |Handle|, e.g., |MessagePipeHandle| and |DataPipeProducerHandle|. |Handle| -// and its subclasses impose *no* extra overhead over using |MojoHandle|s -// directly. -// -// Note that though we provide constructors for |Handle|/|...Handle| from a -// |MojoHandle|, we do not provide, e.g., a constructor for |MessagePipeHandle| -// from a |Handle|. This is for type safety: If we did, you'd then be able to -// construct a |MessagePipeHandle| from, e.g., a |DataPipeProducerHandle| (since -// it's a |Handle|). -// -// |ScopedHandleBase| and |Scoped...Handle|: -// -// |ScopedHandleBase<HandleType>| is a templated scoped wrapper, for the handle -// types above (in the same sense that a C++11 |unique_ptr<T>| is a scoped -// wrapper for a |T*|). It provides lifetime management, closing its owned -// handle on destruction. It also provides (emulated) move semantics, again -// along the lines of C++11's |unique_ptr| (and exactly like Chromium's -// |scoped_ptr|). -// -// |ScopedHandle| is just (a typedef of) a |ScopedHandleBase<Handle>|. -// Similarly, |ScopedMessagePipeHandle| is just a -// |ScopedHandleBase<MessagePipeHandle>|. Etc. Note that a -// |ScopedMessagePipeHandle| is *not* a (subclass of) |ScopedHandle|. -// -// Wrapper functions: -// -// We provide simple wrappers for the |Mojo...()| functions (in -// mojo/public/c/system/core.h -- see that file for details on individual -// functions). -// -// The general guideline is functions that imply ownership transfer of a handle -// should take (or produce) an appropriate |Scoped...Handle|, while those that -// don't take a |...Handle|. For example, |CreateMessagePipe()| has two -// |ScopedMessagePipe| "out" parameters, whereas |Wait()| and |WaitMany()| take -// |Handle| parameters. Some, have both: e.g., |DuplicatedBuffer()| takes a -// suitable (unscoped) handle (e.g., |SharedBufferHandle|) "in" parameter and -// produces a suitable scoped handle (e.g., |ScopedSharedBufferHandle| a.k.a. -// |ScopedHandleBase<SharedBufferHandle>|) as an "out" parameter. -// -// An exception are some of the |...Raw()| functions. E.g., |CloseRaw()| takes a -// |Handle|, leaving the user to discard the handle. -// -// More significantly, |WriteMessageRaw()| exposes the full API complexity of -// |MojoWriteMessage()| (but doesn't require any extra overhead). It takes a raw -// array of |Handle|s as input, and takes ownership of them (i.e., invalidates -// them) on *success* (but not on failure). There are a number of reasons for -// this. First, C++03 |std::vector|s cannot contain the move-only -// |Scoped...Handle|s. Second, |std::vector|s impose extra overhead -// (necessitating heap-allocation of the buffer). Third, |std::vector|s wouldn't -// provide the desired level of flexibility/safety: a vector of handles would -// have to be all of the same type (probably |Handle|/|ScopedHandle|). Fourth, -// it's expected to not be used directly, but instead be used by generated -// bindings. -// -// Other |...Raw()| functions expose similar rough edges, e.g., dealing with raw -// pointers (and lengths) instead of taking |std::vector|s or similar. - -// ScopedHandleBase ------------------------------------------------------------ - -// Scoper for the actual handle types defined further below. It's move-only, -// like the C++11 |unique_ptr|. -template <class HandleType> -class ScopedHandleBase { - MOJO_MOVE_ONLY_TYPE(ScopedHandleBase) - - public: - ScopedHandleBase() {} - explicit ScopedHandleBase(HandleType handle) : handle_(handle) {} - ~ScopedHandleBase() { CloseIfNecessary(); } - - template <class CompatibleHandleType> - explicit ScopedHandleBase(ScopedHandleBase<CompatibleHandleType> other) - : handle_(other.release()) {} - - // Move-only constructor and operator=. - ScopedHandleBase(ScopedHandleBase&& other) : handle_(other.release()) {} - ScopedHandleBase& operator=(ScopedHandleBase&& other) { - if (&other != this) { - CloseIfNecessary(); - handle_ = other.release(); - } - return *this; - } - - const HandleType& get() const { return handle_; } - - template <typename PassedHandleType> - static ScopedHandleBase<HandleType> From( - ScopedHandleBase<PassedHandleType> other) { - static_assert( - sizeof(static_cast<PassedHandleType*>(static_cast<HandleType*>(0))), - "HandleType is not a subtype of PassedHandleType"); - return ScopedHandleBase<HandleType>( - static_cast<HandleType>(other.release().value())); - } - - void swap(ScopedHandleBase& other) { handle_.swap(other.handle_); } - - HandleType release() MOJO_WARN_UNUSED_RESULT { - HandleType rv; - rv.swap(handle_); - return rv; - } - - void reset(HandleType handle = HandleType()) { - CloseIfNecessary(); - handle_ = handle; - } - - bool is_valid() const { return handle_.is_valid(); } - - private: - void CloseIfNecessary() { - if (!handle_.is_valid()) - return; - MojoResult result = MojoClose(handle_.value()); - MOJO_ALLOW_UNUSED_LOCAL(result); - assert(result == MOJO_RESULT_OK); - } - - HandleType handle_; -}; - -template <typename HandleType> -inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) { - return ScopedHandleBase<HandleType>(handle); -} - -// Handle ---------------------------------------------------------------------- - -const MojoHandle kInvalidHandleValue = MOJO_HANDLE_INVALID; - -// Wrapper base class for |MojoHandle|. -class Handle { - public: - Handle() : value_(kInvalidHandleValue) {} - explicit Handle(MojoHandle value) : value_(value) {} - ~Handle() {} - - void swap(Handle& other) { - MojoHandle temp = value_; - value_ = other.value_; - other.value_ = temp; - } - - bool is_valid() const { return value_ != kInvalidHandleValue; } - - const MojoHandle& value() const { return value_; } - MojoHandle* mutable_value() { return &value_; } - void set_value(MojoHandle value) { value_ = value; } - - private: - MojoHandle value_; - - // Copying and assignment allowed. -}; - -// Should have zero overhead. -static_assert(sizeof(Handle) == sizeof(MojoHandle), "Bad size for C++ Handle"); - -// The scoper should also impose no more overhead. -typedef ScopedHandleBase<Handle> ScopedHandle; -static_assert(sizeof(ScopedHandle) == sizeof(Handle), - "Bad size for C++ ScopedHandle"); - -inline MojoResult Wait(Handle handle, - MojoHandleSignals signals, - MojoDeadline deadline, - MojoHandleSignalsState* signals_state) { - return MojoWait(handle.value(), signals, deadline, signals_state); -} - -const uint32_t kInvalidWaitManyIndexValue = static_cast<uint32_t>(-1); - -// Simplify the interpretation of the output from |MojoWaitMany()|. -class WaitManyResult { - public: - explicit WaitManyResult(MojoResult mojo_wait_many_result) - : result(mojo_wait_many_result), index(kInvalidWaitManyIndexValue) {} - - WaitManyResult(MojoResult mojo_wait_many_result, uint32_t result_index) - : result(mojo_wait_many_result), index(result_index) {} - - // A valid handle index is always returned if |WaitMany()| succeeds, but may - // or may not be returned if |WaitMany()| returns an error. Use this helper - // function to check if |index| is a valid index into the handle array. - bool IsIndexValid() const { return index != kInvalidWaitManyIndexValue; } - - // The |signals_states| array is always returned by |WaitMany()| on success, - // but may or may not be returned if |WaitMany()| returns an error. Use this - // helper function to check if |signals_states| holds valid data. - bool AreSignalsStatesValid() const { - return result != MOJO_RESULT_INVALID_ARGUMENT && - result != MOJO_RESULT_RESOURCE_EXHAUSTED; - } - - MojoResult result; - uint32_t index; -}; - -// |HandleVectorType| and |FlagsVectorType| should be similar enough to -// |std::vector<Handle>| and |std::vector<MojoHandleSignals>|, respectively: -// - They should have a (const) |size()| method that returns an unsigned type. -// - They must provide contiguous storage, with access via (const) reference to -// that storage provided by a (const) |operator[]()| (by reference). -template <class HandleVectorType, - class FlagsVectorType, - class SignalsStateVectorType> -inline WaitManyResult WaitMany(const HandleVectorType& handles, - const FlagsVectorType& signals, - MojoDeadline deadline, - SignalsStateVectorType* signals_states) { - if (signals.size() != handles.size() || - (signals_states && signals_states->size() != signals.size())) - return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); - if (handles.size() >= kInvalidWaitManyIndexValue) - return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); - - if (handles.size() == 0) { - return WaitManyResult( - MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); - } - - uint32_t result_index = kInvalidWaitManyIndexValue; - const Handle& first_handle = handles[0]; - const MojoHandleSignals& first_signals = signals[0]; - MojoHandleSignalsState* first_state = - signals_states ? &(*signals_states)[0] : nullptr; - MojoResult result = - MojoWaitMany(reinterpret_cast<const MojoHandle*>(&first_handle), - &first_signals, static_cast<uint32_t>(handles.size()), - deadline, &result_index, first_state); - return WaitManyResult(result, result_index); -} - -// C++ 4.10, regarding pointer conversion, says that an integral null pointer -// constant can be converted to |std::nullptr_t| (which is a typedef for -// |decltype(nullptr)|). The opposite direction is not allowed. -template <class HandleVectorType, class FlagsVectorType> -inline WaitManyResult WaitMany(const HandleVectorType& handles, - const FlagsVectorType& signals, - MojoDeadline deadline, - decltype(nullptr) signals_states) { - if (signals.size() != handles.size()) - return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); - if (handles.size() >= kInvalidWaitManyIndexValue) - return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); - - if (handles.size() == 0) { - return WaitManyResult( - MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); - } - - uint32_t result_index = kInvalidWaitManyIndexValue; - const Handle& first_handle = handles[0]; - const MojoHandleSignals& first_signals = signals[0]; - MojoResult result = MojoWaitMany( - reinterpret_cast<const MojoHandle*>(&first_handle), &first_signals, - static_cast<uint32_t>(handles.size()), deadline, &result_index, nullptr); - return WaitManyResult(result, result_index); -} - -// |Close()| takes ownership of the handle, since it'll invalidate it. -// Note: There's nothing to do, since the argument will be destroyed when it -// goes out of scope. -template <class HandleType> -inline void Close(ScopedHandleBase<HandleType> /*handle*/) { -} - -// Most users should typically use |Close()| (above) instead. -inline MojoResult CloseRaw(Handle handle) { - return MojoClose(handle.value()); -} - -// Strict weak ordering, so that |Handle|s can be used as keys in |std::map|s, -inline bool operator<(const Handle a, const Handle b) { - return a.value() < b.value(); -} - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ diff --git a/mojo/public/cpp/system/macros.h b/mojo/public/cpp/system/macros.h deleted file mode 100644 index f2bd0bc..0000000 --- a/mojo/public/cpp/system/macros.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_SYSTEM_MACROS_H_ -#define MOJO_PUBLIC_CPP_SYSTEM_MACROS_H_ - -#include "mojo/public/c/system/macros.h" - -// Define a set of C++ specific macros. -// Mojo C++ API users can assume that mojo/public/cpp/system/macros.h -// includes mojo/public/c/system/macros.h. - -// A macro to disallow the copy constructor and operator= functions. -// This should be used in the private: declarations for a class. -#define MOJO_DISALLOW_COPY_AND_ASSIGN(TypeName) \ - TypeName(const TypeName&); \ - void operator=(const TypeName&) - -// Used to calculate the number of elements in an array. -// (See |arraysize()| in Chromium's base/basictypes.h for more details.) -namespace mojo { -template <typename T, size_t N> -char(&ArraySizeHelper(T(&array)[N]))[N]; -#if !defined(_MSC_VER) -template <typename T, size_t N> -char(&ArraySizeHelper(const T(&array)[N]))[N]; -#endif -} // namespace mojo -#define MOJO_ARRAYSIZE(array) (sizeof(::mojo::ArraySizeHelper(array))) - -// Used to make a type move-only. See Chromium's base/move.h for more -// details. The MoveOnlyTypeForCPP03 typedef is for Chromium's base/callback to -// tell that this type is move-only. -#define MOJO_MOVE_ONLY_TYPE(type) \ - private: \ - type(type&); \ - void operator=(type&); \ - \ - public: \ - type&& Pass() MOJO_WARN_UNUSED_RESULT { return static_cast<type&&>(*this); } \ - typedef void MoveOnlyTypeForCPP03; \ - \ - private: -#endif // MOJO_PUBLIC_CPP_SYSTEM_MACROS_H_ diff --git a/mojo/public/cpp/system/message_pipe.h b/mojo/public/cpp/system/message_pipe.h deleted file mode 100644 index b41469e..0000000 --- a/mojo/public/cpp/system/message_pipe.h +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ -#define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ - -#include <assert.h> - -#include "mojo/public/c/system/message_pipe.h" -#include "mojo/public/cpp/system/handle.h" -#include "mojo/public/cpp/system/macros.h" - -namespace mojo { - -// MessagePipeHandle ----------------------------------------------------------- - -class MessagePipeHandle : public Handle { - public: - MessagePipeHandle() {} - explicit MessagePipeHandle(MojoHandle value) : Handle(value) {} - - // Copying and assignment allowed. -}; - -static_assert(sizeof(MessagePipeHandle) == sizeof(Handle), - "Bad size for C++ MessagePipeHandle"); - -typedef ScopedHandleBase<MessagePipeHandle> ScopedMessagePipeHandle; -static_assert(sizeof(ScopedMessagePipeHandle) == sizeof(MessagePipeHandle), - "Bad size for C++ ScopedMessagePipeHandle"); - -inline MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options, - ScopedMessagePipeHandle* message_pipe0, - ScopedMessagePipeHandle* message_pipe1) { - assert(message_pipe0); - assert(message_pipe1); - MessagePipeHandle handle0; - MessagePipeHandle handle1; - MojoResult rv = MojoCreateMessagePipe( - options, handle0.mutable_value(), handle1.mutable_value()); - // Reset even on failure (reduces the chances that a "stale"/incorrect handle - // will be used). - message_pipe0->reset(handle0); - message_pipe1->reset(handle1); - return rv; -} - -// These "raw" versions fully expose the underlying API, but don't help with -// ownership of handles (especially when writing messages). -// TODO(vtl): Write "baked" versions. -inline MojoResult WriteMessageRaw(MessagePipeHandle message_pipe, - const void* bytes, - uint32_t num_bytes, - const MojoHandle* handles, - uint32_t num_handles, - MojoWriteMessageFlags flags) { - return MojoWriteMessage( - message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); -} - -inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe, - void* bytes, - uint32_t* num_bytes, - MojoHandle* handles, - uint32_t* num_handles, - MojoReadMessageFlags flags) { - return MojoReadMessage( - message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); -} - -// A wrapper class that automatically creates a message pipe and owns both -// handles. -class MessagePipe { - public: - MessagePipe(); - explicit MessagePipe(const MojoCreateMessagePipeOptions& options); - ~MessagePipe(); - - ScopedMessagePipeHandle handle0; - ScopedMessagePipeHandle handle1; -}; - -inline MessagePipe::MessagePipe() { - MojoResult result = CreateMessagePipe(nullptr, &handle0, &handle1); - MOJO_ALLOW_UNUSED_LOCAL(result); - assert(result == MOJO_RESULT_OK); -} - -inline MessagePipe::MessagePipe(const MojoCreateMessagePipeOptions& options) { - MojoResult result = CreateMessagePipe(&options, &handle0, &handle1); - MOJO_ALLOW_UNUSED_LOCAL(result); - assert(result == MOJO_RESULT_OK); -} - -inline MessagePipe::~MessagePipe() { -} - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ diff --git a/mojo/public/cpp/system/tests/BUILD.gn b/mojo/public/cpp/system/tests/BUILD.gn deleted file mode 100644 index 7a5b7ed..0000000 --- a/mojo/public/cpp/system/tests/BUILD.gn +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2014 The Chromium Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -import("../../../mojo_sdk.gni") - -mojo_sdk_source_set("tests") { - testonly = true - - sources = [ - "core_unittest.cc", - "macros_unittest.cc", - ] - - deps = [ - "//testing/gtest", - ] - - mojo_sdk_deps = [ - "mojo/public/c/system/tests", - "mojo/public/cpp/system", - "mojo/public/cpp/test_support:test_utils", - ] -} diff --git a/mojo/public/cpp/system/tests/core_unittest.cc b/mojo/public/cpp/system/tests/core_unittest.cc deleted file mode 100644 index 4dcad43..0000000 --- a/mojo/public/cpp/system/tests/core_unittest.cc +++ /dev/null @@ -1,495 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This file tests the C++ Mojo system core wrappers. -// TODO(vtl): Maybe rename "CoreCppTest" -> "CoreTest" if/when this gets -// compiled into a different binary from the C API tests. - -#include "mojo/public/cpp/system/core.h" - -#include <stddef.h> - -#include <map> - -#include "mojo/public/cpp/system/macros.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace mojo { -namespace { - -const MojoHandleSignals kSignalReadableWritable = - MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE; - -const MojoHandleSignals kSignalAll = MOJO_HANDLE_SIGNAL_READABLE | - MOJO_HANDLE_SIGNAL_WRITABLE | - MOJO_HANDLE_SIGNAL_PEER_CLOSED; - -TEST(CoreCppTest, GetTimeTicksNow) { - const MojoTimeTicks start = GetTimeTicksNow(); - EXPECT_NE(static_cast<MojoTimeTicks>(0), start) - << "GetTimeTicksNow should return nonzero value"; -} - -TEST(CoreCppTest, Basic) { - // Basic |Handle| implementation: - { - EXPECT_EQ(MOJO_HANDLE_INVALID, kInvalidHandleValue); - - Handle h0; - EXPECT_EQ(kInvalidHandleValue, h0.value()); - EXPECT_EQ(kInvalidHandleValue, *h0.mutable_value()); - EXPECT_FALSE(h0.is_valid()); - - Handle h1(static_cast<MojoHandle>(123)); - EXPECT_EQ(static_cast<MojoHandle>(123), h1.value()); - EXPECT_EQ(static_cast<MojoHandle>(123), *h1.mutable_value()); - EXPECT_TRUE(h1.is_valid()); - *h1.mutable_value() = static_cast<MojoHandle>(456); - EXPECT_EQ(static_cast<MojoHandle>(456), h1.value()); - EXPECT_TRUE(h1.is_valid()); - - h1.swap(h0); - EXPECT_EQ(static_cast<MojoHandle>(456), h0.value()); - EXPECT_TRUE(h0.is_valid()); - EXPECT_FALSE(h1.is_valid()); - - h1.set_value(static_cast<MojoHandle>(789)); - h0.swap(h1); - EXPECT_EQ(static_cast<MojoHandle>(789), h0.value()); - EXPECT_TRUE(h0.is_valid()); - EXPECT_EQ(static_cast<MojoHandle>(456), h1.value()); - EXPECT_TRUE(h1.is_valid()); - - // Make sure copy constructor works. - Handle h2(h0); - EXPECT_EQ(static_cast<MojoHandle>(789), h2.value()); - // And assignment. - h2 = h1; - EXPECT_EQ(static_cast<MojoHandle>(456), h2.value()); - - // Make sure that we can put |Handle|s into |std::map|s. - h0 = Handle(static_cast<MojoHandle>(987)); - h1 = Handle(static_cast<MojoHandle>(654)); - h2 = Handle(static_cast<MojoHandle>(321)); - Handle h3; - std::map<Handle, int> handle_to_int; - handle_to_int[h0] = 0; - handle_to_int[h1] = 1; - handle_to_int[h2] = 2; - handle_to_int[h3] = 3; - - EXPECT_EQ(4u, handle_to_int.size()); - EXPECT_FALSE(handle_to_int.find(h0) == handle_to_int.end()); - EXPECT_EQ(0, handle_to_int[h0]); - EXPECT_FALSE(handle_to_int.find(h1) == handle_to_int.end()); - EXPECT_EQ(1, handle_to_int[h1]); - EXPECT_FALSE(handle_to_int.find(h2) == handle_to_int.end()); - EXPECT_EQ(2, handle_to_int[h2]); - EXPECT_FALSE(handle_to_int.find(h3) == handle_to_int.end()); - EXPECT_EQ(3, handle_to_int[h3]); - EXPECT_TRUE(handle_to_int.find(Handle(static_cast<MojoHandle>(13579))) == - handle_to_int.end()); - - // TODO(vtl): With C++11, support |std::unordered_map|s, etc. (Or figure out - // how to support the variations of |hash_map|.) - } - - // |Handle|/|ScopedHandle| functions: - { - ScopedHandle h; - - EXPECT_EQ(kInvalidHandleValue, h.get().value()); - - // This should be a no-op. - Close(h.Pass()); - - // It should still be invalid. - EXPECT_EQ(kInvalidHandleValue, h.get().value()); - - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, - Wait(h.get(), ~MOJO_HANDLE_SIGNAL_NONE, 1000000, nullptr)); - - std::vector<Handle> wh; - wh.push_back(h.get()); - std::vector<MojoHandleSignals> sigs; - sigs.push_back(~MOJO_HANDLE_SIGNAL_NONE); - WaitManyResult wait_many_result = - WaitMany(wh, sigs, MOJO_DEADLINE_INDEFINITE, nullptr); - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, wait_many_result.result); - EXPECT_TRUE(wait_many_result.IsIndexValid()); - EXPECT_FALSE(wait_many_result.AreSignalsStatesValid()); - - // Make sure that our specialized template correctly handles |NULL| as well - // as |nullptr|. - wait_many_result = WaitMany(wh, sigs, MOJO_DEADLINE_INDEFINITE, NULL); - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, wait_many_result.result); - EXPECT_EQ(0u, wait_many_result.index); - EXPECT_TRUE(wait_many_result.IsIndexValid()); - EXPECT_FALSE(wait_many_result.AreSignalsStatesValid()); - } - - // |MakeScopedHandle| (just compilation tests): - { - EXPECT_FALSE(MakeScopedHandle(Handle()).is_valid()); - EXPECT_FALSE(MakeScopedHandle(MessagePipeHandle()).is_valid()); - EXPECT_FALSE(MakeScopedHandle(DataPipeProducerHandle()).is_valid()); - EXPECT_FALSE(MakeScopedHandle(DataPipeConsumerHandle()).is_valid()); - EXPECT_FALSE(MakeScopedHandle(SharedBufferHandle()).is_valid()); - } - - // |MessagePipeHandle|/|ScopedMessagePipeHandle| functions: - { - MessagePipeHandle h_invalid; - EXPECT_FALSE(h_invalid.is_valid()); - EXPECT_EQ( - MOJO_RESULT_INVALID_ARGUMENT, - WriteMessageRaw( - h_invalid, nullptr, 0, nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE)); - char buffer[10] = {0}; - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, - WriteMessageRaw(h_invalid, - buffer, - sizeof(buffer), - nullptr, - 0, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, - ReadMessageRaw(h_invalid, - nullptr, - nullptr, - nullptr, - nullptr, - MOJO_READ_MESSAGE_FLAG_NONE)); - uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, - ReadMessageRaw(h_invalid, - buffer, - &buffer_size, - nullptr, - nullptr, - MOJO_READ_MESSAGE_FLAG_NONE)); - - // Basic tests of waiting and closing. - MojoHandle hv0 = kInvalidHandleValue; - { - ScopedMessagePipeHandle h0; - ScopedMessagePipeHandle h1; - EXPECT_FALSE(h0.get().is_valid()); - EXPECT_FALSE(h1.get().is_valid()); - - CreateMessagePipe(nullptr, &h0, &h1); - EXPECT_TRUE(h0.get().is_valid()); - EXPECT_TRUE(h1.get().is_valid()); - EXPECT_NE(h0.get().value(), h1.get().value()); - // Save the handle values, so we can check that things got closed - // correctly. - hv0 = h0.get().value(); - MojoHandle hv1 = h1.get().value(); - MojoHandleSignalsState state; - - EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, - Wait(h0.get(), MOJO_HANDLE_SIGNAL_READABLE, 0, &state)); - - EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, state.satisfied_signals); - EXPECT_EQ(kSignalAll, state.satisfiable_signals); - - std::vector<Handle> wh; - wh.push_back(h0.get()); - wh.push_back(h1.get()); - std::vector<MojoHandleSignals> sigs; - sigs.push_back(MOJO_HANDLE_SIGNAL_READABLE); - sigs.push_back(MOJO_HANDLE_SIGNAL_WRITABLE); - std::vector<MojoHandleSignalsState> states(sigs.size()); - WaitManyResult wait_many_result = WaitMany(wh, sigs, 1000, &states); - EXPECT_EQ(MOJO_RESULT_OK, wait_many_result.result); - EXPECT_EQ(1u, wait_many_result.index); - EXPECT_TRUE(wait_many_result.IsIndexValid()); - EXPECT_TRUE(wait_many_result.AreSignalsStatesValid()); - EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, states[0].satisfied_signals); - EXPECT_EQ(kSignalAll, states[0].satisfiable_signals); - EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, states[1].satisfied_signals); - EXPECT_EQ(kSignalAll, states[1].satisfiable_signals); - - // Test closing |h1| explicitly. - Close(h1.Pass()); - EXPECT_FALSE(h1.get().is_valid()); - - // Make sure |h1| is closed. - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, - Wait(Handle(hv1), ~MOJO_HANDLE_SIGNAL_NONE, - MOJO_DEADLINE_INDEFINITE, nullptr)); - - EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, - Wait(h0.get(), MOJO_HANDLE_SIGNAL_READABLE, - MOJO_DEADLINE_INDEFINITE, &state)); - - EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals); - EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfiable_signals); - } - // |hv0| should have been closed when |h0| went out of scope, so this close - // should fail. - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(hv0)); - - // Actually test writing/reading messages. - { - ScopedMessagePipeHandle h0; - ScopedMessagePipeHandle h1; - CreateMessagePipe(nullptr, &h0, &h1); - - const char kHello[] = "hello"; - const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h0.get(), - kHello, - kHelloSize, - nullptr, - 0, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - - MojoHandleSignalsState state; - EXPECT_EQ(MOJO_RESULT_OK, Wait(h1.get(), MOJO_HANDLE_SIGNAL_READABLE, - MOJO_DEADLINE_INDEFINITE, &state)); - EXPECT_EQ(kSignalReadableWritable, state.satisfied_signals); - EXPECT_EQ(kSignalAll, state.satisfiable_signals); - - char buffer[10] = {0}; - uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); - EXPECT_EQ(MOJO_RESULT_OK, - ReadMessageRaw(h1.get(), - buffer, - &buffer_size, - nullptr, - nullptr, - MOJO_READ_MESSAGE_FLAG_NONE)); - EXPECT_EQ(kHelloSize, buffer_size); - EXPECT_STREQ(kHello, buffer); - - // Send a handle over the previously-establish message pipe. Use the - // |MessagePipe| wrapper (to test it), which automatically creates a - // message pipe. - MessagePipe mp; - - // Write a message to |mp.handle0|, before we send |mp.handle1|. - const char kWorld[] = "world!"; - const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(mp.handle0.get(), - kWorld, - kWorldSize, - nullptr, - 0, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - - // Send |mp.handle1| over |h1| to |h0|. - MojoHandle handles[5]; - handles[0] = mp.handle1.release().value(); - EXPECT_NE(kInvalidHandleValue, handles[0]); - EXPECT_FALSE(mp.handle1.get().is_valid()); - uint32_t handles_count = 1; - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h1.get(), - kHello, - kHelloSize, - handles, - handles_count, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - // |handles[0]| should actually be invalid now. - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handles[0])); - - // Read "hello" and the sent handle. - EXPECT_EQ(MOJO_RESULT_OK, Wait(h0.get(), MOJO_HANDLE_SIGNAL_READABLE, - MOJO_DEADLINE_INDEFINITE, &state)); - EXPECT_EQ(kSignalReadableWritable, state.satisfied_signals); - EXPECT_EQ(kSignalAll, state.satisfiable_signals); - - memset(buffer, 0, sizeof(buffer)); - buffer_size = static_cast<uint32_t>(sizeof(buffer)); - for (size_t i = 0; i < MOJO_ARRAYSIZE(handles); i++) - handles[i] = kInvalidHandleValue; - handles_count = static_cast<uint32_t>(MOJO_ARRAYSIZE(handles)); - EXPECT_EQ(MOJO_RESULT_OK, - ReadMessageRaw(h0.get(), - buffer, - &buffer_size, - handles, - &handles_count, - MOJO_READ_MESSAGE_FLAG_NONE)); - EXPECT_EQ(kHelloSize, buffer_size); - EXPECT_STREQ(kHello, buffer); - EXPECT_EQ(1u, handles_count); - EXPECT_NE(kInvalidHandleValue, handles[0]); - - // Read from the sent/received handle. - mp.handle1.reset(MessagePipeHandle(handles[0])); - // Save |handles[0]| to check that it gets properly closed. - hv0 = handles[0]; - - EXPECT_EQ(MOJO_RESULT_OK, - Wait(mp.handle1.get(), MOJO_HANDLE_SIGNAL_READABLE, - MOJO_DEADLINE_INDEFINITE, &state)); - EXPECT_EQ(kSignalReadableWritable, state.satisfied_signals); - EXPECT_EQ(kSignalAll, state.satisfiable_signals); - - memset(buffer, 0, sizeof(buffer)); - buffer_size = static_cast<uint32_t>(sizeof(buffer)); - for (size_t i = 0; i < MOJO_ARRAYSIZE(handles); i++) - handles[i] = kInvalidHandleValue; - handles_count = static_cast<uint32_t>(MOJO_ARRAYSIZE(handles)); - EXPECT_EQ(MOJO_RESULT_OK, - ReadMessageRaw(mp.handle1.get(), - buffer, - &buffer_size, - handles, - &handles_count, - MOJO_READ_MESSAGE_FLAG_NONE)); - EXPECT_EQ(kWorldSize, buffer_size); - EXPECT_STREQ(kWorld, buffer); - EXPECT_EQ(0u, handles_count); - } - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(hv0)); - } - - // TODO(vtl): Test |CloseRaw()|. - // TODO(vtl): Test |reset()| more thoroughly? -} - -TEST(CoreCppTest, TearDownWithMessagesEnqueued) { - // Tear down a message pipe which still has a message enqueued, with the - // message also having a valid message pipe handle. - { - ScopedMessagePipeHandle h0; - ScopedMessagePipeHandle h1; - CreateMessagePipe(nullptr, &h0, &h1); - - // Send a handle over the previously-establish message pipe. - ScopedMessagePipeHandle h2; - ScopedMessagePipeHandle h3; - CreateMessagePipe(nullptr, &h2, &h3); - - // Write a message to |h2|, before we send |h3|. - const char kWorld[] = "world!"; - const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h2.get(), - kWorld, - kWorldSize, - nullptr, - 0, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - // And also a message to |h3|. - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h3.get(), - kWorld, - kWorldSize, - nullptr, - 0, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - - // Send |h3| over |h1| to |h0|. - const char kHello[] = "hello"; - const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); - MojoHandle h3_value; - h3_value = h3.release().value(); - EXPECT_NE(kInvalidHandleValue, h3_value); - EXPECT_FALSE(h3.get().is_valid()); - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h1.get(), - kHello, - kHelloSize, - &h3_value, - 1, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - // |h3_value| should actually be invalid now. - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(h3_value)); - - EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0.release().value())); - EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1.release().value())); - EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h2.release().value())); - } - - // Do this in a different order: make the enqueued message pipe handle only - // half-alive. - { - ScopedMessagePipeHandle h0; - ScopedMessagePipeHandle h1; - CreateMessagePipe(nullptr, &h0, &h1); - - // Send a handle over the previously-establish message pipe. - ScopedMessagePipeHandle h2; - ScopedMessagePipeHandle h3; - CreateMessagePipe(nullptr, &h2, &h3); - - // Write a message to |h2|, before we send |h3|. - const char kWorld[] = "world!"; - const uint32_t kWorldSize = static_cast<uint32_t>(sizeof(kWorld)); - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h2.get(), - kWorld, - kWorldSize, - nullptr, - 0, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - // And also a message to |h3|. - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h3.get(), - kWorld, - kWorldSize, - nullptr, - 0, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - - // Send |h3| over |h1| to |h0|. - const char kHello[] = "hello"; - const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); - MojoHandle h3_value; - h3_value = h3.release().value(); - EXPECT_NE(kInvalidHandleValue, h3_value); - EXPECT_FALSE(h3.get().is_valid()); - EXPECT_EQ(MOJO_RESULT_OK, - WriteMessageRaw(h1.get(), - kHello, - kHelloSize, - &h3_value, - 1, - MOJO_WRITE_MESSAGE_FLAG_NONE)); - // |h3_value| should actually be invalid now. - EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(h3_value)); - - EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h2.release().value())); - EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0.release().value())); - EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1.release().value())); - } -} - -TEST(CoreCppTest, ScopedHandleMoveCtor) { - ScopedSharedBufferHandle buffer1; - EXPECT_EQ(MOJO_RESULT_OK, CreateSharedBuffer(nullptr, 1024, &buffer1)); - EXPECT_TRUE(buffer1.is_valid()); - - ScopedSharedBufferHandle buffer2; - EXPECT_EQ(MOJO_RESULT_OK, CreateSharedBuffer(nullptr, 1024, &buffer2)); - EXPECT_TRUE(buffer2.is_valid()); - - // If this fails to close buffer1, ScopedHandleBase::CloseIfNecessary() will - // assert. - buffer1 = buffer2.Pass(); - - EXPECT_TRUE(buffer1.is_valid()); - EXPECT_FALSE(buffer2.is_valid()); -} - -TEST(CoreCppTest, ScopedHandleMoveCtorSelf) { - ScopedSharedBufferHandle buffer1; - EXPECT_EQ(MOJO_RESULT_OK, CreateSharedBuffer(nullptr, 1024, &buffer1)); - EXPECT_TRUE(buffer1.is_valid()); - - buffer1 = buffer1.Pass(); - - EXPECT_TRUE(buffer1.is_valid()); -} - -// TODO(vtl): Write data pipe tests. - -} // namespace -} // namespace mojo diff --git a/mojo/public/cpp/system/tests/macros_unittest.cc b/mojo/public/cpp/system/tests/macros_unittest.cc deleted file mode 100644 index 27a61bd..0000000 --- a/mojo/public/cpp/system/tests/macros_unittest.cc +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This file tests the C++ Mojo system macros and consists of "positive" tests, -// i.e., those verifying that things work (without compile errors, or even -// warnings if warnings are treated as errors). -// TODO(vtl): Maybe rename "MacrosCppTest" -> "MacrosTest" if/when this gets -// compiled into a different binary from the C API tests. -// TODO(vtl): Fix no-compile tests (which are all disabled; crbug.com/105388) -// and write some "negative" tests. - -#include "mojo/public/cpp/system/macros.h" - -#include <assert.h> -#include <stdint.h> -#include <stdlib.h> - -#include "testing/gtest/include/gtest/gtest.h" - -namespace mojo { -namespace { - -// Note: MSVS is very strict (and arguably buggy) about warnings for classes -// defined in a local scope, so define these globally. -struct TestOverrideBaseClass { - virtual ~TestOverrideBaseClass() {} - virtual void ToBeOverridden() {} - virtual void AlsoToBeOverridden() = 0; -}; - -struct TestOverrideSubclass : public TestOverrideBaseClass { - ~TestOverrideSubclass() override {} - void ToBeOverridden() override {} - void AlsoToBeOverridden() override {} -}; - -TEST(MacrosCppTest, Override) { - TestOverrideSubclass x; - x.ToBeOverridden(); - x.AlsoToBeOverridden(); -} - -// Note: MSVS is very strict (and arguably buggy) about warnings for classes -// defined in a local scope, so define these globally. -class TestDisallowCopyAndAssignClass { - public: - TestDisallowCopyAndAssignClass() {} - explicit TestDisallowCopyAndAssignClass(int) {} - void NoOp() {} - - private: - MOJO_DISALLOW_COPY_AND_ASSIGN(TestDisallowCopyAndAssignClass); -}; - -TEST(MacrosCppTest, DisallowCopyAndAssign) { - TestDisallowCopyAndAssignClass x; - x.NoOp(); - TestDisallowCopyAndAssignClass y(789); - y.NoOp(); -} - -// Test that |MOJO_ARRAYSIZE()| works in a |static_assert()|. -const int kGlobalArray[5] = {1, 2, 3, 4, 5}; -static_assert(MOJO_ARRAYSIZE(kGlobalArray) == 5u, - "MOJO_ARRAY_SIZE() failed in static_assert()"); - -TEST(MacrosCppTest, ArraySize) { - double local_array[4] = {6.7, 7.8, 8.9, 9.0}; - // MSVS considers this local variable unused since MOJO_ARRAYSIZE only takes - // the size of the type of the local and not the values itself. - MOJO_ALLOW_UNUSED_LOCAL(local_array); - EXPECT_EQ(4u, MOJO_ARRAYSIZE(local_array)); -} - -// Note: MSVS is very strict (and arguably buggy) about warnings for classes -// defined in a local scope, so define these globally. -class MoveOnlyInt { - MOJO_MOVE_ONLY_TYPE(MoveOnlyInt) - - public: - MoveOnlyInt() : is_set_(false), value_() {} - explicit MoveOnlyInt(int value) : is_set_(true), value_(value) {} - ~MoveOnlyInt() {} - - // Move-only constructor and operator=. - MoveOnlyInt(MoveOnlyInt&& other) { *this = other.Pass(); } - MoveOnlyInt& operator=(MoveOnlyInt&& other) { - if (&other != this) { - is_set_ = other.is_set_; - value_ = other.value_; - other.is_set_ = false; - } - return *this; - } - - int value() const { - assert(is_set()); - return value_; - } - bool is_set() const { return is_set_; } - - private: - bool is_set_; - int value_; -}; - -TEST(MacrosCppTest, MoveOnlyTypeForCpp03) { - MoveOnlyInt x(123); - EXPECT_TRUE(x.is_set()); - EXPECT_EQ(123, x.value()); - MoveOnlyInt y; - EXPECT_FALSE(y.is_set()); - y = x.Pass(); - EXPECT_FALSE(x.is_set()); - EXPECT_TRUE(y.is_set()); - EXPECT_EQ(123, y.value()); - MoveOnlyInt z(y.Pass()); - EXPECT_FALSE(y.is_set()); - EXPECT_TRUE(z.is_set()); - EXPECT_EQ(123, z.value()); - z = z.Pass(); - EXPECT_TRUE(z.is_set()); - EXPECT_EQ(123, z.value()); -} - -} // namespace -} // namespace mojo |