1 // Copyright 2016 The Chromium Authors
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 "build/branding_buildflags.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"
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.
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/gaia/gaia_config.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_REMOTING
58 #undef GOOGLE_CLIENT_SECRET_REMOTING
59 #undef GOOGLE_CLIENT_ID_REMOTING_HOST
60 #undef GOOGLE_CLIENT_SECRET_REMOTING_HOST
61 #undef GOOGLE_DEFAULT_CLIENT_ID
62 #undef GOOGLE_DEFAULT_CLIENT_SECRET
64 #define GOOGLE_API_KEY "API_KEY"
65 #define GOOGLE_CLIENT_ID_MAIN "ID_MAIN"
66 #define GOOGLE_CLIENT_SECRET_MAIN "SECRET_MAIN"
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"
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"
77 } // namespace override_all_keys_env
79 TEST_F(GoogleAPIKeysTest, OverrideSomeKeysUsingInfoPlist) {
80 namespace testcase = override_some_keys_info_plist::google_apis;
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);
90 EXPECT_TRUE(testcase::HasAPIKeyConfigured());
91 EXPECT_TRUE(testcase::HasOAuthClientConfigured());
93 // Once the keys have been configured, the bundle isn't used anymore.
94 base::mac::SetOverrideFrameworkBundle(nil);
96 std::string api_key = testcase::g_api_key_cache.Get().api_key();
98 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_MAIN);
99 std::string secret_main =
100 testcase::g_api_key_cache.Get().GetClientSecret(testcase::CLIENT_MAIN);
101 std::string id_remoting =
102 testcase::g_api_key_cache.Get().GetClientID(testcase::CLIENT_REMOTING);
103 std::string secret_remoting = testcase::g_api_key_cache.Get().GetClientSecret(
104 testcase::CLIENT_REMOTING);
105 std::string id_remoting_host = testcase::g_api_key_cache.Get().GetClientID(
106 testcase::CLIENT_REMOTING_HOST);
107 std::string secret_remoting_host =
108 testcase::g_api_key_cache.Get().GetClientSecret(
109 testcase::CLIENT_REMOTING_HOST);
111 EXPECT_EQ("plist-API_KEY", api_key);
112 EXPECT_EQ("plist-ID_MAIN", id_main);
113 EXPECT_EQ("SECRET_MAIN", secret_main);
114 EXPECT_EQ("ID_REMOTING", id_remoting);
115 EXPECT_EQ("SECRET_REMOTING", secret_remoting);
116 EXPECT_EQ("ID_REMOTING_HOST", id_remoting_host);
117 EXPECT_EQ("SECRET_REMOTING_HOST", secret_remoting_host);