Best Python code snippet using autotest_python
testrunner.py
Source:testrunner.py
1#!/usr/bin/python2import sys3sys.path.append('../src')4import unittest5import SELinux_CTS6from SELinux_CTS import SELinuxPolicy7policy_file_name = 'policy_test.conf'8types = set([9 'bluetooth',10 'healthd',11 'healthd_exec',12 'testTYPE' ]) #testTYPE added for neverallow rule to make sense13attributes = {14 'domain': set(['bluetooth', 'healthd', 'testTYPE']),15 'unconfineddomain': set(['bluetooth']),16 'appdomain': set(['bluetooth', 'testTYPE']),17 'file_type': set(['healthd_exec']),18 'exec_type': set(['healthd_exec']) }19common_classes = {20 'file': set([21 'ioctl',22 'read',23 'write',24 'create',25 'getattr',26 'setattr',27 'lock',28 'relabelfrom',29 'relabelto',30 'append',31 'unlink',32 'link',33 'rename',34 'execute',35 'swapon',36 'quotaon',37 'mounton' ]) }38classes = {39 'capability': set([40 'chown',41 'dac_override',42 'dac_read_search',43 'fowner',44 'fsetid',45 'kill',46 'setgid',47 'setuid',48 'setpcap',49 'linux_immutable',50 'net_bind_service',51 'net_broadcast',52 'net_admin',53 'net_raw',54 'ipc_lock',55 'ipc_owner',56 'sys_module',57 'sys_rawio',58 'sys_chroot',59 'sys_ptrace',60 'sys_pacct',61 'sys_admin',62 'sys_boot',63 'sys_nice',64 'sys_resource',65 'sys_time',66 'sys_tty_config',67 'mknod',68 'lease',69 'audit_write',70 'audit_control',71 'setfcap' ]),72 'file': (set([73 'execute_no_trans',74 'entrypoint',75 'execmod',76 'open',77 'audit_access' ]) | common_classes['file']) }78# allow healthd healthd_exec:file { entrypoint read execute };79allow_rules = [80 { 'source_types': {81 'set': set([82 'healthd']),83 'flags': { 'complement': False } },84 'target_types': {85 'set': set([86 'healthd_exec']),87 'flags': { 'complement': False } },88 'classes': {89 'set': set([90 'file']),91 'flags': { 'complement': False } },92 'permissions': {93 'set': set([94 'entrypoint',95 'read',96 'execute' ]),97 'flags': { 'complement': False } } } ]98# neverallow { appdomain -unconfineddomain -bluetooth } self:capability *;99neverallow_rules = [100 { 'source_types': {101 'set': set([102 'appdomain',103 '-unconfineddomain',104 '-bluetooth' ]),105 'flags': { 'complement': False } },106 'target_types': {107 'set': set([108 'self']),109 'flags': { 'complement': False } },110 'classes': {111 'set': set([112 'capability']),113 'flags': { 'complement': False } },114 'permissions': {115 'set': set([116 '*' ]),117 'flags': { 'complement': False } } } ]118expected_final_allow_list = [119 [ ('healthd', 'healthd_exec', 'file', 'entrypoint'),120 ('healthd', 'healthd_exec', 'file', 'read'),121 ('healthd', 'healthd_exec', 'file', 'execute') ] ]122expected_final_neverallow_list = [123 [ ('testTYPE', 'testTYPE', 'capability', 'chown'),124 ('testTYPE', 'testTYPE', 'capability', 'dac_override'),125 ('testTYPE', 'testTYPE', 'capability', 'dac_read_search'),126 ('testTYPE', 'testTYPE', 'capability', 'fowner'),127 ('testTYPE', 'testTYPE', 'capability', 'fsetid'),128 ('testTYPE', 'testTYPE', 'capability', 'kill'),129 ('testTYPE', 'testTYPE', 'capability', 'setgid'),130 ('testTYPE', 'testTYPE', 'capability', 'setuid'),131 ('testTYPE', 'testTYPE', 'capability', 'setpcap'),132 ('testTYPE', 'testTYPE', 'capability', 'linux_immutable'),133 ('testTYPE', 'testTYPE', 'capability', 'net_bind_service'),134 ('testTYPE', 'testTYPE', 'capability', 'net_broadcast'),135 ('testTYPE', 'testTYPE', 'capability', 'net_admin'),136 ('testTYPE', 'testTYPE', 'capability', 'net_raw'),137 ('testTYPE', 'testTYPE', 'capability', 'ipc_lock'),138 ('testTYPE', 'testTYPE', 'capability', 'ipc_owner'),139 ('testTYPE', 'testTYPE', 'capability', 'sys_module'),140 ('testTYPE', 'testTYPE', 'capability', 'sys_rawio'),141 ('testTYPE', 'testTYPE', 'capability', 'sys_chroot'),142 ('testTYPE', 'testTYPE', 'capability', 'sys_ptrace'),143 ('testTYPE', 'testTYPE', 'capability', 'sys_pacct'),144 ('testTYPE', 'testTYPE', 'capability', 'sys_admin'),145 ('testTYPE', 'testTYPE', 'capability', 'sys_boot'),146 ('testTYPE', 'testTYPE', 'capability', 'sys_nice'),147 ('testTYPE', 'testTYPE', 'capability', 'sys_resource'),148 ('testTYPE', 'testTYPE', 'capability', 'sys_time'),149 ('testTYPE', 'testTYPE', 'capability', 'sys_tty_config'),150 ('testTYPE', 'testTYPE', 'capability', 'mknod'),151 ('testTYPE', 'testTYPE', 'capability', 'lease'),152 ('testTYPE', 'testTYPE', 'capability', 'audit_write'),153 ('testTYPE', 'testTYPE', 'capability', 'audit_control'),154 ('testTYPE', 'testTYPE', 'capability', 'setfcap') ] ]155class SELinuxPolicyTests(unittest.TestCase):156 def setUp(self):157 self.test_policy = SELinuxPolicy()158 self.test_file = open(policy_file_name, 'r')159 self.test_policy.types = types160 self.test_policy.attributes = attributes161 self.test_policy.common_classes = common_classes162 self.test_policy.classes = classes163 self.test_policy.allow_rules = allow_rules164 self.test_policy.neverallow_rules = neverallow_rules165 return166 def testExpandAvcRule(self):167 #TODO: add more examples here to cover different cases168 expanded_allow_list = SELinux_CTS.expand_avc_rule(self.test_policy, self.test_policy.allow_rules[0])169 for a in expected_final_allow_list[0]:170 self.failUnless(a in expanded_allow_list)171 expanded_neverallow_list = SELinux_CTS.expand_avc_rule(self.test_policy, self.test_policy.neverallow_rules[0])172 for n in expected_final_neverallow_list[0]:173 self.failUnless(n in expanded_neverallow_list)174 def testExpandBrackets(self):175 #test position without bracket:176 self.test_file.seek(279)177 self.failIf(SELinux_CTS.expand_brackets(self.test_file))178 #test position with bracket:179 self.test_file.seek(26123)180 self.failUnless(SELinux_CTS.expand_brackets(self.test_file) == " entrypoint read execute ")181 #test position with nested brackets:182 self.test_file.seek(26873)183 self.failUnless(SELinux_CTS.expand_brackets(self.test_file)184 == " dir chr_file blk_file file lnk_file sock_file fifo_file ")185 def testGetAvcRuleComponent(self):186 #test against normal ('allow healthd healthd_exec:file ...)187 self.test_file.seek(26096)188 normal_src = { 'flags': { 'complement': False },189 'set': set(['healthd']) }190 normal_tgt = { 'flags': { 'complement': False },191 'set': set(['healthd_exec']) }192 normal_class = { 'flags': { 'complement': False },193 'set': set(['file']) }194 normal_perm = { 'flags': { 'complement': False },195 'set': set(['entrypoint', 'read', 'execute']) }196 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)197 == normal_src)198 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)199 == normal_tgt)200 c = SELinux_CTS.advance_past_whitespace(self.test_file)201 if c == ':':202 self.test_file.read(1)203 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)204 == normal_class)205 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)206 == normal_perm)207 #test against 'hard' ('init {fs_type ...' )208 self.test_file.seek(26838)209 hard_src = { 'flags': { 'complement': False },210 'set': set(['init']) }211 hard_tgt = { 'flags': { 'complement': False },212 'set': set(['fs_type', 'dev_type', 'file_type']) }213 hard_class = { 'flags': { 'complement': False },214 'set': set(['dir', 'chr_file', 'blk_file', 'file', 'lnk_file', 'sock_file', 'fifo_file']) }215 hard_perm = { 'flags': { 'complement': False },216 'set': set(['relabelto']) }217 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)218 == hard_src)219 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)220 == hard_tgt)221 #mimic ':' check:222 c = SELinux_CTS.advance_past_whitespace(self.test_file)223 if c == ':':224 self.test_file.read(1)225 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)226 == hard_class)227 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)228 == hard_perm)229 #test against 'multi-line' ('init {fs_type ...' )230 self.test_file.seek(26967)231 multi_src = { 'flags': { 'complement': False },232 'set': set(['appdomain', '-unconfineddomain']) }233 multi_tgt = { 'flags': { 'complement': False },234 'set': set(['audio_device', 'camera_device', 'dm_device', 'radio_device', 'gps_device', 'rpmsg_device']) }235 multi_class = { 'flags': { 'complement': False },236 'set': set(['chr_file']) }237 multi_perm = { 'flags': { 'complement': False },238 'set': set(['read', 'write']) }239 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)240 == multi_src)241 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)242 == multi_tgt)243 c = SELinux_CTS.advance_past_whitespace(self.test_file)244 if c == ':':245 self.test_file.read(1)246 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)247 == multi_class)248 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)249 == multi_perm)250 #test against 'complement'251 self.test_file.seek(26806)252 complement = { 'flags': { 'complement': True },253 'set': set(['entrypoint', 'relabelto']) }254 self.failUnless(SELinux_CTS.get_avc_rule_component(self.test_file)255 == complement)256 def testGetLineType(self):257 self.failUnless(SELinux_CTS.get_line_type('type bluetooth, domain;')258 == SELinux_CTS.TYPE)259 self.failUnless(SELinux_CTS.get_line_type('attribute unconfineddomain;')260 == SELinux_CTS.ATTRIBUTE)261 self.failUnless(SELinux_CTS.get_line_type('typeattribute bluetooth appdomain;')262 == SELinux_CTS.TYPEATTRIBUTE)263 self.failUnless(SELinux_CTS.get_line_type('class file')264 == SELinux_CTS.CLASS)265 self.failUnless(SELinux_CTS.get_line_type('common file')266 == SELinux_CTS.COMMON)267 self.failUnless(SELinux_CTS.get_line_type('allow healthd healthd_exec:file { entrypoint read execute };')268 == SELinux_CTS.ALLOW_RULE)269 self.failUnless(SELinux_CTS.get_line_type('neverallow { appdomain -unconfineddomain -bluetooth } self:capability *;')270 == SELinux_CTS.NEVERALLOW_RULE)271 self.failUnless(SELinux_CTS.get_line_type('# FLASK')272 == SELinux_CTS.OTHER)273 def testIsMultiLine(self):274 self.failIf(SELinux_CTS.is_multi_line(SELinux_CTS.TYPE))275 self.failIf(SELinux_CTS.is_multi_line(SELinux_CTS.ATTRIBUTE))276 self.failIf(SELinux_CTS.is_multi_line(SELinux_CTS.TYPEATTRIBUTE))277 self.failUnless(SELinux_CTS.is_multi_line(SELinux_CTS.CLASS))278 self.failUnless(SELinux_CTS.is_multi_line(SELinux_CTS.COMMON))279 self.failUnless(SELinux_CTS.is_multi_line(SELinux_CTS.ALLOW_RULE))280 self.failUnless(SELinux_CTS.is_multi_line(SELinux_CTS.NEVERALLOW_RULE))281 self.failIf(SELinux_CTS.is_multi_line(SELinux_CTS.OTHER))282 def testProcessInheritsSegment(self):283 inherit_offset = 448 # needs changing if file changes284 self.test_file.seek(inherit_offset, 0)285 inherit_result = SELinux_CTS.process_inherits_segment(self.test_file)286 self.failUnless(inherit_result == 'file')287 return288 def testFromFileName(self):289 #using a special file, since the test_file has some lines which don't 'jive'290 clean_policy_file = 'policy_clean_test.conf'291 from_file_policy = SELinuxPolicy()292 from_file_policy.from_file_name(clean_policy_file)293 self.failUnless(from_file_policy.types == self.test_policy.types)294 self.failUnless(from_file_policy.attributes == self.test_policy.attributes)295 self.failUnless(from_file_policy.classes == self.test_policy.classes)296 self.failUnless(from_file_policy.common_classes == self.test_policy.common_classes)297 self.failUnless(from_file_policy.allow_rules == self.test_policy.allow_rules)298 self.failUnless(from_file_policy.neverallow_rules == self.test_policy.neverallow_rules)299 def testExpandPermissions(self):300 #test general case301 test_class_obj = 'file'302 general_set = set(['read', 'write', 'execute'])303 expanded_general_set = general_set304 self.failUnless(self.test_policy.expand_permissions(test_class_obj, general_set)305 == general_set)306 star_set = set(['*'])307 expanded_star_set = self.test_policy.classes['file'] #everything in the class308 self.failUnless(self.test_policy.expand_permissions(test_class_obj, star_set)309 == expanded_star_set)310 complement_set = set(['*', '-open'])311 expanded_complement_set = self.test_policy.classes['file'] - set(['open'])312 self.failUnless(self.test_policy.expand_permissions(test_class_obj, complement_set)313 == expanded_complement_set)314 def testExpandTypes(self):315 #test general case and '-' handling316 test_source_set = set([317 'domain',318 '-bluetooth' ])319 expanded_test_source_set = set([320 'healthd', 'testTYPE' ])321 self.failUnless(self.test_policy.expand_types(test_source_set) == expanded_test_source_set)322 #test '*' handling323 test_source_set = set([ '*' ])324 expanded_test_source_set = set([325 'bluetooth', 'healthd', 'testTYPE' ])326 self.failUnless(self.test_policy.expand_types(test_source_set) == types)327 #test - handling328 test_source_set = set([329 '*',330 '-bluetooth'])331 expanded_test_source_set = set([332 'healthd', 'healthd_exec', 'testTYPE' ])333 self.failUnless(self.test_policy.expand_types(test_source_set) == expanded_test_source_set)334 def testProcessAttributeLine(self):335 attribute_policy = SELinuxPolicy()336 #test with 'normal input'337 test_normal_string = 'attribute TEST_att;'338 test_attribute = 'TEST_att'339 attribute_policy.process_attribute_line(test_normal_string)340 self.failUnless( test_attribute in attribute_policy.attributes)341 #TODO: test on bogus inputs342 def testProcessClassLine(self):343 class_policy = SELinuxPolicy()344 #offsets need changing if test file changes345 common_offset = 279346 class_initial_offset = 212347 class_perm_offset = 437348 self.test_file.seek(common_offset, 0)349 line = self.test_file.readline()350 class_policy.process_common_line(line, self.test_file)351 self.test_file.seek(class_initial_offset, 0)352 line = self.test_file.readline()353 class_policy.process_class_line(line, self.test_file)354 self.failUnless('file' in class_policy.classes)355 self.test_file.seek(class_perm_offset, 0)356 line = self.test_file.readline()357 class_policy.process_class_line(line, self.test_file)358 self.failUnless(class_policy.classes['file'] == classes['file'])359 def testProcessCommonLine(self):360 common_policy = SELinuxPolicy()361 common_offset = 279 # needs changing if file changes362 self.test_file.seek(common_offset, 0)363 line = self.test_file.readline()364 common_policy.process_common_line(line, self.test_file)365 self.failUnless('file' in common_policy.common_classes )366 self.failUnless(common_policy.common_classes['file'] == common_classes['file'])367 def testProcessAvcRuleLine(self):368 avc_policy = SELinuxPolicy()369 allow_offset = 26091 # needs changing if file changes370 neverallow_offset = 26311 # needs changing if file changes371 self.test_file.seek(allow_offset, 0)372 line = self.test_file.readline()373 avc_policy.process_avc_rule_line(line, self.test_file)374 self.failUnless(avc_policy.allow_rules[0] == allow_rules[0] ) # always '0'?375 self.test_file.seek(neverallow_offset, 0)376 line = self.test_file.readline()377 avc_policy.process_avc_rule_line(line, self.test_file)378 self.failUnless(avc_policy.neverallow_rules[0] == neverallow_rules[0] ) # always '0'?379 def testProcessTypeLine(self):380 type_policy = SELinuxPolicy()381 test_normal_string = 'type TEST_type, TEST_att1, TEST_att2;'382 test_type = 'TEST_type'383 test_atts = ['TEST_att1', 'TEST_att2']384 #test with 'normal input'385 type_policy.process_type_line(test_normal_string)386 self.failUnless(test_type in type_policy.types)387 for a in test_atts:388 self.failUnless(a in type_policy.attributes)389 self.failUnless(test_type in type_policy.attributes[a])390 #TODO: test with domain only, no attributes391 # and test on bogus inputs392 def testProcessTypeattributeLine(self):393 typ_att_policy = SELinuxPolicy()394 test_normal_string = 'typeattribute TEST_type TEST_att1, TEST_att2;'395 test_type = 'TEST_type'396 test_atts = ['TEST_att1', 'TEST_att2']397 #test with 'normal input' (type should already be declared)398 typ_att_policy.process_type_line('type ' + test_type + ';')399 typ_att_policy.process_typeattribute_line(test_normal_string)400 self.failUnless(test_type in typ_att_policy.types)401 for a in test_atts:402 self.failUnless(a in typ_att_policy.attributes)403 self.failUnless(test_type in typ_att_policy.attributes[a])404 #TODO: test with domain only, no attributes405 # and test on bogus inputs406def main():407 unittest.main()408if __name__ == '__main__':...
pascalize.py
Source:pascalize.py
...54 Description: Test with a string with numerics in it.55 Arguments:56 """57 self.assertEqual(gen_libs.pascalize(self.data_str2), self.data_test2)58 def test_normal_string(self):59 """Function: test_normal_string60 Description: Test with a string with only characters.61 Arguments:62 """63 self.assertEqual(gen_libs.pascalize(self.data_str1), self.data_test1)64 def test_empty_string(self):65 """Function: test_empty_string66 Description: Test with an empty string.67 Arguments:68 """69 self.assertEqual(gen_libs.pascalize(self.data_str), self.data_test)70if __name__ == "__main__":...
test_encode.py
Source:test_encode.py
1# test_encode.py:2from encode import encode3def test_empty_string():4 assert encode("") == ""5def test_normal_string():6 assert encode("ad") == "ad"7def test_normal_string():8 assert encode("ad") == "ad"9def test_simple_compressed_string():10 assert encode("Aabb") == "Aab2"11 assert encode("AAb") == "A2b"12def test_encoding():...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!