Blink roll 168661:148720
[chromium-blink-merge.git] / google_apis / google_api_keys_unittest.cc
blobab50d3f7494cb000fc1ac82de1ac107f56683086
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.
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.
13 #include "google_apis/google_api_keys.h"
15 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 // The Win builders fail (with a linker crash) when trying to link
19 // unit_tests, and the Android builders complain about multiply
20 // defined symbols (likely they don't do name decoration as well as
21 // the Mac and Linux linkers). Therefore these tests are only built
22 // and run on Mac and Linux, which should provide plenty of coverage
23 // since there are no platform-specific bits in this code.
24 #if defined(OS_LINUX) || defined(OS_MACOSX)
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 <string>
33 #include "base/command_line.h"
34 #include "base/environment.h"
35 #include "base/lazy_instance.h"
36 #include "base/logging.h"
37 #include "base/memory/scoped_ptr.h"
38 #include "base/strings/stringize_macros.h"
40 // This is the default baked-in value for OAuth IDs and secrets.
41 static const char kDummyToken[] = "dummytoken";
43 struct EnvironmentCache {
44 public:
45 EnvironmentCache() : variable_name(NULL), was_set(false) {}
47 const char* variable_name;
48 bool was_set;
49 std::string value;
52 class GoogleAPIKeysTest : public testing::Test {
53 public:
54 GoogleAPIKeysTest() : env_(base::Environment::Create()) {
55 env_cache_[0].variable_name = "GOOGLE_API_KEY";
56 env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN";
57 env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN";
58 env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT";
59 env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT";
60 env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING";
61 env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING";
62 env_cache_[7].variable_name = "GOOGLE_DEFAULT_CLIENT_ID";
63 env_cache_[8].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET";
66 virtual void SetUp() {
67 // Unset all environment variables that can affect these tests,
68 // for the duration of the tests.
69 for (size_t i = 0; i < arraysize(env_cache_); ++i) {
70 EnvironmentCache& cache = env_cache_[i];
71 cache.was_set = env_->HasVar(cache.variable_name);
72 cache.value.clear();
73 if (cache.was_set) {
74 env_->GetVar(cache.variable_name, &cache.value);
75 env_->UnSetVar(cache.variable_name);
80 virtual void TearDown() {
81 // Restore environment.
82 for (size_t i = 0; i < arraysize(env_cache_); ++i) {
83 EnvironmentCache& cache = env_cache_[i];
84 if (cache.was_set) {
85 env_->SetVar(cache.variable_name, cache.value);
90 private:
91 scoped_ptr<base::Environment> env_;
93 // Why 3? It is for GOOGLE_API_KEY, GOOGLE_DEFAULT_CLIENT_ID and
94 // GOOGLE_DEFAULT_CLIENT_SECRET.
96 // Why 2 times CLIENT_NUM_ITEMS? This is the number of different
97 // clients in the OAuth2Client enumeration, and for each of these we
98 // have both an ID and a secret.
99 EnvironmentCache env_cache_[3 + 2 * google_apis::CLIENT_NUM_ITEMS];
102 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
103 // Test official build behavior, since we are in a checkout where this
104 // is possible.
105 namespace official_build {
107 // We start every test by creating a clean environment for the
108 // preprocessor defines used in google_api_keys.cc
109 #undef DUMMY_API_TOKEN
110 #undef GOOGLE_API_KEY
111 #undef GOOGLE_CLIENT_ID_MAIN
112 #undef GOOGLE_CLIENT_SECRET_MAIN
113 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT
114 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
115 #undef GOOGLE_CLIENT_ID_REMOTING
116 #undef GOOGLE_CLIENT_SECRET_REMOTING
117 #undef GOOGLE_DEFAULT_CLIENT_ID
118 #undef GOOGLE_DEFAULT_CLIENT_SECRET
120 // Try setting some keys, these should be ignored since it's a build
121 // with official keys.
122 #define GOOGLE_API_KEY "bogus api_key"
123 #define GOOGLE_CLIENT_ID_MAIN "bogus client_id_main"
125 // Undef include guard so things get defined again, within this namespace.
126 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
127 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
128 #include "google_apis/google_api_keys.cc"
130 } // namespace official_build
132 TEST_F(GoogleAPIKeysTest, OfficialKeys) {
133 namespace testcase = official_build::google_apis;
135 std::string api_key = testcase::g_api_key_cache.Get().api_key();
136 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
137 testcase::CLIENT_MAIN);
138 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
139 testcase::CLIENT_MAIN);
140 std::string id_cloud_print =
141 testcase::g_api_key_cache.Get().GetClientID(
142 testcase::CLIENT_CLOUD_PRINT);
143 std::string secret_cloud_print =
144 testcase::g_api_key_cache.Get().GetClientSecret(
145 testcase::CLIENT_CLOUD_PRINT);
146 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
147 testcase::CLIENT_REMOTING);
148 std::string secret_remoting =
149 testcase::g_api_key_cache.Get().GetClientSecret(
150 testcase::CLIENT_REMOTING);
152 EXPECT_NE(0u, api_key.size());
153 EXPECT_NE(DUMMY_API_TOKEN, api_key);
154 EXPECT_NE("bogus api_key", api_key);
155 EXPECT_NE(kDummyToken, api_key);
157 EXPECT_NE(0u, id_main.size());
158 EXPECT_NE(DUMMY_API_TOKEN, id_main);
159 EXPECT_NE("bogus client_id_main", id_main);
160 EXPECT_NE(kDummyToken, id_main);
162 EXPECT_NE(0u, secret_main.size());
163 EXPECT_NE(DUMMY_API_TOKEN, secret_main);
164 EXPECT_NE(kDummyToken, secret_main);
166 EXPECT_NE(0u, id_cloud_print.size());
167 EXPECT_NE(DUMMY_API_TOKEN, id_cloud_print);
168 EXPECT_NE(kDummyToken, id_cloud_print);
170 EXPECT_NE(0u, secret_cloud_print.size());
171 EXPECT_NE(DUMMY_API_TOKEN, secret_cloud_print);
172 EXPECT_NE(kDummyToken, secret_cloud_print);
174 EXPECT_NE(0u, id_remoting.size());
175 EXPECT_NE(DUMMY_API_TOKEN, id_remoting);
176 EXPECT_NE(kDummyToken, id_remoting);
178 EXPECT_NE(0u, secret_remoting.size());
179 EXPECT_NE(DUMMY_API_TOKEN, secret_remoting);
180 EXPECT_NE(kDummyToken, secret_remoting);
182 #endif // defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
184 // After this test, for the remainder of this compilation unit, we
185 // need official keys to not be used.
186 #undef GOOGLE_CHROME_BUILD
187 #undef USE_OFFICIAL_GOOGLE_API_KEYS
189 // Test the set of keys temporarily baked into Chromium by default.
190 namespace default_keys {
192 // We start every test by creating a clean environment for the
193 // preprocessor defines used in google_api_keys.cc
194 #undef DUMMY_API_TOKEN
195 #undef GOOGLE_API_KEY
196 #undef GOOGLE_CLIENT_ID_MAIN
197 #undef GOOGLE_CLIENT_SECRET_MAIN
198 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT
199 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
200 #undef GOOGLE_CLIENT_ID_REMOTING
201 #undef GOOGLE_CLIENT_SECRET_REMOTING
202 #undef GOOGLE_DEFAULT_CLIENT_ID
203 #undef GOOGLE_DEFAULT_CLIENT_SECRET
205 // Undef include guard so things get defined again, within this namespace.
206 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
207 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
208 #include "google_apis/google_api_keys.cc"
210 } // namespace default_keys
212 TEST_F(GoogleAPIKeysTest, DefaultKeys) {
213 namespace testcase = default_keys::google_apis;
215 std::string api_key = testcase::g_api_key_cache.Get().api_key();
216 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
217 testcase::CLIENT_MAIN);
218 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
219 testcase::CLIENT_MAIN);
220 std::string id_cloud_print =
221 testcase::g_api_key_cache.Get().GetClientID(
222 testcase::CLIENT_CLOUD_PRINT);
223 std::string secret_cloud_print =
224 testcase::g_api_key_cache.Get().GetClientSecret(
225 testcase::CLIENT_CLOUD_PRINT);
226 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
227 testcase::CLIENT_REMOTING);
228 std::string secret_remoting =
229 testcase::g_api_key_cache.Get().GetClientSecret(
230 testcase::CLIENT_REMOTING);
232 EXPECT_EQ(kDummyToken, api_key);
233 EXPECT_EQ(kDummyToken, id_main);
234 EXPECT_EQ(kDummyToken, secret_main);
235 EXPECT_EQ(kDummyToken, id_cloud_print);
236 EXPECT_EQ(kDummyToken, secret_cloud_print);
237 EXPECT_EQ(kDummyToken, id_remoting);
238 EXPECT_EQ(kDummyToken, secret_remoting);
241 // Override a couple of keys, leave the rest default.
242 namespace override_some_keys {
244 // We start every test by creating a clean environment for the
245 // preprocessor defines used in google_api_keys.cc
246 #undef DUMMY_API_TOKEN
247 #undef GOOGLE_API_KEY
248 #undef GOOGLE_CLIENT_ID_MAIN
249 #undef GOOGLE_CLIENT_SECRET_MAIN
250 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT
251 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
252 #undef GOOGLE_CLIENT_ID_REMOTING
253 #undef GOOGLE_CLIENT_SECRET_REMOTING
254 #undef GOOGLE_DEFAULT_CLIENT_ID
255 #undef GOOGLE_DEFAULT_CLIENT_SECRET
257 #define GOOGLE_API_KEY "API_KEY override"
258 #define GOOGLE_CLIENT_ID_REMOTING "CLIENT_ID_REMOTING override"
260 // Undef include guard so things get defined again, within this namespace.
261 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
262 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
263 #include "google_apis/google_api_keys.cc"
265 } // namespace override_some_keys
267 TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) {
268 namespace testcase = override_some_keys::google_apis;
270 std::string api_key = testcase::g_api_key_cache.Get().api_key();
271 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
272 testcase::CLIENT_MAIN);
273 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
274 testcase::CLIENT_MAIN);
275 std::string id_cloud_print =
276 testcase::g_api_key_cache.Get().GetClientID(
277 testcase::CLIENT_CLOUD_PRINT);
278 std::string secret_cloud_print =
279 testcase::g_api_key_cache.Get().GetClientSecret(
280 testcase::CLIENT_CLOUD_PRINT);
281 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
282 testcase::CLIENT_REMOTING);
283 std::string secret_remoting =
284 testcase::g_api_key_cache.Get().GetClientSecret(
285 testcase::CLIENT_REMOTING);
287 EXPECT_EQ("API_KEY override", api_key);
288 EXPECT_EQ(kDummyToken, id_main);
289 EXPECT_EQ(kDummyToken, secret_main);
290 EXPECT_EQ(kDummyToken, id_cloud_print);
291 EXPECT_EQ(kDummyToken, secret_cloud_print);
292 EXPECT_EQ("CLIENT_ID_REMOTING override", id_remoting);
293 EXPECT_EQ(kDummyToken, secret_remoting);
296 // Override all keys.
297 namespace override_all_keys {
299 // We start every test by creating a clean environment for the
300 // preprocessor defines used in google_api_keys.cc
301 #undef DUMMY_API_TOKEN
302 #undef GOOGLE_API_KEY
303 #undef GOOGLE_CLIENT_ID_MAIN
304 #undef GOOGLE_CLIENT_SECRET_MAIN
305 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT
306 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
307 #undef GOOGLE_CLIENT_ID_REMOTING
308 #undef GOOGLE_CLIENT_SECRET_REMOTING
309 #undef GOOGLE_DEFAULT_CLIENT_ID
310 #undef GOOGLE_DEFAULT_CLIENT_SECRET
312 #define GOOGLE_API_KEY "API_KEY"
313 #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
314 #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
315 #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT"
316 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
317 #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
318 #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
320 // Undef include guard so things get defined again, within this namespace.
321 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
322 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
323 #include "google_apis/google_api_keys.cc"
325 } // namespace override_all_keys
327 TEST_F(GoogleAPIKeysTest, OverrideAllKeys) {
328 namespace testcase = override_all_keys::google_apis;
330 std::string api_key = testcase::g_api_key_cache.Get().api_key();
331 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
332 testcase::CLIENT_MAIN);
333 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
334 testcase::CLIENT_MAIN);
335 std::string id_cloud_print =
336 testcase::g_api_key_cache.Get().GetClientID(
337 testcase::CLIENT_CLOUD_PRINT);
338 std::string secret_cloud_print =
339 testcase::g_api_key_cache.Get().GetClientSecret(
340 testcase::CLIENT_CLOUD_PRINT);
341 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
342 testcase::CLIENT_REMOTING);
343 std::string secret_remoting =
344 testcase::g_api_key_cache.Get().GetClientSecret(
345 testcase::CLIENT_REMOTING);
347 EXPECT_EQ("API_KEY", api_key);
348 EXPECT_EQ("ID_MAIN", id_main);
349 EXPECT_EQ("SECRET_MAIN", secret_main);
350 EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print);
351 EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print);
352 EXPECT_EQ("ID_REMOTING", id_remoting);
353 EXPECT_EQ("SECRET_REMOTING", secret_remoting);
356 // Override all keys using both preprocessor defines and environment
357 // variables. The environment variables should win.
358 namespace override_all_keys_env {
360 // We start every test by creating a clean environment for the
361 // preprocessor defines used in google_api_keys.cc
362 #undef DUMMY_API_TOKEN
363 #undef GOOGLE_API_KEY
364 #undef GOOGLE_CLIENT_ID_MAIN
365 #undef GOOGLE_CLIENT_SECRET_MAIN
366 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT
367 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT
368 #undef GOOGLE_CLIENT_ID_REMOTING
369 #undef GOOGLE_CLIENT_SECRET_REMOTING
370 #undef GOOGLE_DEFAULT_CLIENT_ID
371 #undef GOOGLE_DEFAULT_CLIENT_SECRET
373 #define GOOGLE_API_KEY "API_KEY"
374 #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
375 #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
376 #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT"
377 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT"
378 #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
379 #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
381 // Undef include guard so things get defined again, within this namespace.
382 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
383 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
384 #include "google_apis/google_api_keys.cc"
386 } // namespace override_all_keys_env
388 TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) {
389 namespace testcase = override_all_keys_env::google_apis;
391 scoped_ptr<base::Environment> env(base::Environment::Create());
392 env->SetVar("GOOGLE_API_KEY", "env-API_KEY");
393 env->SetVar("GOOGLE_CLIENT_ID_MAIN", "env-ID_MAIN");
394 env->SetVar("GOOGLE_CLIENT_ID_CLOUD_PRINT", "env-ID_CLOUD_PRINT");
395 env->SetVar("GOOGLE_CLIENT_ID_REMOTING", "env-ID_REMOTING");
396 env->SetVar("GOOGLE_CLIENT_SECRET_MAIN", "env-SECRET_MAIN");
397 env->SetVar("GOOGLE_CLIENT_SECRET_CLOUD_PRINT", "env-SECRET_CLOUD_PRINT");
398 env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING", "env-SECRET_REMOTING");
400 // It's important that the first call to Get() only happen after the
401 // environment variables have been set.
402 std::string api_key = testcase::g_api_key_cache.Get().api_key();
403 std::string id_main = testcase::g_api_key_cache.Get().GetClientID(
404 testcase::CLIENT_MAIN);
405 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret(
406 testcase::CLIENT_MAIN);
407 std::string id_cloud_print =
408 testcase::g_api_key_cache.Get().GetClientID(
409 testcase::CLIENT_CLOUD_PRINT);
410 std::string secret_cloud_print =
411 testcase::g_api_key_cache.Get().GetClientSecret(
412 testcase::CLIENT_CLOUD_PRINT);
413 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID(
414 testcase::CLIENT_REMOTING);
415 std::string secret_remoting =
416 testcase::g_api_key_cache.Get().GetClientSecret(
417 testcase::CLIENT_REMOTING);
419 EXPECT_EQ("env-API_KEY", api_key);
420 EXPECT_EQ("env-ID_MAIN", id_main);
421 EXPECT_EQ("env-SECRET_MAIN", secret_main);
422 EXPECT_EQ("env-ID_CLOUD_PRINT", id_cloud_print);
423 EXPECT_EQ("env-SECRET_CLOUD_PRINT", secret_cloud_print);
424 EXPECT_EQ("env-ID_REMOTING", id_remoting);
425 EXPECT_EQ("env-SECRET_REMOTING", secret_remoting);
428 #endif // defined(OS_LINUX) || defined(OS_MACOSX)