diff options
author | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2014-12-15 23:54:10 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2014-12-15 23:54:21 +0100 |
commit | b5e5c718fa1f7821d04ac684eb21003caa88b93d (patch) | |
tree | 93b463524b4b46c5aaed57cbeb1fda43d2cbe892 | |
parent | ac9ab8b35d2865b4afcad1fa0cc9f86117a235a8 (diff) |
Provide unit test for password protected PDFs
-rw-r--r-- | src/pdf/qpdfdocument.cpp | 9 | ||||
-rw-r--r-- | src/pdf/qpdfdocument_p.h | 1 | ||||
-rw-r--r-- | tests/auto/qpdfdocument/pdf-sample.protected.pdf | bin | 0 -> 9138 bytes | |||
-rw-r--r-- | tests/auto/qpdfdocument/tst_qpdfdocument.cpp | 11 |
4 files changed, 19 insertions, 2 deletions
diff --git a/src/pdf/qpdfdocument.cpp b/src/pdf/qpdfdocument.cpp index a202bc9..b6fd203 100644 --- a/src/pdf/qpdfdocument.cpp +++ b/src/pdf/qpdfdocument.cpp @@ -96,7 +96,10 @@ QPdfDocument::Error QPdfDocumentPrivate::load(QIODevice *newDevice, bool transfe password = documentPassword.toUtf8(); doc = FPDF_LoadCustomDocument(this, password.constData()); - setErrorCode(); + if (doc) + clearError(); + else + setErrorCode(); return lastError; } @@ -147,7 +150,9 @@ void QPdfDocumentPrivate::tryLoadDocument() Q_ASSERT(!doc); doc = FPDFAvail_GetDocument(avail, password); - if (!doc) { + if (doc) { + clearError(); + } else { setErrorCode(); if (lastError == QPdfDocument::IncorrectPasswordError) emit q->passwordRequired(); diff --git a/src/pdf/qpdfdocument_p.h b/src/pdf/qpdfdocument_p.h index b2e1aaf..4d274b1 100644 --- a/src/pdf/qpdfdocument_p.h +++ b/src/pdf/qpdfdocument_p.h @@ -46,6 +46,7 @@ public: static int fpdf_GetBlock(void* param, unsigned long position, unsigned char* pBuf, unsigned long size); static void fpdf_AddSegment(struct _FX_DOWNLOADHINTS* pThis, size_t offset, size_t size); void setErrorCode(); + void clearError() { lastError = QPdfDocument::NoError; } }; QT_END_NAMESPACE diff --git a/tests/auto/qpdfdocument/pdf-sample.protected.pdf b/tests/auto/qpdfdocument/pdf-sample.protected.pdf Binary files differnew file mode 100644 index 0000000..d76fdd1 --- /dev/null +++ b/tests/auto/qpdfdocument/pdf-sample.protected.pdf diff --git a/tests/auto/qpdfdocument/tst_qpdfdocument.cpp b/tests/auto/qpdfdocument/tst_qpdfdocument.cpp index 3065912..6ebd197 100644 --- a/tests/auto/qpdfdocument/tst_qpdfdocument.cpp +++ b/tests/auto/qpdfdocument/tst_qpdfdocument.cpp @@ -18,6 +18,7 @@ private slots: void pageCount(); void loadFromIODevice(); void loadAsync(); + void password(); }; struct TemporaryPdf: public QTemporaryFile @@ -89,6 +90,16 @@ void tst_QPdfDocument::loadAsync() QCOMPARE(doc.pageCount(), 2); } +void tst_QPdfDocument::password() +{ + QPdfDocument doc; + QCOMPARE(doc.pageCount(), 0); + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.protected.pdf")), QPdfDocument::IncorrectPasswordError); + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.protected.pdf"), QStringLiteral("WrongPassword")), QPdfDocument::IncorrectPasswordError); + QCOMPARE(doc.load(QFINDTESTDATA("pdf-sample.protected.pdf"), QStringLiteral("Qt")), QPdfDocument::NoError); + QCOMPARE(doc.pageCount(), 1); +} + QTEST_MAIN(tst_QPdfDocument) #include "tst_qpdfdocument.moc" |