OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "mojo/public/cpp/bindings/lib/synchronous_connector.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include <string> | |
10 #include <utility> | |
11 | |
12 #include "gtest/gtest.h" | |
13 #include "mojo/public/cpp/bindings/lib/message_builder.h" | |
14 #include "mojo/public/cpp/bindings/message.h" | |
15 | |
16 namespace mojo { | |
17 namespace test { | |
18 namespace { | |
19 | |
20 void AllocMessage(const std::string& text, Message* message) { | |
21 size_t payload_size = text.length() + 1; // Plus null terminator. | |
22 MessageBuilder builder(1, payload_size); | |
23 memcpy(builder.buffer()->Allocate(payload_size), text.c_str(), payload_size); | |
24 | |
25 builder.message()->MoveTo(message); | |
26 } | |
27 | |
28 // Simple success case. | |
29 TEST(SynchronousConnectorTest, Basic) { | |
30 MessagePipe pipe; | |
31 internal::SynchronousConnector connector0(std::move(pipe.handle0)); | |
32 internal::SynchronousConnector connector1(std::move(pipe.handle1)); | |
33 | |
34 const std::string kText("hello world"); | |
35 Message message; | |
36 AllocMessage(kText, &message); | |
37 | |
38 EXPECT_TRUE(connector0.Write(&message)); | |
39 | |
40 Message message_received; | |
41 EXPECT_TRUE(connector1.BlockingRead(&message_received)); | |
42 | |
43 EXPECT_EQ( | |
44 kText, | |
45 std::string(reinterpret_cast<const char*>(message_received.payload()))); | |
46 } | |
47 | |
48 // Writing to a closed pipe should fail. | |
49 TEST(SynchronousConnectorTest, WriteToClosedPipe) { | |
50 MessagePipe pipe; | |
51 internal::SynchronousConnector connector0(std::move(pipe.handle0)); | |
52 | |
53 // Close the other end of the pipe. | |
54 pipe.handle1.reset(); | |
55 | |
56 Message message; | |
57 EXPECT_FALSE(connector0.Write(&message)); | |
58 } | |
59 | |
60 // Reading from a closed pipe should fail (while waiting on it). | |
61 TEST(SynchronousConnectorTest, ReadFromClosedPipe) { | |
62 MessagePipe pipe; | |
63 internal::SynchronousConnector connector0(std::move(pipe.handle0)); | |
64 | |
65 // Close the other end of the pipe. | |
66 pipe.handle1.reset(); | |
67 | |
68 Message message; | |
69 EXPECT_FALSE(connector0.BlockingRead(&message)); | |
70 } | |
71 | |
72 } // namespace | |
73 } // namespace test | |
74 } // namespace mojo | |
OLD | NEW |