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 android.net.wifi;
18 
19 import static android.net.wifi.WifiStringResourceWrapper.CARRIER_ID_RESOURCE_NAME_SUFFIX;
20 
21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
22 
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.any;
28 import static org.mockito.Mockito.eq;
29 import static org.mockito.Mockito.lenient;
30 import static org.mockito.Mockito.validateMockitoUsage;
31 import static org.mockito.Mockito.when;
32 
33 import android.content.Context;
34 import android.content.res.Resources;
35 import android.telephony.SubscriptionManager;
36 
37 import androidx.test.filters.SmallTest;
38 
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.mockito.MockitoSession;
45 
46 /**
47  * Unit tests for {@link WifiStringResourceWrapper}
48  */
49 @SmallTest
50 public class WifiStringResourceWrapperTest {
51     private MockitoSession mStaticMockSession = null;
52 
53     @Mock WifiContext mContext;
54     @Mock Resources mResources;
55     @Mock Context mApkContext;
56 
57     WifiStringResourceWrapper mDut;
58 
59     private static final int SUB_ID = 123;
60     private static final int CARRIER_ID = 4567;
61 
62     private static final int RES_ID_NOT_FOUND = 0;
63 
64     private static final String RES_NAME_1 = "some_eap_error";
65     private static final int RES_ID_1 = 32764;
66     private static final String RES_STRING_VAL_1 = "Some message";
67 
68     private static final int RES_ID_2 = 32765;
69     private static final String[] RES_STRING_ARRAY_VAL_2 = { // carrier ID not included
70             ":::1234:::Some message AA",
71             ":::45678:::Some message BB"
72     };
73     private static final String[] RES_STRING_ARRAY_VAL_3 = { // carrier ID is here!
74             ":::1234:::Some message CC",
75             ":::4567:::Some message DD"
76     };
77     private static final String[] RES_STRING_ARRAY_VAL_4 = { // carrier ID is here - empty message
78             ":::1234:::Some message EE",
79             ":::4567:::"
80     };
81     private static final String[] RES_STRING_ARRAY_VAL_5 = { // carrier ID is here - bad format
82             ":::1234:::Some message FF",
83             ":::4567::"
84     };
85 
86     private static final String RES_NAME_DISABLE_THRESHOLD =
87             "config_wifiDisableReasonAuthenticationFailureCarrierSpecificThreshold";
88     private static final int RES_ID_DISABLE_THRESHOLD = 32766;
89     private static final int RES_ID_DISABLE_THRESHOLD_2 = 32767;
90     private static final int RES_VAL_DISABLE_THRESHOLD = 1;
91 
92     private static final String RES_NAME_OOB_PSEUDONYM_ENABLED =
93             "config_wifiOobPseudonymEnabled";
94     private static final int RES_ID_OOB_PSEUDONYM_ENABLED = 32768;
95     /**
96      * Sets up for unit test
97      */
98     @Before
setUp()99     public void setUp() throws Exception {
100         MockitoAnnotations.initMocks(this);
101 
102         // static mocking
103         mStaticMockSession = mockitoSession()
104                 .mockStatic(SubscriptionManager.class)
105                 .startMocking();
106         lenient().when(SubscriptionManager.getResourcesForSubId(any(), eq(SUB_ID)))
107                 .thenReturn(mResources);
108 
109         when(mResources.getIdentifier(eq(RES_NAME_1), eq("string"), any())).thenReturn(RES_ID_1);
110         when(mResources.getString(eq(RES_ID_1), any())).thenReturn(RES_STRING_VAL_1);
111         when(mResources.getIdentifier(eq(RES_NAME_1 + CARRIER_ID_RESOURCE_NAME_SUFFIX),
112                 eq("array"), any())).thenReturn(RES_ID_NOT_FOUND);
113 
114         when(mResources.getIdentifier(eq(RES_NAME_DISABLE_THRESHOLD), eq("integer"), any()))
115                 .thenReturn(RES_ID_DISABLE_THRESHOLD);
116         when(mResources.getInteger(eq(RES_ID_DISABLE_THRESHOLD)))
117                 .thenReturn(RES_VAL_DISABLE_THRESHOLD);
118         when(mResources.getIdentifier(
119                 eq(RES_NAME_DISABLE_THRESHOLD + CARRIER_ID_RESOURCE_NAME_SUFFIX),
120                 eq("array"), any())).thenReturn(RES_ID_NOT_FOUND);
121 
122         when(mContext.getResourcesApkContext()).thenReturn(mApkContext);
123         mDut = new WifiStringResourceWrapper(mContext, SUB_ID, CARRIER_ID);
124         when(mResources.getIdentifier(eq(RES_NAME_OOB_PSEUDONYM_ENABLED), eq("bool"), any()))
125                 .thenReturn(RES_ID_OOB_PSEUDONYM_ENABLED);
126         when(mResources.getBoolean(RES_ID_OOB_PSEUDONYM_ENABLED))
127                 .thenReturn(false);
128         when(mResources.getIdentifier(
129                 eq(RES_NAME_OOB_PSEUDONYM_ENABLED + CARRIER_ID_RESOURCE_NAME_SUFFIX),
130                 eq("array"), any())).thenReturn(RES_ID_NOT_FOUND);
131     }
132 
133     @After
cleanUp()134     public void cleanUp() throws Exception {
135         validateMockitoUsage();
136         if (mStaticMockSession != null) {
137             mStaticMockSession.finishMocking();
138         }
139     }
140 
141     @Test
testBasicOperations()142     public void testBasicOperations() {
143         // test for #getString
144         assertEquals("Some message", mDut.getString(RES_NAME_1));
145         assertNull(mDut.getString("something else"));
146 
147         // test for #getInt
148         assertEquals(RES_VAL_DISABLE_THRESHOLD, mDut.getInt(RES_NAME_DISABLE_THRESHOLD, 19432));
149         assertEquals(19432, mDut.getInt(RES_NAME_DISABLE_THRESHOLD + "_Garbage", 19432));
150 
151         // test for #getBoolean
152         assertFalse(mDut.getBoolean(RES_NAME_OOB_PSEUDONYM_ENABLED, true));
153         assertFalse(mDut.getBoolean(RES_NAME_OOB_PSEUDONYM_ENABLED + "_Garbage", false));
154     }
155 
156     @Test
testCarrierIdWithBaseNoOverride()157     public void testCarrierIdWithBaseNoOverride() {
158         // test for #getString
159         when(mResources.getIdentifier(eq(RES_NAME_1 + CARRIER_ID_RESOURCE_NAME_SUFFIX),
160                 eq("array"), any())).thenReturn(RES_ID_2);
161         when(mResources.getStringArray(eq(RES_ID_2))).thenReturn(RES_STRING_ARRAY_VAL_2);
162         assertEquals("Some message", mDut.getString(RES_NAME_1));
163 
164         // test for #getInt
165         when(mResources.getIdentifier(
166                 eq(RES_NAME_DISABLE_THRESHOLD + CARRIER_ID_RESOURCE_NAME_SUFFIX),
167                 eq("array"), any())).thenReturn(RES_ID_DISABLE_THRESHOLD_2);
168         final String[] resStringArray = {
169                 ":::1234:::3",
170                 ":::45678:::4"
171         };
172         when(mResources.getStringArray(RES_ID_DISABLE_THRESHOLD_2)).thenReturn(resStringArray);
173         assertEquals(RES_VAL_DISABLE_THRESHOLD, mDut.getInt(RES_NAME_DISABLE_THRESHOLD, 19432));
174 
175         // test for #getboolean
176         when(mResources.getIdentifier(
177                 eq(RES_NAME_OOB_PSEUDONYM_ENABLED + CARRIER_ID_RESOURCE_NAME_SUFFIX),
178                 eq("array"), any())).thenReturn(RES_ID_OOB_PSEUDONYM_ENABLED);
179         final String[] boolResStringArray = {
180                 ":::1234:::true",
181                 ":::2345:::true"
182         };
183         when(mResources.getStringArray(RES_ID_OOB_PSEUDONYM_ENABLED))
184                 .thenReturn(boolResStringArray);
185 
186         assertFalse(mDut.getBoolean(RES_NAME_OOB_PSEUDONYM_ENABLED, true));
187     }
188 
189     @Test
testCarrierIdAvailable()190     public void testCarrierIdAvailable() {
191         // test for #getString
192         when(mResources.getIdentifier(eq(RES_NAME_1 + CARRIER_ID_RESOURCE_NAME_SUFFIX),
193                 eq("array"), any())).thenReturn(RES_ID_2);
194         when(mResources.getStringArray(eq(RES_ID_2))).thenReturn(RES_STRING_ARRAY_VAL_3);
195         assertEquals("Some message DD", mDut.getString(RES_NAME_1));
196 
197         // test for #getInt
198         when(mResources.getIdentifier(
199                 eq(RES_NAME_DISABLE_THRESHOLD + CARRIER_ID_RESOURCE_NAME_SUFFIX),
200                 eq("array"), any())).thenReturn(RES_ID_DISABLE_THRESHOLD_2);
201         final String[] resStringArray = {
202                 ":::1234:::2",
203                 ":::4567:::123"
204         };
205         when(mResources.getStringArray(RES_ID_DISABLE_THRESHOLD_2)).thenReturn(resStringArray);
206         assertEquals(123, mDut.getInt(RES_NAME_DISABLE_THRESHOLD, 19432));
207 
208         // test for #getBoolean
209         when(mResources.getIdentifier(
210                 eq(RES_NAME_OOB_PSEUDONYM_ENABLED + CARRIER_ID_RESOURCE_NAME_SUFFIX),
211                 eq("array"), any())).thenReturn(RES_ID_OOB_PSEUDONYM_ENABLED);
212         final String[] boolResStringArray = {
213                 ":::1234:::false",
214                 ":::4567:::true"
215         };
216         when(mResources.getStringArray(RES_ID_OOB_PSEUDONYM_ENABLED))
217                 .thenReturn(boolResStringArray);
218         assertTrue(mDut.getBoolean(RES_NAME_OOB_PSEUDONYM_ENABLED, false));
219     }
220 
221     @Test
testCarrierIdAvailableEmptyMessage()222     public void testCarrierIdAvailableEmptyMessage() {
223         // test for #getString
224         when(mResources.getIdentifier(eq(RES_NAME_1 + CARRIER_ID_RESOURCE_NAME_SUFFIX),
225                 eq("array"), any())).thenReturn(RES_ID_2);
226         when(mResources.getStringArray(eq(RES_ID_2))).thenReturn(RES_STRING_ARRAY_VAL_4);
227         assertEquals("", mDut.getString(RES_NAME_1));
228 
229         // test for #getInt
230         when(mResources.getIdentifier(
231                 eq(RES_NAME_DISABLE_THRESHOLD + CARRIER_ID_RESOURCE_NAME_SUFFIX),
232                 eq("array"), any())).thenReturn(RES_ID_DISABLE_THRESHOLD_2);
233         final String[] resStringArray = {
234                 ":::1234:::2",
235                 ":::4567:::"
236         };
237         when(mResources.getStringArray(RES_ID_DISABLE_THRESHOLD_2)).thenReturn(resStringArray);
238         assertEquals(RES_VAL_DISABLE_THRESHOLD, mDut.getInt(RES_NAME_DISABLE_THRESHOLD, 19432));
239 
240         // test for #getBoolean
241         when(mResources.getIdentifier(
242                 eq(RES_NAME_OOB_PSEUDONYM_ENABLED + CARRIER_ID_RESOURCE_NAME_SUFFIX),
243                 eq("array"), any())).thenReturn(RES_ID_OOB_PSEUDONYM_ENABLED);
244         final String[] boolResStringArray = {
245                 ":::1234:::true",
246                 ":::4567:::"
247         };
248         when(mResources.getStringArray(RES_ID_OOB_PSEUDONYM_ENABLED))
249                 .thenReturn(boolResStringArray);
250         assertFalse(mDut.getBoolean(RES_NAME_OOB_PSEUDONYM_ENABLED, true));
251     }
252 
253     @Test
testCarrierIdBadlyFormatted()254     public void testCarrierIdBadlyFormatted() {
255         // test for #getString
256         when(mResources.getIdentifier(eq(RES_NAME_1 + CARRIER_ID_RESOURCE_NAME_SUFFIX),
257                 eq("array"), any())).thenReturn(RES_ID_2);
258         when(mResources.getStringArray(eq(RES_ID_2))).thenReturn(RES_STRING_ARRAY_VAL_5);
259         assertEquals("Some message", mDut.getString(RES_NAME_1));
260 
261         // test for #getInt
262         when(mResources.getIdentifier(
263                 eq(RES_NAME_DISABLE_THRESHOLD + CARRIER_ID_RESOURCE_NAME_SUFFIX),
264                 eq("array"), any())).thenReturn(RES_ID_DISABLE_THRESHOLD_2);
265         final String[] resStringArray = {
266                 ":::1234:::2",
267                 ":::4567:::343asdf"
268         };
269         when(mResources.getStringArray(RES_ID_DISABLE_THRESHOLD_2)).thenReturn(resStringArray);
270         assertEquals(RES_VAL_DISABLE_THRESHOLD, mDut.getInt(RES_NAME_DISABLE_THRESHOLD, 19432));
271 
272         // test for #getBoolean
273         when(mResources.getIdentifier(
274                 eq(RES_NAME_OOB_PSEUDONYM_ENABLED + CARRIER_ID_RESOURCE_NAME_SUFFIX),
275                 eq("array"), any())).thenReturn(RES_ID_OOB_PSEUDONYM_ENABLED);
276         final String[] boolResStringArray = {
277                 ":::1234:::true",
278                 ":::4567:::badTrue"
279         };
280         when(mResources.getStringArray(RES_ID_OOB_PSEUDONYM_ENABLED))
281                 .thenReturn(boolResStringArray);
282         assertFalse(mDut.getBoolean(RES_NAME_OOB_PSEUDONYM_ENABLED, true));
283     }
284 }
285