TestCsvExporter.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <QBuffer>
  20. #include <QTest>
  21. #include "core/Group.h"
  22. #include "core/Totp.h"
  23. #include "crypto/Crypto.h"
  24. #include "format/CsvExporter.h"
  25. QTEST_GUILESS_MAIN(TestCsvExporter)
  26. const QString TestCsvExporter::ExpectedHeaderLine =
  27. QString("\"Group\",\"Title\",\"Username\",\"Password\",\"URL\",\"Notes\",\"TOTP\",\"Icon\",\"Last "
  28. "Modified\",\"Created\"\n");
  29. void TestCsvExporter::init()
  30. {
  31. m_db = QSharedPointer<Database>::create();
  32. m_csvExporter = QSharedPointer<CsvExporter>::create();
  33. }
  34. void TestCsvExporter::initTestCase()
  35. {
  36. Crypto::init();
  37. }
  38. void TestCsvExporter::cleanup()
  39. {
  40. }
  41. void TestCsvExporter::testExport()
  42. {
  43. Group* groupRoot = m_db->rootGroup();
  44. auto* group = new Group();
  45. group->setName("Test Group Name");
  46. group->setParent(groupRoot);
  47. auto* entry = new Entry();
  48. entry->setGroup(group);
  49. entry->setTitle("Test Entry Title");
  50. entry->setUsername("Test Username");
  51. entry->setPassword("Test Password");
  52. entry->setUrl("http://test.url");
  53. entry->setNotes("Test Notes");
  54. entry->setTotp(Totp::createSettings("DFDF", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP));
  55. entry->setIcon(5);
  56. QBuffer buffer;
  57. QVERIFY(buffer.open(QIODevice::ReadWrite));
  58. m_csvExporter->exportDatabase(&buffer, m_db);
  59. auto exported = QString::fromUtf8(buffer.buffer());
  60. QString expectedResult = QString()
  61. .append(ExpectedHeaderLine)
  62. .append("\"Passwords/Test Group Name\",\"Test Entry Title\",\"Test Username\",\"Test "
  63. "Password\",\"http://test.url\",\"Test Notes\"");
  64. QVERIFY(exported.startsWith(expectedResult));
  65. exported.remove(expectedResult);
  66. QVERIFY(exported.contains("otpauth://"));
  67. QVERIFY(exported.contains(",\"5\","));
  68. }
  69. void TestCsvExporter::testEmptyDatabase()
  70. {
  71. QBuffer buffer;
  72. QVERIFY(buffer.open(QIODevice::ReadWrite));
  73. m_csvExporter->exportDatabase(&buffer, m_db);
  74. QCOMPARE(QString::fromUtf8(buffer.buffer().constData()), ExpectedHeaderLine);
  75. }
  76. void TestCsvExporter::testNestedGroups()
  77. {
  78. Group* groupRoot = m_db->rootGroup();
  79. auto* group = new Group();
  80. group->setName("Test Group Name");
  81. group->setParent(groupRoot);
  82. auto* childGroup = new Group();
  83. childGroup->setName("Test Sub Group Name");
  84. childGroup->setParent(group);
  85. auto* entry = new Entry();
  86. entry->setGroup(childGroup);
  87. entry->setTitle("Test Entry Title");
  88. QBuffer buffer;
  89. QVERIFY(buffer.open(QIODevice::ReadWrite));
  90. m_csvExporter->exportDatabase(&buffer, m_db);
  91. auto exported = QString::fromUtf8(buffer.buffer());
  92. QVERIFY(exported.startsWith(
  93. QString()
  94. .append(ExpectedHeaderLine)
  95. .append("\"Passwords/Test Group Name/Test Sub Group Name\",\"Test Entry Title\",\"\",\"\",\"\",\"\"")));
  96. }