From bf0c87e73466e820d871eb7928fa0759f66949f6 Mon Sep 17 00:00:00 2001 From: Fred Gylys-Colwell Date: Wed, 16 Sep 2015 13:26:04 -0700 Subject: [PATCH] Modify Code to Work with Clang/C++11 Merge from widevine repo of http://go/wvgerrit/15659 The clang compiler is more strict about C++11. This is needed for future Android work. In particular, iostream no longer converts to bool automtically, so those instances were replaced with ss.fail(). Arrays or structures that appear to be variable length need to be placed last in a structure. In oemcrypto_test a variable size structure was replaced with an explicit buffer size, and a check was added to make sure the buffer is not exceeded. bug: 20893039 Change-Id: I5e25fc618dcf68262079c15554ee4ceae1858b8b --- .../cdm/test/cdm_extended_duration_test.cpp | 3 +- .../cdm/test/request_license_test.cpp | 27 ++++++++++------ libwvdrmengine/cdm/test/unit-test.mk | 2 -- libwvdrmengine/oemcrypto/test/Android.mk | 2 -- .../oemcrypto/test/oemcrypto_test.cpp | 31 +++++++++---------- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/libwvdrmengine/cdm/test/cdm_extended_duration_test.cpp b/libwvdrmengine/cdm/test/cdm_extended_duration_test.cpp index 2e9024b6..968d8812 100644 --- a/libwvdrmengine/cdm/test/cdm_extended_duration_test.cpp +++ b/libwvdrmengine/cdm/test/cdm_extended_duration_test.cpp @@ -168,7 +168,8 @@ std::string kOfflineClip2PstInitData = wvcdm::a2bs_hex( bool StringToInt64(const std::string& input, int64_t* output) { std::istringstream ss(input); - return ss >> *output; + ss >> *output; + return !ss.fail(); } } // namespace diff --git a/libwvdrmengine/cdm/test/request_license_test.cpp b/libwvdrmengine/cdm/test/request_license_test.cpp index 3191442a..f3554d8b 100644 --- a/libwvdrmengine/cdm/test/request_license_test.cpp +++ b/libwvdrmengine/cdm/test/request_license_test.cpp @@ -2068,13 +2068,15 @@ TEST_F(WvCdmRequestLicenseTest, QueryKeyStatus) { itr = query_info.find(wvcdm::QUERY_KEY_LICENSE_DURATION_REMAINING); ASSERT_TRUE(itr != query_info.end()); ss.str(itr->second); - ASSERT_TRUE(ss >> remaining_time); + ss >> remaining_time; + ASSERT_FALSE(ss.fail()); EXPECT_LT(0, remaining_time); itr = query_info.find(wvcdm::QUERY_KEY_PLAYBACK_DURATION_REMAINING); ASSERT_TRUE(itr != query_info.end()); ss.clear(); ss.str(itr->second); - ASSERT_TRUE(ss >> remaining_time); + ss >> remaining_time; + ASSERT_FALSE(ss.fail()); EXPECT_LT(0, remaining_time); itr = query_info.find(wvcdm::QUERY_KEY_RENEWAL_SERVER_URL); @@ -2103,7 +2105,8 @@ TEST_F(WvCdmRequestLicenseTest, QueryStatus) { ASSERT_TRUE(itr != query_info.end()); std::istringstream ss(itr->second); uint32_t system_id; - EXPECT_TRUE(ss >> system_id); + ss >> system_id; + ASSERT_FALSE(ss.fail()); EXPECT_TRUE(ss.eof()); itr = query_info.find(wvcdm::QUERY_KEY_PROVISIONING_ID); @@ -2138,7 +2141,8 @@ TEST_F(WvCdmRequestLicenseTest, QueryStatus) { ss.clear(); ss.str(itr->second); uint32_t open_sessions; - EXPECT_TRUE(ss >> open_sessions); + ss >> open_sessions; + ASSERT_FALSE(ss.fail()); EXPECT_TRUE(ss.eof()); itr = query_info.find(QUERY_KEY_MAX_NUMBER_OF_SESSIONS); @@ -2146,7 +2150,8 @@ TEST_F(WvCdmRequestLicenseTest, QueryStatus) { ss.clear(); ss.str(itr->second); uint32_t max_sessions; - EXPECT_TRUE(ss >> max_sessions); + ss >> max_sessions; + ASSERT_FALSE(ss.fail()); EXPECT_TRUE(ss.eof()); EXPECT_LE(open_sessions, max_sessions); EXPECT_LE(8u, max_sessions); @@ -2156,7 +2161,8 @@ TEST_F(WvCdmRequestLicenseTest, QueryStatus) { ss.clear(); ss.str(itr->second); uint32_t api_version; - EXPECT_TRUE(ss >> api_version); + ss >> api_version; + ASSERT_FALSE(ss.fail()); EXPECT_TRUE(ss.eof()); EXPECT_LE(10u, api_version); } @@ -2181,7 +2187,8 @@ TEST_F(WvCdmRequestLicenseTest, QueryStatusL3) { ASSERT_TRUE(itr != query_info.end()); std::istringstream ss(itr->second); uint32_t system_id; - EXPECT_TRUE(ss >> system_id); + ss >> system_id; + ASSERT_FALSE(ss.fail()); EXPECT_TRUE(ss.eof()); itr = query_info.find(wvcdm::QUERY_KEY_PROVISIONING_ID); @@ -2198,7 +2205,8 @@ TEST_F(WvCdmRequestLicenseTest, QueryStatusL3) { ASSERT_TRUE(itr != query_info_default.end()); std::istringstream ss(itr->second); uint32_t default_system_id; - EXPECT_TRUE(ss >> system_id); + ss >> system_id; + ASSERT_FALSE(ss.fail()); EXPECT_TRUE(ss.eof()); EXPECT_NE(system_id, default_system_id); } @@ -2222,7 +2230,8 @@ TEST_F(WvCdmRequestLicenseTest, QueryKeyControlInfo) { ASSERT_TRUE(itr != query_info.end()); std::istringstream ss; ss.str(itr->second); - EXPECT_TRUE(ss >> oem_crypto_session_id); + ss >> oem_crypto_session_id; + ASSERT_FALSE(ss.fail()); decryptor_.CloseSession(session_id_); } diff --git a/libwvdrmengine/cdm/test/unit-test.mk b/libwvdrmengine/cdm/test/unit-test.mk index 8a38b4cf..a13d78be 100644 --- a/libwvdrmengine/cdm/test/unit-test.mk +++ b/libwvdrmengine/cdm/test/unit-test.mk @@ -6,8 +6,6 @@ $(call assert-not-null,test_name) include $(CLEAR_VARS) -LOCAL_CLANG := false - LOCAL_MODULE := $(test_name) LOCAL_MODULE_TAGS := tests diff --git a/libwvdrmengine/oemcrypto/test/Android.mk b/libwvdrmengine/oemcrypto/test/Android.mk index 92ee0313..56e6fff6 100644 --- a/libwvdrmengine/oemcrypto/test/Android.mk +++ b/libwvdrmengine/oemcrypto/test/Android.mk @@ -2,8 +2,6 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) -LOCAL_CLANG := false - LOCAL_MODULE:=oemcrypto_test LOCAL_MODULE_TAGS := tests diff --git a/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp b/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp index 4f9e1003..d6cc17b6 100644 --- a/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp +++ b/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp @@ -98,11 +98,6 @@ struct RSAPrivateKeyMessage { uint32_t nonce; }; -struct PaddedPSTReport { - OEMCrypto_PST_Report report; - uint8_t padding[256]; -}; - // These are test keyboxes. They will not be accepted by production systems. // By using known keyboxes for these tests, the results for a given set of // inputs to a test are predictable and can be compared to the actual results. @@ -1400,16 +1395,17 @@ class Session { size_t length = 0; OEMCryptoResult sts = OEMCrypto_ReportUsage( session_id(), reinterpret_cast(pst.c_str()), - pst.length(), &pst_report_.report, &length); + pst.length(), pst_report(), &length); if (expect_success) { ASSERT_EQ(OEMCrypto_ERROR_SHORT_BUFFER, sts); } if (sts == OEMCrypto_ERROR_SHORT_BUFFER) { - ASSERT_GE(sizeof(PaddedPSTReport), length); + ASSERT_LE(sizeof(OEMCrypto_PST_Report), length); + pst_report_buffer_.resize(length); } sts = OEMCrypto_ReportUsage(session_id(), reinterpret_cast(pst.c_str()), - pst.length(), &pst_report_.report, &length); + pst.length(), pst_report(), &length); if (!expect_success) { ASSERT_NE(OEMCrypto_SUCCESS, sts); return; @@ -1418,16 +1414,19 @@ class Session { vector computed_signature(SHA_DIGEST_LENGTH); unsigned int sig_len = SHA_DIGEST_LENGTH; HMAC(EVP_sha1(), &mac_key_client_[0], mac_key_client_.size(), - reinterpret_cast(&pst_report_.report) + SHA_DIGEST_LENGTH, + reinterpret_cast(pst_report()) + SHA_DIGEST_LENGTH, length - SHA_DIGEST_LENGTH, &computed_signature[0], &sig_len); - EXPECT_EQ(0, memcmp(&computed_signature[0], pst_report_.report.signature, + EXPECT_EQ(0, memcmp(&computed_signature[0], pst_report()->signature, SHA_DIGEST_LENGTH)); - EXPECT_GE(kInactive, pst_report_.report.status); - EXPECT_GE(kHardwareSecureClock, pst_report_.report.clock_security_level); - EXPECT_EQ(pst.length(), pst_report_.report.pst_length); - EXPECT_EQ(0, memcmp(pst.c_str(), pst_report_.report.pst, pst.length())); + EXPECT_GE(kInactive, pst_report()->status); + EXPECT_GE(kHardwareSecureClock, pst_report()->clock_security_level); + EXPECT_EQ(pst.length(), pst_report()->pst_length); + EXPECT_EQ(0, memcmp(pst.c_str(), pst_report()->pst, pst.length())); + } + + OEMCrypto_PST_Report* pst_report() { + return reinterpret_cast(&pst_report_buffer_[0]); } - OEMCrypto_PST_Report* pst_report() { return &pst_report_.report; } void DeleteEntry(const std::string& pst) { uint8_t* pst_ptr = encrypted_license_.pst; @@ -1462,7 +1461,7 @@ class Session { vector enc_key_; uint32_t nonce_; RSA* public_rsa_; - PaddedPSTReport pst_report_; + vector pst_report_buffer_; MessageData license_; MessageData encrypted_license_; OEMCrypto_KeyObject key_array_[kNumKeys];