2014-10-21 18:08:11 +04:00
|
|
|
import hashlib
|
2012-08-06 22:30:31 +04:00
|
|
|
from mock import patch
|
2014-10-28 16:57:58 +03:00
|
|
|
from nose.tools import eq_, ok_
|
2011-08-05 00:55:55 +04:00
|
|
|
|
2012-08-06 22:30:31 +04:00
|
|
|
from helper import MockXPI, TestCase
|
2011-04-08 08:49:39 +04:00
|
|
|
|
2012-08-06 22:30:31 +04:00
|
|
|
from appvalidator.zip import ZipPackage
|
2012-08-02 02:43:36 +04:00
|
|
|
import appvalidator.testcases.content as content
|
|
|
|
from appvalidator.constants import *
|
2011-01-29 04:31:44 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
|
|
|
|
class MockTestEndpoint(object):
|
2011-07-19 02:31:58 +04:00
|
|
|
"""
|
|
|
|
Simulates a test module and reports whether individual tests have been
|
|
|
|
attempted on it.
|
|
|
|
"""
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2011-01-31 22:04:15 +03:00
|
|
|
def __init__(self, expected, td_error=False):
|
2010-06-24 20:26:19 +04:00
|
|
|
expectations = {}
|
|
|
|
for expectation in expected:
|
2012-08-06 22:30:31 +04:00
|
|
|
expectations[expectation] = {"count": 0, "subpackage": 0}
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
self.expectations = expectations
|
2011-01-31 22:04:15 +03:00
|
|
|
self.td_error = td_error
|
2011-02-12 03:09:15 +03:00
|
|
|
self.found_tiers = []
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2012-08-06 22:30:31 +04:00
|
|
|
def _tier_test(self, err, package, name):
|
2011-02-12 03:09:15 +03:00
|
|
|
"A simulated test case for tier errors"
|
|
|
|
print "Generating subpackage tier error..."
|
|
|
|
self.found_tiers.append(err.tier)
|
|
|
|
err.error(("foo", ),
|
|
|
|
"Tier error",
|
|
|
|
"Just a test")
|
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
def __getattribute__(self, name):
|
2011-03-08 23:02:05 +03:00
|
|
|
"""Detects requests for validation tests and returns an
|
2010-06-24 20:26:19 +04:00
|
|
|
object that simulates the outcome of a test."""
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
print "Requested: %s" % name
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2011-01-31 22:04:15 +03:00
|
|
|
if name == "test_package" and self.td_error:
|
2011-02-12 03:09:15 +03:00
|
|
|
return self._tier_test
|
2011-01-31 22:04:15 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
if name in ("expectations",
|
2011-01-31 22:04:15 +03:00
|
|
|
"assert_expectation",
|
2011-02-12 03:09:15 +03:00
|
|
|
"td_error",
|
|
|
|
"_tier_test",
|
|
|
|
"found_tiers"):
|
2010-06-24 20:26:19 +04:00
|
|
|
return object.__getattribute__(self, name)
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
if name in self.expectations:
|
|
|
|
self.expectations[name]["count"] += 1
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
if name == "test_package":
|
2012-08-06 22:30:31 +04:00
|
|
|
def wrap(package, name):
|
2010-06-24 20:26:19 +04:00
|
|
|
pass
|
2012-08-06 22:30:31 +04:00
|
|
|
elif name in ("test_css_file", "test_js_file", "process"):
|
2011-05-24 02:12:14 +04:00
|
|
|
def wrap(err, name, file_data):
|
|
|
|
pass
|
2010-06-24 20:26:19 +04:00
|
|
|
else:
|
2011-05-24 02:12:14 +04:00
|
|
|
def wrap(err, pak):
|
2012-08-06 22:30:31 +04:00
|
|
|
if isinstance(pak, ZipPackage) and pak.subpackage:
|
2010-06-24 20:26:19 +04:00
|
|
|
self.expectations[name]["subpackage"] += 1
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
return wrap
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
def assert_expectation(self, name, count, type_="count"):
|
|
|
|
"""Asserts that a particular test has been run a certain number
|
|
|
|
of times"""
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
print self.expectations
|
|
|
|
assert name in self.expectations
|
2012-08-06 22:30:31 +04:00
|
|
|
eq_(self.expectations[name][type_], count)
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
|
|
|
|
class MockMarkupEndpoint(MockTestEndpoint):
|
|
|
|
"Simulates the markup test module"
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
def __getattribute__(self, name):
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
if name == "MarkupParser":
|
|
|
|
return lambda x: self
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2010-06-24 20:26:19 +04:00
|
|
|
return MockTestEndpoint.__getattribute__(self, name)
|
2011-03-08 23:02:05 +03:00
|
|
|
|
2014-10-21 18:08:11 +04:00
|
|
|
|
2012-08-06 22:30:31 +04:00
|
|
|
class TestContent(TestCase):
|
|
|
|
|
|
|
|
def _run_test(self, mock_package):
|
|
|
|
return content.test_packed_packages(self.err, mock_package)
|
|
|
|
|
|
|
|
@patch("appvalidator.testcases.content.testendpoint_markup",
|
|
|
|
MockMarkupEndpoint(("process", )))
|
|
|
|
def test_markup(self):
|
|
|
|
"Tests markup files in the content validator."
|
|
|
|
self.setup_err()
|
|
|
|
mock_package = MockXPI({"foo.xml": "tests/resources/content/junk.xpi"})
|
|
|
|
|
|
|
|
eq_(self._run_test(mock_package), 1)
|
|
|
|
content.testendpoint_markup.assert_expectation("process", 1)
|
|
|
|
content.testendpoint_markup.assert_expectation(
|
|
|
|
"process", 0, "subpackage")
|
|
|
|
|
|
|
|
@patch("appvalidator.testcases.content.testendpoint_css",
|
|
|
|
MockTestEndpoint(("test_css_file", )))
|
|
|
|
def test_css(self):
|
|
|
|
"Tests css files in the content validator."
|
|
|
|
|
|
|
|
self.setup_err()
|
|
|
|
mock_package = MockXPI(
|
|
|
|
{"foo.css": "tests/resources/content/junk.xpi"})
|
|
|
|
|
|
|
|
eq_(self._run_test(mock_package), 1)
|
|
|
|
content.testendpoint_css.assert_expectation("test_css_file", 1)
|
|
|
|
content.testendpoint_css.assert_expectation(
|
|
|
|
"test_css_file", 0, "subpackage")
|
|
|
|
|
|
|
|
@patch("appvalidator.testcases.content.testendpoint_js",
|
|
|
|
MockTestEndpoint(("test_js_file", )))
|
|
|
|
def test_js(self):
|
|
|
|
"""Test that JS files are properly tested in the content validator."""
|
|
|
|
|
|
|
|
self.setup_err()
|
|
|
|
mock_package = MockXPI(
|
|
|
|
{"foo.js": "tests/resources/content/junk.xpi"})
|
|
|
|
|
|
|
|
eq_(self._run_test(mock_package), 1)
|
|
|
|
content.testendpoint_js.assert_expectation("test_js_file", 1)
|
|
|
|
content.testendpoint_js.assert_expectation(
|
|
|
|
"test_js_file", 0, "subpackage")
|
|
|
|
|
|
|
|
def test_hidden_files(self):
|
|
|
|
"""Tests that hidden files are reported."""
|
|
|
|
|
|
|
|
def test_structure(structure):
|
|
|
|
self.setup_err()
|
|
|
|
mock_package = MockXPI(
|
|
|
|
dict([(structure, "tests/resources/content/junk.xpi")]))
|
|
|
|
content.test_packed_packages(self.err, mock_package)
|
2013-09-27 06:38:03 +04:00
|
|
|
print structure
|
2012-08-06 22:30:31 +04:00
|
|
|
print self.err.print_summary(verbose=True)
|
|
|
|
self.assert_failed()
|
|
|
|
|
|
|
|
for structure in (".hidden", "dir/__MACOSX/foo", "dir/.foo.swp",
|
|
|
|
"dir/file.old", "dir/file.xul~"):
|
|
|
|
yield test_structure, structure
|
2013-09-27 06:38:03 +04:00
|
|
|
|
|
|
|
def test_too_much_garbage(self):
|
|
|
|
"""Tests that hidden files are reported."""
|
|
|
|
self.setup_err()
|
|
|
|
mock_package = MockXPI(
|
|
|
|
{".junky": "tests/resources/content/junk.xpi"},
|
|
|
|
default_size=50 * 1024)
|
|
|
|
|
|
|
|
content.test_packed_packages(self.err, mock_package)
|
|
|
|
self.assert_failed(with_warnings=True)
|
|
|
|
|
|
|
|
mock_package = MockXPI(
|
|
|
|
{".junky": "tests/resources/content/junk.xpi",
|
|
|
|
".morejunk": "tests/resources/content/junk.xpi",},
|
|
|
|
default_size=50 * 1024)
|
|
|
|
|
|
|
|
content.test_packed_packages(self.err, mock_package)
|
|
|
|
self.assert_failed(with_warnings=True, with_errors=True)
|
2013-11-02 01:11:39 +04:00
|
|
|
|
2014-10-21 18:08:11 +04:00
|
|
|
def test_whitelist(self):
|
|
|
|
"""Test that whitelisted files are properly skipped tested by the
|
|
|
|
content validator."""
|
|
|
|
|
|
|
|
self.setup_err()
|
|
|
|
# Build a fake package with a js file that would not validate if it
|
|
|
|
# wasn't whitelisted.
|
|
|
|
mock_package = MockXPI({"foo.js": "tests/resources/content/error.js"})
|
2014-10-28 16:57:58 +03:00
|
|
|
|
|
|
|
# Build the mock whitelist. Convert line-endings to unix-style before
|
|
|
|
# building the hash, it should still validate properly as the code that
|
|
|
|
# validates the package converts every js file to unix-style endings
|
|
|
|
# first.
|
|
|
|
ok_('\r\n' in mock_package.read('foo.js'))
|
|
|
|
foo_js = mock_package.read('foo.js').replace('\r\n', '\n')
|
2014-10-21 18:08:11 +04:00
|
|
|
hashes_whitelist = [hashlib.sha256(foo_js).hexdigest()]
|
|
|
|
|
|
|
|
with patch("appvalidator.testcases.content.hashes_whitelist",
|
|
|
|
hashes_whitelist):
|
|
|
|
eq_(self._run_test(mock_package), 0)
|
|
|
|
self.assert_passes()
|
|
|
|
|
|
|
|
# Prove that it would fail otherwise.
|
|
|
|
eq_(self._run_test(mock_package), 1)
|
|
|
|
self.assert_failed()
|
|
|
|
|
|
|
|
|
2013-11-02 01:12:28 +04:00
|
|
|
class TestCordova(TestCase):
|
2013-11-02 01:11:39 +04:00
|
|
|
|
|
|
|
def test_cordova_fail(self):
|
|
|
|
"Test that cordova is detected in the content tests."
|
|
|
|
self.setup_err()
|
|
|
|
mock_package = MockXPI({"foo.bar": "tests/resources/content/junk.xpi"})
|
|
|
|
|
|
|
|
content.test_cordova(self.err, mock_package)
|
|
|
|
assert not self.err.metadata["cordova"]
|
|
|
|
|
|
|
|
# We can recycle the error bundle since it's clean.
|
|
|
|
mock_package = MockXPI({"www/cordova.js": "tests/resources/content/junk.xpi"})
|
|
|
|
|
|
|
|
content.test_cordova(self.err, mock_package)
|
|
|
|
assert self.err.metadata["cordova"]
|