summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-11-15 21:42:23 (GMT)
committerMontel Laurent <montel@kde.org>2016-11-15 21:42:23 (GMT)
commit3adaa075aa6284b026ea6fb706e4e50949b725f8 (patch)
tree96b979fc0bbc898e6316920b74f55bc95d40bc08
parent40d0a5adf74717a5244b33d416bbf1e618d72a2e (diff)
Improve createphishingurldatabasejob
-rw-r--r--webengineviewer/src/CMakeLists.txt2
-rw-r--r--webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt8
-rw-r--r--webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp60
-rw-r--r--webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h7
-rw-r--r--webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.cpp12
-rw-r--r--webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.h2
6 files changed, 76 insertions, 15 deletions
diff --git a/webengineviewer/src/CMakeLists.txt b/webengineviewer/src/CMakeLists.txt
index a2172aa..5970414 100644
--- a/webengineviewer/src/CMakeLists.txt
+++ b/webengineviewer/src/CMakeLists.txt
@@ -34,6 +34,7 @@ set(libwebengineviewer_checkphishingurl_SRCS
checkphishingurl/checkphishingurljob.cpp
checkphishingurl/checkphishingurlcache.cpp
checkphishingurl/createphishingurldatabasejob.cpp
+ checkphishingurl/checkphishingurlfromlocaldatabasejob.cpp
)
set(libwebengineviewer_interceptor_SRCS
@@ -147,6 +148,7 @@ ecm_generate_headers(WebEngineViewer_Camelcasewebengine_checkurl_HEADERS
CheckPhishingUrlJob
CheckPhishingUrlCache
CreatePhishingUrlDataBaseJob
+ CheckPhishingUrlFromLocalDataBaseJob
REQUIRED_HEADERS WebEngineViewer_webengine_checkurl_HEADERS
PREFIX WebEngineViewer
RELATIVE checkphishingurl
diff --git a/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt b/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt
index ac870a2..a98876c 100644
--- a/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt
+++ b/webengineviewer/src/checkphishingurl/autotests/CMakeLists.txt
@@ -3,6 +3,12 @@ ecm_qt_declare_logging_category(testwebengineviewer_webengine_common_SRCS HEADER
ecm_add_test(checkphishingurljobtest.cpp ../checkphishingurljob.cpp ${testwebengineviewer_webengine_common_SRCS}
TEST_NAME checkphishingurljobtest
- NAME_PREFIX "messageviewer-checkphishingurl-"
+ NAME_PREFIX "webengineview-checkphishingurl-"
+ LINK_LIBRARIES Qt5::Test Qt5::WebKitWidgets KF5::WebEngineViewer
+ )
+
+ecm_add_test(checkphishingurlfromlocaldatabasejobtest.cpp ../checkphishingurlfromlocaldatabasejob.cpp ${testwebengineviewer_webengine_common_SRCS}
+ TEST_NAME checkphishingurlfromlocaldatabasetest
+ NAME_PREFIX "webengineview-checkphishingurl-"
LINK_LIBRARIES Qt5::Test Qt5::WebKitWidgets KF5::WebEngineViewer
)
diff --git a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp
index 752541e..6df6ed6 100644
--- a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp
+++ b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp
@@ -24,7 +24,7 @@
#include <QNetworkConfigurationManager>
#include <QJsonDocument>
-//#define DEBUG_JSON_REQUEST 1
+#define DEBUG_JSON_REQUEST 1
using namespace WebEngineViewer;
CreatePhishingUrlDataBaseJob::CreatePhishingUrlDataBaseJob(QObject *parent)
@@ -32,7 +32,7 @@ CreatePhishingUrlDataBaseJob::CreatePhishingUrlDataBaseJob(QObject *parent)
mDataBaseDownloadNeeded(FullDataBase)
{
mNetworkAccessManager = new QNetworkAccessManager(this);
- connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &CreatePhishingUrlDataBaseJob::slotCheckUrlFinished);
+ connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &CreatePhishingUrlDataBaseJob::slotDownloadDataBaseFinished);
connect(mNetworkAccessManager, &QNetworkAccessManager::sslErrors, this, &CreatePhishingUrlDataBaseJob::slotSslErrors);
}
@@ -66,6 +66,7 @@ void CreatePhishingUrlDataBaseJob::start()
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
const QByteArray baPostData = jsonRequest();
+ Q_EMIT debugJson(baPostData);
qCDebug(WEBENGINEVIEWER_LOG) << " postData.toJson()" << baPostData;
//curl -H "Content-Type: application/json" -X POST -d '{"client":{"clientId":"KDE","clientVersion":"5.4.0"},"threatInfo":{"platformTypes":["WINDOWS"],"threatEntries":[{"url":"http://www.kde.org"}],"threatEntryTypes":["URL"],"threatTypes":["MALWARE"]}}' https://safebrowsing.googleapis.com/v4/threatMatches:find?key=AIzaSyBS62pXATjabbH2RM_jO2EzDg1mTMHlnyo
QNetworkReply *reply = mNetworkAccessManager->post(request, baPostData);
@@ -73,6 +74,11 @@ void CreatePhishingUrlDataBaseJob::start()
}
}
+void CreatePhishingUrlDataBaseJob::setDataBaseState(const QString &value)
+{
+ mDataBaseState = value;
+}
+
void CreatePhishingUrlDataBaseJob::slotError(QNetworkReply::NetworkError error)
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
@@ -110,21 +116,28 @@ QByteArray CreatePhishingUrlDataBaseJob::jsonRequest() const
clientMap.insert(QStringLiteral("clientVersion"), QStringLiteral("5.4.0")); //FIXME
map.insert(QStringLiteral("client"), clientMap);
+ QVariantList listUpdateRequests;
+
QVariantMap threatMap;
- const QVariantList platformList = { QStringLiteral("WINDOWS") };
- threatMap.insert(QStringLiteral("platformTypes"), platformList);
- const QVariantList threatTypesList = { QStringLiteral("MALWARE") };
- threatMap.insert(QStringLiteral("threatTypes"), threatTypesList);
- const QVariantList threatEntryTypesList = { QStringLiteral("URL") };
- threatMap.insert(QStringLiteral("threatEntryTypes"), threatEntryTypesList);
+ threatMap.insert(QStringLiteral("platformType"), QStringLiteral("WINDOWS"));
+ threatMap.insert(QStringLiteral("threatType"), QStringLiteral("MALWARE"));
+ threatMap.insert(QStringLiteral("threatEntryType"), QStringLiteral("URL"));
//Define state when we want to define update database. Empty is full.
- threatMap.insert(QStringLiteral("state"), QString());
+ switch(mDataBaseDownloadNeeded) {
+ case FullDataBase:
+ threatMap.insert(QStringLiteral("state"), QString());
+ break;
+ case UpdateDataBase:
+ threatMap.insert(QStringLiteral("state"), mDataBaseState);
+ break;
+ }
+ listUpdateRequests.append(threatMap);
//TODO define contraints
- map.insert(QStringLiteral("listUpdateRequests"), threatMap);
+ map.insert(QStringLiteral("listUpdateRequests"), listUpdateRequests);
const QJsonDocument postData = QJsonDocument::fromVariant(map);
#ifdef DEBUG_JSON_REQUEST
@@ -132,6 +145,7 @@ QByteArray CreatePhishingUrlDataBaseJob::jsonRequest() const
#else
const QByteArray baPostData = postData.toJson(QJsonDocument::Compact);
#endif
+ //qDebug()<<" baPostData "<<baPostData;
return baPostData;
}
@@ -141,9 +155,29 @@ void CreatePhishingUrlDataBaseJob::setDataBaseDownloadNeeded(CreatePhishingUrlDa
mDataBaseDownloadNeeded = type;
}
-void CreatePhishingUrlDataBaseJob::slotCheckUrlFinished(QNetworkReply *reply)
+void CreatePhishingUrlDataBaseJob::slotDownloadDataBaseFinished(QNetworkReply *reply)
{
- Q_EMIT debugJsonResult(reply->readAll());
+ const QByteArray returnValue(reply->readAll());
+ Q_EMIT debugJsonResult(returnValue);
+ parseResult(returnValue);
reply->deleteLater();
- deleteLater();
+ //TODO deleteLater ?
+}
+
+void CreatePhishingUrlDataBaseJob::parseResult(const QByteArray &value)
+{
+ QJsonDocument document = QJsonDocument::fromJson(value);
+ if (document.isNull()) {
+ //TODO
+ deleteLater();
+ } else {
+ const QVariantMap answer = document.toVariant().toMap();
+ if (answer.isEmpty()) {
+ //TODO
+ } else {
+ const QVariantList info = answer.value(QStringLiteral("listUpdateResponses")).toList();
+ //TODO
+ }
+ //TODO
+ }
}
diff --git a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h
index dbcc54b..3c6288e 100644
--- a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h
+++ b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h
@@ -43,6 +43,8 @@ public:
void start();
+ void setDataBaseState(const QString &value);
+
void setDataBaseDownloadNeeded(WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownload type);
QByteArray jsonRequest() const;
@@ -50,13 +52,16 @@ public:
Q_SIGNALS:
void finished();
void debugJsonResult(const QByteArray &ba);
+ void debugJson(const QByteArray &ba);
private:
QString apiKey() const;
- void slotCheckUrlFinished(QNetworkReply *reply);
+ void slotDownloadDataBaseFinished(QNetworkReply *reply);
void slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error);
void slotError(QNetworkReply::NetworkError error);
+ void parseResult(const QByteArray &value);
+ QString mDataBaseState;
DataBaseDownload mDataBaseDownloadNeeded;
QNetworkAccessManager *mNetworkAccessManager;
};
diff --git a/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.cpp b/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.cpp
index 8259c4e..a530652 100644
--- a/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.cpp
+++ b/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.cpp
@@ -38,6 +38,12 @@ CreatePhisingUrlDataBaseGui::CreatePhisingUrlDataBaseGui(QWidget *parent)
mResult->setReadOnly(true);
mResult->setAcceptRichText(false);
layout->addWidget(mResult);
+
+ mJson = new QTextEdit(this);
+ mJson->setReadOnly(true);
+ mJson->setAcceptRichText(false);
+ layout->addWidget(mJson);
+
QPushButton *button = new QPushButton(QStringLiteral("DownLoad full database"), this);
connect(button, &QPushButton::clicked, this, &CreatePhisingUrlDataBaseGui::slotDownloadFullDatabase);
layout->addWidget(button);
@@ -52,9 +58,15 @@ void CreatePhisingUrlDataBaseGui::slotDownloadFullDatabase()
{
WebEngineViewer::CreatePhishingUrlDataBaseJob *job = new WebEngineViewer::CreatePhishingUrlDataBaseJob(this);
connect(job, &WebEngineViewer::CreatePhishingUrlDataBaseJob::debugJsonResult, this, &CreatePhisingUrlDataBaseGui::slotResult);
+ connect(job, &WebEngineViewer::CreatePhishingUrlDataBaseJob::debugJson, this, &CreatePhisingUrlDataBaseGui::slotDebugJSon);
job->start();
}
+void CreatePhisingUrlDataBaseGui::slotDebugJSon(const QByteArray &data)
+{
+ mJson->setText(QString::fromLatin1(data));
+}
+
void CreatePhisingUrlDataBaseGui::slotResult(const QByteArray &data)
{
mResult->setText(QString::fromLatin1(data));
diff --git a/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.h b/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.h
index 6591b2f..b767fbc 100644
--- a/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.h
+++ b/webengineviewer/src/checkphishingurl/tests/createphishingurldatabasegui.h
@@ -31,8 +31,10 @@ public:
private Q_SLOTS:
void slotResult(const QByteArray &data);
void slotDownloadFullDatabase();
+ void slotDebugJSon(const QByteArray &data);
private:
QTextEdit *mResult;
+ QTextEdit *mJson;
};
#endif // CREATEPHISHINGURLDATABASEGUI_H