TestFdoSecrets.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "TestGlobal.h"
  19. #include "core/EntrySearcher.h"
  20. #include "crypto/Crypto.h"
  21. #include "crypto/Random.h"
  22. #include "fdosecrets/dbus/DBusMgr.h"
  23. #include "fdosecrets/objects/Collection.h"
  24. #include "fdosecrets/objects/Item.h"
  25. #include "fdosecrets/objects/SessionCipher.h"
  26. QTEST_GUILESS_MAIN(TestFdoSecrets)
  27. void TestFdoSecrets::testDhIetf1024Sha256Aes128CbcPkcs7()
  28. {
  29. FdoSecrets::DhIetf1024Sha256Aes128CbcPkcs7 cipher(randomGen()->randomArray(128));
  30. QVERIFY(cipher.isValid());
  31. }
  32. void TestFdoSecrets::testCrazyAttributeKey()
  33. {
  34. using FdoSecrets::Collection;
  35. using FdoSecrets::Item;
  36. const QScopedPointer<Group> root(new Group());
  37. const QScopedPointer<Entry> e1(new Entry());
  38. e1->setGroup(root.data());
  39. const QString key = "_a:bc&-+'-e%12df_d";
  40. const QString value = "value";
  41. e1->attributes()->set(key, value);
  42. // search for custom entries
  43. const auto term = Collection::attributeToTerm(key, value);
  44. const auto res = EntrySearcher().search({term}, root.data());
  45. QCOMPARE(res.count(), 1);
  46. }
  47. void TestFdoSecrets::testSpecialCharsInAttributeValue()
  48. {
  49. using FdoSecrets::Collection;
  50. using FdoSecrets::Item;
  51. const QScopedPointer<Group> root(new Group());
  52. QScopedPointer<Entry> e1(new Entry());
  53. e1->setGroup(root.data());
  54. e1->setTitle("titleA");
  55. e1->attributes()->set("testAttribute", "OAuth::[test.name@gmail.com]");
  56. QScopedPointer<Entry> e2(new Entry());
  57. e2->setGroup(root.data());
  58. e2->setTitle("titleB");
  59. e2->attributes()->set("testAttribute", "Abc:*+.-");
  60. // search for custom entries via programatic API
  61. {
  62. const auto term = Collection::attributeToTerm("testAttribute", "OAuth::[test.name@gmail.com]");
  63. const auto res = EntrySearcher().search({term}, root.data());
  64. QCOMPARE(res.count(), 1);
  65. QCOMPARE(res[0]->title(), QStringLiteral("titleA"));
  66. }
  67. {
  68. const auto term = Collection::attributeToTerm("testAttribute", "Abc:*+.-");
  69. const auto res = EntrySearcher().search({term}, root.data());
  70. QCOMPARE(res.count(), 1);
  71. QCOMPARE(res[0]->title(), QStringLiteral("titleB"));
  72. }
  73. }
  74. void TestFdoSecrets::testDBusPathParse()
  75. {
  76. using FdoSecrets::DBusMgr;
  77. using PathType = FdoSecrets::DBusMgr::PathType;
  78. auto parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets"));
  79. QCOMPARE(parsed.type, PathType::Service);
  80. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/collection/xxx"));
  81. QCOMPARE(parsed.type, PathType::Collection);
  82. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  83. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/collection/xxx/yyy"));
  84. QCOMPARE(parsed.type, PathType::Item);
  85. QCOMPARE(parsed.id, QStringLiteral("yyy"));
  86. QCOMPARE(parsed.parentId, QStringLiteral("xxx"));
  87. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/aliases/xxx"));
  88. QCOMPARE(parsed.type, PathType::Aliases);
  89. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  90. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/session/xxx"));
  91. QCOMPARE(parsed.type, PathType::Session);
  92. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  93. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/secrets/prompt/xxx"));
  94. QCOMPARE(parsed.type, PathType::Prompt);
  95. QCOMPARE(parsed.id, QStringLiteral("xxx"));
  96. parsed = DBusMgr::parsePath(QStringLiteral("/org/freedesktop/other/prompt/xxx"));
  97. QCOMPARE(parsed.type, PathType::Unknown);
  98. parsed = DBusMgr::parsePath(QStringLiteral("/org"));
  99. QCOMPARE(parsed.type, PathType::Unknown);
  100. }