1 /*
2  * Copyright (C) 2022 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.server.connectivity;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertThrows;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.annotation.NonNull;
25 import android.annotation.Nullable;
26 import android.net.UidRange;
27 import android.os.Build;
28 import android.util.ArraySet;
29 
30 import com.android.testutils.DevSdkIgnoreRule;
31 import com.android.testutils.DevSdkIgnoreRunner;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Set;
39 
40 /**
41  * Tests for UidRangeUtils.
42  *
43  * Build, install and run with:
44  *  runtest frameworks-net -c com.android.server.connectivity.UidRangeUtilsTest
45  */
46 @RunWith(DevSdkIgnoreRunner.class)
47 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
48 public class UidRangeUtilsTest {
assertInSameRange(@onNull final String msg, @Nullable final UidRange r1, @Nullable final Set<UidRange> s2)49     private static void assertInSameRange(@NonNull final String msg,
50             @Nullable final UidRange r1,
51             @Nullable final Set<UidRange> s2) {
52         assertTrue(msg + " : " + s2 + " unexpectedly is not in range of " + r1,
53                 UidRangeUtils.isRangeSetInUidRange(r1, s2));
54     }
55 
assertNotInSameRange(@onNull final String msg, @Nullable final UidRange r1, @Nullable final Set<UidRange> s2)56     private static void assertNotInSameRange(@NonNull final String msg,
57             @Nullable final UidRange r1, @Nullable final Set<UidRange> s2) {
58         assertFalse(msg + " : " + s2 + " unexpectedly is in range of " + r1,
59                 UidRangeUtils.isRangeSetInUidRange(r1, s2));
60     }
61 
62     @Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
testRangeSetInUidRange()63     public void testRangeSetInUidRange() {
64         final UidRange uids1 = new UidRange(1, 100);
65         final UidRange uids2 = new UidRange(3, 300);
66         final UidRange uids3 = new UidRange(1, 1000);
67         final UidRange uids4 = new UidRange(1, 100);
68         final UidRange uids5 = new UidRange(2, 20);
69         final UidRange uids6 = new UidRange(3, 30);
70 
71         assertThrows(NullPointerException.class,
72                 () -> UidRangeUtils.isRangeSetInUidRange(null, null));
73         assertThrows(NullPointerException.class,
74                 () -> UidRangeUtils.isRangeSetInUidRange(uids1, null));
75 
76         final ArraySet<UidRange> set1 = new ArraySet<>();
77         final ArraySet<UidRange> set2 = new ArraySet<>();
78 
79         assertThrows(NullPointerException.class,
80                 () -> UidRangeUtils.isRangeSetInUidRange(null, set1));
81         assertInSameRange("uids1 <=> empty", uids1, set2);
82 
83         set2.add(uids1);
84         assertInSameRange("uids1 <=> uids1", uids1, set2);
85 
86         set2.clear();
87         set2.add(uids2);
88         assertNotInSameRange("uids1 <=> uids2", uids1, set2);
89         set2.clear();
90         set2.add(uids3);
91         assertNotInSameRange("uids1 <=> uids3", uids1, set2);
92         set2.clear();
93         set2.add(uids4);
94         assertInSameRange("uids1 <=> uids4", uids1, set2);
95 
96         set2.clear();
97         set2.add(uids5);
98         set2.add(uids6);
99         assertInSameRange("uids1 <=> uids5, 6", uids1, set2);
100 
101         set2.clear();
102         set2.add(uids2);
103         set2.add(uids6);
104         assertNotInSameRange("uids1 <=> uids2, 6", uids1, set2);
105     }
106 
107     @Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
testRemoveRangeSetFromUidRange()108     public void testRemoveRangeSetFromUidRange() {
109         final UidRange uids1 = new UidRange(1, 100);
110         final UidRange uids2 = new UidRange(3, 300);
111         final UidRange uids3 = new UidRange(1, 1000);
112         final UidRange uids4 = new UidRange(1, 100);
113         final UidRange uids5 = new UidRange(2, 20);
114         final UidRange uids6 = new UidRange(3, 30);
115         final UidRange uids7 = new UidRange(30, 39);
116 
117         final UidRange uids8 = new UidRange(1, 1);
118         final UidRange uids9 = new UidRange(21, 100);
119         final UidRange uids10 = new UidRange(1, 2);
120         final UidRange uids11 = new UidRange(31, 100);
121 
122         final UidRange uids12 = new UidRange(1, 1);
123         final UidRange uids13 = new UidRange(21, 29);
124         final UidRange uids14 = new UidRange(40, 100);
125 
126         final UidRange uids15 = new UidRange(3, 30);
127         final UidRange uids16 = new UidRange(31, 39);
128 
129         assertThrows(NullPointerException.class,
130                 () -> UidRangeUtils.removeRangeSetFromUidRange(null, null));
131         Set<UidRange> expected = new ArraySet<>();
132         expected.add(uids1);
133         assertThrows(NullPointerException.class,
134                 () -> UidRangeUtils.removeRangeSetFromUidRange(uids1, null));
135         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, new ArraySet<>()));
136 
137         expected.clear();
138         final ArraySet<UidRange> set2 = new ArraySet<>();
139         set2.add(uids1);
140         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
141         set2.clear();
142         set2.add(uids4);
143         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
144 
145         expected.add(uids10);
146         set2.clear();
147         set2.add(uids2);
148         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
149 
150         expected.clear();
151         set2.clear();
152         set2.add(uids3);
153         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
154 
155         set2.clear();
156         set2.add(uids3);
157         set2.add(uids6);
158         assertThrows(IllegalArgumentException.class,
159                 () -> UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
160 
161         expected.clear();
162         expected.add(uids8);
163         expected.add(uids9);
164         set2.clear();
165         set2.add(uids5);
166         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
167 
168         expected.clear();
169         expected.add(uids10);
170         expected.add(uids11);
171         set2.clear();
172         set2.add(uids6);
173         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
174 
175         expected.clear();
176         expected.add(uids12);
177         expected.add(uids13);
178         expected.add(uids14);
179         set2.clear();
180         set2.add(uids5);
181         set2.add(uids7);
182         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
183 
184         expected.clear();
185         expected.add(uids10);
186         expected.add(uids14);
187         set2.clear();
188         set2.add(uids15);
189         set2.add(uids16);
190         assertEquals(expected, UidRangeUtils.removeRangeSetFromUidRange(uids1, set2));
191     }
192 
assertRangeOverlaps(@onNull final String msg, @Nullable final Set<UidRange> s1, @Nullable final Set<UidRange> s2)193     private static void assertRangeOverlaps(@NonNull final String msg,
194             @Nullable final Set<UidRange> s1,
195             @Nullable final Set<UidRange> s2) {
196         assertTrue(msg + " : " + s2 + " unexpectedly does not overlap with " + s1,
197                 UidRangeUtils.doesRangeSetOverlap(s1, s2));
198     }
199 
assertRangeDoesNotOverlap(@onNull final String msg, @Nullable final Set<UidRange> s1, @Nullable final Set<UidRange> s2)200     private static void assertRangeDoesNotOverlap(@NonNull final String msg,
201             @Nullable final Set<UidRange> s1, @Nullable final Set<UidRange> s2) {
202         assertFalse(msg + " : " + s2 + " unexpectedly ovelaps with " + s1,
203                 UidRangeUtils.doesRangeSetOverlap(s1, s2));
204     }
205 
206     @Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
testRangeSetOverlap()207     public void testRangeSetOverlap() {
208         final UidRange uids1 = new UidRange(1, 100);
209         final UidRange uids2 = new UidRange(3, 300);
210         final UidRange uids3 = new UidRange(1, 1000);
211         final UidRange uids4 = new UidRange(1, 100);
212         final UidRange uids5 = new UidRange(2, 20);
213         final UidRange uids6 = new UidRange(3, 30);
214         final UidRange uids7 = new UidRange(0, 0);
215         final UidRange uids8 = new UidRange(1, 500);
216         final UidRange uids9 = new UidRange(101, 200);
217 
218         assertThrows(NullPointerException.class,
219                 () -> UidRangeUtils.doesRangeSetOverlap(null, null));
220 
221         final ArraySet<UidRange> set1 = new ArraySet<>();
222         final ArraySet<UidRange> set2 = new ArraySet<>();
223         assertThrows(NullPointerException.class,
224                 () -> UidRangeUtils.doesRangeSetOverlap(set1, null));
225         assertThrows(NullPointerException.class,
226                 () -> UidRangeUtils.doesRangeSetOverlap(null, set2));
227         assertRangeDoesNotOverlap("empty <=> null", set1, set2);
228 
229         set2.add(uids1);
230         set1.add(uids1);
231         assertRangeOverlaps("uids1 <=> uids1", set1, set2);
232 
233         set1.clear();
234         set1.add(uids1);
235         set2.clear();
236         set2.add(uids2);
237         assertRangeOverlaps("uids1 <=> uids2", set1, set2);
238 
239         set1.clear();
240         set1.add(uids1);
241         set2.clear();
242         set2.add(uids3);
243         assertRangeOverlaps("uids1 <=> uids3", set1, set2);
244 
245         set1.clear();
246         set1.add(uids1);
247         set2.clear();
248         set2.add(uids4);
249         assertRangeOverlaps("uids1 <=> uids4", set1, set2);
250 
251         set1.clear();
252         set1.add(uids1);
253         set2.clear();
254         set2.add(uids5);
255         set2.add(uids6);
256         assertRangeOverlaps("uids1 <=> uids5,6", set1, set2);
257 
258         set1.clear();
259         set1.add(uids1);
260         set2.clear();
261         set2.add(uids7);
262         assertRangeDoesNotOverlap("uids1 <=> uids7", set1, set2);
263 
264         set1.clear();
265         set1.add(uids1);
266         set2.clear();
267         set2.add(uids9);
268         assertRangeDoesNotOverlap("uids1 <=> uids9", set1, set2);
269 
270         set1.clear();
271         set1.add(uids1);
272         set2.clear();
273         set2.add(uids8);
274         assertRangeOverlaps("uids1 <=> uids8", set1, set2);
275 
276 
277         set1.clear();
278         set1.add(uids1);
279         set2.clear();
280         set2.add(uids8);
281         set2.add(uids7);
282         assertRangeOverlaps("uids1 <=> uids7, 8", set1, set2);
283     }
284 
285     @Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
testConvertListToUidRange()286     public void testConvertListToUidRange() {
287         final UidRange uids1 = new UidRange(1, 1);
288         final UidRange uids2 = new UidRange(1, 2);
289         final UidRange uids3 = new UidRange(100, 100);
290         final UidRange uids4 = new UidRange(10, 10);
291 
292         final UidRange uids5 = new UidRange(10, 14);
293         final UidRange uids6 = new UidRange(20, 24);
294 
295         final Set<UidRange> expected = new ArraySet<>();
296         final List<Integer> input = new ArrayList<Integer>();
297 
298         assertThrows(NullPointerException.class, () -> UidRangeUtils.convertListToUidRange(null));
299         assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
300 
301         input.add(1);
302         expected.add(uids1);
303         assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
304 
305         input.add(2);
306         expected.clear();
307         expected.add(uids2);
308         assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
309 
310         input.clear();
311         input.add(1);
312         input.add(100);
313         expected.clear();
314         expected.add(uids1);
315         expected.add(uids3);
316         assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
317 
318         input.clear();
319         input.add(100);
320         input.add(1);
321         expected.clear();
322         expected.add(uids1);
323         expected.add(uids3);
324         assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
325 
326         input.clear();
327         input.add(100);
328         input.add(1);
329         input.add(2);
330         input.add(1);
331         input.add(10);
332         expected.clear();
333         expected.add(uids2);
334         expected.add(uids4);
335         expected.add(uids3);
336         assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
337 
338         input.clear();
339         input.add(10);
340         input.add(11);
341         input.add(12);
342         input.add(13);
343         input.add(14);
344         input.add(20);
345         input.add(21);
346         input.add(22);
347         input.add(23);
348         input.add(24);
349         expected.clear();
350         expected.add(uids5);
351         expected.add(uids6);
352         assertEquals(expected, UidRangeUtils.convertListToUidRange(input));
353     }
354 
355     @Test @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
testConvertArrayToUidRange()356     public void testConvertArrayToUidRange() {
357         final UidRange uids1_1 = new UidRange(1, 1);
358         final UidRange uids1_2 = new UidRange(1, 2);
359         final UidRange uids100_100 = new UidRange(100, 100);
360         final UidRange uids10_10 = new UidRange(10, 10);
361 
362         final UidRange uids10_14 = new UidRange(10, 14);
363         final UidRange uids20_24 = new UidRange(20, 24);
364 
365         final Set<UidRange> expected = new ArraySet<>();
366         int[] input = new int[0];
367 
368         assertThrows(NullPointerException.class, () -> UidRangeUtils.convertArrayToUidRange(null));
369         assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
370 
371         input = new int[] {1};
372         expected.add(uids1_1);
373         assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
374 
375         input = new int[]{1, 2};
376         expected.clear();
377         expected.add(uids1_2);
378         assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
379 
380         input = new int[]{1, 100};
381         expected.clear();
382         expected.add(uids1_1);
383         expected.add(uids100_100);
384         assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
385 
386         input = new int[]{100, 1};
387         expected.clear();
388         expected.add(uids1_1);
389         expected.add(uids100_100);
390         assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
391 
392         input = new int[]{100, 1, 2, 1, 10};
393         expected.clear();
394         expected.add(uids1_2);
395         expected.add(uids10_10);
396         expected.add(uids100_100);
397         assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
398 
399         input = new int[]{10, 11, 12, 13, 14, 20, 21, 22, 23, 24};
400         expected.clear();
401         expected.add(uids10_14);
402         expected.add(uids20_24);
403         assertEquals(expected, UidRangeUtils.convertArrayToUidRange(input));
404     }
405 
406     @Test
testSortRangesByStartUid()407     public void testSortRangesByStartUid() throws Exception {
408         final UidRange uid1 = new UidRange(100, 110);
409         final UidRange uid2 = new UidRange(120, 130);
410         final UidRange[] unsortedRanges = new UidRange[] {uid2, uid1};
411         final UidRange[] sortedRanges = UidRangeUtils.sortRangesByStartUid(unsortedRanges);
412         assertEquals(uid1, sortedRanges[0]);
413         assertEquals(uid2, sortedRanges[1]);
414     }
415 
416     @Test
testSortedRangesContainOverlap()417     public void testSortedRangesContainOverlap() throws Exception {
418         final UidRange uid1 = new UidRange(100, 110);
419         final UidRange uid2 = new UidRange(109, 120);
420         final UidRange uid3 = new UidRange(120, 130);
421         final UidRange[] overlapRanges1 = new UidRange[] {uid1, uid2};
422         final UidRange[] overlapRanges2 = new UidRange[] {uid2, uid3};
423         final UidRange[] notOverlapRanges = new UidRange[] {uid1, uid3};
424         assertTrue(UidRangeUtils.sortedRangesContainOverlap(overlapRanges1));
425         assertTrue(UidRangeUtils.sortedRangesContainOverlap(overlapRanges2));
426         assertFalse(UidRangeUtils.sortedRangesContainOverlap(notOverlapRanges));
427     }
428 }
429