RPC interface testing technology - interface testing of Tcp protocol

Posted by lisa3711 on Thu, 30 Dec 2021 06:41:10 +0100

This article is excerpted from the internal textbook of Hogwarts Testing Institute

Firstly, clarify the concept of Tcp and conduct interface test for Tcp protocol, which refers to the upper layer protocols based on Tcp protocol, such as Http, serial port, network port, Socket, etc. These agreements and
The Http test method is similar (see the interface automation test section for details), but some adjustments need to be made during the test process.

Socket

Socket is also known as socket. Processes can communicate through the socket, so that multiple devices have the ability to interact. Socket
It is suitable for applications with strict requirements on transmission speed and security, such as the transmission of test data between mobile phone kernel and the outside world. Socket devices support not only computers, but also mobile terminals. If you test socket
The protocol requires the ability to send and receive Socket data or proxy Socket.

The following figure shows the normal Socket communication process:

If you test the Socket protocol, you need to make the following modifications, that is, use the Socket agent to receive Socket data:

Special attention should be paid to changing the Socket address before using the agent. Take Python Socket as an example. Here is a simple Socket
Client and server:

# client import socket               # Import socket module  
s = socket.socket()         # establish socket object host = '127.0.0.1'          # Get local host name port = 12345                # Set the port number s.connect ((host, port)) print (s.recv (1024) decode())s.close()
# Server  
import socket               # Import socket modular s = socket.socket()         # establish socket object host = '127.0.0.1'          # Get local host name port = 12345                # Set port s.bind((host, port))        # Binding port  
s.listen(5)                 # Waiting for client connection while True:    c,addr = s.accept()     # Establish client connection    print(addr)    c.send('Receive the message'.encode())    c.close()                # Close connection

The client can communicate with the server, but the Socket address cannot be changed, that is, 127.0 of the above client code 0.1 and 12345
The port cannot be changed through the configuration file. If both cannot be changed, the road to the agent is blocked:

How to modify? Taking the client code as an example, you can configure host and port through the configuration file:

import socketimport yaml# Through the configuration file, configure the host and port with open ("config. Yaml", "R", encoding = "UTF-8") as F: data = yaml safe_ load(f)host = data. get("host")port = data. get("port")s = socket. socket()s.connect((host, port))print(s.recv(1024). decode())s.close()

config. The contents of yaml are as follows:

host: "127.0.0.1"port: 12345

The above changes can make the application go through the Socket agent. Testers also need a suitable proxy tool, which recommends mitmproxy or self writing Socket proxy. mitmproxy
Please refer to:

mitmproxy official website: https://www.mitmproxy.org/

Other agreements

Other protocols, such as serial port, network port, visa, etc., are similar to the Socket test mode, and can be briefly described with the same diagram:

Other agreements are more unpopular than Sokcet, and there is no suitable agency tool. Testers need to write their own agents, such as serial port protocol. Although Python supports pymaterial
Send and receive serial port, but there is no agent. At this time, the tester needs to write the serial port agent tool by himself. In this process, two listening services need to be started. As shown in the figure below, listening service A listens on port 123
, if data comes in, it will be transmitted to port 456 (or make data changes and mock) and listen to service B. the same is true:

Using two monitoring services, you can write any protocol, but pay attention to the shortcomings. The data transmission time will increase. If you pay too much attention to performance, use this scheme with caution. The following is the reference code, in which only the key logic is retained:

def forward(self):    """    Turn on listening    :return:    """    while True:        # Request received from virtual serial port        virtual_req = self.virtual_ser.recv()        if b'' == virtual_req:            continue        if self.is_call_back:            # Return a null value, let mock_server Decide what to return            real_result = b""        else:            # Wait for the real device to appear            if self.real_ser is None :               # Code omission  
            # Forward the request to the real serial port real_result = self.real_ser.write_by_bytes(virtual_req)  
        # obtain mock The results can be added here mock operation        mock_result = self.mock_server.mock(virtual_req, real_result)        # Write the mock result to the virtual serial port self virtual_ ser. send(mock_result)

Again, the application needs to support port modification before using the agent tool. This part needs to communicate with the development and put forward modification requirements.

** _
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Click for more information