summaryrefslogtreecommitdiffstats
path: root/tests/unit-wipe.c
blob: d381a839bdff6c5ae390f083b5733afb0c0a0726 (plain)
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
/*
 * unit test helper for crypt_wipe API call
 *
 * Copyright (C) 2022-2024 Milan Broz
 *
 * 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 2
 * of the License, or (at your option) 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>

#include "libcryptsetup.h"

const char *test_file;
uint64_t test_offset, test_length, test_block;
uint32_t flags;
crypt_wipe_pattern pattern;

static void usage(void)
{
	fprintf(stderr, "Use:\tunit-wipe file/device zero|random|special offset length bsize [no-dio].\n");
}

static bool parse_u64(const char *arg, uint64_t *u64)
{
	unsigned long long ull;
	char *end;

	ull = strtoull(arg, &end, 10);
	if (*end || !*arg || errno == ERANGE)
		return false;

	if (ull % 512)
		return false;

	*u64 = ull;
	return true;
}

static bool parse_input_params(int argc, char **argv)
{
	struct stat st;

	if (argc < 6 || argc > 7) {
		usage();
		return false;
	}

	if (stat(argv[1], &st)) {
		fprintf(stderr, "File/device %s is missing?\n", argv[1]);
		return false;
	}
	test_file = argv[1];

	if (!strcmp(argv[2], "random"))
		pattern = CRYPT_WIPE_RANDOM;
	else if (!strcmp(argv[2], "zero"))
		pattern = CRYPT_WIPE_ZERO;
	else if (!strcmp(argv[2], "special"))
		pattern = CRYPT_WIPE_SPECIAL;
	else {
		fprintf(stderr, "Wrong pattern specification.\n");
		return false;
	}

	if (!parse_u64(argv[3], &test_offset)) {
		fprintf(stderr, "Wrong offset specification.\n");
		return false;
	}

	if (!parse_u64(argv[4], &test_length)) {
		fprintf(stderr, "Wrong length specification.\n");
		return false;
	}

	if (!parse_u64(argv[5], &test_block)) {
		fprintf(stderr, "Wrong block length specification.\n");
		return false;
	}

	if (argc > 6) {
		if (!strcmp(argv[6], "no-dio"))
			flags = CRYPT_WIPE_NO_DIRECT_IO;
		else {
			fprintf(stderr, "Wrong flags specification.\n");
			return false;
		}
	}

	return true;
}

int main(int argc, char **argv)
{
	struct crypt_device *cd;
	int r;

#ifndef NO_CRYPTSETUP_PATH
	if (getenv("CRYPTSETUP_PATH")) {
		printf("Cannot run this test with CRYPTSETUP_PATH set.\n");
		exit(77);
	}
#endif

	if (!parse_input_params(argc, argv))
		return EXIT_FAILURE;

	r = crypt_init(&cd, NULL);
	if (r < 0) {
		fprintf(stderr, "Context init failure %i.\n", r);
		return EXIT_FAILURE;
	}

	r = crypt_wipe(cd, test_file, pattern, test_offset, test_length,
		       test_block, flags, NULL, NULL);
	crypt_free(cd);

	if (r)
		fprintf(stderr, "Failure %i\n", r);

	return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}