• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
25 import com.google.android.libraries.mobiledatadownload.file.backends.FileDescriptorUri;
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.common.collect.ImmutableList;
30 import com.google.common.io.CharStreams;
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.Reader;
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 NativeReadOpenerAndroidTest {
45 
46   private SynchronousFileStorage storage;
47 
48   @Rule public FileDescriptorLeakChecker leakChecker = new FileDescriptorLeakChecker();
49   @Rule public TemporaryUri tmpUri = new TemporaryUri();
50 
51   @Before
initStorage()52   public void initStorage() throws Exception {
53     storage = new SynchronousFileStorage(ImmutableList.of(new JavaFileBackend()));
54   }
55 
56   @Test
openForNativeRead_returnsAWorkingFileDescriptor()57   public void openForNativeRead_returnsAWorkingFileDescriptor() throws Exception {
58     Uri uri = tmpUri.newUri();
59     createFile(storage, uri, "content");
60     CloseableUri result = storage.open(uri, NativeReadOpener.create());
61 
62     assertThat(result.uri().getScheme()).isEqualTo("fd");
63     assertThat(FileDescriptorUri.getFd(result.uri())).isGreaterThan(0);
64 
65     // Use proc filesystem to verify data.
66     File fdFile = new File("/proc/self/fd/" + result.uri().getSchemeSpecificPart());
67     InputStream in = new FileInputStream(fdFile);
68     Reader reader = new InputStreamReader(in, UTF_8);
69     assertThat(CharStreams.toString(reader)).isEqualTo("content");
70 
71     // Verify closing behavior.
72     assertThat(fdFile.exists()).isTrue();
73     reader.close(); // This won't actually close the fd.
74     assertThat(fdFile.exists()).isTrue();
75     result.close(); // This does.
76     assertThat(fdFile.exists()).isFalse();
77   }
78 
79   @Test
openForNativeRead_withMissingFile_throwsFileNotFound()80   public void openForNativeRead_withMissingFile_throwsFileNotFound() throws Exception {
81     Uri uri = Uri.parse("file:/does-not-exist");
82     assertThrows(FileNotFoundException.class, () -> storage.open(uri, NativeReadOpener.create()));
83   }
84 }
85