class zzzTestRuleConfigureKerberos(RuleTest): def setUp(self): RuleTest.setUp(self) self.rule = ConfigureKerberos(self.config, self.environ, self.logdispatch, self.statechglogger) self.rulename = self.rule.rulename self.rulenumber = self.rule.rulenumber self.ch = CommandHelper(self.logdispatch) self.fh = FileHelper(self.logdispatch, self.statechglogger) if self.environ.getosfamily() == 'darwin': self.files = { "kerb5.conf": { "path": "/etc/krb5.conf", "remove": True, "content": None, "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "kerb5ut" }, "edu.mit.Kerberos": { "path": "/Library/Preferences/edu.mit.Kerberos", "remove": False, "content": "test", "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "Kerberosut" }, "edu.mit.Kerberos.krb5kdc.launchd": { "path": "/Library/Preferences/edu.mit.Kerberos.krb5kdc.launchd", "remove": False, "content": "test", "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "krb5kdcut" }, "edu.mit.Kerberos.kadmind.launchd": { "path": "/Library/Preferences/edu.mit.Kerberos.kadmind.launchd", "remove": False, "content": "test", "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "kadmindut" }, } else: self.files = { "kerb5.conf": { "path": "/etc/krb5.conf", "remove": True, "content": None, "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "kerb5ut" } } 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 ''' for filelabel, fileinfo in sorted(self.files.items()): self.fh.addFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"], fileinfo["eventid"]) success = self.fh.fixFiles() 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 zzzTestFrameworkfilehelper(unittest.TestCase): def setUp(self): self.environ = environment.Environment() self.environ.setverbosemode(True) self.logdispatch = logdispatcher.LogDispatcher(self.environ) self.state = StateChgLogger.StateChgLogger(self.logdispatch, self.environ) self.homedirectory = os.path.expanduser('~') self.fh = FileHelper(self.logdispatch, self.state) def tearDown(self): rmtree(self.homedirectory + "/temp") def test_create_file_and_remove(self): # Attempt to remove a file that does not exist self.logdispatch.log(LogPriority.DEBUG, "Adding non-existent file for deletion") addfilesuccess = self.fh.addFile("tf0", self.homedirectory + "/temp/tf0.txt", True, None, 0o444, os.getuid(), os.getegid()) if addfilesuccess: self.logdispatch.log(LogPriority.DEBUG, "Fixing non-existent file") self.assertTrue(self.fh.fixFiles(), "Fix failed for removing a non-existent file") # Create Files self.fh.removeAllFiles() self.files = {"tf3": {"path": self.homedirectory + "/temp/temp/temp/tf3.txt", "remove": False, "content": None, "permissions": 0o777, "owner": os.getuid(), "group": os.getegid()}, "tf2": {"path": self.homedirectory + "/temp/temp/tf2.txt", "remove": False, "content": "This is a test", "permissions": "0777", "owner": "root", "group": 20}, "tf1": {"path": self.homedirectory + "/temp/tf1.txt", "remove": False, "content": None, "permissions": None, "owner": None, "group": None} } for filelabel, fileinfo in sorted(self.files.items()): addfilereturn = self.fh.addFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not addfilereturn: addfilesuccess = False self.assertTrue(addfilesuccess, "Initial adding of Files to FileHelper failed!") filescreated = self.fh.fixFiles() self.assertTrue(filescreated, "1st creation of Files Failed!") # Remove Files without removing directories updatefilesuccess = True self.files["tf1"]["remove"] = True self.files["tf2"]["remove"] = True self.files["tf3"]["remove"] = True for filelabel, fileinfo in sorted(self.files.items()): updatefilereturn = self.fh.updateFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not updatefilereturn: updatefilesuccess = False self.assertTrue(updatefilesuccess, "1st updating of Files to FileHelper failed!") filesremoval = self.fh.fixFiles() self.assertTrue(filesremoval, "1st removal of Files Failed!") self.fh.setDefaultRemoveEmptyParentDirectories(True) # Remove Files without removing directories updatefilesuccess = True self.files["tf1"]["remove"] = False self.files["tf2"]["remove"] = False self.files["tf3"]["remove"] = False for filelabel, fileinfo in sorted(self.files.items()): updatefilereturn = self.fh.updateFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not updatefilereturn: updatefilesuccess = False filescreated = self.fh.fixFiles() self.assertTrue(filescreated, "2nd creation of Files Failed!") filesremoval = self.fh.fixFiles() # Remove Files with removing directories updatefilesuccess = True self.files["tf1"]["remove"] = True self.files["tf2"]["remove"] = True self.files["tf3"]["remove"] = True for filelabel, fileinfo in sorted(self.files.items()): updatefilereturn = self.fh.updateFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not updatefilereturn: updatefilesuccess = False self.assertTrue(updatefilesuccess, "2nd updating of Files to FileHelper failed!") filesremoval = self.fh.fixFiles() self.assertTrue(filesremoval, "2nd removal of Files Failed!")
class zzzTestFrameworkfilehelper(unittest.TestCase): def setUp(self): self.environ = environment.Environment() self.environ.setverbosemode(True) self.logdispatch = logdispatcher.LogDispatcher(self.environ) self.state = StateChgLogger.StateChgLogger(self.logdispatch, self.environ) self.homedirectory = os.path.expanduser('~') self.fh = FileHelper(self.logdispatch, self.state) def tearDown(self): rmtree(self.homedirectory + "/temp") def test_create_file_and_remove(self): # Create Files addfilesuccess = True self.fh.removeAllFiles() self.files = {"tf3": {"path": self.homedirectory + "/temp/temp/temp/tf3.txt", "remove": False, "content": None, "permissions": 0777, "owner": os.getuid(), "group": os.getegid()}, "tf2": {"path": self.homedirectory + "/temp/temp/tf2.txt", "remove": False, "content": "This is a test", "permissions": "0777", "owner": "root", "group": 20}, "tf1": {"path": self.homedirectory + "/temp/tf1.txt", "remove": False, "content": None, "permissions": None, "owner": None, "group": None} } for filelabel, fileinfo in sorted(self.files.items()): addfilereturn = self.fh.addFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not addfilereturn: addfilesuccess = False self.assertTrue(addfilesuccess, "Initial adding of Files to FileHelper failed!") filescreated = self.fh.fixFiles() self.assertTrue(filescreated, "1st creation of Files Failed!") # Remove Files without removing directories updatefilesuccess = True self.files["tf1"]["remove"] = True self.files["tf2"]["remove"] = True self.files["tf3"]["remove"] = True for filelabel, fileinfo in sorted(self.files.items()): updatefilereturn = self.fh.updateFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not updatefilereturn: updatefilesuccess = False self.assertTrue(updatefilesuccess, "1st updating of Files to FileHelper failed!") filesremoval = self.fh.fixFiles() self.assertTrue(filesremoval, "1st removal of Files Failed!") self.fh.setDefaultRemoveEmptyParentDirectories(True) # Remove Files without removing directories updatefilesuccess = True self.files["tf1"]["remove"] = False self.files["tf2"]["remove"] = False self.files["tf3"]["remove"] = False for filelabel, fileinfo in sorted(self.files.items()): updatefilereturn = self.fh.updateFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not updatefilereturn: updatefilesuccess = False filescreated = self.fh.fixFiles() self.assertTrue(filescreated, "2nd creation of Files Failed!") filesremoval = self.fh.fixFiles() # Remove Files with removing directories updatefilesuccess = True self.files["tf1"]["remove"] = True self.files["tf2"]["remove"] = True self.files["tf3"]["remove"] = True for filelabel, fileinfo in sorted(self.files.items()): updatefilereturn = self.fh.updateFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not updatefilereturn: updatefilesuccess = False self.assertTrue(updatefilesuccess, "2nd updating of Files to FileHelper failed!") filesremoval = self.fh.fixFiles() self.assertTrue(filesremoval, "2nd removal of Files Failed!")
class zzzTestRuleNetworkTuning(RuleTest): def setUp(self): RuleTest.setUp(self) self.rule = NetworkTuning(self.config, self.environ, self.logdispatch, self.statechglogger) self.rulename = self.rule.rulename self.rulenumber = self.rule.rulenumber self.fh = FileHelper(self.logdispatch) def tearDown(self): pass def runTest(self): self.simpleRuleTest() def setConditionsForRule(self): ''' Reset sysctl.conf to original @author: ekkehard j. koch ''' success = True if self.environ.getosfamily() == 'darwin': if success: self.files = { "sysctl.conf": { "path": "/private/etc/sysctl.conf", "remove": True, "content": None, "permissions": None, "owner": "root", "group": "wheel" } } for filelabel, fileinfo in sorted(self.files.items()): success = self.fh.addFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"]) if not success: break if success: success = self.fh.fixFiles() 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 zzzTestRuleSetDaemonUmask(RuleTest): def setUp(self): RuleTest.setUp(self) self.rule = SetDaemonUmask(self.config, self.environ, self.logdispatch, self.statechglogger) self.rulename = self.rule.rulename self.rulenumber = self.rule.rulenumber self.ch = CommandHelper(self.logdispatch) self.fh = FileHelper(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': if success: self.files = {"launchd.conf": {"path": "/etc/launchd.conf", "remove": True, "content": None, "permissions": None, "owner": "root", "group": "wheel"} } for filelabel, fileinfo in sorted(self.files.items()): success = self.fh.addFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"] ) if not success: break if success: success = self.fh.fixFiles() 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 zzzTestRuleConfigureKerberos(RuleTest): def setUp(self): RuleTest.setUp(self) self.rule = ConfigureKerberos(self.config, self.environ, self.logdispatch, self.statechglogger) self.rulename = self.rule.rulename self.rulenumber = self.rule.rulenumber self.ch = CommandHelper(self.logdispatch) self.fh = FileHelper(self.logdispatch, self.statechglogger) if self.environ.getosfamily() == 'darwin': self.files = {"kerb5.conf": {"path": "/etc/krb5.conf", "remove": True, "content": None, "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "kerb5ut"}, "edu.mit.Kerberos": {"path": "/Library/Preferences/edu.mit.Kerberos", "remove": False, "content": "test", "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "Kerberosut"}, "edu.mit.Kerberos.krb5kdc.launchd": {"path": "/Library/Preferences/edu.mit.Kerberos.krb5kdc.launchd", "remove": False, "content": "test", "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "krb5kdcut"}, "edu.mit.Kerberos.kadmind.launchd": {"path": "/Library/Preferences/edu.mit.Kerberos.kadmind.launchd", "remove": False, "content": "test", "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "kadmindut"}, } else: self.files = {"kerb5.conf": {"path": "/etc/krb5.conf", "remove": True, "content": None, "permissions": None, "owner": None, "group": None, "eventid": str(self.rulenumber).zfill(4) + "kerb5ut"}} 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 ''' for filelabel, fileinfo in sorted(self.files.items()): self.fh.addFile(filelabel, fileinfo["path"], fileinfo["remove"], fileinfo["content"], fileinfo["permissions"], fileinfo["owner"], fileinfo["group"], fileinfo["eventid"]) success = self.fh.fixFiles() 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