[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 | |
bzanotti | bd01460 | 2016-08-10 18:42:58 | [diff] [blame] | 13 | #include "google_apis/google_api_keys_unittest.h" |
dcheng | f064ccc | 2016-04-08 17:35:40 | [diff] [blame] | 14 | |
avi | b32eea9b | 2015-12-22 22:35:00 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | 850b353e | 2014-02-11 15:56:28 | [diff] [blame] | 17 | #include "google_apis/gaia/gaia_switches.h" |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 18 | |
[email protected] | 65eae708 | 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. |
avi | b32eea9b | 2015-12-22 22:35:00 | [diff] [blame] | 33 | |
| 34 | #include <stddef.h> |
| 35 | |
[email protected] | e03604b | 2013-01-10 23:38:22 | [diff] [blame] | 36 | #include <string> |
| 37 | #include "base/command_line.h" |
[email protected] | e03604b | 2013-01-10 23:38:22 | [diff] [blame] | 38 | #include "base/lazy_instance.h" |
| 39 | #include "base/logging.h" |
[email protected] | 8edbebd | 2013-01-31 19:45:02 | [diff] [blame] | 40 | #include "base/strings/stringize_macros.h" |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 41 | |
bzanotti | bd01460 | 2016-08-10 18:42:58 | [diff] [blame] | 42 | #if defined(OS_MACOSX) |
| 43 | #include "google_apis/google_api_keys_mac.h" |
| 44 | #endif |
| 45 | |
| 46 | GoogleAPIKeysTest::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 | |
| 62 | GoogleAPIKeysTest::~GoogleAPIKeysTest() {} |
| 63 | |
| 64 | void 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 | |
| 78 | void 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] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 88 | // This is the default baked-in value for OAuth IDs and secrets. |
| 89 | static const char kDummyToken[] = "dummytoken"; |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 90 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 91 | #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 92 | // Test official build behavior, since we are in a checkout where this |
| 93 | // is possible. |
| 94 | namespace official_build { |
| 95 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 96 | // 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] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 99 | #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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 106 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 107 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 108 | #undef GOOGLE_DEFAULT_CLIENT_ID |
| 109 | #undef GOOGLE_DEFAULT_CLIENT_SECRET |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 110 | |
| 111 | // Try setting some keys, these should be ignored since it's a build |
| 112 | // with official keys. |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 113 | #define GOOGLE_API_KEY "bogus api_key" |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 114 | #define GOOGLE_CLIENT_ID_MAIN "bogus client_id_main" |
| 115 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 116 | // 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] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 119 | #include "google_apis/google_api_keys.cc" |
| 120 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 121 | } // namespace official_build |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 122 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 123 | TEST_F(GoogleAPIKeysTest, OfficialKeys) { |
| 124 | namespace testcase = official_build::google_apis; |
| 125 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 126 | EXPECT_TRUE(testcase::HasKeysConfigured()); |
| 127 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 128 | 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 144 | 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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 149 | |
| 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] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 153 | EXPECT_NE(kDummyToken, api_key); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 154 | |
| 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] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 158 | EXPECT_NE(kDummyToken, id_main); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 159 | |
| 160 | EXPECT_NE(0u, secret_main.size()); |
| 161 | EXPECT_NE(DUMMY_API_TOKEN, secret_main); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 162 | EXPECT_NE(kDummyToken, secret_main); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 163 | |
| 164 | EXPECT_NE(0u, id_cloud_print.size()); |
| 165 | EXPECT_NE(DUMMY_API_TOKEN, id_cloud_print); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 166 | EXPECT_NE(kDummyToken, id_cloud_print); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 167 | |
| 168 | EXPECT_NE(0u, secret_cloud_print.size()); |
| 169 | EXPECT_NE(DUMMY_API_TOKEN, secret_cloud_print); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 170 | EXPECT_NE(kDummyToken, secret_cloud_print); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 171 | |
| 172 | EXPECT_NE(0u, id_remoting.size()); |
| 173 | EXPECT_NE(DUMMY_API_TOKEN, id_remoting); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 174 | EXPECT_NE(kDummyToken, id_remoting); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 175 | |
| 176 | EXPECT_NE(0u, secret_remoting.size()); |
| 177 | EXPECT_NE(DUMMY_API_TOKEN, secret_remoting); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 178 | EXPECT_NE(kDummyToken, secret_remoting); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 179 | |
| 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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 187 | } |
| 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. |
| 196 | namespace 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 208 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 209 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 210 | #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 | |
| 220 | TEST_F(GoogleAPIKeysTest, DefaultKeys) { |
| 221 | namespace testcase = default_keys::google_apis; |
| 222 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 223 | EXPECT_FALSE(testcase::HasKeysConfigured()); |
| 224 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 225 | 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 241 | 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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 246 | |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 247 | 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 254 | EXPECT_EQ(kDummyToken, id_remoting_host); |
| 255 | EXPECT_EQ(kDummyToken, secret_remoting_host); |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 258 | // Override a couple of keys, leave the rest default. |
| 259 | namespace override_some_keys { |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 260 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 261 | // 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 271 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 272 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 273 | #undef GOOGLE_DEFAULT_CLIENT_ID |
| 274 | #undef GOOGLE_DEFAULT_CLIENT_SECRET |
[email protected] | d647e28 | 2012-09-13 13:10:10 | [diff] [blame] | 275 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 276 | #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 | |
| 286 | TEST_F(GoogleAPIKeysTest, OverrideSomeKeys) { |
| 287 | namespace testcase = override_some_keys::google_apis; |
| 288 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 289 | EXPECT_FALSE(testcase::HasKeysConfigured()); |
| 290 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 291 | 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 307 | 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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 312 | |
| 313 | EXPECT_EQ("API_KEY override", api_key); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 314 | 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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 318 | EXPECT_EQ("CLIENT_ID_REMOTING override", id_remoting); |
[email protected] | 1abac7a | 2013-01-31 14:01:25 | [diff] [blame] | 319 | EXPECT_EQ(kDummyToken, secret_remoting); |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 320 | EXPECT_EQ(kDummyToken, id_remoting_host); |
| 321 | EXPECT_EQ(kDummyToken, secret_remoting_host); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // Override all keys. |
| 325 | namespace 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 337 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 338 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 339 | #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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 349 | #define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST" |
| 350 | #define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST" |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 351 | |
| 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 | |
| 359 | TEST_F(GoogleAPIKeysTest, OverrideAllKeys) { |
| 360 | namespace testcase = override_all_keys::google_apis; |
| 361 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 362 | EXPECT_TRUE(testcase::HasKeysConfigured()); |
| 363 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 364 | 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 380 | 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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 385 | |
| 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 393 | EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host); |
| 394 | EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 395 | } |
| 396 | |
robertshield | 156c709 | 2017-06-20 21:26:51 | [diff] [blame] | 397 | #if !defined(GOOGLE_CHROME_BUILD) |
| 398 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 399 | // Override all keys using both preprocessor defines and environment |
| 400 | // variables. The environment variables should win. |
| 401 | namespace 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 413 | #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 414 | #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 415 | #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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 425 | #define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST" |
| 426 | #define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST" |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 427 | |
| 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 | |
| 435 | TEST_F(GoogleAPIKeysTest, OverrideAllKeysUsingEnvironment) { |
| 436 | namespace testcase = override_all_keys_env::google_apis; |
| 437 | |
dcheng | f064ccc | 2016-04-08 17:35:40 | [diff] [blame] | 438 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 439 | 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 443 | env->SetVar("GOOGLE_CLIENT_ID_REMOTING_HOST", "env-ID_REMOTING_HOST"); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 444 | 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 447 | env->SetVar("GOOGLE_CLIENT_SECRET_REMOTING_HOST", "env-SECRET_REMOTING_HOST"); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 448 | |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 449 | EXPECT_TRUE(testcase::HasKeysConfigured()); |
| 450 | |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 451 | // 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 469 | 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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 474 | |
| 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] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 482 | EXPECT_EQ("env-ID_REMOTING_HOST", id_remoting_host); |
| 483 | EXPECT_EQ("env-SECRET_REMOTING_HOST", secret_remoting_host); |
[email protected] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 484 | } |
| 485 | |
robertshield | 156c709 | 2017-06-20 21:26:51 | [diff] [blame] | 486 | #endif // !defined(GOOGLE_CHROME_BUILD) |
| 487 | |
jzw | 2d292082 | 2017-04-20 09:19:29 | [diff] [blame] | 488 | #if defined(OS_IOS) |
| 489 | // Override all keys using both preprocessor defines and setters. |
| 490 | // Setters should win. |
| 491 | namespace 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 | |
| 525 | TEST_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] | 65eae708 | 2012-09-21 13:55:47 | [diff] [blame] | 578 | #endif // defined(OS_LINUX) || defined(OS_MACOSX) |