-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfake_libusb_wrapper.h
More file actions
90 lines (73 loc) · 3.3 KB
/
fake_libusb_wrapper.h
File metadata and controls
90 lines (73 loc) · 3.3 KB
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
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PERMISSION_BROKER_FAKE_LIBUSB_WRAPPER_H_
#define PERMISSION_BROKER_FAKE_LIBUSB_WRAPPER_H_
#include "permission_broker/libusb_wrapper.h"
#include <memory>
#include <vector>
#include <base/macros.h>
#include <gmock/gmock.h>
namespace permission_broker {
// Fake implementation of the UsbDeviceInterface used for testing. This class
// implements all the required APIs but returns variables and state that is
// decided through the constructor.
class FakeUsbDevice : public UsbDeviceInterface {
public:
// Container struct used to pass state information to the FakeUsbDevice.
// Mostly used to configure whether to fail or succeed SetPowerState() and to
// return recorded internal state to be later checked.
struct State {
const bool fail_power_off;
const bool fail_power_on;
// |power_off_counter| is used in tests to track whether the
// SetPowerState function was called to turn off a device.
int power_off_counter;
// |power_on_counter| is used in tests to track whether the
// SetPowerState function was called to turn on a device.
int power_on_counter;
State(bool fail_power_off, bool fail_power_on)
: fail_power_off(fail_power_off),
fail_power_on(fail_power_on),
power_off_counter(0),
power_on_counter(0) {}
};
// This class considers an UsbDeviceInfo to be valid only when both VID and
// PID are non-zero. This means that when |parent_info| is invalid, the device
// is lacking the parent. Ownership of |state| remains with the caller.
FakeUsbDevice(const UsbDeviceInfo& info,
const UsbDeviceInfo& parent_info,
State* state);
~FakeUsbDevice() override;
UsbDeviceInfo GetInfo() const override;
uint8_t GetPort() const override;
// This function returns a nullptr the parent's UsbDeviceInfo is invalid.
// Otherwise, the function returns a new FakeUsbDevice parent.
std::unique_ptr<UsbDeviceInterface> GetParent() const override;
bool SetPowerState(bool enabled, uint16_t port) const override;
private:
const UsbDeviceInfo info_;
// |parent_info| is considered 'invalid' when both VID and PID are set to 0.
const UsbDeviceInfo parent_info_;
// |state_| is a pointer which is owned by the caller of the constructor of
// this class. |state_| is meant as a way to communicate back the collected
// state of a UsbDeviceInterface object in tests.
State* state_;
DISALLOW_COPY_AND_ASSIGN(FakeUsbDevice);
};
// Fake implementation of the UsbDeviceManagerInterface used for testing. This
// class implements all the required APIs but returns variables and state that
// is decided through the constructor.
class FakeUsbDeviceManager : public UsbDeviceManagerInterface {
public:
FakeUsbDeviceManager(
std::vector<std::unique_ptr<UsbDeviceInterface>> devices);
~FakeUsbDeviceManager() override;
std::vector<std::unique_ptr<UsbDeviceInterface>> GetDevicesByVidPid(
uint16_t vid, uint16_t pid) override;
private:
std::vector<std::unique_ptr<UsbDeviceInterface>> devices_;
DISALLOW_COPY_AND_ASSIGN(FakeUsbDeviceManager);
};
} // namespace permission_broker
#endif // PERMISSION_BROKER_FAKE_LIBUSB_WRAPPER_H_