1 /*
2 * Copyright (C) 2010 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 <array>
18
19 #include "TestHelpers.h"
20
21 #include <unistd.h>
22 #include <time.h>
23 #include <errno.h>
24
25 #include <binder/Binder.h>
26 #include <binder/Parcel.h>
27 #include <gtest/gtest.h>
28 #include <input/InputTransport.h>
29 #include <utils/StopWatch.h>
30 #include <utils/StrongPointer.h>
31 #include <utils/Timers.h>
32
33 namespace android {
34
35 class InputChannelTest : public testing::Test {
36 };
37
38
TEST_F(InputChannelTest,ConstructorAndDestructor_TakesOwnershipOfFileDescriptors)39 TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptors) {
40 // Our purpose here is to verify that the input channel destructor closes the
41 // file descriptor provided to it. One easy way is to provide it with one end
42 // of a pipe and to check for EPIPE on the other end after the channel is destroyed.
43 Pipe pipe;
44
45 android::base::unique_fd sendFd(pipe.sendFd);
46
47 std::unique_ptr<InputChannel> inputChannel =
48 InputChannel::create("channel name", std::move(sendFd), new BBinder());
49
50 EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created";
51 EXPECT_STREQ("channel name", inputChannel->getName().c_str())
52 << "channel should have provided name";
53 EXPECT_NE(-1, inputChannel->getFd()) << "channel should have valid fd";
54
55 // InputChannel should be the owner of the file descriptor now
56 ASSERT_FALSE(sendFd.ok());
57 }
58
TEST_F(InputChannelTest,SetAndGetToken)59 TEST_F(InputChannelTest, SetAndGetToken) {
60 Pipe pipe;
61 sp<IBinder> token = new BBinder();
62 std::unique_ptr<InputChannel> channel =
63 InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd), token);
64
65 EXPECT_EQ(token, channel->getConnectionToken());
66 }
67
TEST_F(InputChannelTest,OpenInputChannelPair_ReturnsAPairOfConnectedChannels)68 TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
69 std::unique_ptr<InputChannel> serverChannel, clientChannel;
70
71 status_t result = InputChannel::openInputChannelPair("channel name",
72 serverChannel, clientChannel);
73
74 ASSERT_EQ(OK, result)
75 << "should have successfully opened a channel pair";
76
77 // Name
78 EXPECT_STREQ("channel name (server)", serverChannel->getName().c_str())
79 << "server channel should have suffixed name";
80 EXPECT_STREQ("channel name (client)", clientChannel->getName().c_str())
81 << "client channel should have suffixed name";
82
83 // Server->Client communication
84 InputMessage serverMsg;
85 memset(&serverMsg, 0, sizeof(InputMessage));
86 serverMsg.header.type = InputMessage::Type::KEY;
87 serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
88 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
89 << "server channel should be able to send message to client channel";
90
91 InputMessage clientMsg;
92 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
93 << "client channel should be able to receive message from server channel";
94 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
95 << "client channel should receive the correct message from server channel";
96 EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
97 << "client channel should receive the correct message from server channel";
98
99 // Client->Server communication
100 InputMessage clientReply;
101 memset(&clientReply, 0, sizeof(InputMessage));
102 clientReply.header.type = InputMessage::Type::FINISHED;
103 clientReply.header.seq = 0x11223344;
104 clientReply.body.finished.handled = true;
105 EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
106 << "client channel should be able to send message to server channel";
107
108 InputMessage serverReply;
109 EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply))
110 << "server channel should be able to receive message from client channel";
111 EXPECT_EQ(clientReply.header.type, serverReply.header.type)
112 << "server channel should receive the correct message from client channel";
113 EXPECT_EQ(clientReply.header.seq, serverReply.header.seq)
114 << "server channel should receive the correct message from client channel";
115 EXPECT_EQ(clientReply.body.finished.handled, serverReply.body.finished.handled)
116 << "server channel should receive the correct message from client channel";
117 }
118
TEST_F(InputChannelTest,ReceiveSignal_WhenNoSignalPresent_ReturnsAnError)119 TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
120 std::unique_ptr<InputChannel> serverChannel, clientChannel;
121
122 status_t result = InputChannel::openInputChannelPair("channel name",
123 serverChannel, clientChannel);
124
125 ASSERT_EQ(OK, result)
126 << "should have successfully opened a channel pair";
127
128 InputMessage msg;
129 EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
130 << "receiveMessage should have returned WOULD_BLOCK";
131 }
132
TEST_F(InputChannelTest,ReceiveSignal_WhenPeerClosed_ReturnsAnError)133 TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
134 std::unique_ptr<InputChannel> serverChannel, clientChannel;
135
136 status_t result = InputChannel::openInputChannelPair("channel name",
137 serverChannel, clientChannel);
138
139 ASSERT_EQ(OK, result)
140 << "should have successfully opened a channel pair";
141
142 serverChannel.reset(); // close server channel
143
144 InputMessage msg;
145 EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
146 << "receiveMessage should have returned DEAD_OBJECT";
147 }
148
TEST_F(InputChannelTest,SendSignal_WhenPeerClosed_ReturnsAnError)149 TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
150 std::unique_ptr<InputChannel> serverChannel, clientChannel;
151
152 status_t result = InputChannel::openInputChannelPair("channel name",
153 serverChannel, clientChannel);
154
155 ASSERT_EQ(OK, result)
156 << "should have successfully opened a channel pair";
157
158 serverChannel.reset(); // close server channel
159
160 InputMessage msg;
161 msg.header.type = InputMessage::Type::KEY;
162 EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
163 << "sendMessage should have returned DEAD_OBJECT";
164 }
165
TEST_F(InputChannelTest,SendAndReceive_MotionClassification)166 TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
167 std::unique_ptr<InputChannel> serverChannel, clientChannel;
168 status_t result = InputChannel::openInputChannelPair("channel name",
169 serverChannel, clientChannel);
170 ASSERT_EQ(OK, result)
171 << "should have successfully opened a channel pair";
172
173 std::array<MotionClassification, 3> classifications = {
174 MotionClassification::NONE,
175 MotionClassification::AMBIGUOUS_GESTURE,
176 MotionClassification::DEEP_PRESS,
177 };
178
179 InputMessage serverMsg = {}, clientMsg;
180 serverMsg.header.type = InputMessage::Type::MOTION;
181 serverMsg.header.seq = 1;
182 serverMsg.body.motion.pointerCount = 1;
183
184 for (MotionClassification classification : classifications) {
185 // Send and receive a message with classification
186 serverMsg.body.motion.classification = classification;
187 EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
188 << "server channel should be able to send message to client channel";
189
190 EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
191 << "client channel should be able to receive message from server channel";
192 EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
193 EXPECT_EQ(classification, clientMsg.body.motion.classification) <<
194 "Expected to receive " << motionClassificationToString(classification);
195 }
196 }
197
TEST_F(InputChannelTest,InputChannelParcelAndUnparcel)198 TEST_F(InputChannelTest, InputChannelParcelAndUnparcel) {
199 std::unique_ptr<InputChannel> serverChannel, clientChannel;
200
201 status_t result =
202 InputChannel::openInputChannelPair("channel parceling", serverChannel, clientChannel);
203
204 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
205
206 InputChannel chan;
207 Parcel parcel;
208 ASSERT_EQ(OK, serverChannel->writeToParcel(&parcel));
209 parcel.setDataPosition(0);
210 chan.readFromParcel(&parcel);
211
212 EXPECT_EQ(chan == *serverChannel, true)
213 << "inputchannel should be equal after parceling and unparceling.\n"
214 << "name " << chan.getName() << " name " << serverChannel->getName();
215 }
216
TEST_F(InputChannelTest,DuplicateChannelAndAssertEqual)217 TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) {
218 std::unique_ptr<InputChannel> serverChannel, clientChannel;
219
220 status_t result =
221 InputChannel::openInputChannelPair("channel dup", serverChannel, clientChannel);
222
223 ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";
224
225 std::unique_ptr<InputChannel> dupChan = serverChannel->dup();
226
227 EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication";
228 }
229
230 } // namespace android
231