diff options
author | jar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 23:40:51 +0000 |
---|---|---|
committer | jar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 23:40:51 +0000 |
commit | 022001851f2f6be890f9b44c49fa45ea38fa80b2 (patch) | |
tree | 9ea0cc9ac76b7002421110a5dee0ae60082740aa /base/metrics/histogram_unittest.cc | |
parent | ada794814ca34a4761b21b1c2562cc75de22ff52 (diff) | |
download | chromium_src-022001851f2f6be890f9b44c49fa45ea38fa80b2.zip chromium_src-022001851f2f6be890f9b44c49fa45ea38fa80b2.tar.gz chromium_src-022001851f2f6be890f9b44c49fa45ea38fa80b2.tar.bz2 |
Try to detect internal corruption of histogram instances.
Corruptions can include changes in bucket boundaries,
large changes in sample counts (in a bucket), etc.
We now detect problems, and don't forward the corrupt
data any further. This means it won't exit the renderer
and go to the browser if corrupt, and it won't exit
the browser and be sent up via UMA if corrupt.
IF the would-be corruption is caused by a race to
snapshot the data, then a later snapshot should get
the clean copy, and all data (across the precluded
period) will be sent onward.
BUG=61281
r=mbelshe
Review URL: http://codereview.chromium.org/4174002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64687 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/metrics/histogram_unittest.cc')
-rw-r--r-- | base/metrics/histogram_unittest.cc | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/base/metrics/histogram_unittest.cc b/base/metrics/histogram_unittest.cc index e7e3983..b9c51ad 100644 --- a/base/metrics/histogram_unittest.cc +++ b/base/metrics/histogram_unittest.cc @@ -308,4 +308,57 @@ TEST(HistogramTest, BucketPlacementTest) { } } // namespace + +//------------------------------------------------------------------------------ +// We can't be an an anonymous namespace while being friends, so we pop back +// out to the base namespace here. We need to be friends to corrupt the +// internals of the histogram and/or sampleset. +TEST(HistogramTest, CorruptSampleCounts) { + scoped_refptr<Histogram> histogram = Histogram::FactoryGet( + "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. + + EXPECT_EQ(0, histogram->sample_.redundant_count()); + histogram->Add(20); // Add some samples. + histogram->Add(40); + EXPECT_EQ(2, histogram->sample_.redundant_count()); + + Histogram::SampleSet snapshot; + histogram->SnapshotSample(&snapshot); + EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); + EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. + EXPECT_EQ(2, snapshot.redundant_count()); + + snapshot.counts_[3] += 100; // Sample count won't match redundant count. + EXPECT_EQ(Histogram::COUNT_LOW_ERROR, histogram->FindCorruption(snapshot)); + snapshot.counts_[2] -= 200; + EXPECT_EQ(Histogram::COUNT_HIGH_ERROR, histogram->FindCorruption(snapshot)); + + // But we can't spot a corruption if it is compensated for. + snapshot.counts_[1] += 100; + EXPECT_EQ(0, histogram->FindCorruption(snapshot)); +} + +TEST(HistogramTest, CorruptBucketBounds) { + scoped_refptr<Histogram> histogram = Histogram::FactoryGet( + "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. + + Histogram::SampleSet snapshot; + histogram->SnapshotSample(&snapshot); + EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); + EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. + + std::swap(histogram->ranges_[1], histogram->ranges_[2]); + EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR, histogram->FindCorruption(snapshot)); + + std::swap(histogram->ranges_[1], histogram->ranges_[2]); + EXPECT_EQ(0, histogram->FindCorruption(snapshot)); + + ++histogram->ranges_[3]; + EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, + histogram->FindCorruption(snapshot)); + + // Repair histogram so that destructor won't DCHECK(). + --histogram->ranges_[3]; +} + } // namespace base |