File: stub_binder_wrapper.h

package info (click to toggle)
android-platform-system-core 1%3A8.1.0%2Br23-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 19,480 kB
  • sloc: cpp: 137,841; ansic: 30,813; asm: 3,504; python: 1,754; makefile: 235; sh: 225; xml: 73
file content (133 lines) | stat: -rw-r--r-- 4,638 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_
#define SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_

#include <map>
#include <string>
#include <vector>

#include <base/macros.h>
#include <binder/Binder.h>
#include <binder/IBinder.h>
#include <binderwrapper/binder_wrapper.h>

namespace android {

// Stub implementation of BinderWrapper for testing.
//
// Example usage:
//
// First, assuming a base IFoo binder interface, create a stub class that
// derives from BnFoo to implement the receiver side of the communication:
//
//   class StubFoo : public BnFoo {
//    public:
//     ...
//     status_t doSomething(int arg) override {
//       // e.g. save passed-in value for later inspection by tests.
//       return OK;
//     }
//   };
//
// Next, from your test code, inject a StubBinderManager either directly or by
// inheriting from the BinderTestBase class:
//
//   StubBinderWrapper* wrapper = new StubBinderWrapper();
//   BinderWrapper::InitForTesting(wrapper);  // Takes ownership.
//
// Also from your test, create a StubFoo and register it with the wrapper:
//
//   StubFoo* foo = new StubFoo();
//   sp<IBinder> binder(foo);
//   wrapper->SetBinderForService("foo", binder);
//
// The code being tested can now use the wrapper to get the stub and call it:
//
//   sp<IBinder> binder = BinderWrapper::Get()->GetService("foo");
//   CHECK(binder.get());
//   sp<IFoo> foo = interface_cast<IFoo>(binder);
//   CHECK_EQ(foo->doSomething(3), OK);
//
// To create a local BBinder object, production code can call
// CreateLocalBinder(). Then, a test can get the BBinder's address via
// local_binders() to check that they're passed as expected in binder calls.
//
class StubBinderWrapper : public BinderWrapper {
 public:
  StubBinderWrapper();
  ~StubBinderWrapper() override;

  const std::vector<sp<BBinder>>& local_binders() const {
    return local_binders_;
  }
  void clear_local_binders() { local_binders_.clear(); }

  void set_calling_uid(uid_t uid) { calling_uid_ = uid; }
  void set_calling_pid(pid_t pid) { calling_pid_ = pid; }

  // Sets the binder to return when |service_name| is passed to GetService() or
  // WaitForService().
  void SetBinderForService(const std::string& service_name,
                           const sp<IBinder>& binder);

  // Returns the binder previously registered for |service_name| via
  // RegisterService(), or null if the service hasn't been registered.
  sp<IBinder> GetRegisteredService(const std::string& service_name) const;

  // Run the calback in |death_callbacks_| corresponding to |binder|.
  void NotifyAboutBinderDeath(const sp<IBinder>& binder);

  // BinderWrapper:
  sp<IBinder> GetService(const std::string& service_name) override;
  bool RegisterService(const std::string& service_name,
                       const sp<IBinder>& binder) override;
  sp<BBinder> CreateLocalBinder() override;
  bool RegisterForDeathNotifications(const sp<IBinder>& binder,
                                     const ::base::Closure& callback) override;
  bool UnregisterForDeathNotifications(const sp<IBinder>& binder) override;
  uid_t GetCallingUid() override;
  pid_t GetCallingPid() override;

 private:
  using ServiceMap = std::map<std::string, sp<IBinder>>;

  // Map from service name to associated binder handle. Used by GetService() and
  // WaitForService().
  ServiceMap services_to_return_;

  // Map from service name to associated binder handle. Updated by
  // RegisterService().
  ServiceMap registered_services_;

  // Local binders returned by CreateLocalBinder().
  std::vector<sp<BBinder>> local_binders_;

  // Map from binder handle to the callback that should be invoked on binder
  // death.
  std::map<sp<IBinder>, ::base::Closure> death_callbacks_;

  // Values to return from GetCallingUid() and GetCallingPid();
  uid_t calling_uid_;
  pid_t calling_pid_;

  DISALLOW_COPY_AND_ASSIGN(StubBinderWrapper);
};

}  // namespace android

#endif  // SYSTEM_CORE_INCLUDE_BINDERWRAPPER_STUB_BINDER_WRAPPER_H_