Files
android/libwvdrmengine/cdm/metrics/test/value_metric_unittest.cpp
Alex Dale 08c703a7b8 Added numeric value guards to ValueMetric.
[ Merge of http://go/wvgerrit/137951 ]

ValueMetric was not checking whether certain unsigned types will
overflow when casted to protobuf's signed int64 type.  This CL
adds a max value to prevent potential overflows.

The value metric has also been updated to an emum to represent its
possible three state, as opposed to using two boolean flags.  For basic
value metrics, this saves about 4 bytes of space.  This works out to
being ~100 bytes per crypto session, ~20 bytes per CDM session and ~20
bytes per engine metrics.

Bug: 204946540
Test: Metric unit tests
Change-Id: I688bece8f31cccaf2a25bd8f99d9b080b0efd0de
2021-11-08 14:52:29 -08:00

89 lines
2.6 KiB
C++

// Copyright 2017 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Unit tests for ValueMetric.
#include "value_metric.h"
#include <memory>
#include <string>
#include <gtest/gtest.h>
namespace wvcdm {
namespace metrics {
TEST(ValueMetricTest, StringValue) {
ValueMetric<std::string> metric;
metric.Record("foo");
std::unique_ptr<drm_metrics::ValueMetric> metric_proto(metric.ToProto());
ASSERT_EQ("foo", metric_proto->string_value());
ASSERT_FALSE(metric_proto->has_error_code());
}
TEST(ValueMetricTest, DoubleValue) {
ValueMetric<double> metric;
metric.Record(42.0);
std::unique_ptr<drm_metrics::ValueMetric> metric_proto(metric.ToProto());
ASSERT_EQ(42.0, metric_proto->double_value());
ASSERT_FALSE(metric_proto->has_error_code());
}
TEST(ValueMetricTest, Int32Value) {
ValueMetric<int32_t> metric;
metric.Record(42);
std::unique_ptr<drm_metrics::ValueMetric> metric_proto(metric.ToProto());
ASSERT_EQ(42, metric_proto->int_value());
ASSERT_FALSE(metric_proto->has_error_code());
}
TEST(ValueMetricTest, Int64Value) {
ValueMetric<int64_t> metric;
metric.Record(42);
std::unique_ptr<drm_metrics::ValueMetric> metric_proto(metric.ToProto());
ASSERT_EQ(42, metric_proto->int_value());
ASSERT_FALSE(metric_proto->has_error_code());
}
TEST(ValueMetricTest, SetError) {
ValueMetric<int64_t> metric;
metric.Record(42);
metric.SetError(7);
std::unique_ptr<drm_metrics::ValueMetric> metric_proto(metric.ToProto());
ASSERT_EQ(7, metric_proto->error_code());
ASSERT_FALSE(metric_proto->has_int_value());
}
TEST(ValueMetricTest, ValueOverflow) {
constexpr int64_t kMaxValue = std::numeric_limits<int64_t>::max();
constexpr uint64_t kMaxU64 = std::numeric_limits<uint64_t>::max();
constexpr size_t kMaxSizeT = std::numeric_limits<size_t>::max();
ValueMetric<uint64_t> metric_u64;
metric_u64.Record(kMaxU64);
// Runtime value should not be capped.
ASSERT_EQ(kMaxU64, metric_u64.GetValue());
std::unique_ptr<drm_metrics::ValueMetric> metric_proto(metric_u64.ToProto());
// Proto value should be capped.
ASSERT_EQ(kMaxValue, metric_proto->int_value());
ASSERT_FALSE(metric_proto->has_error_code());
ValueMetric<uint64_t> metric_z;
metric_z.Record(kMaxSizeT);
ASSERT_EQ(kMaxSizeT, metric_z.GetValue());
metric_proto.reset(metric_z.ToProto());
if (sizeof(int64_t) <= sizeof(size_t) &&
static_cast<size_t>(kMaxValue) <= kMaxSizeT) {
ASSERT_EQ(kMaxValue, metric_proto->int_value());
} else {
ASSERT_GT(kMaxValue, metric_proto->int_value());
}
}
} // namespace metrics
} // namespace wvcdm