Show only violating entries on sepolicy_tests

This is for more visibility upon error.

Test: m sepolicy_test
Change-Id: Idad76505c9574e356d101c14f24ef68414475f65
This commit is contained in:
Inseob Kim 2023-09-27 17:32:26 +09:00
parent c9daa54919
commit 34d6c64705

View file

@ -109,17 +109,22 @@ class Policy:
# Query policy for the types associated with Attr # Query policy for the types associated with Attr
TypesPol = self.QueryTypeAttribute(Attr, True) - set(ExcludedTypes) TypesPol = self.QueryTypeAttribute(Attr, True) - set(ExcludedTypes)
# Search file_contexts to find types associated with input paths. # Search file_contexts to find types associated with input paths.
TypesFc, Files = self.__GetTypesAndFilesByFilePathPrefix(MatchPrefix, DoNotMatchPrefix) PathTypes = self.__GetTypesAndFilesByFilePathPrefix(MatchPrefix, DoNotMatchPrefix)
violators = TypesFc.intersection(TypesPol) violators = set()
for PathType in PathTypes:
filepath, filetype = PathType
if filetype in TypesPol:
violators.add((str(filetype), str(filepath)))
ret = "" ret = ""
if len(violators) > 0: if len(violators) > 0:
ret += "The following types on " ret += "The following types on "
ret += " ".join(str(x) for x in sorted(MatchPrefix)) ret += " ".join(str(x) for x in sorted(MatchPrefix))
ret += " must not be associated with the " ret += " must not be associated with the "
ret += "\"" + Attr + "\" attribute: " ret += "\"" + Attr + "\" attribute.\n"
ret += " ".join(str(x) for x in sorted(violators)) + "\n" ret += "Violator types and corresponding paths:\n"
ret += " corresponding to files: " ret += "\n".join(str(x) for x in sorted(violators))
ret += " ".join(str(x) for x in sorted(Files)) + "\n" ret += "\n"
return ret return ret
# Check that all types for "filesystem" have "attribute" associated with them # Check that all types for "filesystem" have "attribute" associated with them
@ -146,18 +151,22 @@ class Policy:
TypesPol = self.QueryTypeAttribute(Attr, True) TypesPol = self.QueryTypeAttribute(Attr, True)
# Search file_contexts to find paths/types that should be associated with # Search file_contexts to find paths/types that should be associated with
# Attr. # Attr.
TypesFc, Files = self.__GetTypesAndFilesByFilePathPrefix(MatchPrefix, DoNotMatchPrefix) PathTypes = self.__GetTypesAndFilesByFilePathPrefix(MatchPrefix, DoNotMatchPrefix)
violators = TypesFc.difference(TypesPol) violators = set()
for PathType in PathTypes:
filepath, filetype = PathType
if filetype not in TypesPol:
violators.add((str(filetype), str(filepath)))
ret = "" ret = ""
if len(violators) > 0: if len(violators) > 0:
ret += "The following types on " ret += "The following types on "
ret += " ".join(str(x) for x in sorted(MatchPrefix)) ret += " ".join(str(x) for x in sorted(MatchPrefix))
ret += " must be associated with the " ret += " must be associated with the "
ret += "\"" + Attr + "\" attribute: " ret += "\"" + Attr + "\" attribute.\n"
ret += " ".join(str(x) for x in sorted(violators)) + "\n" ret += "Violator types and corresponding paths:\n"
ret += " corresponding to files: " ret += "\n".join(str(x) for x in sorted(violators))
ret += " ".join(str(x) for x in sorted(Files)) + "\n" ret += "\n"
return ret return ret
def AssertPropertyOwnersAreExclusive(self): def AssertPropertyOwnersAreExclusive(self):
@ -334,8 +343,7 @@ class Policy:
# Return types that match MatchPrefixes but do not match # Return types that match MatchPrefixes but do not match
# DoNotMatchPrefixes # DoNotMatchPrefixes
def __GetTypesAndFilesByFilePathPrefix(self, MatchPrefixes, DoNotMatchPrefixes): def __GetTypesAndFilesByFilePathPrefix(self, MatchPrefixes, DoNotMatchPrefixes):
Types = set() ret = []
Files = set()
MatchPrefixesWithIndex = [] MatchPrefixesWithIndex = []
for MatchPrefix in MatchPrefixes: for MatchPrefix in MatchPrefixes:
@ -346,9 +354,8 @@ class Policy:
for PathType in PathTypes: for PathType in PathTypes:
if MatchPathPrefixes(PathType[0], DoNotMatchPrefixes): if MatchPathPrefixes(PathType[0], DoNotMatchPrefixes):
continue continue
Types.add(PathType[1]) ret.append(PathType)
Files.add(PathType[0]) return ret
return Types, Files
def __GetTERules(self, policydbP, avtabIterP, Rules): def __GetTERules(self, policydbP, avtabIterP, Rules):
if Rules is None: if Rules is None: