• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.virtualdevice.cts.applaunch;
18 
19 
20 import static com.google.common.truth.Truth.assertThat;
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import android.app.Activity;
24 import android.companion.virtual.VirtualDeviceManager.VirtualDevice;
25 import android.hardware.display.DisplayManager;
26 import android.hardware.display.VirtualDisplay;
27 import android.media.MediaDrm;
28 import android.media.NotProvisionedException;
29 import android.media.ResourceBusyException;
30 import android.media.UnsupportedSchemeException;
31 import android.platform.test.annotations.AppModeFull;
32 import android.util.Log;
33 import android.virtualdevice.cts.common.VirtualDeviceRule;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 import java.util.UUID;
43 
44 /**
45  * Tests for DRM APIs called from applications running on virtual devices.
46  */
47 @RunWith(AndroidJUnit4.class)
48 @AppModeFull(reason = "VirtualDeviceManager cannot be accessed by instant apps")
49 public class VirtualDeviceDrmTest {
50 
51     private static final String TAG = "MediaDrmTest";
52 
53     @Rule
54     public VirtualDeviceRule mRule = VirtualDeviceRule.createDefault();
55 
56     private DrmActivity mDrmActivity;
57 
58     @Before
setUp()59     public void setUp() throws Exception {
60         VirtualDevice virtualDevice = mRule.createManagedVirtualDevice();
61         VirtualDisplay virtualDisplay = mRule.createManagedVirtualDisplayWithFlags(virtualDevice,
62                 DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC
63                         | DisplayManager.VIRTUAL_DISPLAY_FLAG_TRUSTED
64                         | DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY);
65 
66         mDrmActivity = mRule.startActivityOnDisplaySync(virtualDisplay, DrmActivity.class);
67     }
68 
69     @Test
activityOnVirtualDevice_isCryptoSchemeSupported_testUnsupported()70     public void activityOnVirtualDevice_isCryptoSchemeSupported_testUnsupported() {
71         for (UUID scheme : MediaDrm.getSupportedCryptoSchemes()) {
72             int[] unsupportedLevels = {
73                     MediaDrm.SECURITY_LEVEL_HW_SECURE_ALL,
74                     MediaDrm.SECURITY_LEVEL_HW_SECURE_CRYPTO,
75                     MediaDrm.SECURITY_LEVEL_HW_SECURE_DECODE,
76                     MediaDrm.SECURITY_LEVEL_SW_SECURE_DECODE
77             };
78             for (int level : unsupportedLevels) {
79                 for (String mime : new String[]{"audio/mp4", "video/mp4"}) {
80                     assertWithMessage(String.format(
81                             "Should not be supported on virtual devices: Scheme: %s, Mime: %s, "
82                                     + "Securitylevel: %d",
83                             scheme, mime, level)).that(
84                             mDrmActivity.isCryptoSchemeSupported(scheme, mime, level)).isFalse();
85                 }
86             }
87         }
88     }
89 
90     @Test
activityOnVirtualDevice_openSession_verifyDowngradedSecurityLevel()91     public void activityOnVirtualDevice_openSession_verifyDowngradedSecurityLevel() {
92         for (UUID scheme : MediaDrm.getSupportedCryptoSchemes()) {
93             int[] securityLevels = {
94                     MediaDrm.SECURITY_LEVEL_SW_SECURE_CRYPTO,
95                     MediaDrm.SECURITY_LEVEL_SW_SECURE_DECODE,
96                     MediaDrm.SECURITY_LEVEL_HW_SECURE_CRYPTO,
97                     MediaDrm.SECURITY_LEVEL_HW_SECURE_DECODE,
98                     MediaDrm.SECURITY_LEVEL_HW_SECURE_ALL,
99                     MediaDrm.getMaxSecurityLevel()
100             };
101             for (int level : securityLevels) {
102                 assertThat(mDrmActivity.getSecurityLevel(scheme, level)).isAtMost(
103                         MediaDrm.SECURITY_LEVEL_SW_SECURE_CRYPTO);
104             }
105         }
106     }
107 
108     public static class DrmActivity extends Activity {
109 
110         private static final String TAG = "DrmActivity";
111 
isCryptoSchemeSupported(UUID uuid, String mimeType, int securityLevel)112         boolean isCryptoSchemeSupported(UUID uuid, String mimeType, int securityLevel) {
113             return MediaDrm.isCryptoSchemeSupported(uuid, mimeType, securityLevel);
114         }
115 
getSecurityLevel(UUID uuid, int securityLevel)116         int getSecurityLevel(UUID uuid, int securityLevel) {
117             try (MediaDrm drm = new MediaDrm(uuid)) {
118                 byte[] sessionId = drm.openSession(securityLevel);
119                 return drm.getSecurityLevel(sessionId);
120             } catch (UnsupportedSchemeException | NotProvisionedException
121                      | ResourceBusyException | IllegalArgumentException e) {
122                 Log.w(TAG, "Skipping scheme: " + uuid + " at security level: " + securityLevel);
123             }
124             return MediaDrm.SECURITY_LEVEL_UNKNOWN;
125         }
126     }
127 }
128