Test the partition filter against real life partition tables created
by sfdisk.
---
tests/test-partition.c | 101 ---------------------------
README | 2 +
tests/Makefile.am | 7 +-
tests/test-partition.sh | 148 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 152 insertions(+), 106 deletions(-)
diff --git a/tests/test-partition.c b/tests/test-partition.c
deleted file mode 100644
index 3de60d8..0000000
--- a/tests/test-partition.c
+++ /dev/null
@@ -1,101 +0,0 @@
-/* nbdkit
- * Copyright (C) 2018 Red Hat Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * * Neither the name of Red Hat nor the names of its contributors may be
- * used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <config.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <inttypes.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <guestfs.h>
-
-#include "test.h"
-
-int
-main (int argc, char *argv[])
-{
- guestfs_h *g;
- int r;
- char *data;
-
- if (test_start_nbdkit ("-r",
- "--filter", "partition",
- "file", "disk",
- "partition=1",
- NULL) == -1)
- exit (EXIT_FAILURE);
-
- g = guestfs_create ();
- if (g == NULL) {
- perror ("guestfs_create");
- exit (EXIT_FAILURE);
- }
-
- r = guestfs_add_drive_opts (g, "",
- GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
- GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
- GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "nbd",
- GUESTFS_ADD_DRIVE_OPTS_SERVER, server,
- -1);
- if (r == -1)
- exit (EXIT_FAILURE);
-
- if (guestfs_launch (g) == -1)
- exit (EXIT_FAILURE);
-
- /* Because we're using the partition filter, the device should
- * appear to be a filesystem directly on a whole disk.
- */
- if (guestfs_mount_ro (g, "/dev/sda", "/") == -1)
- exit (EXIT_FAILURE);
-
- data = guestfs_cat (g, "/hello.txt");
- if (!data)
- exit (EXIT_FAILURE);
-
- if (strcmp (data, "hello,world") != 0) {
- fprintf (stderr,
- "%s FAILED: unexpected content of /hello.txt file "
- "(actual: %s, expected: \"hello,world\")\n",
- program_name, data);
- exit (EXIT_FAILURE);
- }
-
- free (data);
-
- guestfs_close (g);
- exit (EXIT_SUCCESS);
-}
diff --git a/README b/README
index 5824d38..63e1bea 100644
--- a/README
+++ b/README
@@ -149,6 +149,8 @@ For non-essential enhancements to the test suite:
- qemu-io (usually shipped with qemu)
+ - sfdisk (from util-linux)
+
- socat
- ss (from iproute package)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 27abbaf..007203f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -79,6 +79,7 @@ EXTRA_DIST = \
test-offset2.sh \
test-parallel-file.sh \
test-parallel-nbd.sh \
+ test-partition.sh \
test-partitioning1.sh \
test-partitioning2.sh \
test-partitioning3.sh \
@@ -792,11 +793,7 @@ test_offset_LDADD = libtest.la $(LIBGUESTFS_LIBS)
TESTS += test-offset2.sh
# partition filter test.
-LIBGUESTFS_TESTS += test-partition
-
-test_partition_SOURCES = test-partition.c test.h
-test_partition_CFLAGS = $(WARNINGS_CFLAGS) $(LIBGUESTFS_CFLAGS)
-test_partition_LDADD = libtest.la $(LIBGUESTFS_LIBS)
+TESTS += test-partition.sh
# truncate filter tests.
TESTS += \
diff --git a/tests/test-partition.sh b/tests/test-partition.sh
new file mode 100755
index 0000000..a9b2bdf
--- /dev/null
+++ b/tests/test-partition.sh
@@ -0,0 +1,148 @@
+#!/usr/bin/env bash
+# nbdkit
+# Copyright (C) 2018-2019 Red Hat Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+source ./functions.sh
+set -e
+set -x
+
+d="partition.d"
+rm -rf $d
+cleanup_fn rm -rf $d
+mkdir $d
+
+# Test that sfdisk is available and working.
+if ! sfdisk --help >/dev/null; then
+ echo "$0: missing or broken sfdisk"
+ exit 77
+fi
+
+# Test that /dev/urandom exists and can be read.
+if ! test -r /dev/urandom; then
+ echo "$0: mising or unreadable /dev/urandom"
+ exit 77
+fi
+
+# Test that qemu-img is available and working.
+if ! qemu-img --help >/dev/null; then
+ echo "$0: missing or broken qemu-img"
+ exit 77
+fi
+
+test ()
+{
+ label=$1
+ nrparts=$2
+
+ rm -f $d/disk
+ truncate -s 1G $d/disk
+ sfdisk -X $label $d/disk
+
+ # Run nbdkit on each partition, copying data in and out.
+ for ((part=1; part <= $nrparts; ++part)); do
+ # The smallest partition in any test is 1023 sectors. However
+ # to make things quicker only write a sector of random data.
+ dd if=/dev/urandom of=$d/rand bs=512 count=1
+
+ if [ $nrparts -le 4 ] || [ $label != "dos" ] || [ $part -ne 4 ]; then
+ nbdkit -f -v --filter=partition file $d/disk partition=$part \
+ --run "qemu-img convert -n $d/rand \$nbd"
+ nbdkit -f -v --filter=partition file $d/disk partition=$part \
+ --run "qemu-img convert \$nbd $d/out"
+ truncate -s 512 $d/out
+ cmp $d/rand $d/out
+ fi
+ done
+}
+
+test dos 1 <<'EOF'
+2048 1023 L -
+EOF
+
+test dos 2 <<'EOF'
+2048 1023 L -
+4096 4095 L -
+EOF
+
+test dos 3 <<'EOF'
+2048 1023 L -
+4096 4095 L -
+8192 8191 L -
+EOF
+
+test dos 6 <<'EOF'
+2048 2047 L -
+4096 4095 L -
+8192 8191 L -
+16384 16383 E -
+17000 999 L -
+18000 999 L -
+EOF
+
+test gpt 1 <<'EOF'
+2048 1023 L -
+EOF
+
+test gpt 2 <<'EOF'
+2048 1023 L -
+4096 4095 L -
+EOF
+
+test gpt 3 <<'EOF'
+2048 1023 L -
+4096 4095 L -
+8192 8191 L -
+EOF
+
+test gpt 4 <<'EOF'
+2048 1023 L -
+4096 4095 L -
+8192 8191 L -
+16384 16383 L -
+EOF
+
+test gpt 5 <<'EOF'
+2048 2047 L -
+4096 4095 L -
+8192 8191 L -
+16384 16383 L -
+32768 32767 L -
+EOF
+
+test gpt 6 <<'EOF'
+2048 2047 L -
+4096 4095 L -
+8192 8191 L -
+16384 16383 L -
+32768 32767 L -
+65536 65535 L -
+EOF
--
2.20.1