forked from mozilla/test-amo-validator
/
test_validator.py
246 lines (185 loc) · 8.25 KB
/
test_validator.py
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
import json
import os
import sys
import unittest
from cStringIO import StringIO
from nose.tools import eq_
from validator.validate import validate
FIREFOX_GUID = '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}'
def _validator(file_path, compatibility=None):
# TODO(Kumar) This is currently copied from Zamboni because
# it's really hard to import from zamboni outside of itself.
# TODO(Kumar) remove this when validator is fixed, see bug 620503
from validator.testcases import scripting
import validator.constants
js = os.environ.get('SPIDERMONKEY_INSTALLATION', 'js')
scripting.SPIDERMONKEY_INSTALLATION = js
validator.constants.SPIDERMONKEY_INSTALLATION = js
apps = os.path.join(os.path.dirname(__file__), 'apps.json')
orig = sys.stderr
sys.stderr = StringIO()
try:
result = validate(file_path, format='json',
# Test all tiers at once. This will make sure we see
# all error messages.
determined=True,
approved_applications=apps,
spidermonkey=js,
# Commented out because we want to let the tests
# choose whether to run or not. This step is
# unnecessary.
#for_appversions=compatibility,
overrides={"targetapp_maxVersion": compatibility or {}})
sys.stdout.write(sys.stderr.getvalue())
if 'Traceback' in sys.stderr.getvalue():
# the validator catches and ignores certain errors in an attempt
# to remain versatile. There should not be any exceptions
# while testing.
raise RuntimeError(
"An exception was raised during validation. Check stderr")
finally:
sys.stderr = orig
return result
_cached_validation = {}
class ValidatorTest(unittest.TestCase):
def setUp(self):
self.validation = None
self.messages = None
self.ids = None
def msg_set(self, d):
return sorted(set([m['message'] for m in d['messages']]))
def id_set(self, d):
return sorted(set([str(m['id']) for m in d['messages']]))
def validate(self, xpi, compatibility=None):
self.validation = self._run_validation(xpi, compatibility)
self.messages = self.msg_set(self.validation)
self.ids = self.id_set(self.validation)
return self.validation
def _run_validation(self, xpi, compatibility=None):
path = os.path.join(os.path.dirname(__file__), 'addons', xpi)
if path in _cached_validation:
return _cached_validation[path]
v = json.loads(_validator(path, compatibility))
_cached_validation[path] = v
return v
def assertPartialMsg(self, partial_msg):
found = False
for m in self.messages:
if m.startswith(partial_msg):
found = True
assert found, ('Unexpected: %r' % self.messages)
def expectMsg(self, msg):
assert msg in self.messages, (
'Expected %r but only got %r' % (msg, self.messages))
def expectId(self, id):
assert id in self.ids, (
'Expected %r but only got %r' % (id, self.ids))
class JavaScriptTests(ValidatorTest):
def test_createelement__used(self):
self.validate('glee-20101227219.xpi')
self.assertPartialMsg('createElement() used to create script tag')
def test_dangerous_global(self):
self.validate('feedly-addon-201101111013.xpi')
self.expectMsg(u'Dangerous Global Object')
def test_global_called(self):
self.validate('babuji-20110124355.xpi')
self.expectMsg(u'Global called in dangerous manner')
def test_potentially_malicious(self):
self.validate('add-on201101101027.xpi')
self.expectMsg(u'Potentially unsafe JS in use.')
def test_variable_element(self):
self.validate('glee-20101227219.xpi')
self.expectMsg(u'Variable element type being created')
class GeneralTests(ValidatorTest):
def test_contains_jar_files(self):
self.validate('test-theme-3004.jar')
self.expectMsg(u'Add-on contains JAR files, no <em:unpack>')
def test_potentially_illegal_name(self):
self.validate('add-on20110110322.xpi')
self.expectMsg(u'Add-on has potentially illegal name.')
def test_banned_element(self):
self.validate('gabbielsan_tools-1.01-ff.xpi')
self.expectMsg(u'Banned element in install.rdf')
def test_blacklisted_file(self):
self.validate('babuji-20110124355.xpi')
self.expectMsg(u'Flagged file extension found')
def test_blacklisted_file_2(self):
self.validate('peerscape-3.1.5-fx.xpi')
self.expectMsg(u'Flagged file type found')
def test_em_type_not(self):
self.validate('babuji-20110124355.xpi')
self.expectMsg(u'No <em:type> element found in install.rdf')
def test_obsolete_element(self):
self.validate('gabbielsan_tools-1.01-ff.xpi')
self.expectMsg(u'Banned element in install.rdf')
def test_unknown_file(self):
self.validate('gabbielsan_tools-1.01-ff.xpi')
self.expectMsg(u'Unrecognized element in install.rdf')
def test_unrecognized_element(self):
self.validate('littlemonkey-1.8.56-sm.xpi')
self.expectMsg(u'Add-on missing install.rdf.')
def test_invalid_id(self):
self.validate('add-ongoogle-201101121132.xpi')
self.expectMsg(u'The value of <em:id> is invalid.')
def test_xpi_cannot(self):
self.validate('lavafox_test-theme-20101130538.xpi')
self.expectMsg(u'The XPI could not be opened.')
def test_invalid_version(self):
self.validate('invalid maximum version number.xpi')
self.expectMsg(u'Invalid maximum version number')
def test_non_ascii_html_markup(self):
# should be no Unicode errors
self.validate('non-ascii-html.xpi')
class LocalizationTests(ValidatorTest):
def test_translation(self):
self.validate('babuji-20110124355.xpi')
self.expectMsg(u'Unchanged translation entities')
def test_encodings(self):
self.validate('babuji-20110124355.xpi')
self.expectMsg(u'Unexpected encodings in locale files')
def test_missing_translation(self):
self.validate('download_statusbar-0.9.7.2-fx (1).xpi')
self.expectMsg(u'Missing translation entity')
class SecurityTests(ValidatorTest):
def test_missing_comments(self):
self.validate('add-on-20110113408.xpi')
self.expectMsg(u'Missing comments in <script> tag')
def test_typeless_iframes_browsers(self):
self.validate('add-on201101081038.xpi')
self.expectMsg(u'Typeless iframes/browsers must be local.')
def test_binary_files(self):
self.validate('cooliris-1.12.2.44172-fx-mac.xpi.xpi',
compatibility={FIREFOX_GUID: "5.0a2"})
self.expectMsg(u"Flagged file extension found")
self.expectMsg(u"Flagged file type found")
self.expectId("[u'testcases_packagelayout',"
" u'test_compatibility_binary',"
" u'disallowed_extension']")
class NoErrorsExpected(ValidatorTest):
def test_an_attempt(self):
d = self.validate('tmp.xpi')
eq_(d['errors'], 0)
def test_don_t_freak(self):
d = self.validate('test (1).xpi')
eq_(d['errors'], 0)
def test_don_t_freak_2(self):
d = self.validate('littlemonkey-1.8.56-sm.xpi')
msg = self.msg_set(d)
ok = True
for m in msg:
if 'install.js' in msg:
ok = False
assert ok, ('Unexpected: %r' % msg)
def test_unknown_file(self):
d = self.validate('add-on20101228444 (1).jar')
eq_(d['errors'], 0)
class SearchTools(ValidatorTest):
def test_opensearch_providers(self):
self.validate('sms_search-20110115 .xml')
self.expectMsg(u'OpenSearch: <Url> elements may not be rel=self')
def test_opensearch_shortname(self):
self.validate('lexisone_citation_search-20100116 .xml')
self.expectMsg(u'OpenSearch: <ShortName> element too long')
def test_too_many(self):
self.validate('addon-12201-latest.xml')
self.expectMsg(u'OpenSearch: Too many <ShortName> elements')