1 /*
2  * Copyright (C) 2023 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 com.android.cts.net.hostside.networkslicingtestapp;
18 
19 import static org.junit.Assert.assertThrows;
20 
21 import android.content.Context;
22 import android.net.ConnectivityManager;
23 import android.net.NetworkCapabilities;
24 import android.net.NetworkRequest;
25 import android.os.Build;
26 
27 import androidx.test.platform.app.InstrumentationRegistry;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import com.android.testutils.DevSdkIgnoreRule;
31 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
32 
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 @RunWith(AndroidJUnit4.class)
38 public class NetworkSelfDeclaredCapabilitiesTest {
39 
40     @Rule
41     public final DevSdkIgnoreRule mDevSdkIgnoreRule = new DevSdkIgnoreRule();
42 
43     @Test
44     @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
requestNetwork_withoutRequestCapabilities()45     public void requestNetwork_withoutRequestCapabilities() {
46         final ConnectivityManager cm =
47                 (ConnectivityManager)
48                         InstrumentationRegistry.getInstrumentation()
49                                 .getContext()
50                                 .getSystemService(Context.CONNECTIVITY_SERVICE);
51         final NetworkRequest request =
52                 new NetworkRequest.Builder().build();
53         final ConnectivityManager.NetworkCallback callback =
54                 new ConnectivityManager.NetworkCallback();
55         cm.requestNetwork(request, callback);
56         cm.unregisterNetworkCallback(callback);
57     }
58 
59     @Test
60     @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
requestNetwork_withSelfDeclaredCapabilities()61     public void requestNetwork_withSelfDeclaredCapabilities() {
62         final ConnectivityManager cm =
63                 (ConnectivityManager)
64                         InstrumentationRegistry.getInstrumentation()
65                                 .getContext()
66                                 .getSystemService(Context.CONNECTIVITY_SERVICE);
67         final NetworkRequest request =
68                 new NetworkRequest.Builder()
69                         .addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
70                         .addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
71                         .build();
72         final ConnectivityManager.NetworkCallback callback =
73                 new ConnectivityManager.NetworkCallback();
74         cm.requestNetwork(request, callback);
75         cm.unregisterNetworkCallback(callback);
76     }
77 
78     @Test
79     @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
requestNetwork_lackingRequiredSelfDeclaredCapabilities()80     public void requestNetwork_lackingRequiredSelfDeclaredCapabilities() {
81         final ConnectivityManager cm =
82                 (ConnectivityManager)
83                         InstrumentationRegistry.getInstrumentation()
84                                 .getContext()
85                                 .getSystemService(Context.CONNECTIVITY_SERVICE);
86         final NetworkRequest request =
87                 new NetworkRequest.Builder()
88                         .addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
89                         .addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
90                         .build();
91         final ConnectivityManager.NetworkCallback callback =
92                 new ConnectivityManager.NetworkCallback();
93         assertThrows(
94                 SecurityException.class,
95                 () -> cm.requestNetwork(request, callback));
96     }
97 
98 }
99