1 /* 2 * Copyright (C) 2015 The Android Open Source Project 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 17 package com.android.mtp; 18 19 import android.os.ParcelFileDescriptor; 20 import android.test.AndroidTestCase; 21 22 import androidx.test.filters.MediumTest; 23 24 import java.io.IOException; 25 import java.util.concurrent.ExecutorService; 26 import java.util.concurrent.Executors; 27 import java.util.concurrent.TimeUnit; 28 29 @MediumTest 30 public class PipeManagerTest extends AndroidTestCase { 31 private static final byte[] HELLO_BYTES = new byte[] { 'h', 'e', 'l', 'l', 'o' }; 32 33 private TestMtpManager mtpManager; 34 private ExecutorService mExecutor; 35 private PipeManager mPipeManager; 36 private MtpDatabase mDatabase; 37 38 @Override setUp()39 public void setUp() { 40 mtpManager = new TestMtpManager(getContext()); 41 mExecutor = Executors.newSingleThreadExecutor(); 42 mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY); 43 mPipeManager = new PipeManager(mDatabase, mExecutor); 44 } 45 46 @Override tearDown()47 protected void tearDown() throws Exception { 48 assertTrue(mPipeManager.close()); 49 mDatabase.close(); 50 } 51 testReadDocument_basic()52 public void testReadDocument_basic() throws Exception { 53 mtpManager.setImportFileBytes(0, 1, HELLO_BYTES); 54 final ParcelFileDescriptor descriptor = mPipeManager.readDocument( 55 mtpManager, 56 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); 57 assertDescriptor(descriptor, HELLO_BYTES); 58 } 59 testReadDocument_error()60 public void testReadDocument_error() throws Exception { 61 final ParcelFileDescriptor descriptor = mPipeManager.readDocument( 62 mtpManager, 63 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); 64 assertDescriptorError(descriptor); 65 } 66 testReadThumbnail_basic()67 public void testReadThumbnail_basic() throws Exception { 68 mtpManager.setThumbnail(0, 1, HELLO_BYTES); 69 final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail( 70 mtpManager, 71 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); 72 assertDescriptor(descriptor, HELLO_BYTES); 73 } 74 testReadThumbnail_error()75 public void testReadThumbnail_error() throws Exception { 76 final ParcelFileDescriptor descriptor = mPipeManager.readThumbnail( 77 mtpManager, 78 new Identifier(0, 0, 1, null, MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT)); 79 assertDescriptorError(descriptor); 80 } 81 assertDescriptor(ParcelFileDescriptor descriptor, byte[] expectedBytes)82 private void assertDescriptor(ParcelFileDescriptor descriptor, byte[] expectedBytes) 83 throws IOException, InterruptedException { 84 mExecutor.shutdown(); 85 assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); 86 try (final ParcelFileDescriptor.AutoCloseInputStream stream = 87 new ParcelFileDescriptor.AutoCloseInputStream(descriptor)) { 88 byte[] results = new byte[100]; 89 assertEquals(expectedBytes.length, stream.read(results)); 90 for (int i = 0; i < expectedBytes.length; i++) { 91 assertEquals(expectedBytes[i], results[i]); 92 } 93 } 94 } 95 assertDescriptorError(ParcelFileDescriptor descriptor)96 private void assertDescriptorError(ParcelFileDescriptor descriptor) 97 throws InterruptedException { 98 mExecutor.shutdown(); 99 assertTrue(mExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS)); 100 try { 101 descriptor.checkError(); 102 fail(); 103 } catch (Throwable error) { 104 assertTrue(error instanceof IOException); 105 } 106 } 107 } 108