File: testdata.h

package info (click to toggle)
librecast 0.11.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,760 kB
  • sloc: ansic: 31,144; asm: 28,570; sh: 3,164; makefile: 713; python: 70
file content (89 lines) | stat: -rw-r--r-- 2,980 bytes parent folder | download | duplicates (3)
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
/* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only */
/* Copyright (c) 2023 Brett Sheffield <bacs@librecast.net> */

#ifndef _TESTDATA_H
#define _TESTDATA_H 1

#include "test.h"
#include <fcntl.h>
#include <librecast/types.h>
#include <librecast/crypto.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>

enum {
	TEST_TMP = 1,
	TEST_RND = 2,
	TEST_OWN = 4,
	TEST_MOD = 8,
};

/* write sz random bytes to ptr */
int test_random_bytes(void *ptr, size_t sz);

/* create test file with of sz bytes at path filename
 *
 * flags:
 *   TEST_TMP - if set, a temp file is created using filename as the template.
 *   TEST_RND - fill the file with sz random bytes
 *
 * if sz > 0 but TEST_RND is not set, a sparse file is created.
 */
int test_data_file(char *filename, size_t sz, int flags);

/* verify filename is a regular file and is correct size
 * return 0 if correct, -1 on error */
int test_data_size(char *filename, size_t sz);

/* hash file
 *   filename - file to hash
 *   hash - buffer to return hash in
 *   hashlen - length of hash buffer
 *   buf - either NULL or a pointer to a preallocated buffer for file I/O
 *   buflen - length of buf in bytes
 * returns 0 on success, -1 on error, setting errno
 */
int test_hash_file(const char *filename, unsigned char *hash, size_t hashlen, char *buf, size_t buflen);

/* hash files and confirm data matches, return 0 on match, nonzero fail */
int test_file_match(const char *file1, const char *file2);

/* write len random bytes to file, starting at offset off
 * returns 0 on success, -1 on error, setting errno */
int test_file_scratch(const char *filename, long offset, size_t len);

/* extend file sparsely to sz bytes */
int test_sparsify_fd(int fd, size_t sz);

/* randomize file metadata
 * path must be an existing directory
 * flags can be:
 * - TEST_OWN - randomize uid and gid
 * - TEST_MOD - randomize permissions */
int test_random_meta(const char *path, int flags);

/* create a directory tree of files and directories with directories nested
 * depth levels deep, up to max_files files and max_dirs directories in each
 * level. Fill files with up to max_filesz random bytes.
 * path must be an existing directory
 * flags can be:
 * - TEST_OWN - randomize uid and gid
 * - TEST_MOD - randomize permissions */
int test_createtesttree(const char *path, off_t max_filesz, int max_files, int max_dirs, int depth,
		int flags);

/* create a pair of source and destination directories using the template
 * of the form 0000-0000.{src,dst}.tmp.XXXXXX based on the test name.
 * testprog should be passed as basename(argv[0])
 * Both src and dst need to be free()ed after use. */
int test_createtestdirs(char *testprog, char **src, char **dst);

/* create a single test directory as above. srcdst must be "src" or "dst" */
int test_createtestdir(char *testprog, char **dir, const char *srcdst);

#endif /* _TESTDATA_H */