testnetrcparser.h 2.4 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. #ifndef MIRALL_INOTIFYWATCHER_H
  7. #define MIRALL_INOTIFYWATCHER_H
  8. #include <QtTest>
  9. #include "cmd/netrcparser.h"
  10. using namespace OCC;
  11. namespace {
  12. const char testfileC[] = "netrctest";
  13. const char testfileWithDefaultC[] = "netrctestDefault";
  14. const char testfileEmptyC[] = "netrctestEmpty";
  15. }
  16. class TestNetrcParser : public QObject
  17. {
  18. Q_OBJECT
  19. private slots:
  20. void initTestCase() {
  21. QFile netrc(testfileC);
  22. QVERIFY(netrc.open(QIODevice::WriteOnly));
  23. netrc.write("machine foo login bar password baz\n");
  24. netrc.write("machine broken login bar2 dontbelonghere password baz2 extratokens dontcare andanother\n");
  25. netrc.write("machine\nfunnysplit\tlogin bar3 password baz3\n");
  26. QFile netrcWithDefault(testfileWithDefaultC);
  27. QVERIFY(netrcWithDefault.open(QIODevice::WriteOnly));
  28. netrcWithDefault.write("machine foo login bar password baz\n");
  29. netrcWithDefault.write("default login user password pass\n");
  30. QFile netrcEmpty(testfileEmptyC);
  31. QVERIFY(netrcEmpty.open(QIODevice::WriteOnly));
  32. }
  33. void cleanupTestCase() {
  34. QVERIFY(QFile::remove(testfileC));
  35. QVERIFY(QFile::remove(testfileWithDefaultC));
  36. QVERIFY(QFile::remove(testfileEmptyC));
  37. }
  38. void testValidNetrc() {
  39. NetrcParser parser(testfileC);
  40. QVERIFY(parser.parse());
  41. QCOMPARE(parser.find("foo"), qMakePair(QString("bar"), QString("baz")));
  42. QCOMPARE(parser.find("broken"), qMakePair(QString("bar2"), QString("baz2")));
  43. QCOMPARE(parser.find("funnysplit"), qMakePair(QString("bar3"), QString("baz3")));
  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. #endif