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 "mojo/system/embedder.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 // TODO(vtl): Remove build_config.h include when fully implemented on Windows. | |
13 #include "build/build_config.h" | |
14 #include "mojo/public/system/core.h" | |
15 #include "mojo/system/platform_channel_pair.h" | |
16 #include "mojo/system/test_embedder.h" | |
17 #include "mojo/system/test_utils.h" | |
18 | |
19 namespace mojo { | |
20 namespace embedder { | |
21 namespace { | |
22 | |
23 typedef system::test::TestWithIOThreadBase EmbedderTest; | |
24 | |
25 void StoreChannelInfo(ChannelInfo** store_channel_info_here, | |
26 ChannelInfo* channel_info) { | |
27 CHECK(store_channel_info_here); | |
28 CHECK(channel_info); | |
29 *store_channel_info_here = channel_info; | |
30 } | |
31 | |
32 TEST_F(EmbedderTest, ChannelsBasic) { | |
33 Init(); | |
34 | |
35 // TODO(vtl): |PlatformChannelPair| not implemented on Windows yet. | |
36 #if !defined(OS_WIN) | |
37 PlatformChannelPair channel_pair; | |
38 ScopedPlatformHandle server_handle = channel_pair.PassServerHandle(); | |
39 ScopedPlatformHandle client_handle = channel_pair.PassClientHandle(); | |
40 | |
41 ChannelInfo* server_channel_info = NULL; | |
42 MojoHandle server_mp = CreateChannel(server_handle.Pass(), | |
43 io_thread_task_runner(), | |
44 base::Bind(&StoreChannelInfo, | |
45 &server_channel_info)); | |
46 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); | |
47 | |
48 ChannelInfo* client_channel_info = NULL; | |
49 MojoHandle client_mp = CreateChannel(client_handle.Pass(), | |
50 io_thread_task_runner(), | |
51 base::Bind(&StoreChannelInfo, | |
52 &client_channel_info)); | |
53 EXPECT_NE(client_mp, MOJO_HANDLE_INVALID); | |
54 | |
55 // We can write to a message pipe handle immediately. | |
56 const char kHello[] = "hello"; | |
57 EXPECT_EQ(MOJO_RESULT_OK, | |
58 MojoWriteMessage(server_mp, kHello, | |
59 static_cast<uint32_t>(sizeof(kHello)), NULL, 0u, | |
60 MOJO_WRITE_MESSAGE_FLAG_NONE)); | |
61 | |
62 // Now wait for the other side to become readable. | |
63 EXPECT_EQ(MOJO_RESULT_OK, | |
64 MojoWait(client_mp, MOJO_WAIT_FLAG_READABLE, | |
65 MOJO_DEADLINE_INDEFINITE)); | |
66 | |
67 char buffer[1000] = {}; | |
68 uint32_t num_bytes = static_cast<uint32_t>(sizeof(kHello)); | |
69 EXPECT_EQ(MOJO_RESULT_OK, | |
70 MojoReadMessage(client_mp, buffer, &num_bytes, NULL, NULL, | |
71 MOJO_READ_MESSAGE_FLAG_NONE)); | |
72 EXPECT_EQ(sizeof(kHello), num_bytes); | |
73 EXPECT_EQ(0, memcmp(buffer, kHello, num_bytes)); | |
74 | |
75 // TODO(vtl): FIXME -- This rapid-fire closing leads to a warning: "Received a | |
76 // message for nonexistent local destination ID 1". This is due to a race | |
77 // condition (in channel.cc/message_pipe.cc). | |
78 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp)); | |
79 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp)); | |
80 | |
81 EXPECT_TRUE(server_channel_info != NULL); | |
82 system::test::PostTaskAndWait(io_thread_task_runner(), | |
83 FROM_HERE, | |
84 base::Bind(&DestroyChannelOnIOThread, | |
85 server_channel_info)); | |
86 | |
87 EXPECT_TRUE(client_channel_info != NULL); | |
88 system::test::PostTaskAndWait(io_thread_task_runner(), | |
89 FROM_HERE, | |
90 base::Bind(&DestroyChannelOnIOThread, | |
91 client_channel_info)); | |
92 #endif // !defined(OS_WIN) | |
93 | |
94 test::Shutdown(); | |
95 } | |
96 | |
97 // TODO(vtl): Test immediate write & close. | |
98 // TODO(vtl): Test broken-connection cases. | |
99 | |
100 } // namespace | |
101 } // namespace embedder | |
102 } // namespace mojo | |
OLD | NEW |