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