1// Copyright 2014 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#include "components/autofill/content/browser/risk/fingerprint.h" 6 7#include "base/bind.h" 8#include "base/message_loop/message_loop.h" 9#include "base/port.h" 10#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h" 11#include "content/public/browser/geolocation_provider.h" 12#include "content/public/browser/gpu_data_manager.h" 13#include "content/public/common/geoposition.h" 14#include "content/public/test/content_browser_test.h" 15#include "content/public/test/test_utils.h" 16#include "testing/gmock/include/gmock/gmock.h" 17#include "testing/gtest/include/gtest/gtest.h" 18#include "third_party/WebKit/public/platform/WebRect.h" 19#include "third_party/WebKit/public/platform/WebScreenInfo.h" 20#include "ui/gfx/rect.h" 21 22using testing::ElementsAre; 23 24namespace autofill { 25namespace risk { 26 27namespace internal { 28 29// Defined in the implementation file corresponding to this test. 30void GetFingerprintInternal( 31 uint64 obfuscated_gaia_id, 32 const gfx::Rect& window_bounds, 33 const gfx::Rect& content_bounds, 34 const blink::WebScreenInfo& screen_info, 35 const std::string& version, 36 const std::string& charset, 37 const std::string& accept_languages, 38 const base::Time& install_time, 39 const std::string& app_locale, 40 const std::string& user_agent, 41 const base::TimeDelta& timeout, 42 const base::Callback<void(scoped_ptr<Fingerprint>)>& callback); 43 44} // namespace internal 45 46// Constants that are passed verbatim to the fingerprinter code and should be 47// serialized into the resulting protocol buffer. 48const uint64 kObfuscatedGaiaId = GG_UINT64_C(16571487432910023183); 49const char kCharset[] = "UTF-8"; 50const char kAcceptLanguages[] = "en-US,en"; 51const int kScreenColorDepth = 53; 52const char kLocale[] = "en-GB"; 53const char kUserAgent[] = "TestUserAgent"; 54 55// Geolocation constants that are passed verbatim to the fingerprinter code and 56// should be serialized into the resulting protocol buffer. 57const double kLatitude = -42.0; 58const double kLongitude = 17.3; 59const double kAltitude = 123.4; 60const double kAccuracy = 73.7; 61const int kGeolocationTime = 87; 62 63class AutofillRiskFingerprintTest : public content::ContentBrowserTest { 64 public: 65 AutofillRiskFingerprintTest() 66 : window_bounds_(2, 3, 5, 7), 67 content_bounds_(11, 13, 17, 37), 68 screen_bounds_(0, 0, 101, 71), 69 available_screen_bounds_(0, 11, 101, 60), 70 unavailable_screen_bounds_(0, 0, 101, 11) {} 71 72 void GetFingerprintTestCallback(scoped_ptr<Fingerprint> fingerprint) { 73 // Verify that all fields Chrome can fill have been filled. 74 ASSERT_TRUE(fingerprint->has_machine_characteristics()); 75 const Fingerprint::MachineCharacteristics& machine = 76 fingerprint->machine_characteristics(); 77 EXPECT_TRUE(machine.has_operating_system_build()); 78 EXPECT_TRUE(machine.has_browser_install_time_hours()); 79 EXPECT_GT(machine.font_size(), 0); 80 81 // TODO(isherman): http://crbug.com/358548 and EXPECT_EQ. 82 EXPECT_GE(machine.plugin_size(), 0); 83 84 EXPECT_TRUE(machine.has_utc_offset_ms()); 85 EXPECT_TRUE(machine.has_browser_language()); 86 EXPECT_GT(machine.requested_language_size(), 0); 87 EXPECT_TRUE(machine.has_charset()); 88 EXPECT_TRUE(machine.has_screen_count()); 89 ASSERT_TRUE(machine.has_screen_size()); 90 EXPECT_TRUE(machine.screen_size().has_width()); 91 EXPECT_TRUE(machine.screen_size().has_height()); 92 EXPECT_TRUE(machine.has_screen_color_depth()); 93 ASSERT_TRUE(machine.has_unavailable_screen_size()); 94 EXPECT_TRUE(machine.unavailable_screen_size().has_width()); 95 EXPECT_TRUE(machine.unavailable_screen_size().has_height()); 96 EXPECT_TRUE(machine.has_user_agent()); 97 ASSERT_TRUE(machine.has_cpu()); 98 EXPECT_TRUE(machine.cpu().has_vendor_name()); 99 EXPECT_TRUE(machine.cpu().has_brand()); 100 EXPECT_TRUE(machine.has_ram()); 101 EXPECT_TRUE(machine.has_browser_build()); 102 EXPECT_TRUE(machine.has_browser_feature()); 103 if (content::GpuDataManager::GetInstance()->GpuAccessAllowed(NULL)) { 104 ASSERT_TRUE(machine.has_graphics_card()); 105 EXPECT_TRUE(machine.graphics_card().has_vendor_id()); 106 EXPECT_TRUE(machine.graphics_card().has_device_id()); 107 } else { 108 EXPECT_FALSE(machine.has_graphics_card()); 109 } 110 111 ASSERT_TRUE(fingerprint->has_transient_state()); 112 const Fingerprint::TransientState& transient_state = 113 fingerprint->transient_state(); 114 ASSERT_TRUE(transient_state.has_inner_window_size()); 115 ASSERT_TRUE(transient_state.has_outer_window_size()); 116 EXPECT_TRUE(transient_state.inner_window_size().has_width()); 117 EXPECT_TRUE(transient_state.inner_window_size().has_height()); 118 EXPECT_TRUE(transient_state.outer_window_size().has_width()); 119 EXPECT_TRUE(transient_state.outer_window_size().has_height()); 120 121 ASSERT_TRUE(fingerprint->has_user_characteristics()); 122 const Fingerprint::UserCharacteristics& user_characteristics = 123 fingerprint->user_characteristics(); 124 ASSERT_TRUE(user_characteristics.has_location()); 125 const Fingerprint::UserCharacteristics::Location& location = 126 user_characteristics.location(); 127 EXPECT_TRUE(location.has_altitude()); 128 EXPECT_TRUE(location.has_latitude()); 129 EXPECT_TRUE(location.has_longitude()); 130 EXPECT_TRUE(location.has_accuracy()); 131 EXPECT_TRUE(location.has_time_in_ms()); 132 133 ASSERT_TRUE(fingerprint->has_metadata()); 134 EXPECT_TRUE(fingerprint->metadata().has_timestamp_ms()); 135 EXPECT_TRUE(fingerprint->metadata().has_obfuscated_gaia_id()); 136 EXPECT_TRUE(fingerprint->metadata().has_fingerprinter_version()); 137 138 // Some values have exact known (mocked out) values: 139 EXPECT_THAT(machine.requested_language(), ElementsAre("en-US", "en")); 140 EXPECT_EQ(kLocale, machine.browser_language()); 141 EXPECT_EQ(kUserAgent, machine.user_agent()); 142 EXPECT_EQ(kCharset, machine.charset()); 143 EXPECT_EQ(kScreenColorDepth, machine.screen_color_depth()); 144 EXPECT_EQ(unavailable_screen_bounds_.width(), 145 machine.unavailable_screen_size().width()); 146 EXPECT_EQ(unavailable_screen_bounds_.height(), 147 machine.unavailable_screen_size().height()); 148 EXPECT_EQ(Fingerprint::MachineCharacteristics::FEATURE_REQUEST_AUTOCOMPLETE, 149 machine.browser_feature()); 150 EXPECT_EQ(content_bounds_.width(), 151 transient_state.inner_window_size().width()); 152 EXPECT_EQ(content_bounds_.height(), 153 transient_state.inner_window_size().height()); 154 EXPECT_EQ(window_bounds_.width(), 155 transient_state.outer_window_size().width()); 156 EXPECT_EQ(window_bounds_.height(), 157 transient_state.outer_window_size().height()); 158 EXPECT_EQ(kObfuscatedGaiaId, fingerprint->metadata().obfuscated_gaia_id()); 159 EXPECT_EQ(kAltitude, location.altitude()); 160 EXPECT_EQ(kLatitude, location.latitude()); 161 EXPECT_EQ(kLongitude, location.longitude()); 162 EXPECT_EQ(kAccuracy, location.accuracy()); 163 EXPECT_EQ(kGeolocationTime, location.time_in_ms()); 164 165 message_loop_.Quit(); 166 } 167 168 protected: 169 // Constants defining bounds in the screen coordinate system that are passed 170 // verbatim to the fingerprinter code and should be serialized into the 171 // resulting protocol buffer. Declared as class members because gfx::Rect is 172 // not a POD type, so it cannot be statically initialized. 173 const gfx::Rect window_bounds_; 174 const gfx::Rect content_bounds_; 175 const gfx::Rect screen_bounds_; 176 const gfx::Rect available_screen_bounds_; 177 const gfx::Rect unavailable_screen_bounds_; 178 179 // A message loop to block on the asynchronous loading of the fingerprint. 180 base::MessageLoopForUI message_loop_; 181}; 182 183// Test that getting a fingerprint works on some basic level. 184IN_PROC_BROWSER_TEST_F(AutofillRiskFingerprintTest, GetFingerprint) { 185 content::Geoposition position; 186 position.latitude = kLatitude; 187 position.longitude = kLongitude; 188 position.altitude = kAltitude; 189 position.accuracy = kAccuracy; 190 position.timestamp = 191 base::Time::UnixEpoch() + 192 base::TimeDelta::FromMilliseconds(kGeolocationTime); 193 content::GeolocationProvider::GetInstance()->OverrideLocationForTesting( 194 position); 195 196 blink::WebScreenInfo screen_info; 197 screen_info.depth = kScreenColorDepth; 198 screen_info.rect = blink::WebRect(screen_bounds_); 199 screen_info.availableRect = blink::WebRect(available_screen_bounds_); 200 201 internal::GetFingerprintInternal( 202 kObfuscatedGaiaId, window_bounds_, content_bounds_, screen_info, 203 "25.0.0.123", kCharset, kAcceptLanguages, base::Time::Now(), kLocale, 204 kUserAgent, 205 base::TimeDelta::FromDays(1), // Ought to be longer than any test run. 206 base::Bind(&AutofillRiskFingerprintTest::GetFingerprintTestCallback, 207 base::Unretained(this))); 208 209 // Wait for the callback to be called. 210 message_loop_.Run(); 211} 212 213} // namespace risk 214} // namespace autofill 215