Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2011 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 #include "zip_archive.h"
     18 
     19 #include <fcntl.h>
     20 #include <sys/stat.h>
     21 #include <sys/types.h>
     22 
     23 #include "UniquePtr.h"
     24 #include "common_test.h"
     25 #include "os.h"
     26 
     27 namespace art {
     28 
     29 class ZipArchiveTest : public CommonTest {};
     30 
     31 TEST_F(ZipArchiveTest, FindAndExtract) {
     32   UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(GetLibCoreDexFileName()));
     33   ASSERT_TRUE(zip_archive.get() != false);
     34   UniquePtr<ZipEntry> zip_entry(zip_archive->Find("classes.dex"));
     35   ASSERT_TRUE(zip_entry.get() != false);
     36 
     37   ScratchFile tmp;
     38   ASSERT_NE(-1, tmp.GetFd());
     39   UniquePtr<File> file(new File(tmp.GetFd(), tmp.GetFilename()));
     40   ASSERT_TRUE(file.get() != NULL);
     41   bool success = zip_entry->ExtractToFile(*file);
     42   ASSERT_TRUE(success);
     43   file.reset(NULL);
     44 
     45   uint32_t computed_crc = crc32(0L, Z_NULL, 0);
     46   int fd = open(tmp.GetFilename().c_str(), O_RDONLY);
     47   ASSERT_NE(-1, fd);
     48   const size_t kBufSize = 32768;
     49   uint8_t buf[kBufSize];
     50   while (true) {
     51     ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buf, kBufSize));
     52     if (bytes_read == 0) {
     53       break;
     54     }
     55     computed_crc = crc32(computed_crc, buf, bytes_read);
     56   }
     57   EXPECT_EQ(zip_entry->GetCrc32(), computed_crc);
     58 }
     59 
     60 }  // namespace art
     61