TestCsvExporter.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2015 Florian Geyer <blueice@fobos.de>
  3. * Copyright (C) 2015 Felix Geyer <debfx@fobos.de>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 or (at your option)
  8. * version 3 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "TestCsvExporter.h"
  19. #include "TestGlobal.h"
  20. #include <QBuffer>
  21. #include "crypto/Crypto.h"
  22. #include "format/CsvExporter.h"
  23. #include "totp/totp.h"
  24. QTEST_GUILESS_MAIN(TestCsvExporter)
  25. const QString TestCsvExporter::ExpectedHeaderLine =
  26. QString("\"Group\",\"Title\",\"Username\",\"Password\",\"URL\",\"Notes\",\"TOTP\",\"Icon\",\"Last "
  27. "Modified\",\"Created\"\n");
  28. void TestCsvExporter::init()
  29. {
  30. m_db = QSharedPointer<Database>::create();
  31. m_csvExporter = QSharedPointer<CsvExporter>::create();
  32. }
  33. void TestCsvExporter::initTestCase()
  34. {
  35. Crypto::init();
  36. }
  37. void TestCsvExporter::cleanup()
  38. {
  39. }
  40. void TestCsvExporter::testExport()
  41. {
  42. Group* groupRoot = m_db->rootGroup();
  43. auto* group = new Group();
  44. group->setName("Test Group Name");
  45. group->setParent(groupRoot);
  46. auto* entry = new Entry();
  47. entry->setGroup(group);
  48. entry->setTitle("Test Entry Title");
  49. entry->setUsername("Test Username");
  50. entry->setPassword("Test Password");
  51. entry->setUrl("http://test.url");
  52. entry->setNotes("Test Notes");
  53. entry->setTotp(Totp::createSettings("DFDF", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP));
  54. entry->setIcon(5);
  55. QBuffer buffer;
  56. QVERIFY(buffer.open(QIODevice::ReadWrite));
  57. m_csvExporter->exportDatabase(&buffer, m_db);
  58. auto exported = QString::fromUtf8(buffer.buffer());
  59. QString expectedResult = QString()
  60. .append(ExpectedHeaderLine)
  61. .append("\"Passwords/Test Group Name\",\"Test Entry Title\",\"Test Username\",\"Test "
  62. "Password\",\"http://test.url\",\"Test Notes\"");
  63. QVERIFY(exported.startsWith(expectedResult));
  64. exported.remove(expectedResult);
  65. QVERIFY(exported.contains("otpauth://"));
  66. QVERIFY(exported.contains(",\"5\","));
  67. }
  68. void TestCsvExporter::testEmptyDatabase()
  69. {
  70. QBuffer buffer;
  71. QVERIFY(buffer.open(QIODevice::ReadWrite));
  72. m_csvExporter->exportDatabase(&buffer, m_db);
  73. QCOMPARE(QString::fromUtf8(buffer.buffer().constData()), ExpectedHeaderLine);
  74. }
  75. void TestCsvExporter::testNestedGroups()
  76. {
  77. Group* groupRoot = m_db->rootGroup();
  78. auto* group = new Group();
  79. group->setName("Test Group Name");
  80. group->setParent(groupRoot);
  81. auto* childGroup = new Group();
  82. childGroup->setName("Test Sub Group Name");
  83. childGroup->setParent(group);
  84. auto* entry = new Entry();
  85. entry->setGroup(childGroup);
  86. entry->setTitle("Test Entry Title");
  87. QBuffer buffer;
  88. QVERIFY(buffer.open(QIODevice::ReadWrite));
  89. m_csvExporter->exportDatabase(&buffer, m_db);
  90. auto exported = QString::fromUtf8(buffer.buffer());
  91. QVERIFY(exported.startsWith(
  92. QString()
  93. .append(ExpectedHeaderLine)
  94. .append("\"Passwords/Test Group Name/Test Sub Group Name\",\"Test Entry Title\",\"\",\"\",\"\",\"\"")));
  95. }