2017-03-03 20:20:13 -04:00
|
|
|
/** Unit tests for advio
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-03-03 20:20:13 -04:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <criterion/logging.h>
|
|
|
|
|
2017-02-12 14:36:44 -03:00
|
|
|
#include <villas/utils.h>
|
2017-01-19 23:12:52 -02:00
|
|
|
#include <villas/advio.h>
|
2017-06-17 18:50:38 +02:00
|
|
|
#include <villas/sample.h>
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/formats/villas_human.h>
|
2017-01-19 23:12:52 -02:00
|
|
|
|
2017-03-13 00:04:56 -03:00
|
|
|
/** This URI points to a Sciebo share which contains some test files.
|
|
|
|
* The Sciebo share is read/write accessible via WebDAV. */
|
|
|
|
#define BASE_URI "https://1Nrd46fZX8HbggT:badpass@rwth-aachen.sciebo.de/public.php/webdav/node/tests"
|
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
Test(advio, islocal)
|
|
|
|
{
|
|
|
|
int ret;
|
2017-09-04 14:30:07 +02:00
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
ret = aislocal("/var/log/villas/dta.dat");
|
|
|
|
cr_assert_eq(ret, 1);
|
2017-09-04 14:30:07 +02:00
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
ret = aislocal("http://www.google.de");
|
|
|
|
cr_assert_eq(ret, 0);
|
|
|
|
|
|
|
|
ret = aislocal("torrent://www.google.de");
|
|
|
|
cr_assert_eq(ret, -1);
|
|
|
|
}
|
|
|
|
|
2017-05-07 15:49:25 +02:00
|
|
|
Test(advio, local)
|
|
|
|
{
|
|
|
|
AFILE *af;
|
|
|
|
int ret;
|
2017-06-17 03:16:27 +02:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t buflen = 0;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 03:16:27 +02:00
|
|
|
/* We open this file and check the first line */
|
|
|
|
af = afopen(__FILE__, "r");
|
2017-05-07 15:49:25 +02:00
|
|
|
cr_assert(af, "Failed to open local file");
|
2017-06-17 03:16:27 +02:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
ret = getline(&buf, &buflen, af->file);
|
2017-06-17 03:16:27 +02:00
|
|
|
cr_assert_gt(ret, 1);
|
|
|
|
cr_assert_str_eq(buf, "/** Unit tests for advio\n");
|
2017-05-07 15:49:25 +02:00
|
|
|
}
|
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
Test(advio, download)
|
|
|
|
{
|
|
|
|
AFILE *af;
|
|
|
|
int ret;
|
2017-03-13 00:04:56 -03:00
|
|
|
size_t len;
|
|
|
|
char buffer[64];
|
2017-03-25 21:23:48 +01:00
|
|
|
char expect[64] = "ook4iekohC2Teegoghu6ayoo1OThooregheebaet8Zod1angah0che7quai4ID7A";
|
2017-01-19 23:12:52 -02:00
|
|
|
|
2017-03-15 00:51:24 -03:00
|
|
|
af = afopen(BASE_URI "/download" , "r");
|
2017-01-19 23:12:52 -02:00
|
|
|
cr_assert(af, "Failed to download file");
|
|
|
|
|
2017-03-13 00:04:56 -03:00
|
|
|
len = afread(buffer, 1, sizeof(buffer), af);
|
2017-03-25 21:23:48 +01:00
|
|
|
cr_assert_gt(len, 0, "len=%zu, feof=%u", len, afeof(af));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
cr_assert_arr_eq(buffer, expect, sizeof(expect));
|
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
ret = afclose(af);
|
2017-03-25 21:23:48 +01:00
|
|
|
cr_assert_eq(ret, 0, "Failed to close file");
|
2017-01-19 23:12:52 -02:00
|
|
|
}
|
|
|
|
|
2017-06-17 18:50:38 +02:00
|
|
|
Test(advio, download_large)
|
|
|
|
{
|
|
|
|
AFILE *af;
|
|
|
|
int ret, len = 16;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct sample *smp = (struct sample *) alloc(SAMPLE_LEN(len));
|
2017-06-17 18:50:38 +02:00
|
|
|
smp->capacity = len;
|
|
|
|
|
|
|
|
af = afopen(BASE_URI "/download-large" , "r");
|
|
|
|
cr_assert(af, "Failed to download file");
|
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
char line[4096];
|
|
|
|
|
2018-05-14 08:31:35 +02:00
|
|
|
char *f;
|
|
|
|
|
|
|
|
f = afgets(line, 4096, af);
|
|
|
|
cr_assert_not_null(f);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
/* Check first line */
|
|
|
|
cr_assert_str_eq(line, "# VILLASnode signal params: type=mixed, values=4, rate=1000.000000, limit=100000, amplitude=1.000000, freq=1.000000\n");
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
while(afgets(line, 4096, af));
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2018-05-12 13:56:12 +02:00
|
|
|
/* Check last line */
|
|
|
|
cr_assert_str_eq(line, "1497710478.862332239(99999) 0.752074 -0.006283 1.000000 0.996000\n");
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:50:38 +02:00
|
|
|
ret = afclose(af);
|
|
|
|
cr_assert_eq(ret, 0, "Failed to close file");
|
|
|
|
}
|
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
Test(advio, resume)
|
|
|
|
{
|
|
|
|
int ret;
|
2017-10-20 11:37:25 +02:00
|
|
|
char *retp;
|
2017-06-17 18:39:57 +02:00
|
|
|
AFILE *af1, *af2;
|
|
|
|
char *fn, dir[] = "/tmp/temp.XXXXXX";
|
|
|
|
char line1[32];
|
|
|
|
char *line2 = NULL;
|
|
|
|
size_t linelen = 0;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-10-20 11:37:25 +02:00
|
|
|
retp = mkdtemp(dir);
|
|
|
|
cr_assert_not_null(retp);
|
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
ret = asprintf(&fn, "%s/file", dir);
|
|
|
|
cr_assert_gt(ret, 0);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
af1 = afopen(fn, "w+");
|
|
|
|
cr_assert_not_null(af1);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
/* We flush once the empty file in order to upload an empty file. */
|
|
|
|
aupload(af1, 0);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
af2 = afopen(fn, "r");
|
|
|
|
cr_assert_not_null(af2);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
snprintf(line1, sizeof(line1), "This is line %d\n", i);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
afputs(line1, af1);
|
|
|
|
aupload(af1, 1);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
adownload(af2, 1);
|
2017-10-20 11:37:25 +02:00
|
|
|
|
|
|
|
ret = agetline(&line2, &linelen, af2);
|
|
|
|
cr_assert_gt(ret, 0);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
cr_assert_str_eq(line1, line2);
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
ret = afclose(af1);
|
|
|
|
cr_assert_eq(ret, 0);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
ret = afclose(af2);
|
|
|
|
cr_assert_eq(ret, 0);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
ret = unlink(fn);
|
|
|
|
cr_assert_eq(ret, 0);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
ret = rmdir(dir);
|
|
|
|
cr_assert_eq(ret, 0);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-06-17 18:39:57 +02:00
|
|
|
free(line2);
|
|
|
|
}
|
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
Test(advio, upload)
|
|
|
|
{
|
|
|
|
AFILE *af;
|
|
|
|
int ret;
|
2017-03-13 00:04:56 -03:00
|
|
|
size_t len;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
char upload[64];
|
|
|
|
char buffer[64];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
/* Get some random data to upload */
|
|
|
|
len = read_random(upload, sizeof(upload));
|
|
|
|
cr_assert_eq(len, sizeof(upload));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
/* Open file for writing */
|
2017-03-15 00:51:24 -03:00
|
|
|
af = afopen(BASE_URI "/upload", "w+");
|
2017-03-13 00:04:56 -03:00
|
|
|
cr_assert(af, "Failed to download file");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
len = afwrite(upload, 1, sizeof(upload), af);
|
|
|
|
cr_assert_eq(len, sizeof(upload));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-13 00:04:56 -03:00
|
|
|
ret = afclose(af);
|
|
|
|
cr_assert_eq(ret, 0, "Failed to close/upload file");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-13 00:04:56 -03:00
|
|
|
/* Open for reading and comparison */
|
2017-03-15 00:51:24 -03:00
|
|
|
af = afopen(BASE_URI "/upload", "r");
|
2017-03-13 00:04:56 -03:00
|
|
|
cr_assert(af, "Failed to download file");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
len = afread(buffer, 1, sizeof(upload), af);
|
|
|
|
cr_assert_eq(len, sizeof(upload));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
cr_assert_arr_eq(buffer, upload, len);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-13 00:04:56 -03:00
|
|
|
ret = afclose(af);
|
|
|
|
cr_assert(ret == 0, "Failed to close file");
|
|
|
|
}
|
|
|
|
|
|
|
|
Test(advio, append)
|
|
|
|
{
|
|
|
|
AFILE *af;
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
char append1[64] = "xa5gieTohlei9iu1uVaePae6Iboh3eeheeme5iejue5sheshae4uzisha9Faesei";
|
|
|
|
char append2[64] = "bitheeRae7igee2miepahJaefoGad1Ooxeif0Mooch4eojoumueYahn4ohc9poo2";
|
|
|
|
char expect[128] = "xa5gieTohlei9iu1uVaePae6Iboh3eeheeme5iejue5sheshae4uzisha9FaeseibitheeRae7igee2miepahJaefoGad1Ooxeif0Mooch4eojoumueYahn4ohc9poo2";
|
|
|
|
char buffer[128];
|
2017-03-13 00:04:56 -03:00
|
|
|
|
|
|
|
/* Open file for writing first chunk */
|
2017-03-15 00:51:24 -03:00
|
|
|
af = afopen(BASE_URI "/append", "w+");
|
2017-03-13 00:04:56 -03:00
|
|
|
cr_assert(af, "Failed to download file");
|
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
/* The append file might already exist and be not empty from a previous run. */
|
|
|
|
ret = ftruncate(afileno(af), 0);
|
|
|
|
cr_assert_eq(ret, 0);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
char c;
|
|
|
|
fseek(af->file, 0, SEEK_SET);
|
|
|
|
if (af->file) {
|
|
|
|
while ((c = getc(af->file)) != EOF)
|
|
|
|
putchar(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = afwrite(append1, 1, sizeof(append1), af);
|
|
|
|
cr_assert_eq(len, sizeof(append1));
|
2017-03-13 00:04:56 -03:00
|
|
|
|
|
|
|
ret = afclose(af);
|
|
|
|
cr_assert_eq(ret, 0, "Failed to close/upload file");
|
|
|
|
|
|
|
|
/* Open file for writing second chunk */
|
2017-03-15 00:51:24 -03:00
|
|
|
af = afopen(BASE_URI "/append", "a");
|
2017-01-19 23:12:52 -02:00
|
|
|
cr_assert(af, "Failed to download file");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
len = afwrite(append2, 1, sizeof(append2), af);
|
|
|
|
cr_assert_eq(len, sizeof(append2));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
ret = afclose(af);
|
|
|
|
cr_assert_eq(ret, 0, "Failed to close/upload file");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
/* Open for reading and comparison */
|
2017-03-15 00:51:24 -03:00
|
|
|
af = afopen(BASE_URI "/append", "r");
|
2017-01-19 23:12:52 -02:00
|
|
|
cr_assert(af, "Failed to download file");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
len = afread(buffer, 1, sizeof(buffer), af);
|
|
|
|
cr_assert_eq(len, sizeof(buffer));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-01-19 23:12:52 -02:00
|
|
|
ret = afclose(af);
|
|
|
|
cr_assert(ret == 0, "Failed to close file");
|
2017-03-13 00:04:56 -03:00
|
|
|
|
2017-03-25 21:23:48 +01:00
|
|
|
cr_assert_arr_eq(buffer, expect, sizeof(expect));
|
2017-08-05 21:02:09 +02:00
|
|
|
}
|