Files
core/Common/cfcpp/test/tst_compondfile.h
Alexander Trofimov 76ee07f61c [copyright] Update copyright header
Co-authored-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com>
Co-committed-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com>
2026-05-14 08:23:56 +00:00

228 lines
6.4 KiB
C++
Executable File

/*
* Copyright (C) Ascensio System SIA, 2009-2026
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation, together with the
* additional terms provided in the LICENSE file.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA by email at info@onlyoffice.com
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
* LV-1050, Latvia, European Union.
*
* The interactive user interfaces in modified versions of the Program
* are required to display Appropriate Legal Notices in accordance with
* Section 5 of the GNU AGPL version 3.
*
* No trademark rights are granted under this License.
*
* All non-code elements of the Product, including illustrations,
* icon sets, and technical writing content, are licensed under the
* Creative Commons Attribution-ShareAlike 4.0 International License:
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
*
* This license applies only to such non-code elements and does not
* modify or replace the licensing terms applicable to the Program's
* source code, which remains licensed under the GNU Affero General
* Public License v3.
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
#pragma once
#include "global.h"
#include "../compoundfile.h"
#include <iostream>
#include <iomanip>
struct CompoundFileTest : testing::Test
{
wstring srcFilePath = sourcePath + L"ex.ppt";
CompoundFile cf;
CompoundFileTest() : cf(srcFilePath, CFSUpdateMode::Update)
{
}
};
wstring SaveToFile(CompoundFile& cf, wstring filename)
{
wstring filePath = InitOutPath(filename);
EXPECT_NO_THROW(cf.Save(filePath));
return filePath;
}
TEST_F(CompoundFileTest, read)
{
EXPECT_TRUE(cf.HasSourceStream());
}
TEST_F(CompoundFileTest, copy)
{
wstring savedFilePath = SaveToFile(cf, L"ex2.ppt");
EXPECT_EQ(FileLenght(srcFilePath), FileLenght(savedFilePath));
EXPECT_EQ(FileFNVHash(srcFilePath), FileFNVHash(savedFilePath));
}
void TestLargeCompoundFileWrite(CompoundFile& cf, wstring largeFileName)
{
auto largeStream = cf.RootStorage()->AddStream(L"stream");
EXPECT_NO_THROW(largeStream->Write(_70MBVector, 0));
wstring savedFilePath = SaveToFile(cf, largeFileName);
EXPECT_GT(FileLenght(savedFilePath), _70MBLen);
}
TEST(test_compoundfile, largeStream_v3)
{
CompoundFile cf_v3(CFCPP::Ver_3, CFCPP::Default);
TestLargeCompoundFileWrite(cf_v3, L"largeStream_v3.cfb");
}
TEST(test_compoundfile, largeStream_v4)
{
CompoundFile cf_v4(CFCPP::Ver_4, CFCPP::Default);
TestLargeCompoundFileWrite(cf_v4, L"largeStream_v3.cfb");
}
TEST_F(CompoundFileTest, SID)
{
auto PowerPointData = cf.GetDataBySID(1);
EXPECT_NE(PowerPointData.size(), 0);
}
TEST_F(CompoundFileTest, addEmptyStorage)
{
wstring storageName = L"storage";
auto storage1 = cf.RootStorage()->AddStorage(storageName);
wstring savedFilePath = SaveToFile(cf, L"ex3.ppt");
CompoundFile cf2(savedFilePath);
EXPECT_NO_THROW(cf2.RootStorage()->GetStorage(storageName));
}
TEST_F(CompoundFileTest, timestamp)
{
wstring storageName = L"storage";
auto storage = cf.RootStorage()->AddStorage(storageName);
EXPECT_TRUE(storage->getDataTime().getUINT64() > 116444736000000000ULL);
}
TEST_F(CompoundFileTest, addEmptyStream)
{
wstring streamName = L"stream";
cf.RootStorage()->AddStream(streamName);
wstring savedFilePath = SaveToFile(cf, L"ex4.ppt");
CompoundFile cf2(savedFilePath);
EXPECT_NO_THROW(cf2.RootStorage()->GetStream(streamName));
}
TEST_F(CompoundFileTest, add2StreamWithSameName)
{
wstring storageName = L"storage";
wstring streamsName = L"stream";
cf.RootStorage()->AddStorage(storageName)->AddStream(streamsName);
cf.RootStorage()->AddStream(streamsName);
wstring savedFilePath = SaveToFile(cf, L"ex5.ppt");
CompoundFile cf2(savedFilePath);
EXPECT_NO_THROW(cf2.RootStorage()->GetStream(streamsName));
EXPECT_NO_THROW(cf2.RootStorage()->GetStorage(storageName)->GetStream(streamsName));
}
TEST_F(CompoundFileTest, deleteStream)
{
const wstring streamName = L"PowerPoint Document";
EXPECT_TRUE(cf.RootStorage()->GetStream(streamName));
cf.RootStorage()->Delete(streamName);
EXPECT_THROW(cf.RootStorage()->GetStream(streamName), CFItemNotFound);
}
TEST_F(CompoundFileTest, deleteStreamWithSave)
{
const wstring streamName = L"PowerPoint Document";
cf.RootStorage()->Delete(streamName);
wstring savedFilePath = SaveToFile(cf, L"ex6.ppt");
CompoundFile cf2(savedFilePath);
EXPECT_THROW(cf2.RootStorage()->GetStream(streamName), CFItemNotFound);
}
TEST_F(CompoundFileTest, deleteStorage)
{
auto storage1 = cf.RootStorage()->AddStorage(L"sto1");
auto storage2 = storage1->AddStorage(L"sto2");
auto stream1 = storage2->AddStream(L"str1");
EXPECT_NO_THROW(cf.RootStorage()->GetStorage(L"sto1"));
cf.RootStorage()->Delete(L"sto1");
EXPECT_THROW(cf.RootStorage()->GetStorage(L"sto1"), CFItemNotFound);
}
void WriteStreamWithDefaultData(CompoundFile &cf, wstring streamName)
{
auto stream = cf.RootStorage()->AddStream(streamName);
EXPECT_NO_THROW(stream->Write(_8ByteData, 0));
EXPECT_NO_THROW(cf.RootStorage()->GetStream(streamName));
}
void CheckStreamWithDefaultData(CompoundFile &cf, wstring streamName)
{
vector<BYTE> vecForRead(_8ByteData.size());
shared_ptr<CFStream> stream;
EXPECT_TRUE(cf.RootStorage()->TryGetStream(streamName, stream));
EXPECT_NO_THROW(stream->Read(vecForRead, 0, _8ByteData.size()));
EXPECT_EQ(_8ByteData, vecForRead);
}
TEST_F(CompoundFileTest, writeStreams)
{
for (const auto& name : arrForeignFileNames)
{
WriteStreamWithDefaultData(cf, name);
CheckStreamWithDefaultData(cf, name);
}
}
void TryToSaveEmptyCompoundFile(wstring filename)
{
wstring outPath = InitOutPath(filename);
try {
CompoundFile cf;
cf.Save(outPath);
} catch (...) {
FAIL();
}
}
TEST(test_compoundfile, foreign_languages)
{
for (const auto& name : arrForeignFileNames)
{
wstring filename = name + L".cfb";
TryToSaveEmptyCompoundFile(filename);
}
SUCCEED();
}