Embodiment
The present invention is directed to the problem that existing method exists, adopt the source code analysis technology, mainly solve the input control problem of various unusual inputs not being handled in the program of how finding out, thereby improve testing adequacy, reduce the test job amount.
Below with reference to exemplary preferred embodiments and reference drawings, technical scheme of the present invention is described in detail.
As shown in Figure 1, be to detect the workflow diagram that the method for correctness is handled in the software anomaly input among the present invention, the implementation of this programme is following.
One, morphology and grammatical analysis
The fundamental purpose of this step is abstract syntax tree and the symbol table that obtains the tested software source code.
Adopt type Principles & Technology of Compiler Design, the tested software source code is carried out morphology and grammatical analysis,, obtain abstract syntax tree and symbol table through analyzing.
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 the source code; E is the limit of tree, is responsible for connecting various syntactic structures.
Definition 2: symbol table is a table graphic data structure, and the identifier that produces in the lexical analysis process is as the inlet of symbol table, the information that subsequent analysis obtains, as: the type of identifier, use-pattern, storage space etc. can join in the table as required.
As shown in Figure 2, be the workflow diagram of morphology and grammatical analysis among the present invention.Morphology and grammatical analysis are the bases of generating code and interface control mapping relations.This programme employing method in common is carried 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
Output (AST, SymTable): the abstract syntax tree of P and symbol table
1.TokenStream<-Lexical_Analyze(P)
2.(AST,)SymTable)<-Syntax_Analyze(TokenStream)
3.Return(AST,SymTable)
Source code will obtain its abstract syntax tree and symbol table after process morphology and grammatical analysis.The 1st step of algorithm 1 is carried out lexical analysis to source code P, obtains the marked flows TokenStream of P; The 2nd step was that grammatical analysis is carried out on the basis with the marked flows, obtained abstract syntax tree AST and the symbol table SymTable of P; The 3rd step was returned AST and SymTable.
Be the example code segment of a Java Swing below.
1、javax.swing.JMenufileMenu=new?javax.swing.JMenu();
2、javax.swing.JMenuItemopenFileMenuItem=new?javax.swing.JMenuItem();
3、fileMenu.add(openFileMenuItem);
Table 1 is a code corresponding symbol table
Identifier |
Type |
The definition number of being expert at |
fileMenu | JMenu | |
1 |
openFileMenuItem |
JMenuItem |
|
2 |
In the example code segment the 1st row has defined a menu fileMenu, and the 2nd row has defined a menu item openFileMenuItem, and the 3rd is about to openFileMenuItem joins among the fileMenu.
As shown in Figure 3, be after above-mentioned Java example code segment is passed through morphology and grammatical analysis, the abstract syntax tree synoptic diagram of the 3rd line code.
After abstract syntax tree that obtains source code and symbol table, just can analyze it, purpose is to find out all input controls (input control identification).
Two, input control identification
Adopt depth first search technique, traversal abstract syntax tree and symbol table obtain input control all in the source code (as: text input frame, drop-down menu etc.).
Control in the graphic user interface (GUI) is the imbody of software function, and software function realizes through control.Wherein, input control is meant that those allow the control of user through key in data.The purpose of input control identification is to find out input controls all in the program.This step is mainly through accomplishing to the abstract syntax traversal of tree with to searching of symbol table.Main method is: with depth first method traversal abstract syntax tree, if certain leaf node is a variable, at this moment through searching symbol table, can obtain this type of variables; If the type is the input control type, then explain this variable described be an input control.
Abstract syntax tree and symbol table have all been used in input control identification in program compilation and this programme, but both purposes are different, and the former purpose is to generate and the optimization aim code, and the latter's purpose is to find out input controls all in the source code.
1, input control type
Programming language is different, and the input control type that it comprised is also different, is example with Java SWT, and the input control that it comprised is as shown in table 2.
Table 2 input control and corresponding function thereof
Though these controls have method of application separately,, be to confirm certain variable-definition for the mode of certain control by the standard of language itself, therefore, and can be according to these standards, these controls of employing unified method identification.
2, variable-definition mode.
Definition mode to control variable mainly contains 2 kinds:
(1) variable declarations
Call the turn at program design language, the existence that variable declarations is responsible for pointing out certain variable with and relevant character, but it is not carried out initialization.
For example: private javax.swing.JButtoncomputeButton; It is the variable computeButton of javax.swing.JButton that this statement has defined type.
(2) variable-definition
Call the turn at program design language, the existence that variable-definition not only is responsible for pointing out certain variable with and relevant character, also need carry out initialization to it.
For example: javax.swing.JMenufileMenu=new javax.swing.JMenu ();
This statement has defined type to be the variable fileMenu of javax.swing.JMenu and to use the new statement that it has been carried out initialization.
3, input control discrimination method
Through in abstract syntax tree, above-mentioned two kinds of definition modes being searched for, the content in the combined symbol table just can pick out input controls all in the source code and variable thereof simultaneously.
As shown in Figure 4, be input control identification workflow diagram among the present invention.Through traveling through abstract syntax tree and searching symbol table,, can generate the input control tabulation according to the input control characteristic.(AST SymTable) has described the input control identification algorithm to algorithm 2:GEL_AST_SymTable.
Algorithm 2:GEL_AST_SymTable (AST, SymTable)
Input AST: the abstract syntax tree of source code
Input SymTable: the symbol table of source code
Output GEL: the input control tabulation in the source code
1.GEL<-[];
2. travel through all leaf node Node of AST
3.if (LookUpType (SymTable, Node)=the input control type)
4.GEL<-Node::GEL
5.Return?GEL
The 1st step initialization rreturn value GEL of algorithm 2 is empty tabulation; The 2nd step to the 4th step begins to travel through abstract syntax tree AST; Wherein (SymTable Node) judges whether leaf node Node is the input control type to the 3rd step, if then in the 4th step this node is added rreturn value GEL tabulation with function LookUpType; After abstract syntax tree AST traversal was accomplished, the 5th step was returned the input control GEL that tabulates.
Three, do not judge the input control identification of unusual input.
User's input has certain restriction usually, if its abnormal conditions are not judged, when user's input did not have in allowed band, various mistakes will appear in software so.Therefore, need judge unusually all user's inputs, and take measures areput in view of the above.
In order to achieve the above object, need find out the control that those are not judged its unusual input in program.Main method is: search each input control in the input control tabulation; Then with depth first method traversal abstract syntax tree; Whether inspection wherein has the statement that this input is judged unusually, if do not have, then this control just belongs to and do not judge unusual input control.This is the part of most critical among the present invention, and how it detects input control and whether the treatment technology problem has been carried out in unusual input if efficiently solving.
As shown in Figure 5, be the workflow diagram of not judging the input control identification of unusual input among the present invention.According to abstract syntax tree and input control tabulation and unusual input judgement grammar property, do not judged unusual input control tabulation.
Detect software and whether unusual input is handled, at first need obtain the input of software.Be example with Java SWT below, provide the pairing user of different input controls and import acquisition methods, as shown in table 3.
The corresponding input data capture method of the different input controls of table 3
Sequence number |
The input control title |
The input data capture method |
1 |
Text input control Text |
getText() |
2 |
List control List |
getItem() |
3 |
Can import list control Combo and CCombo |
getText() |
4 |
Grid control Table |
GetItem () function obtains certain element among the Table |
5 |
Tree control Tree |
GetItem () function obtains certain element among the Table |
6 |
Form tree control TableTree |
GetItems () function obtains some data wherein |
7 |
Pop-up list control PopupList |
GetItems () function obtains the element that the user chooses |
8 |
Pattern text control StyledText |
getText() |
After obtaining user's input, just can whether carry out unusual judgement in the determining program to this input (normally String type).
Programming language is different, and its corresponding unusual input judging method is different.With the Java language is example, as shown in table 4 to the common method that String categorical variable x judges unusually.
Table 4String categorical variable is judged classification and language feature unusually
Sequence number |
Unusual judgement classification | Language feature | |
1 |
Judge whether x is identical with certain character string s that directly provides |
?if(x.isEqual(s))... |
2 |
Judge whether x is inequality with certain character string s that directly provides |
?if(!x.isEqual(s))... |
3 |
Judge that x is whether in certain aggregate type variable S |
?if(S.contains(x))... |
4 |
Judge that x is whether in the key assignments set of certain Map categorical variable M |
?if(M.containsKey(x))... |
According to the pairing language feature of the unusual judgement type that provides in the table 4 traversal abstract syntax tree, if the corresponding language feature of this input control not in the code, this control is exactly not judge unusual input control so, and algorithm 3 has been described said process.
Algorithm 3:GNL_AST_GEL (AST, GEL)
Input AST: the abstract syntax tree of source code
Input GEL: input control tabulation
Output NL: do not judge unusual input control tabulation in the source code
1.NL<-[];
2. travel through all the input control GEmt1 among the GEL
3. with depth first method traversal abstract syntax tree AST
4.if (thereby the associated functional calls of GEmt1 is not obtained user's input || input is not judged unusually)
5.then?NL<-NL::GEmt1
6.Return?NL;
The course of work of algorithm 3 is: the 1st step was initialized as sky with return-list NL; The 2nd step to the 5th step travels through all the input control GEmt1 among the GEL; The 3rd step traversal abstract syntax tree AST; The 4th step judged that the user who whether has obtained this input control in the code imported and it has been carried out unusual judgement; If not then this input control is added return-list NL in the 5th step; The 6th step was returned the result.
Be example with Java SWT code sample section below, describe the present invention in detail.
In the above-mentioned code the 4th row has defined main window shell; The 10th row has defined a Text control text1, and the 13rd row calls getText () function and obtains user's input, and the 14th to 16 row is judged this input; If be input as sky then print prompt information; The 17th row has defined a Text control text2, and the 20th row has defined a Combo control cb1, and the 23rd to 27 is about to whole interface is shown on the screen.
As shown in Figure 6, be the corresponding software interface synoptic diagram of code sample section among 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 after passing through morphology and grammatical analysis among the present invention, 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, write down their type in the symbol table and in source code by the row in when definition number.
Table 5 code sample section symbol table
Identifier |
Type |
The definition number of being expert at |
?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 the abstract syntax tree and the symbol table of above-mentioned code sample section, use the algorithm 2 in this programme to carry out the input control identification, obtain input control tabulation as shown in table 6.
The tabulation of table 6 code sample section input control
Identifier |
Character display |
Type |
text1 |
Text |
10 |
text2 |
Text |
17 |
cb1 |
Combo |
20 |
3, do not judge unusual input control identification
According to the abstract syntax tree and the symbol table of code sample section, use the algorithm 3 in this programme not judge unusual input control identification.Can be learnt that by algorithm 3 code is imported tx1 the user that the 13rd row has obtained the text1 control, at the 14th row it carried out unusual judgement then, and quite different to text2 and cb1 control, detailed results is as shown in table 7.
Table 7 is not judged unusual input control tabulation
Identifier |
Character display |
Type |
text2 |
Text |
17 |
cb1 |
Combo |
20 |
That is: text2 and cb1 control are not judged user's input unusually.Information according to table 7 provides for the developer, can increase following statement behind the 19th line statement:
Increase following statement at the 22nd line statement:
Revise the mistake that occurs;
For the tester, can design following test case:
A, locate not import any value in " input 2 " ...; B, locate not import any value in " input 3 " ....Thereby, effectively find the mistake that software exists.