1 /*
2  * Copyright (C) 2023 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.mms.service;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.telephony.CarrierConfigManager;
31 import android.telephony.NetworkRegistrationInfo;
32 import android.telephony.ServiceState;
33 import android.telephony.SmsManager;
34 import android.telephony.SubscriptionManager;
35 import android.telephony.TelephonyManager;
36 
37 import androidx.test.core.app.ApplicationProvider;
38 
39 import com.android.mms.service.metrics.MmsStats;
40 import com.android.mms.service.metrics.PersistMmsAtomsStorage;
41 
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RobolectricTestRunner;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class MmsRequestRoboTest {
51     // Mocked classes
52     private Context mContext;
53     private TelephonyManager mTelephonyManager;
54     private SubscriptionManager mSubscriptionManager;
55 
56     private final int mSubId = 1;
57     private MmsService mMmsService;
58     private MmsStats mMmsStats;
59     private static final String sFakeUri = "http://greatdogs.com";
60     private static final String sFakeLocationUri = "http://greatdogs.com";
61     private static final long sFakeMessageId = 8675309L;
62     private PersistMmsAtomsStorage mPersistMmsAtomsStorage;
63     private SmsManager mSmsManager;
64     private Bundle mCarrierConfigValues;
65     private static final int sMaxPduSize = 3 * 1000;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         mContext = spy(ApplicationProvider.getApplicationContext());
71         mTelephonyManager = mock(TelephonyManager.class);
72         mSubscriptionManager = mock(SubscriptionManager.class);
73         mSmsManager = spy(SmsManager.getSmsManagerForSubscriptionId(mSubId));
74 
75         when(mContext.getSystemService(Context.TELEPHONY_SERVICE))
76                 .thenReturn(mTelephonyManager);
77         when(mTelephonyManager.createForSubscriptionId(anyInt()))
78                 .thenReturn(mTelephonyManager);
79         when(mContext.getSystemService(SubscriptionManager.class))
80                 .thenReturn(mSubscriptionManager);
81 
82         mPersistMmsAtomsStorage = mock(PersistMmsAtomsStorage.class);
83         mMmsStats = new MmsStats(mContext, mPersistMmsAtomsStorage, mSubId,
84                 mTelephonyManager, null, true);
85         mCarrierConfigValues = new Bundle();
86         mCarrierConfigValues.putInt(
87                 CarrierConfigManager.KEY_MMS_MAX_NTN_PAYLOAD_SIZE_BYTES_INT,
88                 sMaxPduSize);
89     }
90 
91     @After
tearDown()92     public void tearDown() {
93         mContext = null;
94         mTelephonyManager = null;
95         mSubscriptionManager = null;
96     }
97 
98     @Test
sendRequest_noSatellite_sendSuccessful()99     public void sendRequest_noSatellite_sendSuccessful() {
100         SendRequest request = new SendRequest(mMmsService, mSubId, Uri.parse(sFakeUri),
101                 sFakeLocationUri, /* sentIntent= */ null, /* callingPkg= */ null,
102                 mCarrierConfigValues, /* context= */ mMmsService,
103                 sFakeMessageId, mMmsStats, mTelephonyManager);
104         request.mPduData = new byte[sMaxPduSize + 100];
105 
106         boolean okToSend = request.canTransferPayloadOnCurrentNetwork();
107 
108         assertThat(okToSend).isTrue();
109     }
110 
111     @Test
sendRequest_connectedToSatellite_smallPdu_sendSuccessful()112     public void sendRequest_connectedToSatellite_smallPdu_sendSuccessful() {
113         ServiceState ss = new ServiceState();
114         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
115                 .setIsNonTerrestrialNetwork(true)
116                 .build();
117         ss.addNetworkRegistrationInfo(nri);
118         doReturn(ss).when(mTelephonyManager).getServiceState();
119         SendRequest request = new SendRequest(mMmsService, mSubId, Uri.parse(sFakeUri),
120                 sFakeLocationUri, /* sentIntent= */ null, /* callingPkg= */ null,
121                 mCarrierConfigValues, /* context= */ mMmsService,
122                 sFakeMessageId, mMmsStats, mTelephonyManager);
123         request.mPduData = new byte[sMaxPduSize - 1];
124 
125         boolean okToSend = request.canTransferPayloadOnCurrentNetwork();
126 
127         assertThat(okToSend).isTrue();
128     }
129 
130     @Test
sendRequest_connectedToSatellite_largePdu_sendSFails()131     public void sendRequest_connectedToSatellite_largePdu_sendSFails() {
132         ServiceState ss = new ServiceState();
133         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
134                 .setIsNonTerrestrialNetwork(true)
135                 .build();
136         ss.addNetworkRegistrationInfo(nri);
137         doReturn(ss).when(mTelephonyManager).getServiceState();
138         SendRequest request = new SendRequest(mMmsService, mSubId, Uri.parse(sFakeUri),
139                 sFakeLocationUri, /* sentIntent= */ null, /* callingPkg= */ null,
140                 mCarrierConfigValues, /* context= */ mMmsService,
141                 sFakeMessageId, mMmsStats, mTelephonyManager);
142         request.mPduData = new byte[sMaxPduSize + 1];
143 
144         boolean okToSend = request.canTransferPayloadOnCurrentNetwork();
145 
146         assertThat(okToSend).isFalse();
147     }
148 
149     @Test
downloadRequest_noSatellite_downloadSuccessful()150     public void downloadRequest_noSatellite_downloadSuccessful() {
151         doReturn(150L).when(mSmsManager).getWapMessageSize(sFakeUri);
152         DownloadRequest request = new DownloadRequest(mMmsService, mSubId, sFakeUri,
153                 Uri.parse(sFakeUri), /* downloadIntent= */ null, /* callingPkg= */ null,
154                 mCarrierConfigValues, /* context= */ mMmsService, sFakeMessageId, mMmsStats,
155                 mTelephonyManager);
156 
157         boolean okToDownload = request.canTransferPayloadOnCurrentNetwork();
158 
159         assertThat(okToDownload).isTrue();
160     }
161 }
162