1 /*
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 
17 package android.net;
18 
19 import static android.os.UserHandle.MIN_SECONDARY_USER_ID;
20 import static android.os.UserHandle.SYSTEM;
21 import static android.os.UserHandle.USER_SYSTEM;
22 import static android.os.UserHandle.getUid;
23 
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 
28 import android.os.Build;
29 import android.os.UserHandle;
30 
31 import androidx.test.filters.SmallTest;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import com.android.testutils.DevSdkIgnoreRule;
35 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
36 
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 @RunWith(AndroidJUnit4.class)
42 @SmallTest
43 public 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
testSingleItemUidRangeAllowed()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
testNegativeUidsDisallowed()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
testStopLessThanStartDisallowed()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
testGetStartAndEndUser()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)
testCreateForUser()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 }
114