1 /*
2     This file is part of the KDE Baloo project.
3     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "documenturldb.h"
9 #include "singledbtest.h"
10 #include "idutils.h"
11 
12 
13 using namespace Baloo;
14 
15 class DocumentUrlDBTest : public QObject
16 {
17     Q_OBJECT
18 
touchFile(const QByteArray & path)19     void touchFile(const QByteArray& path) {
20         QFile file(QString::fromUtf8(path));
21         file.open(QIODevice::WriteOnly);
22         file.write("data");
23     }
24 
25 private Q_SLOTS:
init()26     void init()
27     {
28         m_tempDir = new QTemporaryDir();
29 
30         mdb_env_create(&m_env);
31         mdb_env_set_maxdbs(m_env, 2);
32 
33         // The directory needs to be created before opening the environment
34         QByteArray path = QFile::encodeName(m_tempDir->path());
35         mdb_env_open(m_env, path.constData(), 0, 0664);
36         mdb_txn_begin(m_env, nullptr, 0, &m_txn);
37     }
38 
cleanup()39     void cleanup()
40     {
41         mdb_txn_abort(m_txn);
42         mdb_env_close(m_env);
43         delete m_tempDir;
44     }
45 
testNonExistingPath()46     void testNonExistingPath() {
47         /*
48         DocumentUrlDB db(m_txn);
49         db.put(1, "/fire/does/not/exist");
50 
51         // FIXME: What about error handling?
52         QCOMPARE(db.get(1), QByteArray());
53         */
54     }
55 
testSingleFile()56     void testSingleFile() {
57         QTemporaryDir dir;
58 
59         QByteArray filePath(dir.path().toUtf8() + "/file");
60         touchFile(filePath);
61         quint64 id = filePathToId(filePath);
62 
63         DocumentUrlDB db(IdTreeDB::create(m_txn), IdFilenameDB::create(m_txn), m_txn);
64         db.put(id, filePath);
65 
66         QCOMPARE(db.get(id), filePath);
67 
68         db.del(id, [](quint64) { return true; });
69         QCOMPARE(db.get(id), QByteArray());
70     }
71 
testTwoFilesAndAFolder()72     void testTwoFilesAndAFolder() {
73         QTemporaryDir dir;
74 
75         QByteArray dirPath = QFile::encodeName(dir.path());
76         QByteArray filePath1(dirPath + "/file");
77         QByteArray filePath2(dirPath + "/file2");
78 
79         touchFile(filePath1);
80         touchFile(filePath2);
81         quint64 did = filePathToId(dirPath);
82         quint64 id1 = filePathToId(filePath1);
83         quint64 id2 = filePathToId(filePath2);
84 
85         DocumentUrlDB db(IdTreeDB::create(m_txn), IdFilenameDB::create(m_txn), m_txn);
86         db.put(did, dirPath);
87         db.put(id1, filePath1);
88         db.put(id2, filePath2);
89 
90         QCOMPARE(db.get(id1), filePath1);
91         QCOMPARE(db.get(id2), filePath2);
92         QCOMPARE(db.get(did), dirPath);
93 
94         db.del(id1, [=](quint64 id) { return id != did; });
95 
96         QCOMPARE(db.get(id1), QByteArray());
97         QCOMPARE(db.get(id2), filePath2);
98         QCOMPARE(db.get(did), dirPath);
99 
100         db.del(id2, [=](quint64 id) { return id != did; });
101 
102         QCOMPARE(db.get(id1), QByteArray());
103         QCOMPARE(db.get(id2), QByteArray());
104         QCOMPARE(db.get(did), dirPath);
105 
106         db.del(did, [=](quint64 id) { return id != did; });
107 
108         QCOMPARE(db.get(id1), QByteArray());
109         QCOMPARE(db.get(id2), QByteArray());
110         QCOMPARE(db.get(did), QByteArray());
111     }
112 
testDuplicateId()113     void testDuplicateId() {
114     }
115 
testGetId()116     void testGetId() {
117         QTemporaryDir dir;
118         const QByteArray path = QFile::encodeName(dir.path());
119         quint64 id = filePathToId(path);
120 
121         QByteArray filePath1(path + "/file");
122         touchFile(filePath1);
123         quint64 id1 = filePathToId(filePath1);
124         QByteArray filePath2(path + "/file2");
125         touchFile(filePath2);
126         quint64 id2 = filePathToId(filePath2);
127 
128         DocumentUrlDB db(IdTreeDB::create(m_txn), IdFilenameDB::create(m_txn), m_txn);
129         db.put(id, path);
130         db.put(id1, filePath1);
131         db.put(id2, filePath2);
132 
133         QCOMPARE(db.getId(id, QByteArray("file")), id1);
134         QCOMPARE(db.getId(id, QByteArray("file2")), id2);
135     }
136 
testSortedIdInsert()137     void testSortedIdInsert()
138     {
139         // test sorted insert used in Baloo::DocumentUrlDB::add, bug 367991
140         std::vector<quint64> test;
141         test.push_back(9);
142 
143         // shall not crash
144         sortedIdInsert(test, quint64(1));
145 
146         // stuff shall be ok inserted
147         QVERIFY(test.size() == 2);
148         QVERIFY(test[0] == 1);
149         QVERIFY(test[1] == 9);
150 
151         // shall not crash
152         sortedIdInsert(test, quint64(1));
153 
154         // no insert please
155         QVERIFY(test.size() == 2);
156 
157         // shall not crash
158         sortedIdInsert(test, quint64(10));
159 
160         // stuff shall be ok inserted
161         QVERIFY(test.size() == 3);
162         QVERIFY(test[0] == 1);
163         QVERIFY(test[1] == 9);
164         QVERIFY(test[2] == 10);
165 
166         // shall not crash
167         sortedIdInsert(test, quint64(2));
168 
169         // stuff shall be ok inserted
170         QVERIFY(test.size() == 4);
171         QVERIFY(test[0] == 1);
172         QVERIFY(test[1] == 2);
173         QVERIFY(test[2] == 9);
174         QVERIFY(test[3] == 10);
175 
176         // shall not crash
177         sortedIdInsert(test, quint64(2));
178 
179         // no insert please
180         QVERIFY(test.size() == 4);
181     }
182 
183 protected:
184     MDB_env* m_env;
185     MDB_txn* m_txn;
186     QTemporaryDir* m_tempDir;
187 };
188 
189 QTEST_MAIN(DocumentUrlDBTest)
190 
191 #include "documenturldbtest.moc"
192