Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 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 | |
| 13 #include "google_apis/google_api_keys.h" | |
| 14 | |
| 15 #include <memory> | |
| 16 | |
| 17 #include "base/mac/bundle_locations.h" | |
| 18 #include "base/macros.h" | |
| 19 #include "build/build_config.h" | |
| 20 #include "google_apis/gaia/gaia_switches.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 #import "third_party/ocmock/OCMock/OCMock.h" | |
| 23 | |
| 24 // We need to include everything included by google_api_keys.cc once | |
| 25 // at global scope so that things like STL and classes from base don't | |
| 26 // get defined when we re-include the google_api_keys.cc file | |
| 27 // below. We used to include that file in its entirety here, but that | |
| 28 // can cause problems if the linker decides the version of symbols | |
| 29 // from that file included here is the "right" version. | |
| 30 | |
| 31 #include <stddef.h> | |
| 32 | |
| 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/strings/stringize_macros.h" | |
| 39 #include "google_apis/google_api_keys_mac.h" | |
| 40 | |
| 41 struct EnvironmentCache { | |
| 42 public: | |
| 43 EnvironmentCache() : variable_name(NULL), was_set(false) {} | |
| 44 | |
| 45 const char* variable_name; | |
| 46 bool was_set; | |
| 47 std::string value; | |
| 48 }; | |
| 49 | |
| 50 class GoogleAPIKeysTest : public testing::Test { | |
| 51 public: | |
| 52 GoogleAPIKeysTest() : env_(base::Environment::Create()) { | |
| 53 env_cache_[0].variable_name = "GOOGLE_API_KEY"; | |
| 54 env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN"; | |
| 55 env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN"; | |
| 56 env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT"; | |
| 57 env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT"; | |
| 58 env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING"; | |
| 59 env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING"; | |
| 60 env_cache_[7].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST"; | |
| 61 env_cache_[8].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST"; | |
| 62 env_cache_[9].variable_name = "GOOGLE_DEFAULT_CLIENT_ID"; | |
| 63 env_cache_[10].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET"; | |
| 64 } | |
|
Roger Tawa OOO till Jul 10th
2016/08/08 18:03:05
COMPILE_ASSERT(11 == 3 + 2 * google_apis::CLIENT_N
bzanotti
2016/08/10 13:23:39
Done.
| |
| 65 | |
| 66 void SetUp() override { | |
| 67 // Unset all environment variables that can affect these tests, | |
| 68 // for the duration of the tests. | |
| 69 for (size_t i = 0; i < arraysize(env_cache_); ++i) { | |
| 70 EnvironmentCache& cache = env_cache_[i]; | |
| 71 cache.was_set = env_->HasVar(cache.variable_name); | |
| 72 cache.value.clear(); | |
| 73 if (cache.was_set) { | |
| 74 env_->GetVar(cache.variable_name, &cache.value); | |
| 75 env_->UnSetVar(cache.variable_name); | |
| 76 } | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 void TearDown() override { | |
| 81 // Restore environment. | |
| 82 for (size_t i = 0; i < arraysize(env_cache_); ++i) { | |
| 83 EnvironmentCache& cache = env_cache_[i]; | |
| 84 if (cache.was_set) { | |
| 85 env_->SetVar(cache.variable_name, cache.value); | |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 std::unique_ptr<base::Environment> env_; | |
| 92 | |
| 93 // Why 3? It is for GOOGLE_API_KEY, GOOGLE_DEFAULT_CLIENT_ID and | |
| 94 // GOOGLE_DEFAULT_CLIENT_SECRET. | |
| 95 // | |
| 96 // Why 2 times CLIENT_NUM_ITEMS? This is the number of different | |
| 97 // clients in the OAuth2Client enumeration, and for each of these we | |
| 98 // have both an ID and a secret. | |
| 99 EnvironmentCache env_cache_[3 + 2 * google_apis::CLIENT_NUM_ITEMS]; | |
| 100 }; | |
| 101 | |
| 102 // After this test, for the remainder of this compilation unit, we | |
| 103 // need official keys to not be used. | |
| 104 #undef GOOGLE_CHROME_BUILD | |
| 105 #undef USE_OFFICIAL_GOOGLE_API_KEYS | |
| 106 | |
| 107 // Override some keys using both preprocessor defines and Info.plist entries. | |
| 108 // The Info.plist entries should win. | |
| 109 namespace override_some_keys_info_plist { | |
| 110 | |
| 111 // We start every test by creating a clean environment for the | |
| 112 // preprocessor defines used in google_api_keys.cc | |
| 113 #undef DUMMY_API_TOKEN | |
| 114 #undef GOOGLE_API_KEY | |
| 115 #undef GOOGLE_CLIENT_ID_MAIN | |
| 116 #undef GOOGLE_CLIENT_SECRET_MAIN | |
| 117 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT | |
| 118 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT | |
| 119 #undef GOOGLE_CLIENT_ID_REMOTING | |
| 120 #undef GOOGLE_CLIENT_SECRET_REMOTING | |
| 121 #undef GOOGLE_CLIENT_ID_REMOTING_HOST | |
| 122 #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST | |
| 123 #undef GOOGLE_DEFAULT_CLIENT_ID | |
| 124 #undef GOOGLE_DEFAULT_CLIENT_SECRET | |
| 125 | |
| 126 #define GOOGLE_API_KEY "API_KEY" | |
| 127 #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN" | |
| 128 #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN" | |
| 129 #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT" | |
| 130 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT" | |
| 131 #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING" | |
| 132 #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING" | |
| 133 #define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST" | |
| 134 #define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST" | |
| 135 | |
| 136 // Undef include guard so things get defined again, within this namespace. | |
| 137 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ | |
| 138 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ | |
| 139 #include "google_apis/google_api_keys.cc" | |
| 140 | |
| 141 } // namespace override_all_keys_env | |
| 142 | |
| 143 TEST_F(GoogleAPIKeysTest, OverrideSomeKeysUsingInfoPlist) { | |
| 144 namespace testcase = override_some_keys_info_plist::google_apis; | |
| 145 | |
| 146 id mock_bundle = [OCMockObject mockForClass:[NSBundle class]]; | |
| 147 [[[mock_bundle stub] andReturn:@"plist-API_KEY"] | |
| 148 objectForInfoDictionaryKey:@"GOOGLE_API_KEY"]; | |
| 149 [[[mock_bundle stub] andReturn:@"plist-ID_MAIN"] | |
| 150 objectForInfoDictionaryKey:@"GOOGLE_CLIENT_ID_MAIN"]; | |
| 151 [[[mock_bundle stub] andReturn:nil] objectForInfoDictionaryKey:[OCMArg any]]; | |
| 152 base::mac::SetOverrideFrameworkBundle(mock_bundle); | |
| 153 | |
| 154 EXPECT_TRUE(testcase::HasKeysConfigured()); | |
| 155 | |
| 156 // Once the keys have been configured, the bundle isn't used anymore. | |
| 157 base::mac::SetOverrideFrameworkBundle(nil); | |
| 158 | |
| 159 std::string api_key = testcase::g_api_key_cache.Get().api_key(); | |
| 160 std::string id_main = | |
| 161 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_MAIN); | |
| 162 std::string secret_main = | |
| 163 testcase::g_api_key_cache.Get().GetClientSecret(testcase::CLIENT_MAIN); | |
| 164 std::string id_cloud_print = | |
| 165 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_CLOUD_PRINT); | |
| 166 std::string secret_cloud_print = | |
| 167 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 168 testcase::CLIENT_CLOUD_PRINT); | |
| 169 std::string id_remoting = | |
| 170 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_REMOTING); | |
| 171 std::string secret_remoting = testcase::g_api_key_cache.Get().GetClientSecret( | |
| 172 testcase::CLIENT_REMOTING); | |
| 173 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( | |
| 174 testcase::CLIENT_REMOTING_HOST); | |
| 175 std::string secret_remoting_host = | |
| 176 testcase::g_api_key_cache.Get().GetClientSecret( | |
| 177 testcase::CLIENT_REMOTING_HOST); | |
| 178 | |
| 179 EXPECT_EQ("plist-API_KEY", api_key); | |
| 180 EXPECT_EQ("plist-ID_MAIN", id_main); | |
| 181 EXPECT_EQ("SECRET_MAIN", secret_main); | |
| 182 EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print); | |
| 183 EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print); | |
| 184 EXPECT_EQ("ID_REMOTING", id_remoting); | |
| 185 EXPECT_EQ("SECRET_REMOTING", secret_remoting); | |
| 186 EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host); | |
| 187 EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host); | |
| 188 } | |
| OLD | NEW |