Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Aug 1999 11:13:29 -0700 (PDT)
From:      Matthew Jacob <mjacob@feral.com>
To:        Brian McGovern <bmcgover@cisco.com>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: Looking for good QA tests... Part 1..
Message-ID:  <Pine.BSF.4.05.9908271110230.2537-100000@semuta.feral.com>
In-Reply-To: <199908261747.NAA00420@bmcgover-pc.cisco.com>

next in thread | previous in thread | raw e-mail | index | archive | help

A bit of cleanup and filing off of serial numbers, etc...

test script && c program... very stupid test, except that in large doses
brings systems to their knees and very much exercises buffer ownership
issues. The test looks too stupid to catch things, but believe me, it
does.

#!/bin/sh
#
# Copyright (c) 1999 Matthew Jacob
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer,
#    without modification, immediately at the beginning of the file.
# 2. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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.
#
#      $Id: testfil,v 1.1 1999/08/27 18:11:19 mjacob Exp $
#
FILESIZ=${FILESIZ-1m}
FILECNT=${FILECNT-10}
tripcount=1

if [ $# -ne 1 ]; then
	echo "usage: $0 testdirectory"
	exit 1
fi

while :
do
	echo "Begin Pass $tripcount"
	i=1
	while [ $i -lt ${FILECNT} ]
	do
		filbuf -p $i -s ${FILESIZ} -o $1/file.$i &
		i=`expr $i '+' 1`
	done
	wait
	i=1
	while [ $i -lt ${FILECNT} ]
	do
		filbuf -p $i -s ${FILESIZ} -i $1/file.$i
		if [ $? -ne 0 ]
		then
			exit 1
		fi
		rm -f $1/file.$i
		i=`expr $i '+' 1`
	done
	tripcount=`expr $tripcount '+' 1`
done

/*
 * Copyright (c) 1999 Matthew Jacob
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification, immediately at the beginning of the file.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR 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.
 *
 *      $Id: filbuf.c,v 1.1 1999/08/27 18:11:09 mjacob Exp $
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <memory.h>

static off_t szarg(char *);
static void miscompare(char *, char *, int, off_t);

#define	DEFAULT_BUFLEN			(63 << 10)
#define	DEFAULT_FILESIZE		(1 << 20)

extern int optind;
extern char *optarg;

int
main(int argc, char **argv)
{
	static const char *usage =
	    "usage: %s -{o,i} filename [ -s size[{k|m|g}] ] [ -p idpattern ]";
	int c, fd, io, amt;
	off_t filesize, curoff;
	size_t buflen;
	char *idp, *filename, *buf, *ptr;

	io = 0;
	idp = argv[0];
	filename = NULL;
	buflen = DEFAULT_BUFLEN;
	filesize = DEFAULT_FILESIZE;

	while ((c = getopt(argc, argv, "p:s:o:i:")) != -1) {
		switch (c) {
		case 'p':
			idp = optarg;
			break;
		case 'i':
			if (filename != NULL) {
				fprintf(stderr, usage, argv[0]);
				return (1);
			}
			filename = optarg;
			io = 'i';
			break;
		case 'o':
			if (filename != NULL) {
				fprintf(stderr, usage, argv[0]);
				return (1);
			}
			filename = optarg;
			io = 'o';
			break;
		case 's':
			filesize = szarg(optarg);
			break;
		default:
			fprintf(stderr, usage, argv[0]);
			return (1);
		}

	}	

	if (filename == NULL) {
		fprintf(stderr, usage, argv[0]);
		return (1);
	}

	if ((buf = malloc(buflen)) == NULL) {
		perror("malloc");
		return (1);
	}

	/*
	 * Fill the buffer with a pattern we write out or compare to.
	 */
	ptr = buf;
	c = strlen(idp);
	for (ptr = buf; ptr <= &buf[buflen-c]; ptr += c) {
		strcpy(ptr, idp);
	}

	if (io == 'o') {
		fd = open(filename, O_RDWR|O_CREAT, 0644);
		if (fd < 0) {
			perror(filename);
			return (1);
		}
		for (curoff = 0; curoff < filesize; curoff += amt) {
			amt = buflen;
			if ((curoff + amt) > filesize) {
				amt = (int) (filesize - curoff);
			}
			io = write(fd, buf, amt);
			if (io != amt) {
				if (errno)
					perror("write");
				fprintf(stderr, "Wrote %d of %d\n", io, amt);
				(void) close(fd);
				return (1);
			}
		}
	} else {
		char *cmpbuf = malloc(buflen);
		if (cmpbuf == NULL) {
			perror("malloc");
			return (1);
		}
		fd = open(filename, O_RDONLY, 0);
		if (fd < 0) {
			perror(filename);
			return (1);
		}
		for (curoff = 0; curoff < filesize; curoff += amt) {
			amt = buflen;
			if ((curoff + amt) > filesize) {
				amt = (int) (filesize - curoff);
			}
			io = read(fd, cmpbuf, amt);
			if (io != amt) {
				if (errno)
					perror("read");
				fprintf(stderr, "Read %d of %d\n", io, amt);
				(void) close(fd);
				return (1);
			}
			if (memcmp(buf, cmpbuf, amt)) {
				miscompare(buf, cmpbuf, amt, curoff);
				(void) close(fd);
				return (1);
			}
		}
	}
	(void) close(fd);
	return (0);
}

static void
miscompare(char *diskbuf, char *patternbuf, int bufsize, off_t curoff)
{
	int i;
	fprintf(stdout, "\tData Miscompare Detected\n");
	fprintf(stdout, "   Offset\t Original -> Read\n");
	fprintf(stdout, "---------------------------------\n");
	for (i = 0; i < bufsize; i++) {
		if (diskbuf[i] != patternbuf[i]) {
			fprintf(stdout, "% 9ld\t 0x%02x     -> 0x%02x\n",
			    (long) (curoff + i), patternbuf[i] & 0xff,
			    diskbuf[i] & 0xff);
		}
	}
}

static off_t
szarg(char *n)
{
	register int shift = 0;
	register char *q = n;

	while (*q != (char) 0)
		q++;
	q--;

	if (*q == 'b' || *q == 'B')
		q--;

	if (*q == 'k' || *q == 'K') {
		shift = 10;
		*q = 0;
	} else if (*q == 'm' || *q == 'M') {
		shift = 20;
		*q = 0;
	} else if (*q == 'g' || *q == 'G') {
		shift = 30;
		*q = 0;
	}
	return ((off_t) strtol((const char *)n, (char **) NULL, 0) << shift);
}
/*
 * Local variables:
 * c-indent-level: 8
 * c-brace-imaginary-offset: 0
 * c-brace-offset: -8
 * c-argdecl-indent: 8
 * c-label-offset: -8
 * c-continued-statement-offset: 8
 * c-continued-brace-offset: 0
 * End:
 */



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.9908271110230.2537-100000>