def test_ls_var(output, expected_output): test = LsVarWwwPerms(context_wrap(output)) assert len(test.file_permissions) == len(expected_output) for test_line, expected_line in zip(test.file_permissions, expected_output): assert repr(test_line) == repr(FilePermissions(expected_line)) assert test_line.line == FilePermissions(expected_line).line
def test_permissions_invalid(): for vector in PERMISSIONS_TEST_EXCEPTION_VECTORS: garbage, should_raise = vector if should_raise: with pytest.raises(ValueError): FilePermissions(garbage) else: # shouldn't raise an exception FilePermissions(garbage)
def test_dir_parsed(): context = context_wrap(LS_1) result = LsVarLog(context) ls = {} current_dir = "" test_lines = LS_1.splitlines() for line in test_lines: # wonky parsing from memory to test the actual implementation; doesn't expect evil input! if line.endswith(":"): current_dir = line.split(":")[0] ls[current_dir] = [] elif line.startswith("total"): pass elif line: fileperm = FilePermissions(line) ls[current_dir].append(fileperm.path) for dir in ls: assert dir in result dir_from_parser = result.listings[dir]['entries'] # Two way cross-check: # Were all files in our simple parse found in the parser? for fil in ls[dir]: assert fil in dir_from_parser # Were all files in the parser found in our simple parse? for fil in dir_from_parser: assert fil in ls[dir]
def parse_content(self, content): super(LsVarWwwPerms, self).parse_content(content) self.file_permissions = [] if "/var/www" in self: for filename, info in sorted(self.listing_of("/var/www").items()): self.file_permissions.append(FilePermissions( info["raw_entry"]))
def test_multiple_directories(): dirs = FileListing(context_wrap(test_file_listing.MULTIPLE_DIRECTORIES)) assert '/etc/sysconfig' in dirs assert 'cbq' in dirs.dirs_of('/etc/sysconfig') # drwxr-xr-x. 2 0 0 41 Jul 6 23:32 cbq obj = FilePermissions.from_dict(dirs.path_entry('/etc/sysconfig/cbq')) assert hasattr(obj, 'name') assert obj.name == 'cbq' assert obj.perms_owner == 'rwx' assert obj.perms_group == 'r-x' assert obj.perms_other == 'r-x'
def get_filepermissions(self, dir_name_where_to_search, dir_or_file_name_to_get): """ Returns a FilePermissions object, if found, for the specified dir or file name in the specified directory. The directory must be specified by the full path without trailing slash. The dir or file name to get must be specified by the name only (without path). This is provided for several parsers which rely on this functionality, and may be deprecated and removed in the future. Args: dir_name_where_to_search (string): Full path without trailing slash where to search. dir_or_file_name_to_getl (string): Name of the dir or file to get FilePermissions for. Returns: FilePermissions: If found or None if not found. """ if dir_name_where_to_search in self: d = self.listings[dir_name_where_to_search]['entries'] if dir_or_file_name_to_get in d: return FilePermissions(d[dir_or_file_name_to_get]['raw_entry'])
def test_get_filepermissions(): context = context_wrap(LS_1) result = LsVarLog(context) ls = {} current_dir = "" test_lines = LS_1.splitlines() for line in test_lines: # wonky parsing from memory to test the actual implementation; doesn't expect evil input! if line.endswith(":"): current_dir = line.split(":")[0] ls[current_dir] = [] elif line.startswith("total"): pass elif line: fileperm = FilePermissions(line) ls[current_dir].append(fileperm) for dir in ls: assert dir in result for fil in ls[dir]: found = result.get_filepermissions(dir, fil.path) assert found is not None assert fil.line == found.line not_found = result.get_filepermissions(dir, "nonexisting" + fil.path) assert not_found is None
def test_permissions(): for vector in PERMISSIONS_TEST_VECTORS: (line, with_group, permissions_owner, permissions_group, permissions_other, owner, group, path, owned_by_root_user, owned_by_root_user_and_group, only_root_can_read, only_root_can_write) = vector p = FilePermissions(line) assert p.perms_owner == permissions_owner assert p.perms_group == permissions_group assert p.perms_other == permissions_other assert p.owner == owner assert p.group == group assert p.owned_by('root', also_check_group=False) == owned_by_root_user assert p.owned_by( 'root', also_check_group=True) == owned_by_root_user_and_group assert p.only_root_can_read( root_group_can_read=with_group) == only_root_can_read assert p.only_root_can_write( root_group_can_write=with_group) == only_root_can_write assert p.all_zero() == all( (p.perms_owner == '---', p.perms_group == '---', p.perms_other == '---')) assert p.owner_can_read() == ('r' in p.perms_owner) assert p.owner_can_write() == ('w' in p.perms_owner) assert p.owner_can_only_read() == ('r--' == p.perms_owner) assert p.group_can_read() == ('r' in p.perms_group) assert p.group_can_write() == ('w' in p.perms_group) assert p.group_can_only_read() == ('r--' == p.perms_group) assert p.others_can_read() == ('r' in p.perms_other) assert p.others_can_write() == ('w' in p.perms_other) assert p.others_can_only_read() == ('r--' == p.perms_other)
def __init__(self, context): self.line = "" CommandParser.__init__(self, context) FilePermissions.__init__(self, self.line)