From f2c3db81d349577420f23d64d95f04cf55106049 Mon Sep 17 00:00:00 2001 From: Jeff Tinker Date: Thu, 18 Apr 2013 17:11:02 -0700 Subject: [PATCH] Unit test script and end-to-end license/decrypt test update Does not affect any production code, only unit tests Makes the end-to-end license/decrypt test compatible with the fix for b/8604068 (merged). related-to-bug: 8604068 Merge of https://widevine-internal-review.googlesource.com/#/c/4982/ from widevine cdm repository to android repository. Change-Id: Ic5739c9de0cd385a672311ec87b9a475e367121a --- .../build_and_run_all_unit_tests.sh | 5 +- .../com/widevine/test/MediaDrmAPITest.java | 100 ++++++++++++------ 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/libwvdrmengine/build_and_run_all_unit_tests.sh b/libwvdrmengine/build_and_run_all_unit_tests.sh index 750b6f5c..d2ae3a6b 100755 --- a/libwvdrmengine/build_and_run_all_unit_tests.sh +++ b/libwvdrmengine/build_and_run_all_unit_tests.sh @@ -35,7 +35,7 @@ cd $ANDROID_BUILD_TOP/vendor/widevine/libwvdrmengine/oemcrypto/test pwd mm -cd $ANDROID_BUILD_TOP/vendor/widevine/libwvdrmengine/test +cd $ANDROID_BUILD_TOP/vendor/widevine/libwvdrmengine/test/unit pwd mm @@ -46,14 +46,13 @@ mm echo "waiting for device" adb root && adb wait-for-device remount && adb sync - adb shell /system/bin/request_license_test adb shell /system/bin/policy_engine_unittest adb shell /system/bin/libwvdrmmediacrypto_test adb shell /system/bin/libwvdrmdrmplugin_test -adb shell /system/bin/http_socket_test adb shell /system/bin/cdm_engine_test adb shell /system/bin/oemcrypto_test +adb shell LD_LIBRARY_PATH=/system/vendor/lib/mediadrm/ /system/bin/libwvdrmengine_test adb shell am start com.widevine.test/com.widevine.test.MediaDrmAPITest diff --git a/libwvdrmengine/test/java/src/com/widevine/test/MediaDrmAPITest.java b/libwvdrmengine/test/java/src/com/widevine/test/MediaDrmAPITest.java index 26d424dd..56aa9059 100644 --- a/libwvdrmengine/test/java/src/com/widevine/test/MediaDrmAPITest.java +++ b/libwvdrmengine/test/java/src/com/widevine/test/MediaDrmAPITest.java @@ -22,6 +22,7 @@ import java.util.UUID; import java.util.Arrays; import java.util.LinkedList; import java.util.ListIterator; +import java.util.Random; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; @@ -258,50 +259,87 @@ public class MediaDrmAPITest extends TabActivity { } Log.i(TAG, "Got index " + index); + final int kMaxSubsamplesPerSample = 10; + final int kMaxSampleSize = 128 * 1024; +; + int clearSizes[] = new int[kMaxSubsamplesPerSample]; + int encryptedSizes[] = new int[kMaxSubsamplesPerSample]; + LinkedList vectors = TestVectors.GetTestVectors(); ListIterator iter = vectors.listIterator(0); + + ByteBuffer refBuffer = ByteBuffer.allocate(kMaxSampleSize); + + Random rand = new Random(); + + byte iv[] = null; + byte keyID[] = null; + + int numSubSamples = 0; + int sampleSize = 0; + while (iter.hasNext()) { TestVector tv = iter.next(); + if (tv.mByteOffset == 0) { + // start of a new sample - CryptoInfo info = new CryptoInfo(); - int clearSizes[] = { tv.mByteOffset }; - int encryptedSizes[] = { tv.mEncryptedBuf.length }; + if (numSubSamples > 0) { + // send the sample we have + CryptoInfo info = new CryptoInfo(); + info.set(numSubSamples, clearSizes, encryptedSizes, keyID, iv, + MediaCodec.CRYPTO_MODE_AES_CTR); - info.set(1, clearSizes, encryptedSizes, tv.mKeyID, tv.mIV, - MediaCodec.CRYPTO_MODE_AES_CTR); + try { + Log.i(TAG,"Sending " + sampleSize + " bytes, numSubSamples=" + numSubSamples); + codec.queueSecureInputBuffer(index, 0 /* offset */, info, + 0 /* sampleTime */, 0 /* flags */); + } catch (CryptoException e) { + Log.i(TAG,"Checking " + sampleSize + " bytes"); - byte clearBuf[] = new byte[tv.mByteOffset]; + // in test mode, the WV CryptoPlugin throws a CryptoException where the + // message string contains a SHA256 hash of the decrypted data, for verification + // purposes. + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte buf[] = Arrays.copyOf(refBuffer.array(), sampleSize); + byte[] sha256 = digest.digest(buf); + if (Arrays.equals(sha256, hex2ba(e.getMessage()))) { + Log.i(TAG, "sha256: " + e.getMessage() + " matches OK"); + } else { + Log.i(TAG, "MediaCrypto sha256: " + e.getMessage() + + " does not match test vector sha256: "); + for (int i = 0; i < sha256.length; i++) { + System.out.printf("%02x", sha256[i]); + } + } + } + + // clear buffers for next sample + numSubSamples = 0; + sampleSize = 0; + inputBuffers[index].clear(); + refBuffer.clear(); + } + keyID = tv.mKeyID; + iv = tv.mIV; + } + + // add this subsample vector to the list + int clearSize = rand.nextInt(100); + byte clearBuf[] = new byte[clearSize]; for (int i = 0; i < clearBuf.length; i++) { clearBuf[i] = (byte)i; } - inputBuffers[index].clear(); + clearSizes[numSubSamples] = clearSize; + encryptedSizes[numSubSamples] = tv.mEncryptedBuf.length; + numSubSamples++; + inputBuffers[index].put(clearBuf, 0, clearBuf.length); inputBuffers[index].put(tv.mEncryptedBuf, 0, tv.mEncryptedBuf.length); - try { - codec.queueSecureInputBuffer(index, 0 /* offset */, info, - 0 /* sampleTime */, 0 /* flags */); - } catch (CryptoException e) { - ByteBuffer refBuffer = ByteBuffer.allocate(clearBuf.length + tv.mClearBuf.length); - refBuffer.put(clearBuf, 0, clearBuf.length); - refBuffer.put(tv.mClearBuf, 0, tv.mClearBuf.length); - - // in test mode, the WV CryptoPlugin throws a CryptoException where the - // message string contains a SHA256 hash of the decrypted data, for verification - // purposes. - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] sha256 = digest.digest(refBuffer.array()); - if (Arrays.equals(sha256, hex2ba(e.getMessage()))) { - Log.i(TAG, "sha256: " + e.getMessage() + " matches OK"); - } else { - Log.i(TAG, "MediaCrypto sha256: " + e.getMessage() + - "does not match test vector sha256: "); - for (int i = 0; i < sha256.length; i++) { - System.out.printf("%02x", sha256[i]); - } - } - } + refBuffer.put(clearBuf, 0, clearBuf.length); + refBuffer.put(tv.mClearBuf, 0, tv.mClearBuf.length); + sampleSize += clearSize + tv.mEncryptedBuf.length; } codec.stop(); @@ -369,7 +407,7 @@ public class MediaDrmAPITest extends TabActivity { Log.i(TAG, "sha256: " + e.getMessage() + " matches OK"); } else { Log.i(TAG, "MediaCrypto sha256: " + e.getMessage() + - "does not match test vector sha256: "); + " does not match test vector sha256: "); for (int i = 0; i < sha256.length; i++) { System.out.printf("%02x", sha256[i]); }