Instrument Neutral Distributed Interface INDI  2.0.2
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 
37 #define TEST_TCP_PORT 17624
38 #define TEST_UNIX_SOCKET "/tmp/indi-test-server"
39 #define STRINGIFY_TOK(x) #x
40 #define TO_STRING(x) STRINGIFY_TOK(x)
41 
42 class MyClient : public INDI::BaseClient
43 {
44  std::string dev, prop;
45  public:
46  MyClient(const std::string &dev, const std::string &prop) : dev(dev), prop(prop) {}
47  virtual ~MyClient() {}
48  protected:
49  virtual void newDevice(INDI::BaseDevice) override
50  {
51  std::cerr << "new device\n";
52  }
53  virtual void removeDevice(INDI::BaseDevice) override
54  {
55  std::cerr << "remove device\n";
56  }
57  virtual void newProperty(INDI::Property) override
58  {
59  };
60  virtual void removeProperty(INDI::Property) override
61  {
62  }
63 
64  virtual void serverConnected() override
65  {
66  std::cerr << "server connected\n";
67  }
68  virtual void serverDisconnected(int) override
69  {
70  std::cerr << "server disconnected\n";
71  }
72 };
73 
74 TEST(IndiclientTcpConnect, ClientConnect)
75 {
76  ServerMock fakeServer;
77  IndiClientMock indiServerCnx;
78 
79  setupSigPipe();
80 
81  fakeServer.listen(TEST_TCP_PORT);
82 
83  MyClient * client = new MyClient("machin", "truc");
84  client->setServer("127.0.0.1", TEST_TCP_PORT);
85 
86  // FIXME : async
87  // auto wait = runAsync([&fakeServer]=>{
88  std::thread t1([&fakeServer, &indiServerCnx]()
89  {
90  fakeServer.accept(indiServerCnx);
91  indiServerCnx.cnx.expectXml("<getProperties version='1.7'/>");
92  });
93  // });
94  bool connected = client->connectServer();
95  ASSERT_EQ(connected, true);
96 
97  t1.join();
98 
99  // Check client reply to ping...
100  indiServerCnx.cnx.send("<pingRequest uid='123456'/>");
101  indiServerCnx.cnx.expectXml("<pingReply uid='123456'/>");
102 }
#define TEST_TCP_PORT
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.
Definition: baseclient.cpp:308
Class to provide basic INDI device functionality.
Definition: basedevice.h:52
Provides generic container for INDI properties.
Definition: indiproperty.h:48
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)
Definition: ServerMock.cpp:48
void accept(IndiClientMock &into)
Definition: ServerMock.cpp:60
void setupSigPipe()
Definition: utils.cpp:37