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.net.module.util
18 
19 import androidx.test.filters.SmallTest
20 import androidx.test.runner.AndroidJUnit4
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import kotlin.test.assertEquals
24 import kotlin.test.assertFailsWith
25 
26 @RunWith(AndroidJUnit4::class)
27 @SmallTest
28 class PerUidCounterTest {
29     private val UID_A = 1000
30     private val UID_B = 1001
31     private val UID_C = 1002
32 
33     @Test
testCounterMaximumnull34     fun testCounterMaximum() {
35         assertFailsWith<IllegalArgumentException> {
36             PerUidCounter(-1)
37         }
38         assertFailsWith<IllegalArgumentException> {
39             PerUidCounter(0)
40         }
41 
42         val testLimit = 1000
43         val testCounter = PerUidCounter(testLimit)
44         assertEquals(0, testCounter[UID_A])
45         repeat(testLimit) {
46             testCounter.incrementCountOrThrow(UID_A)
47         }
48         assertEquals(testLimit, testCounter[UID_A])
49         assertFailsWith<IllegalStateException> {
50             testCounter.incrementCountOrThrow(UID_A)
51         }
52         assertEquals(testLimit, testCounter[UID_A])
53     }
54 
55     @Test
testIncrementCountOrThrownull56     fun testIncrementCountOrThrow() {
57         val counter = PerUidCounter(3)
58 
59         // Verify the counters work independently.
60         counter.incrementCountOrThrow(UID_A)
61         counter.incrementCountOrThrow(UID_B)
62         counter.incrementCountOrThrow(UID_B)
63         counter.incrementCountOrThrow(UID_A)
64         counter.incrementCountOrThrow(UID_A)
65         assertEquals(3, counter[UID_A])
66         assertEquals(2, counter[UID_B])
67         assertFailsWith<IllegalStateException> {
68             counter.incrementCountOrThrow(UID_A)
69         }
70         counter.incrementCountOrThrow(UID_B)
71         assertFailsWith<IllegalStateException> {
72             counter.incrementCountOrThrow(UID_B)
73         }
74 
75         // Verify exception can be triggered again.
76         assertFailsWith<IllegalStateException> {
77             counter.incrementCountOrThrow(UID_A)
78         }
79         assertFailsWith<IllegalStateException> {
80             repeat(3) {
81                 counter.incrementCountOrThrow(UID_A)
82             }
83         }
84         assertEquals(3, counter[UID_A])
85         assertEquals(3, counter[UID_B])
86         assertEquals(0, counter[UID_C])
87     }
88 
89     @Test
testDecrementCountOrThrownull90     fun testDecrementCountOrThrow() {
91         val counter = PerUidCounter(3)
92 
93         // Verify the count cannot go below zero.
94         assertFailsWith<IllegalStateException> {
95             counter.decrementCountOrThrow(UID_A)
96         }
97         assertFailsWith<IllegalStateException> {
98             repeat(5) {
99                 counter.decrementCountOrThrow(UID_A)
100             }
101         }
102 
103         // Verify the counters work independently.
104         counter.incrementCountOrThrow(UID_A)
105         counter.incrementCountOrThrow(UID_B)
106         assertEquals(1, counter[UID_A])
107         assertEquals(1, counter[UID_B])
108         assertFailsWith<IllegalStateException> {
109             repeat(3) {
110                 counter.decrementCountOrThrow(UID_A)
111             }
112         }
113         assertFailsWith<IllegalStateException> {
114             counter.decrementCountOrThrow(UID_A)
115         }
116         assertEquals(0, counter[UID_A])
117         assertEquals(1, counter[UID_B])
118 
119         // Verify mixing increment and decrement.
120         val largeCounter = PerUidCounter(100)
121         repeat(90) {
122             largeCounter.incrementCountOrThrow(UID_A)
123         }
124         repeat(70) {
125             largeCounter.decrementCountOrThrow(UID_A)
126         }
127         repeat(80) {
128             largeCounter.incrementCountOrThrow(UID_A)
129         }
130         assertFailsWith<IllegalStateException> {
131             largeCounter.incrementCountOrThrow(UID_A)
132         }
133         assertEquals(100, largeCounter[UID_A])
134         repeat(100) {
135             largeCounter.decrementCountOrThrow(UID_A)
136         }
137         assertFailsWith<IllegalStateException> {
138             largeCounter.decrementCountOrThrow(UID_A)
139         }
140         assertEquals(0, largeCounter[UID_A])
141     }
142 }