Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.media.cts;
     18 
     19 import android.media.MediaCas;
     20 import android.media.MediaCas.PluginDescriptor;
     21 import android.media.MediaCas.Session;
     22 import android.media.MediaCasException;
     23 import android.media.MediaCasException.UnsupportedCasException;
     24 import android.media.MediaCasStateException;
     25 import android.media.MediaCodec;
     26 import android.media.MediaDescrambler;
     27 import android.os.Handler;
     28 import android.os.HandlerThread;
     29 import android.os.Looper;
     30 import android.platform.test.annotations.RequiresDevice;
     31 import android.support.test.filters.SmallTest;
     32 import android.test.AndroidTestCase;
     33 import android.util.Log;
     34 
     35 import java.lang.ArrayIndexOutOfBoundsException;
     36 import java.nio.ByteBuffer;
     37 import java.util.Arrays;
     38 import java.util.concurrent.CountDownLatch;
     39 import java.util.concurrent.TimeUnit;
     40 import java.util.regex.Matcher;
     41 import java.util.regex.Pattern;
     42 
     43 @SmallTest
     44 @RequiresDevice
     45 public class MediaCasTest extends AndroidTestCase {
     46     private static final String TAG = "MediaCasTest";
     47 
     48     // CA System Ids used for testing
     49     private static final int sInvalidSystemId = 0;
     50     private static final int sClearKeySystemId = 0xF6D8;
     51 
     52     // ClearKey CAS/Descrambler test vectors
     53     private static final String sProvisionStr =
     54             "{                                                   " +
     55             "  \"id\": 21140844,                                 " +
     56             "  \"name\": \"Test Title\",                         " +
     57             "  \"lowercase_organization_name\": \"Android\",     " +
     58             "  \"asset_key\": {                                  " +
     59             "  \"encryption_key\": \"nezAr3CHFrmBR9R8Tedotw==\"  " +
     60             "  },                                                " +
     61             "  \"cas_type\": 1,                                  " +
     62             "  \"track_types\": [ ]                              " +
     63             "}                                                   " ;
     64 
     65     private static final String sEcmBufferStr =
     66             "00 00 01 f0 00 50 00 01  00 00 00 01 00 46 00 00" +
     67             "00 02 00 00 00 00 00 01  00 00 27 10 02 00 01 77" +
     68             "01 42 95 6c 0e e3 91 bc  fd 05 b1 60 4f 17 82 a4" +
     69             "86 9b 23 56 00 01 00 00  00 01 00 00 27 10 02 00" +
     70             "01 77 01 42 95 6c d7 43  62 f8 1c 62 19 05 c7 3a" +
     71             "42 cd fd d9 13 48                               " ;
     72 
     73     private static final String sInputBufferStr =
     74             "00 00 00 01 09 f0 00 00  00 01 67 42 c0 1e db 01" +
     75             "40 16 ec 04 40 00 00 03  00 40 00 00 0f 03 c5 8b" +
     76             "b8 00 00 00 01 68 ca 8c  b2 00 00 01 06 05 ff ff" +
     77             "70 dc 45 e9 bd e6 d9 48  b7 96 2c d8 20 d9 23 ee" +
     78             "ef 78 32 36 34 20 2d 20  63 6f 72 65 20 31 34 32" +
     79             "20 2d 20 48 2e 32 36 34  2f 4d 50 45 47 2d 34 20" +
     80             "41 56 43 20 63 6f 64 65  63 20 2d 20 43 6f 70 79" +
     81             "6c 65 66 74 20 32 30 30  33 2d 32 30 31 34 20 2d" +
     82             "20 68 74 74 70 3a 2f 2f  77 77 77 2e 76 69 64 65" +
     83             "6f 6c 61 6e 2e 6f 72 67  2f 78 32 36 34 2e 68 74" +
     84             "6d 6c 6e 45 21 82 38 f0  9d 7d 96 e6 94 ae e2 87" +
     85             "8f 04 49 e5 f6 8c 8b 9a  10 18 ba 94 e9 22 31 04" +
     86             "7e 60 5b c4 24 00 90 62  0d dc 85 74 75 78 d0 14" +
     87             "08 cb 02 1d 7d 9d 34 e8  81 b9 f7 09 28 79 29 8d" +
     88             "e3 14 ed 5f ca af f4 1c  49 15 e1 80 29 61 76 80" +
     89             "43 f8 58 53 40 d7 31 6d  61 81 41 e9 77 9f 9c e1" +
     90             "6d f2 ee d9 c8 67 d2 5f  48 73 e3 5c cd a7 45 58" +
     91             "bb dd 28 1d 68 fc b4 c6  f6 92 f6 30 03 aa e4 32" +
     92             "f6 34 51 4b 0f 8c f9 ac  98 22 fb 49 c8 bf ca 8c" +
     93             "80 86 5d d7 a4 52 b1 d9  a6 04 4e b3 2d 1f b8 35" +
     94             "cc 45 6d 9c 20 a7 a4 34  59 72 e3 ae ba 49 de d1" +
     95             "aa ee 3d 77 fc 5d c6 1f  9d ac c2 15 66 b8 e1 54" +
     96             "4e 74 93 db 9a 24 15 6e  20 a3 67 3e 5a 24 41 5e" +
     97             "b0 e6 35 87 1b c8 7a f9  77 65 e0 01 f2 4c e4 2b" +
     98             "a9 64 96 96 0b 46 ca ea  79 0e 78 a3 5f 43 fc 47" +
     99             "6a 12 fa c4 33 0e 88 1c  19 3a 00 c3 4e b5 d8 fa" +
    100             "8e f1 bc 3d b2 7e 50 8d  67 c3 6b ed e2 ea a6 1f" +
    101             "25 24 7c 94 74 50 49 e3  c6 58 2e fd 28 b4 c6 73" +
    102             "b1 53 74 27 94 5c df 69  b7 a1 d7 f5 d3 8a 2c 2d" +
    103             "b4 5e 8a 16 14 54 64 6e  00 6b 11 59 8a 63 38 80" +
    104             "76 c3 d5 59 f7 3f d2 fa  a5 ca 82 ff 4a 62 f0 e3" +
    105             "42 f9 3b 38 27 8a 89 aa  50 55 4b 29 f1 46 7c 75" +
    106             "ef 65 af 9b 0d 6d da 25  94 14 c1 1b f0 c5 4c 24" +
    107             "0e 65                                           " ;
    108 
    109     private static final String sExpectedOutputBufferStr =
    110             "00 00 00 01 09 f0 00 00  00 01 67 42 c0 1e db 01" +
    111             "40 16 ec 04 40 00 00 03  00 40 00 00 0f 03 c5 8b" +
    112             "b8 00 00 00 01 68 ca 8c  b2 00 00 01 06 05 ff ff" +
    113             "70 dc 45 e9 bd e6 d9 48  b7 96 2c d8 20 d9 23 ee" +
    114             "ef 78 32 36 34 20 2d 20  63 6f 72 65 20 31 34 32" +
    115             "20 2d 20 48 2e 32 36 34  2f 4d 50 45 47 2d 34 20" +
    116             "41 56 43 20 63 6f 64 65  63 20 2d 20 43 6f 70 79" +
    117             "6c 65 66 74 20 32 30 30  33 2d 32 30 31 34 20 2d" +
    118             "20 68 74 74 70 3a 2f 2f  77 77 77 2e 76 69 64 65" +
    119             "6f 6c 61 6e 2e 6f 72 67  2f 78 32 36 34 2e 68 74" +
    120             "6d 6c 20 2d 20 6f 70 74  69 6f 6e 73 3a 20 63 61" +
    121             "62 61 63 3d 30 20 72 65  66 3d 32 20 64 65 62 6c" +
    122             "6f 63 6b 3d 31 3a 30 3a  30 20 61 6e 61 6c 79 73" +
    123             "65 3d 30 78 31 3a 30 78  31 31 31 20 6d 65 3d 68" +
    124             "65 78 20 73 75 62 6d 65  3d 37 20 70 73 79 3d 31" +
    125             "20 70 73 79 5f 72 64 3d  31 2e 30 30 3a 30 2e 30" +
    126             "30 20 6d 69 78 65 64 5f  72 65 66 3d 31 20 6d 65" +
    127             "5f 72 61 6e 67 65 3d 31  36 20 63 68 72 6f 6d 61" +
    128             "5f 6d 65 3d 31 20 74 72  65 6c 6c 69 73 3d 31 20" +
    129             "38 78 38 64 63 74 3d 30  20 63 71 6d 3d 30 20 64" +
    130             "65 61 64 7a 6f 6e 65 3d  32 31 2c 31 31 20 66 61" +
    131             "73 74 5f 70 73 6b 69 70  3d 31 20 63 68 72 6f 6d" +
    132             "61 5f 71 70 5f 6f 66 66  73 65 74 3d 2d 32 20 74" +
    133             "68 72 65 61 64 73 3d 36  30 20 6c 6f 6f 6b 61 68" +
    134             "65 61 64 5f 74 68 72 65  61 64 73 3d 35 20 73 6c" +
    135             "69 63 65 64 5f 74 68 72  65 61 64 73 3d 30 20 6e" +
    136             "72 3d 30 20 64 65 63 69  6d 61 74 65 3d 31 20 69" +
    137             "6e 74 65 72 6c 61 63 65  64 3d 30 20 62 6c 75 72" +
    138             "61 79 5f 63 6f 6d 70 61  74 3d 30 20 63 6f 6e 73" +
    139             "74 72 61 69 6e 65 64 5f  69 6e 74 72 61 3d 30 20" +
    140             "62 66 72 61 6d 65 73 3d  30 20 77 65 69 67 68 74" +
    141             "70 3d 30 20 6b 65 79 69  6e 74 3d 32 35 30 20 6b" +
    142             "65 79 69 6e 74 5f 6d 69  6e 3d 32 35 20 73 63 65" +
    143             "6e 65                                           " ;
    144 
    145     /**
    146      * Test that all enumerated CA systems can be instantiated.
    147      *
    148      * Due to the vendor-proprietary nature of CAS, we cannot verify all operations
    149      * of an arbitrary plugin. We can only verify that isSystemIdSupported() is
    150      * consistent with the enumeration results, and all enumerated CA system ids can
    151      * be instantiated.
    152      */
    153     public void testEnumeratePlugins() throws Exception {
    154         PluginDescriptor[] descriptors = MediaCas.enumeratePlugins();
    155         for (int i = 0; i < descriptors.length; i++) {
    156             Log.d(TAG, "desciptor[" + i + "]: id=" + descriptors[i].getSystemId()
    157                     + ", name=" + descriptors[i].getName());
    158             MediaCas mediaCas = null;
    159             MediaDescrambler descrambler = null;
    160             byte[] sessionId = null, streamSessionId = null;
    161             try {
    162                 final int CA_system_id = descriptors[i].getSystemId();
    163                 if (!MediaCas.isSystemIdSupported(CA_system_id)) {
    164                     fail("Enumerated " + descriptors[i] + " but is not supported.");
    165                 }
    166                 mediaCas = new MediaCas(CA_system_id);
    167                 if (mediaCas == null) {
    168                     fail("Enumerated " + descriptors[i] + " but cannot instantiate MediaCas.");
    169                 }
    170                 descrambler = new MediaDescrambler(CA_system_id);
    171                 if (descrambler == null) {
    172                     fail("Enumerated " + descriptors[i] + " but cannot instantiate MediaDescrambler.");
    173                 }
    174 
    175                 // Should always accept a listener (even if the plugin doesn't use it)
    176                 mediaCas.setEventListener(new MediaCas.EventListener() {
    177                     @Override
    178                     public void onEvent(MediaCas MediaCas, int event, int arg, byte[] data) {
    179                         Log.d(TAG, "Received MediaCas event: "
    180                                 + "event=" + event + ", arg=" + arg + ", data=" + data);
    181                     }
    182                 }, null);
    183             } finally {
    184                 if (mediaCas != null) {
    185                     mediaCas.close();
    186                 }
    187                 if (descrambler != null) {
    188                     descrambler.close();
    189                 }
    190             }
    191         }
    192     }
    193 
    194     public void testInvalidSystemIdFails() throws Exception {
    195         assertFalse("Invalid id " + sInvalidSystemId + " should not be supported",
    196                 MediaCas.isSystemIdSupported(sInvalidSystemId));
    197 
    198         MediaCas unsupportedCAS = null;
    199         MediaDescrambler unsupportedDescrambler = null;
    200 
    201         try {
    202             try {
    203                 unsupportedCAS = new MediaCas(sInvalidSystemId);
    204                 fail("Shouldn't be able to create MediaCas with invalid id " + sInvalidSystemId);
    205             } catch (UnsupportedCasException e) {
    206                 // expected
    207             }
    208 
    209             try {
    210                 unsupportedDescrambler = new MediaDescrambler(sInvalidSystemId);
    211                 fail("Shouldn't be able to create MediaDescrambler with invalid id " + sInvalidSystemId);
    212             } catch (UnsupportedCasException e) {
    213                 // expected
    214             }
    215         } finally {
    216             if (unsupportedCAS != null) {
    217                 unsupportedCAS.close();
    218             }
    219             if (unsupportedDescrambler != null) {
    220                 unsupportedDescrambler.close();
    221             }
    222         }
    223     }
    224 
    225     public void testClearKeyPluginInstalled() throws Exception {
    226         PluginDescriptor[] descriptors = MediaCas.enumeratePlugins();
    227         for (int i = 0; i < descriptors.length; i++) {
    228             if (descriptors[i].getSystemId() == sClearKeySystemId) {
    229                 return;
    230             }
    231         }
    232         fail("ClearKey plugin " + String.format("0x%d", sClearKeySystemId) + " is not found");
    233     }
    234 
    235     /**
    236      * Test that valid call sequences succeed.
    237      */
    238     public void testClearKeyApis() throws Exception {
    239         MediaCas mediaCas = null;
    240         MediaDescrambler descrambler = null;
    241 
    242         try {
    243             mediaCas = new MediaCas(sClearKeySystemId);
    244             descrambler = new MediaDescrambler(sClearKeySystemId);
    245 
    246             mediaCas.provision(sProvisionStr);
    247 
    248             byte[] pvtData = new byte[256];
    249             mediaCas.setPrivateData(pvtData);
    250 
    251             Session session = mediaCas.openSession();
    252             if (session == null) {
    253                 fail("Can't open session for program");
    254             }
    255 
    256             session.setPrivateData(pvtData);
    257 
    258             Session streamSession = mediaCas.openSession();
    259             if (streamSession == null) {
    260                 fail("Can't open session for stream");
    261             }
    262             streamSession.setPrivateData(pvtData);
    263 
    264             descrambler.setMediaCasSession(session);
    265 
    266             descrambler.setMediaCasSession(streamSession);
    267 
    268             mediaCas.refreshEntitlements(3, null);
    269 
    270             byte[] refreshBytes = new byte[4];
    271             refreshBytes[0] = 0;
    272             refreshBytes[1] = 1;
    273             refreshBytes[2] = 2;
    274             refreshBytes[3] = 3;
    275 
    276             mediaCas.refreshEntitlements(10, refreshBytes);
    277 
    278             final HandlerThread thread = new HandlerThread("EventListenerHandlerThread");
    279             thread.start();
    280             Handler handler = new Handler(thread.getLooper());
    281             testEventEcho(mediaCas, 1, 2, null /* data */, handler);
    282             thread.interrupt();
    283 
    284             String eventDataString = "event data string";
    285             byte[] eventData = eventDataString.getBytes();
    286             testEventEcho(mediaCas, 3, 4, eventData, null /* handler */);
    287 
    288             String emm = "clear key emm";
    289             byte[] emmData = emm.getBytes();
    290             mediaCas.processEmm(emmData);
    291 
    292             byte[] ecmData = loadByteArrayFromString(sEcmBufferStr);
    293             session.processEcm(ecmData);
    294             streamSession.processEcm(ecmData);
    295 
    296             ByteBuffer outputBuf = descrambleTestInputBuffer(descrambler);
    297             ByteBuffer expectedOutputBuf = ByteBuffer.wrap(
    298                     loadByteArrayFromString(sExpectedOutputBufferStr));
    299             assertTrue("Incorrect decryption result",
    300                     expectedOutputBuf.compareTo(outputBuf) == 0);
    301 
    302             session.close();
    303             streamSession.close();
    304         } finally {
    305             if (mediaCas != null) {
    306                 mediaCas.close();
    307             }
    308             if (descrambler != null) {
    309                 descrambler.close();
    310             }
    311         }
    312     }
    313 
    314     /**
    315      * Test that all sessions are closed after a MediaCas object is released.
    316      */
    317     public void testClearKeySessionClosedAfterRelease() throws Exception {
    318         MediaCas mediaCas = null;
    319         MediaDescrambler descrambler = null;
    320 
    321         try {
    322             mediaCas = new MediaCas(sClearKeySystemId);
    323             descrambler = new MediaDescrambler(sClearKeySystemId);
    324             mediaCas.provision(sProvisionStr);
    325 
    326             Session session = mediaCas.openSession();
    327             if (session == null) {
    328                 fail("Can't open session for program");
    329             }
    330 
    331             Session streamSession = mediaCas.openSession();
    332             if (streamSession == null) {
    333                 fail("Can't open session for stream");
    334             }
    335 
    336             mediaCas.close();
    337             mediaCas = null;
    338 
    339             try {
    340                 descrambler.setMediaCasSession(session);
    341                 fail("Program session not closed after MediaCas is released");
    342             } catch (MediaCasStateException e) {
    343                 Log.d(TAG, "setMediaCasSession throws "
    344                         + e.getDiagnosticInfo() + " (as expected)");
    345             }
    346             try {
    347                 descrambler.setMediaCasSession(streamSession);
    348                 fail("Stream session not closed after MediaCas is released");
    349             } catch (MediaCasStateException e) {
    350                 Log.d(TAG, "setMediaCasSession throws "
    351                         + e.getDiagnosticInfo() + " (as expected)");
    352             }
    353         } finally {
    354             if (mediaCas != null) {
    355                 mediaCas.close();
    356             }
    357             if (descrambler != null) {
    358                 descrambler.close();
    359             }
    360         }
    361     }
    362 
    363     /**
    364      * Test that invalid call sequences fail with expected exceptions.
    365      */
    366     public void testClearKeyExceptions() throws Exception {
    367         MediaCas mediaCas = null;
    368         MediaDescrambler descrambler = null;
    369 
    370         try {
    371             mediaCas = new MediaCas(sClearKeySystemId);
    372             descrambler = new MediaDescrambler(sClearKeySystemId);
    373 
    374             /*
    375              * Test MediaCas exceptions
    376              */
    377 
    378             // provision should fail with an invalid asset string
    379             try {
    380                 mediaCas.provision("invalid asset string");
    381                 fail("provision shouldn't succeed with invalid asset");
    382             } catch (MediaCasStateException e) {
    383                 Log.d(TAG, "provision throws " + e.getDiagnosticInfo() + " (as expected)");
    384             }
    385 
    386             // processEmm should reject invalid offset and length
    387             String emm = "clear key emm";
    388             byte[] emmData = emm.getBytes();
    389             try {
    390                 mediaCas.processEmm(emmData, 8, 40);
    391             } catch (ArrayIndexOutOfBoundsException e) {
    392                 Log.d(TAG, "processEmm throws ArrayIndexOutOfBoundsException (as expected)");
    393             }
    394 
    395             // open a session, then close it so that it should become invalid
    396             Session invalidSession = mediaCas.openSession();
    397             if (invalidSession == null) {
    398                 fail("Can't open session for program");
    399             }
    400             invalidSession.close();
    401 
    402             byte[] ecmData = loadByteArrayFromString(sEcmBufferStr);
    403 
    404             // processEcm should fail with an invalid session id
    405             try {
    406                 invalidSession.processEcm(ecmData);
    407                 fail("processEcm shouldn't succeed with invalid session id");
    408             } catch (MediaCasStateException e) {
    409                 Log.d(TAG, "processEcm throws " + e.getDiagnosticInfo() + " (as expected)");
    410             }
    411 
    412             Session session = mediaCas.openSession();
    413             if (session == null) {
    414                 fail("Can't open session for program");
    415             }
    416 
    417             // processEcm should fail without provisioning
    418             try {
    419                 session.processEcm(ecmData);
    420                 fail("processEcm shouldn't succeed without provisioning");
    421             } catch (MediaCasException.NotProvisionedException e) {
    422                 Log.d(TAG, "processEcm throws NotProvisionedException (as expected)");
    423             }
    424 
    425             // Now provision it, and expect failures other than NotProvisionedException
    426             mediaCas.provision(sProvisionStr);
    427 
    428             // processEcm should fail with ecm buffer that's too short
    429             try {
    430                 session.processEcm(ecmData, 0, 8);
    431                 fail("processEcm shouldn't succeed with truncated ecm");
    432             } catch (IllegalArgumentException e) {
    433                 Log.d(TAG, "processEcm throws " + e.toString() + " (as expected)");
    434             }
    435 
    436             // processEcm should fail with ecm with bad descriptor count
    437             try {
    438                 ecmData[17] = 3; // change the descriptor count field to 3 (invalid)
    439                 session.processEcm(ecmData);
    440                 fail("processEcm shouldn't succeed with altered descriptor count");
    441             } catch (MediaCasStateException e) {
    442                 Log.d(TAG, "processEcm throws " + e.getDiagnosticInfo() + " (as expected)");
    443             }
    444 
    445             /*
    446              * Test MediaDescrambler exceptions
    447              */
    448 
    449             // setMediaCasSession should fail with an invalid session id
    450             try {
    451                 descrambler.setMediaCasSession(invalidSession);
    452                 fail("setMediaCasSession shouldn't succeed with invalid session id");
    453             } catch (MediaCasStateException e) {
    454                 Log.d(TAG, "setMediaCasSession throws "
    455                         + e.getDiagnosticInfo() + " (as expected)");
    456             }
    457 
    458             // descramble should fail without a valid session
    459             try {
    460                 ByteBuffer outputBuf = descrambleTestInputBuffer(descrambler);
    461                 fail("descramble should fail without a valid session");
    462             } catch (MediaCasStateException e) {
    463                 Log.d(TAG, "descramble throws " + e.getDiagnosticInfo() + " (as expected)");
    464             }
    465 
    466             // Now set a valid session, should still fail because no valid ecm is processed
    467             descrambler.setMediaCasSession(session);
    468             try {
    469                 ByteBuffer outputBuf = descrambleTestInputBuffer(descrambler);
    470                 fail("descramble should fail without valid ecm");
    471             } catch (MediaCasStateException e) {
    472                 Log.d(TAG, "descramble throws " + e.getDiagnosticInfo() + " (as expected)");
    473             }
    474         } finally {
    475             if (mediaCas != null) {
    476                 mediaCas.close();
    477             }
    478             if (descrambler != null) {
    479                 descrambler.close();
    480             }
    481         }
    482     }
    483 
    484     private class TestEventListener implements MediaCas.EventListener {
    485         private final CountDownLatch mLatch = new CountDownLatch(1);
    486         private final MediaCas mMediaCas;
    487         private final int mEvent;
    488         private final int mArg;
    489         private final byte[] mData;
    490         private boolean mIsIdential;
    491 
    492         TestEventListener(MediaCas mediaCas, int event, int arg, byte[] data) {
    493             mMediaCas = mediaCas;
    494             mEvent = event;
    495             mArg = arg;
    496             mData = data;
    497         }
    498 
    499         boolean waitForResult() {
    500             try {
    501                 if (!mLatch.await(1, TimeUnit.SECONDS)) {
    502                     return false;
    503                 }
    504                 return mIsIdential;
    505             } catch (InterruptedException e) {}
    506             return false;
    507         }
    508 
    509         @Override
    510         public void onEvent(MediaCas mediaCas, int event, int arg, byte[] data) {
    511             Log.d(TAG, "Received MediaCas event: event=" + event
    512                     + ", arg=" + arg + ", data=" + data);
    513             if (mediaCas == mMediaCas && event == mEvent
    514                     && arg == mArg && Arrays.equals(data, mData)) {
    515                 mIsIdential = true;
    516             }
    517             mLatch.countDown();
    518         }
    519     }
    520 
    521     // helper to send an event and wait for echo
    522     private void testEventEcho(MediaCas mediaCas, int event,
    523             int arg, byte[] data, Handler handler) throws Exception {
    524         TestEventListener listener = new TestEventListener(mediaCas, event, arg, data);
    525         mediaCas.setEventListener(listener, handler);
    526         mediaCas.sendEvent(event, arg, data);
    527         assertTrue("Didn't receive event callback for " + event, listener.waitForResult());
    528     }
    529 
    530     // helper to descramble from the sample input (sInputBufferStr) and get output buffer
    531     private ByteBuffer descrambleTestInputBuffer(
    532             MediaDescrambler descrambler) throws Exception {
    533         MediaCodec.CryptoInfo cryptoInfo = new MediaCodec.CryptoInfo();
    534         int[] numBytesOfClearData     = new int[] { 162,   0,   0 };
    535         int[] numBytesOfEncryptedData = new int[] {   0, 184, 184 };
    536         byte[] key = new byte[16];
    537         key[0] = 2; // scrambling mode = even key
    538         byte[] iv = new byte[16]; // not used
    539         cryptoInfo.set(3, numBytesOfClearData, numBytesOfEncryptedData,
    540                 key, iv, MediaCodec.CRYPTO_MODE_AES_CBC);
    541         ByteBuffer inputBuf = ByteBuffer.wrap(
    542                 loadByteArrayFromString(sInputBufferStr));
    543         ByteBuffer outputBuf = ByteBuffer.allocate(inputBuf.capacity());
    544         descrambler.descramble(inputBuf, outputBuf, cryptoInfo);
    545 
    546         return outputBuf;
    547     }
    548 
    549     // helper to load byte[] from a String
    550     private byte[] loadByteArrayFromString(final String str) {
    551         Pattern pattern = Pattern.compile("[0-9a-fA-F]{2}");
    552         Matcher matcher = pattern.matcher(str);
    553         // allocate a large enough byte array first
    554         byte[] tempArray = new byte[str.length() / 2];
    555         int i = 0;
    556         while (matcher.find()) {
    557           tempArray[i++] = (byte)Integer.parseInt(matcher.group(), 16);
    558         }
    559         return Arrays.copyOfRange(tempArray, 0, i);
    560     }
    561 }
    562