Index: google_apis/google_api_keys_mac_unittest.mm |
diff --git a/google_apis/google_api_keys_mac_unittest.mm b/google_apis/google_api_keys_mac_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f6e539f62f99de4ed92930886801df2a506af7a2 |
--- /dev/null |
+++ b/google_apis/google_api_keys_mac_unittest.mm |
@@ -0,0 +1,188 @@ |
+// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Unit tests for implementation of google_api_keys namespace. |
+// |
+// Because the file deals with a lot of preprocessor defines and |
+// optionally includes an internal header, the way we test is by |
+// including the .cc file multiple times with different defines set. |
+// This is a little unorthodox, but it lets us test the behavior as |
+// close to unmodified as possible. |
+ |
+#include "google_apis/google_api_keys.h" |
+ |
+#include <memory> |
+ |
+#include "base/mac/bundle_locations.h" |
+#include "base/macros.h" |
+#include "build/build_config.h" |
+#include "google_apis/gaia/gaia_switches.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#import "third_party/ocmock/OCMock/OCMock.h" |
+ |
+// We need to include everything included by google_api_keys.cc once |
+// at global scope so that things like STL and classes from base don't |
+// get defined when we re-include the google_api_keys.cc file |
+// below. We used to include that file in its entirety here, but that |
+// can cause problems if the linker decides the version of symbols |
+// from that file included here is the "right" version. |
+ |
+#include <stddef.h> |
+ |
+#include <string> |
+#include "base/command_line.h" |
+#include "base/environment.h" |
+#include "base/lazy_instance.h" |
+#include "base/logging.h" |
+#include "base/strings/stringize_macros.h" |
+#include "google_apis/google_api_keys_mac.h" |
+ |
+struct EnvironmentCache { |
+ public: |
+ EnvironmentCache() : variable_name(NULL), was_set(false) {} |
+ |
+ const char* variable_name; |
+ bool was_set; |
+ std::string value; |
+}; |
+ |
+class GoogleAPIKeysTest : public testing::Test { |
+ public: |
+ GoogleAPIKeysTest() : env_(base::Environment::Create()) { |
+ env_cache_[0].variable_name = "GOOGLE_API_KEY"; |
+ env_cache_[1].variable_name = "GOOGLE_CLIENT_ID_MAIN"; |
+ env_cache_[2].variable_name = "GOOGLE_CLIENT_SECRET_MAIN"; |
+ env_cache_[3].variable_name = "GOOGLE_CLIENT_ID_CLOUD_PRINT"; |
+ env_cache_[4].variable_name = "GOOGLE_CLIENT_SECRET_CLOUD_PRINT"; |
+ env_cache_[5].variable_name = "GOOGLE_CLIENT_ID_REMOTING"; |
+ env_cache_[6].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING"; |
+ env_cache_[7].variable_name = "GOOGLE_CLIENT_ID_REMOTING_HOST"; |
+ env_cache_[8].variable_name = "GOOGLE_CLIENT_SECRET_REMOTING_HOST"; |
+ env_cache_[9].variable_name = "GOOGLE_DEFAULT_CLIENT_ID"; |
+ env_cache_[10].variable_name = "GOOGLE_DEFAULT_CLIENT_SECRET"; |
+ } |
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.
|
+ |
+ void SetUp() override { |
+ // Unset all environment variables that can affect these tests, |
+ // for the duration of the tests. |
+ for (size_t i = 0; i < arraysize(env_cache_); ++i) { |
+ EnvironmentCache& cache = env_cache_[i]; |
+ cache.was_set = env_->HasVar(cache.variable_name); |
+ cache.value.clear(); |
+ if (cache.was_set) { |
+ env_->GetVar(cache.variable_name, &cache.value); |
+ env_->UnSetVar(cache.variable_name); |
+ } |
+ } |
+ } |
+ |
+ void TearDown() override { |
+ // Restore environment. |
+ for (size_t i = 0; i < arraysize(env_cache_); ++i) { |
+ EnvironmentCache& cache = env_cache_[i]; |
+ if (cache.was_set) { |
+ env_->SetVar(cache.variable_name, cache.value); |
+ } |
+ } |
+ } |
+ |
+ private: |
+ std::unique_ptr<base::Environment> env_; |
+ |
+ // Why 3? It is for GOOGLE_API_KEY, GOOGLE_DEFAULT_CLIENT_ID and |
+ // GOOGLE_DEFAULT_CLIENT_SECRET. |
+ // |
+ // Why 2 times CLIENT_NUM_ITEMS? This is the number of different |
+ // clients in the OAuth2Client enumeration, and for each of these we |
+ // have both an ID and a secret. |
+ EnvironmentCache env_cache_[3 + 2 * google_apis::CLIENT_NUM_ITEMS]; |
+}; |
+ |
+// After this test, for the remainder of this compilation unit, we |
+// need official keys to not be used. |
+#undef GOOGLE_CHROME_BUILD |
+#undef USE_OFFICIAL_GOOGLE_API_KEYS |
+ |
+// Override some keys using both preprocessor defines and Info.plist entries. |
+// The Info.plist entries should win. |
+namespace override_some_keys_info_plist { |
+ |
+// We start every test by creating a clean environment for the |
+// preprocessor defines used in google_api_keys.cc |
+#undef DUMMY_API_TOKEN |
+#undef GOOGLE_API_KEY |
+#undef GOOGLE_CLIENT_ID_MAIN |
+#undef GOOGLE_CLIENT_SECRET_MAIN |
+#undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
+#undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
+#undef GOOGLE_CLIENT_ID_REMOTING |
+#undef GOOGLE_CLIENT_SECRET_REMOTING |
+#undef GOOGLE_CLIENT_ID_REMOTING_HOST |
+#undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
+#undef GOOGLE_DEFAULT_CLIENT_ID |
+#undef GOOGLE_DEFAULT_CLIENT_SECRET |
+ |
+#define GOOGLE_API_KEY "API_KEY" |
+#define GOOGLE_CLIENT_ID_MAIN "ID_MAIN" |
+#define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN" |
+#define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT" |
+#define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT" |
+#define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING" |
+#define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING" |
+#define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST" |
+#define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST" |
+ |
+// Undef include guard so things get defined again, within this namespace. |
+#undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
+#undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
+#include "google_apis/google_api_keys.cc" |
+ |
+} // namespace override_all_keys_env |
+ |
+TEST_F(GoogleAPIKeysTest, OverrideSomeKeysUsingInfoPlist) { |
+ namespace testcase = override_some_keys_info_plist::google_apis; |
+ |
+ id mock_bundle = [OCMockObject mockForClass:[NSBundle class]]; |
+ [[[mock_bundle stub] andReturn:@"plist-API_KEY"] |
+ objectForInfoDictionaryKey:@"GOOGLE_API_KEY"]; |
+ [[[mock_bundle stub] andReturn:@"plist-ID_MAIN"] |
+ objectForInfoDictionaryKey:@"GOOGLE_CLIENT_ID_MAIN"]; |
+ [[[mock_bundle stub] andReturn:nil] objectForInfoDictionaryKey:[OCMArg any]]; |
+ base::mac::SetOverrideFrameworkBundle(mock_bundle); |
+ |
+ EXPECT_TRUE(testcase::HasKeysConfigured()); |
+ |
+ // Once the keys have been configured, the bundle isn't used anymore. |
+ base::mac::SetOverrideFrameworkBundle(nil); |
+ |
+ std::string api_key = testcase::g_api_key_cache.Get().api_key(); |
+ std::string id_main = |
+ testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_MAIN); |
+ std::string secret_main = |
+ testcase::g_api_key_cache.Get().GetClientSecret(testcase::CLIENT_MAIN); |
+ std::string id_cloud_print = |
+ testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_CLOUD_PRINT); |
+ std::string secret_cloud_print = |
+ testcase::g_api_key_cache.Get().GetClientSecret( |
+ testcase::CLIENT_CLOUD_PRINT); |
+ std::string id_remoting = |
+ testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_REMOTING); |
+ std::string secret_remoting = testcase::g_api_key_cache.Get().GetClientSecret( |
+ testcase::CLIENT_REMOTING); |
+ std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( |
+ testcase::CLIENT_REMOTING_HOST); |
+ std::string secret_remoting_host = |
+ testcase::g_api_key_cache.Get().GetClientSecret( |
+ testcase::CLIENT_REMOTING_HOST); |
+ |
+ EXPECT_EQ("plist-API_KEY", api_key); |
+ EXPECT_EQ("plist-ID_MAIN", id_main); |
+ EXPECT_EQ("SECRET_MAIN", secret_main); |
+ EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print); |
+ EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print); |
+ EXPECT_EQ("ID_REMOTING", id_remoting); |
+ EXPECT_EQ("SECRET_REMOTING", secret_remoting); |
+ EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host); |
+ EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host); |
+} |