class zzzTestFrameworkServiceHelper(unittest.TestCase):

    def setUp(self):
        self.enviro = Environment()
        self.enviro.setdebugmode(False)
        self.logger = LogDispatcher(self.enviro)
        self.mysh = ServiceHelper(self.enviro, self.logger)
        self.myservice = 'crond'
        self.myservicename = ""
        if self.enviro.getosfamily() == 'darwin':
            self.myservice = "/System/Library/PrivateFrameworks/CalendarAgent.framework/Executables/CalendarAgent"
            self.myservicename = "com.apple.CalendarAgent"
        elif self.enviro.getosfamily() == 'solaris':
            self.myservice = 'svc:/system/cron:default'
        elif self.enviro.getosfamily() == 'freebsd':
            self.myservice = 'cron'
        elif os.path.exists('/usr/lib/systemd/system/cron.service'):
            self.myservice = 'cron.service'
        elif os.path.exists('/usr/lib/systemd/system/crond.service'):
            self.myservice = 'crond.service'
        elif os.path.exists('/etc/init.d/vixie-cron'):
            self.myservice = 'vixie-cron'
        elif os.path.exists('/etc/init.d/cron'):
            self.myservice = 'cron'

    def tearDown(self):
        pass

    def testListServices(self):
        svcslist = self.mysh.listservices()
        self.assertTrue(len(svcslist) > 0)

    def testDisableEnable(self):
        self.mysh.disableservice(self.myservice)
        auditresult = self.mysh.auditservice(self.myservice,
                                             self.myservicename)
        self.assertFalse(auditresult,
                         "Service not disabled or return from audit not valid")
        time.sleep(3)
        self.assertFalse(self.mysh.isrunning(self.myservice,
                                             self.myservicename),
                         "Service is still running or return from isrunning not valid")
        self.mysh.enableservice(self.myservice)
        self.assertTrue(self.mysh.auditservice(self.myservice,
                                               self.myservicename),
                        "Service not enabled or return from audit not valid")
        time.sleep(3)
        self.assertTrue(self.mysh.isrunning(self.myservice,
                                            self.myservicename),
                        "Service is not running or return from isrunning not valid")

    def testReloadService(self):
        self.assertTrue(self.mysh.reloadservice(self.myservice,
                                                self.myservicename),
                        'Service reload returned false')
class zzzTestRuleSecureMDNS(RuleTest):
    def setUp(self):
        RuleTest.setUp(self)
        self.rule = SecureMDNS(self.config, self.environ, self.logdispatch,
                               self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        self.dc = "/usr/bin/defaults"
        self.lc = "/bin/launchctl"
        self.plb = "/usr/libexec/PlistBuddy"
        self.sh = ServiceHelper(self.environ, self.logdispatch)

    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
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        success = True
        if self.environ.getosfamily() == "darwin":
            success = False
            osxversion = str(self.environ.getosver())
            if osxversion.startswith("10.10.0") or osxversion.startswith(
                    "10.10.1") or osxversion.startswith(
                        "10.10.2") or osxversion.startswith("10.10.3"):
                self.service = "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"
                self.servicename = "com.apple.networking.discoveryd"
                self.parameter = "--no-multicast"
                self.pbd = self.plb + ' -c "Delete :ProgramArguments: string ' + self.parameter + '" ' + self.service
                success = True
            else:
                self.service = "/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist"
                if osxversion.startswith("10.10"):
                    self.servicename = "com.apple.mDNSResponder.reloaded"
                    self.parameter = "-NoMulticastAdvertisements"
                else:
                    self.servicename = "com.apple.mDNSResponder"
                    self.parameter = "-NoMulticastAdvertisements"
                self.pbd = self.plb + ' -c "Delete :ProgramArguments: string ' + self.parameter + '" ' + self.service
                success = True


# This needs to be fixed
#            if success:
#                command = self.pbd
#                success = self.ch.executeCommand(command)
            if success:
                success = self.sh.reloadservice(self.service, self.servicename)
        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
class zzzTestRuleSecureMDNS(RuleTest):

    def setUp(self):
        RuleTest.setUp(self)
        self.rule = SecureMDNS(self.config,
                               self.environ,
                               self.logdispatch,
                               self.statechglogger)
        self.rulename = self.rule.rulename
        self.rulenumber = self.rule.rulenumber
        self.ch = CommandHelper(self.logdispatch)
        self.dc = "/usr/bin/defaults"
        self.lc = "/bin/launchctl"
        self.plb = "/usr/libexec/PlistBuddy"
        self.sh = ServiceHelper(self.environ, self.logdispatch)

    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
        @return: boolean - If successful True; If failure False
        @author: ekkehard j. koch
        '''
        success = True
        if self.environ.getosfamily() == "darwin":
            success = False
            osxversion = str(self.environ.getosver())
            if osxversion.startswith("10.10.0") or osxversion.startswith("10.10.1") or osxversion.startswith("10.10.2") or osxversion.startswith("10.10.3"):
                self.service = "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"
                self.servicename = "com.apple.networking.discoveryd"
                self.parameter = "--no-multicast"
                self.pbd =  self.plb + ' -c "Delete :ProgramArguments: string '  + self.parameter + '" ' +  self.service
                success = True
            else:
                self.service = "/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist"
                if osxversion.startswith("10.10"):
                    self.servicename = "com.apple.mDNSResponder.reloaded"
                    self.parameter = "-NoMulticastAdvertisements"
                else:
                    self.servicename = "com.apple.mDNSResponder"
                    self.parameter = "-NoMulticastAdvertisements"
                self.pbd =  self.plb + ' -c "Delete :ProgramArguments: string ' + self.parameter + '" ' +  self.service
                success = True
# This needs to be fixed
#            if success:
#                command = self.pbd
#                success = self.ch.executeCommand(command)
            if success:
                success = self.sh.reloadservice(self.service, self.servicename)
        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