Changeset 82832 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- Jan 22, 2020 4:21:56 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/testcase/tstFile.cpp
r80585 r82832 29 29 * Header Files * 30 30 *********************************************************************************************************************************/ 31 #include <iprt/test.h> 31 32 #include <iprt/file.h> 32 33 #include <iprt/errcore.h> 33 34 #include <iprt/string.h> 34 35 #include <iprt/stream.h> 35 #include <iprt/initterm.h> 36 37 38 int main() 36 37 38 /********************************************************************************************************************************* 39 * Global Variables * 40 *********************************************************************************************************************************/ 41 static const char g_szTestStr[] = "Sausages and bacon for breakfast again!\n"; 42 static char g_szTestStr2[] = 43 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " 44 "enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor " 45 "in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " 46 "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n" 47 "\n" 48 "Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum " 49 "elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus " 50 "vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod " 51 "turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. " 52 "Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl " 53 "adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur " 54 "augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac " 55 "habitasse platea dictumst.\n"; 56 57 58 static void tstAppend(RTFILE hFile) 39 59 { 40 int cErrors = 0; 41 RTPrintf("tstFile: TESTING\n"); 42 RTR3InitExeNoArguments(0); 43 44 RTFILE File; 45 int rc = RTFileOpen(&File, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE); 60 char achBuf[sizeof(g_szTestStr2) * 4]; 61 62 /* 63 * Write some stuff and read it back. 64 */ 65 size_t const cbWrite1 = sizeof(g_szTestStr2) / 4; 66 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, g_szTestStr2, sizeof(g_szTestStr2) - 1, NULL), VINF_SUCCESS); 67 68 size_t const offWrite2 = cbWrite1; 69 size_t const cbWrite2 = sizeof(g_szTestStr2) / 2; 70 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); 71 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite2], cbWrite2, NULL), VINF_SUCCESS); 72 73 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); 74 RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite1 + cbWrite2, NULL), VINF_SUCCESS); 75 if (memcmp(achBuf, g_szTestStr2, cbWrite1 + cbWrite2) != 0) 76 RTTestIFailed("Read back #1 failed (%#zx + %#zx)", cbWrite1, cbWrite2); 77 78 #ifndef RT_OS_WINDOWS 79 /* 80 * Truncate the file and write some more. This is problematic on windows. 81 */ 82 RTTESTI_CHECK_RC_RETV(RTFileSetSize(hFile, 0), VINF_SUCCESS); 83 84 size_t const offWrite3 = cbWrite1 + cbWrite2; 85 size_t const cbWrite3 = sizeof(g_szTestStr2) - 1 - offWrite3; 86 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); 87 RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite3], cbWrite3, NULL), VINF_SUCCESS); 88 89 RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS); 90 RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite3, NULL), VINF_SUCCESS); 91 if (memcmp(achBuf, &g_szTestStr2[offWrite3], cbWrite3) != 0) 92 RTTestIFailed("Read back #2 failed (%#zx)", cbWrite3); 93 #endif 94 } 95 96 97 static void tstBasics(RTFILE hFile) 98 { 99 RTFOFF cbMax = -2; 100 int rc = RTFileQueryMaxSizeEx(hFile, &cbMax); 101 if (rc != VERR_NOT_IMPLEMENTED) 102 { 103 if (rc != VINF_SUCCESS) 104 RTTestIFailed("RTFileQueryMaxSizeEx failed: %Rrc", rc); 105 else 106 { 107 RTTESTI_CHECK_MSG(cbMax > 0, ("cbMax=%RTfoff", cbMax)); 108 RTTESTI_CHECK_MSG(cbMax == RTFileGetMaxSize(hFile), 109 ("cbMax=%RTfoff, RTFileGetMaxSize->%RTfoff", cbMax, RTFileGetMaxSize(hFile))); 110 } 111 } 112 113 /* grow file beyond 2G */ 114 rc = RTFileSetSize(hFile, _2G + _1M); 46 115 if (RT_FAILURE(rc)) 47 { 48 RTPrintf("tstFile: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc); 49 return 1; 50 } 51 52 RTFOFF cbMax = -2; 53 rc = RTFileQueryMaxSizeEx(File, &cbMax); 54 if (RT_FAILURE(rc)) 55 { 56 RTPrintf("tstFile: RTFileQueryMaxSizeEx failed: %Rrc\n", rc); 57 cErrors++; 58 } 59 else if (cbMax <= 0) 60 { 61 RTPrintf("tstFile: RTFileQueryMaxSizeEx failed: cbMax=%RTfoff\n", cbMax); 62 cErrors++; 63 } 64 else if (RTFileGetMaxSize(File) != cbMax) 65 { 66 RTPrintf("tstFile: RTFileGetMaxSize failed; returns %RTfoff instead of %RTfoff\n", RTFileGetMaxSize(File), cbMax); 67 cErrors++; 68 } 69 else 70 RTPrintf("Maximum file size is %RTfoff bytes.\n", cbMax); 71 72 /* grow file beyond 2G */ 73 rc = RTFileSetSize(File, _2G + _1M); 74 if (RT_FAILURE(rc)) 75 { 76 RTPrintf("Failed to grow file #1 to 2.001GB. rc=%Rrc\n", rc); 77 cErrors++; 78 } 116 RTTestIFailed("Failed to grow file #1 to 2.001GB. rc=%Rrc", rc); 79 117 else 80 118 { 81 119 uint64_t cb; 82 rc = RTFileQuerySize(File, &cb); 83 if (RT_FAILURE(rc)) 84 { 85 RTPrintf("Failed to get file size of #1. rc=%Rrc\n", rc); 86 cErrors++; 87 } 88 else if (cb != _2G + _1M) 89 { 90 RTPrintf("RTFileQuerySize return %RX64 bytes, expected %RX64.\n", cb, _2G + _1M); 91 cErrors++; 92 } 93 else 94 RTPrintf("tstFile: cb=%RX64\n", cb); 120 RTTESTI_CHECK_RC(RTFileQuerySize(hFile, &cb), VINF_SUCCESS); 121 RTTESTI_CHECK_MSG(cb == _2G + _1M, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, _2G + _1M)); 95 122 96 123 /* 97 124 * Try some writes at the beginning of the file. 98 125 */ 99 uint64_t offFile = RTFileTell(File); 100 if (offFile != 0) 101 { 102 RTPrintf("RTFileTell -> %#RX64, expected 0 (#1)\n", offFile); 103 cErrors++; 104 } 105 static const char szTestBuf[] = "Sausages and bacon for breakfast again!"; 126 uint64_t offFile = RTFileTell(hFile); 127 RTTESTI_CHECK_MSG(offFile == 0, ("RTFileTell -> %#RX64, expected 0 (#1)", offFile)); 128 106 129 size_t cbWritten = 0; 107 while (cbWritten < sizeof( szTestBuf))130 while (cbWritten < sizeof(g_szTestStr)) 108 131 { 109 132 size_t cbWrittenPart; 110 rc = RTFileWrite( File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);133 rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart); 111 134 if (RT_FAILURE(rc)) 112 135 break; … … 114 137 } 115 138 if (RT_FAILURE(rc)) 116 { 117 RTPrintf("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc); 118 cErrors++; 119 } 139 RTTestIFailed("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc); 120 140 else 121 141 { 122 142 /* check that it was written correctly. */ 123 rc = RTFileSeek( File, 0, RTFILE_SEEK_BEGIN, NULL);143 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL); 124 144 if (RT_FAILURE(rc)) 125 { 126 RTPrintf("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc); 127 cErrors++; 128 } 145 RTTestIFailed("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc); 129 146 else 130 147 { 131 char szReadBuf[sizeof( szTestBuf)];148 char szReadBuf[sizeof(g_szTestStr)]; 132 149 size_t cbRead = 0; 133 while (cbRead < sizeof( szTestBuf))150 while (cbRead < sizeof(g_szTestStr)) 134 151 { 135 152 size_t cbReadPart; 136 rc = RTFileRead( File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);153 rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart); 137 154 if (RT_FAILURE(rc)) 138 155 break; … … 140 157 } 141 158 if (RT_FAILURE(rc)) 142 { 143 RTPrintf("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc); 144 cErrors++; 145 } 159 RTTestIFailed("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc); 146 160 else 147 161 { 148 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))162 if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr))) 149 163 RTPrintf("tstFile: head write ok\n"); 150 164 else 151 { 152 RTPrintf("Data read from file #1 at offset 0 differs from what we wrote there.\n"); 153 cErrors++; 154 } 165 RTTestIFailed("Data read from file #1 at offset 0 differs from what we wrote there.\n"); 155 166 } 156 167 } … … 160 171 * Try some writes at the end of the file. 161 172 */ 162 rc = RTFileSeek(File, _2G + _1M, RTFILE_SEEK_BEGIN, NULL); 163 if (RT_FAILURE(rc)) 164 { 165 RTPrintf("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc); 166 cErrors++; 167 } 168 else 169 { 170 offFile = RTFileTell(File); 173 rc = RTFileSeek(hFile, _2G + _1M, RTFILE_SEEK_BEGIN, NULL); 174 if (RT_FAILURE(rc)) 175 RTTestIFailed("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc); 176 else 177 { 178 offFile = RTFileTell(hFile); 171 179 if (offFile != _2G + _1M) 172 { 173 RTPrintf("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M); 174 cErrors++; 175 } 180 RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M); 176 181 else 177 182 { 178 183 cbWritten = 0; 179 while (cbWritten < sizeof( szTestBuf))184 while (cbWritten < sizeof(g_szTestStr)) 180 185 { 181 186 size_t cbWrittenPart; 182 rc = RTFileWrite( File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);187 rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart); 183 188 if (RT_FAILURE(rc)) 184 189 break; … … 186 191 } 187 192 if (RT_FAILURE(rc)) 188 { 189 RTPrintf("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc); 190 cErrors++; 191 } 193 RTTestIFailed("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc); 192 194 else 193 195 { 194 rc = RTFileSeek( File, offFile, RTFILE_SEEK_BEGIN, NULL);196 rc = RTFileSeek(hFile, offFile, RTFILE_SEEK_BEGIN, NULL); 195 197 if (RT_FAILURE(rc)) 196 { 197 RTPrintf("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc); 198 cErrors++; 199 } 198 RTTestIFailed("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc); 200 199 else 201 200 { 202 char szReadBuf[sizeof( szTestBuf)];201 char szReadBuf[sizeof(g_szTestStr)]; 203 202 size_t cbRead = 0; 204 while (cbRead < sizeof( szTestBuf))203 while (cbRead < sizeof(g_szTestStr)) 205 204 { 206 205 size_t cbReadPart; 207 rc = RTFileRead( File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);206 rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart); 208 207 if (RT_FAILURE(rc)) 209 208 break; … … 211 210 } 212 211 if (RT_FAILURE(rc)) 213 { 214 RTPrintf("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc); 215 cErrors++; 216 } 212 RTTestIFailed("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc); 217 213 else 218 214 { 219 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))215 if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr))) 220 216 RTPrintf("tstFile: tail write ok\n"); 221 217 else 222 { 223 RTPrintf("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n"); 224 cErrors++; 225 } 218 RTTestIFailed("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n"); 226 219 } 227 220 } … … 233 226 * Some general seeking around. 234 227 */ 235 rc = RTFileSeek(File, _2G + 1, RTFILE_SEEK_BEGIN, NULL); 236 if (RT_FAILURE(rc)) 237 { 238 RTPrintf("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc); 239 cErrors++; 240 } 241 else 242 { 243 offFile = RTFileTell(File); 228 rc = RTFileSeek(hFile, _2G + 1, RTFILE_SEEK_BEGIN, NULL); 229 if (RT_FAILURE(rc)) 230 RTTestIFailed("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc); 231 else 232 { 233 offFile = RTFileTell(hFile); 244 234 if (offFile != _2G + 1) 245 { 246 RTPrintf("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1); 247 cErrors++; 248 } 235 RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1); 249 236 } 250 237 251 238 /* seek end */ 252 rc = RTFileSeek(File, 0, RTFILE_SEEK_END, NULL); 253 if (RT_FAILURE(rc)) 254 { 255 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc); 256 cErrors++; 257 } 258 else 259 { 260 offFile = RTFileTell(File); 261 if (offFile != _2G + _1M + sizeof(szTestBuf)) /* assuming tail write was ok. */ 262 { 263 RTPrintf("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(szTestBuf)); 264 cErrors++; 265 } 239 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, NULL); 240 if (RT_FAILURE(rc)) 241 RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc); 242 else 243 { 244 offFile = RTFileTell(hFile); 245 if (offFile != _2G + _1M + sizeof(g_szTestStr)) /* assuming tail write was ok. */ 246 RTTestIFailed("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(g_szTestStr)); 266 247 } 267 248 268 249 /* seek start */ 269 rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL); 270 if (RT_FAILURE(rc)) 271 { 272 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc); 273 cErrors++; 274 } 275 else 276 { 277 offFile = RTFileTell(File); 250 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL); 251 if (RT_FAILURE(rc)) 252 RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc); 253 else 254 { 255 offFile = RTFileTell(hFile); 278 256 if (offFile != 0) 279 { 280 RTPrintf("RTFileTell -> %#llx, expected 0 (#5)\n", offFile); 281 cErrors++; 282 } 257 RTTestIFailed("RTFileTell -> %#llx, expected 0 (#5)\n", offFile); 283 258 } 284 259 } 285 286 287 /* 288 * Cleanup. 289 */ 290 rc = RTFileClose(File); 291 if (RT_FAILURE(rc)) 260 } 261 262 int main() 263 { 264 RTTEST hTest; 265 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTFile", &hTest); 266 if (rcExit != RTEXITCODE_SUCCESS) 267 return rcExit; 268 RTTestBanner(hTest); 269 270 /* 271 * Some basic tests. 272 */ 273 RTTestSub(hTest, "Basics"); 274 int rc = -1; 275 RTFILE hFile = NIL_RTFILE; 276 RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE), 277 VINF_SUCCESS); 278 if (RT_SUCCESS(rc)) 292 279 { 293 RTPrintf("Failed to close file #1. rc=%Rrc\n", rc); 294 cErrors++; 280 tstBasics(hFile); 281 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS); 282 RTTESTI_CHECK_RC(RTFileDelete("tstFile#1.tst"), VINF_SUCCESS); 295 283 } 296 rc = RTFileDelete("tstFile#1.tst"); 297 if (RT_FAILURE(rc)) 284 285 /* 286 * Test appending & truncation. 287 */ 288 RTTestSub(hTest, "Basics"); 289 hFile = NIL_RTFILE; 290 RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#2.tst", 291 RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_APPEND), 292 VINF_SUCCESS); 293 if (RT_SUCCESS(rc)) 298 294 { 299 RTPrintf("Failed to delete file #1. rc=%Rrc\n", rc); 300 cErrors++; 295 tstAppend(hFile); 296 RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS); 297 RTTESTI_CHECK_RC(RTFileDelete("tstFile#2.tst"), VINF_SUCCESS); 301 298 } 302 299 303 300 /* 304 * Summary 305 */ 306 if (cErrors == 0) 307 RTPrintf("tstFile: SUCCESS\n"); 308 else 309 RTPrintf("tstFile: FAILURE - %d errors\n", cErrors); 310 return !!cErrors; 301 * Done. 302 */ 303 return RTTestSummaryAndDestroy(hTest); 311 304 } 312 305
Note:
See TracChangeset
for help on using the changeset viewer.