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
|
/*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud GmbH
* SPDX-License-Identifier: CC0-1.0
*
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*/
#include <QtTest>
#include "cmd/netrcparser.h"
#include "logger.h"
using namespace OCC;
namespace {
const char testfileC[] = "netrctest";
const char testfileWithDefaultC[] = "netrctestDefault";
const char testfileEmptyC[] = "netrctestEmpty";
}
class TestNetrcParser : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
QFile netrc(testfileC);
QVERIFY(netrc.open(QIODevice::WriteOnly));
netrc.write("machine foo login bar password baz\n");
netrc.write("machine broken login bar2 dontbelonghere password baz2 extratokens dontcare andanother\n");
netrc.write("machine\nfunnysplit\tlogin bar3 password baz3\n");
netrc.write("machine frob login \"user with spaces\" password 'space pwd'\n");
QFile netrcWithDefault(testfileWithDefaultC);
QVERIFY(netrcWithDefault.open(QIODevice::WriteOnly));
netrcWithDefault.write("machine foo login bar password baz\n");
netrcWithDefault.write("default login user password pass\n");
QFile netrcEmpty(testfileEmptyC);
QVERIFY(netrcEmpty.open(QIODevice::WriteOnly));
}
void cleanupTestCase() {
QVERIFY(QFile::remove(testfileC));
QVERIFY(QFile::remove(testfileWithDefaultC));
QVERIFY(QFile::remove(testfileEmptyC));
}
void testValidNetrc() {
NetrcParser parser(testfileC);
QVERIFY(parser.parse());
QCOMPARE(parser.find("foo"), qMakePair(QStringLiteral("bar"), QStringLiteral("baz")));
QCOMPARE(parser.find("broken"), qMakePair(QStringLiteral("bar2"), QString()));
QCOMPARE(parser.find("funnysplit"), qMakePair(QStringLiteral("bar3"), QStringLiteral("baz3")));
QEXPECT_FAIL("", "Current implementation do not support spaces in username or password", Continue);
QCOMPARE(parser.find("frob"), qMakePair(QStringLiteral("user with spaces"), QStringLiteral("space pwd")));
}
void testEmptyNetrc() {
NetrcParser parser(testfileEmptyC);
QVERIFY(!parser.parse());
QCOMPARE(parser.find("foo"), qMakePair(QString(), QString()));
}
void testValidNetrcWithDefault() {
NetrcParser parser(testfileWithDefaultC);
QVERIFY(parser.parse());
QCOMPARE(parser.find("foo"), qMakePair(QStringLiteral("bar"), QStringLiteral("baz")));
QCOMPARE(parser.find("dontknow"), qMakePair(QStringLiteral("user"), QStringLiteral("pass")));
}
void testInvalidNetrc() {
NetrcParser parser("/invalid");
QVERIFY(!parser.parse());
}
};
QTEST_APPLESS_MAIN(TestNetrcParser)
#include "testnetrcparser.moc"
|