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/files/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"
16 class LzmaUtilTest
: public testing::Test
{
18 void SetUp() override
{
19 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &data_dir_
));
20 data_dir_
= data_dir_
.AppendASCII("installer");
21 ASSERT_TRUE(base::PathExists(data_dir_
));
23 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
26 base::ScopedTempDir temp_dir_
;
28 // The path to input data used in tests.
29 base::FilePath data_dir_
;
34 // Test that we can open archives successfully.
35 TEST_F(LzmaUtilTest
, OpenArchiveTest
) {
36 base::FilePath archive
= data_dir_
.AppendASCII("archive1.7z");
38 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
40 // We allow opening another archive (which will automatically close the first
42 archive
= data_dir_
.AppendASCII("archive2.7z");
43 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
45 // Explicitly close and open the first archive again.
46 lzma_util
.CloseArchive();
47 archive
= data_dir_
.AppendASCII("archive1.7z");
48 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
50 // Make sure non-existent archive returns error.
51 archive
= data_dir_
.AppendASCII("archive.non_existent.7z");
52 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), ERROR_FILE_NOT_FOUND
);
55 // Test that we can extract archives successfully.
56 TEST_F(LzmaUtilTest
, UnPackTest
) {
57 base::FilePath
extract_dir(temp_dir_
.path());
58 extract_dir
= extract_dir
.AppendASCII("UnPackTest");
59 ASSERT_FALSE(base::PathExists(extract_dir
));
60 EXPECT_TRUE(base::CreateDirectory(extract_dir
));
61 ASSERT_TRUE(base::PathExists(extract_dir
));
63 base::FilePath archive
= data_dir_
.AppendASCII("archive1.7z");
65 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
66 std::wstring unpacked_file
;
67 EXPECT_EQ(lzma_util
.UnPack(extract_dir
.value(), &unpacked_file
),
69 EXPECT_TRUE(base::PathExists(extract_dir
.AppendASCII("a.exe")));
70 EXPECT_TRUE(unpacked_file
== extract_dir
.AppendASCII("a.exe").value());
72 archive
= data_dir_
.AppendASCII("archive2.7z");
73 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
74 EXPECT_EQ(lzma_util
.UnPack(extract_dir
.value(), &unpacked_file
),
76 EXPECT_TRUE(base::PathExists(extract_dir
.AppendASCII("b.exe")));
77 EXPECT_TRUE(unpacked_file
== extract_dir
.AppendASCII("b.exe").value());
79 lzma_util
.CloseArchive();
80 archive
= data_dir_
.AppendASCII("invalid_archive.7z");
81 EXPECT_EQ(lzma_util
.UnPack(extract_dir
.value(), &unpacked_file
),
82 ERROR_INVALID_HANDLE
);
83 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
84 EXPECT_EQ(lzma_util
.UnPack(extract_dir
.value(), &unpacked_file
),
85 ERROR_INVALID_HANDLE
);
87 archive
= data_dir_
.AppendASCII("archive3.7z");
88 EXPECT_EQ(lzma_util
.OpenArchive(archive
.value()), NO_ERROR
);
89 EXPECT_EQ(lzma_util
.UnPack(extract_dir
.value(), &unpacked_file
),
91 EXPECT_TRUE(base::PathExists(extract_dir
.AppendASCII("archive\\a.exe")));
92 EXPECT_TRUE(base::PathExists(
93 extract_dir
.AppendASCII("archive\\sub_dir\\text.txt")));
96 // Test the static method that can be used to unpack archives.
97 TEST_F(LzmaUtilTest
, UnPackArchiveTest
) {
98 base::FilePath
extract_dir(temp_dir_
.path());
99 extract_dir
= extract_dir
.AppendASCII("UnPackArchiveTest");
100 ASSERT_FALSE(base::PathExists(extract_dir
));
101 EXPECT_TRUE(base::CreateDirectory(extract_dir
));
102 ASSERT_TRUE(base::PathExists(extract_dir
));
104 base::FilePath archive
= data_dir_
.AppendASCII("archive1.7z");
105 std::wstring unpacked_file
;
106 EXPECT_EQ(LzmaUtil::UnPackArchive(archive
.value(), extract_dir
.value(),
107 &unpacked_file
), NO_ERROR
);
108 EXPECT_TRUE(base::PathExists(extract_dir
.AppendASCII("a.exe")));
109 EXPECT_TRUE(unpacked_file
== extract_dir
.AppendASCII("a.exe").value());
111 archive
= data_dir_
.AppendASCII("archive2.7z");
112 EXPECT_EQ(LzmaUtil::UnPackArchive(archive
.value(), extract_dir
.value(),
113 &unpacked_file
), NO_ERROR
);
114 EXPECT_TRUE(base::PathExists(extract_dir
.AppendASCII("b.exe")));
115 EXPECT_TRUE(unpacked_file
== extract_dir
.AppendASCII("b.exe").value());
117 archive
= data_dir_
.AppendASCII("invalid_archive.7z");
118 EXPECT_NE(LzmaUtil::UnPackArchive(archive
.value(), extract_dir
.value(),
119 &unpacked_file
), NO_ERROR
);