OLD | NEW |
| (Empty) |
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 #include <stdlib.h> | |
6 #include <string.h> | |
7 | |
8 #include "mojo/public/cpp/bindings/lib/message_builder.h" | |
9 #include "mojo/public/cpp/bindings/lib/message_queue.h" | |
10 #include "mojo/public/cpp/bindings/lib/router.h" | |
11 #include "mojo/public/cpp/environment/environment.h" | |
12 #include "mojo/public/cpp/system/macros.h" | |
13 #include "mojo/public/cpp/utility/run_loop.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace mojo { | |
17 namespace test { | |
18 namespace { | |
19 | |
20 void AllocRequestMessage(uint32_t name, const char* text, Message* message) { | |
21 size_t payload_size = strlen(text) + 1; // Plus null terminator. | |
22 internal::RequestMessageBuilder builder(name, payload_size); | |
23 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); | |
24 builder.Finish(message); | |
25 } | |
26 | |
27 void AllocResponseMessage(uint32_t name, | |
28 const char* text, | |
29 uint64_t request_id, | |
30 Message* message) { | |
31 size_t payload_size = strlen(text) + 1; // Plus null terminator. | |
32 internal::ResponseMessageBuilder builder(name, payload_size, request_id); | |
33 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); | |
34 builder.Finish(message); | |
35 } | |
36 | |
37 class MessageAccumulator : public MessageReceiver { | |
38 public: | |
39 explicit MessageAccumulator(internal::MessageQueue* queue) : queue_(queue) {} | |
40 | |
41 bool Accept(Message* message) override { | |
42 queue_->Push(message); | |
43 return true; | |
44 } | |
45 | |
46 private: | |
47 internal::MessageQueue* queue_; | |
48 }; | |
49 | |
50 class ResponseGenerator : public MessageReceiverWithResponder { | |
51 public: | |
52 ResponseGenerator() {} | |
53 | |
54 bool Accept(Message* message) override { return false; } | |
55 | |
56 bool AcceptWithResponder(Message* message, | |
57 MessageReceiver* responder) override { | |
58 EXPECT_TRUE(message->has_flag(internal::kMessageExpectsResponse)); | |
59 | |
60 return SendResponse(message->name(), message->request_id(), responder); | |
61 } | |
62 | |
63 bool SendResponse(uint32_t name, | |
64 uint64_t request_id, | |
65 MessageReceiver* responder) { | |
66 Message response; | |
67 AllocResponseMessage(name, "world", request_id, &response); | |
68 | |
69 bool result = responder->Accept(&response); | |
70 delete responder; | |
71 return result; | |
72 } | |
73 }; | |
74 | |
75 class LazyResponseGenerator : public ResponseGenerator { | |
76 public: | |
77 LazyResponseGenerator() : responder_(nullptr), name_(0), request_id_(0) {} | |
78 | |
79 ~LazyResponseGenerator() override { delete responder_; } | |
80 | |
81 bool AcceptWithResponder(Message* message, | |
82 MessageReceiver* responder) override { | |
83 name_ = message->name(); | |
84 request_id_ = message->request_id(); | |
85 responder_ = responder; | |
86 return true; | |
87 } | |
88 | |
89 bool has_responder() const { return !!responder_; } | |
90 | |
91 void Complete() { | |
92 SendResponse(name_, request_id_, responder_); | |
93 responder_ = nullptr; | |
94 } | |
95 | |
96 private: | |
97 MessageReceiver* responder_; | |
98 uint32_t name_; | |
99 uint64_t request_id_; | |
100 }; | |
101 | |
102 class RouterTest : public testing::Test { | |
103 public: | |
104 RouterTest() {} | |
105 | |
106 void SetUp() override { | |
107 CreateMessagePipe(nullptr, &handle0_, &handle1_); | |
108 } | |
109 | |
110 void TearDown() override {} | |
111 | |
112 void PumpMessages() { loop_.RunUntilIdle(); } | |
113 | |
114 protected: | |
115 ScopedMessagePipeHandle handle0_; | |
116 ScopedMessagePipeHandle handle1_; | |
117 | |
118 private: | |
119 Environment env_; | |
120 RunLoop loop_; | |
121 }; | |
122 | |
123 TEST_F(RouterTest, BasicRequestResponse) { | |
124 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | |
125 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | |
126 | |
127 ResponseGenerator generator; | |
128 router1.set_incoming_receiver(&generator); | |
129 | |
130 Message request; | |
131 AllocRequestMessage(1, "hello", &request); | |
132 | |
133 internal::MessageQueue message_queue; | |
134 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | |
135 | |
136 PumpMessages(); | |
137 | |
138 EXPECT_FALSE(message_queue.IsEmpty()); | |
139 | |
140 Message response; | |
141 message_queue.Pop(&response); | |
142 | |
143 EXPECT_EQ(std::string("world"), | |
144 std::string(reinterpret_cast<const char*>(response.payload()))); | |
145 } | |
146 | |
147 TEST_F(RouterTest, BasicRequestResponse_Synchronous) { | |
148 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | |
149 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | |
150 | |
151 ResponseGenerator generator; | |
152 router1.set_incoming_receiver(&generator); | |
153 | |
154 Message request; | |
155 AllocRequestMessage(1, "hello", &request); | |
156 | |
157 internal::MessageQueue message_queue; | |
158 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | |
159 | |
160 router1.WaitForIncomingMessage(); | |
161 router0.WaitForIncomingMessage(); | |
162 | |
163 EXPECT_FALSE(message_queue.IsEmpty()); | |
164 | |
165 Message response; | |
166 message_queue.Pop(&response); | |
167 | |
168 EXPECT_EQ(std::string("world"), | |
169 std::string(reinterpret_cast<const char*>(response.payload()))); | |
170 } | |
171 | |
172 TEST_F(RouterTest, RequestWithNoReceiver) { | |
173 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | |
174 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | |
175 | |
176 // Without an incoming receiver set on router1, we expect router0 to observe | |
177 // an error as a result of sending a message. | |
178 | |
179 Message request; | |
180 AllocRequestMessage(1, "hello", &request); | |
181 | |
182 internal::MessageQueue message_queue; | |
183 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | |
184 | |
185 PumpMessages(); | |
186 | |
187 EXPECT_TRUE(router0.encountered_error()); | |
188 EXPECT_TRUE(router1.encountered_error()); | |
189 EXPECT_TRUE(message_queue.IsEmpty()); | |
190 } | |
191 | |
192 TEST_F(RouterTest, LateResponse) { | |
193 // Test that things won't blow up if we try to send a message to a | |
194 // MessageReceiver, which was given to us via AcceptWithResponder, | |
195 // after the router has gone away. | |
196 | |
197 LazyResponseGenerator generator; | |
198 { | |
199 internal::Router router0(handle0_.Pass(), internal::FilterChain()); | |
200 internal::Router router1(handle1_.Pass(), internal::FilterChain()); | |
201 | |
202 router1.set_incoming_receiver(&generator); | |
203 | |
204 Message request; | |
205 AllocRequestMessage(1, "hello", &request); | |
206 | |
207 internal::MessageQueue message_queue; | |
208 router0.AcceptWithResponder(&request, | |
209 new MessageAccumulator(&message_queue)); | |
210 | |
211 PumpMessages(); | |
212 | |
213 EXPECT_TRUE(generator.has_responder()); | |
214 } | |
215 | |
216 generator.Complete(); // This should end up doing nothing. | |
217 } | |
218 | |
219 } // namespace | |
220 } // namespace test | |
221 } // namespace mojo | |
OLD | NEW |