1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Unit tests for functions in google_apis/google_api_keys.h.
//
// Because the file deals with a lot of preprocessor defines and
// optionally includes an internal header, the way we test is by
// including the .cc file multiple times with different defines set.
// This is a little unorthodox, but it lets us test the behavior as
// close to unmodified as possible.
#include "google_apis/google_api_keys_unittest.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h"
#include "base/version_info/channel.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "google_apis/api_key_cache.h"
#include "google_apis/default_api_keys.h"
#include "google_apis/gaia/gaia_config.h"
#include "google_apis/gaia/gaia_switches.h"
#include "google_apis/google_api_keys.h"
GoogleAPIKeysTest::GoogleAPIKeysTest() : env_(base::Environment::Create()) {
static_assert(9 == 3 + 2 * google_apis::CLIENT_NUM_ITEMS,
"Unexpected number of key entries.");
env_cache_[0].variable_name = "GOOGLE_API_KEY";
env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN";
env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN";
env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_REMOTING";
env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING";
env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST";
env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST";
env_cache_[7].variable_name = "GOOGLE_DEFAULT_CLIENT_ID";
env_cache_[8].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET";
}
GoogleAPIKeysTest::~GoogleAPIKeysTest() {}
void GoogleAPIKeysTest::SetUp() {
// Unset all environment variables that can affect these tests,
// for the duration of the tests.
for (size_t i = 0; i < std::size(env_cache_); ++i) {
EnvironmentCache& cache = env_cache_[i];
cache.was_set = env_->HasVar(cache.variable_name);
cache.value.clear();
if (cache.was_set) {
cache.value = env_->GetVar(cache.variable_name).value();
env_->UnSetVar(cache.variable_name);
}
}
}
void GoogleAPIKeysTest::TearDown() {
// Restore environment.
for (size_t i = 0; i < std::size(env_cache_); ++i) {
EnvironmentCache& cache = env_cache_[i];
if (cache.was_set) {
env_->SetVar(cache.variable_name, cache.value);
}
}
}
base::FilePath GetTestFilePath(const std::string& relative_path) {
base::FilePath path;
if (!base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path)) {
return base::FilePath();
}
return path.AppendASCII("google_apis")
.AppendASCII("test")
.AppendASCII("data")
.AppendASCII("gaia")
.AppendASCII(relative_path);
}
#if defined(USE_OFFICIAL_GOOGLE_API_KEYS)
// Test official build behavior, since we are in a checkout where this
// is possible.
namespace official_build {
// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_REMOTING
#undef GOOGLE_CLIENT_SECRET_REMOTING
#undef GOOGLE_CLIENT_ID_REMOTING_HOST
#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
#undef GOOGLE_DEFAULT_CLIENT_ID
#undef GOOGLE_DEFAULT_CLIENT_SECRET
// Try setting some keys, these should be ignored since it's a build
// with official keys.
#define GOOGLE_API_KEY "bogus api_key"
#define GOOGLE_CLIENT_ID_MAIN "bogus client_id_main"
// Undef include guard so things get defined again, within this namespace.
#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
#undef GOOGLE_APIS_INTERNAL_METRICS_SIGNING_KEY_H_
#include "google_apis/internal/google_chrome_api_keys.h"
#include "google_apis/internal/metrics_signing_key.h"
// This file must be included after the internal files defining official keys.
#include "google_apis/default_api_keys-inc.cc"
} // namespace official_build
TEST_F(GoogleAPIKeysTest, OfficialKeys) {
google_apis::ApiKeyCache api_key_cache(
official_build::GetDefaultApiKeysFromDefinedValues());
auto scoped_override =
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
std::string api_key = google_apis::GetAPIKey();
std::string id_main =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
std::string secret_main =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
std::string id_remoting =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
std::string secret_remoting =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
std::string id_remoting_host =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
EXPECT_NE(0u, api_key.size());
EXPECT_NE(google_apis::DefaultApiKeys::kUnsetApiToken, api_key);
EXPECT_NE("bogus api_key", api_key);
EXPECT_NE(0u, id_main.size());
EXPECT_NE(google_apis::DefaultApiKeys::kUnsetApiToken, id_main);
EXPECT_NE("bogus client_id_main", id_main);
EXPECT_NE(0u, secret_main.size());
EXPECT_NE(google_apis::DefaultApiKeys::kUnsetApiToken, secret_main);
EXPECT_NE(0u, id_remoting.size());
EXPECT_NE(google_apis::DefaultApiKeys::kUnsetApiToken, id_remoting);
EXPECT_NE(0u, secret_remoting.size());
EXPECT_NE(google_apis::DefaultApiKeys::kUnsetApiToken, secret_remoting);
EXPECT_NE(0u, id_remoting_host.size());
EXPECT_NE(google_apis::DefaultApiKeys::kUnsetApiToken, id_remoting_host);
EXPECT_NE(0u, secret_remoting_host.size());
EXPECT_NE(google_apis::DefaultApiKeys::kUnsetApiToken, secret_remoting_host);
}
#endif // defined(USE_OFFICIAL_GOOGLE_API_KEYS)
// After this test, for the remainder of this compilation unit, we
// need official keys to not be used.
#undef BUILDFLAG_INTERNAL_CHROMIUM_BRANDING
#undef BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING
#define BUILDFLAG_INTERNAL_CHROMIUM_BRANDING() (1)
#define BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING() (0)
#undef USE_OFFICIAL_GOOGLE_API_KEYS
// Test the set of keys temporarily baked into Chromium by default.
namespace default_keys {
// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_REMOTING
#undef GOOGLE_CLIENT_SECRET_REMOTING
#undef GOOGLE_CLIENT_ID_REMOTING_HOST
#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
#undef GOOGLE_DEFAULT_CLIENT_ID
#undef GOOGLE_DEFAULT_CLIENT_SECRET
#include "google_apis/default_api_keys-inc.cc"
} // namespace default_keys
TEST_F(GoogleAPIKeysTest, DefaultKeys) {
google_apis::ApiKeyCache api_key_cache(
default_keys::GetDefaultApiKeysFromDefinedValues());
auto scoped_override =
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
EXPECT_FALSE(google_apis::HasAPIKeyConfigured());
EXPECT_FALSE(google_apis::HasOAuthClientConfigured());
std::string api_key = google_apis::GetAPIKey();
std::string id_main =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
std::string secret_main =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
std::string id_remoting =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
std::string secret_remoting =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
std::string id_remoting_host =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, api_key);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, id_main);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, secret_main);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, id_remoting);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, secret_remoting);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, id_remoting_host);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, secret_remoting_host);
}
// Override a couple of keys, leave the rest default.
namespace override_some_keys {
// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_REMOTING
#undef GOOGLE_CLIENT_SECRET_REMOTING
#undef GOOGLE_CLIENT_ID_REMOTING_HOST
#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
#undef GOOGLE_DEFAULT_CLIENT_ID
#undef GOOGLE_DEFAULT_CLIENT_SECRET
#define GOOGLE_API_KEY "API_KEY override"
#define GOOGLE_CLIENT_ID_REMOTING "CLIENT_ID_REMOTING override"
#include "google_apis/default_api_keys-inc.cc"
} // namespace override_some_keys
TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) {
google_apis::ApiKeyCache api_key_cache(
override_some_keys::GetDefaultApiKeysFromDefinedValues());
auto scoped_override =
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
EXPECT_FALSE(google_apis::HasOAuthClientConfigured());
std::string api_key = google_apis::GetAPIKey();
std::string id_main =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
std::string secret_main =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
std::string id_remoting =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
std::string secret_remoting =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
std::string id_remoting_host =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
EXPECT_EQ("API_KEY override", api_key);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, id_main);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, secret_main);
EXPECT_EQ("CLIENT_ID_REMOTING override", id_remoting);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, secret_remoting);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, id_remoting_host);
EXPECT_EQ(google_apis::DefaultApiKeys::kUnsetApiToken, secret_remoting_host);
}
// Override all keys.
namespace override_all_keys {
// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_REMOTING
#undef GOOGLE_CLIENT_SECRET_REMOTING
#undef GOOGLE_CLIENT_ID_REMOTING_HOST
#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
#undef GOOGLE_DEFAULT_CLIENT_ID
#undef GOOGLE_DEFAULT_CLIENT_SECRET
#define GOOGLE_API_KEY "API_KEY"
#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
#include "google_apis/default_api_keys-inc.cc"
} // namespace override_all_keys
TEST_F(GoogleAPIKeysTest, OverrideAllKeys) {
google_apis::ApiKeyCache api_key_cache(
override_all_keys::GetDefaultApiKeysFromDefinedValues());
auto scoped_override =
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
std::string api_key = google_apis::GetAPIKey();
std::string id_main =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
std::string secret_main =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
std::string id_remoting =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
std::string secret_remoting =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
std::string id_remoting_host =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
EXPECT_EQ("API_KEY", api_key);
EXPECT_EQ("ID_MAIN", id_main);
EXPECT_EQ("SECRET_MAIN", secret_main);
EXPECT_EQ("ID_REMOTING", id_remoting);
EXPECT_EQ("SECRET_REMOTING", secret_remoting);
EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host);
EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host);
}
#if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Override all keys using both preprocessor defines and environment
// variables. The environment variables should win.
namespace override_all_keys_env {
// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_REMOTING
#undef GOOGLE_CLIENT_SECRET_REMOTING
#undef GOOGLE_CLIENT_ID_REMOTING_HOST
#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
#undef GOOGLE_DEFAULT_CLIENT_ID
#undef GOOGLE_DEFAULT_CLIENT_SECRET
#define GOOGLE_API_KEY "API_KEY"
#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
#include "google_apis/default_api_keys-inc.cc"
} // namespace override_all_keys_env
TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
env->SetVar("GOOGLE_API_KEY", "env-API_KEY");
env->SetVar("GOOGLE_CLIENT_ID_MAIN", "env-ID_MAIN");
env->SetVar("GOOGLE_CLIENT_ID_REMOTING", "env-ID_REMOTING");
env->SetVar("GOOGLE_CLIENT_ID_REMOTING_HOST", "env-ID_REMOTING_HOST");
env->SetVar("GOOGLE_CLIENT_SECRET_MAIN", "env-SECRET_MAIN");
env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING", "env-SECRET_REMOTING");
env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING_HOST", "env-SECRET_REMOTING_HOST");
google_apis::ApiKeyCache api_key_cache(
override_all_keys_env::GetDefaultApiKeysFromDefinedValues());
auto scoped_override =
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
// It's important that the first call to Get() only happen after the
// environment variables have been set.
std::string api_key = google_apis::GetAPIKey();
std::string id_main =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
std::string secret_main =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
std::string id_remoting =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
std::string secret_remoting =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
std::string id_remoting_host =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
EXPECT_EQ("env-API_KEY", api_key);
EXPECT_EQ("env-ID_MAIN", id_main);
EXPECT_EQ("env-SECRET_MAIN", secret_main);
EXPECT_EQ("env-ID_REMOTING", id_remoting);
EXPECT_EQ("env-SECRET_REMOTING", secret_remoting);
EXPECT_EQ("env-ID_REMOTING_HOST", id_remoting_host);
EXPECT_EQ("env-SECRET_REMOTING_HOST", secret_remoting_host);
}
#endif // !BUILDFLAG(GOOGLE_CHROME_BRANDING)
#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
// Override all keys using both preprocessor defines and setters.
// Setters should win.
namespace override_all_keys_setters {
// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_REMOTING
#undef GOOGLE_CLIENT_SECRET_REMOTING
#undef GOOGLE_CLIENT_ID_REMOTING_HOST
#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
#undef GOOGLE_DEFAULT_CLIENT_ID
#undef GOOGLE_DEFAULT_CLIENT_SECRET
#define GOOGLE_API_KEY "API_KEY"
#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
#include "google_apis/default_api_keys-inc.cc"
} // namespace override_all_keys_setters
TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingSetters) {
google_apis::ApiKeyCache api_key_cache(
override_all_keys_setters::GetDefaultApiKeysFromDefinedValues());
auto scoped_override =
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
std::string api_key("setter-API_KEY");
std::string client_id("setter-CLIENT_ID");
std::string client_secret("setter-CLIENT_SECRET");
google_apis::InitializeAndOverrideAPIKeyAndOAuthClient(api_key, client_id,
client_secret);
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
EXPECT_EQ(api_key, google_apis::GetAPIKey(::version_info::Channel::STABLE));
EXPECT_EQ(api_key, google_apis::GetAPIKey());
EXPECT_EQ(client_id,
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN));
EXPECT_EQ(client_secret,
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN));
EXPECT_EQ(client_id,
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING));
EXPECT_EQ(client_secret,
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING));
EXPECT_EQ(client_id,
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST));
EXPECT_EQ(client_secret, google_apis::GetOAuth2ClientSecret(
google_apis::CLIENT_REMOTING_HOST));
}
#endif // BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
// Override all keys using both preprocessor defines and gaia config.
// Config should win.
namespace override_all_keys_config {
// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_REMOTING
#undef GOOGLE_CLIENT_SECRET_REMOTING
#undef GOOGLE_CLIENT_ID_REMOTING_HOST
#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
#undef GOOGLE_DEFAULT_CLIENT_ID
#undef GOOGLE_DEFAULT_CLIENT_SECRET
#define GOOGLE_API_KEY "API_KEY"
#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
#include "google_apis/default_api_keys-inc.cc"
} // namespace override_all_keys_config
TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingConfig) {
auto command_line = std::make_unique<base::test::ScopedCommandLine>();
command_line->GetProcessCommandLine()->AppendSwitchPath(
"gaia-config", GetTestFilePath("api_keys.json"));
GaiaConfig::ResetInstanceForTesting();
google_apis::ApiKeyCache api_key_cache(
override_all_keys_config::GetDefaultApiKeysFromDefinedValues());
auto scoped_override =
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
EXPECT_EQ("config-API_KEY",
google_apis::GetAPIKey(version_info::Channel::STABLE));
EXPECT_EQ("config-API_KEY", google_apis::GetAPIKey());
EXPECT_EQ("config-ID_MAIN",
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN));
EXPECT_EQ("config-SECRET_MAIN",
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN));
EXPECT_EQ("config-ID_REMOTING",
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING));
EXPECT_EQ("config-SECRET_REMOTING",
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING));
EXPECT_EQ("config-ID_REMOTING_HOST",
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST));
EXPECT_EQ(
"config-SECRET_REMOTING_HOST",
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST));
// It's important to reset the global config state for other tests running in
// the same process.
command_line.reset();
GaiaConfig::ResetInstanceForTesting();
}
|