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