1*84d9c625SLionel Sambuc /* $NetBSD: t_fmemopen.c,v 1.4 2013/10/19 17:45:00 christos Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c)2010 Takehiko NOZAKI,
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1711be35a1SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1811be35a1SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1911be35a1SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2011be35a1SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2111be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2211be35a1SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2311be35a1SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2411be35a1SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2511be35a1SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2611be35a1SLionel Sambuc * SUCH DAMAGE.
2711be35a1SLionel Sambuc *
2811be35a1SLionel Sambuc */
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc #if defined(__NetBSD__)
3111be35a1SLionel Sambuc #include <atf-c.h>
3211be35a1SLionel Sambuc #else
3311be35a1SLionel Sambuc #if defined(__linux__)
3411be35a1SLionel Sambuc #define _GNU_SOURCE
3511be35a1SLionel Sambuc #include <features.h>
3611be35a1SLionel Sambuc #endif
3711be35a1SLionel Sambuc #include <assert.h>
3811be35a1SLionel Sambuc #include <stdio.h>
3911be35a1SLionel Sambuc #define ATF_TC(arg0) static void arg0##_head(void)
4011be35a1SLionel Sambuc #define ATF_TC_HEAD(arg0, arg1) static void arg0##_head()
4111be35a1SLionel Sambuc #define atf_tc_set_md_var(arg0, arg1, ...) do { \
4211be35a1SLionel Sambuc printf(__VA_ARGS__); \
4311be35a1SLionel Sambuc puts(""); \
4411be35a1SLionel Sambuc } while (/*CONSTCOND*/0)
4511be35a1SLionel Sambuc #define ATF_TC_BODY(arg0, arg1) static void arg0##_body()
4611be35a1SLionel Sambuc #define ATF_CHECK(arg0) assert(arg0)
4711be35a1SLionel Sambuc #define ATF_TP_ADD_TCS(arg0) int main(void)
4811be35a1SLionel Sambuc #define ATF_TP_ADD_TC(arg0, arg1) arg1##_head(); arg1##_body()
4911be35a1SLionel Sambuc #define atf_no_error() 0
5011be35a1SLionel Sambuc #endif
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc #include <errno.h>
5311be35a1SLionel Sambuc #include <stdint.h>
5411be35a1SLionel Sambuc #include <stdio.h>
5511be35a1SLionel Sambuc #include <limits.h>
5611be35a1SLionel Sambuc #include <stdlib.h>
5711be35a1SLionel Sambuc #include <string.h>
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc const char *mode_rwa[] = {
6011be35a1SLionel Sambuc "r", "rb", "r+", "rb+", "r+b",
6111be35a1SLionel Sambuc "w", "wb", "w+", "wb+", "w+b",
6211be35a1SLionel Sambuc "a", "ab", "a+", "ab+", "a+b",
6311be35a1SLionel Sambuc NULL
6411be35a1SLionel Sambuc };
6511be35a1SLionel Sambuc
6611be35a1SLionel Sambuc const char *mode_r[] = { "r", "rb", "r+", "rb+", "r+b", NULL };
6711be35a1SLionel Sambuc const char *mode_w[] = { "w", "wb", "w+", "wb+", "w+b", NULL };
6811be35a1SLionel Sambuc const char *mode_a[] = { "a", "ab", "a+", "ab+", "a+b", NULL };
6911be35a1SLionel Sambuc
7011be35a1SLionel Sambuc struct testcase {
7111be35a1SLionel Sambuc const char *s;
7211be35a1SLionel Sambuc off_t n;
7311be35a1SLionel Sambuc } testcases[] = {
7411be35a1SLionel Sambuc #define TESTSTR(s) { s, sizeof(s)-1 }
7511be35a1SLionel Sambuc TESTSTR("\0he quick brown fox jumps over the lazy dog"),
7611be35a1SLionel Sambuc TESTSTR("T\0e quick brown fox jumps over the lazy dog"),
7711be35a1SLionel Sambuc TESTSTR("Th\0 quick brown fox jumps over the lazy dog"),
7811be35a1SLionel Sambuc TESTSTR("The\0quick brown fox jumps over the lazy dog"),
7911be35a1SLionel Sambuc TESTSTR("The \0uick brown fox jumps over the lazy dog"),
8011be35a1SLionel Sambuc TESTSTR("The q\0ick brown fox jumps over the lazy dog"),
8111be35a1SLionel Sambuc TESTSTR("The qu\0ck brown fox jumps over the lazy dog"),
8211be35a1SLionel Sambuc TESTSTR("The qui\0k brown fox jumps over the lazy dog"),
8311be35a1SLionel Sambuc TESTSTR("The quic\0 brown fox jumps over the lazy dog"),
8411be35a1SLionel Sambuc TESTSTR("The quick\0brown fox jumps over the lazy dog"),
8511be35a1SLionel Sambuc TESTSTR("The quick \0rown fox jumps over the lazy dog"),
8611be35a1SLionel Sambuc TESTSTR("The quick b\0own fox jumps over the lazy dog"),
8711be35a1SLionel Sambuc TESTSTR("The quick br\0wn fox jumps over the lazy dog"),
8811be35a1SLionel Sambuc TESTSTR("The quick bro\0n fox jumps over the lazy dog"),
8911be35a1SLionel Sambuc TESTSTR("The quick brow\0 fox jumps over the lazy dog"),
9011be35a1SLionel Sambuc TESTSTR("The quick brown\0fox jumps over the lazy dog"),
9111be35a1SLionel Sambuc TESTSTR("The quick brown \0ox jumps over the lazy dog"),
9211be35a1SLionel Sambuc TESTSTR("The quick brown f\0x jumps over the lazy dog"),
9311be35a1SLionel Sambuc TESTSTR("The quick brown fo\0 jumps over the lazy dog"),
9411be35a1SLionel Sambuc TESTSTR("The quick brown fox\0jumps over the lazy dog"),
9511be35a1SLionel Sambuc TESTSTR("The quick brown fox \0umps over the lazy dog"),
9611be35a1SLionel Sambuc TESTSTR("The quick brown fox j\0mps over the lazy dog"),
9711be35a1SLionel Sambuc TESTSTR("The quick brown fox ju\0ps over the lazy dog"),
9811be35a1SLionel Sambuc TESTSTR("The quick brown fox jum\0s over the lazy dog"),
9911be35a1SLionel Sambuc TESTSTR("The quick brown fox jump\0 over the lazy dog"),
10011be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps\0over the lazy dog"),
10111be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps \0ver the lazy dog"),
10211be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps o\0er the lazy dog"),
10311be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps ov\0r the lazy dog"),
10411be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps ove\0 the lazy dog"),
10511be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over\0the lazy dog"),
10611be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over \0he lazy dog"),
10711be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over t\0e lazy dog"),
10811be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over th\0 lazy dog"),
10911be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the\0lazy dog"),
11011be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the \0azy dog"),
11111be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the l\0zy dog"),
11211be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the la\0y dog"),
11311be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the laz\0 dog"),
11411be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the lazy\0dog"),
11511be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the lazy \0og"),
11611be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the lazy d\0g"),
11711be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the lazy do\0"),
11811be35a1SLionel Sambuc TESTSTR("The quick brown fox jumps over the lazy dog"),
11911be35a1SLionel Sambuc { NULL, 0 },
12011be35a1SLionel Sambuc };
12111be35a1SLionel Sambuc
12211be35a1SLionel Sambuc ATF_TC(test00);
ATF_TC_HEAD(test00,tc)12311be35a1SLionel Sambuc ATF_TC_HEAD(test00, tc)
12411be35a1SLionel Sambuc {
12511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test00");
12611be35a1SLionel Sambuc }
ATF_TC_BODY(test00,tc)12711be35a1SLionel Sambuc ATF_TC_BODY(test00, tc)
12811be35a1SLionel Sambuc {
12911be35a1SLionel Sambuc const char **p;
13011be35a1SLionel Sambuc char buf[BUFSIZ];
13111be35a1SLionel Sambuc FILE *fp;
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc for (p = &mode_rwa[0]; *p != NULL; ++p) {
13411be35a1SLionel Sambuc fp = fmemopen(&buf[0], sizeof(buf), *p);
13511be35a1SLionel Sambuc /*
13611be35a1SLionel Sambuc * Upon successful completion, fmemopen() shall return a pointer to the
13711be35a1SLionel Sambuc * object controlling the stream.
13811be35a1SLionel Sambuc */
13911be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
14011be35a1SLionel Sambuc
14111be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
14211be35a1SLionel Sambuc }
14311be35a1SLionel Sambuc }
14411be35a1SLionel Sambuc
14511be35a1SLionel Sambuc ATF_TC(test01);
ATF_TC_HEAD(test01,tc)14611be35a1SLionel Sambuc ATF_TC_HEAD(test01, tc)
14711be35a1SLionel Sambuc {
14811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test01");
14911be35a1SLionel Sambuc }
ATF_TC_BODY(test01,tc)15011be35a1SLionel Sambuc ATF_TC_BODY(test01, tc)
15111be35a1SLionel Sambuc {
15211be35a1SLionel Sambuc const char **p;
15311be35a1SLionel Sambuc const char *mode[] = {
15411be35a1SLionel Sambuc "r+", "rb+", "r+b",
15511be35a1SLionel Sambuc "w+", "wb+", "w+b",
15611be35a1SLionel Sambuc "a+", "ab+", "a+b",
15711be35a1SLionel Sambuc NULL
15811be35a1SLionel Sambuc };
15911be35a1SLionel Sambuc FILE *fp;
16011be35a1SLionel Sambuc
16111be35a1SLionel Sambuc for (p = &mode[0]; *p != NULL; ++p) {
16211be35a1SLionel Sambuc /*
16311be35a1SLionel Sambuc * If a null pointer is specified as the buf argument, fmemopen() shall
16411be35a1SLionel Sambuc * allocate size bytes of memory as if by a call to malloc().
16511be35a1SLionel Sambuc */
16611be35a1SLionel Sambuc fp = fmemopen(NULL, BUFSIZ, *p);
16711be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
16811be35a1SLionel Sambuc
16911be35a1SLionel Sambuc /*
17011be35a1SLionel Sambuc * If buf is a null pointer, the initial position shall always be set
17111be35a1SLionel Sambuc * to the beginning of the buffer.
17211be35a1SLionel Sambuc */
17311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
17411be35a1SLionel Sambuc
17511be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
17611be35a1SLionel Sambuc }
17711be35a1SLionel Sambuc }
17811be35a1SLionel Sambuc
17911be35a1SLionel Sambuc ATF_TC(test02);
ATF_TC_HEAD(test02,tc)18011be35a1SLionel Sambuc ATF_TC_HEAD(test02, tc)
18111be35a1SLionel Sambuc {
18211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test02");
18311be35a1SLionel Sambuc }
ATF_TC_BODY(test02,tc)18411be35a1SLionel Sambuc ATF_TC_BODY(test02, tc)
18511be35a1SLionel Sambuc {
18611be35a1SLionel Sambuc const char **p;
18711be35a1SLionel Sambuc char buf[BUFSIZ];
18811be35a1SLionel Sambuc FILE *fp;
18911be35a1SLionel Sambuc
19011be35a1SLionel Sambuc for (p = &mode_r[0]; *p != NULL; ++p) {
19111be35a1SLionel Sambuc
19211be35a1SLionel Sambuc memset(&buf[0], 0x1, sizeof(buf));
19311be35a1SLionel Sambuc fp = fmemopen(&buf[0], sizeof(buf), *p);
19411be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
19511be35a1SLionel Sambuc
19611be35a1SLionel Sambuc /*
19711be35a1SLionel Sambuc * This position is initially set to either the beginning of the buffer
19811be35a1SLionel Sambuc * (for r and w modes)
19911be35a1SLionel Sambuc */
20011be35a1SLionel Sambuc ATF_CHECK((unsigned char)buf[0] == 0x1);
20111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
20211be35a1SLionel Sambuc
20311be35a1SLionel Sambuc /*
20411be35a1SLionel Sambuc * The stream also maintains the size of the current buffer contents.
20511be35a1SLionel Sambuc * For modes r and r+ the size is set to the value given by the size argument.
20611be35a1SLionel Sambuc */
20711be35a1SLionel Sambuc #if !defined(__GLIBC__)
20811be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
20911be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
21011be35a1SLionel Sambuc #endif
21111be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
21211be35a1SLionel Sambuc }
21311be35a1SLionel Sambuc }
21411be35a1SLionel Sambuc
21511be35a1SLionel Sambuc ATF_TC(test03);
ATF_TC_HEAD(test03,tc)21611be35a1SLionel Sambuc ATF_TC_HEAD(test03, tc)
21711be35a1SLionel Sambuc {
21811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test03");
21911be35a1SLionel Sambuc }
ATF_TC_BODY(test03,tc)22011be35a1SLionel Sambuc ATF_TC_BODY(test03, tc)
22111be35a1SLionel Sambuc {
22211be35a1SLionel Sambuc const char **p;
22311be35a1SLionel Sambuc char buf[BUFSIZ];
22411be35a1SLionel Sambuc FILE *fp;
22511be35a1SLionel Sambuc
22611be35a1SLionel Sambuc for (p = &mode_w[0]; *p != NULL; ++p) {
22711be35a1SLionel Sambuc
22811be35a1SLionel Sambuc memset(&buf[0], 0x1, sizeof(buf));
22911be35a1SLionel Sambuc fp = fmemopen(&buf[0], sizeof(buf), *p);
23011be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
23111be35a1SLionel Sambuc
23211be35a1SLionel Sambuc /*
23311be35a1SLionel Sambuc * This position is initially set to either the beginning of the buffer
23411be35a1SLionel Sambuc * (for r and w modes)
23511be35a1SLionel Sambuc */
23611be35a1SLionel Sambuc ATF_CHECK(buf[0] == '\0');
23711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
23811be35a1SLionel Sambuc
23911be35a1SLionel Sambuc /*
24011be35a1SLionel Sambuc * For modes w and w+ the initial size is zero
24111be35a1SLionel Sambuc */
24211be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
24311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
24411be35a1SLionel Sambuc
24511be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
24611be35a1SLionel Sambuc }
24711be35a1SLionel Sambuc }
24811be35a1SLionel Sambuc
24911be35a1SLionel Sambuc ATF_TC(test04);
ATF_TC_HEAD(test04,tc)25011be35a1SLionel Sambuc ATF_TC_HEAD(test04, tc)
25111be35a1SLionel Sambuc {
25211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test04");
25311be35a1SLionel Sambuc }
ATF_TC_BODY(test04,tc)25411be35a1SLionel Sambuc ATF_TC_BODY(test04, tc)
25511be35a1SLionel Sambuc {
25611be35a1SLionel Sambuc const char **p;
25711be35a1SLionel Sambuc char buf[BUFSIZ];
25811be35a1SLionel Sambuc FILE *fp;
25911be35a1SLionel Sambuc
26011be35a1SLionel Sambuc /*
26111be35a1SLionel Sambuc * or to the first null byte in the buffer (for a modes)
26211be35a1SLionel Sambuc */
26311be35a1SLionel Sambuc for (p = &mode_a[0]; *p != NULL; ++p) {
26411be35a1SLionel Sambuc
26511be35a1SLionel Sambuc memset(&buf[0], 0x1, sizeof(buf));
26611be35a1SLionel Sambuc fp = fmemopen(&buf[0], sizeof(buf), *p);
26711be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
26811be35a1SLionel Sambuc
26911be35a1SLionel Sambuc ATF_CHECK((unsigned char)buf[0] == 0x1);
27011be35a1SLionel Sambuc
27111be35a1SLionel Sambuc /* If no null byte is found in append mode,
27211be35a1SLionel Sambuc * the initial position is set to one byte after the end of the buffer.
27311be35a1SLionel Sambuc */
27411be35a1SLionel Sambuc #if !defined(__GLIBC__)
27511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
27611be35a1SLionel Sambuc #endif
27711be35a1SLionel Sambuc
27811be35a1SLionel Sambuc /*
27911be35a1SLionel Sambuc * and for modes a and a+ the initial size is either the position of the
28011be35a1SLionel Sambuc * first null byte in the buffer or the value of the size argument
28111be35a1SLionel Sambuc * if no null byte is found.
28211be35a1SLionel Sambuc */
28311be35a1SLionel Sambuc #if !defined(__GLIBC__)
28411be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
28511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)sizeof(buf));
28611be35a1SLionel Sambuc #endif
28711be35a1SLionel Sambuc
28811be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
28911be35a1SLionel Sambuc }
29011be35a1SLionel Sambuc }
29111be35a1SLionel Sambuc
29211be35a1SLionel Sambuc ATF_TC(test05);
ATF_TC_HEAD(test05,tc)29311be35a1SLionel Sambuc ATF_TC_HEAD(test05, tc)
29411be35a1SLionel Sambuc {
29511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test05");
29611be35a1SLionel Sambuc }
ATF_TC_BODY(test05,tc)29711be35a1SLionel Sambuc ATF_TC_BODY(test05, tc)
29811be35a1SLionel Sambuc {
29911be35a1SLionel Sambuc const char **p;
30011be35a1SLionel Sambuc FILE *fp;
30111be35a1SLionel Sambuc char buf[BUFSIZ];
30211be35a1SLionel Sambuc
30311be35a1SLionel Sambuc for (p = &mode_rwa[0]; *p != NULL; ++p) {
30411be35a1SLionel Sambuc /*
30511be35a1SLionel Sambuc * Otherwise, a null pointer shall be returned, and errno shall be set
30611be35a1SLionel Sambuc * to indicate the error.
30711be35a1SLionel Sambuc */
30811be35a1SLionel Sambuc errno = 0;
30911be35a1SLionel Sambuc fp = fmemopen(NULL, (size_t)0, *p);
31011be35a1SLionel Sambuc ATF_CHECK(fp == NULL);
31111be35a1SLionel Sambuc ATF_CHECK(errno == EINVAL);
31211be35a1SLionel Sambuc
31311be35a1SLionel Sambuc errno = 0;
31411be35a1SLionel Sambuc fp = fmemopen((void *)&buf[0], 0, *p);
31511be35a1SLionel Sambuc ATF_CHECK(fp == NULL);
31611be35a1SLionel Sambuc ATF_CHECK(errno == EINVAL);
31711be35a1SLionel Sambuc }
31811be35a1SLionel Sambuc }
31911be35a1SLionel Sambuc
32011be35a1SLionel Sambuc ATF_TC(test06);
ATF_TC_HEAD(test06,tc)32111be35a1SLionel Sambuc ATF_TC_HEAD(test06, tc)
32211be35a1SLionel Sambuc {
32311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test06");
32411be35a1SLionel Sambuc }
ATF_TC_BODY(test06,tc)32511be35a1SLionel Sambuc ATF_TC_BODY(test06, tc)
32611be35a1SLionel Sambuc {
32711be35a1SLionel Sambuc const char **p;
32811be35a1SLionel Sambuc const char *mode[] = { "", " ", "???", NULL };
32911be35a1SLionel Sambuc FILE *fp;
33011be35a1SLionel Sambuc
33111be35a1SLionel Sambuc for (p = &mode[0]; *p != NULL; ++p) {
33211be35a1SLionel Sambuc /*
33311be35a1SLionel Sambuc * The value of the mode argument is not valid.
33411be35a1SLionel Sambuc */
33511be35a1SLionel Sambuc fp = fmemopen(NULL, 1, *p);
33611be35a1SLionel Sambuc ATF_CHECK(fp == NULL);
33711be35a1SLionel Sambuc ATF_CHECK(errno == EINVAL);
33811be35a1SLionel Sambuc }
33911be35a1SLionel Sambuc }
34011be35a1SLionel Sambuc
34111be35a1SLionel Sambuc ATF_TC(test07);
ATF_TC_HEAD(test07,tc)34211be35a1SLionel Sambuc ATF_TC_HEAD(test07, tc)
34311be35a1SLionel Sambuc {
34411be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test07");
34511be35a1SLionel Sambuc }
ATF_TC_BODY(test07,tc)34611be35a1SLionel Sambuc ATF_TC_BODY(test07, tc)
34711be35a1SLionel Sambuc {
34811be35a1SLionel Sambuc #if !defined(__GLIBC__)
34911be35a1SLionel Sambuc const char **p;
35011be35a1SLionel Sambuc const char *mode[] = {
35111be35a1SLionel Sambuc "r", "rb",
35211be35a1SLionel Sambuc "w", "wb",
35311be35a1SLionel Sambuc "a", "ab",
35411be35a1SLionel Sambuc NULL
35511be35a1SLionel Sambuc };
35611be35a1SLionel Sambuc FILE *fp;
35711be35a1SLionel Sambuc
35811be35a1SLionel Sambuc for (p = &mode[0]; *p != NULL; ++p) {
35911be35a1SLionel Sambuc /*
36011be35a1SLionel Sambuc * Because this feature is only useful when the stream is opened for updating
36111be35a1SLionel Sambuc * (because there is no way to get a pointer to the buffer) the fmemopen()
36211be35a1SLionel Sambuc * call may fail if the mode argument does not include a '+' .
36311be35a1SLionel Sambuc */
36411be35a1SLionel Sambuc errno = 0;
36511be35a1SLionel Sambuc fp = fmemopen(NULL, 1, *p);
36611be35a1SLionel Sambuc ATF_CHECK(fp == NULL);
36711be35a1SLionel Sambuc ATF_CHECK(errno == EINVAL);
36811be35a1SLionel Sambuc }
36911be35a1SLionel Sambuc #endif
37011be35a1SLionel Sambuc }
37111be35a1SLionel Sambuc
37211be35a1SLionel Sambuc ATF_TC(test08);
ATF_TC_HEAD(test08,tc)37311be35a1SLionel Sambuc ATF_TC_HEAD(test08, tc)
37411be35a1SLionel Sambuc {
37511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test08");
37611be35a1SLionel Sambuc }
ATF_TC_BODY(test08,tc)37711be35a1SLionel Sambuc ATF_TC_BODY(test08, tc)
37811be35a1SLionel Sambuc {
37911be35a1SLionel Sambuc #if !defined(__GLIBC__)
38011be35a1SLionel Sambuc const char **p;
38111be35a1SLionel Sambuc const char *mode[] = {
38211be35a1SLionel Sambuc "r+", "rb+", "r+b",
38311be35a1SLionel Sambuc "w+", "wb+", "w+b",
38411be35a1SLionel Sambuc "a+", "ab+", "a+b",
38511be35a1SLionel Sambuc NULL
38611be35a1SLionel Sambuc };
38711be35a1SLionel Sambuc FILE *fp;
38811be35a1SLionel Sambuc
38911be35a1SLionel Sambuc for (p = &mode[0]; *p != NULL; ++p) {
39011be35a1SLionel Sambuc /*
39111be35a1SLionel Sambuc * The buf argument is a null pointer and the allocation of a buffer of
39211be35a1SLionel Sambuc * length size has failed.
39311be35a1SLionel Sambuc */
39411be35a1SLionel Sambuc fp = fmemopen(NULL, SIZE_MAX, *p);
39511be35a1SLionel Sambuc ATF_CHECK(fp == NULL);
39611be35a1SLionel Sambuc ATF_CHECK(errno == ENOMEM);
39711be35a1SLionel Sambuc }
39811be35a1SLionel Sambuc #endif
39911be35a1SLionel Sambuc }
40011be35a1SLionel Sambuc
40111be35a1SLionel Sambuc /*
40211be35a1SLionel Sambuc * test09 - test14:
40311be35a1SLionel Sambuc * An attempt to seek a memory buffer stream to a negative position or to a
40411be35a1SLionel Sambuc * position larger than the buffer size given in the size argument shall fail.
40511be35a1SLionel Sambuc */
40611be35a1SLionel Sambuc
40711be35a1SLionel Sambuc ATF_TC(test09);
ATF_TC_HEAD(test09,tc)40811be35a1SLionel Sambuc ATF_TC_HEAD(test09, tc)
40911be35a1SLionel Sambuc {
41011be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test09");
41111be35a1SLionel Sambuc }
ATF_TC_BODY(test09,tc)41211be35a1SLionel Sambuc ATF_TC_BODY(test09, tc)
41311be35a1SLionel Sambuc {
41411be35a1SLionel Sambuc struct testcase *t;
41511be35a1SLionel Sambuc const char **p;
41611be35a1SLionel Sambuc char buf[BUFSIZ];
41711be35a1SLionel Sambuc FILE *fp;
41811be35a1SLionel Sambuc off_t i;
41911be35a1SLionel Sambuc
42011be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
42111be35a1SLionel Sambuc for (p = &mode_rwa[0]; *p != NULL; ++p) {
42211be35a1SLionel Sambuc
42311be35a1SLionel Sambuc memcpy(&buf[0], t->s, t->n);
42411be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
42511be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
42611be35a1SLionel Sambuc
42711be35a1SLionel Sambuc /*
42811be35a1SLionel Sambuc * test fmemopen_seek(SEEK_SET)
42911be35a1SLionel Sambuc */
43011be35a1SLionel Sambuc /* zero */
43111be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_SET) == 0);
43211be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
43311be35a1SLionel Sambuc
43411be35a1SLionel Sambuc /* positive */
43511be35a1SLionel Sambuc for (i = (off_t)1; i <= (off_t)t->n; ++i) {
43611be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, i, SEEK_SET) == 0);
43711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == i);
43811be35a1SLionel Sambuc }
43911be35a1SLionel Sambuc /* positive + OOB */
44011be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, t->n + 1, SEEK_SET) == -1);
44111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == t->n);
44211be35a1SLionel Sambuc
44311be35a1SLionel Sambuc /* negative + OOB */
44411be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_SET) == -1);
44511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == t->n);
44611be35a1SLionel Sambuc
44711be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
44811be35a1SLionel Sambuc }
44911be35a1SLionel Sambuc }
45011be35a1SLionel Sambuc }
45111be35a1SLionel Sambuc
45211be35a1SLionel Sambuc const char *mode_rw[] = {
45311be35a1SLionel Sambuc "r", "rb", "r+", "rb+", "r+b",
45411be35a1SLionel Sambuc "w", "wb", "w+", "wb+", "w+b",
45511be35a1SLionel Sambuc NULL
45611be35a1SLionel Sambuc };
45711be35a1SLionel Sambuc
45811be35a1SLionel Sambuc ATF_TC(test10);
ATF_TC_HEAD(test10,tc)45911be35a1SLionel Sambuc ATF_TC_HEAD(test10, tc)
46011be35a1SLionel Sambuc {
46111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test10");
46211be35a1SLionel Sambuc }
ATF_TC_BODY(test10,tc)46311be35a1SLionel Sambuc ATF_TC_BODY(test10, tc)
46411be35a1SLionel Sambuc {
46511be35a1SLionel Sambuc struct testcase *t;
466*84d9c625SLionel Sambuc off_t i;
46711be35a1SLionel Sambuc const char **p;
46811be35a1SLionel Sambuc char buf[BUFSIZ];
46911be35a1SLionel Sambuc FILE *fp;
47011be35a1SLionel Sambuc
47111be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
47211be35a1SLionel Sambuc for (p = &mode_rw[0]; *p != NULL; ++p) {
47311be35a1SLionel Sambuc
47411be35a1SLionel Sambuc memcpy(&buf[0], t->s, t->n);
47511be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
47611be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
47711be35a1SLionel Sambuc
47811be35a1SLionel Sambuc /*
47911be35a1SLionel Sambuc * test fmemopen_seek(SEEK_CUR)
48011be35a1SLionel Sambuc */
48111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
48211be35a1SLionel Sambuc
48311be35a1SLionel Sambuc /* zero */
48411be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_CUR) == 0);
48511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
48611be35a1SLionel Sambuc
48711be35a1SLionel Sambuc /* negative & OOB */
48811be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == -1);
48911be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
49011be35a1SLionel Sambuc
49111be35a1SLionel Sambuc /* positive */
49211be35a1SLionel Sambuc for (i = 0; i < (off_t)t->n; ++i) {
49311be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == 0);
49411be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == i + 1);
49511be35a1SLionel Sambuc }
49611be35a1SLionel Sambuc
49711be35a1SLionel Sambuc /* positive & OOB */
49811be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == -1);
49911be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
50011be35a1SLionel Sambuc
50111be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
50211be35a1SLionel Sambuc }
50311be35a1SLionel Sambuc }
50411be35a1SLionel Sambuc }
50511be35a1SLionel Sambuc
50611be35a1SLionel Sambuc ATF_TC(test11);
ATF_TC_HEAD(test11,tc)50711be35a1SLionel Sambuc ATF_TC_HEAD(test11, tc)
50811be35a1SLionel Sambuc {
50911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test11");
51011be35a1SLionel Sambuc }
ATF_TC_BODY(test11,tc)51111be35a1SLionel Sambuc ATF_TC_BODY(test11, tc)
51211be35a1SLionel Sambuc {
51311be35a1SLionel Sambuc struct testcase *t;
51411be35a1SLionel Sambuc off_t len, rest, i;
51511be35a1SLionel Sambuc const char **p;
51611be35a1SLionel Sambuc char buf[BUFSIZ];
51711be35a1SLionel Sambuc FILE *fp;
51811be35a1SLionel Sambuc
51911be35a1SLionel Sambuc /* test fmemopen_seek(SEEK_CUR) */
52011be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
52111be35a1SLionel Sambuc len = (off_t)strnlen(t->s, t->n);
52211be35a1SLionel Sambuc rest = (off_t)t->n - len;
52311be35a1SLionel Sambuc for (p = &mode_a[0]; *p != NULL; ++p) {
52411be35a1SLionel Sambuc
52511be35a1SLionel Sambuc memcpy(&buf[0], t->s, t->n);
52611be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
52711be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
52811be35a1SLionel Sambuc /*
52911be35a1SLionel Sambuc * test fmemopen_seek(SEEK_CUR)
53011be35a1SLionel Sambuc */
53111be35a1SLionel Sambuc #if defined(__GLIBC__)
53211be35a1SLionel Sambuc if (i < (off_t)t->n) {
53311be35a1SLionel Sambuc #endif
53411be35a1SLionel Sambuc /* zero */
53511be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_CUR) == 0);
53611be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
53711be35a1SLionel Sambuc
53811be35a1SLionel Sambuc /* posive */
53911be35a1SLionel Sambuc for (i = (off_t)1; i <= rest; ++i) {
54011be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == 0);
54111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len + i);
54211be35a1SLionel Sambuc }
54311be35a1SLionel Sambuc
54411be35a1SLionel Sambuc /* positive + OOB */
54511be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)1, SEEK_CUR) == -1);
54611be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
54711be35a1SLionel Sambuc
54811be35a1SLionel Sambuc /* negative */
54911be35a1SLionel Sambuc for (i = (off_t)1; i <= (off_t)t->n; ++i) {
55011be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == 0);
55111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n - i);
55211be35a1SLionel Sambuc }
55311be35a1SLionel Sambuc
55411be35a1SLionel Sambuc /* negative + OOB */
55511be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)-1, SEEK_CUR) == -1);
55611be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
55711be35a1SLionel Sambuc
55811be35a1SLionel Sambuc #if defined(__GLIBC__)
55911be35a1SLionel Sambuc }
56011be35a1SLionel Sambuc #endif
56111be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
56211be35a1SLionel Sambuc }
56311be35a1SLionel Sambuc }
56411be35a1SLionel Sambuc }
56511be35a1SLionel Sambuc
56611be35a1SLionel Sambuc ATF_TC(test12);
ATF_TC_HEAD(test12,tc)56711be35a1SLionel Sambuc ATF_TC_HEAD(test12, tc)
56811be35a1SLionel Sambuc {
56911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test12");
57011be35a1SLionel Sambuc }
ATF_TC_BODY(test12,tc)57111be35a1SLionel Sambuc ATF_TC_BODY(test12, tc)
57211be35a1SLionel Sambuc {
57311be35a1SLionel Sambuc struct testcase *t;
57411be35a1SLionel Sambuc off_t len, rest, i;
57511be35a1SLionel Sambuc const char **p;
57611be35a1SLionel Sambuc char buf[BUFSIZ];
57711be35a1SLionel Sambuc FILE *fp;
57811be35a1SLionel Sambuc
57911be35a1SLionel Sambuc /* test fmemopen_seek(SEEK_END) */
58011be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
58111be35a1SLionel Sambuc len = (off_t)strnlen(t->s, t->n);
58211be35a1SLionel Sambuc rest = t->n - len;
58311be35a1SLionel Sambuc for (p = &mode_r[0]; *p != NULL; ++p) {
58411be35a1SLionel Sambuc
58511be35a1SLionel Sambuc memcpy(buf, t->s, t->n);
58611be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
58711be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
58811be35a1SLionel Sambuc
58911be35a1SLionel Sambuc /*
59011be35a1SLionel Sambuc * test fmemopen_seek(SEEK_END)
59111be35a1SLionel Sambuc */
59211be35a1SLionel Sambuc #if !defined(__GLIBC__)
59311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
59411be35a1SLionel Sambuc
59511be35a1SLionel Sambuc /* zero */
59611be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
59711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
59811be35a1SLionel Sambuc
59911be35a1SLionel Sambuc /* positive + OOB */
60011be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, rest + 1, SEEK_END) == -1);
60111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
60211be35a1SLionel Sambuc
60311be35a1SLionel Sambuc /* negative + OOB */
60411be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, -(len + 1), SEEK_END) == -1);
60511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
60611be35a1SLionel Sambuc
60711be35a1SLionel Sambuc /* positive */
60811be35a1SLionel Sambuc for (i = 1; i <= rest; ++i) {
60911be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
61011be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len + i);
61111be35a1SLionel Sambuc }
61211be35a1SLionel Sambuc
61311be35a1SLionel Sambuc /* negative */
61411be35a1SLionel Sambuc for (i = 1; i < len; ++i) {
61511be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, -i, SEEK_END) == 0);
61611be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len - i);
61711be35a1SLionel Sambuc }
61811be35a1SLionel Sambuc #endif
61911be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
62011be35a1SLionel Sambuc }
62111be35a1SLionel Sambuc }
62211be35a1SLionel Sambuc }
62311be35a1SLionel Sambuc
62411be35a1SLionel Sambuc ATF_TC(test13);
ATF_TC_HEAD(test13,tc)62511be35a1SLionel Sambuc ATF_TC_HEAD(test13, tc)
62611be35a1SLionel Sambuc {
62711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test13");
62811be35a1SLionel Sambuc }
ATF_TC_BODY(test13,tc)62911be35a1SLionel Sambuc ATF_TC_BODY(test13, tc)
63011be35a1SLionel Sambuc {
63111be35a1SLionel Sambuc struct testcase *t;
632*84d9c625SLionel Sambuc off_t i;
63311be35a1SLionel Sambuc const char **p;
63411be35a1SLionel Sambuc char buf[BUFSIZ];
63511be35a1SLionel Sambuc FILE *fp;
63611be35a1SLionel Sambuc
63711be35a1SLionel Sambuc /* test fmemopen_seek(SEEK_END) */
63811be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
63911be35a1SLionel Sambuc for (p = &mode_w[0]; *p != NULL; ++p) {
64011be35a1SLionel Sambuc
64111be35a1SLionel Sambuc memcpy(buf, t->s, t->n);
64211be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
64311be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
64411be35a1SLionel Sambuc /*
64511be35a1SLionel Sambuc * test fmemopen_seek(SEEK_END)
64611be35a1SLionel Sambuc */
64711be35a1SLionel Sambuc #if !defined(__GLIBC__)
64811be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
64911be35a1SLionel Sambuc ATF_CHECK(buf[0] == '\0');
65011be35a1SLionel Sambuc
65111be35a1SLionel Sambuc /* zero */
65211be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)0, SEEK_END) == 0);
65311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
65411be35a1SLionel Sambuc
65511be35a1SLionel Sambuc /* positive + OOB */
65611be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, (off_t)t->n + 1, SEEK_END) == -1);
65711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
65811be35a1SLionel Sambuc
65911be35a1SLionel Sambuc /* negative + OOB */
66011be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, -1, SEEK_END) == -1);
66111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
66211be35a1SLionel Sambuc
66311be35a1SLionel Sambuc /* positive */
66411be35a1SLionel Sambuc for (i = 1; i <= t->n; ++i) {
66511be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
66611be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == i);
66711be35a1SLionel Sambuc }
66811be35a1SLionel Sambuc #endif
66911be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
67011be35a1SLionel Sambuc }
67111be35a1SLionel Sambuc }
67211be35a1SLionel Sambuc }
67311be35a1SLionel Sambuc
67411be35a1SLionel Sambuc ATF_TC(test14);
ATF_TC_HEAD(test14,tc)67511be35a1SLionel Sambuc ATF_TC_HEAD(test14, tc)
67611be35a1SLionel Sambuc {
67711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test14");
67811be35a1SLionel Sambuc }
ATF_TC_BODY(test14,tc)67911be35a1SLionel Sambuc ATF_TC_BODY(test14, tc)
68011be35a1SLionel Sambuc {
68111be35a1SLionel Sambuc struct testcase *t;
68211be35a1SLionel Sambuc off_t len, rest, i;
68311be35a1SLionel Sambuc const char **p;
68411be35a1SLionel Sambuc char buf[BUFSIZ];
68511be35a1SLionel Sambuc FILE *fp;
68611be35a1SLionel Sambuc
68711be35a1SLionel Sambuc /* test fmemopen_seek(SEEK_END) */
68811be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
68911be35a1SLionel Sambuc len = (off_t)strnlen(t->s, t->n);
69011be35a1SLionel Sambuc rest = (off_t)t->n - len;
69111be35a1SLionel Sambuc for (p = &mode_a[0]; *p != NULL; ++p) {
69211be35a1SLionel Sambuc
69311be35a1SLionel Sambuc memcpy(buf, t->s, t->n);
69411be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
69511be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
69611be35a1SLionel Sambuc /*
69711be35a1SLionel Sambuc * test fmemopen_seek(SEEK_END)
69811be35a1SLionel Sambuc */
69911be35a1SLionel Sambuc #if !defined(__GLIBC__)
70011be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
70111be35a1SLionel Sambuc
70211be35a1SLionel Sambuc /* zero */
70311be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, 0, SEEK_END) == 0);
70411be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
70511be35a1SLionel Sambuc
70611be35a1SLionel Sambuc /* positive + OOB */
70711be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, rest + 1, SEEK_END) == -1);
70811be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
70911be35a1SLionel Sambuc
71011be35a1SLionel Sambuc /* negative + OOB */
71111be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, -(len + 1), SEEK_END) == -1);
71211be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len);
71311be35a1SLionel Sambuc
71411be35a1SLionel Sambuc /* positive */
71511be35a1SLionel Sambuc for (i = 1; i <= rest; ++i) {
71611be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, i, SEEK_END) == 0);
71711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len + i);
71811be35a1SLionel Sambuc }
71911be35a1SLionel Sambuc
72011be35a1SLionel Sambuc /* negative */
72111be35a1SLionel Sambuc for (i = 1; i < len; ++i) {
72211be35a1SLionel Sambuc ATF_CHECK(fseeko(fp, -i, SEEK_END) == 0);
72311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == len - i);
72411be35a1SLionel Sambuc }
72511be35a1SLionel Sambuc #endif
72611be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
72711be35a1SLionel Sambuc }
72811be35a1SLionel Sambuc }
72911be35a1SLionel Sambuc }
73011be35a1SLionel Sambuc
73111be35a1SLionel Sambuc const char *mode_rw1[] = {
73211be35a1SLionel Sambuc "r", "rb", "r+", "rb+", "r+b",
73311be35a1SLionel Sambuc "w+", "wb+",
73411be35a1SLionel Sambuc NULL
73511be35a1SLionel Sambuc };
73611be35a1SLionel Sambuc
73711be35a1SLionel Sambuc /* test15 - 18:
73811be35a1SLionel Sambuc * When a stream open for writing is flushed or closed, a null byte is written
73911be35a1SLionel Sambuc * at the current position or at the end of the buffer, depending on the size
74011be35a1SLionel Sambuc * of the contents.
74111be35a1SLionel Sambuc */
74211be35a1SLionel Sambuc
74311be35a1SLionel Sambuc ATF_TC(test15);
ATF_TC_HEAD(test15,tc)74411be35a1SLionel Sambuc ATF_TC_HEAD(test15, tc)
74511be35a1SLionel Sambuc {
74611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test15");
74711be35a1SLionel Sambuc }
ATF_TC_BODY(test15,tc)74811be35a1SLionel Sambuc ATF_TC_BODY(test15, tc)
74911be35a1SLionel Sambuc {
75011be35a1SLionel Sambuc struct testcase *t;
75111be35a1SLionel Sambuc const char **p;
75211be35a1SLionel Sambuc char buf0[BUFSIZ];
75311be35a1SLionel Sambuc FILE *fp;
75411be35a1SLionel Sambuc int i;
75511be35a1SLionel Sambuc
75611be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
75711be35a1SLionel Sambuc for (p = &mode_rw1[0]; *p != NULL; ++p) {
75811be35a1SLionel Sambuc
75911be35a1SLionel Sambuc memcpy(&buf0[0], t->s, t->n);
76011be35a1SLionel Sambuc fp = fmemopen(&buf0[0], t->n, *p);
76111be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
76211be35a1SLionel Sambuc /*
76311be35a1SLionel Sambuc * test fmemopen_read + fgetc(3)
76411be35a1SLionel Sambuc */
76511be35a1SLionel Sambuc for (i = 0; i < t->n; ++i) {
76611be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i);
76711be35a1SLionel Sambuc ATF_CHECK(fgetc(fp) == buf0[i]);
76811be35a1SLionel Sambuc ATF_CHECK(feof(fp) == 0);
76911be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i + 1);
77011be35a1SLionel Sambuc }
77111be35a1SLionel Sambuc ATF_CHECK(fgetc(fp) == EOF);
77211be35a1SLionel Sambuc ATF_CHECK(feof(fp) != 0);
77311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
77411be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
77511be35a1SLionel Sambuc }
77611be35a1SLionel Sambuc }
77711be35a1SLionel Sambuc }
77811be35a1SLionel Sambuc
77911be35a1SLionel Sambuc ATF_TC(test16);
ATF_TC_HEAD(test16,tc)78011be35a1SLionel Sambuc ATF_TC_HEAD(test16, tc)
78111be35a1SLionel Sambuc {
78211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test16");
78311be35a1SLionel Sambuc }
ATF_TC_BODY(test16,tc)78411be35a1SLionel Sambuc ATF_TC_BODY(test16, tc)
78511be35a1SLionel Sambuc {
78611be35a1SLionel Sambuc struct testcase *t;
78711be35a1SLionel Sambuc const char **p;
78811be35a1SLionel Sambuc char buf0[BUFSIZ], buf1[BUFSIZ];
78911be35a1SLionel Sambuc FILE *fp;
79011be35a1SLionel Sambuc
79111be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
79211be35a1SLionel Sambuc for (p = &mode_rw1[0]; *p != NULL; ++p) {
79311be35a1SLionel Sambuc
79411be35a1SLionel Sambuc memcpy(&buf0[0], t->s, t->n);
79511be35a1SLionel Sambuc buf1[t->n] = 0x1;
79611be35a1SLionel Sambuc fp = fmemopen(&buf0[0], t->n, *p);
79711be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
79811be35a1SLionel Sambuc /*
79911be35a1SLionel Sambuc * test fmemopen_read + fread(4)
80011be35a1SLionel Sambuc */
80111be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
80211be35a1SLionel Sambuc ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp) == (size_t)t->n);
80311be35a1SLionel Sambuc ATF_CHECK(feof(fp) != 0);
80411be35a1SLionel Sambuc ATF_CHECK(memcmp(&buf0[0], &buf1[0], t->n) == 0);
80511be35a1SLionel Sambuc ATF_CHECK((unsigned char)buf1[t->n] == 0x1);
80611be35a1SLionel Sambuc
80711be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
80811be35a1SLionel Sambuc }
80911be35a1SLionel Sambuc }
81011be35a1SLionel Sambuc }
81111be35a1SLionel Sambuc
81211be35a1SLionel Sambuc const char *mode_a1[] = { "a+", "ab+", NULL };
81311be35a1SLionel Sambuc
81411be35a1SLionel Sambuc ATF_TC(test17);
ATF_TC_HEAD(test17,tc)81511be35a1SLionel Sambuc ATF_TC_HEAD(test17, tc)
81611be35a1SLionel Sambuc {
81711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test17");
81811be35a1SLionel Sambuc }
ATF_TC_BODY(test17,tc)81911be35a1SLionel Sambuc ATF_TC_BODY(test17, tc)
82011be35a1SLionel Sambuc {
82111be35a1SLionel Sambuc struct testcase *t;
82211be35a1SLionel Sambuc size_t len;
82311be35a1SLionel Sambuc int i;
82411be35a1SLionel Sambuc const char **p;
82511be35a1SLionel Sambuc char buf[BUFSIZ];
82611be35a1SLionel Sambuc FILE *fp;
82711be35a1SLionel Sambuc
82811be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
82911be35a1SLionel Sambuc len = strnlen(t->s, t->n);
83011be35a1SLionel Sambuc for (p = &mode_a1[0]; *p != NULL; ++p) {
83111be35a1SLionel Sambuc
83211be35a1SLionel Sambuc memcpy(&buf[0], t->s, t->n);
83311be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
83411be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
83511be35a1SLionel Sambuc /*
83611be35a1SLionel Sambuc * test fmemopen_read + fgetc(3)
83711be35a1SLionel Sambuc */
83811be35a1SLionel Sambuc #if defined(__GLIBC__)
83911be35a1SLionel Sambuc if (i < t->n) {
84011be35a1SLionel Sambuc #endif
84111be35a1SLionel Sambuc for (i = len; i < t->n; ++i) {
84211be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i);
84311be35a1SLionel Sambuc ATF_CHECK(fgetc(fp) == buf[i]);
84411be35a1SLionel Sambuc ATF_CHECK(feof(fp) == 0);
84511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i + 1);
84611be35a1SLionel Sambuc }
84711be35a1SLionel Sambuc ATF_CHECK(fgetc(fp) == EOF);
84811be35a1SLionel Sambuc ATF_CHECK(feof(fp) != 0);
84911be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
85011be35a1SLionel Sambuc rewind(fp);
85111be35a1SLionel Sambuc for (i = 0; i < t->n; ++i) {
85211be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i);
85311be35a1SLionel Sambuc ATF_CHECK(fgetc(fp) == buf[i]);
85411be35a1SLionel Sambuc ATF_CHECK(feof(fp) == 0);
85511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i + 1);
85611be35a1SLionel Sambuc }
85711be35a1SLionel Sambuc ATF_CHECK(fgetc(fp) == EOF);
85811be35a1SLionel Sambuc ATF_CHECK(feof(fp) != 0);
85911be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
86011be35a1SLionel Sambuc #if defined(__GLIBC__)
86111be35a1SLionel Sambuc }
86211be35a1SLionel Sambuc #endif
86311be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
86411be35a1SLionel Sambuc }
86511be35a1SLionel Sambuc }
86611be35a1SLionel Sambuc }
86711be35a1SLionel Sambuc
86811be35a1SLionel Sambuc ATF_TC(test18);
ATF_TC_HEAD(test18,tc)86911be35a1SLionel Sambuc ATF_TC_HEAD(test18, tc)
87011be35a1SLionel Sambuc {
87111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test18");
87211be35a1SLionel Sambuc }
ATF_TC_BODY(test18,tc)87311be35a1SLionel Sambuc ATF_TC_BODY(test18, tc)
87411be35a1SLionel Sambuc {
87511be35a1SLionel Sambuc struct testcase *t;
87611be35a1SLionel Sambuc size_t len;
87711be35a1SLionel Sambuc const char **p;
87811be35a1SLionel Sambuc char buf0[BUFSIZ], buf1[BUFSIZ];
87911be35a1SLionel Sambuc FILE *fp;
88011be35a1SLionel Sambuc
88111be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
88211be35a1SLionel Sambuc len = strnlen(t->s, t->n);
88311be35a1SLionel Sambuc for (p = &mode_a1[0]; *p != NULL; ++p) {
88411be35a1SLionel Sambuc
88511be35a1SLionel Sambuc memcpy(&buf0[0], t->s, t->n);
88611be35a1SLionel Sambuc buf1[t->n - len] = 0x1;
88711be35a1SLionel Sambuc fp = fmemopen(&buf0[0], t->n, *p);
88811be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
88911be35a1SLionel Sambuc /*
89011be35a1SLionel Sambuc * test fmemopen_read + fread(3)
89111be35a1SLionel Sambuc */
89211be35a1SLionel Sambuc #if defined(__GLIBC__)
89311be35a1SLionel Sambuc if (i < t->n) {
89411be35a1SLionel Sambuc #endif
89511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)len);
89611be35a1SLionel Sambuc ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp)
89711be35a1SLionel Sambuc == t->n - len);
89811be35a1SLionel Sambuc ATF_CHECK(feof(fp) != 0);
89911be35a1SLionel Sambuc ATF_CHECK(!memcmp(&buf0[len], &buf1[0], t->n - len));
90011be35a1SLionel Sambuc ATF_CHECK((unsigned char)buf1[t->n - len] == 0x1);
90111be35a1SLionel Sambuc rewind(fp);
90211be35a1SLionel Sambuc buf1[t->n] = 0x1;
90311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)0);
90411be35a1SLionel Sambuc ATF_CHECK(fread(&buf1[0], 1, sizeof(buf1), fp)
90511be35a1SLionel Sambuc == (size_t)t->n);
90611be35a1SLionel Sambuc ATF_CHECK(feof(fp) != 0);
90711be35a1SLionel Sambuc ATF_CHECK(!memcmp(&buf0[0], &buf1[0], t->n));
90811be35a1SLionel Sambuc ATF_CHECK((unsigned char)buf1[t->n] == 0x1);
90911be35a1SLionel Sambuc #if defined(__GLIBC__)
91011be35a1SLionel Sambuc }
91111be35a1SLionel Sambuc #endif
91211be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
91311be35a1SLionel Sambuc }
91411be35a1SLionel Sambuc }
91511be35a1SLionel Sambuc }
91611be35a1SLionel Sambuc
91711be35a1SLionel Sambuc /*
91811be35a1SLionel Sambuc * test19 - test22:
91911be35a1SLionel Sambuc * If a stream open for update is flushed or closed and the last write has
92011be35a1SLionel Sambuc * advanced the current buffer size, a null byte is written at the end of the
92111be35a1SLionel Sambuc * buffer if it fits.
92211be35a1SLionel Sambuc */
92311be35a1SLionel Sambuc
92411be35a1SLionel Sambuc const char *mode_rw2[] = {
92511be35a1SLionel Sambuc "r+", "rb+", "r+b",
92611be35a1SLionel Sambuc "w", "wb", "w+", "wb+", "w+b",
92711be35a1SLionel Sambuc NULL
92811be35a1SLionel Sambuc };
92911be35a1SLionel Sambuc
93011be35a1SLionel Sambuc ATF_TC(test19);
ATF_TC_HEAD(test19,tc)93111be35a1SLionel Sambuc ATF_TC_HEAD(test19, tc)
93211be35a1SLionel Sambuc {
93311be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test19");
93411be35a1SLionel Sambuc }
ATF_TC_BODY(test19,tc)93511be35a1SLionel Sambuc ATF_TC_BODY(test19, tc)
93611be35a1SLionel Sambuc {
93711be35a1SLionel Sambuc struct testcase *t;
93811be35a1SLionel Sambuc int i;
93911be35a1SLionel Sambuc const char **p;
94011be35a1SLionel Sambuc char buf[BUFSIZ];
94111be35a1SLionel Sambuc FILE *fp;
94211be35a1SLionel Sambuc
94311be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
94411be35a1SLionel Sambuc for (p = &mode_rw2[0]; *p != NULL; ++p) {
94511be35a1SLionel Sambuc
94611be35a1SLionel Sambuc memcpy(&buf[0], t->s, t->n);
94711be35a1SLionel Sambuc buf[t->n] = 0x1;
94811be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n + 1, *p);
94911be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
95011be35a1SLionel Sambuc setbuf(fp, NULL);
95111be35a1SLionel Sambuc /*
95211be35a1SLionel Sambuc * test fmemopen_write + fputc(3)
95311be35a1SLionel Sambuc */
95411be35a1SLionel Sambuc for (i = 0; i < t->n; ++i) {
95511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i);
95611be35a1SLionel Sambuc ATF_CHECK(fputc(t->s[i], fp) == t->s[i]);
95711be35a1SLionel Sambuc ATF_CHECK(buf[i] == t->s[i]);
95811be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i + 1);
95911be35a1SLionel Sambuc ATF_CHECK(buf[i] == t->s[i]);
96011be35a1SLionel Sambuc #if !defined(__GLIBC__)
96111be35a1SLionel Sambuc ATF_CHECK(buf[i + 1] == '\0');
96211be35a1SLionel Sambuc #endif
96311be35a1SLionel Sambuc }
96411be35a1SLionel Sambuc
96511be35a1SLionel Sambuc /* don't accept non nul character at end of buffer */
96611be35a1SLionel Sambuc ATF_CHECK(fputc(0x1, fp) == EOF);
96711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
96811be35a1SLionel Sambuc ATF_CHECK(feof(fp) == 0);
96911be35a1SLionel Sambuc
97011be35a1SLionel Sambuc /* accept nul character at end of buffer */
97111be35a1SLionel Sambuc ATF_CHECK(fputc('\0', fp) == '\0');
97211be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
97311be35a1SLionel Sambuc ATF_CHECK(feof(fp) == 0);
97411be35a1SLionel Sambuc
97511be35a1SLionel Sambuc /* reach EOF */
97611be35a1SLionel Sambuc ATF_CHECK(fputc('\0', fp) == EOF);
97711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
97811be35a1SLionel Sambuc
97911be35a1SLionel Sambuc /* compare */
98011be35a1SLionel Sambuc ATF_CHECK(memcmp(&buf[0], t->s, t->n) == 0);
98111be35a1SLionel Sambuc ATF_CHECK(buf[t->n] == '\0');
98211be35a1SLionel Sambuc
98311be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
98411be35a1SLionel Sambuc }
98511be35a1SLionel Sambuc }
98611be35a1SLionel Sambuc }
98711be35a1SLionel Sambuc
98811be35a1SLionel Sambuc ATF_TC(test20);
ATF_TC_HEAD(test20,tc)98911be35a1SLionel Sambuc ATF_TC_HEAD(test20, tc)
99011be35a1SLionel Sambuc {
99111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test20");
99211be35a1SLionel Sambuc }
ATF_TC_BODY(test20,tc)99311be35a1SLionel Sambuc ATF_TC_BODY(test20, tc)
99411be35a1SLionel Sambuc {
99511be35a1SLionel Sambuc struct testcase *t;
99611be35a1SLionel Sambuc const char **p;
99711be35a1SLionel Sambuc char buf[BUFSIZ];
99811be35a1SLionel Sambuc FILE *fp;
99911be35a1SLionel Sambuc
100011be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
100111be35a1SLionel Sambuc for (p = &mode_rw2[0]; *p != NULL; ++p) {
100211be35a1SLionel Sambuc
100311be35a1SLionel Sambuc memcpy(&buf[0], t->s, t->n);
100411be35a1SLionel Sambuc buf[t->n] = 0x1;
100511be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n + 1, *p);
100611be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
100711be35a1SLionel Sambuc setbuf(fp, NULL);
100811be35a1SLionel Sambuc ATF_CHECK(fwrite(t->s, 1, t->n, fp) == (size_t)t->n);
100911be35a1SLionel Sambuc /*
101011be35a1SLionel Sambuc * test fmemopen_write + fwrite(3)
101111be35a1SLionel Sambuc */
101211be35a1SLionel Sambuc #if !defined(__GLIBC__)
101311be35a1SLionel Sambuc ATF_CHECK(buf[t->n] == '\0');
101411be35a1SLionel Sambuc
101511be35a1SLionel Sambuc /* don't accept non nul character at end of buffer */
101611be35a1SLionel Sambuc ATF_CHECK(fwrite("\x1", 1, 1, fp) == 0);
101711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
101811be35a1SLionel Sambuc ATF_CHECK(feof(fp) == 0);
101911be35a1SLionel Sambuc #endif
102011be35a1SLionel Sambuc
102111be35a1SLionel Sambuc /* accept nul character at end of buffer */
102211be35a1SLionel Sambuc ATF_CHECK(fwrite("\x0", 1, 1, fp) == 1);
102311be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
102411be35a1SLionel Sambuc ATF_CHECK(feof(fp) == 0);
102511be35a1SLionel Sambuc
102611be35a1SLionel Sambuc /* reach EOF */
102711be35a1SLionel Sambuc ATF_CHECK(fputc('\0', fp) == EOF);
102811be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n + 1);
102911be35a1SLionel Sambuc
103011be35a1SLionel Sambuc /* compare */
103111be35a1SLionel Sambuc ATF_CHECK(memcmp(&buf[0], t->s, t->n) == 0);
103211be35a1SLionel Sambuc ATF_CHECK(buf[t->n] == '\0');
103311be35a1SLionel Sambuc
103411be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
103511be35a1SLionel Sambuc }
103611be35a1SLionel Sambuc }
103711be35a1SLionel Sambuc }
103811be35a1SLionel Sambuc
103911be35a1SLionel Sambuc ATF_TC(test21);
ATF_TC_HEAD(test21,tc)104011be35a1SLionel Sambuc ATF_TC_HEAD(test21, tc)
104111be35a1SLionel Sambuc {
104211be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test21");
104311be35a1SLionel Sambuc }
ATF_TC_BODY(test21,tc)104411be35a1SLionel Sambuc ATF_TC_BODY(test21, tc)
104511be35a1SLionel Sambuc {
104611be35a1SLionel Sambuc struct testcase *t;
104711be35a1SLionel Sambuc int len, i;
104811be35a1SLionel Sambuc const char **p;
104911be35a1SLionel Sambuc char buf[BUFSIZ];
105011be35a1SLionel Sambuc FILE *fp;
105111be35a1SLionel Sambuc
105211be35a1SLionel Sambuc for (t = &testcases[0]; t->s != NULL; ++t) {
105311be35a1SLionel Sambuc len = strnlen(t->s, t->n);
105411be35a1SLionel Sambuc for (p = &mode_a[0]; *p != NULL; ++p) {
105511be35a1SLionel Sambuc memcpy(&buf[0], t->s, t->n);
105611be35a1SLionel Sambuc fp = fmemopen(&buf[0], t->n, *p);
105711be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
105811be35a1SLionel Sambuc setbuf(fp, NULL);
105911be35a1SLionel Sambuc /*
106011be35a1SLionel Sambuc * test fmemopen_write + fputc(3)
106111be35a1SLionel Sambuc */
106211be35a1SLionel Sambuc if (len < t->n) {
106311be35a1SLionel Sambuc for (i = len; i < t->n - 1; ++i) {
106411be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i);
106511be35a1SLionel Sambuc ATF_CHECK(fputc(t->s[i - len], fp)
106611be35a1SLionel Sambuc == t->s[i - len]);
106711be35a1SLionel Sambuc ATF_CHECK(buf[i] == t->s[i - len]);
106811be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)i + 1);
106911be35a1SLionel Sambuc #if !defined(__GLIBC__)
107011be35a1SLionel Sambuc ATF_CHECK(buf[i + 1] == '\0');
107111be35a1SLionel Sambuc #endif
107211be35a1SLionel Sambuc }
107311be35a1SLionel Sambuc
107411be35a1SLionel Sambuc /* don't accept non nul character at end of buffer */
107511be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
107611be35a1SLionel Sambuc ATF_CHECK(fputc(0x1, fp) == EOF);
107711be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
107811be35a1SLionel Sambuc
107911be35a1SLionel Sambuc /* accept nul character at end of buffer */
108011be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n - 1);
108111be35a1SLionel Sambuc ATF_CHECK(fputc('\0', fp) == '\0');
108211be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
108311be35a1SLionel Sambuc }
108411be35a1SLionel Sambuc
108511be35a1SLionel Sambuc /* reach EOF */
108611be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
108711be35a1SLionel Sambuc ATF_CHECK(fputc('\0', fp) == EOF);
108811be35a1SLionel Sambuc ATF_CHECK(ftello(fp) == (off_t)t->n);
108911be35a1SLionel Sambuc
109011be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
109111be35a1SLionel Sambuc }
109211be35a1SLionel Sambuc }
109311be35a1SLionel Sambuc }
109411be35a1SLionel Sambuc
109511be35a1SLionel Sambuc ATF_TC(test22);
ATF_TC_HEAD(test22,tc)109611be35a1SLionel Sambuc ATF_TC_HEAD(test22, tc)
109711be35a1SLionel Sambuc {
109811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "test22");
109911be35a1SLionel Sambuc }
ATF_TC_BODY(test22,tc)110011be35a1SLionel Sambuc ATF_TC_BODY(test22, tc)
110111be35a1SLionel Sambuc {
110211be35a1SLionel Sambuc struct testcase *t0, *t1;
110311be35a1SLionel Sambuc size_t len0, len1, nleft;
110411be35a1SLionel Sambuc const char **p;
110511be35a1SLionel Sambuc char buf[BUFSIZ];
110611be35a1SLionel Sambuc FILE *fp;
110711be35a1SLionel Sambuc
110811be35a1SLionel Sambuc for (t0 = &testcases[0]; t0->s != NULL; ++t0) {
110911be35a1SLionel Sambuc len0 = strnlen(t0->s, t0->n);
111011be35a1SLionel Sambuc for (t1 = &testcases[0]; t1->s != NULL; ++t1) {
111111be35a1SLionel Sambuc len1 = strnlen(t1->s, t1->n);
111211be35a1SLionel Sambuc for (p = &mode_a[0]; *p != NULL; ++p) {
111311be35a1SLionel Sambuc
111411be35a1SLionel Sambuc memcpy(&buf[0], t0->s, t0->n);
111511be35a1SLionel Sambuc fp = fmemopen(&buf[0], t0->n, *p);
111611be35a1SLionel Sambuc ATF_CHECK(fp != NULL);
111711be35a1SLionel Sambuc setbuf(fp, NULL);
111811be35a1SLionel Sambuc /*
111911be35a1SLionel Sambuc * test fmemopen_write + fwrite(3)
112011be35a1SLionel Sambuc */
112111be35a1SLionel Sambuc nleft = t0->n - len0;
112211be35a1SLionel Sambuc #if !defined(__GLIBC__)
112311be35a1SLionel Sambuc if (nleft == 0 || len1 == nleft - 1) {
112411be35a1SLionel Sambuc ATF_CHECK(fwrite(t1->s, 1, t1->n, fp)
112511be35a1SLionel Sambuc == nleft);
112611be35a1SLionel Sambuc ATF_CHECK(ftell(fp) == t1->n);
112711be35a1SLionel Sambuc } else {
112811be35a1SLionel Sambuc ATF_CHECK(fwrite(t1->s, 1, t1->n, fp)
112911be35a1SLionel Sambuc == nleft - 1);
113011be35a1SLionel Sambuc ATF_CHECK(ftell(fp) == t1->n - 1);
113111be35a1SLionel Sambuc }
113211be35a1SLionel Sambuc #endif
113311be35a1SLionel Sambuc ATF_CHECK(fclose(fp) == 0);
113411be35a1SLionel Sambuc }
113511be35a1SLionel Sambuc }
113611be35a1SLionel Sambuc }
113711be35a1SLionel Sambuc }
113811be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)113911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
114011be35a1SLionel Sambuc {
114111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test00);
114211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test01);
114311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test02);
114411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test03);
114511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test04);
114611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test05);
114711be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test06);
114811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test07);
114911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test08);
115011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test09);
115111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test10);
115211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test11);
115311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test12);
115411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test13);
115511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test14);
115611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test15);
115711be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test16);
115811be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test17);
115911be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test18);
116011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test19);
116111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test20);
116211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test21);
116311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, test22);
116411be35a1SLionel Sambuc
116511be35a1SLionel Sambuc return atf_no_error();
116611be35a1SLionel Sambuc }
1167