From kde-commits Sun Sep 04 15:26:47 2011 From: Michael Leupold Date: Sun, 04 Sep 2011 15:26:47 +0000 To: kde-commits Subject: [kdelibs/ksecretsservice] /: Get some of the unit-tests back into Message-Id: <20110904152647.8A3CCA60DC () git ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=131515004926880 Git commit f306a8a4684b17b131bd9af657c45cf1407d5e30 by Michael Leupold. Committed on 29/10/2010 at 23:32. Pushed by vrusu into branch 'ksecretsservice'. Get some of the unit-tests back into shape after the latest commits. Some parts still missing unfortunately. svn path=/trunk/playground/base/ksecretservice/; revision=1191187 M +39 -8 peer.h M +16 -26 jobinfostructs.h M +70 -12 peer.cpp http://commits.kde.org/kdelibs/f306a8a4684b17b131bd9af657c45cf1407d5e30 diff --git a/jobinfostructs.h b/jobinfostructs.h index 267d668..3b65351 100644 --- a/jobinfostructs.h +++ b/jobinfostructs.h @@ -47,35 +47,27 @@ /** * This is the base class of all the jobs value objects * It takes ownership of the Peer pointer and deletes it - * when it's destructor is called + * when it's destructor is called. */ struct JobBaseInfo { /** * The peer that requested the delete operation. - * Beware when storing the pointer to the peer outside - * this class as it's deleting it when destructed */ - const Peer* m_peer; - JobBaseInfo( const Peer* peer ) : m_peer( peer ) + const Peer m_peer; + JobBaseInfo(const Peer &peer) : m_peer(peer) { /* nothing */ } - - virtual ~JobBaseInfo() - { - if (m_peer != 0) - delete m_peer, m_peer = 0; - } }; class BackendCollection; /** * This job value object holds information needed when creating - * a new collection.auto. + * a new collection. */ struct CollectionCreateInfo : public JobBaseInfo { - const QString &m_label; /*!< new collection label */ + const QString m_label; /*!< new collection label */ bool m_locked; /*!< if true, the collection will be locked after creation */ /** @@ -84,8 +76,8 @@ struct CollectionCreateInfo : public JobBaseInfo * @param label New collection's label * @param peer Information about the d-bus peer which requests collection creation */ - CollectionCreateInfo( const QString& label, const Peer* peer ) : - JobBaseInfo(peer), m_label( label ), m_locked(false) + CollectionCreateInfo(const QString& label, const Peer &peer) : + JobBaseInfo(peer), m_label(label), m_locked(false) { /* nothing */ } }; @@ -101,8 +93,7 @@ struct CollectionDeleteInfo : public JobBaseInfo * @param peer Information about the d-bus peer which requests collection delete * @param collection The collection to be deleted. If NULL, it must be specified later */ - CollectionDeleteInfo( const Peer* peer, BackendCollection *collection =0) : JobBaseInfo( peer ), - m_collection(collection) + CollectionDeleteInfo(const Peer &peer) : JobBaseInfo(peer), m_collection(0) { /* nothing */ } }; @@ -118,8 +109,7 @@ struct CollectionUnlockInfo : public JobBaseInfo * @param peer Information about the d-bus peer which requests collection unlocking * @param collection The collection to be unkocked. If NULL, it must be specified later. */ - CollectionUnlockInfo( const Peer* peer, BackendCollection *collection =0) : JobBaseInfo( peer ), - m_collection(collection) + CollectionUnlockInfo(const Peer &peer) : JobBaseInfo(peer), m_collection(0) { /* nothing */ } }; @@ -127,9 +117,9 @@ class BackendItem; struct ItemCreateInfo : public JobBaseInfo { - const QString &m_label; /*!< Item label */ - const QMap &m_attributes; /*!< Collection of attributes to set for the new item */ - const QCA::SecureArray &m_secret; /*!< The secret to store */ + const QString m_label; /*!< Item label */ + const QMap m_attributes; /*!< Collection of attributes to set for the new item */ + const QCA::SecureArray m_secret; /*!< The secret to store */ bool m_replace; /*!< If true, replace an item with the same attributes if it already exists */ bool m_locked; /*!< true if the item should be locked after creation, false else */ @@ -146,8 +136,8 @@ struct ItemCreateInfo : public JobBaseInfo const QCA::SecureArray &secret, bool replace, bool locked, - const Peer* peer) : - JobBaseInfo( peer ), + const Peer &peer) : + JobBaseInfo(peer), m_label(label), m_attributes(attributes), m_secret(secret), m_replace(replace), m_locked(locked) { /* nothing */ } @@ -164,7 +154,7 @@ struct ItemDeleteInfo : public JobBaseInfo * @param peer the d-bus peer that initiated the item delete operation * @param item the item to be deleted. If NULL, it must be specified later. */ - ItemDeleteInfo( const Peer* peer, BackendItem *item =0) : JobBaseInfo( peer ), m_item( item ) + ItemDeleteInfo( const Peer &peer) : JobBaseInfo(peer), m_item(0) { /* nothing */ } }; @@ -179,7 +169,7 @@ struct ItemUnlockInfo : public JobBaseInfo * @param peer the d-bus peer that initiated the item delete operation * @param item the item to be deleted. If NULL, it must be specified later. */ - ItemUnlockInfo( const Peer *peer, BackendItem *item =0) : JobBaseInfo( peer ), m_item( item ) + ItemUnlockInfo( const Peer &peer) : JobBaseInfo(peer), m_item(0) { /* nothing */ } }; diff --git a/peer.cpp b/peer.cpp index 2f11768..e235b70 100644 --- a/peer.cpp +++ b/peer.cpp @@ -20,38 +20,96 @@ #include "peer.h" -#include +#include +#include #include -Peer::Peer( uint pid ) : - m_pid( pid ) +class PeerData : public QSharedData { +public: + PeerData() : m_valid(false), m_pid(0) {} + PeerData(const PeerData &other) : QSharedData(other), m_pid(other.m_pid) {} + ~PeerData() {} + + // designates whether this Peer is valid as it's not certain that + // a pid of 0 can always denote an invalid peer. + bool m_valid; + + // peer process pid + uint m_pid; +}; + +Peer::Peer() : d(new PeerData) +{ +} + +Peer::Peer(const Peer &other) : d(other.d) +{ +} + +Peer &Peer::operator=(const Peer &other) +{ + d = other.d; + return *this; +} + +Peer::Peer(uint pid) : d(new PeerData) +{ + d->m_valid = true; + d->m_pid = pid; + kDebug() << "Peer " << pid; kDebug() << " cmdLine = " << cmdLine(); kDebug() << " exePath = " << exePath(); } +Peer::~Peer() +{ +} + QString Peer::procFileName() const { - return QString("/proc/%1").arg( m_pid ); + Q_ASSERT(d->m_valid); + return QString("/proc/%1").arg(d->m_pid); +} + +QByteArray Peer::cmdLine() const +{ + if (!d->m_valid) { + return QByteArray(); + } else { + QFile procFile(QString("%1/cmd").arg(procFileName())); + if (!procFile.open(QIODevice::ReadOnly | QIODevice::Text) || + procFile.atEnd()) { + // file doesn't exist or is empty + return QByteArray(); + } else { + return procFile.readLine(); + } + } } -QString Peer::cmdLine() const +bool Peer::isValid() const { - QFile procFile( QString("%s/cmd").arg( procFileName() ) ); - return procFile.readLine(); + return d->m_valid; } bool Peer::isStillRunning() const { - QFile procFile( procFileName() ); - return procFile.exists(); + if (!d->m_valid) { + return false; + } else { + return QFile::exists(procFileName()); + } } QString Peer::exePath() const { - QFile procFile( QString("%s/exe").arg( procFileName() )) ; - // TODO: add a watch an trigger signal when the peer process ends - return procFile.symLinkTarget(); + if (!d->m_valid) { + return QString(); + } else { + // TODO: add a watch an trigger signal when the peer process ends + QFile::symLinkTarget(QString("%1/exe").arg(procFileName())); + } } diff --git a/peer.h b/peer.h index 2e3a6e7..a16adaf 100644 --- a/peer.h +++ b/peer.h @@ -21,7 +21,10 @@ #ifndef DEAMON_PEER_H #define DEAMON_PEER_H -#include +#include +#include + +class PeerData; /** * Representation of a daemon peer, wich is typically a client application. @@ -31,7 +34,30 @@ class Peer { public: - Peer( uint pid ); + /** + * Construct an invalid (non-existant) Peer. + */ + Peer(); + + /** + * Copy constructor. + */ + Peer(const Peer &other); + + /** + * Copy operator. + */ + Peer &operator=(const Peer &other); + + /** + * Construct a valid Peer representing the process with the given pid. + */ + explicit Peer(uint pid); + + /** + * Destructor. + */ + ~Peer(); /** * Get the running state of the peer process @@ -46,19 +72,24 @@ public: /** * Get the command line used to launch the running process */ - QString cmdLine() const; + QByteArray cmdLine() const; + + /** + * Check if the peer is valid. An invalid peer identifies test applications + * or requests sent in-process. + */ + bool isValid() const; private: /** * Helper method witch returns the /proc/pid path for the - * peer process + * peer process. + * + * @note Must only be called on valid peers. */ QString procFileName() const; - /** - * Peer process PID - */ - uint m_pid; + QSharedDataPointer d; }; #endif // DEAMON_PEER_H