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 #include "ipc/ipc.h"
31 
32 #include <algorithm>
33 #include <vector>
34 
35 #include "base/flags.h"
36 #include "base/port.h"
37 #include "base/system_util.h"
38 #include "base/thread.h"
39 #include "base/util.h"
40 #include "ipc/ipc_test_util.h"
41 #include "testing/base/public/googletest.h"
42 #include "testing/base/public/gunit.h"
43 
44 DECLARE_string(test_tmpdir);
45 
46 namespace {
47 
48 // NOTE(komatsu): The name should not end with "_test", otherwise our
49 // testing tool rut.py misunderstood that the file named
50 // kServerAddress is a binary to be tested.
51 static const char kServerAddress[] = "test_echo_server";
52 #ifdef OS_WIN
53 // On windows, multiple-connections failed.
54 static const int kNumThreads = 1;
55 #else
56 static const int kNumThreads = 5;
57 #endif
58 static const int kNumRequests = 2000;
59 
GenRandomString(size_t size)60 string GenRandomString(size_t size) {
61   string result;
62   while (result.length() < size) {
63     result += static_cast<char>(mozc::Util::Random(256));
64   }
65   return result;
66 }
67 
68 class MultiConnections: public mozc::Thread {
69  public:
70 #ifdef OS_MACOSX
MultiConnections()71   MultiConnections()
72       : mach_port_manager_(NULL) {}
73 
SetMachPortManager(mozc::MachPortManagerInterface * manager)74   void SetMachPortManager(mozc::MachPortManagerInterface *manager) {
75     mach_port_manager_ = manager;
76   }
77 #endif
78 
Run()79   void Run() {
80     char buf[8192];
81     mozc::Util::Sleep(2000);
82     for (int i = 0; i < kNumRequests; ++i) {
83       mozc::IPCClient con(kServerAddress, "");
84 #ifdef OS_MACOSX
85       con.SetMachPortManager(mach_port_manager_);
86 #endif
87       ASSERT_TRUE(con.Connected());
88       const int size = std::max(mozc::Util::Random(8000), 1);
89       string input = "test";
90       input += GenRandomString(size);
91       size_t length = sizeof(buf);
92       ASSERT_TRUE(con.Call(input.data(), input.size(), buf, &length, 1000));
93       string output(buf, length);
94       EXPECT_EQ(input.size(), output.size());
95       EXPECT_EQ(input, output);
96     }
97   }
98 
99  private:
100 #ifdef OS_MACOSX
101   mozc::MachPortManagerInterface *mach_port_manager_;
102 #endif
103 };
104 
105 class EchoServer: public mozc::IPCServer {
106  public:
EchoServer(const string & path,int32 num_connections,int32 timeout)107   EchoServer(const string &path,
108              int32 num_connections,
109              int32 timeout) :
110       IPCServer(path, num_connections, timeout) {}
Process(const char * input_buffer,size_t input_length,char * output_buffer,size_t * output_length)111   virtual bool Process(const char *input_buffer,
112                        size_t input_length,
113                        char *output_buffer,
114                        size_t *output_length) {
115     if (::memcmp("kill", input_buffer, 4) == 0) {
116       *output_length = 0;
117       return false;
118     }
119     ::memcpy(output_buffer, input_buffer, input_length);
120     *output_length = input_length;
121     return true;
122   }
123 };
124 }  // namespace
125 
TEST(IPCTest,IPCTest)126 TEST(IPCTest, IPCTest) {
127   mozc::SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
128 #ifdef OS_MACOSX
129   mozc::TestMachPortManager manager;
130 #endif
131 
132   EchoServer con(kServerAddress, 10, 1000);
133 #ifdef OS_MACOSX
134   con.SetMachPortManager(&manager);
135 #endif
136   con.LoopAndReturn();
137 
138   // mozc::Thread is not designed as value-semantics.
139   // So here we use pointers to maintain these instances.
140   std::vector<MultiConnections *> cons(kNumThreads);
141   for (size_t i = 0; i < cons.size(); ++i) {
142     cons[i] = new MultiConnections;
143 #ifdef OS_MACOSX
144     cons[i]->SetMachPortManager(&manager);
145 #endif
146     cons[i]->SetJoinable(true);
147     cons[i]->Start("IPCTest");
148   }
149   for (size_t i = 0; i < cons.size(); ++i) {
150     cons[i]->Join();
151     delete cons[i];
152     cons[i] = NULL;
153   }
154 
155   mozc::IPCClient kill(kServerAddress, "");
156   const char kill_cmd[32] = "kill";
157   char output[32];
158   size_t output_size = sizeof(output);
159 #ifdef OS_MACOSX
160   kill.SetMachPortManager(&manager);
161 #endif
162   // We don't care the return value of this Call() because the return
163   // value for server finish can change based on the platform
164   // implementations.
165   // TODO(mukai, team): determine the spec of return value for that
166   // case and add EXPECT_(TRUE|FALSE) here.
167   kill.Call(kill_cmd, strlen(kill_cmd), output, &output_size, 1000);
168 
169   con.Wait();
170 }
171