2016-08-06 22:40:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
#include <detail/vector_streambuf.hpp>
|
2017-02-18 13:11:06 +08:00
|
|
|
#include <detail/xlsx_crypto.hpp>
|
2016-08-06 22:40:17 +08:00
|
|
|
#include <helpers/path_helper.hpp>
|
|
|
|
#include <helpers/xml_helper.hpp>
|
|
|
|
#include <xlnt/workbook/workbook.hpp>
|
|
|
|
|
|
|
|
class test_round_trip : public CxxTest::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
2016-09-08 10:39:13 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Read file as an XLSX-formatted ZIP file in the filesystem to a workbook,
|
|
|
|
/// write the workbook back to memory, then ensure that the contents of the two files are equivalent.
|
|
|
|
/// </summary>
|
2017-02-18 13:11:06 +08:00
|
|
|
bool round_trip_matches_rw(const xlnt::path &source)
|
2016-08-06 22:40:17 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
xlnt::workbook source_workbook;
|
|
|
|
source_workbook.load(source);
|
2016-10-31 03:48:40 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
std::vector<std::uint8_t> destination;
|
|
|
|
source_workbook.save(destination);
|
2016-09-08 10:39:13 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
std::ifstream source_stream(source.string(), std::ios::binary);
|
2016-08-06 22:40:17 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
return xml_helper::xlsx_archives_match(xlnt::detail::to_vector(source_stream), destination);
|
2016-08-06 22:40:17 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
bool round_trip_matches_rw(const xlnt::path &source, const std::string &password)
|
|
|
|
{
|
|
|
|
xlnt::workbook source_workbook;
|
|
|
|
source_workbook.load(source, password);
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> destination;
|
|
|
|
source_workbook.save(destination);
|
|
|
|
|
|
|
|
std::ifstream source_stream(source.string(), std::ios::binary);
|
|
|
|
const auto source_decrypted = xlnt::detail::crypto_helper::decrypt_xlsx(
|
|
|
|
xlnt::detail::to_vector(source_stream), password);
|
|
|
|
|
|
|
|
return xml_helper::xlsx_archives_match(source_decrypted, destination);
|
|
|
|
}
|
|
|
|
|
2017-02-25 22:55:11 +08:00
|
|
|
void test_round_trip_rw()
|
2016-08-06 22:40:17 +08:00
|
|
|
{
|
2017-01-23 03:54:07 +08:00
|
|
|
const auto files = std::vector<std::string>
|
|
|
|
{
|
|
|
|
"2_minimal",
|
|
|
|
"3_default",
|
|
|
|
"4_every_style",
|
|
|
|
"10_comments_hyperlinks_formulae",
|
|
|
|
"11_print_settings",
|
|
|
|
"12_advanced_properties"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto file : files)
|
|
|
|
{
|
|
|
|
auto path = path_helper::data_directory(file + ".xlsx");
|
|
|
|
TS_ASSERT(round_trip_matches_rw(path));
|
|
|
|
}
|
2016-09-12 02:57:34 +08:00
|
|
|
}
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-02-25 22:55:11 +08:00
|
|
|
void test_round_trip_rw_encrypted()
|
2017-02-18 13:11:06 +08:00
|
|
|
{
|
|
|
|
const auto files = std::vector<std::string>
|
|
|
|
{
|
|
|
|
"5_encrypted_agile",
|
|
|
|
"6_encrypted_libre",
|
|
|
|
"7_encrypted_standard",
|
|
|
|
"8_encrypted_numbers"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto file : files)
|
|
|
|
{
|
|
|
|
auto path = path_helper::data_directory(file + ".xlsx");
|
|
|
|
TS_ASSERT(round_trip_matches_rw(path, file
|
|
|
|
== "7_encrypted_standard" ? "password" : "secret"));
|
|
|
|
}
|
|
|
|
}
|
2016-08-06 22:40:17 +08:00
|
|
|
};
|