mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-14 09:14:14 +02:00
Merge pull request #1462 from ispyisail/feat/qetz-container
Linux build and tests / Build and test (Qt 6, Debug) (push) Failing after 2m8s
Linux build and tests / Build and test (Qt 6, Debug) (push) Failing after 2m8s
Add the zipped project format (.qetz) as a save option
This commit is contained in:
@@ -1221,3 +1221,29 @@ if(Qt6_VERSION VERSION_LESS 6.5.0)
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# The zip a .qetz project is stored in: round trip, same bytes twice,
|
||||
# unsafe entry names refused (sources/container/qetzip.*, bundled miniz).
|
||||
add_executable(
|
||||
tst_qetzip
|
||||
tst_qetzip.cpp
|
||||
${QET_DIR}/sources/container/qetzip.cpp
|
||||
${QET_DIR}/sources/container/miniz/miniz.c)
|
||||
add_test(NAME tst_qetzip COMMAND tst_qetzip)
|
||||
target_include_directories(tst_qetzip PRIVATE ${QET_DIR}/sources)
|
||||
target_link_libraries(tst_qetzip PRIVATE Qt::Test)
|
||||
|
||||
# A project split into the parts of a .qetz and joined back is the same
|
||||
# document, on every example (and QET_CONTAINER_CORPUS if set).
|
||||
add_executable(
|
||||
tst_qetcontainer
|
||||
tst_qetcontainer.cpp
|
||||
${QET_DIR}/sources/container/qetcontainer.cpp
|
||||
${QET_DIR}/sources/container/qetcontainerdb.cpp
|
||||
${QET_DIR}/sources/container/qetzip.cpp
|
||||
${QET_DIR}/sources/container/miniz/miniz.c)
|
||||
add_test(NAME tst_qetcontainer COMMAND tst_qetcontainer)
|
||||
target_include_directories(tst_qetcontainer PRIVATE ${QET_DIR}/sources)
|
||||
target_link_libraries(tst_qetcontainer PRIVATE Qt::Test Qt::Xml SQLite3::SQLite3)
|
||||
target_compile_definitions(tst_qetcontainer PRIVATE
|
||||
"QET_EXAMPLES_DIR=\"${QET_DIR}/examples\"")
|
||||
|
||||
@@ -0,0 +1,313 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#include <QtTest>
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "container/qetcontainer.h"
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
// A project document split into the parts of a .qetz, zipped, unzipped and
|
||||
// joined back is the document it was: every example, and every .qet in the
|
||||
// folder QET_CONTAINER_CORPUS names, if set -- exactly, whitespace text
|
||||
// included: QETProject writes the symbol definitions back as it read them,
|
||||
// spacing and all, so a part that came back re-indented would change the
|
||||
// next save.
|
||||
class tst_qetcontainer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
static QList<QDomNode> children(const QDomNode &node)
|
||||
{
|
||||
QList<QDomNode> all;
|
||||
for (QDomNode c = node.firstChild() ; !c.isNull() ; c = c.nextSibling())
|
||||
all << c;
|
||||
return all;
|
||||
}
|
||||
|
||||
// empty when equal, else where they differ
|
||||
static QString difference(const QDomNode &a, const QDomNode &b, const QString &path)
|
||||
{
|
||||
const QString here = path + QLatin1Char('/') + a.nodeName();
|
||||
if (a.nodeType() != b.nodeType() || a.nodeName() != b.nodeName())
|
||||
return here + QStringLiteral(": %1 vs %2").arg(a.nodeName(), b.nodeName());
|
||||
if (!a.isElement() && !a.isDocument() && a.nodeValue() != b.nodeValue())
|
||||
return here + QStringLiteral(": value differs");
|
||||
if (a.isElement()) {
|
||||
const QDomNamedNodeMap aa = a.attributes(), ba = b.attributes();
|
||||
if (aa.count() != ba.count())
|
||||
return here + QStringLiteral(": %1 vs %2 attributes").arg(aa.count()).arg(ba.count());
|
||||
for (int i = 0 ; i < aa.count() ; ++i) {
|
||||
const QDomAttr attr = aa.item(i).toAttr();
|
||||
if (!ba.contains(attr.name()) || ba.namedItem(attr.name()).nodeValue() != attr.value())
|
||||
return here + QStringLiteral(": attribute %1 differs").arg(attr.name());
|
||||
}
|
||||
}
|
||||
const QList<QDomNode> ac = children(a), bc = children(b);
|
||||
if (ac.size() != bc.size())
|
||||
return here + QStringLiteral(": %1 vs %2 children").arg(ac.size()).arg(bc.size());
|
||||
for (int i = 0 ; i < ac.size() ; ++i) {
|
||||
const QString d = difference(ac.at(i), bc.at(i), here + QStringLiteral("[%1]").arg(i));
|
||||
if (!d.isEmpty()) return d;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static QByteArray read(const QString &path)
|
||||
{
|
||||
QFile f(path);
|
||||
return f.open(QIODevice::ReadOnly) ? f.readAll() : QByteArray();
|
||||
}
|
||||
|
||||
static QList<QetZip::Entry> splitFile(const QString &path, QDomDocument *original)
|
||||
{
|
||||
QString error;
|
||||
if (!QetContainer::parse(read(path), original, &error)) return {};
|
||||
return QetContainer::split(*original, QStringLiteral("tst_qetcontainer"), &error);
|
||||
}
|
||||
|
||||
private slots:
|
||||
void sameDocumentBack_data()
|
||||
{
|
||||
QTest::addColumn<QString>("file");
|
||||
QStringList dirs{QStringLiteral(QET_EXAMPLES_DIR)};
|
||||
const QString corpus = qEnvironmentVariable("QET_CONTAINER_CORPUS");
|
||||
if (!corpus.isEmpty()) dirs << corpus;
|
||||
for (const QString &dir : dirs) {
|
||||
QDirIterator it(dir, {QStringLiteral("*.qet")}, QDir::Files, QDirIterator::Subdirectories);
|
||||
QStringList files;
|
||||
while (it.hasNext()) files << it.next();
|
||||
files.sort();
|
||||
for (const QString &f : files)
|
||||
QTest::newRow(qPrintable(QDir(dir).relativeFilePath(f))) << f;
|
||||
}
|
||||
}
|
||||
void sameDocumentBack()
|
||||
{
|
||||
QFETCH(QString, file);
|
||||
QDomDocument original;
|
||||
QString error;
|
||||
if (!QetContainer::parse(read(file), &original, &error))
|
||||
QSKIP(qPrintable(QStringLiteral("not readable as XML, as QElectroTech finds too: ") + error));
|
||||
const QList<QetZip::Entry> entries = QetContainer::split(original, QString(), &error);
|
||||
QVERIFY2(!entries.isEmpty(), qPrintable(error));
|
||||
QCOMPARE(entries.first().name, QStringLiteral("mimetype"));
|
||||
|
||||
QList<QetZip::Entry> unzipped;
|
||||
QVERIFY2(QetZip::fromBytes(QetZip::toBytes(entries, &error), &unzipped, &error), qPrintable(error));
|
||||
QDomDocument back;
|
||||
QVERIFY2(QetContainer::join(unzipped, &back, &error), qPrintable(error));
|
||||
const QString d = difference(original.documentElement(), back.documentElement(), QString());
|
||||
QVERIFY2(d.isEmpty(), qPrintable(d));
|
||||
}
|
||||
|
||||
// Splitting and joining adds no whitespace text. QETProject keeps the
|
||||
// symbol definitions as it read them and writes them back as they
|
||||
// are, so indentation a part gained would change the next save
|
||||
// (forum attachments 1328, 1978, 2057, 3003, 3004 did, with indented
|
||||
// parts). Here: a document without any whitespace-only text, as
|
||||
// QETProject::toXml() builds one, must come back without any.
|
||||
void noWhitespaceAdded()
|
||||
{
|
||||
QDomDocument original;
|
||||
QVERIFY(original.setContent(read(QStringLiteral(QET_EXAMPLES_DIR "/industrial.qet"))));
|
||||
auto whitespace_nodes = [](const QDomDocument &d) {
|
||||
int n = 0;
|
||||
std::function<void(const QDomNode &)> walk = [&](const QDomNode &node) {
|
||||
for (QDomNode c = node.firstChild() ; !c.isNull() ; c = c.nextSibling()) {
|
||||
if (c.isText() && c.nodeValue().trimmed().isEmpty()) ++n;
|
||||
walk(c);
|
||||
}
|
||||
};
|
||||
walk(d);
|
||||
return n;
|
||||
};
|
||||
QCOMPARE(whitespace_nodes(original), 0);
|
||||
QString error;
|
||||
const QList<QetZip::Entry> entries = QetContainer::split(original, QString(), &error);
|
||||
QVERIFY2(!entries.isEmpty(), qPrintable(error));
|
||||
QDomDocument back;
|
||||
QVERIFY2(QetContainer::join(entries, &back, &error), qPrintable(error));
|
||||
QCOMPARE(whitespace_nodes(back), 0);
|
||||
}
|
||||
|
||||
// The engineering data is in project.sqlite and nowhere else: the folio
|
||||
// files hold no symbol information, links, wire numbers or title-block
|
||||
// fields, and the database holds them all (industrial.qet: 50 folios).
|
||||
void databaseHoldsTheData()
|
||||
{
|
||||
QDomDocument original;
|
||||
const QList<QetZip::Entry> entries = splitFile(QStringLiteral(QET_EXAMPLES_DIR "/industrial.qet"), &original);
|
||||
QVERIFY(!entries.isEmpty());
|
||||
QByteArray database;
|
||||
for (const QetZip::Entry &e : entries) {
|
||||
if (e.name == QLatin1String("project.sqlite")) database = e.data;
|
||||
if (e.name.startsWith(QStringLiteral("folios/")) || e.name == QLatin1String("project.xml")) {
|
||||
QVERIFY2(!e.data.contains("<elementInformation "), qPrintable(e.name));
|
||||
QVERIFY2(!e.data.contains("<link_uuid "), qPrintable(e.name));
|
||||
QVERIFY2(!e.data.contains("<property "), qPrintable(e.name));
|
||||
QVERIFY2(!QString::fromUtf8(e.data).contains(QRegularExpression(QStringLiteral("<conductor [^>]* num="))),
|
||||
qPrintable(e.name));
|
||||
}
|
||||
}
|
||||
QVERIFY(database.startsWith("SQLite format 3"));
|
||||
|
||||
sqlite3 *db = nullptr;
|
||||
QCOMPARE(sqlite3_open(":memory:", &db), SQLITE_OK);
|
||||
unsigned char *copy = static_cast<unsigned char *>(sqlite3_malloc64(sqlite3_uint64(database.size())));
|
||||
memcpy(copy, database.constData(), size_t(database.size()));
|
||||
QCOMPARE(sqlite3_deserialize(db, "main", copy, database.size(), database.size(),
|
||||
SQLITE_DESERIALIZE_FREEONCLOSE), SQLITE_OK);
|
||||
auto count = [db](const char *table) {
|
||||
sqlite3_stmt *st = nullptr;
|
||||
sqlite3_prepare_v2(db, (QByteArray("SELECT count(*) FROM ") + table).constData(), -1, &st, nullptr);
|
||||
sqlite3_step(st);
|
||||
const int n = sqlite3_column_int(st, 0);
|
||||
sqlite3_finalize(st);
|
||||
return n;
|
||||
};
|
||||
const QDomNodeList diagrams = original.elementsByTagName(QStringLiteral("diagram"));
|
||||
QCOMPARE(count("folio"), int(diagrams.size()));
|
||||
QCOMPARE(count("element"), int(original.elementsByTagName(QStringLiteral("element")).size())
|
||||
- int(original.elementsByTagName(QStringLiteral("collection")).at(0).toElement()
|
||||
.elementsByTagName(QStringLiteral("element")).size()));
|
||||
QCOMPARE(count("element_info"), int(original.elementsByTagName(QStringLiteral("elementInformation")).size()));
|
||||
QCOMPARE(count("link"), int(original.elementsByTagName(QStringLiteral("link_uuid")).size()));
|
||||
QCOMPARE(count("conductor"), int(original.elementsByTagName(QStringLiteral("conductor")).size()));
|
||||
QVERIFY(count("folio_property") + count("project_property") > 0);
|
||||
sqlite3_close(db);
|
||||
}
|
||||
|
||||
// Terminal strips move into the database, and come back exactly:
|
||||
// industrial.qet with two strips on its own terminal symbols, parsed as
|
||||
// a save leaves it (no whitespace text).
|
||||
void terminalStripsInDatabase()
|
||||
{
|
||||
QByteArray xml = read(QStringLiteral(QET_EXAMPLES_DIR "/industrial.qet"));
|
||||
const QByteArray strips =
|
||||
"<terminal_strips>"
|
||||
"<terminal_strip><terminal_strip_data uuid=\"{11111111-0000-4000-8000-000000000001}\"><informations>"
|
||||
"<information name=\"installation\">=A1</information><information name=\"location\">+CAB1</information>"
|
||||
"<information name=\"name\">X1</information><information name=\"comment\">main & aux</information>"
|
||||
"</informations></terminal_strip_data>"
|
||||
"<layout><physical_terminal><real_terminal element_uuid=\"{c2180165-8d35-44eb-83c4-100395451915}\"/></physical_terminal>"
|
||||
"<physical_terminal><real_terminal element_uuid=\"{05f681ee-04dd-489d-afc8-a646ec6016f9}\"/>"
|
||||
"<real_terminal element_uuid=\"{a33935cf-671a-480f-9497-1a632eab7cbd}\"/></physical_terminal></layout>"
|
||||
"<terminal_strip_bridge uuid=\"{22222222-0000-4000-8000-000000000001}\" color=\"#ff0000\"><real_terminals>"
|
||||
"<real_terminal uuid=\"{c2180165-8d35-44eb-83c4-100395451915}\"/>"
|
||||
"<real_terminal uuid=\"{05f681ee-04dd-489d-afc8-a646ec6016f9}\"/></real_terminals></terminal_strip_bridge>"
|
||||
"</terminal_strip>"
|
||||
"<terminal_strip><terminal_strip_data uuid=\"{11111111-0000-4000-8000-000000000002}\"><informations>"
|
||||
"<information name=\"name\">X2</information></informations></terminal_strip_data>"
|
||||
"<layout><physical_terminal><real_terminal element_uuid=\"{c96008ed-1c74-4c0a-836a-bd8458491a16}\"/></physical_terminal></layout>"
|
||||
"</terminal_strip></terminal_strips>";
|
||||
const int at = xml.indexOf("<collection");
|
||||
QVERIFY(at > 0);
|
||||
xml.insert(at, strips);
|
||||
QDomDocument original;
|
||||
QVERIFY(original.setContent(xml)); // as a save leaves it: no whitespace text
|
||||
QString error;
|
||||
const QList<QetZip::Entry> entries = QetContainer::split(original, QString(), &error);
|
||||
QVERIFY2(!entries.isEmpty(), qPrintable(error));
|
||||
for (const QetZip::Entry &e : entries)
|
||||
if (e.name == QLatin1String("project.xml"))
|
||||
QVERIFY2(!e.data.contains("<terminal_strip>"), "the strips stayed in project.xml");
|
||||
QDomDocument back;
|
||||
QVERIFY2(QetContainer::join(entries, &back, &error), qPrintable(error));
|
||||
const QString d = difference(original.documentElement(), back.documentElement(), QString());
|
||||
QVERIFY2(d.isEmpty(), qPrintable(d));
|
||||
}
|
||||
|
||||
// The folio pictures leave the folios, once each
|
||||
void picturesAreFiles()
|
||||
{
|
||||
QDomDocument original;
|
||||
const QList<QetZip::Entry> entries = splitFile(
|
||||
QStringLiteral(QET_EXAMPLES_DIR "/weneedpolonez-Polonez_MR89_wiring_diagram.qet"), &original);
|
||||
QVERIFY(!entries.isEmpty());
|
||||
int pictures = 0;
|
||||
for (const QetZip::Entry &e : entries) {
|
||||
if (e.name.startsWith(QStringLiteral("images/"))) {
|
||||
++pictures;
|
||||
QVERIFY(e.data.startsWith("\x89PNG"));
|
||||
QVERIFY(!e.compress);
|
||||
}
|
||||
if (e.name.startsWith(QStringLiteral("folios/")))
|
||||
QVERIFY2(!e.data.contains("iVBORw0KGgo"), "a folio still holds a PNG in base64");
|
||||
}
|
||||
QVERIFY(pictures > 0);
|
||||
QVERIFY(pictures < 121); // the 121 placed pictures repeat a few files
|
||||
}
|
||||
|
||||
// A file whose manifest needs a later reader is refused, not half read
|
||||
void laterFormatRefused()
|
||||
{
|
||||
QDomDocument original;
|
||||
QList<QetZip::Entry> entries = splitFile(QStringLiteral(QET_EXAMPLES_DIR "/perceuse.qet"), &original);
|
||||
QVERIFY(!entries.isEmpty());
|
||||
for (QetZip::Entry &e : entries)
|
||||
if (e.name == QLatin1String("manifest.xml"))
|
||||
e.data = QString::fromUtf8(e.data).replace(QRegularExpression(QStringLiteral("min-reader=\"\\d+\"")),
|
||||
QStringLiteral("min-reader=\"9\"")).toUtf8();
|
||||
QDomDocument back;
|
||||
QString error;
|
||||
QVERIFY(!QetContainer::join(entries, &back, &error));
|
||||
QVERIFY2(error.contains(QStringLiteral("later")), qPrintable(error));
|
||||
}
|
||||
|
||||
void missingPartRefused()
|
||||
{
|
||||
QDomDocument original;
|
||||
QList<QetZip::Entry> entries = splitFile(QStringLiteral(QET_EXAMPLES_DIR "/perceuse.qet"), &original);
|
||||
for (int i = 0 ; i < entries.size() ; ++i)
|
||||
if (entries.at(i).name.startsWith(QStringLiteral("folios/"))) { entries.removeAt(i); break; }
|
||||
QDomDocument back;
|
||||
QString error;
|
||||
QVERIFY(!QetContainer::join(entries, &back, &error));
|
||||
QVERIFY2(error.contains(QStringLiteral("missing")), qPrintable(error));
|
||||
}
|
||||
|
||||
// A crafted project.sqlite whose "element" is a view, not a table, is
|
||||
// refused: nothing in a file's schema is trusted.
|
||||
void viewInsteadOfTableRefused()
|
||||
{
|
||||
QDomDocument original;
|
||||
QList<QetZip::Entry> entries = splitFile(QStringLiteral(QET_EXAMPLES_DIR "/perceuse.qet"), &original);
|
||||
QVERIFY(!entries.isEmpty());
|
||||
for (QetZip::Entry &e : entries) {
|
||||
if (e.name != QLatin1String("project.sqlite")) continue;
|
||||
sqlite3 *db = nullptr;
|
||||
sqlite3_open(":memory:", &db);
|
||||
unsigned char *copy = static_cast<unsigned char *>(sqlite3_malloc64(sqlite3_uint64(e.data.size())));
|
||||
memcpy(copy, e.data.constData(), size_t(e.data.size()));
|
||||
sqlite3_deserialize(db, "main", copy, e.data.size(), e.data.size(),
|
||||
SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE);
|
||||
QCOMPARE(sqlite3_exec(db, "ALTER TABLE element RENAME TO element_data;"
|
||||
"CREATE VIEW element AS SELECT * FROM element_data;",
|
||||
nullptr, nullptr, nullptr), SQLITE_OK);
|
||||
sqlite3_int64 size = 0;
|
||||
unsigned char *bytes = sqlite3_serialize(db, "main", &size, 0);
|
||||
e.data = QByteArray(reinterpret_cast<const char *>(bytes), qsizetype(size));
|
||||
sqlite3_free(bytes);
|
||||
sqlite3_close(db);
|
||||
}
|
||||
QDomDocument back;
|
||||
QString error;
|
||||
QVERIFY(!QetContainer::join(entries, &back, &error));
|
||||
QVERIFY2(error.contains(QStringLiteral("element")), qPrintable(error));
|
||||
}
|
||||
|
||||
void notAProjectRefused()
|
||||
{
|
||||
QDomDocument back;
|
||||
QString error;
|
||||
QVERIFY(!QetContainer::join({{QStringLiteral("mimetype"), "application/zip", false}}, &back, &error));
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_qetcontainer)
|
||||
|
||||
#include "tst_qetcontainer.moc"
|
||||
@@ -0,0 +1,145 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#include <QtTest>
|
||||
|
||||
#include <QTemporaryDir>
|
||||
|
||||
#include "container/qetzip.h"
|
||||
#include "container/miniz/miniz.h"
|
||||
|
||||
// The zip a .qetz project is stored in (sources/container/qetzip.*):
|
||||
// entries come back as written, in order, stored ones stored; the same
|
||||
// entries give the same bytes; unsafe names are refused both ways.
|
||||
class tst_qetzip : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
static QList<QetZip::Entry> sample()
|
||||
{
|
||||
QList<QetZip::Entry> entries;
|
||||
entries.append({QStringLiteral("mimetype"), QByteArray("application/x-qelectrotech-project+zip"), false});
|
||||
QByteArray xml("<project>");
|
||||
for (int i = 0 ; i < 2000 ; ++i) xml += "<element name=\"K1\"/>";
|
||||
xml += "</project>";
|
||||
entries.append({QStringLiteral("project.xml"), xml, true});
|
||||
QByteArray picture;
|
||||
for (int i = 0 ; i < 4096 ; ++i) picture += char((i * 7919) & 0xff);
|
||||
entries.append({QStringLiteral("images/00.png"), picture, false});
|
||||
entries.append({QStringLiteral("folios/é ü.xml"), QByteArray("<diagram/>"), true});
|
||||
entries.append({QStringLiteral("empty"), QByteArray(), true});
|
||||
return entries;
|
||||
}
|
||||
|
||||
// a zip holding one entry named @p name, written past QetZip's checks
|
||||
static QByteArray rawZip(const char *name)
|
||||
{
|
||||
mz_zip_archive zip;
|
||||
mz_zip_zero_struct(&zip);
|
||||
mz_zip_writer_init_heap(&zip, 0, 0);
|
||||
mz_zip_writer_add_mem(&zip, name, "x", 1, MZ_DEFAULT_LEVEL);
|
||||
void *buffer = nullptr;
|
||||
size_t size = 0;
|
||||
mz_zip_writer_finalize_heap_archive(&zip, &buffer, &size);
|
||||
const QByteArray bytes(static_cast<const char *>(buffer), qsizetype(size));
|
||||
mz_zip_writer_end(&zip);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private slots:
|
||||
void roundTrip()
|
||||
{
|
||||
const QList<QetZip::Entry> in = sample();
|
||||
QString error;
|
||||
const QByteArray zip = QetZip::toBytes(in, &error);
|
||||
QVERIFY2(!zip.isEmpty(), qPrintable(error));
|
||||
QVERIFY(zip.startsWith("PK"));
|
||||
QVERIFY2(zip.size() < in.at(1).data.size() / 4, "the XML entry was not compressed");
|
||||
|
||||
QList<QetZip::Entry> out;
|
||||
QVERIFY2(QetZip::fromBytes(zip, &out, &error), qPrintable(error));
|
||||
QCOMPARE(out.size(), in.size());
|
||||
for (int i = 0 ; i < in.size() ; ++i) {
|
||||
QCOMPARE(out.at(i).name, in.at(i).name);
|
||||
QCOMPARE(out.at(i).data, in.at(i).data);
|
||||
if (!in.at(i).data.isEmpty())
|
||||
QCOMPARE(out.at(i).compress, in.at(i).compress);
|
||||
}
|
||||
// mimetype first and stored: its bytes appear as is after the header
|
||||
QCOMPARE(zip.indexOf("application/x-qelectrotech-project+zip"), 30 + int(qstrlen("mimetype")));
|
||||
}
|
||||
|
||||
void sameBytesTwice()
|
||||
{
|
||||
QCOMPARE(QetZip::toBytes(sample()), QetZip::toBytes(sample()));
|
||||
}
|
||||
|
||||
void refusesUnsafeNamesOnWrite_data()
|
||||
{
|
||||
QTest::addColumn<QString>("name");
|
||||
QTest::newRow("parent") << QStringLiteral("../x.xml");
|
||||
QTest::newRow("inner parent") << QStringLiteral("folios/../../x.xml");
|
||||
QTest::newRow("absolute") << QStringLiteral("/etc/x");
|
||||
QTest::newRow("drive") << QStringLiteral("C:/x");
|
||||
QTest::newRow("backslash") << QStringLiteral("folios\\x.xml");
|
||||
QTest::newRow("empty") << QString();
|
||||
}
|
||||
void refusesUnsafeNamesOnWrite()
|
||||
{
|
||||
QFETCH(QString, name);
|
||||
QString error;
|
||||
QVERIFY(QetZip::toBytes({{name, QByteArray("x"), true}}, &error).isEmpty());
|
||||
QVERIFY(!error.isEmpty());
|
||||
}
|
||||
|
||||
void refusesRepeatedNames()
|
||||
{
|
||||
QString error;
|
||||
QVERIFY(QetZip::toBytes({{QStringLiteral("a"), "1", true}, {QStringLiteral("a"), "2", true}}, &error).isEmpty());
|
||||
}
|
||||
|
||||
void refusesUnsafeNamesOnRead()
|
||||
{
|
||||
QList<QetZip::Entry> out;
|
||||
QString error;
|
||||
QVERIFY(QetZip::fromBytes(rawZip("ok/name.xml"), &out, &error));
|
||||
QVERIFY(!QetZip::fromBytes(rawZip("../evil.xml"), &out, &error));
|
||||
QVERIFY(out.isEmpty());
|
||||
// miniz will not write a name starting with '/': write "Xabs.xml"
|
||||
// and turn the X into '/' in both of its headers
|
||||
QByteArray absolute = rawZip("Xabs.xml");
|
||||
QCOMPARE(absolute.count("Xabs.xml"), 2);
|
||||
absolute.replace("Xabs.xml", "/abs.xml");
|
||||
QVERIFY(!QetZip::fromBytes(absolute, &out, &error));
|
||||
}
|
||||
|
||||
void refusesGarbage()
|
||||
{
|
||||
QList<QetZip::Entry> out;
|
||||
QString error;
|
||||
QVERIFY(!QetZip::fromBytes(QByteArray("not a zip at all"), &out, &error));
|
||||
QVERIFY(!error.isEmpty());
|
||||
QByteArray cut = QetZip::toBytes(sample());
|
||||
cut.chop(cut.size() / 2);
|
||||
QVERIFY(!QetZip::fromBytes(cut, &out, &error));
|
||||
}
|
||||
|
||||
void writeAndRead()
|
||||
{
|
||||
QTemporaryDir dir;
|
||||
QVERIFY(dir.isValid());
|
||||
const QString path = dir.filePath(QStringLiteral("p.qetz"));
|
||||
QString error;
|
||||
QVERIFY2(QetZip::write(path, sample(), &error), qPrintable(error));
|
||||
QList<QetZip::Entry> out;
|
||||
QVERIFY2(QetZip::read(path, &out, &error), qPrintable(error));
|
||||
QCOMPARE(out.size(), sample().size());
|
||||
QCOMPARE(out.at(1).data, sample().at(1).data);
|
||||
// a refused write leaves the file as it was
|
||||
QVERIFY(!QetZip::write(path, {{QStringLiteral("../x"), "x", true}}, &error));
|
||||
QVERIFY(QetZip::read(path, &out, &error));
|
||||
QCOMPARE(out.size(), sample().size());
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_qetzip)
|
||||
|
||||
#include "tst_qetzip.moc"
|
||||
@@ -30,9 +30,12 @@ class tst_resaveunchanged : public QObject
|
||||
|
||||
// --resave @p in to a new file, in a sandbox of its own (so a running
|
||||
// QElectroTech cannot answer instead); returns the new file's path.
|
||||
QString resave(const QString &in)
|
||||
QString resave(const QString &in, const QString &suffix = QStringLiteral(".qet"))
|
||||
{
|
||||
const QString out = m_dir.filePath(QStringLiteral("out%1.qet").arg(m_run));
|
||||
//Always p.qet, in its own folder: an untitled project takes its
|
||||
//file name as title, so two names would make two projects.
|
||||
QDir().mkpath(m_dir.filePath(QStringLiteral("run%1").arg(m_run)));
|
||||
const QString out = m_dir.filePath(QStringLiteral("run%1/p").arg(m_run) + suffix);
|
||||
const QString home = m_dir.filePath(QStringLiteral("home%1").arg(m_run++));
|
||||
QDir().mkpath(home);
|
||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
||||
@@ -282,6 +285,38 @@ private slots:
|
||||
QVERIFY2(read(saved).contains(b), "the arrow without a terminal was not kept");
|
||||
}
|
||||
|
||||
// Saved as a .qetz (the zipped project, #1440) and that saved again as a
|
||||
// .qet, a project is what it is saved straight as a .qet: the same
|
||||
// file but for when and where it was saved. The examples with pictures
|
||||
// (weneedpolonez), tables and links (industrial) and several title
|
||||
// blocks (m_000).
|
||||
void containerSavesTheSameProject_data()
|
||||
{
|
||||
QTest::addColumn<QString>("project");
|
||||
for (const char *name : {"Projet_vierge.qet", "industrial.qet", "m_000.qet",
|
||||
"weneedpolonez-Polonez_MR89_wiring_diagram.qet"})
|
||||
QTest::newRow(name) << QStringLiteral(QET_EXAMPLES_DIR "/") + QLatin1String(name);
|
||||
}
|
||||
void containerSavesTheSameProject()
|
||||
{
|
||||
QFETCH(QString, project);
|
||||
const QString straight = resave(project);
|
||||
QVERIFY2(!straight.isEmpty(), "--resave as .qet failed");
|
||||
const QString zipped = resave(project, QStringLiteral(".qetz"));
|
||||
QVERIFY2(!zipped.isEmpty(), "--resave as .qetz failed");
|
||||
QVERIFY2(read(zipped).startsWith("PK\x03\x04"), "the .qetz is not a zip");
|
||||
QVERIFY2(read(zipped).size() < read(straight).size() / 2, "the .qetz is not smaller");
|
||||
const QString back = resave(zipped);
|
||||
QVERIFY2(!back.isEmpty(), "opening the .qetz failed");
|
||||
|
||||
auto without_save_stamps = [](QByteArray xml) {
|
||||
static const QRegularExpression stamp(QStringLiteral(
|
||||
"\\s*<property [^>]*name=\"saved(filename|filepath|filenamedir|date|date-us|date-eu|time)\"[^>]*>[^<]*</property>"));
|
||||
return QString::fromUtf8(xml).remove(stamp);
|
||||
};
|
||||
QCOMPARE(without_save_stamps(read(back)), without_save_stamps(read(straight)));
|
||||
}
|
||||
|
||||
// A title-block value that is a single space is kept through two saves
|
||||
// (#973), and a value with accents comes back as it went in.
|
||||
void singleSpaceValueKept()
|
||||
|
||||
Reference in New Issue
Block a user