~unity-team/thumbnailer/devel

180.1.2 by Michi Henning
Added copyright test and missing copyright headers.
1
/*
2
 * Copyright (C) 2015 Canonical Ltd.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
189.1.1 by Michi Henning
Made license headers consistent.
5
 * it under the terms of the GNU General Public License version 3 as
180.1.2 by Michi Henning
Added copyright test and missing copyright headers.
6
 * published by the Free Software Foundation.
7
 *
8
 * This program 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
189.1.1 by Michi Henning
Made license headers consistent.
11
 * GNU General Public License for more details.
180.1.2 by Michi Henning
Added copyright test and missing copyright headers.
12
 *
189.1.1 by Michi Henning
Made license headers consistent.
13
 * You should have received a copy of the GNU General Public License
180.1.2 by Michi Henning
Added copyright test and missing copyright headers.
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 *
16
 * Authored by: James Henstridge <james.henstridge@canonical.com>
17
 */
18
176.2.1 by James Henstridge
Add a small utils static library to share code between test suites.
19
#include "artserver.h"
20
#include <testsetup.h>
306.1.2 by Michi Henning
Stop tests from failing when test machine has no internet connection.
21
#include <internal/env_vars.h>
275.1.1 by James Henstridge
Add support for blocking access to the art server.
22
#include <internal/raii.h>
176.2.1 by James Henstridge
Add a small utils static library to share code between test suites.
23
24
#include <QDebug>
169.5.1 by Michi Henning
Added trace message handler for qDebug with locking and time stamp.
25
176.2.1 by James Henstridge
Add a small utils static library to share code between test suites.
26
#include <stdexcept>
27
#include <stdlib.h>
275.1.1 by James Henstridge
Add support for blocking access to the art server.
28
#include <sys/types.h>
29
#include <sys/socket.h>
30
#include <netinet/in.h>
176.2.1 by James Henstridge
Add a small utils static library to share code between test suites.
31
32
ArtServer::ArtServer()
275.1.1 by James Henstridge
Add support for blocking access to the art server.
33
    : socket_(-1, unity::thumbnailer::internal::do_close)
176.2.1 by James Henstridge
Add a small utils static library to share code between test suites.
34
{
35
    server_.setStandardInputFile(QProcess::nullDevice());
36
    server_.setProcessChannelMode(QProcess::ForwardedErrorChannel);
37
    server_.start(FAKE_ART_SERVER, QStringList());
38
    if (!server_.waitForStarted())
39
    {
40
        throw std::runtime_error("ArtServer::ArtServer(): wait for server start failed");
41
    }
42
    if (!server_.waitForReadyRead())
43
    {
44
        throw std::runtime_error("ArtServer::ArtServer(): wait for read from server failed");
45
    }
46
    auto port = QString::fromUtf8(server_.readAllStandardOutput()).trimmed();
306.1.2 by Michi Henning
Stop tests from failing when test machine has no internet connection.
47
    server_url_ = "http://127.0.0.1:" + port.toStdString();
275.1.1 by James Henstridge
Add support for blocking access to the art server.
48
49
    // Create a bound TCP socket with no listen queue.  Attempts to
50
    // connect to this port can not succeed.  And as long as we hold
51
    // the socket open, no other socket can reuse the port number.
52
    socket_.reset(socket(AF_INET, SOCK_STREAM, 0));
53
    if (socket_.get() < 0)
54
    {
55
        throw std::runtime_error("ArtServer::ArtServer(): Could not create socket");
56
    }
57
    struct sockaddr_in addr;
58
    memset(&addr, 0, sizeof(struct sockaddr_in));
59
    if (bind(socket_.get(), reinterpret_cast<struct sockaddr*>(&addr),
60
             sizeof(struct sockaddr_in)) < 0)
61
    {
62
        throw std::runtime_error("ArtServer::ArtServer(): Could not bind socket");
63
    }
64
    socklen_t length = sizeof(struct sockaddr_in);
65
    if (getsockname(socket_.get(), reinterpret_cast<struct sockaddr*>(&addr),
66
                    &length) < 0 || length > sizeof(struct sockaddr_in))
67
    {
68
        throw std::runtime_error("ArtServer::ArtServer(): Could not get socket name");
69
    }
306.1.2 by Michi Henning
Stop tests from failing when test machine has no internet connection.
70
    blocked_server_url_ = "http://127.0.0.1:" + std::to_string(addr.sin_port);
275.1.1 by James Henstridge
Add support for blocking access to the art server.
71
72
    update_env();
176.2.1 by James Henstridge
Add a small utils static library to share code between test suites.
73
}
74
75
ArtServer::~ArtServer()
76
{
77
    server_.terminate();
78
    if (!server_.waitForFinished())
79
    {
80
        qCritical() << "Failed to terminate fake art server";
81
    }
365.4.9 by Michi Henning
Consolidated env var access into EnvVars helper class. Snap path changes
82
    unsetenv(unity::thumbnailer::internal::EnvVars::UBUNTU_SERVER_URL);
176.2.1 by James Henstridge
Add a small utils static library to share code between test suites.
83
}
176.2.2 by James Henstridge
Port tests over to using central art server setup code.
84
306.1.2 by Michi Henning
Stop tests from failing when test machine has no internet connection.
85
std::string const& ArtServer::server_url() const
176.2.2 by James Henstridge
Port tests over to using central art server setup code.
86
{
275.1.1 by James Henstridge
Add support for blocking access to the art server.
87
    if (blocked_)
88
    {
306.1.2 by Michi Henning
Stop tests from failing when test machine has no internet connection.
89
        return blocked_server_url_;
275.1.1 by James Henstridge
Add support for blocking access to the art server.
90
    }
91
    else
92
    {
306.1.2 by Michi Henning
Stop tests from failing when test machine has no internet connection.
93
        return server_url_;
275.1.1 by James Henstridge
Add support for blocking access to the art server.
94
    }
95
}
96
97
void ArtServer::block_access()
98
{
99
    blocked_ = true;
100
    update_env();
101
}
102
103
void ArtServer::unblock_access()
104
{
105
    blocked_ = false;
106
    update_env();
107
}
108
109
void ArtServer::update_env()
110
{
365.4.9 by Michi Henning
Consolidated env var access into EnvVars helper class. Snap path changes
111
    setenv(unity::thumbnailer::internal::EnvVars::UBUNTU_SERVER_URL, server_url().c_str(), true);
176.2.2 by James Henstridge
Port tests over to using central art server setup code.
112
}