blob: 755110e06391d782f6d0e0ff5e5dae4ddb429e39 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Vitaly Buka7ce499f2015-06-09 08:04:11 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_
6#define LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_
Vitaly Buka7ce499f2015-06-09 08:04:11 -07007
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 Sauer2d16dfa2015-09-25 17:08:35 +020016#include "src/privet/cloud_delegate.h"
17#include "src/privet/device_delegate.h"
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020018#include "src/privet/security_delegate.h"
19#include "src/privet/wifi_delegate.h"
Vitaly Buka7ce499f2015-06-09 08:04:11 -070020
21using testing::_;
22using testing::Return;
23using testing::ReturnRef;
24using testing::SetArgPointee;
25
Vitaly Bukab6f015a2015-07-09 14:59:23 -070026namespace weave {
27namespace privet {
Vitaly Buka7ce499f2015-06-09 08:04:11 -070028
29ACTION_TEMPLATE(RunCallback,
30 HAS_1_TEMPLATE_PARAMS(int, k),
31 AND_0_VALUE_PARAMS()) {
32 return std::get<k>(args).Run();
33}
34
35ACTION_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
41class 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
61class 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 Buka0801a1f2015-08-14 10:03:46 -070070 MOCK_METHOD5(
71 StartPairing,
72 bool(PairingType, CryptoType, std::string*, std::string*, ErrorPtr*));
Vitaly Buka7ce499f2015-06-09 08:04:11 -070073 MOCK_METHOD5(ConfirmPairing,
74 bool(const std::string&,
75 const std::string&,
76 std::string*,
77 std::string*,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070078 ErrorPtr*));
79 MOCK_METHOD2(CancelPairing, bool(const std::string&, ErrorPtr*));
Vitaly Buka7ce499f2015-06-09 08:04:11 -070080
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 Buka4ebd3292015-09-23 18:04:17 -070091 PairingType::kPinCode, PairingType::kEmbeddedCode,
Vitaly Buka7ce499f2015-06-09 08:04:11 -070092 }));
93
94 EXPECT_CALL(*this, GetCryptoTypes())
95 .WillRepeatedly(Return(std::set<CryptoType>{
Vitaly Bukac8ba2282015-10-01 17:42:40 -070096 CryptoType::kSpake_p224,
Vitaly Buka7ce499f2015-06-09 08:04:11 -070097 }));
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
111class MockWifiDelegate : public WifiDelegate {
112 public:
113 MOCK_CONST_METHOD0(GetConnectionState, const ConnectionState&());
114 MOCK_CONST_METHOD0(GetSetupState, const SetupState&());
115 MOCK_METHOD3(ConfigureCredentials,
Vitaly Buka0801a1f2015-08-14 10:03:46 -0700116 bool(const std::string&, const std::string&, ErrorPtr*));
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700117 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
136class MockCloudDelegate : public CloudDelegate {
137 public:
Johan Euphrosine0b7bb9f2015-09-29 01:11:21 -0700138 MOCK_CONST_METHOD0(GetDeviceId, std::string());
Vitaly Buka658aa362015-09-15 20:59:12 -0700139 MOCK_CONST_METHOD0(GetModelId, std::string());
140 MOCK_CONST_METHOD0(GetName, std::string());
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700141 MOCK_CONST_METHOD0(GetDescription, std::string());
142 MOCK_CONST_METHOD0(GetLocation, std::string());
Vitaly Bukab624bc42015-09-29 19:13:55 -0700143 MOCK_METHOD3(UpdateDeviceInfo,
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700144 void(const std::string&,
145 const std::string&,
Vitaly Bukab624bc42015-09-29 19:13:55 -0700146 const std::string&));
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700147 MOCK_CONST_METHOD0(GetOemName, std::string());
148 MOCK_CONST_METHOD0(GetModelName, std::string());
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700149 MOCK_CONST_METHOD0(GetAnonymousMaxScope, AuthScope());
150 MOCK_CONST_METHOD0(GetConnectionState, const ConnectionState&());
151 MOCK_CONST_METHOD0(GetSetupState, const SetupState&());
Vitaly Buka0801a1f2015-08-14 10:03:46 -0700152 MOCK_METHOD3(Setup, bool(const std::string&, const std::string&, ErrorPtr*));
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700153 MOCK_CONST_METHOD0(GetCloudId, std::string());
154 MOCK_CONST_METHOD0(GetState, const base::DictionaryValue&());
155 MOCK_CONST_METHOD0(GetCommandDef, const base::DictionaryValue&());
Vitaly Buka74763422015-10-11 00:39:52 -0700156 MOCK_METHOD3(AddCommand,
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700157 void(const base::DictionaryValue&,
158 const UserInfo&,
Vitaly Buka74763422015-10-11 00:39:52 -0700159 const CommandDoneCallback&));
160 MOCK_METHOD3(GetCommand,
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700161 void(const std::string&,
162 const UserInfo&,
Vitaly Buka74763422015-10-11 00:39:52 -0700163 const CommandDoneCallback&));
164 MOCK_METHOD3(CancelCommand,
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700165 void(const std::string&,
166 const UserInfo&,
Vitaly Buka74763422015-10-11 00:39:52 -0700167 const CommandDoneCallback&));
168 MOCK_METHOD2(ListCommands, void(const UserInfo&, const CommandDoneCallback&));
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700169
170 MockCloudDelegate() {
Johan Euphrosine0b7bb9f2015-09-29 01:11:21 -0700171 EXPECT_CALL(*this, GetDeviceId()).WillRepeatedly(Return("TestId"));
Vitaly Buka658aa362015-09-15 20:59:12 -0700172 EXPECT_CALL(*this, GetModelId()).WillRepeatedly(Return("ABMID"));
173 EXPECT_CALL(*this, GetName()).WillRepeatedly(Return("TestDevice"));
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700174 EXPECT_CALL(*this, GetDescription()).WillRepeatedly(Return(""));
175 EXPECT_CALL(*this, GetLocation()).WillRepeatedly(Return(""));
Vitaly Bukab624bc42015-09-29 19:13:55 -0700176 EXPECT_CALL(*this, UpdateDeviceInfo(_, _, _)).WillRepeatedly(Return());
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700177 EXPECT_CALL(*this, GetOemName()).WillRepeatedly(Return("Chromium"));
178 EXPECT_CALL(*this, GetModelName()).WillRepeatedly(Return("Brillo"));
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700179 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 Bukab6f015a2015-07-09 14:59:23 -0700195} // namespace privet
196} // namespace weave
Vitaly Buka7ce499f2015-06-09 08:04:11 -0700197
Vitaly Buka912b6982015-07-06 11:13:03 -0700198#endif // LIBWEAVE_SRC_PRIVET_MOCK_DELEGATES_H_