File: tests-net.cpp

package info (click to toggle)
appstream-generator 0.10.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,344 kB
  • sloc: cpp: 42,038; python: 292; xml: 259; sh: 223; makefile: 20
file content (247 lines) | stat: -rw-r--r-- 7,399 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (C) 2019-2025 Matthias Klumpp <matthias@tenstral.net>
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>

#include <fstream>
#include <filesystem>
#include <optional>
#include <cstdlib>

#include "downloader.h"
#include "utils.h"
#include "logging.h"

using namespace ASGenerator;

static struct TestSetup {
    TestSetup()
    {
        setVerbose(true);
    }
} testSetup;

static bool canRunNetworkTests()
{
    // Check if network tests should be skipped
    const char *skipNetEnv = std::getenv("ASGEN_TESTS_NO_NET");
    if (skipNetEnv && std::string(skipNetEnv) != "no") {
        SKIP("Network dependent tests skipped (explicitly disabled via ASGEN_TESTS_NO_NET)");
        return false;
    }

    auto &downloader = Downloader::get();
    const std::string urlFirefoxDetectportal = "https://detectportal.firefox.com/";

    try {
        downloader.downloadText(urlFirefoxDetectportal);
    } catch (const DownloadException &e) {
        SKIP("Network dependent tests skipped (automatically, no network detected: " + std::string(e.what()) + ")");
        return false;
    }

    return true;
}

TEST_CASE("Downloader functionality", "[downloader][network]")
{
    if (!canRunNetworkTests())
        return;

    auto &downloader = Downloader::get();
    const std::string urlFirefoxDetectportal = "https://detectportal.firefox.com/";

    SECTION("File download functionality")
    {
        const std::string testFileName = "/tmp/asgen-test-ffdp-" + Utils::randomString(4);

        // Clean up file on exit
        auto cleanup = [&testFileName]() {
            if (std::filesystem::exists(testFileName)) {
                std::filesystem::remove(testFileName);
            }
        };

        try {
            downloader.downloadFile(urlFirefoxDetectportal, testFileName);

            // Verify file contents
            std::ifstream file(testFileName);
            REQUIRE(file.is_open());

            std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
            REQUIRE(content == "success\n");

            cleanup();
        } catch (const DownloadException &e) {
            cleanup();
            SKIP("Network test skipped: " + std::string(e.what()));
        }
    }

    SECTION("Download larger file")
    {
        const std::string testFileName = "/tmp/asgen-test-debian-" + Utils::randomString(4);

        auto cleanup = [&testFileName]() {
            if (std::filesystem::exists(testFileName)) {
                std::filesystem::remove(testFileName);
            }
        };

        try {
            downloader.downloadFile("https://debian.org", testFileName);

            // Verify file exists and has content
            REQUIRE(std::filesystem::exists(testFileName));
            REQUIRE(std::filesystem::file_size(testFileName) > 0);

            cleanup();
        } catch (const DownloadException &e) {
            cleanup();
            SKIP("Network test skipped: " + std::string(e.what()));
        }
    }

    SECTION("Error handling for non-existent file")
    {
        const std::string testFileName = "/tmp/asgen-dltest-" + Utils::randomString(4);

        auto cleanup = [&testFileName]() {
            if (std::filesystem::exists(testFileName)) {
                std::filesystem::remove(testFileName);
            }
        };

        try {
            REQUIRE_THROWS_AS(
                downloader.downloadFile("https://appstream.debian.org/nonexistent", testFileName, 2),
                DownloadException);
            cleanup();
        } catch (...) {
            cleanup();
            throw;
        }
    }

    SECTION("HTTP to HTTPS redirect handling")
    {
        const std::string testFileName = "/tmp/asgen-test-mozilla-" + Utils::randomString(4);

        auto cleanup = [&testFileName]() {
            if (std::filesystem::exists(testFileName)) {
                std::filesystem::remove(testFileName);
            }
        };

        try {
            // This should work as mozilla.org redirects HTTP to HTTPS
            downloader.downloadFile("http://mozilla.org", testFileName, 1);

            // Verify file exists and has content
            REQUIRE(std::filesystem::exists(testFileName));
            REQUIRE(std::filesystem::file_size(testFileName) > 0);

            cleanup();
        } catch (const DownloadException &e) {
            cleanup();
            SKIP("Network test skipped: " + std::string(e.what()));
        }
    }

    SECTION("Download to memory")
    {
        try {
            auto data = downloader.download(urlFirefoxDetectportal);

            std::string content(data.begin(), data.end());
            REQUIRE(content == "success\n");
        } catch (const DownloadException &e) {
            SKIP("Network test skipped: " + std::string(e.what()));
        }
    }

    SECTION("Download text lines")
    {
        try {
            auto lines = downloader.downloadTextLines(urlFirefoxDetectportal);

            REQUIRE(lines.size() == 1);
            REQUIRE(lines[0] == "success");
        } catch (const DownloadException &e) {
            SKIP("Network test skipped: " + std::string(e.what()));
        }
    }
}

TEST_CASE("Downloader edge cases", "[downloader]")
{
    if (!canRunNetworkTests())
        return;

    auto &downloader = Downloader::get();

    SECTION("Invalid URL handling")
    {
        REQUIRE_THROWS_AS(downloader.downloadText("not-a-url"), DownloadException);
    }

    SECTION("Empty URL handling")
    {
        REQUIRE_THROWS_AS(downloader.downloadText(""), DownloadException);
    }

    SECTION("Retry mechanism with zero retries")
    {
        REQUIRE_THROWS_AS(downloader.downloadText("https://nonexistent.example.invalid", 0), DownloadException);
    }
}

TEST_CASE("Downloader file skipping", "[downloader]")
{
    if (!canRunNetworkTests())
        return;
    auto &downloader = Downloader::get();

    SECTION("Skip download if file already exists")
    {
        const std::string testFileName = "/tmp/asgen-test-existing-" + Utils::randomString(4);

        // Create a file first
        {
            std::ofstream file(testFileName);
            file << "existing content\n";
        }

        auto cleanup = [&testFileName]() {
            if (std::filesystem::exists(testFileName)) {
                std::filesystem::remove(testFileName);
            }
        };

        try {
            // This should skip the download since file exists
            downloader.downloadFile("https://detectportal.firefox.com/", testFileName);

            // Verify the original content is still there
            std::ifstream file(testFileName);
            std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
            REQUIRE(content == "existing content\n");

            cleanup();
        } catch (const DownloadException &e) {
            cleanup();
            // If network is not available, the test should still pass
            // since the file exists and download should be skipped
            std::ifstream file(testFileName);
            if (file.is_open()) {
                std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
                REQUIRE(content == "existing content\n");
            }
        }
    }
}