OLD | NEW |
(Empty) | |
| 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 |
| 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_unittest.h" |
| 14 |
| 15 #include "base/mac/bundle_locations.h" |
| 16 #include "base/macros.h" |
| 17 #include "build/build_config.h" |
| 18 #include "google_apis/gaia/gaia_switches.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #import "third_party/ocmock/OCMock/OCMock.h" |
| 21 |
| 22 // We need to include everything included by google_api_keys.cc once |
| 23 // at global scope so that things like STL and classes from base don't |
| 24 // get defined when we re-include the google_api_keys.cc file |
| 25 // below. We used to include that file in its entirety here, but that |
| 26 // can cause problems if the linker decides the version of symbols |
| 27 // from that file included here is the "right" version. |
| 28 |
| 29 #include <stddef.h> |
| 30 |
| 31 #include <string> |
| 32 #include "base/command_line.h" |
| 33 #include "base/lazy_instance.h" |
| 34 #include "base/logging.h" |
| 35 #include "base/strings/stringize_macros.h" |
| 36 #include "google_apis/google_api_keys_mac.h" |
| 37 |
| 38 // After this test, for the remainder of this compilation unit, we |
| 39 // need official keys to not be used. |
| 40 #undef GOOGLE_CHROME_BUILD |
| 41 #undef USE_OFFICIAL_GOOGLE_API_KEYS |
| 42 |
| 43 // Override some keys using both preprocessor defines and Info.plist entries. |
| 44 // The Info.plist entries should win. |
| 45 namespace override_some_keys_info_plist { |
| 46 |
| 47 // We start every test by creating a clean environment for the |
| 48 // preprocessor defines used in google_api_keys.cc |
| 49 #undef DUMMY_API_TOKEN |
| 50 #undef GOOGLE_API_KEY |
| 51 #undef GOOGLE_CLIENT_ID_MAIN |
| 52 #undef GOOGLE_CLIENT_SECRET_MAIN |
| 53 #undef GOOGLE_CLIENT_ID_CLOUD_PRINT |
| 54 #undef GOOGLE_CLIENT_SECRET_CLOUD_PRINT |
| 55 #undef GOOGLE_CLIENT_ID_REMOTING |
| 56 #undef GOOGLE_CLIENT_SECRET_REMOTING |
| 57 #undef GOOGLE_CLIENT_ID_REMOTING_HOST |
| 58 #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST |
| 59 #undef GOOGLE_DEFAULT_CLIENT_ID |
| 60 #undef GOOGLE_DEFAULT_CLIENT_SECRET |
| 61 |
| 62 #define GOOGLE_API_KEY "API_KEY" |
| 63 #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN" |
| 64 #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN" |
| 65 #define GOOGLE_CLIENT_ID_CLOUD_PRINT "ID_CLOUD_PRINT" |
| 66 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "SECRET_CLOUD_PRINT" |
| 67 #define GOOGLE_CLIENT_ID_REMOTING "ID_REMOTING" |
| 68 #define GOOGLE_CLIENT_SECRET_REMOTING "SECRET_REMOTING" |
| 69 #define GOOGLE_CLIENT_ID_REMOTING_HOST "ID_REMOTING_HOST" |
| 70 #define GOOGLE_CLIENT_SECRET_REMOTING_HOST "SECRET_REMOTING_HOST" |
| 71 |
| 72 // Undef include guard so things get defined again, within this namespace. |
| 73 #undef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 74 #undef GOOGLE_APIS_INTERNAL_GOOGLE_CHROME_API_KEYS_ |
| 75 #include "google_apis/google_api_keys.cc" |
| 76 |
| 77 } // namespace override_all_keys_env |
| 78 |
| 79 TEST_F(GoogleAPIKeysTest, OverrideSomeKeysUsingInfoPlist) { |
| 80 namespace testcase = override_some_keys_info_plist::google_apis; |
| 81 |
| 82 id mock_bundle = [OCMockObject mockForClass:[NSBundle class]]; |
| 83 [[[mock_bundle stub] andReturn:@"plist-API_KEY"] |
| 84 objectForInfoDictionaryKey:@"GOOGLE_API_KEY"]; |
| 85 [[[mock_bundle stub] andReturn:@"plist-ID_MAIN"] |
| 86 objectForInfoDictionaryKey:@"GOOGLE_CLIENT_ID_MAIN"]; |
| 87 [[[mock_bundle stub] andReturn:nil] objectForInfoDictionaryKey:[OCMArg any]]; |
| 88 base::mac::SetOverrideFrameworkBundle(mock_bundle); |
| 89 |
| 90 EXPECT_TRUE(testcase::HasKeysConfigured()); |
| 91 |
| 92 // Once the keys have been configured, the bundle isn't used anymore. |
| 93 base::mac::SetOverrideFrameworkBundle(nil); |
| 94 |
| 95 std::string api_key = testcase::g_api_key_cache.Get().api_key(); |
| 96 std::string id_main = |
| 97 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_MAIN); |
| 98 std::string secret_main = |
| 99 testcase::g_api_key_cache.Get().GetClientSecret(testcase::CLIENT_MAIN); |
| 100 std::string id_cloud_print = |
| 101 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_CLOUD_PRINT); |
| 102 std::string secret_cloud_print = |
| 103 testcase::g_api_key_cache.Get().GetClientSecret( |
| 104 testcase::CLIENT_CLOUD_PRINT); |
| 105 std::string id_remoting = |
| 106 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_REMOTING); |
| 107 std::string secret_remoting = testcase::g_api_key_cache.Get().GetClientSecret( |
| 108 testcase::CLIENT_REMOTING); |
| 109 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID( |
| 110 testcase::CLIENT_REMOTING_HOST); |
| 111 std::string secret_remoting_host = |
| 112 testcase::g_api_key_cache.Get().GetClientSecret( |
| 113 testcase::CLIENT_REMOTING_HOST); |
| 114 |
| 115 EXPECT_EQ("plist-API_KEY", api_key); |
| 116 EXPECT_EQ("plist-ID_MAIN", id_main); |
| 117 EXPECT_EQ("SECRET_MAIN", secret_main); |
| 118 EXPECT_EQ("ID_CLOUD_PRINT", id_cloud_print); |
| 119 EXPECT_EQ("SECRET_CLOUD_PRINT", secret_cloud_print); |
| 120 EXPECT_EQ("ID_REMOTING", id_remoting); |
| 121 EXPECT_EQ("SECRET_REMOTING", secret_remoting); |
| 122 EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host); |
| 123 EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host); |
| 124 } |
OLD | NEW |