google_api_keys_mac_unittest.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 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. // Unit tests for implementation of google_api_keys namespace.
  5. //
  6. // Because the file deals with a lot of preprocessor defines and
  7. // optionally includes an internal header, the way we test is by
  8. // including the .cc file multiple times with different defines set.
  9. // This is a little unorthodox, but it lets us test the behavior as
  10. // close to unmodified as possible.
  11. #include "google_apis/google_api_keys_unittest.h"
  12. #include "base/mac/bundle_locations.h"
  13. #include "build/branding_buildflags.h"
  14. #include "build/build_config.h"
  15. #include "google_apis/gaia/gaia_switches.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. #import "third_party/ocmock/OCMock/OCMock.h"
  18. // We need to include everything included by google_api_keys.cc once
  19. // at global scope so that things like STL and classes from base don't
  20. // get defined when we re-include the google_api_keys.cc file
  21. // below. We used to include that file in its entirety here, but that
  22. // can cause problems if the linker decides the version of symbols
  23. // from that file included here is the "right" version.
  24. #include <stddef.h>
  25. #include <string>
  26. #include "base/command_line.h"
  27. #include "base/lazy_instance.h"
  28. #include "base/logging.h"
  29. #include "base/strings/stringize_macros.h"
  30. #include "google_apis/gaia/gaia_config.h"
  31. #include "google_apis/google_api_keys_mac.h"
  32. // After this test, for the remainder of this compilation unit, we
  33. // need official keys to not be used.
  34. #undef BUILDFLAG_INTERNAL_CHROMIUM_BRANDING
  35. #undef BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING
  36. #define BUILDFLAG_INTERNAL_CHROMIUM_BRANDING() (1)
  37. #define BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING() (0)
  38. #undef USE_OFFICIAL_GOOGLE_API_KEYS
  39. // Override some keys using both preprocessor defines and Info.plist entries.
  40. // The Info.plist entries should win.
  41. namespace override_some_keys_info_plist {
  42. // We start every test by creating a clean environment for the
  43. // preprocessor defines used in google_api_keys.cc
  44. #undef DUMMY_API_TOKEN
  45. #undef GOOGLE_API_KEY
  46. #undef GOOGLE_CLIENT_ID_MAIN
  47. #undef GOOGLE_CLIENT_SECRET_MAIN
  48. #undef GOOGLE_CLIENT_ID_REMOTING
  49. #undef GOOGLE_CLIENT_SECRET_REMOTING
  50. #undef GOOGLE_CLIENT_ID_REMOTING_HOST
  51. #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
  52. #undef GOOGLE_DEFAULT_CLIENT_ID
  53. #undef GOOGLE_DEFAULT_CLIENT_SECRET
  54. #define GOOGLE_API_KEY "API_KEY"
  55. #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
  56. #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
  57. #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING"
  58. #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING"
  59. #define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST"
  60. #define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST"
  61. // Undef include guard so things get defined again, within this namespace.
  62. #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_
  63. #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_
  64. #include "google_apis/google_api_keys.cc"
  65. } // namespace override_all_keys_env
  66. TEST_F(GoogleAPIKeysTest, OverrideSomeKeysUsingInfoPlist) {
  67. namespace testcase = override_some_keys_info_plist::google_apis;
  68. id mock_bundle = [OCMockObject mockForClass:[NSBundle class]];
  69. [[[mock_bundle stub] andReturn:@"plist-API_KEY"]
  70. objectForInfoDictionaryKey:@"GOOGLE_API_KEY"];
  71. [[[mock_bundle stub] andReturn:@"plist-ID_MAIN"]
  72. objectForInfoDictionaryKey:@"GOOGLE_CLIENT_ID_MAIN"];
  73. [[[mock_bundle stub] andReturn:nil] objectForInfoDictionaryKey:[OCMArg any]];
  74. base::mac::SetOverrideFrameworkBundle(mock_bundle);
  75. EXPECT_TRUE(testcase::HasAPIKeyConfigured());
  76. EXPECT_TRUE(testcase::HasOAuthClientConfigured());
  77. // Once the keys have been configured, the bundle isn't used anymore.
  78. base::mac::SetOverrideFrameworkBundle(nil);
  79. std::string api_key = testcase::g_api_key_cache.Get().api_key();
  80. std::string id_main =
  81. testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_MAIN);
  82. std::string secret_main =
  83. testcase::g_api_key_cache.Get().GetClientSecret(testcase::CLIENT_MAIN);
  84. std::string id_remoting =
  85. testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_REMOTING);
  86. std::string secret_remoting = testcase::g_api_key_cache.Get().GetClientSecret(
  87. testcase::CLIENT_REMOTING);
  88. std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
  89. testcase::CLIENT_REMOTING_HOST);
  90. std::string secret_remoting_host =
  91. testcase::g_api_key_cache.Get().GetClientSecret(
  92. testcase::CLIENT_REMOTING_HOST);
  93. EXPECT_EQ("plist-API_KEY", api_key);
  94. EXPECT_EQ("plist-ID_MAIN", id_main);
  95. EXPECT_EQ("SECRET_MAIN", secret_main);
  96. EXPECT_EQ("ID_REMOTING", id_remoting);
  97. EXPECT_EQ("SECRET_REMOTING", secret_remoting);
  98. EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host);
  99. EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host);
  100. }