Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_ |
| 6 | #define LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_ |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <base/values.h> |
| 13 | #include <gmock/gmock.h> |
| 14 | #include <gtest/gtest.h> |
| 15 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 16 | #include "src/privet/cloud_delegate.h" |
| 17 | #include "src/privet/device_delegate.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 18 | #include "src/privet/security_delegate.h" |
| 19 | #include "src/privet/wifi_delegate.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 20 | |
| 21 | using testing::_; |
| 22 | using testing::Return; |
| 23 | using testing::ReturnRef; |
| 24 | using testing::SetArgPointee; |
| 25 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 26 | namespace weave { |
| 27 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 28 | |
| 29 | ACTION_TEMPLATE(RunCallback, |
| 30 | HAS_1_TEMPLATE_PARAMS(int, k), |
| 31 | AND_0_VALUE_PARAMS()) { |
| 32 | return std::get<k>(args).Run(); |
| 33 | } |
| 34 | |
| 35 | ACTION_TEMPLATE(RunCallback, |
| 36 | HAS_1_TEMPLATE_PARAMS(int, k), |
| 37 | AND_1_VALUE_PARAMS(p0)) { |
| 38 | return std::get<k>(args).Run(p0); |
| 39 | } |
| 40 | |
| 41 | class MockDeviceDelegate : public DeviceDelegate { |
| 42 | using IntPair = std::pair<uint16_t, uint16_t>; |
| 43 | |
| 44 | public: |
| 45 | MOCK_CONST_METHOD0(GetHttpEnpoint, IntPair()); |
| 46 | MOCK_CONST_METHOD0(GetHttpsEnpoint, IntPair()); |
| 47 | MOCK_CONST_METHOD0(GetUptime, base::TimeDelta()); |
| 48 | MOCK_METHOD1(SetHttpPort, void(uint16_t)); |
| 49 | MOCK_METHOD1(SetHttpsPort, void(uint16_t)); |
| 50 | |
| 51 | MockDeviceDelegate() { |
| 52 | EXPECT_CALL(*this, GetHttpEnpoint()) |
| 53 | .WillRepeatedly(Return(std::make_pair(0, 0))); |
| 54 | EXPECT_CALL(*this, GetHttpsEnpoint()) |
| 55 | .WillRepeatedly(Return(std::make_pair(0, 0))); |
| 56 | EXPECT_CALL(*this, GetUptime()) |
| 57 | .WillRepeatedly(Return(base::TimeDelta::FromHours(1))); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class MockSecurityDelegate : public SecurityDelegate { |
| 62 | public: |
| 63 | MOCK_METHOD2(CreateAccessToken, |
| 64 | std::string(const UserInfo&, const base::Time&)); |
| 65 | MOCK_CONST_METHOD2(ParseAccessToken, |
| 66 | UserInfo(const std::string&, base::Time*)); |
| 67 | MOCK_CONST_METHOD0(GetPairingTypes, std::set<PairingType>()); |
| 68 | MOCK_CONST_METHOD0(GetCryptoTypes, std::set<CryptoType>()); |
| 69 | MOCK_CONST_METHOD1(IsValidPairingCode, bool(const std::string&)); |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 70 | MOCK_METHOD5( |
| 71 | StartPairing, |
| 72 | bool(PairingType, CryptoType, std::string*, std::string*, ErrorPtr*)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 73 | MOCK_METHOD5(ConfirmPairing, |
| 74 | bool(const std::string&, |
| 75 | const std::string&, |
| 76 | std::string*, |
| 77 | std::string*, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 78 | ErrorPtr*)); |
| 79 | MOCK_METHOD2(CancelPairing, bool(const std::string&, ErrorPtr*)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 80 | |
| 81 | MockSecurityDelegate() { |
| 82 | EXPECT_CALL(*this, CreateAccessToken(_, _)) |
| 83 | .WillRepeatedly(Return("GuestAccessToken")); |
| 84 | |
| 85 | EXPECT_CALL(*this, ParseAccessToken(_, _)) |
| 86 | .WillRepeatedly(DoAll(SetArgPointee<1>(base::Time::Now()), |
| 87 | Return(UserInfo{AuthScope::kViewer, 1234567}))); |
| 88 | |
| 89 | EXPECT_CALL(*this, GetPairingTypes()) |
| 90 | .WillRepeatedly(Return(std::set<PairingType>{ |
Vitaly Buka | 4ebd329 | 2015-09-23 18:04:17 -0700 | [diff] [blame] | 91 | PairingType::kPinCode, PairingType::kEmbeddedCode, |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 92 | })); |
| 93 | |
| 94 | EXPECT_CALL(*this, GetCryptoTypes()) |
| 95 | .WillRepeatedly(Return(std::set<CryptoType>{ |
Vitaly Buka | c8ba228 | 2015-10-01 17:42:40 -0700 | [diff] [blame] | 96 | CryptoType::kSpake_p224, |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 97 | })); |
| 98 | |
| 99 | EXPECT_CALL(*this, StartPairing(_, _, _, _, _)) |
| 100 | .WillRepeatedly(DoAll(SetArgPointee<2>("testSession"), |
| 101 | SetArgPointee<3>("testCommitment"), |
| 102 | Return(true))); |
| 103 | |
| 104 | EXPECT_CALL(*this, ConfirmPairing(_, _, _, _, _)) |
| 105 | .WillRepeatedly(DoAll(SetArgPointee<2>("testFingerprint"), |
| 106 | SetArgPointee<3>("testSignature"), Return(true))); |
| 107 | EXPECT_CALL(*this, CancelPairing(_, _)).WillRepeatedly(Return(true)); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | class MockWifiDelegate : public WifiDelegate { |
| 112 | public: |
| 113 | MOCK_CONST_METHOD0(GetConnectionState, const ConnectionState&()); |
| 114 | MOCK_CONST_METHOD0(GetSetupState, const SetupState&()); |
| 115 | MOCK_METHOD3(ConfigureCredentials, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 116 | bool(const std::string&, const std::string&, ErrorPtr*)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 117 | MOCK_CONST_METHOD0(GetCurrentlyConnectedSsid, std::string()); |
| 118 | MOCK_CONST_METHOD0(GetHostedSsid, std::string()); |
| 119 | MOCK_CONST_METHOD0(GetTypes, std::set<WifiType>()); |
| 120 | |
| 121 | MockWifiDelegate() { |
| 122 | EXPECT_CALL(*this, GetConnectionState()) |
| 123 | .WillRepeatedly(ReturnRef(connection_state_)); |
| 124 | EXPECT_CALL(*this, GetSetupState()).WillRepeatedly(ReturnRef(setup_state_)); |
| 125 | EXPECT_CALL(*this, GetCurrentlyConnectedSsid()) |
| 126 | .WillRepeatedly(Return("TestSsid")); |
| 127 | EXPECT_CALL(*this, GetHostedSsid()).WillRepeatedly(Return("")); |
| 128 | EXPECT_CALL(*this, GetTypes()) |
| 129 | .WillRepeatedly(Return(std::set<WifiType>{WifiType::kWifi24})); |
| 130 | } |
| 131 | |
| 132 | ConnectionState connection_state_{ConnectionState::kOffline}; |
| 133 | SetupState setup_state_{SetupState::kNone}; |
| 134 | }; |
| 135 | |
| 136 | class MockCloudDelegate : public CloudDelegate { |
| 137 | public: |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 138 | MOCK_CONST_METHOD0(GetDeviceId, std::string()); |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 139 | MOCK_CONST_METHOD0(GetModelId, std::string()); |
| 140 | MOCK_CONST_METHOD0(GetName, std::string()); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 141 | MOCK_CONST_METHOD0(GetDescription, std::string()); |
| 142 | MOCK_CONST_METHOD0(GetLocation, std::string()); |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 143 | MOCK_METHOD3(UpdateDeviceInfo, |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 144 | void(const std::string&, |
| 145 | const std::string&, |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 146 | const std::string&)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 147 | MOCK_CONST_METHOD0(GetOemName, std::string()); |
| 148 | MOCK_CONST_METHOD0(GetModelName, std::string()); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 149 | MOCK_CONST_METHOD0(GetAnonymousMaxScope, AuthScope()); |
| 150 | MOCK_CONST_METHOD0(GetConnectionState, const ConnectionState&()); |
| 151 | MOCK_CONST_METHOD0(GetSetupState, const SetupState&()); |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 152 | MOCK_METHOD3(Setup, bool(const std::string&, const std::string&, ErrorPtr*)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 153 | MOCK_CONST_METHOD0(GetCloudId, std::string()); |
| 154 | MOCK_CONST_METHOD0(GetState, const base::DictionaryValue&()); |
| 155 | MOCK_CONST_METHOD0(GetCommandDef, const base::DictionaryValue&()); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 156 | MOCK_METHOD3(AddCommand, |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 157 | void(const base::DictionaryValue&, |
| 158 | const UserInfo&, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 159 | const CommandDoneCallback&)); |
| 160 | MOCK_METHOD3(GetCommand, |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 161 | void(const std::string&, |
| 162 | const UserInfo&, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 163 | const CommandDoneCallback&)); |
| 164 | MOCK_METHOD3(CancelCommand, |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 165 | void(const std::string&, |
| 166 | const UserInfo&, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 167 | const CommandDoneCallback&)); |
| 168 | MOCK_METHOD2(ListCommands, void(const UserInfo&, const CommandDoneCallback&)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 169 | |
| 170 | MockCloudDelegate() { |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 171 | EXPECT_CALL(*this, GetDeviceId()).WillRepeatedly(Return("TestId")); |
Vitaly Buka | 658aa36 | 2015-09-15 20:59:12 -0700 | [diff] [blame] | 172 | EXPECT_CALL(*this, GetModelId()).WillRepeatedly(Return("ABMID")); |
| 173 | EXPECT_CALL(*this, GetName()).WillRepeatedly(Return("TestDevice")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 174 | EXPECT_CALL(*this, GetDescription()).WillRepeatedly(Return("")); |
| 175 | EXPECT_CALL(*this, GetLocation()).WillRepeatedly(Return("")); |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 176 | EXPECT_CALL(*this, UpdateDeviceInfo(_, _, _)).WillRepeatedly(Return()); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 177 | EXPECT_CALL(*this, GetOemName()).WillRepeatedly(Return("Chromium")); |
| 178 | EXPECT_CALL(*this, GetModelName()).WillRepeatedly(Return("Brillo")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 179 | EXPECT_CALL(*this, GetAnonymousMaxScope()) |
| 180 | .WillRepeatedly(Return(AuthScope::kUser)); |
| 181 | EXPECT_CALL(*this, GetConnectionState()) |
| 182 | .WillRepeatedly(ReturnRef(connection_state_)); |
| 183 | EXPECT_CALL(*this, GetSetupState()).WillRepeatedly(ReturnRef(setup_state_)); |
| 184 | EXPECT_CALL(*this, GetCloudId()).WillRepeatedly(Return("TestCloudId")); |
| 185 | test_dict_.Set("test", new base::DictionaryValue); |
| 186 | EXPECT_CALL(*this, GetCommandDef()).WillRepeatedly(ReturnRef(test_dict_)); |
| 187 | EXPECT_CALL(*this, GetState()).WillRepeatedly(ReturnRef(test_dict_)); |
| 188 | } |
| 189 | |
| 190 | ConnectionState connection_state_{ConnectionState::kOnline}; |
| 191 | SetupState setup_state_{SetupState::kNone}; |
| 192 | base::DictionaryValue test_dict_; |
| 193 | }; |
| 194 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 195 | } // namespace privet |
| 196 | } // namespace weave |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 197 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 198 | #endif // LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_ |