Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Unit tests for implementation of google_api_keys namespace. | 5 // Unit tests for implementation of google_api_keys namespace. |
| 6 // | 6 // |
| 7 // Because the file deals with a lot of preprocessor defines and | 7 // Because the file deals with a lot of preprocessor defines and |
| 8 // optionally includes an internal header, the way we test is by | 8 // optionally includes an internal header, the way we test is by |
| 9 // including the .cc file multiple times with different defines set. | 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 | 10 // This is a little unorthodox, but it lets us test the behavior as |
| 11 // close to unmodified as possible. | 11 // close to unmodified as possible. |
| 12 | 12 |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace { | 15 // We need to include this once at global scope so things like STL and |
| 16 // classes from base do not get defined again within the different | |
| 17 // namespaces below. | |
| 18 #include "google_apis/google_api_keys.cc" | |
| 16 | 19 |
| 17 #if defined(GOOGLE_CHROME_BUILD) or defined(USE_OFFICIAL_GOOGLE_API_KEYS) | 20 // These are the (temporary) default values for OAuth IDs and secrets. |
| 21 static const char kDefaultNonOfficialAPIKey[] = | |
| 22 "AIzaSyBHDrl33hwRp4rMQY0ziRbj8K9LPA6vUCY"; | |
| 23 static const char kDefaultNonOfficialClientID[] = | |
| 24 "609716072145.apps.googleusercontent.com"; | |
| 25 static const char kDefaultNonOfficialClientSecret[] = | |
| 26 "WF4uG3gJzEH0KLpS7OuFBDux"; | |
| 27 | |
| 28 struct EnvironmentCache { | |
| 29 public: | |
| 30 EnvironmentCache() : variable_name(NULL), was_set(false) {} | |
| 31 | |
| 32 const char* variable_name; | |
| 33 bool was_set; | |
| 34 std::string value; | |
| 35 }; | |
| 36 | |
| 37 class GoogleAPIKeysTest : public testing::Test { | |
| 38 public: | |
| 39 GoogleAPIKeysTest() : env_(base::Environment::Create()) { | |
| 40 | |
| 41 | |
|
MAD
2012/09/20 18:07:47
Why two blank lines?
| |
| 42 env_cache_[0].variable_name = "GOOGLE_API_KEY"; | |
| 43 env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN"; | |
| 44 env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN"; | |
| 45 env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT"; | |
| 46 env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT"; | |
| 47 env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING"; | |
| 48 env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING"; | |
| 49 env_cache_[7].variable_name = "GOOGLE_DEFAULT_CLIENT_ID"; | |
| 50 env_cache_[8].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET"; | |
| 51 } | |
| 52 | |
| 53 void SetUp() { | |
| 54 // Unset all environment variables that can affect these tests, | |
| 55 // for the duration of the tests. | |
| 56 for (size_t i = 0; i < arraysize(env_cache_); ++i) { | |
| 57 EnvironmentCache& cache = env_cache_[i]; | |
| 58 cache.was_set = env_->HasVar(cache.variable_name); | |
| 59 cache.value.clear(); | |
| 60 if (cache.was_set) { | |
| 61 env_->GetVar(cache.variable_name, &cache.value); | |
| 62 env_->UnSetVar(cache.variable_name); | |
| 63 } | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void TearDown() { | |
| 68 // Restore environment. | |
| 69 for (size_t i = 0; i < arraysize(env_cache_); ++i) { | |
| 70 EnvironmentCache& cache = env_cache_[i]; | |
| 71 if (cache.was_set) { | |
| 72 env_->SetVar(cache.variable_name, cache.value); | |
| 73 } | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 scoped_ptr<base::Environment> env_; | |
| 79 | |
| 80 // Why 3? It is for GOOGLE_API_KEY, GOOGLE_DEFAULT_CLIENT_ID and | |
| 81 // GOOGLE_DEFAULT_CLIENT_SECRET. | |
| 82 // | |
| 83 // Why 2 times CLIENT_NUM_ITEMS? This is the number of different | |
| 84 // clients in the OAuth2Client enumeration, and for each of these we | |
| 85 // have both an ID and a secret. | |
| 86 EnvironmentCache env_cache_[3 + 2 * google_apis::CLIENT_NUM_ITEMS]; | |
| 87 }; | |
| 88 | |
| 89 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) | |
| 18 // Test official build behavior, since we are in a checkout where this | 90 // Test official build behavior, since we are in a checkout where this |
| 19 // is possible. | 91 // is possible. |
| 20 namespace official_build { | 92 namespace official_build { |
| 21 | 93 |
| 94 // We start every test by creating a clean environment for the | |
| 95 // preprocessor defines used in google_api_keys.cc | |
| 96 #undef DUMMY_API_TOKEN | |
| 22 #undef GOOGLE_API_KEY | 97 #undef GOOGLE_API_KEY |
| 23 #undef GOOGLE_CLIENT_ID_MAIN | 98 #undef GOOGLE_CLIENT_ID_MAIN |
| 24 #undef GOOGLE_CLIENT_SECRET_MAIN | 99 #undef GOOGLE_CLIENT_SECRET_MAIN |
| 25 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT | 100 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
| 26 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT | 101 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
| 27 #undef GOOGLE_CLIENT_ID_REMOTING | 102 #undef GOOGLE_CLIENT_ID_REMOTING |
| 28 #undef GOOGLE_CLIENT_SECRET_REMOTING | 103 #undef GOOGLE_CLIENT_SECRET_REMOTING |
| 104 #undef GOOGLE_DEFAULT_CLIENT_ID | |
| 105 #undef GOOGLE_DEFAULT_CLIENT_SECRET | |
| 29 | 106 |
| 30 // Try setting some keys, these should be ignored since it's a build | 107 // Try setting some keys, these should be ignored since it's a build |
| 31 // with official keys. | 108 // with official keys. |
| 32 #define GOOGLE_API_KEY "bogus api key" | 109 #define GOOGLE_API_KEY "bogus api_key" |
| 33 #define GOOGLE_CLIENT_ID_MAIN "bogus client_id_main" | 110 #define GOOGLE_CLIENT_ID_MAIN "bogus client_id_main" |
| 34 | 111 |
| 35 #include "google_apis/google_api_keys.cc" | 112 // Undef include guard so things get defined again, within this namespace. |
| 36 | 113 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 37 TEST(GoogleAPIKeys, OfficialKeys) { | 114 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
| 38 std::string api_key = g_api_key_cache.Get().api_key(); | 115 #include "google_apis/google_api_keys.cc" |
| 39 std::string id_main = g_api_key_cache.Get().GetClientID(CLIENT_MAIN); | |
| 40 std::string secret_main = g_api_key_cache.Get().GetClientSecret(CLIENT_MAIN); | |
| 41 std::string id_cloud_print = | |
| 42 g_api_key_cache.Get().GetClientID(CLIENT_CLOUD_PRINT); | |
| 43 std::string secret_cloud_print = | |
| 44 g_api_key_cache.Get().GetClientSecret(CLIENT_CLOUD_PRINT); | |
| 45 std::string id_remoting = g_api_key_cache.Get().GetClientID(CLIENT_REMOTING); | |
| 46 std::string secret_remoting = | |
| 47 g_api_key_cache.Get().GetClientSecret(CLIENT_REMOTING); | |
| 48 | |
| 49 ASSERT_TRUE(api_key.size() == 0); | |
| 50 } | |
| 51 | 116 |
| 52 } // namespace official_build | 117 } // namespace official_build |
| 53 #endif // defined(GOOGLE_CHROME_BUILD) or defined(USE_OFFICIAL_GOOGLE_API_KEYS) | 118 |
| 54 | 119 TEST_F(GoogleAPIKeysTest, OfficialKeys) { |
| 55 | 120 namespace testcase = official_build::google_apis; |
| 56 } // namespace | 121 |
| 122 std::string api_key = testcase::g_api_key_cache.Get().api_key(); | |
| 123 std::string id_main = testcase::g_api_key_cache.Get().GetClientID( | |
| 124 testcase::CLIENT_MAIN); | |
| 125 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( | |
| 126 testcase::CLIENT_MAIN); | |
| 127 std::string id_cloud_print = | |
| 128 testcase::g_api_key_cache.Get().GetClientID( | |
| 129 testcase::CLIENT_CLOUD_PRINT); | |
| 130 std::string secret_cloud_print = | |
| 131 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 132 testcase::CLIENT_CLOUD_PRINT); | |
| 133 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( | |
| 134 testcase::CLIENT_REMOTING); | |
| 135 std::string secret_remoting = | |
| 136 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 137 testcase::CLIENT_REMOTING); | |
| 138 | |
| 139 EXPECT_NE(0u, api_key.size()); | |
| 140 EXPECT_NE(DUMMY_API_TOKEN, api_key); | |
| 141 EXPECT_NE("bogus api_key", api_key); | |
| 142 EXPECT_NE(kDefaultNonOfficialAPIKey, api_key); | |
| 143 | |
| 144 EXPECT_NE(0u, id_main.size()); | |
| 145 EXPECT_NE(DUMMY_API_TOKEN, id_main); | |
| 146 EXPECT_NE("bogus client_id_main", id_main); | |
| 147 EXPECT_NE(kDefaultNonOfficialClientID, id_main); | |
| 148 | |
| 149 EXPECT_NE(0u, secret_main.size()); | |
| 150 EXPECT_NE(DUMMY_API_TOKEN, secret_main); | |
| 151 EXPECT_NE(kDefaultNonOfficialClientSecret, secret_main); | |
| 152 | |
| 153 EXPECT_NE(0u, id_cloud_print.size()); | |
| 154 EXPECT_NE(DUMMY_API_TOKEN, id_cloud_print); | |
| 155 EXPECT_NE(kDefaultNonOfficialClientID, id_cloud_print); | |
| 156 | |
| 157 EXPECT_NE(0u, secret_cloud_print.size()); | |
| 158 EXPECT_NE(DUMMY_API_TOKEN, secret_cloud_print); | |
| 159 EXPECT_NE(kDefaultNonOfficialClientSecret, secret_cloud_print); | |
| 160 | |
| 161 EXPECT_NE(0u, id_remoting.size()); | |
| 162 EXPECT_NE(DUMMY_API_TOKEN, id_remoting); | |
| 163 EXPECT_NE(kDefaultNonOfficialClientID, id_remoting); | |
| 164 | |
| 165 EXPECT_NE(0u, secret_remoting.size()); | |
| 166 EXPECT_NE(DUMMY_API_TOKEN, secret_remoting); | |
| 167 EXPECT_NE(kDefaultNonOfficialClientSecret, secret_remoting); | |
| 168 } | |
| 169 #endif // defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) | |
| 170 | |
| 171 // After this test, for the remainder of this compilation unit, we | |
| 172 // need official keys to not be used. | |
| 173 #undef GOOGLE_CHROME_BUILD | |
| 174 #undef USE_OFFICIAL_GOOGLE_API_KEYS | |
| 175 | |
| 176 // Test the set of keys temporarily baked into Chromium by default. | |
| 177 namespace default_keys { | |
| 178 | |
| 179 // We start every test by creating a clean environment for the | |
| 180 // preprocessor defines used in google_api_keys.cc | |
| 181 #undef DUMMY_API_TOKEN | |
| 182 #undef GOOGLE_API_KEY | |
| 183 #undef GOOGLE_CLIENT_ID_MAIN | |
| 184 #undef GOOGLE_CLIENT_SECRET_MAIN | |
| 185 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT | |
| 186 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT | |
| 187 #undef GOOGLE_CLIENT_ID_REMOTING | |
| 188 #undef GOOGLE_CLIENT_SECRET_REMOTING | |
| 189 #undef GOOGLE_DEFAULT_CLIENT_ID | |
| 190 #undef GOOGLE_DEFAULT_CLIENT_SECRET | |
| 191 | |
| 192 // Undef include guard so things get defined again, within this namespace. | |
| 193 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ | |
| 194 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ | |
| 195 #include "google_apis/google_api_keys.cc" | |
| 196 | |
| 197 } // namespace default_keys | |
| 198 | |
| 199 TEST_F(GoogleAPIKeysTest, DefaultKeys) { | |
| 200 namespace testcase = default_keys::google_apis; | |
| 201 | |
| 202 std::string api_key = testcase::g_api_key_cache.Get().api_key(); | |
| 203 std::string id_main = testcase::g_api_key_cache.Get().GetClientID( | |
| 204 testcase::CLIENT_MAIN); | |
| 205 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( | |
| 206 testcase::CLIENT_MAIN); | |
| 207 std::string id_cloud_print = | |
| 208 testcase::g_api_key_cache.Get().GetClientID( | |
| 209 testcase::CLIENT_CLOUD_PRINT); | |
| 210 std::string secret_cloud_print = | |
| 211 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 212 testcase::CLIENT_CLOUD_PRINT); | |
| 213 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( | |
| 214 testcase::CLIENT_REMOTING); | |
| 215 std::string secret_remoting = | |
| 216 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 217 testcase::CLIENT_REMOTING); | |
| 218 | |
| 219 EXPECT_EQ(kDefaultNonOfficialAPIKey, api_key); | |
| 220 EXPECT_EQ(kDefaultNonOfficialClientID, id_main); | |
| 221 EXPECT_EQ(kDefaultNonOfficialClientSecret, secret_main); | |
| 222 EXPECT_EQ(kDefaultNonOfficialClientID, id_cloud_print); | |
| 223 EXPECT_EQ(kDefaultNonOfficialClientSecret, secret_cloud_print); | |
| 224 EXPECT_EQ(kDefaultNonOfficialClientID, id_remoting); | |
| 225 EXPECT_EQ(kDefaultNonOfficialClientSecret, secret_remoting); | |
| 226 } | |
| 227 | |
| 228 // Override a couple of keys, leave the rest default. | |
| 229 namespace override_some_keys { | |
| 230 | |
| 231 // We start every test by creating a clean environment for the | |
| 232 // preprocessor defines used in google_api_keys.cc | |
| 233 #undef DUMMY_API_TOKEN | |
| 234 #undef GOOGLE_API_KEY | |
| 235 #undef GOOGLE_CLIENT_ID_MAIN | |
| 236 #undef GOOGLE_CLIENT_SECRET_MAIN | |
| 237 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT | |
| 238 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT | |
| 239 #undef GOOGLE_CLIENT_ID_REMOTING | |
| 240 #undef GOOGLE_CLIENT_SECRET_REMOTING | |
| 241 #undef GOOGLE_DEFAULT_CLIENT_ID | |
| 242 #undef GOOGLE_DEFAULT_CLIENT_SECRET | |
| 243 | |
| 244 #define GOOGLE_API_KEY "API_KEY override" | |
| 245 #define GOOGLE_CLIENT_ID_REMOTING "CLIENT_ID_REMOTING override" | |
| 246 | |
| 247 // Undef include guard so things get defined again, within this namespace. | |
| 248 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ | |
| 249 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ | |
| 250 #include "google_apis/google_api_keys.cc" | |
| 251 | |
| 252 } // namespace override_some_keys | |
| 253 | |
| 254 TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) { | |
| 255 namespace testcase = override_some_keys::google_apis; | |
| 256 | |
| 257 std::string api_key = testcase::g_api_key_cache.Get().api_key(); | |
| 258 std::string id_main = testcase::g_api_key_cache.Get().GetClientID( | |
| 259 testcase::CLIENT_MAIN); | |
| 260 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( | |
| 261 testcase::CLIENT_MAIN); | |
| 262 std::string id_cloud_print = | |
| 263 testcase::g_api_key_cache.Get().GetClientID( | |
| 264 testcase::CLIENT_CLOUD_PRINT); | |
| 265 std::string secret_cloud_print = | |
| 266 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 267 testcase::CLIENT_CLOUD_PRINT); | |
| 268 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( | |
| 269 testcase::CLIENT_REMOTING); | |
| 270 std::string secret_remoting = | |
| 271 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 272 testcase::CLIENT_REMOTING); | |
| 273 | |
| 274 EXPECT_EQ("API_KEY override", api_key); | |
| 275 EXPECT_EQ(kDefaultNonOfficialClientID, id_main); | |
| 276 EXPECT_EQ(kDefaultNonOfficialClientSecret, secret_main); | |
| 277 EXPECT_EQ(kDefaultNonOfficialClientID, id_cloud_print); | |
| 278 EXPECT_EQ(kDefaultNonOfficialClientSecret, secret_cloud_print); | |
| 279 EXPECT_EQ("CLIENT_ID_REMOTING override", id_remoting); | |
| 280 EXPECT_EQ(kDefaultNonOfficialClientSecret, secret_remoting); | |
| 281 } | |
| 282 | |
| 283 // Override all keys. | |
| 284 namespace override_all_keys { | |
| 285 | |
| 286 // We start every test by creating a clean environment for the | |
| 287 // preprocessor defines used in google_api_keys.cc | |
| 288 #undef DUMMY_API_TOKEN | |
| 289 #undef GOOGLE_API_KEY | |
| 290 #undef GOOGLE_CLIENT_ID_MAIN | |
| 291 #undef GOOGLE_CLIENT_SECRET_MAIN | |
| 292 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT | |
| 293 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT | |
| 294 #undef GOOGLE_CLIENT_ID_REMOTING | |
| 295 #undef GOOGLE_CLIENT_SECRET_REMOTING | |
| 296 #undef GOOGLE_DEFAULT_CLIENT_ID | |
| 297 #undef GOOGLE_DEFAULT_CLIENT_SECRET | |
| 298 | |
| 299 #define GOOGLE_API_KEY "API_KEY" | |
| 300 #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN" | |
| 301 #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN" | |
| 302 #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT" | |
| 303 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT" | |
| 304 #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING" | |
| 305 #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING" | |
| 306 | |
| 307 // Undef include guard so things get defined again, within this namespace. | |
| 308 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ | |
| 309 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ | |
| 310 #include "google_apis/google_api_keys.cc" | |
| 311 | |
| 312 } // namespace override_all_keys | |
| 313 | |
| 314 TEST_F(GoogleAPIKeysTest, OverrideAllKeys) { | |
| 315 namespace testcase = override_all_keys::google_apis; | |
| 316 | |
| 317 std::string api_key = testcase::g_api_key_cache.Get().api_key(); | |
| 318 std::string id_main = testcase::g_api_key_cache.Get().GetClientID( | |
| 319 testcase::CLIENT_MAIN); | |
| 320 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( | |
| 321 testcase::CLIENT_MAIN); | |
| 322 std::string id_cloud_print = | |
| 323 testcase::g_api_key_cache.Get().GetClientID( | |
| 324 testcase::CLIENT_CLOUD_PRINT); | |
| 325 std::string secret_cloud_print = | |
| 326 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 327 testcase::CLIENT_CLOUD_PRINT); | |
| 328 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( | |
| 329 testcase::CLIENT_REMOTING); | |
| 330 std::string secret_remoting = | |
| 331 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 332 testcase::CLIENT_REMOTING); | |
| 333 | |
| 334 EXPECT_EQ("API_KEY", api_key); | |
| 335 EXPECT_EQ("ID_MAIN", id_main); | |
| 336 EXPECT_EQ("SECRET_MAIN", secret_main); | |
| 337 EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print); | |
| 338 EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print); | |
| 339 EXPECT_EQ("ID_REMOTING", id_remoting); | |
| 340 EXPECT_EQ("SECRET_REMOTING", secret_remoting); | |
| 341 } | |
| 342 | |
| 343 // Override all keys using both preprocessor defines and environment | |
| 344 // variables. The environment variables should win. | |
| 345 namespace override_all_keys_env { | |
| 346 | |
| 347 // We start every test by creating a clean environment for the | |
| 348 // preprocessor defines used in google_api_keys.cc | |
| 349 #undef DUMMY_API_TOKEN | |
| 350 #undef GOOGLE_API_KEY | |
| 351 #undef GOOGLE_CLIENT_ID_MAIN | |
| 352 #undef GOOGLE_CLIENT_SECRET_MAIN | |
| 353 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT | |
| 354 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT | |
| 355 #undef GOOGLE_CLIENT_ID_REMOTING | |
| 356 #undef GOOGLE_CLIENT_SECRET_REMOTING | |
| 357 #undef GOOGLE_DEFAULT_CLIENT_ID | |
| 358 #undef GOOGLE_DEFAULT_CLIENT_SECRET | |
| 359 | |
| 360 #define GOOGLE_API_KEY "API_KEY" | |
| 361 #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN" | |
| 362 #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN" | |
| 363 #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT" | |
| 364 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT" | |
| 365 #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING" | |
| 366 #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING" | |
| 367 | |
| 368 // Undef include guard so things get defined again, within this namespace. | |
| 369 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ | |
| 370 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ | |
| 371 #include "google_apis/google_api_keys.cc" | |
| 372 | |
| 373 } // namespace override_all_keys_env | |
| 374 | |
| 375 TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) { | |
| 376 namespace testcase = override_all_keys_env::google_apis; | |
| 377 | |
| 378 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 379 env->SetVar("GOOGLE_API_KEY", "env-API_KEY"); | |
| 380 env->SetVar("GOOGLE_CLIENT_ID_MAIN", "env-ID_MAIN"); | |
| 381 env->SetVar("GOOGLE_CLIENT_ID_CLOUD_PRINT", "env-ID_CLOUD_PRINT"); | |
| 382 env->SetVar("GOOGLE_CLIENT_ID_REMOTING", "env-ID_REMOTING"); | |
| 383 env->SetVar("GOOGLE_CLIENT_SECRET_MAIN", "env-SECRET_MAIN"); | |
| 384 env->SetVar("GOOGLE_CLIENT_SECRET_CLOUD_PRINT", "env-SECRET_CLOUD_PRINT"); | |
| 385 env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING", "env-SECRET_REMOTING"); | |
| 386 | |
| 387 // It's important that the first call to Get() only happen after the | |
| 388 // environment variables have been set. | |
| 389 std::string api_key = testcase::g_api_key_cache.Get().api_key(); | |
| 390 std::string id_main = testcase::g_api_key_cache.Get().GetClientID( | |
| 391 testcase::CLIENT_MAIN); | |
| 392 std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( | |
| 393 testcase::CLIENT_MAIN); | |
| 394 std::string id_cloud_print = | |
| 395 testcase::g_api_key_cache.Get().GetClientID( | |
| 396 testcase::CLIENT_CLOUD_PRINT); | |
| 397 std::string secret_cloud_print = | |
| 398 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 399 testcase::CLIENT_CLOUD_PRINT); | |
| 400 std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( | |
| 401 testcase::CLIENT_REMOTING); | |
| 402 std::string secret_remoting = | |
| 403 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 404 testcase::CLIENT_REMOTING); | |
| 405 | |
| 406 EXPECT_EQ("env-API_KEY", api_key); | |
| 407 EXPECT_EQ("env-ID_MAIN", id_main); | |
| 408 EXPECT_EQ("env-SECRET_MAIN", secret_main); | |
| 409 EXPECT_EQ("env-ID_CLOUD_PRINT", id_cloud_print); | |
| 410 EXPECT_EQ("env-SECRET_CLOUD_PRINT", secret_cloud_print); | |
| 411 EXPECT_EQ("env-ID_REMOTING", id_remoting); | |
| 412 EXPECT_EQ("env-SECRET_REMOTING", secret_remoting); | |
| 413 } | |
| OLD | NEW |