| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/rappor/rappor_service.h" | 5 #include "components/rappor/rappor_service_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/base64.h" | 13 #include "base/base64.h" |
| 14 #include "base/metrics/metrics_hashes.h" | 14 #include "base/metrics/metrics_hashes.h" |
| 15 #include "components/prefs/testing_pref_service.h" | 15 #include "components/prefs/testing_pref_service.h" |
| 16 #include "components/rappor/byte_vector_utils.h" | 16 #include "components/rappor/byte_vector_utils.h" |
| 17 #include "components/rappor/proto/rappor_metric.pb.h" | 17 #include "components/rappor/proto/rappor_metric.pb.h" |
| 18 #include "components/rappor/rappor_parameters.h" | 18 #include "components/rappor/public/rappor_parameters.h" |
| 19 #include "components/rappor/rappor_pref_names.h" | 19 #include "components/rappor/rappor_pref_names.h" |
| 20 #include "components/rappor/test_log_uploader.h" | 20 #include "components/rappor/test_log_uploader.h" |
| 21 #include "components/rappor/test_rappor_service.h" | 21 #include "components/rappor/test_rappor_service.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 namespace rappor { | 24 namespace rappor { |
| 25 | 25 |
| 26 TEST(RapporServiceTest, Update) { | 26 TEST(RapporServiceImplTest, Update) { |
| 27 // Test rappor service initially has uploading and reporting enabled. | 27 // Test rappor service initially has uploading and reporting enabled. |
| 28 TestRapporService rappor_service; | 28 TestRapporServiceImpl rappor_service; |
| 29 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation()); | 29 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation()); |
| 30 EXPECT_TRUE(rappor_service.test_uploader()->is_running()); | 30 EXPECT_TRUE(rappor_service.test_uploader()->is_running()); |
| 31 | 31 |
| 32 // Disabling both should stop both uploads and reports. | 32 // Disabling both should stop both uploads and reports. |
| 33 rappor_service.Update(0, false); | 33 rappor_service.Update(0, false); |
| 34 EXPECT_EQ(base::TimeDelta(), rappor_service.next_rotation()); | 34 EXPECT_EQ(base::TimeDelta(), rappor_service.next_rotation()); |
| 35 EXPECT_FALSE(rappor_service.test_uploader()->is_running()); | 35 EXPECT_FALSE(rappor_service.test_uploader()->is_running()); |
| 36 | 36 |
| 37 // Some recording, but no reporting. | 37 // Some recording, but no reporting. |
| 38 rappor_service.Update(UMA_RAPPOR_GROUP, false); | 38 rappor_service.Update(UMA_RAPPOR_GROUP, false); |
| 39 // Reports generation should still be scheduled. | 39 // Reports generation should still be scheduled. |
| 40 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation()); | 40 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation()); |
| 41 EXPECT_FALSE(rappor_service.test_uploader()->is_running()); | 41 EXPECT_FALSE(rappor_service.test_uploader()->is_running()); |
| 42 | 42 |
| 43 // Some recording and reporting enabled. | 43 // Some recording and reporting enabled. |
| 44 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, true); | 44 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, true); |
| 45 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation()); | 45 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation()); |
| 46 EXPECT_TRUE(rappor_service.test_uploader()->is_running()); | 46 EXPECT_TRUE(rappor_service.test_uploader()->is_running()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Check that samples can be recorded and exported. | 49 // Check that samples can be recorded and exported. |
| 50 TEST(RapporServiceTest, RecordAndExportMetrics) { | 50 TEST(RapporServiceImplTest, RecordAndExportMetrics) { |
| 51 TestRapporService rappor_service; | 51 TestRapporServiceImpl rappor_service; |
| 52 | 52 |
| 53 // Multiple samples for the same metric should only generate one report. | 53 // Multiple samples for the same metric should only generate one report. |
| 54 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo"); | 54 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 55 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "bar"); | 55 "foo"); |
| 56 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 57 "bar"); |
| 56 | 58 |
| 57 RapporReports reports; | 59 RapporReports reports; |
| 58 rappor_service.GetReports(&reports); | 60 rappor_service.GetReports(&reports); |
| 59 EXPECT_EQ(1, reports.report_size()); | 61 EXPECT_EQ(1, reports.report_size()); |
| 60 | 62 |
| 61 const RapporReports::Report& report = reports.report(0); | 63 const RapporReports::Report& report = reports.report(0); |
| 62 EXPECT_TRUE(report.name_hash()); | 64 EXPECT_TRUE(report.name_hash()); |
| 63 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits | 65 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits |
| 64 EXPECT_EQ(16u, report.bits().size()); | 66 EXPECT_EQ(16u, report.bits().size()); |
| 65 } | 67 } |
| 66 | 68 |
| 67 // Check that the reporting groups are respected. | 69 // Check that the reporting groups are respected. |
| 68 TEST(RapporServiceTest, UmaRecordingGroup) { | 70 TEST(RapporServiceImplTest, UmaRecordingGroup) { |
| 69 TestRapporService rappor_service; | 71 TestRapporServiceImpl rappor_service; |
| 70 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, false); | 72 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, false); |
| 71 | 73 |
| 72 // Wrong recording group. | 74 // Wrong recording group. |
| 73 rappor_service.RecordSample("UmaMetric", UMA_RAPPOR_TYPE, "foo"); | 75 rappor_service.RecordSampleString("UmaMetric", UMA_RAPPOR_TYPE, "foo"); |
| 74 | 76 |
| 75 RapporReports reports; | 77 RapporReports reports; |
| 76 rappor_service.GetReports(&reports); | 78 rappor_service.GetReports(&reports); |
| 77 EXPECT_EQ(0, reports.report_size()); | 79 EXPECT_EQ(0, reports.report_size()); |
| 78 } | 80 } |
| 79 | 81 |
| 80 // Check that the reporting groups are respected. | 82 // Check that the reporting groups are respected. |
| 81 TEST(RapporServiceTest, SafeBrowsingRecordingGroup) { | 83 TEST(RapporServiceImplTest, SafeBrowsingRecordingGroup) { |
| 82 TestRapporService rappor_service; | 84 TestRapporServiceImpl rappor_service; |
| 83 rappor_service.Update(UMA_RAPPOR_GROUP, false); | 85 rappor_service.Update(UMA_RAPPOR_GROUP, false); |
| 84 | 86 |
| 85 // Wrong recording group. | 87 // Wrong recording group. |
| 86 rappor_service.RecordSample("SbMetric", SAFEBROWSING_RAPPOR_TYPE, "foo"); | 88 rappor_service.RecordSampleString("SbMetric", SAFEBROWSING_RAPPOR_TYPE, |
| 89 "foo"); |
| 87 | 90 |
| 88 RapporReports reports; | 91 RapporReports reports; |
| 89 rappor_service.GetReports(&reports); | 92 rappor_service.GetReports(&reports); |
| 90 EXPECT_EQ(0, reports.report_size()); | 93 EXPECT_EQ(0, reports.report_size()); |
| 91 } | 94 } |
| 92 | 95 |
| 93 // Check that GetRecordedSampleForMetric works as expected. | 96 // Check that GetRecordedSampleForMetric works as expected. |
| 94 TEST(RapporServiceTest, GetRecordedSampleForMetric) { | 97 TEST(RapporServiceImplTest, GetRecordedSampleForMetric) { |
| 95 TestRapporService rappor_service; | 98 TestRapporServiceImpl rappor_service; |
| 96 | 99 |
| 97 // Multiple samples for the same metric; only the latest is remembered. | 100 // Multiple samples for the same metric; only the latest is remembered. |
| 98 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo"); | 101 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 99 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "bar"); | 102 "foo"); |
| 103 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 104 "bar"); |
| 100 | 105 |
| 101 std::string sample; | 106 std::string sample; |
| 102 RapporType type; | 107 RapporType type; |
| 103 EXPECT_FALSE( | 108 EXPECT_FALSE( |
| 104 rappor_service.GetRecordedSampleForMetric("WrongMetric", &sample, &type)); | 109 rappor_service.GetRecordedSampleForMetric("WrongMetric", &sample, &type)); |
| 105 EXPECT_TRUE( | 110 EXPECT_TRUE( |
| 106 rappor_service.GetRecordedSampleForMetric("MyMetric", &sample, &type)); | 111 rappor_service.GetRecordedSampleForMetric("MyMetric", &sample, &type)); |
| 107 EXPECT_EQ("bar", sample); | 112 EXPECT_EQ("bar", sample); |
| 108 EXPECT_EQ(ETLD_PLUS_ONE_RAPPOR_TYPE, type); | 113 EXPECT_EQ(ETLD_PLUS_ONE_RAPPOR_TYPE, type); |
| 109 } | 114 } |
| 110 | 115 |
| 111 // Check that the incognito is respected. | 116 // Check that the incognito is respected. |
| 112 TEST(RapporServiceTest, Incognito) { | 117 TEST(RapporServiceImplTest, Incognito) { |
| 113 TestRapporService rappor_service; | 118 TestRapporServiceImpl rappor_service; |
| 114 rappor_service.set_is_incognito(true); | 119 rappor_service.set_is_incognito(true); |
| 115 | 120 |
| 116 rappor_service.RecordSample("MyMetric", SAFEBROWSING_RAPPOR_TYPE, "foo"); | 121 rappor_service.RecordSampleString("MyMetric", SAFEBROWSING_RAPPOR_TYPE, |
| 122 "foo"); |
| 117 | 123 |
| 118 RapporReports reports; | 124 RapporReports reports; |
| 119 rappor_service.GetReports(&reports); | 125 rappor_service.GetReports(&reports); |
| 120 EXPECT_EQ(0, reports.report_size()); | 126 EXPECT_EQ(0, reports.report_size()); |
| 121 } | 127 } |
| 122 | 128 |
| 123 // Check that Sample objects record correctly. | 129 // Check that Sample objects record correctly. |
| 124 TEST(RapporServiceTest, RecordSample) { | 130 TEST(RapporServiceImplTest, RecordSample) { |
| 125 TestRapporService rappor_service; | 131 TestRapporServiceImpl rappor_service; |
| 126 std::unique_ptr<Sample> sample = | 132 std::unique_ptr<Sample> sample = |
| 127 rappor_service.CreateSample(SAFEBROWSING_RAPPOR_TYPE); | 133 rappor_service.CreateSample(SAFEBROWSING_RAPPOR_TYPE); |
| 128 sample->SetStringField("Url", "example.com"); | 134 sample->SetStringField("Url", "example.com"); |
| 129 sample->SetFlagsField("Flags1", 0xbcd, 12); | 135 sample->SetFlagsField("Flags1", 0xbcd, 12); |
| 130 rappor_service.RecordSampleObj("ObjMetric", std::move(sample)); | 136 rappor_service.RecordSample("ObjMetric", std::move(sample)); |
| 131 uint64_t url_hash = base::HashMetricName("ObjMetric.Url"); | 137 uint64_t url_hash = base::HashMetricName("ObjMetric.Url"); |
| 132 uint64_t flags_hash = base::HashMetricName("ObjMetric.Flags1"); | 138 uint64_t flags_hash = base::HashMetricName("ObjMetric.Flags1"); |
| 133 RapporReports reports; | 139 RapporReports reports; |
| 134 rappor_service.GetReports(&reports); | 140 rappor_service.GetReports(&reports); |
| 135 EXPECT_EQ(2, reports.report_size()); | 141 EXPECT_EQ(2, reports.report_size()); |
| 136 size_t url_index = reports.report(0).name_hash() == url_hash ? 0 : 1; | 142 size_t url_index = reports.report(0).name_hash() == url_hash ? 0 : 1; |
| 137 size_t flags_index = url_index == 0 ? 1 : 0; | 143 size_t flags_index = url_index == 0 ? 1 : 0; |
| 138 EXPECT_EQ(url_hash, reports.report(url_index).name_hash()); | 144 EXPECT_EQ(url_hash, reports.report(url_index).name_hash()); |
| 139 EXPECT_EQ(1u, reports.report(url_index).bits().size()); | 145 EXPECT_EQ(1u, reports.report(url_index).bits().size()); |
| 140 EXPECT_EQ(flags_hash, reports.report(flags_index).name_hash()); | 146 EXPECT_EQ(flags_hash, reports.report(flags_index).name_hash()); |
| 141 EXPECT_EQ(2u, reports.report(flags_index).bits().size()); | 147 EXPECT_EQ(2u, reports.report(flags_index).bits().size()); |
| 142 } | 148 } |
| 143 | 149 |
| 144 } // namespace rappor | 150 } // namespace rappor |
| OLD | NEW |