blob: 1b0515ffa6949bf6f3c50d1962c1eeeb447246b7 [file] [log] [blame]
[email protected]d647e282012-09-13 13:10:101// 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
5// Unit tests for implementation of google_api_keys namespace.
6//
7// Because the file deals with a lot of preprocessor defines and
8// optionally includes an internal header, the way we test is by
9// including the .cc file multiple times with different defines set.
10// This is a little unorthodox, but it lets us test the behavior as
11// close to unmodified as possible.
12
bzanottibd014602016-08-10 18:42:5813#include "google_apis/google_api_keys_unittest.h"
dchengf064ccc2016-04-08 17:35:4014
avib32eea9b2015-12-22 22:35:0015#include "base/macros.h"
[email protected]65eae7082012-09-21 13:55:4716#include "build/build_config.h"
[email protected]850b353e2014-02-11 15:56:2817#include "google_apis/gaia/gaia_switches.h"
[email protected]d647e282012-09-13 13:10:1018
[email protected]65eae7082012-09-21 13:55:4719// The Win builders fail (with a linker crash) when trying to link
20// unit_tests, and the Android builders complain about multiply
21// defined symbols (likely they don't do name decoration as well as
22// the Mac and Linux linkers). Therefore these tests are only built
23// and run on Mac and Linux, which should provide plenty of coverage
24// since there are no platform-specific bits in this code.
25#if defined(OS_LINUX) || defined(OS_MACOSX)
[email protected]d647e282012-09-13 13:10:1026
[email protected]e03604b2013-01-10 23:38:2227// We need to include everything included by google_api_keys.cc once
28// at global scope so that things like STL and classes from base don't
29// get defined when we re-include the google_api_keys.cc file
30// below. We used to include that file in its entirety here, but that
31// can cause problems if the linker decides the version of symbols
32// from that file included here is the "right" version.
avib32eea9b2015-12-22 22:35:0033
34#include <stddef.h>
35
[email protected]e03604b2013-01-10 23:38:2236#include <string>
37#include "base/command_line.h"
[email protected]e03604b2013-01-10 23:38:2238#include "base/lazy_instance.h"
39#include "base/logging.h"
[email protected]8edbebd2013-01-31 19:45:0240#include "base/strings/stringize_macros.h"
[email protected]65eae7082012-09-21 13:55:4741
bzanottibd014602016-08-10 18:42:5842#if defined(OS_MACOSX)
43#include "google_apis/google_api_keys_mac.h"
44#endif
45
46GoogleAPIKeysTest::GoogleAPIKeysTest() : env_(base::Environment::Create()) {
47 static_assert(11 == 3 + 2 * google_apis::CLIENT_NUM_ITEMS,
48 "Unexpected number of key entries.");
49 env_cache_[0].variable_name = "GOOGLE_API_KEY";
50 env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN";
51 env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN";
52 env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT";
53 env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT";
54 env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING";
55 env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING";
56 env_cache_[7].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST";
57 env_cache_[8].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST";
58 env_cache_[9].variable_name = "GOOGLE_DEFAULT_CLIENT_ID";
59 env_cache_[10].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET";
60}
61
62GoogleAPIKeysTest::~GoogleAPIKeysTest() {}
63
64void GoogleAPIKeysTest::SetUp() {
65 // Unset all environment variables that can affect these tests,
66 // for the duration of the tests.
67 for (size_t i = 0; i < arraysize(env_cache_); ++i) {
68 EnvironmentCache& cache = env_cache_[i];
69 cache.was_set = env_->HasVar(cache.variable_name);
70 cache.value.clear();
71 if (cache.was_set) {
72 env_->GetVar(cache.variable_name, &cache.value);
73 env_->UnSetVar(cache.variable_name);
74 }
75 }
76}
77
78void GoogleAPIKeysTest::TearDown() {
79 // Restore environment.
80 for (size_t i = 0; i < arraysize(env_cache_); ++i) {
81 EnvironmentCache& cache = env_cache_[i];
82 if (cache.was_set) {
83 env_->SetVar(cache.variable_name, cache.value);
84 }
85 }
86}
87
[email protected]1abac7a2013-01-31 14:01:2588// This is the default baked-in value for OAuth IDs and secrets.
89static const char kDummyToken[] = "dummytoken";
[email protected]65eae7082012-09-21 13:55:4790
[email protected]65eae7082012-09-21 13:55:4791#if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
[email protected]d647e282012-09-13 13:10:1092// Test official build behavior, since we are in a checkout where this
93// is possible.
94namespace official_build {
95
[email protected]65eae7082012-09-21 13:55:4796// We start every test by creating a clean environment for the
97// preprocessor defines used in google_api_keys.cc
98#undef DUMMY_API_TOKEN
[email protected]d647e282012-09-13 13:10:1099#undef GOOGLE_API_KEY
100#undef GOOGLE_CLIENT_ID_MAIN
101#undef GOOGLE_CLIENT_SECRET_MAIN
102#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
103#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
104#undef GOOGLE_CLIENT_ID_REMOTING
105#undef GOOGLE_CLIENT_SECRET_REMOTING
[email protected]d66d81742013-08-16 05:18:38106#undef GOOGLE_CLIENT_ID_REMOTING_HOST
107#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
[email protected]65eae7082012-09-21 13:55:47108#undef GOOGLE_DEFAULT_CLIENT_ID
109#undef GOOGLE_DEFAULT_CLIENT_SECRET
[email protected]d647e282012-09-13 13:10:10110
111// Try setting some keys, these should be ignored since it's a build
112// with official keys.
[email protected]65eae7082012-09-21 13:55:47113#define GOOGLE_API_KEY "bogus api_key"
[email protected]d647e282012-09-13 13:10:10114#define GOOGLE_CLIENT_ID_MAIN "bogus client_id_main"
115
[email protected]65eae7082012-09-21 13:55:47116// Undef include guard so things get defined again, within this namespace.
117#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
118#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
[email protected]d647e282012-09-13 13:10:10119#include "google_apis/google_api_keys.cc"
120
[email protected]65eae7082012-09-21 13:55:47121} // namespace official_build
[email protected]d647e282012-09-13 13:10:10122
[email protected]65eae7082012-09-21 13:55:47123TEST_F(GoogleAPIKeysTest, OfficialKeys) {
124 namespace testcase = official_build::google_apis;
125
[email protected]e7dd7892013-05-16 19:10:40126 EXPECT_TRUE(testcase::HasKeysConfigured());
127
[email protected]65eae7082012-09-21 13:55:47128 std::string api_key = testcase::g_api_key_cache.Get().api_key();
129 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
130 testcase::CLIENT_MAIN);
131 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
132 testcase::CLIENT_MAIN);
133 std::string id_cloud_print =
134 testcase::g_api_key_cache.Get().GetClientID(
135 testcase::CLIENT_CLOUD_PRINT);
136 std::string secret_cloud_print =
137 testcase::g_api_key_cache.Get().GetClientSecret(
138 testcase::CLIENT_CLOUD_PRINT);
139 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
140 testcase::CLIENT_REMOTING);
141 std::string secret_remoting =
142 testcase::g_api_key_cache.Get().GetClientSecret(
143 testcase::CLIENT_REMOTING);
[email protected]d66d81742013-08-16 05:18:38144 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
145 testcase::CLIENT_REMOTING_HOST);
146 std::string secret_remoting_host =
147 testcase::g_api_key_cache.Get().GetClientSecret(
148 testcase::CLIENT_REMOTING_HOST);
[email protected]65eae7082012-09-21 13:55:47149
150 EXPECT_NE(0u, api_key.size());
151 EXPECT_NE(DUMMY_API_TOKEN, api_key);
152 EXPECT_NE("bogus api_key", api_key);
[email protected]1abac7a2013-01-31 14:01:25153 EXPECT_NE(kDummyToken, api_key);
[email protected]65eae7082012-09-21 13:55:47154
155 EXPECT_NE(0u, id_main.size());
156 EXPECT_NE(DUMMY_API_TOKEN, id_main);
157 EXPECT_NE("bogus client_id_main", id_main);
[email protected]1abac7a2013-01-31 14:01:25158 EXPECT_NE(kDummyToken, id_main);
[email protected]65eae7082012-09-21 13:55:47159
160 EXPECT_NE(0u, secret_main.size());
161 EXPECT_NE(DUMMY_API_TOKEN, secret_main);
[email protected]1abac7a2013-01-31 14:01:25162 EXPECT_NE(kDummyToken, secret_main);
[email protected]65eae7082012-09-21 13:55:47163
164 EXPECT_NE(0u, id_cloud_print.size());
165 EXPECT_NE(DUMMY_API_TOKEN, id_cloud_print);
[email protected]1abac7a2013-01-31 14:01:25166 EXPECT_NE(kDummyToken, id_cloud_print);
[email protected]65eae7082012-09-21 13:55:47167
168 EXPECT_NE(0u, secret_cloud_print.size());
169 EXPECT_NE(DUMMY_API_TOKEN, secret_cloud_print);
[email protected]1abac7a2013-01-31 14:01:25170 EXPECT_NE(kDummyToken, secret_cloud_print);
[email protected]65eae7082012-09-21 13:55:47171
172 EXPECT_NE(0u, id_remoting.size());
173 EXPECT_NE(DUMMY_API_TOKEN, id_remoting);
[email protected]1abac7a2013-01-31 14:01:25174 EXPECT_NE(kDummyToken, id_remoting);
[email protected]65eae7082012-09-21 13:55:47175
176 EXPECT_NE(0u, secret_remoting.size());
177 EXPECT_NE(DUMMY_API_TOKEN, secret_remoting);
[email protected]1abac7a2013-01-31 14:01:25178 EXPECT_NE(kDummyToken, secret_remoting);
[email protected]d66d81742013-08-16 05:18:38179
180 EXPECT_NE(0u, id_remoting_host.size());
181 EXPECT_NE(DUMMY_API_TOKEN, id_remoting_host);
182 EXPECT_NE(kDummyToken, id_remoting_host);
183
184 EXPECT_NE(0u, secret_remoting_host.size());
185 EXPECT_NE(DUMMY_API_TOKEN, secret_remoting_host);
186 EXPECT_NE(kDummyToken, secret_remoting_host);
[email protected]65eae7082012-09-21 13:55:47187}
188#endif // defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
189
190// After this test, for the remainder of this compilation unit, we
191// need official keys to not be used.
192#undef GOOGLE_CHROME_BUILD
193#undef USE_OFFICIAL_GOOGLE_API_KEYS
194
195// Test the set of keys temporarily baked into Chromium by default.
196namespace default_keys {
197
198// We start every test by creating a clean environment for the
199// preprocessor defines used in google_api_keys.cc
200#undef DUMMY_API_TOKEN
201#undef GOOGLE_API_KEY
202#undef GOOGLE_CLIENT_ID_MAIN
203#undef GOOGLE_CLIENT_SECRET_MAIN
204#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
205#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
206#undef GOOGLE_CLIENT_ID_REMOTING
207#undef GOOGLE_CLIENT_SECRET_REMOTING
[email protected]d66d81742013-08-16 05:18:38208#undef GOOGLE_CLIENT_ID_REMOTING_HOST
209#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
[email protected]65eae7082012-09-21 13:55:47210#undef GOOGLE_DEFAULT_CLIENT_ID
211#undef GOOGLE_DEFAULT_CLIENT_SECRET
212
213// Undef include guard so things get defined again, within this namespace.
214#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
215#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
216#include "google_apis/google_api_keys.cc"
217
218} // namespace default_keys
219
220TEST_F(GoogleAPIKeysTest, DefaultKeys) {
221 namespace testcase = default_keys::google_apis;
222
[email protected]e7dd7892013-05-16 19:10:40223 EXPECT_FALSE(testcase::HasKeysConfigured());
224
[email protected]65eae7082012-09-21 13:55:47225 std::string api_key = testcase::g_api_key_cache.Get().api_key();
226 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
227 testcase::CLIENT_MAIN);
228 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
229 testcase::CLIENT_MAIN);
230 std::string id_cloud_print =
231 testcase::g_api_key_cache.Get().GetClientID(
232 testcase::CLIENT_CLOUD_PRINT);
233 std::string secret_cloud_print =
234 testcase::g_api_key_cache.Get().GetClientSecret(
235 testcase::CLIENT_CLOUD_PRINT);
236 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
237 testcase::CLIENT_REMOTING);
238 std::string secret_remoting =
239 testcase::g_api_key_cache.Get().GetClientSecret(
240 testcase::CLIENT_REMOTING);
[email protected]d66d81742013-08-16 05:18:38241 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
242 testcase::CLIENT_REMOTING_HOST);
243 std::string secret_remoting_host =
244 testcase::g_api_key_cache.Get().GetClientSecret(
245 testcase::CLIENT_REMOTING_HOST);
[email protected]65eae7082012-09-21 13:55:47246
[email protected]1abac7a2013-01-31 14:01:25247 EXPECT_EQ(kDummyToken, api_key);
248 EXPECT_EQ(kDummyToken, id_main);
249 EXPECT_EQ(kDummyToken, secret_main);
250 EXPECT_EQ(kDummyToken, id_cloud_print);
251 EXPECT_EQ(kDummyToken, secret_cloud_print);
252 EXPECT_EQ(kDummyToken, id_remoting);
253 EXPECT_EQ(kDummyToken, secret_remoting);
[email protected]d66d81742013-08-16 05:18:38254 EXPECT_EQ(kDummyToken, id_remoting_host);
255 EXPECT_EQ(kDummyToken, secret_remoting_host);
[email protected]d647e282012-09-13 13:10:10256}
257
[email protected]65eae7082012-09-21 13:55:47258// Override a couple of keys, leave the rest default.
259namespace override_some_keys {
[email protected]d647e282012-09-13 13:10:10260
[email protected]65eae7082012-09-21 13:55:47261// We start every test by creating a clean environment for the
262// preprocessor defines used in google_api_keys.cc
263#undef DUMMY_API_TOKEN
264#undef GOOGLE_API_KEY
265#undef GOOGLE_CLIENT_ID_MAIN
266#undef GOOGLE_CLIENT_SECRET_MAIN
267#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
268#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
269#undef GOOGLE_CLIENT_ID_REMOTING
270#undef GOOGLE_CLIENT_SECRET_REMOTING
[email protected]d66d81742013-08-16 05:18:38271#undef GOOGLE_CLIENT_ID_REMOTING_HOST
272#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
[email protected]65eae7082012-09-21 13:55:47273#undef GOOGLE_DEFAULT_CLIENT_ID
274#undef GOOGLE_DEFAULT_CLIENT_SECRET
[email protected]d647e282012-09-13 13:10:10275
[email protected]65eae7082012-09-21 13:55:47276#define GOOGLE_API_KEY "API_KEY override"
277#define GOOGLE_CLIENT_ID_REMOTING "CLIENT_ID_REMOTING override"
278
279// Undef include guard so things get defined again, within this namespace.
280#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
281#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
282#include "google_apis/google_api_keys.cc"
283
284} // namespace override_some_keys
285
286TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) {
287 namespace testcase = override_some_keys::google_apis;
288
[email protected]e7dd7892013-05-16 19:10:40289 EXPECT_FALSE(testcase::HasKeysConfigured());
290
[email protected]65eae7082012-09-21 13:55:47291 std::string api_key = testcase::g_api_key_cache.Get().api_key();
292 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
293 testcase::CLIENT_MAIN);
294 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
295 testcase::CLIENT_MAIN);
296 std::string id_cloud_print =
297 testcase::g_api_key_cache.Get().GetClientID(
298 testcase::CLIENT_CLOUD_PRINT);
299 std::string secret_cloud_print =
300 testcase::g_api_key_cache.Get().GetClientSecret(
301 testcase::CLIENT_CLOUD_PRINT);
302 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
303 testcase::CLIENT_REMOTING);
304 std::string secret_remoting =
305 testcase::g_api_key_cache.Get().GetClientSecret(
306 testcase::CLIENT_REMOTING);
[email protected]d66d81742013-08-16 05:18:38307 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
308 testcase::CLIENT_REMOTING_HOST);
309 std::string secret_remoting_host =
310 testcase::g_api_key_cache.Get().GetClientSecret(
311 testcase::CLIENT_REMOTING_HOST);
[email protected]65eae7082012-09-21 13:55:47312
313 EXPECT_EQ("API_KEY override", api_key);
[email protected]1abac7a2013-01-31 14:01:25314 EXPECT_EQ(kDummyToken, id_main);
315 EXPECT_EQ(kDummyToken, secret_main);
316 EXPECT_EQ(kDummyToken, id_cloud_print);
317 EXPECT_EQ(kDummyToken, secret_cloud_print);
[email protected]65eae7082012-09-21 13:55:47318 EXPECT_EQ("CLIENT_ID_REMOTING override", id_remoting);
[email protected]1abac7a2013-01-31 14:01:25319 EXPECT_EQ(kDummyToken, secret_remoting);
[email protected]d66d81742013-08-16 05:18:38320 EXPECT_EQ(kDummyToken, id_remoting_host);
321 EXPECT_EQ(kDummyToken, secret_remoting_host);
[email protected]65eae7082012-09-21 13:55:47322}
323
324// Override all keys.
325namespace override_all_keys {
326
327// We start every test by creating a clean environment for the
328// preprocessor defines used in google_api_keys.cc
329#undef DUMMY_API_TOKEN
330#undef GOOGLE_API_KEY
331#undef GOOGLE_CLIENT_ID_MAIN
332#undef GOOGLE_CLIENT_SECRET_MAIN
333#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
334#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
335#undef GOOGLE_CLIENT_ID_REMOTING
336#undef GOOGLE_CLIENT_SECRET_REMOTING
[email protected]d66d81742013-08-16 05:18:38337#undef GOOGLE_CLIENT_ID_REMOTING_HOST
338#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
[email protected]65eae7082012-09-21 13:55:47339#undef GOOGLE_DEFAULT_CLIENT_ID
340#undef GOOGLE_DEFAULT_CLIENT_SECRET
341
342#define GOOGLE_API_KEY "API_KEY"
343#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
344#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
345#define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT"
346#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
347#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
348#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
[email protected]d66d81742013-08-16 05:18:38349#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
350#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
[email protected]65eae7082012-09-21 13:55:47351
352// Undef include guard so things get defined again, within this namespace.
353#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
354#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
355#include "google_apis/google_api_keys.cc"
356
357} // namespace override_all_keys
358
359TEST_F(GoogleAPIKeysTest, OverrideAllKeys) {
360 namespace testcase = override_all_keys::google_apis;
361
[email protected]e7dd7892013-05-16 19:10:40362 EXPECT_TRUE(testcase::HasKeysConfigured());
363
[email protected]65eae7082012-09-21 13:55:47364 std::string api_key = testcase::g_api_key_cache.Get().api_key();
365 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
366 testcase::CLIENT_MAIN);
367 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
368 testcase::CLIENT_MAIN);
369 std::string id_cloud_print =
370 testcase::g_api_key_cache.Get().GetClientID(
371 testcase::CLIENT_CLOUD_PRINT);
372 std::string secret_cloud_print =
373 testcase::g_api_key_cache.Get().GetClientSecret(
374 testcase::CLIENT_CLOUD_PRINT);
375 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
376 testcase::CLIENT_REMOTING);
377 std::string secret_remoting =
378 testcase::g_api_key_cache.Get().GetClientSecret(
379 testcase::CLIENT_REMOTING);
[email protected]d66d81742013-08-16 05:18:38380 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
381 testcase::CLIENT_REMOTING_HOST);
382 std::string secret_remoting_host =
383 testcase::g_api_key_cache.Get().GetClientSecret(
384 testcase::CLIENT_REMOTING_HOST);
[email protected]65eae7082012-09-21 13:55:47385
386 EXPECT_EQ("API_KEY", api_key);
387 EXPECT_EQ("ID_MAIN", id_main);
388 EXPECT_EQ("SECRET_MAIN", secret_main);
389 EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print);
390 EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print);
391 EXPECT_EQ("ID_REMOTING", id_remoting);
392 EXPECT_EQ("SECRET_REMOTING", secret_remoting);
[email protected]d66d81742013-08-16 05:18:38393 EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host);
394 EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host);
[email protected]65eae7082012-09-21 13:55:47395}
396
robertshield156c7092017-06-20 21:26:51397#if !defined(GOOGLE_CHROME_BUILD)
398
[email protected]65eae7082012-09-21 13:55:47399// Override all keys using both preprocessor defines and environment
400// variables. The environment variables should win.
401namespace override_all_keys_env {
402
403// We start every test by creating a clean environment for the
404// preprocessor defines used in google_api_keys.cc
405#undef DUMMY_API_TOKEN
406#undef GOOGLE_API_KEY
407#undef GOOGLE_CLIENT_ID_MAIN
408#undef GOOGLE_CLIENT_SECRET_MAIN
409#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
410#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
411#undef GOOGLE_CLIENT_ID_REMOTING
412#undef GOOGLE_CLIENT_SECRET_REMOTING
[email protected]d66d81742013-08-16 05:18:38413#undef GOOGLE_CLIENT_ID_REMOTING_HOST
414#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
[email protected]65eae7082012-09-21 13:55:47415#undef GOOGLE_DEFAULT_CLIENT_ID
416#undef GOOGLE_DEFAULT_CLIENT_SECRET
417
418#define GOOGLE_API_KEY "API_KEY"
419#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
420#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
421#define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT"
422#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
423#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
424#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
[email protected]d66d81742013-08-16 05:18:38425#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
426#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
[email protected]65eae7082012-09-21 13:55:47427
428// Undef include guard so things get defined again, within this namespace.
429#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
430#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
431#include "google_apis/google_api_keys.cc"
432
433} // namespace override_all_keys_env
434
435TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) {
436 namespace testcase = override_all_keys_env::google_apis;
437
dchengf064ccc2016-04-08 17:35:40438 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]65eae7082012-09-21 13:55:47439 env->SetVar("GOOGLE_API_KEY", "env-API_KEY");
440 env->SetVar("GOOGLE_CLIENT_ID_MAIN", "env-ID_MAIN");
441 env->SetVar("GOOGLE_CLIENT_ID_CLOUD_PRINT", "env-ID_CLOUD_PRINT");
442 env->SetVar("GOOGLE_CLIENT_ID_REMOTING", "env-ID_REMOTING");
[email protected]d66d81742013-08-16 05:18:38443 env->SetVar("GOOGLE_CLIENT_ID_REMOTING_HOST", "env-ID_REMOTING_HOST");
[email protected]65eae7082012-09-21 13:55:47444 env->SetVar("GOOGLE_CLIENT_SECRET_MAIN", "env-SECRET_MAIN");
445 env->SetVar("GOOGLE_CLIENT_SECRET_CLOUD_PRINT", "env-SECRET_CLOUD_PRINT");
446 env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING", "env-SECRET_REMOTING");
[email protected]d66d81742013-08-16 05:18:38447 env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING_HOST", "env-SECRET_REMOTING_HOST");
[email protected]65eae7082012-09-21 13:55:47448
[email protected]e7dd7892013-05-16 19:10:40449 EXPECT_TRUE(testcase::HasKeysConfigured());
450
[email protected]65eae7082012-09-21 13:55:47451 // It's important that the first call to Get() only happen after the
452 // environment variables have been set.
453 std::string api_key = testcase::g_api_key_cache.Get().api_key();
454 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
455 testcase::CLIENT_MAIN);
456 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
457 testcase::CLIENT_MAIN);
458 std::string id_cloud_print =
459 testcase::g_api_key_cache.Get().GetClientID(
460 testcase::CLIENT_CLOUD_PRINT);
461 std::string secret_cloud_print =
462 testcase::g_api_key_cache.Get().GetClientSecret(
463 testcase::CLIENT_CLOUD_PRINT);
464 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
465 testcase::CLIENT_REMOTING);
466 std::string secret_remoting =
467 testcase::g_api_key_cache.Get().GetClientSecret(
468 testcase::CLIENT_REMOTING);
[email protected]d66d81742013-08-16 05:18:38469 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
470 testcase::CLIENT_REMOTING_HOST);
471 std::string secret_remoting_host =
472 testcase::g_api_key_cache.Get().GetClientSecret(
473 testcase::CLIENT_REMOTING_HOST);
[email protected]65eae7082012-09-21 13:55:47474
475 EXPECT_EQ("env-API_KEY", api_key);
476 EXPECT_EQ("env-ID_MAIN", id_main);
477 EXPECT_EQ("env-SECRET_MAIN", secret_main);
478 EXPECT_EQ("env-ID_CLOUD_PRINT", id_cloud_print);
479 EXPECT_EQ("env-SECRET_CLOUD_PRINT", secret_cloud_print);
480 EXPECT_EQ("env-ID_REMOTING", id_remoting);
481 EXPECT_EQ("env-SECRET_REMOTING", secret_remoting);
[email protected]d66d81742013-08-16 05:18:38482 EXPECT_EQ("env-ID_REMOTING_HOST", id_remoting_host);
483 EXPECT_EQ("env-SECRET_REMOTING_HOST", secret_remoting_host);
[email protected]65eae7082012-09-21 13:55:47484}
485
robertshield156c7092017-06-20 21:26:51486#endif // !defined(GOOGLE_CHROME_BUILD)
487
jzw2d2920822017-04-20 09:19:29488#if defined(OS_IOS)
489// Override all keys using both preprocessor defines and setters.
490// Setters should win.
491namespace override_all_keys_setters {
492
493// We start every test by creating a clean environment for the
494// preprocessor defines used in google_api_keys.cc
495#undef DUMMY_API_TOKEN
496#undef GOOGLE_API_KEY
497#undef GOOGLE_CLIENT_ID_MAIN
498#undef GOOGLE_CLIENT_SECRET_MAIN
499#undef GOOGLE_CLIENT_ID_CLOUD_PRINT
500#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
501#undef GOOGLE_CLIENT_ID_REMOTING
502#undef GOOGLE_CLIENT_SECRET_REMOTING
503#undef GOOGLE_CLIENT_ID_REMOTING_HOST
504#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
505#undef GOOGLE_DEFAULT_CLIENT_ID
506#undef GOOGLE_DEFAULT_CLIENT_SECRET
507
508#define GOOGLE_API_KEY "API_KEY"
509#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
510#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
511#define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT"
512#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
513#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
514#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
515#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
516#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
517
518// Undef include guard so things get defined again, within this namespace.
519#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
520#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
521#include "google_apis/google_api_keys.cc"
522
523} // namespace override_all_keys_setters
524
525TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingSetters) {
526 namespace testcase = override_all_keys_setters::google_apis;
527
528 std::string api_key("setter-API_KEY");
529 testcase::SetAPIKey(api_key);
530
531 std::string id_main("setter-ID_MAIN");
532 std::string secret_main("setter-SECRET_MAIN");
533 testcase::SetOAuth2ClientID(testcase::CLIENT_MAIN, id_main);
534 testcase::SetOAuth2ClientSecret(testcase::CLIENT_MAIN, secret_main);
535
536 std::string id_cloud_print("setter-ID_CLOUD_PRINT");
537 std::string secret_cloud_print("setter-SECRET_CLOUD_PRINT");
538 testcase::SetOAuth2ClientID(testcase::CLIENT_CLOUD_PRINT, id_cloud_print);
539 testcase::SetOAuth2ClientSecret(testcase::CLIENT_CLOUD_PRINT,
540 secret_cloud_print);
541
542 std::string id_remoting("setter-ID_REMOTING");
543 std::string secret_remoting("setter-SECRET_REMOTING");
544 testcase::SetOAuth2ClientID(testcase::CLIENT_REMOTING, id_remoting);
545 testcase::SetOAuth2ClientSecret(testcase::CLIENT_REMOTING, secret_remoting);
546
547 std::string id_remoting_host("setter-ID_REMOTING_HOST");
548 std::string secret_remoting_host("setter-SECRET_REMOTING_HOST");
549 testcase::SetOAuth2ClientID(testcase::CLIENT_REMOTING_HOST, id_remoting_host);
550 testcase::SetOAuth2ClientSecret(testcase::CLIENT_REMOTING_HOST,
551 secret_remoting_host);
552
553 EXPECT_TRUE(testcase::HasKeysConfigured());
554
555 EXPECT_EQ(api_key, testcase::GetAPIKey());
556
557 EXPECT_EQ(id_main, testcase::GetOAuth2ClientID(testcase::CLIENT_MAIN));
558 EXPECT_EQ(secret_main,
559 testcase::GetOAuth2ClientSecret(testcase::CLIENT_MAIN));
560
561 EXPECT_EQ(id_cloud_print,
562 testcase::GetOAuth2ClientID(testcase::CLIENT_CLOUD_PRINT));
563 EXPECT_EQ(secret_cloud_print,
564 testcase::GetOAuth2ClientSecret(testcase::CLIENT_CLOUD_PRINT));
565
566 EXPECT_EQ(id_remoting,
567 testcase::GetOAuth2ClientID(testcase::CLIENT_REMOTING));
568 EXPECT_EQ(secret_remoting,
569 testcase::GetOAuth2ClientSecret(testcase::CLIENT_REMOTING));
570
571 EXPECT_EQ(id_remoting_host,
572 testcase::GetOAuth2ClientID(testcase::CLIENT_REMOTING_HOST));
573 EXPECT_EQ(secret_remoting_host,
574 testcase::GetOAuth2ClientSecret(testcase::CLIENT_REMOTING_HOST));
575}
576#endif // defined(OS_IOS)
577
[email protected]65eae7082012-09-21 13:55:47578#endif // defined(OS_LINUX) || defined(OS_MACOSX)