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.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 com.google.android.apps.common.testing.util.AndroidTestUtil; 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.testing.FileDescriptorLeakChecker; 28 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri; 29 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform; 30 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos; 31 import com.google.common.collect.ImmutableList; 32 import com.google.common.io.ByteStreams; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import java.util.concurrent.Callable; 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 43 @RunWith(JUnit4.class) 44 public final class SystemLibraryOpenerAndroidTest { 45 46 private static final String SO_DIR = 47 "third_party/java_src/android_libs/mobiledatadownload/javatests/com/google/android/libraries/mobiledatadownload/file/openers/"; 48 private static final String HELLO1NATIVE_SO = SO_DIR + "libhello1native.so"; 49 private static final String HELLO2NATIVE_SO = SO_DIR + "libhello2native.so"; 50 private final Context context = ApplicationProvider.getApplicationContext(); 51 private SynchronousFileStorage storage; 52 53 @Rule public TemporaryUri tmpUri = new TemporaryUri(); 54 @Rule public FileDescriptorLeakChecker leakChecker = new FileDescriptorLeakChecker(); 55 56 @Before initializeStorage()57 public void initializeStorage() throws Exception { 58 storage = 59 new SynchronousFileStorage( 60 ImmutableList.of(new JavaFileBackend()), ImmutableList.of(new CompressTransform())); 61 } 62 63 @Test openFromFileWithoutCache_shouldLoad()64 public void openFromFileWithoutCache_shouldLoad() throws Exception { 65 Uri uri = tmpUri.newUri(); 66 try (InputStream in = 67 AndroidTestUtil.getTestDataInputStream(context.getContentResolver(), HELLO1NATIVE_SO); 68 OutputStream out = storage.open(uri, WriteStreamOpener.create())) { 69 ByteStreams.copy(in, out); 70 storage.open(uri, SystemLibraryOpener.create()); 71 assertThat(HelloNative.sayHello()).isEqualTo("hello1"); 72 } 73 } 74 75 @Test openCompressedFromFileWithoutCache_shouldFail()76 public void openCompressedFromFileWithoutCache_shouldFail() throws Exception { 77 Uri uri = tmpUri.newUriBuilder().withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC).build(); 78 try (InputStream in = 79 AndroidTestUtil.getTestDataInputStream(context.getContentResolver(), HELLO1NATIVE_SO); 80 OutputStream out = storage.open(uri, WriteStreamOpener.create())) { 81 ByteStreams.copy(in, out); 82 } 83 assertThrows(IOException.class, () -> storage.open(uri, SystemLibraryOpener.create())); 84 } 85 86 @Test openCompressedFromFileWithCache_shouldSucceed()87 public void openCompressedFromFileWithCache_shouldSucceed() throws Exception { 88 Uri uri = tmpUri.newUriBuilder().withTransform(TransformProtos.DEFAULT_COMPRESS_SPEC).build(); 89 try (InputStream in = 90 AndroidTestUtil.getTestDataInputStream(context.getContentResolver(), HELLO2NATIVE_SO); 91 OutputStream out = storage.open(uri, WriteStreamOpener.create())) { 92 ByteStreams.copy(in, out); 93 } 94 Uri cacheDir = tmpUri.getRootUri(); 95 96 Callable<Integer> countLibs = 97 () -> { 98 int numLibs = 0; 99 for (Uri child : storage.children(cacheDir)) { 100 if (child.getPath().endsWith(".so")) { 101 numLibs++; 102 } 103 } 104 return numLibs; 105 }; 106 assertThat(countLibs.call()).isEqualTo(0); // Initially, no libraries 107 108 storage.open(uri, SystemLibraryOpener.create().withCacheDirectory(cacheDir)); 109 assertThat(HelloNative.sayHello()).isEqualTo("hello2"); 110 assertThat(countLibs.call()).isEqualTo(1); // Now we see the one library. 111 112 storage.open(uri, SystemLibraryOpener.create().withCacheDirectory(cacheDir)); 113 assertThat(countLibs.call()).isEqualTo(1); // The one library is found again. 114 } 115 } 116