OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/android/data_usage/external_data_use_observer.h" | 5 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 {"http://www.google.com:80", "http://www.google.com", false}, | 99 {"http://www.google.com:80", "http://www.google.com", false}, |
100 {"http://www.google.com:80/", "http://www.google.com/", true}, | 100 {"http://www.google.com:80/", "http://www.google.com/", true}, |
101 {"", "http://www.google.com", false}, | 101 {"", "http://www.google.com", false}, |
102 {"", "", false}, | 102 {"", "", false}, |
103 {"https://www.google.com", "http://www.google.com", false}, | 103 {"https://www.google.com", "http://www.google.com", false}, |
104 }; | 104 }; |
105 | 105 |
106 std::string label("test"); | 106 std::string label("test"); |
107 for (size_t i = 0; i < arraysize(tests); ++i) { | 107 for (size_t i = 0; i < arraysize(tests); ++i) { |
108 external_data_use_observer()->RegisterURLRegexes( | 108 external_data_use_observer()->RegisterURLRegexes( |
| 109 // App package name not specified in the matching rule. |
109 std::vector<std::string>(1, std::string()), | 110 std::vector<std::string>(1, std::string()), |
110 std::vector<std::string>(1, tests[i].regex), | 111 std::vector<std::string>(1, tests[i].regex), |
111 std::vector<std::string>(1, "label")); | 112 std::vector<std::string>(1, "label")); |
112 EXPECT_EQ(tests[i].expect_match, | 113 EXPECT_EQ(tests[i].expect_match, |
113 external_data_use_observer()->Matches(GURL(tests[i].url), &label)) | 114 external_data_use_observer()->Matches(GURL(tests[i].url), &label)) |
114 << i; | 115 << i; |
115 | 116 |
116 // Verify label matches the expected label. | 117 // Verify label matches the expected label. |
117 std::string expected_label = ""; | 118 std::string expected_label = ""; |
118 if (tests[i].expect_match) | 119 if (tests[i].expect_match) |
119 expected_label = "label"; | 120 expected_label = "label"; |
120 | 121 |
121 EXPECT_EQ(expected_label, label); | 122 EXPECT_EQ(expected_label, label); |
| 123 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
| 124 "com.example.helloworld", &label)) |
| 125 << i; |
| 126 // Empty package name should not match against empty package name in the |
| 127 // matching rule. |
| 128 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
| 129 std::string(), &label)) |
| 130 << i; |
122 } | 131 } |
123 } | 132 } |
124 | 133 |
125 TEST_F(ExternalDataUseObserverTest, TwoRegex) { | 134 TEST_F(ExternalDataUseObserverTest, TwoRegex) { |
126 const struct { | 135 const struct { |
127 std::string url; | 136 std::string url; |
128 std::string regex1; | 137 std::string regex1; |
129 std::string regex2; | 138 std::string regex2; |
130 bool expect_match; | 139 bool expect_match; |
131 } tests[] = { | 140 } tests[] = { |
(...skipping 19 matching lines...) Expand all Loading... |
151 "https://www[.]google[.]com/search.*", false}, | 160 "https://www[.]google[.]com/search.*", false}, |
152 {"https://www.google.com/Search=test", "", | 161 {"https://www.google.com/Search=test", "", |
153 "https://www[.]google[.]com/search.*", true}, | 162 "https://www[.]google[.]com/search.*", true}, |
154 {"www.google.com", "http://www.google.com", "", false}, | 163 {"www.google.com", "http://www.google.com", "", false}, |
155 {"www.google.com:80", "http://www.google.com", "", false}, | 164 {"www.google.com:80", "http://www.google.com", "", false}, |
156 {"http://www.google.com:80", "http://www.google.com", "", false}, | 165 {"http://www.google.com:80", "http://www.google.com", "", false}, |
157 {"", "http://www.google.com", "", false}, | 166 {"", "http://www.google.com", "", false}, |
158 {"https://www.google.com", "http://www.google.com", "", false}, | 167 {"https://www.google.com", "http://www.google.com", "", false}, |
159 }; | 168 }; |
160 | 169 |
161 std::string label; | 170 std::string got_label; |
162 for (size_t i = 0; i < arraysize(tests); ++i) { | 171 for (size_t i = 0; i < arraysize(tests); ++i) { |
163 std::vector<std::string> url_regexes; | 172 std::vector<std::string> url_regexes; |
164 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2); | 173 url_regexes.push_back(tests[i].regex1 + "|" + tests[i].regex2); |
| 174 const std::string label("label"); |
165 external_data_use_observer()->RegisterURLRegexes( | 175 external_data_use_observer()->RegisterURLRegexes( |
166 std::vector<std::string>(url_regexes.size(), std::string()), | 176 std::vector<std::string>(url_regexes.size(), "com.example.helloworld"), |
167 url_regexes, std::vector<std::string>(url_regexes.size(), "label")); | 177 url_regexes, std::vector<std::string>(url_regexes.size(), label)); |
168 EXPECT_EQ(tests[i].expect_match, | 178 EXPECT_EQ(tests[i].expect_match, external_data_use_observer()->Matches( |
169 external_data_use_observer()->Matches(GURL(tests[i].url), &label)) | 179 GURL(tests[i].url), &got_label)) |
170 << i; | 180 << i; |
| 181 const std::string expected_label = |
| 182 tests[i].expect_match ? label : std::string(); |
| 183 EXPECT_EQ(got_label, expected_label); |
| 184 |
| 185 EXPECT_TRUE(external_data_use_observer()->MatchesAppPackageName( |
| 186 "com.example.helloworld", &got_label)) |
| 187 << i; |
| 188 EXPECT_EQ(label, got_label); |
171 } | 189 } |
172 } | 190 } |
173 | 191 |
174 TEST_F(ExternalDataUseObserverTest, MultipleRegex) { | 192 TEST_F(ExternalDataUseObserverTest, MultipleRegex) { |
175 std::vector<std::string> url_regexes; | 193 std::vector<std::string> url_regexes; |
176 url_regexes.push_back( | 194 url_regexes.push_back( |
177 "https?://www[.]google[.]com/#q=.*|https?://www[.]google[.]com[.]ph/" | 195 "https?://www[.]google[.]com/#q=.*|https?://www[.]google[.]com[.]ph/" |
178 "#q=.*|https?://www[.]google[.]com[.]ph/[?]gws_rd=ssl#q=.*"); | 196 "#q=.*|https?://www[.]google[.]com[.]ph/[?]gws_rd=ssl#q=.*"); |
179 external_data_use_observer()->RegisterURLRegexes( | 197 external_data_use_observer()->RegisterURLRegexes( |
180 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | 198 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 EXPECT_EQ(0, external_data_use_observer() | 477 EXPECT_EQ(0, external_data_use_observer() |
460 ->buffered_data_reports_.begin() | 478 ->buffered_data_reports_.begin() |
461 ->second.start_time.ToJavaTime()); | 479 ->second.start_time.ToJavaTime()); |
462 // Convert from seconds to milliseconds. | 480 // Convert from seconds to milliseconds. |
463 EXPECT_EQ(static_cast<int64_t>(num_iterations * 1000), | 481 EXPECT_EQ(static_cast<int64_t>(num_iterations * 1000), |
464 external_data_use_observer() | 482 external_data_use_observer() |
465 ->buffered_data_reports_.begin() | 483 ->buffered_data_reports_.begin() |
466 ->second.end_time.ToJavaTime()); | 484 ->second.end_time.ToJavaTime()); |
467 } | 485 } |
468 | 486 |
469 // Tests the behavior when multiple matching rules are available. | 487 // Tests the behavior when multiple matching rules are available for URL and |
| 488 // package name matching. |
470 TEST_F(ExternalDataUseObserverTest, MultipleMatchingRules) { | 489 TEST_F(ExternalDataUseObserverTest, MultipleMatchingRules) { |
471 std::vector<std::string> url_regexes; | 490 std::vector<std::string> url_regexes; |
472 url_regexes.push_back( | 491 url_regexes.push_back( |
473 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); | 492 "http://www[.]foo[.]com/#q=.*|https://www[.]foo[.]com/#q=.*"); |
474 url_regexes.push_back( | 493 url_regexes.push_back( |
475 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); | 494 "http://www[.]bar[.]com/#q=.*|https://www[.]bar[.]com/#q=.*"); |
476 | 495 |
477 std::vector<std::string> labels; | 496 std::vector<std::string> labels; |
478 const std::string label_foo("label_foo"); | 497 const std::string label_foo("label_foo"); |
479 const std::string label_bar("label_bar"); | 498 const std::string label_bar("label_bar"); |
480 labels.push_back(label_foo); | 499 labels.push_back(label_foo); |
481 labels.push_back(label_bar); | 500 labels.push_back(label_bar); |
482 | 501 |
| 502 std::vector<std::string> app_package_names; |
| 503 const std::string app_foo("com.example.foo"); |
| 504 const std::string app_bar("com.example.bar"); |
| 505 app_package_names.push_back(app_foo); |
| 506 app_package_names.push_back(app_bar); |
| 507 |
483 external_data_use_observer()->FetchMatchingRulesDoneOnIOThread( | 508 external_data_use_observer()->FetchMatchingRulesDoneOnIOThread( |
484 std::vector<std::string>(url_regexes.size(), std::string()), url_regexes, | 509 app_package_names, url_regexes, labels); |
485 labels); | |
486 EXPECT_EQ(0U, external_data_use_observer()->buffered_data_reports_.size()); | 510 EXPECT_EQ(0U, external_data_use_observer()->buffered_data_reports_.size()); |
487 EXPECT_FALSE(external_data_use_observer()->submit_data_report_pending_); | 511 EXPECT_FALSE(external_data_use_observer()->submit_data_report_pending_); |
488 EXPECT_FALSE(external_data_use_observer()->matching_rules_fetch_pending_); | 512 EXPECT_FALSE(external_data_use_observer()->matching_rules_fetch_pending_); |
489 | 513 |
490 // Check |label_foo| matching rule. | 514 // Check |label_foo| matching rule. |
491 std::vector<const data_usage::DataUse*> data_use_sequence; | 515 std::vector<const data_usage::DataUse*> data_use_sequence; |
492 data_usage::DataUse data_foo_1( | 516 data_usage::DataUse data_foo_1( |
493 GURL("http://www.foo.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 517 GURL("http://www.foo.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, |
494 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_1", | 518 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc_1", |
495 external_data_use_observer()->data_use_report_min_bytes_, 0); | 519 external_data_use_observer()->data_use_report_min_bytes_, 0); |
(...skipping 23 matching lines...) Expand all Loading... |
519 // Check |label_bar| matching rule. | 543 // Check |label_bar| matching rule. |
520 data_usage::DataUse data_bar( | 544 data_usage::DataUse data_bar( |
521 GURL("http://www.bar.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, | 545 GURL("http://www.bar.com/#q=abc"), base::TimeTicks::Now(), GURL(), 0, |
522 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc", 0, 0); | 546 net::NetworkChangeNotifier::CONNECTION_UNKNOWN, "mccmnc", 0, 0); |
523 data_use_sequence.push_back(&data_bar); | 547 data_use_sequence.push_back(&data_bar); |
524 external_data_use_observer()->OnDataUse(data_use_sequence); | 548 external_data_use_observer()->OnDataUse(data_use_sequence); |
525 for (const auto& it : external_data_use_observer()->buffered_data_reports_) { | 549 for (const auto& it : external_data_use_observer()->buffered_data_reports_) { |
526 EXPECT_EQ(label_bar, it.first.label); | 550 EXPECT_EQ(label_bar, it.first.label); |
527 EXPECT_EQ("mccmnc", it.first.mcc_mnc); | 551 EXPECT_EQ("mccmnc", it.first.mcc_mnc); |
528 } | 552 } |
| 553 |
| 554 // Test if labels are matched properly for app package names. |
| 555 std::string got_label; |
| 556 EXPECT_TRUE( |
| 557 external_data_use_observer()->MatchesAppPackageName(app_foo, &got_label)); |
| 558 EXPECT_EQ(label_foo, got_label); |
| 559 |
| 560 got_label = ""; |
| 561 EXPECT_TRUE( |
| 562 external_data_use_observer()->MatchesAppPackageName(app_bar, &got_label)); |
| 563 EXPECT_EQ(label_bar, got_label); |
| 564 |
| 565 got_label = ""; |
| 566 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
| 567 "com.example.unmatched", &got_label)); |
| 568 EXPECT_EQ(std::string(), got_label); |
| 569 |
| 570 EXPECT_FALSE(external_data_use_observer()->MatchesAppPackageName( |
| 571 std::string(), &got_label)); |
| 572 EXPECT_EQ(std::string(), got_label); |
529 } | 573 } |
530 | 574 |
531 // Tests that hash function reports distinct values. This test may fail if there | 575 // Tests that hash function reports distinct values. This test may fail if there |
532 // is a hash collision, however the chances of that happening are very low. | 576 // is a hash collision, however the chances of that happening are very low. |
533 TEST_F(ExternalDataUseObserverTest, HashFunction) { | 577 TEST_F(ExternalDataUseObserverTest, HashFunction) { |
534 ExternalDataUseObserver::DataUseReportKeyHash hash; | 578 ExternalDataUseObserver::DataUseReportKeyHash hash; |
535 | 579 |
536 ExternalDataUseObserver::DataUseReportKey foo( | 580 ExternalDataUseObserver::DataUseReportKey foo( |
537 "foo_label", net::NetworkChangeNotifier::CONNECTION_UNKNOWN, | 581 "foo_label", net::NetworkChangeNotifier::CONNECTION_UNKNOWN, |
538 "foo_mcc_mnc"); | 582 "foo_mcc_mnc"); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 external_data_use_obsever_with_variations | 718 external_data_use_obsever_with_variations |
675 ->fetch_matching_rules_duration_); | 719 ->fetch_matching_rules_duration_); |
676 EXPECT_EQ( | 720 EXPECT_EQ( |
677 data_use_report_min_bytes, | 721 data_use_report_min_bytes, |
678 external_data_use_obsever_with_variations->data_use_report_min_bytes_); | 722 external_data_use_obsever_with_variations->data_use_report_min_bytes_); |
679 } | 723 } |
680 | 724 |
681 } // namespace android | 725 } // namespace android |
682 | 726 |
683 } // namespace chrome | 727 } // namespace chrome |
OLD | NEW |