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