Merge lp:~mardy/ubuntu-system-settings/rtm-lp1387734 into lp:ubuntu-system-settings/rtm-14.09

Proposed by Alberto Mardegan
Status: Merged
Approved by: Sebastien Bacher
Approved revision: 975
Merged at revision: 975
Proposed branch: lp:~mardy/ubuntu-system-settings/rtm-lp1387734
Merge into: lp:ubuntu-system-settings/rtm-14.09
Diff against target: 170 lines (+72/-20)
2 files modified
plugins/security-privacy/trust-store-model.cpp (+19/-8)
tests/plugins/security-privacy/tst_trust_store_model.cpp (+53/-12)
To merge this branch: bzr merge lp:~mardy/ubuntu-system-settings/rtm-lp1387734
Reviewer Review Type Date Requested Status
Sebastien Bacher (community) Approve
David Barth (community) Approve
Review via email: mp+247709@code.launchpad.net

Commit message

Security: ignore older requests when building the applications' grants

Also update the unit tests to ensure that we'll compute the same results no matter what the order of the rows is.

Description of the change

Security: ignore older requests when building the applications' grants

Also update the unit tests to ensure that we'll compute the same results no matter what the order of the rows is.

To post a comment you must log in.
Revision history for this message
David Barth (dbarth) wrote :

Same as the vivid branch which was tested to work fine

review: Approve
Revision history for this message
Sebastien Bacher (seb128) wrote :

thanks

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/security-privacy/trust-store-model.cpp'
2--- plugins/security-privacy/trust-store-model.cpp 2014-09-29 11:02:13 +0000
3+++ plugins/security-privacy/trust-store-model.cpp 2015-01-27 13:47:59 +0000
4@@ -35,6 +35,11 @@
5 class Application
6 {
7 public:
8+ struct GrantData {
9+ bool granted{false};
10+ std::chrono::system_clock::time_point timestamp; // initialized with the epoch
11+ };
12+
13 Application() {}
14
15 void setId(const QString &id) {
16@@ -125,19 +130,25 @@
17 }
18
19 void addRequest(const core::trust::Request &request) {
20- if (request.answer == core::trust::Request::Answer::granted) {
21- grantedFeatures.insert(request.feature.value);
22- } else {
23- grantedFeatures.remove(request.feature.value);
24+ GrantData &data = grantedFeatures[request.feature.value];
25+ /* Ignore older requests */
26+ if (request.when <= data.timestamp) return;
27+
28+ data.granted = (request.answer == core::trust::Request::Answer::granted);
29+ data.timestamp = request.when;
30+ }
31+
32+ bool hasGrants() const {
33+ Q_FOREACH(const GrantData &data, grantedFeatures) {
34+ if (data.granted) return true;
35 }
36+ return false;
37 }
38
39- bool hasGrants() const { return !grantedFeatures.isEmpty(); }
40-
41 QString id;
42 QString displayName;
43 QString iconName;
44- QSet<std::uint64_t> grantedFeatures;
45+ QHash<std::uint64_t,GrantData> grantedFeatures;
46 };
47
48 class TrustStoreModelPrivate: public QObject
49@@ -355,7 +366,7 @@
50
51 /* When disabling, we must disable all the features */
52 if (!enabled) {
53- Q_FOREACH(std::int64_t feature, app.grantedFeatures) {
54+ Q_FOREACH(std::int64_t feature, app.grantedFeatures.keys()) {
55 /* Skip the default feature, we already disabled it */
56 if (feature == core::trust::Request::default_feature) continue;
57
58
59=== modified file 'tests/plugins/security-privacy/tst_trust_store_model.cpp'
60--- tests/plugins/security-privacy/tst_trust_store_model.cpp 2014-08-11 09:38:00 +0000
61+++ tests/plugins/security-privacy/tst_trust_store_model.cpp 2015-01-27 13:47:59 +0000
62@@ -32,11 +32,20 @@
63 {
64 struct Store: public core::trust::Store
65 {
66+ enum SortOrder {
67+ Unsorted = 0,
68+ TimeAsc,
69+ TimeDesc,
70+ LastSortOrder
71+ };
72+
73 std::string m_name;
74 std::list<core::trust::Request> m_allRequests;
75+ SortOrder m_sortOrder;
76 static std::shared_ptr<mock::Store> m_instance;
77
78- Store()
79+ Store():
80+ m_sortOrder(Unsorted)
81 {
82 }
83
84@@ -48,6 +57,22 @@
85 m_instance = store;
86 }
87
88+ void setSortOrder(SortOrder order) {
89+ m_sortOrder = order;
90+ }
91+
92+ static bool requestCompareTimeAsc(const core::trust::Request &left,
93+ const core::trust::Request &right)
94+ {
95+ return left.when < right.when;
96+ }
97+
98+ static bool requestCompareTimeDesc(const core::trust::Request &left,
99+ const core::trust::Request &right)
100+ {
101+ return left.when > right.when;
102+ }
103+
104 struct Query: public core::trust::Store::Query
105 {
106 mock::Store *m_store;
107@@ -98,6 +123,12 @@
108 }
109 }
110 }
111+ // sort the results
112+ if (m_store->m_sortOrder != Unsorted) {
113+ m_requests.sort(m_store->m_sortOrder == TimeAsc ?
114+ requestCompareTimeAsc : requestCompareTimeDesc);
115+ }
116+
117 m_it = m_requests.begin();
118 m_status = (m_it == m_requests.end()) ?
119 core::trust::Store::Query::Status::eor :
120@@ -263,13 +294,17 @@
121 QFETCH(QStringList, expectedAppIds);
122 QFETCH(QList<bool>, expectedAppGrants);
123
124+ std::chrono::system_clock::time_point requestTime =
125+ std::chrono::system_clock::now();
126+
127 for (int i = 0; i < appIds.count(); i++) {
128 core::trust::Request r;
129 r.from = appIds[i].toStdString();
130 r.feature = core::trust::Feature(core::trust::Request::default_feature);
131 r.answer = appGrants[i] ?
132 core::trust::Request::Answer::granted : core::trust::Request::Answer::denied;
133- r.when = std::chrono::system_clock::now();
134+ // make sure all times are monotonically increasing
135+ r.when = requestTime + std::chrono::duration<int>(i);
136 m_store->add(r);
137 }
138
139@@ -280,16 +315,22 @@
140 " serviceName: \"storeTest\"\n"
141 "}",
142 QUrl());
143- QObject *object = component.create();
144- QVERIFY(object != 0);
145- QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
146- QVERIFY(model != 0);
147-
148- QCOMPARE(model->rowCount(), expectedAppIds.count());
149-
150- for (int i = 0; i < model->rowCount(); i++) {
151- QCOMPARE(get(model, i, "applicationId").toString(), expectedAppIds[i]);
152- QCOMPARE(get(model, i, "granted").toBool(), expectedAppGrants[i]);
153+ // Test unsorted, sorted asc and sorted desc
154+ for (int i_order = 0; i_order < mock::Store::LastSortOrder; i_order++) {
155+ m_store->setSortOrder(mock::Store::SortOrder(i_order));
156+ QObject *object = component.create();
157+ QVERIFY(object != 0);
158+ QAbstractListModel *model = qobject_cast<QAbstractListModel*>(object);
159+ QVERIFY(model != 0);
160+
161+ QCOMPARE(model->rowCount(), expectedAppIds.count());
162+
163+ for (int i = 0; i < model->rowCount(); i++) {
164+ QCOMPARE(get(model, i, "applicationId").toString(), expectedAppIds[i]);
165+ QCOMPARE(get(model, i, "granted").toBool(), expectedAppGrants[i]);
166+ }
167+
168+ delete object;
169 }
170 }
171

Subscribers

People subscribed via source and target branches