1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159

google_apis / google_api_keys_mac_unittest.mm [blame]

// Copyright 2016 The Chromium Authors
// 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_apis/google_api_keys.h.
//
// 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 "base/apple/bundle_locations.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "google_apis/api_key_cache.h"
#include "google_apis/default_api_keys.h"
#include "google_apis/gaia/gaia_switches.h"
#include "google_apis/google_api_keys.h"
#include "google_apis/google_api_keys_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "third_party/ocmock/OCMock/OCMock.h"

// We need official keys to not be used.
#undef BUILDFLAG_INTERNAL_CHROMIUM_BRANDING
#undef BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING
#define BUILDFLAG_INTERNAL_CHROMIUM_BRANDING() (1)
#define BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING() (0)
#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 define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#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_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"

#include "google_apis/default_api_keys-inc.cc"

}  // namespace override_some_keys_info_plist

TEST_F(GoogleAPIKeysTest, OverrideSomeKeysUsingInfoPlist) {
  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::apple::SetOverrideFrameworkBundle(mock_bundle);

  google_apis::ApiKeyCache api_key_cache(
      override_some_keys_info_plist::GetDefaultApiKeysFromDefinedValues());
  auto scoped_override =
      google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);

  EXPECT_TRUE(google_apis::HasAPIKeyConfigured());
  EXPECT_TRUE(google_apis::HasOAuthClientConfigured());

  // Once the keys have been configured, the bundle isn't used anymore.
  base::apple::SetOverrideFrameworkBundle(nil);

  std::string api_key = google_apis::GetAPIKey();
  std::string id_main =
      google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
  std::string secret_main =
      google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
  std::string id_remoting =
      google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING);
  std::string secret_remoting =
      google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING);
  std::string id_remoting_host =
      google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING_HOST);
  std::string secret_remoting_host =
      google_apis::GetOAuth2ClientSecret(google_apis::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_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);
}

// Override some keys using both preprocessor defines and Info.plist entries.
// The Info.plist entries should win.
namespace override_apikey_from_plist_with_feature {

// We start every test by creating a clean environment for the
// preprocessor defines used in define_baked_in_api_keys-inc.cc
#undef GOOGLE_API_KEY
#undef GOOGLE_CLIENT_ID_MAIN
#undef GOOGLE_CLIENT_SECRET_MAIN
#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_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"

#include "google_apis/default_api_keys-inc.cc"

}  // namespace override_apikey_from_plist_with_feature

TEST_F(GoogleAPIKeysTest, OverrideAPIKeyFromPlistWithFeature) {
  id mock_bundle = [OCMockObject mockForClass:[NSBundle class]];
  [[[mock_bundle stub] andReturn:@"plist-API_KEY"]
      objectForInfoDictionaryKey:@"GOOGLE_API_KEY"];
  [[[mock_bundle stub] andReturn:nil] objectForInfoDictionaryKey:[OCMArg any]];
  base::apple::SetOverrideFrameworkBundle(mock_bundle);

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      google_apis::kOverrideAPIKeyFeature, {{"api_key", "feature-API_KEY"}});

  base::HistogramTester tester;

  google_apis::ApiKeyCache api_key_cache(
      override_apikey_from_plist_with_feature::
          GetDefaultApiKeysFromDefinedValues());
  auto scoped_override =
      google_apis::SetScopedApiKeyCacheForTesting(&api_key_cache);

  EXPECT_EQ("feature-API_KEY", google_apis::GetAPIKey());

  tester.ExpectUniqueSample("Signin.APIKeyMatchesFeatureOnStartup", 1, 1);

  // Once the keys have been configured, the bundle isn't used anymore.
  base::apple::SetOverrideFrameworkBundle(nil);
}