Anonymous avatar Anonymous committed e5060bf

Comments (0)

Files changed (6)

 static const char* usage_msg = N_(
 	"Usage: cfdisk [OPTION]... [DEVICE]\n"
 );
-
+/* TODO: Should we move to argp? */
 static struct { const char* lopt; const char opt; const char *arg; const char *help; } options_help[] = 
 {
 	{"help",	'h', NULL,	N_("displays this help message")},
    if they FAIL and leave need_commit to true, please reread the partition table from the device.
    Sorry for the inconvinience. I might change this in the future so that it is not needed */
 extern int do_check (PedDisk*, PedPartition*, UICalls *);
+/* The parameters are the *destination* disk and partition */
 extern int do_cp (PedDisk*, PedPartition*, UIOpts, UICalls *);
 extern int do_mklabel (PedDevice*, PedDisk**, const PedDiskType*, UICalls *);
 

tests/check_advanced.c

 }
 END_TEST
 #endif
-
+/* */
+static void
+warn () {
+	printf("We are about to run a very CPU and disk intensive test.\n"
+	       "It can take about 500 MB of free space and continue half an hour.\n"
+               "Are you sure you want to continue (y/N)? ");
+	if (getchar() != 'y' && getchar() != 'Y') {
+		exit(0);
+	}
+}
 /* We need to create a new tempfile for each test, so we can save disk space */
 static void
 open_tempfile(int mklabel) {
 	if (!init_tempfile()) fail("Could not init tempfile");
 	if (!open_device()) {
-		unlink_tempfile;
+		unlink_tempfile();
 		fail("Could not open the tempfile");
 	}
 	if (mklabel && !do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery))
 }
 static void
 close_tempfile() {
+	ped_disk_destroy(disk);
+	ped_device_destroy(dev);
 	unlink_tempfile();
 }
 
 START_TEST (test_resize) {
 	open_tempfile(0);
 	change_size("msdos", UI_DEFAULT);
-	close_tempfile();
+	/* We will use the result file for testing partition copy */
+	rename(tempfile_name(),tempfile2_name());
+	//close_tempfile();
 }
 END_TEST
 
 }
 END_TEST
 
+/* This uses about 30 MB */
+START_TEST (test_copy) {
+	PedPartition *part = NULL;
+	PedGeometry *geom = NULL;
+	PedFileSystem *fs;
+	PedFileSystemType *fs_type = ped_file_system_type_get("fat32");
+	open_tempfile(0);
+	/* Why not amiga? */
+	fail_unless(do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery),
+	            "Failed to create partition table for partition copy");
+	PedConstraint *constraint = ped_device_get_constraint(dev);
+	
+	/* Opening the disk with our partitions */
+	PedDevice *dev2 = ped_device_get(tempfile2_name());
+	fail_unless(ped_device_open(dev2), 
+		            "Failed to open the device for copying");
+	PedDisk	*disk2 = NULL;
+	/* This throws an exception on failure, we should fail */
+	disk2 = ped_disk_new(dev2);
+	//fail_unless(1, "hm");
+
+
+	srand(892791);
+	int i;
+	/* We copy all partitions to the new disk */
+	for (i = 0; i < 4; i++) {
+		/* We create a partition on the empty  disk */
+		next_part_geometry(&geom, constraint, 1);
+		PUT_STRING("Label");
+		set_exception_error("Creating partition %d for copy at "
+			"%llds-%llds", i, geom->start, geom->end);
+		fail_unless(_mkpart (0, geom->start,  geom->end,
+				PED_PARTITION_NORMAL,
+ 				fs_type, &part, do_mkpart),
+			"Failed to create %d partition for copy. "
+			"Size %llds, start %llds, end %llds",
+			i, geom->length, geom->start, geom->end);
+		/* We will query the source disk and partition */
+		uiquery.getdev = getdev;
+		device = dev2;
+		uiquery.getpart = NULL;
+		uiquery.getint = getint;
+		integer = i+1;
+		fail_unless(do_cp (disk, part, UI_DEFAULT, &uiquery), 
+		            "%d->%d partition copy failed (from %llds-%llds "
+		            "to %llds-%llds)", 0LL, 0LL, part->geom.start,
+		            part->geom.end);
+		fs = ped_file_system_open(&(part->geom));
+		fail_unless(fs != NULL, "No filesystem on partition %d", i);
+		fail_unless(ped_file_system_check(fs,NULL),
+		            "Error on partition %d", i);
+		ped_file_system_close(fs);
+
+	}
+	/* Now we try to copy from a partition on the same disk */
+	device = dev;
+	integer = 1;
+	fail_unless(do_cp (disk, part, UI_DEFAULT, &uiquery), 
+	            "Partition copy on a single disk failed");
+	ped_disk_destroy(disk2);
+	ped_device_destroy(dev2);
+	/* We don't need this anymore. We won't unlink the tempfile, because *
+         * we will test the rescue on it.                                    */
+	unlink(tempfile2_name());
+}
+END_TEST
+
+/* This does not take any additional disk space */
+START_TEST (test_rescue) {
+	int i;
+	PedPartition *part = NULL;
+	PedGeometry geom;
+
+	dev = ped_device_get(tempfile_name());
+	fail_unless(ped_device_open(dev), 
+		            "Failed to open the device for rescue");
+	disk = ped_disk_new(dev);
+
+	/* First we delete the partitions */
+	part = ped_disk_get_partition(disk,
+		ped_disk_get_last_partition_num (disk));
+	geom = part->geom;
+	while (part) {
+		PedPartition *temp = part;
+		part = part->prev;
+		if (!temp->type) {
+			fail_unless(do_rm(disk, part, &uiquery),
+			            "Rescue could not delete %d partition",
+			            part->num);
+		}
+	}
+	/* Then we try to undelete the last one */
+	fail_unless(do_rescue(disk, geom.start, geom.end, UI_DEFAULT, &uiquery),
+	            "Failed to rescue a single partition at %llds-%llds",
+	            geom.start, geom.end);
+	part = NULL;
+	do {
+		part = ped_disk_next_partition(disk,NULL);
+	} while (part && part->type);
+	fail_unless(geom.start==part->geom.start && geom.end==part->geom.end,
+                    "Rescued partition is not the same as the deleted. "
+                    "Deleted was %llds-%llds, we got %llds-%llds.",
+	            geom.start, geom.end, part->geom.start, part->geom.end);
+	/* We delete it again and try to rescue all */
+	fail_unless(do_rm(disk, part, &uiquery),
+	            "Rescue could not delete %d partition",
+	            part->num);
+	/* The end of the last partition is before 8388604s */
+	fail_unless(do_rescue(disk, 0LL, geom.end, UI_DEFAULT, &uiquery),
+	            "Failed to rescue the partitions");
+	/* Now we count them, if they aren't four, we did something wrong */
+	for (part = ped_disk_next_partition(disk,NULL), i = 0;
+	     part; part = ped_disk_next_partition(disk,part), i++);
+	fail_unless(i == 4, "We got %d partitions instead of 4", i);
+	close_tempfile();
+}
+END_TEST
 
 Suite *common_suite(void)
 {
 	Suite *s = suite_create("Advanced checks");
 	TCase *tc_resize = tcase_create("Resize");
-	TCase *tc_move = tcase_create("move");
+	TCase *tc_move = tcase_create("Move");
+	TCase *tc_copy = tcase_create("Copy");
+	TCase *tc_rescue = tcase_create("Rescue");
 	suite_add_tcase (s, tc_resize);
 	suite_add_tcase (s, tc_move);
+	suite_add_tcase (s, tc_copy);
+	/* FIXME: Too slow. Make faster and turn on */
+	//suite_add_tcase (s, tc_rescue);
 	tcase_set_timeout(tc_resize, 360);
 	tcase_set_timeout(tc_move, 360);
+	tcase_set_timeout(tc_copy, 360);
+	/* This could take a lot of time, I'm not sure that's enough */
+	tcase_set_timeout(tc_rescue, 1200);
 	tcase_add_test(tc_resize, test_resize);
 	tcase_add_test(tc_move, test_move);
+	tcase_add_test(tc_copy, test_copy);
+	tcase_add_test(tc_rescue, test_rescue);
 	return s;
 }
 
 
 int main (void)
 {
+	warn();
 	int nf;
 	ped_exception_set_handler(exception_handler);
 	reset_exception_error();

tests/check_common.c

 
 
 
-
+/* FIXME: Make use of rand_r instead of rand ! See functions.h */
 
 /* Here we test mklabel */
 START_TEST (test_mklabel)

tests/functions.c

 #include "functions.h"
 #include "../src/common.h"
 
+
+/* NOTE: In the comments we say that we "query" or "ask" something when we
+   emulate user query function and pass the parameter through there */
+
 /* We can't seek more... */
 #define SIZE 2147483647L
 /* We will give the partitions below different sizes. I'll use rand */
 PedDisk *disk = NULL;
 UICalls uiquery;
 
+/* We need a seed for the random functions */
+int seed = 1;
+
+void
+set_seed(int sd) {
+	seed = sd;
+}
+
 /* No chance someone else decides to call this file this way, I hope */
 static char tempfile[] = "/tmp/check_cfdisk_temporary_disk_drive";
+static char tempfile2[] = "/tmp/check_cfdisk_temporary_disk_drive2";
+
+const char *
+tempfile_name()
+{
+	return tempfile;
+}
+
+const char *
+tempfile2_name()
+{
+	return tempfile2;
+}
 
 
 /* We need an exception handler */
                                      PedPartition **, UIOpts, UICalls *))
 {
 	switch(how % 3) {
-		/* We create the partition by giving start and end */
+		/* We create the partition by specifying start and end */
 		case 0:
 			uiquery.getstring = getstring;
 			strncpy(string[0], fs_type->name, BUFSIZE-1);
 			uiquery.getstring = getstring;
 			return ch_mkpart(disk,start,end,type,fs_type,newpart,
 			                 UI_CUSTOM_VALUES,&uiquery);
-		/* We "let the user" specify and pretend to be the user */
+		/* We query the start and end */
 		case 2:
 			uiquery.getstring = getstring;
 			string[0][BUFSIZE] = 2;
 	if (*geom) ped_geometry_destroy(*geom);
 	PedSector start, end;
 	int i = 0;
-
+	/* We check if we have free space before or after the partition */
 	if (part->prev && part->prev->type & PED_PARTITION_FREESPACE)
 		start = part->prev->geom.start;
 	else
 		end = part->next->geom.end;
 	else
 		end = part->geom.end;	
+
 	temp = ped_geometry_new(dev, start, end-start+1LL);
 	*geom = ped_constraint_solve_nearest(constraint,temp);
 	ped_geometry_destroy(temp);
 void
 randomize_position(PedSector *start, PedSector *end, PedGeometry *where, int i)
 {
-
+	/* This chooses random partition position within where */
+	/* If i is 0, we place it at the beginning, if i is 3, at the end */
 	if (i == 0) {
 		*start = where->start;
 		do {

tests/functions.h

 #define DEFAULT_STRING "RESERVED"
 #define BUFSIZE 4093
 
+/* The names of the tempfiles */
+extern const char* tempfile_name();
+extern const char* tempfile2_name();
+
 /* We have our device here */
-PedDevice *dev;
-PedDisk *disk;
-UICalls uiquery;
+extern PedDevice *dev;
+extern PedDisk *disk;
+extern UICalls uiquery;
+
+/* Random seed */
+extern int seed;
+extern void set_seed(int);
+/* TODO: Substitute this in the code and remove these, when finalized */
+#define srand(x) set_seed(x)
+#define rand() rand_r(&seed)
 
 /* We will use common exception handling for tests */
 extern PedExceptionOption ex_opts;
 /* This are functions for UICalls */
 /* NOTE: This macros are only for reference */
 #define PUT_STRING(x) string[0][BUFSIZE] = 0; \
+                      uiquery.getstring = getstring; \
                       strncpy(string[0], x, BUFSIZE-1)
 #define PUT_TWO_STRINGS(x,y) string[0][BUFSIZE] = 2; \
+                             uiquery.getstring = getstring; \
                              strncpy(string[0], x, BUFSIZE-1); \
                              strncpy(string[1], x, BUFSIZE-1)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.