blob: 9c9aa3002cfa07b7a8eadd282861a9eaa5bfcf64 [file] [log] [blame]
hashimoto9aaf6362015-12-15 06:48:121// 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
hashimoto8079ea62015-12-16 05:40:395#include "base/message_loop/message_loop.h"
hashimotoc5ffb8b2016-03-09 03:19:316#include "chromeos/binder/binder_driver_api.h"
hashimoto9aaf6362015-12-15 06:48:127#include "chromeos/binder/command_stream.h"
hashimoto8079ea62015-12-16 05:40:398#include "chromeos/binder/constants.h"
hashimoto9aaf6362015-12-15 06:48:129#include "chromeos/binder/driver.h"
hashimoto8079ea62015-12-16 05:40:3910#include "chromeos/binder/transaction_data.h"
hashimoto9aaf6362015-12-15 06:48:1211#include "testing/gtest/include/gtest/gtest.h"
12
13namespace binder {
14
15namespace {
16
hashimoto9aaf6362015-12-15 06:48:1217class 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
hashimoto8079ea62015-12-16 05:40:3925 // CommandStream::IncomingCommandHandler override:
hashimotobb42dee2016-01-14 10:13:5626 bool OnTransaction(const TransactionData& data) override { return false; }
27
dcheng0a6e80c2016-04-08 18:37:3828 void OnReply(std::unique_ptr<TransactionData> data) override {
hashimoto8079ea62015-12-16 05:40:3929 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
hashimoto7c8426e22016-01-12 05:58:5440 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
hashimoto8079ea62015-12-16 05:40:3948 void OnFailedReply() override { received_response_ = RESPONSE_FAILED; }
49
hashimoto9aaf6362015-12-15 06:48:1250 protected:
hashimoto8079ea62015-12-16 05:40:3951 base::MessageLoop message_loop_;
hashimoto9aaf6362015-12-15 06:48:1252 Driver driver_;
53 CommandStream command_stream_;
hashimoto8079ea62015-12-16 05:40:3954
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;
hashimoto9aaf6362015-12-15 06:48:1263};
64
65} // namespace
66
67TEST_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
74TEST_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, &params,
78 sizeof(params));
79 EXPECT_FALSE(command_stream_.Flush());
80}
81
hashimoto8079ea62015-12-16 05:40:3982TEST_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
hashimoto9aaf6362015-12-15 06:48:12112} // namespace binder