Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 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.apksig.util;
     18 
     19 import com.android.apksig.util.DataSourceFromRAFTest.TmpFileCloseable;
     20 import java.io.File;
     21 import java.io.IOException;
     22 import java.io.RandomAccessFile;
     23 import java.nio.charset.StandardCharsets;
     24 import java.nio.file.Files;
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 import org.junit.runners.JUnit4;
     28 
     29 /**
     30  * Tests for the {@link DataSource} returned by
     31  * {@link DataSources#asDataSource(RandomAccessFile, long, long)}.
     32  */
     33 @RunWith(JUnit4.class)
     34 public class DataSourceFromRAFChunkTest extends DataSourceTestBase {
     35 
     36     @Test
     37     public void testFileSizeChangesNotVisible() throws Exception {
     38         try (CloseableWithDataSource c = createDataSource("abcdefg")) {
     39             DataSource ds = c.getDataSource();
     40             DataSource slice = ds.slice(3, 2);
     41             File f = ((TmpFileCloseable) c.getCloseable()).getFile();
     42             assertGetByteBufferEquals("abcdefg", ds, 0, (int) ds.size());
     43             assertGetByteBufferEquals("de", slice, 0, (int) slice.size());
     44             assertFeedEquals("cdefg", ds, 2, 5);
     45             assertFeedEquals("e", slice, 1, 1);
     46             assertCopyToEquals("cdefg", ds, 2, 5);
     47             assertCopyToEquals("e", slice, 1, 1);
     48             assertSliceEquals("cdefg", ds, 2, 5);
     49             assertSliceEquals("e", slice, 1, 1);
     50             try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
     51                 raf.seek(raf.length());
     52                 raf.write("hijkl".getBytes(StandardCharsets.UTF_8));
     53             }
     54 
     55             assertGetByteBufferEquals("abcdefg", ds, 0, (int) ds.size());
     56             assertGetByteBufferEquals("de", slice, 0, (int) slice.size());
     57             assertGetByteBufferThrowsIOOB(ds, 0, (int) ds.size() + 3);
     58             assertFeedThrowsIOOB(ds, 0, (int) ds.size() + 3);
     59             assertSliceThrowsIOOB(ds, 0, (int) ds.size() + 3);
     60             assertCopyToThrowsIOOB(ds, 0, (int) ds.size() + 3);
     61         }
     62     }
     63 
     64     @Override
     65     protected CloseableWithDataSource createDataSource(byte[] contents) throws IOException {
     66         // "01" | contents | "9"
     67         byte[] fullContents = new byte[2 + contents.length + 1];
     68         fullContents[0] = '0';
     69         fullContents[1] = '1';
     70         System.arraycopy(contents, 0, fullContents, 2, contents.length);
     71         fullContents[fullContents.length - 1] = '9';
     72 
     73         File tmp = File.createTempFile(DataSourceFromRAFChunkTest.class.getSimpleName(), ".bin");
     74         RandomAccessFile f = null;
     75         try {
     76             Files.write(tmp.toPath(), fullContents);
     77             f = new RandomAccessFile(tmp, "r");
     78         } finally {
     79             if (f == null) {
     80                 tmp.delete();
     81             }
     82         }
     83 
     84         return CloseableWithDataSource.of(
     85                 DataSources.asDataSource(f, 2, contents.length),
     86                 new DataSourceFromRAFTest.TmpFileCloseable(tmp, f));
     87     }
     88 }
     89