1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // Utility functions for testing with IPC.
31
32 #include "ipc/ipc_test_util.h"
33
34 #include "base/logging.h"
35
36 namespace mozc {
37 #ifdef OS_MACOSX
TestMachPortManager()38 TestMachPortManager::TestMachPortManager() {
39 ipc_space_t self_task = mach_task_self();
40 // Create a new mach port with receive right.
41 CHECK_EQ(KERN_SUCCESS,
42 mach_port_allocate(self_task, MACH_PORT_RIGHT_RECEIVE, &port_));
43 // Add send right to the new mach port.
44 CHECK_EQ(KERN_SUCCESS,
45 mach_port_insert_right(
46 self_task, port_, port_, MACH_MSG_TYPE_MAKE_SEND));
47 }
48
~TestMachPortManager()49 TestMachPortManager::~TestMachPortManager() {
50 mach_port_destroy(mach_task_self(), port_);
51 }
52
GetMachPort(const string & name,mach_port_t * port)53 bool TestMachPortManager::GetMachPort(const string &name, mach_port_t *port) {
54 *port = port_;
55 return true;
56 }
57
58 // Server is always running for test because both client and server is
59 // running in a same process.
IsServerRunning(const string & name) const60 bool TestMachPortManager::IsServerRunning(const string &name) const {
61 return true;
62 }
63 #endif
64
NewClient(const string & name,const string & path_name)65 IPCClientInterface *IPCClientFactoryOnMemory::NewClient(
66 const string &name, const string &path_name) {
67 IPCClient *new_client = new IPCClient(name, path_name);
68 #ifdef OS_MACOSX
69 new_client->SetMachPortManager(&mach_manager_);
70 #endif
71 return new_client;
72 }
73
NewClient(const string & name)74 IPCClientInterface *IPCClientFactoryOnMemory::NewClient(const string &name) {
75 IPCClient *new_client = new IPCClient(name);
76 #ifdef OS_MACOSX
77 new_client->SetMachPortManager(&mach_manager_);
78 #endif
79 return new_client;
80 }
81 } // namespace mozc
82