1247411Sjhb/*- 2281887Sjhb * Copyright (c) 2013 Hudson River Trading LLC 3247411Sjhb * Written by: John H. Baldwin <jhb@FreeBSD.org> 4247411Sjhb * All rights reserved. 5247411Sjhb * 6247411Sjhb * Redistribution and use in source and binary forms, with or without 7247411Sjhb * modification, are permitted provided that the following conditions 8247411Sjhb * are met: 9247411Sjhb * 1. Redistributions of source code must retain the above copyright 10247411Sjhb * notice, this list of conditions and the following disclaimer. 11247411Sjhb * 2. Redistributions in binary form must reproduce the above copyright 12247411Sjhb * notice, this list of conditions and the following disclaimer in the 13247411Sjhb * documentation and/or other materials provided with the distribution. 14247411Sjhb * 15247411Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16247411Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17247411Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18247411Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19247411Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20247411Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21247411Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22247411Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23247411Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24247411Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25247411Sjhb * SUCH DAMAGE. 26247411Sjhb */ 27247411Sjhb 28247411Sjhb#include <sys/cdefs.h> 29247411Sjhb__FBSDID("$FreeBSD: stable/11/lib/libc/tests/stdio/open_memstream2_test.c 309468 2016-12-03 02:48:55Z ngie $"); 30247411Sjhb 31247411Sjhb#include <err.h> 32247411Sjhb#include <errno.h> 33247411Sjhb#include <limits.h> 34247411Sjhb#include <stdint.h> 35247411Sjhb#include <stdio.h> 36247411Sjhb#include <stdlib.h> 37247411Sjhb#include <string.h> 38247411Sjhb#include <wchar.h> 39247411Sjhb 40290537Sngie#include <atf-c.h> 41290537Sngie 42247411Sjhbstatic char *buf; 43247411Sjhbstatic size_t len; 44247411Sjhb 45247411Sjhbstatic void 46247411Sjhbassert_stream(const char *contents) 47247411Sjhb{ 48247411Sjhb if (strlen(contents) != len) 49247411Sjhb printf("bad length %zd for \"%s\"\n", len, contents); 50247411Sjhb else if (strncmp(buf, contents, strlen(contents)) != 0) 51247411Sjhb printf("bad buffer \"%s\" for \"%s\"\n", buf, contents); 52247411Sjhb} 53247411Sjhb 54290537SngieATF_TC_WITHOUT_HEAD(open_group_test); 55290537SngieATF_TC_BODY(open_group_test, tc) 56247411Sjhb{ 57247411Sjhb FILE *fp; 58247411Sjhb off_t eob; 59247411Sjhb 60247411Sjhb fp = open_memstream(&buf, &len); 61290537Sngie ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed"); 62247411Sjhb 63247411Sjhb fprintf(fp, "hello my world"); 64247411Sjhb fflush(fp); 65247411Sjhb assert_stream("hello my world"); 66247411Sjhb eob = ftello(fp); 67247411Sjhb rewind(fp); 68247411Sjhb fprintf(fp, "good-bye"); 69247411Sjhb fseeko(fp, eob, SEEK_SET); 70247411Sjhb fclose(fp); 71247411Sjhb assert_stream("good-bye world"); 72247411Sjhb free(buf); 73247411Sjhb} 74247411Sjhb 75290537SngieATF_TC_WITHOUT_HEAD(simple_tests); 76290537SngieATF_TC_BODY(simple_tests, tc) 77247411Sjhb{ 78247411Sjhb static const char zerobuf[] = 79247411Sjhb { 'f', 'o', 'o', 0, 0, 0, 0, 'b', 'a', 'r', 0 }; 80247411Sjhb char c; 81247411Sjhb FILE *fp; 82247411Sjhb 83247411Sjhb fp = open_memstream(&buf, NULL); 84290537Sngie ATF_REQUIRE_MSG(fp == NULL, "open_memstream did not fail"); 85290537Sngie ATF_REQUIRE_MSG(errno == EINVAL, 86290537Sngie "open_memstream didn't fail with EINVAL"); 87247411Sjhb fp = open_memstream(NULL, &len); 88290537Sngie ATF_REQUIRE_MSG(fp == NULL, "open_memstream did not fail"); 89290537Sngie ATF_REQUIRE_MSG(errno == EINVAL, 90290537Sngie "open_memstream didn't fail with EINVAL"); 91247411Sjhb fp = open_memstream(&buf, &len); 92290537Sngie ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed; errno=%d", errno); 93247411Sjhb fflush(fp); 94247411Sjhb assert_stream(""); 95247411Sjhb if (fwide(fp, 0) >= 0) 96247411Sjhb printf("stream is not byte-oriented\n"); 97247411Sjhb 98247411Sjhb fprintf(fp, "fo"); 99247411Sjhb fflush(fp); 100247411Sjhb assert_stream("fo"); 101247411Sjhb fputc('o', fp); 102247411Sjhb fflush(fp); 103247411Sjhb assert_stream("foo"); 104247411Sjhb rewind(fp); 105247411Sjhb fflush(fp); 106247411Sjhb assert_stream(""); 107247411Sjhb fseek(fp, 0, SEEK_END); 108247411Sjhb fflush(fp); 109247411Sjhb assert_stream("foo"); 110247411Sjhb 111247411Sjhb /* 112247411Sjhb * Test seeking out past the current end. Should zero-fill the 113247411Sjhb * intermediate area. 114247411Sjhb */ 115247411Sjhb fseek(fp, 4, SEEK_END); 116247411Sjhb fprintf(fp, "bar"); 117247411Sjhb fflush(fp); 118247411Sjhb 119247411Sjhb /* 120247411Sjhb * Can't use assert_stream() here since this should contain 121247411Sjhb * embedded null characters. 122247411Sjhb */ 123247411Sjhb if (len != 10) 124247411Sjhb printf("bad length %zd for zero-fill test\n", len); 125247411Sjhb else if (memcmp(buf, zerobuf, sizeof(zerobuf)) != 0) 126247411Sjhb printf("bad buffer for zero-fill test\n"); 127247411Sjhb 128247411Sjhb fseek(fp, 3, SEEK_SET); 129247411Sjhb fprintf(fp, " in "); 130247411Sjhb fflush(fp); 131247411Sjhb assert_stream("foo in "); 132247411Sjhb fseek(fp, 0, SEEK_END); 133247411Sjhb fflush(fp); 134247411Sjhb assert_stream("foo in bar"); 135247411Sjhb 136247411Sjhb rewind(fp); 137247411Sjhb if (fread(&c, sizeof(c), 1, fp) != 0) 138247411Sjhb printf("fread did not fail\n"); 139247411Sjhb else if (!ferror(fp)) 140247411Sjhb printf("error indicator not set after fread\n"); 141247411Sjhb else 142247411Sjhb clearerr(fp); 143247411Sjhb 144247411Sjhb fseek(fp, 4, SEEK_SET); 145247411Sjhb fprintf(fp, "bar baz"); 146247411Sjhb fclose(fp); 147247411Sjhb assert_stream("foo bar baz"); 148247411Sjhb free(buf); 149247411Sjhb} 150247411Sjhb 151290537SngieATF_TC_WITHOUT_HEAD(seek_tests); 152290537SngieATF_TC_BODY(seek_tests, tc) 153247411Sjhb{ 154247411Sjhb FILE *fp; 155247411Sjhb 156247411Sjhb fp = open_memstream(&buf, &len); 157290537Sngie ATF_REQUIRE_MSG(fp != NULL, "open_memstream failed: %d", errno); 158290537Sngie 159290537Sngie#define SEEK_FAIL(offset, whence, error) do { \ 160290537Sngie errno = 0; \ 161290537Sngie ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) != 0, \ 162309468Sngie "fseeko(%s, %s) did not fail, set pos to %jd", \ 163290537Sngie __STRING(offset), __STRING(whence), \ 164290537Sngie (intmax_t)ftello(fp)); \ 165290537Sngie ATF_REQUIRE_MSG(errno == (error), \ 166309468Sngie "fseeko(%s, %s) failed with %d rather than %s", \ 167290537Sngie __STRING(offset), __STRING(whence), errno, \ 168290537Sngie __STRING(error)); \ 169247411Sjhb} while (0) 170247411Sjhb 171290537Sngie#define SEEK_OK(offset, whence, result) do { \ 172290537Sngie ATF_REQUIRE_MSG(fseeko(fp, (offset), (whence)) == 0, \ 173290537Sngie "fseeko(%s, %s) failed: %s", \ 174290537Sngie __STRING(offset), __STRING(whence), strerror(errno)); \ 175290537Sngie ATF_REQUIRE_MSG(ftello(fp) == (result), \ 176309468Sngie "fseeko(%s, %s) seeked to %jd rather than %s", \ 177290537Sngie __STRING(offset), __STRING(whence), \ 178290537Sngie (intmax_t)ftello(fp), __STRING(result)); \ 179247411Sjhb} while (0) 180247411Sjhb 181247411Sjhb SEEK_FAIL(-1, SEEK_SET, EINVAL); 182247411Sjhb SEEK_FAIL(-1, SEEK_CUR, EINVAL); 183247411Sjhb SEEK_FAIL(-1, SEEK_END, EINVAL); 184247411Sjhb fprintf(fp, "foo"); 185247411Sjhb SEEK_OK(-1, SEEK_CUR, 2); 186247411Sjhb SEEK_OK(0, SEEK_SET, 0); 187247411Sjhb SEEK_OK(-1, SEEK_END, 2); 188247411Sjhb SEEK_OK(OFF_MAX - 1, SEEK_SET, OFF_MAX - 1); 189247411Sjhb SEEK_FAIL(2, SEEK_CUR, EOVERFLOW); 190247411Sjhb fclose(fp); 191247411Sjhb} 192247411Sjhb 193290537SngieATF_TP_ADD_TCS(tp) 194247411Sjhb{ 195247411Sjhb 196290537Sngie ATF_TP_ADD_TC(tp, open_group_test); 197290537Sngie ATF_TP_ADD_TC(tp, simple_tests); 198290537Sngie ATF_TP_ADD_TC(tp, seek_tests); 199290537Sngie 200290537Sngie return (atf_no_error()); 201247411Sjhb} 202