Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "buffet/device_registration_info.h" |
| 6 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 7 | #include <base/json/json_reader.h> |
| 8 | #include <base/values.h> |
Alex Vakulenko | cf0b33a | 2014-08-20 15:02:10 -0700 | [diff] [blame] | 9 | #include <chromeos/bind_lambda.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 10 | #include <chromeos/http/http_request.h> |
| 11 | #include <chromeos/http/http_transport_fake.h> |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 12 | #include <chromeos/key_value_store.h> |
Alex Vakulenko | 3aeea1c | 2014-08-20 16:33:12 -0700 | [diff] [blame] | 13 | #include <chromeos/mime_utils.h> |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
| 15 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 16 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 17 | #include "buffet/commands/unittest_utils.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 18 | #include "buffet/device_registration_storage_keys.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 19 | #include "buffet/states/mock_state_change_queue_interface.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 20 | #include "buffet/states/state_manager.h" |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 21 | #include "buffet/storage_impls.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 22 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 23 | using chromeos::http::request_header::kAuthorization; |
| 24 | using chromeos::http::fake::ServerRequest; |
| 25 | using chromeos::http::fake::ServerResponse; |
| 26 | |
| 27 | namespace buffet { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 28 | |
| 29 | namespace { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 30 | |
| 31 | namespace test_data { |
| 32 | |
| 33 | const char kServiceURL[] = "http://gcd.server.com/"; |
| 34 | const char kOAuthURL[] = "http://oauth.server.com/"; |
| 35 | const char kApiKey[] = "GOadRdTf9FERf0k4w6EFOof56fUJ3kFDdFL3d7f"; |
| 36 | const char kClientId[] = "123543821385-sfjkjshdkjhfk234sdfsdfkskd" |
| 37 | "fkjh7f.apps.googleusercontent.com"; |
| 38 | const char kClientSecret[] = "5sdGdGlfolGlrFKfdFlgP6FG"; |
| 39 | const char kDeviceId[] = "4a7ea2d1-b331-1e1f-b206-e863c7635196"; |
| 40 | const char kClaimTicketId[] = "RTcUE"; |
| 41 | const char kAccessToken[] = "ya29.1.AADtN_V-dLUM-sVZ0qVjG9Dxm5NgdS9J" |
| 42 | "Mx_JLUqhC9bED_YFjzHZtYt65ZzXCS35NMAeaVZ" |
| 43 | "Dei530-w0yE2urpQ"; |
| 44 | const char kRefreshToken[] = "1/zQmxR6PKNvhcxf9SjXUrCjcmCrcqRKXctc6cp" |
| 45 | "1nI-GQ"; |
| 46 | const char kRobotAccountAuthCode[] = "4/Mf_ujEhPejVhOq-OxW9F5cSOnWzx." |
| 47 | "YgciVjTYGscRshQV0ieZDAqiTIjMigI"; |
| 48 | const char kRobotAccountEmail[] = "6ed0b3f54f9bd619b942f4ad2441c252@" |
| 49 | "clouddevices.gserviceaccount.com"; |
| 50 | const char kUserAccountAuthCode[] = "2/sd_GD1TGFKpJOLJ34-0g5fK0fflp.GlT" |
| 51 | "I0F5g7hNtFgj5HFGOf8FlGK9eflO"; |
| 52 | const char kUserAccessToken[] = "sd56.4.FGDjG_F-gFGF-dFG6gGOG9Dxm5NgdS9" |
| 53 | "JMx_JLUqhC9bED_YFjLKjlkjLKJlkjLKjlKJea" |
| 54 | "VZDei530-w0yE2urpQ"; |
| 55 | const char kUserRefreshToken[] = "1/zQLKjlKJlkLkLKjLkjLKjLkjLjLkjl0ftc6" |
| 56 | "cp1nI-GQ"; |
| 57 | |
| 58 | } // namespace test_data |
| 59 | |
| 60 | // Fill in the storage with default environment information (URLs, etc). |
| 61 | void InitDefaultStorage(base::DictionaryValue* data) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 62 | data->SetString(storage_keys::kRefreshToken, ""); |
| 63 | data->SetString(storage_keys::kDeviceId, ""); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 64 | data->SetString(storage_keys::kRobotAccount, ""); |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 65 | data->SetString(storage_keys::kDisplayName, ""); |
| 66 | data->SetString(storage_keys::kDescription, ""); |
| 67 | data->SetString(storage_keys::kLocation, ""); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // Add the test device registration information. |
| 71 | void SetDefaultDeviceRegistration(base::DictionaryValue* data) { |
| 72 | data->SetString(storage_keys::kRefreshToken, test_data::kRefreshToken); |
| 73 | data->SetString(storage_keys::kDeviceId, test_data::kDeviceId); |
| 74 | data->SetString(storage_keys::kRobotAccount, test_data::kRobotAccountEmail); |
| 75 | } |
| 76 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 77 | void OAuth2Handler(const ServerRequest& request, ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 78 | base::DictionaryValue json; |
| 79 | if (request.GetFormField("grant_type") == "refresh_token") { |
| 80 | // Refresh device access token. |
| 81 | EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token")); |
| 82 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 83 | EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret")); |
| 84 | json.SetString("access_token", test_data::kAccessToken); |
| 85 | } else if (request.GetFormField("grant_type") == "authorization_code") { |
| 86 | // Obtain access token. |
| 87 | std::string code = request.GetFormField("code"); |
| 88 | if (code == test_data::kUserAccountAuthCode) { |
| 89 | // Get user access token. |
| 90 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 91 | EXPECT_EQ(test_data::kClientSecret, |
| 92 | request.GetFormField("client_secret")); |
| 93 | EXPECT_EQ("urn:ietf:wg:oauth:2.0:oob", |
| 94 | request.GetFormField("redirect_uri")); |
| 95 | json.SetString("access_token", test_data::kUserAccessToken); |
| 96 | json.SetString("token_type", "Bearer"); |
| 97 | json.SetString("refresh_token", test_data::kUserRefreshToken); |
| 98 | } else if (code == test_data::kRobotAccountAuthCode) { |
| 99 | // Get device access token. |
| 100 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 101 | EXPECT_EQ(test_data::kClientSecret, |
| 102 | request.GetFormField("client_secret")); |
| 103 | EXPECT_EQ("oob", request.GetFormField("redirect_uri")); |
| 104 | EXPECT_EQ("https://www.googleapis.com/auth/clouddevices", |
| 105 | request.GetFormField("scope")); |
| 106 | json.SetString("access_token", test_data::kAccessToken); |
| 107 | json.SetString("token_type", "Bearer"); |
| 108 | json.SetString("refresh_token", test_data::kRefreshToken); |
| 109 | } else { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 110 | FAIL() << "Unexpected authorization code"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 111 | } |
| 112 | } else { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 113 | FAIL() << "Unexpected grant type"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 114 | } |
| 115 | json.SetInteger("expires_in", 3600); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 116 | response->ReplyJson(chromeos::http::status_code::Ok, &json); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 119 | void OAuth2HandlerFail(const ServerRequest& request, |
| 120 | ServerResponse* response) { |
| 121 | base::DictionaryValue json; |
| 122 | EXPECT_EQ("refresh_token", request.GetFormField("grant_type")); |
| 123 | EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token")); |
| 124 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 125 | EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret")); |
| 126 | json.SetString("error", "unable_to_authenticate"); |
| 127 | response->ReplyJson(chromeos::http::status_code::BadRequest, &json); |
| 128 | } |
| 129 | |
| 130 | void OAuth2HandlerDeregister(const ServerRequest& request, |
| 131 | ServerResponse* response) { |
| 132 | base::DictionaryValue json; |
| 133 | EXPECT_EQ("refresh_token", request.GetFormField("grant_type")); |
| 134 | EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token")); |
| 135 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 136 | EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret")); |
| 137 | json.SetString("error", "invalid_grant"); |
| 138 | response->ReplyJson(chromeos::http::status_code::BadRequest, &json); |
| 139 | } |
| 140 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 141 | void DeviceInfoHandler(const ServerRequest& request, ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 142 | std::string auth = "Bearer "; |
| 143 | auth += test_data::kAccessToken; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 144 | EXPECT_EQ(auth, |
| 145 | request.GetHeader(chromeos::http::request_header::kAuthorization)); |
| 146 | response->ReplyJson(chromeos::http::status_code::Ok, { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 147 | {"channel.supportedType", "xmpp"}, |
| 148 | {"deviceKind", "vendor"}, |
| 149 | {"id", test_data::kDeviceId}, |
| 150 | {"kind", "clouddevices#device"}, |
| 151 | }); |
| 152 | } |
| 153 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 154 | void FinalizeTicketHandler(const ServerRequest& request, |
| 155 | ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 156 | EXPECT_EQ(test_data::kApiKey, request.GetFormField("key")); |
| 157 | EXPECT_TRUE(request.GetData().empty()); |
| 158 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 159 | response->ReplyJson(chromeos::http::status_code::Ok, { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 160 | {"id", test_data::kClaimTicketId}, |
| 161 | {"kind", "clouddevices#registrationTicket"}, |
| 162 | {"oauthClientId", test_data::kClientId}, |
| 163 | {"userEmail", "user@email.com"}, |
| 164 | {"deviceDraft.id", test_data::kDeviceId}, |
| 165 | {"deviceDraft.kind", "clouddevices#device"}, |
| 166 | {"deviceDraft.channel.supportedType", "xmpp"}, |
| 167 | {"robotAccountEmail", test_data::kRobotAccountEmail}, |
| 168 | {"robotAccountAuthorizationCode", test_data::kRobotAccountAuthCode}, |
| 169 | }); |
| 170 | } |
| 171 | |
| 172 | } // anonymous namespace |
| 173 | |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 174 | // This is a helper class that allows the unit tests to access private |
| 175 | // methods and data since TestHelper is declared as a friend to |
| 176 | // DeviceRegistrationInfo. |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 177 | class DeviceRegistrationInfo::TestHelper { |
| 178 | public: |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 179 | static bool Save(DeviceRegistrationInfo* info) { |
| 180 | return info->Save(); |
| 181 | } |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | class DeviceRegistrationInfoTest : public ::testing::Test { |
| 185 | protected: |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 186 | void SetUp() override { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 187 | InitDefaultStorage(&data_); |
| 188 | storage_ = std::make_shared<MemStorage>(); |
| 189 | storage_->Save(&data_); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 190 | transport_ = std::make_shared<chromeos::http::fake::Transport>(); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 191 | command_manager_ = std::make_shared<CommandManager>(); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 192 | state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 193 | chromeos::KeyValueStore config_store; |
| 194 | config_store.SetString("client_id", test_data::kClientId); |
| 195 | config_store.SetString("client_secret", test_data::kClientSecret); |
| 196 | config_store.SetString("api_key", test_data::kApiKey); |
| 197 | config_store.SetString("device_kind", "vendor"); |
| 198 | config_store.SetString("name", "coffee_pot"); |
| 199 | config_store.SetString("default_display_name", "Coffee Pot"); |
| 200 | config_store.SetString("default_description", "Easy to clean"); |
| 201 | config_store.SetString("default_location", "Kitchen"); |
| 202 | config_store.SetString("model_id", "AAA"); |
| 203 | config_store.SetString("oauth_url", test_data::kOAuthURL); |
| 204 | config_store.SetString("service_url", test_data::kServiceURL); |
| 205 | std::unique_ptr<BuffetConfig> config{new BuffetConfig}; |
| 206 | config->Load(config_store); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 207 | auto mock_callback = base::Bind( |
| 208 | &DeviceRegistrationInfoTest::OnRegistrationStatusChange, |
| 209 | base::Unretained(this)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 210 | dev_reg_ = std::unique_ptr<DeviceRegistrationInfo>( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 211 | new DeviceRegistrationInfo(command_manager_, state_manager_, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 212 | std::move(config), |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 213 | transport_, storage_, |
| 214 | mock_callback)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 217 | MOCK_METHOD0(OnRegistrationStatusChange, void()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 218 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 219 | base::DictionaryValue data_; |
| 220 | std::shared_ptr<MemStorage> storage_; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 221 | std::shared_ptr<chromeos::http::fake::Transport> transport_; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 222 | std::unique_ptr<DeviceRegistrationInfo> dev_reg_; |
| 223 | std::shared_ptr<CommandManager> command_manager_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 224 | testing::NiceMock<MockStateChangeQueueInterface> mock_state_change_queue_; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 225 | std::shared_ptr<StateManager> state_manager_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | //////////////////////////////////////////////////////////////////////////////// |
| 229 | TEST_F(DeviceRegistrationInfoTest, GetServiceURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 230 | EXPECT_TRUE(dev_reg_->Load()); |
| 231 | EXPECT_EQ(test_data::kServiceURL, dev_reg_->GetServiceURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 232 | std::string url = test_data::kServiceURL; |
| 233 | url += "registrationTickets"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 234 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets")); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 235 | url += "?key="; |
| 236 | url += test_data::kApiKey; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 237 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 238 | {"key", test_data::kApiKey} |
| 239 | })); |
| 240 | url += "&restart=true"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 241 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 242 | {"key", test_data::kApiKey}, |
| 243 | {"restart", "true"}, |
| 244 | })); |
| 245 | } |
| 246 | |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 247 | TEST_F(DeviceRegistrationInfoTest, VerifySave) { |
| 248 | base::DictionaryValue data; |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 249 | data.SetString(storage_keys::kRefreshToken, "d"); |
| 250 | data.SetString(storage_keys::kDeviceId, "e"); |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 251 | data.SetString(storage_keys::kRobotAccount, "h"); |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 252 | data.SetString(storage_keys::kDisplayName, "k"); |
| 253 | data.SetString(storage_keys::kDescription, "l"); |
| 254 | data.SetString(storage_keys::kLocation, "m"); |
| 255 | |
| 256 | storage_->Save(&data); |
| 257 | |
| 258 | // This test isn't really trying to test Load, it is just the easiest |
| 259 | // way to initialize the properties in dev_reg_. |
| 260 | EXPECT_TRUE(dev_reg_->Load()); |
| 261 | |
| 262 | // Clear the storage to get a clean test. |
| 263 | base::DictionaryValue empty; |
| 264 | storage_->Save(&empty); |
| 265 | EXPECT_TRUE(DeviceRegistrationInfo::TestHelper::Save(dev_reg_.get())); |
| 266 | EXPECT_TRUE(storage_->Load()->Equals(&data)); |
| 267 | } |
| 268 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 269 | TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 270 | EXPECT_TRUE(dev_reg_->Load()); |
| 271 | EXPECT_EQ(test_data::kOAuthURL, dev_reg_->GetOAuthURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 272 | std::string url = test_data::kOAuthURL; |
| 273 | url += "auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclouddevices&"; |
| 274 | url += "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"; |
| 275 | url += "response_type=code&"; |
| 276 | url += "client_id="; |
| 277 | url += test_data::kClientId; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 278 | EXPECT_EQ(url, dev_reg_->GetOAuthURL("auth", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 279 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 280 | {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"}, |
| 281 | {"response_type", "code"}, |
| 282 | {"client_id", test_data::kClientId} |
| 283 | })); |
| 284 | } |
| 285 | |
| 286 | TEST_F(DeviceRegistrationInfoTest, CheckRegistration) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 287 | EXPECT_TRUE(dev_reg_->Load()); |
| 288 | EXPECT_FALSE(dev_reg_->CheckRegistration(nullptr)); |
| 289 | EXPECT_EQ(0, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 290 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 291 | SetDefaultDeviceRegistration(&data_); |
| 292 | storage_->Save(&data_); |
| 293 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 294 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 295 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 296 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 297 | base::Bind(OAuth2Handler)); |
| 298 | transport_->ResetRequestCount(); |
| 299 | EXPECT_TRUE(dev_reg_->CheckRegistration(nullptr)); |
| 300 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 303 | TEST_F(DeviceRegistrationInfoTest, CheckAuthenticationFailure) { |
| 304 | SetDefaultDeviceRegistration(&data_); |
| 305 | storage_->Save(&data_); |
| 306 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 307 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 308 | |
| 309 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 310 | chromeos::http::request_type::kPost, |
| 311 | base::Bind(OAuth2HandlerFail)); |
| 312 | transport_->ResetRequestCount(); |
| 313 | chromeos::ErrorPtr error; |
| 314 | EXPECT_FALSE(dev_reg_->CheckRegistration(&error)); |
| 315 | EXPECT_EQ(1, transport_->GetRequestCount()); |
| 316 | EXPECT_TRUE(error->HasError(buffet::kErrorDomainOAuth2, |
| 317 | "unable_to_authenticate")); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 318 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | TEST_F(DeviceRegistrationInfoTest, CheckDeregistration) { |
| 322 | SetDefaultDeviceRegistration(&data_); |
| 323 | storage_->Save(&data_); |
| 324 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 325 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 326 | |
| 327 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 328 | chromeos::http::request_type::kPost, |
| 329 | base::Bind(OAuth2HandlerDeregister)); |
| 330 | transport_->ResetRequestCount(); |
| 331 | chromeos::ErrorPtr error; |
| 332 | EXPECT_FALSE(dev_reg_->CheckRegistration(&error)); |
| 333 | EXPECT_EQ(1, transport_->GetRequestCount()); |
| 334 | EXPECT_TRUE(error->HasError(buffet::kErrorDomainOAuth2, |
| 335 | "invalid_grant")); |
| 336 | EXPECT_EQ(RegistrationStatus::kInvalidCredentials, |
| 337 | dev_reg_->GetRegistrationStatus()); |
| 338 | } |
| 339 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 340 | TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 341 | SetDefaultDeviceRegistration(&data_); |
| 342 | storage_->Save(&data_); |
| 343 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 344 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 345 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 346 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 347 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 348 | transport_->AddHandler(dev_reg_->GetDeviceURL(), |
| 349 | chromeos::http::request_type::kGet, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 350 | base::Bind(DeviceInfoHandler)); |
| 351 | transport_->ResetRequestCount(); |
| 352 | auto device_info = dev_reg_->GetDeviceInfo(nullptr); |
| 353 | EXPECT_EQ(2, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 354 | EXPECT_NE(nullptr, device_info.get()); |
| 355 | base::DictionaryValue* dict = nullptr; |
| 356 | EXPECT_TRUE(device_info->GetAsDictionary(&dict)); |
| 357 | std::string id; |
| 358 | EXPECT_TRUE(dict->GetString("id", &id)); |
| 359 | EXPECT_EQ(test_data::kDeviceId, id); |
| 360 | } |
| 361 | |
| 362 | TEST_F(DeviceRegistrationInfoTest, GetDeviceId) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 363 | SetDefaultDeviceRegistration(&data_); |
| 364 | storage_->Save(&data_); |
| 365 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 366 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 367 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 368 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 369 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 370 | transport_->AddHandler(dev_reg_->GetDeviceURL(), |
| 371 | chromeos::http::request_type::kGet, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 372 | base::Bind(DeviceInfoHandler)); |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 373 | EXPECT_EQ(test_data::kDeviceId, dev_reg_->GetDeviceId()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 376 | TEST_F(DeviceRegistrationInfoTest, RegisterDevice) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 377 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 378 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 379 | auto update_ticket = [](const ServerRequest& request, |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 380 | ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 381 | EXPECT_EQ(test_data::kApiKey, request.GetFormField("key")); |
| 382 | auto json = request.GetDataAsJson(); |
| 383 | EXPECT_NE(nullptr, json.get()); |
| 384 | std::string value; |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 385 | EXPECT_TRUE(json->GetString("id", &value)); |
| 386 | EXPECT_EQ(test_data::kClaimTicketId, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 387 | EXPECT_TRUE(json->GetString("deviceDraft.channel.supportedType", &value)); |
| 388 | EXPECT_EQ("xmpp", value); |
| 389 | EXPECT_TRUE(json->GetString("oauthClientId", &value)); |
| 390 | EXPECT_EQ(test_data::kClientId, value); |
| 391 | EXPECT_TRUE(json->GetString("deviceDraft.deviceKind", &value)); |
| 392 | EXPECT_EQ("vendor", value); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 393 | EXPECT_TRUE(json->GetString("deviceDraft.description", &value)); |
| 394 | EXPECT_EQ("Easy to clean", value); |
| 395 | EXPECT_TRUE(json->GetString("deviceDraft.location", &value)); |
| 396 | EXPECT_EQ("Kitchen", value); |
Vitaly Buka | 6522ab6 | 2015-02-19 10:26:31 -0800 | [diff] [blame] | 397 | EXPECT_TRUE(json->GetString("deviceDraft.modelManifestId", &value)); |
| 398 | EXPECT_EQ("AAA", value); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 399 | EXPECT_TRUE(json->GetString("deviceDraft.displayName", &value)); |
| 400 | EXPECT_EQ("Coffee Pot", value); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 401 | base::DictionaryValue* commandDefs = nullptr; |
| 402 | EXPECT_TRUE(json->GetDictionary("deviceDraft.commandDefs", &commandDefs)); |
| 403 | EXPECT_FALSE(commandDefs->empty()); |
| 404 | EXPECT_EQ("{'base':{'reboot':{'parameters':{" |
| 405 | "'delay':{'minimum':10,'type':'integer'}}}}," |
| 406 | "'robot':{'_jump':{'parameters':{" |
| 407 | "'_height':{'type':'integer'}}}}}", |
| 408 | buffet::unittests::ValueToString(commandDefs)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 409 | |
| 410 | base::DictionaryValue json_resp; |
| 411 | json_resp.SetString("id", test_data::kClaimTicketId); |
| 412 | json_resp.SetString("kind", "clouddevices#registrationTicket"); |
| 413 | json_resp.SetString("oauthClientId", test_data::kClientId); |
| 414 | base::DictionaryValue* device_draft = nullptr; |
| 415 | EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft)); |
| 416 | device_draft = device_draft->DeepCopy(); |
| 417 | device_draft->SetString("id", test_data::kDeviceId); |
| 418 | device_draft->SetString("kind", "clouddevices#device"); |
| 419 | json_resp.Set("deviceDraft", device_draft); |
| 420 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 421 | response->ReplyJson(chromeos::http::status_code::Ok, &json_resp); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 422 | }; |
| 423 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 424 | auto json_base = buffet::unittests::CreateDictionaryValue(R"({ |
| 425 | 'base': { |
| 426 | 'reboot': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 427 | 'parameters': {'delay': 'integer'}, |
| 428 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 429 | }, |
| 430 | 'shutdown': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 431 | 'parameters': {}, |
| 432 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | })"); |
| 436 | EXPECT_TRUE(command_manager_->LoadBaseCommands(*json_base, nullptr)); |
| 437 | auto json_cmds = buffet::unittests::CreateDictionaryValue(R"({ |
| 438 | 'base': { |
| 439 | 'reboot': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 440 | 'parameters': {'delay': {'minimum': 10}}, |
| 441 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 442 | } |
| 443 | }, |
| 444 | 'robot': { |
| 445 | '_jump': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 446 | 'parameters': {'_height': 'integer'}, |
| 447 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | })"); |
| 451 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr)); |
| 452 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 453 | transport_->AddHandler(dev_reg_->GetServiceURL( |
| 454 | std::string("registrationTickets/") + test_data::kClaimTicketId), |
| 455 | chromeos::http::request_type::kPatch, |
| 456 | base::Bind(update_ticket)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 457 | std::string ticket_url = |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 458 | dev_reg_->GetServiceURL("registrationTickets/" + |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 459 | std::string(test_data::kClaimTicketId)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 460 | transport_->AddHandler(ticket_url + "/finalize", |
| 461 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 462 | base::Bind(FinalizeTicketHandler)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 463 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 464 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 465 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 466 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 467 | storage_->reset_save_count(); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 468 | |
| 469 | std::map<std::string, std::string> params; |
| 470 | params["ticket_id"] = test_data::kClaimTicketId; |
| 471 | std::string device_id = dev_reg_->RegisterDevice(params, nullptr); |
| 472 | |
| 473 | EXPECT_EQ(test_data::kDeviceId, device_id); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 474 | EXPECT_EQ(1, |
| 475 | storage_->save_count()); // The device info must have been saved. |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 476 | EXPECT_EQ(3, transport_->GetRequestCount()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 477 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 478 | |
| 479 | // Validate the device info saved to storage... |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 480 | auto storage_data = storage_->Load(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 481 | base::DictionaryValue* dict = nullptr; |
| 482 | EXPECT_TRUE(storage_data->GetAsDictionary(&dict)); |
| 483 | std::string value; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 484 | EXPECT_TRUE(dict->GetString(storage_keys::kDeviceId, &value)); |
| 485 | EXPECT_EQ(test_data::kDeviceId, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 486 | EXPECT_TRUE(dict->GetString(storage_keys::kRefreshToken, &value)); |
| 487 | EXPECT_EQ(test_data::kRefreshToken, value); |
| 488 | EXPECT_TRUE(dict->GetString(storage_keys::kRobotAccount, &value)); |
| 489 | EXPECT_EQ(test_data::kRobotAccountEmail, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 492 | TEST_F(DeviceRegistrationInfoTest, OOBRegistrationStatus) { |
| 493 | // After we've been initialized, we should be either offline or unregistered, |
| 494 | // depending on whether or not we've found credentials. |
| 495 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 496 | EXPECT_EQ(RegistrationStatus::kUnconfigured, |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 497 | dev_reg_->GetRegistrationStatus()); |
| 498 | // Put some credentials into our state, make sure we call that offline. |
| 499 | SetDefaultDeviceRegistration(&data_); |
| 500 | storage_->Save(&data_); |
| 501 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 502 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 505 | } // namespace buffet |