Merge "Created an ArraySize utility function."

This commit is contained in:
Alex Dale
2019-07-23 22:08:39 +00:00
committed by Android (Google) Code Review
5 changed files with 57 additions and 33 deletions

View File

@@ -2,12 +2,15 @@
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#include "device_files.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <string>
#include "device_files.h"
#include "arraysize.h"
#include "file_store.h"
#include "properties.h"
#include "string_conversions.h"
@@ -2513,8 +2516,7 @@ TEST_F(DeviceFilesTest, UpdateLicenseState) {
DeviceFiles device_files(&file_system);
EXPECT_TRUE(device_files.Init(kSecurityLevelL1));
for (size_t i = 0; i < sizeof(license_update_test_data) / sizeof(LicenseInfo);
i++) {
for (size_t i = 0; i < ArraySize(license_update_test_data); i++) {
// Call to Open will return a unique_ptr, freeing this object.
MockFile* file = new MockFile();
EXPECT_CALL(file_system, DoOpen(StrEq(license_path), IsCreateFileFlagSet()))
@@ -2954,7 +2956,7 @@ TEST_P(DeviceFilesUsageInfoTest, RetrieveByProviderSessionToken) {
std::string path = device_base_path_ + file_name;
size_t max_index_by_app_id = 0;
for (size_t i = 0; i < sizeof(kUsageInfoTestData) / sizeof(UsageInfo); ++i) {
for (size_t i = 0; i < ArraySize(kUsageInfoTestData); ++i) {
if (app_id == kUsageInfoTestData[i].app_id) max_index_by_app_id = i;
}
std::string file_data =
@@ -3014,7 +3016,7 @@ TEST_P(DeviceFilesUsageInfoTest, UpdateUsageInfo) {
std::vector<std::string> usage_data_fields;
size_t max_index_by_app_id = 0;
for (size_t i = 0; i < sizeof(kUsageInfoTestData) / sizeof(UsageInfo); ++i) {
for (size_t i = 0; i < ArraySize(kUsageInfoTestData); ++i) {
if (app_id == kUsageInfoTestData[i].app_id) {
max_index_by_app_id = i;

View File

@@ -6,8 +6,10 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include "arraysize.h"
#include "crypto_session.h"
#include "device_files.h"
#include "file_store.h"
@@ -103,8 +105,7 @@ const std::string kLicenseArray[] = {
kUsageEntryInfoOfflineLicense2.key_set_id,
kUsageEntryInfoOfflineLicense3.key_set_id,
};
const size_t kLicenseArraySize = sizeof(kLicenseArray)/
sizeof(kLicenseArray[0]);
const size_t kLicenseArraySize = ArraySize(kLicenseArray);
std::vector<std::string> kLicenseList;
const std::vector<std::string> kEmptyUsageInfoFilesList;
@@ -114,8 +115,7 @@ const std::string kUsageInfoFileArray[] = {
kUsageEntryInfoSecureStop2.usage_info_file_name,
kUsageEntryInfoSecureStop3.usage_info_file_name,
};
const size_t kUsageInfoFileArraySize = sizeof(kUsageInfoFileArray)/
sizeof(kUsageInfoFileArray[0]);
const size_t kUsageInfoFileArraySize = ArraySize(kUsageInfoFileArray);
std::vector<std::string> kUsageInfoFileList;
const DeviceFiles::CdmUsageData kCdmUsageData1 = {
@@ -322,6 +322,7 @@ class UsageTableHeaderTest : public WvCdmTestBase {
// UsageTableHeader will take ownership of the pointer
device_files_ = new MockDeviceFiles();
crypto_session_ = new MockCryptoSession(&crypto_metrics_);
usage_table_header_ = new UsageTableHeader();
// usage_table_header_ object takes ownership of these objects

View File

@@ -26,6 +26,7 @@
#include <gtest/gtest.h>
#include "OEMCryptoCENC.h"
#include "arraysize.h"
#include "config_test_env.h"
#include "license_protocol.pb.h"
#include "license_request.h"
@@ -50,8 +51,6 @@ using ::testing::StrictMock;
namespace {
#define N_ELEM(a) (sizeof(a) / sizeof(a[0]))
// HTTP response codes.
const int kHttpOk = 200;
// The following two responses are unused, but left here for human debuggers.
@@ -535,7 +534,7 @@ TEST_P(WvCdmSrmTest, Srm) {
INSTANTIATE_TEST_CASE_P(
Cdm, WvCdmSrmTest,
::testing::Range(&kSrmTestConfiguration[0],
&kSrmTestConfiguration[N_ELEM(kSrmTestConfiguration)]));
&kSrmTestConfiguration[ArraySize(kSrmTestConfiguration)]));
// These parameterized tests validate SRM scenarios described in
// SRM End-to-End Test Plan doc
@@ -585,6 +584,6 @@ TEST_P(WvCdmSrmNotSupportedTest, Srm) {
INSTANTIATE_TEST_CASE_P(
Cdm, WvCdmSrmNotSupportedTest,
::testing::Range(&kSrmNotSupportedTestConfiguration[0],
&kSrmNotSupportedTestConfiguration[N_ELEM(
&kSrmNotSupportedTestConfiguration[ArraySize(
kSrmNotSupportedTestConfiguration)]));
} // namespace wvcdm

View File

@@ -13,6 +13,8 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "OEMCryptoCENC.h"
#include "arraysize.h"
#include "cdm_identifier.h"
#include "config_test_env.h"
#include "device_files.h"
@@ -23,7 +25,6 @@
#include "license_request.h"
#include "log.h"
#include "oemcrypto_adapter.h"
#include "OEMCryptoCENC.h"
#include "properties.h"
#include "string_conversions.h"
#include "test_base.h"
@@ -47,8 +48,6 @@ using ::testing::UnorderedElementsAreArray;
namespace {
#define N_ELEM(a) (sizeof(a)/sizeof(a[0]))
// HTTP response codes.
const int kHttpOk = 200;
@@ -3776,7 +3775,7 @@ TEST_F(WvCdmRequestLicenseTest, UsageRemoveAllTest) {
EXPECT_TRUE(handle.DeleteAllUsageInfoForApp(
DeviceFiles::GetUsageInfoFileName(""), &psts));
for (size_t i = 0; i < N_ELEM(usage_info_sub_samples_icp); ++i) {
for (size_t i = 0; i < ArraySize(usage_info_sub_samples_icp); ++i) {
SubSampleInfo* data = usage_info_sub_samples_icp + i;
property_set.set_app_id(i % 2 == 0 ? app_id_empty : app_id_not_empty);
decryptor_->OpenSession(config_.key_system(), &property_set,
@@ -3874,7 +3873,7 @@ TEST_F(WvCdmRequestLicenseTest, RemoveCorruptedUsageInfoTest) {
EXPECT_TRUE(handle.DeleteAllUsageInfoForApp(
DeviceFiles::GetUsageInfoFileName(""), &psts));
for (size_t i = 0; i < N_ELEM(usage_info_sub_samples_icp); ++i) {
for (size_t i = 0; i < ArraySize(usage_info_sub_samples_icp); ++i) {
SubSampleInfo* data = usage_info_sub_samples_icp + i;
property_set.set_app_id(i % 2 == 0 ? app_id_empty : app_id_not_empty);
decryptor_->OpenSession(config_.key_system(), &property_set,
@@ -4020,7 +4019,7 @@ TEST_F(WvCdmRequestLicenseTest, RemoveCorruptedUsageInfoTest2) {
EXPECT_TRUE(handle.DeleteAllUsageInfoForApp(
DeviceFiles::GetUsageInfoFileName(""), &psts));
for (size_t i = 0; i < N_ELEM(usage_info_sub_samples_icp); ++i) {
for (size_t i = 0; i < ArraySize(usage_info_sub_samples_icp); ++i) {
SubSampleInfo* data = usage_info_sub_samples_icp + i;
property_set.set_app_id(i % 2 == 0 ? app_id_empty : app_id_not_empty);
decryptor_->OpenSession(config_.key_system(), &property_set,
@@ -4187,7 +4186,7 @@ TEST_F(WvCdmRequestLicenseTest, GetSecureStopIdsTest) {
EXPECT_TRUE(retrieved_secure_stop_ids.empty());
// First fetch licenses for the default app
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector1); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector1); ++i) {
SubSampleInfo* data = kUsageLicenseTestVector1[i].sub_sample;
property_set.set_app_id(app_id_empty);
@@ -4219,7 +4218,7 @@ TEST_F(WvCdmRequestLicenseTest, GetSecureStopIdsTest) {
// Verify that there are usage records for the default identifier but
// none yet for the non-default one
std::vector<CdmSecureStopId> expected_provider_session_tokens;
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector1); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector1); ++i) {
expected_provider_session_tokens.push_back(
kUsageLicenseTestVector1[i].provider_session_token);
}
@@ -4228,7 +4227,8 @@ TEST_F(WvCdmRequestLicenseTest, GetSecureStopIdsTest) {
NO_ERROR,
decryptor_->GetSecureStopIds(app_id_empty, kDefaultCdmIdentifier,
&retrieved_secure_stop_ids));
EXPECT_EQ(N_ELEM(kUsageLicenseTestVector1), retrieved_secure_stop_ids.size());
EXPECT_EQ(ArraySize(kUsageLicenseTestVector1),
retrieved_secure_stop_ids.size());
EXPECT_THAT(retrieved_secure_stop_ids,
UnorderedElementsAreArray(expected_provider_session_tokens));
@@ -4240,7 +4240,7 @@ TEST_F(WvCdmRequestLicenseTest, GetSecureStopIdsTest) {
EXPECT_TRUE(retrieved_secure_stop_ids.empty());
// Now fetch licenses for the other identifier
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector2); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector2); ++i) {
SubSampleInfo* data = kUsageLicenseTestVector2[i].sub_sample;
property_set.set_app_id(kExampleIdentifier.app_package_name);
@@ -4278,14 +4278,15 @@ TEST_F(WvCdmRequestLicenseTest, GetSecureStopIdsTest) {
decryptor_->GetSecureStopIds(kDefaultCdmIdentifier.app_package_name,
kDefaultCdmIdentifier,
&retrieved_secure_stop_ids));
EXPECT_EQ(N_ELEM(kUsageLicenseTestVector1), retrieved_secure_stop_ids.size());
EXPECT_EQ(ArraySize(kUsageLicenseTestVector1),
retrieved_secure_stop_ids.size());
EXPECT_THAT(retrieved_secure_stop_ids,
UnorderedElementsAreArray(expected_provider_session_tokens));
retrieved_secure_stop_ids.clear();
expected_provider_session_tokens.clear();
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector2); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector2); ++i) {
expected_provider_session_tokens.push_back(
kUsageLicenseTestVector2[i].provider_session_token);
}
@@ -4295,7 +4296,8 @@ TEST_F(WvCdmRequestLicenseTest, GetSecureStopIdsTest) {
decryptor_->GetSecureStopIds(kExampleIdentifier.app_package_name,
kExampleIdentifier,
&retrieved_secure_stop_ids));
EXPECT_EQ(N_ELEM(kUsageLicenseTestVector2), retrieved_secure_stop_ids.size());
EXPECT_EQ(ArraySize(kUsageLicenseTestVector2),
retrieved_secure_stop_ids.size());
EXPECT_THAT(retrieved_secure_stop_ids,
UnorderedElementsAreArray(expected_provider_session_tokens));
@@ -4447,7 +4449,7 @@ TEST_F(WvCdmRequestLicenseTest, UsageRemoveSecureStopTest) {
DeviceFiles::GetUsageInfoFileName(""), &psts));
// First fetch licenses for the default app
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector1); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector1); ++i) {
SubSampleInfo* data = kUsageLicenseTestVector1[i].sub_sample;
property_set.set_app_id(app_id_empty);
@@ -4476,7 +4478,7 @@ TEST_F(WvCdmRequestLicenseTest, UsageRemoveSecureStopTest) {
// Provision and fetch licenses for the other identifier
Provision(kExampleIdentifier, kLevelDefault);
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector2); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector2); ++i) {
SubSampleInfo* data = kUsageLicenseTestVector2[i].sub_sample;
property_set.set_app_id(kExampleIdentifier.app_package_name);
@@ -4513,9 +4515,9 @@ TEST_F(WvCdmRequestLicenseTest, UsageRemoveSecureStopTest) {
NO_ERROR,
decryptor_->GetSecureStopIds(kDefaultCdmIdentifier.app_package_name,
kDefaultCdmIdentifier, &secure_stop_ids));
EXPECT_EQ(N_ELEM(kUsageLicenseTestVector1), secure_stop_ids.size());
EXPECT_EQ(ArraySize(kUsageLicenseTestVector1), secure_stop_ids.size());
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector1); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector1); ++i) {
EXPECT_EQ(
NO_ERROR,
decryptor_->RemoveUsageInfo(kDefaultCdmIdentifier.app_package_name,
@@ -4532,9 +4534,9 @@ TEST_F(WvCdmRequestLicenseTest, UsageRemoveSecureStopTest) {
NO_ERROR,
decryptor_->GetSecureStopIds(kExampleIdentifier.app_package_name,
kExampleIdentifier, &secure_stop_ids));
EXPECT_EQ(N_ELEM(kUsageLicenseTestVector2), secure_stop_ids.size());
EXPECT_EQ(ArraySize(kUsageLicenseTestVector2), secure_stop_ids.size());
for (size_t i = 0; i < N_ELEM(kUsageLicenseTestVector2); ++i) {
for (size_t i = 0; i < ArraySize(kUsageLicenseTestVector2); ++i) {
EXPECT_EQ(
NO_ERROR,
decryptor_->RemoveUsageInfo(kExampleIdentifier.app_package_name,
@@ -5807,7 +5809,7 @@ TEST_P(WvCenc30SwitchCipherModeTest, DecryptionTest) {
CdmKeyStatusMap key_status_map = listener.GetKeyStatusMap();
EXPECT_EQ(8u, key_status_map.size());
for (size_t i = 0; i < N_ELEM(info->sample_info); ++i) {
for (size_t i = 0; i < ArraySize(info->sample_info); ++i) {
Cenc30SampleInfo* data = &info->sample_info[i];
std::vector<uint8_t> output_buffer(data->encrypted_data.size(), 0);
std::vector<uint8_t> iv(data->iv.begin(), data->iv.end());

View File

@@ -0,0 +1,20 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#ifndef WVCDM_UTIL_ARRAYSIZE_H_
#define WVCDM_UTIL_ARRAYSIZE_H_
#include <stdint.h>
namespace wvcdm {
// Returns the size of a fixed-length array.
template <typename T, size_t N>
constexpr size_t ArraySize(const T (&)[N]) {
return N;
}
} // namespace wvcdm
#endif // WVCDM_UTIL_ARRAYSIZE_H_