hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 1 | // Copyright 2015 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 | |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 5 | #include "base/message_loop/message_loop.h" |
hashimoto | c5ffb8b | 2016-03-09 03:19:31 | [diff] [blame] | 6 | #include "chromeos/binder/binder_driver_api.h" |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 7 | #include "chromeos/binder/command_stream.h" |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 8 | #include "chromeos/binder/constants.h" |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 9 | #include "chromeos/binder/driver.h" |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 10 | #include "chromeos/binder/transaction_data.h" |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace binder { |
| 14 | |
| 15 | namespace { |
| 16 | |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 17 | class BinderCommandStreamTest : public ::testing::Test, |
| 18 | public CommandStream::IncomingCommandHandler { |
| 19 | public: |
| 20 | BinderCommandStreamTest() : command_stream_(&driver_, this) {} |
| 21 | ~BinderCommandStreamTest() override {} |
| 22 | |
| 23 | void SetUp() override { ASSERT_TRUE(driver_.Initialize()); } |
| 24 | |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 25 | // CommandStream::IncomingCommandHandler override: |
hashimoto | bb42dee | 2016-01-14 10:13:56 | [diff] [blame] | 26 | bool OnTransaction(const TransactionData& data) override { return false; } |
| 27 | |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame] | 28 | void OnReply(std::unique_ptr<TransactionData> data) override { |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 29 | received_response_ = RESPONSE_REPLY; |
| 30 | ASSERT_TRUE(data); |
| 31 | EXPECT_FALSE(data->HasStatus()); // No error. |
| 32 | } |
| 33 | |
| 34 | void OnDeadReply() override { received_response_ = RESPONSE_DEAD; } |
| 35 | |
| 36 | void OnTransactionComplete() override { |
| 37 | received_response_ = RESPONSE_COMPLETE; |
| 38 | } |
| 39 | |
hashimoto | 7c8426e2 | 2016-01-12 05:58:54 | [diff] [blame] | 40 | void OnIncrementWeakReference(void* ptr, void* cookie) override {} |
| 41 | |
| 42 | void OnIncrementStrongReference(void* ptr, void* cookie) override {} |
| 43 | |
| 44 | void OnDecrementStrongReference(void* ptr, void* cookie) override {} |
| 45 | |
| 46 | void OnDecrementWeakReference(void* ptr, void* cookie) override {} |
| 47 | |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 48 | void OnFailedReply() override { received_response_ = RESPONSE_FAILED; } |
| 49 | |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 50 | protected: |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 51 | base::MessageLoop message_loop_; |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 52 | Driver driver_; |
| 53 | CommandStream command_stream_; |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 54 | |
| 55 | enum ResponseType { |
| 56 | RESPONSE_NONE, |
| 57 | RESPONSE_REPLY, |
| 58 | RESPONSE_DEAD, |
| 59 | RESPONSE_COMPLETE, |
| 60 | RESPONSE_FAILED, |
| 61 | }; |
| 62 | ResponseType received_response_ = RESPONSE_NONE; |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | TEST_F(BinderCommandStreamTest, EnterLooper) { |
| 68 | command_stream_.AppendOutgoingCommand(BC_ENTER_LOOPER, nullptr, 0); |
| 69 | EXPECT_TRUE(command_stream_.Flush()); |
| 70 | command_stream_.AppendOutgoingCommand(BC_EXIT_LOOPER, nullptr, 0); |
| 71 | EXPECT_TRUE(command_stream_.Flush()); |
| 72 | } |
| 73 | |
| 74 | TEST_F(BinderCommandStreamTest, Error) { |
| 75 | // Kernel's binder.h says BC_ATTEMPT_ACQUIRE is not currently supported. |
| 76 | binder_pri_desc params = {}; |
| 77 | command_stream_.AppendOutgoingCommand(BC_ATTEMPT_ACQUIRE, ¶ms, |
| 78 | sizeof(params)); |
| 79 | EXPECT_FALSE(command_stream_.Flush()); |
| 80 | } |
| 81 | |
hashimoto | 8079ea6 | 2015-12-16 05:40:39 | [diff] [blame] | 82 | TEST_F(BinderCommandStreamTest, PingContextManager) { |
| 83 | // Perform ping transaction with the context manager. |
| 84 | binder_transaction_data data = {}; |
| 85 | data.target.handle = kContextManagerHandle; |
| 86 | data.code = kPingTransactionCode; |
| 87 | command_stream_.AppendOutgoingCommand(BC_TRANSACTION, &data, sizeof(data)); |
| 88 | ASSERT_TRUE(command_stream_.Flush()); |
| 89 | |
| 90 | // Wait for transaction complete. |
| 91 | while (received_response_ == RESPONSE_NONE) { |
| 92 | if (command_stream_.CanProcessIncomingCommand()) { |
| 93 | ASSERT_TRUE(command_stream_.ProcessIncomingCommand()); |
| 94 | } else { |
| 95 | ASSERT_TRUE(command_stream_.Fetch()); |
| 96 | } |
| 97 | } |
| 98 | ASSERT_EQ(RESPONSE_COMPLETE, received_response_); |
| 99 | |
| 100 | // Wait for transaction reply. |
| 101 | received_response_ = RESPONSE_NONE; |
| 102 | while (received_response_ == RESPONSE_NONE) { |
| 103 | if (command_stream_.CanProcessIncomingCommand()) { |
| 104 | ASSERT_TRUE(command_stream_.ProcessIncomingCommand()); |
| 105 | } else { |
| 106 | ASSERT_TRUE(command_stream_.Fetch()); |
| 107 | } |
| 108 | } |
| 109 | ASSERT_EQ(RESPONSE_REPLY, received_response_); |
| 110 | } |
| 111 | |
hashimoto | 9aaf636 | 2015-12-15 06:48:12 | [diff] [blame] | 112 | } // namespace binder |