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