blob: 1b1c95431d6f79ca8844e0285b65a518603684ef [file] [log] [blame]
lucaslinaad38542021-02-25 18:32:40 +08001/*
2 * Copyright (C) 2016 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
17package android.net;
18
19import static android.os.UserHandle.MIN_SECONDARY_USER_ID;
20import static android.os.UserHandle.SYSTEM;
21import static android.os.UserHandle.USER_SYSTEM;
22import static android.os.UserHandle.getUid;
23
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertTrue;
26import static org.junit.Assert.fail;
27
28import android.os.Build;
29import android.os.UserHandle;
30
31import androidx.test.filters.SmallTest;
32import androidx.test.runner.AndroidJUnit4;
33
34import com.android.testutils.DevSdkIgnoreRule;
35import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
36
37import org.junit.Rule;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40
41@RunWith(AndroidJUnit4.class)
42@SmallTest
43public class UidRangeTest {
44
45 /*
46 * UidRange is no longer passed to netd. UID ranges between the framework and netd are passed as
47 * UidRangeParcel objects.
48 */
49
50 @Rule
51 public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();
52
53 @Test
54 public void testSingleItemUidRangeAllowed() {
55 new UidRange(123, 123);
56 new UidRange(0, 0);
57 new UidRange(Integer.MAX_VALUE, Integer.MAX_VALUE);
58 }
59
60 @Test
61 public void testNegativeUidsDisallowed() {
62 try {
63 new UidRange(-2, 100);
64 fail("Exception not thrown for negative start UID");
65 } catch (IllegalArgumentException expected) {
66 }
67
68 try {
69 new UidRange(-200, -100);
70 fail("Exception not thrown for negative stop UID");
71 } catch (IllegalArgumentException expected) {
72 }
73 }
74
75 @Test
76 public void testStopLessThanStartDisallowed() {
77 final int x = 4195000;
78 try {
79 new UidRange(x, x - 1);
80 fail("Exception not thrown for negative-length UID range");
81 } catch (IllegalArgumentException expected) {
82 }
83 }
84
85 @Test
86 public void testGetStartAndEndUser() throws Exception {
87 final UidRange uidRangeOfPrimaryUser = new UidRange(
88 getUid(USER_SYSTEM, 10000), getUid(USER_SYSTEM, 10100));
89 final UidRange uidRangeOfSecondaryUser = new UidRange(
90 getUid(MIN_SECONDARY_USER_ID, 10000), getUid(MIN_SECONDARY_USER_ID, 10100));
91 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser());
92 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getEndUser());
93 assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getStartUser());
94 assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getEndUser());
95
96 final UidRange uidRangeForDifferentUsers = new UidRange(
97 getUid(USER_SYSTEM, 10000), getUid(MIN_SECONDARY_USER_ID, 10100));
98 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser());
99 assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getEndUser());
100 }
101
102 @Test @IgnoreUpTo(Build.VERSION_CODES.R)
103 public void testCreateForUser() throws Exception {
104 final UidRange uidRangeOfPrimaryUser = UidRange.createForUser(SYSTEM);
105 final UidRange uidRangeOfSecondaryUser = UidRange.createForUser(
106 UserHandle.of(USER_SYSTEM + 1));
107 assertTrue(uidRangeOfPrimaryUser.stop < uidRangeOfSecondaryUser.start);
108 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser());
109 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getEndUser());
110 assertEquals(USER_SYSTEM + 1, uidRangeOfSecondaryUser.getStartUser());
111 assertEquals(USER_SYSTEM + 1, uidRangeOfSecondaryUser.getEndUser());
112 }
113}