Ejemplo n.º 1
0
class zzzTestRuleDisableIPV6(RuleTest):
    def setUp(self):
        RuleTest.setUp(self)
        self.rule = DisableIPV6(self.config, self.environ, self.logdispatch,
                                self.statechglogger)
        self.logger = self.logdispatch
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        self.checkUndo = True

    def tearDown(self):
        pass

    def runTest(self):
        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''Configure system for the unit test

        :param self: essential if you override this definition
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        success = True
        if self.environ.getosfamily() == "linux":
            success = self.setLinuxConditions()
        elif self.environ.getosfamily() == "darwin":
            success = self.setMacConditions()
        return success

    def setLinuxConditions(self):
        success = True
        self.ph = Pkghelper(self.logger, self.environ)
        if not self.messupNetconfigFile():
            success = False
        if not self.messupSysctl():
            success = False
        if not self.messupModprobeFiles():
            success = False
        if not self.messupInterfaceFile():
            success = False
        if self.ph.manager == "apt-get":
            if not self.messupSSHDFile():
                success = False
        return success

    def setMacConditions(self):
        success = True
        debug = ""
        networksetup = "/usr/sbin/networksetup"
        listnetworkservices = networksetup + " -listallnetworkservices"
        ipv6status = "^IPv6:\s+On"
        getinfo = networksetup + " -getinfo"
        self.ch.executeCommand(listnetworkservices)
        retcode = self.ch.getReturnCode()
        if retcode != 0:
            success = False
            debug = "Failed to get list of network services"
            self.logger.log(LogPriority.DEBUG, debug)
        else:
            networkservices = self.ch.getOutput()
            for ns in networkservices:
                # ignore non-network service output lines
                if re.search("denotes that", ns, re.IGNORECASE):
                    continue
                else:
                    self.ch.executeCommand(networksetup + ' -setv6automatic ' +
                                           '"' + ns + '"')
                    retcode = self.ch.getReturnCode()
                    if retcode != 0:
                        success = False
                        debug = "Failed to get information for network service: " + ns
                        self.logger.log(LogPriority.DEBUG, debug)
        return success

    def messupNetconfigFile(self):
        success = True
        # stig portion, check netconfig file for correct contents
        if self.ph.manager == "apt-get":
            nfspkg = "nfs-common"
        else:
            nfspkg = "nfs-utils.x86_64"
        if self.ph.check(nfspkg):
            if not self.ph.remove(nfspkg):
                success = False
                debug = "Unable to remove nfs package for preconditions"
                self.logger.log(LogPriority.DEBUG, debug)
        if os.path.exists("/etc/netconfig"):
            item1 = "udp6 tpi_clts v inet6 udp - -"
            item2 = "tcp6 tpi_cots_ord v inet6 tcp - -"
            item1found, item2found, fixFile = False, False, False
            writestring = ""
            contents = readFile("/etc/netconfig", self.logger)
            for line in contents:
                writestring += line
                line = re.sub("\s+", " ", line.strip())
                if re.search(item1, line):
                    item1found = True
                if re.search(item2, line):
                    item2found = True
            if not item1found:
                writestring += item1
                fixFile = True
            if not item2found:
                writestring += item2
                fixFile = True
            if fixFile:
                if not writeFile("/etc/netconfig", writestring, self.logger):
                    success = False
                    debug = "Unable tomess up /etc/netconfig file for preconditions"
                    self.logger.log(LogPriority.DEBUG, debug)
        return success

    def messupSysctl(self):
        success = True
        sysctlcmd = ""
        sysctl = "/etc/sysctl.conf"
        directives = [
            "net.ipv6.conf.all.disable_ipv6=0",
            "net.ipv6.conf.default.disable_ipv6=0"
        ]
        filedirectives = {
            "net.ipv6.conf.all.disable_ipv6": "0",
            "net.ipv6.conf.default.disable_ipv6": "0"
        }
        tmpfile = sysctl + ".tmp"

        if os.path.exists(sysctl):
            editor = KVEditorStonix(self.statechglogger, self.logger, "conf",
                                    sysctl, tmpfile, filedirectives, "present",
                                    "openeq")
            if not editor.report():
                if not editor.fix():
                    success = False
                    debug = "Unable to mess up " + sysctl + " file for preconditions"
                    self.logger.log(LogPriority.DEBUG, debug)
                elif not editor.commit():
                    success = False
                    debug = "Unable to mess up " + sysctl + " file for preconditions"
                    self.logger.log(LogPriority.DEBUG, debug)
        sysctllocs = ["/sbin/sysctl", "/usr/sbin/sysctl"]
        for loc in sysctllocs:
            if os.path.exists(loc):
                sysctlcmd = loc

        if sysctlcmd:
            for d in directives:
                setbadopt = sysctlcmd + " -w " + d
                self.ch.executeCommand(setbadopt)
                retcode = self.ch.getReturnCode()
                if retcode != 0:
                    success = False
                    debug = "Failed to write configuration change: " + d + "\n"
                    self.logger.log(LogPriority.DEBUG, debug)
        else:
            debug = "sysctl command not found on system\n"
            self.logger.log(LogPriority.DEBUG, debug)
            success = False
        return success

    def messupModprobeFiles(self):
        success = True
        modprobes = {
            "options": "ipv6 disable=1",
            "install": "ipv6 /bin/true",
            "helloworld": ""
        }
        if os.path.exists("/etc/modprobe.d/"):
            modprobefiles = glob.glob("/etc/modprobe.d/*")
            for modfile in modprobefiles:
                tmpfile = modfile + ".tmp"
                editor = KVEditorStonix(self.statechglogger, self.logger,
                                        "conf", modfile, tmpfile, modprobes,
                                        "notpresent", "space")
                if not editor.report():
                    if not editor.fix():
                        success = False
                        debug = "Unable to mess up " + modfile + " file for preconditions"
                        self.logger.log(LogPriority.DEBUG, debug)
                    elif not editor.commit():
                        success = False
                        debug = "Unable to mess up " + modfile + " file for preconditions"
                        self.logger.log(LogPriority.DEBUG, debug)
        return success

    def messupInterfaceFile(self):
        success = True
        interface = {"IPV6INIT": '"yes"', "NETWORKING_IPV6": '"yes"'}
        # Check for existence of interface and network files to be configured
        if self.ph.manager == "yum":
            ifacefile = "/etc/sysconfig/network-scripts/"
            if not os.path.exists(ifacefile):
                ifacefile = ""
            netwrkfile = "/etc/sysconfig/network"
            if not os.path.exists(netwrkfile):
                netwrkfile = ""
        elif self.ph.manager == "zypper":
            ifacefile = "/etc/sysconfig/network/"
            if not os.path.exists(ifacefile):
                ifacefile = ""
        if ifacefile:
            dirs = glob.glob(ifacefile + "*")
            for loc in dirs:
                contents = []
                if re.search('^' + ifacefile + 'ifcfg', loc):
                    tmpfile = loc + ".tmp"
                    editor = KVEditorStonix(self.statechglogger, self.logger,
                                            "conf", loc, tmpfile, interface,
                                            "present", "closedeq")
                    if not editor.report():
                        if not editor.fix():
                            success = False
                            debug = "Unable to mess up " + loc + " file for preconditions"
                            self.logger.log(LogPriority.DEBUG, debug)
                        elif not editor.commit():
                            success = False
                            debug = "Unable to mess up " + loc + " file for preconditions"
                            self.logger.log(LogPriority.DEBUG, debug)
        return success

    def messupSSHDFile(self):
        success = True
        sshfile = "/etc/ssh/sshd_config"
        if os.path.exists(sshfile):
            tmpfile = sshfile + ".tmp"
            data = {"AddressFamily": "inet"}
            editor = KVEditorStonix(self.statechglogger, self.logger, "conf",
                                    sshfile, tmpfile, data, "notpresent",
                                    "space")
            if not editor.report():
                if not editor.fix():
                    success = False
                    debug = "Unable to mess up " + sshfile + " file for preconditions"
                    self.logger.log(LogPriority.DEBUG, debug)
                elif not editor.commit():
                    success = False
                    debug = "Unable to mess up " + sshfile + " file for preconditions"
                    self.logger.log(LogPriority.DEBUG, debug)
        return success

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''check on whether report was correct

        :param self: essential if you override this definition
        :param pCompliance: the self.iscompliant value of rule
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG, "pCompliance = " + \
                             str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " + \
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''check on whether fix was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " + \
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''check on whether undo was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " + \
                             str(pRuleSuccess) + ".")
        success = True
        return success
Ejemplo n.º 2
0
class zzzTestRuleDisablePrelinking(RuleTest):

    def setUp(self):
        RuleTest.setUp(self)
        self.rule = DisablePrelinking(self.config,
                                      self.environ,
                                      self.logdispatch,
                                      self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        self.ph = Pkghelper(self.logdispatch, self.environ)
        self.prelinkInstalled = False

    def tearDown(self):
        if not self.prelinkInstalled:
            self.ph.remove("prelink")

    def runTest(self):
        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''Configure system for the unit test

        :param self: essential if you override this definition
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        success = True
        if self.ph.check("prelink"):
            self.prelinkInstalled = True
        elif self.ph.checkAvailable("prelink"):
            self.ph.install("prelink")
        else:
            return True
        path = "/usr/sbin/prelink"
        cmd = [path, "/bin/ls"]
        if os.path.exists(path):
            self.ch.executeCommand(cmd)

        if re.search("debian|ubuntu", self.environ.getostype().lower()):
            path = "/etc/default/prelink"
        else:
            path = "/etc/sysconfig/prelink"
        if os.path.exists(path):
            tmppath = path + ".tmp"
            data = {"PRELINKING": "yes"}
            self.editor = KVEditorStonix(self.statechglogger, self.logdispatch,
                                         "conf", path, tmppath,
                                         data, "present", "closedeq")
            if not self.editor.report():
                if self.editor.fix():
                    if not self.editor.commit():
                        success = False
                        self.logdispatch.log(LogPriority.ERROR,
                                             "KVEditor failed to commit.")
                else:
                    success = False
                    self.logdispatch.log(LogPriority.ERROR,
                                         "KVEditor failed to fix.")
        else:
            writeFile(path, "PRELINKING=yes", self.logdispatch)

        return success

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''check on whether report was correct

        :param self: essential if you override this definition
        :param pCompliance: the self.iscompliant value of rule
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG, "pCompliance = " +
                             str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''check on whether fix was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''check on whether undo was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success
Ejemplo n.º 3
0
class zzzTestRuleSecureMTA(RuleTest):
    def setUp(self):
        RuleTest.setUp(self)
        self.rule = SecureMTA(self.config, self.environ, self.logdispatch,
                              self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        if self.environ.operatingsystem == "Mac OS X":
            self.isMac = True
        else:
            self.isMac = False
        if not self.isMac:
            self.ph = Pkghelper(self.logdispatch, self.environ)
            self.origState = [False, False, False, False]

            self.smPath = "/etc/mail/sendmail.cf"
            self.smTmp = "/tmp/" + os.path.split(self.smPath)[1] + ".utmp"
            self.pfPathlist = [
                '/etc/postfix/main.cf', '/private/etc/postfix/main.cf',
                '/usr/lib/postfix/main.cf'
            ]
            self.pfPath = ""
            for path in self.pfPathlist:
                if os.path.exists(path):
                    self.pfPath = path
            if self.pfPath == "":
                self.pfPath = "/etc/postfix/main.cf"
            self.pfTmp = "/tmp/" + os.path.split(self.pfPath)[1] + ".utmp"

    def tearDown(self):
        if os.path.exists(self.smTmp):
            os.rename(self.smTmp, self.smPath)
        if os.path.exists(self.pfTmp):
            os.rename(self.pfTmp, self.pfPath)

    def runTest(self):
        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''
        Configure system for the unit test
        @param self: essential if you override this definition
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        success = True
        # origState variables are not currently used
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.origState[0] = True
            if self.ph.check("postfix"):
                self.origState[1] = True
            if os.path.exists(self.smPath):
                self.origState[2] = True
                os.rename(self.smPath, self.smTmp)
            if os.path.exists(self.pfPath):
                self.origState[3] = True
                os.rename(self.pfPath, self.pfTmp)
        return success

    def testFalseFalseFalseFalse(self):
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.ph.remove("sendmail")
            if self.ph.check("postfix"):
                self.ph.remove("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueFalseFalseFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if self.ph.check("postfix"):
                self.ph.remove("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueTrueFalseFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueTrueTrueFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if not os.path.exists(self.smPath):
                open(self.smPath, "w")
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueTrueTrueTrue(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if not os.path.exists(self.smPath):
                open(self.smPath, "w")
            if not os.path.exists(self.pfPath):
                open(self.pfPath, "w")
            self.simpleRuleTest()

    def testTrueFalseTrueFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if self.ph.check("postfix"):
                self.ph.remove("postfix")
            if not os.path.exists(self.smPath):
                open(self.smPath, "w")
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testFalseTrueFalseFalse(self):
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.ph.remove("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testFalseTrueFalseTrue(self):
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.ph.remove("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if not os.path.exists(self.pfPath):
                open(self.pfPath, "w")
            self.simpleRuleTest()

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''
        check on whether report was correct
        @param self: essential if you override this definition
        @param pCompliance: the self.iscompliant value of rule
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG,
                             "pCompliance = " + str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''
        check on whether fix was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''
        check on whether undo was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success
Ejemplo n.º 4
0
class zzzTestRuleDisableUbuntuDataCollection(RuleTest):
    def setUp(self):
        '''


        :returns: None
        @author: ekkehard j. koch, Breen Malmberg

        '''

        RuleTest.setUp(self)
        self.rule = DisableUbuntuDataCollection(self.config, self.environ,
                                                self.logdispatch,
                                                self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ph = Pkghelper(self.logdispatch, self.environ)
        self.datacollectionpkgs = [
            "popularity-contest", "apport", "ubuntu-report"
        ]
        self.teardownpkgs = []

    def tearDown(self):
        '''


        :returns: None

        '''

        for pkg in self.teardownpkgs:
            self.ph.remove(pkg)
            self.teardownpkgs.remove(pkg)

    def runTest(self):
        '''


        :returns: None

        '''

        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''Configure system for the unit test


        :returns: success

        :rtype: bool
@author: ekkehard j. koch, Breen Malmberg

        '''

        success = True
        self.rule.enabledCI.updatecurrvalue(True)

        for pkg in self.datacollectionpkgs:
            if not self.ph.check(pkg):
                self.ph.install(pkg)
                self.teardownpkgs.append(pkg)
        return success

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''check on whether report was correct

        :param pCompliance: the self.iscompliant value of rule
        :param pRuleSuccess: did report run successfully
        :returns: success
        :rtype: bool
@author: ekkehard j. koch

        '''

        self.logdispatch.log(LogPriority.DEBUG,
                             "pCompliance = " + str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''check on whether fix was correct

        :param pRuleSuccess: did report run successfully
        :returns: success
        :rtype: bool
@author: ekkehard j. koch

        '''

        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''check on whether undo was correct

        :param pRuleSuccess: did report run successfully
        :returns: success
        :rtype: bool
@author: ekkehard j. koch

        '''

        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success
Ejemplo n.º 5
0
class zzzTestRuleSecureMTA(RuleTest):

    def setUp(self):
        RuleTest.setUp(self)
        self.rule = SecureMTA(self.config,
                              self.environ,
                              self.logdispatch,
                              self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        if self.environ.operatingsystem == "Mac OS X":
            self.isMac = True
        else:
            self.isMac = False
        if not self.isMac:
            self.ph = Pkghelper(self.logdispatch, self.environ)
            self.origState = [False, False, False, False]

            self.smPath = "/etc/mail/sendmail.cf"
            self.smTmp = "/tmp/" + os.path.split(self.smPath)[1] + ".utmp"
            self.pfPathlist = ['/etc/postfix/main.cf',
                               '/private/etc/postfix/main.cf',
                               '/usr/lib/postfix/main.cf']
            self.pfPath = ""
            for path in self.pfPathlist:
                if os.path.exists(path):
                    self.pfPath = path
            if self.pfPath == "":
                self.pfPath = "/etc/postfix/main.cf"
            self.pfTmp = "/tmp/" + os.path.split(self.pfPath)[1] + ".utmp"

    def tearDown(self):
        if not self.isMac:
            if self.origState[0] is True and not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            elif self.origState[0] is False and self.ph.check("sendmail"):
                self.ph.remove("sendmail")

            if self.origState[1] is True and not self.ph.check("postfix"):
                self.ph.install("postfix")
            elif self.origState[1] is False and self.ph.check("postfix"):
                self.ph.remove("postfix")

            if self.origState[2] is True and os.path.exists(self.smTmp):
                smDir = os.path.split(self.smPath)[0]
                if not os.path.exists(smDir):
                    os.makedirs(smDir)
                os.rename(self.smTmp, self.smPath)
            elif self.origState[2] is False and os.path.exists(self.smPath):
                os.remove(self.smPath)

            if self.origState[3] is True and os.path.exists(self.pfTmp):
                pfDir = os.path.split(self.pfPath)[0]
                if not os.path.exists(pfDir):
                    os.makedirs(pfDir)
                os.rename(self.pfTmp, self.pfPath)
            elif self.origState[3] is False and os.path.exists(self.pfPath):
                os.remove(self.pfPath)

    def runTest(self):
        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''
        Configure system for the unit test
        @param self: essential if you override this definition
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        success = True
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.origState[0] = True
            if self.ph.check("postfix"):
                self.origState[1] = True
            if os.path.exists(self.smPath):
                self.origState[2] = True
                os.rename(self.smPath, self.smTmp)
            if os.path.exists(self.pfPath):
                self.origState[3] = True
                os.rename(self.pfPath, self.pfTmp)
        return success

    def testFalseFalseFalseFalse(self):
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.ph.remove("sendmail")
            if self.ph.check("postfix"):
                self.ph.remove("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueFalseFalseFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if self.ph.check("postfix"):
                self.ph.remove("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueTrueFalseFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueTrueTrueFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if not os.path.exists(self.smPath):
                open(self.smPath, "w")
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testTrueTrueTrueTrue(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if not os.path.exists(self.smPath):
                open(self.smPath, "w")
            if not os.path.exists(self.pfPath):
                open(self.pfPath, "w")
            self.simpleRuleTest()

    def testTrueFalseTrueFalse(self):
        if not self.isMac:
            if not self.ph.check("sendmail"):
                self.ph.install("sendmail")
            if self.ph.check("postfix"):
                self.ph.remove("postfix")
            if not os.path.exists(self.smPath):
                open(self.smPath, "w")
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testFalseTrueFalseFalse(self):
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.ph.remove("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if os.path.exists(self.pfPath):
                os.remove(self.pfPath)
            self.simpleRuleTest()

    def testFalseTrueFalseTrue(self):
        if not self.isMac:
            if self.ph.check("sendmail"):
                self.ph.remove("sendmail")
            if not self.ph.check("postfix"):
                self.ph.install("postfix")
            if os.path.exists(self.smPath):
                os.remove(self.smPath)
            if not os.path.exists(self.pfPath):
                open(self.pfPath, "w")
            self.simpleRuleTest()

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''
        check on whether report was correct
        @param self: essential if you override this definition
        @param pCompliance: the self.iscompliant value of rule
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pCompliance = " +
                             str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''
        check on whether fix was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''
        check on whether undo was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success
Ejemplo n.º 6
0
class zzzTestRuleSecureMTA(RuleTest):
    def setUp(self):
        RuleTest.setUp(self)
        self.rule = SecureMTA(self.config, self.environ, self.logdispatch,
                              self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.logger = self.logdispatch
        self.ch = CommandHelper(self.logdispatch)
        if self.environ.operatingsystem == "Mac OS X":
            self.isMac = True
        else:
            self.isMac = False

    def tearDown(self):
        pass
#         if not self.isMac:
#             if self.origState[0] is True and not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             elif self.origState[0] is False and self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
#
#             if self.origState[1] is True and not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             elif self.origState[1] is False and self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
#
#             if self.origState[2] is True and os.path.exists(self.smTmp):
#                 smDir = os.path.split(self.smPath)[0]
#                 if not os.path.exists(smDir):
#                     os.makedirs(smDir)
#                 os.rename(self.smTmp, self.smPath)
#             elif self.origState[2] is False and os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#
#             if self.origState[3] is True and os.path.exists(self.pfTmp):
#                 pfDir = os.path.split(self.pfPath)[0]
#                 if not os.path.exists(pfDir):
#                     os.makedirs(pfDir)
#                 os.rename(self.pfTmp, self.pfPath)
#             elif self.origState[3] is False and os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)

    def runTest(self):
        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''Configure system for the unit test

        :param self: essential if you override this definition
        :returns: boolean - If successful True; If failure False
        @author: dwalker

        '''
        success = True
        pfdata = {
            'inet_interfaces': 'localhost',
            'default_process_limit': '100',
            'smtpd_client_connection_count_limit': '10',
            'smtpd_client_connection_rate_limit': '30',
            'queue_minfree': '20971520',
            'header_size_limit': '51200',
            'message_size_limit': '10485760',
            'smtpd_recipient_limit': '100',
            'smtpd_banner': '$myhostname ESMTP',
            'mynetworks_style': 'host',
            'smtpd_recipient_restrictions':
            'permit_mynetworks, reject_unauth_destination',
            'relayhost': MAILRELAYSERVER
        }
        if not self.isMac:
            smdata = {
                "O SmtpGreetingMessage": "",
                "O PrivacyOptions": "goaway"
            }
            self.ph = Pkghelper(self.logdispatch, self.environ)
            self.origState = [False, False, False, False]
            self.smPath = "/etc/mail/sendmail.cf"
            self.smTmp = self.smPath + ".stonixUT"
            self.pfPathlist = [
                '/etc/postfix/main.cf', '/usr/lib/postfix/main.cf'
            ]
            self.postfixpkg = "postfix"
            self.pfPath = ""
            for path in self.pfPathlist:
                if os.path.exists(path):
                    self.pfPath = path
                    break
            self.pfTmp = self.pfPath + ".stonixUT"
            #if postfix file exists, remove any correct contents
            if self.pfPath:
                self.postfixed = KVEditorStonix(self.statechglogger,
                                                self.logger, "conf",
                                                self.pfPath, self.pfTmp,
                                                pfdata, "notpresent", "openeq")
                if not self.postfixed.report():
                    if self.postfixed.removeables:
                        print("kveditor has removeables\n")
                        if not self.postfixed.fix():
                            success = False
            if os.path.exists(self.smPath):
                self.sndmailed = KVEditorStonix(self.statechglogger,
                                                self.logger, "conf",
                                                self.smPath, self.smTmp,
                                                smdata, "notpresent",
                                                "closedeq")
                if not self.sndmailed.report():
                    if self.postfixed.removeables:
                        if not self.postfixed.fix():
                            success = False

            #remove postfix if installed
            if self.ph.check("postfix"):
                if not self.ph.remove("postfix"):
                    success = False
            #remove sendmail if installed
            if not self.ph.checkAvailable("postfix"):
                if self.ph.check("sendmail"):
                    if not self.ph.remove("sendmail"):
                        success = False
        else:
            self.pfPath = "/private/etc/postfix/main.cf"
            self.pfTmp = self.pfPath + ".stonixUT"
            if os.path.exists(self.pfPath):
                self.postfixed = KVEditorStonix(self.statechglogger,
                                                self.logger, "conf",
                                                self.pfPath, self.pfTmp,
                                                pfdata, "notpresent", "openeq")
                if not self.postfixed.report():
                    if self.postfixed.removeables:
                        if not self.postfixed.fix():
                            success = False
#             if self.ph.check("sendmail"):
#                 self.origState[0] = True
#             if self.ph.check(self.postfixpkg):
#                 self.origState[1] = True
#             if os.path.exists(self.smPath):
#                 self.origState[2] = True
#                 os.rename(self.smPath, self.smTmp)
#             if os.path.exists(self.pfPath):
#                 self.origState[3] = True
#                 os.rename(self.pfPath, self.pfTmp)
        return success


#     def testFalseFalseFalseFalse(self):
#         if not self.isMac:
#             if self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
#             if self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
#
#     def testTrueFalseFalseFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
#
#     def testTrueTrueFalseFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
#
#     def testTrueTrueTrueFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if not os.path.exists(self.smPath):
#                 splitpath = self.smPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.smPath, "w")
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
#
#     def testTrueTrueTrueTrue(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if not os.path.exists(self.smPath):
#                 splitpath = self.smPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.smPath, "w")
#             if not os.path.exists(self.pfPath):
#                 splitpath = self.pfPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.pfPath, "w")
#             self.simpleRuleTest()
#
#     def testTrueFalseTrueFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
#             if not os.path.exists(self.smPath):
#                 splitpath = self.smPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.smPath, "w")
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
#
#     def testFalseTrueFalseFalse(self):
#         if not self.isMac:
#             if self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
#
#     def testFalseTrueFalseTrue(self):
#         if not self.isMac:
#             if self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if not os.path.exists(self.pfPath):
#                 splitpath = self.pfPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.pfPath, "w")
#             self.simpleRuleTest()

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''check on whether report was correct

        :param self: essential if you override this definition
        :param pCompliance: the self.iscompliant value of rule
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG,
                             "pCompliance = " + str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''check on whether fix was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''check on whether undo was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: ekkehard j. koch

        '''
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success
class zzzTestRuleDisableUbuntuDataCollection(RuleTest):

    def setUp(self):
        '''

        @return: None
        @author: ekkehard j. koch, Breen Malmberg
        '''

        RuleTest.setUp(self)
        self.rule = DisableUbuntuDataCollection(self.config, self.environ, self.logdispatch, self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ph = Pkghelper(self.logdispatch, self.environ)
        self.datacollectionpkgs = ["popularity-contest", "apport", "ubuntu-report"]
        self.teardownpkgs = []

    def tearDown(self):
        '''

        @return: None
        '''

        for pkg in self.teardownpkgs:
            self.ph.remove(pkg)
            self.teardownpkgs.remove(pkg)

    def runTest(self):
        '''

        @return: None
        '''

        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''
        Configure system for the unit test

        @return: success
        @rtype: bool
        @author: ekkehard j. koch, Breen Malmberg
        '''

        success = True
        self.rule.enabledCI.updatecurrvalue(True)

        for pkg in self.datacollectionpkgs:
            if not self.ph.check(pkg):
                self.ph.install(pkg)
                self.teardownpkgs.append(pkg)
        return success

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''
        check on whether report was correct

        @param pCompliance: the self.iscompliant value of rule
        @param pRuleSuccess: did report run successfully
        @return: success
        @rtype: bool
        @author: ekkehard j. koch
        '''

        self.logdispatch.log(LogPriority.DEBUG, "pCompliance = " + str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''
        check on whether fix was correct

        @param pRuleSuccess: did report run successfully
        @return: success
        @rtype: bool
        @author: ekkehard j. koch
        '''

        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''
        check on whether undo was correct

        @param pRuleSuccess: did report run successfully
        @return: success
        @rtype: bool
        @author: ekkehard j. koch
        '''

        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success
Ejemplo n.º 8
0
class zzzTestRuleSystemAccounting(RuleTest):
    def setUp(self):
        RuleTest.setUp(self)
        self.rule = SystemAccounting(self.config, self.environ,
                                     self.logdispatch, self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        self.ph = Pkghelper(self.logdispatch, self.environ)
        self.rule.ci.updatecurrvalue(True)

    def tearDown(self):
        pass

    def runTest(self):
        result = self.simpleRuleTest()
        self.assertTrue(
            result, "SystemAccounting(9): rule.iscompliant() is " +
            "'False' after rule.fix() and rule.report() have " +
            "run. This may be due to a proxy error; if the " +
            "proper proxy is not set in localize.py, set it and " +
            "run this test again.")

    def test_default_sysstat_empty(self):
        """
        test correction of /etc/default/sysstat if it has no entry in it

        :return:
        """

        file = "/etc/default/sysstat"
        backup = "/etc/default/sysstat.stonix_test_bak"

        if os.path.isfile(file):
            self._backup_file(file)
            f = open(file, "w")
            f.write("")
            f.close()

            self.rule._set_paths()
            self.assertFalse(self.rule._report_configuration())
            self.rule._fix_configuration()
            self.assertTrue(self.rule._report_configuration())

            self._restore_file(backup)
        else:
            return True

    def test_default_sysstat_comment(self):
        """
        test correction of /etc/default/sysstat if it has the entry commented out

        :return:
        """

        file = "/etc/default/sysstat"
        backup = "/etc/default/sysstat.stonix_test_bak"

        if os.path.isfile(file):
            self._backup_file(file)
            f = open(file, "w")
            f.write('# ENABLED="true"')
            f.close()

            self.rule._set_paths()
            self.assertFalse(self.rule._report_configuration())
            self.rule._fix_configuration()
            self.assertTrue(self.rule._report_configuration())

            self._restore_file(backup)
        else:
            return True

    def test_default_sysstat_wrongvalue(self):
        """
        test correction of /etc/default/sysstat if it has the entry set to the wrong value

        :return:
        """

        file = "/etc/default/sysstat"
        backup = "/etc/default/sysstat.stonix_test_bak"

        if os.path.isfile(file):
            self._backup_file(file)
            f = open(file, "w")
            f.write('ENABLED="false"')
            f.close()

            self.rule._set_paths()
            self.assertFalse(self.rule._report_configuration())
            self.rule._fix_configuration()
            self.assertTrue(self.rule._report_configuration())

            self._restore_file(backup)
        else:
            return True

    def test_default_sysstat_rightvalue(self):
        """
        test correction of /etc/default/sysstat if it has the entry set to the right value

        :return:
        """

        file = "/etc/default/sysstat"
        backup = "/etc/default/sysstat.stonix_test_bak"

        if os.path.isfile(file):
            self._backup_file(file)
            f = open(file, "w")
            f.write('ENABLED="true"')
            f.close()

            self.rule._set_paths()
            self.assertTrue(self.rule._report_configuration())
            self.rule._fix_configuration()
            self.assertTrue(self.rule._report_configuration())

            self._restore_file(backup)
        else:
            return True

    def test_installation_installed(self):
        """
        test installation report/fix if package already installed
        applies to Linux only

        :return:
        """

        if self.rule.ostype == "Mac OS X":
            return True

        package = "sysstat"

        if self.ph.check(package):
            self.rule._set_paths()
            self.assertTrue(self.rule._report_installation())
            self.rule._fix_installation()
            self.assertTrue(self.rule._report_installation())
        else:
            return True

    def test_installation_missing(self):
        """
        test installation report/fix if package not installed
        applies to Linux only

        :return:
        """

        if self.rule.ostype == "Mac OS X":
            return True

        package = "sysstat"

        if not self.ph.check(package):
            self.rule._set_paths()
            self.assertFalse(self.rule._report_installation())
            self.rule._fix_installation()
            self.assertTrue(self.rule._report_installation())
        else:
            return True

    def test_set_paths(self):
        """
        test that all paths and necessary variables for the class are able to be properly
        determined and set once package is installed

        :return:
        """

        package = "sysstat"

        self.ph.install(package)

        self.rule._set_paths()

        self.assertTrue(self.rule.sysstat_package)
        self.assertTrue(self.rule.sysstat_service_file)
        self.assertTrue(self.rule.sa1)
        self.assertTrue(self.rule.sa2)
        self.assertTrue(self.rule.sysstat_service_contents)
        self.assertTrue(self.rule.sysstat_cron_contents)
        self.assertTrue(self.rule.ostype)

        self.ph.remove(package)

    def _restore_file(self, backup):
        """

        :param backup:
        :return:
        """

        if os.path.isfile(backup):
            if re.search("\.stonix_test_bak", backup):
                shutil.copy2(backup, backup.replace(".stonix_test_bak", ""))

    def _backup_file(self, original):
        """

        :param original:
        :return:
        """

        if os.path.isfile(original):
            shutil.copy2(original, original + ".stonix_test_bak")

    def setConditionsForRule(self):
        """Configure system for the unit test

        :param self: essential if you override this definition
        :returns: boolean - If successful True; If failure False
        @author: Breen Malmberg

        """

        success = True
        self.rule.ci.updatecurrvalue(True)
        return success

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        """check on whether report was correct

        :param self: essential if you override this definition
        :param pCompliance: the self.iscompliant value of rule
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: Breen Malmberg

        """
        self.logdispatch.log(LogPriority.DEBUG,
                             "pCompliance = " + str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        """check on whether fix was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: Breen Malmberg

        """
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        """check on whether undo was correct

        :param self: essential if you override this definition
        :param pRuleSuccess: did report run successfully
        :returns: boolean - If successful True; If failure False
        @author: Breen Malmberg

        """
        self.logdispatch.log(LogPriority.DEBUG,
                             "pRuleSuccess = " + str(pRuleSuccess) + ".")
        success = True
        return success
class zzzTestRuleDisablePrelinking(RuleTest):

    def setUp(self):
        RuleTest.setUp(self)
        self.rule = DisablePrelinking(self.config,
                                      self.environ,
                                      self.logdispatch,
                                      self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        self.ph = Pkghelper(self.logdispatch, self.environ)
        self.prelinkInstalled = False

    def tearDown(self):
        if not self.prelinkInstalled:
            self.ph.remove("prelink")

    def runTest(self):
        self.simpleRuleTest()

    def setConditionsForRule(self):
        '''
        Configure system for the unit test
        @param self: essential if you override this definition
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        success = True
        if self.ph.check("prelink"):
            self.prelinkInstalled = True
        elif self.ph.checkAvailable("prelink"):
            self.ph.install("prelink")
        else:
            return True
        path = "/usr/sbin/prelink"
        cmd = [path, "/bin/ls"]
        if os.path.exists(path):
            self.ch.executeCommand(cmd)

        if re.search("debian|ubuntu", self.environ.getostype().lower()):
            path = "/etc/default/prelink"
        else:
            path = "/etc/sysconfig/prelink"
        if os.path.exists(path):
            tmppath = path + ".tmp"
            data = {"PRELINKING": "yes"}
            self.editor = KVEditorStonix(self.statechglogger, self.logdispatch,
                                         "conf", path, tmppath,
                                         data, "present", "closedeq")
            if not self.editor.report():
                if self.editor.fix():
                    if not self.editor.commit():
                        success = False
                        self.logdispatch.log(LogPriority.ERROR,
                                             "KVEditor failed to commit.")
                else:
                    success = False
                    self.logdispatch.log(LogPriority.ERROR,
                                         "KVEditor failed to fix.")
        else:
            writeFile(path, "PRELINKING=yes", self.logdispatch)

        return success

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''
        check on whether report was correct
        @param self: essential if you override this definition
        @param pCompliance: the self.iscompliant value of rule
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pCompliance = " +
                             str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''
        check on whether fix was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''
        check on whether undo was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success
Ejemplo n.º 10
0
class zzzTestRuleSecureMTA(RuleTest):

    def setUp(self):
        RuleTest.setUp(self)
        self.rule = SecureMTA(self.config,
                              self.environ,
                              self.logdispatch,
                              self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.logger = self.logdispatch
        self.ch = CommandHelper(self.logdispatch)
        if self.environ.operatingsystem == "Mac OS X":
            self.isMac = True
        else:
            self.isMac = False

    def tearDown(self):
        pass
#         if not self.isMac:
#             if self.origState[0] is True and not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             elif self.origState[0] is False and self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
# 
#             if self.origState[1] is True and not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             elif self.origState[1] is False and self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
# 
#             if self.origState[2] is True and os.path.exists(self.smTmp):
#                 smDir = os.path.split(self.smPath)[0]
#                 if not os.path.exists(smDir):
#                     os.makedirs(smDir)
#                 os.rename(self.smTmp, self.smPath)
#             elif self.origState[2] is False and os.path.exists(self.smPath):
#                 os.remove(self.smPath)
# 
#             if self.origState[3] is True and os.path.exists(self.pfTmp):
#                 pfDir = os.path.split(self.pfPath)[0]
#                 if not os.path.exists(pfDir):
#                     os.makedirs(pfDir)
#                 os.rename(self.pfTmp, self.pfPath)
#             elif self.origState[3] is False and os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
    def runTest(self):
        self.simpleRuleTest()
    def setConditionsForRule(self):
        '''
        Configure system for the unit test
        @param self: essential if you override this definition
        @return: boolean - If successful True; If failure False
        @author: dwalker
        '''
        success = True
        pfdata = {'inet_interfaces': 'localhost',
                'default_process_limit': '100',
                'smtpd_client_connection_count_limit': '10',
                'smtpd_client_connection_rate_limit': '30',
                'queue_minfree': '20971520',
                'header_size_limit': '51200',
                'message_size_limit': '10485760',
                'smtpd_recipient_limit': '100',
                'smtpd_banner': '$myhostname ESMTP',
                'mynetworks_style': 'host',
                'smtpd_recipient_restrictions':
                'permit_mynetworks, reject_unauth_destination',
                'relayhost': MAILRELAYSERVER}
        if not self.isMac:
            smdata = {"O SmtpGreetingMessage": "",
                      "O PrivacyOptions": "goaway"}
            self.ph = Pkghelper(self.logdispatch, self.environ)
            self.origState = [False, False, False, False]
            self.smPath = "/etc/mail/sendmail.cf"
            self.smTmp = self.smPath + ".stonixUT"
            self.pfPathlist = ['/etc/postfix/main.cf',
                               '/usr/lib/postfix/main.cf']
            self.postfixpkg = "postfix"
            self.pfPath = ""
            for path in self.pfPathlist:
                if os.path.exists(path):
                    self.pfPath = path
                    break
            self.pfTmp = self.pfPath + ".stonixUT"
            #if postfix file exists, remove any correct contents
            if self.pfPath:
                self.postfixed = KVEditorStonix(self.statechglogger,
                    self.logger, "conf", self.pfPath, self.pfTmp,
                    pfdata, "notpresent", "openeq")
                if not self.postfixed.report():
                    if self.postfixed.removeables:
                        print "kveditor has removeables\n"
                        if not self.postfixed.fix():
                            success = False
            if os.path.exists(self.smPath):
                self.sndmailed = KVEditorStonix(self.statechglogger, self.logger,
                    "conf", self.smPath, self.smTmp, smdata, "notpresent",
                    "closedeq")
                if not self.sndmailed.report():
                    if self.postfixed.removeables:
                        if not self.postfixed.fix():
                            success = False

            #remove postfix if installed
            if self.ph.check("postfix"):
                if not self.ph.remove("postfix"):
                    success = False
            #remove sendmail if installed
            if not self.ph.checkAvailable("postfix"):
                if self.ph.check("sendmail"):
                    if not self.ph.remove("sendmail"):
                        success = False
        else:
            self.pfPath = "/private/etc/postfix/main.cf"
            self.pfTmp = self.pfPath + ".stonixUT"
            if os.path.exists(self.pfPath):
                self.postfixed = KVEditorStonix(self.statechglogger,
                    self.logger, "conf", self.pfPath, self.pfTmp,
                    pfdata, "notpresent", "openeq")
                if not self.postfixed.report():
                    if self.postfixed.removeables:
                        if not self.postfixed.fix():
                            success = False
#             if self.ph.check("sendmail"):
#                 self.origState[0] = True
#             if self.ph.check(self.postfixpkg):
#                 self.origState[1] = True
#             if os.path.exists(self.smPath):
#                 self.origState[2] = True
#                 os.rename(self.smPath, self.smTmp)
#             if os.path.exists(self.pfPath):
#                 self.origState[3] = True
#                 os.rename(self.pfPath, self.pfTmp)
        return success
#     def testFalseFalseFalseFalse(self):
#         if not self.isMac:
#             if self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
#             if self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
# 
#     def testTrueFalseFalseFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
# 
#     def testTrueTrueFalseFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
# 
#     def testTrueTrueTrueFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if not os.path.exists(self.smPath):
#                 splitpath = self.smPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.smPath, "w")
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
# 
#     def testTrueTrueTrueTrue(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if not os.path.exists(self.smPath):
#                 splitpath = self.smPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.smPath, "w")
#             if not os.path.exists(self.pfPath):
#                 splitpath = self.pfPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.pfPath, "w")
#             self.simpleRuleTest()
# 
#     def testTrueFalseTrueFalse(self):
#         if not self.isMac:
#             if not self.ph.check("sendmail"):
#                 self.ph.install("sendmail")
#             if self.ph.check(self.postfixpkg):
#                 self.ph.remove(self.postfixpkg)
#             if not os.path.exists(self.smPath):
#                 splitpath = self.smPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.smPath, "w")
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
# 
#     def testFalseTrueFalseFalse(self):
#         if not self.isMac:
#             if self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if os.path.exists(self.pfPath):
#                 os.remove(self.pfPath)
#             self.simpleRuleTest()
# 
#     def testFalseTrueFalseTrue(self):
#         if not self.isMac:
#             if self.ph.check("sendmail"):
#                 self.ph.remove("sendmail")
#             if not self.ph.check(self.postfixpkg):
#                 self.ph.install(self.postfixpkg)
#             if os.path.exists(self.smPath):
#                 os.remove(self.smPath)
#             if not os.path.exists(self.pfPath):
#                 splitpath = self.pfPath.split('/')
#                 del splitpath[-1]
#                 subdir = "/".join(splitpath)
#                 if not os.path.exists(subdir):
#                     os.makedirs(subdir, 0755)
#                 open(self.pfPath, "w")
#             self.simpleRuleTest()

    def checkReportForRule(self, pCompliance, pRuleSuccess):
        '''
        check on whether report was correct
        @param self: essential if you override this definition
        @param pCompliance: the self.iscompliant value of rule
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pCompliance = " +
                             str(pCompliance) + ".")
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkFixForRule(self, pRuleSuccess):
        '''
        check on whether fix was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success

    def checkUndoForRule(self, pRuleSuccess):
        '''
        check on whether undo was correct
        @param self: essential if you override this definition
        @param pRuleSuccess: did report run successfully
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        self.logdispatch.log(LogPriority.DEBUG, "pRuleSuccess = " +
                             str(pRuleSuccess) + ".")
        success = True
        return success