Commit c04c8f40 authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Merge branch 'push-ntslpmkskzxq' into 'master'

tests: improve portability

See merge request !7
parents 2c3df223 065f09a7
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -465,7 +465,7 @@ int main (int argc, char** argv)
            std::shuffle(dirents.begin(), dirents.end(), g);
        }

        for (const auto dirent : dirents) {
        for (const auto &dirent : dirents) {
            st.st_ino = dirent.second;
            if (filler(buf, dirent.first.c_str(), &st, 0) != 0) {
                return -ENOMEM;
+0 −1
Original line number Diff line number Diff line
trap "Unmount 2>/dev/null" EXIT

Mount () {
	Unmount
	mkdir -p target
	../disorderfs -q "${@}" fixtures/ target/
}
+24 −45
Original line number Diff line number Diff line
@@ -9,26 +9,11 @@ trap "Unmount 2>/dev/null; rm -rf ${TEMPDIR}" EXIT
Setup () {
	cat >${TEMPDIR}/inodes.cpp <<EOF
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>

#include <map>
#include <iostream>

struct linux_dirent {
	unsigned long 	d_ino;
	off_t		d_off;
	unsigned short	d_reclen;
	char		d_name[];
};

#define BUF_SIZE 1024

void perror_and_die (const char* s)
{
	std::perror(s);
@@ -38,26 +23,21 @@ void perror_and_die (const char* s)
int
main(int argc, char *argv[])
{
	int				fd, nread, ret = EXIT_SUCCESS;
	char				buf[BUF_SIZE];
	int				ret = EXIT_SUCCESS;
	struct stat			st;
	const std::string		dirname = argc > 1 ? argv[1] : ".";
	struct linux_dirent		*d;
	std::map<ino_t, std::string>	inodes;
	DIR *dirp;
	struct dirent *d;

	if ((fd = open(dirname.c_str(), O_RDONLY | O_DIRECTORY)) == -1)
		perror_and_die("open");
	if ((dirp = opendir("target")) == NULL) perror_and_die("opendir");

	while ((nread = syscall(SYS_getdents, fd, buf, BUF_SIZE)) > 0) {
		for (int pos = 0; pos < nread; pos += d->d_reclen) {
			d = (struct linux_dirent *) (buf + pos);

			std::string filename = dirname + "/" + std::string(d->d_name);
	while ((d = readdir(dirp)) != NULL) {
		const std::string filename = "target/" + std::string(d->d_name);
		if (stat(filename.c_str(), &st) == -1)
			perror_and_die("stat");

		if (d->d_ino != st.st_ino) {
				std::cerr << filename << ": inode from getdents does not match stat: "
			std::cerr << filename << ": inode from readdir does not match stat: "
                                        << d->d_ino << " != " << st.st_ino << std::endl;
			ret = EXIT_FAILURE;
		}
@@ -70,18 +50,17 @@ main(int argc, char *argv[])
			ret = EXIT_FAILURE;
		}
	}
	}

	if (nread == -1)
		perror_and_die("open");
	if (closedir(dirp) == -1) perror_and_die("closedir");

	exit(ret);
	return ret;
}
EOF
	g++ -Wall -o${TEMPDIR}/inodes ${TEMPDIR}/inodes.cpp || Fail "Could not compile testcase"
	c++ -Wall -Werror -pedantic -o${TEMPDIR}/inodes ${TEMPDIR}/inodes.cpp || \
		Fail "Could not compile testcase"
}

Setup
Mount
${TEMPDIR}/inodes target || Fail "inodes"
${TEMPDIR}/inodes || Fail "inodes"
Unmount
+39 −18
Original line number Diff line number Diff line
@@ -5,32 +5,53 @@

Mount

EXPECTED_HUMAN="1970-05-23T21:21:18.000Z"
EXPECTED="12345678"
FILENAME="target/a"

# $1 = 'a' for access time OR 'm' for modified time
Expect() {
	cat > ${TEMPDIR}/test_touch.c <<EOF
#include <stdio.h>
#include <sys/stat.h>

int main(void) {
	struct stat st;

	if (stat("${FILENAME}", &st) == -1) {
		fprintf(stderr, "ERROR: could not stat '${FILENAME}'\n");
		return 1;
	}

	if (st.st_${1}tim.tv_sec != ${EXPECTED}) {
		fprintf(stderr, "ASSERTION FAILED: ${1}time result=%ld != expected=${EXPECTED}\n",
			st.st_${1}tim.tv_sec);
		return 1;
	}

	return 0;
}
EOF
	cc -Wall -Werror -pedantic -o ${TEMPDIR}/test_touch ${TEMPDIR}/test_touch.c || \
		Fail "Could not compile test_touch.c testcase"
	${TEMPDIR}/test_touch
}

TEMPDIR="$(mktemp -d -t test_touch.XXXXXXXXXX)"
trap "Unmount 2>/dev/null; rm -rf ${TEMPDIR}" EXIT

touch ${FILENAME}
touch -d @${EXPECTED} ${FILENAME}
RESULT="$(stat --format=%X-%Y ${FILENAME})"
if [ "${RESULT}" != "${EXPECTED}-${EXPECTED}" ]
then
	Fail "test1: Got=${RESULT} Expected=${EXPECTED}"
fi
touch -d ${EXPECTED_HUMAN} ${FILENAME}
Expect m || Fail "test1: mtime"
Expect a || Fail "test1: atime"

# This is what tar xf does for extracted files via futimens(2)
touch ${FILENAME}
touch -m -d @${EXPECTED} ${FILENAME}
RESULT="$(stat --format=%Y ${FILENAME})"
if [ "${RESULT}" != "${EXPECTED}" ]
then
	Fail "test2: Got=${RESULT} Expected=${EXPECTED}"
fi
touch -m -d ${EXPECTED_HUMAN} ${FILENAME}
Expect m || Fail "test2: mtime"

touch ${FILENAME}
touch -a -d @${EXPECTED} ${FILENAME}
RESULT="$(stat --format=%X ${FILENAME})"
if [ "${RESULT}" != "${EXPECTED}" ]
then
	Fail "test3: Got=${RESULT} Expected=${EXPECTED}"
fi
touch -a -d ${EXPECTED_HUMAN} ${FILENAME}
Expect a || Fail "test3: atime"

Unmount