google_api_keys_unittest.cc 22 KB

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