mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-07-20 01:04:34 +08:00
Add tests
This commit is contained in:
1
OfficeUtils/tests/.gitignore
vendored
Normal file
1
OfficeUtils/tests/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
unzip
|
||||
@ -1,82 +1,241 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "../src/OfficeUtils.h"
|
||||
#include "../../DesktopEditor/common/File.h"
|
||||
#include "../../DesktopEditor/common/Directory.h"
|
||||
#include "../../DesktopEditor/xml/include/xmlutils.h"
|
||||
#include "../../DesktopEditor/common/StringBuilder.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
testing::AssertionResult IsFilesPathsEqual(std::vector<std::wstring> files1, std::vector<std::wstring> files2)
|
||||
{
|
||||
std::wstring sep;
|
||||
#ifdef WIN32
|
||||
sep = L"\\";
|
||||
#else
|
||||
sep = L"/";
|
||||
#endif // WIN32
|
||||
// sort
|
||||
std::sort(files1.begin(), files1.end());
|
||||
std::sort(files2.begin(), files2.end());
|
||||
|
||||
COfficeUtils utils;
|
||||
std::wstring curr_dir = NSFile::GetProcessDirectory();
|
||||
std::wstring output_dir = curr_dir + sep + L"output";
|
||||
std::wstring input_dir = curr_dir + sep + L"input";
|
||||
std::wstring temp_dir = curr_dir + sep + L"temp";
|
||||
// compare count
|
||||
if(files1.size() != files2.size())
|
||||
return testing::AssertionFailure() << "count of files is not equal "
|
||||
<< files1.size()
|
||||
<< " != "
|
||||
<< files2.size();
|
||||
|
||||
if(NSDirectory::Exists(input_dir))
|
||||
NSDirectory::DeleteDirectory(input_dir);
|
||||
NSDirectory::CreateDirectory(input_dir);
|
||||
|
||||
if(NSDirectory::Exists(output_dir))
|
||||
NSDirectory::DeleteDirectory(output_dir);
|
||||
NSDirectory::CreateDirectory(output_dir);
|
||||
|
||||
if(NSDirectory::Exists(temp_dir))
|
||||
NSDirectory::DeleteDirectory(temp_dir);
|
||||
NSDirectory::CreateDirectory(temp_dir);
|
||||
|
||||
std::vector<std::wstring> tests;
|
||||
tests.push_back(L"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890");
|
||||
tests.push_back(L"-_=+[{]};\',.`~!@#$%^&()!№;%()");
|
||||
tests.push_back(L"こんにちは世界 مرحبا بالعالم");
|
||||
tests.push_back(L"⌦ⵄ⬺▨⿐⸌⍒⿆⹖Ⅼ⌡┓ⓡ⣾⩬⍠⊆≆⳩≾⌮⢸⠐⩏„⨑☰⟬∬⤢⹛⮚∅✢⓴✎ℙ⥅ⅱ‟⥩⩲⺐⫒Ⱌⴳ⒎ⲧ⩷ₗ∸⬐⭹⓾✓⤓≪♽™⠒⌙➴⡣⼷⬖⓭⒱Ȿ⸪⼺⒣≕◼⾙‖");
|
||||
|
||||
#ifdef LINUX
|
||||
tests.push_back(L"\\\\hello\\world!\\\\");
|
||||
#endif // LINUX
|
||||
|
||||
std::wstring long_test;
|
||||
for(int i = 0; i < 100; i++)
|
||||
long_test += L"a";
|
||||
tests.push_back(long_test);
|
||||
|
||||
for(int i = 0; i < tests.size(); i++)
|
||||
// compare filenames
|
||||
for(size_t i = 0; i < files1.size(); ++i)
|
||||
{
|
||||
while(!NSDirectory::Exists(input_dir + sep + tests[i]))
|
||||
NSDirectory::CreateDirectories(input_dir + sep + tests[i]);
|
||||
std::wstring dir = input_dir + sep + tests[i];
|
||||
std::wstring filename1 = NSFile::GetFileName(files1[i]);
|
||||
std::wstring filename2 = NSFile::GetFileName(files2[i]);
|
||||
|
||||
std::wstring file_path = dir + sep + tests[i];
|
||||
std::wstring simple_file_path = dir + sep + L"file";
|
||||
|
||||
NSFile::CFileBinary file;
|
||||
file.CreateFile(file_path);
|
||||
file.WriteStringUTF8(file_path, true);
|
||||
file.CloseFile();
|
||||
|
||||
file.CreateFile(simple_file_path);
|
||||
file.WriteStringUTF8(simple_file_path, true);
|
||||
file.CloseFile();
|
||||
|
||||
utils.CompressFileOrDirectory(input_dir + sep + tests[i], temp_dir + sep + tests[i] + L".zip");
|
||||
if(filename1 != filename2)
|
||||
return testing::AssertionFailure() << U_TO_UTF8(filename2)
|
||||
<< " is not found";
|
||||
}
|
||||
|
||||
std::vector<std::wstring> files = NSDirectory::GetFiles(temp_dir);
|
||||
|
||||
for(auto &file : files)
|
||||
for(size_t i = 0; i < files1.size(); ++i)
|
||||
{
|
||||
std::wstring filename = NSFile::GetFileName(file);
|
||||
NSDirectory::CreateDirectory(output_dir + sep + filename);
|
||||
utils.ExtractToDirectory(file, output_dir + sep + filename, NULL, false);
|
||||
if(files1[i] != files2[i])
|
||||
return testing::AssertionFailure() << U_TO_UTF8(files1[i])
|
||||
<< " is not equal "
|
||||
<< U_TO_UTF8(files2[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
|
||||
class COfficeUtilsTest : public testing::Test
|
||||
{
|
||||
public:
|
||||
COfficeUtils utils;
|
||||
|
||||
std::wstring workDirectory;
|
||||
std::wstring zipDirectory;
|
||||
std::wstring unzipDirectory;
|
||||
std::wstring wsep;
|
||||
|
||||
virtual void SetUp() override
|
||||
{
|
||||
|
||||
#ifdef WIN32
|
||||
wsep = L"\\";
|
||||
#else
|
||||
wsep = L"/";
|
||||
#endif // WIN32
|
||||
workDirectory = NSFile::GetProcessDirectory();
|
||||
unzipDirectory = workDirectory + wsep + L".." + wsep + L"unzip";
|
||||
zipDirectory = workDirectory + wsep + L".." + wsep + L"zip";
|
||||
|
||||
if(!NSDirectory::Exists(unzipDirectory))
|
||||
NSDirectory::CreateDirectories(unzipDirectory);
|
||||
}
|
||||
|
||||
virtual void TearDown() override
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(COfficeUtilsTest, general_win)
|
||||
{
|
||||
std::wstring zip_filename = L"general_win";
|
||||
std::wstring zip_path = zipDirectory + wsep + zip_filename;
|
||||
std::wstring unzip_folder = unzipDirectory + wsep + zip_filename;
|
||||
|
||||
if(NSDirectory::Exists(unzip_folder))
|
||||
NSDirectory::DeleteDirectory(unzip_folder);
|
||||
|
||||
NSDirectory::CreateDirectories(unzip_folder);
|
||||
|
||||
HRESULT error_code = utils.ExtractToDirectory(zip_path, unzip_folder, NULL, false);
|
||||
ASSERT_EQ(error_code, S_OK);
|
||||
|
||||
std::vector<std::wstring> unzip_files = NSDirectory::GetFiles(unzip_folder, true);
|
||||
std::vector<std::wstring> actual;
|
||||
|
||||
for(size_t i = 0; i < unzip_files.size(); ++i)
|
||||
{
|
||||
size_t subpath_n = unzip_files[i].size() - unzip_folder.size() - 1;
|
||||
std::wstring subpath = unzip_files[i].substr(unzip_folder.size() + 1, subpath_n);
|
||||
actual.push_back(subpath);
|
||||
}
|
||||
|
||||
std::vector<std::wstring> expected;
|
||||
|
||||
expected.push_back(L"1.txt");
|
||||
expected.push_back(L"2.txt");
|
||||
expected.push_back(L"3.txt");
|
||||
|
||||
expected.push_back(L"こんにちは世界 مرحبا بالعالم" + wsep + L"こんにちは世界 مرحبا بالعالم");
|
||||
expected.push_back(L"こんにちは世界 مرحبا بالعالم" + wsep + L"こんにちは世界 مرحبا بالعالم123" + wsep + L"222.txt");
|
||||
|
||||
expected.push_back(L"НП" + wsep + L"НП" + wsep + L"NF" + wsep + L"new.txt");
|
||||
expected.push_back(L"MF" + wsep + L"file.txt");
|
||||
|
||||
expected.push_back(L"⌦ⵄ⬺▨⿐⸌⍒⿆⹖Ⅼ⌡┓ⓡ⣾⩬⍠⊆≆⳩≾⌮⢸⠐⩏„⨑☰⟬∬⤢"
|
||||
L"⹛⮚∅✢⓴✎ℙ⥅ⅱ‟⥩⩲⺐⫒Ⱌⴳ⒎ⲧ⩷ₗ∸⬐⭹⓾✓⤓≪♽™⠒⌙➴"
|
||||
L"⡣⼷⬖⓭⒱Ȿ⸪⼺⒣≕◼⾙‖" + wsep + L"⌦ⵄ⬺▨⿐⸌⍒⿆⹖Ⅼ⌡┓");
|
||||
|
||||
expected.push_back(L"-_=+[{]};',.`~!@#$%^&()!№;%()" + wsep + L"-_=+[{]};',.`~!@#$%^&()!№;%()");
|
||||
expected.push_back(L"-_=+[{]};',.`~!@#$%^&()!№;%()" + wsep + L"こんにちは世界 مرحبا بالعالم");
|
||||
|
||||
EXPECT_TRUE(IsFilesPathsEqual(actual, expected));
|
||||
}
|
||||
|
||||
TEST_F(COfficeUtilsTest, general_win_no_folder)
|
||||
{
|
||||
std::wstring zip_filename = L"general_win";
|
||||
std::wstring zip_path = zipDirectory + wsep + zip_filename;
|
||||
std::wstring unzip_folder = unzipDirectory + wsep + L"general_win_no_folder";
|
||||
|
||||
if(NSDirectory::Exists(unzip_folder))
|
||||
NSDirectory::DeleteDirectory(unzip_folder);
|
||||
|
||||
NSDirectory::CreateDirectories(unzip_folder);
|
||||
|
||||
HRESULT error_code = utils.ExtractToDirectory(zip_path, unzip_folder, NULL, true);
|
||||
ASSERT_EQ(error_code, S_OK);
|
||||
|
||||
std::vector<std::wstring> unzip_files = NSDirectory::GetFiles(unzip_folder, true);
|
||||
std::vector<std::wstring> actual;
|
||||
|
||||
for(size_t i = 0; i < unzip_files.size(); ++i)
|
||||
{
|
||||
size_t subpath_n = unzip_files[i].size() - unzip_folder.size() - 1;
|
||||
std::wstring subpath = unzip_files[i].substr(unzip_folder.size() + 1, subpath_n);
|
||||
actual.push_back(subpath);
|
||||
}
|
||||
|
||||
std::vector<std::wstring> expected;
|
||||
|
||||
expected.push_back(L"1.txt");
|
||||
expected.push_back(L"2.txt");
|
||||
expected.push_back(L"3.txt");
|
||||
|
||||
expected.push_back(L"こんにちは世界 مرحبا بالعالم");
|
||||
expected.push_back(L"222.txt");
|
||||
|
||||
expected.push_back(L"new.txt");
|
||||
expected.push_back(L"file.txt");
|
||||
|
||||
expected.push_back(L"⌦ⵄ⬺▨⿐⸌⍒⿆⹖Ⅼ⌡┓");
|
||||
expected.push_back(L"-_=+[{]};',.`~!@#$%^&()!№;%()");
|
||||
|
||||
EXPECT_TRUE(IsFilesPathsEqual(actual, expected));
|
||||
}
|
||||
|
||||
TEST_F(COfficeUtilsTest, deep_win)
|
||||
{
|
||||
std::wstring zip_filename = L"deep_win";
|
||||
std::wstring zip_path = zipDirectory + wsep + zip_filename;
|
||||
std::wstring unzip_folder = unzipDirectory + wsep + zip_filename;
|
||||
|
||||
if(NSDirectory::Exists(unzip_folder))
|
||||
NSDirectory::DeleteDirectory(unzip_folder);
|
||||
|
||||
NSDirectory::CreateDirectories(unzip_folder);
|
||||
|
||||
HRESULT error_code = utils.ExtractToDirectory(zip_path, unzip_folder, NULL, false);
|
||||
ASSERT_EQ(error_code, S_OK);
|
||||
|
||||
std::vector<std::wstring> unzip_files = NSDirectory::GetFiles(unzip_folder, true);
|
||||
std::vector<std::wstring> actual;
|
||||
|
||||
for(size_t i = 0; i < unzip_files.size(); ++i)
|
||||
{
|
||||
size_t subpath_n = unzip_files[i].size() - unzip_folder.size() - 1;
|
||||
std::wstring subpath = unzip_files[i].substr(unzip_folder.size() + 1, subpath_n);
|
||||
actual.push_back(subpath);
|
||||
}
|
||||
|
||||
std::vector<std::wstring> expected;
|
||||
|
||||
expected.push_back(L"1" + wsep + L"2" + wsep + L"3" + wsep + L"4" + wsep + L"hi.txt");
|
||||
expected.push_back(L"1" + wsep + L"2" + wsep + L"3" + wsep + L"4" + wsep + L"5" + wsep +
|
||||
L"6" + wsep + L"7" + wsep + L"8" + wsep + L"9" + wsep + L"10" + wsep + L"hi.txt");
|
||||
|
||||
EXPECT_TRUE(IsFilesPathsEqual(actual, expected));
|
||||
}
|
||||
|
||||
TEST_F(COfficeUtilsTest, docx_like_win)
|
||||
{
|
||||
std::wstring zip_filename = L"docx_like_win";
|
||||
std::wstring zip_path = zipDirectory + wsep + zip_filename;
|
||||
std::wstring unzip_folder = unzipDirectory + wsep + zip_filename;
|
||||
|
||||
if(NSDirectory::Exists(unzip_folder))
|
||||
NSDirectory::DeleteDirectory(unzip_folder);
|
||||
|
||||
NSDirectory::CreateDirectories(unzip_folder);
|
||||
|
||||
HRESULT error_code = utils.ExtractToDirectory(zip_path, unzip_folder, NULL, false);
|
||||
ASSERT_EQ(error_code, S_OK);
|
||||
|
||||
std::vector<std::wstring> unzip_files = NSDirectory::GetFiles(unzip_folder, true);
|
||||
std::vector<std::wstring> actual;
|
||||
|
||||
for(size_t i = 0; i < unzip_files.size(); ++i)
|
||||
{
|
||||
size_t subpath_n = unzip_files[i].size() - unzip_folder.size() - 1;
|
||||
std::wstring subpath = unzip_files[i].substr(unzip_folder.size() + 1, subpath_n);
|
||||
actual.push_back(subpath);
|
||||
}
|
||||
|
||||
std::vector<std::wstring> expected;
|
||||
|
||||
expected.push_back(L"[Content_Types].xml");
|
||||
expected.push_back(L"_rels" + wsep + L".rels");
|
||||
|
||||
expected.push_back(L"docProps" + wsep + L"app.xml");
|
||||
expected.push_back(L"docProps" + wsep + L"core.xml");
|
||||
|
||||
expected.push_back(L"word" + wsep + L"document.xml");
|
||||
expected.push_back(L"word" + wsep + L"endnotes.xml");
|
||||
expected.push_back(L"word" + wsep + L"fontTable.xml");
|
||||
expected.push_back(L"word" + wsep + L"footnotes.xml");
|
||||
expected.push_back(L"word" + wsep + L"settings.xml");
|
||||
expected.push_back(L"word" + wsep + L"webSettings.xml");
|
||||
expected.push_back(L"word" + wsep + L"theme" + wsep + L"theme1.xml");
|
||||
|
||||
EXPECT_TRUE(IsFilesPathsEqual(actual, expected));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -12,13 +12,13 @@ $$PWD/../../Common/OfficeFileFormatChecker2.cpp \
|
||||
|
||||
SOURCES += main.cpp
|
||||
|
||||
|
||||
CORE_ROOT_DIR = $$PWD/../../
|
||||
PWD_ROOT_DIR = $$PWD
|
||||
|
||||
include($$CORE_ROOT_DIR/Common/base.pri)
|
||||
include($$CORE_ROOT_DIR/Common/3dParty/icu/icu.pri)
|
||||
include($$CORE_ROOT_DIR/Common/3dParty/googletest/googletest.pri)
|
||||
|
||||
ADD_DEPENDENCY(kernel, graphics, UnicodeConverter)
|
||||
ADD_DEPENDENCY(kernel, UnicodeConverter)
|
||||
|
||||
DESTDIR = $$PWD/build
|
||||
|
||||
BIN
OfficeUtils/tests/zip/deep_win
Normal file
BIN
OfficeUtils/tests/zip/deep_win
Normal file
Binary file not shown.
BIN
OfficeUtils/tests/zip/docx_like_win
Normal file
BIN
OfficeUtils/tests/zip/docx_like_win
Normal file
Binary file not shown.
BIN
OfficeUtils/tests/zip/general_win
Normal file
BIN
OfficeUtils/tests/zip/general_win
Normal file
Binary file not shown.
Reference in New Issue
Block a user