1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QTest>
#include <QVector>
#include <QWebSocketServer>
#include <QSignalSpy>
#include "accountfwd.h"
#include "pushnotifications.h"
#include "pushnotificationstestutils.h"
#include "logger.h"
#include <QStandardPaths>
#define RETURN_FALSE_ON_FAIL(expr) \
if (!(expr)) { \
return false; \
}
bool verifyCalledOnceWithAccount(QSignalSpy &spy, OCC::AccountPtr account)
{
RETURN_FALSE_ON_FAIL(spy.count() == 1);
auto accountFromSpy = spy.at(0).at(0).value<OCC::Account *>();
RETURN_FALSE_ON_FAIL(accountFromSpy == account.data());
return true;
}
bool failThreeAuthenticationAttempts(FakeWebSocketServer &fakeServer, OCC::AccountPtr account)
{
RETURN_FALSE_ON_FAIL(account);
RETURN_FALSE_ON_FAIL(account->pushNotifications());
account->pushNotifications()->setReconnectTimerInterval(0);
QSignalSpy authenticationFailedSpy(account->pushNotifications(), &OCC::PushNotifications::authenticationFailed);
// Let three authentication attempts fail
for (uint8_t i = 0; i < 3; ++i) {
RETURN_FALSE_ON_FAIL(fakeServer.waitForTextMessages());
RETURN_FALSE_ON_FAIL(fakeServer.textMessagesCount() == 2);
auto socket = fakeServer.socketForTextMessage(0);
fakeServer.clearTextMessages();
socket->sendTextMessage("err: Invalid credentials");
}
// Now the authenticationFailed Signal should be emitted
RETURN_FALSE_ON_FAIL(authenticationFailedSpy.wait());
RETURN_FALSE_ON_FAIL(authenticationFailedSpy.count() == 1);
return true;
}
class TestPushNotifications : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
}
void testTryReconnect_capabilitesReportPushNotificationsAvailable_reconnectForEver()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
// Let if fail a few times
QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
account->pushNotifications()->setup();
QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
account->setPushNotificationsReconnectInterval(0);
// Push notifications should try to reconnect
QVERIFY(fakeServer.authenticateAccount(account));
account->setPushNotificationsReconnectInterval(1000 * 60 * 2);
}
void testSetup_correctCredentials_authenticateAndEmitReady()
{
FakeWebSocketServer fakeServer;
std::unique_ptr<QSignalSpy> filesChangedSpy;
std::unique_ptr<QSignalSpy> notificationsChangedSpy;
std::unique_ptr<QSignalSpy> activitiesChangedSpy;
auto account = FakeWebSocketServer::createAccount();
QVERIFY(fakeServer.authenticateAccount(
account, [&](OCC::PushNotifications *pushNotifications) {
filesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::filesChanged));
notificationsChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::notificationsChanged));
activitiesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::activitiesChanged));
},
[&] {
QVERIFY(verifyCalledOnceWithAccount(*filesChangedSpy, account));
QVERIFY(verifyCalledOnceWithAccount(*notificationsChangedSpy, account));
QVERIFY(verifyCalledOnceWithAccount(*activitiesChangedSpy, account));
}));
}
void testOnWebSocketTextMessageReceived_notifyFileMessage_emitFilesChanged()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
const auto socket = fakeServer.authenticateAccount(account);
QVERIFY(socket);
QSignalSpy filesChangedSpy(account->pushNotifications(), &OCC::PushNotifications::filesChanged);
socket->sendTextMessage("notify_file");
// filesChanged signal should be emitted
QVERIFY(filesChangedSpy.wait());
QVERIFY(verifyCalledOnceWithAccount(filesChangedSpy, account));
}
void testOnWebSocketTextMessageReceived_notifyActivityMessage_emitNotification()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
const auto socket = fakeServer.authenticateAccount(account);
QVERIFY(socket);
QSignalSpy activitySpy(account->pushNotifications(), &OCC::PushNotifications::activitiesChanged);
QVERIFY(activitySpy.isValid());
// Send notify_file push notification
socket->sendTextMessage("notify_activity");
// notification signal should be emitted
QVERIFY(activitySpy.wait());
QVERIFY(verifyCalledOnceWithAccount(activitySpy, account));
}
void testOnWebSocketTextMessageReceived_notifyNotificationMessage_emitNotification()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
const auto socket = fakeServer.authenticateAccount(account);
QVERIFY(socket);
QSignalSpy notificationSpy(account->pushNotifications(), &OCC::PushNotifications::notificationsChanged);
QVERIFY(notificationSpy.isValid());
// Send notify_file push notification
socket->sendTextMessage("notify_notification");
// notification signal should be emitted
QVERIFY(notificationSpy.wait());
QVERIFY(verifyCalledOnceWithAccount(notificationSpy, account));
}
void testOnWebSocketTextMessageReceived_invalidCredentialsMessage_reconnectWebSocket()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
// Need to set reconnect timer interval to zero for tests
account->pushNotifications()->setReconnectTimerInterval(0);
// Wait for authentication attempt and then sent invalid credentials
QVERIFY(fakeServer.waitForTextMessages());
QCOMPARE(fakeServer.textMessagesCount(), 2);
const auto socket = fakeServer.socketForTextMessage(0);
const auto firstPasswordSent = fakeServer.textMessage(1);
QCOMPARE(firstPasswordSent, account->credentials()->password());
fakeServer.clearTextMessages();
socket->sendTextMessage("err: Invalid credentials");
// Wait for a new authentication attempt
QVERIFY(fakeServer.waitForTextMessages());
QCOMPARE(fakeServer.textMessagesCount(), 2);
const auto secondPasswordSent = fakeServer.textMessage(1);
QCOMPARE(secondPasswordSent, account->credentials()->password());
}
void testOnWebSocketError_connectionLost_emitConnectionLost()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
QSignalSpy connectionLostSpy(account->pushNotifications(), &OCC::PushNotifications::connectionLost);
QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
QVERIFY(connectionLostSpy.isValid());
// Wait for authentication and then sent a network error
QVERIFY(fakeServer.waitForTextMessages());
QCOMPARE(fakeServer.textMessagesCount(), 2);
auto socket = fakeServer.socketForTextMessage(0);
socket->abort();
QVERIFY(connectionLostSpy.wait());
// Account handled connectionLost signal and disabled push notifications
QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
}
void testSetup_maxConnectionAttemptsReached_disablePushNotifications()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
// Account disabled the push notifications
QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
}
void testOnWebSocketSslError_sslError_disablePushNotifications()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
QVERIFY(fakeServer.waitForTextMessages());
// FIXME: This a little bit ugly but I had no better idea how to trigger a error on the websocket client.
// The websocket that is retrieved through the server is not connected to the ssl error signal.
auto pushNotificationsWebSocketChildren = account->pushNotifications()->findChildren<QWebSocket *>();
QVERIFY(pushNotificationsWebSocketChildren.size() == 1);
emit pushNotificationsWebSocketChildren[0]->sslErrors(QList<QSslError>());
// Account handled connectionLost signal and the authenticationFailed Signal should be emitted
QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
}
void testAccount_web_socket_connectionLost_emitNotificationsDisabled()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
// Need to set reconnect timer interval to zero for tests
account->pushNotifications()->setReconnectTimerInterval(0);
const auto socket = fakeServer.authenticateAccount(account);
QVERIFY(socket);
QSignalSpy connectionLostSpy(account->pushNotifications(), &OCC::PushNotifications::connectionLost);
QVERIFY(connectionLostSpy.isValid());
QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
QVERIFY(pushNotificationsDisabledSpy.isValid());
// Wait for authentication and then sent a network error
socket->abort();
QVERIFY(pushNotificationsDisabledSpy.wait());
QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
QCOMPARE(connectionLostSpy.count(), 1);
const auto accountSent = pushNotificationsDisabledSpy.at(0).at(0).value<OCC::AccountPtr>();
QCOMPARE(accountSent.data(), account.data());
}
void testAccount_web_socket_authenticationFailed_emitNotificationsDisabled()
{
FakeWebSocketServer fakeServer;
auto account = FakeWebSocketServer::createAccount();
QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
QVERIFY(pushNotificationsDisabledSpy.isValid());
QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
// Now the pushNotificationsDisabled Signal should be emitted
QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
const auto accountSent = pushNotificationsDisabledSpy.at(0).at(0).value<OCC::AccountPtr>();
QCOMPARE(accountSent.data(), account.data());
}
void testPingTimeout_pingTimedOut_reconnect()
{
FakeWebSocketServer fakeServer;
std::unique_ptr<QSignalSpy> filesChangedSpy;
std::unique_ptr<QSignalSpy> notificationsChangedSpy;
std::unique_ptr<QSignalSpy> activitiesChangedSpy;
auto account = FakeWebSocketServer::createAccount();
QVERIFY(fakeServer.authenticateAccount(account));
// Set the ping timeout interval to zero and check if the server attempts to authenticate again
fakeServer.clearTextMessages();
account->pushNotifications()->setPingInterval(0);
QVERIFY(fakeServer.authenticateAccount(
account, [&](OCC::PushNotifications *pushNotifications) {
filesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::filesChanged));
notificationsChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::notificationsChanged));
activitiesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::activitiesChanged));
},
[&] {
QVERIFY(verifyCalledOnceWithAccount(*filesChangedSpy, account));
QVERIFY(verifyCalledOnceWithAccount(*notificationsChangedSpy, account));
QVERIFY(verifyCalledOnceWithAccount(*activitiesChangedSpy, account));
}));
}
};
QTEST_GUILESS_MAIN(TestPushNotifications)
#include "testpushnotifications.moc"
|