Summary of the invention
In view of this, the invention provides code segment method of testing and the device of program, to reduce the interference object to original program.
First aspect, the invention provides a kind of code segment method of testing of program, comprising:
When tested program is tested, in described tested program, call the position of tested code section, breakpoint/redirect processing unit is set;
Move described tested program, when described tested program, carry out when calling described tested code fragment position, described breakpoint/redirect processing unit interrupts the execution flow process of described tested code section, and the execution of described tested program is jumped to testing process;
Carrying out in described testing process, move test code section, and obtain test result.
In the possible implementation of the first of first aspect, the type of code of described tested code section comprises:
Binary program reflection.
In conjunction with the possible implementation of the first of first aspect, in the possible implementation of the second, described tested program comprises:
Operating system program or application program.
In the possible implementation of the second of first aspect, in the third possible implementation, described breakpoint/redirect processing unit comprises:
By breakpoint, probe being set, on-the-fly modifying code segment or virtual executory one or combination in any realization.
In the 4th kind of possible implementation of first aspect, described in obtain after test result, also comprise:
After testing process finishes, recover the execution of described tested program journey.
In conjunction with the 4th kind of possible implementation of first aspect, in the 5th kind of possible implementation, described testing process comprises:
Structure and checkout environment;
Call described tested code section;
After described tested code section is finished, carry out inspection and the record of described test environment result.
In conjunction with the 4th kind of possible implementation of first aspect, in the 6th kind of possible implementation, described testing process comprises:
Structure and checkout environment;
Call the copy of described tested code section;
After the copy of described tested code section is finished, carry out inspection and the record of described test environment result.
In conjunction with the 5th kind or the 6th kind of possible implementation of first aspect, in the 7th kind of implementation, also comprise:
Recover, before the execution of interrupted described tested program, to clear up described test environment.
In conjunction with the 6th kind of possible implementation of first aspect, in the 8th kind of possible implementation, before described structure and checkout environment, also comprise:
When the copy of described tested code section does not exist, create the copy of described tested code section.
In conjunction with the 8th kind of possible implementation of first aspect, in the 9th kind of possible implementation, the copy of described tested code section comprises:
Code segment with the equivalence of described tested code section logic function.
Second aspect, the invention provides a kind of code segment proving installation of program, comprising:
Breakpoint setting unit for when tested program is tested, is called the position of tested code section in described tested program, and breakpoint/redirect processing unit is set;
Described breakpoint/redirect processing unit, for when the execution of described tested program is when calling described tested code fragment position, described breakpoint/redirect processing unit interrupts the execution flow process of described tested code section, and the execution of described tested program is jumped to testing process;
Test cell, for carrying out described testing process, operation test code section, and obtain test result.
In the possible implementation of the first of second aspect, the type of code of described tested code section comprises:
Binary program reflection.
In conjunction with the possible implementation of the first of second aspect, in the possible implementation of the second, described tested program comprises:
Operating system program or application program order.
In conjunction with the possible implementation of the second of second aspect, in the third possible implementation, described breakpoint/redirect processing unit comprises:
Breakpoint, probe, on-the-fly modify code segment or virtual executory one or combination in any.
In the 4th kind of possible implementation of second aspect, also comprise:
Recovery unit, for by described breakpoint/redirect processing unit, the execution that recovers described tested program journey.
In conjunction with the 4th kind of possible implementation of second aspect, in the 5th kind of possible implementation, described test cell, comprising:
Test environment constructing module, for structure and checkout environment;
Code segment calling module, for calling described tested code section;
Test result acquisition module, after being finished in described tested code section, carries out inspection and the record of described test environment result.
In conjunction with the 4th kind of possible implementation of second aspect, in the 6th kind of possible implementation, described test cell, comprising:
Test environment constructing module, for structure and checkout environment;
Code segment calling module, for calling the copy of described tested code section;
Test result acquisition module, after being finished, carries out inspection and the record of described test environment result for the copy of described tested code section.
In conjunction with the 5th kind or the 6th kind of possible implementation of second aspect, at test cell described in the 7th kind of implementation, also comprise:
Test environment cleaning module, for before the execution of the interrupted described tested program journey of recovery, clears up described test environment.
In conjunction with the 6th kind of possible implementation of second aspect, in the 8th kind of possible implementation, described test cell, also comprises:
Copy creating module, for before described structure and checkout environment, when the copy of described tested code section does not exist, creates the copy of described tested code section.
In conjunction with the 8th kind of possible implementation of second aspect, in the 9th kind of possible implementation, the copy of described tested code section comprises:
Code segment with the equivalence of described tested code section logic function.
The third aspect, the invention provides a kind of code segment test macro of program, comprises the code segment proving installation of program; Described code segment proving installation comprises:
Breakpoint setting unit for when tested program is tested, is called the position of tested code section in described tested program, and breakpoint/redirect processing unit is set;
Described breakpoint/redirect processing unit, for when the execution of described tested program is when calling described tested code fragment position, described breakpoint/redirect processing unit interrupts the execution flow process of described tested code section, and the execution of described tested program is jumped to testing process;
Test cell, for carrying out described testing process, operation test code section, and obtain test result.
From above-mentioned technical scheme, can find out, be no longer in prior art for the test of code segment in the present invention, in the mode that adopts hard coded, and need to be at the internal build test module of tested program; But can control tested program by a test procedure, carry out, it is interrupted in operation tested program to the position of calling tested code section, and will carry out flow process and jump to testing process and come the test of completion code section; Because the technical scheme in the present invention is not provided with test module in tested program, so can not improve the complexity of tested program logic, also just can not introduce new defect to tested program, and then by the present invention, just can avoid, due to what the new defect of introducing caused, original tested program is caused to interference.
Embodiment
Below in conjunction with the accompanying drawing in the embodiment of the present invention, the technical scheme in the embodiment of the present invention is clearly and completely described, obviously, described embodiment is only the present invention's part embodiment, rather than whole embodiment.Based on the embodiment in the present invention, those of ordinary skills, not making the every other embodiment obtaining under creative work prerequisite, belong to the scope of protection of the invention.
In order to realize the object that reduces the interference to original program, in one aspect of the invention, provide a kind of code segment method of testing of program, as shown in Figure 1, comprise step:
S11, when tested program is tested, in tested program, call the position of tested code section, breakpoint/redirect processing unit is set.
In the present invention, the code segment in program is tested, can be realized by a test procedure, for the ease of understanding and distinguishing, in the present invention, the program under tested code section is called to tested program, the program that tested code section is tested is called to test procedure.
For the code segment section to setting in tested program, test, first test procedure is provided with breakpoint/redirect processing unit in the calling station of tested code section, to facilitate the control for tested program.
S12, operation tested program, carry out when calling the position of described tested code section when tested program, and breakpoint/redirect processing unit interrupts the execution flow process of tested code section, and the execution of tested program is jumped to the testing process of tested code section
When carrying out the test of code segment, first move tested program, when tested program runs to the position that need to call tested code section, breakpoint/redirect processing unit will be taken over the invoked procedure of tested code section; And tested program is redirected to the testing process of test procedure, take over the invoked procedure of tested code section.Specifically can comprise, first interrupt the execution flow process of tested code section, then, the execution of tested program be jumped to the testing process of tested code section.
S13, carrying out in described testing process, operation test code section, and obtain test result.
In the execution of tested program, jump to after the testing process of tested code section, test procedure can be carried out corresponding testing process, and then can obtain test result.
In actual applications, test code section can be the tested code self that modulation is used, and can be also the copy of test code section.Thereby can to tested code, test by the operation of test code section.
In the present invention, test for code segment is no longer in prior art, in the mode that adopts hard coded, need to be at the internal build test module of tested program, but can control by a test procedure execution route of tested code section corresponding programme, making wherein has no progeny enters testing process and comes the test of completion code section; Like this, just can not improve the complexity of logic in the affiliated program of tested code section, thereby avoid the interference to original program.
Preferably, in the present invention, for the type of code of carrying out the tested code section of testing, can be binary code segment program image, such as, operating system program reflection, the very difficult ISP who controls and itself be bottom while moving due to kernel program, so cannot directly apply existing, as testing tools such as Xunit, so by the test mode in the present invention, can, not changing under kernel scale-of-two mirror image scene, it be tested.Like this, after redirect/breakpoint processing unit of kernel setting is triggered, program jumps in testing process and carries out.In testing process, can be provided with test code, test code can be compiled in a scale-of-two image file with tested code, also can adopt dynamic-form to load.Like this, in the present invention, by redirect/breakpoint technology, solved the problem that does not change the kernel test difficulty under kernel scale-of-two mirror image scene, thereby promoted debugging and the testing efficiency of kernel development.
In the test process of code section, test code modulation tested code, can coordinating by redirect/breakpoint trigger and test code logic, or by generating the mode of tested code equivalent copy, prevent from again being triggered by tested code, thereby avoiding occurring occurs endless loop between redirect/breakpoint processing unit and tested code, finally realize the encapsulation function of test code part to tested code.
In actual applications, breakpoint/redirect processing unit can be specifically to realize by breakpoint, probe, the combination that on-the-fly modifies code segment or virtual executory one or more settings.
Further, in the present invention, in order not affect the execution of tested program, or, can proceed the test of other follow-up code segments, obtain after the test result of tested code section, the execution flow process of test code section can also be jumped to tested program; Like this, after testing process finishes, just can recover the execution of tested program.
In actual applications, the step of testing process can as shown in Figure 2, comprise:
S21, structure and checkout environment;
S22, call described tested code section;
After S23, described tested code section are finished, carry out inspection and the record of described test environment result.
As shown in Figure 3, when the testing process that adopts step S21 to S23 to record, the overall flow of carrying out the test of code segment is:
When tested program is normally carried out to the position of tested code section, test procedure is taken over the invoked procedure of tested program by the breakpoint/redirect processing unit being positioned at herein,, the execution flow process of interrupting tested code section, is then relocated to testing process by jump instruction by the execution of the program under tested code section.
In testing process, first structure and checkout environment, in addition, before test process calls tested code, test procedure also will be forbidden repeating of jump instruction in redirect/breakpoint processing logic.
Then, test procedure can call tested code section, makes tested code section carry out its operation;
After the operation of tested code section is finished, carry out inspection and the record of test environment result, that is, generate test result;
Then, the execution flow process of test code section is jumped to tested program, thereby can recover the follow-up execution of tested program.
Preferably, before the execution flow process of test code section is jumped to tested program, can also comprise step: cleaning test environment, thus carry out environment cleaning work.
In actual applications, the step of testing process can also as shown in Figure 4, comprise:
S31, structure and checkout environment;
S32, call the copy of described tested code section;
After S33, described tested code section are finished, carry out inspection and the record of described test environment result.
As shown in Figure 5, when the testing process that adopts step S31 to S33 to record, the overall flow of carrying out the test of code segment is:
When tested program is normally carried out to the position of tested code section, test procedure is taken over the invoked procedure of tested program by the breakpoint/redirect processing unit being positioned at herein,, the execution flow process of interrupting tested code section, is then relocated to testing process by jump instruction by the execution of the program under tested code section.
In testing process, first structure and checkout environment.
Then, test procedure can call the copy of tested code section, and moves the copy of tested code section; Concrete, in the present invention, the copy of tested code section can be identical with tested code section, and in addition, the copy of tested code section can be also and the code segment of tested code section logic function equivalence.
After the operation of the copy of tested code section is finished, return to inspection and the record of carrying out test environment result, that is, generate test result;
Then, the execution flow process of test code section is jumped to tested program, thereby can recover the follow-up execution of tested program.
Preferably, before the execution flow process of test code section is jumped to tested program, can also comprise step: cleaning test environment, thus carry out environment cleaning work.
Preferably, before the above-mentioned copy that calls tested code section, when the copy of tested code section does not exist, can also create the step of the copy of tested code section.
Corresponding to the code segment method of testing embodiment of program illustrated in fig. 1, in the application on the other hand, also provide a kind of code segment proving installation of program, as shown in Figure 6, comprising:
Breakpoint setting unit 01 for breakpoint setting unit, for when tested program is tested, is called the position of tested code section in described tested program, and breakpoint/redirect processing unit is set;
Breakpoint/redirect processing unit 02, for described breakpoint/redirect processing unit, for when the execution of described tested program is when calling described tested code fragment position, described breakpoint/redirect processing unit interrupts the execution flow process of described tested code section, and the execution of described tested program is jumped to testing process;
Test cell 03, for carrying out described testing process, operation test code section, and obtain test result.
In the present invention, the code segment proving installation that code segment in program is tested, it can be a test procedure of realizing by software, for the ease of understanding and distinguishing, in the present invention, program under tested code section is called to tested program, the program that tested code section is tested is called to test procedure.
For the code segment section to setting in tested program is tested, first breakpoint setting unit 01 is provided with breakpoint/redirect processing unit in the calling station of tested code section, like this, when carrying out the test of code segment section, first move tested program, when tested program runs to the position that need to call tested code section, breakpoint/redirect processing unit 02 will be taken over the invoked procedure of tested code section; And tested program being redirected to the testing process of test procedure, the invoked procedure of taking over tested code section specifically comprises, first interrupts the execution flow process of tested code section, then, the execution of tested program is jumped to the testing process of tested code section.
Jump to the testing process of tested code section in the execution of tested program after, test cell 03 can start the execution of testing process, and then can move test code section, and obtains test result.
In the present invention, test for code segment is no longer in prior art, in the mode that adopts hard coded, need to be at the internal build test module of tested program, but can control by a test procedure execution route of tested code section corresponding programme, making wherein has no progeny enters testing process and comes the test of completion code section; Like this, just can not improve the complexity of logic in the affiliated program of tested code section, thereby avoid the interference to original program.
Preferably, in the present invention, for the type of code of carrying out the tested code section of testing, can be binary code segment program image, such as, operating system program reflection, the very difficult ISP who controls and itself be bottom while moving due to kernel program, so cannot directly apply existing, as testing tools such as Xunit, so by the test mode in the present invention, can, not changing under kernel scale-of-two mirror image scene, it be tested.Like this, after redirect/breakpoint processing unit of kernel setting is triggered, program jumps in testing process and carries out.In testing process, can be provided with test code, test code can be compiled in a scale-of-two image file with tested code, also can adopt dynamic-form to load.Like this, in the present invention, by redirect/breakpoint technology, solved the problem that does not change the kernel test difficulty under kernel scale-of-two mirror image scene, thereby promoted debugging and the testing efficiency of kernel development.
In the test process of code section, test code modulation tested code, can coordinating by redirect/breakpoint trigger and test code logic, or by generating the mode of tested code equivalent copy, prevent from again being triggered by tested code, thereby avoiding occurring occurs endless loop between redirect/breakpoint processing unit and tested code, finally realize the encapsulation function of test code part to tested code.
In actual applications, breakpoint/redirect processing unit can be specifically to realize by breakpoint, probe, the combination that on-the-fly modifies code segment or virtual executory one or more settings.
Further, in the present invention, in order not affect the execution of tested program, or, the test of other follow-up code segments can be proceeded, obtaining after the test result of tested code section, recovery unit can also be comprised, like this, by recovery unit, lead to the execution flow process of test code section is jumped to tested program; Thereby make, after testing process finishes, just can recover the execution of tested program.
In actual applications, test cell 03 specifically can comprise:
Test environment constructing module, for structure and checkout environment;
Code segment calling module, for calling tested code section;
Test result acquisition module, after being finished, carries out inspection and the record of test environment result for tested code section.
As shown in Figure 3, when adopting test cell to comprise above-mentioned test environment constructing module, code segment calling module and test result acquisition module, the overall flow of carrying out the test of code segment can be:
When tested program is normally carried out to the position of tested code section, breakpoint/redirect the processing unit 02 being positioned at is herein taken over the invoked procedure of tested program,, the execution flow process of interrupting tested code section, is then relocated to testing process by jump instruction by the execution of the program under tested code section.
In testing process, first test environment constructing module structure and checkout environment, in addition, before test process calls tested code, test environment constructing module also will be forbidden repeating of jump instruction in redirect/breakpoint processing logic.
Then, code segment calling module calls tested code section, makes tested code section carry out its operation;
After the operation of tested code section is finished, test result acquisition module returns to inspection and the record of carrying out test environment result,, generates test result that is;
Then, recovery unit jumps to tested program by the execution flow process of test code section, thereby can recover the follow-up execution of tested program.
Preferably, the test cell in the application, can also comprise test environment cleaning module, and test environment cleaning module, for before the execution flow process of test code section is jumped to tested program, is cleared up test environment, thereby carried out environment cleaning work.
In actual applications, test cell 03 specifically can also comprise:
Test environment constructing module, for structure and checkout environment;
Code segment calling module, for calling the copy of described tested code section;
Test result acquisition module, after being finished, carries out inspection and the record of described test environment result for tested code section.
As shown in Figure 3, when adopting test cell 03 to comprise above-mentioned test environment constructing module, code segment calling module and test result acquisition module, the overall flow of carrying out the test of code segment can be:
When tested program is normally carried out to the position of tested code section, breakpoint/redirect the processing unit 02 being positioned at is herein taken over the invoked procedure of tested program,, the execution flow process of interrupting tested code section, is then relocated to testing process by jump instruction by the execution of the program under tested code section.
In testing process, first test environment constructing module structure and checkout environment.
Then, code segment calling module test procedure can call the copy of tested code section, and moves the copy of tested code section; Concrete, in the present invention, the copy of tested code section can be identical with tested code section, and in addition, the copy of tested code section can be also and the code segment of tested code section logic function equivalence.
After the operation of the copy of tested code section is finished, test result acquisition module returns to inspection and the record of carrying out test environment result,, generates test result that is;
Then, recovery unit jumps to tested program by the execution flow process of test code section, thereby can recover the follow-up execution of tested program.
Preferably, the test cell in the application, can also comprise test environment cleaning module, and test environment cleaning module, for before the execution flow process of test code section is jumped to tested program, is cleared up test environment, thereby carried out environment cleaning work.
Preferably, the test cell 03 in the present invention can also comprise copy creating module, and copy creating module, for before the above-mentioned copy that calls tested code section, when the copy of tested code section does not exist, creates the copy of tested code section.
In the application on the other hand, the code segment test macro of another kind of program is also provided, this code segment test macro has comprised the code segment proving installation in a upper embodiment, because the code segment proving installation in the code segment test macro in the present embodiment is identical with the structure of the code segment proving installation in a upper embodiment, principle of work is identical and beneficial effect is identical, so just repeat no more at this.
Further, the embodiment of the present invention also provides the hardware of code segment proving installation to form.Can comprise at least one processor (for example CPU), and at least one storer.The executable module that processor is stored for execute store, for example computer program.Storer may comprise high-speed random access memory (RAM:Random Access Memory), also may also comprise non-unsettled storer (non-volatile memory), for example at least one magnetic disk memory.By at least one network interface (can be wired or wireless), realize the communication connection between this system gateway and at least one other network element, can use internet, wide area network, local network, Metropolitan Area Network (MAN) etc.
Referring to Fig. 7, in some embodiments, in storer, stored programmed instruction, programmed instruction can be executed by processor, wherein, programmed instruction comprises breakpoint setting unit 01, breakpoint/redirect processing unit 02 and test cell 03, the corresponding units that the specific implementation of each unit can disclose referring to Fig. 6.
The present invention program can describe in the general context of the computer executable instructions of being carried out by computing machine, for example program element.Usually, program element comprises and carries out particular task or realize routine, program, object, assembly, data structure of particular abstract data type etc.Also can in distributed computing environment, put into practice the present invention program, in these distributed computing environment, by the teleprocessing equipment being connected by communication network, be executed the task.In distributed computing environment, program element can be arranged in the local and remote computer-readable storage medium including memory device.
Each embodiment in this instructions all adopts the mode of going forward one by one to describe, between each embodiment identical similar part mutually referring to, each embodiment stresses is and the difference of other embodiment.Especially, for device embodiment, because it is substantially similar in appearance to embodiment of the method, so describe fairly simplely, relevant part is referring to the part explanation of embodiment of the method.Device embodiment described above is only schematic, the wherein said unit as separating component explanation can or can not be also physically to separate, the parts that show as unit can be or can not be also physical locations, can be positioned at a place, or also can be distributed in multiple network element.Can select according to the actual needs some or all of module wherein to realize the object of the present embodiment scheme.Those of ordinary skills, in the situation that not paying creative work, are appreciated that and implement.
Above the embodiment of the present invention is described in detail, has applied embodiment herein the present invention is set forth, the explanation of above embodiment is just for helping to understand method and apparatus of the present invention; , for one of ordinary skill in the art, according to thought of the present invention, all will change in specific embodiments and applications, in sum, this description should not be construed as limitation of the present invention meanwhile.