| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/safe_browsing/phishing_term_feature_extractor.h" | 5 #include "chrome/renderer/safe_browsing/phishing_term_feature_extractor.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 } | 199 } |
| 200 | 200 |
| 201 TEST_F(PhishingTermFeatureExtractorTest, Continuation) { | 201 TEST_F(PhishingTermFeatureExtractorTest, Continuation) { |
| 202 // For this test, we'll cause the feature extraction to run multiple | 202 // For this test, we'll cause the feature extraction to run multiple |
| 203 // iterations by incrementing the clock. | 203 // iterations by incrementing the clock. |
| 204 | 204 |
| 205 // This page has a total of 30 words. For the features to be computed | 205 // This page has a total of 30 words. For the features to be computed |
| 206 // correctly, the extractor has to process the entire string of text. | 206 // correctly, the extractor has to process the entire string of text. |
| 207 string16 page_text(ASCIIToUTF16("one ")); | 207 string16 page_text(ASCIIToUTF16("one ")); |
| 208 for (int i = 0; i < 28; ++i) { | 208 for (int i = 0; i < 28; ++i) { |
| 209 page_text.append(ASCIIToUTF16(StringPrintf("%d ", i))); | 209 page_text.append(ASCIIToUTF16(base::StringPrintf("%d ", i))); |
| 210 } | 210 } |
| 211 page_text.append(ASCIIToUTF16("two")); | 211 page_text.append(ASCIIToUTF16("two")); |
| 212 | 212 |
| 213 // Advance the clock 3 ms every 5 words processed, 10 ms between chunks. | 213 // Advance the clock 3 ms every 5 words processed, 10 ms between chunks. |
| 214 // Note that this assumes kClockCheckGranularity = 5 and | 214 // Note that this assumes kClockCheckGranularity = 5 and |
| 215 // kMaxTimePerChunkMs = 10. | 215 // kMaxTimePerChunkMs = 10. |
| 216 base::TimeTicks now = base::TimeTicks::Now(); | 216 base::TimeTicks now = base::TimeTicks::Now(); |
| 217 EXPECT_CALL(clock_, Now()) | 217 EXPECT_CALL(clock_, Now()) |
| 218 // Time check at the start of extraction. | 218 // Time check at the start of extraction. |
| 219 .WillOnce(Return(now)) | 219 .WillOnce(Return(now)) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 // A final time check for the histograms. | 267 // A final time check for the histograms. |
| 268 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(620))); | 268 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(620))); |
| 269 | 269 |
| 270 features.Clear(); | 270 features.Clear(); |
| 271 EXPECT_FALSE(ExtractFeatures(&page_text, &features)); | 271 EXPECT_FALSE(ExtractFeatures(&page_text, &features)); |
| 272 } | 272 } |
| 273 | 273 |
| 274 TEST_F(PhishingTermFeatureExtractorTest, PartialExtractionTest) { | 274 TEST_F(PhishingTermFeatureExtractorTest, PartialExtractionTest) { |
| 275 scoped_ptr<string16> page_text(new string16(ASCIIToUTF16("one "))); | 275 scoped_ptr<string16> page_text(new string16(ASCIIToUTF16("one "))); |
| 276 for (int i = 0; i < 28; ++i) { | 276 for (int i = 0; i < 28; ++i) { |
| 277 page_text->append(ASCIIToUTF16(StringPrintf("%d ", i))); | 277 page_text->append(ASCIIToUTF16(base::StringPrintf("%d ", i))); |
| 278 } | 278 } |
| 279 | 279 |
| 280 base::TimeTicks now = base::TimeTicks::Now(); | 280 base::TimeTicks now = base::TimeTicks::Now(); |
| 281 EXPECT_CALL(clock_, Now()) | 281 EXPECT_CALL(clock_, Now()) |
| 282 // Time check at the start of extraction. | 282 // Time check at the start of extraction. |
| 283 .WillOnce(Return(now)) | 283 .WillOnce(Return(now)) |
| 284 // Time check at the start of the first chunk of work. | 284 // Time check at the start of the first chunk of work. |
| 285 .WillOnce(Return(now)) | 285 .WillOnce(Return(now)) |
| 286 // Time check after the first 5 words. | 286 // Time check after the first 5 words. |
| 287 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(7))) | 287 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(7))) |
| 288 // Time check after the next 5 words. This should be greater than | 288 // Time check after the next 5 words. This should be greater than |
| 289 // kMaxTimePerChunkMs so that we stop and schedule extraction for later. | 289 // kMaxTimePerChunkMs so that we stop and schedule extraction for later. |
| 290 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(14))); | 290 .WillOnce(Return(now + base::TimeDelta::FromMilliseconds(14))); |
| 291 | 291 |
| 292 FeatureMap features; | 292 FeatureMap features; |
| 293 // Extract first 10 words then stop. | 293 // Extract first 10 words then stop. |
| 294 PartialExtractFeatures(page_text.get(), &features); | 294 PartialExtractFeatures(page_text.get(), &features); |
| 295 | 295 |
| 296 page_text.reset(new string16()); | 296 page_text.reset(new string16()); |
| 297 for (int i = 30; i < 58; ++i) { | 297 for (int i = 30; i < 58; ++i) { |
| 298 page_text->append(ASCIIToUTF16(StringPrintf("%d ", i))); | 298 page_text->append(ASCIIToUTF16(base::StringPrintf("%d ", i))); |
| 299 } | 299 } |
| 300 page_text->append(ASCIIToUTF16("multi word test ")); | 300 page_text->append(ASCIIToUTF16("multi word test ")); |
| 301 features.Clear(); | 301 features.Clear(); |
| 302 | 302 |
| 303 // This part doesn't exercise the extraction timing. | 303 // This part doesn't exercise the extraction timing. |
| 304 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); | 304 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); |
| 305 | 305 |
| 306 // Now extract normally and make sure nothing breaks. | 306 // Now extract normally and make sure nothing breaks. |
| 307 EXPECT_TRUE(ExtractFeatures(page_text.get(), &features)); | 307 EXPECT_TRUE(ExtractFeatures(page_text.get(), &features)); |
| 308 | 308 |
| 309 FeatureMap expected_features; | 309 FeatureMap expected_features; |
| 310 expected_features.AddBooleanFeature(features::kPageTerm + | 310 expected_features.AddBooleanFeature(features::kPageTerm + |
| 311 std::string("multi word test")); | 311 std::string("multi word test")); |
| 312 ExpectFeatureMapsAreEqual(features, expected_features); | 312 ExpectFeatureMapsAreEqual(features, expected_features); |
| 313 } | 313 } |
| 314 | 314 |
| 315 } // namespace safe_browsing | 315 } // namespace safe_browsing |
| OLD | NEW |