TestModified.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
  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 "TestModified.h"
  18. #include "mock/MockClock.h"
  19. #include <QSignalSpy>
  20. #include <QTest>
  21. #include "core/Clock.h"
  22. #include "core/Database.h"
  23. #include "core/Group.h"
  24. #include "core/Metadata.h"
  25. #include "crypto/Crypto.h"
  26. QTEST_GUILESS_MAIN(TestModified)
  27. namespace
  28. {
  29. MockClock* m_clock = nullptr;
  30. }
  31. void TestModified::initTestCase()
  32. {
  33. QVERIFY(Crypto::init());
  34. }
  35. void TestModified::init()
  36. {
  37. Q_ASSERT(m_clock == nullptr);
  38. m_clock = new MockClock(2010, 5, 5, 10, 30, 10);
  39. MockClock::setup(m_clock);
  40. }
  41. void TestModified::cleanup()
  42. {
  43. MockClock::teardown();
  44. m_clock = nullptr;
  45. }
  46. void TestModified::testSignals()
  47. {
  48. int spyCount = 0;
  49. int spyCount2 = 0;
  50. auto compositeKey = QSharedPointer<CompositeKey>::create();
  51. QScopedPointer<Database> db(new Database());
  52. auto* root = db->rootGroup();
  53. QSignalSpy spyModified(db.data(), SIGNAL(databaseModified()));
  54. db->setKey(compositeKey);
  55. ++spyCount;
  56. QTRY_COMPARE(spyModified.count(), spyCount);
  57. auto* group1 = new Group();
  58. group1->setParent(root);
  59. ++spyCount;
  60. QTRY_COMPARE(spyModified.count(), spyCount);
  61. auto* group2 = new Group();
  62. group2->setParent(root);
  63. ++spyCount;
  64. QTRY_COMPARE(spyModified.count(), spyCount);
  65. group2->setParent(root, 0);
  66. ++spyCount;
  67. QTRY_COMPARE(spyModified.count(), spyCount);
  68. auto* entry1 = new Entry();
  69. entry1->setGroup(group1);
  70. ++spyCount;
  71. QTRY_COMPARE(spyModified.count(), spyCount);
  72. QScopedPointer<Database> db2(new Database());
  73. auto* root2 = db2->rootGroup();
  74. QSignalSpy spyModified2(db2.data(), SIGNAL(databaseModified()));
  75. group1->setParent(root2);
  76. ++spyCount;
  77. QTRY_COMPARE(spyModified.count(), spyCount);
  78. ++spyCount2;
  79. QTRY_COMPARE(spyModified2.count(), spyCount2);
  80. entry1->setTitle("test");
  81. QTRY_COMPARE(spyModified.count(), spyCount);
  82. ++spyCount2;
  83. QTRY_COMPARE(spyModified2.count(), spyCount2);
  84. auto* entry2 = new Entry();
  85. entry2->setGroup(group2);
  86. ++spyCount;
  87. QTRY_COMPARE(spyModified.count(), spyCount);
  88. QTRY_COMPARE(spyModified2.count(), spyCount2);
  89. entry2->setGroup(root2);
  90. ++spyCount;
  91. QTRY_COMPARE(spyModified.count(), spyCount);
  92. ++spyCount2;
  93. QTRY_COMPARE(spyModified2.count(), spyCount2);
  94. entry2->setTitle("test2");
  95. QTRY_COMPARE(spyModified.count(), spyCount);
  96. ++spyCount2;
  97. QTRY_COMPARE(spyModified2.count(), spyCount2);
  98. auto* group3 = new Group();
  99. group3->setParent(root);
  100. ++spyCount;
  101. QTRY_COMPARE(spyModified.count(), spyCount);
  102. auto* group4 = new Group();
  103. group4->setParent(group3);
  104. ++spyCount;
  105. QTRY_COMPARE(spyModified.count(), spyCount);
  106. delete group4;
  107. ++spyCount;
  108. QTRY_COMPARE(spyModified.count(), spyCount);
  109. delete entry2;
  110. ++spyCount2;
  111. QTRY_COMPARE(spyModified2.count(), spyCount2);
  112. QTRY_COMPARE(spyModified.count(), spyCount);
  113. QTRY_COMPARE(spyModified2.count(), spyCount2);
  114. }
  115. void TestModified::testGroupSets()
  116. {
  117. int spyCount = 0;
  118. QScopedPointer<Database> db(new Database());
  119. auto* root = db->rootGroup();
  120. auto* group = new Group();
  121. group->setParent(root);
  122. QSignalSpy spyModified(db.data(), SIGNAL(databaseModified()));
  123. root->setUuid(QUuid::createUuid());
  124. ++spyCount;
  125. QTRY_COMPARE(spyModified.count(), spyCount);
  126. root->setUuid(root->uuid());
  127. QTRY_COMPARE(spyModified.count(), spyCount);
  128. root->setName("test");
  129. ++spyCount;
  130. QTRY_COMPARE(spyModified.count(), spyCount);
  131. root->setName(root->name());
  132. QTRY_COMPARE(spyModified.count(), spyCount);
  133. root->setNotes("test");
  134. ++spyCount;
  135. QTRY_COMPARE(spyModified.count(), spyCount);
  136. root->setNotes(root->notes());
  137. QTRY_COMPARE(spyModified.count(), spyCount);
  138. root->setIcon(1);
  139. ++spyCount;
  140. QTRY_COMPARE(spyModified.count(), spyCount);
  141. root->setIcon(root->iconNumber());
  142. QTRY_COMPARE(spyModified.count(), spyCount);
  143. root->setIcon(QUuid::createUuid());
  144. ++spyCount;
  145. QTRY_COMPARE(spyModified.count(), spyCount);
  146. root->setIcon(root->iconUuid());
  147. QTRY_COMPARE(spyModified.count(), spyCount);
  148. group->setUuid(QUuid::createUuid());
  149. ++spyCount;
  150. QTRY_COMPARE(spyModified.count(), spyCount);
  151. group->setUuid(group->uuid());
  152. QTRY_COMPARE(spyModified.count(), spyCount);
  153. group->setName("test");
  154. ++spyCount;
  155. QTRY_COMPARE(spyModified.count(), spyCount);
  156. group->setName(group->name());
  157. QTRY_COMPARE(spyModified.count(), spyCount);
  158. group->setNotes("test");
  159. ++spyCount;
  160. QTRY_COMPARE(spyModified.count(), spyCount);
  161. group->setNotes(group->notes());
  162. QTRY_COMPARE(spyModified.count(), spyCount);
  163. group->setIcon(1);
  164. ++spyCount;
  165. QTRY_COMPARE(spyModified.count(), spyCount);
  166. group->setIcon(group->iconNumber());
  167. QTRY_COMPARE(spyModified.count(), spyCount);
  168. group->setIcon(QUuid::createUuid());
  169. ++spyCount;
  170. QTRY_COMPARE(spyModified.count(), spyCount);
  171. group->setIcon(group->iconUuid());
  172. QTRY_COMPARE(spyModified.count(), spyCount);
  173. }
  174. void TestModified::testEntrySets()
  175. {
  176. int spyCount = 0;
  177. QScopedPointer<Database> db(new Database());
  178. auto* root = db->rootGroup();
  179. auto* group = new Group();
  180. group->setParent(root);
  181. auto* entry = new Entry();
  182. entry->setGroup(group);
  183. QSignalSpy spyModified(db.data(), SIGNAL(databaseModified()));
  184. entry->setUuid(QUuid::createUuid());
  185. ++spyCount;
  186. QTRY_COMPARE(spyModified.count(), spyCount);
  187. entry->setUuid(entry->uuid());
  188. QTRY_COMPARE(spyModified.count(), spyCount);
  189. entry->setTitle("test");
  190. ++spyCount;
  191. QTRY_COMPARE(spyModified.count(), spyCount);
  192. entry->setTitle(entry->title());
  193. QTRY_COMPARE(spyModified.count(), spyCount);
  194. entry->setUrl("test");
  195. ++spyCount;
  196. QTRY_COMPARE(spyModified.count(), spyCount);
  197. entry->setUrl(entry->url());
  198. QTRY_COMPARE(spyModified.count(), spyCount);
  199. entry->setUsername("test");
  200. ++spyCount;
  201. QTRY_COMPARE(spyModified.count(), spyCount);
  202. entry->setUsername(entry->username());
  203. QTRY_COMPARE(spyModified.count(), spyCount);
  204. entry->setPassword("test");
  205. ++spyCount;
  206. QTRY_COMPARE(spyModified.count(), spyCount);
  207. entry->setPassword(entry->password());
  208. QTRY_COMPARE(spyModified.count(), spyCount);
  209. entry->setNotes("test");
  210. ++spyCount;
  211. QTRY_COMPARE(spyModified.count(), spyCount);
  212. entry->setNotes(entry->notes());
  213. QTRY_COMPARE(spyModified.count(), spyCount);
  214. entry->setIcon(1);
  215. ++spyCount;
  216. QTRY_COMPARE(spyModified.count(), spyCount);
  217. entry->setIcon(entry->iconNumber());
  218. QTRY_COMPARE(spyModified.count(), spyCount);
  219. entry->setIcon(QUuid::createUuid());
  220. ++spyCount;
  221. QTRY_COMPARE(spyModified.count(), spyCount);
  222. entry->setIcon(entry->iconUuid());
  223. QTRY_COMPARE(spyModified.count(), spyCount);
  224. entry->setTags("test");
  225. ++spyCount;
  226. QTRY_COMPARE(spyModified.count(), spyCount);
  227. entry->setTags(entry->tags());
  228. QTRY_COMPARE(spyModified.count(), spyCount);
  229. entry->setExpires(true);
  230. ++spyCount;
  231. QTRY_COMPARE(spyModified.count(), spyCount);
  232. entry->setExpires(entry->timeInfo().expires());
  233. QTRY_COMPARE(spyModified.count(), spyCount);
  234. entry->setExpiryTime(Clock::currentDateTimeUtc().addYears(1));
  235. ++spyCount;
  236. QTRY_COMPARE(spyModified.count(), spyCount);
  237. entry->setExpiryTime(entry->timeInfo().expiryTime());
  238. QTRY_COMPARE(spyModified.count(), spyCount);
  239. entry->setAutoTypeEnabled(false);
  240. ++spyCount;
  241. QTRY_COMPARE(spyModified.count(), spyCount);
  242. entry->setAutoTypeEnabled(entry->autoTypeEnabled());
  243. QTRY_COMPARE(spyModified.count(), spyCount);
  244. entry->setAutoTypeObfuscation(1);
  245. ++spyCount;
  246. QTRY_COMPARE(spyModified.count(), spyCount);
  247. entry->setAutoTypeObfuscation(entry->autoTypeObfuscation());
  248. QTRY_COMPARE(spyModified.count(), spyCount);
  249. entry->setDefaultAutoTypeSequence("test");
  250. ++spyCount;
  251. QTRY_COMPARE(spyModified.count(), spyCount);
  252. entry->setDefaultAutoTypeSequence(entry->defaultAutoTypeSequence());
  253. QTRY_COMPARE(spyModified.count(), spyCount);
  254. entry->setForegroundColor(QString("#FF0000"));
  255. ++spyCount;
  256. QTRY_COMPARE(spyModified.count(), spyCount);
  257. entry->setForegroundColor(entry->foregroundColor());
  258. QTRY_COMPARE(spyModified.count(), spyCount);
  259. entry->setBackgroundColor(QString("#FF0000"));
  260. ++spyCount;
  261. QTRY_COMPARE(spyModified.count(), spyCount);
  262. entry->setBackgroundColor(entry->backgroundColor());
  263. QTRY_COMPARE(spyModified.count(), spyCount);
  264. entry->setOverrideUrl("test");
  265. ++spyCount;
  266. QTRY_COMPARE(spyModified.count(), spyCount);
  267. entry->setOverrideUrl(entry->overrideUrl());
  268. QTRY_COMPARE(spyModified.count(), spyCount);
  269. entry->attributes()->set("test key", "test value", false);
  270. ++spyCount;
  271. QTRY_COMPARE(spyModified.count(), spyCount);
  272. entry->attributes()->set("test key", entry->attributes()->value("test key"), false);
  273. QTRY_COMPARE(spyModified.count(), spyCount);
  274. entry->attributes()->set("test key", entry->attributes()->value("test key"), true);
  275. ++spyCount;
  276. QTRY_COMPARE(spyModified.count(), spyCount);
  277. entry->attributes()->set("test key", "new test value", true);
  278. ++spyCount;
  279. QTRY_COMPARE(spyModified.count(), spyCount);
  280. entry->attributes()->set("test key2", "test value2", true);
  281. ++spyCount;
  282. QTRY_COMPARE(spyModified.count(), spyCount);
  283. entry->attributes()->set("test key2", entry->attributes()->value("test key2"), true);
  284. QTRY_COMPARE(spyModified.count(), spyCount);
  285. }
  286. void TestModified::testHistoryItems()
  287. {
  288. QScopedPointer<Entry> entry(new Entry());
  289. QDateTime created = entry->timeInfo().creationTime();
  290. entry->setUuid(QUuid::createUuid());
  291. entry->setTitle("a");
  292. entry->setTags("a");
  293. QScopedPointer<EntryAttributes> attributes(new EntryAttributes());
  294. attributes->copyCustomKeysFrom(entry->attributes());
  295. int historyItemsSize = 0;
  296. entry->beginUpdate();
  297. entry->setTitle("a");
  298. entry->setTags("a");
  299. entry->setOverrideUrl("");
  300. entry->endUpdate();
  301. QCOMPARE(entry->historyItems().size(), historyItemsSize);
  302. QDateTime modified = entry->timeInfo().lastModificationTime();
  303. m_clock->advanceSecond(10);
  304. entry->beginUpdate();
  305. entry->setTitle("b");
  306. entry->endUpdate();
  307. QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
  308. auto* historyEntry = entry->historyItems().at(historyItemsSize - 1);
  309. QCOMPARE(historyEntry->title(), QString("a"));
  310. QCOMPARE(historyEntry->uuid(), entry->uuid());
  311. QCOMPARE(historyEntry->tags(), entry->tags());
  312. QCOMPARE(historyEntry->overrideUrl(), entry->overrideUrl());
  313. QCOMPARE(historyEntry->timeInfo().creationTime(), created);
  314. QCOMPARE(historyEntry->timeInfo().lastModificationTime(), modified);
  315. QCOMPARE(historyEntry->historyItems().size(), 0);
  316. entry->beginUpdate();
  317. entry->setTags("b");
  318. entry->endUpdate();
  319. QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
  320. QCOMPARE(entry->historyItems().at(historyItemsSize - 1)->tags(), QString("a"));
  321. entry->beginUpdate();
  322. entry->attachments()->set("test", QByteArray("value"));
  323. entry->endUpdate();
  324. QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
  325. QCOMPARE(entry->historyItems().at(historyItemsSize - 1)->attachments()->keys().size(), 0);
  326. attributes->set("k", "myvalue");
  327. entry->beginUpdate();
  328. entry->attributes()->copyCustomKeysFrom(attributes.data());
  329. entry->endUpdate();
  330. QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
  331. QVERIFY(!entry->historyItems().at(historyItemsSize - 1)->attributes()->keys().contains("k"));
  332. QScopedPointer<Database> db(new Database());
  333. auto* root = db->rootGroup();
  334. db->metadata()->setHistoryMaxItems(3);
  335. db->metadata()->setHistoryMaxSize(-1);
  336. auto* entry2 = new Entry();
  337. entry2->setGroup(root);
  338. entry2->beginUpdate();
  339. entry2->setTitle("1");
  340. entry2->endUpdate();
  341. entry2->beginUpdate();
  342. entry2->setTitle("2");
  343. entry2->endUpdate();
  344. entry2->beginUpdate();
  345. entry2->setTitle("3");
  346. entry2->endUpdate();
  347. QCOMPARE(entry2->historyItems().size(), 3);
  348. entry2->beginUpdate();
  349. entry2->setTitle("4");
  350. entry2->endUpdate();
  351. QCOMPARE(entry2->historyItems().size(), 3);
  352. db->metadata()->setHistoryMaxItems(1);
  353. entry2->beginUpdate();
  354. entry2->setTitle("5");
  355. entry2->endUpdate();
  356. QCOMPARE(entry2->historyItems().size(), 1);
  357. auto* historyEntry2 = entry2->historyItems().at(0);
  358. QCOMPARE(historyEntry2->title(), QString("4"));
  359. db->metadata()->setHistoryMaxItems(-1);
  360. for (int i = 0; i < 20; i++) {
  361. entry2->beginUpdate();
  362. entry2->setTitle("6");
  363. entry2->endUpdate();
  364. entry2->beginUpdate();
  365. entry2->setTitle("6b");
  366. entry2->endUpdate();
  367. }
  368. QCOMPARE(entry2->historyItems().size(), 41);
  369. db->metadata()->setHistoryMaxItems(0);
  370. entry2->beginUpdate();
  371. entry2->setTitle("7");
  372. entry2->endUpdate();
  373. QCOMPARE(entry2->historyItems().size(), 0);
  374. const int historyMaxSize = 19000;
  375. db->metadata()->setHistoryMaxItems(-1);
  376. db->metadata()->setHistoryMaxSize(historyMaxSize);
  377. const QString key("test");
  378. entry2->beginUpdate();
  379. entry2->attachments()->set(key, QByteArray(18000, 'X'));
  380. entry2->endUpdate();
  381. QCOMPARE(entry2->attachments()->attachmentsSize(), 18000 + key.size());
  382. QCOMPARE(entry2->historyItems().size(), 1);
  383. historyEntry2 = entry2->historyItems().at(0);
  384. QCOMPARE(historyEntry2->title(), QString("7"));
  385. entry2->beginUpdate();
  386. entry2->setTitle("8");
  387. entry2->endUpdate();
  388. QCOMPARE(entry2->historyItems().size(), 2);
  389. entry2->beginUpdate();
  390. entry2->attachments()->remove(key);
  391. entry2->endUpdate();
  392. QCOMPARE(entry2->attachments()->attachmentsSize(), 0);
  393. QCOMPARE(entry2->historyItems().size(), 1);
  394. entry2->beginUpdate();
  395. entry2->attachments()->set("test2", QByteArray(6000, 'a'));
  396. entry2->endUpdate();
  397. QCOMPARE(entry2->attachments()->attachmentsSize(), 6000 + key.size() + 1);
  398. QCOMPARE(entry2->historyItems().size(), 2);
  399. entry2->beginUpdate();
  400. entry2->attachments()->set("test3", QByteArray(6000, 'b'));
  401. entry2->endUpdate();
  402. QCOMPARE(entry2->attachments()->attachmentsSize(), 12000 + (key.size() + 1) * 2);
  403. QCOMPARE(entry2->historyItems().size(), 2);
  404. entry2->beginUpdate();
  405. entry2->attachments()->set("test4", QByteArray(6000, 'c'));
  406. entry2->endUpdate();
  407. QCOMPARE(entry2->attachments()->attachmentsSize(), 18000 + (key.size() + 1) * 3);
  408. QCOMPARE(entry2->historyItems().size(), 3);
  409. entry2->beginUpdate();
  410. entry2->attachments()->set("test5", QByteArray(6000, 'd'));
  411. entry2->endUpdate();
  412. QCOMPARE(entry2->attachments()->attachmentsSize(), 24000 + (key.size() + 1) * 4);
  413. QCOMPARE(entry2->historyItems().size(), 1);
  414. }
  415. void TestModified::testHistoryMaxSize()
  416. {
  417. QScopedPointer<Database> db(new Database());
  418. const QString key("test");
  419. auto entry1 = new Entry();
  420. entry1->setGroup(db->rootGroup());
  421. QCOMPARE(entry1->historyItems().size(), 0);
  422. const int reservedSize1 = entry1->attributes()->attributesSize();
  423. db->metadata()->setHistoryMaxItems(-1);
  424. db->metadata()->setHistoryMaxSize(18000 + key.size() * 3 + reservedSize1 * 4);
  425. entry1->beginUpdate();
  426. entry1->attachments()->set(key, QByteArray(6000, 'a'));
  427. entry1->endUpdate();
  428. QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
  429. QCOMPARE(entry1->historyItems().size(), 1);
  430. entry1->beginUpdate();
  431. entry1->attachments()->set(key, QByteArray(6000, 'b'));
  432. entry1->endUpdate();
  433. QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
  434. QCOMPARE(entry1->historyItems().size(), 2);
  435. entry1->beginUpdate();
  436. entry1->attachments()->set(key, QByteArray(6000, 'c'));
  437. entry1->endUpdate();
  438. QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
  439. QCOMPARE(entry1->historyItems().size(), 3);
  440. entry1->beginUpdate();
  441. entry1->attachments()->set(key, QByteArray(6000, 'd'));
  442. entry1->endUpdate();
  443. QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
  444. QCOMPARE(entry1->historyItems().size(), 4);
  445. auto entry2 = new Entry();
  446. entry2->setGroup(db->rootGroup());
  447. QCOMPARE(entry2->historyItems().size(), 0);
  448. const int historyMaxSize = 17000;
  449. const int reservedSize2 = entry2->attributes()->attributesSize();
  450. db->metadata()->setHistoryMaxSize(historyMaxSize);
  451. entry2->beginUpdate();
  452. entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a'));
  453. entry2->endUpdate();
  454. QCOMPARE(entry2->attachments()->attachmentsSize(), historyMaxSize - reservedSize2 + 1);
  455. QCOMPARE(entry2->historyItems().size(), 1);
  456. // history size overflow
  457. entry2->beginUpdate();
  458. entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'b'));
  459. entry2->endUpdate();
  460. QCOMPARE(entry2->historyItems().size(), 0);
  461. entry2->beginUpdate();
  462. entry2->attachments()->remove(key);
  463. entry2->endUpdate();
  464. QCOMPARE(entry2->attachments()->attachmentsSize(), 0);
  465. QCOMPARE(entry2->historyItems().size(), 0);
  466. entry2->beginUpdate();
  467. entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a'));
  468. entry2->endUpdate();
  469. QCOMPARE(entry2->attachments()->attachmentsSize(), historyMaxSize - reservedSize2 + 1);
  470. QCOMPARE(entry2->historyItems().size(), 1);
  471. // history size overflow
  472. entry2->beginUpdate();
  473. entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'b'));
  474. entry2->endUpdate();
  475. QCOMPARE(entry2->historyItems().size(), 0);
  476. entry2->beginUpdate();
  477. entry2->attachments()->remove(key);
  478. entry2->endUpdate();
  479. QCOMPARE(entry2->attachments()->attachmentsSize(), 0);
  480. QCOMPARE(entry2->historyItems().size(), 0);
  481. entry2->beginUpdate();
  482. entry2->setTags(QByteArray(historyMaxSize - reservedSize2 + 1, 'a'));
  483. entry2->endUpdate();
  484. QCOMPARE(entry2->tags().size(), historyMaxSize - reservedSize2 + 1);
  485. QCOMPARE(entry2->historyItems().size(), 1);
  486. // history size overflow
  487. entry2->beginUpdate();
  488. entry2->setTags(QByteArray(historyMaxSize - reservedSize2 + 1, 'b'));
  489. entry2->endUpdate();
  490. QCOMPARE(entry2->historyItems().size(), 0);
  491. entry2->beginUpdate();
  492. entry2->setTags("");
  493. entry2->endUpdate();
  494. QCOMPARE(entry2->historyItems().size(), 0);
  495. entry2->beginUpdate();
  496. entry2->attributes()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a'));
  497. entry2->endUpdate();
  498. QCOMPARE(entry2->attributes()->attributesSize(), historyMaxSize + 1);
  499. QCOMPARE(entry2->historyItems().size(), 1);
  500. // history size overflow
  501. entry2->beginUpdate();
  502. entry2->attributes()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'b'));
  503. entry2->endUpdate();
  504. QCOMPARE(entry2->attributes()->attributesSize(), historyMaxSize + 1);
  505. QCOMPARE(entry2->historyItems().size(), 0);
  506. entry2->beginUpdate();
  507. entry2->attributes()->remove(key);
  508. entry2->endUpdate();
  509. QCOMPARE(entry2->attributes()->attributesSize(), reservedSize2);
  510. QCOMPARE(entry2->historyItems().size(), 0);
  511. entry2->beginUpdate();
  512. AutoTypeAssociations::Association association;
  513. association.window = key;
  514. association.sequence = QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a');
  515. entry2->autoTypeAssociations()->add(association);
  516. entry2->endUpdate();
  517. QCOMPARE(entry2->autoTypeAssociations()->associationsSize(), historyMaxSize - reservedSize2 + 1);
  518. QCOMPARE(entry2->historyItems().size(), 1);
  519. entry2->beginUpdate();
  520. entry2->autoTypeAssociations()->remove(0);
  521. entry2->endUpdate();
  522. QCOMPARE(entry2->autoTypeAssociations()->associationsSize(), 0);
  523. QCOMPARE(entry2->historyItems().size(), 0);
  524. }
  525. void TestModified::testCustomData()
  526. {
  527. int spyCount = 0;
  528. QScopedPointer<Database> db(new Database());
  529. auto* root = db->rootGroup();
  530. auto* group = new Group();
  531. group->setParent(root);
  532. auto* entry = new Entry();
  533. entry->setGroup(group);
  534. QSignalSpy spyModified(db.data(), SIGNAL(databaseModified()));
  535. db->metadata()->customData()->set("Key", "Value");
  536. ++spyCount;
  537. QTRY_COMPARE(spyModified.count(), spyCount);
  538. db->metadata()->customData()->set("Key", "Value");
  539. QTRY_COMPARE(spyModified.count(), spyCount);
  540. entry->customData()->set("Key", "Value");
  541. ++spyCount;
  542. QTRY_COMPARE(spyModified.count(), spyCount);
  543. entry->customData()->set("Key", "Value");
  544. QTRY_COMPARE(spyModified.count(), spyCount);
  545. group->customData()->set("Key", "Value");
  546. ++spyCount;
  547. QTRY_COMPARE(spyModified.count(), spyCount);
  548. group->customData()->set("Key", "Value");
  549. QTRY_COMPARE(spyModified.count(), spyCount);
  550. }