2 * unit test helper for crypt_wipe API call
4 * Copyright (C) 2022-2023 Milan Broz
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "libcryptsetup.h"
30 const char *test_file;
31 uint64_t test_offset, test_length, test_block;
33 crypt_wipe_pattern pattern;
35 static void usage(void)
37 fprintf(stderr, "Use:\tunit-wipe file/device zero|random|special offset length bsize [no-dio].\n");
40 static bool parse_u64(const char *arg, uint64_t *u64)
42 unsigned long long ull;
45 ull = strtoull(arg, &end, 10);
46 if (*end || !*arg || errno == ERANGE)
56 static bool parse_input_params(int argc, char **argv)
60 if (argc < 6 || argc > 7) {
65 if (stat(argv[1], &st)) {
66 fprintf(stderr, "File/device %s is missing?\n", argv[1]);
71 if (!strcmp(argv[2], "random"))
72 pattern = CRYPT_WIPE_RANDOM;
73 else if (!strcmp(argv[2], "zero"))
74 pattern = CRYPT_WIPE_ZERO;
75 else if (!strcmp(argv[2], "special"))
76 pattern = CRYPT_WIPE_SPECIAL;
78 fprintf(stderr, "Wrong pattern specification.\n");
82 if (!parse_u64(argv[3], &test_offset)) {
83 fprintf(stderr, "Wrong offset specification.\n");
87 if (!parse_u64(argv[4], &test_length)) {
88 fprintf(stderr, "Wrong length specification.\n");
92 if (!parse_u64(argv[5], &test_block)) {
93 fprintf(stderr, "Wrong block length specification.\n");
98 if (!strcmp(argv[6], "no-dio"))
99 flags = CRYPT_WIPE_NO_DIRECT_IO;
101 fprintf(stderr, "Wrong flags specification.\n");
109 int main(int argc, char **argv)
111 struct crypt_device *cd;
114 #ifndef NO_CRYPTSETUP_PATH
115 if (getenv("CRYPTSETUP_PATH")) {
116 printf("Cannot run this test with CRYPTSETUP_PATH set.\n");
121 if (!parse_input_params(argc, argv))
124 r = crypt_init(&cd, NULL);
126 fprintf(stderr, "Context init failure %i.\n", r);
130 r = crypt_wipe(cd, test_file, pattern, test_offset, test_length,
131 test_block, flags, NULL, NULL);
135 fprintf(stderr, "Failure %i\n", r);
137 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;