TestFdoSecrets.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2019 Aetf <aetf@unlimitedcodeworks.xyz>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestFdoSecrets.h"
  18. #include "core/EntrySearcher.h"
  19. #include "core/Group.h"
  20. #include "crypto/Random.h"
  21. #include "fdosecrets/objects/Collection.h"
  22. #include "fdosecrets/objects/SessionCipher.h"
  23. #include <QTest>
  24. QTEST_GUILESS_MAIN(TestFdoSecrets)
  25. void TestFdoSecrets::testDhIetf1024Sha256Aes128CbcPkcs7()
  26. {
  27. FdoSecrets::DhIetf1024Sha256Aes128CbcPkcs7 cipher(randomGen()->randomArray(128));
  28. QVERIFY(cipher.isValid());
  29. }
  30. void TestFdoSecrets::testCrazyAttributeKey()
  31. {
  32. using FdoSecrets::Collection;
  33. using FdoSecrets::Item;
  34. const QScopedPointer<Group> root(new Group());
  35. const QScopedPointer<Entry> e1(new Entry());
  36. e1->setGroup(root.data());
  37. const QString key = "_a:bc&-+'-e%12df_d";
  38. const QString value = "value";
  39. e1->attributes()->set(key, value);
  40. // search for custom entries
  41. const auto term = Collection::attributeToTerm(key, value);
  42. const auto res = EntrySearcher().search({term}, root.data());
  43. QCOMPARE(res.count(), 1);
  44. }
  45. void TestFdoSecrets::testSpecialCharsInAttributeValue()
  46. {
  47. using FdoSecrets::Collection;
  48. using FdoSecrets::Item;
  49. const QScopedPointer<Group> root(new Group());
  50. QScopedPointer<Entry> e1(new Entry());
  51. e1->setGroup(root.data());
  52. e1->setTitle("titleA");
  53. e1->attributes()->set("testAttribute", "OAuth::[test.name@gmail.com]");
  54. QScopedPointer<Entry> e2(new Entry());
  55. e2->setGroup(root.data());
  56. e2->setTitle("titleB");
  57. e2->attributes()->set("testAttribute", "Abc:*+.-");
  58. // search for custom entries via programmatic API
  59. {
  60. const auto term = Collection::attributeToTerm("testAttribute", "OAuth::[test.name@gmail.com]");
  61. const auto res = EntrySearcher().search({term}, root.data());
  62. QCOMPARE(res.count(), 1);
  63. QCOMPARE(res[0]->title(), QStringLiteral("titleA"));
  64. }
  65. {
  66. const auto term = Collection::attributeToTerm("testAttribute", "Abc:*+.-");
  67. const auto res = EntrySearcher().search({term}, root.data());
  68. QCOMPARE(res.count(), 1);
  69. QCOMPARE(res[0]->title(), QStringLiteral("titleB"));
  70. }
  71. {
  72. const auto term = Collection::attributeToTerm("testAttribute", "v|");
  73. const auto res = EntrySearcher().search({term}, root.data());
  74. QCOMPARE(res.count(), 0);
  75. }
  76. }
  77. void TestFdoSecrets::testDBusPathParse()
  78. {
  79. using FdoSecrets::DBusMgr;
  80. using PathType = FdoSecrets::DBusMgr::PathType;
  81. auto parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets"));
  82. QCOMPARE(parsed.type, PathType::Service);
  83. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/collection/xxx"));
  84. QCOMPARE(parsed.type, PathType::Collection);
  85. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  86. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/collection/xxx/yyy"));
  87. QCOMPARE(parsed.type, PathType::Item);
  88. QCOMPARE(parsed.id, QStringLiteral("yyy"));
  89. QCOMPARE(parsed.parentId, QStringLiteral("xxx"));
  90. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/aliases/xxx"));
  91. QCOMPARE(parsed.type, PathType::Aliases);
  92. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  93. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/session/xxx"));
  94. QCOMPARE(parsed.type, PathType::Session);
  95. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  96. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/prompt/xxx"));
  97. QCOMPARE(parsed.type, PathType::Prompt);
  98. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  99. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/other/prompt/xxx"));
  100. QCOMPARE(parsed.type, PathType::Unknown);
  101. parsed = DBusMgr::parsePath(QStringLiteral("/org"));
  102. QCOMPARE(parsed.type, PathType::Unknown);
  103. }