1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/testfile.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,961 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nspr.h" 1.10 +#include "prpriv.h" 1.11 + 1.12 +#include <stdio.h> 1.13 +#include <stdlib.h> 1.14 +#include <string.h> 1.15 +#ifdef WIN32 1.16 +#include <windows.h> 1.17 +#include <process.h> 1.18 +#endif 1.19 +#if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) 1.20 +#include <pthread.h> 1.21 +#endif 1.22 +#ifdef SYMBIAN 1.23 +#include <getopt.h> 1.24 +#endif 1.25 + 1.26 +#if defined(XP_OS2) 1.27 +#define INCL_DOSFILEMGR 1.28 +#include <os2.h> 1.29 +#include <getopt.h> 1.30 +#include <errno.h> 1.31 +#endif /* XP_OS2 */ 1.32 + 1.33 +static int _debug_on = 0; 1.34 + 1.35 +#ifdef WINCE 1.36 +#define setbuf(x,y) 1.37 +#endif 1.38 + 1.39 +#ifdef XP_WIN 1.40 +#define mode_t int 1.41 +#endif 1.42 + 1.43 +#define DPRINTF(arg) if (_debug_on) printf arg 1.44 + 1.45 +PRLock *lock; 1.46 +PRMonitor *mon; 1.47 +PRInt32 count; 1.48 +int thread_count; 1.49 + 1.50 +#ifdef WIN16 1.51 +#define BUF_DATA_SIZE 256 * 120 1.52 +#else 1.53 +#define BUF_DATA_SIZE 256 * 1024 1.54 +#endif 1.55 + 1.56 +#define NUM_RDWR_THREADS 10 1.57 +#define NUM_DIRTEST_THREADS 4 1.58 +#define CHUNK_SIZE 512 1.59 + 1.60 +typedef struct buffer { 1.61 + char data[BUF_DATA_SIZE]; 1.62 +} buffer; 1.63 + 1.64 +typedef struct File_Rdwr_Param { 1.65 + char *pathname; 1.66 + char *buf; 1.67 + int offset; 1.68 + int len; 1.69 +} File_Rdwr_Param; 1.70 + 1.71 +#ifdef XP_PC 1.72 +#ifdef XP_OS2 1.73 +char *TEST_DIR = "prdir"; 1.74 +#else 1.75 +char *TEST_DIR = "C:\\temp\\prdir"; 1.76 +#endif 1.77 +char *FILE_NAME = "pr_testfile"; 1.78 +char *HIDDEN_FILE_NAME = "hidden_pr_testfile"; 1.79 +#else 1.80 +#ifdef SYMBIAN 1.81 +char *TEST_DIR = "c:\\data\\testfile_dir"; 1.82 +#else 1.83 +char *TEST_DIR = "/tmp/testfile_dir"; 1.84 +#endif 1.85 +char *FILE_NAME = "pr_testfile"; 1.86 +char *HIDDEN_FILE_NAME = ".hidden_pr_testfile"; 1.87 +#endif 1.88 +buffer *in_buf, *out_buf; 1.89 +char pathname[256], renamename[256]; 1.90 +#ifdef WINCE 1.91 +WCHAR wPathname[256]; 1.92 +#endif 1.93 +#define TMPDIR_LEN 64 1.94 +char testdir[TMPDIR_LEN]; 1.95 +static PRInt32 PR_CALLBACK DirTest(void *argunused); 1.96 +PRInt32 dirtest_failed = 0; 1.97 + 1.98 +PRThread* create_new_thread(PRThreadType type, 1.99 + void (*start)(void *arg), 1.100 + void *arg, 1.101 + PRThreadPriority priority, 1.102 + PRThreadScope scope, 1.103 + PRThreadState state, 1.104 + PRUint32 stackSize, PRInt32 index) 1.105 +{ 1.106 +PRInt32 native_thread = 0; 1.107 + 1.108 + PR_ASSERT(state == PR_UNJOINABLE_THREAD); 1.109 + 1.110 +#if (defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)) || defined(WIN32) || defined(XP_OS2) 1.111 + 1.112 + switch(index % 4) { 1.113 + case 0: 1.114 + scope = (PR_LOCAL_THREAD); 1.115 + break; 1.116 + case 1: 1.117 + scope = (PR_GLOBAL_THREAD); 1.118 + break; 1.119 + case 2: 1.120 + scope = (PR_GLOBAL_BOUND_THREAD); 1.121 + break; 1.122 + case 3: 1.123 + native_thread = 1; 1.124 + break; 1.125 + default: 1.126 + PR_ASSERT(!"Invalid scope"); 1.127 + break; 1.128 + } 1.129 + if (native_thread) { 1.130 +#if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) 1.131 + pthread_t tid; 1.132 + if (!pthread_create(&tid, NULL, start, arg)) 1.133 + return((PRThread *) tid); 1.134 + else 1.135 + return (NULL); 1.136 +#elif defined(XP_OS2) 1.137 + TID tid; 1.138 + 1.139 + tid = (TID)_beginthread((void(* _Optlink)(void*))start, 1.140 + NULL, 32768, arg); 1.141 + if (tid == -1) { 1.142 + printf("_beginthread failed. errno %d\n", errno); 1.143 + return (NULL); 1.144 + } 1.145 + else 1.146 + return((PRThread *) tid); 1.147 +#else 1.148 + HANDLE thandle; 1.149 + unsigned tid; 1.150 + 1.151 + thandle = (HANDLE) _beginthreadex( 1.152 + NULL, 1.153 + stackSize, 1.154 + (unsigned (__stdcall *)(void *))start, 1.155 + arg, 1.156 + STACK_SIZE_PARAM_IS_A_RESERVATION, 1.157 + &tid); 1.158 + return((PRThread *) thandle); 1.159 +#endif 1.160 + } else { 1.161 + return(PR_CreateThread(type,start,arg,priority,scope,state,stackSize)); 1.162 + } 1.163 +#else 1.164 + return(PR_CreateThread(type,start,arg,priority,scope,state,stackSize)); 1.165 +#endif 1.166 +} 1.167 + 1.168 +static void PR_CALLBACK File_Write(void *arg) 1.169 +{ 1.170 +PRFileDesc *fd_file; 1.171 +File_Rdwr_Param *fp = (File_Rdwr_Param *) arg; 1.172 +char *name, *buf; 1.173 +int offset, len; 1.174 + 1.175 + setbuf(stdout, NULL); 1.176 + name = fp->pathname; 1.177 + buf = fp->buf; 1.178 + offset = fp->offset; 1.179 + len = fp->len; 1.180 + 1.181 + fd_file = PR_Open(name, PR_RDWR | PR_CREATE_FILE, 0777); 1.182 + if (fd_file == NULL) { 1.183 + printf("testfile failed to create/open file %s\n",name); 1.184 + return; 1.185 + } 1.186 + if (PR_Seek(fd_file, offset, PR_SEEK_SET) < 0) { 1.187 + printf("testfile failed to seek in file %s\n",name); 1.188 + return; 1.189 + } 1.190 + if ((PR_Write(fd_file, buf, len)) < 0) { 1.191 + printf("testfile failed to write to file %s\n",name); 1.192 + return; 1.193 + } 1.194 + DPRINTF(("Write out_buf[0] = 0x%x\n",(*((int *) buf)))); 1.195 + PR_Close(fd_file); 1.196 + PR_DELETE(fp); 1.197 + 1.198 + PR_EnterMonitor(mon); 1.199 + --thread_count; 1.200 + PR_Notify(mon); 1.201 + PR_ExitMonitor(mon); 1.202 +} 1.203 + 1.204 +static void PR_CALLBACK File_Read(void *arg) 1.205 +{ 1.206 +PRFileDesc *fd_file; 1.207 +File_Rdwr_Param *fp = (File_Rdwr_Param *) arg; 1.208 +char *name, *buf; 1.209 +int offset, len; 1.210 + 1.211 + setbuf(stdout, NULL); 1.212 + name = fp->pathname; 1.213 + buf = fp->buf; 1.214 + offset = fp->offset; 1.215 + len = fp->len; 1.216 + 1.217 + fd_file = PR_Open(name, PR_RDONLY, 0); 1.218 + if (fd_file == NULL) { 1.219 + printf("testfile failed to open file %s\n",name); 1.220 + return; 1.221 + } 1.222 + if (PR_Seek(fd_file, offset, PR_SEEK_SET) < 0) { 1.223 + printf("testfile failed to seek in file %s\n",name); 1.224 + return; 1.225 + } 1.226 + if ((PR_Read(fd_file, buf, len)) < 0) { 1.227 + printf("testfile failed to read to file %s\n",name); 1.228 + return; 1.229 + } 1.230 + DPRINTF(("Read in_buf[0] = 0x%x\n",(*((int *) buf)))); 1.231 + PR_Close(fd_file); 1.232 + PR_DELETE(fp); 1.233 + 1.234 + PR_EnterMonitor(mon); 1.235 + --thread_count; 1.236 + PR_Notify(mon); 1.237 + PR_ExitMonitor(mon); 1.238 +} 1.239 + 1.240 + 1.241 +static PRInt32 Misc_File_Tests(char *pathname) 1.242 +{ 1.243 +PRFileDesc *fd_file; 1.244 +int len, rv = 0; 1.245 +PRFileInfo file_info, file_info1; 1.246 +char tmpname[1024]; 1.247 + 1.248 + setbuf(stdout, NULL); 1.249 + /* 1.250 + * Test PR_Available, PR_Seek, PR_GetFileInfo, PR_Rename, PR_Access 1.251 + */ 1.252 + 1.253 + fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777); 1.254 + 1.255 + if (fd_file == NULL) { 1.256 + printf("testfile failed to create/open file %s\n",pathname); 1.257 + return -1; 1.258 + } 1.259 + if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) { 1.260 + printf("testfile PR_GetFileInfo failed on file %s\n",pathname); 1.261 + rv = -1; 1.262 + goto cleanup; 1.263 + } 1.264 + if (PR_Access(pathname, PR_ACCESS_EXISTS) != 0) { 1.265 + printf("testfile PR_Access failed on file %s\n",pathname); 1.266 + rv = -1; 1.267 + goto cleanup; 1.268 + } 1.269 + if (PR_Access(pathname, PR_ACCESS_WRITE_OK) != 0) { 1.270 + printf("testfile PR_Access failed on file %s\n",pathname); 1.271 + rv = -1; 1.272 + goto cleanup; 1.273 + } 1.274 + if (PR_Access(pathname, PR_ACCESS_READ_OK) != 0) { 1.275 + printf("testfile PR_Access failed on file %s\n",pathname); 1.276 + rv = -1; 1.277 + goto cleanup; 1.278 + } 1.279 + 1.280 + 1.281 + if (PR_GetFileInfo(pathname, &file_info) < 0) { 1.282 + printf("testfile PR_GetFileInfo failed on file %s\n",pathname); 1.283 + rv = -1; 1.284 + goto cleanup; 1.285 + } 1.286 + if (file_info.type != PR_FILE_FILE) { 1.287 + printf( 1.288 + "testfile: Error - PR_GetFileInfo returned incorrect type for file %s\n", 1.289 + pathname); 1.290 + rv = -1; 1.291 + goto cleanup; 1.292 + } 1.293 + if (file_info.size != 0) { 1.294 + printf( 1.295 + "testfile PR_GetFileInfo returned incorrect size (%d should be 0) for file %s\n", 1.296 + file_info.size, pathname); 1.297 + rv = -1; 1.298 + goto cleanup; 1.299 + } 1.300 + file_info1 = file_info; 1.301 + 1.302 + len = PR_Available(fd_file); 1.303 + if (len < 0) { 1.304 + printf("testfile PR_Available failed on file %s\n",pathname); 1.305 + rv = -1; 1.306 + goto cleanup; 1.307 + } else if (len != 0) { 1.308 + printf( 1.309 + "testfile PR_Available failed: expected/returned = %d/%d bytes\n", 1.310 + 0, len); 1.311 + rv = -1; 1.312 + goto cleanup; 1.313 + } 1.314 + if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) { 1.315 + printf("testfile PR_GetFileInfo failed on file %s\n",pathname); 1.316 + goto cleanup; 1.317 + } 1.318 + if (LL_NE(file_info.creationTime , file_info1.creationTime)) { 1.319 + printf( 1.320 + "testfile PR_GetFileInfo returned incorrect status-change time: %s\n", 1.321 + pathname); 1.322 + printf("ft = %lld, ft1 = %lld\n",file_info.creationTime, 1.323 + file_info1.creationTime); 1.324 + rv = -1; 1.325 + goto cleanup; 1.326 + } 1.327 + len = PR_Write(fd_file, out_buf->data, CHUNK_SIZE); 1.328 + if (len < 0) { 1.329 + printf("testfile failed to write to file %s\n",pathname); 1.330 + rv = -1; 1.331 + goto cleanup; 1.332 + } 1.333 + if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) { 1.334 + printf("testfile PR_GetFileInfo failed on file %s\n",pathname); 1.335 + goto cleanup; 1.336 + } 1.337 + if (file_info.size != CHUNK_SIZE) { 1.338 + printf( 1.339 + "testfile PR_GetFileInfo returned incorrect size (%d should be %d) for file %s\n", 1.340 + file_info.size, CHUNK_SIZE, pathname); 1.341 + rv = -1; 1.342 + goto cleanup; 1.343 + } 1.344 + if (LL_CMP(file_info.modifyTime, < , file_info1.modifyTime)) { 1.345 + printf( 1.346 + "testfile PR_GetFileInfo returned incorrect modify time: %s\n", 1.347 + pathname); 1.348 + printf("ft = %lld, ft1 = %lld\n",file_info.modifyTime, 1.349 + file_info1.modifyTime); 1.350 + rv = -1; 1.351 + goto cleanup; 1.352 + } 1.353 + 1.354 + len = PR_Available(fd_file); 1.355 + if (len < 0) { 1.356 + printf("testfile PR_Available failed on file %s\n",pathname); 1.357 + rv = -1; 1.358 + goto cleanup; 1.359 + } else if (len != 0) { 1.360 + printf( 1.361 + "testfile PR_Available failed: expected/returned = %d/%d bytes\n", 1.362 + 0, len); 1.363 + rv = -1; 1.364 + goto cleanup; 1.365 + } 1.366 + 1.367 + PR_Seek(fd_file, 0, PR_SEEK_SET); 1.368 + len = PR_Available(fd_file); 1.369 + if (len < 0) { 1.370 + printf("testfile PR_Available failed on file %s\n",pathname); 1.371 + rv = -1; 1.372 + goto cleanup; 1.373 + } else if (len != CHUNK_SIZE) { 1.374 + printf( 1.375 + "testfile PR_Available failed: expected/returned = %d/%d bytes\n", 1.376 + CHUNK_SIZE, len); 1.377 + rv = -1; 1.378 + goto cleanup; 1.379 + } 1.380 + PR_Close(fd_file); 1.381 + 1.382 + strcpy(tmpname,pathname); 1.383 + strcat(tmpname,".RENAMED"); 1.384 + if (PR_FAILURE == PR_Rename(pathname, tmpname)) { 1.385 + printf("testfile failed to rename file %s\n",pathname); 1.386 + rv = -1; 1.387 + goto cleanup; 1.388 + } 1.389 + 1.390 + fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777); 1.391 + len = PR_Write(fd_file, out_buf->data, CHUNK_SIZE); 1.392 + PR_Close(fd_file); 1.393 + if (PR_SUCCESS == PR_Rename(pathname, tmpname)) { 1.394 + printf("testfile renamed to existing file %s\n",pathname); 1.395 + } 1.396 + 1.397 + if ((PR_Delete(tmpname)) < 0) { 1.398 + printf("testfile failed to unlink file %s\n",tmpname); 1.399 + rv = -1; 1.400 + } 1.401 + 1.402 +cleanup: 1.403 + if ((PR_Delete(pathname)) < 0) { 1.404 + printf("testfile failed to unlink file %s\n",pathname); 1.405 + rv = -1; 1.406 + } 1.407 + return rv; 1.408 +} 1.409 + 1.410 + 1.411 +static PRInt32 PR_CALLBACK FileTest(void) 1.412 +{ 1.413 +PRDir *fd_dir; 1.414 +int i, offset, len, rv = 0; 1.415 +PRThread *t; 1.416 +PRThreadScope scope = PR_GLOBAL_THREAD; 1.417 +File_Rdwr_Param *fparamp; 1.418 + 1.419 + /* 1.420 + * Create Test dir 1.421 + */ 1.422 + if ((PR_MkDir(TEST_DIR, 0777)) < 0) { 1.423 + printf("testfile failed to create dir %s\n",TEST_DIR); 1.424 + return -1; 1.425 + } 1.426 + fd_dir = PR_OpenDir(TEST_DIR); 1.427 + if (fd_dir == NULL) { 1.428 + printf("testfile failed to open dir %s\n",TEST_DIR); 1.429 + rv = -1; 1.430 + goto cleanup; 1.431 + } 1.432 + 1.433 + PR_CloseDir(fd_dir); 1.434 + 1.435 + strcat(pathname, TEST_DIR); 1.436 + strcat(pathname, "/"); 1.437 + strcat(pathname, FILE_NAME); 1.438 + 1.439 + in_buf = PR_NEW(buffer); 1.440 + if (in_buf == NULL) { 1.441 + printf( 1.442 + "testfile failed to alloc buffer struct\n"); 1.443 + rv = -1; 1.444 + goto cleanup; 1.445 + } 1.446 + out_buf = PR_NEW(buffer); 1.447 + if (out_buf == NULL) { 1.448 + printf( 1.449 + "testfile failed to alloc buffer struct\n"); 1.450 + rv = -1; 1.451 + goto cleanup; 1.452 + } 1.453 + 1.454 + /* 1.455 + * Start a bunch of writer threads 1.456 + */ 1.457 + offset = 0; 1.458 + len = CHUNK_SIZE; 1.459 + PR_EnterMonitor(mon); 1.460 + for (i = 0; i < NUM_RDWR_THREADS; i++) { 1.461 + fparamp = PR_NEW(File_Rdwr_Param); 1.462 + if (fparamp == NULL) { 1.463 + printf( 1.464 + "testfile failed to alloc File_Rdwr_Param struct\n"); 1.465 + rv = -1; 1.466 + goto cleanup; 1.467 + } 1.468 + fparamp->pathname = pathname; 1.469 + fparamp->buf = out_buf->data + offset; 1.470 + fparamp->offset = offset; 1.471 + fparamp->len = len; 1.472 + memset(fparamp->buf, i, len); 1.473 + 1.474 + t = create_new_thread(PR_USER_THREAD, 1.475 + File_Write, (void *)fparamp, 1.476 + PR_PRIORITY_NORMAL, 1.477 + scope, 1.478 + PR_UNJOINABLE_THREAD, 1.479 + 0, i); 1.480 + offset += len; 1.481 + } 1.482 + thread_count = i; 1.483 + /* Wait for writer threads to exit */ 1.484 + while (thread_count) { 1.485 + PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT); 1.486 + } 1.487 + PR_ExitMonitor(mon); 1.488 + 1.489 + 1.490 + /* 1.491 + * Start a bunch of reader threads 1.492 + */ 1.493 + offset = 0; 1.494 + len = CHUNK_SIZE; 1.495 + PR_EnterMonitor(mon); 1.496 + for (i = 0; i < NUM_RDWR_THREADS; i++) { 1.497 + fparamp = PR_NEW(File_Rdwr_Param); 1.498 + if (fparamp == NULL) { 1.499 + printf( 1.500 + "testfile failed to alloc File_Rdwr_Param struct\n"); 1.501 + rv = -1; 1.502 + goto cleanup; 1.503 + } 1.504 + fparamp->pathname = pathname; 1.505 + fparamp->buf = in_buf->data + offset; 1.506 + fparamp->offset = offset; 1.507 + fparamp->len = len; 1.508 + 1.509 + t = create_new_thread(PR_USER_THREAD, 1.510 + File_Read, (void *)fparamp, 1.511 + PR_PRIORITY_NORMAL, 1.512 + scope, 1.513 + PR_UNJOINABLE_THREAD, 1.514 + 0, i); 1.515 + offset += len; 1.516 + if ((offset + len) > BUF_DATA_SIZE) 1.517 + break; 1.518 + } 1.519 + thread_count = i; 1.520 + 1.521 + /* Wait for reader threads to exit */ 1.522 + while (thread_count) { 1.523 + PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT); 1.524 + } 1.525 + PR_ExitMonitor(mon); 1.526 + 1.527 + if (memcmp(in_buf->data, out_buf->data, offset) != 0) { 1.528 + printf("File Test failed: file data corrupted\n"); 1.529 + rv = -1; 1.530 + goto cleanup; 1.531 + } 1.532 + 1.533 + if ((PR_Delete(pathname)) < 0) { 1.534 + printf("testfile failed to unlink file %s\n",pathname); 1.535 + rv = -1; 1.536 + goto cleanup; 1.537 + } 1.538 + 1.539 + /* 1.540 + * Test PR_Available, PR_Seek, PR_GetFileInfo, PR_Rename, PR_Access 1.541 + */ 1.542 + if (Misc_File_Tests(pathname) < 0) { 1.543 + rv = -1; 1.544 + } 1.545 + 1.546 +cleanup: 1.547 + if ((PR_RmDir(TEST_DIR)) < 0) { 1.548 + printf("testfile failed to rmdir %s\n", TEST_DIR); 1.549 + rv = -1; 1.550 + } 1.551 + return rv; 1.552 +} 1.553 + 1.554 +struct dirtest_arg { 1.555 + PRMonitor *mon; 1.556 + PRInt32 done; 1.557 +}; 1.558 + 1.559 +static PRInt32 RunDirTest(void) 1.560 +{ 1.561 +int i; 1.562 +PRThread *t; 1.563 +PRMonitor *mon; 1.564 +struct dirtest_arg thrarg; 1.565 + 1.566 + mon = PR_NewMonitor(); 1.567 + if (!mon) { 1.568 + printf("RunDirTest: Error - failed to create monitor\n"); 1.569 + dirtest_failed = 1; 1.570 + return -1; 1.571 + } 1.572 + thrarg.mon = mon; 1.573 + 1.574 + for (i = 0; i < NUM_DIRTEST_THREADS; i++) { 1.575 + 1.576 + thrarg.done= 0; 1.577 + t = create_new_thread(PR_USER_THREAD, 1.578 + DirTest, &thrarg, 1.579 + PR_PRIORITY_NORMAL, 1.580 + PR_LOCAL_THREAD, 1.581 + PR_UNJOINABLE_THREAD, 1.582 + 0, i); 1.583 + if (!t) { 1.584 + printf("RunDirTest: Error - failed to create thread\n"); 1.585 + dirtest_failed = 1; 1.586 + return -1; 1.587 + } 1.588 + PR_EnterMonitor(mon); 1.589 + while (!thrarg.done) 1.590 + PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT); 1.591 + PR_ExitMonitor(mon); 1.592 + 1.593 + } 1.594 + PR_DestroyMonitor(mon); 1.595 + return 0; 1.596 +} 1.597 + 1.598 +static PRInt32 PR_CALLBACK DirTest(void *arg) 1.599 +{ 1.600 +struct dirtest_arg *tinfo = (struct dirtest_arg *) arg; 1.601 +PRFileDesc *fd_file; 1.602 +PRDir *fd_dir; 1.603 +int i; 1.604 +int path_len; 1.605 +PRDirEntry *dirEntry; 1.606 +PRFileInfo info; 1.607 +PRInt32 num_files = 0; 1.608 +#if defined(XP_PC) && defined(WIN32) 1.609 +HANDLE hfile; 1.610 +#endif 1.611 + 1.612 +#define FILES_IN_DIR 20 1.613 + 1.614 + /* 1.615 + * Create Test dir 1.616 + */ 1.617 + DPRINTF(("Creating test dir %s\n",TEST_DIR)); 1.618 + if ((PR_MkDir(TEST_DIR, 0777)) < 0) { 1.619 + printf( 1.620 + "testfile failed to create dir %s [%d, %d]\n", 1.621 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.622 + return -1; 1.623 + } 1.624 + fd_dir = PR_OpenDir(TEST_DIR); 1.625 + if (fd_dir == NULL) { 1.626 + printf( 1.627 + "testfile failed to open dirctory %s [%d, %d]\n", 1.628 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.629 + return -1; 1.630 + } 1.631 + 1.632 + strcpy(pathname, TEST_DIR); 1.633 + strcat(pathname, "/"); 1.634 + strcat(pathname, FILE_NAME); 1.635 + path_len = strlen(pathname); 1.636 + 1.637 + for (i = 0; i < FILES_IN_DIR; i++) { 1.638 + 1.639 + sprintf(pathname + path_len,"%d%s",i,""); 1.640 + 1.641 + DPRINTF(("Creating test file %s\n",pathname)); 1.642 + 1.643 + fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777); 1.644 + 1.645 + if (fd_file == NULL) { 1.646 + printf( 1.647 + "testfile failed to create/open file %s [%d, %d]\n", 1.648 + pathname, PR_GetError(), PR_GetOSError()); 1.649 + return -1; 1.650 + } 1.651 + PR_Close(fd_file); 1.652 + } 1.653 +#if defined(XP_UNIX) || (defined(XP_PC) && defined(WIN32)) || defined(XP_OS2) || defined(XP_BEOS) 1.654 + /* 1.655 + * Create a hidden file - a platform-dependent operation 1.656 + */ 1.657 + strcpy(pathname, TEST_DIR); 1.658 + strcat(pathname, "/"); 1.659 + strcat(pathname, HIDDEN_FILE_NAME); 1.660 +#if defined(XP_UNIX) || defined(XP_BEOS) 1.661 + DPRINTF(("Creating hidden test file %s\n",pathname)); 1.662 + fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777); 1.663 + 1.664 + if (fd_file == NULL) { 1.665 + printf( 1.666 + "testfile failed to create/open hidden file %s [%d, %d]\n", 1.667 + pathname, PR_GetError(), PR_GetOSError()); 1.668 + return -1; 1.669 + } 1.670 + 1.671 + PR_Close(fd_file); 1.672 + 1.673 +#elif defined(WINCE) 1.674 + DPRINTF(("Creating hidden test file %s\n",pathname)); 1.675 + MultiByteToWideChar(CP_ACP, 0, pathname, -1, wPathname, 256); 1.676 + hfile = CreateFile(wPathname, GENERIC_READ, 1.677 + FILE_SHARE_READ|FILE_SHARE_WRITE, 1.678 + NULL, 1.679 + CREATE_NEW, 1.680 + FILE_ATTRIBUTE_HIDDEN, 1.681 + NULL); 1.682 + if (hfile == INVALID_HANDLE_VALUE) { 1.683 + printf("testfile failed to create/open hidden file %s [0, %d]\n", 1.684 + pathname, GetLastError()); 1.685 + return -1; 1.686 + } 1.687 + CloseHandle(hfile); 1.688 + 1.689 +#elif defined(XP_PC) && defined(WIN32) 1.690 + DPRINTF(("Creating hidden test file %s\n",pathname)); 1.691 + hfile = CreateFile(pathname, GENERIC_READ, 1.692 + FILE_SHARE_READ|FILE_SHARE_WRITE, 1.693 + NULL, 1.694 + CREATE_NEW, 1.695 + FILE_ATTRIBUTE_HIDDEN, 1.696 + NULL); 1.697 + if (hfile == INVALID_HANDLE_VALUE) { 1.698 + printf("testfile failed to create/open hidden file %s [0, %d]\n", 1.699 + pathname, GetLastError()); 1.700 + return -1; 1.701 + } 1.702 + CloseHandle(hfile); 1.703 + 1.704 +#elif defined(OS2) 1.705 + DPRINTF(("Creating hidden test file %s\n",pathname)); 1.706 + fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, (int)FILE_HIDDEN); 1.707 + 1.708 + if (fd_file == NULL) { 1.709 + printf("testfile failed to create/open hidden file %s [%d, %d]\n", 1.710 + pathname, PR_GetError(), PR_GetOSError()); 1.711 + return -1; 1.712 + } 1.713 + PR_Close(fd_file); 1.714 +#endif /* XP_UNIX */ 1.715 + 1.716 +#endif /* XP_UNIX || (XP_PC && WIN32) */ 1.717 + 1.718 + 1.719 + if (PR_FAILURE == PR_CloseDir(fd_dir)) 1.720 + { 1.721 + printf( 1.722 + "testfile failed to close dirctory %s [%d, %d]\n", 1.723 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.724 + return -1; 1.725 + } 1.726 + fd_dir = PR_OpenDir(TEST_DIR); 1.727 + if (fd_dir == NULL) { 1.728 + printf( 1.729 + "testfile failed to reopen dirctory %s [%d, %d]\n", 1.730 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.731 + return -1; 1.732 + } 1.733 + 1.734 + /* 1.735 + * List all files, including hidden files 1.736 + */ 1.737 + DPRINTF(("Listing all files in directory %s\n",TEST_DIR)); 1.738 +#if defined(XP_UNIX) || (defined(XP_PC) && defined(WIN32)) || defined(XP_OS2) || defined(XP_BEOS) 1.739 + num_files = FILES_IN_DIR + 1; 1.740 +#else 1.741 + num_files = FILES_IN_DIR; 1.742 +#endif 1.743 + while ((dirEntry = PR_ReadDir(fd_dir, PR_SKIP_BOTH)) != NULL) { 1.744 + num_files--; 1.745 + strcpy(pathname, TEST_DIR); 1.746 + strcat(pathname, "/"); 1.747 + strcat(pathname, dirEntry->name); 1.748 + DPRINTF(("\t%s\n",dirEntry->name)); 1.749 + 1.750 + if ((PR_GetFileInfo(pathname, &info)) < 0) { 1.751 + printf( 1.752 + "testfile failed to GetFileInfo file %s [%d, %d]\n", 1.753 + pathname, PR_GetError(), PR_GetOSError()); 1.754 + return -1; 1.755 + } 1.756 + 1.757 + if (info.type != PR_FILE_FILE) { 1.758 + printf( 1.759 + "testfile incorrect fileinfo for file %s [%d, %d]\n", 1.760 + pathname, PR_GetError(), PR_GetOSError()); 1.761 + return -1; 1.762 + } 1.763 + } 1.764 + if (num_files != 0) 1.765 + { 1.766 + printf( 1.767 + "testfile failed to find all files in directory %s [%d, %d]\n", 1.768 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.769 + return -1; 1.770 + } 1.771 + 1.772 + PR_CloseDir(fd_dir); 1.773 + 1.774 +#if defined(XP_UNIX) || (defined(XP_PC) && defined(WIN32)) || defined(XP_OS2) || defined(XP_BEOS) 1.775 + 1.776 + /* 1.777 + * List all files, except hidden files 1.778 + */ 1.779 + 1.780 + fd_dir = PR_OpenDir(TEST_DIR); 1.781 + if (fd_dir == NULL) { 1.782 + printf( 1.783 + "testfile failed to reopen dirctory %s [%d, %d]\n", 1.784 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.785 + return -1; 1.786 + } 1.787 + 1.788 + DPRINTF(("Listing non-hidden files in directory %s\n",TEST_DIR)); 1.789 + while ((dirEntry = PR_ReadDir(fd_dir, PR_SKIP_HIDDEN)) != NULL) { 1.790 + DPRINTF(("\t%s\n",dirEntry->name)); 1.791 + if (!strcmp(HIDDEN_FILE_NAME, dirEntry->name)) { 1.792 + printf("testfile found hidden file %s\n", pathname); 1.793 + return -1; 1.794 + } 1.795 + 1.796 + } 1.797 + /* 1.798 + * Delete hidden file 1.799 + */ 1.800 + strcpy(pathname, TEST_DIR); 1.801 + strcat(pathname, "/"); 1.802 + strcat(pathname, HIDDEN_FILE_NAME); 1.803 + if (PR_FAILURE == PR_Delete(pathname)) { 1.804 + printf( 1.805 + "testfile failed to delete hidden file %s [%d, %d]\n", 1.806 + pathname, PR_GetError(), PR_GetOSError()); 1.807 + return -1; 1.808 + } 1.809 + 1.810 + PR_CloseDir(fd_dir); 1.811 +#endif /* XP_UNIX || (XP_PC && WIN32) */ 1.812 + 1.813 + strcpy(renamename, TEST_DIR); 1.814 + strcat(renamename, ".RENAMED"); 1.815 + if (PR_FAILURE == PR_Rename(TEST_DIR, renamename)) { 1.816 + printf( 1.817 + "testfile failed to rename directory %s [%d, %d]\n", 1.818 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.819 + return -1; 1.820 + } 1.821 + 1.822 + if (PR_FAILURE == PR_MkDir(TEST_DIR, 0777)) { 1.823 + printf( 1.824 + "testfile failed to recreate dir %s [%d, %d]\n", 1.825 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.826 + return -1; 1.827 + } 1.828 + if (PR_SUCCESS == PR_Rename(renamename, TEST_DIR)) { 1.829 + printf( 1.830 + "testfile renamed directory to existing name %s\n", 1.831 + renamename); 1.832 + return -1; 1.833 + } 1.834 + 1.835 + if (PR_FAILURE == PR_RmDir(TEST_DIR)) { 1.836 + printf( 1.837 + "testfile failed to rmdir %s [%d, %d]\n", 1.838 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.839 + return -1; 1.840 + } 1.841 + 1.842 + if (PR_FAILURE == PR_Rename(renamename, TEST_DIR)) { 1.843 + printf( 1.844 + "testfile failed to rename directory %s [%d, %d]\n", 1.845 + renamename, PR_GetError(), PR_GetOSError()); 1.846 + return -1; 1.847 + } 1.848 + fd_dir = PR_OpenDir(TEST_DIR); 1.849 + if (fd_dir == NULL) { 1.850 + printf( 1.851 + "testfile failed to reopen directory %s [%d, %d]\n", 1.852 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.853 + return -1; 1.854 + } 1.855 + 1.856 + strcpy(pathname, TEST_DIR); 1.857 + strcat(pathname, "/"); 1.858 + strcat(pathname, FILE_NAME); 1.859 + path_len = strlen(pathname); 1.860 + 1.861 + for (i = 0; i < FILES_IN_DIR; i++) { 1.862 + 1.863 + sprintf(pathname + path_len,"%d%s",i,""); 1.864 + 1.865 + if (PR_FAILURE == PR_Delete(pathname)) { 1.866 + printf( 1.867 + "testfile failed to delete file %s [%d, %d]\n", 1.868 + pathname, PR_GetError(), PR_GetOSError()); 1.869 + return -1; 1.870 + } 1.871 + } 1.872 + 1.873 + PR_CloseDir(fd_dir); 1.874 + 1.875 + if (PR_FAILURE == PR_RmDir(TEST_DIR)) { 1.876 + printf( 1.877 + "testfile failed to rmdir %s [%d, %d]\n", 1.878 + TEST_DIR, PR_GetError(), PR_GetOSError()); 1.879 + return -1; 1.880 + } 1.881 + PR_EnterMonitor(tinfo->mon); 1.882 + tinfo->done = 1; 1.883 + PR_Notify(tinfo->mon); 1.884 + PR_ExitMonitor(tinfo->mon); 1.885 + 1.886 + return 0; 1.887 +} 1.888 +/************************************************************************/ 1.889 + 1.890 +/* 1.891 + * Test file and directory NSPR APIs 1.892 + */ 1.893 + 1.894 +int main(int argc, char **argv) 1.895 +{ 1.896 +#ifdef WIN32 1.897 + PRUint32 len; 1.898 +#endif 1.899 +#if defined(XP_UNIX) || defined(XP_OS2) 1.900 + int opt; 1.901 + extern char *optarg; 1.902 + extern int optind; 1.903 +#endif 1.904 +#if defined(XP_UNIX) || defined(XP_OS2) 1.905 + while ((opt = getopt(argc, argv, "d")) != EOF) { 1.906 + switch(opt) { 1.907 + case 'd': 1.908 + _debug_on = 1; 1.909 + break; 1.910 + default: 1.911 + break; 1.912 + } 1.913 + } 1.914 +#endif 1.915 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.916 + PR_STDIO_INIT(); 1.917 + 1.918 + mon = PR_NewMonitor(); 1.919 + if (mon == NULL) { 1.920 + printf("testfile: PR_NewMonitor failed\n"); 1.921 + exit(2); 1.922 + } 1.923 +#ifdef WIN32 1.924 + 1.925 +#ifdef WINCE 1.926 + { 1.927 + WCHAR tdir[TMPDIR_LEN]; 1.928 + len = GetTempPath(TMPDIR_LEN, tdir); 1.929 + if ((len > 0) && (len < (TMPDIR_LEN - 6))) { 1.930 + /* 1.931 + * enough space for prdir 1.932 + */ 1.933 + WideCharToMultiByte(CP_ACP, 0, tdir, -1, testdir, TMPDIR_LEN, 0, 0); 1.934 + } 1.935 + } 1.936 +#else 1.937 + len = GetTempPath(TMPDIR_LEN, testdir); 1.938 +#endif /* WINCE */ 1.939 + 1.940 + if ((len > 0) && (len < (TMPDIR_LEN - 6))) { 1.941 + /* 1.942 + * enough space for prdir 1.943 + */ 1.944 + strcpy((testdir + len),"prdir"); 1.945 + TEST_DIR = testdir; 1.946 + printf("TEST_DIR = %s\n",TEST_DIR); 1.947 + } 1.948 +#endif /* WIN32 */ 1.949 + 1.950 + if (FileTest() < 0) { 1.951 + printf("File Test failed\n"); 1.952 + exit(2); 1.953 + } 1.954 + printf("File Test passed\n"); 1.955 + if ((RunDirTest() < 0) || dirtest_failed) { 1.956 + printf("Dir Test failed\n"); 1.957 + exit(2); 1.958 + } 1.959 + printf("Dir Test passed\n"); 1.960 + 1.961 + PR_DestroyMonitor(mon); 1.962 + PR_Cleanup(); 1.963 + return 0; 1.964 +}