lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* fmemopen tests. |
| 2 | Copyright (C) 2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <errno.h> |
| 25 | |
| 26 | /* Check fmemopen with user provided buffer open for write. */ |
| 27 | static int |
| 28 | do_test_with_buffer (void) |
| 29 | { |
| 30 | int result = 0; |
| 31 | char buf[100]; |
| 32 | const size_t nbuf = sizeof (buf); |
| 33 | |
| 34 | FILE *fp = fmemopen (buf, nbuf, "w"); |
| 35 | if (fp == NULL) |
| 36 | { |
| 37 | printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__); |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | /* Default write operation, check if file position is correct after it. */ |
| 42 | static const char str[] = "hello world"; |
| 43 | const size_t nstr = sizeof (str) - 1; |
| 44 | fputs (str, fp); |
| 45 | off_t o = ftello (fp); |
| 46 | if (o != nstr) |
| 47 | { |
| 48 | printf ("FAIL: first ftello returned %jd, expected %zu\n", |
| 49 | (intmax_t)o, nstr); |
| 50 | result = 1; |
| 51 | } |
| 52 | |
| 53 | /* Rewind stream and seek tests, the position size should be equal to |
| 54 | buffer size provided in open function. */ |
| 55 | rewind (fp); |
| 56 | o = ftello (fp); |
| 57 | if (o != 0) |
| 58 | { |
| 59 | printf ("FAIL: second ftello returned %jd, expected 0\n", |
| 60 | (intmax_t)o); |
| 61 | result = 1; |
| 62 | } |
| 63 | if (fseeko (fp, 0, SEEK_END) != 0) |
| 64 | { |
| 65 | printf ("FAIL: fseeko failed\n"); |
| 66 | result = 1; |
| 67 | } |
| 68 | o = ftello (fp); |
| 69 | if (o != nstr) |
| 70 | { |
| 71 | printf ("FAIL: third ftello returned %jd, expected %zu\n", |
| 72 | (intmax_t)o, nstr); |
| 73 | result = 1; |
| 74 | } |
| 75 | |
| 76 | /* Rewind the stream and recheck by using a shorter string. */ |
| 77 | rewind (fp); |
| 78 | static const char str2[] = "just hello"; |
| 79 | const size_t nstr2 = sizeof (str2) - 1; |
| 80 | assert (nstr2 < nstr); |
| 81 | fputs (str2, fp); |
| 82 | o = ftello (fp); |
| 83 | if (o != nstr2) |
| 84 | { |
| 85 | printf ("FAIL: fourth ftello returned %jd, expected %zu\n", |
| 86 | (intmax_t)o, nstr2); |
| 87 | result = 1; |
| 88 | } |
| 89 | fclose (fp); |
| 90 | |
| 91 | /* Again, but now with a larger string. */ |
| 92 | static const char str3[] = "just hellod"; |
| 93 | if (strcmp (buf, str3) != 0) |
| 94 | { |
| 95 | printf ("FAIL: final string is \"%s\", expected \"%s\"\n", |
| 96 | buf, str3); |
| 97 | result = 1; |
| 98 | } |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | /* Check fmemopen without user provided buffer open for write. */ |
| 103 | static int |
| 104 | do_test_without_buffer (void) |
| 105 | { |
| 106 | int result = 0; |
| 107 | const size_t nbuf = 100; |
| 108 | |
| 109 | FILE *fp = fmemopen (NULL, nbuf, "w"); |
| 110 | if (fp == NULL) |
| 111 | { |
| 112 | printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__); |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | static const char str[] = "hello world"; |
| 117 | const size_t nstr = sizeof (str) - 1; |
| 118 | |
| 119 | /* Default write operation, check if file position is correct after it. */ |
| 120 | fputs (str, fp); |
| 121 | off_t o = ftello (fp); |
| 122 | if (o != nstr) |
| 123 | { |
| 124 | printf ("FAIL: first ftello returned %jd, expected %zu\n", |
| 125 | (intmax_t) o, nstr); |
| 126 | result = 1; |
| 127 | } |
| 128 | if (fseeko (fp, 0, SEEK_END) != 0) |
| 129 | { |
| 130 | printf ("FAIL: fseeko failed\n"); |
| 131 | result = 1; |
| 132 | } |
| 133 | o = ftello (fp); |
| 134 | if (o != nstr) |
| 135 | { |
| 136 | printf ("FAIL: second ftello returned %jd, expected %zu\n", |
| 137 | (intmax_t) o, nbuf); |
| 138 | result = 1; |
| 139 | } |
| 140 | |
| 141 | /* Rewind the stream and recheck by using a shorter string. */ |
| 142 | rewind (fp); |
| 143 | static const char str2[] = "just hello"; |
| 144 | const size_t nstr2 = sizeof (str2) - 1; |
| 145 | assert (nstr2 < nstr); |
| 146 | fputs (str2, fp); |
| 147 | o = ftello (fp); |
| 148 | if (o != nstr2) |
| 149 | { |
| 150 | printf ("FAIL: third ftello returned %jd, expected %zu\n", |
| 151 | (intmax_t) o, nstr2); |
| 152 | result = 1; |
| 153 | } |
| 154 | fclose (fp); |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | /* Check fmemopen with a buffer lenght of zero. */ |
| 160 | static int |
| 161 | do_test_length_zero (void) |
| 162 | { |
| 163 | int result = 0; |
| 164 | FILE *fp; |
| 165 | #define BUFCONTENTS "testing buffer" |
| 166 | char buf[100] = BUFCONTENTS; |
| 167 | const size_t nbuf = 0; |
| 168 | int r; |
| 169 | |
| 170 | fp = fmemopen (buf, nbuf, "r"); |
| 171 | if (fp == NULL) |
| 172 | { |
| 173 | printf ("FAIL: fmemopen failed (%s)\n", __FUNCTION__); |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | /* Reading any data on a zero-length buffer should return EOF. */ |
| 178 | if ((r = fgetc (fp)) != EOF) |
| 179 | { |
| 180 | printf ("FAIL: fgetc on a zero-length returned: %d\n", r); |
| 181 | result = 1; |
| 182 | } |
| 183 | off_t o = ftello (fp); |
| 184 | if (o != 0) |
| 185 | { |
| 186 | printf ("FAIL: first ftello returned %jd, expected 0\n", |
| 187 | (intmax_t) o); |
| 188 | result = 1; |
| 189 | } |
| 190 | fclose (fp); |
| 191 | |
| 192 | /* Writing any data shall start at current position and shall not pass |
| 193 | current buffer size beyond the size in fmemopen call. */ |
| 194 | fp = fmemopen (buf, nbuf, "w"); |
| 195 | if (fp == NULL) |
| 196 | { |
| 197 | printf ("FAIL: second fmemopen failed (%s)\n", __FUNCTION__); |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | static const char str[] = "hello world"; |
| 202 | /* Because of buffering, the fputs call itself will not fail. However the |
| 203 | final buffer should be not changed because length 0 was passed to the |
| 204 | fmemopen call. */ |
| 205 | fputs (str, fp); |
| 206 | r = 0; |
| 207 | errno = 0; |
| 208 | if (fflush (fp) != EOF) |
| 209 | { |
| 210 | printf ("FAIL: fflush did not return EOF\n"); |
| 211 | fclose (fp); |
| 212 | return 1; |
| 213 | } |
| 214 | if (errno != ENOSPC) |
| 215 | { |
| 216 | printf ("FAIL: errno is %i (expected ENOSPC)\n", errno); |
| 217 | fclose (fp); |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | fclose (fp); |
| 222 | |
| 223 | if (strcmp (buf, BUFCONTENTS) != 0) |
| 224 | { |
| 225 | printf ("FAIL: strcmp (%s, %s) failed\n", buf, BUFCONTENTS); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | /* Different than 'w' mode, 'w+' truncates the buffer. */ |
| 230 | fp = fmemopen (buf, nbuf, "w+"); |
| 231 | if (fp == NULL) |
| 232 | { |
| 233 | printf ("FAIL: third fmemopen failed (%s)\n", __FUNCTION__); |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | fclose (fp); |
| 238 | |
| 239 | if (strcmp (buf, "") != 0) |
| 240 | { |
| 241 | printf ("FAIL: strcmp (%s, \"\") failed\n", buf); |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | return result; |
| 246 | } |
| 247 | |
| 248 | static int |
| 249 | do_test (void) |
| 250 | { |
| 251 | int ret = 0; |
| 252 | |
| 253 | ret += do_test_with_buffer (); |
| 254 | ret += do_test_without_buffer (); |
| 255 | ret += do_test_length_zero (); |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | #define TEST_FUNCTION do_test () |
| 261 | #include "../test-skeleton.c" |