def buildCreateUpdateEventReq(self, request_name, use_default_param=False, **kargs): req_str = SysUtils.getReqByName(request_name) #format request with default params(no default attendees) if use_default_param: param_dict = SysUtils.getDefaultParams(request_name) req_str = self.__createReqDefaultVals(req_str, param_dict) print param_dict #format request with user params req_str = self.__createReqFinalVals(req_str, kargs) req_str = self.__delEmptyReqFields( req_str) #empty all %s field except placeholder-%s for attendees #format with atandee, if there is any sub_req = '' if kargs.has_key('attendees'): node_req = SysUtils.getReqByName('EVENT_CREATE_ATTENDEE') attendee_param = SysUtils.getDefaultParams('EVENT_CREATE_ATTENDEE') for each_attendee in kargs['attendees']: node_req = self.__createReqDefaultVals(node_req, attendee_param) sub_req = sub_req + self.__createReqFinalVals( node_req, each_attendee) #format ctnr with sub_req(attendees) req_str = req_str % sub_req return req_str
def buildContactCreateUpdateReq(self, request_name, use_default_param=False, **param_dict): """ build request for create/update contact and vcard body """ sub_req_str = SysUtils.getReqByName("CONTACT_FIELD") ctnr_req_str = SysUtils.getReqByName(request_name) sub_req = "" req_str = ctnr_req_str # deal with contactFields if param_dict.has_key("contactFields"): for each_contact_field in param_dict["contactFields"]: sub_req = sub_req + self.__createReqDefaultVals(sub_req_str, each_contact_field) param_dict.pop("contactFields") # this param will be used later else: if use_default_param: # deal with contactFields format default_params_dict = SysUtils.getDefaultParams("CONTACT_CREATE") if default_params_dict.has_key("contactFields"): for each_contact_field in default_params_dict["contactFields"]: sub_req = sub_req + self.__createReqDefaultVals(sub_req_str, each_contact_field) sub_req = self.__delEmptyReqFields(sub_req) # no need for this,maybe # deal with common params # default params if use_default_param: default_params_dict = SysUtils.getDefaultParams("CONTACT_CREATE") if default_params_dict.has_key("contactFields"): default_params_dict.pop("contactFields") req_str = self.__createReqDefaultVals(ctnr_req_str, default_params_dict) # user params req_str = self.__createReqFinalVals(req_str, param_dict) # empty fields req_str = self.__delEmptyReqFields(req_str % sub_req) return req_str
def buildAutoReplyReq(self, request_name='MAIL_AUTO_REPLY_CPMS', use_default_param=False, endday='2', **kargs): req_str = SysUtils.getReqByName(request_name) if use_default_param: param_dict = SysUtils.getDefaultParams(request_name) req_str = self.__createReqDefaultVals(req_str, param_dict) # if not use_default_param: # param_dict['endDate'] = SysUtils.getEndTime(kargs['endday']) req_str = self.__createReqFinalVals(req_str, kargs) req_str = self.__delEmptyReqFields(req_str) sub_req = '' optionsNode = '' if kargs.has_key('option'): for opt in eval(kargs['option']): kargs_dict = opt sub_req = '<option name="%s" value="%s"/>' sub_req = sub_req % (kargs_dict['name'], kargs_dict['value']) optionsNode = optionsNode + sub_req #format with atandee, if there is any #format ctnr with sub_req(attendees) req_str = req_str % ('<options>' + optionsNode + '</options>') return req_str
def testFileRead(self): ## test existing file cont = SysUtils.readFileContent("content.txt") self.assertEqual(cont, "hello") ## test non-existing file cont = SysUtils.readFileContent("nosuchfile.txt") self.assertIsNone(cont)
def buildSafeSenderBatchRemoveReq(self, use_default_param=False, *blocksender): remove = '' for sender in blocksender: remove = remove + SysUtils.getReqByName( 'SAFE_SENDER_REMOVE_NODE') % sender return SysUtils.getReqByName('SAFE_SENDER_CTNR') % remove
def buildGetContactsReq(self, use_default_param=False, **kargs): sub_req = "" id_list = [] if kargs.has_key("id_list"): id_list = kargs["id_list"] kargs.pop("id_list") req_str = self.__createReqFinalVals(SysUtils.getReqByName("GET_CONTACT_CTNR"), kargs) for contact_id in id_list: sub_req = sub_req + SysUtils.getReqByName("GET_CONTACT_NODE") % contact_id return req_str % sub_req
def buildPreviewMergeReq(self, use_default_param=False, **kargs): # similar with buildGetContactsReq sub_req = "" id_list = [] if kargs.has_key("id_list"): id_list = kargs["id_list"] kargs.pop("id_list") req_str = self.__createReqFinalVals(SysUtils.getReqByName("PREVIEW_MERGE_CTNR"), kargs) for contact_id in id_list: sub_req = sub_req + SysUtils.getReqByName("PREVIEW_MERGE_NODE") % contact_id return req_str % sub_req
def buildGetContactsReq(self, use_default_param=False, **kargs): sub_req = '' id_list = [] if kargs.has_key('id_list'): id_list = kargs['id_list'] kargs.pop('id_list') req_str = self.__createReqFinalVals( SysUtils.getReqByName('GET_CONTACT_CTNR'), kargs) for contact_id in id_list: sub_req = sub_req + SysUtils.getReqByName( 'GET_CONTACT_NODE') % contact_id return req_str % sub_req
def buildPreviewMergeReq(self, use_default_param=False, **kargs): #similar with buildGetContactsReq sub_req = '' id_list = [] if kargs.has_key('id_list'): id_list = kargs['id_list'] kargs.pop('id_list') req_str = self.__createReqFinalVals( SysUtils.getReqByName('PREVIEW_MERGE_CTNR'), kargs) for contact_id in id_list: sub_req = sub_req + SysUtils.getReqByName( 'PREVIEW_MERGE_NODE') % contact_id return req_str % sub_req
def attach(pid=str, injection_method=0): global currentpid global child global infinite_thread_location global infinite_thread_id global codes_injected SysUtils.create_PINCE_IPC_PATH(pid) currentdir = SysUtils.get_current_script_directory() child = pexpect.spawnu('sudo LC_NUMERIC=C gdb --interpreter=mi', cwd=currentdir) child.setecho(False) child.logfile=open(SysUtils.get_gdb_async_file(pid),"w") # a creative and meaningful number for such a marvelous and magnificent program PINCE is child.timeout = 900000 child.expect_exact("(gdb)") # gdb scripts needs to know PINCE directory, unfortunately they don't start from the place where script exists send_command('set $PINCE_PATH=' + '"' + currentdir + '"') send_command("source gdb_python_scripts/GDBCommandExtensions.py") if injection_method is 1: # linux-inject codes_injected = inject_with_linux_inject(pid) send_command("attach " + pid + " &") send_command("interrupt") currentpid = int(pid) if injection_method is 0: # simple dlopen call injectionpath = currentdir + "/Injection/InitialCodeInjections.so" codes_injected = inject_with_dlopen_call(injectionpath) if codes_injected: # address_table_update_thread = PINCE.UpdateAddressTable(pid) # planned for future # address_table_update_thread.start() result = send_command("call inject_infinite_thread()") filtered_result = search(r"New Thread\s*0x\w+", result) # New Thread 0x7fab41ffb700 (LWP 7944) send_command("c &") # Return True if the injection is successful, False if not if not filtered_result: return False threadaddress = split(" ", filtered_result.group(0))[-1] match_from_info_threads = search(r"\d+\s*Thread\s*" + threadaddress, send_command("info threads")).group(0) # 1 Thread 0x7fab41ffb700 infinite_thread_id = split(" ", match_from_info_threads)[0] infinite_thread_location = threadaddress send_command("thread " + infinite_thread_id) send_command("interrupt") # send_command("call inject_table_update_thread()") # planned for future else: send_command("source gdb_python_scripts/on_code_injection_failure") send_command("c &") return codes_injected
def buildSimpleReq(self, request_name, use_default_param=True, del_empty_field=True, **args): # get request str by request name req_str = SysUtils.getReqByName(request_name) # replace with default params if use_default_param: req_str = self.__createReqDefaultVals(req_str, SysUtils.getDefaultParams(request_name)) # replace with final params: key=%s and <key>%s</key> req_str = self.__createReqFinalVals(req_str, args) # delete empty request field like xxx=%s or <aa>%s</aaa> if del_empty_field: req_str = self.__delEmptyReqFields(req_str) req_str = self.__delEmptyOtherPlaceHolder(req_str) req_str = self.__delEmptyXMLNode(req_str) print req_str return req_str
def buildMobileMailPrefrenceRequest(self, use_default_param=False, **param_dict): """ <request><mobileMailPreference action="save">%s</mobileMailPreference></request> """ sub_req_str = SysUtils.getReqByName("MOBILE_SUB_MAIL_PREFERRENCE") sub_req = "" if use_default_param: for (key, value) in SysUtils.getDefaultParams("MOBILE_SUB_MAIL_PREFERRENCE").items(): sub_req = sub_req + sub_req_str % (key, value) else: print param_dict for key, value in param_dict.items(): sub_req = sub_req + sub_req_str % (key, value) req_str = SysUtils.getReqByName("MOBILE_MAIL_PREFERENCE_SAVE") return req_str % sub_req
def loadTermFile(self, ruleFile): if not os.path.exists(ruleFile): cfgDirecotry = os.path.dirname(os.path.abspath(Config.getConfigFile())) ## pick at config directory ruleFile = os.path.join(cfgDirecotry, ruleFile) if not os.path.exists(ruleFile): raise NLPException("Unable to load rule file " + os.path.basename(ruleFile)) return json.loads(SysUtils.readFileContent(ruleFile))
def load(self, inFile): ## load configuration from argument or ./config.cfg.json or config/config.cfg.json cfgFile = inFile or ("config.cfg.json" if os.path.isfile("config.cfg.json") else os.path.join("config", "config.cfg.json")) if (not os.path.isfile(cfgFile)): raise ConfigException("unable to find config file") self.cfgFile = cfgFile content = SysUtils.readFileContent(cfgFile) self.json = json.loads(content)
def buildMailPrefrenceRequest(self, use_default_param=False, **param_dict): ''' <mailPreference action="save"><mailpreference %s="%s" /></mailPreference> ''' sub_req_str = SysUtils.getReqByName('SUB_MAIL_PREFERRENCE') sub_req = '' if use_default_param: for (key, value) in SysUtils.getDefaultParams( 'SUB_MAIL_PREFERRENCE').items(): sub_req = sub_req + sub_req_str % (key, value) else: print param_dict for key, value in param_dict.items(): sub_req = sub_req + sub_req_str % (key, value) req_str = SysUtils.getReqByName('MAIL_PREFERENCE_SAVE') return req_str % sub_req
def buildPrefRequest(self, use_default_param=False, **param_dict): """ special for preference setting <request><prefs action="set"></prefs></request> """ sub_req_str = SysUtils.getReqByName("SUB_PREF") sub_req = "" if use_default_param: for (key, value) in SysUtils.getDefaultParams("SUB_PREF").items(): sub_req = sub_req + sub_req_str % (key, value) else: print param_dict for key, value in param_dict.items(): sub_req = sub_req + sub_req_str % (key, value) req_str = SysUtils.getReqByName("PREF_SET") return req_str % sub_req
def buildPrefRequest(self, use_default_param=False, **param_dict): ''' special for preference setting <request><prefs action="set"></prefs></request> ''' sub_req_str = SysUtils.getReqByName('SUB_PREF') sub_req = '' if use_default_param: for (key, value) in SysUtils.getDefaultParams('SUB_PREF').items(): sub_req = sub_req + sub_req_str % (key, value) else: print param_dict for key, value in param_dict.items(): sub_req = sub_req + sub_req_str % (key, value) req_str = SysUtils.getReqByName('PREF_SET') return req_str % sub_req
def set_auto_reply_cpms(self, **kargs): try: if kargs.has_key('endday'): kargs['endDate'] = SysUtils.getEndTime(plus=kargs['endday']) print kargs request = self.reqBuilder.buildAutoReplyReq('MAIL_AUTO_REPLY_CPMS', True, **kargs) res = self.client.send(request) return res except Exception, e: raise Exception('Set auto reply (CPMS) failed. - %s'%str(e))
def listALLItem(expr_key, res, exception_when_not_found=False): ''' expr_key:expression str res: xml response exception_when_not_found:nothing found in res,exception will be raised if exception_when_not_found=True ''' #res.replace('><','>\n<') result_list = SysUtils.findAll(expr_key, res) if exception_when_not_found and (len(result_list) == 0): raise TestCodeException(MsgConsts.MAIL_NO_KEY_FOUND % expr_key) return result_list
def inject_with_linux_inject(pid=str): scriptdirectory = SysUtils.get_current_script_directory() injectionpath = scriptdirectory + "/Injection/InitialCodeInjections.so" if is_32bit: result = pexpect.run("sudo ./inject32 -p " + pid + " " + injectionpath, cwd=scriptdirectory + "/linux-inject") else: result = pexpect.run("sudo ./inject -p " + pid + " " + injectionpath, cwd=scriptdirectory + "/linux-inject") print(result) # for debug if search(b"successfully injected", result): # literal string return True return False
def listALLItem(expr_key, res, exception_when_not_found=False): ''' expr_key:expression str res: xml response exception_when_not_found:nothing found in res,exception will be raised if exception_when_not_found=True ''' #res.replace('><','>\n<') result_list = SysUtils.findAll(expr_key, res) if exception_when_not_found and (len(result_list) == 0): raise TestCodeException(MsgConsts.MAIL_NO_KEY_FOUND%expr_key) return result_list
def set_auto_reply_cpms(self, **kargs): try: if kargs.has_key('endday'): kargs['endDate'] = SysUtils.getEndTime(plus=kargs['endday']) print kargs request = self.reqBuilder.buildAutoReplyReq( 'MAIL_AUTO_REPLY_CPMS', True, **kargs) res = self.client.send(request) return res except Exception, e: raise Exception('Set auto reply (CPMS) failed. - %s' % str(e))
def buildSimpleReq(self, request_name, use_default_param=True, del_empty_field=True, **args): #get request str by request name req_str = SysUtils.getReqByName(request_name) #replace with default params if use_default_param: req_str = self.__createReqDefaultVals( req_str, SysUtils.getDefaultParams(request_name)) #replace with final params: key=%s and <key>%s</key> req_str = self.__createReqFinalVals(req_str, args) #delete empty request field like xxx=%s or <aa>%s</aaa> if del_empty_field: req_str = self.__delEmptyReqFields(req_str) req_str = self.__delEmptyOtherPlaceHolder(req_str) req_str = self.__delEmptyXMLNode(req_str) print req_str return req_str
def buildContactCreateUpdateReq(self, request_name, use_default_param=False, **param_dict): ''' build request for create/update contact and vcard body ''' sub_req_str = SysUtils.getReqByName('CONTACT_FIELD') ctnr_req_str = SysUtils.getReqByName(request_name) sub_req = '' req_str = ctnr_req_str #deal with contactFields if param_dict.has_key('contactFields'): for each_contact_field in param_dict['contactFields']: sub_req = sub_req + self.__createReqDefaultVals( sub_req_str, each_contact_field) param_dict.pop('contactFields') #this param will be used later else: if use_default_param: #deal with contactFields format default_params_dict = SysUtils.getDefaultParams( 'CONTACT_CREATE') if default_params_dict.has_key('contactFields'): for each_contact_field in default_params_dict[ 'contactFields']: sub_req = sub_req + self.__createReqDefaultVals( sub_req_str, each_contact_field) sub_req = self.__delEmptyReqFields(sub_req) #no need for this,maybe #deal with common params #default params if use_default_param: default_params_dict = SysUtils.getDefaultParams('CONTACT_CREATE') if default_params_dict.has_key('contactFields'): default_params_dict.pop('contactFields') req_str = self.__createReqDefaultVals(ctnr_req_str, default_params_dict) #user params req_str = self.__createReqFinalVals(req_str, param_dict) #empty fields req_str = self.__delEmptyReqFields(req_str % sub_req) return req_str
def buildEventInviteReq(self, use_default_param=False, **kargs): req_str = SysUtils.getReqByName("EVENT_SEND_INVITE") # format request with default params(no default attendees) if use_default_param: param_dict = SysUtils.getDefaultParams("EVENT_SEND_INVITE") req_str = self.__createReqDefaultVals(req_str, param_dict) print param_dict # format request with user params req_str = self.__createReqFinalVals(req_str, kargs) req_str = self.__delEmptyReqFields(req_str) # empty all %s field except placeholder-%s for attendees # format with atandee, if there is any sub_req = "" if kargs.has_key("attendees"): node_req = SysUtils.getReqByName("EVENT_CREATE_ATTENDEE") attendee_param = SysUtils.getDefaultParams("EVENT_CREATE_ATTENDEE") for each_attendee in kargs["attendees"]: node_req = self.__createReqDefaultVals(node_req, attendee_param) sub_req = sub_req + self.__createReqFinalVals(node_req, each_attendee) # format ctnr with sub_req(attendees) req_str = req_str % sub_req return req_str
def buildSimpleReq(self, request_name, **kargs): request_name = SysUtils.getReqByName(request_name) # print '~~~~~request_name~~~~~~~' # print request_name for arg in kargs: if '.' in arg: request_name = self.setParamsItem(request_name, path = arg, value = kargs[arg]) else: request_name['params'][arg] = str(kargs[arg]) # request_name['request'][arg] = str(kargs[arg]) request_name = str(request_name).replace('None', 'null')#for MSG_HEADER_LST print request_name return request_name
def set_calendar_prefs(self,**kargs): ''' input: name: a setting name description: iterator all value of each settings ''' pref_key_name = kargs['name'] value_list = SysUtils.getDefaultParams('CAL_PREFS_DEFAULT_VALUE')[pref_key_name] for value in value_list: time.sleep(1) param_dict = {pref_key_name : value } res = self.set_mail_prefs(**param_dict)
def buildAutoReplyReq(self, request_name="MAIL_AUTO_REPLY_CPMS", use_default_param=False, endday="2", **kargs): req_str = SysUtils.getReqByName(request_name) if use_default_param: param_dict = SysUtils.getDefaultParams(request_name) req_str = self.__createReqDefaultVals(req_str, param_dict) # if not use_default_param: # param_dict['endDate'] = SysUtils.getEndTime(kargs['endday']) req_str = self.__createReqFinalVals(req_str, kargs) req_str = self.__delEmptyReqFields(req_str) sub_req = "" optionsNode = "" if kargs.has_key("option"): for opt in eval(kargs["option"]): kargs_dict = opt sub_req = '<option name="%s" value="%s"/>' sub_req = sub_req % (kargs_dict["name"], kargs_dict["value"]) optionsNode = optionsNode + sub_req # format with atandee, if there is any # format ctnr with sub_req(attendees) req_str = req_str % ("<options>" + optionsNode + "</options>") return req_str
def getIPs(): out = SysUtils.runSys('ifconfig | grep -E "BROADCAST|netmask"') outLines = out.splitlines() ips = {} for i, line in enumerate(outLines): if ('BROADCAST' in line): # This line contains the logical name for NIC nicName = line.strip().split(':')[0] if ('broadcast' in outLines[i + 1]): address = outLines[i + 1].strip().split(' ')[1] else: address = 'No Connection' ips[nicName] = address return ips
def set_calendar_prefs(self, **kargs): ''' input: name: a setting name description: iterator all value of each settings ''' pref_key_name = kargs['name'] value_list = SysUtils.getDefaultParams( 'CAL_PREFS_DEFAULT_VALUE')[pref_key_name] for value in value_list: time.sleep(1) param_dict = {pref_key_name: value} res = self.set_mail_prefs(**param_dict)
def checkConnections(iPs: dict): for nic in iPs.keys(): if ('No Connection' in iPs[nic]): continue else: localIP = iPs[nic].split('.')[:-1] localIP.append('1') localIP = '.'.join(localIP) print(localIP) # pingResult = runSys(f'ping -c 1 -W 2 {localIP}') pingResult = SysUtils.runSys(f'ping -c 1 -W 2 -I {nic} {localIP}') if ('0 received, 100% packet loss' in pingResult): iPs[nic] = 'No Connection' else: continue
def buildSimpleReq(self, request_name, **kargs): request_name = SysUtils.getReqByName(request_name) # print '~~~~~request_name~~~~~~~' # print request_name for arg in kargs: if '.' in arg: request_name = self.setParamsItem(request_name, path=arg, value=kargs[arg]) else: request_name['params'][arg] = str(kargs[arg]) # request_name['request'][arg] = str(kargs[arg]) request_name = str(request_name).replace('None', 'null') #for MSG_HEADER_LST print request_name return request_name
def __init__(self, url): self.client = Client.Client(url) self.reqBuilder = SysUtils.getRequestBuilder()
def buildSafeSenderBatchRemoveReq(self, use_default_param=False, *blocksender): remove = "" for sender in blocksender: remove = remove + SysUtils.getReqByName("SAFE_SENDER_REMOVE_NODE") % sender return SysUtils.getReqByName("SAFE_SENDER_CTNR") % remove
def buildGetIndexReq(self, use_default_param=False, **kargs): req_str = SysUtils.getReqByName('CONTACT_GETINDEX') return req_str % (kargs['addressBookId'], kargs['field'])
def buildDelBatchAuotocompleteReq(self, use_default_param=False, *id_list): remove = "" for auto_id in id_list: remove = remove + SysUtils.getReqByName("DEL_AUTOCOMPLETE_NODE") % auto_id return SysUtils.getReqByName("DEL_AUTOCOMPLETE_CTNR") % remove
def buildGetIndexReq(self, use_default_param=False, **kargs): req_str = SysUtils.getReqByName("CONTACT_GETINDEX") return req_str % (kargs["addressBookId"], kargs["field"])
def buildDelBatchAuotocompleteReq(self, use_default_param=False, *id_list): remove = '' for auto_id in id_list: remove = remove + SysUtils.getReqByName( 'DEL_AUTOCOMPLETE_NODE') % auto_id return SysUtils.getReqByName('DEL_AUTOCOMPLETE_CTNR') % remove
def __init__(self,url): Calendar.__init__(self, url) self.url = url self.GHelper = GlobalHelper() self.reqBuilder = SysUtils.getRequestBuilder()
def testJsonParser(self): fileData = json.loads(SysUtils.readFileContent("testJson.json")) parser = Parser.Factory.getParser("json") parser.parseFile("testJson.json") self.assertSequenceEqual(parser.docs(), fileData["docs"]) self.assertEqual(parser.getFileMetaData("metaData"), "some data")
def __init__(self, url): Calendar.__init__(self, url) self.url = url self.GHelper = GlobalHelper() self.reqBuilder = SysUtils.getRequestBuilder()
def parseFile(self, fileName): self.json = json.loads(SysUtils.readFileContent(fileName))