2 * Based on the OpenBSD test
3 * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/cdefs.h>
19 __RCSID("$NetBSD: t_open_memstream.c,v 1.2 2014/10/19 11:17:43 justin Exp $");
28 ATF_TC(test_open_memstream
);
29 ATF_TC_HEAD(test_open_memstream
, tc
)
31 atf_tc_set_md_var(tc
, "descr", "Test open_memstream functionality");
36 const char start
[] = "start";
37 const char hello
[] = "hello";
39 ATF_TC_BODY(test_open_memstream
, tc
)
42 char *buf
= (char *)0xff;
47 fp
= open_memstream(&buf
, &size
);
48 ATF_REQUIRE(fp
!= NULL
);
53 ATF_CHECK(fflush(fp
) == 0);
55 ATF_CHECK(buf
!= (char *)0xff);
56 ATF_CHECK(fseek(fp
, -6, SEEK_SET
) == -1);
57 ATF_CHECK(fseek(fp
, OFFSET
, SEEK_SET
) == 0);
58 ATF_CHECK(fprintf(fp
, hello
) != EOF
);
59 ATF_CHECK(fflush(fp
) != EOF
);
60 ATF_CHECK(size
== OFFSET
+ sizeof(hello
)-1);
61 ATF_CHECK(fseek(fp
, 0, SEEK_SET
) == 0);
62 ATF_CHECK(fprintf(fp
, start
) != EOF
);
63 ATF_CHECK(fflush(fp
) != EOF
);
64 ATF_CHECK(size
== sizeof(start
)-1);
66 /* Needed for sparse files */
67 ATF_CHECK(strncmp(buf
, start
, sizeof(start
)-1) == 0);
68 for (i
= sizeof(start
)-1; i
< OFFSET
; i
++)
69 ATF_CHECK(buf
[i
] == '\0');
71 ATF_CHECK(memcmp(buf
+ OFFSET
, hello
, sizeof(hello
)-1) == 0);
73 /* verify that simply seeking past the end doesn't increase the size */
74 ATF_CHECK(fseek(fp
, 100, SEEK_END
) == 0);
75 ATF_CHECK(fflush(fp
) != EOF
);
76 ATF_CHECK(size
== OFFSET
+ sizeof(hello
)-1);
77 ATF_CHECK(fseek(fp
, 8, SEEK_SET
) == 0);
78 ATF_CHECK(ftell(fp
) == 8);
80 /* Try to seek backward */
81 ATF_CHECK(fseek(fp
, -1, SEEK_CUR
) == 0);
82 ATF_CHECK(ftell(fp
) == 7);
83 ATF_CHECK(fseek(fp
, 5, SEEK_CUR
) == 0);
84 ATF_CHECK(fclose(fp
) != EOF
);
85 ATF_CHECK(size
== 12);
93 ATF_TP_ADD_TC(tp
, test_open_memstream
);
95 return atf_no_error();