File: network.c

package info (click to toggle)
rauc 1.15-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,336 kB
  • sloc: ansic: 36,989; python: 3,354; sh: 1,391; xml: 53; makefile: 41
file content (81 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (2)
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
#include <locale.h>
#include <gio/gio.h>
#include <glib.h>
#include <glib/gstdio.h>

#include <context.h>
#include <utils.h>
#include "network.h"

typedef struct {
	gchar *tmpdir;
} NetworkFixture;

static void network_fixture_set_up(NetworkFixture *fixture,
		gconstpointer user_data)
{
	fixture->tmpdir = g_dir_make_tmp("rauc-XXXXXX", NULL);
	g_assert_nonnull(fixture->tmpdir);
	g_test_message("network tmpdir: %s\n", fixture->tmpdir);
}

static void network_fixture_tear_down(NetworkFixture *fixture,
		gconstpointer user_data)
{
	g_assert_true(rm_tree(fixture->tmpdir, NULL));
	g_free(fixture->tmpdir);
}

static void test_download_file(NetworkFixture *fixture,
		gconstpointer user_data)
{
	g_autofree const gchar *target = NULL;
	GError *ierror = NULL;
	gboolean res;

	target = g_build_filename(fixture->tmpdir, "target", NULL);

	/* basic download (no size limit) */
	res = download_file(target, "https://rauc.io/", 0, &ierror);
	g_assert_no_error(ierror);
	g_assert_true(res);
	g_assert_cmpint(g_unlink(target), ==, 0);

	/* download with large limit */
	res = download_file(target, "https://rauc.io/", 1048576, &ierror);
	g_assert_no_error(ierror);
	g_assert_true(res);
	g_assert_cmpint(g_unlink(target), ==, 0);

	/* abort download for too large files */
	res = download_file(target, "https://rauc.io/", 1024, &ierror);
	g_assert_error(ierror, G_IO_ERROR, G_IO_ERROR_FAILED);
	g_assert_false(res);
	g_clear_error(&ierror);
	g_assert_cmpint(g_unlink(target), ==, 0);

	/* invalid host name */
	res = download_file(target, "https://error.rauc.io/", 1024, &ierror);
	g_assert_error(ierror, G_IO_ERROR, G_IO_ERROR_FAILED);
	g_assert_false(res);
	g_clear_error(&ierror);
	g_assert_cmpint(g_unlink(target), ==, 0);
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL, "C");

	g_assert(g_setenv("GIO_USE_VFS", "local", TRUE));

	r_context_conf()->configpath = g_strdup("test/test.conf");
	r_context();

	g_test_init(&argc, &argv, NULL);

	g_test_add("/network/download_file", NetworkFixture, NULL,
			network_fixture_set_up, test_download_file,
			network_fixture_tear_down);

	return g_test_run();
}