testnetrcparser.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. * */
  6. #include <QtTest>
  7. #include "cmd/netrcparser.h"
  8. using namespace OCC;
  9. namespace {
  10. const char testfileC[] = "netrctest";
  11. const char testfileWithDefaultC[] = "netrctestDefault";
  12. const char testfileEmptyC[] = "netrctestEmpty";
  13. }
  14. class TestNetrcParser : public QObject
  15. {
  16. Q_OBJECT
  17. private slots:
  18. void initTestCase() {
  19. QFile netrc(testfileC);
  20. QVERIFY(netrc.open(QIODevice::WriteOnly));
  21. netrc.write("machine foo login bar password baz\n");
  22. netrc.write("machine broken login bar2 dontbelonghere password baz2 extratokens dontcare andanother\n");
  23. netrc.write("machine\nfunnysplit\tlogin bar3 password baz3\n");
  24. netrc.write("machine frob login \"user with spaces\" password 'space pwd'\n");
  25. QFile netrcWithDefault(testfileWithDefaultC);
  26. QVERIFY(netrcWithDefault.open(QIODevice::WriteOnly));
  27. netrcWithDefault.write("machine foo login bar password baz\n");
  28. netrcWithDefault.write("default login user password pass\n");
  29. QFile netrcEmpty(testfileEmptyC);
  30. QVERIFY(netrcEmpty.open(QIODevice::WriteOnly));
  31. }
  32. void cleanupTestCase() {
  33. QVERIFY(QFile::remove(testfileC));
  34. QVERIFY(QFile::remove(testfileWithDefaultC));
  35. QVERIFY(QFile::remove(testfileEmptyC));
  36. }
  37. void testValidNetrc() {
  38. NetrcParser parser(testfileC);
  39. QVERIFY(parser.parse());
  40. QCOMPARE(parser.find("foo"), qMakePair(QString("bar"), QString("baz")));
  41. QCOMPARE(parser.find("broken"), qMakePair(QString("bar2"), QString()));
  42. QCOMPARE(parser.find("funnysplit"), qMakePair(QString("bar3"), QString("baz3")));
  43. QCOMPARE(parser.find("frob"), qMakePair(QString("user with spaces"), QString("space pwd")));
  44. }
  45. void testEmptyNetrc() {
  46. NetrcParser parser(testfileEmptyC);
  47. QVERIFY(!parser.parse());
  48. QCOMPARE(parser.find("foo"), qMakePair(QString(), QString()));
  49. }
  50. void testValidNetrcWithDefault() {
  51. NetrcParser parser(testfileWithDefaultC);
  52. QVERIFY(parser.parse());
  53. QCOMPARE(parser.find("foo"), qMakePair(QString("bar"), QString("baz")));
  54. QCOMPARE(parser.find("dontknow"), qMakePair(QString("user"), QString("pass")));
  55. }
  56. void testInvalidNetrc() {
  57. NetrcParser parser("/invalid");
  58. QVERIFY(!parser.parse());
  59. }
  60. };
  61. QTEST_APPLESS_MAIN(TestNetrcParser)
  62. #include "testnetrcparser.moc"