keepassxc

Форк
0
/
TestModified.cpp 
722 строки · 23.6 Кб
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

18
#include "TestModified.h"
19
#include "mock/MockClock.h"
20

21
#include <QSignalSpy>
22
#include <QTest>
23

24
#include "core/Group.h"
25
#include "core/Metadata.h"
26
#include "crypto/Crypto.h"
27

28
QTEST_GUILESS_MAIN(TestModified)
29

30
namespace
31
{
32
    MockClock* m_clock = nullptr;
33
}
34

35
void TestModified::initTestCase()
36
{
37
    QVERIFY(Crypto::init());
38
}
39

40
void TestModified::init()
41
{
42
    Q_ASSERT(m_clock == nullptr);
43
    m_clock = new MockClock(2010, 5, 5, 10, 30, 10);
44
    MockClock::setup(m_clock);
45
}
46

47
void TestModified::cleanup()
48
{
49
    MockClock::teardown();
50
    m_clock = nullptr;
51
}
52

53
void TestModified::testSignals()
54
{
55
    int spyCount = 0;
56
    int spyCount2 = 0;
57

58
    auto compositeKey = QSharedPointer<CompositeKey>::create();
59

60
    QScopedPointer<Database> db(new Database());
61
    auto* root = db->rootGroup();
62
    QSignalSpy spyModified(db.data(), SIGNAL(modified()));
63

64
    db->setKey(compositeKey);
65
    ++spyCount;
66
    QTRY_COMPARE(spyModified.count(), spyCount);
67

68
    auto* group1 = new Group();
69
    group1->setParent(root);
70
    ++spyCount;
71
    QTRY_COMPARE(spyModified.count(), spyCount);
72

73
    auto* group2 = new Group();
74
    group2->setParent(root);
75
    ++spyCount;
76
    QTRY_COMPARE(spyModified.count(), spyCount);
77

78
    group2->setParent(root, 0);
79
    ++spyCount;
80
    QTRY_COMPARE(spyModified.count(), spyCount);
81

82
    auto* entry1 = new Entry();
83
    entry1->setGroup(group1);
84
    ++spyCount;
85
    QTRY_COMPARE(spyModified.count(), spyCount);
86

87
    QScopedPointer<Database> db2(new Database());
88
    auto* root2 = db2->rootGroup();
89
    QSignalSpy spyModified2(db2.data(), SIGNAL(modified()));
90

91
    group1->setParent(root2);
92
    ++spyCount;
93
    QTRY_COMPARE(spyModified.count(), spyCount);
94
    ++spyCount2;
95
    QTRY_COMPARE(spyModified2.count(), spyCount2);
96

97
    entry1->setTitle("test");
98
    QTRY_COMPARE(spyModified.count(), spyCount);
99
    ++spyCount2;
100
    QTRY_COMPARE(spyModified2.count(), spyCount2);
101

102
    auto* entry2 = new Entry();
103
    entry2->setGroup(group2);
104
    ++spyCount;
105
    QTRY_COMPARE(spyModified.count(), spyCount);
106
    QTRY_COMPARE(spyModified2.count(), spyCount2);
107

108
    entry2->setGroup(root2);
109
    ++spyCount;
110
    QTRY_COMPARE(spyModified.count(), spyCount);
111
    ++spyCount2;
112
    QTRY_COMPARE(spyModified2.count(), spyCount2);
113

114
    entry2->setTitle("test2");
115
    QTRY_COMPARE(spyModified.count(), spyCount);
116
    ++spyCount2;
117
    QTRY_COMPARE(spyModified2.count(), spyCount2);
118

119
    auto* group3 = new Group();
120
    group3->setParent(root);
121
    ++spyCount;
122
    QTRY_COMPARE(spyModified.count(), spyCount);
123

124
    auto* group4 = new Group();
125
    group4->setParent(group3);
126
    ++spyCount;
127
    QTRY_COMPARE(spyModified.count(), spyCount);
128

129
    delete group4;
130
    ++spyCount;
131
    QTRY_COMPARE(spyModified.count(), spyCount);
132

133
    delete entry2;
134
    ++spyCount2;
135
    QTRY_COMPARE(spyModified2.count(), spyCount2);
136

137
    QTRY_COMPARE(spyModified.count(), spyCount);
138
    QTRY_COMPARE(spyModified2.count(), spyCount2);
139
}
140

141
void TestModified::testGroupSets()
142
{
143
    int spyCount = 0;
144
    QScopedPointer<Database> db(new Database());
145
    auto* root = db->rootGroup();
146

147
    auto* group = new Group();
148
    group->setParent(root);
149

150
    QSignalSpy spyModified(db.data(), SIGNAL(modified()));
151

152
    root->setUuid(QUuid::createUuid());
153
    ++spyCount;
154
    QTRY_COMPARE(spyModified.count(), spyCount);
155
    root->setUuid(root->uuid());
156
    QTRY_COMPARE(spyModified.count(), spyCount);
157

158
    root->setName("test");
159
    ++spyCount;
160
    QTRY_COMPARE(spyModified.count(), spyCount);
161
    root->setName(root->name());
162
    QTRY_COMPARE(spyModified.count(), spyCount);
163

164
    root->setNotes("test");
165
    ++spyCount;
166
    QTRY_COMPARE(spyModified.count(), spyCount);
167
    root->setNotes(root->notes());
168
    QTRY_COMPARE(spyModified.count(), spyCount);
169

170
    root->setIcon(1);
171
    ++spyCount;
172
    QTRY_COMPARE(spyModified.count(), spyCount);
173
    root->setIcon(root->iconNumber());
174
    QTRY_COMPARE(spyModified.count(), spyCount);
175

176
    root->setIcon(QUuid::createUuid());
177
    ++spyCount;
178
    QTRY_COMPARE(spyModified.count(), spyCount);
179
    root->setIcon(root->iconUuid());
180
    QTRY_COMPARE(spyModified.count(), spyCount);
181

182
    group->setUuid(QUuid::createUuid());
183
    ++spyCount;
184
    QTRY_COMPARE(spyModified.count(), spyCount);
185
    group->setUuid(group->uuid());
186
    QTRY_COMPARE(spyModified.count(), spyCount);
187

188
    group->setName("test");
189
    ++spyCount;
190
    QTRY_COMPARE(spyModified.count(), spyCount);
191
    group->setName(group->name());
192
    QTRY_COMPARE(spyModified.count(), spyCount);
193

194
    group->setNotes("test");
195
    ++spyCount;
196
    QTRY_COMPARE(spyModified.count(), spyCount);
197
    group->setNotes(group->notes());
198
    QTRY_COMPARE(spyModified.count(), spyCount);
199

200
    group->setIcon(1);
201
    ++spyCount;
202
    QTRY_COMPARE(spyModified.count(), spyCount);
203
    group->setIcon(group->iconNumber());
204
    QTRY_COMPARE(spyModified.count(), spyCount);
205

206
    group->setIcon(QUuid::createUuid());
207
    ++spyCount;
208
    QTRY_COMPARE(spyModified.count(), spyCount);
209
    group->setIcon(group->iconUuid());
210
    QTRY_COMPARE(spyModified.count(), spyCount);
211
}
212

213
void TestModified::testEntrySets()
214
{
215
    int spyCount = 0;
216
    QScopedPointer<Database> db(new Database());
217
    auto* root = db->rootGroup();
218

219
    auto* group = new Group();
220
    group->setParent(root);
221
    auto* entry = new Entry();
222
    entry->setGroup(group);
223

224
    QSignalSpy spyModified(db.data(), SIGNAL(modified()));
225

226
    entry->setUuid(QUuid::createUuid());
227
    ++spyCount;
228
    QTRY_COMPARE(spyModified.count(), spyCount);
229
    entry->setUuid(entry->uuid());
230
    QTRY_COMPARE(spyModified.count(), spyCount);
231

232
    entry->setTitle("test");
233
    ++spyCount;
234
    QTRY_COMPARE(spyModified.count(), spyCount);
235
    entry->setTitle(entry->title());
236
    QTRY_COMPARE(spyModified.count(), spyCount);
237

238
    entry->setUrl("test");
239
    ++spyCount;
240
    QTRY_COMPARE(spyModified.count(), spyCount);
241
    entry->setUrl(entry->url());
242
    QTRY_COMPARE(spyModified.count(), spyCount);
243

244
    entry->setUsername("test");
245
    ++spyCount;
246
    QTRY_COMPARE(spyModified.count(), spyCount);
247
    entry->setUsername(entry->username());
248
    QTRY_COMPARE(spyModified.count(), spyCount);
249

250
    entry->setPassword("test");
251
    ++spyCount;
252
    QTRY_COMPARE(spyModified.count(), spyCount);
253
    entry->setPassword(entry->password());
254
    QTRY_COMPARE(spyModified.count(), spyCount);
255

256
    entry->setNotes("test");
257
    ++spyCount;
258
    QTRY_COMPARE(spyModified.count(), spyCount);
259
    entry->setNotes(entry->notes());
260
    QTRY_COMPARE(spyModified.count(), spyCount);
261

262
    entry->setIcon(1);
263
    ++spyCount;
264
    QTRY_COMPARE(spyModified.count(), spyCount);
265
    entry->setIcon(entry->iconNumber());
266
    QTRY_COMPARE(spyModified.count(), spyCount);
267

268
    entry->setIcon(QUuid::createUuid());
269
    ++spyCount;
270
    QTRY_COMPARE(spyModified.count(), spyCount);
271
    entry->setIcon(entry->iconUuid());
272
    QTRY_COMPARE(spyModified.count(), spyCount);
273

274
    entry->setTags("test");
275
    ++spyCount;
276
    QTRY_COMPARE(spyModified.count(), spyCount);
277
    entry->setTags(entry->tags());
278
    QTRY_COMPARE(spyModified.count(), spyCount);
279

280
    entry->setExpires(true);
281
    ++spyCount;
282
    QTRY_COMPARE(spyModified.count(), spyCount);
283
    entry->setExpires(entry->timeInfo().expires());
284
    QTRY_COMPARE(spyModified.count(), spyCount);
285

286
    entry->setExpiryTime(Clock::currentDateTimeUtc().addYears(1));
287
    ++spyCount;
288
    QTRY_COMPARE(spyModified.count(), spyCount);
289
    entry->setExpiryTime(entry->timeInfo().expiryTime());
290
    QTRY_COMPARE(spyModified.count(), spyCount);
291

292
    entry->setAutoTypeEnabled(false);
293
    ++spyCount;
294
    QTRY_COMPARE(spyModified.count(), spyCount);
295
    entry->setAutoTypeEnabled(entry->autoTypeEnabled());
296
    QTRY_COMPARE(spyModified.count(), spyCount);
297

298
    entry->setAutoTypeObfuscation(1);
299
    ++spyCount;
300
    QTRY_COMPARE(spyModified.count(), spyCount);
301
    entry->setAutoTypeObfuscation(entry->autoTypeObfuscation());
302
    QTRY_COMPARE(spyModified.count(), spyCount);
303

304
    entry->setDefaultAutoTypeSequence("test");
305
    ++spyCount;
306
    QTRY_COMPARE(spyModified.count(), spyCount);
307
    entry->setDefaultAutoTypeSequence(entry->defaultAutoTypeSequence());
308
    QTRY_COMPARE(spyModified.count(), spyCount);
309

310
    entry->setForegroundColor(QString("#FF0000"));
311
    ++spyCount;
312
    QTRY_COMPARE(spyModified.count(), spyCount);
313
    entry->setForegroundColor(entry->foregroundColor());
314
    QTRY_COMPARE(spyModified.count(), spyCount);
315

316
    entry->setBackgroundColor(QString("#FF0000"));
317
    ++spyCount;
318
    QTRY_COMPARE(spyModified.count(), spyCount);
319
    entry->setBackgroundColor(entry->backgroundColor());
320
    QTRY_COMPARE(spyModified.count(), spyCount);
321

322
    entry->setOverrideUrl("test");
323
    ++spyCount;
324
    QTRY_COMPARE(spyModified.count(), spyCount);
325
    entry->setOverrideUrl(entry->overrideUrl());
326
    QTRY_COMPARE(spyModified.count(), spyCount);
327

328
    entry->attributes()->set("test key", "test value", false);
329
    ++spyCount;
330
    QTRY_COMPARE(spyModified.count(), spyCount);
331
    entry->attributes()->set("test key", entry->attributes()->value("test key"), false);
332
    QTRY_COMPARE(spyModified.count(), spyCount);
333
    entry->attributes()->set("test key", entry->attributes()->value("test key"), true);
334
    ++spyCount;
335
    QTRY_COMPARE(spyModified.count(), spyCount);
336
    entry->attributes()->set("test key", "new test value", true);
337
    ++spyCount;
338
    QTRY_COMPARE(spyModified.count(), spyCount);
339

340
    entry->attributes()->set("test key2", "test value2", true);
341
    ++spyCount;
342
    QTRY_COMPARE(spyModified.count(), spyCount);
343
    entry->attributes()->set("test key2", entry->attributes()->value("test key2"), true);
344
    QTRY_COMPARE(spyModified.count(), spyCount);
345
}
346

347
void TestModified::testHistoryItems()
348
{
349
    QScopedPointer<Entry> entry(new Entry());
350
    QDateTime created = entry->timeInfo().creationTime();
351
    entry->setUuid(QUuid::createUuid());
352
    entry->setTitle("a");
353
    entry->setTags("a");
354
    QScopedPointer<EntryAttributes> attributes(new EntryAttributes());
355
    attributes->copyCustomKeysFrom(entry->attributes());
356

357
    int historyItemsSize = 0;
358

359
    entry->beginUpdate();
360
    entry->setTitle("a");
361
    entry->setTags("a");
362
    entry->setOverrideUrl("");
363
    entry->endUpdate();
364
    QCOMPARE(entry->historyItems().size(), historyItemsSize);
365

366
    QDateTime modified = entry->timeInfo().lastModificationTime();
367
    m_clock->advanceSecond(10);
368
    entry->beginUpdate();
369
    entry->setTitle("b");
370
    entry->endUpdate();
371
    QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
372
    auto* historyEntry = entry->historyItems().at(historyItemsSize - 1);
373
    QCOMPARE(historyEntry->title(), QString("a"));
374
    QCOMPARE(historyEntry->uuid(), entry->uuid());
375
    QCOMPARE(historyEntry->tags(), entry->tags());
376
    QCOMPARE(historyEntry->overrideUrl(), entry->overrideUrl());
377
    QCOMPARE(historyEntry->timeInfo().creationTime(), created);
378
    QCOMPARE(historyEntry->timeInfo().lastModificationTime(), modified);
379
    QCOMPARE(historyEntry->historyItems().size(), 0);
380

381
    entry->beginUpdate();
382
    entry->setTags("b");
383
    entry->endUpdate();
384
    QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
385
    QCOMPARE(entry->historyItems().at(historyItemsSize - 1)->tags(), QString("a"));
386

387
    entry->beginUpdate();
388
    entry->attachments()->set("test", QByteArray("value"));
389
    entry->endUpdate();
390
    QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
391
    QCOMPARE(entry->historyItems().at(historyItemsSize - 1)->attachments()->keys().size(), 0);
392

393
    attributes->set("k", "myvalue");
394
    entry->beginUpdate();
395
    entry->attributes()->copyCustomKeysFrom(attributes.data());
396
    entry->endUpdate();
397
    QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
398
    QVERIFY(!entry->historyItems().at(historyItemsSize - 1)->attributes()->keys().contains("k"));
399

400
    QScopedPointer<Database> db(new Database());
401
    auto* root = db->rootGroup();
402
    db->metadata()->setHistoryMaxItems(3);
403
    db->metadata()->setHistoryMaxSize(-1);
404

405
    auto* entry2 = new Entry();
406
    entry2->setGroup(root);
407
    entry2->beginUpdate();
408
    entry2->setTitle("1");
409
    entry2->endUpdate();
410

411
    entry2->beginUpdate();
412
    entry2->setTitle("2");
413
    entry2->endUpdate();
414
    entry2->beginUpdate();
415
    entry2->setTitle("3");
416
    entry2->endUpdate();
417
    QCOMPARE(entry2->historyItems().size(), 3);
418

419
    entry2->beginUpdate();
420
    entry2->setTitle("4");
421
    entry2->endUpdate();
422
    QCOMPARE(entry2->historyItems().size(), 3);
423

424
    db->metadata()->setHistoryMaxItems(1);
425

426
    entry2->beginUpdate();
427
    entry2->setTitle("5");
428
    entry2->endUpdate();
429
    QCOMPARE(entry2->historyItems().size(), 1);
430

431
    auto* historyEntry2 = entry2->historyItems().at(0);
432
    QCOMPARE(historyEntry2->title(), QString("4"));
433

434
    db->metadata()->setHistoryMaxItems(-1);
435

436
    for (int i = 0; i < 20; i++) {
437
        entry2->beginUpdate();
438
        entry2->setTitle("6");
439
        entry2->endUpdate();
440
        entry2->beginUpdate();
441
        entry2->setTitle("6b");
442
        entry2->endUpdate();
443
    }
444
    QCOMPARE(entry2->historyItems().size(), 41);
445

446
    db->metadata()->setHistoryMaxItems(0);
447

448
    entry2->beginUpdate();
449
    entry2->setTitle("7");
450
    entry2->endUpdate();
451
    QCOMPARE(entry2->historyItems().size(), 0);
452

453
    const int historyMaxSize = 19000;
454

455
    db->metadata()->setHistoryMaxItems(-1);
456
    db->metadata()->setHistoryMaxSize(historyMaxSize);
457

458
    const QString key("test");
459
    entry2->beginUpdate();
460
    entry2->attachments()->set(key, QByteArray(18000, 'X'));
461
    entry2->endUpdate();
462
    QCOMPARE(entry2->attachments()->attachmentsSize(), 18000 + key.size());
463
    QCOMPARE(entry2->historyItems().size(), 1);
464

465
    historyEntry2 = entry2->historyItems().at(0);
466
    QCOMPARE(historyEntry2->title(), QString("7"));
467

468
    entry2->beginUpdate();
469
    entry2->setTitle("8");
470
    entry2->endUpdate();
471
    QCOMPARE(entry2->historyItems().size(), 2);
472

473
    entry2->beginUpdate();
474
    entry2->attachments()->remove(key);
475
    entry2->endUpdate();
476
    QCOMPARE(entry2->attachments()->attachmentsSize(), 0);
477
    QCOMPARE(entry2->historyItems().size(), 1);
478

479
    entry2->beginUpdate();
480
    entry2->attachments()->set("test2", QByteArray(6000, 'a'));
481
    entry2->endUpdate();
482
    QCOMPARE(entry2->attachments()->attachmentsSize(), 6000 + key.size() + 1);
483
    QCOMPARE(entry2->historyItems().size(), 2);
484

485
    entry2->beginUpdate();
486
    entry2->attachments()->set("test3", QByteArray(6000, 'b'));
487
    entry2->endUpdate();
488
    QCOMPARE(entry2->attachments()->attachmentsSize(), 12000 + (key.size() + 1) * 2);
489
    QCOMPARE(entry2->historyItems().size(), 2);
490

491
    entry2->beginUpdate();
492
    entry2->attachments()->set("test4", QByteArray(6000, 'c'));
493
    entry2->endUpdate();
494
    QCOMPARE(entry2->attachments()->attachmentsSize(), 18000 + (key.size() + 1) * 3);
495
    QCOMPARE(entry2->historyItems().size(), 3);
496

497
    entry2->beginUpdate();
498
    entry2->attachments()->set("test5", QByteArray(6000, 'd'));
499
    entry2->endUpdate();
500
    QCOMPARE(entry2->attachments()->attachmentsSize(), 24000 + (key.size() + 1) * 4);
501
    QCOMPARE(entry2->historyItems().size(), 1);
502
}
503

504
void TestModified::testHistoryMaxSize()
505
{
506
    QScopedPointer<Database> db(new Database());
507
    const QString key("test");
508

509
    auto entry1 = new Entry();
510
    entry1->setGroup(db->rootGroup());
511
    QCOMPARE(entry1->historyItems().size(), 0);
512

513
    const int reservedSize1 = entry1->attributes()->attributesSize();
514
    db->metadata()->setHistoryMaxItems(-1);
515
    db->metadata()->setHistoryMaxSize(18000 + key.size() * 3 + reservedSize1 * 4);
516

517
    entry1->beginUpdate();
518
    entry1->attachments()->set(key, QByteArray(6000, 'a'));
519
    entry1->endUpdate();
520
    QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
521
    QCOMPARE(entry1->historyItems().size(), 1);
522

523
    entry1->beginUpdate();
524
    entry1->attachments()->set(key, QByteArray(6000, 'b'));
525
    entry1->endUpdate();
526
    QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
527
    QCOMPARE(entry1->historyItems().size(), 2);
528

529
    entry1->beginUpdate();
530
    entry1->attachments()->set(key, QByteArray(6000, 'c'));
531
    entry1->endUpdate();
532
    QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
533
    QCOMPARE(entry1->historyItems().size(), 3);
534

535
    entry1->beginUpdate();
536
    entry1->attachments()->set(key, QByteArray(6000, 'd'));
537
    entry1->endUpdate();
538
    QCOMPARE(entry1->attachments()->attachmentsSize(), 6000 + key.size());
539
    QCOMPARE(entry1->historyItems().size(), 4);
540

541
    auto entry2 = new Entry();
542
    entry2->setGroup(db->rootGroup());
543
    QCOMPARE(entry2->historyItems().size(), 0);
544

545
    const int historyMaxSize = 17000;
546
    const int reservedSize2 = entry2->attributes()->attributesSize();
547
    db->metadata()->setHistoryMaxSize(historyMaxSize);
548

549
    entry2->beginUpdate();
550
    entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a'));
551
    entry2->endUpdate();
552
    QCOMPARE(entry2->attachments()->attachmentsSize(), historyMaxSize - reservedSize2 + 1);
553
    QCOMPARE(entry2->historyItems().size(), 1);
554

555
    // history size overflow
556
    entry2->beginUpdate();
557
    entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'b'));
558
    entry2->endUpdate();
559
    QCOMPARE(entry2->historyItems().size(), 0);
560

561
    entry2->beginUpdate();
562
    entry2->attachments()->remove(key);
563
    entry2->endUpdate();
564
    QCOMPARE(entry2->attachments()->attachmentsSize(), 0);
565
    QCOMPARE(entry2->historyItems().size(), 0);
566

567
    entry2->beginUpdate();
568
    entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a'));
569
    entry2->endUpdate();
570
    QCOMPARE(entry2->attachments()->attachmentsSize(), historyMaxSize - reservedSize2 + 1);
571
    QCOMPARE(entry2->historyItems().size(), 1);
572

573
    // history size overflow
574
    entry2->beginUpdate();
575
    entry2->attachments()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'b'));
576
    entry2->endUpdate();
577
    QCOMPARE(entry2->historyItems().size(), 0);
578

579
    entry2->beginUpdate();
580
    entry2->attachments()->remove(key);
581
    entry2->endUpdate();
582
    QCOMPARE(entry2->attachments()->attachmentsSize(), 0);
583
    QCOMPARE(entry2->historyItems().size(), 0);
584

585
    entry2->beginUpdate();
586
    entry2->setTags(QByteArray(historyMaxSize - reservedSize2 + 1, 'a'));
587
    entry2->endUpdate();
588
    QCOMPARE(entry2->tags().size(), historyMaxSize - reservedSize2 + 1);
589
    QCOMPARE(entry2->historyItems().size(), 1);
590

591
    // history size overflow
592
    entry2->beginUpdate();
593
    entry2->setTags(QByteArray(historyMaxSize - reservedSize2 + 1, 'b'));
594
    entry2->endUpdate();
595
    QCOMPARE(entry2->historyItems().size(), 0);
596

597
    entry2->beginUpdate();
598
    entry2->setTags("");
599
    entry2->endUpdate();
600
    QCOMPARE(entry2->historyItems().size(), 0);
601

602
    entry2->beginUpdate();
603
    entry2->attributes()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a'));
604
    entry2->endUpdate();
605
    QCOMPARE(entry2->attributes()->attributesSize(), historyMaxSize + 1);
606
    QCOMPARE(entry2->historyItems().size(), 1);
607

608
    // history size overflow
609
    entry2->beginUpdate();
610
    entry2->attributes()->set(key, QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'b'));
611
    entry2->endUpdate();
612
    QCOMPARE(entry2->attributes()->attributesSize(), historyMaxSize + 1);
613
    QCOMPARE(entry2->historyItems().size(), 0);
614

615
    entry2->beginUpdate();
616
    entry2->attributes()->remove(key);
617
    entry2->endUpdate();
618
    QCOMPARE(entry2->attributes()->attributesSize(), reservedSize2);
619
    QCOMPARE(entry2->historyItems().size(), 0);
620

621
    entry2->beginUpdate();
622
    AutoTypeAssociations::Association association;
623
    association.window = key;
624
    association.sequence = QByteArray(historyMaxSize - key.size() - reservedSize2 + 1, 'a');
625
    entry2->autoTypeAssociations()->add(association);
626
    entry2->endUpdate();
627
    QCOMPARE(entry2->autoTypeAssociations()->associationsSize(), historyMaxSize - reservedSize2 + 1);
628
    QCOMPARE(entry2->historyItems().size(), 1);
629

630
    entry2->beginUpdate();
631
    entry2->autoTypeAssociations()->remove(0);
632
    entry2->endUpdate();
633
    QCOMPARE(entry2->autoTypeAssociations()->associationsSize(), 0);
634
    QCOMPARE(entry2->historyItems().size(), 0);
635
}
636

637
void TestModified::testCustomData()
638
{
639
    int spyCount = 0;
640
    QScopedPointer<Database> db(new Database());
641
    auto* root = db->rootGroup();
642

643
    auto* group = new Group();
644
    group->setParent(root);
645
    auto* entry = new Entry();
646
    entry->setGroup(group);
647

648
    QSignalSpy spyModified(db.data(), SIGNAL(modified()));
649

650
    db->metadata()->customData()->set("Key", "Value");
651
    ++spyCount;
652
    QTRY_COMPARE(spyModified.count(), spyCount);
653
    db->metadata()->customData()->set("Key", "Value");
654
    QTRY_COMPARE(spyModified.count(), spyCount);
655

656
    entry->customData()->set("Key", "Value");
657
    ++spyCount;
658
    QTRY_COMPARE(spyModified.count(), spyCount);
659
    entry->customData()->set("Key", "Value");
660
    QTRY_COMPARE(spyModified.count(), spyCount);
661

662
    group->customData()->set("Key", "Value");
663
    ++spyCount;
664
    QTRY_COMPARE(spyModified.count(), spyCount);
665
    group->customData()->set("Key", "Value");
666
    QTRY_COMPARE(spyModified.count(), spyCount);
667
}
668

669
void TestModified::testBlockModifiedSignal()
670
{
671
    QScopedPointer<Database> db(new Database());
672
    auto entry = db->rootGroup()->addEntryWithPath("/abc");
673

674
    QSignalSpy spyDbModified(db.data(), SIGNAL(modified()));
675
    QSignalSpy spyMetadataModified(db->metadata(), SIGNAL(modified()));
676
    QSignalSpy spyCustomDataModified(db->metadata()->customData(), SIGNAL(modified()));
677
    QSignalSpy spyGroupModified(db->rootGroup(), SIGNAL(modified()));
678
    QSignalSpy spyGroupCustomDataModified(db->rootGroup()->customData(), SIGNAL(modified()));
679
    QSignalSpy spyEntryModified(entry, SIGNAL(modified()));
680
    QSignalSpy spyEntryCustomDataModified(entry->customData(), SIGNAL(modified()));
681
    QSignalSpy spyEntryAttributesModified(entry->attributes(), SIGNAL(modified()));
682
    QSignalSpy spyEntryAttachmentModified(entry->attachments(), SIGNAL(modified()));
683
    QSignalSpy spyEntryAutoTypeAssociationsModified(entry->autoTypeAssociations(), SIGNAL(modified()));
684

685
    QVERIFY(spyDbModified.isValid());
686
    QVERIFY(spyMetadataModified.isValid());
687
    QVERIFY(spyCustomDataModified.isValid());
688
    QVERIFY(spyGroupModified.isValid());
689
    QVERIFY(spyGroupCustomDataModified.isValid());
690
    QVERIFY(spyEntryModified.isValid());
691
    QVERIFY(spyEntryCustomDataModified.isValid());
692
    QVERIFY(spyEntryAttributesModified.isValid());
693
    QVERIFY(spyEntryAttachmentModified.isValid());
694
    QVERIFY(spyEntryAutoTypeAssociationsModified.isValid());
695

696
    db->setEmitModified(false);
697

698
    auto* group1 = new Group();
699
    group1->setParent(db->rootGroup());
700
    db->metadata()->setName("Modified Database");
701
    db->metadata()->customData()->set("Key", "Value");
702

703
    group1->customData()->set("abc", "dd");
704
    entry->setTitle("Another Title");
705
    entry->customData()->set("entryabc", "dd");
706
    entry->attributes()->set("aaa", "dd");
707
    entry->attachments()->set("aaa", {});
708
    entry->autoTypeAssociations()->add({"", ""});
709

710
    db.reset();
711

712
    QCOMPARE(spyDbModified.count(), 0);
713
    QCOMPARE(spyMetadataModified.count(), 0);
714
    QCOMPARE(spyCustomDataModified.count(), 0);
715
    QCOMPARE(spyGroupModified.count(), 0);
716
    QCOMPARE(spyGroupCustomDataModified.count(), 0);
717
    QCOMPARE(spyEntryModified.count(), 0);
718
    QCOMPARE(spyEntryCustomDataModified.count(), 0);
719
    QCOMPARE(spyEntryAttributesModified.count(), 0);
720
    QCOMPARE(spyEntryAttachmentModified.count(), 0);
721
    QCOMPARE(spyEntryAutoTypeAssociationsModified.count(), 0);
722
}
723

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.