def validate(metadata, tar, verbose=False): if not 'safe_name' in metadata: raise MusaException("safe name is missing") if not isinstance(metadata['safe_name'], str): raise MusaException("safe name is invalid") tar_path = metadata['safe_name'] if tar is not None: suspect_filenames = tar.getnames() for fn in suspect_filenames: if not fn.startswith(tar_path): raise MusaException("invalid file in tarball") if tar.getmember(fn).isdir(): suspect_filenames.remove(fn) else: suspect_filenames = [] if verbose: print "validating misc data" validate_misc(metadata, tar, tar_path, suspect_filenames) if verbose: print "validating license..." validate_license(metadata, tar, tar_path, suspect_filenames) if verbose: print "validating text" validate_text(metadata, tar, tar_path, suspect_filenames) if verbose: print "validating type..." validate_type(metadata, tar, tar_path, suspect_filenames) if len(suspect_filenames) != 0: if verbose: print "the following unknown files are found:" for sf in suspect_filenames: print " - %s" % sf raise MusaException("unknown files in tarball")
def test_invalid_license(): pool = [_create_license() for _ in range(5)] lcase_key = pool[0].lower() assert not validate_license(lcase_key) shorter_key = pool[1][:-2] assert not validate_license(shorter_key) longer_key = pool[2] + 'A' assert not validate_license(longer_key) wrong_prefix = 'AB-' + pool[3][3:] assert not validate_license(wrong_prefix) empty_key = '' assert not validate_license(empty_key) key_reversed = pool[4][::-1] assert not validate_license(key_reversed)
def test_return_type(): key = _create_license() assert validate_license(key) is True assert validate_license(key[:-1]) is False
def test_valid_license(): for _ in range(10): key = _create_license() assert validate_license(key)
def test_valid_prefix(): license = "PB-U8N435EH-PG65PW87-IXPWQG5T-898XSZI4" assert validate_license(license) assert not validate_license(license.replace('PB', 'APB')) assert not validate_license(license.replace('PB', 'COAPB'))