Instrument Neutral Distributed Interface INDI  2.0.2
DriverMock.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2022 Ludovic Pollet. All rights reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 *******************************************************************************/
18 
19 #include <system_error>
20 
21 #include "DriverMock.h"
22 #include "utils.h"
23 
24 
26  std::string abstractPath;
27  bool ready = false;
28 public:
30  void setup() {
31  if (ready) return;
32  ready = true;
33  // Create a socket that will not have the clo on exec flag ?
34  abstractPath = "/tmp/fakedriver-test";
35  setenv("FAKEDRIVER_ADDRESS", abstractPath.c_str(), 1);
36 
37  serverConnection = unixSocketListen(abstractPath);
38  }
39 };
40 
41 static FakeDriverListener driverListener;
42 
43 
45 {
46  driverListener.setup();
47 }
48 
50 {
51  driverConnection = socketAccept(driverListener.serverConnection);
52  unixSocketRecvFds(driverConnection, 2, driverFds);
53  cnx.setFds(driverFds[0], driverFds[1]);
54 }
55 
57 {
58  cnx.setFds(-1, -1);
59  if (driverConnection != -1) close(driverConnection);
60  if (driverFds[0] != -1) close(driverFds[0]);
61  if (driverFds[1] != -1) close(driverFds[1]);
62  driverConnection = -1;
63  driverFds[0] = -1;
64  driverFds[1] = -1;
65 }
66 
67 
69 {
70  cnx.send("<pingRequest uid='flush'/>\n");
71  cnx.expectXml("<pingReply uid=\"flush\"/>");
72 }
73 
75 {
76  driverConnection = -1;
77  driverFds[0] = -1;
78  driverFds[1] = -1;
79 }
80 
82 {
84 }
void send(const std::string &content)
void setFds(int rd, int wr)
void expectXml(const std::string &xml)
void ping()
Definition: DriverMock.cpp:68
void terminateDriver()
Definition: DriverMock.cpp:56
ConnectionMock cnx
Definition: DriverMock.h:50
virtual ~DriverMock()
Definition: DriverMock.cpp:81
void waitEstablish()
Definition: DriverMock.cpp:49
void setup()
Definition: DriverMock.cpp:44
int socketAccept(int fd)
Definition: utils.cpp:144
int unixSocketListen(const std::string &unixAddr)
Definition: utils.cpp:71
void unixSocketRecvFds(int fd, int count, int *fdsDest)
Definition: utils.cpp:228