Loading tools/fs_config/fs_config_generator.py +35 −22 Original line number Diff line number Diff line Loading @@ -112,7 +112,8 @@ class Utils(object): 'Cannot specify delimiter character ":" in uid: "%s"' % uid) if ':' in logon: raise ValueError( 'Cannot specify delimiter character ":" in logon: "%s"' % logon) 'Cannot specify delimiter character ":" in logon: "%s"' % logon) return logon, uid Loading Loading @@ -158,16 +159,17 @@ class AID(object): try: self.normalized_value = str(int(value, 0)) except ValueException: raise ValueError('Invalid "value", not aid number, got: \"%s\"' % value) except ValueError: raise ValueError( 'Invalid "value", not aid number, got: \"%s\"' % value) # Where we calculate the friendly name friendly = identifier[len(AID.PREFIX):].lower() self.friendly = AID._fixup_friendly(friendly) if len(self.friendly) > 31: raise ValueError('AID names must be under 32 characters "%s"' % self.friendly) raise ValueError( 'AID names must be under 32 characters "%s"' % self.friendly) def __eq__(self, other): Loading Loading @@ -217,6 +219,7 @@ class FSConfig(object): user (str): The uid or #define identifier (AID_SYSTEM) group (str): The gid or #define identifier (AID_SYSTEM) caps (str): The capability set. path (str): The path of the file or directory. filename (str): The file it was found in. """ Loading @@ -227,6 +230,7 @@ class FSConfig(object): user (str): The uid or #define identifier (AID_SYSTEM) group (str): The gid or #define identifier (AID_SYSTEM) caps (str): The capability set as a list. path (str): The path of the file or directory. filename (str): The file it was found in. """ self.mode = mode Loading @@ -242,6 +246,11 @@ class FSConfig(object): and self.group == other.group and self.caps == other.caps \ and self.path == other.path and self.filename == other.filename def __repr__(self): return 'FSConfig(%r, %r, %r, %r, %r, %r)' % (self.mode, self.user, self.group, self.caps, self.path, self.filename) class AIDHeaderParser(object): """Parses an android_filesystem_config.h file. Loading @@ -256,10 +265,10 @@ class AIDHeaderParser(object): work. """ _SKIP_AIDS = [ re.compile(r'%sUNUSED[0-9].*' % AID.PREFIX), re.compile(r'%sAPP' % AID.PREFIX), re.compile(r'%sUSER' % AID.PREFIX) re.compile(r'%sAPP' % AID.PREFIX), re.compile(r'%sUSER' % AID.PREFIX) ] _AID_DEFINE = re.compile(r'\s*#define\s+%s.*' % AID.PREFIX) _OEM_START_KW = 'START' Loading Loading @@ -310,7 +319,9 @@ class AIDHeaderParser(object): identifier = chunks[1] value = chunks[2] if any(x.match(identifier) for x in AIDHeaderParser._SKIP_AIDS): if any( x.match(identifier) for x in AIDHeaderParser._SKIP_AIDS): continue try: Loading @@ -322,8 +333,8 @@ class AIDHeaderParser(object): self._handle_aid(identifier, value) except ValueError as exception: sys.exit( error_message('{} for "{}"'.format(exception, identifier))) error_message('{} for "{}"'.format( exception, identifier))) def _handle_aid(self, identifier, value): """Handle an AID C #define. Loading @@ -346,8 +357,8 @@ class AIDHeaderParser(object): raise ValueError('Duplicate aid "%s"' % identifier) if value in self._aid_value_to_name and aid.identifier not in AIDHeaderParser._COLLISION_OK: raise ValueError('Duplicate aid value "%s" for %s' % (value, identifier)) raise ValueError( 'Duplicate aid value "%s" for %s' % (value, identifier)) self._aid_name_to_value[aid.friendly] = aid self._aid_value_to_name[value] = aid.friendly Loading Loading @@ -400,11 +411,11 @@ class AIDHeaderParser(object): if tmp == int_value: raise ValueError('START and END values equal %u' % int_value) elif is_start and tmp < int_value: raise ValueError('END value %u less than START value %u' % (tmp, int_value)) raise ValueError( 'END value %u less than START value %u' % (tmp, int_value)) elif not is_start and tmp > int_value: raise ValueError('END value %u less than START value %u' % (int_value, tmp)) raise ValueError( 'END value %u less than START value %u' % (int_value, tmp)) # Add START values to the head of the list and END values at the end. # Thus, the list is ordered with index 0 as START and index 1 as END. Loading Loading @@ -596,8 +607,8 @@ class FSConfigFileParser(object): break if not found: sys.exit('Invalid section "%s" in file: "%s"' % (section, file_name)) sys.exit('Invalid section "%s" in file: "%s"' % (section, file_name)) # sort entries: # * specified path before prefix match Loading Loading @@ -959,7 +970,7 @@ class FSConfigGen(BaseGenerator): common = base_set & oem_set if len(common) > 0: if common: emsg = 'Following AID Collisions detected for: \n' for friendly in common: base = base_friendly[friendly] Loading Loading @@ -1234,11 +1245,12 @@ class PasswdGen(BaseGenerator): aids = parser.aids # nothing to do if no aids defined if len(aids) == 0: if not aids: return for aid in aids: if required_prefix is None or aid.friendly.startswith(required_prefix): if required_prefix is None or aid.friendly.startswith( required_prefix): self._print_formatted_line(aid) else: sys.exit("%s: AID '%s' must start with '%s'" % Loading Loading @@ -1294,6 +1306,7 @@ class GroupGen(PasswdGen): print "%s::%s:" % (logon, uid) @generator('print') class PrintGen(BaseGenerator): """Prints just the constants and values, separated by spaces, in an easy to Loading tools/fs_config/test_fs_config_generator.py +20 −18 Original line number Diff line number Diff line Loading @@ -45,19 +45,21 @@ class Tests(unittest.TestCase): def test_aid(self): """Test AID class constructor""" aid = AID('AID_FOO_BAR', '0xFF', 'myfakefile') self.assertEquals(aid.identifier, 'AID_FOO_BAR') self.assertEquals(aid.value, '0xFF') self.assertEquals(aid.found, 'myfakefile') self.assertEquals(aid.normalized_value, '255') self.assertEquals(aid.friendly, 'foo_bar') aid = AID('AID_MEDIA_EX', '1234', 'myfakefile') self.assertEquals(aid.identifier, 'AID_MEDIA_EX') self.assertEquals(aid.value, '1234') self.assertEquals(aid.found, 'myfakefile') self.assertEquals(aid.normalized_value, '1234') self.assertEquals(aid.friendly, 'mediaex') aid = AID('AID_FOO_BAR', '0xFF', 'myfakefile', '/system/bin/sh') self.assertEqual(aid.identifier, 'AID_FOO_BAR') self.assertEqual(aid.value, '0xFF') self.assertEqual(aid.found, 'myfakefile') self.assertEqual(aid.normalized_value, '255') self.assertEqual(aid.friendly, 'foo_bar') self.assertEqual(aid.login_shell, '/system/bin/sh') aid = AID('AID_MEDIA_EX', '1234', 'myfakefile', '/vendor/bin/sh') self.assertEqual(aid.identifier, 'AID_MEDIA_EX') self.assertEqual(aid.value, '1234') self.assertEqual(aid.found, 'myfakefile') self.assertEqual(aid.normalized_value, '1234') self.assertEqual(aid.friendly, 'mediaex') self.assertEqual(aid.login_shell, '/vendor/bin/sh') def test_aid_header_parser_good(self): """Test AID Header Parser good input file""" Loading Loading @@ -265,9 +267,9 @@ class Tests(unittest.TestCase): dirs = parser.dirs aids = parser.aids self.assertEquals(len(files), 1) self.assertEquals(len(dirs), 1) self.assertEquals(len(aids), 1) self.assertEqual(len(files), 1) self.assertEqual(len(dirs), 1) self.assertEqual(len(aids), 1) aid = aids[0] fcap = files[0] Loading @@ -275,14 +277,14 @@ class Tests(unittest.TestCase): self.assertEqual(fcap, FSConfig('0777', 'AID_FOO', 'AID_SYSTEM', '(1ULL << CAP_BLOCK_SUSPEND)', 'CAP_MASK_LONG(CAP_BLOCK_SUSPEND)', '/system/bin/file', temp_file.name)) self.assertEqual(dcap, FSConfig('0777', 'AID_FOO', 'AID_SYSTEM', '(0)', '/vendor/path/dir/', temp_file.name)) self.assertEqual(aid, AID('AID_OEM1', '0x1389', temp_file.name)) self.assertEqual(aid, AID('AID_OEM1', '0x1389', temp_file.name, '/vendor/bin/sh')) def test_fs_config_file_parser_bad(self): """Test FSConfig Parser bad input file""" Loading Loading
tools/fs_config/fs_config_generator.py +35 −22 Original line number Diff line number Diff line Loading @@ -112,7 +112,8 @@ class Utils(object): 'Cannot specify delimiter character ":" in uid: "%s"' % uid) if ':' in logon: raise ValueError( 'Cannot specify delimiter character ":" in logon: "%s"' % logon) 'Cannot specify delimiter character ":" in logon: "%s"' % logon) return logon, uid Loading Loading @@ -158,16 +159,17 @@ class AID(object): try: self.normalized_value = str(int(value, 0)) except ValueException: raise ValueError('Invalid "value", not aid number, got: \"%s\"' % value) except ValueError: raise ValueError( 'Invalid "value", not aid number, got: \"%s\"' % value) # Where we calculate the friendly name friendly = identifier[len(AID.PREFIX):].lower() self.friendly = AID._fixup_friendly(friendly) if len(self.friendly) > 31: raise ValueError('AID names must be under 32 characters "%s"' % self.friendly) raise ValueError( 'AID names must be under 32 characters "%s"' % self.friendly) def __eq__(self, other): Loading Loading @@ -217,6 +219,7 @@ class FSConfig(object): user (str): The uid or #define identifier (AID_SYSTEM) group (str): The gid or #define identifier (AID_SYSTEM) caps (str): The capability set. path (str): The path of the file or directory. filename (str): The file it was found in. """ Loading @@ -227,6 +230,7 @@ class FSConfig(object): user (str): The uid or #define identifier (AID_SYSTEM) group (str): The gid or #define identifier (AID_SYSTEM) caps (str): The capability set as a list. path (str): The path of the file or directory. filename (str): The file it was found in. """ self.mode = mode Loading @@ -242,6 +246,11 @@ class FSConfig(object): and self.group == other.group and self.caps == other.caps \ and self.path == other.path and self.filename == other.filename def __repr__(self): return 'FSConfig(%r, %r, %r, %r, %r, %r)' % (self.mode, self.user, self.group, self.caps, self.path, self.filename) class AIDHeaderParser(object): """Parses an android_filesystem_config.h file. Loading @@ -256,10 +265,10 @@ class AIDHeaderParser(object): work. """ _SKIP_AIDS = [ re.compile(r'%sUNUSED[0-9].*' % AID.PREFIX), re.compile(r'%sAPP' % AID.PREFIX), re.compile(r'%sUSER' % AID.PREFIX) re.compile(r'%sAPP' % AID.PREFIX), re.compile(r'%sUSER' % AID.PREFIX) ] _AID_DEFINE = re.compile(r'\s*#define\s+%s.*' % AID.PREFIX) _OEM_START_KW = 'START' Loading Loading @@ -310,7 +319,9 @@ class AIDHeaderParser(object): identifier = chunks[1] value = chunks[2] if any(x.match(identifier) for x in AIDHeaderParser._SKIP_AIDS): if any( x.match(identifier) for x in AIDHeaderParser._SKIP_AIDS): continue try: Loading @@ -322,8 +333,8 @@ class AIDHeaderParser(object): self._handle_aid(identifier, value) except ValueError as exception: sys.exit( error_message('{} for "{}"'.format(exception, identifier))) error_message('{} for "{}"'.format( exception, identifier))) def _handle_aid(self, identifier, value): """Handle an AID C #define. Loading @@ -346,8 +357,8 @@ class AIDHeaderParser(object): raise ValueError('Duplicate aid "%s"' % identifier) if value in self._aid_value_to_name and aid.identifier not in AIDHeaderParser._COLLISION_OK: raise ValueError('Duplicate aid value "%s" for %s' % (value, identifier)) raise ValueError( 'Duplicate aid value "%s" for %s' % (value, identifier)) self._aid_name_to_value[aid.friendly] = aid self._aid_value_to_name[value] = aid.friendly Loading Loading @@ -400,11 +411,11 @@ class AIDHeaderParser(object): if tmp == int_value: raise ValueError('START and END values equal %u' % int_value) elif is_start and tmp < int_value: raise ValueError('END value %u less than START value %u' % (tmp, int_value)) raise ValueError( 'END value %u less than START value %u' % (tmp, int_value)) elif not is_start and tmp > int_value: raise ValueError('END value %u less than START value %u' % (int_value, tmp)) raise ValueError( 'END value %u less than START value %u' % (int_value, tmp)) # Add START values to the head of the list and END values at the end. # Thus, the list is ordered with index 0 as START and index 1 as END. Loading Loading @@ -596,8 +607,8 @@ class FSConfigFileParser(object): break if not found: sys.exit('Invalid section "%s" in file: "%s"' % (section, file_name)) sys.exit('Invalid section "%s" in file: "%s"' % (section, file_name)) # sort entries: # * specified path before prefix match Loading Loading @@ -959,7 +970,7 @@ class FSConfigGen(BaseGenerator): common = base_set & oem_set if len(common) > 0: if common: emsg = 'Following AID Collisions detected for: \n' for friendly in common: base = base_friendly[friendly] Loading Loading @@ -1234,11 +1245,12 @@ class PasswdGen(BaseGenerator): aids = parser.aids # nothing to do if no aids defined if len(aids) == 0: if not aids: return for aid in aids: if required_prefix is None or aid.friendly.startswith(required_prefix): if required_prefix is None or aid.friendly.startswith( required_prefix): self._print_formatted_line(aid) else: sys.exit("%s: AID '%s' must start with '%s'" % Loading Loading @@ -1294,6 +1306,7 @@ class GroupGen(PasswdGen): print "%s::%s:" % (logon, uid) @generator('print') class PrintGen(BaseGenerator): """Prints just the constants and values, separated by spaces, in an easy to Loading
tools/fs_config/test_fs_config_generator.py +20 −18 Original line number Diff line number Diff line Loading @@ -45,19 +45,21 @@ class Tests(unittest.TestCase): def test_aid(self): """Test AID class constructor""" aid = AID('AID_FOO_BAR', '0xFF', 'myfakefile') self.assertEquals(aid.identifier, 'AID_FOO_BAR') self.assertEquals(aid.value, '0xFF') self.assertEquals(aid.found, 'myfakefile') self.assertEquals(aid.normalized_value, '255') self.assertEquals(aid.friendly, 'foo_bar') aid = AID('AID_MEDIA_EX', '1234', 'myfakefile') self.assertEquals(aid.identifier, 'AID_MEDIA_EX') self.assertEquals(aid.value, '1234') self.assertEquals(aid.found, 'myfakefile') self.assertEquals(aid.normalized_value, '1234') self.assertEquals(aid.friendly, 'mediaex') aid = AID('AID_FOO_BAR', '0xFF', 'myfakefile', '/system/bin/sh') self.assertEqual(aid.identifier, 'AID_FOO_BAR') self.assertEqual(aid.value, '0xFF') self.assertEqual(aid.found, 'myfakefile') self.assertEqual(aid.normalized_value, '255') self.assertEqual(aid.friendly, 'foo_bar') self.assertEqual(aid.login_shell, '/system/bin/sh') aid = AID('AID_MEDIA_EX', '1234', 'myfakefile', '/vendor/bin/sh') self.assertEqual(aid.identifier, 'AID_MEDIA_EX') self.assertEqual(aid.value, '1234') self.assertEqual(aid.found, 'myfakefile') self.assertEqual(aid.normalized_value, '1234') self.assertEqual(aid.friendly, 'mediaex') self.assertEqual(aid.login_shell, '/vendor/bin/sh') def test_aid_header_parser_good(self): """Test AID Header Parser good input file""" Loading Loading @@ -265,9 +267,9 @@ class Tests(unittest.TestCase): dirs = parser.dirs aids = parser.aids self.assertEquals(len(files), 1) self.assertEquals(len(dirs), 1) self.assertEquals(len(aids), 1) self.assertEqual(len(files), 1) self.assertEqual(len(dirs), 1) self.assertEqual(len(aids), 1) aid = aids[0] fcap = files[0] Loading @@ -275,14 +277,14 @@ class Tests(unittest.TestCase): self.assertEqual(fcap, FSConfig('0777', 'AID_FOO', 'AID_SYSTEM', '(1ULL << CAP_BLOCK_SUSPEND)', 'CAP_MASK_LONG(CAP_BLOCK_SUSPEND)', '/system/bin/file', temp_file.name)) self.assertEqual(dcap, FSConfig('0777', 'AID_FOO', 'AID_SYSTEM', '(0)', '/vendor/path/dir/', temp_file.name)) self.assertEqual(aid, AID('AID_OEM1', '0x1389', temp_file.name)) self.assertEqual(aid, AID('AID_OEM1', '0x1389', temp_file.name, '/vendor/bin/sh')) def test_fs_config_file_parser_bad(self): """Test FSConfig Parser bad input file""" Loading