Merge lp:~mandel/ubuntu-download-manager/allow-client-object-path into lp:ubuntu-download-manager

Proposed by Manuel de la Peña
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 126
Merged at revision: 121
Proposed branch: lp:~mandel/ubuntu-download-manager/allow-client-object-path
Merge into: lp:ubuntu-download-manager
Prerequisite: lp:~mandel/ubuntu-download-manager/increase-tests
Diff against target: 372 lines (+218/-13)
7 files modified
libubuntudownloadmanager/apparmor.cpp (+21/-10)
libubuntudownloadmanager/apparmor.h (+1/-0)
libubuntudownloadmanager/download_factory.cpp (+23/-3)
ubuntu-download-manager-tests/fake_apparmor.cpp (+19/-0)
ubuntu-download-manager-tests/fake_apparmor.h (+1/-0)
ubuntu-download-manager-tests/test_download_factory.cpp (+146/-0)
ubuntu-download-manager-tests/test_download_factory.h (+7/-0)
To merge this branch: bzr merge lp:~mandel/ubuntu-download-manager/allow-client-object-path
Reviewer Review Type Date Requested Status
Diego Sarmentero (community) Approve
Alejandro J. Cura (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+184809@code.launchpad.net

Commit message

Added the new metadata key 'objectpath' where the client can propose a uuid for the download that will be used to create the path.

Description of the change

Added the new metadata key 'objectpath' where the client can propose a uuid for the download that will be used to create the path. The string text, which must be formatted as five hex fields separated by '-', e.g., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where 'x' is a hex digit. The curly braces shown here are optional, but it is normal to include them. If the conversion fails the download manager will generate a correct uuid.

To post a comment you must log in.
Revision history for this message
Manuel de la Peña (mandel) wrote :

I have added documentation to the following wiki page: https://wiki.ubuntu.com/DownloadService it contains some example code in python and I'll add more related to the group download.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Alejandro J. Cura (alecu) wrote :

Looks good. I'll check about using UUIDs in the client, and will open a bug if needed.

review: Approve
Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libubuntudownloadmanager/apparmor.cpp'
2--- libubuntudownloadmanager/apparmor.cpp 2013-09-10 15:36:23 +0000
3+++ libubuntudownloadmanager/apparmor.cpp 2013-09-10 15:36:23 +0000
4@@ -41,9 +41,8 @@
5 _uuidFactory = new UuidFactory();
6 }
7
8- QPair<QUuid, QString> getUuidString(QString path) {
9+ QPair<QUuid, QString> getUuidString(QUuid id, QString path) {
10 qDebug() << __PRETTY_FUNCTION__ << path;
11- QUuid id = _uuidFactory->createUuid();
12 QString idString = path + "/" + id.toString().replace(
13 QRegExp("[-{}]"), "");
14 qDebug() << "Download path is" << idString;
15@@ -51,8 +50,13 @@
16 }
17
18 QPair<QUuid, QString> getSecurePath(QString connectionName) {
19+ QUuid id = _uuidFactory->createUuid();
20+ return getSecurePath(id, connectionName);
21+ }
22+
23+ QPair<QUuid, QString> getSecurePath(QUuid id, QString connectionName) {
24 if (connectionName.isEmpty()) {
25- return getUuidString(QString(BASE_ACCOUNT_URL));
26+ return getUuidString(id, QString(BASE_ACCOUNT_URL));
27 }
28
29 QDBusPendingReply<QString> reply =
30@@ -61,14 +65,14 @@
31 reply.waitForFinished();
32 if (reply.isError()) {
33 qCritical() << reply.error();
34- return getUuidString(QString(BASE_ACCOUNT_URL));
35+ return getUuidString(id, QString(BASE_ACCOUNT_URL));
36 } else {
37 // use the returned value
38 QString appId = reply.value();
39 qDebug() << "AppId is " << appId;
40
41 if (appId.isEmpty() || appId == UNCONFINED_ID) {
42- return getUuidString(QString(BASE_ACCOUNT_URL));
43+ return getUuidString(id, QString(BASE_ACCOUNT_URL));
44 } else {
45 QByteArray appIdBa = appId.toUtf8();
46
47@@ -77,17 +81,18 @@
48 appIdBa.data(), NULL);
49
50 if (appIdPath == NULL) {
51- qCritical() << "Unable to allocate memory for nih_dbus_path()";
52- return getUuidString(QString(BASE_ACCOUNT_URL));
53+ qCritical() << "Unable to allocate memory for "
54+ << "nih_dbus_path()";
55+ return getUuidString(id, QString(BASE_ACCOUNT_URL));
56 }
57 QString path = QString(appIdPath);
58 qDebug() << "AppId path is " << appIdPath;
59
60 // free nih data
61 nih_free(appIdPath);
62- return getUuidString(path);
63- } // not empty appid string
64- } // no dbus error
65+ return getUuidString(id, path);
66+ } // not empty appid string
67+ } // no dbus error
68 }
69
70 private:
71@@ -116,3 +121,9 @@
72 Q_D(AppArmor);
73 return d->getSecurePath(connectionName);
74 }
75+
76+QPair<QUuid, QString>
77+AppArmor::getSecurePath(QUuid id, QString connectionName) {
78+ Q_D(AppArmor);
79+ return d->getSecurePath(id, connectionName);
80+}
81
82=== modified file 'libubuntudownloadmanager/apparmor.h'
83--- libubuntudownloadmanager/apparmor.h 2013-09-10 15:36:23 +0000
84+++ libubuntudownloadmanager/apparmor.h 2013-09-10 15:36:23 +0000
85@@ -33,6 +33,7 @@
86 explicit AppArmor(QObject *parent = 0);
87
88 virtual QPair<QUuid, QString> getSecurePath(QString connName);
89+ virtual QPair<QUuid, QString> getSecurePath(QUuid id, QString connName);
90
91 private:
92 // use pimpl so that we can mantains ABI compatibility
93
94=== modified file 'libubuntudownloadmanager/download_factory.cpp'
95--- libubuntudownloadmanager/download_factory.cpp 2013-09-10 15:36:23 +0000
96+++ libubuntudownloadmanager/download_factory.cpp 2013-09-10 15:36:23 +0000
97@@ -23,6 +23,7 @@
98 #include "./single_download.h"
99 #include "./download_factory.h"
100
101+#define OBJECT_PATH_KEY "objectpath"
102 /*
103 * PRIVATE IMPLEMENTATION
104 */
105@@ -45,11 +46,30 @@
106 q_ptr(parent) {
107 }
108
109+ QPair<QUuid, QString> getDownloadPath(const QString& dbusOwner,
110+ const QVariantMap& metadata) {
111+ qDebug() << __PRETTY_FUNCTION__ << dbusOwner << metadata;
112+ if (metadata.contains(OBJECT_PATH_KEY)) {
113+ // create a uuid using the string value form the metadata
114+ QUuid id = QUuid(metadata[OBJECT_PATH_KEY].toString());
115+ if (id.isNull()) {
116+ qCritical() << "Uuid sent by client is NULL";
117+ return _apparmor->getSecurePath(dbusOwner);
118+ } else {
119+ qDebug() << "Using the id from the client" << id;
120+ return _apparmor->getSecurePath(id, dbusOwner);
121+ }
122+ } else {
123+ qDebug() << "DownloadFactory assigns the Download Uuid.";
124+ return _apparmor->getSecurePath(dbusOwner);
125+ }
126+ }
127+
128 Download* createDownload(const QString& dbusOwner,
129 const QUrl& url,
130 const QVariantMap& metadata,
131 const QMap<QString, QString>& headers) {
132- QPair<QUuid, QString> idData = _apparmor->getSecurePath(dbusOwner);
133+ QPair<QUuid, QString> idData = getDownloadPath(dbusOwner, metadata);
134 Download* down = new SingleDownload(idData.first, idData.second, url,
135 metadata, headers, _networkInfo, _nam, _processFactory);
136 DownloadAdaptor* adaptor = new DownloadAdaptor(down);
137@@ -63,7 +83,7 @@
138 QCryptographicHash::Algorithm algo,
139 const QVariantMap& metadata,
140 const QMap<QString, QString>& headers) {
141- QPair<QUuid, QString> idData = _apparmor->getSecurePath(dbusOwner);
142+ QPair<QUuid, QString> idData = getDownloadPath(dbusOwner, metadata);
143 Download* down = new SingleDownload(idData.first, idData.second, url,
144 hash, algo, metadata, headers, _networkInfo, _nam,
145 _processFactory);
146@@ -78,7 +98,7 @@
147 bool allowed3G,
148 const QVariantMap& metadata,
149 StringMap headers) {
150- QPair<QUuid, QString> idData = _apparmor->getSecurePath(dbusOwner);
151+ QPair<QUuid, QString> idData = getDownloadPath(dbusOwner, metadata);
152 Download* down = new GroupDownload(idData.first, idData.second,
153 downloads, algo, allowed3G, metadata, headers, _networkInfo,
154 _self);
155
156=== modified file 'ubuntu-download-manager-tests/fake_apparmor.cpp'
157--- ubuntu-download-manager-tests/fake_apparmor.cpp 2013-09-10 15:36:23 +0000
158+++ ubuntu-download-manager-tests/fake_apparmor.cpp 2013-09-10 15:36:23 +0000
159@@ -42,3 +42,22 @@
160
161 return QPair<QUuid, QString>(id, uuidString);
162 }
163+
164+QPair<QUuid, QString>
165+FakeAppArmor::getSecurePath(QUuid id, QString connName) {
166+ QString uuidString = id.toString().replace(QRegExp("[-{}]"), "");
167+ if (_recording) {
168+ QList<QObject*> inParams;
169+ inParams.append(new UuidWrapper(id));
170+ inParams.append(new StringWrapper(connName));
171+
172+ QList<QObject*> outParams;
173+ outParams.append(new UuidWrapper(id));
174+ outParams.append(new StringWrapper(uuidString));
175+ MethodParams params(inParams, outParams);
176+ MethodData methodData("getSecurePath", params);
177+ _called.append(methodData);
178+ }
179+
180+ return QPair<QUuid, QString>(id, uuidString);
181+}
182
183=== modified file 'ubuntu-download-manager-tests/fake_apparmor.h'
184--- ubuntu-download-manager-tests/fake_apparmor.h 2013-09-10 15:36:23 +0000
185+++ ubuntu-download-manager-tests/fake_apparmor.h 2013-09-10 15:36:23 +0000
186@@ -32,6 +32,7 @@
187 QObject *parent = 0);
188
189 QPair<QUuid, QString> getSecurePath(QString connName) override;
190+ QPair<QUuid, QString> getSecurePath(QUuid id, QString connName) override;
191
192 private:
193 QSharedPointer<UuidFactory> _uuidFactory;
194
195=== modified file 'ubuntu-download-manager-tests/test_download_factory.cpp'
196--- ubuntu-download-manager-tests/test_download_factory.cpp 2013-09-10 15:36:23 +0000
197+++ ubuntu-download-manager-tests/test_download_factory.cpp 2013-09-10 15:36:23 +0000
198@@ -105,3 +105,149 @@
199 QCOMPARE(download->downloadId(), id->value());
200 QCOMPARE(download->path(), path->value());
201 }
202+
203+void
204+TestDownloadFactory::testCreateDownloadWithValidUuid() {
205+ _apparmor->record();
206+
207+ // create a download, assert that it was
208+ // created and that the id and the path are correctly set
209+ QUuid id = QUuid::createUuid();
210+
211+ QVariantMap metadata;
212+ metadata["objectpath"] = id.toString();
213+
214+ Download* download = _downFactory->createDownload("", QUrl(),
215+ metadata, QMap<QString, QString>());
216+
217+ QList<MethodData> calledMethods = _apparmor->calledMethods();
218+ QCOMPARE(1, calledMethods.count());
219+ StringWrapper* path = reinterpret_cast<StringWrapper*>(
220+ calledMethods[0].params().outParams()[1]);
221+ QCOMPARE(download->downloadId(), id);
222+ QCOMPARE(download->path(), path->value());
223+}
224+
225+void
226+TestDownloadFactory::testCreateDownloadWithNullUuid() {
227+ _apparmor->record();
228+
229+ // create a download, assert that it was
230+ // created and that the id and the path are correctly set
231+ QVariantMap metadata;
232+ metadata["objectpath"] = "bad-id";
233+
234+ Download* download = _downFactory->createDownload("", QUrl(),
235+ QVariantMap(), QMap<QString, QString>());
236+
237+ QList<MethodData> calledMethods = _apparmor->calledMethods();
238+ QCOMPARE(1, calledMethods.count());
239+ UuidWrapper* id = reinterpret_cast<UuidWrapper*>(
240+ calledMethods[0].params().outParams()[0]);
241+ StringWrapper* path = reinterpret_cast<StringWrapper*>(
242+ calledMethods[0].params().outParams()[1]);
243+ QCOMPARE(download->downloadId(), id->value());
244+ QCOMPARE(download->path(), path->value());
245+}
246+
247+void
248+TestDownloadFactory::testCreateDownloadWithHashAndUuid() {
249+ _apparmor->record();
250+
251+ QUuid id = QUuid::createUuid();
252+
253+ QVariantMap metadata;
254+ metadata["objectpath"] = id.toString();
255+
256+ QString hash = "my-hash";
257+ QCryptographicHash::Algorithm algo = QCryptographicHash::Md5;
258+
259+ // same as above but assert hash and hash algo
260+ Download* download = _downFactory->createDownload("", QUrl(),
261+ hash, algo, metadata, QMap<QString, QString>());
262+
263+ QList<MethodData> calledMethods = _apparmor->calledMethods();
264+ QCOMPARE(1, calledMethods.count());
265+ StringWrapper* path = reinterpret_cast<StringWrapper*>(
266+ calledMethods[0].params().outParams()[1]);
267+ QCOMPARE(download->downloadId(), id);
268+ QCOMPARE(download->path(), path->value());
269+
270+ SingleDownload* single = reinterpret_cast<SingleDownload*>(download);
271+ QCOMPARE(hash, single->hash());
272+ QCOMPARE(algo, single->hashAlgorithm());
273+}
274+
275+void
276+TestDownloadFactory::testCreateDownloadWithHashAndNullUuid() {
277+ _apparmor->record();
278+
279+ QVariantMap metadata;
280+ metadata["objectpath"] = "bad-id";
281+
282+ QString hash = "my-hash";
283+ QCryptographicHash::Algorithm algo = QCryptographicHash::Md5;
284+
285+ // same as above but assert hash and hash algo
286+ Download* download = _downFactory->createDownload("", QUrl(),
287+ hash, algo, metadata, QMap<QString, QString>());
288+
289+ QList<MethodData> calledMethods = _apparmor->calledMethods();
290+ QCOMPARE(1, calledMethods.count());
291+ UuidWrapper* id = reinterpret_cast<UuidWrapper*>(
292+ calledMethods[0].params().outParams()[0]);
293+ StringWrapper* path = reinterpret_cast<StringWrapper*>(
294+ calledMethods[0].params().outParams()[1]);
295+ QCOMPARE(download->downloadId(), id->value());
296+ QCOMPARE(download->path(), path->value());
297+
298+ SingleDownload* single = reinterpret_cast<SingleDownload*>(download);
299+ QCOMPARE(hash, single->hash());
300+ QCOMPARE(algo, single->hashAlgorithm());
301+}
302+
303+void
304+TestDownloadFactory::testCreateGroupDownloadWithValidUuid() {
305+ _apparmor->record();
306+
307+ // create a download, assert that it was
308+ // created and that the id and the path are correctly set
309+ QUuid id = QUuid::createUuid();
310+
311+ QVariantMap metadata;
312+ metadata["objectpath"] = id.toString();
313+
314+ Download* download = _downFactory->createDownload("",
315+ QList<GroupDownloadStruct>(), QCryptographicHash::Md5,
316+ true, metadata, QMap<QString, QString>());
317+
318+ QList<MethodData> calledMethods = _apparmor->calledMethods();
319+ QCOMPARE(1, calledMethods.count());
320+ StringWrapper* path = reinterpret_cast<StringWrapper*>(
321+ calledMethods[0].params().outParams()[1]);
322+ QCOMPARE(download->downloadId(), id);
323+ QCOMPARE(download->path(), path->value());
324+}
325+
326+void
327+TestDownloadFactory::testCreateGroupDownloadWithNullUuid() {
328+ _apparmor->record();
329+
330+ // create a download, assert that it was
331+ // created and that the id and the path are correctly set
332+ QVariantMap metadata;
333+ metadata["objectpath"] = "bad-id";
334+
335+ Download* download = _downFactory->createDownload("",
336+ QList<GroupDownloadStruct>(), QCryptographicHash::Md5,
337+ true, metadata, QMap<QString, QString>());
338+
339+ QList<MethodData> calledMethods = _apparmor->calledMethods();
340+ QCOMPARE(1, calledMethods.count());
341+ UuidWrapper* id = reinterpret_cast<UuidWrapper*>(
342+ calledMethods[0].params().outParams()[0]);
343+ StringWrapper* path = reinterpret_cast<StringWrapper*>(
344+ calledMethods[0].params().outParams()[1]);
345+ QCOMPARE(download->downloadId(), id->value());
346+ QCOMPARE(download->path(), path->value());
347+}
348
349=== modified file 'ubuntu-download-manager-tests/test_download_factory.h'
350--- ubuntu-download-manager-tests/test_download_factory.h 2013-09-10 15:36:23 +0000
351+++ ubuntu-download-manager-tests/test_download_factory.h 2013-09-10 15:36:23 +0000
352@@ -30,6 +30,7 @@
353
354 class TestDownloadFactory : public QObject {
355 Q_OBJECT
356+
357 public:
358 explicit TestDownloadFactory(QObject *parent = 0);
359
360@@ -41,6 +42,12 @@
361 void testCreateDownload();
362 void testCreateDownloadWithHash();
363 void testCreateGroupDownload();
364+ void testCreateDownloadWithValidUuid();
365+ void testCreateDownloadWithNullUuid();
366+ void testCreateDownloadWithHashAndUuid();
367+ void testCreateDownloadWithHashAndNullUuid();
368+ void testCreateGroupDownloadWithValidUuid();
369+ void testCreateGroupDownloadWithNullUuid();
370
371 private:
372 FakeAppArmor* _apparmor;

Subscribers

People subscribed via source and target branches