[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 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 | |
| 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 | |
[email protected] | e03604b | 2013-01-10 23:38:22 | [diff] [blame] | 13 | #include "google_apis/google_api_keys.h" |
| 14 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | 850b353e | 2014-02-11 15:56:28 | [diff] [blame] | 16 | #include "google_apis/gaia/gaia_switches.h" |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 19 | // 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] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 26 | |
[email protected] | e03604b | 2013-01-10 23:38:22 | [diff] [blame] | 27 | // 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. |
| 33 | #include <string> |
| 34 | #include "base/command_line.h" |
| 35 | #include "base/environment.h" |
| 36 | #include "base/lazy_instance.h" |
| 37 | #include "base/logging.h" |
| 38 | #include "base/memory/scoped_ptr.h" |
[email protected] | 8edbebd | 2013-01-31 19:45:02 | [diff] [blame] | 39 | #include "base/strings/stringize_macros.h" |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 40 | |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 41 | // This is the default baked-in value for OAuth IDs and secrets. |
| 42 | static const char kDummyToken[] = "dummytoken"; |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 43 | |
| 44 | struct EnvironmentCache { |
| 45 | public: |
| 46 | EnvironmentCache() : variable_name(NULL), was_set(false) {} |
| 47 | |
| 48 | const char* variable_name; |
| 49 | bool was_set; |
| 50 | std::string value; |
| 51 | }; |
| 52 | |
| 53 | class GoogleAPIKeysTest : public testing::Test { |
| 54 | public: |
| 55 | GoogleAPIKeysTest() : env_(base::Environment::Create()) { |
| 56 | env_cache_[0].variable_name = "GOOGLE_API_KEY"; |
| 57 | env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN"; |
| 58 | env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN"; |
| 59 | env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT"; |
| 60 | env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT"; |
| 61 | env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING"; |
| 62 | env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING"; |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 63 | env_cache_[7].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST"; |
| 64 | env_cache_[8].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST"; |
| 65 | env_cache_[9].variable_name = "GOOGLE_DEFAULT_CLIENT_ID"; |
| 66 | env_cache_[10].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET"; |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 59e6b80 | 2013-02-07 13:29:33 | [diff] [blame] | 69 | virtual void SetUp() { |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 70 | // Unset all environment variables that can affect these tests, |
| 71 | // for the duration of the tests. |
| 72 | for (size_t i = 0; i < arraysize(env_cache_); ++i) { |
| 73 | EnvironmentCache& cache = env_cache_[i]; |
| 74 | cache.was_set = env_->HasVar(cache.variable_name); |
| 75 | cache.value.clear(); |
| 76 | if (cache.was_set) { |
| 77 | env_->GetVar(cache.variable_name, &cache.value); |
| 78 | env_->UnSetVar(cache.variable_name); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
[email protected] | 59e6b80 | 2013-02-07 13:29:33 | [diff] [blame] | 83 | virtual void TearDown() { |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 84 | // Restore environment. |
| 85 | for (size_t i = 0; i < arraysize(env_cache_); ++i) { |
| 86 | EnvironmentCache& cache = env_cache_[i]; |
| 87 | if (cache.was_set) { |
| 88 | env_->SetVar(cache.variable_name, cache.value); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | scoped_ptr<base::Environment> env_; |
| 95 | |
| 96 | // Why 3? It is for GOOGLE_API_KEY, GOOGLE_DEFAULT_CLIENT_ID and |
| 97 | // GOOGLE_DEFAULT_CLIENT_SECRET. |
| 98 | // |
| 99 | // Why 2 times CLIENT_NUM_ITEMS? This is the number of different |
| 100 | // clients in the OAuth2Client enumeration, and for each of these we |
| 101 | // have both an ID and a secret. |
| 102 | EnvironmentCache env_cache_[3 + 2 * google_apis::CLIENT_NUM_ITEMS]; |
| 103 | }; |
| 104 | |
| 105 | #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 106 | // Test official build behavior, since we are in a checkout where this |
| 107 | // is possible. |
| 108 | namespace official_build { |
| 109 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 110 | // We start every test by creating a clean environment for the |
| 111 | // preprocessor defines used in google_api_keys.cc |
| 112 | #undef DUMMY_API_TOKEN |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 113 | #undef GOOGLE_API_KEY |
| 114 | #undef GOOGLE_CLIENT_ID_MAIN |
| 115 | #undef GOOGLE_CLIENT_SECRET_MAIN |
| 116 | #undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
| 117 | #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
| 118 | #undef GOOGLE_CLIENT_ID_REMOTING |
| 119 | #undef GOOGLE_CLIENT_SECRET_REMOTING |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 120 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 121 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 122 | #undef GOOGLE_DEFAULT_CLIENT_ID |
| 123 | #undef GOOGLE_DEFAULT_CLIENT_SECRET |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 124 | |
| 125 | // Try setting some keys, these should be ignored since it's a build |
| 126 | // with official keys. |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 127 | #define GOOGLE_API_KEY "bogus api_key" |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 128 | #define GOOGLE_CLIENT_ID_MAIN "bogus client_id_main" |
| 129 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 130 | // Undef include guard so things get defined again, within this namespace. |
| 131 | #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 132 | #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 133 | #include "google_apis/google_api_keys.cc" |
| 134 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 135 | } // namespace official_build |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 136 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 137 | TEST_F(GoogleAPIKeysTest, OfficialKeys) { |
| 138 | namespace testcase = official_build::google_apis; |
| 139 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 140 | EXPECT_TRUE(testcase::HasKeysConfigured()); |
| 141 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 142 | std::string api_key = testcase::g_api_key_cache.Get().api_key(); |
| 143 | std::string id_main = testcase::g_api_key_cache.Get().GetClientID( |
| 144 | testcase::CLIENT_MAIN); |
| 145 | std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( |
| 146 | testcase::CLIENT_MAIN); |
| 147 | std::string id_cloud_print = |
| 148 | testcase::g_api_key_cache.Get().GetClientID( |
| 149 | testcase::CLIENT_CLOUD_PRINT); |
| 150 | std::string secret_cloud_print = |
| 151 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 152 | testcase::CLIENT_CLOUD_PRINT); |
| 153 | std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( |
| 154 | testcase::CLIENT_REMOTING); |
| 155 | std::string secret_remoting = |
| 156 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 157 | testcase::CLIENT_REMOTING); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 158 | std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( |
| 159 | testcase::CLIENT_REMOTING_HOST); |
| 160 | std::string secret_remoting_host = |
| 161 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 162 | testcase::CLIENT_REMOTING_HOST); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 163 | |
| 164 | EXPECT_NE(0u, api_key.size()); |
| 165 | EXPECT_NE(DUMMY_API_TOKEN, api_key); |
| 166 | EXPECT_NE("bogus api_key", api_key); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 167 | EXPECT_NE(kDummyToken, api_key); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 168 | |
| 169 | EXPECT_NE(0u, id_main.size()); |
| 170 | EXPECT_NE(DUMMY_API_TOKEN, id_main); |
| 171 | EXPECT_NE("bogus client_id_main", id_main); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 172 | EXPECT_NE(kDummyToken, id_main); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 173 | |
| 174 | EXPECT_NE(0u, secret_main.size()); |
| 175 | EXPECT_NE(DUMMY_API_TOKEN, secret_main); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 176 | EXPECT_NE(kDummyToken, secret_main); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 177 | |
| 178 | EXPECT_NE(0u, id_cloud_print.size()); |
| 179 | EXPECT_NE(DUMMY_API_TOKEN, id_cloud_print); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 180 | EXPECT_NE(kDummyToken, id_cloud_print); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 181 | |
| 182 | EXPECT_NE(0u, secret_cloud_print.size()); |
| 183 | EXPECT_NE(DUMMY_API_TOKEN, secret_cloud_print); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 184 | EXPECT_NE(kDummyToken, secret_cloud_print); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 185 | |
| 186 | EXPECT_NE(0u, id_remoting.size()); |
| 187 | EXPECT_NE(DUMMY_API_TOKEN, id_remoting); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 188 | EXPECT_NE(kDummyToken, id_remoting); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 189 | |
| 190 | EXPECT_NE(0u, secret_remoting.size()); |
| 191 | EXPECT_NE(DUMMY_API_TOKEN, secret_remoting); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 192 | EXPECT_NE(kDummyToken, secret_remoting); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 193 | |
| 194 | EXPECT_NE(0u, id_remoting_host.size()); |
| 195 | EXPECT_NE(DUMMY_API_TOKEN, id_remoting_host); |
| 196 | EXPECT_NE(kDummyToken, id_remoting_host); |
| 197 | |
| 198 | EXPECT_NE(0u, secret_remoting_host.size()); |
| 199 | EXPECT_NE(DUMMY_API_TOKEN, secret_remoting_host); |
| 200 | EXPECT_NE(kDummyToken, secret_remoting_host); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 201 | } |
| 202 | #endif // defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) |
| 203 | |
| 204 | // After this test, for the remainder of this compilation unit, we |
| 205 | // need official keys to not be used. |
| 206 | #undef GOOGLE_CHROME_BUILD |
| 207 | #undef USE_OFFICIAL_GOOGLE_API_KEYS |
| 208 | |
| 209 | // Test the set of keys temporarily baked into Chromium by default. |
| 210 | namespace default_keys { |
| 211 | |
| 212 | // We start every test by creating a clean environment for the |
| 213 | // preprocessor defines used in google_api_keys.cc |
| 214 | #undef DUMMY_API_TOKEN |
| 215 | #undef GOOGLE_API_KEY |
| 216 | #undef GOOGLE_CLIENT_ID_MAIN |
| 217 | #undef GOOGLE_CLIENT_SECRET_MAIN |
| 218 | #undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
| 219 | #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
| 220 | #undef GOOGLE_CLIENT_ID_REMOTING |
| 221 | #undef GOOGLE_CLIENT_SECRET_REMOTING |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 222 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 223 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 224 | #undef GOOGLE_DEFAULT_CLIENT_ID |
| 225 | #undef GOOGLE_DEFAULT_CLIENT_SECRET |
| 226 | |
| 227 | // Undef include guard so things get defined again, within this namespace. |
| 228 | #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 229 | #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
| 230 | #include "google_apis/google_api_keys.cc" |
| 231 | |
| 232 | } // namespace default_keys |
| 233 | |
| 234 | TEST_F(GoogleAPIKeysTest, DefaultKeys) { |
| 235 | namespace testcase = default_keys::google_apis; |
| 236 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 237 | EXPECT_FALSE(testcase::HasKeysConfigured()); |
| 238 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 239 | std::string api_key = testcase::g_api_key_cache.Get().api_key(); |
| 240 | std::string id_main = testcase::g_api_key_cache.Get().GetClientID( |
| 241 | testcase::CLIENT_MAIN); |
| 242 | std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( |
| 243 | testcase::CLIENT_MAIN); |
| 244 | std::string id_cloud_print = |
| 245 | testcase::g_api_key_cache.Get().GetClientID( |
| 246 | testcase::CLIENT_CLOUD_PRINT); |
| 247 | std::string secret_cloud_print = |
| 248 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 249 | testcase::CLIENT_CLOUD_PRINT); |
| 250 | std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( |
| 251 | testcase::CLIENT_REMOTING); |
| 252 | std::string secret_remoting = |
| 253 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 254 | testcase::CLIENT_REMOTING); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 255 | std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( |
| 256 | testcase::CLIENT_REMOTING_HOST); |
| 257 | std::string secret_remoting_host = |
| 258 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 259 | testcase::CLIENT_REMOTING_HOST); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 260 | |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 261 | EXPECT_EQ(kDummyToken, api_key); |
| 262 | EXPECT_EQ(kDummyToken, id_main); |
| 263 | EXPECT_EQ(kDummyToken, secret_main); |
| 264 | EXPECT_EQ(kDummyToken, id_cloud_print); |
| 265 | EXPECT_EQ(kDummyToken, secret_cloud_print); |
| 266 | EXPECT_EQ(kDummyToken, id_remoting); |
| 267 | EXPECT_EQ(kDummyToken, secret_remoting); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 268 | EXPECT_EQ(kDummyToken, id_remoting_host); |
| 269 | EXPECT_EQ(kDummyToken, secret_remoting_host); |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 270 | } |
| 271 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 272 | // Override a couple of keys, leave the rest default. |
| 273 | namespace override_some_keys { |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 274 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 275 | // We start every test by creating a clean environment for the |
| 276 | // preprocessor defines used in google_api_keys.cc |
| 277 | #undef DUMMY_API_TOKEN |
| 278 | #undef GOOGLE_API_KEY |
| 279 | #undef GOOGLE_CLIENT_ID_MAIN |
| 280 | #undef GOOGLE_CLIENT_SECRET_MAIN |
| 281 | #undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
| 282 | #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
| 283 | #undef GOOGLE_CLIENT_ID_REMOTING |
| 284 | #undef GOOGLE_CLIENT_SECRET_REMOTING |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 285 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 286 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 287 | #undef GOOGLE_DEFAULT_CLIENT_ID |
| 288 | #undef GOOGLE_DEFAULT_CLIENT_SECRET |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 289 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 290 | #define GOOGLE_API_KEY "API_KEY override" |
| 291 | #define GOOGLE_CLIENT_ID_REMOTING "CLIENT_ID_REMOTING override" |
| 292 | |
| 293 | // Undef include guard so things get defined again, within this namespace. |
| 294 | #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 295 | #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
| 296 | #include "google_apis/google_api_keys.cc" |
| 297 | |
| 298 | } // namespace override_some_keys |
| 299 | |
| 300 | TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) { |
| 301 | namespace testcase = override_some_keys::google_apis; |
| 302 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 303 | EXPECT_FALSE(testcase::HasKeysConfigured()); |
| 304 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 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); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 321 | std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( |
| 322 | testcase::CLIENT_REMOTING_HOST); |
| 323 | std::string secret_remoting_host = |
| 324 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 325 | testcase::CLIENT_REMOTING_HOST); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 326 | |
| 327 | EXPECT_EQ("API_KEY override", api_key); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 328 | EXPECT_EQ(kDummyToken, id_main); |
| 329 | EXPECT_EQ(kDummyToken, secret_main); |
| 330 | EXPECT_EQ(kDummyToken, id_cloud_print); |
| 331 | EXPECT_EQ(kDummyToken, secret_cloud_print); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 332 | EXPECT_EQ("CLIENT_ID_REMOTING override", id_remoting); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 333 | EXPECT_EQ(kDummyToken, secret_remoting); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 334 | EXPECT_EQ(kDummyToken, id_remoting_host); |
| 335 | EXPECT_EQ(kDummyToken, secret_remoting_host); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Override all keys. |
| 339 | namespace override_all_keys { |
| 340 | |
| 341 | // We start every test by creating a clean environment for the |
| 342 | // preprocessor defines used in google_api_keys.cc |
| 343 | #undef DUMMY_API_TOKEN |
| 344 | #undef GOOGLE_API_KEY |
| 345 | #undef GOOGLE_CLIENT_ID_MAIN |
| 346 | #undef GOOGLE_CLIENT_SECRET_MAIN |
| 347 | #undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
| 348 | #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
| 349 | #undef GOOGLE_CLIENT_ID_REMOTING |
| 350 | #undef GOOGLE_CLIENT_SECRET_REMOTING |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 351 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 352 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 353 | #undef GOOGLE_DEFAULT_CLIENT_ID |
| 354 | #undef GOOGLE_DEFAULT_CLIENT_SECRET |
| 355 | |
| 356 | #define GOOGLE_API_KEY "API_KEY" |
| 357 | #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN" |
| 358 | #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN" |
| 359 | #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT" |
| 360 | #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT" |
| 361 | #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING" |
| 362 | #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING" |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 363 | #define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST" |
| 364 | #define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST" |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 365 | |
| 366 | // Undef include guard so things get defined again, within this namespace. |
| 367 | #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 368 | #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
| 369 | #include "google_apis/google_api_keys.cc" |
| 370 | |
| 371 | } // namespace override_all_keys |
| 372 | |
| 373 | TEST_F(GoogleAPIKeysTest, OverrideAllKeys) { |
| 374 | namespace testcase = override_all_keys::google_apis; |
| 375 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 376 | EXPECT_TRUE(testcase::HasKeysConfigured()); |
| 377 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 378 | std::string api_key = testcase::g_api_key_cache.Get().api_key(); |
| 379 | std::string id_main = testcase::g_api_key_cache.Get().GetClientID( |
| 380 | testcase::CLIENT_MAIN); |
| 381 | std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( |
| 382 | testcase::CLIENT_MAIN); |
| 383 | std::string id_cloud_print = |
| 384 | testcase::g_api_key_cache.Get().GetClientID( |
| 385 | testcase::CLIENT_CLOUD_PRINT); |
| 386 | std::string secret_cloud_print = |
| 387 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 388 | testcase::CLIENT_CLOUD_PRINT); |
| 389 | std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( |
| 390 | testcase::CLIENT_REMOTING); |
| 391 | std::string secret_remoting = |
| 392 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 393 | testcase::CLIENT_REMOTING); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 394 | std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( |
| 395 | testcase::CLIENT_REMOTING_HOST); |
| 396 | std::string secret_remoting_host = |
| 397 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 398 | testcase::CLIENT_REMOTING_HOST); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 399 | |
| 400 | EXPECT_EQ("API_KEY", api_key); |
| 401 | EXPECT_EQ("ID_MAIN", id_main); |
| 402 | EXPECT_EQ("SECRET_MAIN", secret_main); |
| 403 | EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print); |
| 404 | EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print); |
| 405 | EXPECT_EQ("ID_REMOTING", id_remoting); |
| 406 | EXPECT_EQ("SECRET_REMOTING", secret_remoting); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 407 | EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host); |
| 408 | EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Override all keys using both preprocessor defines and environment |
| 412 | // variables. The environment variables should win. |
| 413 | namespace override_all_keys_env { |
| 414 | |
| 415 | // We start every test by creating a clean environment for the |
| 416 | // preprocessor defines used in google_api_keys.cc |
| 417 | #undef DUMMY_API_TOKEN |
| 418 | #undef GOOGLE_API_KEY |
| 419 | #undef GOOGLE_CLIENT_ID_MAIN |
| 420 | #undef GOOGLE_CLIENT_SECRET_MAIN |
| 421 | #undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
| 422 | #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
| 423 | #undef GOOGLE_CLIENT_ID_REMOTING |
| 424 | #undef GOOGLE_CLIENT_SECRET_REMOTING |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 425 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 426 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 427 | #undef GOOGLE_DEFAULT_CLIENT_ID |
| 428 | #undef GOOGLE_DEFAULT_CLIENT_SECRET |
| 429 | |
| 430 | #define GOOGLE_API_KEY "API_KEY" |
| 431 | #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN" |
| 432 | #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN" |
| 433 | #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT" |
| 434 | #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT" |
| 435 | #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING" |
| 436 | #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING" |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 437 | #define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST" |
| 438 | #define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST" |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 439 | |
| 440 | // Undef include guard so things get defined again, within this namespace. |
| 441 | #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 442 | #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
| 443 | #include "google_apis/google_api_keys.cc" |
| 444 | |
| 445 | } // namespace override_all_keys_env |
| 446 | |
| 447 | TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) { |
| 448 | namespace testcase = override_all_keys_env::google_apis; |
| 449 | |
| 450 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 451 | env->SetVar("GOOGLE_API_KEY", "env-API_KEY"); |
| 452 | env->SetVar("GOOGLE_CLIENT_ID_MAIN", "env-ID_MAIN"); |
| 453 | env->SetVar("GOOGLE_CLIENT_ID_CLOUD_PRINT", "env-ID_CLOUD_PRINT"); |
| 454 | env->SetVar("GOOGLE_CLIENT_ID_REMOTING", "env-ID_REMOTING"); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 455 | env->SetVar("GOOGLE_CLIENT_ID_REMOTING_HOST", "env-ID_REMOTING_HOST"); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 456 | env->SetVar("GOOGLE_CLIENT_SECRET_MAIN", "env-SECRET_MAIN"); |
| 457 | env->SetVar("GOOGLE_CLIENT_SECRET_CLOUD_PRINT", "env-SECRET_CLOUD_PRINT"); |
| 458 | env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING", "env-SECRET_REMOTING"); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 459 | env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING_HOST", "env-SECRET_REMOTING_HOST"); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 460 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 461 | EXPECT_TRUE(testcase::HasKeysConfigured()); |
| 462 | |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 463 | // It's important that the first call to Get() only happen after the |
| 464 | // environment variables have been set. |
| 465 | std::string api_key = testcase::g_api_key_cache.Get().api_key(); |
| 466 | std::string id_main = testcase::g_api_key_cache.Get().GetClientID( |
| 467 | testcase::CLIENT_MAIN); |
| 468 | std::string secret_main = testcase::g_api_key_cache.Get().GetClientSecret( |
| 469 | testcase::CLIENT_MAIN); |
| 470 | std::string id_cloud_print = |
| 471 | testcase::g_api_key_cache.Get().GetClientID( |
| 472 | testcase::CLIENT_CLOUD_PRINT); |
| 473 | std::string secret_cloud_print = |
| 474 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 475 | testcase::CLIENT_CLOUD_PRINT); |
| 476 | std::string id_remoting = testcase::g_api_key_cache.Get().GetClientID( |
| 477 | testcase::CLIENT_REMOTING); |
| 478 | std::string secret_remoting = |
| 479 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 480 | testcase::CLIENT_REMOTING); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 481 | std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( |
| 482 | testcase::CLIENT_REMOTING_HOST); |
| 483 | std::string secret_remoting_host = |
| 484 | testcase::g_api_key_cache.Get().GetClientSecret( |
| 485 | testcase::CLIENT_REMOTING_HOST); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 486 | |
| 487 | EXPECT_EQ("env-API_KEY", api_key); |
| 488 | EXPECT_EQ("env-ID_MAIN", id_main); |
| 489 | EXPECT_EQ("env-SECRET_MAIN", secret_main); |
| 490 | EXPECT_EQ("env-ID_CLOUD_PRINT", id_cloud_print); |
| 491 | EXPECT_EQ("env-SECRET_CLOUD_PRINT", secret_cloud_print); |
| 492 | EXPECT_EQ("env-ID_REMOTING", id_remoting); |
| 493 | EXPECT_EQ("env-SECRET_REMOTING", secret_remoting); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 494 | EXPECT_EQ("env-ID_REMOTING_HOST", id_remoting_host); |
| 495 | EXPECT_EQ("env-SECRET_REMOTING_HOST", secret_remoting_host); |
[email protected] | 65eae70 | 2012-09-21 13:55:47 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | #endif // defined(OS_LINUX) || defined(OS_MACOSX) |