1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.file.openers; 17 18 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.createFile; 19 import static com.google.common.truth.Truth.assertThat; 20 import static java.nio.charset.StandardCharsets.UTF_8; 21 import static org.junit.Assert.assertThrows; 22 23 import android.net.Uri; 24 import android.os.ParcelFileDescriptor; 25 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 26 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend; 27 import com.google.android.libraries.mobiledatadownload.file.common.UnsupportedFileStorageOperation; 28 import com.google.android.libraries.mobiledatadownload.file.common.testing.FileDescriptorLeakChecker; 29 import com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils; 30 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri; 31 import com.google.android.libraries.mobiledatadownload.file.samples.ByteCountingMonitor; 32 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform; 33 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos; 34 import com.google.common.collect.ImmutableList; 35 import com.google.common.io.CharStreams; 36 import java.io.FileNotFoundException; 37 import java.io.InputStream; 38 import java.io.InputStreamReader; 39 import java.io.Reader; 40 import org.junit.Before; 41 import org.junit.Rule; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.junit.runners.JUnit4; 45 46 @RunWith(JUnit4.class) 47 public final class ParcelFileDescriptorOpenerAndroidTest { 48 49 private SynchronousFileStorage storage; 50 51 @Rule public FileDescriptorLeakChecker leakChecker = new FileDescriptorLeakChecker(); 52 @Rule public TemporaryUri tmpUri = new TemporaryUri(); 53 54 @Before initStorage()55 public void initStorage() throws Exception { 56 storage = 57 new SynchronousFileStorage( 58 ImmutableList.of(new JavaFileBackend()), ImmutableList.of(new CompressTransform())); 59 } 60 61 @Test openParcelFileDescriptor_shouldReadFile()62 public void openParcelFileDescriptor_shouldReadFile() throws Exception { 63 Uri uri = tmpUri.newUri(); 64 createFile(storage, uri, "content"); 65 try (InputStream in = 66 new ParcelFileDescriptor.AutoCloseInputStream( 67 storage.open(uri, ParcelFileDescriptorOpener.create()))) { 68 Reader reader = new InputStreamReader(in, UTF_8); 69 assertThat(CharStreams.toString(reader)).isEqualTo("content"); 70 } 71 } 72 73 @Test openParcelFileDescriptor_withMissingFile_throwsFileNotFound()74 public void openParcelFileDescriptor_withMissingFile_throwsFileNotFound() throws Exception { 75 Uri uri = Uri.parse("file:/does-not-exist"); 76 assertThrows( 77 FileNotFoundException.class, () -> storage.open(uri, ParcelFileDescriptorOpener.create())); 78 } 79 80 @Test openParcelFileDescriptor_withMonitor_shouldReadFile()81 public void openParcelFileDescriptor_withMonitor_shouldReadFile() throws Exception { 82 SynchronousFileStorage storageWithMonitor = 83 new SynchronousFileStorage( 84 ImmutableList.of(new JavaFileBackend()), 85 ImmutableList.of(), 86 ImmutableList.of(new ByteCountingMonitor())); 87 Uri uri = tmpUri.newUri(); 88 byte[] content = StreamUtils.makeArrayOfBytesContent(); 89 StreamUtils.createFile(storageWithMonitor, uri, content); 90 91 try (InputStream in = 92 new ParcelFileDescriptor.AutoCloseInputStream( 93 storageWithMonitor.open(uri, ParcelFileDescriptorOpener.create()))) { 94 assertThat(StreamUtils.readFileInBytesFromSource(in)).isEqualTo(content); 95 } 96 } 97 98 @Test openParcelFileDescriptor_withTransform_throwsUnsupportedOperation()99 public void openParcelFileDescriptor_withTransform_throwsUnsupportedOperation() throws Exception { 100 Uri uri = tmpUri.newUriBuilder().withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC).build(); 101 byte[] content = StreamUtils.makeArrayOfBytesContent(); 102 StreamUtils.createFile(storage, uri, content); 103 104 assertThrows( 105 UnsupportedFileStorageOperation.class, 106 () -> storage.open(uri, ParcelFileDescriptorOpener.create())); 107 } 108 } 109