Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 25016640 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

Close file descriptor

Close the opened file descriptor with
try-with-resources.

Bug: 327672309
Flag: EXEMPT, strict close unused fd
Test: atest BluetoothOppUtilityTest
Change-Id: I577e349dab15991e3acb68ff415d6fdef4c237f4
parent 780e89f6
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -302,10 +302,9 @@ public class BluetoothOppUtility {
        // Open a specific media item using ParcelFileDescriptor.
        ContentResolver resolver = context.getContentResolver();
        String readOnlyMode = "r";
        ParcelFileDescriptor pfd = null;
        try {
            pfd = BluetoothMethodProxy.getInstance()
                    .contentResolverOpenFileDescriptor(resolver, uri, readOnlyMode);
        try (ParcelFileDescriptor unusedPfd =
                BluetoothMethodProxy.getInstance()
                        .contentResolverOpenFileDescriptor(resolver, uri, readOnlyMode)) {
            return true;
        } catch (IOException e) {
            ContentProfileErrorReportUtils.report(
+11 −10
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

import androidx.test.platform.app.InstrumentationRegistry;

@@ -54,7 +55,6 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -170,10 +170,12 @@ public class BluetoothOppUtilityTest {
    }

    @Test
    public void openReceivedFile_fileExist_HandlingApplicationExist() throws FileNotFoundException {
    public void openReceivedFile_fileExist_HandlingApplicationExist() throws Exception {
        Uri contentResolverUri = Uri.parse("content://com.android.bluetooth.opp/btopp/0123");
        Uri fileUri = Uri.parse("content:///tmp/randomFileName.txt");

        ParcelFileDescriptor pfd = mock(ParcelFileDescriptor.class);

        Context spiedContext = spy(new ContextWrapper(mContext));
        // Control BluetoothOppUtility#fileExists flow
        doReturn(mCursor).when(mCallProxy).contentResolverQuery(any(),
@@ -184,8 +186,7 @@ public class BluetoothOppUtilityTest {
        doReturn(fileUri.toString()).when(mCursor).getString(anyInt());

        doReturn(0).when(mCallProxy).contentResolverDelete(any(), any(), any(), any());
        doReturn(null).when(mCallProxy).contentResolverOpenFileDescriptor(any(),
                eq(fileUri), any());
        doReturn(pfd).when(mCallProxy).contentResolverOpenFileDescriptor(any(), eq(fileUri), any());

        // Control BluetoothOppUtility#isRecognizedFileType flow
        PackageManager mockManager = mock(PackageManager.class);
@@ -201,14 +202,15 @@ public class BluetoothOppUtilityTest {
                        argument.getData(), Uri.parse("content:///tmp/randomFileName.txt")
                ) && Objects.equals(argument.getAction(), Intent.ACTION_VIEW)
        ));

        verify(pfd).close();
    }

    @Test
    public void openReceivedFile_fileExist_HandlingApplicationNotExist()
            throws FileNotFoundException {

    public void openReceivedFile_fileExist_HandlingApplicationNotExist() throws Exception {
        Uri contentResolverUri = Uri.parse("content://com.android.bluetooth.opp/btopp/0123");
        Uri fileUri = Uri.parse("content:///tmp/randomFileName.txt");
        ParcelFileDescriptor pfd = mock(ParcelFileDescriptor.class);

        Context spiedContext = spy(new ContextWrapper(mContext));
        // Control BluetoothOppUtility#fileExists flow
@@ -221,8 +223,7 @@ public class BluetoothOppUtilityTest {


        doReturn(0).when(mCallProxy).contentResolverDelete(any(), any(), any(), any());
        doReturn(null).when(mCallProxy).contentResolverOpenFileDescriptor(any(),
                eq(fileUri), any());
        doReturn(pfd).when(mCallProxy).contentResolverOpenFileDescriptor(any(), eq(fileUri), any());

        // Control BluetoothOppUtility#isRecognizedFileType flow
        PackageManager mockManager = mock(PackageManager.class);
@@ -236,9 +237,9 @@ public class BluetoothOppUtilityTest {
                argThat(argument -> argument.getComponent().getClassName().equals(
                        BluetoothOppBtErrorActivity.class.getName())
                ));
        verify(pfd).close();
    }


    @Test
    public void fillRecord_filledAllProperties() {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();