• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 "build/build_config.h"
6 #include "mojo/core/test/mojo_test_base.h"
7 #include "mojo/public/c/system/buffer.h"
8 #include "mojo/public/c/system/data_pipe.h"
9 #include "mojo/public/c/system/functions.h"
10 #include "mojo/public/c/system/message_pipe.h"
11 #include "mojo/public/c/system/trap.h"
12 #include "mojo/public/c/system/types.h"
13 
14 namespace mojo {
15 namespace core {
16 namespace {
17 
18 using SignalsTest = test::MojoTestBase;
19 
TEST_F(SignalsTest,QueryInvalidArguments)20 TEST_F(SignalsTest, QueryInvalidArguments) {
21   MojoHandleSignalsState state = {0, 0};
22   EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
23             MojoQueryHandleSignalsState(MOJO_HANDLE_INVALID, &state));
24 
25   MojoHandle a, b;
26   CreateMessagePipe(&a, &b);
27   EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
28             MojoQueryHandleSignalsState(a, nullptr));
29 }
30 
TEST_F(SignalsTest,QueryMessagePipeSignals)31 TEST_F(SignalsTest, QueryMessagePipeSignals) {
32   MojoHandleSignalsState state = {0, 0};
33 
34   MojoHandle a, b;
35   CreateMessagePipe(&a, &b);
36 
37   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &state));
38   EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, state.satisfied_signals);
39   EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE |
40                 MOJO_HANDLE_SIGNAL_PEER_CLOSED |
41                 MOJO_HANDLE_SIGNAL_PEER_REMOTE |
42                 MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
43             state.satisfiable_signals);
44 
45   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &state));
46   EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, state.satisfied_signals);
47   EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE |
48                 MOJO_HANDLE_SIGNAL_PEER_CLOSED |
49                 MOJO_HANDLE_SIGNAL_PEER_REMOTE |
50                 MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
51             state.satisfiable_signals);
52 
53   WriteMessage(a, "ok");
54   EXPECT_EQ(MOJO_RESULT_OK, WaitForSignals(b, MOJO_HANDLE_SIGNAL_READABLE));
55 
56   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &state));
57   EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE,
58             state.satisfied_signals);
59   EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE |
60                 MOJO_HANDLE_SIGNAL_PEER_CLOSED |
61                 MOJO_HANDLE_SIGNAL_PEER_REMOTE |
62                 MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
63             state.satisfiable_signals);
64 
65   EXPECT_EQ("ok", ReadMessage(b));
66 
67   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &state));
68   EXPECT_EQ(MOJO_HANDLE_SIGNAL_WRITABLE, state.satisfied_signals);
69   EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE |
70                 MOJO_HANDLE_SIGNAL_PEER_CLOSED |
71                 MOJO_HANDLE_SIGNAL_PEER_REMOTE |
72                 MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
73             state.satisfiable_signals);
74 
75   EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
76 
77   EXPECT_EQ(MOJO_RESULT_OK, WaitForSignals(b, MOJO_HANDLE_SIGNAL_PEER_CLOSED));
78 
79   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &state));
80   EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, state.satisfied_signals);
81   EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED | MOJO_HANDLE_SIGNAL_QUOTA_EXCEEDED,
82             state.satisfiable_signals);
83 }
84 
TEST_F(SignalsTest,LocalPeers)85 TEST_F(SignalsTest, LocalPeers) {
86   MojoHandleSignalsState state = {0, 0};
87   MojoHandle a, b, c, d;
88   CreateMessagePipe(&a, &b);
89   CreateMessagePipe(&c, &d);
90 
91   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &state));
92   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
93   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
94 
95   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &state));
96   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
97   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
98 
99   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(c, &state));
100   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
101   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
102 
103   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(d, &state));
104   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
105   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
106 
107   // Verify that sending a local pipe over a local pipe doesn't change the
108   // perceived locality of the peer.
109   const char kMessage[] = "ayyy";
110   WriteMessageWithHandles(a, kMessage, &c, 1);
111   EXPECT_EQ(kMessage, ReadMessageWithHandles(b, &c, 1));
112 
113   WriteMessage(c, kMessage);
114   EXPECT_EQ(kMessage, ReadMessage(d));
115 
116   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(c, &state));
117   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
118   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
119 
120   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(d, &state));
121   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
122   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
123 
124   // Sanity check: a closed peer can never signal remoteness.
125   EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c));
126   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(d, &state));
127   EXPECT_FALSE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
128   EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
129 
130   EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
131   EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
132 }
133 
134 #if !defined(OS_IOS)
135 
TEST_F(SignalsTest,RemotePeers)136 TEST_F(SignalsTest, RemotePeers) {
137   MojoHandleSignalsState state = {0, 0};
138   MojoHandle a, b;
139   CreateMessagePipe(&a, &b);
140 
141   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &state));
142   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
143   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
144   EXPECT_EQ(MOJO_RESULT_OK,
145             WaitForSignals(a, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
146                            MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED));
147 
148   EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(b, &state));
149   EXPECT_TRUE(state.satisfiable_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
150   EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
151   EXPECT_EQ(MOJO_RESULT_OK,
152             WaitForSignals(b, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
153                            MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED));
154 
155   RunTestClient("RemotePeersClient", [&](MojoHandle h) {
156     // The bootstrap pipe should eventually signal remoteness.
157     EXPECT_EQ(MOJO_RESULT_OK,
158               WaitForSignals(h, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
159                              MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED));
160 
161     // And so should |a| after we send its peer.
162     WriteMessageWithHandles(h, ":)", &b, 1);
163     EXPECT_EQ(MOJO_RESULT_OK,
164               WaitForSignals(a, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
165                              MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED));
166     EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &state));
167     EXPECT_TRUE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
168 
169     // And so should |c| after we fuse |d| to |a|.
170     MojoHandle c, d;
171     CreateMessagePipe(&c, &d);
172     EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(d, a, nullptr));
173     EXPECT_EQ(MOJO_RESULT_OK,
174               WaitForSignals(c, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
175                              MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED));
176     EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(c, &state));
177     EXPECT_TRUE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
178 
179     // We fused c-d to a-b, so we'll just sort of "rename" |c| back to |a| so
180     // the system resembles the state it was in before we did that.
181     a = c;
182 
183     WriteMessage(h, "OK!");
184 
185     // Read |b| back before joining the client.
186     EXPECT_EQ("O_O", ReadMessageWithHandles(h, &b, 1));
187 
188     // Wait for |a| to see its peer as local again.
189     EXPECT_EQ(MOJO_RESULT_OK,
190               WaitForSignals(a, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
191                              MOJO_TRIGGER_CONDITION_SIGNALS_UNSATISFIED));
192     EXPECT_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(a, &state));
193     EXPECT_FALSE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_PEER_REMOTE);
194   });
195 }
196 
DEFINE_TEST_CLIENT_TEST_WITH_PIPE(RemotePeersClient,SignalsTest,h)197 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(RemotePeersClient, SignalsTest, h) {
198   // The bootstrap pipe should eventually signal remoteness.
199   EXPECT_EQ(MOJO_RESULT_OK,
200             WaitForSignals(h, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
201                            MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED));
202 
203   MojoHandle b;
204   EXPECT_EQ(":)", ReadMessageWithHandles(h, &b, 1));
205 
206   // And so should |b|.
207   EXPECT_EQ(MOJO_RESULT_OK,
208             WaitForSignals(b, MOJO_HANDLE_SIGNAL_PEER_REMOTE,
209                            MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED));
210 
211   // Wait for the test to signal that it's ready to read |b| back.
212   EXPECT_EQ("OK!", ReadMessage(h));
213 
214   // Now send |b| back home.
215   WriteMessageWithHandles(h, "O_O", &b, 1);
216 }
217 
218 #endif  // !defined(OS_IOS)
219 
220 }  // namespace
221 }  // namespace core
222 }  // namespace mojo
223