Path: blob/main/lib/libc/tests/stdio/open_wmemstream_test.c
39530 views
/*-1* Copyright (c) 2013 Hudson River Trading LLC2* Written by: John H. Baldwin <[email protected]>3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <err.h>28#include <errno.h>29#include <limits.h>30#include <stdint.h>31#include <stdio.h>32#include <stdlib.h>33#include <string.h>34#include <wchar.h>3536#include <atf-c.h>3738static wchar_t *buf;39static size_t len;4041static void42assert_stream(const wchar_t *contents)43{44if (wcslen(contents) != len)45printf("bad length %zd for \"%ls\"\n", len, contents);46else if (wcsncmp(buf, contents, wcslen(contents)) != 0)47printf("bad buffer \"%ls\" for \"%ls\"\n", buf, contents);48}4950ATF_TC_WITHOUT_HEAD(open_group_test);51ATF_TC_BODY(open_group_test, tc)52{53FILE *fp;54off_t eob;5556fp = open_wmemstream(&buf, &len);57ATF_REQUIRE_MSG(fp != NULL, "open_wmemstream failed");5859fwprintf(fp, L"hello my world");60fflush(fp);61assert_stream(L"hello my world");62eob = ftello(fp);63rewind(fp);64fwprintf(fp, L"good-bye");65fseeko(fp, eob, SEEK_SET);66fclose(fp);67assert_stream(L"good-bye world");68free(buf);69}7071ATF_TC_WITHOUT_HEAD(simple_tests);72ATF_TC_BODY(simple_tests, tc)73{74static const wchar_t zerobuf[] =75{ L'f', L'o', L'o', 0, 0, 0, 0, L'b', L'a', L'r', 0 };76wchar_t c;77FILE *fp;7879fp = open_wmemstream(&buf, NULL);80ATF_REQUIRE_MSG(fp == NULL, "open_wmemstream did not fail");81ATF_REQUIRE_MSG(errno == EINVAL,82"open_wmemstream didn't fail with EINVAL");83fp = open_wmemstream(NULL, &len);84ATF_REQUIRE_MSG(fp == NULL, "open_wmemstream did not fail");85ATF_REQUIRE_MSG(errno == EINVAL,86"open_wmemstream didn't fail with EINVAL");87fp = open_wmemstream(&buf, &len);88ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed; errno=%d", errno);89fflush(fp);90assert_stream(L"");91if (fwide(fp, 0) <= 0)92printf("stream is not wide-oriented\n");9394fwprintf(fp, L"fo");95fflush(fp);96assert_stream(L"fo");97fputwc(L'o', fp);98fflush(fp);99assert_stream(L"foo");100rewind(fp);101fflush(fp);102assert_stream(L"");103fseek(fp, 0, SEEK_END);104fflush(fp);105assert_stream(L"foo");106107/*108* Test seeking out past the current end. Should zero-fill the109* intermediate area.110*/111fseek(fp, 4, SEEK_END);112fwprintf(fp, L"bar");113fflush(fp);114115/*116* Can't use assert_stream() here since this should contain117* embedded null characters.118*/119if (len != 10)120printf("bad length %zd for zero-fill test\n", len);121else if (memcmp(buf, zerobuf, sizeof(zerobuf)) != 0)122printf("bad buffer for zero-fill test\n");123124fseek(fp, 3, SEEK_SET);125fwprintf(fp, L" in ");126fflush(fp);127assert_stream(L"foo in ");128fseek(fp, 0, SEEK_END);129fflush(fp);130assert_stream(L"foo in bar");131132rewind(fp);133if (fread(&c, sizeof(c), 1, fp) != 0)134printf("fread did not fail\n");135else if (!ferror(fp))136printf("error indicator not set after fread\n");137else138clearerr(fp);139140fseek(fp, 4, SEEK_SET);141fwprintf(fp, L"bar baz");142fclose(fp);143assert_stream(L"foo bar baz");144free(buf);145}146147ATF_TC_WITHOUT_HEAD(seek_tests);148ATF_TC_BODY(seek_tests, tc)149{150FILE *fp;151152fp = open_wmemstream(&buf, &len);153ATF_REQUIRE_MSG(fp != NULL, "open_wmemstream failed; errno=%d", errno);154155#define SEEK_FAIL(offset, whence, error) do { \156errno = 0; \157ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) != 0, \158"fseeko(%s, %s) did not fail, set pos to %jd", \159__STRING(offset), __STRING(whence), \160(intmax_t)ftello(fp)); \161ATF_REQUIRE_MSG(errno == (error), \162"fseeko(%s, %s) failed with %d rather than %s", \163__STRING(offset), __STRING(whence), errno, \164__STRING(error)); \165} while (0)166167#define SEEK_OK(offset, whence, result) do { \168ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) == 0, \169"fseeko(%s, %s) failed: %s", \170__STRING(offset), __STRING(whence), strerror(errno)); \171ATF_REQUIRE_MSG(ftello(fp) == (result), \172"fseeko(%s, %s) seeked to %jd rather than %s", \173__STRING(offset), __STRING(whence), \174(intmax_t)ftello(fp), __STRING(result)); \175} while (0)176177SEEK_FAIL(-1, SEEK_SET, EINVAL);178SEEK_FAIL(-1, SEEK_CUR, EINVAL);179SEEK_FAIL(-1, SEEK_END, EINVAL);180fwprintf(fp, L"foo");181SEEK_OK(-1, SEEK_CUR, 2);182SEEK_OK(0, SEEK_SET, 0);183SEEK_OK(-1, SEEK_END, 2);184SEEK_OK(OFF_MAX - 1, SEEK_SET, OFF_MAX - 1);185SEEK_FAIL(2, SEEK_CUR, EOVERFLOW);186fclose(fp);187}188189ATF_TP_ADD_TCS(tp)190{191192ATF_TP_ADD_TC(tp, open_group_test);193ATF_TP_ADD_TC(tp, simple_tests);194ATF_TP_ADD_TC(tp, seek_tests);195196return (atf_no_error());197}198199200