BJQA-IIATF1.0框架之《自動生成有效請求Json串》
阿新 • • 發佈:2017-07-29
params break lap err lag reat 參數 utils net
2017年7月28日,今天是一個裏程碑的日子,根據參數及數值通過結對有效算法自動生成字典形式的測試用例功能模塊完成,鼓掌,鼓掌!
替換postdata模板內指定元素值的代碼如下:
1 import copy 2 import createPWtestcase 3 dicta ={ 4 "H": { 5 "ACID": "aaaaa", 6 "Ver": ["aaaa","bbbb"], 7 "CInf": [100,2,1000], 8 "TID": "df22839322dfceb0622b5c6be5879ed852d594d5CTF2.py" 9 }, 10 "B": { 11 "Login": { 12 "Type": "LP", 13 "Name": "18601231676", 14 "Pwd": "123456", 15 "ForToken": 1 16 }, 17 "TInfo": { 18 "IMSI": "460029636249253", 19 "IMEI": "868349029037662", 20 "AndroidID": "ed93e839f67279df", 21 "Mac": "68: 3e: 34: 14: 61: e7", 22 "NetType": "Wifi", 23 "Model": "m2note", 24 "OsVer": "5.1", 25 "AppVer": "4.0.0" 26 }, 27 "TT": "kAEBLgABABQzMjAxMTcwNjA1MTcyMjMxMDEzNwEACjMwMDA2NjkxNDIAAAFcjH7IjgERrT0YjQhgPAjXvmY9Z1lM8A" 28 } 29 } 30 31 dictd={‘ACID‘: [‘999‘,‘appchina‘], ‘Ver‘: [[‘IOS_5.0.0_1.0‘, ‘IOS_5.0.0‘],[‘Android2.0.0‘]], ‘CInf‘: [100, 2, 1000], 32 ‘TID‘: [‘df22839322dfceb0622b5c6be5879ed852d594d5‘],‘Type‘: [[‘LP‘],[‘PH‘]], ‘Name‘: [[‘18232441676‘],[‘15806666690‘]], 33 ‘Pwd‘: [‘123456‘], ‘ForToken‘:[ 1], ‘IMSI‘: [‘460029636249253‘], ‘IMEI‘: [[‘868349029037662‘],[‘666666‘]], ‘AndroidID‘: [‘ed93e839f67279df‘], 34 ‘Mac‘: [‘68: 3e: 34: 14: 61: e7‘], ‘NetType‘: [‘Wifi‘,‘4G‘,‘3G‘], ‘Model‘: [‘m2note‘], ‘OsVer‘: [[‘5.1‘],[‘ios9‘],[‘IOS10‘]] 35 , ‘AppVer‘: [[‘4.0.0‘],[‘4.0.1‘],[‘4.0.2‘]], ‘TT‘: [‘kAEBLgABABQzMjAxMTcwNjA1MTcyM‘]} 36 def quantizeJsonTemplet(targetDict,sourceList_dict): 37 copytargetDict = copy.deepcopy(targetDict) 38 def recursionReplace(copytargetDict,serchKey,newValue): 39 for k,v in copytargetDict.items(): 40 if isinstance(v,dict) : #值是字典元素,則遞歸處理 41 recursionReplace(v,serchKey,newValue) 42 else: 43 if k == serchKey: 44 # print (‘-----Find it:‘,k) 45 copytargetDict[k] = newValue #對目標字典元素的值重寫 46 return copytargetDict 47 48 for k,v in sourceList_dict.items():#遍歷替換列表,使多測試用例同時生成 49 recursionReplace(copytargetDict,k,v) 50 return copytargetDict 51 52 53 #測試程序 54 u2 = createPWtestcase.utils2() 55 allvalues=dictd.values() 56 allkeys = dictd.keys() 57 58 finallist = u2.dictzip(allkeys,allvalues,2) 59 u2.pprint(finallist)
CTF2.py需要依賴的py文件:
1 # -*- coding: utf-8 -*- 2 from datetime import * 3 import random,os,copy,time 4 import logging 5 import itertools 6 ‘‘‘ 7 #Author:Kuzaman 8 #Time:2017-07-18 9 ‘‘‘ 10 class utils2 : 11 #1、笛卡爾積 對參數分組全排列 12 def product(self,allparams): 13 newlist=[] 14 for x in eval(‘itertools.product‘+str(tuple(allparams))): 15 newlist.append(x) 16 print (len(newlist)) 17 return newlist 18 19 #2、對笛卡爾積處理後的二維原始數據進行N配對處理,得到Pairwise計算之前的數據 20 def get_pairslist(self,productedlist,pairlen): 21 pwlist = [] 22 for i in productedlist: 23 subtemplist = [] 24 for sublista in itertools.combinations(i,pairlen): 25 subtemplist.append(sublista) 26 pwlist.append(subtemplist) 27 return pwlist 28 29 #3、進行Pirwise算法計算 30 def pairwise(self,allparams,pairlen): 31 productedlist=self.product(allparams) #productedlist笛卡爾積全排列組合的測試用例 32 # self.pprint(productedlist) 33 # print (‘笛卡爾積全排列組合數量:‘,len(productedlist),‘--‘*11) 34 listb = self.get_pairslist(productedlist,pairlen) #listb:對測試用例結對拆分後的二維列表 35 sublistlen = len(listb[1]) #sublistlen:每個測試用例拆分後一維列表長度 36 flag = [0]*sublistlen #一條測試用例拆分後,每個結對在二維列表同位置上是否有相 37 #同值,其標識列表為flag,flag長度根據sublistlen改變 38 templistb = copy.deepcopy(listb)#【有效組】的原始值與listb相同 39 delmenu = [] #無效測試用例編號列表 40 holdmenu=[] #有效測試用例編號列表 41 # self.pprint (listb) 42 # print (‘--‘*7,‘有效測試用例計算結果‘,‘--‘*7) 43 for cow in listb: #一維遍歷,等同於二維數組按照行遍歷 44 for column in cow: #二維遍歷,等同二維數組中任意一行按照‘列’橫向遍歷 45 for templistbcow in templistb: #【有效組=templistb】中按照行,從上至下遍歷 46 Xa = cow.index(column) #待校驗元素的橫坐標 47 Ya = listb.index(cow) #待校驗元素的縱坐標 48 #有效組中行不能是當前要對比元素所在的行, 49 #且帶對比元素與【有效組】的行templistbcow中的坐標Xa元素相同, 50 #則認為對比成功,跳出第三層for循環。 51 if templistbcow != cow and column == templistbcow[Xa]: 52 # print (column,‘===>‘ ,templistbcow[Xa],‘相等了。。。‘) 53 flag[Xa] = 1 #1表示對比成功 54 break 55 else: #對比不成功,需要繼續第三層for循環對比 56 # print (column,‘===>‘ ,templistbcow[Xa],‘不不不等了。。。‘) 57 flag[Xa] = 0 #0表示對比不成功 58 # print (‘下標%d,子元素 %s 雙匹配對比結果flag:%s‘%(listb.index(cow),cow,flag)) 59 if 0 not in flag: #如果對比列表中都是1,表明該行的所有結對都在同列的對應位置找到了 60 num = listb.index(cow) 61 delmenu.append(num) #記錄該無用用例所在總列表listb中的位置 62 templistb.remove(cow) #有效組中刪除該無用用例,保持有效性 63 # print (‘下標為%d行應刪除,內容=%s,‘%(num,cow)) 64 # print (‘delmenu:‘,delmenu) 65 else: #如果有0出現在對比列表flag中,則表示該行所有結對組並未全在同列對應位置找到,此用例為有效用例應保留 66 num2 = listb.index(cow) 67 holdmenu.append(num2) #記錄有效用例在總列表listb中的坐標 68 # print (‘---------------下標為%d行應保留,內容=%s‘%(num2,cow)) 69 # print(‘holdmenu=‘,holdmenu) 70 # print (‘***‘*20) 71 print (‘保留元素列表:%s \n匹配重復元素列表:%s‘%(holdmenu,delmenu)) 72 return self.pwresult(productedlist,holdmenu) 73 74 def pwresult(self,slist,holdmenu): 75 holdparamslist = [] 76 for item in holdmenu: 77 holdparamslist.append(slist[item]) 78 return holdparamslist 79 80 def pprint(self,list): 81 for item in list: 82 print (‘line %d:‘%(list.index(item)+1),item) 83 # print (item) 84 def dictzip(self,listkeys,allparams,pairlen): 85 twoLevellistvalues = self.pairwise(allparams,pairlen) 86 list_dict = [] 87 for listvalues in twoLevellistvalues: 88 list_dict.append(dict(zip(listkeys,listvalues))) 89 return list_dict 90 91 if __name__ == ‘__main__‘: 92 u2 = utils2() 93 # allparams=[[‘M‘,‘O‘,‘P‘],[‘W‘,‘L‘,‘I‘],[‘C‘,‘E‘ 94 # allparamsdict={‘os‘:[‘M‘,‘O‘,‘T‘],‘Brow‘:[‘L‘,‘I‘,‘T‘],‘top‘:[‘s‘,‘T‘,‘E‘,‘K‘],‘Level‘:[1,3],‘flag‘:[‘Yes‘,‘No‘],‘is666‘:[‘666‘,‘‘]} 95 96 dictd={‘ACID‘: [‘999‘,‘appchina‘], ‘Ver‘: [[‘IOS_5.0.0_1.0‘, ‘IOS_5.0.0‘],[‘Android2.0.0‘]], ‘CInf‘: [100],‘TID‘: [‘df22839322dfceb0622b5c6be5879ed852d594d5‘] 97 ,‘Type‘: [‘LP‘,‘PH‘], ‘Name‘: [‘15806666690‘],‘Pwd‘: [‘123456‘], ‘ForToken‘: [‘1‘], ‘IMSI‘: ["460029636249253"], ‘IMEI‘: [‘666666‘], 98 ‘AndroidID‘: [‘ed93e839f67279df‘], ‘Mac‘: [‘68: 3e: 34: 14: 61: e7‘], ‘NetType‘: [‘Wifi‘,‘4G‘], ‘Model‘: [‘m2note‘], ‘OsVer‘: [[‘4.4‘],[‘6.0‘],[‘5.1‘]], 99 ‘AppVer‘: [‘4.0.0‘,‘4.0.1‘], ‘TT‘: [‘kAEBLgABABQzMjAxMTcwNjA1MTcyM‘]} 100 101 102 103 104 allvalues=dictd.values()#[[‘M‘,‘O‘,‘T‘],[‘L‘,‘I‘,‘T‘],[‘s‘,‘T‘,‘E‘,‘K‘],[1,3],[‘Yes‘,‘No‘],[‘666‘,‘‘]] 105 allkeys = dictd.keys() 106 u2.product(allvalues) 107 # finallist = u2.dictzip(allkeys,allvalues,2) 108 # print(‘最終保留測試用例個數:%d 個‘%(len(finallist))) 109 # u2.pprint(finallist)createPWtestcase.py
BJQA-IIATF1.0框架之《自動生成有效請求Json串》