download.c 6.22 KB
Newer Older
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
/*
 * Copyright 2018, CZ.NIC z.s.p.o. (http://www.nic.cz/)
 *
 * This file is part of the turris updater.
 *
 * Updater is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * Updater 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Updater.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "ctest.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../src/lib/download.h"
#include "../src/lib/util.h"

#define HTTP_URL "http://applications-test.turris.cz"
#define HTTP_SMALL ( HTTP_URL "/li.txt" )
#define HTTP_BIG ( HTTP_URL "/lorem_ipsum.txt" )
#define SMALL_CONTENT "lorem ipsum\n"
#define SMALL_SIZE 12

START_TEST(downloader_empty) {
	struct downloader *d = downloader_new(1);
	ck_assert(downloader_run(d) == NULL);
	downloader_free(d);
}
END_TEST

// Test simple download from http with redirect to https and Let's encrypt certificate
START_TEST(simple_download) {
	struct downloader *d = downloader_new(1);
	ck_assert(downloader_run(d) == NULL);
	struct download_opts opts;
	download_opts_def(&opts);

	struct download_i *inst = download_data(d, HTTP_SMALL, &opts);

	ck_assert(downloader_run(d) == NULL);

	ck_assert_uint_eq(SMALL_SIZE, inst->out.buff->size);
	ck_assert_str_eq(SMALL_CONTENT, (char *)inst->out.buff->data);

	downloader_free(d);
}
END_TEST

// Test download to file. Otherwise it's same test as in case of simple_download.
START_TEST(file_download) {
	struct downloader *d = downloader_new(1);
	ck_assert(downloader_run(d) == NULL);
	struct download_opts opts;
	download_opts_def(&opts);

	char *tmpdir = getenv("TMPDIR");
	if (!tmpdir)
		tmpdir = "/tmp";
	char *file = aprintf("%s/updater-download.txt", tmpdir);

	download_file(d, HTTP_SMALL, file, &opts);

	ck_assert(downloader_run(d) == NULL);

	char *str = readfile(file);
	ck_assert(str);
	ck_assert_uint_eq(SMALL_SIZE, strlen(str));
	ck_assert_str_eq(SMALL_CONTENT, str);
	free(str);

	unlink(file);

	downloader_free(d);
}
END_TEST

// Test that we can have multiple downloads and that all are downloaded
// Half of them are small file and half of them are bigger ones
// This test requires min. 20MB of memory.
START_TEST(multiple_downloads) {
	struct downloader *d = downloader_new(4);
	ck_assert(downloader_run(d) == NULL);
	struct download_opts opts;
	download_opts_def(&opts);

	const size_t cnt = 32;
	struct download_i *insts[cnt];
	for (size_t i = 0; i < cnt; i++) {
		if (i % 2)
			insts[i] = download_data(d, HTTP_SMALL, &opts);
		else
			insts[i] = download_data(d, HTTP_BIG, &opts);
	}

	ck_assert(downloader_run(d) == NULL);

	const char *s_dir = getenv("S");
	if (!s_dir)
		s_dir = ".";
	char *lorem_ipsum_file = aprintf("%s/tests/data/lorem_ipsum.txt", s_dir);
	char *big_content = readfile(lorem_ipsum_file);
	size_t big_size = strlen(big_content);

	for (size_t i = 0; i < cnt; i++) {
		if (i % 2) {
			ck_assert_uint_eq(SMALL_SIZE, insts[i]->out.buff->size);
			ck_assert_str_eq(SMALL_CONTENT, (char *)insts[i]->out.buff->data);
		} else {
			ck_assert_uint_eq(big_size, insts[i]->out.buff->size);
			ck_assert_str_eq(big_content, (char *)insts[i]->out.buff->data);
		}
	}

	free(big_content);
	downloader_free(d);
}
END_TEST

// Test failure if we access non-existent url
START_TEST(invalid) {
	struct downloader *d = downloader_new(1);
	ck_assert(downloader_run(d) == NULL);
	struct download_opts opts;
	download_opts_def(&opts);

	struct download_i *inst = download_data(d, HTTP_URL "/invalid", &opts);

	ck_assert_ptr_eq(downloader_run(d), inst);

	downloader_free(d);
}
END_TEST

// Test that even if one of download fail that all other will be downloaded
START_TEST(invalid_continue) {
	struct downloader *d = downloader_new(4);
	ck_assert(downloader_run(d) == NULL);
	struct download_opts opts;
	download_opts_def(&opts);

	const size_t cnt = 3;
	struct download_i *insts[cnt];
	for (size_t i = 0; i < cnt; i++)
		insts[i] = download_data(d, HTTP_SMALL, &opts);
	struct download_i *fail_inst = download_data(d, HTTP_URL "/invalid", &opts);

	ck_assert_ptr_eq(downloader_run(d), fail_inst);
	ck_assert(downloader_run(d) == NULL);

	for (size_t i = 0; i < cnt; i++) {
		ck_assert_uint_eq(SMALL_SIZE, insts[i]->out.buff->size);
		ck_assert_str_eq(SMALL_CONTENT, (char *)insts[i]->out.buff->data);
	}

	downloader_free(d);
}
END_TEST

// Test certification pinning
START_TEST(cert_pinning) {
	struct downloader *d = downloader_new(1);
	ck_assert(downloader_run(d) == NULL);
	struct download_opts opts;
	download_opts_def(&opts);

	const char *s_dir = getenv("S");
	if (!s_dir)
		s_dir = ".";
	opts.cacert_file = aprintf("%s/tests/data/lets_encrypt_roots.pem", s_dir);
	opts.capath = "/dev/null";

	struct download_i *inst = download_data(d, HTTP_SMALL, &opts);

	ck_assert(downloader_run(d) == NULL);

	ck_assert_uint_eq(SMALL_SIZE, inst->out.buff->size);
	ck_assert_str_eq(SMALL_CONTENT, (char *)inst->out.buff->data);

	downloader_free(d);
}
END_TEST

// Test failure if we try invalid certificate
START_TEST(cert_invalid) {
	struct downloader *d = downloader_new(1);
	ck_assert(downloader_run(d) == NULL);
	struct download_opts opts;
	download_opts_def(&opts);


	const char *s_dir = getenv("S");
	if (!s_dir)
		s_dir = ".";
	opts.cacert_file = aprintf("%s/tests/data/opentrust_ca_g1.pem", s_dir);
	opts.capath = "/dev/null";

	struct download_i *inst = download_data(d, HTTP_SMALL, &opts);

	ck_assert_ptr_eq(downloader_run(d), inst);

	downloader_free(d);
}
END_TEST


Suite *gen_test_suite(void) {
	Suite *result = suite_create("Download");
	TCase *down = tcase_create("download");
	tcase_set_timeout(down, 30);
	tcase_add_test(down, downloader_empty);
	tcase_add_test(down, simple_download);
	tcase_add_test(down, multiple_downloads);
	tcase_add_test(down, file_download);
	tcase_add_test(down, invalid);
	tcase_add_test(down, invalid_continue);
	tcase_add_test(down, cert_pinning);
	tcase_add_test(down, cert_invalid);
	suite_add_tcase(result, down);
	return result;
}