~xavi-garcia-mena/storage-provider-webdav/base-url-host-fix

« back to all changes in this revision

Viewing changes to tests/utils/DavEnvironment.cpp

  • Committer: Tarmac
  • Author(s): James Henstridge
  • Date: 2016-09-23 05:03:11 UTC
  • mfrom: (7.1.13 provider-test-fixture)
  • Revision ID: tarmac-20160923050311-bb3m6zal58cpccqu
Add test infrastructure for testing DavProvider.

Approved by Michi Henning, unity-api-1-bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "DavEnvironment.h"
 
2
#include <testsetup.h>
 
3
 
 
4
#include <QDebug>
 
5
 
 
6
#include <unistd.h>
 
7
#include <sys/types.h>
 
8
#include <sys/socket.h>
 
9
#include <arpa/inet.h>
 
10
#include <netinet/in.h>
 
11
#include <chrono>
 
12
#include <stdexcept>
 
13
#include <thread>
 
14
 
 
15
using namespace std;
 
16
 
 
17
namespace
 
18
{
 
19
 
 
20
// Get a probably free IPv4 port number.  We do this by binding a
 
21
// socket and then closing it.  It is possible that the port will be
 
22
// reused, but that is less likely than with fixed port numbers.
 
23
int get_free_port()
 
24
{
 
25
    int sock = socket(AF_INET, SOCK_STREAM, 0);
 
26
    if (sock < 0)
 
27
    {
 
28
        throw runtime_error("get_free_port: Could not create socket");
 
29
    }
 
30
 
 
31
    struct sockaddr_in addr;
 
32
    socklen_t length = sizeof(addr);
 
33
    memset(&addr, 0, length);
 
34
    if (bind(sock, reinterpret_cast<struct sockaddr*>(&addr), length) < 0)
 
35
    {
 
36
        close(sock);
 
37
        throw runtime_error("get_free_port: Could not bind socket");
 
38
    }
 
39
 
 
40
    if (getsockname(sock, reinterpret_cast<struct sockaddr*>(&addr),
 
41
                    &length) < 0 || length > sizeof(addr))
 
42
    {
 
43
        close(sock);
 
44
        throw runtime_error("get_free_port: Could not get socket name");
 
45
    }
 
46
    close(sock);
 
47
    return ntohs(addr.sin_port);
 
48
}
 
49
 
 
50
void wait_for_listen(int port)
 
51
{
 
52
    int sock = socket(AF_INET, SOCK_STREAM, 0);
 
53
    if (sock < 0)
 
54
    {
 
55
        throw runtime_error("wait_for_listen: Could not create socket");
 
56
    }
 
57
    struct sockaddr_in addr;
 
58
    addr.sin_family = AF_INET;
 
59
    addr.sin_port = htons(port);
 
60
    inet_aton("127.0.0.1", &addr.sin_addr);
 
61
 
 
62
    bool success = false;
 
63
    for (int i = 0; i < 100; i++)
 
64
    {
 
65
        if (connect(sock, reinterpret_cast<struct sockaddr*>(&addr),
 
66
                    sizeof(addr)) == 0)
 
67
        {
 
68
            success = true;
 
69
            break;
 
70
        }
 
71
        this_thread::sleep_for(chrono::milliseconds(50));
 
72
    }
 
73
    close(sock);
 
74
    if (!success)
 
75
    {
 
76
        throw runtime_error("wait_for_listen: Could not connect to socket");
 
77
    }
 
78
}
 
79
 
 
80
}
 
81
 
 
82
DavEnvironment::DavEnvironment(QString const& base_dir)
 
83
{
 
84
    int port = get_free_port();
 
85
 
 
86
    server_.setProcessChannelMode(QProcess::ForwardedChannels);
 
87
    server_.setWorkingDirectory(base_dir);
 
88
    server_.start("php", {"-S", QStringLiteral("127.0.0.1:%1").arg(port),
 
89
                          TEST_SRC_DIR "/utils/sabredav-server.php"});
 
90
    if (!server_.waitForStarted())
 
91
    {
 
92
        throw runtime_error("DavServer::DavServer(): wait for server start failed");
 
93
    }
 
94
    wait_for_listen(port);
 
95
 
 
96
    base_url_.setUrl(QStringLiteral("http://127.0.0.1:%1/").arg(port),
 
97
                     QUrl::StrictMode);
 
98
}
 
99
 
 
100
DavEnvironment::~DavEnvironment()
 
101
{
 
102
    server_.terminate();
 
103
    if (!server_.waitForFinished())
 
104
    {
 
105
        qCritical() << "Failed to terminate DAV server";
 
106
    }
 
107
}
 
108
 
 
109
QUrl const& DavEnvironment::base_url() const
 
110
{
 
111
    return base_url_;
 
112
}