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 #include <zlib.h>
     23 #include <memory>
     24 
     25 #include "base/unix_file/fd_file.h"
     26 #include "common_runtime_test.h"
     27 #include "os.h"
     28 
     29 namespace art {
     30 
     31 class ZipArchiveTest : public CommonRuntimeTest {};
     32 
     33 TEST_F(ZipArchiveTest, FindAndExtract) {
     34   std::string error_msg;
     35   std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(GetLibCoreDexFileName().c_str(), &error_msg));
     36   ASSERT_TRUE(zip_archive.get() != nullptr) << error_msg;
     37   ASSERT_TRUE(error_msg.empty());
     38   std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("classes.dex", &error_msg));
     39   ASSERT_TRUE(zip_entry.get() != nullptr);
     40   ASSERT_TRUE(error_msg.empty());
     41 
     42   ScratchFile tmp;
     43   ASSERT_NE(-1, tmp.GetFd());
     44   std::unique_ptr<File> file(new File(tmp.GetFd(), tmp.GetFilename()));
     45   ASSERT_TRUE(file.get() != NULL);
     46   bool success = zip_entry->ExtractToFile(*file, &error_msg);
     47   ASSERT_TRUE(success) << error_msg;
     48   ASSERT_TRUE(error_msg.empty());
     49   file.reset(NULL);
     50 
     51   uint32_t computed_crc = crc32(0L, Z_NULL, 0);
     52   int fd = open(tmp.GetFilename().c_str(), O_RDONLY);
     53   ASSERT_NE(-1, fd);
     54   const size_t kBufSize = 32768;
     55   uint8_t buf[kBufSize];
     56   while (true) {
     57     ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buf, kBufSize));
     58     if (bytes_read == 0) {
     59       break;
     60     }
     61     computed_crc = crc32(computed_crc, buf, bytes_read);
     62   }
     63   EXPECT_EQ(zip_entry->GetCrc32(), computed_crc);
     64 }
     65 
     66 }  // namespace art
     67