summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Dzyubenko <denis.dzyubenko@nokia.com>2012-05-25 20:54:08 +0200
committerQt by Nokia <qt-info@nokia.com>2012-05-25 22:09:17 +0200
commit61b1d78172c2f205e871f912a414d97105445fab (patch)
tree8687e4dbb119c2036dbe215ab8f07ac014e59837
parent75a9436d2bc16803cd1e5ea9e60fc156a1a22dbc (diff)
Added a test that reads from multiple private partitions.
For that to work I've added QJsonDbStandardPaths that has convenience functions for retrieving home directories for different users and a switch for using a prefix instead of real users, which can be used in autotests. Change-Id: Ibcbba2fe17498b722f5cbe33fc3a1c16f75f3283 Reviewed-by: Tapani Mikola <tapani.mikola@nokia.com>
-rw-r--r--src/client/client.pro2
-rw-r--r--src/client/qjsondbprivatepartition.cpp32
-rw-r--r--src/client/qjsondbprivatepartition_p.h2
-rw-r--r--src/client/qjsondbstandardpaths.cpp80
-rw-r--r--src/client/qjsondbstandardpaths_p.h77
-rw-r--r--tests/auto/client/test-jsondb-client.cpp11
-rw-r--r--tests/auto/qjsondbflushrequest/testqjsondbflushrequest.cpp1
-rw-r--r--tests/auto/qjsondbrequest/testqjsondbrequest.cpp69
-rw-r--r--tests/auto/qjsondbwatcher/testqjsondbwatcher.cpp5
-rw-r--r--tests/benchmarks/client/client-benchmark.cpp1
-rw-r--r--tests/shared/testhelper.cpp25
-rw-r--r--tests/shared/testhelper.h2
12 files changed, 261 insertions, 46 deletions
diff --git a/src/client/client.pro b/src/client/client.pro
index 8344f2f..65fbb3a 100644
--- a/src/client/client.pro
+++ b/src/client/client.pro
@@ -36,6 +36,7 @@ HEADERS += \
qjsondbwatcher.h \
qjsondbobject.h \
qjsondbprivatepartition_p.h \
+ qjsondbstandardpaths_p.h \
qjsondblogrequest_p.h \
qjsondblogrequest_p_p.h
@@ -48,6 +49,7 @@ SOURCES += \
qjsondbwatcher.cpp \
qjsondbobject.cpp \
qjsondbprivatepartition.cpp \
+ qjsondbstandardpaths.cpp \
qjsondblogrequest.cpp
mac:QMAKE_FRAMEWORK_BUNDLE_NAME = $$QT.jsondb.name
diff --git a/src/client/qjsondbprivatepartition.cpp b/src/client/qjsondbprivatepartition.cpp
index c4c2f94..6219ba6 100644
--- a/src/client/qjsondbprivatepartition.cpp
+++ b/src/client/qjsondbprivatepartition.cpp
@@ -43,6 +43,7 @@
#include "qjsondbconnection_p.h"
#include "qjsondbrequest_p.h"
#include "qjsondbstrings_p.h"
+#include "qjsondbstandardpaths_p.h"
#include "jsondbowner.h"
#include "jsondbpartition.h"
@@ -50,7 +51,6 @@
#include "jsondbqueryparser.h"
#include <qdir.h>
-#include <pwd.h>
QT_BEGIN_NAMESPACE_JSONDB
@@ -61,16 +61,6 @@ QJsonDbPrivatePartition::QJsonDbPrivatePartition(QJsonDbConnectionPrivate *conn)
QJsonDbPrivatePartition::~QJsonDbPrivatePartition()
{
- if (privatePartition) {
- privatePartition->close();
- delete privatePartition;
- privatePartition = 0;
- }
-
- if (partitionOwner) {
- delete partitionOwner;
- partitionOwner = 0;
- }
}
void QJsonDbPrivatePartition::handleRequest(const QJsonObject &request)
@@ -159,7 +149,7 @@ QtJsonDb::QJsonDbRequest::ErrorCode QJsonDbPrivatePartition::ensurePartition(con
QString user;
QString fullPartitionName;
if (partitionName == JsonDbStrings::Partition::privatePartition()) {
- user = QString::fromLatin1(qgetenv("USER"));
+ user = QJsonDbStandardPaths::currentUser();
fullPartitionName = user + JsonDbStrings::Partition::dotPrivatePartition();
} else {
Q_ASSERT(partitionName.endsWith(JsonDbStrings::Partition::dotPrivatePartition()));
@@ -175,31 +165,33 @@ QtJsonDb::QJsonDbRequest::ErrorCode QJsonDbPrivatePartition::ensurePartition(con
}
if (!privatePartition) {
- struct passwd *pwd = getpwnam(user.toLatin1()); // not thread-safe!
- if (!pwd) {
+ QString home = QJsonDbStandardPaths::homePath(user);
+ if (home.isEmpty()) {
message = QStringLiteral("Private partition not found");
return QJsonDbRequest::InvalidPartition;
}
+ QDir homeDir(home);
+ homeDir.mkpath(QStringLiteral(".jsondb"));
+ homeDir.cd(QStringLiteral(".jsondb"));
+
if (!partitionOwner) {
- partitionOwner = new Partition::JsonDbOwner();
+ partitionOwner = new Partition::JsonDbOwner(this);
partitionOwner->setAllowAll(true);
}
- QDir homeDir(QString::fromUtf8(pwd->pw_dir));
- homeDir.mkdir(QStringLiteral(".jsondb"));
- homeDir.cd(QStringLiteral(".jsondb"));
-
Partition::JsonDbPartitionSpec spec;
spec.name = fullPartitionName;
spec.path = homeDir.absolutePath();
- privatePartition = new Partition::JsonDbPartition;
+ privatePartition = new Partition::JsonDbPartition(this);
privatePartition->setPartitionSpec(spec);
privatePartition->setDefaultOwner(partitionOwner);
privatePartition->setObjectName(QStringLiteral("private"));
if (!privatePartition->open()) {
+ delete privatePartition;
+ privatePartition = 0;
message = QStringLiteral("Unable to open private partition");
return QJsonDbRequest::InvalidPartition;
}
diff --git a/src/client/qjsondbprivatepartition_p.h b/src/client/qjsondbprivatepartition_p.h
index 838e26b..6a18e7f 100644
--- a/src/client/qjsondbprivatepartition_p.h
+++ b/src/client/qjsondbprivatepartition_p.h
@@ -71,7 +71,7 @@ QT_BEGIN_NAMESPACE_JSONDB
class QJsonDbConnectionPrivate;
-class QJsonDbPrivatePartition : public QObject
+class Q_JSONDB_EXPORT QJsonDbPrivatePartition : public QObject
{
Q_OBJECT
public:
diff --git a/src/client/qjsondbstandardpaths.cpp b/src/client/qjsondbstandardpaths.cpp
new file mode 100644
index 0000000..6897a17
--- /dev/null
+++ b/src/client/qjsondbstandardpaths.cpp
@@ -0,0 +1,80 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "private/qjsondbstandardpaths_p.h"
+
+#include <QtCore/qdir.h>
+
+#include <pwd.h>
+
+QT_BEGIN_NAMESPACE_JSONDB
+
+static bool qtjsondb_autotestMode = false;
+
+QString QJsonDbStandardPaths::homePath(const QString &user)
+{
+ if (qtjsondb_autotestMode) {
+ if (user == QLatin1String("userthatdoesnotexist"))
+ return QString();
+ return QDir::homePath() + QLatin1String("/.qttest/qtjsondb/") + user;
+ }
+
+ struct passwd *pwd = getpwnam(user.toLatin1().constData()); // not thread-safe!
+ if (!pwd)
+ return QString();
+
+ return QString::fromUtf8(pwd->pw_dir);
+}
+
+QString QJsonDbStandardPaths::currentUser()
+{
+ if (qtjsondb_autotestMode)
+ return QStringLiteral("fakeroot");
+ return QString::fromLatin1(qgetenv("USER"));
+}
+
+void QJsonDbStandardPaths::setAutotestMode(bool value)
+{
+ qtjsondb_autotestMode = value;
+}
+
+
+QT_END_NAMESPACE_JSONDB
diff --git a/src/client/qjsondbstandardpaths_p.h b/src/client/qjsondbstandardpaths_p.h
new file mode 100644
index 0000000..f2de19c
--- /dev/null
+++ b/src/client/qjsondbstandardpaths_p.h
@@ -0,0 +1,77 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QJSONDB_STANDARD_PATHS_P_H
+#define QJSONDB_STANDARD_PATHS_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the QtJsonDb API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QString>
+
+#include "qjsondbglobal.h"
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE_JSONDB
+
+class Q_JSONDB_EXPORT QJsonDbStandardPaths
+{
+public:
+ static QString homePath(const QString &user);
+ static QString currentUser();
+
+ static void setAutotestMode(bool value);
+};
+
+QT_END_NAMESPACE_JSONDB
+
+QT_END_HEADER
+
+#endif // QJSONDB_STANDARD_PATHS_P_H
diff --git a/tests/auto/client/test-jsondb-client.cpp b/tests/auto/client/test-jsondb-client.cpp
index 45dda54..03099c4 100644
--- a/tests/auto/client/test-jsondb-client.cpp
+++ b/tests/auto/client/test-jsondb-client.cpp
@@ -436,21 +436,12 @@ void TestJsonDbClient::cleanupTestCase()
removeDbFiles();
- struct passwd *pwd = getpwnam(qgetenv("USER"));
- if (pwd) {
- QDir homePartition(QString::fromLatin1("%1/.jsondb").arg(QString::fromUtf8(pwd->pw_dir)));
- foreach (const QString &file, homePartition.entryList())
- QFile::remove(homePartition.absoluteFilePath(file));
-
- homePartition.cdUp();
- homePartition.rmpath(QStringLiteral(".jsondb"));
- }
-
stopDaemon();
}
void TestJsonDbClient::init()
{
+ clearHelperData();
connectToServer();
}
diff --git a/tests/auto/qjsondbflushrequest/testqjsondbflushrequest.cpp b/tests/auto/qjsondbflushrequest/testqjsondbflushrequest.cpp
index 8a0550b..d4062ba 100644
--- a/tests/auto/qjsondbflushrequest/testqjsondbflushrequest.cpp
+++ b/tests/auto/qjsondbflushrequest/testqjsondbflushrequest.cpp
@@ -81,6 +81,7 @@ void TestQJsonDbFlushRequest::cleanupTestCase()
void TestQJsonDbFlushRequest::init()
{
+ clearHelperData();
connectToServer();
}
diff --git a/tests/auto/qjsondbrequest/testqjsondbrequest.cpp b/tests/auto/qjsondbrequest/testqjsondbrequest.cpp
index 86a1e82..e0de07f 100644
--- a/tests/auto/qjsondbrequest/testqjsondbrequest.cpp
+++ b/tests/auto/qjsondbrequest/testqjsondbrequest.cpp
@@ -43,6 +43,7 @@
#include "qjsondbobject.h"
#include "qjsondbreadrequest.h"
#include "qjsondbwriterequest.h"
+#include "private/qjsondbstandardpaths_p.h"
#include "testhelper.h"
#include <QDebug>
@@ -96,6 +97,7 @@ private slots:
void updateRequest();
void privatePartition();
void privatePartition2();
+ void privatePartitions();
void invalidPrivatePartition();
void removeRequest();
void forced();
@@ -110,6 +112,7 @@ private:
void TestQJsonDbRequest::initTestCase()
{
+ QJsonDbStandardPaths::setAutotestMode(true);
removeDbFiles();
QStringList arg_list = QStringList() << "-validate-schemas";
@@ -123,21 +126,12 @@ void TestQJsonDbRequest::cleanupTestCase()
QFile partitionsFile(QFileInfo(QFINDTESTDATA("partitions.json")).absoluteDir().absoluteFilePath(QLatin1String("partitions-test.json")));
partitionsFile.remove();
- struct passwd *pwd = getpwnam(qgetenv("USER"));
- if (pwd) {
- QDir homePartition(QString::fromLatin1("%1/.jsondb").arg(QString::fromUtf8(pwd->pw_dir)));
- foreach (const QString &file, homePartition.entryList(QStringList() << QLatin1String("*.db")))
- QFile::remove(homePartition.absoluteFilePath(file));
-
- homePartition.cdUp();
- homePartition.rmdir(QStringLiteral(".jsondb"));
- }
-
stopDaemon();
}
void TestQJsonDbRequest::init()
{
+ clearHelperData();
connectToServer();
}
@@ -826,7 +820,7 @@ void TestQJsonDbRequest::privatePartition()
QJsonDbWriteRequest write;
write.setObjects(QList<QJsonObject>() << contact1 << contact2);
// use the explicit form of the private partition (with full username)
- write.setPartition(QString::fromLatin1("%1.Private").arg(QString::fromLatin1(qgetenv("USER"))));
+ write.setPartition(QString::fromLatin1("%1.Private").arg(QJsonDbStandardPaths::currentUser()));
mConnection->send(&write);
QVERIFY(waitForResponse(&write));
QVERIFY(!mRequestErrors.contains(&write));
@@ -879,7 +873,7 @@ void TestQJsonDbRequest::privatePartition()
QCOMPARE(results[0].value(QStringLiteral("name")).toString(), contact2.value(QStringLiteral("name")).toString());
// switch
- query.setPartition(QString::fromLatin1("%1.Private").arg(QString::fromLatin1(qgetenv("USER"))));
+ query.setPartition(QString::fromLatin1("%1.Private").arg(QJsonDbStandardPaths::currentUser()));
write.setPartition(QStringLiteral("Private"));
contact2.insert(QStringLiteral("_deleted"), true);
@@ -937,6 +931,57 @@ void TestQJsonDbRequest::privatePartition2()
QCOMPARE((int)statuses.at(2), (int)QJsonDbRequest::Finished);
}
+void TestQJsonDbRequest::privatePartitions()
+{
+ QJsonDbObject contact1;
+ contact1.insert(QStringLiteral("_uuid"), QStringLiteral("{9a57c619-fa16-4259-acf5-7d670f59674f}"));
+ contact1.insert(QStringLiteral("_type"), QStringLiteral("privatePartitions"));
+ contact1.insert(QStringLiteral("name"), QStringLiteral("Joe"));
+ QJsonDbObject contact2;
+ contact2.insert(QStringLiteral("_type"), QStringLiteral("privatePartitions"));
+ contact2.insert(QStringLiteral("_uuid"), QStringLiteral("{d87707c5-71ae-4524-813e-4dd9dda55a87}"));
+ contact2.insert(QStringLiteral("name"), QStringLiteral("Alice"));
+
+ QJsonDbWriteRequest write1;
+ write1.setObjects(QList<QJsonObject>() << contact1);
+ write1.setPartition(QStringLiteral("joe.Private"));
+ mConnection->send(&write1);
+ QVERIFY(waitForResponse(&write1));
+
+ QJsonDbWriteRequest write2;
+ write2.setObjects(QList<QJsonObject>() << contact2);
+ write2.setPartition(QStringLiteral("alice.Private"));
+ mConnection->send(&write2);
+ QVERIFY(waitForResponse(&write2));
+
+ {
+ QJsonDbReadRequest read(QStringLiteral("[?_type=\"privatePartitions\"]"));
+ read.setPartition(QStringLiteral("Private"));
+ mConnection->send(&read);
+ QVERIFY(waitForResponse(&read));
+ QList<QJsonObject> results = read.takeResults();
+ QCOMPARE(results.size(), 0);
+ }
+ {
+ QJsonDbReadRequest read(QStringLiteral("[?_type=\"privatePartitions\"]"));
+ read.setPartition(QStringLiteral("alice.Private"));
+ mConnection->send(&read);
+ QVERIFY(waitForResponse(&read));
+ QList<QJsonObject> results = read.takeResults();
+ QCOMPARE(results.size(), 1);
+ QCOMPARE(results.at(0).value(QStringLiteral("name")).toString(), QLatin1String("Alice"));
+ }
+ {
+ QJsonDbReadRequest read(QStringLiteral("[?_type=\"privatePartitions\"]"));
+ read.setPartition(QStringLiteral("joe.Private"));
+ mConnection->send(&read);
+ QVERIFY(waitForResponse(&read));
+ QList<QJsonObject> results = read.takeResults();
+ QCOMPARE(results.size(), 1);
+ QCOMPARE(results.at(0).value(QStringLiteral("name")).toString(), QLatin1String("Joe"));
+ }
+}
+
void TestQJsonDbRequest::invalidPrivatePartition()
{
QJsonObject contact1;
diff --git a/tests/auto/qjsondbwatcher/testqjsondbwatcher.cpp b/tests/auto/qjsondbwatcher/testqjsondbwatcher.cpp
index 3ddeacc..2c1da18 100644
--- a/tests/auto/qjsondbwatcher/testqjsondbwatcher.cpp
+++ b/tests/auto/qjsondbwatcher/testqjsondbwatcher.cpp
@@ -60,6 +60,7 @@
#include "qjsondbwatcher.h"
#include "qjsondbwriterequest.h"
#include "private/qjsondbstrings_p.h"
+#include "private/qjsondbstandardpaths_p.h"
#include "testhelper.h"
@@ -103,6 +104,7 @@ TestQJsonDbWatcher::~TestQJsonDbWatcher()
void TestQJsonDbWatcher::initTestCase()
{
+ QJsonDbStandardPaths::setAutotestMode(true);
removeDbFiles();
QStringList arg_list = QStringList() << "-validate-schemas";
@@ -117,6 +119,7 @@ void TestQJsonDbWatcher::cleanupTestCase()
void TestQJsonDbWatcher::init()
{
+ clearHelperData();
connectToServer();
}
@@ -799,7 +802,7 @@ void TestQJsonDbWatcher::privatePartition()
// watchers are not supported on private partitions, so it should fail
QJsonDbWatcher privateWatcher;
privateWatcher.setQuery("[?_type=\"foo\"]");
- privateWatcher.setPartition(QString::fromLatin1("%1.Private").arg(QString::fromLatin1(qgetenv("USER"))));
+ privateWatcher.setPartition(QString::fromLatin1("%1.Private").arg(QJsonDbStandardPaths::currentUser()));
QVERIFY(!mConnection->addWatcher(&privateWatcher));
}
diff --git a/tests/benchmarks/client/client-benchmark.cpp b/tests/benchmarks/client/client-benchmark.cpp
index 27c7bba..6ff2ffd 100644
--- a/tests/benchmarks/client/client-benchmark.cpp
+++ b/tests/benchmarks/client/client-benchmark.cpp
@@ -179,6 +179,7 @@ void ClientBenchmark::cleanupTestCase()
void ClientBenchmark::init()
{
+ clearHelperData();
connectToServer();
}
diff --git a/tests/shared/testhelper.cpp b/tests/shared/testhelper.cpp
index b4ceef0..bb79d41 100644
--- a/tests/shared/testhelper.cpp
+++ b/tests/shared/testhelper.cpp
@@ -268,10 +268,23 @@ void TestHelper::removeDbFiles(const QStringList &additionalFiles)
if (dontLaunch())
return;
- QStringList files = QDir().entryList(QStringList() << QLatin1String("*.db"));
+ QStringList files = QDir().entryList(QStringList() << QStringLiteral("*.db"));
files << additionalFiles;
foreach (const QString &fileName, files)
QFile::remove(fileName);
+
+ QDir privatePartitionsDir = QDir::home();
+ if (privatePartitionsDir.cd(QStringLiteral(".qttest")) && privatePartitionsDir.cd(QStringLiteral("qtjsondb"))) {
+ QStringList subdirs = privatePartitionsDir.entryList(QDir::NoDotAndDotDot | QDir::Dirs);
+ foreach (const QString &subdirName, subdirs) {
+ QDir subdir = privatePartitionsDir;
+ subdir.cd(subdirName);
+ subdir.cd(QStringLiteral(".jsondb"));
+ QStringList files = subdir.entryList(QStringList() << QStringLiteral("*.db"));
+ foreach (const QString &filename, files)
+ subdir.remove(filename);
+ }
+ }
}
bool TestHelper::waitForResponse(QJsonDbRequest *request)
@@ -541,4 +554,12 @@ void TestHelper::timeout()
qCritical() << "A timeout occurred";
}
-
+void TestHelper::clearHelperData()
+{
+ mNotificationsReceived = 0;
+ mNotificationsExpected = 0;
+ mLastStateChangedExpected = 0;
+ mLastStateChangedReceived = 0;
+ mRequestErrors.clear();
+ mRequestStatuses.clear();
+}
diff --git a/tests/shared/testhelper.h b/tests/shared/testhelper.h
index c6411de..3a46e13 100644
--- a/tests/shared/testhelper.h
+++ b/tests/shared/testhelper.h
@@ -99,6 +99,8 @@ public:
const QString &workingDirectory() const { return mWorkingDirectory; }
void setWorkingDirectory(const QString &workingDirectory) { mWorkingDirectory = workingDirectory; }
+ void clearHelperData();
+
protected:
QProcess *mProcess;
QtJsonDb::QJsonDbConnection *mConnection;