summaryrefslogtreecommitdiff
path: root/test/dbus/blacklist-test.py
blob: 5a216d2ace26a0b35f328276ef57c7dcfb6ec5ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#! /usr/bin/env python3
# -.- coding: utf-8 -.-

# remote-test.py
#
# Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
# Copyright © 2010 Markus Korn <thekorn@gmx.de>
# Copyright © 2010 Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
# Copyright © 2011 Manish Sinha <manishsinha@ubuntu.com>
# Copyright © 2011-2012 Collabora Ltd.
#             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Update python path to use local zeitgeist module
import sys
import os
import unittest
import gobject

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from zeitgeist.client import ZeitgeistDBusInterface
from zeitgeist.datamodel import *
from testutils import RemoteTestCase, asyncTestMethod

class BlacklistTest(RemoteTestCase):

	def __init__(self, methodName):
		super(BlacklistTest, self).__init__(methodName)
		self.blacklist = None

	def setUp(self):
		# lazy import to get a chance to use the private bus
		import dbus
		
		# We set up the connection lazily in order to wait for the
		# engine to come up
		super(BlacklistTest, self).setUp()
		obj = dbus.SessionBus().get_object("org.gnome.zeitgeist.Engine",
			"/org/gnome/zeitgeist/blacklist")
		self.blacklist = dbus.Interface(obj, "org.gnome.zeitgeist.Blacklist")

	def testClear(self):
		# Insert a blacklist template
		self.blacklist.AddTemplate("unicorns",
			Event.new_for_values(subject_uri="a"))
		self.assertTrue(len(self.blacklist.GetTemplates()) > 0)
		
		# Now remove all existing templates...
		allTemplates = self.blacklist.GetTemplates()
		[self.blacklist.RemoveTemplate(key) for key in list(allTemplates.keys())]
		
		# And ensure that they are indeed gone.
		self.assertEqual(len(self.blacklist.GetTemplates()), 0)

	def _add_template(self, name, template):
		self.blacklist.AddTemplate(name, template)

		res = self.blacklist.GetTemplates()
		self.assertEventsEqual(template, Event(res[name]))

	def _assert_template_count(self, num):
		self.assertEqual(len(self.blacklist.GetTemplates()), num)

	def _assert_insert_blocked(self, *events):
		inserted_ids = list(map(int, self.insertEventsAndWait(events)))
		zeros = [x for x in inserted_ids if x == 0]
		self.assertEqual(len(events), len(inserted_ids))
		self.assertEqual(len(events), len(zeros))
	
	def _assert_insert_allowed(self, *events):
		inserted_ids = list(map(int, self.insertEventsAndWait(events)))
		self.assertEqual(len(events), len(inserted_ids))
		self.assertEqual([], [x for x in inserted_ids if x == 0])

	def testSetOne(self):
		orig = Event.new_for_values(
			interpretation=Interpretation.ACCESS_EVENT,
			subject_uri="http://nothingtoseehere.gov")

		self._add_template("Foobar", orig)
		self._assert_template_count(1)

	def testApplyBlacklist(self):
		ev = Event.new_for_values(
			interpretation=Interpretation.ACCESS_EVENT,
			manifestation=Manifestation.USER_ACTIVITY,
			actor="app://foo.desktop",
			subject_uri="http://nothingtoseehere.gov")
		self._add_template("Foobar", ev)
		self._assert_template_count(1)

		self._assert_insert_blocked(ev)

		# Now change the event to pass the blacklist
		ev.get_subjects()[0].uri = "http://totallyvaliduri.com"
		self._assert_insert_allowed(ev)

	def testApplyBlacklistWithTwoTemplates(self):
		# Setup an event we'll use to test insertions
		event = Event.new_for_values(
			timestamp = 0,
			interpretation=Interpretation.ACCESS_EVENT,
			manifestation=Manifestation.SCHEDULED_ACTIVITY,
            actor="actor",
			subject_uri="blarg")

		# With no blacklisted templates we can insert it without problems
		self._assert_insert_allowed(event)

		# We blacklist the event's interpretation
		templ1 = Event.new_for_values(
			interpretation=Interpretation.ACCESS_EVENT)
		self._add_template("One", templ1)
		self._assert_template_count(1)

		# Now it can't be inserted anymore
		event.timestamp = 1
		self._assert_insert_blocked(event)

		# We blacklist the event's URI
		templ2 = Event.new_for_values(
			subject_uri="blarg")
		self._add_template("Two", templ1)
		self._assert_template_count(2)

		# No way it can be logged now
		event.timestamp = 2 # change timestamp so it isn't a duplicate event
		self._assert_insert_blocked(event)

		# We remove the first blacklisted template, it still can't get logged
		self.blacklist.RemoveTemplate("One")
		self._assert_template_count(1)
		event.timestamp = 3
		self._assert_insert_blocked(event)

		# Removing the second template, now it'll let us insert it
		self.blacklist.RemoveTemplate("Two")
		self._assert_template_count(0)
		event.timestamp = 4
		self._assert_insert_allowed(event)

		# Finally, we blacklist a template that doesn't match the event
		templ3 = Event.new_for_values(
			interpretation=Interpretation.ACCESS_EVENT,
			manifestation=Manifestation.USER_ACTIVITY)
		self._add_template("One", templ3) # reuse the template identifier, why not?
		self._assert_template_count(1)

		# And of course we can still insert it :)
		event.timestamp = 5
		self._assert_insert_allowed(event)

	def testApplyBlacklistWithSpacesInURI(self):
		# We blacklist a particular URI
		templ1 = Event.new_for_values(subject_uri="New York is a city")
		self._add_template("One", templ1)
		self._assert_template_count(1)

		# And check that it works
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a",	manifestation="b", actor="c",
			subject_uri="New York is a city"))
		self._assert_insert_allowed(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="New York is a city NOT"))
		self._assert_insert_allowed(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="Do you like cheese?"))
		self._assert_insert_allowed(Event.new_for_values(
			interpretation=Interpretation.MOVE_EVENT,
			manifestation="b", actor="c",
			subject_uri="kung fu",
			subject_current_uri="New York is a city"))

	def testApplyBlacklistWithAccentsInURI(self):
		# We blacklist some particular URIs
		self._add_template("weirdo", Event.new_for_values(
			subject_uri="çàrßá€"))
		self._add_template("normalo", Event.new_for_values(
			subject_uri="hello"))
		self._assert_template_count(2)

		# And check that the blacklisting works
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="çàrßá€"))
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="hello"))
		self._assert_insert_allowed(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="hola"))
		self._assert_insert_allowed(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="çàrßá"))

	def testApplyBlacklistForEventWithEmptyCurrentURI(self):
		# We blacklist some particular current URI
		self._add_template("t", Event.new_for_values(subject_current_uri="t"))
		self._assert_template_count(1)

		# Blocking the current_uri works
		self._assert_insert_blocked(Event.new_for_values(
			interpretation=Interpretation.MOVE_EVENT,
			manifestation="manifestation",
			actor="actor",
			subject_uri="unrelated",
			subject_current_uri="t"))

		# But if we only set uri (and leave it up to Zeitgeist to set current_uri
		# to the same value?
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="t"))

	def testApplyBlacklistWithWildcardInURI(self):
		# We blacklist some particular URIs
		self._add_template("wild", Event.new_for_values(
			subject_uri="block me*"))
		self._assert_template_count(1)

		# And check that the blacklisting works
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="block me"))
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="block me*"))
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="block me now"))
		self._assert_insert_blocked(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="block meß :)"))
		self._assert_insert_allowed(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="block mNOT"))
		self._assert_insert_allowed(Event.new_for_values(
			interpretation="a", manifestation="b", actor="c",
			subject_uri="nblock me"))

	def _get_blacklist_iface(self):
		"""
		Create a blacklist interface using the get_extension() method
		from client.py.
		"""
		del self.blacklist
		iface = ZeitgeistDBusInterface()
		blacklist = iface.get_extension("Blacklist", "blacklist")
		return blacklist

	def testBlacklistUsingClientDBusInterface(self):
		"""
		Ensure that get_extension() from client.py method works correctly.
		"""
		blacklist = self._get_blacklist_iface()
		allTemplates = blacklist.GetTemplates()
		[blacklist.RemoveTemplate(key) for key in list(allTemplates.keys())]
		newAllTemplates = blacklist.GetTemplates()
		self.assertEqual(len(newAllTemplates), 0)

	def testBlacklistSignals(self, mainloop=None, connect_signals=True):
		self.blacklist = self._get_blacklist_iface()
		if mainloop is None:
			mainloop = self.create_mainloop()

		template1 = Event.new_for_values(
			timestamp=1349453012265,
			interpretation=Interpretation.ACCESS_EVENT,
			subject_uri="http://nothingtoseehere.gov")

		global hit
		hit = 0

		@asyncTestMethod(mainloop)
		def cb_added(template_id, event_template):
			global hit
			self.assertEqual(hit, 0)
			hit = 1
			self.assertEqual(template_id, "TestTemplate")
			self.assertEventsEqual(template1, event_template)

		@asyncTestMethod(mainloop)
		def cb_removed(template_id, event_template):
			global hit
			self.assertEqual(hit, 1)
			hit = 2
			self.assertEqual(template_id, "TestTemplate")
			self.assertEventsEqual(template1, event_template)
			mainloop.quit()

		# Connect to signals
		if connect_signals:
			self.blacklist.connect('TemplateAdded', cb_added)
			self.blacklist.connect('TemplateRemoved', cb_removed)

		def launch_tests():
			self.blacklist.AddTemplate("TestTemplate", template1)
			self.blacklist.RemoveTemplate("TestTemplate")
		gobject.idle_add(launch_tests)

		mainloop.run()

	def testBlacklistSignalWithReconnection(self):
		mainloop = self.create_mainloop()
		self.testBlacklistSignals(mainloop)

		# Restart the Zeitgeist daemon...
		self.kill_daemon()
		self.spawn_daemon()

		# ... and try again without re-connecting to the signals
		self.testBlacklistSignals(mainloop, connect_signals=False)

if __name__ == "__main__":
	unittest.main()

# vim:noexpandtab:ts=4:sw=4