| 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_impl.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> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 namespace rappor { | 24 namespace rappor { |
| 25 | 25 |
| 26 TEST(RapporServiceImplTest, 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 TestRapporServiceImpl 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(false, 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 // Recording, but no reporting. |
| 38 rappor_service.Update(UMA_RAPPOR_GROUP, false); | 38 rappor_service.Update(true, 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 // Recording and reporting enabled. |
| 44 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, true); | 44 rappor_service.Update(true, 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(RapporServiceImplTest, RecordAndExportMetrics) { | 50 TEST(RapporServiceImplTest, RecordAndExportMetrics) { |
| 51 TestRapporServiceImpl 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.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, | 54 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 55 "foo"); | 55 "foo"); |
| 56 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, | 56 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 57 "bar"); | 57 "bar"); |
| 58 | 58 |
| 59 RapporReports reports; | 59 RapporReports reports; |
| 60 rappor_service.GetReports(&reports); | 60 rappor_service.GetReports(&reports); |
| 61 EXPECT_EQ(1, reports.report_size()); | 61 EXPECT_EQ(1, reports.report_size()); |
| 62 | 62 |
| 63 const RapporReports::Report& report = reports.report(0); | 63 const RapporReports::Report& report = reports.report(0); |
| 64 EXPECT_TRUE(report.name_hash()); | 64 EXPECT_TRUE(report.name_hash()); |
| 65 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits | 65 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits |
| 66 EXPECT_EQ(16u, report.bits().size()); | 66 EXPECT_EQ(16u, report.bits().size()); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Check that the reporting groups are respected. | 69 // Check that reporting_enabled_ is respected. |
| 70 TEST(RapporServiceImplTest, UmaRecordingGroup) { | 70 TEST(RapporServiceImplTest, UmaRecordingGroup) { |
| 71 TestRapporServiceImpl rappor_service; | 71 TestRapporServiceImpl rappor_service; |
| 72 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, false); | 72 rappor_service.Update(false, false); |
| 73 | 73 |
| 74 // Wrong recording group. | 74 // Reporting disabled. |
| 75 rappor_service.RecordSampleString("UmaMetric", UMA_RAPPOR_TYPE, "foo"); | 75 rappor_service.RecordSampleString("UmaMetric", UMA_RAPPOR_TYPE, "foo"); |
| 76 | 76 |
| 77 RapporReports reports; | 77 RapporReports reports; |
| 78 rappor_service.GetReports(&reports); | 78 rappor_service.GetReports(&reports); |
| 79 EXPECT_EQ(0, reports.report_size()); | 79 EXPECT_EQ(0, reports.report_size()); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Check that the reporting groups are respected. | |
| 83 TEST(RapporServiceImplTest, SafeBrowsingRecordingGroup) { | |
| 84 TestRapporServiceImpl rappor_service; | |
| 85 rappor_service.Update(UMA_RAPPOR_GROUP, false); | |
| 86 | |
| 87 // Wrong recording group. | |
| 88 rappor_service.RecordSampleString("SbMetric", SAFEBROWSING_RAPPOR_TYPE, | |
| 89 "foo"); | |
| 90 | |
| 91 RapporReports reports; | |
| 92 rappor_service.GetReports(&reports); | |
| 93 EXPECT_EQ(0, reports.report_size()); | |
| 94 } | |
| 95 | |
| 96 // Check that GetRecordedSampleForMetric works as expected. | 82 // Check that GetRecordedSampleForMetric works as expected. |
| 97 TEST(RapporServiceImplTest, GetRecordedSampleForMetric) { | 83 TEST(RapporServiceImplTest, GetRecordedSampleForMetric) { |
| 98 TestRapporServiceImpl rappor_service; | 84 TestRapporServiceImpl rappor_service; |
| 99 | 85 |
| 100 // Multiple samples for the same metric; only the latest is remembered. | 86 // Multiple samples for the same metric; only the latest is remembered. |
| 101 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, | 87 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 102 "foo"); | 88 "foo"); |
| 103 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, | 89 rappor_service.RecordSampleString("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, |
| 104 "bar"); | 90 "bar"); |
| 105 | 91 |
| 106 std::string sample; | 92 std::string sample; |
| 107 RapporType type; | 93 RapporType type; |
| 108 EXPECT_FALSE( | 94 EXPECT_FALSE( |
| 109 rappor_service.GetRecordedSampleForMetric("WrongMetric", &sample, &type)); | 95 rappor_service.GetRecordedSampleForMetric("WrongMetric", &sample, &type)); |
| 110 EXPECT_TRUE( | 96 EXPECT_TRUE( |
| 111 rappor_service.GetRecordedSampleForMetric("MyMetric", &sample, &type)); | 97 rappor_service.GetRecordedSampleForMetric("MyMetric", &sample, &type)); |
| 112 EXPECT_EQ("bar", sample); | 98 EXPECT_EQ("bar", sample); |
| 113 EXPECT_EQ(ETLD_PLUS_ONE_RAPPOR_TYPE, type); | 99 EXPECT_EQ(ETLD_PLUS_ONE_RAPPOR_TYPE, type); |
| 114 } | 100 } |
| 115 | 101 |
| 116 // Check that the incognito is respected. | 102 // Check that the incognito is respected. |
| 117 TEST(RapporServiceImplTest, Incognito) { | 103 TEST(RapporServiceImplTest, Incognito) { |
| 118 TestRapporServiceImpl rappor_service; | 104 TestRapporServiceImpl rappor_service; |
| 119 rappor_service.set_is_incognito(true); | 105 rappor_service.set_is_incognito(true); |
| 120 | 106 |
| 121 rappor_service.RecordSampleString("MyMetric", SAFEBROWSING_RAPPOR_TYPE, | 107 rappor_service.RecordSampleString("MyMetric", UMA_RAPPOR_TYPE, "foo"); |
| 122 "foo"); | |
| 123 | 108 |
| 124 RapporReports reports; | 109 RapporReports reports; |
| 125 rappor_service.GetReports(&reports); | 110 rappor_service.GetReports(&reports); |
| 126 EXPECT_EQ(0, reports.report_size()); | 111 EXPECT_EQ(0, reports.report_size()); |
| 127 } | 112 } |
| 128 | 113 |
| 129 // Check that Sample objects record correctly. | 114 // Check that Sample objects record correctly. |
| 130 TEST(RapporServiceImplTest, RecordSample) { | 115 TEST(RapporServiceImplTest, RecordSample) { |
| 131 TestRapporServiceImpl rappor_service; | 116 TestRapporServiceImpl rappor_service; |
| 132 std::unique_ptr<Sample> sample = | 117 std::unique_ptr<Sample> sample = rappor_service.CreateSample(UMA_RAPPOR_TYPE); |
| 133 rappor_service.CreateSample(SAFEBROWSING_RAPPOR_TYPE); | |
| 134 sample->SetStringField("Url", "example.com"); | 118 sample->SetStringField("Url", "example.com"); |
| 135 sample->SetFlagsField("Flags1", 0xbcd, 12); | 119 sample->SetFlagsField("Flags1", 0xbcd, 12); |
| 136 rappor_service.RecordSample("ObjMetric", std::move(sample)); | 120 rappor_service.RecordSample("ObjMetric", std::move(sample)); |
| 137 uint64_t url_hash = base::HashMetricName("ObjMetric.Url"); | 121 uint64_t url_hash = base::HashMetricName("ObjMetric.Url"); |
| 138 uint64_t flags_hash = base::HashMetricName("ObjMetric.Flags1"); | 122 uint64_t flags_hash = base::HashMetricName("ObjMetric.Flags1"); |
| 139 RapporReports reports; | 123 RapporReports reports; |
| 140 rappor_service.GetReports(&reports); | 124 rappor_service.GetReports(&reports); |
| 141 EXPECT_EQ(2, reports.report_size()); | 125 EXPECT_EQ(2, reports.report_size()); |
| 142 size_t url_index = reports.report(0).name_hash() == url_hash ? 0 : 1; | 126 size_t url_index = reports.report(0).name_hash() == url_hash ? 0 : 1; |
| 143 size_t flags_index = url_index == 0 ? 1 : 0; | 127 size_t flags_index = url_index == 0 ? 1 : 0; |
| 144 EXPECT_EQ(url_hash, reports.report(url_index).name_hash()); | 128 EXPECT_EQ(url_hash, reports.report(url_index).name_hash()); |
| 145 EXPECT_EQ(1u, reports.report(url_index).bits().size()); | 129 EXPECT_EQ(4u, reports.report(url_index).bits().size()); |
| 146 EXPECT_EQ(flags_hash, reports.report(flags_index).name_hash()); | 130 EXPECT_EQ(flags_hash, reports.report(flags_index).name_hash()); |
| 147 EXPECT_EQ(2u, reports.report(flags_index).bits().size()); | 131 EXPECT_EQ(2u, reports.report(flags_index).bits().size()); |
| 148 } | 132 } |
| 149 | 133 |
| 150 } // namespace rappor | 134 } // namespace rappor |
| OLD | NEW |