[go: up one dir, main page]

CN119902971A - Code testing method, device, medium and electronic equipment - Google Patents

Code testing method, device, medium and electronic equipment Download PDF

Info

Publication number
CN119902971A
CN119902971A CN202311377715.4A CN202311377715A CN119902971A CN 119902971 A CN119902971 A CN 119902971A CN 202311377715 A CN202311377715 A CN 202311377715A CN 119902971 A CN119902971 A CN 119902971A
Authority
CN
China
Prior art keywords
function
code
library
static
original
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.)
Pending
Application number
CN202311377715.4A
Other languages
Chinese (zh)
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.)
Tencent Technology Shanghai Co Ltd
Original Assignee
Tencent Technology Shanghai 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 Tencent Technology Shanghai Co Ltd filed Critical Tencent Technology Shanghai Co Ltd
Priority to CN202311377715.4A priority Critical patent/CN119902971A/en
Publication of CN119902971A publication Critical patent/CN119902971A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Prevention of errors by analysis, debugging or testing of software
    • G06F11/3668Testing of software
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Prevention of errors by analysis, debugging or testing of software
    • G06F11/3668Testing of software
    • G06F11/3672Test management
    • G06F11/3692Test management for test results analysis
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Software Systems (AREA)
  • Stored Programmes (AREA)

Abstract

本申请属于计算机技术领域,具体涉及一种代码测试方法、代码测试装置、计算机可读介质、电子设备以及计算机程序产品。该代码测试方法,包括:获取与待测试的源代码相对应的替换函数列表,替换函数列表用于保存对源代码中的原始函数进行替换调用的包装函数;根据替换函数列表以及具有录制回放功能的库函数生成规则文件,规则文件用于定义源代码的测试项目的构建规则;根据规则文件对源代码进行编译处理,得到待测试的目标代码;当编译至源代码中的原始函数时,将原始函数替换为调用包装函数;当编译至源代码中的库函数时,将库函数的调用地址替换为具有录制回放功能的库函数;对目标代码执行录制回放测试。本申请实施例可以提高代码测试效率。

The present application belongs to the field of computer technology, and specifically relates to a code testing method, a code testing device, a computer-readable medium, an electronic device, and a computer program product. The code testing method comprises: obtaining a replacement function list corresponding to a source code to be tested, the replacement function list is used to save a wrapper function that replaces the original function in the source code; generating a rule file according to the replacement function list and a library function with a recording and playback function, the rule file is used to define the construction rules of the test project of the source code; compiling the source code according to the rule file to obtain the target code to be tested; when compiled to the original function in the source code, the original function is replaced by a call to the wrapper function; when compiled to the library function in the source code, the call address of the library function is replaced by a library function with a recording and playback function; and performing a recording and playback test on the target code. The embodiment of the present application can improve the efficiency of code testing.

Description

Code testing method, device, medium and electronic equipment
Technical Field
The application belongs to the technical field of computers, and particularly relates to a code testing method, a code testing device, a computer readable medium, electronic equipment and a computer program product.
Background
The flow recording and playback is a common background test technology, and by recording online flow and using the same input parameters during playback, the output parameters of online environment and test environment are compared, so as to judge whether the interface code logic of the test environment is abnormal. However, in the process of recording the online flow, a lot of noise flow irrelevant to the test requirement is often recorded, and the noise needs to be manually screened out or the original code needs to be modified, so that the problem of low test efficiency exists.
Disclosure of Invention
The application provides a code testing method, a code testing device, a computer readable medium, an electronic device and a computer program product, and aims to improve code testing efficiency.
Other features and advantages of the application will be apparent from the following detailed description, or may be learned by the practice of the application.
According to one aspect of the embodiment of the application, a code testing method is provided, and the code testing method comprises the steps of obtaining a replacement function list corresponding to source codes to be tested, wherein the replacement function list is used for storing a packaging function for replacing and calling original functions in the source codes, generating a rule file according to the replacement function list and a library function with a recording and playback function, wherein the rule file is used for defining construction rules of test items of the source codes, compiling the source codes according to the rule file to obtain target codes to be tested, replacing the original functions with the packaging functions when compiling the original functions in the source codes, replacing calling addresses of the library functions with the recording and playback function when compiling the library functions in the source codes, and executing recording and playback tests on the target codes.
According to an aspect of an embodiment of the present application, there is provided a code testing apparatus including:
The acquisition module is configured to acquire a replacement function list corresponding to source codes to be tested, wherein the replacement function list is used for storing a packing function for replacing and calling an original function in the source codes;
a generation module configured to generate a rule file according to the replacement function list and a library function with a recording playback function, wherein the rule file is used for defining a construction rule of a test item of the source code;
The compiling module is configured to compile the source code according to the rule file to obtain an object code to be tested, replace the original function with the calling packaging function when compiling the original function into the source code, and replace the calling address of the library function with the recording and playback function when compiling the library function into the source code;
and the test module is configured to execute a recording playback test on the target code.
In some embodiments of the present application, based on the above technical solution, the code testing device further includes:
the function library acquisition module is configured to acquire a function library for calling the source code according to the code language of the source code;
A library function screening module configured to screen the original library functions from the function library that can introduce noise in the recording playback test;
and the library function writing module is configured to rewrite the original library function to obtain the library function with the recording and playback functions.
In some embodiments of the present application, based on the above technical solution, the library function writing module is further configured to create a proxy function corresponding to the original library function, write recording logic and playback logic into the proxy function to obtain a library function with a recording playback function, where the recording logic is configured to record output parameters of the original library function in a recording mode, and the playback logic is configured to read the output parameters of the original library function recorded in the recording mode in a playback mode, and to call the original library function in a non-playback mode.
In some embodiments of the present application, based on the above technical solutions, the original library functions capable of introducing noise in the recording playback test include a function whose output parameters have a temporal correlation or a function whose output parameters have a randomness.
In some embodiments of the present application, based on the above technical solution, the code testing device further includes:
And the function writing module is configured to write the replacement function list after the library function with the recording and playback function is associated with the original library function.
In some embodiments of the present application, based on the above technical solutions, the acquiring module includes:
The analysis module is configured to analyze the source code to be tested to obtain an abstract syntax tree of the source code;
the traversing module is configured to traverse the abstract syntax tree to obtain an original function of a specified type, wherein the original function of the specified type is a function needing replacement calling in a recording playback test;
And the creation module is configured to create a replacement function list corresponding to the source code to be tested according to the original function of the specified type.
In some embodiments of the present application, based on the above technical solution, the specified type of original function includes one or more of an inline function, a static function, or a class member function.
In some embodiments of the present application, based on the above technical solution, the replacing function list includes an inline function list, a static function list, or a class member function list, and the creating module includes:
The first creation module is configured to write the function name of the inline function into the inline function list when the original function of the specified type is the inline function;
the second creation module is configured to create a non-static function with the same grammar information as the static function when the original function of the designated type is the static function, and write the mapping relation between the static function and the non-static function into the static function list;
And the third creation module is configured to create a wrapper function and a real function which have a redirection relation with the class member function when the original function of the specified type is the class member function, and write the mapping relation of the wrapper function and the real function into the class member function list.
In some embodiments of the present application, based on the above technical solution, the second creation module is further configured to obtain syntax information of the static function, where the syntax information includes a file name, a line number, and function call information, rename the static function according to the file name and the line number, remove keywords in the static function that represent a definition of the static function, and obtain a non-static function, and modify a function that calls the static function to call the non-static function according to the function call information.
In some embodiments of the present application, based on the above technical solution, the third creating module is further configured to obtain a symbol table parsed from the source code to be tested, find a modified function matching the class member function according to the symbol table, create a wrapper function and a real function corresponding to the modified function, redirect the class member function to the wrapper function, and direct the real function to the class member function.
In some embodiments of the present application, based on the above technical solution, the third creating module is further configured to perform decoding processing on the symbol table to obtain a decoded name corresponding to the modified name of the class member function, find the decoded name identical to the original name of the class member function, and determine a modified function matched with the class member function according to the modified name corresponding to the decoded name.
In some embodiments of the present application, based on the above technical solution, the compiling module includes:
A parameter acquisition module configured to acquire compiling parameters added or updated to the rule file, wherein the compiling parameters comprise one or more of an inline function, a static function or a class member function;
the first compiling module is configured to skip code embedding operation on the inline function when the compiling parameter is the inline function;
The second compiling module is configured to replace the static function with a non-static function corresponding to the static function when the compiling parameter is the static function;
and the third compiling module is configured to replace the class member function with a wrapper function corresponding to the class member function when the compiling parameter is the class member function.
According to an aspect of the embodiments of the present application, there is provided a computer-readable medium having stored thereon a computer program which, when executed by a processor, implements a code testing method as in the above technical solution.
According to an aspect of an embodiment of the present application, there is provided an electronic device including a processor, and a memory for storing executable instructions of the processor, wherein the processor is configured to execute the executable instructions to implement a code testing method as in the above technical solution.
According to an aspect of the embodiments of the present application, there is provided a computer program product comprising a computer program which, when executed by a processor, implements a code testing method as in the above technical solution.
In the technical scheme provided by the embodiment of the application, the rule file can be generated according to the replacement function list and the library function with the recording playback function by acquiring the replacement function list corresponding to the source code to be tested, and the source code is further compiled according to the rule file to obtain the target code to be tested, so that the recording playback test can be executed on the target code. The embodiment of the application changes the library function and the business function in the source code into the replacement calling wrapper function by utilizing the replacement function list, thereby solving the problem that noise flow is easy to be introduced in recording and playback test, and improving the test efficiency.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the application and together with the description, serve to explain the principles of the application. It is evident that the drawings in the following description are only some embodiments of the present application and that other drawings may be obtained from these drawings without inventive effort for a person of ordinary skill in the art.
Fig. 1 shows a system architecture block diagram of a code testing system to which the technical scheme of the present application is applied.
FIG. 2 shows a flow chart of a code testing method in one embodiment of the application.
FIG. 3 illustrates a flow chart of a method of obtaining a list of replacement functions in one embodiment of the application.
FIG. 4 illustrates a flow chart of a method of creating a non-static function in one embodiment of the application.
FIG. 5 illustrates a flow diagram of a method for creating a redirection function for class membership functions in one embodiment of the application.
Fig. 6 shows an interactive flow chart of recording playback testing in an application scenario according to an embodiment of the present application.
Fig. 7 schematically shows a block diagram of a code testing apparatus according to an embodiment of the present application.
Fig. 8 schematically shows a block diagram of a computer system suitable for use in implementing embodiments of the application.
Detailed Description
Example embodiments will now be described more fully with reference to the accompanying drawings. However, the exemplary embodiments can be embodied in many different forms and should not be construed as limited to the examples set forth herein, but rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the concept of the exemplary embodiments to those skilled in the art.
Furthermore, the described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments. In the following description, numerous specific details are provided to give a thorough understanding of embodiments of the application. One skilled in the relevant art will recognize, however, that the application may be practiced without one or more of the specific details, or with other methods, components, devices, steps, etc. In other instances, well-known methods, devices, implementations, or operations are not shown or described in detail to avoid obscuring aspects of the application.
The block diagrams depicted in the figures are merely functional entities and do not necessarily correspond to physically separate entities. That is, the functional entities may be implemented in software, or in one or more hardware modules or integrated circuits, or in different networks and/or processor devices and/or microcontroller devices.
The flow diagrams depicted in the figures are exemplary only, and do not necessarily include all of the elements and operations/steps, nor must they be performed in the order described. For example, some operations/steps may be decomposed, and some operations/steps may be combined or partially combined, so that the order of actual execution may be changed according to actual situations.
In the embodiments of the present application, related data such as input data of a user, online traffic, etc., when the embodiments of the present application are applied to specific products or technologies, user permission or consent needs to be obtained, and collection, use and processing of related data need to comply with related laws and regulations and standards of related countries and regions.
Fig. 1 shows a system architecture block diagram of a code testing system to which the technical scheme of the present application is applied.
As shown in fig. 1, the system architecture of the code testing system may include a terminal device 110 and a server 120. Terminal device 110 may include various electronic devices such as smart phones, tablet computers, notebook computers, desktop smart speakers, smart wearable devices, smart vehicle devices, smart payment terminals, and the like. The server 120 may be an independent physical server, a server cluster or a distributed system formed by a plurality of physical servers, or a cloud server providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDNs, basic cloud computing services such as big data and artificial intelligence platforms, and the like. There are various connection types of communication media between terminal device 110 and server 120 that provide a communication link, which may be a wired communication link or a wireless communication link, for example.
The system architecture in embodiments of the present application may have any number of terminal devices and servers, as desired for implementation. For example, the server 120 may be a server group composed of a plurality of server devices. In addition, the technical solution provided in the embodiment of the present application may be applied to the terminal device 110, or may be applied to the server 120, or may be implemented by both the terminal device 110 and the server 120, which is not limited in particular.
Taking an application scenario of the record playback test as an example, the record playback test performed on the source code 140 may include a traffic recording phase in a real service environment and a traffic playback phase in a test environment.
In the traffic recording phase, the terminal device 110 initiates a service request to the server 120, and stores corresponding request data in real time in the database 130. When the server 120 returns response data to the terminal device 110, the response data may be synchronously saved to the database 130.
In the flow playback stage, the request data stored in the database 130 is input as input parameters to the source code 140 to be tested, and corresponding output parameters are obtained by running the source code 140. By comparing whether the output parameters are consistent with the response data stored in the database 130, a corresponding test result can be obtained, thereby judging whether the source code is abnormal.
In the related art of the present application, since there is a large amount of noise data in the online traffic stored in the traffic recording stage, a tester is usually required to manually screen the required traffic. For example, when a certain functional module of the service platform is recorded on line and a scene use case is wanted to be formed, the platform is inevitably used along with other user operations, so that recorded traffic is mixed with traffic of other service scenes. If a large amount of modification and intrusion is made into the service code in order to eliminate the interference generated by the noise data and to reduce the test result validation cost for the noise data, this results in reduced code readability, and the code structure also introduces much unnecessary redundancy.
The application adopts a dynamic library preloading mode or an address replacement mode to solve the problem of code invasion.
The dynamic library preloading approach is to use the preloading environment variable to set the path of the shared library and the library will be loaded before any other library, i.e. the symbol priority in this dynamic library is highest. When the program runs, the default dynamic link library path of the system is searched first, and the needed shared library is found and the function in the shared library is loaded. However, if the preload environment variable is set, the program will first search the designated preload shared library path and load the function therein. The functions in the preloaded shared library have the same symbol names as the homonymous functions in the program, so that when the program calls the homonymous functions, the functions in the preloaded shared library are preferentially called instead of the original functions in the program. The functions in the preloaded shared libraries can be modified or replaced on the basis of the original functions, thereby changing the behavior of the program. This replacement is dynamic and only takes effect when the program is running, without modifying the source code or binary file of the program.
The address replacement mode refers to direct reading and writing of a system call table replacement address, is a technology used for Hook system call, and can be used for replacing a realization function of system call in operation. The operating principle is that the system call table is a data structure for storing the function addresses of the system call, and each system call corresponds to one function address. In Linux systems, a system call table is typically stored in kernel code, so operations using privilege levels are required to access and modify. The direct read-write system call table replacing address mode utilizes a symbol export mechanism in the kernel code to find the address of the system call table in the kernel code. Then, using the privilege level operation, the address of the system call table may be mapped to the user space so that the function address in the system call table may be read and modified in the user space. The implementation function of the replacement system call needs to define a function with the same name as the original function in the user space for replacing the implementation of the original function. And replacing the address mode by using a direct read-write system call table, and replacing the address of the function corresponding to the system call in the system call table with the address of the new function. When a program calls a system call, the kernel firstly checks the function address corresponding to the system call in the system call table, and if the function address is replaced by the address of a new function, the kernel calls the realization of the new function. Otherwise, the implementation of the original function is called.
The above implementation scheme may bring security risk, because malicious attacker can use a dynamic library preloading mode or a direct read-write system call table to replace an address mode to inject malicious codes, large complex engineering of an actual service scene usually has a complete execution framework, and the practice with potential safety hazard usually does not have the possibility of landing. Furthermore, the above implementations operate at program run-time, where there is typically a recursive trap (Recursion Trap). Recursive traps refer to situations where when a recursive function is written, the program has infinite recursion due to excessive recursion depth or incorrect recursion termination conditions, etc., thereby depleting system resources or causing program crashes.
Aiming at the problems in the related art, the embodiment of the application provides a scheme for solving the noise flow problem, which is realized at the compiling period of a program code, and the scheme can sense a system function to be recorded in real time and dynamically generate a rule file of a test item, so that the required function can be dynamically added, the dynamic replacement processing of various library functions and business functions is supported, the noise factor can be eliminated, the intrusion of the business code is avoided, and the control in the code is realized to avoid the occurrence of a recursion trap.
The following describes in detail the technical schemes such as the code testing method, the code testing device, the computer readable medium, the electronic device, the computer program product and the like provided by the application in combination with the specific embodiments.
Fig. 2 shows a flowchart of a code testing method in an embodiment of the present application, which may be executed by the terminal device or the server shown in fig. 1 alone or may be executed by both the terminal device and the server, and the embodiment of the present application is described by taking the code testing method executed by the terminal device as an example. As shown in fig. 2, the code testing method may include the following steps S210 to S240.
S210, acquiring a replacement function list corresponding to the source code to be tested, wherein the replacement function list is used for storing a wrapper function for replacing and calling an original function in the source code.
And S220, generating a rule file according to the replacement function list and the library function with the recording and playback functions, wherein the rule file is used for defining the construction rule of the test item of the source code.
S230, compiling the source code according to the rule file to obtain an object code to be tested, replacing the original function with a calling wrapper function when compiling the original function in the source code, and replacing the calling address of the library function with a recording playback function when compiling the library function in the source code.
And S240, performing a recording playback test on the target code.
In the code testing method provided by the embodiment of the application, the rule file can be generated according to the replacement function list and the library function with the recording playback function by acquiring the replacement function list corresponding to the source code to be tested, and the target code to be tested is obtained by compiling the source code according to the rule file, so that the recording playback test can be executed on the target code. The embodiment of the application changes the library function and the business function in the source code into the replacement calling wrapper function by utilizing the replacement function list, thereby solving the problem that noise flow is easy to be introduced in recording and playback test, and improving the test efficiency.
The following describes each method step of the code testing method in the embodiment of the present application in detail in connection with a specific application scenario.
In step S210, a replacement function list corresponding to the source code to be tested is acquired, and the replacement function list is used to store a wrapper function that makes a replacement call to the original function in the source code.
FIG. 3 illustrates a flow chart of a method of obtaining a list of replacement functions in one embodiment of the application. As shown in fig. 3, the method for obtaining the replacement function list in the embodiment of the present application may include the following steps S310 to S330.
S310, analyzing the source code to be tested to obtain an abstract syntax tree of the source code.
An abstract syntax tree is a tree-like representation of an abstract syntax structure of text (i.e. source code) written in a formal language. Each node on the tree represents a structure that appears in the text. The grammar is abstract in that it does not represent every detail that appears in the real grammar, but only structural or content-related details. For example, packet brackets are implicit in the tree structure, so these brackets need not be represented as separate nodes. Likewise, a grammar structure such as an if-condition-then statement may be represented by a node having three branches. The abstract syntax tree comprises lexical analysis, grammar analysis, semantic analysis and the like, and can accurately determine the type, scope, control domain and the like of each symbol in the code, and a plurality of symbol information form a symbol table.
S320, traversing the abstract syntax tree to obtain an original function of a specified type, wherein the original function of the specified type is a function needing replacement calling in a recording playback test.
In one embodiment of the application, the original functions of the specified type include one or more of inline functions, static functions, or class member functions.
Inline functions refer to functions that are modified with an inline key, and functions defined within a class are defaulted to inline functions. The inline function has a function structure from the source code layer, but does not have the function property after compiling. The inline functions do not take control transfer at call time, but rather embed a function body at compile time at each call. At compile time, similar to macro substitution, function names for calling are replaced with function volumes.
Inline functions are used to insert the code of a function directly into the place of a function call at compile time, rather than executing by way of a function call. In other words, the compiler will copy the code of the inline function to the call to eliminate the overhead of the function call. The inline functions are mainly used to optimize the performance of the program. Function calls typically require some overhead, including save and restore operations on site, parameter transfer, and jumps. For some short functions, these overheads may be relatively large, so that using inline functions may avoid these additional overheads, improving the execution efficiency of the program.
The static function refers to a function decorated by a static keyword, and the static function comprises a static class member function and a global static function. The static functions that appear in a class are static class member functions that function in that invoking this function does not access or modify any non-static data objects. Static members (variables and methods) of a class belong to the class itself, memory is allocated when the class is loaded, and can be accessed directly through class names, and non-static members (variables and methods) belong to objects of the class, so that memory is allocated only when the objects of the class are generated (an instance of the class is created), and then accessed through the objects (instance) of the class. Static functions that do not appear in a class are global static functions. The global static function is different from the common function in that the function decorated by static is limited in the source code file and can not be called by the code files except the source code file, and the common function can be called by other code files by default.
The function is defined as a static function by adding a key static before the return type of the function. The definition and declaration of the normal function is externally visible by default, but the static function is only visible among the files in which it is declared and cannot be used by other files. Thus, a static function is characterized in that functions having the same name as that of the static function can be defined in other files without collision, and the static function cannot be used by other files.
Class membership functions are functions written in a class body to describe the behavior of the class and may include, for example, constructors, destructors, copy constructors.
The constructor is a special method, which is mainly used for initializing the object when the object is created, namely, the initial value is given to the member variable of the object, and the initial value is used in the sentence of creating the object together with the new operator. A particular class may have multiple constructors that may be distinguished by a different number of parameters or a different type of parameters, i.e., reloading of constructors.
The destructor is opposite to the constructor, and when the object ends its life cycle, such as when the function in which the object is located has been called, the system automatically executes the destructor. The destructor is often used to perform "clean-up" work, for example, when an object is created, new is used to open up a piece of memory space, and delete will automatically call the destructor and then release the memory. A copy constructor, also known as a replication constructor, is a special constructor that is called by a compiler to complete the construction and initialization of some other objects based on the same class.
The adjectives of the copy constructor must be references, but are not limited to const, and const limitations are generally imposed. Copy constructors are often used for user-defined types of value passing and returning at function call time. The copy constructor calls the copy constructor and the member function of the base class. It will be called in a constant manner if possible, and in addition, it can be called in a non-constant manner.
S330, creating a replacement function list corresponding to the source code to be tested according to the original function of the designated type.
In one embodiment of the application, the list of replacement functions may include one or more of a list of inline functions, a list of static functions, or a list of class member functions.
When the original function of the specified type is an inline function, the method of creating a list of replacement functions corresponding to the source code to be tested may include writing a function name of the inline function to the list of inline functions.
The inline function list is used for modifying function embedding logic of the inline function into function calling logic, and the inline function appearing in the inline function list is not embedded into the code in the code compiling process, but keeps calling relations and called relations of the inline function and other functions in the code.
When the original function of the designated type is a static function, the method of creating a replacement function list corresponding to the source code to be tested may include creating a non-static function having the same syntax information as the static function, and writing a mapping relationship of the static function and the non-static function to the static function list.
FIG. 4 illustrates a flow chart of a method of creating a non-static function in one embodiment of the application. As shown in fig. 4, the method of creating a non-static function having the same syntax information as the static function may further include the following steps S410 to S430.
S410, grammar information of the static function is obtained, wherein the grammar information comprises file names, line numbers and function call information.
Based on the abstract syntax tree obtained by analyzing the source code, syntax information of all function objects, such as a line number, a file name and function call information of a single function, can be obtained. Wherein the function call information includes other functions that call the static function.
S420, renaming the static function according to the file name and the line number, and removing keywords in the static function, which represent the definition of the static function, so as to obtain the non-static function.
Static functions are generally effective in a single file, and direct removal of keywords static representing static function definitions may introduce problems of homonymic function interference resulting in compiling errors. The embodiment of the application removes the static definition from the static function of the static function list static_functions_list, renames the function at the same time, and modifies the names of all the calling functions.
S430, modifying the function calling the static function into a function calling the non-static function according to the function calling information.
For example, the original static function STATIC INT MY _function () { } in the source code would be replaced with the non-static function int my_function_filename_ linenum _5678styc { }. Wherein filename and linenum represent the file name and line number where the function is located, 5678styc is a random 8-bit string, respectively. All code that calls static functions will be modified to call non-static functions.
The static function and the created non-static function are associated and then written into a static function list, so that codes which originally call the static function can be replaced by calling the non-static function according to the mapping relation stored in the static function list when the source code is compiled, the non-staticization of the static function is realized, and the problem of introducing homonymic function interference is avoided.
In one embodiment of the application, when the original function of the designated type is a class member function, a wrapper function and a real function with a redirection relation with the class member function are created, and the mapping relation of the wrapper function and the real function is written into a class member function list.
FIG. 5 illustrates a flow diagram of a method for creating a redirection function for class membership functions in one embodiment of the application. As shown in fig. 5, the method of creating a wrapper function and a real function having a redirection relation to a class member function may further include the following steps S510 to S530.
S510, obtaining a symbol table obtained by analysis from the source code to be tested.
In the process of analyzing the source code to be tested to obtain the abstract syntax tree, a symbol table corresponding to the source code can be synchronously generated, and the symbol table is used for recording various symbol information such as the type, the acting domain, the control domain and the like of each symbol in the code.
S520, searching the modified function matched with the class member function according to the symbol table.
In one embodiment of the application, the method for searching the modified function matched with the class member function according to the symbol table can further comprise the steps of decoding the symbol table to obtain a decoding name corresponding to the modified name of the class member function, searching the decoding name identical to the original name of the class member function, and determining the modified function matched with the class member function according to the modified name corresponding to the decoding name.
S530, creating a wrapper function and a real function corresponding to the decorated function, redirecting the class member function to the wrapper function, and directing the real function to the class member function.
After finding the corresponding decorated name, the original function needs to be redirected to create new symbols for the wrapper function and the real function. For example, in the embodiment of the present application, the original function name is my_function, the modified function name is my_ decorated _function, and then a corresponding wrapper function __ wrap __ my_ decorated _function and a corresponding real function __ real __ my_ decorated _function are created for the modified function. By redirecting the class member functions to the wrapper function, the function that originally called the class member function will instead call the wrapper function, and by redirecting the real function to the class member function, the code that uses the real function can actually use the original class member function.
Based on the above embodiment, the application realizes the applicability of the inline functions, static functions and class member functions to recording and playback tests.
In step S220, a rule file is generated from the replacement function list and the library function having the record playback function, the rule file defining construction rules of test items of the source code.
In one embodiment of the application, before a rule file is generated according to a replacement function list and library functions with recording and playback functions, a function library for calling a source code can be obtained according to a code language of the source code, an original library function capable of introducing noise in recording and playback tests is screened from the function library, and the original library function is rewritten to obtain the library function with the recording and playback functions.
A function library is a collection of functions with certain functions built by the system. The library stores the names of the functions and the corresponding object codes, and relocation information required in the connection process. The user can also establish own user function library according to own needs.
Library functions are functions stored in a library of functions. Library functions have explicit functions, entry call parameters, and return values.
In one embodiment of the present application, the method for re-writing the original library function to obtain the library function with the recording and playback function may further comprise creating a proxy function corresponding to the original library function, writing recording logic and playback logic into the proxy function to obtain the library function with the recording and playback function, the recording logic being configured to record output parameters of the original library function in the recording mode, and the playback logic being configured to read the output parameters of the original library function recorded in the recording mode in the playback mode and to call the original library function in the non-playback mode.
In one embodiment of the application, the original library functions that can introduce noise in the recording playback test include functions whose output parameters have a temporal correlation or functions whose output parameters have a randomness.
For example, the function of the output parameter having time correlation may be a function of acquiring a time stamp or a function of acquiring a buffer, and the function of the output parameter having randomness may be a function of acquiring a random number.
For example, the original library function is GETTRACEID (), and the embodiment of the present application may initialize a proxy function GetTraceIDWithMock () corresponding thereto. In the recording mode, judging whether a called function needs to be played back, if not, directly calling the original library function GETTRACEID (), and if so, calling the proxy function GetTraceIDWithMock ().
In one embodiment of the present application, before a rule file is generated according to a replacement function list and a library function having a recording playback function, the library function having the recording playback function is associated with an original library function and then written into the function list.
By writing a library function having a record playback function into the function list, a function calling the original library function can be replaced with a library function calling a record playback function when compiling the source code.
In step S230, the source code is compiled according to the rule file to obtain the target code to be tested. When compiling to the original function in the source code, replacing the original function with the calling wrapper function, and when compiling to the library function in the source code, replacing the calling address of the library function with the recording playback function.
In one embodiment of the application, a method for compiling source code according to a rule file may include obtaining a compilation parameter added to or updated in the rule file, the compilation parameter including one or more of an inline function, a static function, or a class member function. The method comprises the steps of obtaining compiling parameters, skipping code embedding operation on the inline function when the compiling parameters are the inline function, replacing the static function with a non-static function corresponding to the static function when the compiling parameters are the static function, and replacing the class member function with a wrapper function corresponding to the class member function when the compiling parameters are the class member function.
In step S240, a record playback test is performed on the object code.
Fig. 6 shows an interactive flow chart of recording playback testing in an application scenario according to an embodiment of the present application. As shown in fig. 6, the interactive process of recording and playing back the application scene includes the following procedures.
And S601, the developer initiates a compiling task of the source code through a compiler.
The compiler used in the embodiment of the application can be a compiling tool formed by taking Bazel as a bottom layer tool and combining business characteristic encapsulation. Bazel is a build tool for building and testing items of software, responsible for scheduling and managing the individual build tasks. The project build process is managed using Bazel, including dependency parsing, resource collection, and compilation task scheduling, among others.
When the compilation task is triggered Bazel, the source code (e.g., C language code and C++ code) is compiled into an executable file or library file using GCC (GNU Compiler Collection) to act as a compiler. GCC is a collection of compiler tools, including C, C ++, objective-C, fortran, ada, and compilers in other languages. GCC is widely used in a variety of operating systems and architectures, and can be used to build various types of applications and libraries.
The compiler builds a rule file using a pre-configured list of replacement functions and library functions with record playback functions S602.
The compiler reads a preconfigured function list needing dynamic replacement, and pulls a preconfigured C++ base library function with recording and playback capability to generate a BUILD file of the tested module. In Bazel, the BUILD file is a text file that is used to describe the construction rules in the project. There may be one or more BUILD files under each directory that define the BUILD rules under that directory. Various BUILD rules may be defined in the BUILD file, such as compiling source code, generating library files, generating executable files, and so forth.
Taking a GCC compiler as an example, the embodiment of the present application may add a new command line option-wrap_class_functions=xx, -fkeep _inline_functions=xx, -fkeep _static_functions=xx at the front end of the GCC compiler, and support dynamic replacement of c++ class member functions, inline functions, and static functions, respectively.
Meanwhile, the embodiment of the application can add a new command line analysis code at the front end of the GCC compiler to acquire a C++ class member function list wrap_class_functions_list, an inline function list inline_functions_list and a static function list static_functions_list which need special processing, and store the lists in a memory for processing in the following steps.
To embody the- -wrap option using GCC in the Bazel BUILD file, compilation parameters need to be added to the cc_binary or cc_library rules of Bazel. This may be achieved by using linkopts parameters. Since the wrap capability is only used in recorded playback scenes, the burl file cannot be incorporated into the code backbone and needs to be dynamically generated. The implementation process is as follows:
The call configuration management system pulls a list of functions that need dynamic replacement. The data format is exemplified as follows:
{
"my_test_module":{
{
"function_name":"my_function_1",
"function_type":"common"
},
{
"function_name":"my_function_2",
"function_type":"inline"
},
{
"function_name":"my_function_3",
"function_type":"class"
},
{
"function_name":"my_function_4",
"function_type":"static"
}
}
}
according to the tested module name my_test_module and the function type to be replaced, the compiling dependent part of the BUILD file of the service module is found, and linkopts is added or updated as follows:
cc_binary(
name="my_binary",
srcs=["main.cpp","original_function.cpp"],
linkopts=["-Wl,--wrap=my_function_1,
-Wl,--wrap=my_function_2,
-Wl,--wrap=my_function_3,
-Wl,--wrap=my_function_4,
-Wl,--fkeep_inline_functions=my_function_2,
-Wl,--wrap_class_functions=my_function_3",
-Wl,--fkeep_static_functions=my_function_4"],
)
Wherein-fkeep _inline_functions, -wrap_class_functions, -fkeep _static_functions represent inline functions, class member functions and static functions, respectively, that require dynamic replacement.
S603, compiling the source code according to the rule file to obtain the target code.
The binary is compiled in combination with the BUILD file and the source code and deployed to the recording environment.
The embodiment of the application modifies the optimizer in the compiler to process the support of the wrap option on the inline function, and mainly optimizes the inline function optimizing step of the optimizer. And when the function name to be optimized is obtained from the inline function list inline functions list in the step, skipping the inline function optimization of the part. The skip processing does not affect the compiling result, but may affect the running performance, and the embodiment of the application controls through fkeep _line_functions options, so that the performance is affected controllably.
The embodiment of the application modifies the optimizer to process the support of the wrap option to the static function, and mainly performs non-static processing on the static method. Static functions are typically validated in a single file, and direct removal of static definitions may introduce homonymic function interference resulting in misinterpretation. The embodiment of the application removes the static definition of the static function list static_functions_list in the steps, renames the function and modifies the names of all calling functions.
The embodiment of the application modifies the optimizer to process the original C++ class member function names. According to the embodiment of the application, a parser is realized in the optimizer, the symbol table generated by the optimizer is decoded by utilizing __ cxa _ demangle functions, and the modified names are searched according to the provided original names of the C++ member functions.
The embodiment of the application modifies the linker in the compiler to enable the linker to support the dynamic replacement of the C++ class member functions. According to the class member function list obtained by the optimizer in the above steps, the C++ class member function to be mapped can be extracted and obtained. The embodiment of the application uses objcopy to create an alias mapping the my_ decorated _function symbol to __ wrap_my_function. objcopy can also be used to modify the symbol table of the target file, for example to change the name of the symbol to a new name. When there is a designation in the compilation command at the beginning-wrap=my_function, the function will be dynamically replaced when linking the processing workflow.
The modification linker of the embodiment of the application supports dynamic replacement of static functions. And according to the static function list obtained by the optimizer in the steps, the dynamic replacement of the static function can be realized by executing the same operation as the class member function.
And S604, performing a recording playback test on the target code.
After the flow comes in, the results of the functions are recorded.
The embodiment of the application supports two types of functions, namely an inline function and a static function, by adding compiling options of-fkeep-inline-functions and-fwrap-cpp-method. In addition, the compiling tool in the embodiment of the application is transformed, when the compiling action before the recording task is triggered, the function list which is registered by the configuration management system and needs dynamic replacement is pulled, and the compiling action is executed after the corresponding BUILD file is generated and replaced.
The embodiment of the application aims at the automatic recording of the library function and the business function of the C++ code, is applied to daily regression testing, and greatly reduces the invasion to the business code. The method supports automatic recording and playback of the service library functions containing noise factors in a more scientific and accurate mode, improves maintainability and readability of service codes, and reduces reconstruction workload for noise flow elimination in the recording and playback access process.
The scheme for automatically recording the C++ library function and the business function based on GCC secondary development, provided by the embodiment of the application, can be used for recording and playing back scenes, and can also be used in the field of safety test analysis and the like.
It should be noted that although the steps of the methods of the present application are depicted in the accompanying drawings in a particular order, this does not require or imply that the steps must be performed in that particular order, or that all illustrated steps be performed, to achieve desirable results. Additionally or alternatively, certain steps may be omitted, multiple steps combined into one step to perform, and/or one step decomposed into multiple steps to perform, etc.
The following describes an embodiment of the apparatus of the present application, which may be used to perform the code testing method of the above-described embodiment of the present application. Fig. 7 schematically shows a block diagram of a code testing apparatus according to an embodiment of the present application. As shown in fig. 7, the code testing apparatus 700 includes:
An obtaining module 710 configured to obtain a replacement function list corresponding to a source code to be tested, where the replacement function list is used to store a wrapper function that makes a replacement call to an original function in the source code;
A generating module 720 configured to generate a rule file according to the replacement function list and a library function with a recording playback function, wherein the rule file is used for defining a construction rule of a test item of the source code;
The compiling module 730 is configured to compile the source code according to the rule file to obtain an object code to be tested, replace the original function with the calling wrapper function when compiling to the original function in the source code, and replace the calling address of the library function with the recording and playback function when compiling to the library function in the source code;
A test module 740 configured to perform a record playback test on the object code.
In some embodiments of the present application, based on the above technical solutions, the code testing apparatus 700 further includes:
the function library acquisition module is configured to acquire a function library for calling the source code according to the code language of the source code;
A library function screening module configured to screen the original library functions from the function library that can introduce noise in the recording playback test;
and the library function writing module is configured to rewrite the original library function to obtain the library function with the recording and playback functions.
In some embodiments of the present application, based on the above technical solution, the library function writing module is further configured to create a proxy function corresponding to the original library function, write recording logic and playback logic into the proxy function to obtain a library function with a recording playback function, where the recording logic is configured to record output parameters of the original library function in a recording mode, and the playback logic is configured to read the output parameters of the original library function recorded in the recording mode in a playback mode, and to call the original library function in a non-playback mode.
In some embodiments of the present application, based on the above technical solutions, the original library functions capable of introducing noise in the recording playback test include a function whose output parameters have a temporal correlation or a function whose output parameters have a randomness.
In some embodiments of the present application, based on the above technical solutions, the code testing apparatus 700 further includes:
And the function writing module is configured to write the replacement function list after the library function with the recording and playback function is associated with the original library function.
In some embodiments of the present application, based on the above technical solutions, the obtaining module 710 includes:
The analysis module is configured to analyze the source code to be tested to obtain an abstract syntax tree of the source code;
the traversing module is configured to traverse the abstract syntax tree to obtain an original function of a specified type, wherein the original function of the specified type is a function needing replacement calling in a recording playback test;
And the creation module is configured to create a replacement function list corresponding to the source code to be tested according to the original function of the specified type.
In some embodiments of the present application, based on the above technical solution, the specified type of original function includes one or more of an inline function, a static function, or a class member function.
In some embodiments of the present application, based on the above technical solution, the replacing function list includes an inline function list, a static function list, or a class member function list, and the creating module includes:
The first creation module is configured to write the function name of the inline function into the inline function list when the original function of the specified type is the inline function;
the second creation module is configured to create a non-static function with the same grammar information as the static function when the original function of the designated type is the static function, and write the mapping relation between the static function and the non-static function into the static function list;
And the third creation module is configured to create a wrapper function and a real function which have a redirection relation with the class member function when the original function of the specified type is the class member function, and write the mapping relation of the wrapper function and the real function into the class member function list.
In some embodiments of the present application, based on the above technical solution, the second creation module is further configured to obtain syntax information of the static function, where the syntax information includes a file name, a line number, and function call information, rename the static function according to the file name and the line number, remove keywords in the static function that represent a definition of the static function, and obtain a non-static function, and modify a function that calls the static function to call the non-static function according to the function call information.
In some embodiments of the present application, based on the above technical solution, the third creating module is further configured to obtain a symbol table parsed from the source code to be tested, find a modified function matching the class member function according to the symbol table, create a wrapper function and a real function corresponding to the modified function, redirect the class member function to the wrapper function, and direct the real function to the class member function.
In some embodiments of the present application, based on the above technical solution, the third creating module is further configured to perform decoding processing on the symbol table to obtain a decoded name corresponding to the modified name of the class member function, find the decoded name identical to the original name of the class member function, and determine a modified function matched with the class member function according to the modified name corresponding to the decoded name.
In some embodiments of the present application, based on the above technical solution, the compiling module 730 includes:
A parameter acquisition module configured to acquire compiling parameters added or updated to the rule file, wherein the compiling parameters comprise one or more of an inline function, a static function or a class member function;
the first compiling module is configured to skip code embedding operation on the inline function when the compiling parameter is the inline function;
The second compiling module is configured to replace the static function with a non-static function corresponding to the static function when the compiling parameter is the static function;
and the third compiling module is configured to replace the class member function with a wrapper function corresponding to the class member function when the compiling parameter is the class member function.
Specific details of the code testing device provided in each embodiment of the present application have been described in the corresponding method embodiments, and are not described herein.
Fig. 8 schematically shows a block diagram of a computer system of an electronic device for implementing an embodiment of the application.
It should be noted that, the computer system 800 of the electronic device shown in fig. 8 is only an example, and should not impose any limitation on the functions and the application scope of the embodiments of the present application.
As shown in fig. 8, the computer system 800 includes a central processing unit 801 (Central Processing Unit, CPU) that can perform various appropriate actions and processes according to a program stored in a Read-Only Memory 802 (ROM) or a program loaded from a storage section 808 into a random access Memory 803 (Random Access Memory, RAM). In the random access memory 803, various programs and data required for system operation are also stored. The central processing unit 801, the read only memory 802, and the random access memory 803 are connected to each other through a bus 804. An Input/Output interface 805 (i.e., an I/O interface) is also connected to the bus 804.
Connected to the input/output interface 805 are an input section 806 including a keyboard, a mouse, and the like, an output section 807 including a display such as a Cathode Ray Tube (CRT), a Liquid crystal display (Liquid CRYSTAL DISPLAY, LCD), and a speaker, a storage section 808 including a hard disk, and the like, and a communication section 809 including a network interface card such as a lan card, a modem, and the like. The communication section 809 performs communication processing via a network such as the internet. The drive 810 is also connected to the input/output interface 805 as needed. A removable medium 811 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 810 as needed so that a computer program read out therefrom is mounted into the storage section 808 as needed.
In particular, the processes described in the various method flowcharts may be implemented as computer software programs according to embodiments of the application. For example, embodiments of the present application include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flowcharts. In such an embodiment, the computer program may be downloaded and installed from a network via the communication section 809, and/or installed from the removable media 811. The computer programs, when executed by the central processor 801, perform the various functions defined in the system of the present application.
It should be noted that, the computer readable medium shown in the embodiments of the present application may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of a computer-readable storage medium may include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-Only Memory (ROM), an erasable programmable read-Only Memory (Erasable Programmable Read Only Memory, EPROM), a flash Memory, an optical fiber, a portable compact disc read-Only Memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present application, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, etc., or any suitable combination of the foregoing.
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present application. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
It should be noted that although in the above detailed description several modules or units of a device for action execution are mentioned, such a division is not mandatory. Indeed, the features and functions of two or more modules or units described above may be embodied in one module or unit in accordance with embodiments of the application. Conversely, the features and functions of one module or unit described above may be further divided into a plurality of modules or units to be embodied.
From the above description of embodiments, those skilled in the art will readily appreciate that the example embodiments described herein may be implemented in software, or may be implemented in software in combination with the necessary hardware. Thus, the technical solution according to the embodiments of the present application may be embodied in the form of a software product, which may be stored in a non-volatile storage medium (may be a CD-ROM, a U-disk, a mobile hard disk, etc.) or on a network, and includes several instructions to cause a computing device (may be a personal computer, a server, a touch terminal, or a network device, etc.) to perform the method according to the embodiments of the present application.
Other embodiments of the application will be apparent to those skilled in the art from consideration of the specification and practice of the application disclosed herein. This application is intended to cover any variations, uses, or adaptations of the application following, in general, the principles of the application and including such departures from the present disclosure as come within known or customary practice within the art to which the application pertains.
It is to be understood that the application is not limited to the precise arrangements and instrumentalities shown in the drawings, which have been described above, and that various modifications and changes may be effected without departing from the scope thereof. The scope of the application is limited only by the appended claims.

Claims (16)

1. A code testing method, comprising:
Acquiring a replacement function list corresponding to a source code to be tested, wherein the replacement function list is used for storing a packaging function for replacing and calling an original function in the source code;
Generating a rule file according to the replacement function list and a library function with a recording and playback function, wherein the rule file is used for defining a construction rule of a test item of the source code;
The source code is compiled according to the rule file to obtain an object code to be tested, when the source code is compiled to an original function in the source code, the original function is replaced by a calling packaging function, and when the source code is compiled to a library function in the source code, the calling address of the library function is replaced by the library function with a recording and playback function;
and executing a recording playback test on the target code.
2. The code testing method of claim 1, wherein before generating a rule file from the list of replacement functions and a library function having a record playback function, the method further comprises:
Acquiring a function library for calling the source code according to the code language of the source code;
Screening an original library function capable of introducing noise in a recording playback test from the function library;
and re-writing the original library function to obtain the library function with the recording and playback functions.
3. The code testing method of claim 2, wherein re-writing the original library functions to obtain library functions with recording playback functions, comprising:
creating a proxy function corresponding to the original library function;
Writing recording logic and playback logic into the proxy function to obtain a library function with a recording playback function, wherein the recording logic is used for recording output parameters of the original library function in a recording mode, and the playback logic is used for reading the output parameters of the original library function recorded in the recording mode in a playback mode and calling the original library function in a non-playback mode.
4. The code testing method of claim 2, wherein the original library functions capable of introducing noise in the recording playback test include a function whose output parameters have a temporal correlation or a function whose output parameters have a randomness.
5. The code testing method of claim 2, wherein before generating a rule file from the list of replacement functions and a library function having a record playback function, the method further comprises:
And after the library function with the recording and playback functions and the original library function are subjected to association processing, writing the library function into the replacement function list.
6. The code testing method according to any one of claims 1 to 5, wherein obtaining a replacement function list corresponding to a source code to be tested, comprises:
Analyzing a source code to be tested to obtain an abstract syntax tree of the source code;
Traversing the abstract syntax tree to obtain an original function of a specified type, wherein the original function of the specified type is a function needing replacement calling in a recording playback test;
and creating a replacement function list corresponding to the source code to be tested according to the original function of the specified type.
7. The code testing method of claim 6, wherein the specified type of original function comprises one or more of an inline function, a static function, or a class member function.
8. The code testing method of claim 6, wherein the list of replacement functions comprises a list of inline functions, a list of static functions, or a list of class member functions, creating a list of replacement functions corresponding to the source code to be tested, comprising:
when the original function of the appointed type is an inline function, writing the function name of the inline function into the inline function list;
when the original function of the appointed type is a static function, creating a non-static function with the same grammar information as the static function, and writing the mapping relation between the static function and the non-static function into the static function list;
When the original function of the appointed type is a class member function, a packing function and a real function which have a redirection relation with the class member function are created, and the mapping relation between the packing function and the real function is written into the class member function list.
9. The code testing method of claim 8, wherein creating a non-static function having the same syntax information as the static function comprises:
The grammar information of the static function is obtained, wherein the grammar information comprises file names, line numbers and function call information;
renaming the static function according to the file name and the line number, and removing keywords which represent static function definition in the static function to obtain a non-static function;
and modifying the function calling the static function into calling the non-static function according to the function calling information.
10. The code testing method of claim 8, wherein creating a wrapper function and a real function having a redirection relationship to the class member function comprises:
Acquiring a symbol table obtained by analyzing the source code to be tested;
Searching a modified function matched with the class member function according to the symbol table;
Creating a wrapper function and a real function corresponding to the modified function, redirecting the class member function to the wrapper function, and directing the real function to the class member function.
11. The code testing method of claim 10, wherein looking up a modified function matching the class member function based on the symbol table comprises:
decoding the symbol table to obtain a decoding name corresponding to the modified name of the class member function;
And searching the decoding name which is the same as the original name of the class member function, and determining a modified function matched with the class member function according to the modified name corresponding to the decoding name.
12. The code testing method according to claim 10, wherein compiling the source code according to the rule file comprises:
Obtaining compiling parameters added or updated in the rule file, wherein the compiling parameters comprise one or more of an inline function, a static function or a class member function;
when the compiling parameter is the inline function, skipping code embedding operation of the inline function;
When the compiling parameter is the static function, replacing the static function with a non-static function corresponding to the static function;
And when the compiling parameter is the class member function, replacing the class member function with a wrapper function corresponding to the class member function.
13. A code testing apparatus, comprising:
The acquisition module is configured to acquire a replacement function list corresponding to source codes to be tested, wherein the replacement function list is used for storing a packing function for replacing and calling an original function in the source codes;
a generation module configured to generate a rule file according to the replacement function list and a library function with a recording playback function, wherein the rule file is used for defining a construction rule of a test item of the source code;
The compiling module is configured to compile the source code according to the rule file to obtain an object code to be tested, replace the original function with the calling packaging function when compiling the original function into the source code, and replace the calling address of the library function with the recording and playback function when compiling the library function into the source code;
and the test module is configured to execute a recording playback test on the target code.
14. A computer readable medium, characterized in that the computer readable medium has stored thereon a computer program which, when executed by a processor, implements the code testing method of any of claims 1 to 12.
15. An electronic device, comprising:
Processor, and
A memory for storing executable instructions of the processor;
Wherein the processor is configured to execute the executable instructions to implement the code testing method of any one of claims 1 to 12.
16. A computer program product comprising a computer program, characterized in that the computer program, when executed by a processor, implements the code testing method of any of claims 1 to 12.
CN202311377715.4A 2023-10-23 2023-10-23 Code testing method, device, medium and electronic equipment Pending CN119902971A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311377715.4A CN119902971A (en) 2023-10-23 2023-10-23 Code testing method, device, medium and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311377715.4A CN119902971A (en) 2023-10-23 2023-10-23 Code testing method, device, medium and electronic equipment

Publications (1)

Publication Number Publication Date
CN119902971A true CN119902971A (en) 2025-04-29

Family

ID=95470497

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311377715.4A Pending CN119902971A (en) 2023-10-23 2023-10-23 Code testing method, device, medium and electronic equipment

Country Status (1)

Country Link
CN (1) CN119902971A (en)

Similar Documents

Publication Publication Date Title
JP4183399B2 (en) Multiple language compilation method and system
US8954939B2 (en) Extending a development environment
US9128728B2 (en) Locating security vulnerabilities in source code
US9152796B2 (en) Dynamic analysis interpreter modification for application dataflow
US8707263B2 (en) Using a DSL for calling APIS to test software
CN112100054A (en) A program static analysis method and system for data management and control
US9928156B2 (en) Missing include suggestions for external files
CN110059456B (en) Code protection method, code protection device, storage medium and electronic equipment
US10083016B1 (en) Procedurally specifying calculated database fields, and populating them
US8141035B2 (en) Method for accessing internal states of objects in object oriented programming
US20070250825A1 (en) Compiling Alternative Source Code Based on a Metafunction
US20200249925A1 (en) On-demand loading of dynamic scripting language code for reduced memory usage
US10459708B2 (en) Composing a module system and a non-module system
US5960197A (en) Compiler dispatch function for object-oriented C
CN111880801A (en) Application program dynamic method and device and electronic equipment
CN110609687A (en) Compiling method, device, electronic equipment and storage medium
CN101208690B (en) Translating Expressions in Computing Environments
US10789067B2 (en) System and method for identifying open source usage
CN102216901B (en) Component extension method and device
CN119902971A (en) Code testing method, device, medium and electronic equipment
CN116578282A (en) Code generation method, device, electronic equipment and medium
Liu et al. Hybrid inlining: A framework for compositional and context-sensitive static analysis
CN112860279A (en) Method, device, equipment and medium for generating application installation package
CN115269058B (en) A WPF resource loading method and device
US11615014B2 (en) Using relocatable debugging information entries to save compile time

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication