• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.permission.cts;
18 
19 import static org.junit.Assert.assertThrows;
20 import static org.junit.Assume.assumeNotNull;
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.net.EthernetManager;
26 import android.net.EthernetNetworkUpdateRequest;
27 import android.net.IpConfiguration;
28 import android.net.NetworkCapabilities;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 import java.util.concurrent.Executor;
38 import java.util.function.BiConsumer;
39 
40 /**
41  * Test protected android.net.EthernetManager methods cannot be called without permissions.
42  */
43 @RunWith(AndroidJUnit4.class)
44 public class EthernetManagerPermissionTest {
45     private static final String TEST_IFACE = "test123abc789";
46     private EthernetManager mEthernetManager;
47     private Context mContext;
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         mContext = InstrumentationRegistry.getTargetContext();
52         mEthernetManager = mContext.getSystemService(EthernetManager.class);
53         // mEthernetManager may be null depending on the device's configuration.
54         assumeNotNull(mEthernetManager);
55     }
56 
buildUpdateRequest()57     private EthernetNetworkUpdateRequest buildUpdateRequest() {
58         return new EthernetNetworkUpdateRequest.Builder()
59                 .setIpConfiguration(new IpConfiguration.Builder().build())
60                 .setNetworkCapabilities(new NetworkCapabilities.Builder().build())
61                 .build();
62     }
63 
buildUpdateRequestWithoutCapabilities()64     private EthernetNetworkUpdateRequest buildUpdateRequestWithoutCapabilities() {
65         return new EthernetNetworkUpdateRequest.Builder()
66                 .setIpConfiguration(new IpConfiguration.Builder().build())
67                 .build();
68     }
69 
70     /**
71      * Verify that calling {@link EthernetManager#updateConfiguration(String,
72      * EthernetNetworkUpdateRequest, Executor, BiConsumer)} requires permissions.
73      * <p>Tests Permission:
74      *   {@link android.Manifest.permission#MANAGE_ETHERNET_NETWORKS}.
75      */
76     @Test
testUpdateConfigurationRequiresPermissionManageEthernetNetworks()77     public void testUpdateConfigurationRequiresPermissionManageEthernetNetworks() {
78         assertThrows("Should not be able to call updateConfiguration without permission",
79                 SecurityException.class,
80                 () -> mEthernetManager.updateConfiguration(TEST_IFACE,
81                         buildUpdateRequestWithoutCapabilities(), null, null));
82     }
83 
84     /**
85      * Verify that calling {@link EthernetManager#enableInterface}
86      * requires permissions.
87      * <p>Tests Permission:
88      *   {@link android.Manifest.permission#MANAGE_ETHERNET_NETWORKS}.
89      */
90     @Test
testEnableInterface()91     public void testEnableInterface() {
92         assumeTrue(mContext.getPackageManager().hasSystemFeature(
93                 PackageManager.FEATURE_AUTOMOTIVE));
94         assertThrows("Should not be able to call enableInterface without permission",
95                 SecurityException.class,
96                 () -> mEthernetManager.enableInterface(TEST_IFACE, null, null));
97     }
98 
99     /**
100      * Verify that calling {@link EthernetManager#disableInterface}
101      * requires permissions.
102      * <p>Tests Permission:
103      *   {@link android.Manifest.permission#MANAGE_ETHERNET_NETWORKS}.
104      */
105     @Test
testDisableInterface()106     public void testDisableInterface() {
107         assumeTrue(mContext.getPackageManager().hasSystemFeature(
108                 PackageManager.FEATURE_AUTOMOTIVE));
109         assertThrows("Should not be able to call disableInterface without permission",
110                 SecurityException.class,
111                 () -> mEthernetManager.disableInterface(TEST_IFACE, null, null));
112     }
113 }
114