RPC interface testing technology - interface testing of Tcp protocol

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 protocols are similar to the Http test method (see the interface automation test section for details), but some adjustments need to be made during the test process.


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

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's Socket as an example. Here is a simple Socket client and server:

# client
import socket               # Import socket module

s = socket.socket()         # Create socket object
host = ''          # Get local host name
port = 12345                # Set port number
s.connect((host, port))
# Server

import socket               # Import socket module
s = socket.socket()         # Create socket object
host = ''          # 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
    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, the and 12345 ports of the above client code 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 socket
import yaml
# Configure the host and port through the configuration file
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))

The contents of config.yaml are as follows:

host: ""
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. 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 to send and receive serial ports, it has no agents. At this time, the tester needs to write the serial port agent tool by himself. In this process, two monitoring services need to be started. As shown in the figure below, monitoring service A monitors port 123. If data comes in, it will be transmitted (or changed to mock) to port 456. The same is true for monitoring service B:

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
    while True:
        # Request received from virtual serial port
        virtual_req = self.virtual_ser.recv()
        if b'' == virtual_req:
        if self.is_call_back:
            # Return a null value and let mock_server decides what to return
            real_result = b""
            # 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)

        # Get the result of mock, where you can join the mock operation
        mock_result = self.mock_server.mock(virtual_req, real_result)
        # Write mock result to virtual serial port

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.

The following is the supporting information. For the friends doing [software testing], it should be the most comprehensive and complete war preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Finally, it can be in the official account: the sad spicy bar! Get a 216 page interview document of Software Test Engineer for free. And the corresponding video learning tutorials for free!, It includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced continuous test integration, test architecture, development test framework, performance test, security test, etc.

Don't fight alone in learning. It's best to stay together and grow together. The effect of mass effect is very powerful. If we study together and punch in together, we will have more motivation to learn and stick to it. You can join our testing technology exchange group: 914172719 (there are various software testing resources and technical discussions)

Friends who like software testing, if my blog is helpful to you and if you like my blog content, please click "like", "comment" and "collect" for three times!

Haowen recommendation

Job transfer interview, job hopping interview, these interview skills that software testers must know!

Interview experience: move bricks in first tier cities! Another software testing post, 5000 will be satisfied

Interviewer: I've worked for three years and have a preliminary test? I'm afraid your title of software test engineer should be enclosed in double quotation marks

What kind of person is suitable for software testing?

The man who left work on time was promoted before me

The test post changed jobs repeatedly and disappeared

Tags: Python Programmer software testing IT

Posted on Thu, 18 Nov 2021 00:54:49 -0500 by siropchik