Path: blob/main/lib/libc/tests/stdio/fmemopen2_test.c
39530 views
/*-1Copyright (C) 2013 Pietro Cerutti <[email protected]>23Redistribution and use in source and binary forms, with or without4modification, are permitted provided that the following conditions5are met:61. Redistributions of source code must retain the above copyright7notice, this list of conditions and the following disclaimer.82. Redistributions in binary form must reproduce the above copyright9notice, this list of conditions and the following disclaimer in the10documentation and/or other materials provided with the distribution.1112THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND13ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE16FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22SUCH DAMAGE.23*/2425/*26* Test basic FILE * functions (fread, fwrite, fseek, fclose) against27* a FILE * retrieved using fmemopen()28*/2930#include <errno.h>31#include <stdio.h>32#include <string.h>33#include <strings.h>3435#include <atf-c.h>3637ATF_TC_WITHOUT_HEAD(test_preexisting);38ATF_TC_BODY(test_preexisting, tc)39{40/* Use a pre-existing buffer. */41char buf[512];42char buf2[512];43char str[] = "Test writing some stuff";44char str2[] = "AAAAAAAAA";45char str3[] = "AAAA writing some stuff";46FILE *fp;47size_t nofw, nofr;48int rc;4950/* Open a FILE * using fmemopen. */51fp = fmemopen(buf, sizeof(buf), "w");52ATF_REQUIRE(fp != NULL);5354/* Write to the buffer. */55nofw = fwrite(str, 1, sizeof(str), fp);56ATF_REQUIRE(nofw == sizeof(str));5758/* Close the FILE *. */59rc = fclose(fp);60ATF_REQUIRE(rc == 0);6162/* Re-open the FILE * to read back the data. */63fp = fmemopen(buf, sizeof(buf), "r");64ATF_REQUIRE(fp != NULL);6566/* Read from the buffer. */67bzero(buf2, sizeof(buf2));68nofr = fread(buf2, 1, sizeof(buf2), fp);69ATF_REQUIRE(nofr == sizeof(buf2));7071/*72* Since a write on a FILE * retrieved by fmemopen73* will add a '\0' (if there's space), we can check74* the strings for equality.75*/76ATF_REQUIRE(strcmp(str, buf2) == 0);7778/* Close the FILE *. */79rc = fclose(fp);80ATF_REQUIRE(rc == 0);8182/* Now open a FILE * on the first 4 bytes of the string. */83fp = fmemopen(str, 4, "w");84ATF_REQUIRE(fp != NULL);8586/*87* Try to write more bytes than we shoud, we'll get a short count (4).88*/89nofw = fwrite(str2, 1, sizeof(str2), fp);90ATF_REQUIRE(nofw == 4);9192/* Close the FILE *. */93rc = fclose(fp);94ATF_REQUIRE(rc == 0);9596/* Check that the string was not modified after the first 4 bytes. */97ATF_REQUIRE(strcmp(str, str3) == 0);98}99100ATF_TC_WITHOUT_HEAD(test_autoalloc);101ATF_TC_BODY(test_autoalloc, tc)102{103/* Let fmemopen allocate the buffer. */104FILE *fp;105long pos;106size_t nofw, i;107int rc;108109/* Open a FILE * using fmemopen. */110fp = fmemopen(NULL, 512, "w+");111ATF_REQUIRE(fp != NULL);112113/* fill the buffer */114for (i = 0; i < 512; i++) {115nofw = fwrite("a", 1, 1, fp);116ATF_REQUIRE(nofw == 1);117}118119/* Get the current position into the stream. */120pos = ftell(fp);121ATF_REQUIRE(pos == 512);122123/* Try to write past the end, we should get a short object count (0) */124nofw = fwrite("a", 1, 1, fp);125ATF_REQUIRE(nofw == 0);126127/* Close the FILE *. */128rc = fclose(fp);129ATF_REQUIRE(rc == 0);130131/* Open a FILE * using a wrong mode */132fp = fmemopen(NULL, 512, "r");133ATF_REQUIRE(fp == NULL);134ATF_REQUIRE(errno == EINVAL);135136fp = fmemopen(NULL, 512, "w");137ATF_REQUIRE(fp == NULL);138ATF_REQUIRE(errno == EINVAL);139}140141ATF_TC_WITHOUT_HEAD(test_data_length);142ATF_TC_BODY(test_data_length, tc)143{144/*145* Here we test that a read operation doesn't go past the end of the146* data actually written, and that a SEEK_END seeks from the end of the147* data, not of the whole buffer.148*/149FILE *fp;150char buf[512] = {'\0'};151char str[] = "Test data length. ";152char str2[] = "Do we have two sentences?";153char str3[sizeof(str) + sizeof(str2) -1];154long pos;155size_t nofw, nofr;156int rc;157158/* Open a FILE * for updating our buffer. */159fp = fmemopen(buf, sizeof(buf), "w+");160ATF_REQUIRE(fp != NULL);161162/* Write our string into the buffer. */163nofw = fwrite(str, 1, sizeof(str), fp);164ATF_REQUIRE(nofw == sizeof(str));165166/* Now seek to the end and check that ftell gives us sizeof(str). */167rc = fseek(fp, 0, SEEK_END);168ATF_REQUIRE(rc == 0);169pos = ftell(fp);170ATF_REQUIRE(pos == sizeof(str));171172/* Close the FILE *. */173rc = fclose(fp);174ATF_REQUIRE(rc == 0);175176/* Reopen the buffer for appending. */177fp = fmemopen(buf, sizeof(buf), "a+");178ATF_REQUIRE(fp != NULL);179180/* We should now be writing after the first string. */181nofw = fwrite(str2, 1, sizeof(str2), fp);182ATF_REQUIRE(nofw == sizeof(str2));183184/* Rewind the FILE *. */185rc = fseek(fp, 0, SEEK_SET);186ATF_REQUIRE(rc == 0);187188/* Make sure we're at the beginning. */189pos = ftell(fp);190ATF_REQUIRE(pos == 0);191192/* Read the whole buffer. */193nofr = fread(str3, 1, sizeof(buf), fp);194ATF_REQUIRE(nofr == sizeof(str3));195196/* Make sure the two strings are there. */197ATF_REQUIRE(strncmp(str3, str, sizeof(str) - 1) == 0);198ATF_REQUIRE(strncmp(str3 + sizeof(str) - 1, str2, sizeof(str2)) == 0);199200/* Close the FILE *. */201rc = fclose(fp);202ATF_REQUIRE(rc == 0);203}204205ATF_TC_WITHOUT_HEAD(test_binary);206ATF_TC_BODY(test_binary, tc)207{208/*209* Make sure that NULL bytes are never appended when opening a buffer210* in binary mode.211*/212213FILE *fp;214char buf[20];215char str[] = "Test";216size_t nofw;217int rc, i;218219/* Pre-fill the buffer. */220memset(buf, 'A', sizeof(buf));221222/* Open a FILE * in binary mode. */223fp = fmemopen(buf, sizeof(buf), "w+b");224ATF_REQUIRE(fp != NULL);225226/* Write some data into it. */227nofw = fwrite(str, 1, strlen(str), fp);228ATF_REQUIRE(nofw == strlen(str));229230/* Make sure that the buffer doesn't contain any NULL bytes. */231for (i = 0; i < sizeof(buf); i++)232ATF_REQUIRE(buf[i] != '\0');233234/* Close the FILE *. */235rc = fclose(fp);236ATF_REQUIRE(rc == 0);237}238239ATF_TC_WITHOUT_HEAD(test_append_binary_pos);240ATF_TC_BODY(test_append_binary_pos, tc)241{242/*243* For compatibility with other implementations (glibc), we set the244* position to 0 when opening an automatically allocated binary stream245* for appending.246*/247248FILE *fp;249250fp = fmemopen(NULL, 16, "ab+");251ATF_REQUIRE(fp != NULL);252ATF_REQUIRE(ftell(fp) == 0L);253fclose(fp);254255/* Make sure that a pre-allocated buffer behaves correctly. */256char buf[] = "Hello";257fp = fmemopen(buf, sizeof(buf), "ab+");258ATF_REQUIRE(fp != NULL);259ATF_REQUIRE(ftell(fp) == strlen(buf));260fclose(fp);261}262263ATF_TC_WITHOUT_HEAD(test_size_0);264ATF_TC_BODY(test_size_0, tc)265{266/* POSIX mandates that we return EINVAL if size is 0. */267268FILE *fp;269270fp = fmemopen(NULL, 0, "r+");271ATF_REQUIRE(fp == NULL);272ATF_REQUIRE(errno == EINVAL);273}274275/*276* PR281953 - ensure we cannot write in read-only only mode, and cannot read in277* write-only mode.278*/279ATF_TC_WITHOUT_HEAD(test_rdonly_wronly);280ATF_TC_BODY(test_rdonly_wronly, tc)281{282FILE *fp;283char buf[16];284char buf_orig[16] = "input data";285char buf_write[16] = "write";286size_t sz;287288memcpy(buf, buf_orig, sizeof(buf));289fp = fmemopen(buf, sizeof(buf), "r");290ATF_REQUIRE(fp != NULL);291sz = fwrite(buf_write, 1, strlen(buf_write), fp);292ATF_REQUIRE(sz == 0);293ATF_REQUIRE(errno == EBADF);294ATF_REQUIRE(memcmp(buf, buf_orig, sizeof(buf)) == 0);295fclose(fp);296297fp = fmemopen(buf_orig, sizeof(buf), "w");298ATF_REQUIRE(fp != NULL);299sz = fread(buf, sizeof(buf), 1, fp);300ATF_REQUIRE(sz == 0);301ATF_REQUIRE(errno == EBADF);302fclose(fp);303}304305ATF_TP_ADD_TCS(tp)306{307308ATF_TP_ADD_TC(tp, test_autoalloc);309ATF_TP_ADD_TC(tp, test_preexisting);310ATF_TP_ADD_TC(tp, test_data_length);311ATF_TP_ADD_TC(tp, test_binary);312ATF_TP_ADD_TC(tp, test_append_binary_pos);313ATF_TP_ADD_TC(tp, test_size_0);314ATF_TP_ADD_TC(tp, test_rdonly_wronly);315316return (atf_no_error());317}318319320