def test_write_from_array(self): build_info = BuildInfo(self.buildinfo_file_path) file_path = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, file_path) file_path = os.path.join(file_path, 'BUILD-INFO.txt') BuildInfo.write_from_array(build_info.build_info_array, file_path) build_info_test = BuildInfo(file_path) self.assertEquals(build_info_test.build_info_array, build_info.build_info_array)
def test_parseData_array_expected(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] data = "Format-Version: 2.0" with self.assertRaises(IncorrectDataFormatException): build_info.parseData(data)
def test_parseData_fails(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] data = ["text"] with self.assertRaises(IncorrectDataFormatException): build_info.parseData(data)
def test_get_emptyField(self): value = "notempty" build_info = BuildInfo(self.buildinfo_file_path) for pair in build_info.file_info_array: if "auth-groups" in pair: value = pair["auth-groups"] self.assertFalse(value)
def test_get(self): value = None build_info = BuildInfo(self.buildinfo_file_path) for pair in build_info.file_info_array: if "build-name" in pair: value = pair["build-name"] self.assertEqual(value, "landing-protected")
def test_parseData_format_version(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] data = ["Format-Version: 2.0"] build_info.parseData(data) self.assertEqual(build_info.build_info_array, [{ "format-version": "2.0" }])
def test_buildinfo_simple_filename(self): with temporary_directory() as serve_root: sample_file = serve_root.make_file("MD5SUM", data="blah") serve_root.make_file("BUILD-INFO.txt", data=("Format-Version: 2.0\n\n" "Files-Pattern: MD5SUM\n" "License-Type: open\n")) build_info = BuildInfo(sample_file) file_info = build_info.getInfoForFile() self.assertEqual('open', file_info[0]['license-type'])
def test_apply_to_dir_auth_groups_field(self): dir_path = THIS_DIRECTORY + \ '/testserver_root/build-info/subdir2' build_info = BuildInfo(dir_path) expected = [{ 'build-name': 'landing-protected', 'license-type': 'protected', 'auth-groups': 'linaro' }] self.assertEquals(expected, build_info.getInfoForFile())
def test_parseBlock_license(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.line_no = 0 build_info.build_info_array = [{}] data = ["Format-Version: 2.0", "License-Text: line1", " line2"] values = build_info.parseBlock(data) self.assertEqual(values, [{ "format-version": "2.0", "license-text": "line1\nline2" }])
def set_up_license(target_file, index=0): # Get BuildInfo for target file file_path = os.path.join(TESTSERVER_ROOT, target_file) build_info = BuildInfo(file_path) # Insert license information into database text = build_info.get("license-text", index) digest = hashlib.md5(text).hexdigest() theme = build_info.get("theme", index) _insert_license_into_db(digest, text, theme) return digest
def test_partial_build_info_file_protected(self): target_file = ("partial-license-settings/" "partially-complete-build-info/" "should_be_protected.txt") file_path = os.path.join(TESTSERVER_ROOT, target_file) build_info = BuildInfo(file_path) # Try to fetch file from server - we should be redirected url = urlparse.urljoin("http://testserver/", target_file) response = self.client.get(url, follow=True) digest = hashlib.md5(build_info.get("license-text")).hexdigest() self.assertRedirects(response, '/license?lic=%s&url=%s' % (digest, target_file))
def test_getInfoForFile_no_block_for_file(self): file_path = os.path.abspath(__file__) build_info = BuildInfo(self.buildinfo_file_path) build_info.full_file_name = file_path build_info.build_info_array = [{}] build_info.file_info_array = [{}] data = [ "Format-Version: 2.0", "Files-Pattern: *.txt", "License-Type: protected" ] build_info.parseData(data) build_info.file_info_array = build_info.getInfoForFile() self.assertEquals(build_info.file_info_array, [{}])
def test_parseData_extra_fields(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] data = [ "Format-Version: 2.0", "Files-Pattern: *.txt", "Build-Name: woohoo" ] build_info.parseData(data) self.assertEqual(build_info.build_info_array, [{ "format-version": "2.0", '*.txt': [{ 'build-name': 'woohoo' }] }])
def test_remove_false_positives_real(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] build_info.file_info_array = [{}] data = [ "Format-Version: 2.0", "Files-Pattern: *.txt", "License-Type: protected", "Files-Pattern: *.txt", "License-Type: open" ] build_info.parseData(data) build_info.file_info_array = build_info.getInfoForFile() build_info.remove_false_positives() self.assertEquals(build_info.file_info_array, [{ 'license-type': 'protected' }])
def test_no_exception_ip(self): internal_host = '10.1.2.3' target_file = 'build-info/origen-blob.txt' file_path = os.path.join(TESTSERVER_ROOT, target_file) build_info = BuildInfo(file_path) # Try to fetch file from server - we should be redirected url = urlparse.urljoin("http://testserver/", target_file) response = self.client.get(url, follow=True, REMOTE_ADDR=internal_host) digest = hashlib.md5(build_info.get("license-text")).hexdigest() self.assertRedirects(response, '/license?lic=%s&url=%s' % (digest, target_file)) # Make sure that we get the license text in the license page self.assertContains(response, build_info.get("license-text")) # Test that we use the "samsung" theme. This contains exynos.png self.assertContains(response, "exynos.png")
def test_redirect_to_license_ste(self): # Get BuildInfo for target file target_file = "build-info/snowball-blob.txt" file_path = os.path.join(TESTSERVER_ROOT, target_file) build_info = BuildInfo(file_path) # Try to fetch file from server - we should be redirected url = urlparse.urljoin("http://testserver/", target_file) response = self.client.get(url, follow=True) digest = hashlib.md5(build_info.get("license-text")).hexdigest() self.assertRedirects(response, '/license?lic=%s&url=%s' % (digest, target_file)) # Make sure that we get the license text in the license page self.assertContains(response, build_info.get("license-text")) # Test that we use the "stericsson" theme. This contains igloo.png self.assertContains(response, "igloo.png")
def test_parseData_block_multiple_patterns(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.build_info_array = [{}] data = [ "Format-Version: 2.0", "Files-Pattern: *.txt,*.tgz", "Build-Name: weehee" ] build_info.parseData(data) expected = [{ 'format-version': '2.0', '*.txt': [{ 'build-name': 'weehee' }], '*.tgz': [{ 'build-name': 'weehee' }] }] self.assertEquals(expected, build_info.build_info_array)
def test_parseLine_trims(self): line = "Build-Name: value" build_info = BuildInfo(self.buildinfo_file_path) self.assertDictEqual({"build-name": "value"}, build_info.parseLine(line))
def test_no_buildinfo(self): file_path = THIS_DIRECTORY + \ '/testserver_root/build-info/no-build-info/file' with self.assertRaises(IOError): BuildInfo(file_path)
def test_parseLine_fails(self): line = "no separator" build_info = BuildInfo(self.buildinfo_file_path) with self.assertRaises(IncorrectDataFormatException): build_info.parseLine(line)
def test_getFormatVersion(self): build_info = BuildInfo(self.buildinfo_file_path) self.assertEqual("0.5", build_info.getFormatVersion())
def test_parseContinuation_indexed(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.line_no = 0 self.assertEquals("", build_info.parseContinuation(["no-space", " space"]))
def test_apply_to_nonexistent_file(self): with self.assertRaises(IOError): BuildInfo("nonexistent.file")
def test_apply_to_file(self): build_info = BuildInfo(self.buildinfo_file_path) self.assertIn("Files-Pattern: *.txt", build_info.lines)
def test_parseLine_invalid_field(self): line = "field: value" build_info = BuildInfo(self.buildinfo_file_path) with self.assertRaises(IncorrectDataFormatException): build_info.parseLine(line)
def test_parseData_no_format_version_fails(self): build_info = BuildInfo(self.buildinfo_file_path) with self.assertRaises(IncorrectDataFormatException): build_info.parseData(["Build-Name: blah"])
def test_parseContinuation(self): build_info = BuildInfo(self.buildinfo_file_path) build_info.line_no = 1 val = build_info.parseContinuation(["no-space", " line1", " line2"]) self.assertEquals("\nline1\nline2", val)
def test_isValidField_false(self): build_info = BuildInfo(self.buildinfo_file_path) self.assertFalse(build_info.isValidField("field"))
def test_isValidField_true(self): build_info = BuildInfo(self.buildinfo_file_path) for field in build_info.fields_defined: self.assertTrue(build_info.isValidField(field))