diff options
author | gahr <gahr@FreeBSD.org> | 2013-01-31 16:39:50 +0000 |
---|---|---|
committer | gahr <gahr@FreeBSD.org> | 2013-01-31 16:39:50 +0000 |
commit | 3706909a8374ad86e702bde80ab176b0c1fcb9b7 (patch) | |
tree | 639be13fc132482e3750c6edb3e6b1fcfb09368b /tools/regression/lib | |
parent | 96dbcebea0dd37d6891a4dff6c621d8550f93769 (diff) | |
download | FreeBSD-src-3706909a8374ad86e702bde80ab176b0c1fcb9b7.zip FreeBSD-src-3706909a8374ad86e702bde80ab176b0c1fcb9b7.tar.gz |
- Remove underscores from the internal structure name, as it doesn't collide
with the user's namespace.
- Correct size and position variables type from long to size_t.
- Do not set errno to ENOMEM on malloc failure, as malloc already does so.
- Implement the concept of "buffer data length", which mandates what SEEK_END
refers to and the allowed extent for a read.
- Use NULL as read-callback if the buffer is opened in write-only mode.
Conversely, use NULL as write-callback when opened in read-only mode.
- Implement the handling of the ``b'' character in the mode argument. A binary
buffer differs from a text buffer (default mode if ``b'' is omitted) in that
NULL bytes are never appended to writes and that the "buffer data length"
equals to the size of the buffer.
- Remove shall from the man page. Use indicative instead. Also, specify that
the ``b'' flag does not conform with POSIX but is supported by glibc.
- Update the regression test so that the ``b'' functionality and the "buffer
data length" concepts are tested.
- Minor style(9) corrections.
Suggested by: jilles
Reviewed by: cognet
Approved by: cognet
Diffstat (limited to 'tools/regression/lib')
-rw-r--r-- | tools/regression/lib/libc/stdio/test-fmemopen.c | 163 |
1 files changed, 136 insertions, 27 deletions
diff --git a/tools/regression/lib/libc/stdio/test-fmemopen.c b/tools/regression/lib/libc/stdio/test-fmemopen.c index d9c1276..ca5f1a0 100644 --- a/tools/regression/lib/libc/stdio/test-fmemopen.c +++ b/tools/regression/lib/libc/stdio/test-fmemopen.c @@ -41,7 +41,7 @@ void test_preexisting () { /* - * use a pre-existing buffer + * Use a pre-existing buffer. */ char buf[512]; @@ -53,48 +53,52 @@ test_preexisting () size_t nofw, nofr; int rc; - /* open a FILE * using fmemopen */ - fp = fmemopen (buf, sizeof buf, "w"); + /* Open a FILE * using fmemopen. */ + fp = fmemopen (buf, sizeof(buf), "w"); assert (fp != NULL); - /* write to the buffer */ - nofw = fwrite (str, 1, sizeof str, fp); - assert (nofw == sizeof str); + /* Write to the buffer. */ + nofw = fwrite (str, 1, sizeof(str), fp); + assert (nofw == sizeof(str)); - /* close the FILE * */ + /* Close the FILE *. */ rc = fclose (fp); assert (rc == 0); - /* re-open the FILE * to read back the data */ - fp = fmemopen (buf, sizeof buf, "r"); + /* Re-open the FILE * to read back the data. */ + fp = fmemopen (buf, sizeof(buf), "r"); assert (fp != NULL); - /* read from the buffer */ - bzero (buf2, sizeof buf2); - nofr = fread (buf2, 1, sizeof buf2, fp); - assert (nofr == sizeof buf2); + /* Read from the buffer. */ + bzero (buf2, sizeof(buf2)); + nofr = fread (buf2, 1, sizeof(buf2), fp); + assert (nofr == sizeof(buf2)); - /* since a write on a FILE * retrieved by fmemopen + /* + * Since a write on a FILE * retrieved by fmemopen * will add a '\0' (if there's space), we can check - * the strings for equality */ + * the strings for equality. + */ assert (strcmp(str, buf2) == 0); - /* close the FILE * */ + /* Close the FILE *. */ rc = fclose (fp); assert (rc == 0); - /* now open a FILE * on the first 4 bytes of the string */ + /* Now open a FILE * on the first 4 bytes of the string. */ fp = fmemopen (str, 4, "w"); assert (fp != NULL); - /* try to write more bytes than we shoud, we'll get a short count (4) */ - nofw = fwrite (str2, 1, sizeof str2, fp); + /* + * Try to write more bytes than we shoud, we'll get a short count (4). + */ + nofw = fwrite (str2, 1, sizeof(str2), fp); assert (nofw == 4); - /* close the FILE * */ + /* Close the FILE *. */ rc = fclose (fp); - /* check that the string was not modified after the first 4 bytes */ + /* Check that the string was not modified after the first 4 bytes. */ assert (strcmp (str, str3) == 0); } @@ -102,7 +106,7 @@ void test_autoalloc () { /* - * let fmemopen allocate the buffer + * Let fmemopen allocate the buffer. */ char str[] = "A quick test"; @@ -111,8 +115,8 @@ test_autoalloc () size_t nofw, nofr, i; int rc; - /* open a FILE * using fmemopen */ - fp = fmemopen (NULL, 512, "w"); + /* Open a FILE * using fmemopen. */ + fp = fmemopen (NULL, 512, "w+"); assert (fp != NULL); /* fill the buffer */ @@ -121,15 +125,118 @@ test_autoalloc () assert (nofw == 1); } - /* get the current position into the stream */ + /* Get the current position into the stream. */ pos = ftell (fp); assert (pos == 512); - /* try to write past the end, we should get a short object count (0) */ + /* + * Try to write past the end, we should get a short object count (0) + */ nofw = fwrite ("a", 1, 1, fp); assert (nofw == 0); - /* close the FILE * */ + /* Close the FILE *. */ + rc = fclose (fp); + assert (rc == 0); +} + +void +test_data_length () +{ + /* + * 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. */ + fp = fmemopen (buf, sizeof(buf), "w+"); + assert (fp != NULL); + + /* Write our string into the buffer. */ + nofw = fwrite (str, 1, sizeof(str), fp); + assert (nofw == sizeof(str)); + + /* + * Now seek to the end and check that ftell + * gives us sizeof(str). + */ + rc = fseek (fp, 0, SEEK_END); + assert (rc == 0); + pos = ftell (fp); + assert (pos == sizeof(str)); + + /* Close the FILE *. */ + rc = fclose (fp); + assert (rc == 0); + + /* Reopen the buffer for appending. */ + fp = fmemopen (buf, sizeof(buf), "a+"); + assert (fp != NULL); + + /* We should now be writing after the first string. */ + nofw = fwrite (str2, 1, sizeof(str2), fp); + assert (nofw == sizeof(str2)); + + /* Rewind the FILE *. */ + rc = fseek (fp, 0, SEEK_SET); + assert (rc == 0); + + /* Make sure we're at the beginning. */ + pos = ftell (fp); + assert (pos == 0); + + /* Read the whole buffer. */ + nofr = fread (str3, 1, sizeof(buf), fp); + assert (nofr == sizeof(str3)); + + /* Make sure the two strings are there. */ + assert (strncmp (str3, str, sizeof(str) - 1) == 0); + assert (strncmp (str3 + sizeof(str) - 1, str2, sizeof(str2)) == 0); + + /* Close the FILE *. */ + rc = fclose (fp); + assert (rc == 0); +} + +void +test_binary () +{ + /* + * 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. */ + memset (buf, 'A', sizeof(buf)); + + /* Open a FILE * in binary mode. */ + fp = fmemopen (buf, sizeof(buf), "w+b"); + assert (fp != NULL); + + /* Write some data into it. */ + nofw = fwrite (str, 1, strlen(str), fp); + assert (nofw == strlen(str)); + + /* Make sure that the buffer doesn't contain any NULL bytes. */ + for (i = 0; i < sizeof(buf); i++) + assert (buf[i] != '\0'); + + /* Close the FILE *. */ rc = fclose (fp); assert (rc == 0); } @@ -139,5 +246,7 @@ main (void) { test_autoalloc (); test_preexisting (); + test_data_length (); + test_binary (); return (0); } |