2013-01-30 14:59:26 +00:00
|
|
|
/*-
|
|
|
|
Copyright (C) 2013 Pietro Cerutti <gahr@FreeBSD.org>
|
|
|
|
|
|
|
|
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.
|
|
|
|
2. 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.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY 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 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test basic FILE * functions (fread, fwrite, fseek, fclose) against
|
|
|
|
* a FILE * retrieved using fmemopen()
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
2015-11-08 06:37:50 +00:00
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
#include <atf-c.h>
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_TC_WITHOUT_HEAD(test_preexisting);
|
|
|
|
ATF_TC_BODY(test_preexisting, tc)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2015-11-08 06:37:50 +00:00
|
|
|
/* Use a pre-existing buffer. */
|
2013-01-30 14:59:26 +00:00
|
|
|
char buf[512];
|
|
|
|
char buf2[512];
|
|
|
|
char str[] = "Test writing some stuff";
|
|
|
|
char str2[] = "AAAAAAAAA";
|
|
|
|
char str3[] = "AAAA writing some stuff";
|
|
|
|
FILE *fp;
|
|
|
|
size_t nofw, nofr;
|
|
|
|
int rc;
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Open a FILE * using fmemopen. */
|
2013-02-01 13:04:06 +00:00
|
|
|
fp = fmemopen(buf, sizeof(buf), "w");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp != NULL);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Write to the buffer. */
|
2013-02-01 13:04:06 +00:00
|
|
|
nofw = fwrite(str, 1, sizeof(str), fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofw == sizeof(str));
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Close the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fclose(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Re-open the FILE * to read back the data. */
|
2013-02-01 13:04:06 +00:00
|
|
|
fp = fmemopen(buf, sizeof(buf), "r");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp != NULL);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Read from the buffer. */
|
2013-02-01 13:04:06 +00:00
|
|
|
bzero(buf2, sizeof(buf2));
|
|
|
|
nofr = fread(buf2, 1, sizeof(buf2), fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofr == sizeof(buf2));
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2015-11-08 06:37:50 +00:00
|
|
|
/*
|
2013-01-31 16:39:50 +00:00
|
|
|
* Since a write on a FILE * retrieved by fmemopen
|
2013-01-30 14:59:26 +00:00
|
|
|
* will add a '\0' (if there's space), we can check
|
2013-01-31 16:39:50 +00:00
|
|
|
* the strings for equality.
|
|
|
|
*/
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(strcmp(str, buf2) == 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Close the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fclose(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Now open a FILE * on the first 4 bytes of the string. */
|
2013-02-01 13:04:06 +00:00
|
|
|
fp = fmemopen(str, 4, "w");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp != NULL);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/*
|
|
|
|
* Try to write more bytes than we shoud, we'll get a short count (4).
|
|
|
|
*/
|
2013-02-01 13:04:06 +00:00
|
|
|
nofw = fwrite(str2, 1, sizeof(str2), fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofw == 4);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Close the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fclose(fp);
|
2015-09-29 17:54:28 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Check that the string was not modified after the first 4 bytes. */
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(strcmp(str, str3) == 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_TC_WITHOUT_HEAD(test_autoalloc);
|
|
|
|
ATF_TC_BODY(test_autoalloc, tc)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2015-11-08 06:37:50 +00:00
|
|
|
/* Let fmemopen allocate the buffer. */
|
2013-01-30 14:59:26 +00:00
|
|
|
FILE *fp;
|
|
|
|
long pos;
|
2015-11-15 12:48:42 +00:00
|
|
|
size_t nofw, i;
|
2013-01-30 14:59:26 +00:00
|
|
|
int rc;
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Open a FILE * using fmemopen. */
|
2013-02-01 13:04:06 +00:00
|
|
|
fp = fmemopen(NULL, 512, "w+");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp != NULL);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
|
|
|
/* fill the buffer */
|
|
|
|
for (i = 0; i < 512; i++) {
|
2013-02-01 13:04:06 +00:00
|
|
|
nofw = fwrite("a", 1, 1, fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofw == 1);
|
2013-01-30 14:59:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Get the current position into the stream. */
|
2013-02-01 13:04:06 +00:00
|
|
|
pos = ftell(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(pos == 512);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2015-11-08 06:37:50 +00:00
|
|
|
/* Try to write past the end, we should get a short object count (0) */
|
2013-02-01 13:04:06 +00:00
|
|
|
nofw = fwrite("a", 1, 1, fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofw == 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
|
2013-01-31 16:39:50 +00:00
|
|
|
/* Close the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fclose(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2014-06-02 13:48:57 +00:00
|
|
|
|
|
|
|
/* Open a FILE * using a wrong mode */
|
|
|
|
fp = fmemopen(NULL, 512, "r");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp == NULL);
|
2014-06-02 13:48:57 +00:00
|
|
|
|
|
|
|
fp = fmemopen(NULL, 512, "w");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp == NULL);
|
2013-01-31 16:39:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_TC_WITHOUT_HEAD(test_data_length);
|
|
|
|
ATF_TC_BODY(test_data_length, tc)
|
2013-01-31 16:39:50 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Here we test that a read operation doesn't go past the end of the
|
|
|
|
* data actually written, and that a SEEK_END seeks from the end of the
|
|
|
|
* data, not of the whole buffer.
|
|
|
|
*/
|
|
|
|
FILE *fp;
|
|
|
|
char buf[512] = {'\0'};
|
|
|
|
char str[] = "Test data length. ";
|
|
|
|
char str2[] = "Do we have two sentences?";
|
|
|
|
char str3[sizeof(str) + sizeof(str2) -1];
|
|
|
|
long pos;
|
|
|
|
size_t nofw, nofr;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Open a FILE * for updating our buffer. */
|
2013-02-01 13:04:06 +00:00
|
|
|
fp = fmemopen(buf, sizeof(buf), "w+");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp != NULL);
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Write our string into the buffer. */
|
2013-02-01 13:04:06 +00:00
|
|
|
nofw = fwrite(str, 1, sizeof(str), fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofw == sizeof(str));
|
2013-01-31 16:39:50 +00:00
|
|
|
|
2015-11-08 06:37:50 +00:00
|
|
|
/* Now seek to the end and check that ftell gives us sizeof(str). */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fseek(fp, 0, SEEK_END);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-02-01 13:04:06 +00:00
|
|
|
pos = ftell(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(pos == sizeof(str));
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Close the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fclose(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Reopen the buffer for appending. */
|
2013-02-01 13:04:06 +00:00
|
|
|
fp = fmemopen(buf, sizeof(buf), "a+");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp != NULL);
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* We should now be writing after the first string. */
|
2013-02-01 13:04:06 +00:00
|
|
|
nofw = fwrite(str2, 1, sizeof(str2), fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofw == sizeof(str2));
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Rewind the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fseek(fp, 0, SEEK_SET);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Make sure we're at the beginning. */
|
2013-02-01 13:04:06 +00:00
|
|
|
pos = ftell(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(pos == 0);
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Read the whole buffer. */
|
2013-02-01 13:04:06 +00:00
|
|
|
nofr = fread(str3, 1, sizeof(buf), fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofr == sizeof(str3));
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Make sure the two strings are there. */
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(strncmp(str3, str, sizeof(str) - 1) == 0);
|
|
|
|
ATF_REQUIRE(strncmp(str3 + sizeof(str) - 1, str2, sizeof(str2)) == 0);
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Close the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fclose(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-01-31 16:39:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_TC_WITHOUT_HEAD(test_binary);
|
|
|
|
ATF_TC_BODY(test_binary, tc)
|
2013-01-31 16:39:50 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Make sure that NULL bytes are never appended when opening a buffer
|
|
|
|
* in binary mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
char buf[20];
|
|
|
|
char str[] = "Test";
|
|
|
|
size_t nofw;
|
|
|
|
int rc, i;
|
|
|
|
|
|
|
|
/* Pre-fill the buffer. */
|
2013-02-01 13:04:06 +00:00
|
|
|
memset(buf, 'A', sizeof(buf));
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Open a FILE * in binary mode. */
|
2013-02-01 13:04:06 +00:00
|
|
|
fp = fmemopen(buf, sizeof(buf), "w+b");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp != NULL);
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Write some data into it. */
|
2013-02-01 13:04:06 +00:00
|
|
|
nofw = fwrite(str, 1, strlen(str), fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(nofw == strlen(str));
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Make sure that the buffer doesn't contain any NULL bytes. */
|
|
|
|
for (i = 0; i < sizeof(buf); i++)
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(buf[i] != '\0');
|
2013-01-31 16:39:50 +00:00
|
|
|
|
|
|
|
/* Close the FILE *. */
|
2013-02-01 13:04:06 +00:00
|
|
|
rc = fclose(fp);
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(rc == 0);
|
2013-01-30 14:59:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_TC_WITHOUT_HEAD(test_append_binary_pos);
|
|
|
|
ATF_TC_BODY(test_append_binary_pos, tc)
|
2014-06-02 13:48:57 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For compatibility with other implementations (glibc), we set the
|
|
|
|
* position to 0 when opening an automatically allocated binary stream
|
|
|
|
* for appending.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fmemopen(NULL, 16, "ab+");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(ftell(fp) == 0L);
|
2014-06-02 13:48:57 +00:00
|
|
|
fclose(fp);
|
|
|
|
|
2015-11-08 06:37:50 +00:00
|
|
|
/* Make sure that a pre-allocated buffer behaves correctly. */
|
2014-06-02 13:48:57 +00:00
|
|
|
char buf[] = "Hello";
|
|
|
|
fp = fmemopen(buf, sizeof(buf), "ab+");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(ftell(fp) == strlen(buf));
|
2014-06-02 13:48:57 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_TC_WITHOUT_HEAD(test_size_0);
|
|
|
|
ATF_TC_BODY(test_size_0, tc)
|
2014-06-02 13:48:57 +00:00
|
|
|
{
|
2015-11-08 06:37:50 +00:00
|
|
|
/* POSIX mandates that we return EINVAL if size is 0. */
|
2014-06-02 13:48:57 +00:00
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fmemopen(NULL, 0, "r+");
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_REQUIRE(fp == NULL);
|
|
|
|
ATF_REQUIRE(errno == EINVAL);
|
2014-06-02 13:48:57 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 19:57:47 +00:00
|
|
|
ATF_TP_ADD_TCS(tp)
|
2013-01-30 14:59:26 +00:00
|
|
|
{
|
2014-11-16 19:57:47 +00:00
|
|
|
|
|
|
|
ATF_TP_ADD_TC(tp, test_autoalloc);
|
|
|
|
ATF_TP_ADD_TC(tp, test_preexisting);
|
|
|
|
ATF_TP_ADD_TC(tp, test_data_length);
|
|
|
|
ATF_TP_ADD_TC(tp, test_binary);
|
|
|
|
ATF_TP_ADD_TC(tp, test_append_binary_pos);
|
|
|
|
ATF_TP_ADD_TC(tp, test_size_0);
|
|
|
|
|
|
|
|
return (atf_no_error());
|
2013-01-30 14:59:26 +00:00
|
|
|
}
|