1 //===-- Unittests for the fopencookie function ----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/stdio/clearerr.h" 10 #include "src/stdio/fclose.h" 11 #include "src/stdio/feof.h" 12 #include "src/stdio/ferror.h" 13 #include "src/stdio/fflush.h" 14 #include "src/stdio/fopencookie.h" 15 #include "src/stdio/fread.h" 16 #include "src/stdio/fseek.h" 17 #include "src/stdio/fwrite.h" 18 #include "utils/UnitTest/MemoryMatcher.h" 19 #include "utils/UnitTest/Test.h" 20 21 #include <errno.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 25 using MemoryView = __llvm_libc::memory::testing::MemoryView; 26 27 struct StringStream { 28 char *buf; 29 size_t bufsize; // Size of buf 30 size_t endpos; // 1 more than current fill size 31 size_t offset; // Current read/write location 32 }; 33 34 ssize_t write_ss(void *cookie, const char *buf, size_t size) { 35 auto *ss = reinterpret_cast<StringStream *>(cookie); 36 if (ss->offset + size > ss->bufsize) 37 ss->buf = 38 reinterpret_cast<char *>(realloc(ss->buf, (ss->offset + size) * 2)); 39 for (size_t i = 0; i < size; ++i, ss->offset += 1) 40 ss->buf[ss->offset] = buf[i]; 41 if (ss->offset > ss->endpos) 42 ss->endpos = ss->offset; 43 return size; 44 } 45 46 ssize_t read_ss(void *cookie, char *buf, size_t size) { 47 auto *ss = reinterpret_cast<StringStream *>(cookie); 48 ssize_t copysize = size; 49 if (ss->offset + size > ss->endpos) { 50 // You cannot copy more than what you have available. 51 copysize = ss->endpos - ss->offset; 52 if (copysize < 0) 53 copysize = 0; // A seek could have moved offset past the endpos 54 } 55 for (size_t i = 0; i < size_t(copysize); ++i, ++ss->offset) 56 buf[i] = ss->buf[ss->offset]; 57 return copysize; 58 } 59 60 int seek_ss(void *cookie, off64_t *offset, int whence) { 61 auto *ss = reinterpret_cast<StringStream *>(cookie); 62 off64_t new_offset; 63 if (whence == SEEK_SET) { 64 new_offset = *offset; 65 } else if (whence == SEEK_CUR) { 66 new_offset = *offset + ss->offset; 67 } else if (whence == SEEK_END) { 68 new_offset = *offset + ss->endpos; 69 } else { 70 errno = EINVAL; 71 return -1; 72 } 73 if (new_offset < 0 || size_t(new_offset) > ss->bufsize) 74 return -1; 75 ss->offset = new_offset; 76 *offset = new_offset; 77 return 0; 78 } 79 80 int close_ss(void *cookie) { 81 auto *ss = reinterpret_cast<StringStream *>(cookie); 82 free(ss->buf); 83 ss->buf = nullptr; 84 ss->bufsize = ss->endpos = ss->offset = 0; 85 return 0; 86 } 87 88 constexpr cookie_io_functions_t STRING_STREAM_FUNCS = {&read_ss, &write_ss, 89 &seek_ss, &close_ss}; 90 91 TEST(LlvmLibcFOpenCookie, ReadOnlyCookieTest) { 92 constexpr char CONTENT[] = "Hello,readonly!"; 93 auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream))); 94 ss->buf = reinterpret_cast<char *>(malloc(sizeof(CONTENT))); 95 ss->bufsize = sizeof(CONTENT); 96 ss->offset = 0; 97 ss->endpos = ss->bufsize; 98 for (size_t i = 0; i < sizeof(CONTENT); ++i) 99 ss->buf[i] = CONTENT[i]; 100 101 ::FILE *f = __llvm_libc::fopencookie(ss, "r", STRING_STREAM_FUNCS); 102 ASSERT_TRUE(f != nullptr); 103 char read_data[sizeof(CONTENT)]; 104 ASSERT_EQ(sizeof(CONTENT), 105 __llvm_libc::fread(read_data, 1, sizeof(CONTENT), f)); 106 ASSERT_STREQ(read_data, CONTENT); 107 108 // Reading another time should trigger eof. 109 ASSERT_NE(sizeof(CONTENT), 110 __llvm_libc::fread(read_data, 1, sizeof(CONTENT), f)); 111 ASSERT_NE(__llvm_libc::feof(f), 0); 112 113 ASSERT_EQ(0, __llvm_libc::fseek(f, 0, SEEK_SET)); 114 // Should be an error to write. 115 ASSERT_EQ(size_t(0), __llvm_libc::fwrite(CONTENT, 1, sizeof(CONTENT), f)); 116 ASSERT_NE(__llvm_libc::ferror(f), 0); 117 ASSERT_NE(errno, 0); 118 errno = 0; 119 120 __llvm_libc::clearerr(f); 121 ASSERT_EQ(__llvm_libc::ferror(f), 0); 122 123 ASSERT_EQ(0, __llvm_libc::fclose(f)); 124 free(ss); 125 } 126 127 TEST(LlvmLibcFOpenCookie, WriteOnlyCookieTest) { 128 size_t INIT_BUFSIZE = 32; 129 auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream))); 130 ss->buf = reinterpret_cast<char *>(malloc(INIT_BUFSIZE)); 131 ss->bufsize = INIT_BUFSIZE; 132 ss->offset = 0; 133 ss->endpos = 0; 134 135 ::FILE *f = __llvm_libc::fopencookie(ss, "w", STRING_STREAM_FUNCS); 136 ASSERT_TRUE(f != nullptr); 137 138 constexpr char WRITE_DATA[] = "Hello,writeonly!"; 139 ASSERT_EQ(sizeof(WRITE_DATA), 140 __llvm_libc::fwrite(WRITE_DATA, 1, sizeof(WRITE_DATA), f)); 141 // Flushing will ensure the data to be written to the string stream. 142 ASSERT_EQ(0, __llvm_libc::fflush(f)); 143 ASSERT_STREQ(WRITE_DATA, ss->buf); 144 145 ASSERT_EQ(0, __llvm_libc::fseek(f, 0, SEEK_SET)); 146 char read_data[sizeof(WRITE_DATA)]; 147 // Should be an error to read. 148 ASSERT_EQ(size_t(0), __llvm_libc::fread(read_data, 1, sizeof(WRITE_DATA), f)); 149 ASSERT_NE(__llvm_libc::ferror(f), 0); 150 ASSERT_EQ(errno, EBADF); 151 errno = 0; 152 153 __llvm_libc::clearerr(f); 154 ASSERT_EQ(__llvm_libc::ferror(f), 0); 155 156 ASSERT_EQ(0, __llvm_libc::fclose(f)); 157 free(ss); 158 } 159 160 TEST(LlvmLibcFOpenCookie, AppendOnlyCookieTest) { 161 constexpr char INITIAL_CONTENT[] = "1234567890987654321"; 162 constexpr char WRITE_DATA[] = "append"; 163 auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream))); 164 ss->buf = reinterpret_cast<char *>(malloc(sizeof(INITIAL_CONTENT))); 165 ss->bufsize = sizeof(INITIAL_CONTENT); 166 ss->offset = ss->bufsize; // We want to open the file in append mode. 167 ss->endpos = ss->bufsize; 168 for (size_t i = 0; i < sizeof(INITIAL_CONTENT); ++i) 169 ss->buf[i] = INITIAL_CONTENT[i]; 170 171 ::FILE *f = __llvm_libc::fopencookie(ss, "a", STRING_STREAM_FUNCS); 172 ASSERT_TRUE(f != nullptr); 173 174 constexpr size_t READ_SIZE = 5; 175 char read_data[READ_SIZE]; 176 // This is not a readable file. 177 ASSERT_EQ(__llvm_libc::fread(read_data, 1, READ_SIZE, f), size_t(0)); 178 ASSERT_NE(__llvm_libc::ferror(f), 0); 179 EXPECT_NE(errno, 0); 180 errno = 0; 181 182 __llvm_libc::clearerr(f); 183 ASSERT_EQ(__llvm_libc::ferror(f), 0); 184 185 ASSERT_EQ(__llvm_libc::fwrite(WRITE_DATA, 1, sizeof(WRITE_DATA), f), 186 sizeof(WRITE_DATA)); 187 EXPECT_EQ(__llvm_libc::fflush(f), 0); 188 EXPECT_EQ(ss->endpos, sizeof(WRITE_DATA) + sizeof(INITIAL_CONTENT)); 189 190 ASSERT_EQ(__llvm_libc::fclose(f), 0); 191 free(ss); 192 } 193 194 TEST(LlvmLibcFOpenCookie, ReadUpdateCookieTest) { 195 const char INITIAL_CONTENT[] = "1234567890987654321"; 196 auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream))); 197 ss->buf = reinterpret_cast<char *>(malloc(sizeof(INITIAL_CONTENT))); 198 ss->bufsize = sizeof(INITIAL_CONTENT); 199 ss->offset = 0; 200 ss->endpos = ss->bufsize; 201 for (size_t i = 0; i < sizeof(INITIAL_CONTENT); ++i) 202 ss->buf[i] = INITIAL_CONTENT[i]; 203 204 ::FILE *f = __llvm_libc::fopencookie(ss, "r+", STRING_STREAM_FUNCS); 205 ASSERT_TRUE(f != nullptr); 206 207 constexpr size_t READ_SIZE = sizeof(INITIAL_CONTENT) / 2; 208 char read_data[READ_SIZE]; 209 ASSERT_EQ(READ_SIZE, __llvm_libc::fread(read_data, 1, READ_SIZE, f)); 210 211 MemoryView src1(INITIAL_CONTENT, READ_SIZE), dst1(read_data, READ_SIZE); 212 EXPECT_MEM_EQ(src1, dst1); 213 214 ASSERT_EQ(__llvm_libc::fseek(f, 0, SEEK_SET), 0); 215 constexpr char WRITE_DATA[] = "hello, file"; 216 ASSERT_EQ(sizeof(WRITE_DATA), 217 __llvm_libc::fwrite(WRITE_DATA, 1, sizeof(WRITE_DATA), f)); 218 ASSERT_EQ(__llvm_libc::fflush(f), 0); 219 EXPECT_STREQ(ss->buf, WRITE_DATA); 220 221 ASSERT_EQ(__llvm_libc::fclose(f), 0); 222 free(ss); 223 } 224 225 TEST(LlvmLibcFOpenCookie, WriteUpdateCookieTest) { 226 constexpr char WRITE_DATA[] = "hello, file"; 227 auto *ss = reinterpret_cast<StringStream *>(malloc(sizeof(StringStream))); 228 ss->buf = reinterpret_cast<char *>(malloc(sizeof(WRITE_DATA))); 229 ss->bufsize = sizeof(WRITE_DATA); 230 ss->offset = 0; 231 ss->endpos = 0; 232 233 ::FILE *f = __llvm_libc::fopencookie(ss, "w+", STRING_STREAM_FUNCS); 234 ASSERT_TRUE(f != nullptr); 235 236 ASSERT_EQ(sizeof(WRITE_DATA), 237 __llvm_libc::fwrite(WRITE_DATA, 1, sizeof(WRITE_DATA), f)); 238 239 ASSERT_EQ(__llvm_libc::fseek(f, 0, SEEK_SET), 0); 240 241 char read_data[sizeof(WRITE_DATA)]; 242 ASSERT_EQ(__llvm_libc::fread(read_data, 1, sizeof(read_data), f), 243 sizeof(read_data)); 244 EXPECT_STREQ(read_data, WRITE_DATA); 245 246 ASSERT_EQ(__llvm_libc::fclose(f), 0); 247 free(ss); 248 } 249