blob: a43511994fa283db40193c362d218d35473446b8 [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;
Chalard Jeanf4802fa2021-12-13 21:37:12 +090025import static org.junit.Assert.assertFalse;
lucaslinaad38542021-02-25 18:32:40 +080026import static org.junit.Assert.assertTrue;
27import static org.junit.Assert.fail;
28
Chalard Jeanf4802fa2021-12-13 21:37:12 +090029import android.annotation.NonNull;
30import android.annotation.Nullable;
lucaslinaad38542021-02-25 18:32:40 +080031import android.os.Build;
32import android.os.UserHandle;
Chalard Jeanf4802fa2021-12-13 21:37:12 +090033import android.util.ArraySet;
lucaslinaad38542021-02-25 18:32:40 +080034
35import androidx.test.filters.SmallTest;
36import androidx.test.runner.AndroidJUnit4;
37
38import com.android.testutils.DevSdkIgnoreRule;
39import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
40
41import org.junit.Rule;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
Chalard Jeanf4802fa2021-12-13 21:37:12 +090045import java.util.Set;
46
lucaslinaad38542021-02-25 18:32:40 +080047@RunWith(AndroidJUnit4.class)
48@SmallTest
49public class UidRangeTest {
50
51 /*
52 * UidRange is no longer passed to netd. UID ranges between the framework and netd are passed as
53 * UidRangeParcel objects.
54 */
55
56 @Rule
57 public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();
58
59 @Test
60 public void testSingleItemUidRangeAllowed() {
61 new UidRange(123, 123);
62 new UidRange(0, 0);
63 new UidRange(Integer.MAX_VALUE, Integer.MAX_VALUE);
64 }
65
66 @Test
67 public void testNegativeUidsDisallowed() {
68 try {
69 new UidRange(-2, 100);
70 fail("Exception not thrown for negative start UID");
71 } catch (IllegalArgumentException expected) {
72 }
73
74 try {
75 new UidRange(-200, -100);
76 fail("Exception not thrown for negative stop UID");
77 } catch (IllegalArgumentException expected) {
78 }
79 }
80
81 @Test
82 public void testStopLessThanStartDisallowed() {
83 final int x = 4195000;
84 try {
85 new UidRange(x, x - 1);
86 fail("Exception not thrown for negative-length UID range");
87 } catch (IllegalArgumentException expected) {
88 }
89 }
90
91 @Test
92 public void testGetStartAndEndUser() throws Exception {
93 final UidRange uidRangeOfPrimaryUser = new UidRange(
94 getUid(USER_SYSTEM, 10000), getUid(USER_SYSTEM, 10100));
95 final UidRange uidRangeOfSecondaryUser = new UidRange(
96 getUid(MIN_SECONDARY_USER_ID, 10000), getUid(MIN_SECONDARY_USER_ID, 10100));
97 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser());
98 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getEndUser());
99 assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getStartUser());
100 assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getEndUser());
101
102 final UidRange uidRangeForDifferentUsers = new UidRange(
103 getUid(USER_SYSTEM, 10000), getUid(MIN_SECONDARY_USER_ID, 10100));
104 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser());
105 assertEquals(MIN_SECONDARY_USER_ID, uidRangeOfSecondaryUser.getEndUser());
106 }
107
108 @Test @IgnoreUpTo(Build.VERSION_CODES.R)
109 public void testCreateForUser() throws Exception {
110 final UidRange uidRangeOfPrimaryUser = UidRange.createForUser(SYSTEM);
111 final UidRange uidRangeOfSecondaryUser = UidRange.createForUser(
112 UserHandle.of(USER_SYSTEM + 1));
113 assertTrue(uidRangeOfPrimaryUser.stop < uidRangeOfSecondaryUser.start);
114 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getStartUser());
115 assertEquals(USER_SYSTEM, uidRangeOfPrimaryUser.getEndUser());
116 assertEquals(USER_SYSTEM + 1, uidRangeOfSecondaryUser.getStartUser());
117 assertEquals(USER_SYSTEM + 1, uidRangeOfSecondaryUser.getEndUser());
118 }
Chalard Jeanf4802fa2021-12-13 21:37:12 +0900119
120 private static void assertSameUids(@NonNull final String msg, @Nullable final Set<UidRange> s1,
121 @Nullable final Set<UidRange> s2) {
122 assertTrue(msg + " : " + s1 + " unexpectedly different from " + s2,
123 UidRange.hasSameUids(s1, s2));
124 }
125
126 private static void assertDifferentUids(@NonNull final String msg,
127 @Nullable final Set<UidRange> s1, @Nullable final Set<UidRange> s2) {
128 assertFalse(msg + " : " + s1 + " unexpectedly equal to " + s2,
129 UidRange.hasSameUids(s1, s2));
130 }
131
132 // R doesn't have UidRange.hasSameUids, but since S has the module, it does have hasSameUids.
133 @Test @IgnoreUpTo(Build.VERSION_CODES.R)
134 public void testHasSameUids() {
135 final UidRange uids1 = new UidRange(1, 100);
136 final UidRange uids2 = new UidRange(3, 300);
137 final UidRange uids3 = new UidRange(1, 1000);
138 final UidRange uids4 = new UidRange(800, 1000);
139
140 assertSameUids("null <=> null", null, null);
141 final Set<UidRange> set1 = new ArraySet<>();
142 assertDifferentUids("empty <=> null", set1, null);
143 final Set<UidRange> set2 = new ArraySet<>();
144 set1.add(uids1);
145 assertDifferentUids("uids1 <=> null", set1, null);
146 assertDifferentUids("null <=> uids1", null, set1);
147 assertDifferentUids("uids1 <=> empty", set1, set2);
148 set2.add(uids1);
149 assertSameUids("uids1 <=> uids1", set1, set2);
150 set1.add(uids2);
151 assertDifferentUids("uids1,2 <=> uids1", set1, set2);
152 set1.add(uids3);
153 assertDifferentUids("uids1,2,3 <=> uids1", set1, set2);
154 set2.add(uids3);
155 assertDifferentUids("uids1,2,3 <=> uids1,3", set1, set2);
156 set2.add(uids2);
157 assertSameUids("uids1,2,3 <=> uids1,2,3", set1, set2);
158 set1.remove(uids2);
159 assertDifferentUids("uids1,3 <=> uids1,2,3", set1, set2);
160 set1.add(uids4);
161 assertDifferentUids("uids1,3,4 <=> uids1,2,3", set1, set2);
162 set2.add(uids4);
163 assertDifferentUids("uids1,3,4 <=> uids1,2,3,4", set1, set2);
164 assertDifferentUids("uids1,3,4 <=> null", set1, null);
165 set2.remove(uids2);
166 assertSameUids("uids1,3,4 <=> uids1,3,4", set1, set2);
167 set2.remove(uids1);
168 assertDifferentUids("uids1,3,4 <=> uids3,4", set1, set2);
169 set2.remove(uids3);
170 assertDifferentUids("uids1,3,4 <=> uids4", set1, set2);
171 set2.remove(uids4);
172 assertDifferentUids("uids1,3,4 <=> empty", set1, set2);
173 assertDifferentUids("null <=> empty", null, set2);
174 assertSameUids("empty <=> empty", set2, new ArraySet<>());
175 }
lucaslinaad38542021-02-25 18:32:40 +0800176}