comparison sat/test/test_memory.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/test/test_memory.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.test import helpers
22 from twisted.trial import unittest
23 import traceback
24 from constants import Const
25 from xml.dom import minidom
26
27
28 class MemoryTest(unittest.TestCase):
29
30 def setUp(self):
31 self.host = helpers.FakeSAT()
32
33 def _getParamXML(self, param="1", security_level=None):
34 """Generate XML for testing parameters
35
36 @param param (str): a subset of "123"
37 @param security_level: security level of the parameters
38 @return (str)
39 """
40 def getParam(name):
41 return """
42 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/>
43 """ % {'param_name': name,
44 'param_label': _(name),
45 'security': '' if security_level is None else ('security="%d"' % security_level)
46 }
47 params = ''
48 if "1" in param:
49 params += getParam(Const.ENABLE_UNIBOX_PARAM)
50 if "2" in param:
51 params += getParam(Const.PARAM_IN_QUOTES)
52 if "3" in param:
53 params += getParam("Dummy param")
54 return """
55 <params>
56 <individual>
57 <category name="%(category_name)s" label="%(category_label)s">
58 %(params)s
59 </category>
60 </individual>
61 </params>
62 """ % {
63 'category_name': Const.COMPOSITION_KEY,
64 'category_label': _(Const.COMPOSITION_KEY),
65 'params': params
66 }
67
68 def _paramExists(self, param="1", src=None):
69 """
70
71 @param param (str): a character in "12"
72 @param src (DOM element): the top-level element to look in
73 @return: True is the param exists
74 """
75 if param == "1":
76 name = Const.ENABLE_UNIBOX_PARAM
77 else:
78 name = Const.PARAM_IN_QUOTES
79 category = Const.COMPOSITION_KEY
80 if src is None:
81 src = self.host.memory.params.dom.documentElement
82 for type_node in src.childNodes:
83 # when src comes self.host.memory.params.dom, we have here
84 # some "individual" or "general" elements, when it comes
85 # from Memory.getParams we have here a "params" elements
86 if type_node.nodeName not in ("individual", "general", "params"):
87 continue
88 for cat_node in type_node.childNodes:
89 if cat_node.nodeName != "category" or cat_node.getAttribute("name") != category:
90 continue
91 for param in cat_node.childNodes:
92 if param.nodeName == "param" and param.getAttribute("name") == name:
93 return True
94 return False
95
96 def assertParam_generic(self, param="1", src=None, exists=True, deferred=False):
97 """
98 @param param (str): a character in "12"
99 @param src (DOM element): the top-level element to look in
100 @param exists (boolean): True to assert the param exists, False to assert it doesn't
101 @param deferred (boolean): True if this method is called from a Deferred callback
102 """
103 msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n"
104 if deferred:
105 # in this stack we can see the line where the error came from,
106 # if limit=5, 6 is not enough you can increase the value
107 msg += "\n".join(traceback.format_stack(limit=5 if exists else 6))
108 assertion = self._paramExists(param, src)
109 getattr(self, "assert%s" % exists)(assertion, msg)
110
111 def assertParamExists(self, param="1", src=None):
112 self.assertParam_generic(param, src, True)
113
114 def assertParamNotExists(self, param="1", src=None):
115 self.assertParam_generic(param, src, False)
116
117 def assertParamExists_async(self, src, param="1"):
118 """@param src: a deferred result from Memory.getParams"""
119 self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), True, True)
120
121 def assertParamNotExists_async(self, src, param="1"):
122 """@param src: a deferred result from Memory.getParams"""
123 self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), False, True)
124
125 def _getParams(self, security_limit, app='', profile_key='@NONE@'):
126 """Get the parameters accessible with the given security limit and application name.
127
128 @param security_limit (int): the security limit
129 @param app (str): empty string or "libervia"
130 @param profile_key
131 """
132 if profile_key == '@NONE@':
133 profile_key = '@DEFAULT@'
134 return self.host.memory.params.getParams(security_limit, app, profile_key)
135
136 def test_updateParams(self):
137 self.host.memory.reinit()
138 # check if the update works
139 self.host.memory.updateParams(self._getParamXML())
140 self.assertParamExists()
141 previous = self.host.memory.params.dom.cloneNode(True)
142 # now check if it is really updated and not duplicated
143 self.host.memory.updateParams(self._getParamXML())
144 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8"))
145
146 self.host.memory.reinit()
147 # check successive updates (without intersection)
148 self.host.memory.updateParams(self._getParamXML('1'))
149 self.assertParamExists("1")
150 self.assertParamNotExists("2")
151 self.host.memory.updateParams(self._getParamXML('2'))
152 self.assertParamExists("1")
153 self.assertParamExists("2")
154
155 previous = self.host.memory.params.dom.cloneNode(True) # save for later
156
157 self.host.memory.reinit()
158 # check successive updates (with intersection)
159 self.host.memory.updateParams(self._getParamXML('1'))
160 self.assertParamExists("1")
161 self.assertParamNotExists("2")
162 self.host.memory.updateParams(self._getParamXML('12'))
163 self.assertParamExists("1")
164 self.assertParamExists("2")
165
166 # successive updates with or without intersection should have the same result
167 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8"))
168
169 self.host.memory.reinit()
170 # one update with two params in a new category
171 self.host.memory.updateParams(self._getParamXML('12'))
172 self.assertParamExists("1")
173 self.assertParamExists("2")
174
175 def test_getParams(self):
176 # tests with no security level on the parameter (most secure)
177 params = self._getParamXML()
178 self.host.memory.reinit()
179 self.host.memory.updateParams(params)
180 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
181 self._getParams(0).addCallback(self.assertParamNotExists_async)
182 self._getParams(1).addCallback(self.assertParamNotExists_async)
183 # tests with security level 0 on the parameter (not secure)
184 params = self._getParamXML(security_level=0)
185 self.host.memory.reinit()
186 self.host.memory.updateParams(params)
187 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
188 self._getParams(0).addCallback(self.assertParamExists_async)
189 self._getParams(1).addCallback(self.assertParamExists_async)
190 # tests with security level 1 on the parameter (more secure)
191 params = self._getParamXML(security_level=1)
192 self.host.memory.reinit()
193 self.host.memory.updateParams(params)
194 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
195 self._getParams(0).addCallback(self.assertParamNotExists_async)
196 return self._getParams(1).addCallback(self.assertParamExists_async)
197
198 def test_paramsRegisterApp(self):
199
200 def register(xml, security_limit, app):
201 """
202 @param xml: XML definition of the parameters to be added
203 @param security_limit: -1 means no security, 0 is the maximum security then the higher the less secure
204 @param app: name of the frontend registering the parameters
205 """
206 helpers.muteLogging()
207 self.host.memory.paramsRegisterApp(xml, security_limit, app)
208 helpers.unmuteLogging()
209
210 # tests with no security level on the parameter (most secure)
211 params = self._getParamXML()
212 self.host.memory.reinit()
213 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
214 self.assertParamExists()
215 self.host.memory.reinit()
216 register(params, 0, Const.APP_NAME)
217 self.assertParamNotExists()
218 self.host.memory.reinit()
219 register(params, 1, Const.APP_NAME)
220 self.assertParamNotExists()
221
222 # tests with security level 0 on the parameter (not secure)
223 params = self._getParamXML(security_level=0)
224 self.host.memory.reinit()
225 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
226 self.assertParamExists()
227 self.host.memory.reinit()
228 register(params, 0, Const.APP_NAME)
229 self.assertParamExists()
230 self.host.memory.reinit()
231 register(params, 1, Const.APP_NAME)
232 self.assertParamExists()
233
234 # tests with security level 1 on the parameter (more secure)
235 params = self._getParamXML(security_level=1)
236 self.host.memory.reinit()
237 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
238 self.assertParamExists()
239 self.host.memory.reinit()
240 register(params, 0, Const.APP_NAME)
241 self.assertParamNotExists()
242 self.host.memory.reinit()
243 register(params, 1, Const.APP_NAME)
244 self.assertParamExists()
245
246 # tests with security level 1 and several parameters being registered
247 params = self._getParamXML("12", security_level=1)
248 self.host.memory.reinit()
249 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
250 self.assertParamExists()
251 self.assertParamExists("2")
252 self.host.memory.reinit()
253 register(params, 0, Const.APP_NAME)
254 self.assertParamNotExists()
255 self.assertParamNotExists("2")
256 self.host.memory.reinit()
257 register(params, 1, Const.APP_NAME)
258 self.assertParamExists()
259 self.assertParamExists("2")
260
261 # tests with several parameters being registered in an existing category
262 self.host.memory.reinit()
263 self.host.memory.updateParams(self._getParamXML("3"))
264 register(self._getParamXML("12"), Const.NO_SECURITY_LIMIT, Const.APP_NAME)
265 self.assertParamExists()
266 self.assertParamExists("2")
267 self.host.memory.reinit()
268
269 def test_paramsRegisterApp_getParams(self):
270 # test retrieving the parameter for a specific frontend
271 self.host.memory.reinit()
272 params = self._getParamXML(security_level=1)
273 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
274 self._getParams(1, '').addCallback(self.assertParamExists_async)
275 self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async)
276 self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async)
277
278 # the same with several parameters registered at the same time
279 self.host.memory.reinit()
280 params = self._getParamXML('12', security_level=0)
281 self.host.memory.paramsRegisterApp(params, 5, Const.APP_NAME)
282 self._getParams(5, '').addCallback(self.assertParamExists_async)
283 self._getParams(5, '').addCallback(self.assertParamExists_async, "2")
284 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async)
285 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async, "2")
286 self._getParams(5, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async)
287 return self._getParams(5, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async, "2")