Linux Audio

Check our new training course

Loading...
  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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * Copyright (c) 2019 Peter Bigot Consulting, LLC
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <string.h>
#include <errno.h>
#include <sys/__assert.h>
#include <ztest.h>
#include "testfs_util.h"

static const char *path_vextend(struct testfs_path *pp,
				va_list ap)
{
	const char *ep = va_arg(ap, const char *);
	const char *endp = pp->path + sizeof(pp->path);

	while (ep) {
		size_t len = strlen(ep);
		char *eos = pp->eos;
		size_t rem = (endp - eos);

		if (strcmp(ep, "..") == 0) {
			char *sp = strrchr(pp->path, '/');

			if (sp == pp->path) {
				eos = sp + 1;
			} else {
				eos = sp;
			}
		} else if ((1 + len + 1) < rem) { /* /, ep, EOS */
			*eos = '/';
			++eos;
			memcpy(eos, ep, len);
			eos += len;
		} else {
			break;
		}
		*eos = 0;
		pp->eos = eos;

		ep = va_arg(ap, const char *);
	}
	return pp->path;
}

const char *testfs_path_init(struct testfs_path *pp,
			     const struct fs_mount_t *mp,
			     ...)
{
	va_list ap;

	if (mp == NULL) {
		pp->path[0] = '/';
		pp->eos = pp->path + 1;
	} else {
		size_t len = strlen(mp->mnt_point);

		__ASSERT('/' == mp->mnt_point[0], "relative mount point");
		if ((len + 1) >= sizeof(pp->path)) {
			len = sizeof(pp->path) - 1;
		}
		strncpy(pp->path, mp->mnt_point, len);
		pp->eos = pp->path + len;
	}
	*pp->eos = '\0';

	va_start(ap, mp);
	path_vextend(pp, ap);
	va_end(ap);
	return pp->path;
}

const char *testfs_path_extend(struct testfs_path *pp,
			       ...)
{
	va_list ap;

	va_start(ap, pp);
	path_vextend(pp, ap);
	va_end(ap);

	return pp->path;
}

int testfs_write_constant(struct fs_file_t *fp,
			  u8_t value,
			  unsigned int len)
{
	u8_t buffer[TESTFS_BUFFER_SIZE];
	unsigned int rem = len;

	memset(buffer, value, sizeof(buffer));

	while (rem > 0) {
		unsigned int count = sizeof(buffer);

		if (count > rem) {
			count = rem;
		}

		ssize_t rc = fs_write(fp, buffer, count);

		if (rc < 0) {
			return rc;
		}

		rem -= count;
	}

	return len;
}

int testfs_verify_constant(struct fs_file_t *fp,
			   u8_t value,
			   unsigned int len)
{
	u8_t buffer[TESTFS_BUFFER_SIZE];
	unsigned int match = 0;

	while (len > 0) {
		unsigned int count = sizeof(buffer);

		if (count > len) {
			count = len;
		}

		int rc = fs_read(fp, buffer, count);

		if (rc < 0) {
			return rc;
		}

		if (rc > count) {
			return -EIO;
		}

		for (unsigned int i = 0; i < rc; ++i) {
			if (value != buffer[i]) {
				break;
			}
			++match;
		}

		if (rc < count) {
			break;
		}

		len -= count;
	}
	return match;
}

int testfs_write_incrementing(struct fs_file_t *fp,
			      u8_t value,
			      unsigned int len)
{
	u8_t buffer[TESTFS_BUFFER_SIZE];
	unsigned int rem = len;

	while (rem > 0) {
		unsigned int count = sizeof(buffer);

		if (count > rem) {
			count = rem;
		}

		for (unsigned int i = 0; i < count; ++i) {
			buffer[i] = value++;
		}

		ssize_t rc = fs_write(fp, buffer, count);

		if (rc < 0) {
			return rc;
		}

		rem -= count;
	}

	return len;
}

int testfs_verify_incrementing(struct fs_file_t *fp,
			       u8_t value,
			       unsigned int len)
{
	u8_t buffer[TESTFS_BUFFER_SIZE];
	unsigned int match = 0;

	while (len > 0) {
		unsigned int count = sizeof(buffer);

		if (count > len) {
			count = len;
		}

		int rc = fs_read(fp, buffer, count);

		if (rc < 0) {
			return rc;
		}

		if (rc > count) {
			return -EIO;
		}

		for (unsigned int i = 0; i < rc; ++i) {
			if (value++ != buffer[i]) {
				break;
			}
			++match;
		}

		if (rc < count) {
			break;
		}

		len -= count;
	}
	return match;
}

int testfs_build(struct testfs_path *root,
		 const struct testfs_bcmd *cp)
{
	int rc;

	while (!TESTFS_BCMD_IS_END(cp)) {

		if (TESTFS_BCMD_IS_FILE(cp)) {
			struct fs_file_t file;

			rc = fs_open(&file,
				     testfs_path_extend(root,
							cp->name,
							TESTFS_PATH_END));
			TC_PRINT("create at %s with %u from 0x%02x: %d\n",
				 root->path, cp->size, cp->value, rc);
			if (rc == 0) {
				rc = testfs_write_incrementing(&file, cp->value, cp->size);
				(void)fs_close(&file);
			}
			testfs_path_extend(root, "..", TESTFS_PATH_END);

			if (rc < 0) {
				TC_PRINT("FAILED create/write %s: %d\n",
					 root->path, rc);
				return rc;
			}

		} else if (TESTFS_BCMD_IS_ENTER_DIR(cp)) {
			testfs_path_extend(root,
					   cp->name,
					   TESTFS_PATH_END);
			rc = fs_mkdir(root->path);
			TC_PRINT("mkdir %s: %d\n", root->path, rc);
			if (rc < 0) {
				return rc;
			}
		} else if (TESTFS_BCMD_IS_EXIT_DIR(cp)) {
			TC_PRINT("exit directory %s\n", root->path);
			testfs_path_extend(root, "..", TESTFS_PATH_END);
		} else {
			TC_PRINT("ERROR: unexpected build command");
			return -EINVAL;
		}
		++cp;
	}
	return 0;
}

/* Check the found entry against a probably match.  Recurse into
 * matched directories.
 *
 * Sets the matched field of *cp if all tests pass.
 *
 * Return a negative error, or a nonnegative count of foreign
 * files.
 */
static int check_layout_entry(struct testfs_path *pp,
			      const struct fs_dirent *statp,
			      struct testfs_bcmd *cp,
			      struct testfs_bcmd *ecp)
{
	int rc = 0;
	unsigned int foreign = 0;

	/* Create the full path */
	testfs_path_extend(pp, statp->name,
			   TESTFS_PATH_END);

	/* Also check file content */
	if (statp->type == FS_DIR_ENTRY_FILE) {
		struct fs_file_t file;

		rc = fs_open(&file, pp->path);
		if (rc < 0) {
			TC_PRINT("%s: content check open failed: %d\n",
				 pp->path, rc);
			rc = -ENOENT;
			goto out;
		}
		rc = testfs_verify_incrementing(&file, cp->value, cp->size);
		if (rc != cp->size) {
			TC_PRINT("%s: content check failed: %d\n",
				 pp->path, rc);
			if (rc >= 0) {
				rc = -EIO;
			}
			goto out;
		}
		rc = fs_close(&file);
		if (rc != 0) {
			TC_PRINT("%s: content check close failed: %d\n",
				 pp->path, rc);
		}
	} else if (statp->type == FS_DIR_ENTRY_DIR) {
		rc = testfs_bcmd_verify_layout(pp,
					       cp + 1,
					       testfs_bcmd_exitdir(cp, ecp));
		if (rc >= 0) {
			foreign = rc;
		}
	}

out:
	testfs_path_extend(pp, "..",
			   TESTFS_PATH_END);

	if (rc >= 0) {
		cp->matched = true;
		rc = foreign;
	}

	return rc;
}

int testfs_bcmd_verify_layout(struct testfs_path *pp,
			      struct testfs_bcmd *scp,
			      struct testfs_bcmd *ecp)
{
	struct fs_dir_t dir;
	unsigned int count = 0;
	unsigned int foreign = 0;
	struct testfs_bcmd *cp = scp;

	while (cp < ecp) {
		cp->matched = false;
		++cp;
	}

	int rc = fs_opendir(&dir, pp->path);

	if (rc != 0) {
		TC_PRINT("%s: opendir failed: %d\n", pp->path, rc);
		if (rc > 0) {
			rc = -EIO;
		}
		return rc;
	}

	TC_PRINT("check %s for %zu entries\n", pp->path, ecp - scp);
	while (rc >= 0) {
		struct fs_dirent stat;

		rc = fs_readdir(&dir, &stat);
		if (rc != 0) {
			TC_PRINT("readdir failed: %d", rc);
			rc = -EIO;
			break;
		}

		if (stat.name[0] == '\0') {
			break;
		}

		++count;

		cp = testfs_bcmd_find(&stat, scp, ecp);

		bool dotdir = ((stat.type == FS_DIR_ENTRY_DIR)
			       && ((strcmp(stat.name, ".") == 0)
				   || (strcmp(stat.name, "..") == 0)));

		TC_PRINT("%s %s%s%s %u\n", pp->path,
			 stat.name,
			 (stat.type == FS_DIR_ENTRY_FILE) ? "" : "/",
			 dotdir ? " SYNTHESIZED"
			 : (cp == NULL) ? " FOREIGN"
			 : "",
			 stat.size);

		if (dotdir) {
			zassert_true(false,
				     "special directories observed");
		} else if (cp != NULL) {
			rc = check_layout_entry(pp, &stat, cp, ecp);
			if (rc > 0) {
				foreign += rc;
			}
		} else {
			foreign += 1;
		}
	}
	TC_PRINT("%s found %u entries, %u foreign\n", pp->path, count, foreign);

	int rc2 = fs_closedir(&dir);

	if (rc2 != 0) {
		TC_PRINT("%s: closedir failed: %d\n",
			 pp->path, rc2);
		if (rc >= 0) {
			rc = (rc2 >= 0) ? -EIO : rc2;
		}
	}

	if (rc >= 0) {
		rc = foreign;
	}

	return rc;
}

struct testfs_bcmd *testfs_bcmd_exitdir(struct testfs_bcmd *cp,
					struct testfs_bcmd *ecp)
{
	unsigned int level = 1;

	/* Skip to the paired EXIT_DIR */
	while ((level > 0) && (++cp < ecp)) {
		if (TESTFS_BCMD_IS_ENTER_DIR(cp)) {
			++level;
		} else if (TESTFS_BCMD_IS_EXIT_DIR(cp)) {
			--level;
		}
	}
	return cp;
}

struct testfs_bcmd *testfs_bcmd_find(struct fs_dirent *statp,
				     struct testfs_bcmd *scp,
				     struct testfs_bcmd *ecp)
{
	struct testfs_bcmd *cp = scp;

	while (cp < ecp) {
		if ((cp->type == statp->type)
		    && (cp->name != NULL)
		    && (strcmp(cp->name, statp->name) == 0)
		    && (cp->size == statp->size)) {
			return cp;
		}

		if (TESTFS_BCMD_IS_ENTER_DIR(cp)) {
			cp = testfs_bcmd_exitdir(cp, ecp);
		}
		++cp;
	}

	return NULL;
}