From 6fa44cf98ec4671eb9d8211c6f6b52afc965bab2 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 16 Nov 2019 10:03:05 +0000 Subject: [PATCH] tests/test-offset: Create the test file in the test itself. Allows us to get rid of a shell script used to generate the file, and is also better because strictly speaking it was wrong to reuse the same test file between tests. --- .gitignore | 1 - tests/Makefile.am | 5 ---- tests/generate-offset-data.sh | 40 ----------------------------- tests/test-offset.c | 59 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 49 insertions(+), 56 deletions(-) delete mode 100755 tests/generate-offset-data.sh diff --git a/.gitignore b/.gitignore index c75beead..b25ac7fe 100644 --- a/.gitignore +++ b/.gitignore @@ -78,7 +78,6 @@ Makefile.in /tests/file-data /tests/functions.sh /tests/keys.psk -/tests/offset-data /tests/partition-disk /tests/pki /tests/split1 diff --git a/tests/Makefile.am b/tests/Makefile.am index bc6fcaef..69870b5b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1010,13 +1010,8 @@ TESTS += test-nozero.sh endif HAVE_NBD_PLUGIN # offset filter test. -check_DATA += offset-data -CLEANFILES += offset-data LIBGUESTFS_TESTS += test-offset -offset-data: generate-offset-data.sh - $(srcdir)/generate-offset-data.sh $@ - test_offset_SOURCES = test-offset.c test.h test_offset_CFLAGS = $(WARNINGS_CFLAGS) $(LIBGUESTFS_CFLAGS) test_offset_LDADD = libtest.la $(LIBGUESTFS_LIBS) diff --git a/tests/generate-offset-data.sh b/tests/generate-offset-data.sh deleted file mode 100755 index c97665cd..00000000 --- a/tests/generate-offset-data.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash -# Copyright (C) 2019 Red Hat Inc. -# -# 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. - -set -e - -rm -f $1-t $1 - -for f in {1..1048576}; do - printf '\x55\xAA\x55\xAA\x55\xAA\x55\xAA\x55\xAA' -done > $1-t - -mv $1-t $1 diff --git a/tests/test-offset.c b/tests/test-offset.c index e50c8dc5..1aea3bc2 100644 --- a/tests/test-offset.c +++ b/tests/test-offset.c @@ -47,6 +47,40 @@ #include "test.h" +#define FILENAME "offset-data" +#define FILESIZE (10 * 1024 * 1024) + +static void +create_file (void) +{ + FILE *fp; + char pattern[512]; + size_t i; + + for (i = 0; i < sizeof pattern; i += 2) { + pattern[i] = 0x55; + pattern[i+1] = 0xAA; + } + + fp = fopen (FILENAME, "w"); + if (fp == NULL) { + perror (FILENAME); + exit (EXIT_FAILURE); + } + + for (i = 0; i < FILESIZE; i += sizeof pattern) { + if (fwrite (pattern, sizeof pattern, 1, fp) != 1) { + perror (FILENAME ": write"); + exit (EXIT_FAILURE); + } + } + + if (fclose (fp) == EOF) { + perror (FILENAME); + exit (EXIT_FAILURE); + } +} + static uint8_t buf[1024*1024]; static void @@ -70,14 +104,15 @@ main (int argc, char *argv[]) int r, fd; char *data; - /* offset-data (created by tests/Makefile.am) is a 10 MB file - * containing test pattern data 0x55AA repeated. We use the middle - * 8 MB to create a partition table and filesystem, and check - * afterwards that the test pattern in the first and final megabyte - * has not been overwritten. + /* FILENAME is a 10 MB file containing test pattern data 0x55AA + * repeated. We use the middle 8 MB to create a partition table and + * filesystem, and check afterwards that the test pattern in the + * first and final megabyte has not been overwritten. */ + create_file (); + if (test_start_nbdkit ("--filter", "offset", - "file", "offset-data", + "file", FILENAME, "offset=1M", "range=8M", NULL) == -1) exit (EXIT_FAILURE); @@ -136,22 +171,26 @@ main (int argc, char *argv[]) /* Check the first and final megabyte of test patterns has not been * overwritten in the underlying file. */ - fd = open ("offset-data", O_RDONLY|O_CLOEXEC); + fd = open (FILENAME, O_RDONLY|O_CLOEXEC); if (fd == -1) { - perror ("open: offset-data"); + perror (FILENAME ": open"); exit (EXIT_FAILURE); } if (pread (fd, buf, sizeof buf, 0) != sizeof buf) { - fprintf (stderr, "pread: short read or error (%d)\n", errno); + fprintf (stderr, "%s: pread: short read or error (%d)\n", + FILENAME, errno); exit (EXIT_FAILURE); } check_buf (); if (pread (fd, buf, sizeof buf, 9*1024*1024) != sizeof buf) { - fprintf (stderr, "pread: short read or error (%d)\n", errno); + fprintf (stderr, "%s: pread: short read or error (%d)\n", + FILENAME, errno); exit (EXIT_FAILURE); } check_buf (); close (fd); + unlink (FILENAME); + exit (EXIT_SUCCESS); } -- 2.11.4.GIT