1# Copyright 2021 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import unittest 16 17import fc_sort 18 19class FcSortTest(unittest.TestCase): 20 21 def testGetStemLen(self): 22 self.assertEqual(fc_sort.get_stem_len("/data"), 5) 23 self.assertEqual(fc_sort.get_stem_len("/data/system"), 12) 24 self.assertEqual(fc_sort.get_stem_len("/data/(system)?"), 6) 25 26 def testIsMeta(self): 27 self.assertEqual(fc_sort.is_meta("/data"), False) 28 self.assertEqual(fc_sort.is_meta("/data$"), True) 29 self.assertEqual(fc_sort.is_meta(r"\$data"), False) 30 31 def testLesserThan(self): 32 n1 = fc_sort.FileContextsNode.create("/data u:object_r:rootfs:s0") 33 # shorter stem_len 34 n2 = fc_sort.FileContextsNode.create("/d u:object_r:rootfs:s0") 35 # is meta 36 n3 = fc_sort.FileContextsNode.create("/data/l(/.*)? u:object_r:log:s0") 37 # with file_type 38 n4 = fc_sort.FileContextsNode.create("/data -- u:object_r:rootfs:s0") 39 contexts = [n1, n2, n3, n4] 40 contexts.sort() 41 self.assertEqual(contexts, [n3, n2, n1, n4]) 42 43 def testReadFileContexts(self): 44 content = """# comment 45/ u:object_r:rootfs:s0 46# another comment 47/adb_keys u:object_r:adb_keys_file:s0 48""" 49 fcs = fc_sort.read_file_contexts(content.splitlines()) 50 self.assertEqual(len(fcs), 2) 51 52 self.assertEqual(fcs[0].path, "/") 53 self.assertEqual(fcs[0].type, "rootfs") 54 55 self.assertEqual(fcs[1].path, "/adb_keys") 56 self.assertEqual(fcs[1].type, "adb_keys_file") 57 58if __name__ == '__main__': 59 unittest.main(verbosity=2) 60