diff --git a/Common/cfcpp/header.cpp b/Common/cfcpp/header.cpp index e33f2a98af..6c180ddaaf 100644 --- a/Common/cfcpp/header.cpp +++ b/Common/cfcpp/header.cpp @@ -36,7 +36,7 @@ Header::Header(ushort version) } } -void Header::Write(CFCPP::Stream &stream) +void Header::Write(CFCPP::Stream &stream) const { StreamRW rw(stream); rw.WriteArray(headerSignature, sizeof(headerSignature)); diff --git a/Common/cfcpp/header.h b/Common/cfcpp/header.h index 201137fd31..3e19106a43 100644 --- a/Common/cfcpp/header.h +++ b/Common/cfcpp/header.h @@ -10,7 +10,7 @@ class Header public: Header(); Header(ushort version); - void Write(Stream& stream); + void Write(Stream& stream) const; void Read(Stream& stream); private: diff --git a/Common/cfcpp/stream.cpp b/Common/cfcpp/stream.cpp index f3b7ef64da..05941d6110 100644 --- a/Common/cfcpp/stream.cpp +++ b/Common/cfcpp/stream.cpp @@ -73,7 +73,7 @@ int CFCPP::FileLenght(std::wstring filename) return lenght; } -ULONG64 CFCPP::FileSimpleHash(std::wstring filename, int len, int offset) +ULONG64 CFCPP::FileFNVHash(std::wstring filename, int len, int offset) { auto stream = OpenFileStream(filename); diff --git a/Common/cfcpp/stream.h b/Common/cfcpp/stream.h index 7adb5943e8..559c54033e 100644 --- a/Common/cfcpp/stream.h +++ b/Common/cfcpp/stream.h @@ -46,6 +46,6 @@ bool IsOpen(const Stream& st); std::streamsize Length(const Stream& st); int FileLenght(std::wstring filename); -ULONG64 FileSimpleHash(std::wstring filename, int len = -1, int offset = 0); +ULONG64 FileFNVHash(std::wstring filename, int len = -1, int offset = 0); } diff --git a/Common/cfcpp/test/global.h b/Common/cfcpp/test/global.h index eb8de54abb..954508a6a3 100755 --- a/Common/cfcpp/test/global.h +++ b/Common/cfcpp/test/global.h @@ -6,28 +6,35 @@ #include #include #include "../../DesktopEditor/common/File.h" +#include "../stream.h" #include "../../DesktopEditor/common/Directory.h" + using namespace testing; using namespace std; +using namespace CFCPP; constexpr int _70MBLen = 1024 * 1024 * 70; -const std::vector _8ByteData = {0x28, 0xFF, 0x28, 0x1D, 0x4C, 0xFA, 0x00, 0x79}; -const std::vector _70MBVector(_70MBLen, 0x90); +const vector _8ByteData = {0x28, 0xFF, 0x28, 0x1D, 0x4C, 0xFA, 0x00, 0x79}; +const vector _70MBVector(_70MBLen, 0x90); -const std::wstring testDataPath = L"../../../data/"; -const std::wstring sourcePath = testDataPath + L"src/"; -const std::wstring outPath = testDataPath + L"out/"; +const wstring testDataPath = L"../../../data/"; +const wstring sourcePath = testDataPath + L"src/"; +const wstring outPath = testDataPath + L"out/"; - -void EXPECT_FILE_EXIST(const std::wstring& filePath) +void EXPECT_FILE_EXIST(const wstring& filePath) { EXPECT_TRUE(NSFile::CFileBinary::Exists(filePath)); } -std::wstring InitOutPath(const std::wstring& filename) +void EXPECT_FILE_FNV_HASH(const wstring& filePath, LONG hash) +{ + EXPECT_EQ(CFCPP::FileFNVHash(filePath), hash); +} + +wstring InitOutPath(const wstring& filename) { NSDirectory::CreateDirectory(outPath); wstring fullpath = outPath + filename; @@ -37,7 +44,7 @@ std::wstring InitOutPath(const std::wstring& filename) } -std::vector arrForeignFileNames= +vector arrForeignFileNames= { L"file", // Englang L"fil", // Danish diff --git a/Common/cfcpp/test/main.cpp b/Common/cfcpp/test/main.cpp index 31eec8c327..deb9dde628 100644 --- a/Common/cfcpp/test/main.cpp +++ b/Common/cfcpp/test/main.cpp @@ -2,9 +2,9 @@ #include "tst_header.h" #include "tst_directoryentry.h" #include "tst_compondfile.h" +#include "tst_data_set.h" using namespace CFCPP; -using namespace std; int main(int argc, char *argv[]) { diff --git a/Common/cfcpp/test/tst_compondfile.h b/Common/cfcpp/test/tst_compondfile.h index 9bf25ce9b8..d4f3f374d7 100755 --- a/Common/cfcpp/test/tst_compondfile.h +++ b/Common/cfcpp/test/tst_compondfile.h @@ -4,21 +4,14 @@ #include "compoundfile.h" #include #include -#include - -#include - -using namespace testing; -using namespace std; -using namespace CFCPP; struct CompoundFileTest : testing::Test { - wstring filename = sourcePath + L"ex.ppt"; + wstring srcFilePath = sourcePath + L"ex.ppt"; CompoundFile cf; - CompoundFileTest() : cf(filename, CFSUpdateMode::Update) + CompoundFileTest() : cf(srcFilePath, CFSUpdateMode::Update) { } @@ -43,6 +36,14 @@ struct CompoundFileTest : testing::Test }; +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()); @@ -50,24 +51,20 @@ TEST_F(CompoundFileTest, read) TEST_F(CompoundFileTest, copy) { - wstring other_filename = InitOutPath(L"ex2.ppt"); - cf.Save(other_filename); + wstring savedFilePath = SaveToFile(cf, L"ex2.ppt"); - EXPECT_EQ(FileLenght(filename), FileLenght(other_filename)); - EXPECT_EQ(FileSimpleHash(filename), FileSimpleHash(other_filename)); + EXPECT_EQ(FileLenght(srcFilePath), FileLenght(savedFilePath)); + EXPECT_EQ(FileFNVHash(srcFilePath), FileFNVHash(savedFilePath)); } -void TestLargeCompoundFileWrite(CompoundFile& cf, std::wstring largeFileName) +void TestLargeCompoundFileWrite(CompoundFile& cf, wstring largeFileName) { - wstring largeFilePath = InitOutPath(largeFileName); auto largeStream = cf.RootStorage()->AddStream(L"stream"); EXPECT_NO_THROW(largeStream->Write(_70MBVector, 0)); - EXPECT_NO_THROW(cf.Save(largeFilePath)); - cf.Close(); - - EXPECT_GT(FileLenght(largeFilePath), _70MBLen); + wstring savedFilePath = SaveToFile(cf, largeFileName); + EXPECT_GT(FileLenght(savedFilePath), _70MBLen); } TEST(test_compoundfile, largeStream_v3) @@ -92,54 +89,50 @@ TEST_F(CompoundFileTest, addEmptyStorage) { wstring storageName = L"storage"; auto storage1 = cf.RootStorage()->AddStorage(storageName); - EXPECT_TRUE(storage1->getDataTime().getUINT64() > 116444736000000000ULL); - wstring other_filename = InitOutPath(L"ex3.ppt"); - cf.Save(other_filename); - cf.Close(); + wstring savedFilePath = SaveToFile(cf, L"ex3.ppt"); - CompoundFile cf2(other_filename); + 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 other_filename = InitOutPath(L"ex4.ppt"); - cf.Save(other_filename); + wstring savedFilePath = SaveToFile(cf, L"ex4.ppt"); - cf.Close(); - - CompoundFile cf2(other_filename); - EXPECT_TRUE(cf2.RootStorage()->GetStream(streamName) != nullptr); + CompoundFile cf2(savedFilePath); + EXPECT_NO_THROW(cf2.RootStorage()->GetStream(streamName)); } -TEST_F(CompoundFileTest, add2Stream) +TEST_F(CompoundFileTest, add2StreamWithSameName) { wstring storageName = L"storage"; - wstring streamName = L"stream"; + wstring streamsName = L"stream"; - cf.RootStorage()->AddStorage(storageName)->AddStream(streamName); - cf.RootStorage()->AddStream(streamName); + cf.RootStorage()->AddStorage(storageName)->AddStream(streamsName); + cf.RootStorage()->AddStream(streamsName); - wstring other_filename = InitOutPath(L"ex5.ppt"); - cf.Save(other_filename); - cf.Close(); + wstring savedFilePath = SaveToFile(cf, L"ex5.ppt"); - CompoundFile cf2(other_filename); - EXPECT_TRUE(cf2.RootStorage()->GetStream(streamName) != nullptr); - - auto storage = cf2.RootStorage()->GetStorage(storageName); - EXPECT_TRUE(storage != nullptr); - EXPECT_TRUE(storage->GetStream(streamName) != nullptr); + CompoundFile cf2(savedFilePath); + EXPECT_NO_THROW(cf2.RootStorage()->GetStream(streamsName)); + EXPECT_NO_THROW(cf2.RootStorage()->GetStorage(storageName)->GetStream(streamsName)); } TEST_F(CompoundFileTest, deleteStream) { - const std::wstring streamName = L"PowerPoint Document"; + const wstring streamName = L"PowerPoint Document"; EXPECT_TRUE(cf.RootStorage()->GetStream(streamName)); cf.RootStorage()->Delete(streamName); EXPECT_THROW(cf.RootStorage()->GetStream(streamName), CFItemNotFound); @@ -147,16 +140,13 @@ TEST_F(CompoundFileTest, deleteStream) TEST_F(CompoundFileTest, deleteStreamWithSave) { - const std::wstring streamName = L"PowerPoint Document"; - cf.RootStorage()->Delete(L"PowerPoint Document"); + const wstring streamName = L"PowerPoint Document"; + cf.RootStorage()->Delete(streamName); - wstring other_filename = InitOutPath(L"ex6.ppt"); - cf.Save(other_filename); - cf.Close(); + wstring savedFilePath = SaveToFile(cf, L"ex6.ppt"); - CompoundFile cf2(other_filename); + CompoundFile cf2(savedFilePath); EXPECT_THROW(cf2.RootStorage()->GetStream(streamName), CFItemNotFound); - EXPECT_TRUE(cf2.RootStorage()->GetStream(L"Current User")); } TEST_F(CompoundFileTest, deleteStorage) @@ -165,37 +155,47 @@ TEST_F(CompoundFileTest, deleteStorage) auto storage2 = storage1->AddStorage(L"sto2"); auto stream1 = storage2->AddStream(L"str1"); - EXPECT_TRUE(cf.RootStorage()->GetStorage(L"sto1")); + EXPECT_NO_THROW(cf.RootStorage()->GetStorage(L"sto1")); cf.RootStorage()->Delete(L"sto1"); EXPECT_THROW(cf.RootStorage()->GetStorage(L"sto1"), CFItemNotFound); } -TEST_F(CompoundFileTest, writeStream) +void WriteStreamWithDefaultData(CompoundFile &cf, wstring streamName) { - std::vector read(_8ByteData.size()); - auto stream1 = cf.RootStorage()->AddStream(L"str1"); - stream1->Write(_8ByteData,0); + auto stream = cf.RootStorage()->AddStream(streamName); - EXPECT_TRUE(cf.RootStorage()->GetStream(L"str1")); - EXPECT_EQ(stream1->size(), _8ByteData.size()); - EXPECT_NO_THROW(stream1->Read(read, 0, _8ByteData.size())); - EXPECT_EQ(_8ByteData, read); + EXPECT_NO_THROW(stream->Write(_8ByteData, 0)); + EXPECT_NO_THROW(cf.RootStorage()->GetStream(streamName)); +} + +void CheckStreamWithDefaultData(CompoundFile &cf, wstring streamName) +{ + vector vecForRead(_8ByteData.size()); + + shared_ptr 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) { - std::vector read(_8ByteData.size()); - auto stream1 = cf.RootStorage()->AddStream(name); - EXPECT_TRUE(stream1); + WriteStreamWithDefaultData(cf, name); + CheckStreamWithDefaultData(cf, name); + } +} - EXPECT_NO_THROW(stream1->Write(_8ByteData, 0)); - EXPECT_NO_THROW(cf.RootStorage()->GetStream(name)); - - EXPECT_EQ(stream1->size(), _8ByteData.size()); - EXPECT_NO_THROW(stream1->Read(read, 0, _8ByteData.size())); - EXPECT_EQ(_8ByteData, read); +void TryToSaveEmptyCompoundFile(wstring filename) +{ + wstring outPath = InitOutPath(filename); + try { + CompoundFile cf; + cf.Save(outPath); + } catch (...) { + FAIL(); } } @@ -203,19 +203,8 @@ TEST(test_compoundfile, foreign_languages) { for (const auto& name : arrForeignFileNames) { - try { - wstring path = InitOutPath(name + L".cfb"); - - CompoundFile cf; - auto stream1 = cf.RootStorage()->AddStream(L"stream"); - stream1->Write(_8ByteData, 0); - - cf.Save(path); - cf.Close(); - } catch (...) { - FAIL(); - } - + wstring filename = name + L".cfb"; + TryToSaveEmptyCompoundFile(filename); } SUCCEED(); } diff --git a/Common/cfcpp/test/tst_data_set.h b/Common/cfcpp/test/tst_data_set.h index 6d147233f0..98b37ebdd2 100755 --- a/Common/cfcpp/test/tst_data_set.h +++ b/Common/cfcpp/test/tst_data_set.h @@ -1,11 +1,10 @@ #pragma once - #include "global.h" #include "../../DesktopEditor/common/File.h" -TEST(TestDataSet, allSourceFilesExist) + +TEST(TestDataSet, allSourceFilesExistAndCorrect) { - EXPECT_FILE_EXIST(sourcePath + L"ex.ppt"); - EXPECT_FILE_EXIST(sourcePath + L"big.ppt"); + EXPECT_FILE_FNV_HASH(sourcePath + L"ex.ppt", 4659199142276128161); } diff --git a/Common/cfcpp/test/tst_directoryentry.h b/Common/cfcpp/test/tst_directoryentry.h index 3d174782ae..3db85811cf 100755 --- a/Common/cfcpp/test/tst_directoryentry.h +++ b/Common/cfcpp/test/tst_directoryentry.h @@ -3,8 +3,6 @@ #include "global.h" #include "directoryentry.h" -using namespace CFCPP; - struct DirEntryTest : testing::Test { @@ -14,10 +12,20 @@ struct DirEntryTest : testing::Test DirEntryTest() : filename(sourcePath + L"ex.ppt"), stream(OpenFileStream(filename)) + {} + + DirectoryEntry LoadDirectoryEntryFromPPTFile() { + DirectoryEntry de(L"", StgInvalid); + const int fileShift = 0x400; + stream->seek(fileShift, ios::beg); + de.Read(stream); + + return de; } }; + void test_dirEntry_read(const DirectoryEntry& de) { EXPECT_EQ(de.GetEntryName(), L"Root Entry"); @@ -41,27 +49,35 @@ void test_dirEntry_read(const DirectoryEntry& de) TEST_F(DirEntryTest, read) { - DirectoryEntry de(L"", StgInvalid); - stream->seek(0x400, std::ios::beg); - de.Read(stream); - + auto de = LoadDirectoryEntryFromPPTFile(); EXPECT_EQ(stream->tell(), 0x480); test_dirEntry_read(de); } -TEST_F(DirEntryTest, write) +void SaveDirectoryEntryToFile(const DirectoryEntry& de, wstring filename) { - DirectoryEntry de(L"", StgInvalid); - stream->seek(0x400, std::ios::beg); - de.Read(stream); - - std::wstring other_filename = InitOutPath(L"direntry.bin"); - stream = OpenFileStream(other_filename, true); + wstring other_filename = InitOutPath(filename); + auto stream = OpenFileStream(other_filename, true); de.Write(stream); EXPECT_EQ(stream->tell(), 0x80); - stream->seek(0, std::ios::beg); - - DirectoryEntry other(L"", StgInvalid); - other.Read(stream); - test_dirEntry_read(other); +} + +DirectoryEntry LoadDirectoryEntryFromFile(wstring filePath) +{ + DirectoryEntry de(L"", StgInvalid); + auto stream = OpenFileStream(outPath + filePath, false); + de.Read(stream); + + return de; +} + +TEST_F(DirEntryTest, write) +{ + auto de = LoadDirectoryEntryFromPPTFile(); + + wstring saveFilename = L"direntry.bin"; + SaveDirectoryEntryToFile(de, saveFilename); + auto readDirectoryEntry = LoadDirectoryEntryFromFile(saveFilename); + + test_dirEntry_read(readDirectoryEntry); } diff --git a/Common/cfcpp/test/tst_header.h b/Common/cfcpp/test/tst_header.h index 0dbb4d9a35..0570527fa2 100755 --- a/Common/cfcpp/test/tst_header.h +++ b/Common/cfcpp/test/tst_header.h @@ -4,8 +4,6 @@ #include "header.h" #include "streamrw.h" -using namespace CFCPP; - struct HeaderTest : testing::Test { @@ -20,6 +18,7 @@ struct HeaderTest : testing::Test } }; + void test_header_state(const Header& hd) { ASSERT_THAT(hd.headerSignature, ElementsAre(0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1)); @@ -54,49 +53,29 @@ TEST_F(HeaderTest, read) test_header_state(hd); } +void SaveHeaderToFile(const Header& header, wstring filename) +{ + wstring savePath = InitOutPath(filename); + auto stream = OpenFileStream(savePath, true); + header.Write(stream); +} + +Header LoadHeaderFromFile(wstring filename) +{ + auto stream = OpenFileStream(outPath + filename, false); + Header hd; + hd.Read(stream); + + return hd; +} + TEST_F(HeaderTest, write) { hd.Read(stream); - std::wstring other_filename = InitOutPath(L"header.bin"); - stream = OpenFileStream(other_filename, true); - hd.Write(stream); + wstring saveFilename = L"header.bin"; + SaveHeaderToFile(hd, saveFilename); + Header readHeader = LoadHeaderFromFile(saveFilename); - Header other; - stream->seek(0, std::ios::beg); - other.Read(stream); - test_header_state(other); -} - -TEST_F(HeaderTest, test_header_seek) -{ - hd.Read(stream); - - std::wstring other_filename = InitOutPath(L"sheader.bin"); - stream = OpenFileStream(other_filename, true); - - std::vector zeroArray(512, 0); - stream->write(zeroArray.data(), zeroArray.size()); - zeroArray.clear(); - - EXPECT_EQ(Length(stream), 512); - - - stream->seek(4); - StreamRW rw(stream); - auto pos = rw.Tell(); - EXPECT_EQ(pos, 4); - - hd.Write(stream); - EXPECT_EQ(Length(stream), 512+4); - - - Header other; - stream->seek(0); - int zeroValue = StreamRW(stream).Read(); - EXPECT_EQ(zeroValue, 0); - - - other.Read(stream); - test_header_state(other); + test_header_state(readHeader); } diff --git a/Common/cfcpp/test/tst_streamrw.h b/Common/cfcpp/test/tst_streamrw.h index f7db180a5f..ebdf2fdeb2 100755 --- a/Common/cfcpp/test/tst_streamrw.h +++ b/Common/cfcpp/test/tst_streamrw.h @@ -3,8 +3,8 @@ #include "global.h" #include "streamrw.h" #include +#include -using namespace CFCPP; struct StreamRWTest : testing::Test { @@ -16,7 +16,7 @@ struct StreamRWTest : testing::Test StreamRWTest() : filename(L"types.bin"), - stream(OpenFileStream(filename, true)), + stream(OpenFileStream(InitOutPath(filename), true)), rw(new StreamRW(stream)) { } @@ -27,48 +27,44 @@ struct StreamRWTest : testing::Test }; -TEST_F(StreamRWTest, test_stream_open) +TEST_F(StreamRWTest, open) { EXPECT_TRUE(IsOpen(stream)); } -TEST_F(StreamRWTest, test_stream_write) +TEST_F(StreamRWTest, write) { - rw->Seek(0); rw->Write(symbol); rw->Write(integer); stream->flush(); EXPECT_EQ((int)Length(stream), 5); } - -TEST_F(StreamRWTest, test_stream_read) +TEST_F(StreamRWTest, read) { + rw->Write(symbol); + rw->Write(integer); + EXPECT_EQ(rw->Seek(0), 0); EXPECT_EQ(rw->Read(), symbol); EXPECT_EQ(rw->Read(), integer); - NSFile::CFileBinary::Remove(filename.c_str()); } -TEST_F(StreamRWTest, test_stream_rw_array) +TEST_F(StreamRWTest, rw_array) { int sarr[3] = {99, 0, -3}; int darr[3] = {-1,-1,-1}; rw->WriteArray(reinterpret_cast(sarr), sizeof (sarr)); rw->Seek(0); rw->ReadArray(reinterpret_cast(darr), sizeof (darr)); - EXPECT_EQ(sarr[2], darr[2]); - - NSFile::CFileBinary::Remove(filename.c_str()); + ASSERT_FALSE( memcmp(sarr, darr, sizeof(darr)) ); } -TEST_F(StreamRWTest, test_stream_seek) +TEST_F(StreamRWTest, seek) { - stream->write(std::array().data(), 16); + stream->write(array().data(), 16); stream->seek(2); int dataVal(1234567890); stream->write(reinterpret_cast(&dataVal), 4); EXPECT_EQ(Length(stream), 16); - - NSFile::CFileBinary::Remove(filename.c_str()); }