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 20 import android.content.Context; 21 import android.net.Uri; 22 import android.system.Os; 23 import androidx.test.core.app.ApplicationProvider; 24 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 25 import com.google.android.libraries.mobiledatadownload.file.backends.AndroidFileBackend; 26 import com.google.android.libraries.mobiledatadownload.file.backends.AndroidUriAdapter; 27 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryAndroidUri; 28 import java.util.Arrays; 29 import org.junit.Rule; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 @RunWith(JUnit4.class) 35 public class RecursiveDeleteOpenerAndroidTest { 36 @Rule 37 public TemporaryAndroidUri tmpAndroidUri = 38 new TemporaryAndroidUri(ApplicationProvider.getApplicationContext()); 39 40 private final SynchronousFileStorage storage = 41 new SynchronousFileStorage( 42 Arrays.asList( 43 AndroidFileBackend.builder(ApplicationProvider.getApplicationContext()).build())); 44 45 @Test open_notFollowingSymlink()46 public void open_notFollowingSymlink() throws Exception { 47 Context context = ApplicationProvider.getApplicationContext(); 48 SynchronousFileStorage storage = 49 new SynchronousFileStorage(Arrays.asList(AndroidFileBackend.builder(context).build())); 50 51 Uri rootDir = tmpAndroidUri.newDirectoryUri(); 52 Uri dir = Uri.withAppendedPath(rootDir, "dir"); 53 Uri file0 = Uri.withAppendedPath(dir, "a"); 54 assertThat(storage.open(file0, WriteStringOpener.create("junk"))).isNull(); 55 Uri linkDir = Uri.withAppendedPath(rootDir, "link"); 56 AndroidUriAdapter adapter = AndroidUriAdapter.forContext(context); 57 Os.symlink(adapter.toFile(dir).getAbsolutePath(), adapter.toFile(linkDir).getAbsolutePath()); 58 Uri fileInLinkDir = Uri.withAppendedPath(linkDir, "a"); 59 60 assertThat(storage.exists(fileInLinkDir)).isTrue(); 61 62 assertThat(storage.open(linkDir, RecursiveDeleteOpener.create().withNoFollowLinks())).isNull(); 63 64 assertThat(storage.exists(file0)).isTrue(); 65 assertThat(storage.exists(linkDir)).isFalse(); 66 assertThat(storage.exists(fileInLinkDir)).isFalse(); 67 } 68 } 69