File indexing completed on 2024-04-21 05:27:32

0001 /*
0002 SPDX-FileCopyrightText: 2022 David Edmundson <davidedmundson@kde.org>
0003 
0004 SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QSignalSpy>
0008 #include <QTest>
0009 
0010 #include "../greeter/pamauthenticator.h"
0011 
0012 // This test runs under the expectation that
0013 // we are run with LD_PRELOAD=libpam_wrapper.so pamTest
0014 
0015 class PamTest : public QObject
0016 {
0017     Q_OBJECT
0018 public:
0019     PamTest();
0020 private Q_SLOTS:
0021     void testLogin();
0022 };
0023 
0024 PamTest::PamTest()
0025 {
0026     if (!qgetenv("LD_PRELOAD").contains("libpam_wrapper.so")) {
0027         qFatal("This test must be run with pam_wrapper. See ctest");
0028     }
0029 
0030     qputenv("PAM_WRAPPER", "1");
0031     qputenv("PAM_WRAPPER_DEBUGLEVEL", "2"); // DEBUG level
0032     qputenv("PAM_WRAPPER_SERVICE_DIR", QFINDTESTDATA("data").toUtf8());
0033     qputenv("PAM_MATRIX_PASSWD", QFINDTESTDATA("data/test_db").toUtf8());
0034 }
0035 
0036 void PamTest::testLogin()
0037 {
0038     PamAuthenticator auth("test_service", "test_user");
0039     QSignalSpy promptSpy(&auth, &PamAuthenticator::prompt);
0040     QSignalSpy promptForSecretSpy(&auth, &PamAuthenticator::promptForSecret);
0041     QSignalSpy succeededSpy(&auth, &PamAuthenticator::succeeded);
0042     QSignalSpy failedSpy(&auth, &PamAuthenticator::failed);
0043     QSignalSpy busyChangedSpy(&auth, &PamAuthenticator::busyChanged);
0044 
0045     // invalid password
0046     auth.tryUnlock();
0047 
0048     QVERIFY(promptForSecretSpy.wait());
0049     auth.respond("not_my_password");
0050     QVERIFY(failedSpy.wait());
0051 
0052     QVERIFY(promptSpy.count() == 0);
0053     QVERIFY(succeededSpy.count() == 0);
0054     QVERIFY(busyChangedSpy.count() == 2); // changed to busy and back again.
0055 
0056     // try again, with the right password
0057     auth.tryUnlock();
0058     QVERIFY(promptForSecretSpy.wait());
0059     auth.respond("my_password");
0060     QVERIFY(succeededSpy.wait());
0061 
0062     QVERIFY(busyChangedSpy.count() == 4);
0063 }
0064 
0065 QTEST_MAIN(PamTest)
0066 #include "pamtest.moc"