Instrument Neutral Distributed Interface INDI 2.2.5
TestIndiClient.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 <stdexcept>
20#include <stdio.h>
21#include <unistd.h>
22#include <sys/types.h>
23#include <system_error>
24#include <thread>
25
26#include "gtest/gtest.h"
27
28#include "basedevice.h"
29#include "baseclient.h"
30#include "indiproperty.h"
31
32#include "utils.h"
33
34#include "ServerMock.h"
35#include "IndiClientMock.h"
36
37class MyClient : public INDI::BaseClient
38{
39 std::string dev, prop;
40 public:
41 MyClient(const std::string &dev, const std::string &prop) : dev(dev), prop(prop) {}
42 virtual ~MyClient() {}
43 protected:
44 virtual void newDevice(INDI::BaseDevice) override
45 {
46 std::cerr << "new device\n";
47 }
48 virtual void removeDevice(INDI::BaseDevice) override
49 {
50 std::cerr << "remove device\n";
51 }
52 virtual void newProperty(INDI::Property) override
53 {
54 };
55 virtual void removeProperty(INDI::Property) override
56 {
57 }
58
59 virtual void serverConnected() override
60 {
61 std::cerr << "server connected\n";
62 }
63 virtual void serverDisconnected(int) override
64 {
65 std::cerr << "server disconnected\n";
66 }
67};
68
69TEST(IndiclientTcpConnect, ClientConnect)
70{
71 ServerMock fakeServer;
72 IndiClientMock indiServerCnx;
73
75
76 // Listen on any free port so that tests can run in parallel
77 fakeServer.listen(0);
78
79 MyClient * client = new MyClient("machin", "truc");
80 client->setServer("127.0.0.1", fakeServer.tcpPort());
81
82 // FIXME : async
83 // auto wait = runAsync([&fakeServer]=>{
84 std::thread t1([&fakeServer, &indiServerCnx]()
85 {
86 fakeServer.accept(indiServerCnx);
87 indiServerCnx.cnx.expectXml("<getProperties version='1.7'/>");
88 });
89 // });
90 bool connected = client->connectServer();
91 ASSERT_EQ(connected, true);
92
93 t1.join();
94
95 // Check client reply to ping...
96 indiServerCnx.cnx.send("<pingRequest uid='123456'/>");
97 indiServerCnx.cnx.expectXml("<pingReply uid='123456'/>");
98}
TEST(IndiclientTcpConnect, ClientConnect)
void send(const std::string &content)
void expectXml(const std::string &xml)
void setServer(const char *hostname, unsigned int port)
Set the server host name and port.
Class to provide basic client functionality.
Definition baseclient.h:52
bool connectServer() override
Connect to INDI server.
Class to provide basic INDI device functionality.
Definition basedevice.h:52
Provides generic container for INDI properties.
ConnectionMock cnx
virtual void removeProperty(INDI::Property) override
Emmited when a property is deleted for an INDI driver.
MyClient(const std::string &dev, const std::string &prop)
virtual void serverConnected() override
Emmited when the server is connected.
virtual ~MyClient()
virtual void newDevice(INDI::BaseDevice) override
Emmited when a new device is created from INDI server.
virtual void removeDevice(INDI::BaseDevice) override
Emmited when a device is deleted from INDI server.
virtual void serverDisconnected(int) override
Emmited when the server gets disconnected.
virtual void newProperty(INDI::Property) override
Emmited when a new property is created for an INDI driver.
void listen(int tcpPort)
void accept(IndiClientMock &into)
int tcpPort() const
void setupSigPipe()
Definition utils.cpp:37