From a73dc419fd8b7f87ee4c434b7d15d25482bbb08b Mon Sep 17 00:00:00 2001 From: "John \"Juce\" Bruce" Date: Thu, 7 Apr 2016 12:15:12 -0700 Subject: [PATCH] Restore WVCryptoPlugin Unit Tests (This is a merge of http://go/wvgerrit/17452) When the Crypto API was updated, several unit tests were removed, as they did not work with the new API. This patch restores those tests. Note that this does not add NEW tests for the new functionality enabled by the API changes. Those will be coming in a separate patch. This patch merely restores the previous tests and their functionality. Bug: 26901698 Change-Id: I3315408804bdc6871b58c3620e482bf6d61fe03b --- .../mediacrypto/test/WVCryptoPlugin_test.cpp | 76 +++++++++++-------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/libwvdrmengine/mediacrypto/test/WVCryptoPlugin_test.cpp b/libwvdrmengine/mediacrypto/test/WVCryptoPlugin_test.cpp index edca9dee..b9d45108 100644 --- a/libwvdrmengine/mediacrypto/test/WVCryptoPlugin_test.cpp +++ b/libwvdrmengine/mediacrypto/test/WVCryptoPlugin_test.cpp @@ -86,8 +86,10 @@ class CDPMatcherFactory { // Some values do not change over the course of the test. To avoid having // to re-specify them at every call site, we pass them into the factory // constructor. - CDPMatcherFactory(bool isSecure, uint8_t* keyId, void* out, size_t outLen) - : mIsSecure(isSecure), mKeyId(keyId), mOut(out), mOutLen(outLen) {} + CDPMatcherFactory(bool isSecure, CdmCipherMode cipherMode, uint8_t* keyId, + void* out, size_t outLen) + : mIsSecure(isSecure), mCipherMode(cipherMode), mKeyId(keyId), + mOut(out), mOutLen(outLen) {} Matcher operator()(bool isEncrypted, uint8_t* in, @@ -96,8 +98,9 @@ class CDPMatcherFactory { size_t blockOffset, size_t outOffset, uint8_t flags) const { - return Truly(CDPMatcher(mIsSecure, mKeyId, mOut, mOutLen, isEncrypted, - in, inLen, iv, blockOffset, outOffset, flags)); + return Truly(CDPMatcher(mIsSecure, mCipherMode, mKeyId, mOut, mOutLen, + isEncrypted, in, inLen, iv, blockOffset, + outOffset, flags)); } private: @@ -106,15 +109,18 @@ class CDPMatcherFactory { // time. class CDPMatcher { public: - CDPMatcher(bool isSecure, uint8_t* keyId, void* out, size_t outLen, - bool isEncrypted, uint8_t* in, size_t inLen, uint8_t* iv, - size_t blockOffset, size_t outOffset, uint8_t flags) - : mIsSecure(isSecure), mKeyId(keyId), mOut(out), mOutLen(outLen), - mIsEncrypted(isEncrypted), mIn(in), mInLen(inLen), mIv(iv), - mBlockOffset(blockOffset), mOutOffset(outOffset), mFlags(flags) {} + CDPMatcher(bool isSecure, CdmCipherMode cipherMode, uint8_t* keyId, + void* out, size_t outLen, bool isEncrypted, uint8_t* in, + size_t inLen, uint8_t* iv, size_t blockOffset, + size_t outOffset, uint8_t flags) + : mIsSecure(isSecure), mCipherMode(cipherMode), mKeyId(keyId), + mOut(out), mOutLen(outLen), mIsEncrypted(isEncrypted), mIn(in), + mInLen(inLen), mIv(iv), mBlockOffset(blockOffset), + mOutOffset(outOffset), mFlags(flags) {} bool operator()(const CdmDecryptionParameters& params) const { return params.is_secure == mIsSecure && + params.cipher_mode == mCipherMode && Value(*params.key_id, ElementsAreArray(mKeyId, KEY_ID_SIZE)) && params.decrypt_buffer == mOut && params.decrypt_buffer_length == mOutLen && @@ -129,6 +135,7 @@ class CDPMatcherFactory { private: bool mIsSecure; + CdmCipherMode mCipherMode; uint8_t* mKeyId; void* mOut; size_t mOutLen; @@ -142,13 +149,12 @@ class CDPMatcherFactory { }; bool mIsSecure; + CdmCipherMode mCipherMode; uint8_t* mKeyId; void* mOut; size_t mOutLen; }; -#if 0 // FIXME - handle pattern - TEST_F(WVCryptoPluginTest, AttemptsToDecrypt) { android::sp> cdm = new StrictMock(); @@ -189,7 +195,8 @@ TEST_F(WVCryptoPluginTest, AttemptsToDecrypt) { memcpy(iv[4], baseIv, sizeof(baseIv)); iv[4][15] = 7; - CDPMatcherFactory ParamsAre = CDPMatcherFactory(false, keyId, out, kDataSize); + CDPMatcherFactory ParamsAre = + CDPMatcherFactory(false, kCipherModeCtr, keyId, out, kDataSize); // Provide the expected behavior for IsOpenSession EXPECT_CALL(*cdm, IsOpenSession(_)) @@ -249,11 +256,12 @@ TEST_F(WVCryptoPluginTest, AttemptsToDecrypt) { } WVCryptoPlugin plugin(sessionId, kSessionIdSize, cdm.get()); + android::CryptoPlugin::Pattern noPattern = {0}; AString errorDetailMessage; ssize_t res = plugin.decrypt(false, keyId, iv[0], CryptoPlugin::kMode_AES_CTR, - in, subSamples, kSubSampleCount, out, - &errorDetailMessage); + noPattern, in, subSamples, kSubSampleCount, + out, &errorDetailMessage); EXPECT_EQ(static_cast(kDataSize), res) << "WVCryptoPlugin decrypted the wrong number of bytes"; @@ -302,18 +310,20 @@ TEST_F(WVCryptoPluginTest, CommunicatesSecureBufferRequest) { } WVCryptoPlugin plugin(sessionId, kSessionIdSize, cdm.get()); + android::CryptoPlugin::Pattern noPattern = {0}; AString errorDetailMessage; ssize_t res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, - in, subSamples, kSubSampleCount, out, - &errorDetailMessage); + noPattern, in, subSamples, kSubSampleCount, + out, &errorDetailMessage); ASSERT_GE(res, 0) << "WVCryptoPlugin returned an error"; EXPECT_EQ(0u, errorDetailMessage.size()) << "WVCryptoPlugin reported a detailed error message."; - res = plugin.decrypt(true, keyId, iv, CryptoPlugin::kMode_AES_CTR, in, - subSamples, kSubSampleCount, out, &errorDetailMessage); + res = plugin.decrypt(true, keyId, iv, CryptoPlugin::kMode_AES_CTR, + noPattern, in, subSamples, kSubSampleCount, out, + &errorDetailMessage); ASSERT_GE(res, 0) << "WVCryptoPlugin returned an error"; EXPECT_EQ(0u, errorDetailMessage.size()) << @@ -375,26 +385,27 @@ TEST_F(WVCryptoPluginTest, SetsFlagsForMinimumSubsampleRuns) { } WVCryptoPlugin plugin(sessionId, kSessionIdSize, cdm.get()); + android::CryptoPlugin::Pattern noPattern = {0}; AString errorDetailMessage; ssize_t res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, - in, clearSubSamples, kSubSampleCount, out, - &errorDetailMessage); + noPattern, in, clearSubSamples, + kSubSampleCount, out, &errorDetailMessage); ASSERT_GE(res, 0) << "WVCryptoPlugin returned an error"; EXPECT_EQ(0u, errorDetailMessage.size()) << "WVCryptoPlugin reported a detailed error message."; - res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, in, - encryptedSubSamples, kSubSampleCount, out, - &errorDetailMessage); + res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, + noPattern, in, encryptedSubSamples, kSubSampleCount, + out, &errorDetailMessage); ASSERT_GE(res, 0) << "WVCryptoPlugin returned an error"; EXPECT_EQ(0u, errorDetailMessage.size()) << "WVCryptoPlugin reported a detailed error message."; - res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, in, - mixedSubSamples, kSubSampleCount, out, + res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, + noPattern, in, mixedSubSamples, kSubSampleCount, out, &errorDetailMessage); ASSERT_GE(res, 0) << "WVCryptoPlugin returned an error"; @@ -450,13 +461,15 @@ TEST_F(WVCryptoPluginTest, AllowsSessionIdChanges) { uint8_t blank[1]; // Some compilers will not accept 0. WVCryptoPlugin plugin(blank, 0, cdm.get()); + android::CryptoPlugin::Pattern noPattern = {0}; AString errorDetailMessage; ssize_t res; res = plugin.setMediaDrmSession(sessionIdVector); EXPECT_EQ(android::NO_ERROR, res); - res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, in, - subSamples, kSubSampleCount, out, &errorDetailMessage); + res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, + noPattern, in, subSamples, kSubSampleCount, out, + &errorDetailMessage); EXPECT_GE(res, 0) << "WVCryptoPlugin returned an error"; EXPECT_EQ(0u, errorDetailMessage.size()) << @@ -464,16 +477,15 @@ TEST_F(WVCryptoPluginTest, AllowsSessionIdChanges) { res = plugin.setMediaDrmSession(sessionId2Vector); EXPECT_EQ(android::NO_ERROR, res); - res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, in, - subSamples, kSubSampleCount, out, &errorDetailMessage); + res = plugin.decrypt(false, keyId, iv, CryptoPlugin::kMode_AES_CTR, + noPattern, in, subSamples, kSubSampleCount, out, + &errorDetailMessage); EXPECT_GE(res, 0) << "WVCryptoPlugin returned an error"; EXPECT_EQ(0u, errorDetailMessage.size()) << "WVCryptoPlugin reported a detailed error message."; } -#endif - TEST_F(WVCryptoPluginTest, DisallowsUnopenedSessionIdChanges) { android::sp> cdm = new StrictMock();