Skip to content

Commit

Permalink
Fixed PFD testGetFd_canRead test failed on macOS.
Browse files Browse the repository at this point in the history
Used `new FileInputStream(FileDescriptor)` instead of `new FileInputStream(String)`.

Avoid depending on Linux file system details, and make full use of Java cross platform feature.

Signed-off-by: ZSmallX <diosmallx@gmail.com>
  • Loading branch information
ZSmallX committed Apr 3, 2022
1 parent 3fd918c commit 86f74e5
Showing 1 changed file with 8 additions and 6 deletions.
Expand Up @@ -2,20 +2,20 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeThat;

import android.os.ParcelFileDescriptor;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.util.ReflectionHelpers;

@RunWith(AndroidJUnit4.class)
public class ShadowParcelFileDescriptorTest {
Expand Down Expand Up @@ -255,13 +255,15 @@ public void testCreatePipeTwice() throws IOException {

@Test
public void testGetFd_canRead() throws IOException {
assumeThat("Windows is an affront to decency.",
File.separator, Matchers.equalTo("/"));

pfd = ParcelFileDescriptor.open(readOnlyFile, ParcelFileDescriptor.MODE_READ_ONLY);
int fd = pfd.getFd();
assertThat(fd).isGreaterThan(0);
FileInputStream is = new FileInputStream(new File("/proc/self/fd/" + fd));

final FileDescriptor fD = pfd.getFileDescriptor();
assertThat(fD.valid()).isTrue();
assertThat(fd).isEqualTo(ReflectionHelpers.getField(fD, "fd"));

FileInputStream is = new FileInputStream(fD);
assertThat(is.read()).isEqualTo(READ_ONLY_FILE_CONTENTS);
is.close();
}
Expand Down

0 comments on commit 86f74e5

Please sign in to comment.