|
|
|
@ -7,7 +7,7 @@
|
|
|
|
|
#pragma allow_unsafe_buffers
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Unit tests for implementation of google_api_keys namespace.
|
|
|
|
|
// 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
|
|
|
|
@ -30,35 +30,8 @@
|
|
|
|
|
#include "google_apis/gaia/gaia_switches.h"
|
|
|
|
|
#include "google_apis/google_api_keys.h"
|
|
|
|
|
#include "google_apis/google_api_keys_utils.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). Building and
|
|
|
|
|
// running on other platforms should provide plenty of coverage since there are
|
|
|
|
|
// no platform-specific bits in this code.
|
|
|
|
|
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_WIN)
|
|
|
|
|
|
|
|
|
|
// 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/logging.h"
|
|
|
|
|
#include "base/no_destructor.h"
|
|
|
|
|
#include "base/strings/stringize_macros.h"
|
|
|
|
|
#include "base/version_info/channel.h"
|
|
|
|
|
|
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
|
|
|
|
#include "google_apis/google_api_keys_mac.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
GoogleAPIKeysTest::GoogleAPIKeysTest() : env_(base::Environment::Create()) {
|
|
|
|
|
static_assert(9 == 3 + 2 * google_apis::CLIENT_NUM_ITEMS,
|
|
|
|
|
"Unexpected number of key entries.");
|
|
|
|
@ -120,7 +93,7 @@ base::FilePath GetTestFilePath(const std::string& relative_path) {
|
|
|
|
|
namespace official_build {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -139,28 +112,37 @@ namespace official_build {
|
|
|
|
|
// 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-inc.cc"
|
|
|
|
|
#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) {
|
|
|
|
|
namespace testcase = official_build::google_apis;
|
|
|
|
|
google_apis::ApiKeyCache api_key_cache(
|
|
|
|
|
official_build::GetDefaultApiKeysFromDefinedValues());
|
|
|
|
|
auto scoped_override =
|
|
|
|
|
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
|
|
|
|
|
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
|
|
|
|
|
|
|
|
|
|
std::string api_key = testcase::GetAPIKey();
|
|
|
|
|
std::string id_main = testcase::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string api_key = google_apis::GetAPIKey();
|
|
|
|
|
std::string id_main =
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string secret_main =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string id_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string secret_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string id_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
std::string secret_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
|
|
|
|
|
EXPECT_NE(0u, api_key.size());
|
|
|
|
|
EXPECT_NE(DUMMY_API_TOKEN, api_key);
|
|
|
|
@ -192,8 +174,7 @@ TEST_F(GoogleAPIKeysTest, OfficialKeys) {
|
|
|
|
|
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)
|
|
|
|
|
#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.
|
|
|
|
@ -207,7 +188,7 @@ TEST_F(GoogleAPIKeysTest, OfficialKeys) {
|
|
|
|
|
namespace default_keys {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -218,32 +199,32 @@ namespace default_keys {
|
|
|
|
|
#undef GOOGLE_DEFAULT_CLIENT_ID
|
|
|
|
|
#undef GOOGLE_DEFAULT_CLIENT_SECRET
|
|
|
|
|
|
|
|
|
|
// Undef include guard so things get defined again, within this namespace.
|
|
|
|
|
#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
|
|
|
|
|
#undef GOOGLE_APIS_INTERNAL_METRICS_SIGNING_KEY_H_
|
|
|
|
|
#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
|
|
|
|
|
#include "google_apis/google_api_keys-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.cc"
|
|
|
|
|
|
|
|
|
|
} // namespace default_keys
|
|
|
|
|
|
|
|
|
|
TEST_F(GoogleAPIKeysTest, DefaultKeys) {
|
|
|
|
|
namespace testcase = default_keys::google_apis;
|
|
|
|
|
google_apis::ApiKeyCache api_key_cache(
|
|
|
|
|
default_keys::GetDefaultApiKeysFromDefinedValues());
|
|
|
|
|
auto scoped_override =
|
|
|
|
|
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
|
|
|
|
|
|
|
|
|
|
EXPECT_FALSE(testcase::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_FALSE(testcase::HasOAuthClientConfigured());
|
|
|
|
|
EXPECT_FALSE(google_apis::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_FALSE(google_apis::HasOAuthClientConfigured());
|
|
|
|
|
|
|
|
|
|
std::string api_key = testcase::GetAPIKey();
|
|
|
|
|
std::string id_main = testcase::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string api_key = google_apis::GetAPIKey();
|
|
|
|
|
std::string id_main =
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string secret_main =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string id_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string secret_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string id_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
std::string secret_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(kDummyToken, api_key);
|
|
|
|
|
EXPECT_EQ(kDummyToken, id_main);
|
|
|
|
@ -258,7 +239,7 @@ TEST_F(GoogleAPIKeysTest, DefaultKeys) {
|
|
|
|
|
namespace override_some_keys {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -272,31 +253,32 @@ namespace override_some_keys {
|
|
|
|
|
#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-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.cc"
|
|
|
|
|
|
|
|
|
|
} // namespace override_some_keys
|
|
|
|
|
|
|
|
|
|
TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) {
|
|
|
|
|
namespace testcase = override_some_keys::google_apis;
|
|
|
|
|
google_apis::ApiKeyCache api_key_cache(
|
|
|
|
|
override_some_keys::GetDefaultApiKeysFromDefinedValues());
|
|
|
|
|
auto scoped_override =
|
|
|
|
|
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_FALSE(testcase::HasOAuthClientConfigured());
|
|
|
|
|
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_FALSE(google_apis::HasOAuthClientConfigured());
|
|
|
|
|
|
|
|
|
|
std::string api_key = testcase::GetAPIKey();
|
|
|
|
|
std::string id_main = testcase::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string api_key = google_apis::GetAPIKey();
|
|
|
|
|
std::string id_main =
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string secret_main =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string id_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string secret_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string id_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
std::string secret_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("API_KEY override", api_key);
|
|
|
|
|
EXPECT_EQ(kDummyToken, id_main);
|
|
|
|
@ -311,7 +293,7 @@ TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) {
|
|
|
|
|
namespace override_all_keys {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -330,31 +312,32 @@ namespace override_all_keys {
|
|
|
|
|
#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-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.cc"
|
|
|
|
|
|
|
|
|
|
} // namespace override_all_keys
|
|
|
|
|
|
|
|
|
|
TEST_F(GoogleAPIKeysTest, OverrideAllKeys) {
|
|
|
|
|
namespace testcase = override_all_keys::google_apis;
|
|
|
|
|
google_apis::ApiKeyCache api_key_cache(
|
|
|
|
|
override_all_keys::GetDefaultApiKeysFromDefinedValues());
|
|
|
|
|
auto scoped_override =
|
|
|
|
|
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
|
|
|
|
|
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
|
|
|
|
|
|
|
|
|
|
std::string api_key = testcase::GetAPIKey();
|
|
|
|
|
std::string id_main = testcase::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string api_key = google_apis::GetAPIKey();
|
|
|
|
|
std::string id_main =
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string secret_main =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string id_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string secret_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string id_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
std::string secret_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("API_KEY", api_key);
|
|
|
|
|
EXPECT_EQ("ID_MAIN", id_main);
|
|
|
|
@ -369,7 +352,7 @@ TEST_F(GoogleAPIKeysTest, OverrideAllKeys) {
|
|
|
|
|
namespace override_api_key_via_feature_without_param {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -382,42 +365,32 @@ namespace override_api_key_via_feature_without_param {
|
|
|
|
|
|
|
|
|
|
#define GOOGLE_API_KEY "API_KEY"
|
|
|
|
|
|
|
|
|
|
// 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-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.cc"
|
|
|
|
|
|
|
|
|
|
} // namespace override_api_key_via_feature_without_param
|
|
|
|
|
|
|
|
|
|
TEST_F(GoogleAPIKeysTest, OverrideApiKeyViaFeatureWithNoParamIsIgnored) {
|
|
|
|
|
namespace testcase = override_api_key_via_feature_without_param::google_apis;
|
|
|
|
|
|
|
|
|
|
static int test_iteration = 0;
|
|
|
|
|
test_iteration++;
|
|
|
|
|
|
|
|
|
|
// Use base::test::ScopedFeatureList::InitFromCommandLine() to enable the
|
|
|
|
|
// feature to avoid exposing the feature flag in the header file and exposing
|
|
|
|
|
// it as a component exported symbol.
|
|
|
|
|
base::test::ScopedFeatureList scoped_feature_list;
|
|
|
|
|
scoped_feature_list.InitFromCommandLine("OverrideAPIKey", "");
|
|
|
|
|
scoped_feature_list.InitAndEnableFeature(google_apis::kOverrideAPIKeyFeature);
|
|
|
|
|
|
|
|
|
|
base::HistogramTester tester;
|
|
|
|
|
EXPECT_EQ("API_KEY", testcase::GetAPIKey());
|
|
|
|
|
|
|
|
|
|
// |g_api_key_cache| is loaded only once, so the histogram is only logged
|
|
|
|
|
// during the first iteration of the test.
|
|
|
|
|
if (test_iteration == 1) {
|
|
|
|
|
tester.ExpectUniqueSample("Signin.APIKeyMatchesFeatureOnStartup", 0, 1);
|
|
|
|
|
} else {
|
|
|
|
|
tester.ExpectUniqueSample("Signin.APIKeyMatchesFeatureOnStartup", 0, 0);
|
|
|
|
|
}
|
|
|
|
|
google_apis::ApiKeyCache api_key_cache(
|
|
|
|
|
override_api_key_via_feature_without_param::
|
|
|
|
|
GetDefaultApiKeysFromDefinedValues());
|
|
|
|
|
auto scoped_override =
|
|
|
|
|
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("API_KEY", google_apis::GetAPIKey());
|
|
|
|
|
|
|
|
|
|
tester.ExpectUniqueSample("Signin.APIKeyMatchesFeatureOnStartup", 0, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Override API key via an experiment feature.
|
|
|
|
|
namespace override_api_key_via_feature {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -430,34 +403,24 @@ namespace override_api_key_via_feature {
|
|
|
|
|
|
|
|
|
|
#define GOOGLE_API_KEY "API_KEY"
|
|
|
|
|
|
|
|
|
|
// 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-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.cc"
|
|
|
|
|
|
|
|
|
|
} // namespace override_api_key_via_feature
|
|
|
|
|
|
|
|
|
|
TEST_F(GoogleAPIKeysTest, OverrideApiKeyViaFeature) {
|
|
|
|
|
namespace testcase = override_api_key_via_feature::google_apis;
|
|
|
|
|
static int test_iteration = 0;
|
|
|
|
|
test_iteration++;
|
|
|
|
|
|
|
|
|
|
// Use base::test::ScopedFeatureList::InitFromCommandLine() to enable the
|
|
|
|
|
// feature to avoid exposing the feature flag in the header file and exposing
|
|
|
|
|
// it as a component exported symbol.
|
|
|
|
|
base::test::ScopedFeatureList scoped_feature_list;
|
|
|
|
|
scoped_feature_list.InitFromCommandLine(
|
|
|
|
|
"OverrideAPIKey<foo.bar:api_key/API_KEY2", "");
|
|
|
|
|
|
|
|
|
|
scoped_feature_list.InitAndEnableFeatureWithParameters(
|
|
|
|
|
google_apis::kOverrideAPIKeyFeature, {{"api_key", "API_KEY2"}});
|
|
|
|
|
base::HistogramTester tester;
|
|
|
|
|
EXPECT_EQ("API_KEY2", testcase::GetAPIKey());
|
|
|
|
|
// |g_api_key_cache| is loaded only once, so the histogram is only logged
|
|
|
|
|
// during the first iteration of the test.
|
|
|
|
|
if (test_iteration == 1) {
|
|
|
|
|
tester.ExpectUniqueSample("Signin.APIKeyMatchesFeatureOnStartup", 1, 1);
|
|
|
|
|
} else {
|
|
|
|
|
tester.ExpectUniqueSample("Signin.APIKeyMatchesFeatureOnStartup", 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
google_apis::ApiKeyCache api_key_cache(
|
|
|
|
|
override_api_key_via_feature::GetDefaultApiKeysFromDefinedValues());
|
|
|
|
|
auto scoped_override =
|
|
|
|
|
google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("API_KEY2", google_apis::GetAPIKey());
|
|
|
|
|
|
|
|
|
|
tester.ExpectUniqueSample("Signin.APIKeyMatchesFeatureOnStartup", 1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
|
|
|
|
@ -467,7 +430,7 @@ TEST_F(GoogleAPIKeysTest, OverrideApiKeyViaFeature) {
|
|
|
|
|
namespace override_all_keys_env {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -486,16 +449,11 @@ namespace override_all_keys_env {
|
|
|
|
|
#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-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.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");
|
|
|
|
@ -505,23 +463,29 @@ TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) {
|
|
|
|
|
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());
|
|
|
|
|
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 = testcase::GetAPIKey();
|
|
|
|
|
std::string id_main = testcase::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string api_key = google_apis::GetAPIKey();
|
|
|
|
|
std::string id_main =
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string secret_main =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
|
|
|
|
|
std::string id_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string secret_remoting =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
|
|
|
|
|
std::string id_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
std::string secret_remoting_host =
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ("env-API_KEY", api_key);
|
|
|
|
|
EXPECT_EQ("env-ID_MAIN", id_main);
|
|
|
|
@ -540,7 +504,7 @@ TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) {
|
|
|
|
|
namespace override_all_keys_setters {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -559,56 +523,56 @@ namespace override_all_keys_setters {
|
|
|
|
|
#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-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.cc"
|
|
|
|
|
|
|
|
|
|
} // namespace override_all_keys_setters
|
|
|
|
|
|
|
|
|
|
TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingSetters) {
|
|
|
|
|
namespace testcase = override_all_keys_setters::google_apis;
|
|
|
|
|
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");
|
|
|
|
|
testcase::SetAPIKey(api_key);
|
|
|
|
|
google_apis::SetAPIKey(api_key);
|
|
|
|
|
|
|
|
|
|
std::string id_main("setter-ID_MAIN");
|
|
|
|
|
std::string secret_main("setter-SECRET_MAIN");
|
|
|
|
|
testcase::SetOAuth2ClientID(google_apis::CLIENT_MAIN, id_main);
|
|
|
|
|
testcase::SetOAuth2ClientSecret(google_apis::CLIENT_MAIN, secret_main);
|
|
|
|
|
google_apis::SetOAuth2ClientID(google_apis::CLIENT_MAIN, id_main);
|
|
|
|
|
google_apis::SetOAuth2ClientSecret(google_apis::CLIENT_MAIN, secret_main);
|
|
|
|
|
|
|
|
|
|
std::string id_remoting("setter-ID_REMOTING");
|
|
|
|
|
std::string secret_remoting("setter-SECRET_REMOTING");
|
|
|
|
|
testcase::SetOAuth2ClientID(google_apis::CLIENT_REMOTING, id_remoting);
|
|
|
|
|
testcase::SetOAuth2ClientSecret(google_apis::CLIENT_REMOTING,
|
|
|
|
|
secret_remoting);
|
|
|
|
|
google_apis::SetOAuth2ClientID(google_apis::CLIENT_REMOTING, id_remoting);
|
|
|
|
|
google_apis::SetOAuth2ClientSecret(google_apis::CLIENT_REMOTING,
|
|
|
|
|
secret_remoting);
|
|
|
|
|
|
|
|
|
|
std::string id_remoting_host("setter-ID_REMOTING_HOST");
|
|
|
|
|
std::string secret_remoting_host("setter-SECRET_REMOTING_HOST");
|
|
|
|
|
testcase::SetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST,
|
|
|
|
|
id_remoting_host);
|
|
|
|
|
testcase::SetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST,
|
|
|
|
|
secret_remoting_host);
|
|
|
|
|
google_apis::SetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST,
|
|
|
|
|
id_remoting_host);
|
|
|
|
|
google_apis::SetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST,
|
|
|
|
|
secret_remoting_host);
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(testcase::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_TRUE(testcase::HasOAuthClientConfigured());
|
|
|
|
|
EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
|
|
|
|
|
EXPECT_TRUE(google_apis::HasOAuthClientConfigured());
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(api_key, testcase::GetAPIKey(::version_info::Channel::STABLE));
|
|
|
|
|
EXPECT_EQ(api_key, testcase::GetAPIKey());
|
|
|
|
|
EXPECT_EQ(api_key, google_apis::GetAPIKey(::version_info::Channel::STABLE));
|
|
|
|
|
EXPECT_EQ(api_key, google_apis::GetAPIKey());
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(id_main, testcase::GetOAuth2ClientID(google_apis::CLIENT_MAIN));
|
|
|
|
|
EXPECT_EQ(id_main, google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN));
|
|
|
|
|
EXPECT_EQ(secret_main,
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN));
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(id_remoting,
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING));
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING));
|
|
|
|
|
EXPECT_EQ(secret_remoting,
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING));
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(id_remoting_host,
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST));
|
|
|
|
|
EXPECT_EQ(secret_remoting_host,
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING_HOST));
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST));
|
|
|
|
|
EXPECT_EQ(secret_remoting_host, google_apis::GetOAuth2ClientSecret(
|
|
|
|
|
google_apis::CLIENT_REMOTING_HOST));
|
|
|
|
|
}
|
|
|
|
|
#endif // BUILDFLAG(IS_IOS)
|
|
|
|
|
|
|
|
|
@ -617,7 +581,7 @@ TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingSetters) {
|
|
|
|
|
namespace override_all_keys_config {
|
|
|
|
|
|
|
|
|
|
// We start every test by creating a clean environment for the
|
|
|
|
|
// preprocessor defines used in google_api_keys.cc
|
|
|
|
|
// 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
|
|
|
|
@ -636,44 +600,43 @@ namespace override_all_keys_config {
|
|
|
|
|
#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-inc.cc"
|
|
|
|
|
#include "google_apis/default_api_keys-inc.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());
|
|
|
|
|
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",
|
|
|
|
|
testcase::GetAPIKey(::version_info::Channel::STABLE));
|
|
|
|
|
EXPECT_EQ("config-API_KEY", testcase::GetAPIKey());
|
|
|
|
|
google_apis::GetAPIKey(version_info::Channel::STABLE));
|
|
|
|
|
EXPECT_EQ("config-API_KEY", google_apis::GetAPIKey());
|
|
|
|
|
EXPECT_EQ("config-ID_MAIN",
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_MAIN));
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN));
|
|
|
|
|
EXPECT_EQ("config-SECRET_MAIN",
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN));
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN));
|
|
|
|
|
EXPECT_EQ("config-ID_REMOTING",
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING));
|
|
|
|
|
google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING));
|
|
|
|
|
EXPECT_EQ("config-SECRET_REMOTING",
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING));
|
|
|
|
|
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING));
|
|
|
|
|
EXPECT_EQ("config-ID_REMOTING_HOST",
|
|
|
|
|
testcase::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST));
|
|
|
|
|
EXPECT_EQ("config-SECRET_REMOTING_HOST",
|
|
|
|
|
testcase::GetOAuth2ClientSecret(google_apis::CLIENT_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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_WIN)
|
|
|
|
|