Ai
2 Star 0 Fork 0

chromium_develop/chromium_google_apis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
google_api_keys_unittest.cc 25.97 KB
一键复制 编辑 原始数据 按行查看 历史
李想 提交于 2022-08-16 15:51 +08:00 . chromium origin init
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Unit tests for implementation of google_api_keys namespace.
//
// 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/stl_util.h"
#include "base/test/scoped_command_line.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "google_apis/gaia/gaia_config.h"
#include "google_apis/gaia/gaia_switches.h"
#include "google_apis/google_api_keys.h"
// The Win builders fail (with a linker crash) when trying to link
// unit_tests, and the Android builders complain about multiply
// defined symbols (likely they don't do name decoration as well as
// the Mac and Linux linkers). Therefore these tests are only built
// and run on Mac and Linux, which should provide plenty of coverage
// since there are no platform-specific bits in this code.
#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_APPLE)
// We need to include everything included by google_api_keys.cc once
// at global scope so that things like STL and classes from base don't
// get defined when we re-include the google_api_keys.cc file
// below. We used to include that file in its entirety here, but that
// can cause problems if the linker decides the version of symbols
// from that file included here is the "right" version.
#include <stddef.h>
#include <string>
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/strings/stringize_macros.h"
#if defined(OS_APPLE)
#include "google_apis/google_api_keys_mac.h"
#endif
GoogleAPIKeysTest::GoogleAPIKeysTest() : env_(base::Environment::Create()) {
static_assert(11 == 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_CLOUD_PRINT";
env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT";
env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING";
env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING";
env_cache_[7].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST";
env_cache_[8].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST";
env_cache_[9].variable_name = "GOOGLE_DEFAULT_CLIENT_ID";
env_cache_[10].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 < base::size(env_cache_); ++i) {
EnvironmentCache& cache = env_cache_[i];
cache.was_set = env_->HasVar(cache.variable_name);
cache.value.clear();
if (cache.was_set) {
env_->GetVar(cache.variable_name, &cache.value);
env_->UnSetVar(cache.variable_name);
}
}
}
void GoogleAPIKeysTest::TearDown() {
// Restore environment.
for (size_t i = 0; i < base::size(env_cache_); ++i) {
EnvironmentCache& cache = env_cache_[i];
if (cache.was_set) {
env_->SetVar(cache.variable_name, cache.value);
}
}
}
// This is the default baked-in value for OAuth IDs and secrets.
static const char kDummyToken[] = "dummytoken";
base::FilePath GetTestFilePath(const std::string& relative_path) {
base::FilePath path;
if (!base::PathService::Get(base::DIR_SOURCE_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 google_api_keys.cc
#undef DUMMY_API_TOKEN
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
#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_
#include "google_apis/google_api_keys.cc"
} // namespace official_build
TEST_F(GoogleAPIKeysTest, OfficialKeys) {
namespace testcase = official_build::google_apis;
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
std::string api_key = testcase::g_api_key_cache.Get().api_key();
std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_MAIN);
std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_MAIN);
std::string id_cloud_print =
testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_CLOUD_PRINT);
std::string secret_cloud_print =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_CLOUD_PRINT);
std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING);
std::string secret_remoting =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING);
std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING_HOST);
EXPECT_NE(0u, api_key.size());
EXPECT_NE(DUMMY_API_TOKEN, api_key);
EXPECT_NE("bogus api_key", api_key);
EXPECT_NE(kDummyToken, api_key);
EXPECT_NE(0u, id_main.size());
EXPECT_NE(DUMMY_API_TOKEN, id_main);
EXPECT_NE("bogus client_id_main", id_main);
EXPECT_NE(kDummyToken, id_main);
EXPECT_NE(0u, secret_main.size());
EXPECT_NE(DUMMY_API_TOKEN, secret_main);
EXPECT_NE(kDummyToken, secret_main);
EXPECT_NE(0u, id_cloud_print.size());
EXPECT_NE(DUMMY_API_TOKEN, id_cloud_print);
EXPECT_NE(kDummyToken, id_cloud_print);
EXPECT_NE(0u, secret_cloud_print.size());
EXPECT_NE(DUMMY_API_TOKEN, secret_cloud_print);
EXPECT_NE(kDummyToken, secret_cloud_print);
EXPECT_NE(0u, id_remoting.size());
EXPECT_NE(DUMMY_API_TOKEN, id_remoting);
EXPECT_NE(kDummyToken, id_remoting);
EXPECT_NE(0u, secret_remoting.size());
EXPECT_NE(DUMMY_API_TOKEN, secret_remoting);
EXPECT_NE(kDummyToken, secret_remoting);
EXPECT_NE(0u, id_remoting_host.size());
EXPECT_NE(DUMMY_API_TOKEN, id_remoting_host);
EXPECT_NE(kDummyToken, id_remoting_host);
EXPECT_NE(0u, secret_remoting_host.size());
EXPECT_NE(DUMMY_API_TOKEN, secret_remoting_host);
EXPECT_NE(kDummyToken, secret_remoting_host);
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) ||
// 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 google_api_keys.cc
#undef DUMMY_API_TOKEN
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
#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
// 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_
#include "google_apis/google_api_keys.cc"
} // namespace default_keys
TEST_F(GoogleAPIKeysTest, DefaultKeys) {
namespace testcase = default_keys::google_apis;
EXPECT_FALSE(testcase::HasAPIKeyConfigured());
EXPECT_FALSE(testcase::HasOAuthClientConfigured());
std::string api_key = testcase::g_api_key_cache.Get().api_key();
std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_MAIN);
std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_MAIN);
std::string id_cloud_print =
testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_CLOUD_PRINT);
std::string secret_cloud_print =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_CLOUD_PRINT);
std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING);
std::string secret_remoting =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING);
std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING_HOST);
EXPECT_EQ(kDummyToken, api_key);
EXPECT_EQ(kDummyToken, id_main);
EXPECT_EQ(kDummyToken, secret_main);
EXPECT_EQ(kDummyToken, id_cloud_print);
EXPECT_EQ(kDummyToken, secret_cloud_print);
EXPECT_EQ(kDummyToken, id_remoting);
EXPECT_EQ(kDummyToken, secret_remoting);
EXPECT_EQ(kDummyToken, id_remoting_host);
EXPECT_EQ(kDummyToken, 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 google_api_keys.cc
#undef DUMMY_API_TOKEN
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
#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"
// 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_
#include "google_apis/google_api_keys.cc"
} // namespace override_some_keys
TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) {
namespace testcase = override_some_keys::google_apis;
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
EXPECT_FALSE(testcase::HasOAuthClientConfigured());
std::string api_key = testcase::g_api_key_cache.Get().api_key();
std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_MAIN);
std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_MAIN);
std::string id_cloud_print =
testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_CLOUD_PRINT);
std::string secret_cloud_print =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_CLOUD_PRINT);
std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING);
std::string secret_remoting =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING);
std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING_HOST);
EXPECT_EQ("API_KEY override", api_key);
EXPECT_EQ(kDummyToken, id_main);
EXPECT_EQ(kDummyToken, secret_main);
EXPECT_EQ(kDummyToken, id_cloud_print);
EXPECT_EQ(kDummyToken, secret_cloud_print);
EXPECT_EQ("CLIENT_ID_REMOTING override", id_remoting);
EXPECT_EQ(kDummyToken, secret_remoting);
EXPECT_EQ(kDummyToken, id_remoting_host);
EXPECT_EQ(kDummyToken, 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 google_api_keys.cc
#undef DUMMY_API_TOKEN
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
#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_CLOUD_PRINT "ID_CLOUD_PRINT"
#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
#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"
// 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_
#include "google_apis/google_api_keys.cc"
} // namespace override_all_keys
TEST_F(GoogleAPIKeysTest, OverrideAllKeys) {
namespace testcase = override_all_keys::google_apis;
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
std::string api_key = testcase::g_api_key_cache.Get().api_key();
std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_MAIN);
std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_MAIN);
std::string id_cloud_print =
testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_CLOUD_PRINT);
std::string secret_cloud_print =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_CLOUD_PRINT);
std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING);
std::string secret_remoting =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING);
std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING_HOST);
EXPECT_EQ("API_KEY", api_key);
EXPECT_EQ("ID_MAIN", id_main);
EXPECT_EQ("SECRET_MAIN", secret_main);
EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print);
EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print);
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 google_api_keys.cc
#undef DUMMY_API_TOKEN
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
#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_CLOUD_PRINT "ID_CLOUD_PRINT"
#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
#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"
// 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_
#include "google_apis/google_api_keys.cc"
} // namespace override_all_keys_env
TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) {
namespace testcase = override_all_keys_env::google_apis;
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_CLOUD_PRINT", "env-ID_CLOUD_PRINT");
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_CLOUD_PRINT", "env-SECRET_CLOUD_PRINT");
env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING", "env-SECRET_REMOTING");
env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING_HOST", "env-SECRET_REMOTING_HOST");
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
// It's important that the first call to Get() only happen after the
// environment variables have been set.
std::string api_key = testcase::g_api_key_cache.Get().api_key();
std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_MAIN);
std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_MAIN);
std::string id_cloud_print =
testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_CLOUD_PRINT);
std::string secret_cloud_print =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_CLOUD_PRINT);
std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING);
std::string secret_remoting =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::CLIENT_REMOTING);
std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
testcase::CLIENT_REMOTING_HOST);
std::string secret_remoting_host =
testcase::g_api_key_cache.Get().GetClientSecret(
testcase::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_CLOUD_PRINT", id_cloud_print);
EXPECT_EQ("env-SECRET_CLOUD_PRINT", secret_cloud_print);
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 defined(OS_IOS)
// 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 google_api_keys.cc
#undef DUMMY_API_TOKEN
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
#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_CLOUD_PRINT "ID_CLOUD_PRINT"
#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
#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"
// 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_
#include "google_apis/google_api_keys.cc"
} // namespace override_all_keys_setters
TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingSetters) {
namespace testcase = override_all_keys_setters::google_apis;
std::string api_key("setter-API_KEY");
testcase::SetAPIKey(api_key);
std::string id_main("setter-ID_MAIN");
std::string secret_main("setter-SECRET_MAIN");
testcase::SetOAuth2ClientID(testcase::CLIENT_MAIN, id_main);
testcase::SetOAuth2ClientSecret(testcase::CLIENT_MAIN, secret_main);
std::string id_cloud_print("setter-ID_CLOUD_PRINT");
std::string secret_cloud_print("setter-SECRET_CLOUD_PRINT");
testcase::SetOAuth2ClientID(testcase::CLIENT_CLOUD_PRINT, id_cloud_print);
testcase::SetOAuth2ClientSecret(testcase::CLIENT_CLOUD_PRINT,
secret_cloud_print);
std::string id_remoting("setter-ID_REMOTING");
std::string secret_remoting("setter-SECRET_REMOTING");
testcase::SetOAuth2ClientID(testcase::CLIENT_REMOTING, id_remoting);
testcase::SetOAuth2ClientSecret(testcase::CLIENT_REMOTING, secret_remoting);
std::string id_remoting_host("setter-ID_REMOTING_HOST");
std::string secret_remoting_host("setter-SECRET_REMOTING_HOST");
testcase::SetOAuth2ClientID(testcase::CLIENT_REMOTING_HOST, id_remoting_host);
testcase::SetOAuth2ClientSecret(testcase::CLIENT_REMOTING_HOST,
secret_remoting_host);
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
EXPECT_EQ(api_key, testcase::GetAPIKey());
EXPECT_EQ(id_main, testcase::GetOAuth2ClientID(testcase::CLIENT_MAIN));
EXPECT_EQ(secret_main,
testcase::GetOAuth2ClientSecret(testcase::CLIENT_MAIN));
EXPECT_EQ(id_cloud_print,
testcase::GetOAuth2ClientID(testcase::CLIENT_CLOUD_PRINT));
EXPECT_EQ(secret_cloud_print,
testcase::GetOAuth2ClientSecret(testcase::CLIENT_CLOUD_PRINT));
EXPECT_EQ(id_remoting,
testcase::GetOAuth2ClientID(testcase::CLIENT_REMOTING));
EXPECT_EQ(secret_remoting,
testcase::GetOAuth2ClientSecret(testcase::CLIENT_REMOTING));
EXPECT_EQ(id_remoting_host,
testcase::GetOAuth2ClientID(testcase::CLIENT_REMOTING_HOST));
EXPECT_EQ(secret_remoting_host,
testcase::GetOAuth2ClientSecret(testcase::CLIENT_REMOTING_HOST));
}
#endif // defined(OS_IOS)
// 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 google_api_keys.cc
#undef DUMMY_API_TOKEN
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
#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_CLOUD_PRINT "ID_CLOUD_PRINT"
#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
#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"
// 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_
#include "google_apis/google_api_keys.cc"
} // namespace override_all_keys_config
TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingConfig) {
namespace testcase = override_all_keys_config::google_apis;
auto command_line = std::make_unique<base::test::ScopedCommandLine>();
command_line->GetProcessCommandLine()->AppendSwitchPath(
"gaia-config", GetTestFilePath("api_keys.json"));
GaiaConfig::ResetInstanceForTesting();
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
EXPECT_EQ("config-API_KEY", testcase::GetAPIKey());
EXPECT_EQ("config-ID_MAIN",
testcase::GetOAuth2ClientID(testcase::CLIENT_MAIN));
EXPECT_EQ("config-SECRET_MAIN",
testcase::GetOAuth2ClientSecret(testcase::CLIENT_MAIN));
EXPECT_EQ("config-ID_CLOUD_PRINT",
testcase::GetOAuth2ClientID(testcase::CLIENT_CLOUD_PRINT));
EXPECT_EQ("config-SECRET_CLOUD_PRINT",
testcase::GetOAuth2ClientSecret(testcase::CLIENT_CLOUD_PRINT));
EXPECT_EQ("config-ID_REMOTING",
testcase::GetOAuth2ClientID(testcase::CLIENT_REMOTING));
EXPECT_EQ("config-SECRET_REMOTING",
testcase::GetOAuth2ClientSecret(testcase::CLIENT_REMOTING));
EXPECT_EQ("config-ID_REMOTING_HOST",
testcase::GetOAuth2ClientID(testcase::CLIENT_REMOTING_HOST));
EXPECT_EQ("config-SECRET_REMOTING_HOST",
testcase::GetOAuth2ClientSecret(testcase::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();
}
#endif // defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_APPLE)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chromium_develop/chromium_google_apis.git
git@gitee.com:chromium_develop/chromium_google_apis.git
chromium_develop
chromium_google_apis
chromium_google_apis
master

搜索帮助