• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.robolectric.util.TestUtil.resourceFile;
5 
6 import android.content.pm.PackageParser.Package;
7 import android.content.pm.PackageParser.Permission;
8 import android.content.pm.PermissionInfo;
9 import androidx.test.ext.junit.runners.AndroidJUnit4;
10 import java.util.List;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.robolectric.manifest.AndroidManifest;
15 
16 /** Unit test for {@link org.robolectric.shadows.LegacyManifestParser}. */
17 @RunWith(AndroidJUnit4.class)
18 public class LegacyManifestParserTest {
19 
20   private AndroidManifest androidManifest;
21 
22   @Before
setUp()23   public void setUp() {
24     androidManifest =
25         new AndroidManifest(
26             resourceFile("TestAndroidManifestWithProtectionLevels.xml"),
27             resourceFile("res"),
28             resourceFile("assets"));
29   }
30 
31   @Test
createPackage_signatureOrPrivileged_shouldParseCorrectFlags()32   public void createPackage_signatureOrPrivileged_shouldParseCorrectFlags() {
33     Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
34     int protectionLevel =
35         getPermissionInfo(parsedPackage.permissions, "signature_or_privileged_permission")
36             .protectionLevel;
37     assertThat(protectionLevel)
38         .isEqualTo(PermissionInfo.PROTECTION_SIGNATURE | PermissionInfo.PROTECTION_FLAG_PRIVILEGED);
39   }
40 
41   @Test
createPackage_protectionLevelNotDeclated_shouldParseToNormal()42   public void createPackage_protectionLevelNotDeclated_shouldParseToNormal() {
43     Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
44     int protectionLevel =
45         getPermissionInfo(parsedPackage.permissions, "permission_with_minimal_fields")
46             .protectionLevel;
47     assertThat(protectionLevel).isEqualTo(PermissionInfo.PROTECTION_NORMAL);
48   }
49 
50   @Test
createPackage_protectionLevelVendorOrOem_shouldParseCorrectFlags()51   public void createPackage_protectionLevelVendorOrOem_shouldParseCorrectFlags() {
52     Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
53     int protectionLevel =
54         getPermissionInfo(parsedPackage.permissions, "vendor_privileged_or_oem_permission")
55             .protectionLevel;
56     assertThat(protectionLevel)
57         .isEqualTo(
58             PermissionInfo.PROTECTION_FLAG_VENDOR_PRIVILEGED | PermissionInfo.PROTECTION_FLAG_OEM);
59   }
60 
61   @Test
createPackage_protectionLevelDangerous_shouldParseCorrectFlags()62   public void createPackage_protectionLevelDangerous_shouldParseCorrectFlags() {
63     Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
64     int protectionLevel =
65         getPermissionInfo(parsedPackage.permissions, "dangerous_permission").protectionLevel;
66     assertThat(protectionLevel).isEqualTo(PermissionInfo.PROTECTION_DANGEROUS);
67   }
68 
getPermissionInfo(List<Permission> permissions, String name)69   private PermissionInfo getPermissionInfo(List<Permission> permissions, String name) {
70     name = "org.robolectric." + name;
71     for (Permission permission : permissions) {
72       if (name.equals(permission.info.name)) {
73         return permission.info;
74       }
75     }
76     return null;
77   }
78 }
79