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.
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"
15 class LzmaUtilTest
: public testing::Test
{
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_
));
22 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
25 base::ScopedTempDir temp_dir_
;
27 // The path to input data used in tests.
28 base::FilePath data_dir_
;
32 // Test that we can open archives successfully.
33 TEST_F(LzmaUtilTest
, OpenArchiveTest
) {
34 base::FilePath archive
= data_dir_
.AppendASCII("archive1.7z");
36 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
38 // We allow opening another archive (which will automatically close the first
40 archive
= data_dir_
.AppendASCII("archive2.7z");
41 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
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
);
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
);
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
));
61 base::FilePath archive
= data_dir_
.AppendASCII("archive1.7z");
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
),
67 EXPECT_TRUE(base::PathExists(extract_dir
.AppendASCII("a.exe")));
68 EXPECT_TRUE(unpacked_file
== extract_dir
.AppendASCII("a.exe").value());
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
),
74 EXPECT_TRUE(base::PathExists(extract_dir
.AppendASCII("b.exe")));
75 EXPECT_TRUE(unpacked_file
== extract_dir
.AppendASCII("b.exe").value());
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
);
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
),
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")));
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
));
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());
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());
115 archive
= data_dir_
.AppendASCII("invalid_archive.7z");
116 EXPECT_NE(LzmaUtil::UnPackArchive(archive
.value(), extract_dir
.value(),
117 &unpacked_file
), NO_ERROR
);