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 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/privet/privet_handler.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 6 | |
| 7 | #include <set> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <base/bind.h> |
| 12 | #include <base/json/json_reader.h> |
| 13 | #include <base/json/json_writer.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 14 | #include <base/strings/string_util.h> |
| 15 | #include <base/values.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 16 | #include <gmock/gmock.h> |
| 17 | #include <gtest/gtest.h> |
Vitaly Buka | 03ee8ac | 2016-02-05 11:40:33 -0800 | [diff] [blame] | 18 | #include <weave/device.h> |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 19 | #include <weave/test/unittest_utils.h> |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 20 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 21 | #include "src/privet/constants.h" |
| 22 | #include "src/privet/mock_delegates.h" |
Alex Vakulenko | 532140d | 2015-12-11 17:18:21 -0800 | [diff] [blame] | 23 | #include "src/test/mock_clock.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 24 | |
| 25 | using testing::_; |
| 26 | using testing::DoAll; |
| 27 | using testing::Invoke; |
| 28 | using testing::Return; |
| 29 | using testing::SetArgPointee; |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 30 | using testing::SaveArg; |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 31 | using testing::WithArgs; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 32 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 33 | namespace weave { |
| 34 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
| 38 | void LoadTestJson(const std::string& test_json, |
| 39 | base::DictionaryValue* dictionary) { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 40 | int error = 0; |
| 41 | std::string message; |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 42 | std::unique_ptr<base::Value> value( |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 43 | base::JSONReader::ReadAndReturnError(test_json, base::JSON_PARSE_RFC, |
| 44 | &error, &message) |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 45 | .release()); |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 46 | EXPECT_TRUE(value.get()) << "\nError: " << message << "\n" << test_json; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 47 | base::DictionaryValue* dictionary_ptr = nullptr; |
| 48 | if (value->GetAsDictionary(&dictionary_ptr)) |
| 49 | dictionary->MergeDictionary(dictionary_ptr); |
| 50 | } |
| 51 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 52 | struct CodeWithReason { |
| 53 | CodeWithReason(int code_in, const std::string& reason_in) |
| 54 | : code(code_in), reason(reason_in) {} |
| 55 | int code; |
| 56 | std::string reason; |
| 57 | }; |
| 58 | |
| 59 | std::ostream& operator<<(std::ostream& stream, const CodeWithReason& error) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 60 | return stream << R"({" << error.code << ", " << error.reason << "})"; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | bool IsEqualError(const CodeWithReason& expected, |
| 64 | const base::DictionaryValue& dictionary) { |
| 65 | std::string reason; |
| 66 | int code = 0; |
| 67 | return dictionary.GetInteger("error.http_status", &code) && |
| 68 | code == expected.code && dictionary.GetString("error.code", &reason) && |
| 69 | reason == expected.reason; |
| 70 | } |
| 71 | |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 72 | // Some error sections in response JSON objects contained debugging information |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 73 | // which is of no interest for this test. So, remove the debug info from the |
| 74 | // JSON before running validation logic on it. |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 75 | std::unique_ptr<base::DictionaryValue> StripDebugErrorDetails( |
| 76 | const std::string& path_to_error_object, |
| 77 | const base::DictionaryValue& value) { |
| 78 | std::unique_ptr<base::DictionaryValue> result{value.DeepCopy()}; |
| 79 | base::DictionaryValue* error_dict = nullptr; |
| 80 | EXPECT_TRUE(result->GetDictionary(path_to_error_object, &error_dict)); |
| 81 | scoped_ptr<base::Value> dummy; |
| 82 | error_dict->RemovePath("error.debugInfo", &dummy); |
| 83 | error_dict->RemovePath("error.message", &dummy); |
| 84 | return result; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | class PrivetHandlerTest : public testing::Test { |
| 90 | public: |
| 91 | PrivetHandlerTest() {} |
| 92 | |
| 93 | protected: |
| 94 | void SetUp() override { |
Alex Vakulenko | 532140d | 2015-12-11 17:18:21 -0800 | [diff] [blame] | 95 | EXPECT_CALL(clock_, Now()) |
Vitaly Buka | 1884cf6 | 2015-12-12 15:47:42 -0800 | [diff] [blame] | 96 | .WillRepeatedly(Return(base::Time::FromTimeT(1410000001))); |
Alex Vakulenko | 532140d | 2015-12-11 17:18:21 -0800 | [diff] [blame] | 97 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 98 | auth_header_ = "Privet anonymous"; |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 99 | handler_.reset( |
| 100 | new PrivetHandler(&cloud_, &device_, &security_, &wifi_, &clock_)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | const base::DictionaryValue& HandleRequest( |
| 104 | const std::string& api, |
| 105 | const base::DictionaryValue* input) { |
| 106 | output_.Clear(); |
| 107 | handler_->HandleRequest(api, auth_header_, input, |
| 108 | base::Bind(&PrivetHandlerTest::HandlerCallback, |
| 109 | base::Unretained(this))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 110 | return output_; |
| 111 | } |
| 112 | |
| 113 | const base::DictionaryValue& HandleRequest(const std::string& api, |
| 114 | const std::string& json_input) { |
| 115 | base::DictionaryValue dictionary; |
| 116 | LoadTestJson(json_input, &dictionary); |
| 117 | return HandleRequest(api, &dictionary); |
| 118 | } |
| 119 | |
| 120 | void HandleUnknownRequest(const std::string& api) { |
| 121 | output_.Clear(); |
| 122 | base::DictionaryValue dictionary; |
| 123 | handler_->HandleRequest(api, auth_header_, &dictionary, |
| 124 | base::Bind(&PrivetHandlerTest::HandlerNoFound)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 127 | const base::DictionaryValue& GetResponse() const { return output_; } |
| 128 | int GetResponseCount() const { return response_count_; } |
| 129 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 130 | void SetNoWifiAndGcd() { |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 131 | handler_.reset( |
| 132 | new PrivetHandler(&cloud_, &device_, &security_, nullptr, &clock_)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 133 | EXPECT_CALL(cloud_, GetCloudId()).WillRepeatedly(Return("")); |
| 134 | EXPECT_CALL(cloud_, GetConnectionState()) |
| 135 | .WillRepeatedly(ReturnRef(gcd_disabled_state_)); |
Vitaly Buka | 03ee8ac | 2016-02-05 11:40:33 -0800 | [diff] [blame] | 136 | auto set_error = [](ErrorPtr* error) { |
Vitaly Buka | 48a8669 | 2016-01-21 17:15:58 -0800 | [diff] [blame] | 137 | Error::AddTo(error, FROM_HERE, "setupUnavailable", ""); |
Vitaly Buka | 075b3d4 | 2015-06-09 08:34:25 -0700 | [diff] [blame] | 138 | }; |
Vitaly Buka | 03ee8ac | 2016-02-05 11:40:33 -0800 | [diff] [blame] | 139 | EXPECT_CALL(cloud_, Setup(_, _)) |
| 140 | .WillRepeatedly(DoAll(WithArgs<1>(Invoke(set_error)), Return(false))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alex Vakulenko | 532140d | 2015-12-11 17:18:21 -0800 | [diff] [blame] | 143 | test::MockClock clock_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 144 | testing::StrictMock<MockCloudDelegate> cloud_; |
| 145 | testing::StrictMock<MockDeviceDelegate> device_; |
| 146 | testing::StrictMock<MockSecurityDelegate> security_; |
| 147 | testing::StrictMock<MockWifiDelegate> wifi_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 148 | std::string auth_header_; |
| 149 | |
| 150 | private: |
| 151 | void HandlerCallback(int status, const base::DictionaryValue& output) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 152 | output_.Clear(); |
| 153 | ++response_count_; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 154 | output_.MergeDictionary(&output); |
| 155 | if (!output_.HasKey("error")) { |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 156 | EXPECT_EQ(200, status); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 159 | EXPECT_NE(200, status); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 160 | output_.SetInteger("error.http_status", status); |
| 161 | } |
| 162 | |
| 163 | static void HandlerNoFound(int status, const base::DictionaryValue&) { |
Vitaly Buka | 1da6599 | 2015-08-06 01:38:57 -0700 | [diff] [blame] | 164 | EXPECT_EQ(404, status); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 167 | std::unique_ptr<PrivetHandler> handler_; |
| 168 | base::DictionaryValue output_; |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 169 | int response_count_{0}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 170 | ConnectionState gcd_disabled_state_{ConnectionState::kDisabled}; |
| 171 | }; |
| 172 | |
| 173 | TEST_F(PrivetHandlerTest, UnknownApi) { |
| 174 | HandleUnknownRequest("/privet/foo"); |
| 175 | } |
| 176 | |
| 177 | TEST_F(PrivetHandlerTest, InvalidFormat) { |
| 178 | auth_header_ = ""; |
| 179 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "invalidFormat"), |
| 180 | HandleRequest("/privet/info", nullptr)); |
| 181 | } |
| 182 | |
| 183 | TEST_F(PrivetHandlerTest, MissingAuth) { |
| 184 | auth_header_ = ""; |
| 185 | EXPECT_PRED2(IsEqualError, CodeWithReason(401, "missingAuthorization"), |
| 186 | HandleRequest("/privet/info", "{}")); |
| 187 | } |
| 188 | |
| 189 | TEST_F(PrivetHandlerTest, InvalidAuth) { |
| 190 | auth_header_ = "foo"; |
| 191 | EXPECT_PRED2(IsEqualError, CodeWithReason(401, "invalidAuthorization"), |
| 192 | HandleRequest("/privet/info", "{}")); |
| 193 | } |
| 194 | |
| 195 | TEST_F(PrivetHandlerTest, ExpiredAuth) { |
| 196 | auth_header_ = "Privet 123"; |
Vitaly Buka | a0a8134 | 2015-12-17 13:42:13 -0800 | [diff] [blame] | 197 | EXPECT_CALL(security_, ParseAccessToken(_, _, _)) |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 198 | .WillRepeatedly(WithArgs<2>(Invoke([](ErrorPtr* error) { |
| 199 | return Error::AddTo(error, FROM_HERE, "authorizationExpired", ""); |
| 200 | }))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 201 | EXPECT_PRED2(IsEqualError, CodeWithReason(403, "authorizationExpired"), |
| 202 | HandleRequest("/privet/info", "{}")); |
| 203 | } |
| 204 | |
| 205 | TEST_F(PrivetHandlerTest, InvalidAuthScope) { |
| 206 | EXPECT_PRED2(IsEqualError, CodeWithReason(403, "invalidAuthorizationScope"), |
| 207 | HandleRequest("/privet/v3/setup/start", "{}")); |
| 208 | } |
| 209 | |
| 210 | TEST_F(PrivetHandlerTest, InfoMinimal) { |
| 211 | SetNoWifiAndGcd(); |
| 212 | EXPECT_CALL(security_, GetPairingTypes()) |
| 213 | .WillRepeatedly(Return(std::set<PairingType>{})); |
| 214 | EXPECT_CALL(security_, GetCryptoTypes()) |
| 215 | .WillRepeatedly(Return(std::set<CryptoType>{})); |
Vitaly Buka | ee7322f | 2015-12-18 16:54:05 -0800 | [diff] [blame] | 216 | EXPECT_CALL(security_, GetAuthTypes()) |
| 217 | .WillRepeatedly(Return(std::set<AuthType>{})); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 218 | |
| 219 | const char kExpected[] = R"({ |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 220 | "version": "3.0", |
| 221 | "id": "TestId", |
| 222 | "name": "TestDevice", |
| 223 | "services": [ "developmentBoard" ], |
| 224 | "modelManifestId": "ABMID", |
| 225 | "basicModelManifest": { |
| 226 | "uiDeviceKind": "developmentBoard", |
| 227 | "oemName": "Chromium", |
| 228 | "modelName": "Brillo" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 229 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 230 | "endpoints": { |
| 231 | "httpPort": 0, |
| 232 | "httpUpdatesPort": 0, |
| 233 | "httpsPort": 0, |
| 234 | "httpsUpdatesPort": 0 |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 235 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 236 | "authentication": { |
| 237 | "anonymousMaxScope": "user", |
| 238 | "mode": [ |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 239 | ], |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 240 | "pairing": [ |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 241 | ], |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 242 | "crypto": [ |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 243 | ] |
| 244 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 245 | "gcd": { |
| 246 | "id": "", |
| 247 | "oauth_url": "https://oauths/", |
| 248 | "service_url": "https://service/", |
| 249 | "status": "disabled", |
| 250 | "xmpp_endpoint": "xmpp:678" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 251 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 252 | "time": 1410000001000.0, |
| 253 | "sessionId": "SessionId" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 254 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 255 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/info", "{}")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | TEST_F(PrivetHandlerTest, Info) { |
| 259 | EXPECT_CALL(cloud_, GetDescription()) |
| 260 | .WillRepeatedly(Return("TestDescription")); |
| 261 | EXPECT_CALL(cloud_, GetLocation()).WillRepeatedly(Return("TestLocation")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 262 | EXPECT_CALL(device_, GetHttpEnpoint()) |
| 263 | .WillRepeatedly(Return(std::make_pair(80, 10080))); |
| 264 | EXPECT_CALL(device_, GetHttpsEnpoint()) |
| 265 | .WillRepeatedly(Return(std::make_pair(443, 10443))); |
| 266 | EXPECT_CALL(wifi_, GetHostedSsid()) |
| 267 | .WillRepeatedly(Return("Test_device.BBABCLAprv")); |
| 268 | |
| 269 | const char kExpected[] = R"({ |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 270 | "version": "3.0", |
| 271 | "id": "TestId", |
| 272 | "name": "TestDevice", |
| 273 | "description": "TestDescription", |
| 274 | "location": "TestLocation", |
| 275 | "services": [ "developmentBoard" ], |
| 276 | "modelManifestId": "ABMID", |
| 277 | "basicModelManifest": { |
| 278 | "uiDeviceKind": "developmentBoard", |
| 279 | "oemName": "Chromium", |
| 280 | "modelName": "Brillo" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 281 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 282 | "endpoints": { |
| 283 | "httpPort": 80, |
| 284 | "httpUpdatesPort": 10080, |
| 285 | "httpsPort": 443, |
| 286 | "httpsUpdatesPort": 10443 |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 287 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 288 | "authentication": { |
| 289 | "anonymousMaxScope": "none", |
| 290 | "mode": [ |
| 291 | "anonymous", |
| 292 | "pairing", |
| 293 | "local" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 294 | ], |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 295 | "pairing": [ |
| 296 | "pinCode", |
| 297 | "embeddedCode" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 298 | ], |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 299 | "crypto": [ |
| 300 | "p224_spake2" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 301 | ] |
| 302 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 303 | "wifi": { |
| 304 | "capabilities": [ |
| 305 | "2.4GHz" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 306 | ], |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 307 | "ssid": "TestSsid", |
| 308 | "hostedSsid": "Test_device.BBABCLAprv", |
| 309 | "status": "offline" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 310 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 311 | "gcd": { |
| 312 | "id": "TestCloudId", |
| 313 | "oauth_url": "https://oauths/", |
| 314 | "service_url": "https://service/", |
| 315 | "status": "online", |
| 316 | "xmpp_endpoint": "xmpp:678" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 317 | }, |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 318 | "time": 1410000001000.0, |
| 319 | "sessionId": "SessionId" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 320 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 321 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/info", "{}")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | TEST_F(PrivetHandlerTest, PairingStartInvalidParams) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 325 | EXPECT_PRED2( |
| 326 | IsEqualError, CodeWithReason(400, "invalidParams"), |
| 327 | HandleRequest("/privet/v3/pairing/start", |
| 328 | R"({"pairing":"embeddedCode","crypto":"crypto"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 329 | |
| 330 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "invalidParams"), |
| 331 | HandleRequest("/privet/v3/pairing/start", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 332 | R"({"pairing":"code","crypto":"p224_spake2"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | TEST_F(PrivetHandlerTest, PairingStart) { |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 336 | EXPECT_JSON_EQ( |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 337 | R"({"deviceCommitment": "testCommitment", "sessionId": "testSession"})", |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 338 | HandleRequest("/privet/v3/pairing/start", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 339 | R"({"pairing": "embeddedCode", "crypto": "p224_spake2"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST_F(PrivetHandlerTest, PairingConfirm) { |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 343 | EXPECT_JSON_EQ( |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 344 | R"({"certFingerprint":"testFingerprint","certSignature":"testSignature"})", |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 345 | HandleRequest( |
| 346 | "/privet/v3/pairing/confirm", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 347 | R"({"sessionId":"testSession","clientCommitment":"testCommitment"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | TEST_F(PrivetHandlerTest, PairingCancel) { |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 351 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/pairing/cancel", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 352 | R"({"sessionId": "testSession"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | TEST_F(PrivetHandlerTest, AuthErrorNoType) { |
| 356 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "invalidAuthMode"), |
| 357 | HandleRequest("/privet/v3/auth", "{}")); |
| 358 | } |
| 359 | |
| 360 | TEST_F(PrivetHandlerTest, AuthErrorInvalidType) { |
| 361 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "invalidAuthMode"), |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 362 | HandleRequest("/privet/v3/auth", R"({"mode":"unknown"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | TEST_F(PrivetHandlerTest, AuthErrorNoScope) { |
| 366 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "invalidRequestedScope"), |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 367 | HandleRequest("/privet/v3/auth", R"({"mode":"anonymous"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | TEST_F(PrivetHandlerTest, AuthErrorInvalidScope) { |
| 371 | EXPECT_PRED2( |
| 372 | IsEqualError, CodeWithReason(400, "invalidRequestedScope"), |
| 373 | HandleRequest("/privet/v3/auth", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 374 | R"({"mode":"anonymous","requestedScope":"unknown"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | TEST_F(PrivetHandlerTest, AuthErrorAccessDenied) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 378 | EXPECT_PRED2( |
| 379 | IsEqualError, CodeWithReason(403, "accessDenied"), |
| 380 | HandleRequest("/privet/v3/auth", |
| 381 | R"({"mode":"anonymous","requestedScope":"owner"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | TEST_F(PrivetHandlerTest, AuthErrorInvalidAuthCode) { |
Vitaly Buka | fd2ef68 | 2015-12-17 20:57:01 -0800 | [diff] [blame] | 385 | auto set_error = [](ErrorPtr* error) { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 386 | return Error::AddTo(error, FROM_HERE, "invalidAuthCode", ""); |
Vitaly Buka | fd2ef68 | 2015-12-17 20:57:01 -0800 | [diff] [blame] | 387 | }; |
| 388 | EXPECT_CALL(security_, CreateAccessToken(_, "testToken", _, _, _, _, _)) |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 389 | .WillRepeatedly(WithArgs<6>(Invoke(set_error))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 390 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 391 | "mode": "pairing", |
| 392 | "requestedScope": "user", |
| 393 | "authCode": "testToken" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 394 | })"; |
| 395 | EXPECT_PRED2(IsEqualError, CodeWithReason(403, "invalidAuthCode"), |
| 396 | HandleRequest("/privet/v3/auth", kInput)); |
| 397 | } |
| 398 | |
| 399 | TEST_F(PrivetHandlerTest, AuthAnonymous) { |
| 400 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 401 | "accessToken": "GuestAccessToken", |
| 402 | "expiresIn": 15, |
| 403 | "scope": "viewer", |
| 404 | "tokenType": "Privet" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 405 | })"; |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 406 | EXPECT_JSON_EQ( |
| 407 | kExpected, |
| 408 | HandleRequest("/privet/v3/auth", |
| 409 | R"({"mode":"anonymous","requestedScope":"auto"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | TEST_F(PrivetHandlerTest, AuthPairing) { |
Vitaly Buka | fd2ef68 | 2015-12-17 20:57:01 -0800 | [diff] [blame] | 413 | EXPECT_CALL(security_, CreateAccessToken(_, _, _, _, _, _, _)) |
| 414 | .WillRepeatedly(DoAll(SetArgPointee<3>("OwnerAccessToken"), |
| 415 | SetArgPointee<4>(AuthScope::kOwner), |
| 416 | SetArgPointee<5>(base::TimeDelta::FromSeconds(15)), |
| 417 | Return(true))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 418 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 419 | "mode": "pairing", |
| 420 | "requestedScope": "owner", |
| 421 | "authCode": "testToken" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 422 | })"; |
| 423 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 424 | "accessToken": "OwnerAccessToken", |
| 425 | "expiresIn": 15, |
| 426 | "scope": "owner", |
| 427 | "tokenType": "Privet" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 428 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 429 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/auth", kInput)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 432 | TEST_F(PrivetHandlerTest, AuthLocalAuto) { |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 433 | EXPECT_CALL(security_, CreateAccessToken(_, _, _, _, _, _, _)) |
| 434 | .WillRepeatedly(DoAll(SetArgPointee<3>("UserAccessToken"), |
| 435 | SetArgPointee<4>(AuthScope::kUser), |
| 436 | SetArgPointee<5>(base::TimeDelta::FromSeconds(15)), |
| 437 | Return(true))); |
| 438 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 439 | "mode": "local", |
| 440 | "requestedScope": "auto", |
| 441 | "authCode": "localAuthToken" |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 442 | })"; |
| 443 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 444 | "accessToken": "UserAccessToken", |
| 445 | "expiresIn": 15, |
| 446 | "scope": "user", |
| 447 | "tokenType": "Privet" |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 448 | })"; |
| 449 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/auth", kInput)); |
| 450 | } |
| 451 | |
| 452 | TEST_F(PrivetHandlerTest, AuthLocal) { |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 453 | EXPECT_CALL(security_, CreateAccessToken(_, _, _, _, _, _, _)) |
| 454 | .WillRepeatedly(DoAll(SetArgPointee<3>("ManagerAccessToken"), |
| 455 | SetArgPointee<4>(AuthScope::kManager), |
| 456 | SetArgPointee<5>(base::TimeDelta::FromSeconds(15)), |
| 457 | Return(true))); |
| 458 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 459 | "mode": "local", |
| 460 | "requestedScope": "manager", |
| 461 | "authCode": "localAuthToken" |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 462 | })"; |
| 463 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 464 | "accessToken": "ManagerAccessToken", |
| 465 | "expiresIn": 15, |
| 466 | "scope": "manager", |
| 467 | "tokenType": "Privet" |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 468 | })"; |
| 469 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/auth", kInput)); |
| 470 | } |
| 471 | |
| 472 | TEST_F(PrivetHandlerTest, AuthLocalHighScope) { |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 473 | EXPECT_CALL(security_, CreateAccessToken(_, _, _, _, _, _, _)) |
| 474 | .WillRepeatedly(DoAll(SetArgPointee<3>("UserAccessToken"), |
| 475 | SetArgPointee<4>(AuthScope::kUser), |
| 476 | SetArgPointee<5>(base::TimeDelta::FromSeconds(1)), |
| 477 | Return(true))); |
| 478 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 479 | "mode": "local", |
| 480 | "requestedScope": "manager", |
| 481 | "authCode": "localAuthToken" |
Vitaly Buka | 4957afb | 2015-12-17 22:50:24 -0800 | [diff] [blame] | 482 | })"; |
| 483 | EXPECT_PRED2(IsEqualError, CodeWithReason(403, "accessDenied"), |
| 484 | HandleRequest("/privet/v3/auth", kInput)); |
| 485 | } |
| 486 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 487 | class PrivetHandlerTestWithAuth : public PrivetHandlerTest { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 488 | public: |
| 489 | void SetUp() override { |
| 490 | PrivetHandlerTest::SetUp(); |
| 491 | auth_header_ = "Privet 123"; |
Vitaly Buka | a0a8134 | 2015-12-17 13:42:13 -0800 | [diff] [blame] | 492 | EXPECT_CALL(security_, ParseAccessToken(_, _, _)) |
Vitaly Buka | 66f46b8 | 2015-12-18 15:36:01 -0800 | [diff] [blame] | 493 | .WillRepeatedly(DoAll( |
Vitaly Buka | 4fe71e3 | 2016-01-29 11:50:53 -0800 | [diff] [blame] | 494 | SetArgPointee<1>(UserInfo{AuthScope::kOwner, TestUserId{"1"}}), |
| 495 | Return(true))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 496 | } |
| 497 | }; |
| 498 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 499 | class PrivetHandlerSetupTest : public PrivetHandlerTestWithAuth {}; |
| 500 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 501 | TEST_F(PrivetHandlerSetupTest, StatusEmpty) { |
| 502 | SetNoWifiAndGcd(); |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 503 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/setup/status", "{}")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | TEST_F(PrivetHandlerSetupTest, StatusWifi) { |
| 507 | wifi_.setup_state_ = SetupState{SetupState::kSuccess}; |
| 508 | |
| 509 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 510 | "wifi": { |
| 511 | "ssid": "TestSsid", |
| 512 | "status": "success" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 513 | } |
| 514 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 515 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/setup/status", "{}")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | TEST_F(PrivetHandlerSetupTest, StatusWifiError) { |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 519 | ErrorPtr error; |
Vitaly Buka | 48a8669 | 2016-01-21 17:15:58 -0800 | [diff] [blame] | 520 | Error::AddTo(&error, FROM_HERE, "invalidPassphrase", ""); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 521 | wifi_.setup_state_ = SetupState{std::move(error)}; |
| 522 | |
| 523 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 524 | "wifi": { |
| 525 | "status": "error", |
| 526 | "error": { |
| 527 | "code": "invalidPassphrase" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 531 | EXPECT_JSON_EQ(kExpected, |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 532 | *StripDebugErrorDetails( |
| 533 | "wifi", HandleRequest("/privet/v3/setup/status", "{}"))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | TEST_F(PrivetHandlerSetupTest, StatusGcd) { |
| 537 | cloud_.setup_state_ = SetupState{SetupState::kSuccess}; |
| 538 | |
| 539 | const char kExpected[] = R"({ |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 540 | "gcd": { |
| 541 | "id": "TestCloudId", |
| 542 | "oauth_url": "https://oauths/", |
| 543 | "service_url": "https://service/", |
| 544 | "status": "success", |
| 545 | "xmpp_endpoint": "xmpp:678" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 546 | } |
| 547 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 548 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/setup/status", "{}")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | TEST_F(PrivetHandlerSetupTest, StatusGcdError) { |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 552 | ErrorPtr error; |
Vitaly Buka | 48a8669 | 2016-01-21 17:15:58 -0800 | [diff] [blame] | 553 | Error::AddTo(&error, FROM_HERE, "invalidTicket", ""); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 554 | cloud_.setup_state_ = SetupState{std::move(error)}; |
| 555 | |
| 556 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 557 | "gcd": { |
| 558 | "status": "error", |
| 559 | "error": { |
| 560 | "code": "invalidTicket" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 564 | EXPECT_JSON_EQ(kExpected, |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 565 | *StripDebugErrorDetails( |
| 566 | "gcd", HandleRequest("/privet/v3/setup/status", "{}"))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | TEST_F(PrivetHandlerSetupTest, SetupNameDescriptionLocation) { |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 570 | EXPECT_CALL(cloud_, |
| 571 | UpdateDeviceInfo("testName", "testDescription", "testLocation")) |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 572 | .Times(1); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 573 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 574 | "name": "testName", |
| 575 | "description": "testDescription", |
| 576 | "location": "testLocation" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 577 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 578 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/setup/start", kInput)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | TEST_F(PrivetHandlerSetupTest, InvalidParams) { |
| 582 | const char kInputWifi[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 583 | "wifi": { |
| 584 | "ssid": "" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 585 | } |
| 586 | })"; |
| 587 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "invalidParams"), |
| 588 | HandleRequest("/privet/v3/setup/start", kInputWifi)); |
| 589 | |
| 590 | const char kInputRegistration[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 591 | "gcd": { |
| 592 | "ticketId": "" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 593 | } |
| 594 | })"; |
| 595 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "invalidParams"), |
| 596 | HandleRequest("/privet/v3/setup/start", kInputRegistration)); |
| 597 | } |
| 598 | |
| 599 | TEST_F(PrivetHandlerSetupTest, WifiSetupUnavailable) { |
| 600 | SetNoWifiAndGcd(); |
| 601 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "setupUnavailable"), |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 602 | HandleRequest("/privet/v3/setup/start", R"({"wifi": {}})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | TEST_F(PrivetHandlerSetupTest, WifiSetup) { |
| 606 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 607 | "wifi": { |
| 608 | "ssid": "testSsid", |
| 609 | "passphrase": "testPass" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 610 | } |
| 611 | })"; |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 612 | auto set_error = [](const std::string&, const std::string&, ErrorPtr* error) { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 613 | return Error::AddTo(error, FROM_HERE, "deviceBusy", ""); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 614 | }; |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 615 | EXPECT_CALL(wifi_, ConfigureCredentials(_, _, _)).WillOnce(Invoke(set_error)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 616 | EXPECT_PRED2(IsEqualError, CodeWithReason(503, "deviceBusy"), |
| 617 | HandleRequest("/privet/v3/setup/start", kInput)); |
| 618 | |
| 619 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 620 | "wifi": { |
| 621 | "status": "inProgress" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 622 | } |
| 623 | })"; |
| 624 | wifi_.setup_state_ = SetupState{SetupState::kInProgress}; |
| 625 | EXPECT_CALL(wifi_, ConfigureCredentials("testSsid", "testPass", _)) |
| 626 | .WillOnce(Return(true)); |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 627 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/setup/start", kInput)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | TEST_F(PrivetHandlerSetupTest, GcdSetupUnavailable) { |
| 631 | SetNoWifiAndGcd(); |
| 632 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 633 | "gcd": { |
| 634 | "ticketId": "testTicket", |
| 635 | "user": "testUser" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 636 | } |
| 637 | })"; |
| 638 | |
| 639 | EXPECT_PRED2(IsEqualError, CodeWithReason(400, "setupUnavailable"), |
| 640 | HandleRequest("/privet/v3/setup/start", kInput)); |
| 641 | } |
| 642 | |
| 643 | TEST_F(PrivetHandlerSetupTest, GcdSetup) { |
| 644 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 645 | "gcd": { |
| 646 | "ticketId": "testTicket", |
| 647 | "user": "testUser" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 648 | } |
| 649 | })"; |
| 650 | |
Vitaly Buka | 03ee8ac | 2016-02-05 11:40:33 -0800 | [diff] [blame] | 651 | auto set_error = [](ErrorPtr* error) { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 652 | return Error::AddTo(error, FROM_HERE, "deviceBusy", ""); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 653 | }; |
Vitaly Buka | 03ee8ac | 2016-02-05 11:40:33 -0800 | [diff] [blame] | 654 | EXPECT_CALL(cloud_, Setup(_, _)).WillOnce(WithArgs<1>(Invoke(set_error))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 655 | EXPECT_PRED2(IsEqualError, CodeWithReason(503, "deviceBusy"), |
| 656 | HandleRequest("/privet/v3/setup/start", kInput)); |
| 657 | |
| 658 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 659 | "gcd": { |
| 660 | "status": "inProgress" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 661 | } |
| 662 | })"; |
| 663 | cloud_.setup_state_ = SetupState{SetupState::kInProgress}; |
Vitaly Buka | 03ee8ac | 2016-02-05 11:40:33 -0800 | [diff] [blame] | 664 | EXPECT_CALL(cloud_, Setup(RegistrationData{"testTicket"}, _)) |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 665 | .WillOnce(Return(true)); |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 666 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/setup/start", kInput)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 669 | TEST_F(PrivetHandlerSetupTest, GcdSetupWithEndpoints) { |
| 670 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 671 | "gcd": { |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 672 | "api_key": "test_api_key", |
| 673 | "client_id": "test_client_id", |
| 674 | "client_secret": "test_client_secret", |
| 675 | "oauth_url": "https://oauths/", |
| 676 | "service_url": "https://service/", |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 677 | "xmpp_endpoint": "xmpp:678", |
| 678 | "ticketId": "testTicket", |
| 679 | "user": "testUser" |
| 680 | } |
| 681 | })"; |
| 682 | |
| 683 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 684 | "gcd": { |
| 685 | "status": "inProgress" |
Vitaly Buka | 8b00597 | 2016-02-10 11:24:42 -0800 | [diff] [blame] | 686 | } |
| 687 | })"; |
| 688 | cloud_.setup_state_ = SetupState{SetupState::kInProgress}; |
| 689 | |
| 690 | RegistrationData expected_reg_data; |
| 691 | expected_reg_data.ticket_id = "testTicket"; |
| 692 | expected_reg_data.oauth_url = "https://oauths/"; |
| 693 | expected_reg_data.client_id = "test_client_id"; |
| 694 | expected_reg_data.client_secret = "test_client_secret"; |
| 695 | expected_reg_data.api_key = "test_api_key"; |
| 696 | expected_reg_data.service_url = "https://service/"; |
| 697 | expected_reg_data.xmpp_endpoint = "xmpp:678"; |
| 698 | |
| 699 | EXPECT_CALL(cloud_, Setup(expected_reg_data, _)).WillOnce(Return(true)); |
| 700 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/setup/start", kInput)); |
| 701 | } |
| 702 | |
Vitaly Buka | 287346b | 2015-12-15 14:35:28 -0800 | [diff] [blame] | 703 | TEST_F(PrivetHandlerSetupTest, GcdSetupAsMaster) { |
Vitaly Buka | a0a8134 | 2015-12-17 13:42:13 -0800 | [diff] [blame] | 704 | EXPECT_CALL(security_, ParseAccessToken(_, _, _)) |
Vitaly Buka | 66f46b8 | 2015-12-18 15:36:01 -0800 | [diff] [blame] | 705 | .WillRepeatedly(DoAll( |
Vitaly Buka | 4fe71e3 | 2016-01-29 11:50:53 -0800 | [diff] [blame] | 706 | SetArgPointee<1>(UserInfo{AuthScope::kManager, TestUserId{"1"}}), |
| 707 | Return(true))); |
Vitaly Buka | 287346b | 2015-12-15 14:35:28 -0800 | [diff] [blame] | 708 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 709 | "gcd": { |
| 710 | "ticketId": "testTicket", |
| 711 | "user": "testUser" |
Vitaly Buka | 287346b | 2015-12-15 14:35:28 -0800 | [diff] [blame] | 712 | } |
| 713 | })"; |
| 714 | |
| 715 | EXPECT_PRED2(IsEqualError, CodeWithReason(403, "invalidAuthorizationScope"), |
| 716 | HandleRequest("/privet/v3/setup/start", kInput)); |
| 717 | } |
| 718 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 719 | TEST_F(PrivetHandlerTestWithAuth, ClaimAccessControl) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 720 | EXPECT_JSON_EQ(R"({"clientToken": "RootClientAuthToken"})", |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 721 | HandleRequest("/privet/v3/accessControl/claim", "{}")); |
| 722 | } |
| 723 | |
| 724 | TEST_F(PrivetHandlerTestWithAuth, ConfirmAccessControl) { |
| 725 | EXPECT_JSON_EQ("{}", |
| 726 | HandleRequest("/privet/v3/accessControl/confirm", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 727 | R"({"clientToken": "DerivedClientAuthToken"})")); |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 730 | TEST_F(PrivetHandlerTestWithAuth, Traits) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 731 | EXPECT_JSON_EQ(R"({"traits": {"test": {}}, "fingerprint": "1"})", |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 732 | HandleRequest("/privet/v3/traits", "{}")); |
| 733 | |
| 734 | cloud_.NotifyOnTraitDefsChanged(); |
| 735 | |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 736 | EXPECT_JSON_EQ(R"({"traits": {"test": {}}, "fingerprint": "2"})", |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 737 | HandleRequest("/privet/v3/traits", "{}")); |
| 738 | } |
| 739 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 740 | TEST_F(PrivetHandlerTestWithAuth, Components) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 741 | EXPECT_JSON_EQ(R"({"components": {"test": {}}, "fingerprint": "1"})", |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 742 | HandleRequest("/privet/v3/components", "{}")); |
| 743 | |
| 744 | cloud_.NotifyOnComponentTreeChanged(); |
| 745 | |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 746 | EXPECT_JSON_EQ(R"({"components": {"test": {}}, "fingerprint": "2"})", |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 747 | HandleRequest("/privet/v3/components", "{}")); |
| 748 | |
| 749 | // State change will also change the components fingerprint. |
| 750 | cloud_.NotifyOnStateChanged(); |
| 751 | |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 752 | EXPECT_JSON_EQ(R"({"components": {"test": {}}, "fingerprint": "3"})", |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 753 | HandleRequest("/privet/v3/components", "{}")); |
| 754 | } |
| 755 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 756 | TEST_F(PrivetHandlerTestWithAuth, ComponentsWithFiltersAndPaths) { |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 757 | const char kComponents[] = R"({ |
| 758 | "comp1": { |
| 759 | "traits": ["a", "b"], |
| 760 | "state": { |
| 761 | "a" : { |
| 762 | "prop": 1 |
| 763 | } |
| 764 | }, |
| 765 | "components": { |
| 766 | "comp2": { |
| 767 | "traits": ["c"], |
| 768 | "components": { |
| 769 | "comp4": { |
| 770 | "traits": ["d"] |
| 771 | } |
| 772 | } |
| 773 | }, |
| 774 | "comp3": { |
| 775 | "traits": ["e"] |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | })"; |
| 780 | base::DictionaryValue components; |
| 781 | LoadTestJson(kComponents, &components); |
| 782 | EXPECT_CALL(cloud_, FindComponent(_, _)).WillRepeatedly(Return(nullptr)); |
| 783 | EXPECT_CALL(cloud_, GetComponents()).WillRepeatedly(ReturnRef(components)); |
| 784 | const char kExpected1[] = R"({ |
| 785 | "components": { |
| 786 | "comp1": { |
| 787 | "state": { |
| 788 | "a" : { |
| 789 | "prop": 1 |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | }, |
| 794 | "fingerprint": "1" |
| 795 | })"; |
| 796 | EXPECT_JSON_EQ(kExpected1, HandleRequest("/privet/v3/components", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 797 | R"({"filter":["state"]})")); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 798 | |
| 799 | const char kExpected2[] = R"({ |
| 800 | "components": { |
| 801 | "comp1": { |
| 802 | "traits": ["a", "b"] |
| 803 | } |
| 804 | }, |
| 805 | "fingerprint": "1" |
| 806 | })"; |
| 807 | EXPECT_JSON_EQ(kExpected2, HandleRequest("/privet/v3/components", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 808 | R"({"filter":["traits"]})")); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 809 | |
| 810 | const char kExpected3[] = R"({ |
| 811 | "components": { |
| 812 | "comp1": { |
| 813 | "components": { |
| 814 | "comp2": { |
| 815 | "components": { |
| 816 | "comp4": {} |
| 817 | } |
| 818 | }, |
| 819 | "comp3": {} |
| 820 | } |
| 821 | } |
| 822 | }, |
| 823 | "fingerprint": "1" |
| 824 | })"; |
| 825 | EXPECT_JSON_EQ(kExpected3, HandleRequest("/privet/v3/components", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 826 | R"({"filter":["components"]})")); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 827 | |
| 828 | const char kExpected4[] = R"({ |
| 829 | "components": { |
| 830 | "comp1": { |
| 831 | "traits": ["a", "b"], |
| 832 | "state": { |
| 833 | "a" : { |
| 834 | "prop": 1 |
| 835 | } |
| 836 | }, |
| 837 | "components": { |
| 838 | "comp2": { |
| 839 | "traits": ["c"], |
| 840 | "components": { |
| 841 | "comp4": { |
| 842 | "traits": ["d"] |
| 843 | } |
| 844 | } |
| 845 | }, |
| 846 | "comp3": { |
| 847 | "traits": ["e"] |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | }, |
| 852 | "fingerprint": "1" |
| 853 | })"; |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 854 | EXPECT_JSON_EQ( |
| 855 | kExpected4, |
| 856 | HandleRequest("/privet/v3/components", |
| 857 | R"({"filter":["traits", "components", "state"]})")); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 858 | |
| 859 | const base::DictionaryValue* comp2 = nullptr; |
| 860 | ASSERT_TRUE(components.GetDictionary("comp1.components.comp2", &comp2)); |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 861 | EXPECT_CALL(cloud_, FindComponent("comp1.comp2", _)).WillOnce(Return(comp2)); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 862 | |
| 863 | const char kExpected5[] = R"({ |
| 864 | "components": { |
| 865 | "comp2": { |
| 866 | "traits": ["c"], |
| 867 | "components": { |
| 868 | "comp4": { |
| 869 | "traits": ["d"] |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | }, |
| 874 | "fingerprint": "1" |
| 875 | })"; |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 876 | EXPECT_JSON_EQ( |
| 877 | kExpected5, |
| 878 | HandleRequest( |
| 879 | "/privet/v3/components", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 880 | R"({"path":"comp1.comp2", "filter":["traits", "components"]})")); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 881 | |
| 882 | auto error_handler = [](ErrorPtr* error) -> const base::DictionaryValue* { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 883 | return Error::AddTo(error, FROM_HERE, "componentNotFound", ""); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 884 | }; |
| 885 | EXPECT_CALL(cloud_, FindComponent("comp7", _)) |
| 886 | .WillOnce(WithArgs<1>(Invoke(error_handler))); |
| 887 | |
| 888 | EXPECT_PRED2( |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 889 | IsEqualError, CodeWithReason(500, "componentNotFound"), |
| 890 | HandleRequest("/privet/v3/components", |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 891 | R"({"path":"comp7", "filter":["traits", "components"]})")); |
Alex Vakulenko | 3eb52e7 | 2015-12-14 15:34:08 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 894 | TEST_F(PrivetHandlerTestWithAuth, CommandsExecute) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 895 | const char kInput[] = R"({"name": "test"})"; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 896 | base::DictionaryValue command; |
| 897 | LoadTestJson(kInput, &command); |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 898 | LoadTestJson(R"({"id":"5"})", &command); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 899 | EXPECT_CALL(cloud_, AddCommand(_, _, _)) |
| 900 | .WillOnce(WithArgs<2>(Invoke( |
| 901 | [&command](const CloudDelegate::CommandDoneCallback& callback) { |
| 902 | callback.Run(command, nullptr); |
| 903 | }))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 904 | |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 905 | EXPECT_JSON_EQ(R"({"name":"test", "id":"5"})", |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 906 | HandleRequest("/privet/v3/commands/execute", kInput)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 909 | TEST_F(PrivetHandlerTestWithAuth, CommandsStatus) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 910 | const char kInput[] = R"({"id": "5"})"; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 911 | base::DictionaryValue command; |
| 912 | LoadTestJson(kInput, &command); |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 913 | LoadTestJson(R"({"name":"test"})", &command); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 914 | EXPECT_CALL(cloud_, GetCommand(_, _, _)) |
| 915 | .WillOnce(WithArgs<2>(Invoke( |
| 916 | [&command](const CloudDelegate::CommandDoneCallback& callback) { |
| 917 | callback.Run(command, nullptr); |
| 918 | }))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 919 | |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 920 | EXPECT_JSON_EQ(R"({"name":"test", "id":"5"})", |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 921 | HandleRequest("/privet/v3/commands/status", kInput)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 922 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 923 | ErrorPtr error; |
Vitaly Buka | 48a8669 | 2016-01-21 17:15:58 -0800 | [diff] [blame] | 924 | Error::AddTo(&error, FROM_HERE, "notFound", ""); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 925 | EXPECT_CALL(cloud_, GetCommand(_, _, _)) |
| 926 | .WillOnce(WithArgs<2>( |
| 927 | Invoke([&error](const CloudDelegate::CommandDoneCallback& callback) { |
| 928 | callback.Run({}, std::move(error)); |
| 929 | }))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 930 | |
| 931 | EXPECT_PRED2(IsEqualError, CodeWithReason(404, "notFound"), |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 932 | HandleRequest("/privet/v3/commands/status", R"({"id": "15"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 935 | TEST_F(PrivetHandlerTestWithAuth, CommandsCancel) { |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 936 | const char kExpected[] = R"({"id": "5", "name":"test", "state":"cancelled"})"; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 937 | base::DictionaryValue command; |
| 938 | LoadTestJson(kExpected, &command); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 939 | EXPECT_CALL(cloud_, CancelCommand(_, _, _)) |
| 940 | .WillOnce(WithArgs<2>(Invoke( |
| 941 | [&command](const CloudDelegate::CommandDoneCallback& callback) { |
| 942 | callback.Run(command, nullptr); |
| 943 | }))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 944 | |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 945 | EXPECT_JSON_EQ(kExpected, |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 946 | HandleRequest("/privet/v3/commands/cancel", R"({"id": "8"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 947 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 948 | ErrorPtr error; |
Vitaly Buka | 48a8669 | 2016-01-21 17:15:58 -0800 | [diff] [blame] | 949 | Error::AddTo(&error, FROM_HERE, "notFound", ""); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 950 | EXPECT_CALL(cloud_, CancelCommand(_, _, _)) |
| 951 | .WillOnce(WithArgs<2>( |
| 952 | Invoke([&error](const CloudDelegate::CommandDoneCallback& callback) { |
| 953 | callback.Run({}, std::move(error)); |
| 954 | }))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 955 | |
| 956 | EXPECT_PRED2(IsEqualError, CodeWithReason(404, "notFound"), |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 957 | HandleRequest("/privet/v3/commands/cancel", R"({"id": "11"})")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Vitaly Buka | 2d24e0e | 2015-12-10 14:56:37 -0800 | [diff] [blame] | 960 | TEST_F(PrivetHandlerTestWithAuth, CommandsList) { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 961 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 962 | "commands" : [ |
| 963 | {"id":"5", "state":"cancelled"}, |
| 964 | {"id":"15", "state":"inProgress"} |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 965 | ]})"; |
| 966 | |
| 967 | base::DictionaryValue commands; |
| 968 | LoadTestJson(kExpected, &commands); |
| 969 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 970 | EXPECT_CALL(cloud_, ListCommands(_, _)) |
| 971 | .WillOnce(WithArgs<1>(Invoke( |
| 972 | [&commands](const CloudDelegate::CommandDoneCallback& callback) { |
| 973 | callback.Run(commands, nullptr); |
| 974 | }))); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 975 | |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 976 | EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/commands/list", "{}")); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 979 | class PrivetHandlerCheckForUpdatesTest : public PrivetHandlerTestWithAuth {}; |
| 980 | |
| 981 | TEST_F(PrivetHandlerCheckForUpdatesTest, NoInput) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 982 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 983 | .WillOnce(Return(base::TimeDelta::Max())); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 984 | cloud_.NotifyOnTraitDefsChanged(); |
| 985 | cloud_.NotifyOnComponentTreeChanged(); |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 986 | cloud_.NotifyOnStateChanged(); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 987 | const char kInput[] = "{}"; |
| 988 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 989 | "commandsFingerprint": "2", |
| 990 | "stateFingerprint": "2", |
| 991 | "traitsFingerprint": "2", |
| 992 | "componentsFingerprint": "3" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 993 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 994 | EXPECT_JSON_EQ(kExpected, |
| 995 | HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 996 | EXPECT_EQ(1, GetResponseCount()); |
| 997 | } |
| 998 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 999 | TEST_F(PrivetHandlerCheckForUpdatesTest, AlreadyChanged) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1000 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1001 | .WillOnce(Return(base::TimeDelta::Max())); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 1002 | cloud_.NotifyOnTraitDefsChanged(); |
| 1003 | cloud_.NotifyOnComponentTreeChanged(); |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 1004 | cloud_.NotifyOnStateChanged(); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1005 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1006 | "commandsFingerprint": "1", |
| 1007 | "stateFingerprint": "1", |
| 1008 | "traitsFingerprint": "1", |
| 1009 | "componentsFingerprint": "1" |
Alex Vakulenko | f969132 | 2015-12-07 14:34:48 -0800 | [diff] [blame] | 1010 | })"; |
| 1011 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1012 | "commandsFingerprint": "2", |
| 1013 | "stateFingerprint": "2", |
| 1014 | "traitsFingerprint": "2", |
| 1015 | "componentsFingerprint": "3" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1016 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1017 | EXPECT_JSON_EQ(kExpected, |
| 1018 | HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1019 | EXPECT_EQ(1, GetResponseCount()); |
| 1020 | } |
| 1021 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1022 | TEST_F(PrivetHandlerCheckForUpdatesTest, LongPollCommands) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1023 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1024 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1025 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1026 | "commandsFingerprint": "1", |
| 1027 | "stateFingerprint": "1", |
| 1028 | "traitsFingerprint": "1", |
| 1029 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1030 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1031 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1032 | EXPECT_EQ(0, GetResponseCount()); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 1033 | cloud_.NotifyOnTraitDefsChanged(); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1034 | EXPECT_EQ(1, GetResponseCount()); |
| 1035 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1036 | "commandsFingerprint": "2", |
| 1037 | "stateFingerprint": "1", |
| 1038 | "traitsFingerprint": "2", |
| 1039 | "componentsFingerprint": "1" |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 1040 | })"; |
| 1041 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
| 1042 | } |
| 1043 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1044 | TEST_F(PrivetHandlerCheckForUpdatesTest, LongPollTraits) { |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 1045 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1046 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1047 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1048 | "commandsFingerprint": "1", |
| 1049 | "stateFingerprint": "1", |
| 1050 | "traitsFingerprint": "1", |
| 1051 | "componentsFingerprint": "1" |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 1052 | })"; |
| 1053 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
| 1054 | EXPECT_EQ(0, GetResponseCount()); |
| 1055 | cloud_.NotifyOnTraitDefsChanged(); |
| 1056 | EXPECT_EQ(1, GetResponseCount()); |
| 1057 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1058 | "commandsFingerprint": "2", |
| 1059 | "stateFingerprint": "1", |
| 1060 | "traitsFingerprint": "2", |
| 1061 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1062 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1063 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1066 | TEST_F(PrivetHandlerCheckForUpdatesTest, LongPollState) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1067 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1068 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1069 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1070 | "commandsFingerprint": "1", |
| 1071 | "stateFingerprint": "1", |
| 1072 | "traitsFingerprint": "1", |
| 1073 | "componentsFingerprint": "1" |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 1074 | })"; |
| 1075 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
| 1076 | EXPECT_EQ(0, GetResponseCount()); |
| 1077 | cloud_.NotifyOnStateChanged(); |
| 1078 | EXPECT_EQ(1, GetResponseCount()); |
| 1079 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1080 | "commandsFingerprint": "1", |
| 1081 | "stateFingerprint": "2", |
| 1082 | "traitsFingerprint": "1", |
| 1083 | "componentsFingerprint": "2" |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 1084 | })"; |
| 1085 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
| 1086 | } |
| 1087 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1088 | TEST_F(PrivetHandlerCheckForUpdatesTest, LongPollComponents) { |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame] | 1089 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1090 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1091 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1092 | "commandsFingerprint": "1", |
| 1093 | "stateFingerprint": "1", |
| 1094 | "traitsFingerprint": "1", |
| 1095 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1096 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1097 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1098 | EXPECT_EQ(0, GetResponseCount()); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 1099 | cloud_.NotifyOnComponentTreeChanged(); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1100 | EXPECT_EQ(1, GetResponseCount()); |
| 1101 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1102 | "commandsFingerprint": "1", |
| 1103 | "stateFingerprint": "1", |
| 1104 | "traitsFingerprint": "1", |
| 1105 | "componentsFingerprint": "2" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1106 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1107 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1110 | TEST_F(PrivetHandlerCheckForUpdatesTest, LongPollIgnoreTraits) { |
Alex Vakulenko | f969132 | 2015-12-07 14:34:48 -0800 | [diff] [blame] | 1111 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1112 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1113 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1114 | "stateFingerprint": "1", |
| 1115 | "componentsFingerprint": "1" |
Alex Vakulenko | f969132 | 2015-12-07 14:34:48 -0800 | [diff] [blame] | 1116 | })"; |
| 1117 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
| 1118 | EXPECT_EQ(0, GetResponseCount()); |
| 1119 | cloud_.NotifyOnTraitDefsChanged(); |
| 1120 | EXPECT_EQ(0, GetResponseCount()); |
| 1121 | cloud_.NotifyOnComponentTreeChanged(); |
| 1122 | EXPECT_EQ(1, GetResponseCount()); |
| 1123 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1124 | "commandsFingerprint": "2", |
| 1125 | "stateFingerprint": "1", |
| 1126 | "traitsFingerprint": "2", |
| 1127 | "componentsFingerprint": "2" |
Alex Vakulenko | f969132 | 2015-12-07 14:34:48 -0800 | [diff] [blame] | 1128 | })"; |
| 1129 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
| 1130 | } |
| 1131 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1132 | TEST_F(PrivetHandlerCheckForUpdatesTest, LongPollIgnoreState) { |
Alex Vakulenko | f969132 | 2015-12-07 14:34:48 -0800 | [diff] [blame] | 1133 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1134 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1135 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1136 | "commandsFingerprint": "1", |
| 1137 | "traitsFingerprint": "1" |
Alex Vakulenko | f969132 | 2015-12-07 14:34:48 -0800 | [diff] [blame] | 1138 | })"; |
| 1139 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
| 1140 | EXPECT_EQ(0, GetResponseCount()); |
| 1141 | cloud_.NotifyOnStateChanged(); |
| 1142 | EXPECT_EQ(0, GetResponseCount()); |
| 1143 | cloud_.NotifyOnComponentTreeChanged(); |
| 1144 | EXPECT_EQ(0, GetResponseCount()); |
| 1145 | cloud_.NotifyOnTraitDefsChanged(); |
| 1146 | EXPECT_EQ(1, GetResponseCount()); |
| 1147 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1148 | "commandsFingerprint": "2", |
| 1149 | "stateFingerprint": "2", |
| 1150 | "traitsFingerprint": "2", |
| 1151 | "componentsFingerprint": "3" |
Alex Vakulenko | f969132 | 2015-12-07 14:34:48 -0800 | [diff] [blame] | 1152 | })"; |
| 1153 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
| 1154 | } |
| 1155 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1156 | TEST_F(PrivetHandlerCheckForUpdatesTest, InstantTimeout) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1157 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1158 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1159 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1160 | "commandsFingerprint": "1", |
| 1161 | "stateFingerprint": "1", |
| 1162 | "traitsFingerprint": "1", |
| 1163 | "componentsFingerprint": "1", |
| 1164 | "waitTimeout": 0 |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1165 | })"; |
| 1166 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1167 | "commandsFingerprint": "1", |
| 1168 | "stateFingerprint": "1", |
| 1169 | "traitsFingerprint": "1", |
| 1170 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1171 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1172 | EXPECT_JSON_EQ(kExpected, |
| 1173 | HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1176 | TEST_F(PrivetHandlerCheckForUpdatesTest, UserTimeout) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1177 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1178 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1179 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1180 | "commandsFingerprint": "1", |
| 1181 | "stateFingerprint": "1", |
| 1182 | "traitsFingerprint": "1", |
| 1183 | "componentsFingerprint": "1", |
| 1184 | "waitTimeout": 3 |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1185 | })"; |
| 1186 | base::Closure callback; |
| 1187 | EXPECT_CALL(device_, PostDelayedTask(_, _, base::TimeDelta::FromSeconds(3))) |
| 1188 | .WillOnce(SaveArg<1>(&callback)); |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1189 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1190 | EXPECT_EQ(0, GetResponseCount()); |
| 1191 | callback.Run(); |
| 1192 | EXPECT_EQ(1, GetResponseCount()); |
| 1193 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1194 | "commandsFingerprint": "1", |
| 1195 | "stateFingerprint": "1", |
| 1196 | "traitsFingerprint": "1", |
| 1197 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1198 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1199 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1202 | TEST_F(PrivetHandlerCheckForUpdatesTest, ServerTimeout) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1203 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1204 | .WillOnce(Return(base::TimeDelta::FromMinutes(1))); |
| 1205 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1206 | "commandsFingerprint": "1", |
| 1207 | "stateFingerprint": "1", |
| 1208 | "traitsFingerprint": "1", |
| 1209 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1210 | })"; |
| 1211 | base::Closure callback; |
| 1212 | EXPECT_CALL(device_, PostDelayedTask(_, _, base::TimeDelta::FromSeconds(50))) |
| 1213 | .WillOnce(SaveArg<1>(&callback)); |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1214 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1215 | EXPECT_EQ(0, GetResponseCount()); |
| 1216 | callback.Run(); |
| 1217 | EXPECT_EQ(1, GetResponseCount()); |
| 1218 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1219 | "commandsFingerprint": "1", |
| 1220 | "stateFingerprint": "1", |
| 1221 | "traitsFingerprint": "1", |
| 1222 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1223 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1224 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1227 | TEST_F(PrivetHandlerCheckForUpdatesTest, VeryShortServerTimeout) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1228 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1229 | .WillOnce(Return(base::TimeDelta::FromSeconds(5))); |
| 1230 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1231 | "commandsFingerprint": "1", |
| 1232 | "stateFingerprint": "1", |
| 1233 | "traitsFingerprint": "1", |
| 1234 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1235 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1236 | EXPECT_JSON_EQ(kInput, HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1237 | EXPECT_EQ(1, GetResponseCount()); |
| 1238 | } |
| 1239 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1240 | TEST_F(PrivetHandlerCheckForUpdatesTest, ServerAndUserTimeout) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1241 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1242 | .WillOnce(Return(base::TimeDelta::FromMinutes(1))); |
| 1243 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1244 | "commandsFingerprint": "1", |
| 1245 | "stateFingerprint": "1", |
| 1246 | "traitsFingerprint": "1", |
| 1247 | "componentsFingerprint": "1", |
| 1248 | "waitTimeout": 10 |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1249 | })"; |
| 1250 | base::Closure callback; |
| 1251 | EXPECT_CALL(device_, PostDelayedTask(_, _, base::TimeDelta::FromSeconds(10))) |
| 1252 | .WillOnce(SaveArg<1>(&callback)); |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1253 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1254 | EXPECT_EQ(0, GetResponseCount()); |
| 1255 | callback.Run(); |
| 1256 | EXPECT_EQ(1, GetResponseCount()); |
| 1257 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1258 | "commandsFingerprint": "1", |
| 1259 | "stateFingerprint": "1", |
| 1260 | "traitsFingerprint": "1", |
| 1261 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1262 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1263 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
Alex Vakulenko | e3cc230 | 2015-12-07 15:26:26 -0800 | [diff] [blame] | 1266 | TEST_F(PrivetHandlerCheckForUpdatesTest, ChangeBeforeTimeout) { |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1267 | EXPECT_CALL(device_, GetHttpRequestTimeout()) |
| 1268 | .WillOnce(Return(base::TimeDelta::Max())); |
| 1269 | const char kInput[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1270 | "commandsFingerprint": "1", |
| 1271 | "stateFingerprint": "1", |
| 1272 | "traitsFingerprint": "1", |
| 1273 | "componentsFingerprint": "1", |
| 1274 | "waitTimeout": 10 |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1275 | })"; |
| 1276 | base::Closure callback; |
| 1277 | EXPECT_CALL(device_, PostDelayedTask(_, _, base::TimeDelta::FromSeconds(10))) |
| 1278 | .WillOnce(SaveArg<1>(&callback)); |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1279 | EXPECT_JSON_EQ("{}", HandleRequest("/privet/v3/checkForUpdates", kInput)); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1280 | EXPECT_EQ(0, GetResponseCount()); |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 1281 | cloud_.NotifyOnTraitDefsChanged(); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1282 | EXPECT_EQ(1, GetResponseCount()); |
| 1283 | const char kExpected[] = R"({ |
Vitaly Buka | 3e86099 | 2016-02-10 14:01:46 -0800 | [diff] [blame] | 1284 | "commandsFingerprint": "2", |
| 1285 | "stateFingerprint": "1", |
| 1286 | "traitsFingerprint": "2", |
| 1287 | "componentsFingerprint": "1" |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1288 | })"; |
Alex Vakulenko | 29a11ca | 2015-12-07 14:43:04 -0800 | [diff] [blame] | 1289 | EXPECT_JSON_EQ(kExpected, GetResponse()); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 1290 | callback.Run(); |
| 1291 | EXPECT_EQ(1, GetResponseCount()); |
| 1292 | } |
| 1293 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 1294 | } // namespace privet |
| 1295 | } // namespace weave |