Files
android/libwvdrmengine/cdm/metrics/test/counter_metric_unittest.cpp
Edwin 3c3da01d58 Use aidl interface for Widevine service.
The interface is defined in
hardware/interfaces/drm/aidl(http://go/ag/15329852).

Test: build
  m android.hardware.drm-service.widevine -j128

Test: build_and_run_all_unit_tests.sh
  for hidl tests

Test: atest VtsAidlHalDrmTargetTest

Test:   atest vts_treble_vintf_vendor_test:vts_treble_vintf_vendor_test.DeviceManifest/SingleManifestTest#ManifestAidlHalsServed/0 -- --abi x86_64

Bug: 200055138
Bug: 170964303
Change-Id: I5654d90d8a4b0bae4b4a78e79b27c1cafec36be7
2022-02-01 22:20:04 -08:00

131 lines
5.0 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 CounterMetric
#include "counter_metric.h"
#include <gtest/gtest.h>
#include "pow2bucket.h"
#include "string_conversions.h"
#include "wv_cdm_types.h"
namespace wvcdm {
namespace metrics {
using drm_metrics::TestMetrics;
TEST(CounterMetricTest, NoFieldsEmpty) {
wvcdm::metrics::CounterMetric<> metric;
TestMetrics metric_proto;
metric.ToProto(metric_proto.mutable_test_counters());
ASSERT_EQ(0, metric_proto.test_counters().size());
}
TEST(CounterMetricTest, NoFieldsSuccess) {
wvcdm::metrics::CounterMetric<> metric;
metric.Increment();
metric.Increment(10);
TestMetrics metric_proto;
std::string serialized_metrics;
metric.ToProto(metric_proto.mutable_test_counters());
ASSERT_EQ(1, metric_proto.test_counters().size());
ASSERT_TRUE(metric_proto.SerializeToString(&serialized_metrics));
EXPECT_EQ(11, metric_proto.test_counters(0).count());
EXPECT_FALSE(metric_proto.test_counters(0).has_attributes())
<< std::string("Unexpected attributes value. Serialized metrics: ")
<< wvutil::b2a_hex(serialized_metrics);
}
TEST(CounterMetricTest, OneFieldSuccess) {
wvcdm::metrics::CounterMetric<drm_metrics::Attributes::kErrorCodeFieldNumber,
int>
metric;
metric.Increment(7);
metric.Increment(10, 7);
metric.Increment(13);
metric.Increment(20, 13);
TestMetrics metric_proto;
metric.ToProto(metric_proto.mutable_test_counters());
ASSERT_EQ(2, metric_proto.test_counters().size());
EXPECT_EQ(11u, metric_proto.test_counters(0).count());
EXPECT_EQ(7, metric_proto.test_counters(0).attributes().error_code());
EXPECT_EQ(21, metric_proto.test_counters(1).count());
EXPECT_EQ(13, metric_proto.test_counters(1).attributes().error_code());
}
TEST(CounterMetricTest, TwoFieldsSuccess) {
CounterMetric<drm_metrics::Attributes::kErrorCodeFieldNumber, int,
drm_metrics::Attributes::kLengthFieldNumber, Pow2Bucket>
metric;
metric.Increment(7, Pow2Bucket(23)); // Increment by one.
metric.Increment(2, 7, Pow2Bucket(33));
metric.Increment(3, 11, Pow2Bucket(23));
metric.Increment(4, 11, Pow2Bucket(33));
metric.Increment(5, 7, Pow2Bucket(23));
metric.Increment(-5, 7, Pow2Bucket(33));
// Verify all instances.
TestMetrics metric_proto;
metric.ToProto(metric_proto.mutable_test_counters());
ASSERT_EQ(4, metric_proto.test_counters().size());
EXPECT_EQ(6, metric_proto.test_counters(0).count());
EXPECT_EQ(7, metric_proto.test_counters(0).attributes().error_code());
EXPECT_EQ(16u, metric_proto.test_counters(0).attributes().length());
EXPECT_EQ(-3, metric_proto.test_counters(1).count());
EXPECT_EQ(7, metric_proto.test_counters(1).attributes().error_code());
EXPECT_EQ(32u, metric_proto.test_counters(1).attributes().length());
EXPECT_EQ(3, metric_proto.test_counters(2).count());
EXPECT_EQ(11, metric_proto.test_counters(2).attributes().error_code());
EXPECT_EQ(16u, metric_proto.test_counters(2).attributes().length());
EXPECT_EQ(4, metric_proto.test_counters(3).count());
EXPECT_EQ(11, metric_proto.test_counters(3).attributes().error_code());
EXPECT_EQ(32u, metric_proto.test_counters(3).attributes().length());
}
TEST(CounterMetricTest, ThreeFieldsSuccess) {
CounterMetric<drm_metrics::Attributes::kErrorCodeFieldNumber, int,
drm_metrics::Attributes::kLengthFieldNumber, Pow2Bucket,
drm_metrics::Attributes::kErrorCodeBoolFieldNumber, bool>
metric;
metric.Increment(7, Pow2Bucket(13), true);
TestMetrics metric_proto;
metric.ToProto(metric_proto.mutable_test_counters());
ASSERT_EQ(1, metric_proto.test_counters().size());
EXPECT_EQ(1, metric_proto.test_counters(0).count());
EXPECT_EQ(7, metric_proto.test_counters(0).attributes().error_code());
EXPECT_EQ(8u, metric_proto.test_counters(0).attributes().length());
EXPECT_TRUE(metric_proto.test_counters(0).attributes().error_code_bool());
}
TEST(CounterMetricTest, FourFieldsSuccess) {
CounterMetric<drm_metrics::Attributes::kErrorCodeFieldNumber, int,
drm_metrics::Attributes::kLengthFieldNumber, Pow2Bucket,
drm_metrics::Attributes::kErrorCodeBoolFieldNumber, bool,
drm_metrics::Attributes::kSecurityLevelFieldNumber,
RequestedSecurityLevel>
metric;
metric.Increment(10LL, 7, Pow2Bucket(13), true, kLevel3);
TestMetrics metric_proto;
metric.ToProto(metric_proto.mutable_test_counters());
ASSERT_EQ(1, metric_proto.test_counters().size());
EXPECT_EQ(10, metric_proto.test_counters(0).count());
EXPECT_EQ(7, metric_proto.test_counters(0).attributes().error_code());
EXPECT_EQ(8u, metric_proto.test_counters(0).attributes().length());
EXPECT_TRUE(metric_proto.test_counters(0).attributes().error_code_bool());
EXPECT_EQ(kLevel3,
metric_proto.test_counters(0).attributes().security_level());
}
} // namespace metrics
} // namespace wvcdm