1 // Copyright 2015 The Weave 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 #ifndef LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_ 6 #define LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_ 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 16 #include "src/config.h" 17 #include "src/privet/cloud_delegate.h" 18 #include "src/privet/device_delegate.h" 19 #include "src/privet/security_delegate.h" 20 #include "src/privet/wifi_delegate.h" 21 22 using testing::_; 23 using testing::Return; 24 using testing::ReturnRef; 25 using testing::SetArgPointee; 26 27 namespace weave { 28 29 namespace privet { 30 31 struct TestUserId : public UserAppId { 32 TestUserId(const std::string& user_id) 33 : UserAppId{AuthType::kAnonymous, {user_id.begin(), user_id.end()}, {}} {} 34 }; 35 36 ACTION_TEMPLATE(RunCallback, 37 HAS_1_TEMPLATE_PARAMS(int, k), 38 AND_0_VALUE_PARAMS()) { 39 return std::get<k>(args).Run(); 40 } 41 42 ACTION_TEMPLATE(RunCallback, 43 HAS_1_TEMPLATE_PARAMS(int, k), 44 AND_1_VALUE_PARAMS(p0)) { 45 return std::get<k>(args).Run(p0); 46 } 47 48 class MockDeviceDelegate : public DeviceDelegate { 49 using IntPair = std::pair<uint16_t, uint16_t>; 50 51 public: 52 MOCK_CONST_METHOD0(GetHttpEnpoint, IntPair()); 53 MOCK_CONST_METHOD0(GetHttpsEnpoint, IntPair()); 54 MOCK_CONST_METHOD0(GetHttpRequestTimeout, base::TimeDelta()); 55 MOCK_METHOD3(PostDelayedTask, 56 void(const tracked_objects::Location&, 57 const base::Closure&, 58 base::TimeDelta)); 59 60 MockDeviceDelegate() { 61 EXPECT_CALL(*this, GetHttpEnpoint()) 62 .WillRepeatedly(Return(std::make_pair(0, 0))); 63 EXPECT_CALL(*this, GetHttpsEnpoint()) 64 .WillRepeatedly(Return(std::make_pair(0, 0))); 65 } 66 }; 67 68 class MockSecurityDelegate : public SecurityDelegate { 69 public: 70 MOCK_METHOD7(CreateAccessToken, 71 bool(AuthType, 72 const std::string&, 73 AuthScope, 74 std::string*, 75 AuthScope*, 76 base::TimeDelta*, 77 ErrorPtr*)); 78 MOCK_CONST_METHOD3(ParseAccessToken, 79 bool(const std::string&, UserInfo*, ErrorPtr*)); 80 MOCK_CONST_METHOD0(GetPairingTypes, std::set<PairingType>()); 81 MOCK_CONST_METHOD0(GetCryptoTypes, std::set<CryptoType>()); 82 MOCK_CONST_METHOD0(GetAuthTypes, std::set<AuthType>()); 83 MOCK_METHOD1(ClaimRootClientAuthToken, std::string(ErrorPtr*)); 84 MOCK_METHOD2(ConfirmClientAuthToken, bool(const std::string&, ErrorPtr*)); 85 MOCK_METHOD5( 86 StartPairing, 87 bool(PairingType, CryptoType, std::string*, std::string*, ErrorPtr*)); 88 MOCK_METHOD5(ConfirmPairing, 89 bool(const std::string&, 90 const std::string&, 91 std::string*, 92 std::string*, 93 ErrorPtr*)); 94 MOCK_METHOD2(CancelPairing, bool(const std::string&, ErrorPtr*)); 95 MOCK_METHOD0(CreateSessionId, std::string()); 96 97 MockSecurityDelegate() { 98 EXPECT_CALL(*this, CreateAccessToken(_, _, _, _, _, _, _)) 99 .WillRepeatedly(DoAll( 100 SetArgPointee<3>("GuestAccessToken"), 101 SetArgPointee<4>(AuthScope::kViewer), 102 SetArgPointee<5>(base::TimeDelta::FromSeconds(15)), Return(true))); 103 104 EXPECT_CALL(*this, ClaimRootClientAuthToken(_)) 105 .WillRepeatedly(Return("RootClientAuthToken")); 106 107 EXPECT_CALL(*this, ConfirmClientAuthToken("DerivedClientAuthToken", _)) 108 .WillRepeatedly(Return(true)); 109 110 EXPECT_CALL(*this, ParseAccessToken(_, _, _)) 111 .WillRepeatedly(DoAll(SetArgPointee<1>(UserInfo{ 112 AuthScope::kViewer, 113 UserAppId{AuthType::kLocal, 114 {'1', '2', '3', '4', '5', '6', '7'}, 115 {}}}), 116 Return(true))); 117 118 EXPECT_CALL(*this, GetPairingTypes()) 119 .WillRepeatedly(Return(std::set<PairingType>{ 120 PairingType::kPinCode, PairingType::kEmbeddedCode, 121 })); 122 123 EXPECT_CALL(*this, GetCryptoTypes()) 124 .WillRepeatedly(Return(std::set<CryptoType>{ 125 CryptoType::kSpake_p224, 126 })); 127 EXPECT_CALL(*this, GetAuthTypes()) 128 .WillRepeatedly(Return(std::set<AuthType>{ 129 AuthType::kAnonymous, AuthType::kPairing, AuthType::kLocal, 130 })); 131 132 EXPECT_CALL(*this, StartPairing(_, _, _, _, _)) 133 .WillRepeatedly(DoAll(SetArgPointee<2>("testSession"), 134 SetArgPointee<3>("testCommitment"), 135 Return(true))); 136 137 EXPECT_CALL(*this, ConfirmPairing(_, _, _, _, _)) 138 .WillRepeatedly(DoAll(SetArgPointee<2>("testFingerprint"), 139 SetArgPointee<3>("testSignature"), Return(true))); 140 EXPECT_CALL(*this, CancelPairing(_, _)).WillRepeatedly(Return(true)); 141 EXPECT_CALL(*this, CreateSessionId()).WillRepeatedly(Return("SessionId")); 142 } 143 }; 144 145 class MockWifiDelegate : public WifiDelegate { 146 public: 147 MOCK_CONST_METHOD0(GetConnectionState, const ConnectionState&()); 148 MOCK_CONST_METHOD0(GetSetupState, const SetupState&()); 149 MOCK_METHOD3(ConfigureCredentials, 150 bool(const std::string&, const std::string&, ErrorPtr*)); 151 MOCK_CONST_METHOD0(GetCurrentlyConnectedSsid, std::string()); 152 MOCK_CONST_METHOD0(GetHostedSsid, std::string()); 153 MOCK_CONST_METHOD0(GetTypes, std::set<WifiType>()); 154 155 MockWifiDelegate() { 156 EXPECT_CALL(*this, GetConnectionState()) 157 .WillRepeatedly(ReturnRef(connection_state_)); 158 EXPECT_CALL(*this, GetSetupState()).WillRepeatedly(ReturnRef(setup_state_)); 159 EXPECT_CALL(*this, GetCurrentlyConnectedSsid()) 160 .WillRepeatedly(Return("TestSsid")); 161 EXPECT_CALL(*this, GetHostedSsid()).WillRepeatedly(Return("")); 162 EXPECT_CALL(*this, GetTypes()) 163 .WillRepeatedly(Return(std::set<WifiType>{WifiType::kWifi24})); 164 } 165 166 ConnectionState connection_state_{ConnectionState::kOffline}; 167 SetupState setup_state_{SetupState::kNone}; 168 }; 169 170 class MockCloudDelegate : public CloudDelegate { 171 public: 172 MOCK_CONST_METHOD0(GetDeviceId, std::string()); 173 MOCK_CONST_METHOD0(GetModelId, std::string()); 174 MOCK_CONST_METHOD0(GetName, std::string()); 175 MOCK_CONST_METHOD0(GetDescription, std::string()); 176 MOCK_CONST_METHOD0(GetLocation, std::string()); 177 MOCK_METHOD3(UpdateDeviceInfo, 178 void(const std::string&, 179 const std::string&, 180 const std::string&)); 181 MOCK_CONST_METHOD0(GetOemName, std::string()); 182 MOCK_CONST_METHOD0(GetModelName, std::string()); 183 MOCK_CONST_METHOD0(GetAnonymousMaxScope, AuthScope()); 184 MOCK_CONST_METHOD0(GetConnectionState, const ConnectionState&()); 185 MOCK_CONST_METHOD0(GetSetupState, const SetupState&()); 186 MOCK_METHOD3(Setup, bool(const std::string&, const std::string&, ErrorPtr*)); 187 MOCK_CONST_METHOD0(GetCloudId, std::string()); 188 MOCK_CONST_METHOD0(GetLegacyState, const base::DictionaryValue&()); 189 MOCK_CONST_METHOD0(GetLegacyCommandDef, const base::DictionaryValue&()); 190 MOCK_CONST_METHOD0(GetComponents, const base::DictionaryValue&()); 191 MOCK_CONST_METHOD2(FindComponent, 192 const base::DictionaryValue*(const std::string& path, 193 ErrorPtr* error)); 194 MOCK_CONST_METHOD0(GetTraits, const base::DictionaryValue&()); 195 MOCK_METHOD3(AddCommand, 196 void(const base::DictionaryValue&, 197 const UserInfo&, 198 const CommandDoneCallback&)); 199 MOCK_METHOD3(GetCommand, 200 void(const std::string&, 201 const UserInfo&, 202 const CommandDoneCallback&)); 203 MOCK_METHOD3(CancelCommand, 204 void(const std::string&, 205 const UserInfo&, 206 const CommandDoneCallback&)); 207 MOCK_METHOD2(ListCommands, void(const UserInfo&, const CommandDoneCallback&)); 208 209 MockCloudDelegate() { 210 EXPECT_CALL(*this, GetDeviceId()).WillRepeatedly(Return("TestId")); 211 EXPECT_CALL(*this, GetModelId()).WillRepeatedly(Return("ABMID")); 212 EXPECT_CALL(*this, GetName()).WillRepeatedly(Return("TestDevice")); 213 EXPECT_CALL(*this, GetDescription()).WillRepeatedly(Return("")); 214 EXPECT_CALL(*this, GetLocation()).WillRepeatedly(Return("")); 215 EXPECT_CALL(*this, UpdateDeviceInfo(_, _, _)).WillRepeatedly(Return()); 216 EXPECT_CALL(*this, GetOemName()).WillRepeatedly(Return("Chromium")); 217 EXPECT_CALL(*this, GetModelName()).WillRepeatedly(Return("Brillo")); 218 EXPECT_CALL(*this, GetAnonymousMaxScope()) 219 .WillRepeatedly(Return(AuthScope::kUser)); 220 EXPECT_CALL(*this, GetConnectionState()) 221 .WillRepeatedly(ReturnRef(connection_state_)); 222 EXPECT_CALL(*this, GetSetupState()).WillRepeatedly(ReturnRef(setup_state_)); 223 EXPECT_CALL(*this, GetCloudId()).WillRepeatedly(Return("TestCloudId")); 224 test_dict_.Set("test", new base::DictionaryValue); 225 EXPECT_CALL(*this, GetLegacyState()).WillRepeatedly(ReturnRef(test_dict_)); 226 EXPECT_CALL(*this, GetLegacyCommandDef()) 227 .WillRepeatedly(ReturnRef(test_dict_)); 228 EXPECT_CALL(*this, GetTraits()).WillRepeatedly(ReturnRef(test_dict_)); 229 EXPECT_CALL(*this, GetComponents()).WillRepeatedly(ReturnRef(test_dict_)); 230 EXPECT_CALL(*this, FindComponent(_, _)).Times(0); 231 } 232 233 ConnectionState connection_state_{ConnectionState::kOnline}; 234 SetupState setup_state_{SetupState::kNone}; 235 base::DictionaryValue test_dict_; 236 }; 237 238 } // namespace privet 239 } // namespace weave 240 241 #endif // LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_ 242