Code testing method and device, medium and electronic equipment
By replacing the functions and library functions in the test source code, generating rule files and performing recording and playback tests, the problem of noisy traffic in the traffic recording and playback test is solved, improving the testing efficiency and reducing intrusion of business code.
Patent Information
- Application Number
- CN202311377715.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-23
- Publication Date
- 2025-04-29
AI Technical Summary
In traffic recording and playback tests, a large amount of noise flow that is not related to the test requirements is often recorded, resulting in low test efficiency and requires manual noise screening or modification of the original code.
By obtaining a list of replacement functions corresponding to the source code to be tested, replacing the original functions and library functions in the source code with wrapper functions and library functions with recording and playback functions, generating a rule file to define the construction rules of the test project, and performing a recording and playback test on the target code.
It effectively overcomes the problem of introducing noisy traffic in the recording and playback test, improves the efficiency of code testing, reduces intrusion into business code, and avoids recursive traps.
Smart Images

Figure CN119902971A_ABST
Abstract
Description
Technical Field
[0001] 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. Background Art
[0002] Traffic recording and playback is a commonly used background testing technology. It records online traffic, uses the same input parameters during playback, and compares the output parameters of the online environment and the test environment to determine whether the interface code logic of the test environment is abnormal. However, in the process of recording online traffic, a lot of noise traffic that is irrelevant to the test requirements is often recorded, and it is necessary to manually filter out the noise or modify the original code, resulting in low test efficiency. Summary of the invention
[0003] The present application provides a code testing method, a code testing device, a computer-readable medium, an electronic device, and a computer program product, the purpose of which is to improve code testing efficiency.
[0004] Other features and advantages of the present application will become apparent from the following detailed description, or may be learned in part by the practice of the present application.
[0005] According to one aspect of an embodiment of the present application, a code testing method is provided, which includes: obtaining a replacement function list corresponding to a source code to be tested, the replacement function list being used to store a wrapper function that replaces a call to an original function in the source code; generating a rule file based on the replacement function list and a library function with a recording and playback function, the rule file being used to define a construction rule for a test project of the source code; compiling the source code according to the rule file to obtain a target code to be tested; when compiling to the original function in the source code, replacing the original function with a call to the wrapper function; when compiling to the library function in the source code, replacing the call address of the library function with the library function with the recording and playback function; and performing a recording and playback test on the target code.
[0006] According to one aspect of an embodiment of the present application, a code testing device is provided, the code testing device comprising:
[0007] An acquisition module is configured to acquire a replacement function list corresponding to the source code to be tested, wherein the replacement function list is used to store wrapper functions that replace calls to original functions in the source code;
[0008] A generating module is configured to generate a rule file according to the replacement function list and the library function with recording and playback functions, wherein the rule file is used to define a construction rule of the test project of the source code;
[0009] A compiling module is configured to compile the source code according to the rule file to obtain a target code to be tested; when compiling to an original function in the source code, the original function is replaced by a call to the wrapper function; when compiling to a library function in the source code, the call address of the library function is replaced by the library function with the recording and playback function;
[0010] The test module is configured to perform a recording and playback test on the target code.
[0011] In some embodiments of the present application, based on the above technical solution, the code testing device further includes:
[0012] A function library acquisition module, configured to acquire a function library for the source code to call according to a code language of the source code;
[0013] A library function screening module, configured to screen original library functions that can introduce noise in the recording and playback test from the function library;
[0014] The library function writing module is configured to rewrite the original library function to obtain a library function with recording and playback functions.
[0015] In some embodiments of the present application, based on the above technical solution, the library function writing module is also 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 recording and playback functions; the recording logic is used to record the output parameters of the original library function in the recording mode; the playback logic is used to read the output parameters of the original library function recorded in the recording mode in the playback mode, and is used to call the original library function in the non-playback mode.
[0016] In some embodiments of the present application, based on the above technical solution, the original library function that can introduce noise in the recording and playback test includes a function whose output parameters have time correlation or a function whose output parameters have randomness.
[0017] In some embodiments of the present application, based on the above technical solution, the code testing device further includes:
[0018] The function writing module is configured to write the library function with recording and playback functions into the replacement function list after associating it with the original library function.
[0019] In some embodiments of the present application, based on the above technical solution, the acquisition module includes:
[0020] A parsing module, configured to parse the source code to be tested to obtain an abstract syntax tree of the source code;
[0021] A traversal module is configured to traverse the abstract syntax tree to obtain an original function of a specified type, where the original function of the specified type is a function that needs to be replaced and called in a recording and playback test;
[0022] 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.
[0023] In some embodiments of the present application, based on the above technical solution, the original function of the specified type includes one or more of an inline function, a static function or a class member function.
[0024] In some embodiments of the present application, based on the above technical solution, the replacement function list includes an inline function list, a static function list or a class member function list; the creation module includes:
[0025] A first creation module is configured to write a function name of the inline function into the inline function list when the original function of the specified type is an inline function;
[0026] A second creation module is configured to, when the original function of the specified type is a static function, create a non-static function having the same grammatical information as the static function, and write a mapping relationship between the static function and the non-static function into the static function list;
[0027] The third creation module is configured to create a wrapper function and a real function that have a redirection relationship with the class member function when the original function of the specified type is a class member function, and write the mapping relationship between the wrapper function and the real function into the class member function list.
[0028] In some embodiments of the present application, based on the above technical solution, the second creation module is further configured to: obtain the syntax information of the static function, the syntax information including the file name, line number and function call information; rename the static function according to the file name and line number, and remove the keywords representing the static function definition in the static function to obtain a non-static function; modify the function that calls the static function to call the non-static function according to the function call information.
[0029] In some embodiments of the present application, based on the above technical solution, the third creation module is further configured to: obtain a symbol table parsed from the source code to be tested; search for a modified function that matches 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.
[0030] In some embodiments of the present application, based on the above technical solution, the third creation module is further configured to: decode the symbol table to obtain a decoded name corresponding to the modified name of the class member function; search for a decoded name that is the same as the original name of the class member function, and determine a modified function that matches the class member function based on the modified name corresponding to the decoded name.
[0031] In some embodiments of the present application, based on the above technical solution, the compiling module includes:
[0032] A parameter acquisition module, configured to acquire compilation parameters added or updated to the rule file, wherein the compilation parameters include one or more of an inline function, a static function or a class member function;
[0033] A first compiling module is configured to skip the code embedding operation on the inline function when the compiling parameter is the inline function;
[0034] A second compiling module is configured to replace the static function with a corresponding non-static function when the compiling parameter is the static function;
[0035] The third compiling module is configured to replace the class member function with a corresponding wrapper function when the compiling parameter is the class member function.
[0036] According to one aspect of an embodiment of the present application, a computer-readable medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the code testing method in the above technical solution is implemented.
[0037] According to one aspect of an embodiment of the present application, an electronic device is provided, comprising: 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.
[0038] According to one aspect of an embodiment of the present application, a computer program product is provided, including a computer program, wherein when the computer program is executed by a processor, the code testing method in the above technical solution is implemented.
[0039] In the technical solution provided by the embodiment of the present application, by obtaining a replacement function list corresponding to the source code to be tested, a rule file can be generated according to the replacement function list and the library function with recording and playback function, and the source code is further compiled according to the rule file to obtain the target code to be tested, so that the recording and playback test can be performed on the target code. The embodiment of the present application uses the replacement function list to replace the library function and the business function in the source code with the replacement call wrapper function, which overcomes the problem of easily introducing noise traffic in the recording and playback test, thereby improving the test efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] The drawings herein are incorporated into the specification and constitute a part of the specification, illustrate embodiments consistent with the present application, and together with the specification are used to explain the principles of the present application. Obviously, the drawings described below are only some embodiments of the present application, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0041] Figure 1 A system architecture block diagram of a code testing system using the technical solution of the present application is shown.
[0042] Figure 2 A flow chart of a code testing method in one embodiment of the present application is shown.
[0043] Figure 3 A flow chart of a method for obtaining a replacement function list in one embodiment of the present application is shown.
[0044] Figure 4 A flow chart of a method for creating a non-static function in one embodiment of the present application is shown.
[0045] Figure 5 A flow chart of a method for creating a redirection function for a class member function in one embodiment of the present application is shown.
[0046] Figure 6 An interactive flow chart of performing a recording and playback test in an application scenario in an embodiment of the present application is shown.
[0047] Figure 7 The structural block diagram of the code testing device provided in an embodiment of the present application is schematically shown.
[0048] Figure 8 The structure block diagram of a computer system suitable for implementing an electronic device of an embodiment of the present application is schematically shown. DETAILED DESCRIPTION
[0049] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that this application will be more comprehensive and complete and fully convey the concept of the example embodiments to those skilled in the art.
[0050] In addition, described feature, structure or characteristic can be combined in one or more embodiments in any suitable manner. In the following description, many specific details are provided to provide a full understanding of the embodiments of the present application. However, those skilled in the art will appreciate that the technical scheme of the present application can be put into practice without one or more of the specific details, or other methods, components, devices, steps, etc. can be adopted. In other cases, known methods, devices, realizations or operations are not shown or described in detail to avoid blurring the various aspects of the application.
[0051] The block diagrams shown in the accompanying drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities may be implemented in software form, or in one or more hardware modules or integrated circuits, or in different networks and / or processor devices and / or microcontroller devices.
[0052] The flowcharts shown in the accompanying drawings are only exemplary and do not necessarily include all the contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps can be decomposed, and some operations / steps can be combined or partially combined, so the actual execution order may change according to actual conditions.
[0053] In the specific implementation of this application, related data such as user input data and online traffic are involved. When the various embodiments of this application are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions.
[0054] Figure 1 A system architecture block diagram of a code testing system using the technical solution of the present application is shown.
[0055] like Figure 1As shown, the system architecture of the code testing system may include a terminal device 110 and a server 120. The terminal device 110 may include various electronic devices such as smart phones, tablet computers, laptops, desktop computers, smart speakers, smart wearable devices, smart vehicle-mounted devices, smart payment terminals, etc. The server 120 may be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. There are communication media of various connection types that provide communication links between the terminal device 110 and the server 120, such as wired communication links or wireless communication links.
[0056] According to the implementation requirements, the system architecture in the embodiment of the present application can have any number of terminal devices and servers. For example, the server 120 can be a server group composed of multiple server devices. In addition, the technical solution provided in the embodiment of the present application can be applied to the terminal device 110, can also be applied to the server 120, or can be implemented by the terminal device 110 and the server 120 together, and the present application does not make any special restrictions on this.
[0057] Taking the application scenario of recording and playback testing as an example, the recording and playback testing performed on the source code 140 may include a traffic recording phase in a real business environment and a traffic playback phase in a test environment.
[0058] In the traffic recording stage, the terminal device 110 initiates a service request to the server 120 and saves the corresponding request data in real time to the database 130. When the server 120 returns response data to the terminal device 110, the response data can be saved to the database 130 synchronously.
[0059] In the traffic playback phase, the request data stored in the database 130 is input as an input parameter to the source code 140 to be tested, and the corresponding output parameters are obtained by running the source code 140. By comparing the output parameters with the response data stored in the database 130 to see if they are consistent, the corresponding test results can be obtained, thereby determining whether there is an abnormality in the source code.
[0060] In the related technology of the present application, since there is a large amount of noise data in the online traffic saved in the traffic recording stage, testers are usually required to manually screen the required traffic. For example, when recording a functional module of a business platform online and wanting to form a scenario use case, it is necessary to use the platform with other user operations, resulting in the recorded traffic being mixed with the traffic of other business scenarios. If a large number of modifications and intrusions are performed in the business code in order to eliminate the interference caused by the noise data and reduce the cost of confirming the test results of the noise data, this reduces the readability of the code and introduces a lot of unnecessary redundancy into the code structure.
[0061] The related technology of the present application adopts a dynamic library preloading method or an address replacement method to solve the problem of requiring code intrusion.
[0062] The dynamic library preloading method is to use the preload environment variable to set the path of the shared library, and the library will be loaded before any other library, that is, the symbol priority in this dynamic library is the highest. When the program is running, it will first search the system default dynamic link library path, find the required shared library and load the functions in it. However, if the preload environment variable is set, the program will first search the specified preload shared library path and load the functions in it. The functions in the preloaded shared library have the same symbolic name as the functions with the same name in the program, so when the program calls the function with the same name, the function in the preloaded shared library will be called first instead of the original function in the program. The functions in the preloaded shared library can be modified or replaced based on the original functions, thereby changing the behavior of the program. This replacement is dynamic and only takes effect when the program is running, and will not modify the source code or binary files of the program.
[0063] The address replacement method refers to directly reading and writing the system call table to replace the address. It is a technology used for hooking system calls. It can be used to replace the implementation function of the system call during operation. Its working principle is as follows: the system call table is a data structure that stores the address of the system call function. Each system call corresponds to a function address. In the Linux system, the system call table is usually stored in the kernel code, so it is necessary to use privileged level operations to access and modify it. The direct reading and writing system call table replacement address method uses the symbol export mechanism in the kernel code to find the address of the system call table in the kernel code. Then, using privileged level operations, the address of the system call table can be mapped to the user space, so that the function address in the system call table can be read and modified in the user space. Replacing the implementation function of the system call requires the following steps: define a function with the same name as the original function in the user space to replace the implementation of the original function. Use the direct reading and writing system call table replacement address method to replace the function address of the corresponding system call in the system call table with the address of the new function. When the program calls a system call, the kernel will first check the function address of the corresponding system call in the system call table. If the function address is replaced with the address of the new function, the implementation of the new function will be called. Otherwise, the original function's implementation is called.
[0064] The above implementation scheme may bring security risks, because malicious attackers can use dynamic library preloading or directly read and write system call table to replace the address to inject malicious code. Large and complex projects in actual business scenarios usually have a complete execution framework. This approach with security risks is usually not possible to implement. In addition, the above implementation scheme works during the program runtime, and there are usually recursion traps. Recursion traps refer to the situation in which the program has infinite recursion due to reasons such as too large recursion depth or incorrect recursion termination conditions when writing recursive functions, thereby exhausting system resources or causing the program to crash.
[0065] In response to the problems existing in the above-mentioned related technologies, an embodiment of the present application provides a solution to the noise traffic problem implemented during the compilation period of the program code. The solution can sense the system functions that need to be recorded in real time, dynamically generate rule files for test projects, and thus dynamically add required functions. It supports dynamic replacement of various types of library functions and business functions, eliminates noise factors, avoids intrusion into business codes, and can be controlled in the code to avoid recursive traps.
[0066] The following is a detailed description of the technical solutions such as the code testing method, code testing device, computer-readable medium, electronic device, and computer program product provided by the present application in conjunction with specific implementation methods.
[0067] Figure 2A flowchart of a code testing method in one embodiment of the present application is shown. The code testing method can be performed by Figure 1 The terminal device or server shown in the figure can execute it alone, or it can be executed by the terminal device and the server together. The embodiment of the present application takes the code testing method executed by the terminal device as an example for explanation. Figure 2 As shown, the code testing method may include the following steps S210 to S240.
[0068] S210: Obtain a replacement function list corresponding to the source code to be tested, where the replacement function list is used to store wrapper functions that replace calls to original functions in the source code.
[0069] S220: Generate a rule file according to the replacement function list and the library function with recording and playback functions, where the rule file is used to define the construction rules of the test project of the source code.
[0070] S230: compile the source code according to the rule file to obtain the target code to be tested; when compiling to the original function in the source code, replace the original function with a call wrapper function; when compiling to the library function in the source code, replace the call address of the library function with a library function with recording and playback functions.
[0071] S240: Perform recording and playback testing on the target code.
[0072] In the code testing method provided in the embodiment of the present application, by obtaining a replacement function list corresponding to the source code to be tested, a rule file can be generated according to the replacement function list and the library function with recording and playback function, and the source code is further compiled according to the rule file to obtain the target code to be tested, so that the recording and playback test can be performed on the target code. The embodiment of the present application uses the replacement function list to replace the library function and the business function in the source code with the replacement call wrapper function, which overcomes the problem of easily introducing noise traffic in the recording and playback test, thereby improving the test efficiency.
[0073] The following is a detailed description of each method step of the code testing method in the embodiment of the present application in combination with a specific application scenario.
[0074] In step S210, a replacement function list corresponding to the source code to be tested is obtained, where the replacement function list is used to store wrapper functions that replace calls to original functions in the source code.
[0075] Figure 3 A flow chart of a method for obtaining a replacement function list in one embodiment of the present application is shown. Figure 3 As shown, the method for obtaining a replacement function list in the embodiment of the present application may include the following steps S310 to S330.
[0076] S310: Parse the source code to be tested to obtain an abstract syntax tree of the source code.
[0077] An abstract syntax tree is a tree representation of the abstract grammatical structure of a text (i.e., source code) written in a formal language. Each node on the tree represents a structure that appears in the text. The syntax is abstract because it does not represent every detail that appears in the real syntax, but only the details related to the structure or content. For example, grouping brackets are implicit in the tree structure, so these brackets do not need to be represented as separate nodes. Similarly, a grammatical structure like an if-condition-then statement can be represented by a node with three branches. The abstract syntax tree includes lexical analysis, syntax analysis, semantic analysis, etc., which can accurately determine the type, scope, control domain, etc. of each symbol in the code. A large amount of symbol information will form a symbol table.
[0078] S320: Traverse the abstract syntax tree to obtain an original function of a specified type, where the original function of the specified type is a function that needs to be replaced and called in the recording and playback test.
[0079] In one embodiment of the present application, the original function of the specified type includes one or more of an inline function, a static function, or a class member function.
[0080] An inline function is a function modified with the inline keyword. Functions defined in a class are defined as inline functions by default. From the source code level, an inline function has the structure of a function, but after compilation, it does not have the properties of a function. Inline functions do not transfer control when they are called, but embed the function body in each call during compilation. During compilation, similar to macro replacement, the function body is used to replace the function name at the call site.
[0081] Inline functions are used to insert the code of a function directly into the place where the function is called during compilation, rather than executing it through a function call. In other words, the compiler will copy the code of the inline function to the call site to eliminate the overhead of the function call. Inline functions are mainly used to optimize program performance. Function calls usually require a certain amount of overhead, including operations such as saving and restoring the scene, parameter passing, and jumping. For some short functions, these overheads may be relatively large, so using inline functions can avoid these additional overheads and improve program execution efficiency.
[0082] Static functions refer to functions modified with the static keyword. Static functions include static class member functions and global static functions. Static functions that appear in a class are static class member functions. The function of a static class member function is that calling this function will not access or modify any non-static data objects. Static members (variables and methods) of a class belong to the class itself. Memory will be allocated when the class is loaded, and can be accessed directly through the class name; non-static members (variables and methods) belong to the class object, so memory will be allocated only when the class object is generated (an instance of the class is created), and then accessed through the class object (instance). Static functions that do not appear in a class are global static functions. The difference between global static functions and ordinary functions is that functions modified with static are limited to this source code file and cannot be called by code files outside this source code file, while ordinary functions can be called by other code files by default.
[0083] Add the keyword static before the return type of a function to define it as a static function. The definition and declaration of a normal function are externally visible by default, but a static function is only visible in the file where it is declared and cannot be used by other files. Therefore, the characteristic of a static function is that other files can define functions with the same name without conflict, and a static function cannot be used by other files.
[0084] Class member functions are functions written in the class body to describe the behavior of the class, and may include, for example, constructors, destructors, and copy constructors.
[0085] A constructor is a special method that is mainly used to initialize an object when it is created, that is, to assign initial values to the object's member variables. It is used together with the new operator in the statement that creates the object. A particular class can have multiple constructors, which can be distinguished by the number of parameters or the type of parameters, that is, constructor overloading.
[0086] The destructor is the opposite of the constructor. 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. Destructors are often used to do "cleanup" work. For example, when creating an object, a piece of memory space is opened up with new, and delete will automatically call the destructor to release the memory. The copy constructor, also known as the replication constructor, is a special constructor that is called by the compiler to complete the construction and initialization of other objects based on the same class.
[0087] The parameter of the copy constructor must be a reference, but it is not restricted to const. Generally, the const restriction is added. The copy constructor is often used to pass and return values of user-defined types when calling functions. The copy constructor calls the copy constructor and member functions of the base class. If possible, it will be called in a constant way. In addition, it can also be called in a non-constant way.
[0088] S330: Creating a replacement function list corresponding to the source code to be tested according to the original function of the specified type.
[0089] In one embodiment of the present application, the replacement function list may include one or more of an inline function list, a static function list, or a class member function list.
[0090] When the original function of the specified type is an inline function, the method of creating a replacement function list corresponding to the source code to be tested may include: writing the function name of the inline function into the inline function list.
[0091] The inline function list is used to modify the function embedding logic of the inline function to the function calling logic. The inline functions appearing in the inline function list will not be embedded into the code during the code compilation process, but the calling relationship and called relationship between the inline function and other functions in the code will be retained.
[0092] When the original function of the specified 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 with the same syntax information as the static function, and writing the mapping relationship between the static function and the non-static function into the static function list.
[0093] Figure 4 A flowchart of a method for creating a non-static function in one embodiment of the present application is shown. Figure 4 As shown, the method for creating a non-static function having the same syntax information as a static function may further include the following steps S410 to S430.
[0094] S410: Obtaining syntax information of a static function, where the syntax information includes a file name, a line number, and function call information.
[0095] Based on the abstract syntax tree obtained by parsing the source code, the syntax information of all function objects can be obtained, such as the line number, file name and function call information of a single function, where the function call information includes other functions that call the static function.
[0096] S420: Rename the static function according to the file name and line number, and remove the keyword representing the static function definition in the static function to obtain a non-static function.
[0097] Static functions are generally effective in a single file. Directly removing the keyword static that represents the definition of a static function may introduce interference from functions with the same name, leading to compilation errors. In the embodiment of the present application, the static definition of the static function in the static function list static_functions_list is removed, the function is renamed, and all names that call the function are modified.
[0098] S430: Modify the function that calls the static function to call a non-static function according to the function calling information.
[0099] For example, the original static function static int my_function(){} in the source code will be replaced with the non-static function int my_function_filename_linenum_5678styc{}. Filename and linenum represent the file name and line number of the function, respectively, and 5678styc is a random 8-bit string. All the code that calls the static function will be modified to call the non-static function.
[0100] The static function is associated with the created non-static function and written into the static function list. Therefore, when compiling the source code, the code that originally called the static function can be replaced with the code that called the non-static function according to the mapping relationship saved in the static function list, thereby de-staticizing the static function and avoiding the problem of interference from functions with the same name.
[0101] In one embodiment of the present application, when the original function of the specified type is a class member function, a wrapper function and a real function having a redirection relationship with the class member function are created, and the mapping relationship between the wrapper function and the real function is written into the class member function list.
[0102] Figure 5 A flow chart of a method for creating a redirection function for a class member function in one embodiment of the present application is shown. Figure 5 As shown, the method for creating a wrapper function and a real function having a redirection relationship with a class member function may further include the following steps S510 to S530.
[0103] S510: Obtain a symbol table parsed from the source code to be tested.
[0104] In the process of parsing the source code to be tested to obtain the abstract syntax tree, a symbol table corresponding to the source code can be generated synchronously. The symbol table is used to record various symbol information such as the type, scope, control domain, etc. of each symbol in the code.
[0105] S520: Searching for a modified function that matches the class member function according to the symbol table.
[0106] In one embodiment of the present application, the method of searching for a modified function that matches a class member function according to a symbol table may further include: decoding the symbol table to obtain a decoded name corresponding to the modified name of the class member function; searching for a decoded name that is the same as the original name of the class member function, and determining the modified function that matches the class member function according to the modified name corresponding to the decoded name.
[0107] S530: 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.
[0108] 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, the original function name in the embodiment of the present application is my_function, and the decorated function name is my_decorated_function, then a corresponding wrapper function __wrap__my_decorated_function and a corresponding real function __real__my_decorated_function are created for the decorated function. By redirecting the class member function to the wrapper function, the function that originally called the class member function will call the wrapper function instead; by directing the real function to the class member function, the code that uses the real function can actually use the original class member function.
[0109] Based on the above embodiments, the present application realizes the applicability of inline functions, static functions and class member functions for recording and playback testing.
[0110] In step S220, a rule file is generated according to the replacement function list and the library function with recording and playback functions, and the rule file is used to define the construction rules of the test project of the source code.
[0111] In one embodiment of the present application, before generating a rule file based on a replacement function list and a library function with a recording and playback function, a function library for calling the source code can be obtained based on the code language of the source code; original library functions that can introduce noise in the recording and playback test can be screened from the function library; and the original library functions can be rewritten to obtain a library function with a recording and playback function.
[0112] A function library is a collection of functions with certain functions created by the system. The library stores the function names and corresponding target codes, as well as the relocation information required during the linking process. Users can also create their own user function libraries according to their needs.
[0113] Library functions are functions stored in function libraries. Library functions have clear functions, entry call parameters and return values.
[0114] In one embodiment of the present application, the method of rewriting the original library function to obtain a library function with recording and playback functions may further include: 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 recording and playback functions; the recording logic is used to record the output parameters of the original library function in the recording mode; the playback logic is used to read the output parameters of the original library function recorded in the recording mode in the playback mode, and is used to call the original library function in the non-playback mode.
[0115] In one embodiment of the present application, the original library function that can introduce noise in the recording and playback test includes a function whose output parameter has time correlation or a function whose output parameter has randomness.
[0116] For example, a function whose output parameter has time correlation may be a function for obtaining a timestamp or a function for obtaining a cache, and a function whose output parameter has randomness may be a function for obtaining a random number.
[0117] For example, the original library function is GetTraceID(), and the embodiment of the present application can initialize a corresponding proxy function GetTraceIDWithMock(). In recording mode, it is determined whether a called function needs to be replayed. If replay is not required, the original library function GetTraceID() is directly called; if replay is required, the proxy function GetTraceIDWithMock() is called instead.
[0118] In one embodiment of the present application, before generating a rule file based on the replacement function list and the library function with recording and playback functions, the library function with recording and playback functions is associated with the original library function and then written into the function list.
[0119] By writing the library function with the recording and playback function into the function list, the function that calls the original library function can be replaced by the function that calls the library function with the recording and playback function when compiling the source code.
[0120] In step S230, the source code is compiled according to the rule file to obtain the target code to be tested. When compiled into the original function in the source code, the original function is replaced by calling the wrapper function; when compiled into the library function in the source code, the call address of the library function is replaced by the library function with the recording and playback function.
[0121] In one embodiment of the present application, a method for compiling source code according to a rule file may include: obtaining a compilation parameter added or updated to the rule file, the compilation parameter including one or more of an inline function, a static function, or a class member function. When the compilation parameter is an inline function, the code embedding operation of the inline function is skipped; when the compilation parameter is a static function, the static function is replaced with a corresponding non-static function; when the compilation parameter is a class member function, the class member function is replaced with a corresponding wrapper function.
[0122] In step S240, a recording and playback test is performed on the target code.
[0123] Figure 6 The interactive flow chart of recording and playback testing in an application scenario of the embodiment of the present application is shown. Figure 6 As shown, the interactive process of recording and playback in this application scenario includes the following process.
[0124] S601: The developer initiates a compilation task for source code through a compiler.
[0125] The compiler used in the embodiments of the present application can be a compilation tool encapsulated by Bazel as the underlying tool combined with business characteristics. Bazel is a construction tool for building and testing software projects, responsible for scheduling and managing various construction tasks. Bazel is used to manage the construction process of the project, including dependency resolution, resource collection, and compilation task scheduling, etc.
[0126] When Bazel triggers a compilation task, GCC (GNU Compiler Collection) plays the role of a compiler, responsible for compiling source code (such as C language code and C++ code) into executable files or library files. GCC is a collection of compiler tools, including compilers for C, C++, Objective-C, Fortran, Ada, and other languages. GCC is widely used on various operating systems and architectures, and can be used to build various types of applications and libraries.
[0127] S602: The compiler constructs a rule file using a pre-configured replacement function list and a library function with recording and playback functions.
[0128] The compiler reads the pre-configured list of functions that need to be dynamically replaced, and pulls the pre-written C++ basic library functions with recording and playback capabilities to generate the BUILD file of the module under test. In Bazel, the BUILD file is a text file used to describe the build rules in the project. Each directory can have one or more BUILD files, which define the build rules under this directory. Various build rules can be defined in the BUILD file, such as compiling source code, generating library files, generating executable files, and so on.
[0129] Taking the GCC compiler as an example, the embodiment of the present application can add new command line options --wrap_class_functions=xx, --fkeep_inline_functions=xx, --fkeep_static_functions=xx to the front end of the GCC compiler, which respectively support dynamic replacement of C++ class member functions, inline functions, and static functions.
[0130] At the same time, the embodiment of the present application can add new command line parsing code to the front end of the GCC compiler, obtain the C++ class member function list wrap_class_functions_list, inline function list inline_functions_list, and static function list static_functions_list that require special processing, and save them to memory for processing in subsequent steps.
[0131] To reflect the use of GCC's --wrap option in Bazel's BUILD file, you need to add compilation parameters to Bazel's cc_binary or cc_library rules. This can be achieved by using the linkopts parameter. Since the --wrap capability is only used in recording and playback scenarios, the BUILD file cannot be merged into the code trunk and needs to be generated dynamically. The implementation process is as follows:
[0132] Call the configuration management system to pull the list of functions that need to be dynamically replaced. The data format is as follows:
[0133] {
[0134] "my_test_module":{
[0135] {
[0136] "function_name":"my_function_1",
[0137] "function_type":"common"
[0138] },
[0139] {
[0140] "function_name":"my_function_2",
[0141] "function_type":"inline"
[0142] },
[0143] {
[0144] "function_name":"my_function_3",
[0145] "function_type":"class"
[0146] },
[0147] {
[0148] "function_name":"my_function_4",
[0149] "function_type":"static"
[0150] }
[0151] }
[0152] }
[0153] According to the name of the module under test, my_test_module, and the type of function that needs to be replaced, find the compilation dependency part of the BUILD file of the business module, and add or update linkopts, as shown below:
[0154] cc_binary(
[0155] name="my_binary",
[0156] srcs=["main.cpp","original_function.cpp"],
[0157] linkopts=["-Wl,--wrap=my_function_1,
[0158] -Wl,--wrap=my_function_2,
[0159] -Wl,--wrap=my_function_3,
[0160] -Wl,--wrap=my_function_4,
[0161] -Wl,--fkeep_inline_functions=my_function_2,
[0162] -Wl,--wrap_class_functions=my_function_3",
[0163] -Wl,--fkeep_static_functions=my_function_4"], )
[0165] Among them, --fkeep_inline_functions, --wrap_class_functions, and --fkeep_static_functions respectively represent the inline functions, class member functions, and static functions that need to be dynamically replaced.
[0166] S603: Compile the source code according to the rule file to obtain the target code.
[0167] Combine the BUILD file and source code to compile into binary and deploy to the recording environment.
[0168] The embodiment of the present application modifies the optimizer in the compiler to handle the support of the --wrap option for inline functions, mainly optimizing the function inline optimization step of the optimizer. When the function name to be optimized is obtained in the inline function list inline_functions_list of the above step, the optimization of the inline function of this part will be skipped. Skipping processing will not affect the compilation result, but may affect the performance of the operation. The embodiment of the present application controls it through the fkeep_inline_functions option, so the impact on performance is controllable.
[0169] The embodiment of the present application modifies the optimizer to handle the support of the --wrap option for static functions, mainly to de-staticize static methods. Static functions are generally effective in a single file. Directly removing the static definition may introduce interference from functions of the same name, resulting in compilation errors. The embodiment of the present application removes the static definition of the static function in the static function list static_functions_list in the above steps, renames the function, and modifies the names of all calls to the function.
[0170] The embodiment of the present application modifies the optimizer to process the original C++ class member function name. The embodiment of the present application implements a parser inside the optimizer, uses the __cxa_demangle function to decode the symbol table generated by the optimizer, and searches for the modified name of the C++ member function according to the original name provided.
[0171] The embodiment of the present application modifies the linker in the compiler to support dynamic replacement of C++ class member functions. According to the class member function list obtained by the optimizer in the above steps, the C++ class member functions that need to be mapped can be extracted. The embodiment of the present application uses objcopy to create an alias and maps 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, changing the name of the symbol to a new name. When --wrap=my_function is specified in the initial compilation command, the function will be dynamically replaced during the link processing workflow.
[0172] The embodiment of the present application modifies the linker to support dynamic replacement of static functions. According to the static function list obtained by the optimizer in the above steps, the same operation as the class member function can be performed to realize dynamic replacement of static functions.
[0173] S604: Perform recording and playback testing on the target code.
[0174] When traffic comes in, the input parameters and return results of these functions will be recorded.
[0175] The embodiment of the present application supports both inline functions and static functions by adding the compilation options -fkeep-inline-functions and -fwrap-cpp-method. In addition, the compilation tool of the embodiment of the present application is modified. When the compilation action before the recording task is triggered, the function list that needs to be dynamically replaced that has been registered in the configuration management system is pulled, and the corresponding BUILD file is generated and replaced before the compilation action is executed.
[0176] The embodiment of the present application automatically records library functions and business functions of C++ code and applies them to daily regression testing, greatly reducing the intrusion into business code. It supports automatic recording and playback of business library functions containing noise factors in a more scientific and accurate way, improves the maintainability and readability of business code, and reduces the workload of transformation for noise traffic elimination during recording and playback access.
[0177] The solution for automatically recording C++ library functions and business functions based on GCC secondary development provided in the embodiment of the present application can not only be used in recording and playback scenarios, but also in the field of security testing and analysis, etc.
[0178] It should be noted that although the steps of the method in the present application are described in a specific order in the drawings, this does not require or imply that the steps must be performed in this specific order, or that all the steps shown must be performed to achieve the desired results. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step, and / or one step may be decomposed into multiple steps, etc.
[0179] The following introduces an apparatus embodiment of the present application, which can be used to execute the code testing method in the above-mentioned embodiment of the present application. Figure 7 The structure block diagram of the code testing device provided in the embodiment of the present application is schematically shown. Figure 7 As shown, the code testing device 700 includes:
[0180] The acquisition module 710 is configured to acquire a replacement function list corresponding to the source code to be tested, wherein the replacement function list is used to store wrapper functions that replace calls to original functions in the source code;
[0181] A generating module 720 is configured to generate a rule file according to the replacement function list and the library function with recording and playback functions, wherein the rule file is used to define a construction rule of the test project of the source code;
[0182] The compiling module 730 is configured to compile the source code according to the rule file to obtain the target code to be tested; when compiling to the original function in the source code, the original function is replaced by calling the wrapper function; when compiling to the library function in the source code, the calling address of the library function is replaced by the library function with the recording and playback function;
[0183] The testing module 740 is configured to perform a recording and playback test on the target code.
[0184] In some embodiments of the present application, based on the above technical solution, the code testing device 700 further includes:
[0185] A function library acquisition module, configured to acquire a function library for the source code to call according to a code language of the source code;
[0186] A library function screening module, configured to screen original library functions that can introduce noise in the recording and playback test from the function library;
[0187] The library function writing module is configured to rewrite the original library function to obtain a library function with recording and playback functions.
[0188] In some embodiments of the present application, based on the above technical solution, the library function writing module is also 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 recording and playback functions; the recording logic is used to record the output parameters of the original library function in the recording mode; the playback logic is used to read the output parameters of the original library function recorded in the recording mode in the playback mode, and is used to call the original library function in the non-playback mode.
[0189] In some embodiments of the present application, based on the above technical solution, the original library function that can introduce noise in the recording and playback test includes a function whose output parameters have time correlation or a function whose output parameters have randomness.
[0190] In some embodiments of the present application, based on the above technical solution, the code testing device 700 further includes:
[0191] The function writing module is configured to write the library function with recording and playback functions into the replacement function list after associating it with the original library function.
[0192] In some embodiments of the present application, based on the above technical solution, the acquisition module 710 includes:
[0193] A parsing module, configured to parse the source code to be tested to obtain an abstract syntax tree of the source code;
[0194] A traversal module is configured to traverse the abstract syntax tree to obtain an original function of a specified type, where the original function of the specified type is a function that needs to be replaced and called in a recording and playback test;
[0195] 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.
[0196] In some embodiments of the present application, based on the above technical solution, the original function of the specified type includes one or more of an inline function, a static function or a class member function.
[0197] In some embodiments of the present application, based on the above technical solution, the replacement function list includes an inline function list, a static function list or a class member function list; the creation module includes:
[0198] A first creation module is configured to write a function name of the inline function into the inline function list when the original function of the specified type is an inline function;
[0199] A second creation module is configured to, when the original function of the specified type is a static function, create a non-static function having the same grammatical information as the static function, and write a mapping relationship between the static function and the non-static function into the static function list;
[0200] The third creation module is configured to create a wrapper function and a real function that have a redirection relationship with the class member function when the original function of the specified type is a class member function, and write the mapping relationship between the wrapper function and the real function into the class member function list.
[0201] In some embodiments of the present application, based on the above technical solution, the second creation module is further configured to: obtain the syntax information of the static function, the syntax information including the file name, line number and function call information; rename the static function according to the file name and line number, and remove the keywords representing the static function definition in the static function to obtain a non-static function; modify the function that calls the static function to call the non-static function according to the function call information.
[0202] In some embodiments of the present application, based on the above technical solution, the third creation module is further configured to: obtain a symbol table parsed from the source code to be tested; search for a modified function that matches 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.
[0203] In some embodiments of the present application, based on the above technical solution, the third creation module is further configured to: decode the symbol table to obtain a decoded name corresponding to the modified name of the class member function; search for a decoded name that is the same as the original name of the class member function, and determine a modified function that matches the class member function based on the modified name corresponding to the decoded name.
[0204] In some embodiments of the present application, based on the above technical solution, the compiling module 730 includes:
[0205] A parameter acquisition module, configured to acquire compilation parameters added or updated to the rule file, wherein the compilation parameters include one or more of an inline function, a static function or a class member function;
[0206] A first compiling module is configured to skip the code embedding operation on the inline function when the compiling parameter is the inline function;
[0207] A second compiling module is configured to replace the static function with a corresponding non-static function when the compiling parameter is the static function;
[0208] The third compiling module is configured to replace the class member function with a corresponding wrapper function when the compiling parameter is the class member function.
[0209] The specific details of the code testing device provided in each embodiment of the present application have been described in detail in the corresponding method embodiment and will not be repeated here.
[0210] Figure 8 The structure block diagram of a computer system for implementing an electronic device according to an embodiment of the present application is schematically shown.
[0211] It should be noted that Figure 8 The computer system 800 of the electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0212] like Figure 8 As shown, the computer system 800 includes a central processing unit 801 (CPU), which can perform various appropriate actions and processes according to the program stored in the read-only memory 802 (ROM) or the program loaded from the storage part 808 to the random access memory 803 (RAM). Various programs and data required for system operation are also stored in the random access memory 803. 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. The input / output interface 805 (Input / Output interface, i.e., I / O interface) is also connected to the bus 804.
[0213] The following components are connected to the input / output interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a local area network card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A 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, etc., is installed on the drive 810 as needed so that a computer program read therefrom is installed into the storage section 808 as needed.
[0214] In particular, according to an embodiment of the present application, the process described in each method flow chart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer readable medium, and the computer program contains a program code for executing the method shown in the flow chart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processor 801, various functions defined in the system of the present application are executed.
[0215] It should be noted that the computer-readable medium shown in the embodiment of the present application may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, a system, device or device of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a flash memory, an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by an instruction execution system, device or device or used in combination with it. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, wherein a computer-readable program code is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer readable signal media may also be any computer readable medium other than computer readable storage media, which may send, propagate, or transmit programs for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.
[0216] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the above-mentioned module, program segment or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0217] It should be noted that, although several modules or units of the equipment for action execution are mentioned in the above detailed description, this division is not mandatory. In fact, according to the embodiments of the present application, the features and functions of two or more modules or units described above can be embodied in one module or unit. On the contrary, the features and functions of one module or unit described above can be further divided into being embodied by multiple modules or units.
[0218] Through the description of the above implementation methods, it is easy for those skilled in the art to understand that the example implementation methods described here can be implemented by software or by combining software with necessary hardware. Therefore, the technical solution according to the implementation methods of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, and includes several instructions to enable a computing device (which can be a personal computer, a server, a touch terminal, or a network device, etc.) to execute the method according to the implementation methods of the present application.
[0219] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary technical means in the art that are not disclosed in the present application.
[0220] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.
Claims
1. A code testing method, characterized in that: include: Acquire a replacement function list corresponding to the source code to be tested, wherein the replacement function list is used to store wrapper functions that replace calls to original functions in the source code; Generate a rule file according to the replacement function list and the library function with recording and playback functions, wherein the rule file is used to define the construction rules of the test project of the source code; Compile the source code according to the rule file to obtain the target code to be tested; When compiled into the original function in the source code, the original function is replaced by calling the wrapper function; when compiled into the library function in the source code, the calling address of the library function is replaced by the library function with the recording and playback function; Perform a record-playback test on the target code.
2. The code testing method according to claim 1, characterized in that: Before generating a rule file according to the replacement function list and the library function with recording and playback functions, the method further includes: Acquire a function library for the source code to call according to the code language of the source code; Screening out original library functions that can introduce noise in the recording and playback test from the function library; The original library function is rewritten to obtain a library function with recording and playback functions.
3. The code testing method according to claim 2, characterized in that: Rewrite the original library function to obtain a library function with recording and playback functions, including: Creating a proxy function corresponding to the original library function; Recording logic and playback logic are written into the proxy function to obtain a library function with recording and playback functions; the recording logic is used to record the output parameters of the original library function in the recording mode; the playback logic is used to read the output parameters of the original library function recorded in the recording mode in the playback mode, and is used to call the original library function in the non-playback mode.
4. The code testing method according to claim 2, characterized in that: Original library functions that can introduce noise in record-playback tests include functions whose output parameters have time correlation or functions whose output parameters have randomness.
5. The code testing method according to claim 2, characterized in that: Before generating a rule file according to the replacement function list and the library function with recording and playback functions, the method further includes: After associating the library function with the recording and playback function with the original library function, the library function is written into the replacement function list.
6. The code testing method according to any one of claims 1 to 5, characterized in that: Get a list of replacement functions corresponding to the source code to be tested, including: Parsing the source code to be tested to obtain an abstract syntax tree of the source code; Traversing the abstract syntax tree to obtain a primitive function of a specified type, where the primitive function of the specified type is a function that needs to be replaced and called in a recording and playback test; A replacement function list corresponding to the source code to be tested is created according to the original function of the specified type.
7. The code testing method according to claim 6, characterized in that: The original function of the specified type includes one or more of an inline function, a static function or a class member function.
8. The code testing method according to claim 6, characterized in that: The replacement function list includes an inline function list, a static function list or a class member function list; Create a list of replacement functions corresponding to the source code to be tested, including: When the original function of the specified type is an inline function, writing the function name of the inline function into the inline function list; When the original function of the specified type is a static function, creating a non-static function having the same syntax information as the static function, and writing a mapping relationship between the static function and the non-static function into the static function list; When the original function of the specified type is a class member function, a wrapper function and a real function having a redirection relationship with the class member function are created, and a mapping relationship between the wrapper function and the real function is written into the class member function list.
9. The code testing method according to claim 8, characterized in that: Create a non-static function with the same syntax information as the static function, including: Obtaining syntax information of the static function, the syntax information including file name, line number and function call information; Rename the static function according to the file name and line number, and remove the keyword representing the static function definition in the static function to obtain a non-static function; The function that calls the static function is modified to call the non-static function according to the function calling information.
10. The code testing method according to claim 8, characterized in that: Create a wrapper function and a real function that have a redirection relationship with the class member function, including: Obtaining a symbol table parsed from the source code to be tested; Searching for a modified function that matches the class member function according to the symbol table; A wrapper function and a real function corresponding to the decorated function are created, the class member function is redirected to the wrapper function, and the real function is directed to the class member function.
11. The code testing method according to claim 10, characterized in that: Searching for a modified function matching the class member function according to the symbol table includes: Decoding the symbol table to obtain a decoded name corresponding to the modified name of the class member function; A decoded name that is the same as the original name of the class member function is searched, and a modified function that matches the class member function is determined according to the modified name corresponding to the decoded name.
12. The code testing method according to claim 10, characterized in that: Compiling the source code according to the rule file includes: Acquire compilation parameters added or updated to the rule file, where the compilation parameters include one or more of an inline function, a static function, or a class member function; When the compilation parameter is the inline function, skipping the code embedding operation on the inline function; When the compilation parameter is the static function, replacing the static function with a corresponding non-static function; When the compilation parameter is the class member function, the class member function is replaced with a corresponding wrapper function.
13. A code testing device, characterized in that: include: An acquisition module is configured to acquire a replacement function list corresponding to the source code to be tested, wherein the replacement function list is used to store wrapper functions that replace calls to original functions in the source code; A generating module is configured to generate a rule file according to the replacement function list and the library function with recording and playback functions, wherein the rule file is used to define a construction rule of the test project of the source code; A compiling module is configured to compile the source code according to the rule file to obtain a target code to be tested; when compiling to an original function in the source code, the original function is replaced by a call to the wrapper function; when compiling to a library function in the source code, the call address of the library function is replaced by the library function with the recording and playback function; The test module is configured to perform a recording and playback test on the target code.
14. A computer readable medium, characterized in that The computer-readable medium stores a computer program, and when the computer program is executed by a processor, the code testing method according to any one of claims 1 to 12 is implemented.
15. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor is configured to execute the executable instructions to implement the code testing method as described in any one of claims 1 to 12.
16. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the code testing method according to any one of claims 1 to 12 is implemented.
Citation Information
Cited By
Kernel function debugging method and device and storage medium
CN122432022A