File: TestIndiClient.cpp

package info (click to toggle)
indi 2.1.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,888 kB
  • sloc: cpp: 217,447; ansic: 31,363; xml: 1,195; sh: 311; makefile: 13
file content (102 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
  Copyright(c) 2022 Ludovic Pollet. All rights reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License version 2 as published by the Free Software Foundation.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
*******************************************************************************/

#include <stdexcept>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <system_error>
#include <thread>

#include "gtest/gtest.h"

#include "basedevice.h"
#include "baseclient.h"
#include "indiproperty.h"

#include "utils.h"

#include "ServerMock.h"
#include "IndiClientMock.h"

#define TEST_TCP_PORT 17624
#define TEST_UNIX_SOCKET "/tmp/indi-test-server"
#define STRINGIFY_TOK(x) #x
#define TO_STRING(x) STRINGIFY_TOK(x)

class MyClient : public INDI::BaseClient
{
        std::string dev, prop;
    public:
        MyClient(const std::string &dev, const std::string &prop) : dev(dev), prop(prop) {}
        virtual ~MyClient() {}
    protected:
        virtual void newDevice(INDI::BaseDevice) override
        {
            std::cerr << "new device\n";
        }
        virtual void removeDevice(INDI::BaseDevice) override
        {
            std::cerr << "remove device\n";
        }
        virtual void newProperty(INDI::Property) override
        {
        };
        virtual void removeProperty(INDI::Property)  override
        {
        }

        virtual void serverConnected()  override
        {
            std::cerr << "server connected\n";
        }
        virtual void serverDisconnected(int)  override
        {
            std::cerr << "server disconnected\n";
        }
};

TEST(IndiclientTcpConnect, ClientConnect)
{
    ServerMock fakeServer;
    IndiClientMock indiServerCnx;

    setupSigPipe();

    fakeServer.listen(TEST_TCP_PORT);

    MyClient * client = new MyClient("machin", "truc");
    client->setServer("127.0.0.1", TEST_TCP_PORT);

    // FIXME : async
    // auto wait = runAsync([&fakeServer]=>{
    std::thread t1([&fakeServer, &indiServerCnx]()
    {
        fakeServer.accept(indiServerCnx);
        indiServerCnx.cnx.expectXml("<getProperties version='1.7'/>");
    });
    // });
    bool connected = client->connectServer();
    ASSERT_EQ(connected, true);

    t1.join();

    // Check client reply to ping...
    indiServerCnx.cnx.send("<pingRequest uid='123456'/>");
    indiServerCnx.cnx.expectXml("<pingReply uid='123456'/>");
}