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
This commit is contained in:
Jeff Tinker
2013-04-18 17:11:02 -07:00
parent fa1f5fafe1
commit f2c3db81d3
2 changed files with 71 additions and 34 deletions

View File

@@ -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<TestVector> vectors = TestVectors.GetTestVectors();
ListIterator<TestVector> 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]);
}