1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/ipc/client.h"
18 #include "perfetto/ext/ipc/host.h"
19 #include "src/base/test/test_task_runner.h"
20 #include "src/ipc/test/test_socket.h"
21 #include "test/gtest_and_gmock.h"
22
23 #include "src/ipc/test/greeter_service.gen.h"
24 #include "src/ipc/test/greeter_service.ipc.h"
25
26 namespace ipc_test {
27 namespace {
28
29 using ::testing::_;
30 using ::testing::Invoke;
31 using ::perfetto::ipc::AsyncResult;
32 using ::perfetto::ipc::Client;
33 using ::perfetto::ipc::Deferred;
34 using ::perfetto::ipc::Host;
35 using ::perfetto::ipc::Service;
36 using ::perfetto::ipc::ServiceProxy;
37
38 using namespace ::ipc_test::gen;
39
40 constexpr char kSockName[] = TEST_SOCK_NAME("ipc_integrationtest");
41
42 class MockEventListener : public ServiceProxy::EventListener {
43 public:
44 MOCK_METHOD0(OnConnect, void());
45 MOCK_METHOD0(OnDisconnect, void());
46 };
47
48 class MockGreeterService : public ::ipc_test::gen::Greeter {
49 public:
50 MOCK_METHOD2(OnSayHello,
51 void(const GreeterRequestMsg&, DeferredGreeterReplyMsg*));
SayHello(const GreeterRequestMsg & request,DeferredGreeterReplyMsg reply)52 void SayHello(const GreeterRequestMsg& request,
53 DeferredGreeterReplyMsg reply) override {
54 OnSayHello(request, &reply);
55 }
56
57 MOCK_METHOD2(OnWaveGoodbye,
58 void(const GreeterRequestMsg&, DeferredGreeterReplyMsg*));
WaveGoodbye(const GreeterRequestMsg & request,DeferredGreeterReplyMsg reply)59 void WaveGoodbye(const GreeterRequestMsg& request,
60 DeferredGreeterReplyMsg reply) override {
61 OnWaveGoodbye(request, &reply);
62 }
63 };
64
65 class IPCIntegrationTest : public ::testing::Test {
66 protected:
SetUp()67 void SetUp() override { DESTROY_TEST_SOCK(kSockName); }
TearDown()68 void TearDown() override { DESTROY_TEST_SOCK(kSockName); }
69
70 perfetto::base::TestTaskRunner task_runner_;
71 MockEventListener svc_proxy_events_;
72 };
73
TEST_F(IPCIntegrationTest,SayHelloWaveGoodbye)74 TEST_F(IPCIntegrationTest, SayHelloWaveGoodbye) {
75 std::unique_ptr<Host> host = Host::CreateInstance(kSockName, &task_runner_);
76 ASSERT_TRUE(host);
77
78 MockGreeterService* svc = new MockGreeterService();
79 ASSERT_TRUE(host->ExposeService(std::unique_ptr<Service>(svc)));
80
81 auto on_connect = task_runner_.CreateCheckpoint("on_connect");
82 EXPECT_CALL(svc_proxy_events_, OnConnect()).WillOnce(Invoke(on_connect));
83 std::unique_ptr<Client> cli =
84 Client::CreateInstance(kSockName, &task_runner_);
85 std::unique_ptr<GreeterProxy> svc_proxy(new GreeterProxy(&svc_proxy_events_));
86 cli->BindService(svc_proxy->GetWeakPtr());
87 task_runner_.RunUntilCheckpoint("on_connect");
88
89 {
90 GreeterRequestMsg req;
91 req.set_name("Mr Bojangles");
92 auto on_reply = task_runner_.CreateCheckpoint("on_hello_reply");
93 Deferred<GreeterReplyMsg> deferred_reply(
94 [on_reply](AsyncResult<GreeterReplyMsg> reply) {
95 ASSERT_TRUE(reply.success());
96 ASSERT_FALSE(reply.has_more());
97 ASSERT_EQ("Hello Mr Bojangles", reply->message());
98 on_reply();
99 });
100
101 EXPECT_CALL(*svc, OnSayHello(_, _))
102 .WillOnce(Invoke([](const GreeterRequestMsg& host_req,
103 Deferred<GreeterReplyMsg>* host_reply) {
104 auto reply = AsyncResult<GreeterReplyMsg>::Create();
105 reply->set_message("Hello " + host_req.name());
106 host_reply->Resolve(std::move(reply));
107 }));
108 svc_proxy->SayHello(req, std::move(deferred_reply));
109 task_runner_.RunUntilCheckpoint("on_hello_reply");
110 }
111
112 {
113 GreeterRequestMsg req;
114 req.set_name("Mrs Bojangles");
115 auto on_reply = task_runner_.CreateCheckpoint("on_goodbye_reply");
116 Deferred<GreeterReplyMsg> deferred_reply(
117 [on_reply](AsyncResult<GreeterReplyMsg> reply) {
118 ASSERT_TRUE(reply.success());
119 ASSERT_FALSE(reply.has_more());
120 ASSERT_EQ("Goodbye Mrs Bojangles", reply->message());
121 on_reply();
122 });
123
124 EXPECT_CALL(*svc, OnWaveGoodbye(_, _))
125 .WillOnce(Invoke([](const GreeterRequestMsg& host_req,
126 Deferred<GreeterReplyMsg>* host_reply) {
127 auto reply = AsyncResult<GreeterReplyMsg>::Create();
128 reply->set_message("Goodbye " + host_req.name());
129 host_reply->Resolve(std::move(reply));
130 }));
131 svc_proxy->WaveGoodbye(req, std::move(deferred_reply));
132 task_runner_.RunUntilCheckpoint("on_goodbye_reply");
133 }
134 }
135
136 } // namespace
137 } // namespace ipc_test
138