blob: d0a4a5f61f351b2e8e081d876efb006618818ec4 [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
5#include <unistd.h>
6#include <linux/android/binder.h>
7
hashimoto8079ea62015-12-16 05:40:398#include "base/message_loop/message_loop.h"
hashimoto9aaf6362015-12-15 06:48:129#include "chromeos/binder/command_stream.h"
hashimoto8079ea62015-12-16 05:40:3910#include "chromeos/binder/constants.h"
hashimoto9aaf6362015-12-15 06:48:1211#include "chromeos/binder/driver.h"
hashimoto8079ea62015-12-16 05:40:3912#include "chromeos/binder/transaction_data.h"
hashimoto9aaf6362015-12-15 06:48:1213#include "testing/gtest/include/gtest/gtest.h"
14
15namespace binder {
16
17namespace {
18
hashimoto9aaf6362015-12-15 06:48:1219class BinderCommandStreamTest : public ::testing::Test,
20 public CommandStream::IncomingCommandHandler {
21 public:
22 BinderCommandStreamTest() : command_stream_(&driver_, this) {}
23 ~BinderCommandStreamTest() override {}
24
25 void SetUp() override { ASSERT_TRUE(driver_.Initialize()); }
26
hashimoto8079ea62015-12-16 05:40:3927 // CommandStream::IncomingCommandHandler override:
28 void OnReply(scoped_ptr<TransactionData> data) override {
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
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