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