Migrate tests/ to Python 3

In general, it appears that libselinux and libsepol interpret paths and
contexts as bytes. For instance, selabel_file(5) mentions about the path
field of file_contexts:

  Strings representing paths are processed as bytes (as opposed to
  Unicode), meaning that non-ASCII characters are not matched
  by a single wildcard.

libsepol also uses primitives such as strchr[1], which explicitly
operate at the byte level (see strchr(3)). However, practically, Android
paths and contexts all uses ASCII characters.

Use the str type (i.e., Unicode) for all Python code to avoid a larger
refactoring. Ensure we convert to bytes for inputs and outputs of
libsepolwrap.so. The encoding "ascii" is used, which will raise an error
should a context or type contain non-ASCII characters.

Update headers to match development/docs/copyright-templates.

[1] https://cs.android.com/android/platform/superproject/+/master:external/selinux/libsepol/src/context_record.c;l=224;drc=454466e2e49fd99f36db78396e604962b8682cb4

Bug: 200119288
Test: lunch aosp_bramble-userdebug && m
Test: atest --host fc_sort_test
Test: manually run searchpolicy
Change-Id: I72d41a35f90b2d4112e481cd8d7408764a6c8132
This commit is contained in:
Thiébaud Weksteen 2021-11-26 09:12:41 +11:00
parent 2d059f520c
commit f24b457dd0
8 changed files with 92 additions and 42 deletions

View file

@ -24,18 +24,6 @@ cc_library_host_shared {
}, },
} }
python_defaults {
name: "py2_only",
version: {
py2: {
enabled: true,
},
py3: {
enabled: false,
},
},
}
python_binary_host { python_binary_host {
name: "treble_sepolicy_tests", name: "treble_sepolicy_tests",
srcs: [ srcs: [
@ -45,7 +33,6 @@ python_binary_host {
"treble_sepolicy_tests.py", "treble_sepolicy_tests.py",
], ],
required: ["libsepolwrap"], required: ["libsepolwrap"],
defaults: ["py2_only"],
} }
python_binary_host { python_binary_host {
@ -56,7 +43,6 @@ python_binary_host {
"sepolicy_tests.py", "sepolicy_tests.py",
], ],
required: ["libsepolwrap"], required: ["libsepolwrap"],
defaults: ["py2_only"],
} }
python_binary_host { python_binary_host {
@ -67,7 +53,6 @@ python_binary_host {
"searchpolicy.py", "searchpolicy.py",
], ],
required: ["libsepolwrap"], required: ["libsepolwrap"],
defaults: ["py2_only"],
} }
python_binary_host { python_binary_host {
@ -76,7 +61,6 @@ python_binary_host {
"combine_maps.py", "combine_maps.py",
"mini_parser.py", "mini_parser.py",
], ],
defaults: ["py2_only"],
} }
python_binary_host { python_binary_host {
@ -84,7 +68,6 @@ python_binary_host {
srcs: [ srcs: [
"fc_sort.py", "fc_sort.py",
], ],
defaults: ["py2_only"],
} }
python_test_host { python_test_host {
@ -93,7 +76,6 @@ python_test_host {
"fc_sort.py", "fc_sort.py",
"fc_sort_test.py", "fc_sort_test.py",
], ],
defaults: ["py2_only"],
test_options: { test_options: {
unit_test: true, unit_test: true,
} }

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# Copyright 2021 - The Android Open Source Project # Copyright 2021 The Android Open Source Project
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

View file

@ -1,4 +1,4 @@
# Copyright 2021 - The Android Open Source Project # Copyright 2021 The Android Open Source Project
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.

View file

@ -1,3 +1,17 @@
# Copyright 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from os.path import basename from os.path import basename
import re import re
import sys import sys
@ -6,8 +20,6 @@ import sys
# files and retrieve type and attribute information until proper support is # files and retrieve type and attribute information until proper support is
# built into libsepol # built into libsepol
# get the text in the next matching parens
class MiniCilParser: class MiniCilParser:
def __init__(self, policyFile): def __init__(self, policyFile):
self.types = set() # types declared in mapping self.types = set() # types declared in mapping

View file

@ -1,3 +1,17 @@
# Copyright 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ctypes import * from ctypes import *
import re import re
import os import os
@ -129,7 +143,7 @@ class Policy:
# all types associated with an attribute if IsAttr=True # all types associated with an attribute if IsAttr=True
def QueryTypeAttribute(self, Type, IsAttr): def QueryTypeAttribute(self, Type, IsAttr):
TypeIterP = self.__libsepolwrap.init_type_iter(self.__policydbP, TypeIterP = self.__libsepolwrap.init_type_iter(self.__policydbP,
create_string_buffer(Type), IsAttr) create_string_buffer(Type.encode("ascii")), IsAttr)
if (TypeIterP == None): if (TypeIterP == None):
sys.exit("Failed to initialize type iterator") sys.exit("Failed to initialize type iterator")
buf = create_string_buffer(self.__BUFSIZE) buf = create_string_buffer(self.__BUFSIZE)
@ -138,7 +152,7 @@ class Policy:
ret = self.__libsepolwrap.get_type(buf, self.__BUFSIZE, ret = self.__libsepolwrap.get_type(buf, self.__BUFSIZE,
self.__policydbP, TypeIterP) self.__policydbP, TypeIterP)
if ret == 0: if ret == 0:
TypeAttr.add(buf.value) TypeAttr.add(buf.value.decode("ascii"))
continue continue
if ret == 1: if ret == 1:
break; break;
@ -237,7 +251,7 @@ class Policy:
ret = self.__libsepolwrap.get_type(buf, self.__BUFSIZE, ret = self.__libsepolwrap.get_type(buf, self.__BUFSIZE,
self.__policydbP, TypeIterP) self.__policydbP, TypeIterP)
if ret == 0: if ret == 0:
AllTypes.add(buf.value) AllTypes.add(buf.value.decode("ascii"))
continue continue
if ret == 1: if ret == 1:
break; break;
@ -302,7 +316,7 @@ class Policy:
ret = self.__libsepolwrap.get_allow_rule(buf, self.__BUFSIZE, ret = self.__libsepolwrap.get_allow_rule(buf, self.__BUFSIZE,
policydbP, avtabIterP) policydbP, avtabIterP)
if ret == 0: if ret == 0:
Rule = TERule(buf.value) Rule = TERule(buf.value.decode("ascii"))
Rules.add(Rule) Rules.add(Rule)
continue continue
if ret == 1: if ret == 1:
@ -399,10 +413,10 @@ class Policy:
ret = self.__libsepolwrap.get_genfs(buf, self.__BUFSIZE, ret = self.__libsepolwrap.get_genfs(buf, self.__BUFSIZE,
self.__policydbP, GenfsIterP) self.__policydbP, GenfsIterP)
if ret == 0: if ret == 0:
self.__GenfsDictAdd(self.__GenfsDict, buf.value) self.__GenfsDictAdd(self.__GenfsDict, buf.value.decode("ascii"))
continue continue
if ret == 1: if ret == 1:
self.__GenfsDictAdd(self.__GenfsDict, buf.value) self.__GenfsDictAdd(self.__GenfsDict, buf.value.decode("ascii"))
break; break;
# We should never get here. # We should never get here.
sys.exit("Failed to get genfs entries") sys.exit("Failed to get genfs entries")
@ -434,7 +448,7 @@ class Policy:
# load policy # load policy
def __InitPolicy(self, PolicyPath): def __InitPolicy(self, PolicyPath):
cPolicyPath = create_string_buffer(PolicyPath) cPolicyPath = create_string_buffer(PolicyPath.encode("ascii"))
self.__policydbP = self.__libsepolwrap.load_policy(cPolicyPath) self.__policydbP = self.__libsepolwrap.load_policy(cPolicyPath)
if (self.__policydbP is None): if (self.__policydbP is None):
sys.exit("Failed to load policy") sys.exit("Failed to load policy")

View file

@ -1,4 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python3
#
# Copyright 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse import argparse
import policy import policy
@ -70,4 +84,4 @@ for r in TERules:
" ".join(r.perms) + ";") " ".join(r.perms) + ";")
for r in sorted(rules): for r in sorted(rules):
print r print(r)

View file

@ -1,3 +1,17 @@
# Copyright 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from optparse import OptionParser from optparse import OptionParser
from optparse import Option, OptionValueError from optparse import Option, OptionValueError
import os import os

View file

@ -1,3 +1,17 @@
# Copyright 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from optparse import OptionParser from optparse import OptionParser
from optparse import Option, OptionValueError from optparse import Option, OptionValueError
import os import os
@ -36,17 +50,17 @@ class scontext:
def PrintScontexts(): def PrintScontexts():
for d in sorted(alldomains.keys()): for d in sorted(alldomains.keys()):
sctx = alldomains[d] sctx = alldomains[d]
print d print(d)
print "\tcoredomain="+str(sctx.coredomain) print("\tcoredomain="+str(sctx.coredomain))
print "\tappdomain="+str(sctx.appdomain) print("\tappdomain="+str(sctx.appdomain))
print "\tfromSystem="+str(sctx.fromSystem) print("\tfromSystem="+str(sctx.fromSystem))
print "\tfromVendor="+str(sctx.fromVendor) print("\tfromVendor="+str(sctx.fromVendor))
print "\tattributes="+str(sctx.attributes) print("\tattributes="+str(sctx.attributes))
print "\tentrypoints="+str(sctx.entrypoints) print("\tentrypoints="+str(sctx.entrypoints))
print "\tentrypointpaths=" print("\tentrypointpaths=")
if sctx.entrypointpaths is not None: if sctx.entrypointpaths is not None:
for path in sctx.entrypointpaths: for path in sctx.entrypointpaths:
print "\t\t"+str(path) print("\t\t"+str(path))
alldomains = {} alldomains = {}
coredomains = set() coredomains = set()
@ -367,7 +381,7 @@ if __name__ == '__main__':
# Mapping files and public platform policy are only necessary for the # Mapping files and public platform policy are only necessary for the
# TrebleCompatMapping test. # TrebleCompatMapping test.
if options.tests is None or options.tests is "TrebleCompatMapping": if options.tests is None or options.tests == "TrebleCompatMapping":
if not options.basepolicy: if not options.basepolicy:
sys.exit("Must specify the current platform-only policy file\n" sys.exit("Must specify the current platform-only policy file\n"
+ parser.usage) + parser.usage)