CN112100084A - Method and device for generating intelligent contract test case - Google Patents

Method and device for generating intelligent contract test case Download PDF

Info

Publication number
CN112100084A
CN112100084A CN202011283034.8A CN202011283034A CN112100084A CN 112100084 A CN112100084 A CN 112100084A CN 202011283034 A CN202011283034 A CN 202011283034A CN 112100084 A CN112100084 A CN 112100084A
Authority
CN
China
Prior art keywords
test case
parameter
interface
intelligent contract
structural
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202011283034.8A
Other languages
Chinese (zh)
Other versions
CN112100084B (en
Inventor
邬萌
王海军
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Alipay Hangzhou Information Technology Co Ltd
Original Assignee
Alipay Hangzhou Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Alipay Hangzhou Information Technology Co Ltd filed Critical Alipay Hangzhou Information Technology Co Ltd
Priority to CN202011283034.8A priority Critical patent/CN112100084B/en
Publication of CN112100084A publication Critical patent/CN112100084A/en
Application granted granted Critical
Publication of CN112100084B publication Critical patent/CN112100084B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3676Test management for coverage analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites

Abstract

The embodiment of the specification provides an intelligent contract test case generation method and device, and the method comprises the following steps: firstly, determining a first interface calling sequence corresponding to a current test case, so that the first interface calling sequence can represent the calling sequence of each interface in the intelligent contract to be tested when the test case inputs the intelligent contract to be tested; then, the first interface calling sequence is subjected to variation to obtain a second interface calling sequence different from the interface calling sequence represented by the first interface calling sequence; and then generating a test case corresponding to the second interface calling sequence as a test case for inputting the intelligent contract to be tested.

Description

Method and device for generating intelligent contract test case
Technical Field
One or more embodiments of the present disclosure relate to the field of blockchain technology, and in particular, to a method and an apparatus for generating test cases for testing smart contracts in a blockchain system.
Background
An intelligent contract in a blockchain system is a set of numerically defined commitments, which include an agreement by which contract participants execute the defined commitments, whereby the intelligent contract can automatically enable mutually untrusted participants to implement the agreed agreement without the intervention of a centralized third party. The intelligent contracts are completely transparent to the participants of the blockchain system, and once the deployment is hard to change, the intelligent contracts need to be tested before the intelligent contracts are deployed so as to ensure the safety and correctness of the deployed intelligent contracts.
When the intelligent contract is tested, a series of test cases are required to be generated as the input of the intelligent contract, so that the intelligent contract can cover different program paths when each test case is executed, and the intelligent contract is comprehensively tested. However, since the smart contract generally has a larger number of program paths, and a complex smart contract including a plurality of interfaces has more program paths, it is difficult for the generated test case to cover a sufficient number of program paths, resulting in a less comprehensive test of the smart contract.
Accordingly, improved solutions are desired that can generate test cases that cover more program paths of smart contracts.
Disclosure of Invention
One or more embodiments of the present specification describe a method and apparatus for generating intelligent contract test cases, which can generate test cases covering more program paths of an intelligent contract.
In a first aspect, the present specification provides a method for generating intelligent contract test cases, comprising:
determining a first interface calling sequence corresponding to a current test case;
the interface calling sequence corresponding to one test case is used for representing the calling sequence of the interface in the intelligent contract to be tested when the test case is input into the intelligent contract to be tested;
performing variation on the first interface calling sequence to obtain a second interface calling sequence, wherein the second interface calling sequence is different from the calling sequence represented by the first interface calling sequence;
and generating a test case corresponding to the second interface calling sequence, and taking the test case as the test case for inputting the intelligent contract to be tested.
In a second aspect, the present specification also provides an apparatus for generating intelligent contract test cases, including:
the sequence determining unit is configured to determine, for a current test case, a first interface calling sequence corresponding to the test case, where the interface calling sequence corresponding to one test case is used to characterize a calling sequence of an interface in an intelligent contract to be tested when the test case is input into the intelligent contract to be tested;
a sequence variation unit configured to perform variation on the first interface call sequence determined by the sequence determination unit to obtain a second interface call sequence, where the second interface call sequence is different from the call sequence characterized by the first interface call sequence;
and the first generation unit is configured to generate a test case corresponding to the second interface calling sequence obtained by the sequence variation unit and take the test case as a test case for inputting the intelligent contract to be tested.
According to the method and the device for generating the intelligent contract test case, after the interface calling sequence corresponding to the current test case is determined, the current test case is subjected to variation according to the determined interface calling sequence to obtain a new test case, the newly generated test case and the current test case can be ensured to call the interface in the intelligent contract to be tested in different sequences, and different program paths in the intelligent contract to be tested can be possibly covered, so that the test case covering more program paths in the intelligent contract to be tested can be generated more quickly, the efficiency of testing the intelligent contract is improved, and the comprehensiveness of testing the intelligent contract is improved.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
FIG. 1 is a schematic diagram of an implementation scenario provided by an embodiment of the present description;
FIG. 2 is a flow diagram of a method for generating intelligent contract test cases provided by one embodiment of the present description;
FIG. 3 is a flow diagram of another method for generating intelligent contract test cases provided by one embodiment of the present description;
FIG. 4 is a diagram illustrating a parameter structure template according to one embodiment of the present disclosure;
FIG. 5 is a schematic diagram of another implementation scenario provided by an embodiment of the present description;
FIG. 6 is a schematic diagram of an apparatus for generating smart contract test cases provided in one embodiment of the present specification;
FIG. 7 is a schematic diagram of another apparatus for generating intelligent contract test cases provided in one embodiment of the present description;
FIG. 8 is a schematic diagram of yet another apparatus for generating intelligent contract test cases provided in one embodiment of the present specification;
fig. 9 is a schematic diagram of still another apparatus for generating intelligent contract test cases according to an embodiment of the present disclosure.
Detailed Description
As previously described, to ensure the correctness of intelligent contracts deployed into a blockchain system, the intelligent contracts need to be tested before being deployed onto the blockchain system. The basic idea of testing the intelligent contract is to generate a series of test cases which are used as input in the actual use process of the simulated intelligent contract, input the generated test cases into the intelligent contract, obtain the execution action or the output result of the intelligent contract, and further determine whether the intelligent contract has errors according to the matching between the input test cases and the obtained execution action or the output result.
However, the number of program paths included in the intelligent contract is related to the number of interfaces on the intelligent contract, and different calling sequences of the interfaces may cause different program paths in the intelligent contract to be executed, but when a test case is generated by using a simple random variation strategy such as randomly deleting bytes, repeating bytes, inserting bytes, left-shifting bytes and right-shifting bytes, the calling sequence of the interfaces on the intelligent contract is not concerned, so that the generated test case is generally the same as the calling sequence of the initial test case to the interfaces on the intelligent contract, which may cause a large part of the program paths in the intelligent contract to be not covered during the test process, and therefore the generated test case may not cover a sufficient number of program paths in the intelligent contract, and further cause the test to the intelligent contract to be incomplete.
In order to generate a test case covering more program paths in an intelligent contract, according to the concept of the embodiment of the present specification, first, a first interface call sequence for characterizing the call sequence of each interface in the intelligent contract to be tested when the test case is input into the intelligent contract to be tested is determined for the current test case, then, the first interface call sequence is subjected to variation, a second interface call sequence different from the call sequence of the interface characterized by the first interface call sequence is obtained, and then, a test case corresponding to the second interface call sequence is generated as the test case of the intelligent contract to be tested. Because the current test case and the newly generated test case correspond to different interface calling sequences, the current test case and the newly generated test case can call the interfaces on the intelligent contract in different sequences to cover different program paths in the intelligent contract, so that the test cases covering more program paths in the intelligent contract can be generated, and the comprehensiveness of testing the intelligent contract is improved.
Fig. 1 is a schematic view of an implementation scenario of an embodiment disclosed in this specification. As shown in fig. 1, for a current test case, a first interface call sequence corresponding to the test case is first determined, where the first interface call sequence represents a call sequence of each interface in an intelligent contract to be tested after the test case inputs the intelligent contract to be tested, a second interface call sequence is obtained by mutating the first interface call sequence, then a test case corresponding to the second interface call sequence is generated, and the generated test is input into the intelligent contract to be tested to test the intelligent contract. Because the first interface calling sequence is different from the second interface calling sequence, the calling sequence of the current test case and the newly generated test case to the interface in the intelligent contract to be tested is different, so that the test case for calling the interface in the intelligent contract to be tested according to different sequences can be generated, the purpose of generating the test case covering more program paths in the intelligent contract is achieved, and the comprehensiveness of testing the intelligent contract is improved.
FIG. 2 illustrates a method of generating intelligent contract test cases, according to one embodiment. It is to be appreciated that the method can be performed by any apparatus, device, platform, and cluster of devices having computing and processing capabilities. As shown in fig. 2, the method for generating the intelligent contract test case may include the following steps:
step 22: determining a first interface calling sequence corresponding to a current test case, wherein the interface calling sequence corresponding to one test case is used for representing the calling sequence of an interface in an intelligent contract to be tested when the test case is input into the intelligent contract to be tested;
step 24: performing variation on the first interface calling sequence to obtain a second interface calling sequence, wherein the calling sequence represented by the second interface calling sequence is different from that represented by the first interface calling sequence;
step 26: and generating a test case corresponding to the second interface calling sequence, and taking the test case as a test case for inputting the intelligent contract to be tested.
In this embodiment, the first interface calling sequence represents a calling sequence of a current test case to each interface in the intelligent contract to be tested, the second interface calling sequence represents a calling sequence of a newly generated test case to each interface in the intelligent contract to be tested, and since the first interface calling sequence and the second interface calling sequence are different, the calling sequences of the current test case and the newly generated test case to each interface in the intelligent contract to be tested are different, that is, a test case calling the interfaces in the intelligent contract to be tested according to different sequences can be generated to cover more program paths in the intelligent contract to be tested, thereby improving the comprehensiveness of testing the intelligent contract to be tested.
In this embodiment, an interface calling sequence includes at least one interface identifier, where the interface identifier is used to identify an interface in an intelligent contract to be tested, different interface identifiers are used to identify different interfaces in the intelligent contract to be tested, and the sequence of each interface identifier in the interface calling sequence is used to characterize the calling sequence of the interfaces in the intelligent contract to be tested, that is, the sequence in which each interface in the intelligent contract to be tested is called after a test case corresponding to the interface calling sequence is input into the intelligent contract to be tested.
For example, an interface calling sequence corresponding to one test case is an interface identifier a-an interface identifier B-an interface identifier C, where the interface identifier a is used to identify an interface a in the intelligent contract to be tested, the interface identifier B is used to identify an interface B in the intelligent contract to be tested, and the interface identifier C is used to identify an interface C in the intelligent contract to be tested. Since the sequence of 3 interface identifiers in the interface calling sequence is interface identifier a-interface identifier B-interface identifier C, when the test case corresponding to the interface calling sequence is input into the intelligent contract to be tested, the calling sequence of each interface in the intelligent contract to be tested is interface a-interface B-interface C.
In one embodiment, the step 24 obtains the second interface call sequence by performing a mutation on the first interface call sequence, and the specifically performing a mutation on the first interface call sequence may include at least one of the following: the interface identification of at least one interface in the intelligent contract to be tested is added in the first interface calling sequence, the interface identification of at least one interface in the intelligent contract to be tested is deleted from the first interface calling sequence, and the sequence of the interface identifications in the first interface calling sequence is changed. When the second interface calling sequence is generated by changing the sequence of the interface identifiers in the first interface calling sequence, the premise is that the first interface calling sequence comprises at least two interface identifiers.
When the intelligent contract to be tested comprises a plurality of interfaces, the calling sequence of each interface is different, the program paths executed by the intelligent contract to be tested are also different, a new interface calling sequence is generated by carrying out variation on the interface calling sequence, and then a test case corresponding to the newly generated interface calling sequence is generated, so that test cases for calling each interface on the intelligent contract to be tested in different sequences can be generated, more program paths in the intelligent contract to be tested are covered, and the comprehensiveness of testing the intelligent contract to be tested can be improved.
In one example, the intelligent contract to be tested comprises an interface A, an interface B and an interface C, the first interface calling sequence is an interface identifier A-interface identifier B-interface identifier C, and the second interface calling sequence is obtained by carrying out mutation on the first interface calling sequence, and the second interface calling sequence can be an interface identifier A-interface identifier B-interface identifier C, an interface identifier B-interface identifier A and an interface identifier C, an interface identifier A-interface identifier B-interface identifier C, an interface identifier A-interface identifier C or an interface identifier C-interface identifier A.
When a test case calls an interface on an intelligent contract, a structural parameter which can be identified by the interface needs to be input into the interface, so when a first interface calling sequence is changed by adding an interface identifier, a structural parameter corresponding to a newly added interface identifier needs to be added into the current test case, the newly added structural parameter is different from the original structural parameter in the current test case, and correspondingly, when the first interface calling sequence is changed by deleting the interface identifier, the structural parameter corresponding to the deleted interface identifier needs to be deleted from the current test case.
In one example, an interface call sequence X corresponding to one test case X is an interface identifier a-an interface identifier B-an interface identifier C, where the interface identifier a, the interface identifier B, and the interface identifier C are respectively used to identify an interface a, an interface B, and an interface C in an intelligent contract to be tested. The test case X comprises a structural parameter a, a structural parameter B and a structural parameter C, wherein the structural parameter a is input to the interface A after the interface A is called, the structural parameter B is input to the interface B after the interface B is called, and the structural parameter C is input to the interface C after the interface C is called. And obtaining an interface calling sequence Y by carrying out variation on the interface calling sequence X, wherein the interface calling sequence Y is an interface identifier A-an interface identifier C, and further generating a test case Y according to the interface calling sequence Y. The calling sequence of the test case Y to each interface on the intelligent contract to be tested is interface A-interface C, wherein the second calling of interface A is realized by adding an interface identifier A, therefore, except that interface identifier B is required to be deleted from interface calling sequence X and an interface identifier A is added between interface identifier A and interface identifier C, structured parameter B in test case X is also required to be deleted, and structured parameter a 'corresponding to the second interface identifier A is added in test case X, wherein the structured parameter a and the structured parameter a' have the same parameter type but different specific parameter values.
When one interface calling sequence is changed, the sequence of each interface identifier in the interface calling sequence can be randomly changed, and each interface identifier in the interface calling sequence can be deleted, inserted or changed and ordered according to a preset variation strategy and the sequence.
In one embodiment, the variation method for generating a new test case by changing the call sequence of the test case to the interface is defined as a call sequence variation. Since the test case can be generated not only by calling the sequence variation, but also by changing the parameter values and the parameter structure of the structured parameters in the current test case, it can be determined whether the current variation strategy is the calling sequence variation first before step 22, if so, the above steps 22 to 26 are executed to generate a new test case by calling the sequence variation, otherwise, the test case can be generated by parameter value variation and parameter structure variation. As shown in fig. 3, the method for generating smart contract test cases through parameter value variation and parameter structure variation may include the following steps:
step 32: aiming at the current test case, selecting at least one structural parameter to be mutated from all structural parameters included in the test case, wherein the structural parameter to be mutated can be identified by at least one interface in the intelligent contract to be tested;
step 34: obtaining a parameter structure corresponding to each structural parameter to be mutated aiming at each structural parameter to be mutated, wherein the parameter structure is determined by analyzing a pre-constructed sample structural parameter, and the sample structural parameter can be identified by at least one interface in an intelligent contract to be tested;
step 36: for each structural parameter to be mutated, according to a parameter structure corresponding to the structural parameter to be mutated, mutating the structural parameter to be mutated to obtain a structural parameter after mutation;
step 38: and generating a test case comprising each varied structural parameter, and taking the test case as a test case of the intelligent contract to be tested.
In this embodiment, for a current test case, one or more structural parameters to be varied are selected from the structural parameters included in the test case, and it is ensured that the selected structural parameters to be varied can be identified by an interface in an intelligent contract to be tested, and then the structural parameters to be varied are varied according to the parameter structure of the structural parameters to be varied to obtain varied structural parameters, so that the varied structural parameters and the corresponding structural parameters to be varied have the same parameter structure, and further, the test case including each varied structural parameter is generated as a test case for inputting the intelligent contract to be tested. Because the mutated structural parameters and the corresponding to-be-mutated structural parameters have the same parameter structure, and the to-be-mutated structural parameters can be recognized by the interfaces on the intelligent contract to be tested, the mutated structural parameters can also be recognized by the interfaces on the intelligent contract to be tested, so that the generated test case can be executed by the intelligent contract to be tested and covers the program path deeper than the intelligent contract to be tested, and the efficiency of testing the intelligent contract to be tested is improved.
In step 32, a structural parameter to be mutated is selected from the structural parameters included in the current test case, and it is ensured that the selected structural parameter to be mutated can be identified by the interface of the intelligent contract to be tested. It is understood that the intelligent contract to be tested includes at least one interface, each of the screened structured parameters to be mutated is capable of being identified by the at least one interface included in the intelligent contract to be tested, and the current test case includes at least one structured parameter, preferably each of the structured parameters included in the current test case is capable of being identified by the at least one interface on the intelligent contract to be tested.
Because each structural parameter in the test case is used for calling an interface on the intelligent contract to be tested, and different structural parameters may be used for calling the same interface on the intelligent contract to be tested, if each structural parameter included in the current test case can be identified by at least one interface on the intelligent contract to be tested, when a new test case is generated based on the current test case, more structural parameters to be varied can be selected, and thus a plurality of different test cases can be generated.
Furthermore, each structural parameter included in the current test case can be identified by the interface called by the structural parameter, so that the current test case can be completely analyzed and executed by the intelligent contract to be tested, and a new test case generated based on the current test case can also be completely analyzed and executed by the intelligent contract to be tested, so as to increase the program path which can be covered by a single test case, and further improve the efficiency of testing the intelligent contract.
In step 34, a parameter structure of each of the selected to-be-mutated structural parameters is obtained, so that the corresponding to-be-mutated structural parameters are subsequently mutated according to the parameter structure, and the mutated structural parameters and the corresponding to-be-mutated structural parameters have the same parameter structure, thereby ensuring that the obtained mutated structural parameters can be identified by the interface in the intelligent contract to be tested.
Specifically, for the current test case, the test case includes at least one structural parameter, and the structural parameter included in the test case may be a pre-constructed sample structural parameter or a structural parameter obtained by modifying the sample structural parameter, where the structural parameter obtained by modifying the sample structural parameter refers to a structural parameter obtained by modifying the sample structural parameter one or more times. The sample structured parameters can be identified by at least one interface in the intelligent contract to be tested. In the process of generating the test case by performing variation on the structural parameters, the structural parameters to be varied and the varied structural parameters have the same parameter structure, so that the structural parameters varied from the sample structural parameters have the same parameter structure as the sample structural parameters, and therefore the structural parameters varied from the sample structural parameters can be identified by at least one interface in the intelligent contract to be tested.
Since the structuring parameters in the current test case are the sample structuring parameters or the structuring parameters varied from the sample structuring parameters, each selected structuring parameter to be varied has the same parameter structure as at least one sample structuring parameter, so that the parameter structure of the structuring parameter to be varied can be determined according to the parameter structure of the sample structuring parameters, and the parameter structure of the sample structuring parameters can be determined by performing structure analysis on the sample structuring parameters. The parameter structure of the structural parameters to be mutated is determined through the parameter structure of the sample structural parameters, and the structural analysis of the structural parameters to be mutated is not required to be carried out every time a new test case is generated, so that the efficiency of generating the test case can be improved.
When the sample structured parameters are constructed, one or more sample structured parameters capable of being identified by the interface can be constructed respectively for each interface on the intelligent contract to be tested according to the interface parameters of the interface. Or, an initial test case may be constructed for the intelligent contract to be tested, where the initial test case includes at least one sample structured parameter, and each sample structured parameter can be identified by at least one interface in the intelligent contract to be tested, that is, each sample structured parameter conforms to a complex data structure of at least one interface on the intelligent contract to be tested. When the sample structured parameters are obtained by constructing the initial test case, in order to ensure that each interface in the intelligent contract to be tested has the opportunity to be called by the generated test case, for each interface included in the intelligent contract to be tested, the initial test case includes at least one sample structured parameter which can be identified by the interface.
For example, the initial test case includes a sample structural parameter 1, a sample structural parameter 2, and a sample structural parameter 3, the intelligent contract to be tested includes an interface 1, an interface 2, and an interface 3, and the sample structural parameter 1 can be identified by the interface 1, the sample structural parameter 2 can be identified by the interface 1 and the interface 2, and the sample structural parameter 3 can be identified by the interface 2 and the interface 3. Selecting a structural parameter 1 to be varied and a structural parameter 2 to be varied from a current test case, wherein the structural parameter 1 to be varied is a sample structural parameter 1, and the structural parameter 2 to be varied is a structural parameter varied from the sample structural parameter 2. Therefore, when determining the parameter structure of the structural parameter to be mutated, it is determined that the structural parameter 1 to be mutated and the sample structural parameter 1 have the same parameter structure, and it is determined that the structural parameter 2 to be mutated and the sample structural parameter 2 have the same parameter structure.
At step 38, test cases including the varied structured parameters are generated, and the generated test cases are used as test cases for inputting the intelligent contract to be tested. When a new test case is obtained by performing variation on a test case, all the structural parameters included in the test case are usually not varied, and only one or more structural parameters are selected as structural parameters to be varied, so that the generated new test case usually does not include only varied structural parameters. For this purpose, test cases can be generated which include the post-variation structural parameters and structural parameters of the current test cases which are not selected as structural parameters to be varied, as test cases for inputting intelligent contracts to be tested.
For example, the current test case includes a structural parameter 1, a structural parameter 2 and a structural parameter 3, where the structural parameter 1 is selected as the structural parameter to be mutated, and the structural parameter is obtained by mutating the structural parameter 1
Figure 128460DEST_PATH_IMAGE001
And further generating a data set comprising the structured parameters
Figure 248863DEST_PATH_IMAGE001
Structural parameters 2 andand structuring the test case of the parameter 3 as a test case for inputting the intelligent contract to be tested.
In addition, when generating a test case including each post-variation structural parameter, in addition to including all structural parameters, which are not selected in the current test case as structural parameters to be varied, in the generated test case, the generated test case may include only a part of structural parameters, which are not selected in the current test case as structural parameters to be varied, or the generated test case may include other structural parameters except the post-variation structural parameters to be tested and the structural parameters in the current test case, but it is required to ensure that each structural parameter in the generated test case can be recognized by at least one interface on the intelligent contract.
In one embodiment, step 36 performs variation on a structural parameter to be varied according to a parameter structure of the structural parameter to be varied to obtain a corresponding post-variation structural parameter, where the variation performed on the structural parameter to be varied may include at least one of the following two variation forms:
variant one: carrying out parameter value variation on the structural parameter to be varied to change the parameter value of the structural parameter to be varied;
variant II: and carrying out parameter structure variation on the structural parameter to be varied so as to change the parameter structure of the structural parameter to be varied.
The two variants are described below.
For variant one:
when one structural parameter to be mutated includes at least one original type data, and the parameter structure of the structural parameter to be mutated is used for representing the data type of each original type data in the structural parameter to be mutated, at least one original type data to be mutated can be selected from each original type data included in the structural parameter to be mutated, then the data type of each original type data to be mutated is determined according to the parameter structure of the structural parameter to be mutated, and then parameter value mutation is performed on the original type data to be mutated according to the data type of each original type data to be mutated, so that the parameter value of the whole structural parameter to be mutated is changed, and a structural parameter after mutation different from the structural parameter to be mutated is obtained.
Each test case comprises one or more structured parameters, each structured parameter comprises one or more original type data, and each original type data is composed of specific parameter values. Each structured parameter is used for calling an interface in the intelligent contract to be tested, and different original type data in the same structured parameter can have the same or different data types.
The parameter structure of a structured parameter can be used to characterize the data type of each original type data in the structured parameter, the data type defines the data structure type of the original type data, and the data type can also define the number of bits of bytes included in the original type data. The data structure type of the original type data may be an integer type, a floating point type, a character string type or a boolean type, and the number of bits of the bytes included in the original type data may be a fixed number of bits or an arbitrary number of bits, so that the data type of the original type data defines one or both of the data structure type and the number of bits of the bytes of the original type data. For example, a parameter structure of a structured parameter characterizes that the structured parameter includes original type data 1, original type data 2, and original type data 3, where the data type of the original type data 1 is a 64-bit integer type, the data type of the original type data 2 is a 256-bit hash type, and the data type of the original type data 3 is a character string type.
The to-be-mutated structured parameters can be identified by at least one interface in the intelligent contract to be tested, so that the parameter structure of the to-be-mutated structured parameters corresponds to the complex data structure of the corresponding interface in the intelligent contract to be tested, the structured parameters meeting the parameter structure can be called by data analysis of the intelligent contract to be tested, and then the structured parameters are executed by the intelligent contract to be tested to cover the program path deeper than the intelligent contract to be tested.
When a structural parameter to be mutated is varied, parameter value variation may be performed only on one original type data in the structural parameter to be mutated, or parameter value variation may be performed simultaneously on a plurality of original type data in the structural parameter to be mutated. The number of the original type data involved in the variation of one structural parameter to be varied may be defined by a preset variation strategy, for example, only one original type data in one structural parameter to be varied is subjected to parameter value variation each time one structural parameter to be varied is varied.
Further, when parameter value variation is performed on one original type data to be varied, if the data structure only defines the data structure type of the original type data to be varied, that is, if the data type of the original type data to be varied is a data type with unlimited length, at least one of modifying byte value, deleting byte, inserting byte and shifting byte can be performed on the original type data to be varied, so as to implement parameter value variation on the original type data to be varied; if the data structure defines the data structure type and the bit number of the included byte of the original type data to be mutated, that is, the data type of the original type data to be mutated is a data type with a limited length, at least one of modifying the byte value and shifting the byte can be performed on the original type data to be mutated, so as to implement the parameter value mutation on the original type data to be mutated.
Modifying the byte value means modifying the value of a certain byte, for example, modifying the value of a certain byte in the original type data to be mutated from 1 to 0. Shifting bytes refers to shifting a byte in the original type of data to be mutated one or more byte bits to the left or right. In addition, when parameter value variation is performed on the original type data to be varied, a certain byte in the original type data to be varied can be randomly selected to perform byte modification, byte deletion, byte insertion or byte shifting, and bytes in the original type data to be varied can also be sequentially selected according to a preset variation strategy to perform byte modification, byte deletion, byte insertion or byte shifting.
It can be understood that, when the parameter value of the original type data to be mutated is changed by inserting bytes, one or more bytes in the original type data to be mutated can be copied to be inserted into corresponding positions in the original type data to be mutated, so that the situation that the original type data after being mutated cannot be identified by the intelligent contract to be tested due to the fact that the value of the inserted node exceeds the identifiable value range can be avoided, and the efficiency of generating an effective test case can be further improved.
When parameter value variation is carried out on original type data to be varied, parameter value variation is carried out on the original type data to be varied according to the data type of the original type data to be varied in a parameter structure, and it is guaranteed that new original type data obtained through variation and the original type data to be varied have the same data type, so that it is guaranteed that generated varied structured parameters can still be identified by corresponding interfaces in an intelligent contract to be tested, and it is guaranteed that each generated test case can cover a program path at a deeper position in the intelligent contract to be tested, and not only the program path at the shallowest position in the intelligent contract to be tested for analyzing and checking input data.
For variant two:
and performing parameter structure variation on the structural parameters to be varied to generate post-variation structural parameters different from the contents described by the structural parameters to be varied, wherein the post-variation structural parameters may be covered to a new program path in the intelligent contract to be tested after being input into the intelligent contract to be tested. The parameter structure variation of the structural parameter to be varied mainly includes the following three structure variation modes:
the structural variation mode is as follows: newly adding at least one original type data in the structural parameters to be mutated;
the structural variation mode two: deleting at least one original type data from the structural parameters to be mutated;
structural variation mode three: and changing the sequence of each original type data in the structural parameters to be mutated.
In view of the above first structure variation method, it is assumed that the structural parameter to be varied includes at least one original type data, and a parameter structure of the structural parameter to be varied is used to characterize a start position or an end position of each original type data in the structural parameter to be varied. When the parameter structure of a structural parameter to be mutated is changed, firstly, the initial position or the end position of each original type data in the structural parameter to be mutated is determined according to the parameter structure of the structural parameter to be mutated, and then, at least one original type data is newly added before or after the initial position or the end position of at least one original type data included in the structural parameter to be mutated, so as to change the parameter structure of the structural parameter to be mutated.
For example, a structural form of a structural parameter to be mutated is original type data 1-original type data 2-original type data 3, after determining the starting position or the ending position of the original type data 1, the original type data 2 and the original type data 3 according to the parameter structure of the structural parameter to be mutated, an original type data 4 is added before the starting position of the original type data 2, and a structural parameter after mutation, such as the original type data 1-original type data 4-original type data 2-original type data 3, is generated.
For another example, the structural form of the structural parameter to be mutated is original type data 1-original type data 2-original type data 3, after determining the initial position or the end position of the original type data 1, the original type data 2 and the original type data 3 according to the parameter structure of the structural parameter to be mutated, an original type data 5 is added before the initial position of the original type data 3, and an original type data 6 is added after the end position of the original type data 3, so as to generate the structural parameter after mutation, such as the original type data 1-original type data 2-original type data 5-original type data 3-original type data 6.
It can be understood that, when the structural parameters to be mutated are subjected to parameter structure variation by adding new original type data, one or more original type data in the structural parameters to be mutated can be copied to corresponding positions in the structural parameters to be mutated, so that the situation that the generated mutated structural parameters cannot call corresponding interfaces due to the fact that the parameter types of the randomly generated original type data cannot be identified by the intelligent contract to be tested can be avoided, and the test case generated by parameter structure variation can be ensured to be executed by the intelligent contract to be tested.
In view of the second structure variation method, it is assumed that the structural parameter to be varied includes at least two original type data, and a parameter structure of one structural parameter to be varied is used to represent a start position or an end position of each original type data in the structural parameter to be varied. When the parameter structure of a structural parameter to be mutated is varied, firstly, the starting position or the ending position of each original type data in the structural parameter to be mutated is determined according to the parameter structure of the structural parameter to be mutated, and then, at least one original type data is deleted according to the starting position or the ending position of each original type data in the structural parameter to be mutated, so as to change the parameter structure of the structural parameter to be mutated.
For example, the structural form of one structural parameter to be mutated is original type data 1-original type data 2-original type data 3, and after the initial positions or the end positions of the original type data 1, the original type data 2 and the original type data 3 are determined according to the parameter structure of the structural parameter to be mutated, the original type data 2 is deleted, and the structural parameter after mutation is generated, which is in the form of the original type data 1-the original type data 3.
For the third structural variation, since the content of the structural parameters including the same original type data but different original type data sequences is different, the intelligent approach covers different program paths when executing the structural parameters including the different original type data sequences. For example, the structured parameter a and the structured parameter B each include original type data 1 to original type data 3, but 3 pieces of original type data in the structured parameter a are sorted as original type data 1-original type data 2-original type data 3, and 3 pieces of original type data in the structured parameter B are sorted as original type data 3-original type data 2-original type data 1.
The third structural variation mode is premised on that the structural parameters to be varied comprise at least two original type data, and the parameter structure of one structural parameter to be varied is used for representing the sequence of each original type data in the structural parameters to be varied. When a structural parameter to be mutated is subjected to parameter structure variation, firstly, according to the parameter structure of the structural parameter to be mutated, the sequence of each original type data in the structural parameter to be mutated can be determined, and then, the sequence of each original type data in the structural parameter to be mutated is changed, so as to change the parameter structure of the structural parameter to be mutated.
For example, the structural form of one structural parameter to be mutated is original type data 1-original type data 2-original type data 3, after the order of the original type data 1, the original type data 2 and the original type data 3 is determined according to the parameter structure of the structural parameter to be mutated, the position between the original type data 1 and the original type data 3 in the structural parameter to be mutated is exchanged, and the structural parameter after mutation, such as the original type data 3-original type data 2-original type data 1, is generated.
In summary, when the structural parameter of the structural parameter to be mutated is subjected to parameter structure variation, at least one of adding the original type data, deleting the original type data, or changing the order of the original type data may be performed on the structural parameter to be mutated. When the parameter structure of a structural parameter to be mutated is changed, original type data can be randomly added, deleted or the sequence of each original type data can be changed in the structural parameter to be mutated, or the sequence of each original type data in the structural parameter to be mutated can be deleted, inserted or changed according to a preset mutation strategy and the sequence.
When the parameter structure of a structural parameter to be mutated is varied, the quantity and the sequence of original type data in the structural parameter to be mutated are varied according to the parameter structure of the structural parameter to be mutated to generate the structural parameter after the variation, so that test cases comprising different structural parameters can be generated, but the original type data included in the structural parameter in the test cases can still be identified by the interface of the intelligent contract to be tested, thereby generating the test cases which can possibly cover a new program path in the intelligent contract to be tested, and further improving the comprehensiveness of testing the intelligent contract to be tested on the premise of ensuring that the generated test cases can be executed by the intelligent contract to be tested.
In addition, according to the above description of performing parameter value variation and parameter structure variation on the structural parameter to be varied, the parameter structure may not only represent the data type of the original type data in the structural parameter, but also represent the sequence of each original type data in the structural parameter and the start position or end position of each original type data. The parameter structure of the structured parameter can be represented by a parameter structure template in a tree structure, the parameter structure template comprises at least one parameter structure node, each parameter structure node forms a tree structure, and each upper parameter structure node in the tree structure comprises at least one lower parameter structure node or at least one original type data.
The parameter structure template is of a tree structure, the parameter structure template of the tree structure comprises a plurality of levels, the bottom layer is original type data, each level except the bottom layer comprises at least one parameter structure node, each parameter structure node positioned on the upper level comprises at least one parameter structure node on the lower layer or at least one original type data, and the parameter structure node on the top layer represents the type name of a corresponding structured parameter. When parameter structure variation is carried out on a structural parameter, one or more parameter structure nodes are selected from a parameter structure template corresponding to the structural parameter for deletion, insertion or conversion sequencing, so that parameter structure variation can be carried out on single original type data or combination of multiple original type data, the parameter structure variation of the structural parameter is more diverse, on one hand, personalized requirements of different users can be met, and on the other hand, the efficiency of generating a test case through the parameter structure variation can be improved.
Fig. 4 shows a parameter structure template in a tree structure, where a root node of the parameter structure template is a parameter root type R for characterizing type names of corresponding structural parameters, child nodes are a parameter subtype T1 and a parameter subtype T2 for characterizing member types of parent class nodes thereof, leaf nodes are three original type data, the original type data of 64-bit integer type and the original type data of 256-bit Hash type are leaf nodes under a child node parameter subtype T1, and the original type data of a string type is a leaf node under a child node parameter subtype T2.
According to the parameter structure template shown in fig. 4, it can be determined that the corresponding structured parameter includes three original type data, and the data types and the character lengths of the three original type data are respectively a 64-bit integer type, a 256-bit Hash type and a character string type, where the character length of the character string type is an arbitrary length. But also can determine that the snapshots of the three original types of data are 64-bit integer type-256-bit Hash type-string type.
According to the above description of the embodiments, when obtaining a post-mutation structural parameter for a structural parameter to be mutated, parameter value mutation may be performed only on the structural parameter to be mutated, or both parameter value mutation and parameter structure mutation may be performed on the structural parameter to be mutated.
In one embodiment, before the structural parameters to be mutated are selected from the structural parameters in step 32, a test case that has been executed by the intelligent contract to be tested before can be determined as the current test case, and a test case generated manually can be determined as the current test case.
The test case executed by the intelligent contract to be tested is taken as the current test case, a new test case can be generated according to the program path covered when the intelligent contract to be tested executes the current test case, and the test case which is possibly covered to the new program path is generated in a targeted manner, so that the efficiency of testing the intelligent contract can be further improved, the full-automatic test of the intelligent contract to be tested can be realized, and the labor intensity of testers in the test process of the intelligent contract is reduced.
And determining the artificially generated test case as the current test case, and taking the artificially generated test case as the current test case when the test case is generated, so that the generation process of the test case can be sequentially carried out. In the test case generating process, the artificially generated test case is used as the current test case, the test case can be artificially generated according to the uncovered program path in the intelligent contract to be tested and used as the current test case, and then the test case which is mutated on the basis of the test case can more quickly cover the uncovered program path in the intelligent contract to be tested, so that the efficiency of testing the intelligent contract is further improved.
Further, when a test case executed by the intelligent contract to be tested before is determined as a current test case, firstly, the test case of the intelligent contract to be tested which is input with the value to be tested last time is determined as a first test case, then whether a new program path is covered when the first test case is executed by the intelligent contract to be tested, if the new program path is covered when the first test case is executed by the intelligent contract to be tested, the first test case is determined as the current test case, and if the new program path is not covered when the first test case is executed by the intelligent contract to be tested, a second test case is determined as the current test case, wherein the second test case is the test case which is input with the intelligent contract to be tested, and the first test case is a test case obtained by carrying out variation on the second test case.
After executing a test case, the intelligent contract to be tested generates feedback information for the test case, where the feedback information includes a code coverage condition of the intelligent contract to be tested in the process of executing the test case, for example, the feedback information may be code coverage information, and it is determined whether a new program path is covered when the test case is executed by the intelligent contract to be tested according to the feedback information. If the first test case is covered by the intelligent contract to be tested when the first test case is executed, the first test case can be mutated again to try to mutate the test case which can be covered by the new program path, so that the first test case is determined as the current test case. If the first test case is not covered with the new program path when the intelligent contract to be tested executes the first test case, the first test case is difficult to generate a test case which can be covered with the new program path through variation, so that a second test case used when the first test case is obtained through variation can be determined, the second test case is determined as the current test case, and the new test case is generated through variation on the basis of the second test case.
For example, a test case 2 is obtained by performing variation on a test case 1, feedback information of an intelligent contract to be tested is obtained after the test case 2 is executed by the intelligent contract to be tested, if it is determined according to the feedback information that a new program path is covered when the test case 2 is executed by the intelligent contract to be tested, the test case 2 is determined to be a current test case, a new test case is obtained by performing variation on the test case 2, if it is determined according to the feedback information that a new program path is not covered when the test case 2 is executed by the intelligent contract to be tested, the test case 1 is determined to be a current test case, a new test case is obtained by performing variation again on the test case 1, and it is required to ensure that the test case obtained by performing variation again on the test case 1 is different from the test case 2.
After the intelligent contract to be tested executes a test case, according to the feedback information of the intelligent contract to be tested, if the new program path is covered when the test case is executed according to the feedback information, the test case is generated by carrying out mutation on the test case, and if the test case is determined to be executed not to cover the new program path according to the feedback information, the test case used when the test case is generated by the mutation is mutated again to generate the new test case.
It can be understood that the generated test case may be input into the intelligent contract to be tested through the blockchain execution engine, and the execution feedback (feedback information) of the intelligent contract to be tested is obtained through the blockchain execution engine, so as to generate a new test case again according to the execution feedback. In addition, a stake can be inserted into the intelligent contract to be tested, the value of the stake is set to 0 or 1 after the intelligent contract to be tested is executed to a stake inserting position, and then after the intelligent contract to be tested is executed with a test case, which program paths are covered can be determined according to the number of the stakes set to 0 or 1, so that feedback information can be obtained.
In an embodiment, an initial test case may be obtained, the initial test case is analyzed to obtain a value of each original type data in the initial test case as a user dictionary of the corresponding original type data, when a structural parameter to be tested is changed, at least one original type data to be changed is determined from each original type data included in the current test case according to a preset parameter value variation strategy, then, for each selected original type data to be changed, a user dictionary corresponding to the original type data to be changed is determined, and then, at least one of modification of a byte value, deletion of a byte, insertion of a byte, and shifting of a byte is performed on the user dictionary corresponding to the original type data to be changed according to the parameter value variation strategy.
The initial test case is a test case which can be normally executed by the intelligent contract to be tested, so that the value of each original type data in the initial test case is also a normal numerical value which can be identified by the intelligent contract to be tested, the value of each original type data in the initial test case is used as a user dictionary of the corresponding original type data, and subsequently, when the parameter value of the original type data is changed, the parameter value change is realized by at least one of modifying the byte value, deleting the byte, inserting the byte and shifting the byte of the user dictionary corresponding to the corresponding original type data, so that the value of the original type data after the change can still be identified by the intelligent contract to be tested, and the efficiency of generating an effective test case is improved.
It should be noted that, when a new test case is generated based on the current test case, at least one of parameter value variation, parameter structure variation and call sequence variation may be performed on the current test case to obtain the new test case. On the basis, fig. 5 is a schematic view of an implementation scenario of another embodiment disclosed in the present specification. As shown in fig. 5, after an initial test case is generated by a tester, a parameter structure of each structured parameter in the initial test case and an interface call sequence to which the initial test case is to be tested are determined by analyzing the initial test case, and a value of each original type data in the initial test case is determined as a user dictionary, and then an automatic test scheduler selects one or more of parameter value variation, parameter structure variation and call sequence variation to generate a new test case by performing variation on the current test case according to the determined parameter structure, interface call sequence and user dictionary, and transmits the generated new test case as a contract input to an intelligent contract to be tested. And the block chain execution engine drives the intelligent contract to be tested to execute the input test case, transmits the execution feedback given by the intelligent contract to be tested to the automatic test scheduler, the automatic test scheduler performs variation on the test case generated before the test case is generated again according to the mode to obtain a new test case, and the process is repeated until the test coverage rate of the intelligent contract to be tested reaches a preset value, so that a test report for testing the intelligent contract to be tested is generated.
Reviewing the process of generating the intelligent contract test case, on one hand, after the interface calling sequence corresponding to the current test case is determined, the current test case is varied according to the determined interface calling sequence to obtain a new test case, so that the newly generated test case and the current test case can call the interfaces in the intelligent contract to be tested in different sequences, and further different program paths in the intelligent contract to be tested can be possibly covered, therefore, the test case covering more program paths in the intelligent contract to be tested can be generated more quickly, the efficiency of testing the intelligent contract is improved, and the comprehensiveness of testing the intelligent contract is improved.
On the other hand, the coarsest-granularity calling sequence variation is used for varying the number and sequence of interfaces in the intelligent contract to be tested called by the test case, the medium-granularity parameter structure variation is used for varying the number and sequence of original type data in the structured parameters, the finest-granularity parameter value variation is used for varying the value of the original type data, the test case is varied by combining the variation modes of the three granularities to generate a new test case, the generated test case is ensured to accord with the business logic of the intelligent contract to be tested, and the generated test case can cover more program paths in the intelligent contract to be tested, so that the comprehensiveness and the efficiency of testing the intelligent contract can be improved.
In another aspect, an apparatus for generating intelligent contract test cases is also provided. Fig. 6 shows an intelligent contract test case generation apparatus according to an embodiment. It is to be appreciated that the apparatus can be implemented by any apparatus, device, platform, and cluster of devices having computing and processing capabilities. As shown in fig. 6, the apparatus 60 includes:
the sequence determining unit 61 is configured to determine, for a current test case, a first interface calling sequence corresponding to the test case, where the interface calling sequence corresponding to one test case is used to characterize a calling sequence of an interface in an intelligent contract to be tested when the test case is input into the intelligent contract to be tested;
a sequence variation unit 62, configured to perform variation on the first interface call sequence determined by the sequence determination unit 61 to obtain a second interface call sequence, where the second interface call sequence is different from the call sequence characterized by the first interface call sequence;
a first generating unit 63, configured to generate a test case corresponding to the second interface call sequence obtained by the sequence variation unit 63, and use the test case as a test case for inputting the intelligent contract to be tested.
In one possible embodiment, the first interface call sequence includes an interface identifier of at least one interface in the intelligent contract to be tested;
the sequence variation unit 62 is configured to add, in the first interface call sequence, an interface identifier of at least one interface in the intelligent contract to be tested, and obtain the second interface call sequence.
In one possible embodiment, the first interface call sequence includes an interface identifier of at least one interface in the intelligent contract to be tested;
the sequence variation unit 62 is configured to delete at least one interface identifier from the first interface call sequence to obtain the second interface call sequence.
In one possible embodiment, the first interface call sequence includes interface identifiers of at least two interfaces in the intelligent contract to be tested;
the sequence variation unit 62 is configured to change an ordering of the interface identifiers in the first interface call sequence, where the ordering is used to characterize an order in which the interfaces identified by the interface identifiers in the first interface call sequence are called.
One possible embodiment is based on the apparatus for generating intelligent contract test cases shown in fig. 6, and as shown in fig. 7, the apparatus for generating intelligent contract test cases further includes:
a mode determining unit 64 configured to determine whether the current variation policy is a calling sequence variation, and if so, trigger the sequence determining unit 61 to execute the current test case and determine a first interface calling sequence corresponding to the test case;
a parameter selecting unit 65, configured to select, for a current test case, at least one to-be-mutated structural parameter from the structural parameters included in the test case when the mode determining unit 64 determines that the current mutation policy is not a call sequence mutation, where the to-be-mutated structural parameter can be identified by at least one interface in an intelligent contract to be tested;
a structure identification unit 66 configured to obtain a parameter structure corresponding to each to-be-mutated structural parameter selected by the parameter selection unit 65, where the parameter structure is determined by analyzing a pre-constructed sample structural parameter, and the sample structural parameter can be identified by at least one interface in the intelligent contract to be tested;
a parameter variation unit 67, configured to, for each structural parameter to be varied, perform variation on the structural parameter to be varied according to the parameter structure corresponding to the structural parameter to be varied, which is obtained by the structure identification unit 66, so as to obtain a post-variation structural parameter;
a second generating unit 68, configured to generate a test case including each of the mutated structured parameters obtained by the parameter mutating unit 67, and use the test case as a test case for inputting the smart contract to be tested.
In one possible embodiment, each structured parameter includes at least one original type of data, and the parameter structure includes a data type of the original type of data. In this case, the parameter variation unit 67 includes:
the data selection subunit is configured to select at least one original type data to be mutated from each original type data included in one structural parameter to be mutated;
the type identification subunit is configured to determine the data type of each to-be-mutated original type data selected by the data selection subunit according to a parameter structure corresponding to one to-be-mutated structural parameter;
and the first mutation subunit is configured to perform parameter value mutation on the to-be-mutated original type data according to the data type of each to-be-mutated original type data determined by the type identification subunit, so as to change the parameter value of the to-be-mutated structural parameter where the to-be-mutated original type data is located.
Further, the first mutation subunit is configured to, for each of the original type data to be mutated, if the data type of the original type data to be mutated is a data type with an unlimited length, perform at least one of modifying a byte value, deleting a byte, inserting a byte, and shifting a byte on the original type data to be mutated, and if the data type of the original type data to be mutated is a data type with a limited length, perform at least one of modifying a byte value and shifting a byte on the original type data to be mutated.
Further, the unlimited length data types include: a character string type; the data types of the limit length comprise: a 64-bit integer type or a 256-bit Hash type.
In one possible embodiment, each structured parameter includes at least one original type data, and the parameter structure includes a start position or an end position of the original type data. In this case, the parameter variation unit 67 includes:
a first position identification subunit, configured to determine, according to a start position or an end position of at least one original type data included in the parameter structure, a start position or an end position of each original type data in one of the to-be-mutated structured parameters;
and the data adding subunit is configured to, for one structural parameter to be mutated, identify, according to the first position, a starting position or an ending position of each original type of data in the structural parameter to be mutated, which is determined by the first position identifying subunit, and add at least one original type of data before or after the starting position or the ending position of at least one original type of data included in the structural parameter to be mutated, so as to change a parameter structure of the structural parameter to be mutated.
In one possible embodiment, each structured parameter includes at least two original type data, and the parameter structure includes a start position or an end position of the original type data. In this case, the parameter variation unit 67 includes:
a second position identification subunit, configured to determine, according to a start position or an end position of original type data included in the parameter structure, a start position or an end position of each original type data in one of the to-be-mutated structured parameters;
and a data deleting subunit, configured to, for one of the structural parameters to be mutated, identify a start position or an end position of each original type of data in the structural parameter to be mutated, which is determined by the second position identifying subunit, and delete at least one original type of data in the structural parameter to be mutated, so as to change a parameter structure of the structural parameter to be mutated.
In one possible embodiment, each structured parameter includes at least two original types of data, and the parameter structure includes an order between the original types of data. In this case, the parameter variation unit 67 includes:
the sequence identification subunit is configured to determine a sequence among the original type data in the structural parameters to be varied according to a parameter structure corresponding to the structural parameters to be varied;
and the data ordering subunit is configured to, for one structural parameter to be mutated, change the order of each original type data in the structural parameter to be mutated according to the order between each original type data in the structural parameter to be mutated determined by the order identifying subunit, so as to change the parameter structure of the structural parameter to be mutated.
In one possible embodiment, on the basis of the apparatus 60 for generating intelligent contract test cases shown in fig. 7, as shown in fig. 8, the apparatus 60 for generating intelligent contract test cases may further include:
the first case obtaining unit 69 is configured to determine a test case that has been executed by the smart contract to be tested as the current test case.
In one possible embodiment, on the basis of the apparatus 60 for generating intelligent contract test cases shown in fig. 7, as shown in fig. 9, the apparatus 60 for generating intelligent contract test cases may further include:
the first case acquiring unit 610 is configured to determine a manually generated test case as the current test case.
On the basis of the apparatus 60 for generating intelligent contract test cases shown in fig. 8, the first case obtaining unit 69 is configured to determine a test case that is to be input into the intelligent contract to be tested last time as a first test case, and determine whether a new program path is covered when the first test case is executed by the intelligent contract to be tested, if so, determine the first test case as the current test case, otherwise, determine a second test case as the current test case, where the second test case satisfies: the intelligent contract to be tested is input, and the first test case is obtained by carrying out variation on the structural parameters in the second test case.
It should be noted that the schematic structure of the embodiments in this specification does not constitute a specific limitation on the apparatus for generating the intelligent contract test case. In other embodiments of the specification, the means for generating intelligent contract test cases may include more or fewer components than shown, or combine certain components, or split certain components, or a different arrangement of components. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
For the information interaction, execution process, and other contents between the units in the apparatus, the specific contents may refer to the description in the method embodiment of the present specification because the same concept is based on the method embodiment of the present specification, and are not described herein again.
An embodiment of a further aspect provides a computer-readable storage medium, on which a computer program is stored, which, when executed in a computer, causes the computer to execute the method for generating an intelligent contract test case described in the above embodiments.
In another aspect, a computing device is further provided, where the computing device includes a memory and a processor, where the memory stores executable codes, and the processor executes the executable codes to implement the method for generating an intelligent contract test case according to the foregoing embodiments.
Those skilled in the art will recognize that in one or more of the examples described above, the functions described in this specification can be implemented in hardware, software, firmware, or any combination thereof. When implemented in software, the functions may be stored on or transmitted over as one or more instructions or code on a computer-readable medium.
The above-mentioned embodiments, objects, technical solutions and advantages of the specification are further described in detail, it should be understood that the above-mentioned embodiments are only illustrative of the embodiments of the specification, and are not intended to limit the scope of the specification, and any modifications, equivalent substitutions, improvements and the like made on the basis of the technical solutions of the specification should be included in the scope of the specification.

Claims (18)

1. A method of generating intelligent contract test cases, comprising:
determining a first interface calling sequence corresponding to a current test case;
the interface calling sequence corresponding to one test case is used for representing the calling sequence of the interface in the intelligent contract to be tested when the test case is input into the intelligent contract to be tested;
performing variation on the first interface calling sequence to obtain a second interface calling sequence, wherein the second interface calling sequence is different from the calling sequence represented by the first interface calling sequence;
and generating a test case corresponding to the second interface calling sequence, and taking the test case as the test case for inputting the intelligent contract to be tested.
2. The method of claim 1, wherein the first sequence of interface calls includes interface identifications of two or more interfaces in the smart contract to be tested;
the mutating the first interface calling sequence to obtain a second interface calling sequence includes:
in the first interface calling sequence, adding an interface identifier of at least one interface in the intelligent contract to be tested to obtain a second interface calling sequence;
and/or the presence of a gas in the gas,
deleting the interface identifier of at least one interface in the intelligent contract to be tested from the first interface calling sequence to obtain a second interface calling sequence;
and/or the presence of a gas in the gas,
when the first interface calling sequence comprises at least two interface identifiers, changing the sequence of the interface identifiers in the first interface calling sequence, wherein the sequence is used for representing the sequence of calling the interfaces identified by the interface identifiers in the first interface calling sequence.
3. The method according to claim 1 or 2, wherein before determining, for the current test case, the first interface call sequence corresponding to the test case, further comprising:
judging whether the current variation strategy is the variation of a calling sequence, if so, continuing to execute the current test case, and determining a first interface calling sequence corresponding to the test case;
otherwise, executing:
aiming at a current test case, selecting at least one structural parameter to be mutated from all structural parameters included in the test case, wherein the structural parameter to be mutated can be identified by at least one interface in the intelligent contract to be tested;
obtaining a parameter structure corresponding to each structural parameter to be mutated for each structural parameter to be mutated, wherein the parameter structure is determined by analyzing a pre-constructed sample structural parameter, and the sample structural parameter can be identified by at least one interface in the intelligent contract to be tested;
for each structural parameter to be mutated, according to a parameter structure corresponding to the structural parameter to be mutated, mutating the structural parameter to be mutated to obtain a structural parameter after mutation;
and generating a test case comprising each varied structural parameter, and taking the test case as a test case for inputting the intelligent contract to be tested.
4. The method of claim 3, wherein each structured parameter comprises at least one original type of data, and the parameter structure comprises a start position or an end position of the original type of data;
the step of performing variation on the structural parameter to be varied according to the parameter structure corresponding to the structural parameter to be varied includes:
determining the starting position or the ending position of each original type data in the structural parameter to be mutated according to the starting position or the ending position of the original type data included in the parameter structure;
adding at least one original type data before or after the initial position or the end position of at least one original type data included in the structural parameter to be mutated, so as to change the parameter structure of the structural parameter to be mutated.
5. The method of claim 3, wherein each structured parameter comprises at least two original type data, and the parameter structure comprises a start position or an end position of original type data;
the step of performing variation on the structural parameter to be varied according to the parameter structure corresponding to the structural parameter to be varied includes:
determining the starting position or the ending position of each original type data in the structural parameter to be mutated according to the starting position or the ending position of the original type data included in the parameter structure;
and deleting at least one original type data according to the starting position or the ending position of each original type data in the structural parameter to be mutated so as to change the parameter structure of the structural parameter to be mutated.
6. The method of claim 3, wherein each structured parameter comprises at least two raw type data, and the parameter structure comprises an order between the raw type data;
the step of performing variation on the structural parameter to be varied according to the parameter structure corresponding to the structural parameter to be varied includes:
determining the sequence among the original type data in the structural parameters to be varied according to the parameter structure corresponding to the structural parameters to be varied;
and changing the sequence of each original type data in the structural parameters to be varied so as to change the parameter structure of the structural parameters to be varied.
7. The method as claimed in claim 3, wherein before the selecting, for the current test case, at least one structural parameter to be mutated from the structural parameters included in the test case, further comprising:
determining a test case which is executed by the intelligent contract to be tested as the current test case;
alternatively, the first and second electrodes may be,
and determining a manually generated test case as the current test case.
8. The method of claim 7, wherein the determining a test case that has been previously executed by the intelligent contract to be tested as the current test case comprises:
determining the test case which is input into the intelligent contract to be tested last time as a first test case;
judging whether a new program path is covered when the intelligent contract to be tested executes the first test case;
if so, determining the first test case as the current test case;
if not, determining a second test case as the current test case, wherein the second test case satisfies the following conditions: the intelligent contract to be tested is input, and the first test case is obtained by carrying out variation on the structural parameters in the second test case.
9. An apparatus for generating intelligent contract test cases, comprising:
the sequence determining unit is configured to determine, for a current test case, a first interface calling sequence corresponding to the test case, where the interface calling sequence corresponding to one test case is used to characterize a calling sequence of an interface in an intelligent contract to be tested when the test case is input into the intelligent contract to be tested;
a sequence variation unit configured to perform variation on the first interface call sequence determined by the sequence determination unit to obtain a second interface call sequence, where the second interface call sequence is different from the call sequence characterized by the first interface call sequence;
and the first generation unit is configured to generate a test case corresponding to the second interface calling sequence obtained by the sequence variation unit and take the test case as a test case for inputting the intelligent contract to be tested.
10. The apparatus of claim 9, wherein the first sequence of interface calls comprises interface identifications of two or more interfaces in the smart contract to be tested;
the sequence variation unit is configured to add an interface identifier of at least one interface in the intelligent contract to be tested in the first interface calling sequence to obtain a second interface calling sequence;
and/or the presence of a gas in the gas,
the sequence variation unit is configured to delete at least one interface identifier from the first interface calling sequence to obtain the second interface calling sequence;
and/or the presence of a gas in the gas,
the sequence mutation unit is configured to change an ordering of the interface identifiers in the first interface call sequence when the first interface call sequence includes at least two interface identifiers, where the ordering is used to characterize an order in which the interfaces identified by the interface identifiers in the first interface call sequence are called.
11. The apparatus of claim 9 or 10, further comprising:
the mode judging unit is configured to judge whether the current mutation strategy is calling sequence mutation, and if so, the sequence determining unit is triggered to execute the current test case and determine a first interface calling sequence corresponding to the test case;
the parameter selection unit is configured to select at least one structural parameter to be mutated from all structural parameters included in the test case aiming at the current test case when the mode judgment unit determines that the current mutation strategy is not calling sequence variation, wherein the structural parameter to be mutated can be identified by at least one interface in the intelligent contract to be tested;
the structure identification unit is configured to obtain a parameter structure corresponding to each to-be-mutated structural parameter selected by the parameter selection unit, wherein the parameter structure is determined by analyzing a pre-constructed sample structural parameter, and the sample structural parameter can be identified by at least one interface in the intelligent contract to be tested;
the parameter variation unit is configured to perform variation on each structural parameter to be varied according to the parameter structure corresponding to the structural parameter to be varied, which is obtained by the structure identification unit, so as to obtain a post-variation structural parameter;
and the second generating unit is used for generating a test case comprising each mutated structural parameter obtained by the parameter mutating unit and taking the test case as a test case for inputting the intelligent contract to be tested.
12. The apparatus of claim 11, wherein each structured parameter comprises at least one original type data, and the parameter structure comprises a start position or an end position of the original type data;
the parameter variation unit includes:
a first position identification subunit, configured to determine, according to a start position or an end position of at least one original type data included in the parameter structure, a start position or an end position of each original type data in one of the to-be-mutated structured parameters;
and the data adding subunit is configured to, for one structural parameter to be mutated, identify, according to the first position, a starting position or an ending position of each original type of data in the structural parameter to be mutated, which is determined by the first position identifying subunit, and add at least one original type of data before or after the starting position or the ending position of at least one original type of data included in the structural parameter to be mutated, so as to change a parameter structure of the structural parameter to be mutated.
13. The apparatus of claim 11, wherein each structured parameter comprises at least two original type data, and the parameter structure comprises a start position or an end position of original type data;
the parameter variation unit includes:
a second position identification subunit, configured to determine, according to a start position or an end position of original type data included in the parameter structure, a start position or an end position of each original type data in one of the to-be-mutated structured parameters;
and a data deleting subunit, configured to, for one of the structural parameters to be mutated, identify a start position or an end position of each original type of data in the structural parameter to be mutated, which is determined by the second position identifying subunit, and delete at least one original type of data in the structural parameter to be mutated, so as to change a parameter structure of the structural parameter to be mutated.
14. The apparatus of claim 11, wherein each structured parameter comprises at least two raw type data, and the parameter structure comprises an order between the raw type data;
the parameter variation unit includes:
the sequence identification subunit is configured to determine a sequence among the original type data in the structural parameters to be varied according to a parameter structure corresponding to the structural parameters to be varied;
and the data ordering subunit is configured to, for one structural parameter to be mutated, change the order of each original type data in the structural parameter to be mutated according to the order between each original type data in the structural parameter to be mutated determined by the order identifying subunit, so as to change the parameter structure of the structural parameter to be mutated.
15. The apparatus of claim 11, further comprising:
the first case acquisition unit is configured to determine a test case which is executed by the intelligent contract to be tested before as the current test case;
alternatively, the first and second electrodes may be,
the second case acquisition unit is configured to determine a manually generated test case as the current test case.
16. The apparatus of claim 15, wherein,
the first case obtaining unit is configured to determine a test case, which is input into the intelligent contract to be tested last time, as a first test case, and determine whether a new program path is covered when the intelligent contract to be tested executes the first test case, if so, determine the first test case as the current test case, otherwise, determine a second test case as the current test case, where the second test case satisfies: the intelligent contract to be tested is input, and the first test case is obtained by carrying out variation on the structural parameters in the second test case.
17. A computer-readable storage medium, on which a computer program is stored which, when executed in a computer, causes the computer to carry out the method of any one of claims 1-8.
18. A computing device comprising a memory having executable code stored therein and a processor that, when executing the executable code, implements the method of any of claims 1-8.
CN202011283034.8A 2020-11-17 2020-11-17 Method and device for generating intelligent contract test case Active CN112100084B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202011283034.8A CN112100084B (en) 2020-11-17 2020-11-17 Method and device for generating intelligent contract test case

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011283034.8A CN112100084B (en) 2020-11-17 2020-11-17 Method and device for generating intelligent contract test case

Publications (2)

Publication Number Publication Date
CN112100084A true CN112100084A (en) 2020-12-18
CN112100084B CN112100084B (en) 2021-02-26

Family

ID=73785687

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202011283034.8A Active CN112100084B (en) 2020-11-17 2020-11-17 Method and device for generating intelligent contract test case

Country Status (1)

Country Link
CN (1) CN112100084B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20110208469A1 (en) * 2008-05-15 2011-08-25 Simeon Falk Sheye Automatic testing of a computer software system
CN103853652A (en) * 2012-11-29 2014-06-11 百度在线网络技术(北京)有限公司 Test case generation method and device
CN111324537A (en) * 2020-02-21 2020-06-23 深圳壹账通智能科技有限公司 Test script-based test method and device and computer equipment
CN111897742A (en) * 2020-09-30 2020-11-06 支付宝(杭州)信息技术有限公司 Method and device for generating intelligent contract test case

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20110208469A1 (en) * 2008-05-15 2011-08-25 Simeon Falk Sheye Automatic testing of a computer software system
CN103853652A (en) * 2012-11-29 2014-06-11 百度在线网络技术(北京)有限公司 Test case generation method and device
CN111324537A (en) * 2020-02-21 2020-06-23 深圳壹账通智能科技有限公司 Test script-based test method and device and computer equipment
CN111897742A (en) * 2020-09-30 2020-11-06 支付宝(杭州)信息技术有限公司 Method and device for generating intelligent contract test case

Also Published As

Publication number Publication date
CN112100084B (en) 2021-02-26

Similar Documents

Publication Publication Date Title
CN111897742B (en) Method and device for generating intelligent contract test case
US5913023A (en) Method for automated generation of tests for software
Rauf et al. Automated GUI test coverage analysis using GA
CN109344053B (en) Interface coverage test method, system, computer device and storage medium
CN110334021B (en) Interface test case generation method, device, equipment and storage medium
Yang et al. Blendfuzz: A model-based framework for fuzz testing programs with grammatical inputs
CN110389896A (en) Code automated analysis and test method, device and computer readable storage medium
CN110737584A (en) Service data testing method and device based on block chain and computer equipment
CN109388557A (en) A kind of control visiting method, computer readable storage medium and terminal device
CN107608873B (en) Software testing method and system and testing platform
CN111221721B (en) Automatic recording and executing method and device for unit test cases
CN108897588B (en) Routing method and routing device for communication between modules
US20080104576A1 (en) Method and arrangement for locating input domain boundaries
CN112100084B (en) Method and device for generating intelligent contract test case
Walls et al. Discovering specification violations in networked software systems
CN112799959A (en) Interface test method, device, storage medium and electronic device
CN106776284A (en) A kind of method for testing software and system
CN113918473B (en) Swagger document-based RESTful APIs combined test method
US6681374B1 (en) Hit-or-jump method and system for embedded testing
CN110059485A (en) Privately owned API detection method, terminal and the storage medium of IOS application
Wever et al. Active coevolutionary learning of requirements specifications from examples
US20050177773A1 (en) Software method for exhaustive variation of parameters, independent of type
CN113515452A (en) Automatic test method and system for application, electronic equipment and storage medium
CN112765038A (en) Distributed cluster software testing method, device, equipment and storage medium
CN111382066A (en) Software defined network application security attribute testing method in development

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
REG Reference to a national code

Ref country code: HK

Ref legal event code: DE

Ref document number: 40042995

Country of ref document: HK