Home | History | Annotate | Download | only in util
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <windows.h>
      6 
      7 #include "base/file_util.h"
      8 #include "base/files/scoped_temp_dir.h"
      9 #include "base/path_service.h"
     10 #include "chrome/common/chrome_paths.h"
     11 #include "chrome/installer/util/lzma_util.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 namespace {
     15 class LzmaUtilTest : public testing::Test {
     16  protected:
     17   virtual void SetUp() {
     18     ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
     19     data_dir_ = data_dir_.AppendASCII("installer");
     20     ASSERT_TRUE(base::PathExists(data_dir_));
     21 
     22     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     23   }
     24 
     25   base::ScopedTempDir temp_dir_;
     26 
     27   // The path to input data used in tests.
     28   base::FilePath data_dir_;
     29 };
     30 };
     31 
     32 // Test that we can open archives successfully.
     33 TEST_F(LzmaUtilTest, OpenArchiveTest) {
     34   base::FilePath archive = data_dir_.AppendASCII("archive1.7z");
     35   LzmaUtil lzma_util;
     36   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), NO_ERROR);
     37 
     38   // We allow opening another archive (which will automatically close the first
     39   // archive).
     40   archive = data_dir_.AppendASCII("archive2.7z");
     41   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), NO_ERROR);
     42 
     43   // Explicitly close and open the first archive again.
     44   lzma_util.CloseArchive();
     45   archive = data_dir_.AppendASCII("archive1.7z");
     46   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), NO_ERROR);
     47 
     48   // Make sure non-existent archive returns error.
     49   archive = data_dir_.AppendASCII("archive.non_existent.7z");
     50   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), ERROR_FILE_NOT_FOUND);
     51 }
     52 
     53 // Test that we can extract archives successfully.
     54 TEST_F(LzmaUtilTest, UnPackTest) {
     55   base::FilePath extract_dir(temp_dir_.path());
     56   extract_dir = extract_dir.AppendASCII("UnPackTest");
     57   ASSERT_FALSE(base::PathExists(extract_dir));
     58   EXPECT_TRUE(base::CreateDirectory(extract_dir));
     59   ASSERT_TRUE(base::PathExists(extract_dir));
     60 
     61   base::FilePath archive = data_dir_.AppendASCII("archive1.7z");
     62   LzmaUtil lzma_util;
     63   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), NO_ERROR);
     64   std::wstring unpacked_file;
     65   EXPECT_EQ(lzma_util.UnPack(extract_dir.value(), &unpacked_file),
     66             NO_ERROR);
     67   EXPECT_TRUE(base::PathExists(extract_dir.AppendASCII("a.exe")));
     68   EXPECT_TRUE(unpacked_file == extract_dir.AppendASCII("a.exe").value());
     69 
     70   archive = data_dir_.AppendASCII("archive2.7z");
     71   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), NO_ERROR);
     72   EXPECT_EQ(lzma_util.UnPack(extract_dir.value(), &unpacked_file),
     73             NO_ERROR);
     74   EXPECT_TRUE(base::PathExists(extract_dir.AppendASCII("b.exe")));
     75   EXPECT_TRUE(unpacked_file == extract_dir.AppendASCII("b.exe").value());
     76 
     77   lzma_util.CloseArchive();
     78   archive = data_dir_.AppendASCII("invalid_archive.7z");
     79   EXPECT_EQ(lzma_util.UnPack(extract_dir.value(), &unpacked_file),
     80             ERROR_INVALID_HANDLE);
     81   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), NO_ERROR);
     82   EXPECT_EQ(lzma_util.UnPack(extract_dir.value(), &unpacked_file),
     83             ERROR_INVALID_HANDLE);
     84 
     85   archive = data_dir_.AppendASCII("archive3.7z");
     86   EXPECT_EQ(lzma_util.OpenArchive(archive.value()), NO_ERROR);
     87   EXPECT_EQ(lzma_util.UnPack(extract_dir.value(), &unpacked_file),
     88             NO_ERROR);
     89   EXPECT_TRUE(base::PathExists(extract_dir.AppendASCII("archive\\a.exe")));
     90   EXPECT_TRUE(base::PathExists(
     91       extract_dir.AppendASCII("archive\\sub_dir\\text.txt")));
     92 }
     93 
     94 // Test the static method that can be used to unpack archives.
     95 TEST_F(LzmaUtilTest, UnPackArchiveTest) {
     96   base::FilePath extract_dir(temp_dir_.path());
     97   extract_dir = extract_dir.AppendASCII("UnPackArchiveTest");
     98   ASSERT_FALSE(base::PathExists(extract_dir));
     99   EXPECT_TRUE(base::CreateDirectory(extract_dir));
    100   ASSERT_TRUE(base::PathExists(extract_dir));
    101 
    102   base::FilePath archive = data_dir_.AppendASCII("archive1.7z");
    103   std::wstring unpacked_file;
    104   EXPECT_EQ(LzmaUtil::UnPackArchive(archive.value(), extract_dir.value(),
    105                                     &unpacked_file), NO_ERROR);
    106   EXPECT_TRUE(base::PathExists(extract_dir.AppendASCII("a.exe")));
    107   EXPECT_TRUE(unpacked_file == extract_dir.AppendASCII("a.exe").value());
    108 
    109   archive = data_dir_.AppendASCII("archive2.7z");
    110   EXPECT_EQ(LzmaUtil::UnPackArchive(archive.value(), extract_dir.value(),
    111                                     &unpacked_file), NO_ERROR);
    112   EXPECT_TRUE(base::PathExists(extract_dir.AppendASCII("b.exe")));
    113   EXPECT_TRUE(unpacked_file == extract_dir.AppendASCII("b.exe").value());
    114 
    115   archive = data_dir_.AppendASCII("invalid_archive.7z");
    116   EXPECT_NE(LzmaUtil::UnPackArchive(archive.value(), extract_dir.value(),
    117                                     &unpacked_file), NO_ERROR);
    118 }
    119