1*2fa10f42SJagpal Singh Gill #include "test_base.hpp"
2*2fa10f42SJagpal Singh Gill
BaseTest(const char * clientDevicePath,const char * serverDevicePath,const char * serviceName)3*2fa10f42SJagpal Singh Gill BaseTest::BaseTest(const char* clientDevicePath, const char* serverDevicePath,
4*2fa10f42SJagpal Singh Gill const char* serviceName)
5*2fa10f42SJagpal Singh Gill {
6*2fa10f42SJagpal Singh Gill std::string socatCmd = std::format(
7*2fa10f42SJagpal Singh Gill "socat -x -v -d -d pty,link={},rawer,echo=0,parenb,{} pty,link={},rawer,echo=0,parenb,{} & echo $!",
8*2fa10f42SJagpal Singh Gill serverDevicePath, strBaudeRate, clientDevicePath, strBaudeRate);
9*2fa10f42SJagpal Singh Gill
10*2fa10f42SJagpal Singh Gill // Start socat in the background and capture its PID
11*2fa10f42SJagpal Singh Gill FILE* fp = popen(socatCmd.c_str(), "r");
12*2fa10f42SJagpal Singh Gill EXPECT_NE(fp, nullptr) << "Failed to start socat: " << strerror(errno);
13*2fa10f42SJagpal Singh Gill EXPECT_GT(fscanf(fp, "%d", &socatPid), 0);
14*2fa10f42SJagpal Singh Gill pclose(fp);
15*2fa10f42SJagpal Singh Gill
16*2fa10f42SJagpal Singh Gill // Wait for socat to start up
17*2fa10f42SJagpal Singh Gill sleep(1);
18*2fa10f42SJagpal Singh Gill
19*2fa10f42SJagpal Singh Gill fdClient = open(clientDevicePath, O_RDWR | O_NOCTTY | O_NONBLOCK);
20*2fa10f42SJagpal Singh Gill EXPECT_NE(fdClient, -1) << "Failed to open serial port " << clientDevicePath
21*2fa10f42SJagpal Singh Gill << " with error: " << strerror(errno);
22*2fa10f42SJagpal Singh Gill
23*2fa10f42SJagpal Singh Gill fdServer = open(serverDevicePath, O_RDWR | O_NOCTTY | O_NONBLOCK);
24*2fa10f42SJagpal Singh Gill EXPECT_NE(fdServer, -1) << "Failed to open serial port " << serverDevicePath
25*2fa10f42SJagpal Singh Gill << " with error: " << strerror(errno);
26*2fa10f42SJagpal Singh Gill
27*2fa10f42SJagpal Singh Gill ctx.request_name(serviceName);
28*2fa10f42SJagpal Singh Gill
29*2fa10f42SJagpal Singh Gill serverTester = std::make_unique<TestIntf::ServerTester>(ctx, fdServer);
30*2fa10f42SJagpal Singh Gill }
31*2fa10f42SJagpal Singh Gill
~BaseTest()32*2fa10f42SJagpal Singh Gill BaseTest::~BaseTest() noexcept
33*2fa10f42SJagpal Singh Gill {
34*2fa10f42SJagpal Singh Gill if (fdClient != -1)
35*2fa10f42SJagpal Singh Gill {
36*2fa10f42SJagpal Singh Gill close(fdClient);
37*2fa10f42SJagpal Singh Gill fdClient = -1;
38*2fa10f42SJagpal Singh Gill }
39*2fa10f42SJagpal Singh Gill if (fdServer != -1)
40*2fa10f42SJagpal Singh Gill {
41*2fa10f42SJagpal Singh Gill close(fdServer);
42*2fa10f42SJagpal Singh Gill fdServer = -1;
43*2fa10f42SJagpal Singh Gill }
44*2fa10f42SJagpal Singh Gill kill(socatPid, SIGTERM);
45*2fa10f42SJagpal Singh Gill }
46*2fa10f42SJagpal Singh Gill
SetUp()47*2fa10f42SJagpal Singh Gill void BaseTest::SetUp()
48*2fa10f42SJagpal Singh Gill {
49*2fa10f42SJagpal Singh Gill serverThread = std::thread(&BaseTest::ServerRequestHandler, this);
50*2fa10f42SJagpal Singh Gill }
51*2fa10f42SJagpal Singh Gill
TearDown()52*2fa10f42SJagpal Singh Gill void BaseTest::TearDown()
53*2fa10f42SJagpal Singh Gill {
54*2fa10f42SJagpal Singh Gill exitThread.store(true);
55*2fa10f42SJagpal Singh Gill if (serverThread.joinable())
56*2fa10f42SJagpal Singh Gill {
57*2fa10f42SJagpal Singh Gill serverThread.join();
58*2fa10f42SJagpal Singh Gill }
59*2fa10f42SJagpal Singh Gill }
60*2fa10f42SJagpal Singh Gill
ServerRequestHandler()61*2fa10f42SJagpal Singh Gill void BaseTest::ServerRequestHandler()
62*2fa10f42SJagpal Singh Gill {
63*2fa10f42SJagpal Singh Gill while (!exitThread.load())
64*2fa10f42SJagpal Singh Gill {
65*2fa10f42SJagpal Singh Gill serverTester->processRequests();
66*2fa10f42SJagpal Singh Gill }
67*2fa10f42SJagpal Singh Gill }
68