File indexing completed on 2024-05-12 05:21:34

0001 /*
0002   SPDX-FileCopyrightText: 2010 BetterInbox <contact@betterinbox.com>
0003   SPDX-FileContributor: Christophe Laveault <christophe@betterinbox.com>
0004   SPDX-FileContributor: Gregory Schlomoff <gregory.schlomoff@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include <QMutex>
0012 #include <QThread>
0013 class QTcpServer;
0014 class QTcpSocket;
0015 Q_DECLARE_METATYPE(QList<QByteArray>)
0016 
0017 class FakeServer : public QThread
0018 {
0019     Q_OBJECT
0020 
0021 public:
0022     explicit FakeServer(QObject *parent = nullptr);
0023     ~FakeServer() override;
0024 
0025     void startAndWait();
0026     void run() override;
0027 
0028     static QByteArray greeting();
0029     static QList<QByteArray> greetingAndEhlo(bool multiline = true);
0030     static QList<QByteArray> bye();
0031 
0032     void setScenario(const QList<QByteArray> &scenario);
0033     void addScenario(const QList<QByteArray> &scenario);
0034     void addScenarioFromFile(const QString &fileName);
0035     bool isScenarioDone(int scenarioNumber) const;
0036     bool isAllScenarioDone() const;
0037 
0038 private Q_SLOTS:
0039     void newConnection();
0040     void dataAvailable();
0041     void started();
0042 
0043 private:
0044     void writeServerPart(int scenarioNumber);
0045     void readClientPart(int scenarioNumber);
0046 
0047     QList<QList<QByteArray>> m_scenarios;
0048     QTcpServer *m_tcpServer;
0049     mutable QMutex m_mutex;
0050     QList<QTcpSocket *> m_clientSockets;
0051 };