File: test_EraseFileSystemSignatures.cc

package info (click to toggle)
gparted 1.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,752 kB
  • sloc: cpp: 34,868; sh: 5,073; makefile: 462; sed: 16; ansic: 9
file content (266 lines) | stat: -rw-r--r-- 8,239 bytes parent folder | download
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
/* Copyright (C) 2023 Mike Fleetwood
 *
 *  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, see <http://www.gnu.org/licenses/>.
 */

/* Test GParted_Core::erase_filesystem_signatures()
 */


#include "common.h"
#include "insertion_operators.h"
#include "GParted_Core.h"
#include "OperationDetail.h"
#include "Partition.h"
#include "Utils.h"
#include "gtest/gtest.h"

#include <vector>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <gtkmm.h>
#include <parted/parted.h>
#include <glibmm/thread.h>


namespace GParted
{


// Explicit test fixture class for common variables and methods used in each test.
// Reference:
//     Google Test, Primer, Test Fixtures: Using the Same Data Configuration for Multiple Tests
class EraseFileSystemSignaturesTest : public ::testing::Test
{
protected:
	virtual void create_image_file(Byte_Value size);
	virtual void write_signatures(const char* signature, const std::vector<off_t>& sector_offsets);
	virtual void write_intel_software_raid_signature();
	virtual void write_all_possible_promise_fasttrack_raid_signatures();
	virtual bool image_contains_all_zeros();
	virtual void TearDown();

	bool erase_filesystem_signatures(const Partition& partition, OperationDetail& operationdetail)
		{ return m_gparted_core.erase_filesystem_signatures(partition, operationdetail); };

	static const char* s_image_name;

	GParted_Core    m_gparted_core;
	Partition       m_partition;
	OperationDetail m_operation_detail;
};


const char* EraseFileSystemSignaturesTest::s_image_name = "test_EraseFileSystemSignatures.img";


void EraseFileSystemSignaturesTest::create_image_file(Byte_Value size)
{
	// Create new image file to work with.
	unlink(s_image_name);
	int fd = open(s_image_name, O_WRONLY|O_CREAT|O_NONBLOCK, 0666);
	ASSERT_GE(fd, 0) << "Failed to create image file '" << s_image_name << "'.  errno="
	                 << errno << "," << strerror(errno);
	ASSERT_EQ(ftruncate(fd, (off_t)size), 0) << "Failed to set image file '" << s_image_name << "' to size "
	                                         << size << ".  errno=" << errno << "," << strerror(errno);
	close(fd);

	// Initialise m_partition as a Partition object spanning the whole of the image file.
	m_partition.Reset();

	PedDevice* lp_device = ped_device_get(s_image_name);
	ASSERT_TRUE(lp_device != nullptr);

	m_partition.set_unpartitioned(s_image_name,
	                              lp_device->path,
	                              FS_UNALLOCATED,
	                              lp_device->length,
	                              lp_device->sector_size,
	                              false);

	ped_device_destroy(lp_device);
	lp_device = nullptr;
}


void EraseFileSystemSignaturesTest::write_signatures(const char* signature, const std::vector<off_t>& sector_offsets)
{
	const off_t SectorSize = 512;

	int fd = open(s_image_name, O_WRONLY|O_NONBLOCK);
	ASSERT_GE(fd, 0) << "Failed to open image file '" << s_image_name << "'.  errno="
	                 << errno << "," << strerror(errno);
	size_t signature_len = strlen(signature);

	for (size_t i = 0; i < sector_offsets.size(); i++)
	{
		// Positive offsets are relative to the start of the file, negative
		// offsets relative to the end of the file.
		int whence = sector_offsets[i] >= 0 ? SEEK_SET : SEEK_END;
		ASSERT_GE(lseek(fd, sector_offsets[i] * SectorSize, whence), 0)
		        << "Failed to seek in image file '" << s_image_name << "'.  errno="
		        << errno << "," << strerror(errno);
		ASSERT_EQ(write(fd, signature, signature_len), (ssize_t)signature_len)
		        << "Failed to write to image file '" << s_image_name << "'.  errno="
		        << errno << "," << strerror(errno);
	}
	close(fd);
}


void EraseFileSystemSignaturesTest::write_intel_software_raid_signature()
{
	// Write Intel Software RAID signature at -2 sectors before the end.  Hard codes
	// sector size to 512 bytes for a file.
	// Reference:
	//     .../util-linux/libblkid/src/superblocks/isw_raid.c:probe_iswraid().
	std::vector<off_t> sector_offsets{-2};
	write_signatures("Intel Raid ISM Cfg Sig. ", sector_offsets);
}


void EraseFileSystemSignaturesTest::write_all_possible_promise_fasttrack_raid_signatures()
{
	// Write all possible Promise FastTrack RAID signatures.
	// Reference:
	//     .../util-linux/libblkid/src/superblocks/promise_raid.c:probe_pdcraid().
	std::vector<off_t> sector_offsets{-63, -255, -256, -16, -399, -591, -675, -735, -911, -974, -991, -951, -3087};
	write_signatures("Promise Technology, Inc.", sector_offsets);
}


const char* first_non_zero_byte(const char* buf, size_t size)
{
	while (size > 0)
	{
		if (*buf != '\0')
			return buf;
		buf++;
		size--;
	}
	return nullptr;
}


bool EraseFileSystemSignaturesTest::image_contains_all_zeros()
{
	int fd = open(s_image_name, O_RDONLY|O_NONBLOCK);
	if (fd < 0)
	{
		ADD_FAILURE() << __func__ << "(): Failed to open image file '" << s_image_name << "'.  errno="
		              << errno << "," << strerror(errno);
		return false;
	}

	ssize_t bytes_read = 0;
	size_t offset = 0;
	do
	{
		char buf[BUFSIZ];
		bytes_read = read(fd, buf, sizeof(buf));
		if (bytes_read < 0)
		{
			ADD_FAILURE() << __func__ << "(): Failed to read from image file '" << s_image_name
			              << "'.  errno=" << errno << "," << strerror(errno);
			close(fd);
			return false;
		}
		const char* p = first_non_zero_byte(buf, bytes_read);
		if (p != nullptr)
		{
			ADD_FAILURE() << __func__ << "(): First non-zero bytes:\n"
			              << binary_string_to_print(offset + (p - buf), p, buf + bytes_read - p);
			close(fd);
			return false;
		}
	}
	while (bytes_read > 0);
	close(fd);
	return true;
}


void EraseFileSystemSignaturesTest::TearDown()
{
	unlink(s_image_name);
}


TEST_F(EraseFileSystemSignaturesTest, IntelSoftwareRAIDAligned)
{
	create_image_file(16 * MEBIBYTE);
	write_intel_software_raid_signature();

	EXPECT_TRUE(erase_filesystem_signatures(m_partition, m_operation_detail)) << m_operation_detail;
	EXPECT_TRUE(image_contains_all_zeros());
}


TEST_F(EraseFileSystemSignaturesTest, IntelSoftwareRAIDUnaligned)
{
	create_image_file(16 * MEBIBYTE - 512);
	write_intel_software_raid_signature();

	EXPECT_TRUE(erase_filesystem_signatures(m_partition, m_operation_detail)) << m_operation_detail;
	EXPECT_TRUE(image_contains_all_zeros());
}


TEST_F(EraseFileSystemSignaturesTest, PromiseFastTrackRAIDAligned)
{
	create_image_file(16 * MEBIBYTE);
	write_all_possible_promise_fasttrack_raid_signatures();

	EXPECT_TRUE(erase_filesystem_signatures(m_partition, m_operation_detail)) << m_operation_detail;
	EXPECT_TRUE(image_contains_all_zeros());
}


TEST_F(EraseFileSystemSignaturesTest, PromiseFastTrackRAIDUnaligned)
{
	create_image_file(16 * MEBIBYTE - 512);
	write_all_possible_promise_fasttrack_raid_signatures();

	EXPECT_TRUE(erase_filesystem_signatures(m_partition, m_operation_detail)) << m_operation_detail;
	EXPECT_TRUE(image_contains_all_zeros());
}


}  // namespace GParted


// Custom Google Test main().
// Reference:
// *   Google Test, Primer, Writing the main() function
//     https://github.com/google/googletest/blob/master/googletest/docs/primer.md#writing-the-main-function
int main(int argc, char** argv)
{
	printf("Running main() from %s\n", __FILE__);
	GParted::ensure_x11_display(argc, argv);

	// Initialise threading in GParted to successfully use Utils:: and
	// FileSystem::execute_command().  Must be before InitGoogleTest().
	GParted::GParted_Core::mainthread = Glib::Thread::self();
	Gtk::Main gtk_main = Gtk::Main();

	testing::InitGoogleTest(&argc, argv);

	return RUN_ALL_TESTS();
}