File indexing completed on 2025-01-26 04:52:14
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2005 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "testldapclient.h" 0008 0009 #include <QDebug> 0010 0011 #include <KLDAPCore/LdapObject> 0012 0013 #include <QEventLoop> 0014 0015 #include <QApplication> 0016 #include <QCommandLineParser> 0017 #include <QStandardPaths> 0018 #include <cstdlib> 0019 0020 int main(int argc, char *argv[]) 0021 { 0022 QApplication app(argc, argv); 0023 QStandardPaths::setTestModeEnabled(true); 0024 QCommandLineParser parser; 0025 parser.addVersionOption(); 0026 parser.addHelpOption(); 0027 parser.process(app); 0028 0029 TestLDAPClient test; 0030 test.setup(); 0031 test.runAll(); 0032 test.cleanup(); 0033 qDebug() << "All tests OK."; 0034 return 0; 0035 } 0036 0037 TestLDAPClient::TestLDAPClient() = default; 0038 0039 void TestLDAPClient::setup() 0040 { 0041 } 0042 0043 void TestLDAPClient::runAll() 0044 { 0045 testIntevation(); 0046 } 0047 0048 bool TestLDAPClient::check(const QString &txt, QString a, QString b) 0049 { 0050 if (a.isEmpty()) { 0051 a.clear(); 0052 } 0053 0054 if (b.isEmpty()) { 0055 b.clear(); 0056 } 0057 0058 if (a == b) { 0059 qDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'..." 0060 << "ok"; 0061 } else { 0062 qDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'..." 0063 << "KO !"; 0064 cleanup(); 0065 exit(1); 0066 } 0067 0068 return true; 0069 } 0070 0071 void TestLDAPClient::cleanup() 0072 { 0073 mClient = nullptr; 0074 } 0075 0076 void TestLDAPClient::testIntevation() 0077 { 0078 qDebug(); 0079 mClient = new KLDAPWidgets::LdapClient(0, this); 0080 0081 #if 0 0082 mClient->setHost("ca.intevation.de"); 0083 mClient->setPort("389"); 0084 mClient->setBase("o=Intevation GmbH,c=de"); 0085 #endif 0086 0087 // Same list as in kaddressbook's ldapsearchdialog 0088 QStringList attrs; 0089 attrs << QStringLiteral("l") << QStringLiteral("Company") << QStringLiteral("co") << QStringLiteral("department") << QStringLiteral("description") 0090 << QStringLiteral("mail") << QStringLiteral("facsimileTelephoneNumber") << QStringLiteral("cn") << QStringLiteral("homePhone") 0091 << QStringLiteral("mobile") << QStringLiteral("o") << QStringLiteral("pager") << QStringLiteral("postalAddress") << QStringLiteral("st") 0092 << QStringLiteral("street") << QStringLiteral("title") << QStringLiteral("uid") << QStringLiteral("telephoneNumber") << QStringLiteral("postalCode") 0093 << QStringLiteral("objectClass"); 0094 // the list from ldapclient.cpp 0095 // attrs << "cn" << "mail" << "givenname" << "sn" << "objectClass"; 0096 mClient->setAttributes(attrs); 0097 0098 // Taken from LdapSearch 0099 /* 0100 QString mSearchText = QString::fromUtf8( "Till" ); 0101 QString filter = QString( "&(|(objectclass=person)(objectclass=groupOfNames)(mail=*))" 0102 "(|(cn=%1*)(mail=%2*)(givenName=%3*)(sn=%4*))" ) 0103 .arg( mSearchText ).arg( mSearchText ).arg( mSearchText ).arg( mSearchText ); 0104 */ 0105 0106 // For some reason a fromUtf8 broke the search for me (no results). 0107 // But this certainly looks fishy, it might break on non-utf8 systems. 0108 QString filter = QStringLiteral( 0109 "&(|(objectclass=person)(objectclass=groupofnames)(mail=*))" 0110 "(|(cn=*Ägypten MDK*)(sn=*Ägypten MDK*))"); 0111 0112 connect(mClient, &KLDAPWidgets::LdapClient::result, this, &TestLDAPClient::slotLDAPResult); 0113 connect(mClient, &KLDAPWidgets::LdapClient::done, this, &TestLDAPClient::slotLDAPDone); 0114 connect(mClient, &KLDAPWidgets::LdapClient::error, this, &TestLDAPClient::slotLDAPError); 0115 mClient->startQuery(filter); 0116 0117 QEventLoop eventLoop; 0118 connect(this, &TestLDAPClient::leaveModality, &eventLoop, &QEventLoop::quit); 0119 eventLoop.exec(QEventLoop::ExcludeUserInputEvents); 0120 0121 delete mClient; 0122 mClient = nullptr; 0123 } 0124 0125 // from kaddressbook... ugly though... 0126 static QString asUtf8(const QByteArray &val) 0127 { 0128 if (val.isEmpty()) { 0129 return {}; 0130 } 0131 0132 const char *data = val.data(); 0133 0134 // QString::fromUtf8() bug workaround 0135 if (data[val.size() - 1] == '\0') { 0136 return QString::fromUtf8(data, val.size() - 1); 0137 } else { 0138 return QString::fromUtf8(data, val.size()); 0139 } 0140 } 0141 0142 static QString join(const KLDAPCore::LdapAttrValue &lst, const QString &sep) 0143 { 0144 QString res; 0145 bool already = false; 0146 for (KLDAPCore::LdapAttrValue::ConstIterator it = lst.begin(); it != lst.end(); ++it) { 0147 if (already) { 0148 res += sep; 0149 } 0150 0151 already = true; 0152 res += asUtf8(*it); 0153 } 0154 0155 return res; 0156 } 0157 0158 void TestLDAPClient::slotLDAPResult(const KLDAPWidgets::LdapClient &, const KLDAPCore::LdapObject &obj) 0159 { 0160 QString cn = join(obj.attributes()[QStringLiteral("cn")], QStringLiteral(", ")); 0161 qDebug() << " cn:" << cn; 0162 Q_ASSERT(!obj.attributes()[QStringLiteral("mail")].isEmpty()); 0163 QString mail = join(obj.attributes()[QStringLiteral("mail")], QStringLiteral(", ")); 0164 qDebug() << " mail:" << mail; 0165 Q_ASSERT(mail.contains(QLatin1Char('@'))); 0166 } 0167 0168 void TestLDAPClient::slotLDAPError(const QString &err) 0169 { 0170 qDebug() << err; 0171 ::exit(1); 0172 } 0173 0174 void TestLDAPClient::slotLDAPDone() 0175 { 0176 qDebug(); 0177 Q_EMIT leaveModality(); 0178 } 0179 0180 #include "moc_testldapclient.cpp"