1 #include "uds/ipc_helper.h" 2 3 #include <gmock/gmock.h> 4 #include <gtest/gtest.h> 5 6 using testing::Return; 7 using testing::SetErrnoAndReturn; 8 using testing::_; 9 10 using android::pdx::BorrowedHandle; 11 using android::pdx::uds::SendInterface; 12 using android::pdx::uds::RecvInterface; 13 using android::pdx::uds::SendAll; 14 using android::pdx::uds::SendMsgAll; 15 using android::pdx::uds::RecvAll; 16 using android::pdx::uds::RecvMsgAll; 17 18 namespace { 19 20 // Useful constants for tests. 21 static constexpr intptr_t kPtr = 1234; 22 static constexpr int kSocketFd = 5678; 23 static const BorrowedHandle kSocket{kSocketFd}; 24 25 // Helper functions to construct test data pointer values. 26 void* IntToPtr(intptr_t value) { return reinterpret_cast<void*>(value); } 27 const void* IntToConstPtr(intptr_t value) { 28 return reinterpret_cast<const void*>(value); 29 } 30 31 // Mock classes for SendInterface/RecvInterface. 32 class MockSender : public SendInterface { 33 public: 34 MOCK_METHOD4(Send, ssize_t(int socket_fd, const void* data, size_t size, 35 int flags)); 36 MOCK_METHOD3(SendMessage, 37 ssize_t(int socket_fd, const msghdr* msg, int flags)); 38 }; 39 40 class MockReceiver : public RecvInterface { 41 public: 42 MOCK_METHOD4(Receive, 43 ssize_t(int socket_fd, void* data, size_t size, int flags)); 44 MOCK_METHOD3(ReceiveMessage, ssize_t(int socket_fd, msghdr* msg, int flags)); 45 }; 46 47 // Test case classes. 48 class SendTest : public testing::Test { 49 public: 50 SendTest() { 51 ON_CALL(sender_, Send(_, _, _, _)) 52 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 53 ON_CALL(sender_, SendMessage(_, _, _)) 54 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 55 } 56 57 protected: 58 MockSender sender_; 59 }; 60 61 class RecvTest : public testing::Test { 62 public: 63 RecvTest() { 64 ON_CALL(receiver_, Receive(_, _, _, _)) 65 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 66 ON_CALL(receiver_, ReceiveMessage(_, _, _)) 67 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 68 } 69 70 protected: 71 MockReceiver receiver_; 72 }; 73 74 class MessageTestBase : public testing::Test { 75 public: 76 MessageTestBase() { 77 memset(&msg_, 0, sizeof(msg_)); 78 msg_.msg_iovlen = data_.size(); 79 msg_.msg_iov = data_.data(); 80 } 81 82 protected: 83 static constexpr intptr_t kPtr1 = kPtr; 84 static constexpr intptr_t kPtr2 = kPtr + 200; 85 static constexpr intptr_t kPtr3 = kPtr + 1000; 86 87 MockSender sender_; 88 msghdr msg_; 89 std::vector<iovec> data_{ 90 {IntToPtr(kPtr1), 100}, {IntToPtr(kPtr2), 200}, {IntToPtr(kPtr3), 300}}; 91 }; 92 93 class SendMessageTest : public MessageTestBase { 94 public: 95 SendMessageTest() { 96 ON_CALL(sender_, Send(_, _, _, _)) 97 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 98 ON_CALL(sender_, SendMessage(_, _, _)) 99 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 100 } 101 102 protected: 103 MockSender sender_; 104 }; 105 106 class RecvMessageTest : public MessageTestBase { 107 public: 108 RecvMessageTest() { 109 ON_CALL(receiver_, Receive(_, _, _, _)) 110 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 111 ON_CALL(receiver_, ReceiveMessage(_, _, _)) 112 .WillByDefault(SetErrnoAndReturn(EIO, -1)); 113 } 114 115 protected: 116 MockReceiver receiver_; 117 }; 118 119 // Actual tests. 120 121 // SendAll 122 TEST_F(SendTest, Complete) { 123 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr), 100, MSG_NOSIGNAL)) 124 .WillOnce(Return(100)); 125 126 auto status = SendAll(&sender_, kSocket, IntToConstPtr(kPtr), 100); 127 EXPECT_TRUE(status); 128 } 129 130 TEST_F(SendTest, Signal) { 131 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr), 100, MSG_NOSIGNAL)) 132 .WillOnce(Return(20)); 133 EXPECT_CALL(sender_, 134 Send(kSocketFd, IntToConstPtr(kPtr + 20), 80, MSG_NOSIGNAL)) 135 .WillOnce(Return(40)); 136 EXPECT_CALL(sender_, 137 Send(kSocketFd, IntToConstPtr(kPtr + 60), 40, MSG_NOSIGNAL)) 138 .WillOnce(Return(40)); 139 140 auto status = SendAll(&sender_, kSocket, IntToConstPtr(kPtr), 100); 141 EXPECT_TRUE(status); 142 } 143 144 TEST_F(SendTest, Eintr) { 145 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr), 100, MSG_NOSIGNAL)) 146 .WillOnce(SetErrnoAndReturn(EINTR, -1)) 147 .WillOnce(Return(100)); 148 149 auto status = SendAll(&sender_, kSocket, IntToConstPtr(kPtr), 100); 150 EXPECT_TRUE(status); 151 } 152 153 TEST_F(SendTest, Error) { 154 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr), 100, MSG_NOSIGNAL)) 155 .WillOnce(SetErrnoAndReturn(EIO, -1)); 156 157 auto status = SendAll(&sender_, kSocket, IntToConstPtr(kPtr), 100); 158 ASSERT_FALSE(status); 159 EXPECT_EQ(EIO, status.error()); 160 } 161 162 TEST_F(SendTest, Error2) { 163 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr), 100, MSG_NOSIGNAL)) 164 .WillOnce(Return(50)); 165 EXPECT_CALL(sender_, 166 Send(kSocketFd, IntToConstPtr(kPtr + 50), 50, MSG_NOSIGNAL)) 167 .WillOnce(SetErrnoAndReturn(EIO, -1)); 168 169 auto status = SendAll(&sender_, kSocket, IntToConstPtr(kPtr), 100); 170 ASSERT_FALSE(status); 171 EXPECT_EQ(EIO, status.error()); 172 } 173 174 // RecvAll 175 TEST_F(RecvTest, Complete) { 176 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr), 100, 177 MSG_WAITALL | MSG_CMSG_CLOEXEC)) 178 .WillOnce(Return(100)); 179 180 auto status = RecvAll(&receiver_, kSocket, IntToPtr(kPtr), 100); 181 EXPECT_TRUE(status); 182 } 183 184 TEST_F(RecvTest, Signal) { 185 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr), 100, _)) 186 .WillOnce(Return(20)); 187 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr + 20), 80, _)) 188 .WillOnce(Return(40)); 189 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr + 60), 40, _)) 190 .WillOnce(Return(40)); 191 192 auto status = RecvAll(&receiver_, kSocket, IntToPtr(kPtr), 100); 193 EXPECT_TRUE(status); 194 } 195 196 TEST_F(RecvTest, Eintr) { 197 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr), 100, _)) 198 .WillOnce(SetErrnoAndReturn(EINTR, -1)) 199 .WillOnce(Return(100)); 200 201 auto status = RecvAll(&receiver_, kSocket, IntToPtr(kPtr), 100); 202 EXPECT_TRUE(status); 203 } 204 205 TEST_F(RecvTest, Error) { 206 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr), 100, _)) 207 .WillOnce(SetErrnoAndReturn(EIO, -1)); 208 209 auto status = RecvAll(&receiver_, kSocket, IntToPtr(kPtr), 100); 210 ASSERT_FALSE(status); 211 EXPECT_EQ(EIO, status.error()); 212 } 213 214 TEST_F(RecvTest, Error2) { 215 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr), 100, _)) 216 .WillOnce(Return(30)); 217 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr + 30), 70, _)) 218 .WillOnce(SetErrnoAndReturn(EIO, -1)); 219 220 auto status = RecvAll(&receiver_, kSocket, IntToPtr(kPtr), 100); 221 ASSERT_FALSE(status); 222 EXPECT_EQ(EIO, status.error()); 223 } 224 225 // SendMsgAll 226 TEST_F(SendMessageTest, Complete) { 227 EXPECT_CALL(sender_, SendMessage(kSocketFd, &msg_, MSG_NOSIGNAL)) 228 .WillOnce(Return(600)); 229 230 auto status = SendMsgAll(&sender_, kSocket, &msg_); 231 EXPECT_TRUE(status); 232 } 233 234 TEST_F(SendMessageTest, Partial) { 235 EXPECT_CALL(sender_, SendMessage(kSocketFd, &msg_, _)).WillOnce(Return(70)); 236 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr1 + 70), 30, _)) 237 .WillOnce(Return(30)); 238 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr2), 200, _)) 239 .WillOnce(Return(190)); 240 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr2 + 190), 10, _)) 241 .WillOnce(Return(10)); 242 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr3), 300, _)) 243 .WillOnce(Return(300)); 244 245 auto status = SendMsgAll(&sender_, kSocket, &msg_); 246 EXPECT_TRUE(status); 247 } 248 249 TEST_F(SendMessageTest, Partial2) { 250 EXPECT_CALL(sender_, SendMessage(kSocketFd, &msg_, _)).WillOnce(Return(310)); 251 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr3 + 10), 290, _)) 252 .WillOnce(Return(290)); 253 254 auto status = SendMsgAll(&sender_, kSocket, &msg_); 255 EXPECT_TRUE(status); 256 } 257 258 TEST_F(SendMessageTest, Eintr) { 259 EXPECT_CALL(sender_, SendMessage(kSocketFd, &msg_, _)) 260 .WillOnce(SetErrnoAndReturn(EINTR, -1)) 261 .WillOnce(Return(70)); 262 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr1 + 70), 30, _)) 263 .WillOnce(SetErrnoAndReturn(EINTR, -1)) 264 .WillOnce(Return(30)); 265 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr2), 200, _)) 266 .WillOnce(Return(200)); 267 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr3), 300, _)) 268 .WillOnce(Return(300)); 269 270 auto status = SendMsgAll(&sender_, kSocket, &msg_); 271 EXPECT_TRUE(status); 272 } 273 274 TEST_F(SendMessageTest, Error) { 275 EXPECT_CALL(sender_, SendMessage(kSocketFd, &msg_, _)) 276 .WillOnce(SetErrnoAndReturn(EBADF, -1)); 277 278 auto status = SendMsgAll(&sender_, kSocket, &msg_); 279 ASSERT_FALSE(status); 280 EXPECT_EQ(EBADF, status.error()); 281 } 282 283 TEST_F(SendMessageTest, Error2) { 284 EXPECT_CALL(sender_, SendMessage(kSocketFd, &msg_, _)).WillOnce(Return(20)); 285 EXPECT_CALL(sender_, Send(kSocketFd, IntToConstPtr(kPtr1 + 20), 80, _)) 286 .WillOnce(SetErrnoAndReturn(EBADF, -1)); 287 288 auto status = SendMsgAll(&sender_, kSocket, &msg_); 289 ASSERT_FALSE(status); 290 EXPECT_EQ(EBADF, status.error()); 291 } 292 293 // RecvMsgAll 294 TEST_F(RecvMessageTest, Complete) { 295 EXPECT_CALL(receiver_, 296 ReceiveMessage(kSocketFd, &msg_, MSG_WAITALL | MSG_CMSG_CLOEXEC)) 297 .WillOnce(Return(600)); 298 299 auto status = RecvMsgAll(&receiver_, kSocket, &msg_); 300 EXPECT_TRUE(status); 301 } 302 303 TEST_F(RecvMessageTest, Partial) { 304 EXPECT_CALL(receiver_, ReceiveMessage(kSocketFd, &msg_, _)) 305 .WillOnce(Return(70)); 306 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr1 + 70), 30, _)) 307 .WillOnce(Return(30)); 308 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr2), 200, _)) 309 .WillOnce(Return(190)); 310 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr2 + 190), 10, _)) 311 .WillOnce(Return(10)); 312 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr3), 300, _)) 313 .WillOnce(Return(300)); 314 315 auto status = RecvMsgAll(&receiver_, kSocket, &msg_); 316 EXPECT_TRUE(status); 317 } 318 319 TEST_F(RecvMessageTest, Partial2) { 320 EXPECT_CALL(receiver_, ReceiveMessage(kSocketFd, &msg_, _)) 321 .WillOnce(Return(310)); 322 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr3 + 10), 290, _)) 323 .WillOnce(Return(290)); 324 325 auto status = RecvMsgAll(&receiver_, kSocket, &msg_); 326 EXPECT_TRUE(status); 327 } 328 329 TEST_F(RecvMessageTest, Eintr) { 330 EXPECT_CALL(receiver_, ReceiveMessage(kSocketFd, &msg_, _)) 331 .WillOnce(SetErrnoAndReturn(EINTR, -1)) 332 .WillOnce(Return(70)); 333 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr1 + 70), 30, _)) 334 .WillOnce(SetErrnoAndReturn(EINTR, -1)) 335 .WillOnce(Return(30)); 336 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr2), 200, _)) 337 .WillOnce(Return(200)); 338 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr3), 300, _)) 339 .WillOnce(Return(300)); 340 341 auto status = RecvMsgAll(&receiver_, kSocket, &msg_); 342 EXPECT_TRUE(status); 343 } 344 345 TEST_F(RecvMessageTest, Error) { 346 EXPECT_CALL(receiver_, ReceiveMessage(kSocketFd, &msg_, _)) 347 .WillOnce(SetErrnoAndReturn(EBADF, -1)); 348 349 auto status = RecvMsgAll(&receiver_, kSocket, &msg_); 350 ASSERT_FALSE(status); 351 EXPECT_EQ(EBADF, status.error()); 352 } 353 354 TEST_F(RecvMessageTest, Error2) { 355 EXPECT_CALL(receiver_, ReceiveMessage(kSocketFd, &msg_, _)) 356 .WillOnce(Return(20)); 357 EXPECT_CALL(receiver_, Receive(kSocketFd, IntToPtr(kPtr1 + 20), 80, _)) 358 .WillOnce(SetErrnoAndReturn(EBADF, -1)); 359 360 auto status = RecvMsgAll(&receiver_, kSocket, &msg_); 361 ASSERT_FALSE(status); 362 EXPECT_EQ(EBADF, status.error()); 363 } 364 365 } // namespace 366