Embodiment
The present invention is directed to existing method Problems existing, adopt source code analysis technology, mainly solve how to find out in program and extremely input to various the input control problem processed, thus raising testing adequacy, minimizing test job amount.
Below with reference to the accompanying drawings and preferred embodiment, technical solution of the present invention is described in detail.
As shown in Figure 1, be the workflow diagram of the method for the abnormal input processing correctness of inspection software in the present invention, the implementation of this programme is as follows.
One, morphology and grammatical analysis
The fundamental purpose of this step is the abstract syntax tree and the symbol table that obtain tested software source code.
Adopting class Principles & Technology of Compiler Design, morphology and grammatical analysis are carried out to tested software source code, by analyzing, obtaining abstract syntax tree and symbol table.
Definition 1: abstract syntax tree is one tree T=<V, E>, wherein:
V is the node of tree, is made up of the various syntactic structures in source code; E is the limit of tree, is responsible for connecting various syntactic structure.
Definition 2: symbol table is a table graphic data structure, the identifier produced in lexical analysis process as the entrance of symbol table, the information that subsequent analysis obtains, as: the type, use-pattern, storage space etc. of identifier can join in table as required.
As shown in Figure 2, be the workflow diagram of morphology and grammatical analysis in the present invention.Morphology and grammatical analysis are the bases of generating code and interface control mapping relations.This programme adopts general method to carry out morphology and grammatical analysis to the source code of tested software, is described by algorithm 1.
Algorithm 1.Lexical_Syntax_Analyze (P)
Input P: source code
Export abstract syntax tree and the symbol table of (AST, SymTable): P
1.TokenStream<-Lexical_Analyze(P)
2.(AST,)SymTable)<-Syntax_Analyze(TokenStream)
3.Return(AST,SymTable)
Source code, after morphology and grammatical analysis, will obtain its abstract syntax tree and symbol table.1st step of algorithm 1 carries out lexical analysis to source code P, obtains the mark stream TokenStream of P; 2nd step carries out grammatical analysis to mark based on stream, obtains abstract syntax tree AST and the symbol table SymTable of P; AST and SymTable returns by the 3rd step.
Here is the example code segment of a Java Swing.
1、javax.swing.JMenufileMenu=new javax.swing.JMenu();
2、javax.swing.JMenuItemopenFileMenuItem=new javax.swing.JMenuItem();
3、fileMenu.add(openFileMenuItem);
Table 1 is the symbol table that code is corresponding
Identifier |
Type |
Definition place line number |
fileMenu |
JMenu |
1 |
openFileMenuItem |
JMenuItem |
2 |
The 1st row in example code segment defines a menu fileMenu, and the 2nd row defines a menu item openFileMenuItem, and the 3rd is about to openFileMenuItem joins in fileMenu.
As shown in Figure 3, be above-mentioned Java example code segment after morphology and grammatical analysis, the abstract syntax tree schematic diagram of the 3rd line code.
After the abstract syntax tree obtaining source code and symbol table, just can analyze it, object finds out all input controls (input control identification).
Two, input control identification
Adopt depth first search technique, ergodic abstract syntax tree and symbol table, obtain input controls (as: Text Entry, drop-down menu etc.) all in source code.
Control in graphic user interface (GUI) is the imbody of software function, and software function is realized by control.Wherein, input control refers to that those allow user by the control of key in data.The object of input control identification finds out input controls all in program.This step is mainly through to abstract syntax traversal of tree and having searched symbol table.Main method is: with depth first method ergodic abstract syntax tree, if certain leaf node is variable, at this moment by searching symbol table, can obtain this type of variables; If the type is input control type, then illustrate to be an input control described by this variable.
Input control identification in program compilation and this programme all employ abstract syntax tree and symbol table, but both objects are different, and the former object generates and optimization aim code, and the object of the latter finds out input controls all in source code.
1, input control type
Programming language is different, and the input control type that it comprises is also different, and for Java SWT, its input control comprised is as shown in table 2.
The function of table 2 input control and correspondence thereof
Although these controls have respective using method, be that the mode of certain control is determined by the specification of language itself by certain variable-definition, therefore, according to these specifications, unified these controls of method identification can be adopted.
2, variable-definition mode.
2 kinds are mainly contained to the definition mode of control variable:
(1) variable declarations
Call the turn at program design language, variable declarations is responsible for existence and its relevant character of pointing out certain variable, but does not carry out initialization to it.
Such as: private javax.swing.JButtoncomputeButton; This statement defines the variable computeButton that type is javax.swing.JButton.
(2) variable-definition
Call the turn at program design language, variable-definition is not only responsible for existence and its relevant character of pointing out certain variable, also needs to carry out initialization to it.
Such as: javax.swing.JMenufileMenu=new javax.swing.JMenu ();
This statement defines variable fileMenu that type is javax.swing.JMenu and uses new statement to carry out initialization to it.
3, input control discrimination method
By searching for above-mentioned two kinds of definition modes in abstract syntax tree, the content simultaneously in combined symbol table, just can pick out input controls all in source code and variable thereof.
As shown in Figure 4, be input control identification workflow diagram in the present invention.By ergodic abstract syntax tree and search symbol table, according to input control feature, input control list can be generated.Algorithm 2:GEL_AST_SymTable (AST, SymTable) describes input control identification algorithm.
Algorithm 2:GEL_AST_SymTable (AST, SymTable)
Input AST: the abstract syntax tree of source code
Input SymTable: the symbol table of source code
Export GEL: the input control list in source code
1.GEL<-[];
2. travel through all leaf node Node of AST
3.if (LookUpType (SymTable, Node)=input control type)
4.GEL<-Node::GEL
5.Return GEL
1st step initialization rreturn value GEL of algorithm 2 is empty list; 2nd step starts ergodic abstract syntax tree AST to the 4th step; Wherein the 3rd step call function LookUpType (SymTable, Node) judges whether leaf node Node is input control type, if so, then in the 4th step, this node is added rreturn value GEL list; After abstract syntax tree AST has traveled through, input control list GEL returns by the 5th step.
Three, the input control identification of abnormal input is not judged.
The input of user has certain restriction usually, if do not judged its abnormal conditions, so when the input of user is not in allowed band, software just there will be various mistake.Therefore, need to carry out abnormal judgement to all users input, and take corresponding treatment measures accordingly.
In order to achieve the above object, need to find out those and the control judged is not inputted extremely to it in a program.Main method is: search each input control in input control list, then with depth first method ergodic abstract syntax tree, whether check wherein to have and carries out the abnormal statement judged to this input, if do not had, then this control just belongs to and does not judge abnormal input control.This is the part of most critical in the present invention, and how it detects input control and whether carried out treatment technology problem to abnormal input if efficiently solving.
As shown in Figure 5, be the workflow diagram not judging the abnormal input control identification inputted in the present invention.Judge grammar property according to abstract syntax tree and input control list and abnormal input, do not judged abnormal input control list.
Want inspection software whether to process abnormal input, first need the input obtaining software.Below for Java SWT, the user provided corresponding to different input control inputs acquisition methods, as shown in table 3.
The input data capture method that the different input control of table 3 is corresponding
Sequence number |
Input control title |
Input data capture method |
1 |
Text-entry control Text |
getText() |
2 |
List control List |
getItem() |
3 |
List control Combo and CCombo can be inputted |
getText() |
4 |
Grid control Table |
GetItem () function obtains certain element in Table |
5 |
Tree control Tree |
GetItem () function obtains certain element in Table |
6 |
Form tree control TableTree |
GetItems () function obtains some data wherein |
7 |
Pop-up list control PopupList |
GetItems () function obtains the element that user chooses |
8 |
Pattern text control StyledText |
getText() |
After acquisition user input, just whether can carry out abnormal judgement to this input (normally String type) in determining program.
Programming language is different, and the abnormal input judging method of its correspondence is different.For Java language, the common method of String categorical variable x being carried out to abnormal judgement is as shown in table 4.
Table 4String categorical variable is abnormal judges classification and language feature
Sequence number |
Extremely classification is judged |
Language feature |
1 |
Judge that whether x is identical with the character string s that certain directly provides |
if(x.isEqual(s))... |
2 |
Judge that whether x is not identical with the character string s that certain directly provides |
if(!x.isEqual(s))... |
3 |
Judge x whether in certain aggregate type variable S |
if(S.contains(x))... |
4 |
Judge x whether in the key assignments set of certain Map categorical variable M |
if(M.containsKey(x))... |
Judge the language feature ergodic abstract syntax tree corresponding to type according to the exception provided in table 4, if the language feature that in code, this input control is not corresponding, so this control is exactly do not judge abnormal input control, and algorithm 3 describes said process.
Algorithm 3:GNL_AST_GEL (AST, GEL)
Input AST: the abstract syntax tree of source code
Input GEL: input control list
Export NL: in source code, do not judge abnormal input control list
1.NL<-[];
2. travel through all input control GEmt1 in GEL
3. with depth first method ergodic abstract syntax tree AST
4.if (not to the associated functional calls of GEmt1 thus obtain user's input || abnormal judgement is not carried out to input)
5.then NL<-NL::GEmt1
6.Return NL;
The course of work of algorithm 3 is: return-list NL is initialized as sky by the 1st step; 2nd step is to all input control GEmt1 in the 5th step traversal GEL; 3rd step ergodic abstract syntax tree AST; 4th step judges that the user whether obtaining this input control in code inputs and carried out abnormal judgement to it; If, this input control is not added return-list NL in the 5th step; Result returns by the 6th step.
Below for Java SWT code sample section, describe the present invention in detail.
The 4th row in above-mentioned code defines main window shell, 10th row defines a Text control text1,13rd row calls getText () function and obtains user's input, 14 to 16 row judges this input, if be input as sky, print prompt information, 17th row defines a Text control text2, and the 20th row defines a Combo control cb1, and the 23 to 27 is about to whole interface display on screen.
As shown in Figure 6, be the software interface schematic diagram of code sample section correspondence in the present invention.Wherein comprise 3 input frames: " input 1 " and " input 2 " is the control of Text type, and " input 3 " is the control of Combo type.
As shown in Figure 7, be in the present invention after morphology and grammatical analysis, the part abstract syntax tree structural representation of code sample section.
1, morphology and grammatical analysis
Use the algorithm 1 in this programme to carry out morphology and grammatical analysis, obtain symbol table as shown in table 5.Wherein, have 9 symbols, line number when have recorded their type in symbol table and be defined in source code.
Table 5 code sample section symbol table
Identifier |
Type |
Definition place line number |
display |
Display |
3 |
shell |
Shell |
4 |
layout |
GridLayout |
5 |
data |
GridData |
9 |
text1 |
Text |
10 |
tx1 |
String |
13 |
text2 |
Text |
17 |
cb1 |
Combo |
20 |
2, input control identification
According to abstract syntax tree and the symbol table of above-mentioned code sample section, use the algorithm 2 in this programme to carry out input control identification, obtain input control list as shown in table 6.
The list of table 6 code sample section input control
Identifier |
Display character |
Type |
text1 |
Text |
10 |
text2 |
Text |
17 |
cb1 |
Combo |
20 |
3, abnormal input control identification is not judged
According to abstract syntax tree and the symbol table of code sample section, the algorithm 3 in this programme is used not judge abnormal input control identification.Can be learnt by algorithm 3, the user that code obtains text1 control at the 13rd row inputs tx1, has then carried out abnormal judgement at the 14th row to it, and quite different to text2 and cb1 control, and detailed results is as shown in table 7.
Table 7 does not judge abnormal input control list
Identifier |
Display character |
Type |
text2 |
Text |
17 |
cb1 |
Combo |
20 |
That is: text2 and cb1 control does not carry out abnormal judgement to user's input.According to the information that table 7 provides, for developer, following statement can be increased after the 19th line statement:
Following statement is increased at the 22nd line statement:
The mistake that amendment occurs;
For tester, following test case can be designed:
A, do not input any value at " input 2 " place, B, do not input any value at " input 3 " place ...Thus, effectively find the mistake that software exists.