Best Python code snippet using autotest_python
initsidquery.py
Source:initsidquery.py  
1# Copyright 2014, Tresys Technology, LLC2#3# SPDX-License-Identifier: GPL-2.0-only4#5import os6import unittest7from setools import InitialSIDQuery8from .policyrep.util import compile_policy9class InitialSIDQueryTest(unittest.TestCase):10    @classmethod11    def setUpClass(cls):12        cls.p = compile_policy("tests/initsidquery.conf")13    @classmethod14    def tearDownClass(cls):15        os.unlink(cls.p.path)16    def test_000_unset(self):17        """Initial SID query with no criteria"""18        # query with no parameters gets all SIDs.19        sids = sorted(self.p.initialsids())20        q = InitialSIDQuery(self.p)21        q_sids = sorted(q.results())22        self.assertListEqual(sids, q_sids)23    def test_001_name_exact(self):24        """Initial SID query with exact match"""25        q = InitialSIDQuery(self.p, name="kernel", name_regex=False)26        sids = sorted(str(s) for s in q.results())27        self.assertListEqual(["kernel"], sids)28    def test_002_name_regex(self):29        """Initial SID query with regex match"""30        q = InitialSIDQuery(self.p, name="(security|unlabeled)", name_regex=True)31        sids = sorted(str(s) for s in q.results())32        self.assertListEqual(["security", "unlabeled"], sids)33    def test_010_user_exact(self):34        """Initial SID query with context user exact match"""35        q = InitialSIDQuery(self.p, user="user10", user_regex=False)36        sids = sorted(str(s) for s in q.results())37        self.assertListEqual(["fs"], sids)38    def test_011_user_regex(self):39        """Initial SID query with context user regex match"""40        q = InitialSIDQuery(self.p, user="user11(a|b)", user_regex=True)41        sids = sorted(str(s) for s in q.results())42        self.assertListEqual(["file", "file_labels"], sids)43    def test_020_role_exact(self):44        """Initial SID query with context role exact match"""45        q = InitialSIDQuery(self.p, role="role20_r", role_regex=False)46        sids = sorted(str(s) for s in q.results())47        self.assertListEqual(["any_socket"], sids)48    def test_021_role_regex(self):49        """Initial SID query with context role regex match"""50        q = InitialSIDQuery(self.p, role="role21(a|c)_r", role_regex=True)51        sids = sorted(str(s) for s in q.results())52        self.assertListEqual(["netmsg", "port"], sids)53    def test_030_type_exact(self):54        """Initial SID query with context type exact match"""55        q = InitialSIDQuery(self.p, type_="type30", type_regex=False)56        sids = sorted(str(s) for s in q.results())57        self.assertListEqual(["node"], sids)58    def test_031_type_regex(self):59        """Initial SID query with context type regex match"""60        q = InitialSIDQuery(self.p, type_="type31(b|c)", type_regex=True)61        sids = sorted(str(s) for s in q.results())62        self.assertListEqual(["icmp_socket", "tcp_socket"], sids)63    def test_040_range_exact(self):64        """Initial SID query with context range exact match"""65        q = InitialSIDQuery(self.p, range_="s0:c1 - s0:c0.c4")66        sids = sorted(str(s) for s in q.results())67        self.assertListEqual(["sysctl_modprobe"], sids)68    def test_041_range_overlap1(self):69        """Initial SID query with context range overlap match (equal)"""70        q = InitialSIDQuery(self.p, range_="s1:c1 - s1:c0.c4", range_overlap=True)71        sids = sorted(str(s) for s in q.results())72        self.assertListEqual(["sysctl"], sids)73    def test_041_range_overlap2(self):74        """Initial SID query with context range overlap match (subset)"""75        q = InitialSIDQuery(self.p, range_="s1:c1,c2 - s1:c0.c3", range_overlap=True)76        sids = sorted(str(s) for s in q.results())77        self.assertListEqual(["sysctl"], sids)78    def test_041_range_overlap3(self):79        """Initial SID query with context range overlap match (superset)"""80        q = InitialSIDQuery(self.p, range_="s1 - s1:c0.c4", range_overlap=True)81        sids = sorted(str(s) for s in q.results())82        self.assertListEqual(["sysctl"], sids)83    def test_041_range_overlap4(self):84        """Initial SID query with context range overlap match (overlap low level)"""85        q = InitialSIDQuery(self.p, range_="s1 - s1:c1,c2", range_overlap=True)86        sids = sorted(str(s) for s in q.results())87        self.assertListEqual(["sysctl"], sids)88    def test_041_range_overlap5(self):89        """Initial SID query with context range overlap match (overlap high level)"""90        q = InitialSIDQuery(self.p, range_="s1:c1,c2 - s1:c0.c4", range_overlap=True)91        sids = sorted(str(s) for s in q.results())92        self.assertListEqual(["sysctl"], sids)93    def test_042_range_subset1(self):94        """Initial SID query with context range subset match"""95        q = InitialSIDQuery(self.p, range_="s2:c1,c2 - s2:c0.c3", range_overlap=True)96        sids = sorted(str(s) for s in q.results())97        self.assertListEqual(["sysctl_fs"], sids)98    def test_042_range_subset2(self):99        """Initial SID query with context range subset match (equal)"""100        q = InitialSIDQuery(self.p, range_="s2:c1 - s2:c1.c3", range_overlap=True)101        sids = sorted(str(s) for s in q.results())102        self.assertListEqual(["sysctl_fs"], sids)103    def test_043_range_superset1(self):104        """Initial SID query with context range superset match"""105        q = InitialSIDQuery(self.p, range_="s3 - s3:c0.c4", range_superset=True)106        sids = sorted(str(s) for s in q.results())107        self.assertListEqual(["sysctl_kernel"], sids)108    def test_043_range_superset2(self):109        """Initial SID query with context range superset match (equal)"""110        q = InitialSIDQuery(self.p, range_="s3:c1 - s3:c1.c3", range_superset=True)111        sids = sorted(str(s) for s in q.results())112        self.assertListEqual(["sysctl_kernel"], sids)113    def test_044_range_proper_subset1(self):114        """Initial SID query with context range proper subset match"""115        q = InitialSIDQuery(self.p, range_="s4:c1,c2", range_subset=True, range_proper=True)116        sids = sorted(str(s) for s in q.results())117        self.assertListEqual(["sysctl_net"], sids)118    def test_044_range_proper_subset2(self):119        """Initial SID query with context range proper subset match (equal)"""120        q = InitialSIDQuery(self.p, range_="s4:c1 - s4:c1.c3", range_subset=True, range_proper=True)121        sids = sorted(str(s) for s in q.results())122        self.assertListEqual([], sids)123    def test_044_range_proper_subset3(self):124        """Initial SID query with context range proper subset match (equal low only)"""125        q = InitialSIDQuery(self.p, range_="s4:c1 - s4:c1.c2", range_subset=True, range_proper=True)126        sids = sorted(str(s) for s in q.results())127        self.assertListEqual(["sysctl_net"], sids)128    def test_044_range_proper_subset4(self):129        """Initial SID query with context range proper subset match (equal high only)"""130        q = InitialSIDQuery(self.p,131                            range_="s4:c1,c2 - s4:c1.c3", range_subset=True, range_proper=True)132        sids = sorted(str(s) for s in q.results())133        self.assertListEqual(["sysctl_net"], sids)134    def test_045_range_proper_superset1(self):135        """Initial SID query with context range proper superset match"""136        q = InitialSIDQuery(self.p, range_="s5 - s5:c0.c4", range_superset=True, range_proper=True)137        sids = sorted(str(s) for s in q.results())138        self.assertListEqual(["sysctl_net_unix"], sids)139    def test_045_range_proper_superset2(self):140        """Initial SID query with context range proper superset match (equal)"""141        q = InitialSIDQuery(self.p,142                            range_="s5:c1 - s5:c1.c3", range_superset=True, range_proper=True)143        sids = sorted(str(s) for s in q.results())144        self.assertListEqual([], sids)145    def test_045_range_proper_superset3(self):146        """Initial SID query with context range proper superset match (equal low)"""147        q = InitialSIDQuery(self.p,148                            range_="s5:c1 - s5:c1.c4", range_superset=True, range_proper=True)149        sids = sorted(str(s) for s in q.results())150        self.assertListEqual(["sysctl_net_unix"], sids)151    def test_045_range_proper_superset4(self):152        """Initial SID query with context range proper superset match (equal high)"""153        q = InitialSIDQuery(self.p, range_="s5 - s5:c1.c3", range_superset=True, range_proper=True)154        sids = sorted(str(s) for s in q.results())...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!!
