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.downloader.inline; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.junit.Assert.assertThrows; 20 21 import android.content.Context; 22 import android.net.Uri; 23 import androidx.test.core.app.ApplicationProvider; 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 import com.google.android.libraries.mobiledatadownload.DownloadException; 26 import com.google.android.libraries.mobiledatadownload.DownloadException.DownloadResultCode; 27 import com.google.android.libraries.mobiledatadownload.FileSource; 28 import com.google.android.libraries.mobiledatadownload.downloader.DownloadConstraints; 29 import com.google.android.libraries.mobiledatadownload.downloader.DownloadRequest; 30 import com.google.android.libraries.mobiledatadownload.downloader.InlineDownloadParams; 31 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 32 import com.google.android.libraries.mobiledatadownload.file.backends.AndroidFileBackend; 33 import com.google.android.libraries.mobiledatadownload.file.common.testing.FakeFileBackend; 34 import com.google.android.libraries.mobiledatadownload.file.common.testing.FakeFileBackend.OperationType; 35 import com.google.android.libraries.mobiledatadownload.file.openers.ReadStreamOpener; 36 import com.google.common.collect.ImmutableList; 37 import com.google.protobuf.ByteString; 38 import java.io.IOException; 39 import java.io.InputStream; 40 import java.util.concurrent.ExecutionException; 41 import java.util.concurrent.Executor; 42 import java.util.concurrent.Executors; 43 import org.junit.After; 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 48 @RunWith(AndroidJUnit4.class) 49 public final class InlineFileDownloaderTest { 50 51 private static final String FILE_NAME = "fileName"; 52 private static final FileSource FILE_CONTENT = 53 FileSource.ofByteString(ByteString.copyFromUtf8("TEST_CONTENT")); 54 private static final Executor DOWNLOAD_EXECUTOR = Executors.newScheduledThreadPool(2); 55 private static final Context CONTEXT = ApplicationProvider.getApplicationContext(); 56 57 private static final FakeFileBackend FAKE_FILE_BACKEND = 58 new FakeFileBackend(AndroidFileBackend.builder(CONTEXT).build()); 59 private static final SynchronousFileStorage FILE_STORAGE = 60 new SynchronousFileStorage( 61 /* backends= */ ImmutableList.of(FAKE_FILE_BACKEND), 62 /* transforms= */ ImmutableList.of(), 63 /* monitors= */ ImmutableList.of()); 64 65 private final Uri fileUri = 66 Uri.parse( 67 "android://com.google.android.libraries.mobiledatadownload.downloader.inline/files/datadownload/shared/public/" 68 + FILE_NAME); 69 70 private InlineFileDownloader inlineFileDownloader; 71 72 @Before setUp()73 public void setUp() { 74 inlineFileDownloader = new InlineFileDownloader(FILE_STORAGE, DOWNLOAD_EXECUTOR); 75 } 76 77 @After tearDown()78 public void tearDown() { 79 FAKE_FILE_BACKEND.clearFailure(OperationType.ALL); 80 } 81 82 @Test startDownloading_whenNonInlineFileSchemeGiven_fails()83 public void startDownloading_whenNonInlineFileSchemeGiven_fails() throws Exception { 84 DownloadRequest httpsDownloadRequest = 85 DownloadRequest.newBuilder() 86 .setUrlToDownload("https://url.to.download") 87 .setFileUri(fileUri) 88 .setDownloadConstraints(DownloadConstraints.NONE) 89 .build(); 90 91 ExecutionException ex = 92 assertThrows( 93 ExecutionException.class, 94 () -> inlineFileDownloader.startDownloading(httpsDownloadRequest).get()); 95 96 assertThat(ex).hasCauseThat().isInstanceOf(DownloadException.class); 97 DownloadException dex = (DownloadException) ex.getCause(); 98 assertThat(dex.getDownloadResultCode()) 99 .isEqualTo(DownloadResultCode.INVALID_INLINE_FILE_URL_SCHEME); 100 } 101 102 @Test startDownloading_whenCopyFails_fails()103 public void startDownloading_whenCopyFails_fails() throws Exception { 104 FAKE_FILE_BACKEND.setFailure(OperationType.WRITE_STREAM, new IOException("test exception")); 105 106 DownloadRequest downloadRequest = 107 DownloadRequest.newBuilder() 108 .setUrlToDownload("inlinefile:sha1:abc") 109 .setFileUri(fileUri) 110 .setInlineDownloadParamsOptional( 111 InlineDownloadParams.newBuilder().setInlineFileContent(FILE_CONTENT).build()) 112 .build(); 113 114 ExecutionException ex = 115 assertThrows( 116 ExecutionException.class, 117 () -> inlineFileDownloader.startDownloading(downloadRequest).get()); 118 119 assertThat(ex).hasCauseThat().isInstanceOf(DownloadException.class); 120 DownloadException dex = (DownloadException) ex.getCause(); 121 assertThat(dex.getDownloadResultCode()).isEqualTo(DownloadResultCode.INLINE_FILE_IO_ERROR); 122 } 123 124 @Test startDownloading_whenCopyCompletes_isSuccess()125 public void startDownloading_whenCopyCompletes_isSuccess() throws Exception { 126 DownloadRequest downloadRequest = 127 DownloadRequest.newBuilder() 128 .setUrlToDownload("inlinefile:sha1:abc") 129 .setFileUri(fileUri) 130 .setInlineDownloadParamsOptional( 131 InlineDownloadParams.newBuilder().setInlineFileContent(FILE_CONTENT).build()) 132 .build(); 133 134 inlineFileDownloader.startDownloading(downloadRequest).get(); 135 136 // Read file content back to check that it was copied from input 137 assertThat(FILE_STORAGE.exists(fileUri)).isTrue(); 138 InputStream copiedContentStream = FILE_STORAGE.open(fileUri, ReadStreamOpener.create()); 139 ByteString copiedContent = ByteString.readFrom(copiedContentStream); 140 copiedContentStream.close(); 141 assertThat(copiedContent).isEqualTo(FILE_CONTENT.byteString()); 142 } 143 } 144