[parted-devel] [PATCH 1/2] tests: test for the ped_disk_duplicate needs_clobber fix
Hans de Goede
hdegoede at redhat.com
Wed Feb 24 09:10:53 UTC 2010
Hi,
Thanks for writing a test for this!
Regards,
Hans
On 02/23/2010 06:59 PM, Jim Meyering wrote:
> Here's the test to go with Hans' fix.
> The 2nd commit updates to the latest from gnulib.
>
> From a6f6d8abd2c92171faa462cebeed0f30d2d81947 Mon Sep 17 00:00:00 2001
> From: Jim Meyering<meyering at redhat.com>
> Date: Tue, 23 Feb 2010 17:26:48 +0100
> Subject: [PATCH 1/2] tests: test for the ped_disk_duplicate needs_clobber fix
>
> * tests/dup-clobber.c: New file.
> Exercise the fix in commit jabb411b, "libparted: copy the
> needs_clobber value in ped_disk_duplicate()".
> * tests/Makefile.am (TESTS, check_PROGRAMS): Add dup-clobber.
> ---
> tests/Makefile.am | 3 +-
> tests/dup-clobber.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 105 insertions(+), 1 deletions(-)
> create mode 100644 tests/dup-clobber.c
>
> diff --git a/tests/Makefile.am b/tests/Makefile.am
> index 8008400..afe711c 100644
> --- a/tests/Makefile.am
> +++ b/tests/Makefile.am
> @@ -2,6 +2,7 @@ XFAIL_TESTS = \
> t3200-type-change.sh
>
> TESTS = \
> + dup-clobber \
> t0000-basic.sh \
> t0001-tiny.sh \
> t0010-script-no-ctrl-chars.sh \
> @@ -39,7 +40,7 @@ TESTS = \
> EXTRA_DIST = \
> $(TESTS) test-lib.sh t-lib.sh lvm-utils.sh t-local.sh t-lvm.sh
>
> -check_PROGRAMS = print-align print-max
> +check_PROGRAMS = print-align print-max dup-clobber
> LDADD = \
> $(top_builddir)/libparted/libparted.la
> AM_CPPFLAGS = \
> diff --git a/tests/dup-clobber.c b/tests/dup-clobber.c
> new file mode 100644
> index 0000000..4e7add2
> --- /dev/null
> +++ b/tests/dup-clobber.c
> @@ -0,0 +1,103 @@
> +/* Demonstrate that setting disk->needs_clobber in ped_disk_duplicate
> + is necessary. With that, this test passes. Without it, the last
> + sectors of the disk are cleared, and this test fails. */
> +#include<config.h>
> +#include<parted/parted.h>
> +#include<stdio.h>
> +#include<stdlib.h>
> +#include<assert.h>
> +#include<sys/types.h>
> +#include<unistd.h>
> +#include<fcntl.h>
> +
> +#include "closeout.h"
> +#include "progname.h"
> +
> +static void
> +seek_to_final_sector (int fd, PedSector ss)
> +{
> + /* Seek to EOF. */
> + off_t off = lseek (fd, 0, SEEK_END);
> +
> + /* That had better succeed and determine that the size is> 2 sectors
> + and an exact multiple of ss. */
> + assert (2 * ss< off);
> + assert (off % ss == 0);
> +
> + /* Back up one sector. */
> + off = lseek (fd, -ss, SEEK_CUR);
> + assert (0< off);
> +}
> +
> +static void
> +scribble_on_final_sector (char const *file_name, PedSector ss)
> +{
> + assert (0< ss);
> + assert (ss % 512 == 0);
> + int fd = open (file_name, O_WRONLY);
> + assert (0<= fd);
> +
> + seek_to_final_sector (fd, ss);
> +
> + /* Fill the final sector with ascii 'G's. */
> + char *buf = malloc (ss);
> + assert (buf);
> + memset (buf, 'G', ss);
> + assert (write (fd, buf, ss) == ss);
> + free (buf);
> + assert (close (fd) == 0);
> +}
> +
> +int
> +main (int argc, char **argv)
> +{
> + atexit (close_stdout);
> + set_program_name (argv[0]);
> +
> + if (argc != 1)
> + return EXIT_FAILURE;
> +
> + char const *dev_name = "dev-file";
> +
> + /* Create a file. */
> + int fd = open (dev_name, O_CREAT|O_TRUNC|O_WRONLY, 0644);
> + assert (0<= fd);
> + off_t size = 8 * 1024 * 1024;
> + assert (ftruncate (fd, size) == 0);
> + assert (close (fd) == 0);
> +
> + PedDevice *dev = ped_device_get (dev_name);
> + assert (dev);
> +
> + PedDisk *disk = ped_disk_new_fresh (dev, ped_disk_type_get ("msdos"));
> + assert (disk);
> +
> + assert (ped_disk_commit(disk));
> +
> + PedSector ss = dev->sector_size;
> + scribble_on_final_sector (dev_name, ss);
> +
> + /* Before the fix, this ped_disk_duplicate call would always set
> + copy->needs_clobber, thus causing the subsequent commit to
> + mistakenly clobber 9KiB at each end of the disk. */
> + PedDisk *copy = ped_disk_duplicate (disk);
> + assert (ped_disk_commit(copy));
> +
> + ped_disk_destroy (copy);
> + ped_disk_destroy (disk);
> + ped_device_destroy (dev);
> +
> + /* Read the final sector and ensure it's still all 'G's. */
> + fd = open (dev_name, O_RDONLY);
> + assert (0<= fd);
> + seek_to_final_sector (fd, ss);
> + char *buf = malloc (ss);
> + assert (buf);
> + assert (read (fd, buf, ss) == ss);
> + unsigned int i;
> + for (i = 0; i< ss; i++)
> + assert (buf[i] == 'G');
> + free (buf);
> +
> + return EXIT_SUCCESS;
> +}
> --
> 1.7.0.367.g566c3
>
>
> From 03d3c548a14c7dbd6f19c4fb94dc6da2d1c44050 Mon Sep 17 00:00:00 2001
> From: Jim Meyering<meyering at redhat.com>
> Date: Tue, 23 Feb 2010 18:51:16 +0100
> Subject: [PATCH 2/2] build: update gnulib submodule to latest
>
> ---
> gnulib | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/gnulib b/gnulib
> index 1d27f2a..2709233 160000
> --- a/gnulib
> +++ b/gnulib
> @@ -1 +1 @@
> -Subproject commit 1d27f2ae7983295480d2313d3b2a631f9962840a
> +Subproject commit 2709233ead439b582d82af48bd25e709378cda44
> --
> 1.7.0.367.g566c3
More information about the parted-devel
mailing list