Ejemplo n.º 1
0
static void test_partially_valid_iovec(int initial_file_offset)
{
	int i, fd;
	unsigned char buffer[BUFSIZE], fpattern[BUFSIZE], tmp[BUFSIZE];
	long off_after;
	struct iovec wr_iovec[] = {
		{ buffer, CHUNK },
		{ bad_addr, CHUNK },
		{ buffer + CHUNK, CHUNK },
		{ buffer + CHUNK * 2, CHUNK },
	};

	tst_res(TINFO, "starting test with initial file offset: %d ",
		initial_file_offset);

	for (i = 0; i < BUFSIZE; i++)
		buffer[i] = i % (CHUNK - 1);

	memset(fpattern, 0xff, BUFSIZE);
	tst_fill_file(TESTFILE, 0xff, CHUNK, BUFSIZE / CHUNK);

	fd = SAFE_OPEN(TESTFILE, O_RDWR, 0644);
	SAFE_LSEEK(fd, initial_file_offset, SEEK_SET);
	TEST(writev(fd, wr_iovec, ARRAY_SIZE(wr_iovec)));
	off_after = (long) SAFE_LSEEK(fd, 0, SEEK_CUR);

	/* bad errno */
	if (TEST_RETURN == -1 && TEST_ERRNO != EFAULT) {
		tst_res(TFAIL | TTERRNO, "unexpected errno");
		SAFE_CLOSE(fd);
		return;
	}

	/* nothing has been written */
	if (TEST_RETURN == -1 && TEST_ERRNO == EFAULT) {
		tst_res(TINFO, "got EFAULT");
		/* initial file content remains untouched */
		SAFE_LSEEK(fd, 0, SEEK_SET);
		SAFE_READ(1, fd, tmp, BUFSIZE);
		if (memcmp(tmp, fpattern, BUFSIZE))
			tst_res(TFAIL, "file was written to");
		else
			tst_res(TPASS, "file stayed untouched");

		/* offset hasn't changed */
		if (off_after == initial_file_offset)
			tst_res(TPASS, "offset stayed unchanged");
		else
			tst_res(TFAIL, "offset changed to %ld",
				off_after);

		SAFE_CLOSE(fd);
		return;
	}

	/* writev() wrote more bytes than bytes preceding invalid iovec */
	tst_res(TINFO, "writev() has written %ld bytes", TEST_RETURN);
	if (TEST_RETURN > (long) wr_iovec[0].iov_len) {
		tst_res(TFAIL, "writev wrote more than expected");
		SAFE_CLOSE(fd);
		return;
	}

	/* file content matches written bytes */
	SAFE_LSEEK(fd, initial_file_offset, SEEK_SET);
	SAFE_READ(1, fd, tmp, TEST_RETURN);
	if (memcmp(tmp, wr_iovec[0].iov_base, TEST_RETURN) == 0) {
		tst_res(TPASS, "file has expected content");
	} else {
		tst_res(TFAIL, "file has unexpected content");
		tst_res_hexd(TFAIL, wr_iovec[0].iov_base, TEST_RETURN,
				"expected:");
		tst_res_hexd(TFAIL, tmp, TEST_RETURN,
				"actual file content:");
	}

	/* file offset has been updated according to written bytes */
	if (off_after == initial_file_offset + TEST_RETURN)
		tst_res(TPASS, "offset at %ld as expected", off_after);
	else
		tst_res(TFAIL, "offset unexpected %ld", off_after);

	SAFE_CLOSE(fd);
}
Ejemplo n.º 2
0
static void do_test(void)
{
	char tmp[] = "Hello from tst_res_hexd";

	tst_res_hexd(TPASS, tmp, sizeof(tmp), "%s%d", "dump", 1);
}