summaryrefslogtreecommitdiffstats
path: root/runtime/zip_archive_test.cc
blob: c43fee55f246367f72dd79b55438ea2f865f8c6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zip_archive.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <zlib.h>

#include "UniquePtr.h"
#include "common_runtime_test.h"
#include "os.h"

namespace art {

class ZipArchiveTest : public CommonRuntimeTest {};

TEST_F(ZipArchiveTest, FindAndExtract) {
  std::string error_msg;
  UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(GetLibCoreDexFileName().c_str(), &error_msg));
  ASSERT_TRUE(zip_archive.get() != nullptr) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  UniquePtr<ZipEntry> zip_entry(zip_archive->Find("classes.dex", &error_msg));
  ASSERT_TRUE(zip_entry.get() != nullptr);
  ASSERT_TRUE(error_msg.empty());

  ScratchFile tmp;
  ASSERT_NE(-1, tmp.GetFd());
  UniquePtr<File> file(new File(tmp.GetFd(), tmp.GetFilename()));
  ASSERT_TRUE(file.get() != NULL);
  bool success = zip_entry->ExtractToFile(*file, &error_msg);
  ASSERT_TRUE(success) << error_msg;
  ASSERT_TRUE(error_msg.empty());
  file.reset(NULL);

  uint32_t computed_crc = crc32(0L, Z_NULL, 0);
  int fd = open(tmp.GetFilename().c_str(), O_RDONLY);
  ASSERT_NE(-1, fd);
  const size_t kBufSize = 32768;
  uint8_t buf[kBufSize];
  while (true) {
    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buf, kBufSize));
    if (bytes_read == 0) {
      break;
    }
    computed_crc = crc32(computed_crc, buf, bytes_read);
  }
  EXPECT_EQ(zip_entry->GetCrc32(), computed_crc);
}

}  // namespace art