1 /* 2 * Copyright (C) 2024 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.phone.satellite.entitlement; 18 19 import static com.android.phone.satellite.entitlement.SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_DISABLED; 20 import static com.android.phone.satellite.entitlement.SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_ENABLED; 21 import static com.android.phone.satellite.entitlement.SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_INCOMPATIBLE; 22 import static com.android.phone.satellite.entitlement.SatelliteEntitlementResult.SATELLITE_ENTITLEMENT_STATUS_PROVISIONING; 23 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertNotNull; 26 import static org.junit.Assert.assertTrue; 27 import static org.mockito.ArgumentMatchers.any; 28 import static org.mockito.ArgumentMatchers.anyInt; 29 import static org.mockito.ArgumentMatchers.anyVararg; 30 import static org.mockito.ArgumentMatchers.eq; 31 import static org.mockito.Mockito.doReturn; 32 import static org.mockito.Mockito.verify; 33 34 import android.content.Context; 35 import android.os.PersistableBundle; 36 import android.telephony.CarrierConfigManager; 37 import android.telephony.SubscriptionManager; 38 import android.telephony.TelephonyManager; 39 40 import androidx.test.ext.junit.runners.AndroidJUnit4; 41 42 import com.android.libraries.entitlement.ServiceEntitlement; 43 import com.android.libraries.entitlement.ServiceEntitlementRequest; 44 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.mockito.Mock; 49 import org.mockito.MockitoAnnotations; 50 51 import java.lang.reflect.Field; 52 import java.util.Arrays; 53 import java.util.List; 54 55 @RunWith(AndroidJUnit4.class) 56 public class SatelliteEntitlementApiTest { 57 private static final String TEST_URL = "https://test.url"; 58 private static final List<String> TEST_PLMN_ALLOWED = Arrays.asList("31026", "302820"); 59 private static final String TEST_APP_NAME = "androidSatmode"; 60 @Mock 61 Context mContext; 62 @Mock 63 ServiceEntitlement mServiceEntitlement; 64 @Mock 65 CarrierConfigManager mCarrierConfigManager; 66 @Mock 67 TelephonyManager mTelephonyManager; 68 private PersistableBundle mCarrierConfigBundle; 69 private SatelliteEntitlementApi mSatelliteEntitlementAPI; 70 71 @Before setUp()72 public void setUp() throws Exception { 73 MockitoAnnotations.initMocks(this); 74 75 doReturn(Context.CARRIER_CONFIG_SERVICE).when(mContext).getSystemServiceName( 76 CarrierConfigManager.class); 77 doReturn(mCarrierConfigManager).when(mContext).getSystemService( 78 Context.CARRIER_CONFIG_SERVICE); 79 mCarrierConfigBundle = new PersistableBundle(); 80 doReturn(mCarrierConfigBundle) 81 .when(mCarrierConfigManager).getConfigForSubId(anyInt(), anyVararg()); 82 doReturn(Context.TELEPHONY_SERVICE).when(mContext).getSystemServiceName( 83 TelephonyManager.class); 84 doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE); 85 doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt()); 86 87 mSatelliteEntitlementAPI = new SatelliteEntitlementApi(mContext, mCarrierConfigBundle, 88 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID); 89 90 mCarrierConfigBundle.putString( 91 CarrierConfigManager.ImsServiceEntitlement.KEY_ENTITLEMENT_SERVER_URL_STRING, 92 TEST_URL); 93 mCarrierConfigBundle.putString( 94 CarrierConfigManager.KEY_SATELLITE_ENTITLEMENT_APP_NAME_STRING, 95 TEST_APP_NAME); 96 97 Field fieldServiceEntitlement = SatelliteEntitlementApi.class.getDeclaredField( 98 "mServiceEntitlement"); 99 fieldServiceEntitlement.setAccessible(true); 100 fieldServiceEntitlement.set(mSatelliteEntitlementAPI, mServiceEntitlement); 101 } 102 103 @Test testCheckEntitlementStatus()104 public void testCheckEntitlementStatus() throws Exception { 105 // Get the EntitlementStatus to DISABLED 106 int expectedEntitlementStatus = SATELLITE_ENTITLEMENT_STATUS_DISABLED; 107 doReturn(getResponse(SATELLITE_ENTITLEMENT_STATUS_DISABLED)) 108 .when(mServiceEntitlement) 109 .queryEntitlementStatus(eq(ServiceEntitlement.APP_SATELLITE_ENTITLEMENT), any()); 110 SatelliteEntitlementResult result = 111 mSatelliteEntitlementAPI.checkEntitlementStatus(); 112 assertNotNull(result); 113 assertEquals(expectedEntitlementStatus, result.getEntitlementStatus()); 114 assertTrue(result.getAllowedPLMNList().size() == 0); 115 116 // Get the EntitlementStatus to ENABLED 117 expectedEntitlementStatus = SATELLITE_ENTITLEMENT_STATUS_ENABLED; 118 doReturn(getResponse(SATELLITE_ENTITLEMENT_STATUS_ENABLED)) 119 .when(mServiceEntitlement) 120 .queryEntitlementStatus(eq(ServiceEntitlement.APP_SATELLITE_ENTITLEMENT), any()); 121 result = mSatelliteEntitlementAPI.checkEntitlementStatus(); 122 assertNotNull(result); 123 assertEquals(expectedEntitlementStatus, result.getEntitlementStatus()); 124 assertEquals(TEST_PLMN_ALLOWED, result.getAllowedPLMNList()); 125 126 // Get the EntitlementStatus to INCOMPATIBLE 127 expectedEntitlementStatus = SATELLITE_ENTITLEMENT_STATUS_INCOMPATIBLE; 128 doReturn(getResponse(SATELLITE_ENTITLEMENT_STATUS_INCOMPATIBLE)) 129 .when(mServiceEntitlement) 130 .queryEntitlementStatus(eq(ServiceEntitlement.APP_SATELLITE_ENTITLEMENT), any()); 131 result = mSatelliteEntitlementAPI.checkEntitlementStatus(); 132 assertNotNull(result); 133 assertEquals(expectedEntitlementStatus, result.getEntitlementStatus()); 134 assertTrue(result.getAllowedPLMNList().size() == 0); 135 136 // Get the EntitlementStatus to PROVISIONING 137 expectedEntitlementStatus = SATELLITE_ENTITLEMENT_STATUS_PROVISIONING; 138 doReturn(getResponse(SATELLITE_ENTITLEMENT_STATUS_PROVISIONING)) 139 .when(mServiceEntitlement) 140 .queryEntitlementStatus(eq(ServiceEntitlement.APP_SATELLITE_ENTITLEMENT), any()); 141 result = mSatelliteEntitlementAPI.checkEntitlementStatus(); 142 assertNotNull(result); 143 assertEquals(expectedEntitlementStatus, result.getEntitlementStatus()); 144 assertTrue(result.getAllowedPLMNList().size() == 0); 145 } 146 147 @Test testServiceEntitlementRequest()148 public void testServiceEntitlementRequest() throws Exception { 149 String expectedAppId = ServiceEntitlement.APP_SATELLITE_ENTITLEMENT; 150 doReturn(getResponse(SATELLITE_ENTITLEMENT_STATUS_DISABLED)) 151 .when(mServiceEntitlement) 152 .queryEntitlementStatus(eq(expectedAppId), any()); 153 ServiceEntitlementRequest.Builder requestBuilder = ServiceEntitlementRequest.builder(); 154 requestBuilder.setAcceptContentType(ServiceEntitlementRequest.ACCEPT_CONTENT_TYPE_JSON); 155 requestBuilder.setAppName(TEST_APP_NAME); 156 ServiceEntitlementRequest expectedRequest = requestBuilder.build(); 157 158 mSatelliteEntitlementAPI.checkEntitlementStatus(); 159 160 verify(mServiceEntitlement).queryEntitlementStatus(eq(expectedAppId), eq(expectedRequest)); 161 } 162 getResponse(int entitlementStatus)163 private String getResponse(int entitlementStatus) { 164 return "{\"VERS\":{\"version\":\"1\",\"validity\":\"172800\"}," 165 + "\"TOKEN\":{\"token\":\"ASH127AHHA88SF\"},\"" 166 + ServiceEntitlement.APP_SATELLITE_ENTITLEMENT + "\":{" 167 + "\"EntitlementStatus\":\"" + entitlementStatus + "\"" 168 + getPLMNListOrEmpty(entitlementStatus) 169 + "}}"; 170 } 171 getPLMNListOrEmpty(int entitlementStatus)172 private String getPLMNListOrEmpty(int entitlementStatus) { 173 return entitlementStatus == SATELLITE_ENTITLEMENT_STATUS_ENABLED ? "," 174 + "\"PLMNAllowed\":[{\"PLMN\":\"31026\",\"DataPlanType\":\"unmetered\"}," 175 + "{\"PLMN\":\"302820\",\"DataPlanType\":\"metered\"}]" : ""; 176 } 177 } 178