advio.cpp 6.08 KB
Newer Older
Steffen Vogel's avatar
Steffen Vogel committed
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
/** Unit tests for advio
 *
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
 * @copyright 2017-2018, Institute for Automation of Complex Power Systems, EONERC
 * @license GNU General Public License (version 3)
 *
 * VILLAScommon
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#include <unistd.h>

#include <criterion/criterion.h>

#include <villas/utils.h>
#include <villas/advio.h>

/** 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"

TestSuite(advio,
35
	.description = "Advanced file IO"
Steffen Vogel's avatar
Steffen Vogel committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
);

Test(advio, islocal)
{
	int ret;

	ret = aislocal("/var/log/villas/dta.dat");
	cr_assert_eq(ret, 1);

	ret = aislocal("http://www.google.de");
	cr_assert_eq(ret, 0);

	ret = aislocal("torrent://www.google.de");
	cr_assert_eq(ret, -1);
}

Test(advio, local)
{
	AFILE *af;
	int ret;
Steffen Vogel's avatar
Steffen Vogel committed
56
	char *buf = nullptr;
Steffen Vogel's avatar
Steffen Vogel committed
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
	size_t buflen = 0;

	/* We open this file and check the first line */
	af = afopen(__FILE__, "r");
	cr_assert(af, "Failed to open local file");

	ret = getline(&buf, &buflen, af->file);
	cr_assert_gt(ret, 1);
	cr_assert_str_eq(buf, "/** Unit tests for advio\n");
}

Test(advio, download)
{
	AFILE *af;
	int ret;
	size_t len;
	char buffer[65];
	char expect[65] = "ook4iekohC2Teegoghu6ayoo1OThooregheebaet8Zod1angah0che7quai4ID7A";

	af = afopen(BASE_URI "/download" , "r");
	cr_assert(af, "Failed to download file");

	len = afread(buffer, 1, sizeof(buffer), af);
	cr_assert_gt(len, 0, "len=%zu, feof=%u", len, afeof(af));

Steffen Vogel's avatar
Steffen Vogel committed
82
	cr_assert_arr_eq(buffer, expect, 64);
Steffen Vogel's avatar
Steffen Vogel committed
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

	ret = afclose(af);
	cr_assert_eq(ret, 0, "Failed to close file");
}

Test(advio, download_large)
{
	AFILE *af;
	int ret;

	af = afopen(BASE_URI "/download-large" , "r");
	cr_assert(af, "Failed to download file");

	char line[4096];

	char *f;

	f = afgets(line, 4096, af);
	cr_assert_not_null(f);

	/* 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");

	while(afgets(line, 4096, af));

	/* Check last line */
	cr_assert_str_eq(line, "1497710478.862332239(99999)	0.752074	-0.006283	1.000000	0.996000\n");

	ret = afclose(af);
	cr_assert_eq(ret, 0, "Failed to close file");
}

Test(advio, resume)
{
	int ret;
	char *retp;
	AFILE *af1, *af2;
	char *fn, dir[] = "/tmp/temp.XXXXXX";
	char line1[32];
Steffen Vogel's avatar
Steffen Vogel committed
122
	char *line2 = nullptr;
Steffen Vogel's avatar
Steffen Vogel committed
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
248
249
250
251
252
253
254
255
256
257
258
	size_t linelen = 0;

	retp = mkdtemp(dir);
	cr_assert_not_null(retp);

	ret = asprintf(&fn, "%s/file", dir);
	cr_assert_gt(ret, 0);

	af1 = afopen(fn, "w+");
	cr_assert_not_null(af1);

	/* We flush once the empty file in order to upload an empty file. */
	aupload(af1, 0);

	af2 = afopen(fn, "r");
	cr_assert_not_null(af2);

	for (int i = 0; i < 100; i++) {
		snprintf(line1, sizeof(line1), "This is line %d\n", i);

		afputs(line1, af1);
		aupload(af1, 1);

		adownload(af2, 1);

		ret = agetline(&line2, &linelen, af2);
		cr_assert_gt(ret, 0);

		cr_assert_str_eq(line1, line2);
	}

	ret = afclose(af1);
	cr_assert_eq(ret, 0);

	ret = afclose(af2);
	cr_assert_eq(ret, 0);

	ret = unlink(fn);
	cr_assert_eq(ret, 0);

	ret = rmdir(dir);
	cr_assert_eq(ret, 0);

	free(line2);
}

Test(advio, upload)
{
	AFILE *af;
	int ret;
	size_t len;

	char upload[64];
	char buffer[64];

	/* Get some random data to upload */
	len = read_random(upload, sizeof(upload));
	cr_assert_eq(len, sizeof(upload));

	/* Open file for writing */
	af = afopen(BASE_URI "/upload", "w+");
	cr_assert(af, "Failed to download file");

	len = afwrite(upload, 1, sizeof(upload), af);
	cr_assert_eq(len, sizeof(upload));

	ret = afclose(af);
	cr_assert_eq(ret, 0, "Failed to close/upload file");

	/* Open for reading and comparison */
	af = afopen(BASE_URI "/upload", "r");
	cr_assert(af, "Failed to download file");

	len = afread(buffer, 1, sizeof(upload), af);
	cr_assert_eq(len, sizeof(upload));

	cr_assert_arr_eq(buffer, upload, len);

	ret = afclose(af);
	cr_assert(ret == 0, "Failed to close file");
}

Test(advio, append)
{
	AFILE *af;
	int ret;
	size_t len;

	char append1[65] = "xa5gieTohlei9iu1uVaePae6Iboh3eeheeme5iejue5sheshae4uzisha9Faesei";
	char append2[65] = "bitheeRae7igee2miepahJaefoGad1Ooxeif0Mooch4eojoumueYahn4ohc9poo2";
	char expect[129] = "xa5gieTohlei9iu1uVaePae6Iboh3eeheeme5iejue5sheshae4uzisha9FaeseibitheeRae7igee2miepahJaefoGad1Ooxeif0Mooch4eojoumueYahn4ohc9poo2";
	char buffer[129];

	/* Open file for writing first chunk */
	af = afopen(BASE_URI "/append", "w+");
	cr_assert(af, "Failed to download file");

	/* The append file might already exist and be not empty from a previous run. */
	ret = ftruncate(afileno(af), 0);
	cr_assert_eq(ret, 0);

	char c;
	fseek(af->file, 0, SEEK_SET);
	if (af->file) {
	    while ((c = getc(af->file)) != EOF)
	        putchar(c);
	}

	len = afwrite(append1, 1, 64, af);
	cr_assert_eq(len, 64);

	ret = afclose(af);
	cr_assert_eq(ret, 0, "Failed to close/upload file");

	/* Open file for writing second chunk */
	af = afopen(BASE_URI "/append", "a");
	cr_assert(af, "Failed to download file");

	len = afwrite(append2, 1, 64, af);
	cr_assert_eq(len, 64);

	ret = afclose(af);
	cr_assert_eq(ret, 0, "Failed to close/upload file");

	/* Open for reading and comparison */
	af = afopen(BASE_URI "/append", "r");
	cr_assert(af, "Failed to download file");

	len = afread(buffer, 1, sizeof(buffer), af);
	cr_assert_eq(len, 128);

	ret = afclose(af);
	cr_assert(ret == 0, "Failed to close file");

	cr_assert_arr_eq(buffer, expect, 128);
}