]>
Commit | Line | Data |
---|---|---|
b061a43b A |
1 | /* |
2 | * Based on the OpenBSD test | |
3 | * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org> | |
4 | * | |
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. | |
8 | * | |
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. | |
16 | */ | |
17 | ||
18 | #include <sys/cdefs.h> | |
19 | __RCSID("$NetBSD: t_open_memstream.c,v 1.2 2014/10/19 11:17:43 justin Exp $"); | |
20 | ||
21 | #include <err.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <unistd.h> | |
26 | ||
27 | #include <darwintest.h> | |
28 | ||
29 | #define OFFSET 16384 | |
30 | ||
31 | static const char start[] = "start"; | |
32 | static const char hello[] = "hello"; | |
33 | ||
34 | T_DECL(netbsd_open_memstream_test_open_memstream, "") | |
35 | { | |
36 | FILE *fp; | |
37 | char *buf = (char *)0xff; | |
38 | size_t size = 0; | |
39 | off_t off; | |
40 | int i; | |
41 | ||
42 | fp = open_memstream(&buf, &size); | |
43 | T_ASSERT_NOTNULL(fp, NULL); | |
44 | ||
45 | off = ftello(fp); | |
46 | T_EXPECT_EQ(off, 0LL, NULL); | |
47 | ||
48 | T_EXPECT_POSIX_ZERO(fflush(fp), NULL); | |
49 | T_EXPECT_EQ(size, 0UL, NULL); | |
50 | T_EXPECT_NE((void*)buf, (void *)0xff, NULL); | |
51 | T_EXPECT_EQ(fseek(fp, -6, SEEK_SET), -1, NULL); | |
52 | T_EXPECT_POSIX_ZERO(fseek(fp, OFFSET, SEEK_SET), NULL); | |
53 | T_EXPECT_NE(fprintf(fp, hello), EOF, NULL); | |
54 | T_EXPECT_NE(fflush(fp), EOF, NULL); | |
55 | T_EXPECT_EQ(size, OFFSET + sizeof(hello)-1, NULL); | |
56 | T_EXPECT_POSIX_ZERO(fseek(fp, 0, SEEK_SET), NULL); | |
57 | T_EXPECT_NE(fprintf(fp, start), EOF, NULL); | |
58 | T_EXPECT_NE(fflush(fp), EOF, NULL); | |
59 | T_EXPECT_EQ(size, sizeof(start)-1, NULL); | |
60 | ||
61 | /* Needed for sparse files */ | |
62 | T_EXPECT_EQ(strncmp(buf, start, sizeof(start)-1), 0, NULL); | |
63 | for (i = sizeof(start)-1; i < OFFSET; i++) | |
64 | T_EXPECT_EQ(buf[i], '\0', NULL); | |
65 | ||
66 | T_EXPECT_EQ(memcmp(buf + OFFSET, hello, sizeof(hello)-1), 0, NULL); | |
67 | ||
68 | /* verify that simply seeking past the end doesn't increase the size */ | |
69 | T_EXPECT_POSIX_ZERO(fseek(fp, 100, SEEK_END), NULL); | |
70 | T_EXPECT_NE(fflush(fp), EOF, NULL); | |
71 | T_EXPECT_EQ(size, OFFSET + sizeof(hello)-1, NULL); | |
72 | T_EXPECT_POSIX_ZERO(fseek(fp, 8, SEEK_SET), NULL); | |
73 | T_EXPECT_EQ(ftell(fp), 8L, NULL); | |
74 | ||
75 | /* Try to seek backward */ | |
76 | T_EXPECT_POSIX_ZERO(fseek(fp, -1, SEEK_CUR), NULL); | |
77 | T_EXPECT_EQ(ftell(fp), 7L, NULL); | |
78 | T_EXPECT_POSIX_ZERO(fseek(fp, 5, SEEK_CUR), NULL); | |
79 | T_EXPECT_NE(fclose(fp), EOF, NULL); | |
80 | T_EXPECT_EQ(size, 12UL, NULL); | |
81 | ||
82 | free(buf); | |
83 | } |