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
diff --git a/tests/Android.bp b/tests/Android.bp
index 58d4c4f..959a214 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -24,18 +24,6 @@
     },
 }
 
-python_defaults {
-    name: "py2_only",
-    version: {
-        py2: {
-            enabled: true,
-        },
-        py3: {
-            enabled: false,
-        },
-    },
-}
-
 python_binary_host {
     name: "treble_sepolicy_tests",
     srcs: [
@@ -45,7 +33,6 @@
         "treble_sepolicy_tests.py",
     ],
     required: ["libsepolwrap"],
-    defaults: ["py2_only"],
 }
 
 python_binary_host {
@@ -56,7 +43,6 @@
         "sepolicy_tests.py",
     ],
     required: ["libsepolwrap"],
-    defaults: ["py2_only"],
 }
 
 python_binary_host {
@@ -67,7 +53,6 @@
         "searchpolicy.py",
     ],
     required: ["libsepolwrap"],
-    defaults: ["py2_only"],
 }
 
 python_binary_host {
@@ -76,7 +61,6 @@
         "combine_maps.py",
         "mini_parser.py",
     ],
-    defaults: ["py2_only"],
 }
 
 python_binary_host {
@@ -84,7 +68,6 @@
     srcs: [
         "fc_sort.py",
     ],
-    defaults: ["py2_only"],
 }
 
 python_test_host {
@@ -93,7 +76,6 @@
         "fc_sort.py",
         "fc_sort_test.py",
     ],
-    defaults: ["py2_only"],
     test_options: {
         unit_test: true,
     }
diff --git a/tests/fc_sort.py b/tests/fc_sort.py
index 2bad6fb..4def748 100644
--- a/tests/fc_sort.py
+++ b/tests/fc_sort.py
@@ -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");
 # you may not use this file except in compliance with the License.
diff --git a/tests/fc_sort_test.py b/tests/fc_sort_test.py
index 2d8d2d8..accd0a1 100644
--- a/tests/fc_sort_test.py
+++ b/tests/fc_sort_test.py
@@ -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");
 # you may not use this file except in compliance with the License.
diff --git a/tests/mini_parser.py b/tests/mini_parser.py
index cba9e39..25018a7 100644
--- a/tests/mini_parser.py
+++ b/tests/mini_parser.py
@@ -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
 import re
 import sys
@@ -6,8 +20,6 @@
 # files and retrieve type and attribute information until proper support is
 # built into libsepol
 
-# get the text in the next matching parens
-
 class MiniCilParser:
     def __init__(self, policyFile):
         self.types = set() # types declared in mapping
diff --git a/tests/policy.py b/tests/policy.py
index 4648e30..06157fd 100644
--- a/tests/policy.py
+++ b/tests/policy.py
@@ -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 *
 import re
 import os
@@ -129,7 +143,7 @@
     # all types associated with an attribute if IsAttr=True
     def QueryTypeAttribute(self, Type, IsAttr):
         TypeIterP = self.__libsepolwrap.init_type_iter(self.__policydbP,
-                        create_string_buffer(Type), IsAttr)
+                        create_string_buffer(Type.encode("ascii")), IsAttr)
         if (TypeIterP == None):
             sys.exit("Failed to initialize type iterator")
         buf = create_string_buffer(self.__BUFSIZE)
@@ -138,7 +152,7 @@
             ret = self.__libsepolwrap.get_type(buf, self.__BUFSIZE,
                     self.__policydbP, TypeIterP)
             if ret == 0:
-                TypeAttr.add(buf.value)
+                TypeAttr.add(buf.value.decode("ascii"))
                 continue
             if ret == 1:
                 break;
@@ -237,7 +251,7 @@
             ret = self.__libsepolwrap.get_type(buf, self.__BUFSIZE,
                     self.__policydbP, TypeIterP)
             if ret == 0:
-                AllTypes.add(buf.value)
+                AllTypes.add(buf.value.decode("ascii"))
                 continue
             if ret == 1:
                 break;
@@ -302,7 +316,7 @@
             ret = self.__libsepolwrap.get_allow_rule(buf, self.__BUFSIZE,
                         policydbP, avtabIterP)
             if ret == 0:
-                Rule = TERule(buf.value)
+                Rule = TERule(buf.value.decode("ascii"))
                 Rules.add(Rule)
                 continue
             if ret == 1:
@@ -399,10 +413,10 @@
             ret = self.__libsepolwrap.get_genfs(buf, self.__BUFSIZE,
                         self.__policydbP, GenfsIterP)
             if ret == 0:
-                self.__GenfsDictAdd(self.__GenfsDict, buf.value)
+                self.__GenfsDictAdd(self.__GenfsDict, buf.value.decode("ascii"))
                 continue
             if ret == 1:
-                self.__GenfsDictAdd(self.__GenfsDict, buf.value)
+                self.__GenfsDictAdd(self.__GenfsDict, buf.value.decode("ascii"))
                 break;
             # We should never get here.
             sys.exit("Failed to get genfs entries")
@@ -434,7 +448,7 @@
 
     # load policy
     def __InitPolicy(self, PolicyPath):
-        cPolicyPath = create_string_buffer(PolicyPath)
+        cPolicyPath = create_string_buffer(PolicyPath.encode("ascii"))
         self.__policydbP = self.__libsepolwrap.load_policy(cPolicyPath)
         if (self.__policydbP is None):
             sys.exit("Failed to load policy")
diff --git a/tests/searchpolicy.py b/tests/searchpolicy.py
index ff9318b..9d2c636 100644
--- a/tests/searchpolicy.py
+++ b/tests/searchpolicy.py
@@ -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 policy
@@ -70,4 +84,4 @@
                 " ".join(r.perms) + ";")
 
 for r in sorted(rules):
-    print r
+    print(r)
diff --git a/tests/sepolicy_tests.py b/tests/sepolicy_tests.py
index 1d26dfc..a05d8f2 100644
--- a/tests/sepolicy_tests.py
+++ b/tests/sepolicy_tests.py
@@ -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 Option, OptionValueError
 import os
diff --git a/tests/treble_sepolicy_tests.py b/tests/treble_sepolicy_tests.py
index 27e92b1..1c5b8e2 100644
--- a/tests/treble_sepolicy_tests.py
+++ b/tests/treble_sepolicy_tests.py
@@ -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 Option, OptionValueError
 import os
@@ -36,17 +50,17 @@
 def PrintScontexts():
     for d in sorted(alldomains.keys()):
         sctx = alldomains[d]
-        print d
-        print "\tcoredomain="+str(sctx.coredomain)
-        print "\tappdomain="+str(sctx.appdomain)
-        print "\tfromSystem="+str(sctx.fromSystem)
-        print "\tfromVendor="+str(sctx.fromVendor)
-        print "\tattributes="+str(sctx.attributes)
-        print "\tentrypoints="+str(sctx.entrypoints)
-        print "\tentrypointpaths="
+        print(d)
+        print("\tcoredomain="+str(sctx.coredomain))
+        print("\tappdomain="+str(sctx.appdomain))
+        print("\tfromSystem="+str(sctx.fromSystem))
+        print("\tfromVendor="+str(sctx.fromVendor))
+        print("\tattributes="+str(sctx.attributes))
+        print("\tentrypoints="+str(sctx.entrypoints))
+        print("\tentrypointpaths=")
         if sctx.entrypointpaths is not None:
             for path in sctx.entrypointpaths:
-                print "\t\t"+str(path)
+                print("\t\t"+str(path))
 
 alldomains = {}
 coredomains = set()
@@ -367,7 +381,7 @@
 
     # Mapping files and public platform policy are only necessary for the
     # 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:
             sys.exit("Must specify the current platform-only policy file\n"
                      + parser.usage)