nsprpub/pr/tests/testfile.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nspr.h"
michael@0 7 #include "prpriv.h"
michael@0 8
michael@0 9 #include <stdio.h>
michael@0 10 #include <stdlib.h>
michael@0 11 #include <string.h>
michael@0 12 #ifdef WIN32
michael@0 13 #include <windows.h>
michael@0 14 #include <process.h>
michael@0 15 #endif
michael@0 16 #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
michael@0 17 #include <pthread.h>
michael@0 18 #endif
michael@0 19 #ifdef SYMBIAN
michael@0 20 #include <getopt.h>
michael@0 21 #endif
michael@0 22
michael@0 23 #if defined(XP_OS2)
michael@0 24 #define INCL_DOSFILEMGR
michael@0 25 #include <os2.h>
michael@0 26 #include <getopt.h>
michael@0 27 #include <errno.h>
michael@0 28 #endif /* XP_OS2 */
michael@0 29
michael@0 30 static int _debug_on = 0;
michael@0 31
michael@0 32 #ifdef WINCE
michael@0 33 #define setbuf(x,y)
michael@0 34 #endif
michael@0 35
michael@0 36 #ifdef XP_WIN
michael@0 37 #define mode_t int
michael@0 38 #endif
michael@0 39
michael@0 40 #define DPRINTF(arg) if (_debug_on) printf arg
michael@0 41
michael@0 42 PRLock *lock;
michael@0 43 PRMonitor *mon;
michael@0 44 PRInt32 count;
michael@0 45 int thread_count;
michael@0 46
michael@0 47 #ifdef WIN16
michael@0 48 #define BUF_DATA_SIZE 256 * 120
michael@0 49 #else
michael@0 50 #define BUF_DATA_SIZE 256 * 1024
michael@0 51 #endif
michael@0 52
michael@0 53 #define NUM_RDWR_THREADS 10
michael@0 54 #define NUM_DIRTEST_THREADS 4
michael@0 55 #define CHUNK_SIZE 512
michael@0 56
michael@0 57 typedef struct buffer {
michael@0 58 char data[BUF_DATA_SIZE];
michael@0 59 } buffer;
michael@0 60
michael@0 61 typedef struct File_Rdwr_Param {
michael@0 62 char *pathname;
michael@0 63 char *buf;
michael@0 64 int offset;
michael@0 65 int len;
michael@0 66 } File_Rdwr_Param;
michael@0 67
michael@0 68 #ifdef XP_PC
michael@0 69 #ifdef XP_OS2
michael@0 70 char *TEST_DIR = "prdir";
michael@0 71 #else
michael@0 72 char *TEST_DIR = "C:\\temp\\prdir";
michael@0 73 #endif
michael@0 74 char *FILE_NAME = "pr_testfile";
michael@0 75 char *HIDDEN_FILE_NAME = "hidden_pr_testfile";
michael@0 76 #else
michael@0 77 #ifdef SYMBIAN
michael@0 78 char *TEST_DIR = "c:\\data\\testfile_dir";
michael@0 79 #else
michael@0 80 char *TEST_DIR = "/tmp/testfile_dir";
michael@0 81 #endif
michael@0 82 char *FILE_NAME = "pr_testfile";
michael@0 83 char *HIDDEN_FILE_NAME = ".hidden_pr_testfile";
michael@0 84 #endif
michael@0 85 buffer *in_buf, *out_buf;
michael@0 86 char pathname[256], renamename[256];
michael@0 87 #ifdef WINCE
michael@0 88 WCHAR wPathname[256];
michael@0 89 #endif
michael@0 90 #define TMPDIR_LEN 64
michael@0 91 char testdir[TMPDIR_LEN];
michael@0 92 static PRInt32 PR_CALLBACK DirTest(void *argunused);
michael@0 93 PRInt32 dirtest_failed = 0;
michael@0 94
michael@0 95 PRThread* create_new_thread(PRThreadType type,
michael@0 96 void (*start)(void *arg),
michael@0 97 void *arg,
michael@0 98 PRThreadPriority priority,
michael@0 99 PRThreadScope scope,
michael@0 100 PRThreadState state,
michael@0 101 PRUint32 stackSize, PRInt32 index)
michael@0 102 {
michael@0 103 PRInt32 native_thread = 0;
michael@0 104
michael@0 105 PR_ASSERT(state == PR_UNJOINABLE_THREAD);
michael@0 106
michael@0 107 #if (defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)) || defined(WIN32) || defined(XP_OS2)
michael@0 108
michael@0 109 switch(index % 4) {
michael@0 110 case 0:
michael@0 111 scope = (PR_LOCAL_THREAD);
michael@0 112 break;
michael@0 113 case 1:
michael@0 114 scope = (PR_GLOBAL_THREAD);
michael@0 115 break;
michael@0 116 case 2:
michael@0 117 scope = (PR_GLOBAL_BOUND_THREAD);
michael@0 118 break;
michael@0 119 case 3:
michael@0 120 native_thread = 1;
michael@0 121 break;
michael@0 122 default:
michael@0 123 PR_ASSERT(!"Invalid scope");
michael@0 124 break;
michael@0 125 }
michael@0 126 if (native_thread) {
michael@0 127 #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
michael@0 128 pthread_t tid;
michael@0 129 if (!pthread_create(&tid, NULL, start, arg))
michael@0 130 return((PRThread *) tid);
michael@0 131 else
michael@0 132 return (NULL);
michael@0 133 #elif defined(XP_OS2)
michael@0 134 TID tid;
michael@0 135
michael@0 136 tid = (TID)_beginthread((void(* _Optlink)(void*))start,
michael@0 137 NULL, 32768, arg);
michael@0 138 if (tid == -1) {
michael@0 139 printf("_beginthread failed. errno %d\n", errno);
michael@0 140 return (NULL);
michael@0 141 }
michael@0 142 else
michael@0 143 return((PRThread *) tid);
michael@0 144 #else
michael@0 145 HANDLE thandle;
michael@0 146 unsigned tid;
michael@0 147
michael@0 148 thandle = (HANDLE) _beginthreadex(
michael@0 149 NULL,
michael@0 150 stackSize,
michael@0 151 (unsigned (__stdcall *)(void *))start,
michael@0 152 arg,
michael@0 153 STACK_SIZE_PARAM_IS_A_RESERVATION,
michael@0 154 &tid);
michael@0 155 return((PRThread *) thandle);
michael@0 156 #endif
michael@0 157 } else {
michael@0 158 return(PR_CreateThread(type,start,arg,priority,scope,state,stackSize));
michael@0 159 }
michael@0 160 #else
michael@0 161 return(PR_CreateThread(type,start,arg,priority,scope,state,stackSize));
michael@0 162 #endif
michael@0 163 }
michael@0 164
michael@0 165 static void PR_CALLBACK File_Write(void *arg)
michael@0 166 {
michael@0 167 PRFileDesc *fd_file;
michael@0 168 File_Rdwr_Param *fp = (File_Rdwr_Param *) arg;
michael@0 169 char *name, *buf;
michael@0 170 int offset, len;
michael@0 171
michael@0 172 setbuf(stdout, NULL);
michael@0 173 name = fp->pathname;
michael@0 174 buf = fp->buf;
michael@0 175 offset = fp->offset;
michael@0 176 len = fp->len;
michael@0 177
michael@0 178 fd_file = PR_Open(name, PR_RDWR | PR_CREATE_FILE, 0777);
michael@0 179 if (fd_file == NULL) {
michael@0 180 printf("testfile failed to create/open file %s\n",name);
michael@0 181 return;
michael@0 182 }
michael@0 183 if (PR_Seek(fd_file, offset, PR_SEEK_SET) < 0) {
michael@0 184 printf("testfile failed to seek in file %s\n",name);
michael@0 185 return;
michael@0 186 }
michael@0 187 if ((PR_Write(fd_file, buf, len)) < 0) {
michael@0 188 printf("testfile failed to write to file %s\n",name);
michael@0 189 return;
michael@0 190 }
michael@0 191 DPRINTF(("Write out_buf[0] = 0x%x\n",(*((int *) buf))));
michael@0 192 PR_Close(fd_file);
michael@0 193 PR_DELETE(fp);
michael@0 194
michael@0 195 PR_EnterMonitor(mon);
michael@0 196 --thread_count;
michael@0 197 PR_Notify(mon);
michael@0 198 PR_ExitMonitor(mon);
michael@0 199 }
michael@0 200
michael@0 201 static void PR_CALLBACK File_Read(void *arg)
michael@0 202 {
michael@0 203 PRFileDesc *fd_file;
michael@0 204 File_Rdwr_Param *fp = (File_Rdwr_Param *) arg;
michael@0 205 char *name, *buf;
michael@0 206 int offset, len;
michael@0 207
michael@0 208 setbuf(stdout, NULL);
michael@0 209 name = fp->pathname;
michael@0 210 buf = fp->buf;
michael@0 211 offset = fp->offset;
michael@0 212 len = fp->len;
michael@0 213
michael@0 214 fd_file = PR_Open(name, PR_RDONLY, 0);
michael@0 215 if (fd_file == NULL) {
michael@0 216 printf("testfile failed to open file %s\n",name);
michael@0 217 return;
michael@0 218 }
michael@0 219 if (PR_Seek(fd_file, offset, PR_SEEK_SET) < 0) {
michael@0 220 printf("testfile failed to seek in file %s\n",name);
michael@0 221 return;
michael@0 222 }
michael@0 223 if ((PR_Read(fd_file, buf, len)) < 0) {
michael@0 224 printf("testfile failed to read to file %s\n",name);
michael@0 225 return;
michael@0 226 }
michael@0 227 DPRINTF(("Read in_buf[0] = 0x%x\n",(*((int *) buf))));
michael@0 228 PR_Close(fd_file);
michael@0 229 PR_DELETE(fp);
michael@0 230
michael@0 231 PR_EnterMonitor(mon);
michael@0 232 --thread_count;
michael@0 233 PR_Notify(mon);
michael@0 234 PR_ExitMonitor(mon);
michael@0 235 }
michael@0 236
michael@0 237
michael@0 238 static PRInt32 Misc_File_Tests(char *pathname)
michael@0 239 {
michael@0 240 PRFileDesc *fd_file;
michael@0 241 int len, rv = 0;
michael@0 242 PRFileInfo file_info, file_info1;
michael@0 243 char tmpname[1024];
michael@0 244
michael@0 245 setbuf(stdout, NULL);
michael@0 246 /*
michael@0 247 * Test PR_Available, PR_Seek, PR_GetFileInfo, PR_Rename, PR_Access
michael@0 248 */
michael@0 249
michael@0 250 fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
michael@0 251
michael@0 252 if (fd_file == NULL) {
michael@0 253 printf("testfile failed to create/open file %s\n",pathname);
michael@0 254 return -1;
michael@0 255 }
michael@0 256 if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) {
michael@0 257 printf("testfile PR_GetFileInfo failed on file %s\n",pathname);
michael@0 258 rv = -1;
michael@0 259 goto cleanup;
michael@0 260 }
michael@0 261 if (PR_Access(pathname, PR_ACCESS_EXISTS) != 0) {
michael@0 262 printf("testfile PR_Access failed on file %s\n",pathname);
michael@0 263 rv = -1;
michael@0 264 goto cleanup;
michael@0 265 }
michael@0 266 if (PR_Access(pathname, PR_ACCESS_WRITE_OK) != 0) {
michael@0 267 printf("testfile PR_Access failed on file %s\n",pathname);
michael@0 268 rv = -1;
michael@0 269 goto cleanup;
michael@0 270 }
michael@0 271 if (PR_Access(pathname, PR_ACCESS_READ_OK) != 0) {
michael@0 272 printf("testfile PR_Access failed on file %s\n",pathname);
michael@0 273 rv = -1;
michael@0 274 goto cleanup;
michael@0 275 }
michael@0 276
michael@0 277
michael@0 278 if (PR_GetFileInfo(pathname, &file_info) < 0) {
michael@0 279 printf("testfile PR_GetFileInfo failed on file %s\n",pathname);
michael@0 280 rv = -1;
michael@0 281 goto cleanup;
michael@0 282 }
michael@0 283 if (file_info.type != PR_FILE_FILE) {
michael@0 284 printf(
michael@0 285 "testfile: Error - PR_GetFileInfo returned incorrect type for file %s\n",
michael@0 286 pathname);
michael@0 287 rv = -1;
michael@0 288 goto cleanup;
michael@0 289 }
michael@0 290 if (file_info.size != 0) {
michael@0 291 printf(
michael@0 292 "testfile PR_GetFileInfo returned incorrect size (%d should be 0) for file %s\n",
michael@0 293 file_info.size, pathname);
michael@0 294 rv = -1;
michael@0 295 goto cleanup;
michael@0 296 }
michael@0 297 file_info1 = file_info;
michael@0 298
michael@0 299 len = PR_Available(fd_file);
michael@0 300 if (len < 0) {
michael@0 301 printf("testfile PR_Available failed on file %s\n",pathname);
michael@0 302 rv = -1;
michael@0 303 goto cleanup;
michael@0 304 } else if (len != 0) {
michael@0 305 printf(
michael@0 306 "testfile PR_Available failed: expected/returned = %d/%d bytes\n",
michael@0 307 0, len);
michael@0 308 rv = -1;
michael@0 309 goto cleanup;
michael@0 310 }
michael@0 311 if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) {
michael@0 312 printf("testfile PR_GetFileInfo failed on file %s\n",pathname);
michael@0 313 goto cleanup;
michael@0 314 }
michael@0 315 if (LL_NE(file_info.creationTime , file_info1.creationTime)) {
michael@0 316 printf(
michael@0 317 "testfile PR_GetFileInfo returned incorrect status-change time: %s\n",
michael@0 318 pathname);
michael@0 319 printf("ft = %lld, ft1 = %lld\n",file_info.creationTime,
michael@0 320 file_info1.creationTime);
michael@0 321 rv = -1;
michael@0 322 goto cleanup;
michael@0 323 }
michael@0 324 len = PR_Write(fd_file, out_buf->data, CHUNK_SIZE);
michael@0 325 if (len < 0) {
michael@0 326 printf("testfile failed to write to file %s\n",pathname);
michael@0 327 rv = -1;
michael@0 328 goto cleanup;
michael@0 329 }
michael@0 330 if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) {
michael@0 331 printf("testfile PR_GetFileInfo failed on file %s\n",pathname);
michael@0 332 goto cleanup;
michael@0 333 }
michael@0 334 if (file_info.size != CHUNK_SIZE) {
michael@0 335 printf(
michael@0 336 "testfile PR_GetFileInfo returned incorrect size (%d should be %d) for file %s\n",
michael@0 337 file_info.size, CHUNK_SIZE, pathname);
michael@0 338 rv = -1;
michael@0 339 goto cleanup;
michael@0 340 }
michael@0 341 if (LL_CMP(file_info.modifyTime, < , file_info1.modifyTime)) {
michael@0 342 printf(
michael@0 343 "testfile PR_GetFileInfo returned incorrect modify time: %s\n",
michael@0 344 pathname);
michael@0 345 printf("ft = %lld, ft1 = %lld\n",file_info.modifyTime,
michael@0 346 file_info1.modifyTime);
michael@0 347 rv = -1;
michael@0 348 goto cleanup;
michael@0 349 }
michael@0 350
michael@0 351 len = PR_Available(fd_file);
michael@0 352 if (len < 0) {
michael@0 353 printf("testfile PR_Available failed on file %s\n",pathname);
michael@0 354 rv = -1;
michael@0 355 goto cleanup;
michael@0 356 } else if (len != 0) {
michael@0 357 printf(
michael@0 358 "testfile PR_Available failed: expected/returned = %d/%d bytes\n",
michael@0 359 0, len);
michael@0 360 rv = -1;
michael@0 361 goto cleanup;
michael@0 362 }
michael@0 363
michael@0 364 PR_Seek(fd_file, 0, PR_SEEK_SET);
michael@0 365 len = PR_Available(fd_file);
michael@0 366 if (len < 0) {
michael@0 367 printf("testfile PR_Available failed on file %s\n",pathname);
michael@0 368 rv = -1;
michael@0 369 goto cleanup;
michael@0 370 } else if (len != CHUNK_SIZE) {
michael@0 371 printf(
michael@0 372 "testfile PR_Available failed: expected/returned = %d/%d bytes\n",
michael@0 373 CHUNK_SIZE, len);
michael@0 374 rv = -1;
michael@0 375 goto cleanup;
michael@0 376 }
michael@0 377 PR_Close(fd_file);
michael@0 378
michael@0 379 strcpy(tmpname,pathname);
michael@0 380 strcat(tmpname,".RENAMED");
michael@0 381 if (PR_FAILURE == PR_Rename(pathname, tmpname)) {
michael@0 382 printf("testfile failed to rename file %s\n",pathname);
michael@0 383 rv = -1;
michael@0 384 goto cleanup;
michael@0 385 }
michael@0 386
michael@0 387 fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
michael@0 388 len = PR_Write(fd_file, out_buf->data, CHUNK_SIZE);
michael@0 389 PR_Close(fd_file);
michael@0 390 if (PR_SUCCESS == PR_Rename(pathname, tmpname)) {
michael@0 391 printf("testfile renamed to existing file %s\n",pathname);
michael@0 392 }
michael@0 393
michael@0 394 if ((PR_Delete(tmpname)) < 0) {
michael@0 395 printf("testfile failed to unlink file %s\n",tmpname);
michael@0 396 rv = -1;
michael@0 397 }
michael@0 398
michael@0 399 cleanup:
michael@0 400 if ((PR_Delete(pathname)) < 0) {
michael@0 401 printf("testfile failed to unlink file %s\n",pathname);
michael@0 402 rv = -1;
michael@0 403 }
michael@0 404 return rv;
michael@0 405 }
michael@0 406
michael@0 407
michael@0 408 static PRInt32 PR_CALLBACK FileTest(void)
michael@0 409 {
michael@0 410 PRDir *fd_dir;
michael@0 411 int i, offset, len, rv = 0;
michael@0 412 PRThread *t;
michael@0 413 PRThreadScope scope = PR_GLOBAL_THREAD;
michael@0 414 File_Rdwr_Param *fparamp;
michael@0 415
michael@0 416 /*
michael@0 417 * Create Test dir
michael@0 418 */
michael@0 419 if ((PR_MkDir(TEST_DIR, 0777)) < 0) {
michael@0 420 printf("testfile failed to create dir %s\n",TEST_DIR);
michael@0 421 return -1;
michael@0 422 }
michael@0 423 fd_dir = PR_OpenDir(TEST_DIR);
michael@0 424 if (fd_dir == NULL) {
michael@0 425 printf("testfile failed to open dir %s\n",TEST_DIR);
michael@0 426 rv = -1;
michael@0 427 goto cleanup;
michael@0 428 }
michael@0 429
michael@0 430 PR_CloseDir(fd_dir);
michael@0 431
michael@0 432 strcat(pathname, TEST_DIR);
michael@0 433 strcat(pathname, "/");
michael@0 434 strcat(pathname, FILE_NAME);
michael@0 435
michael@0 436 in_buf = PR_NEW(buffer);
michael@0 437 if (in_buf == NULL) {
michael@0 438 printf(
michael@0 439 "testfile failed to alloc buffer struct\n");
michael@0 440 rv = -1;
michael@0 441 goto cleanup;
michael@0 442 }
michael@0 443 out_buf = PR_NEW(buffer);
michael@0 444 if (out_buf == NULL) {
michael@0 445 printf(
michael@0 446 "testfile failed to alloc buffer struct\n");
michael@0 447 rv = -1;
michael@0 448 goto cleanup;
michael@0 449 }
michael@0 450
michael@0 451 /*
michael@0 452 * Start a bunch of writer threads
michael@0 453 */
michael@0 454 offset = 0;
michael@0 455 len = CHUNK_SIZE;
michael@0 456 PR_EnterMonitor(mon);
michael@0 457 for (i = 0; i < NUM_RDWR_THREADS; i++) {
michael@0 458 fparamp = PR_NEW(File_Rdwr_Param);
michael@0 459 if (fparamp == NULL) {
michael@0 460 printf(
michael@0 461 "testfile failed to alloc File_Rdwr_Param struct\n");
michael@0 462 rv = -1;
michael@0 463 goto cleanup;
michael@0 464 }
michael@0 465 fparamp->pathname = pathname;
michael@0 466 fparamp->buf = out_buf->data + offset;
michael@0 467 fparamp->offset = offset;
michael@0 468 fparamp->len = len;
michael@0 469 memset(fparamp->buf, i, len);
michael@0 470
michael@0 471 t = create_new_thread(PR_USER_THREAD,
michael@0 472 File_Write, (void *)fparamp,
michael@0 473 PR_PRIORITY_NORMAL,
michael@0 474 scope,
michael@0 475 PR_UNJOINABLE_THREAD,
michael@0 476 0, i);
michael@0 477 offset += len;
michael@0 478 }
michael@0 479 thread_count = i;
michael@0 480 /* Wait for writer threads to exit */
michael@0 481 while (thread_count) {
michael@0 482 PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
michael@0 483 }
michael@0 484 PR_ExitMonitor(mon);
michael@0 485
michael@0 486
michael@0 487 /*
michael@0 488 * Start a bunch of reader threads
michael@0 489 */
michael@0 490 offset = 0;
michael@0 491 len = CHUNK_SIZE;
michael@0 492 PR_EnterMonitor(mon);
michael@0 493 for (i = 0; i < NUM_RDWR_THREADS; i++) {
michael@0 494 fparamp = PR_NEW(File_Rdwr_Param);
michael@0 495 if (fparamp == NULL) {
michael@0 496 printf(
michael@0 497 "testfile failed to alloc File_Rdwr_Param struct\n");
michael@0 498 rv = -1;
michael@0 499 goto cleanup;
michael@0 500 }
michael@0 501 fparamp->pathname = pathname;
michael@0 502 fparamp->buf = in_buf->data + offset;
michael@0 503 fparamp->offset = offset;
michael@0 504 fparamp->len = len;
michael@0 505
michael@0 506 t = create_new_thread(PR_USER_THREAD,
michael@0 507 File_Read, (void *)fparamp,
michael@0 508 PR_PRIORITY_NORMAL,
michael@0 509 scope,
michael@0 510 PR_UNJOINABLE_THREAD,
michael@0 511 0, i);
michael@0 512 offset += len;
michael@0 513 if ((offset + len) > BUF_DATA_SIZE)
michael@0 514 break;
michael@0 515 }
michael@0 516 thread_count = i;
michael@0 517
michael@0 518 /* Wait for reader threads to exit */
michael@0 519 while (thread_count) {
michael@0 520 PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
michael@0 521 }
michael@0 522 PR_ExitMonitor(mon);
michael@0 523
michael@0 524 if (memcmp(in_buf->data, out_buf->data, offset) != 0) {
michael@0 525 printf("File Test failed: file data corrupted\n");
michael@0 526 rv = -1;
michael@0 527 goto cleanup;
michael@0 528 }
michael@0 529
michael@0 530 if ((PR_Delete(pathname)) < 0) {
michael@0 531 printf("testfile failed to unlink file %s\n",pathname);
michael@0 532 rv = -1;
michael@0 533 goto cleanup;
michael@0 534 }
michael@0 535
michael@0 536 /*
michael@0 537 * Test PR_Available, PR_Seek, PR_GetFileInfo, PR_Rename, PR_Access
michael@0 538 */
michael@0 539 if (Misc_File_Tests(pathname) < 0) {
michael@0 540 rv = -1;
michael@0 541 }
michael@0 542
michael@0 543 cleanup:
michael@0 544 if ((PR_RmDir(TEST_DIR)) < 0) {
michael@0 545 printf("testfile failed to rmdir %s\n", TEST_DIR);
michael@0 546 rv = -1;
michael@0 547 }
michael@0 548 return rv;
michael@0 549 }
michael@0 550
michael@0 551 struct dirtest_arg {
michael@0 552 PRMonitor *mon;
michael@0 553 PRInt32 done;
michael@0 554 };
michael@0 555
michael@0 556 static PRInt32 RunDirTest(void)
michael@0 557 {
michael@0 558 int i;
michael@0 559 PRThread *t;
michael@0 560 PRMonitor *mon;
michael@0 561 struct dirtest_arg thrarg;
michael@0 562
michael@0 563 mon = PR_NewMonitor();
michael@0 564 if (!mon) {
michael@0 565 printf("RunDirTest: Error - failed to create monitor\n");
michael@0 566 dirtest_failed = 1;
michael@0 567 return -1;
michael@0 568 }
michael@0 569 thrarg.mon = mon;
michael@0 570
michael@0 571 for (i = 0; i < NUM_DIRTEST_THREADS; i++) {
michael@0 572
michael@0 573 thrarg.done= 0;
michael@0 574 t = create_new_thread(PR_USER_THREAD,
michael@0 575 DirTest, &thrarg,
michael@0 576 PR_PRIORITY_NORMAL,
michael@0 577 PR_LOCAL_THREAD,
michael@0 578 PR_UNJOINABLE_THREAD,
michael@0 579 0, i);
michael@0 580 if (!t) {
michael@0 581 printf("RunDirTest: Error - failed to create thread\n");
michael@0 582 dirtest_failed = 1;
michael@0 583 return -1;
michael@0 584 }
michael@0 585 PR_EnterMonitor(mon);
michael@0 586 while (!thrarg.done)
michael@0 587 PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
michael@0 588 PR_ExitMonitor(mon);
michael@0 589
michael@0 590 }
michael@0 591 PR_DestroyMonitor(mon);
michael@0 592 return 0;
michael@0 593 }
michael@0 594
michael@0 595 static PRInt32 PR_CALLBACK DirTest(void *arg)
michael@0 596 {
michael@0 597 struct dirtest_arg *tinfo = (struct dirtest_arg *) arg;
michael@0 598 PRFileDesc *fd_file;
michael@0 599 PRDir *fd_dir;
michael@0 600 int i;
michael@0 601 int path_len;
michael@0 602 PRDirEntry *dirEntry;
michael@0 603 PRFileInfo info;
michael@0 604 PRInt32 num_files = 0;
michael@0 605 #if defined(XP_PC) && defined(WIN32)
michael@0 606 HANDLE hfile;
michael@0 607 #endif
michael@0 608
michael@0 609 #define FILES_IN_DIR 20
michael@0 610
michael@0 611 /*
michael@0 612 * Create Test dir
michael@0 613 */
michael@0 614 DPRINTF(("Creating test dir %s\n",TEST_DIR));
michael@0 615 if ((PR_MkDir(TEST_DIR, 0777)) < 0) {
michael@0 616 printf(
michael@0 617 "testfile failed to create dir %s [%d, %d]\n",
michael@0 618 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 619 return -1;
michael@0 620 }
michael@0 621 fd_dir = PR_OpenDir(TEST_DIR);
michael@0 622 if (fd_dir == NULL) {
michael@0 623 printf(
michael@0 624 "testfile failed to open dirctory %s [%d, %d]\n",
michael@0 625 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 626 return -1;
michael@0 627 }
michael@0 628
michael@0 629 strcpy(pathname, TEST_DIR);
michael@0 630 strcat(pathname, "/");
michael@0 631 strcat(pathname, FILE_NAME);
michael@0 632 path_len = strlen(pathname);
michael@0 633
michael@0 634 for (i = 0; i < FILES_IN_DIR; i++) {
michael@0 635
michael@0 636 sprintf(pathname + path_len,"%d%s",i,"");
michael@0 637
michael@0 638 DPRINTF(("Creating test file %s\n",pathname));
michael@0 639
michael@0 640 fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
michael@0 641
michael@0 642 if (fd_file == NULL) {
michael@0 643 printf(
michael@0 644 "testfile failed to create/open file %s [%d, %d]\n",
michael@0 645 pathname, PR_GetError(), PR_GetOSError());
michael@0 646 return -1;
michael@0 647 }
michael@0 648 PR_Close(fd_file);
michael@0 649 }
michael@0 650 #if defined(XP_UNIX) || (defined(XP_PC) && defined(WIN32)) || defined(XP_OS2) || defined(XP_BEOS)
michael@0 651 /*
michael@0 652 * Create a hidden file - a platform-dependent operation
michael@0 653 */
michael@0 654 strcpy(pathname, TEST_DIR);
michael@0 655 strcat(pathname, "/");
michael@0 656 strcat(pathname, HIDDEN_FILE_NAME);
michael@0 657 #if defined(XP_UNIX) || defined(XP_BEOS)
michael@0 658 DPRINTF(("Creating hidden test file %s\n",pathname));
michael@0 659 fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
michael@0 660
michael@0 661 if (fd_file == NULL) {
michael@0 662 printf(
michael@0 663 "testfile failed to create/open hidden file %s [%d, %d]\n",
michael@0 664 pathname, PR_GetError(), PR_GetOSError());
michael@0 665 return -1;
michael@0 666 }
michael@0 667
michael@0 668 PR_Close(fd_file);
michael@0 669
michael@0 670 #elif defined(WINCE)
michael@0 671 DPRINTF(("Creating hidden test file %s\n",pathname));
michael@0 672 MultiByteToWideChar(CP_ACP, 0, pathname, -1, wPathname, 256);
michael@0 673 hfile = CreateFile(wPathname, GENERIC_READ,
michael@0 674 FILE_SHARE_READ|FILE_SHARE_WRITE,
michael@0 675 NULL,
michael@0 676 CREATE_NEW,
michael@0 677 FILE_ATTRIBUTE_HIDDEN,
michael@0 678 NULL);
michael@0 679 if (hfile == INVALID_HANDLE_VALUE) {
michael@0 680 printf("testfile failed to create/open hidden file %s [0, %d]\n",
michael@0 681 pathname, GetLastError());
michael@0 682 return -1;
michael@0 683 }
michael@0 684 CloseHandle(hfile);
michael@0 685
michael@0 686 #elif defined(XP_PC) && defined(WIN32)
michael@0 687 DPRINTF(("Creating hidden test file %s\n",pathname));
michael@0 688 hfile = CreateFile(pathname, GENERIC_READ,
michael@0 689 FILE_SHARE_READ|FILE_SHARE_WRITE,
michael@0 690 NULL,
michael@0 691 CREATE_NEW,
michael@0 692 FILE_ATTRIBUTE_HIDDEN,
michael@0 693 NULL);
michael@0 694 if (hfile == INVALID_HANDLE_VALUE) {
michael@0 695 printf("testfile failed to create/open hidden file %s [0, %d]\n",
michael@0 696 pathname, GetLastError());
michael@0 697 return -1;
michael@0 698 }
michael@0 699 CloseHandle(hfile);
michael@0 700
michael@0 701 #elif defined(OS2)
michael@0 702 DPRINTF(("Creating hidden test file %s\n",pathname));
michael@0 703 fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, (int)FILE_HIDDEN);
michael@0 704
michael@0 705 if (fd_file == NULL) {
michael@0 706 printf("testfile failed to create/open hidden file %s [%d, %d]\n",
michael@0 707 pathname, PR_GetError(), PR_GetOSError());
michael@0 708 return -1;
michael@0 709 }
michael@0 710 PR_Close(fd_file);
michael@0 711 #endif /* XP_UNIX */
michael@0 712
michael@0 713 #endif /* XP_UNIX || (XP_PC && WIN32) */
michael@0 714
michael@0 715
michael@0 716 if (PR_FAILURE == PR_CloseDir(fd_dir))
michael@0 717 {
michael@0 718 printf(
michael@0 719 "testfile failed to close dirctory %s [%d, %d]\n",
michael@0 720 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 721 return -1;
michael@0 722 }
michael@0 723 fd_dir = PR_OpenDir(TEST_DIR);
michael@0 724 if (fd_dir == NULL) {
michael@0 725 printf(
michael@0 726 "testfile failed to reopen dirctory %s [%d, %d]\n",
michael@0 727 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 728 return -1;
michael@0 729 }
michael@0 730
michael@0 731 /*
michael@0 732 * List all files, including hidden files
michael@0 733 */
michael@0 734 DPRINTF(("Listing all files in directory %s\n",TEST_DIR));
michael@0 735 #if defined(XP_UNIX) || (defined(XP_PC) && defined(WIN32)) || defined(XP_OS2) || defined(XP_BEOS)
michael@0 736 num_files = FILES_IN_DIR + 1;
michael@0 737 #else
michael@0 738 num_files = FILES_IN_DIR;
michael@0 739 #endif
michael@0 740 while ((dirEntry = PR_ReadDir(fd_dir, PR_SKIP_BOTH)) != NULL) {
michael@0 741 num_files--;
michael@0 742 strcpy(pathname, TEST_DIR);
michael@0 743 strcat(pathname, "/");
michael@0 744 strcat(pathname, dirEntry->name);
michael@0 745 DPRINTF(("\t%s\n",dirEntry->name));
michael@0 746
michael@0 747 if ((PR_GetFileInfo(pathname, &info)) < 0) {
michael@0 748 printf(
michael@0 749 "testfile failed to GetFileInfo file %s [%d, %d]\n",
michael@0 750 pathname, PR_GetError(), PR_GetOSError());
michael@0 751 return -1;
michael@0 752 }
michael@0 753
michael@0 754 if (info.type != PR_FILE_FILE) {
michael@0 755 printf(
michael@0 756 "testfile incorrect fileinfo for file %s [%d, %d]\n",
michael@0 757 pathname, PR_GetError(), PR_GetOSError());
michael@0 758 return -1;
michael@0 759 }
michael@0 760 }
michael@0 761 if (num_files != 0)
michael@0 762 {
michael@0 763 printf(
michael@0 764 "testfile failed to find all files in directory %s [%d, %d]\n",
michael@0 765 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 766 return -1;
michael@0 767 }
michael@0 768
michael@0 769 PR_CloseDir(fd_dir);
michael@0 770
michael@0 771 #if defined(XP_UNIX) || (defined(XP_PC) && defined(WIN32)) || defined(XP_OS2) || defined(XP_BEOS)
michael@0 772
michael@0 773 /*
michael@0 774 * List all files, except hidden files
michael@0 775 */
michael@0 776
michael@0 777 fd_dir = PR_OpenDir(TEST_DIR);
michael@0 778 if (fd_dir == NULL) {
michael@0 779 printf(
michael@0 780 "testfile failed to reopen dirctory %s [%d, %d]\n",
michael@0 781 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 782 return -1;
michael@0 783 }
michael@0 784
michael@0 785 DPRINTF(("Listing non-hidden files in directory %s\n",TEST_DIR));
michael@0 786 while ((dirEntry = PR_ReadDir(fd_dir, PR_SKIP_HIDDEN)) != NULL) {
michael@0 787 DPRINTF(("\t%s\n",dirEntry->name));
michael@0 788 if (!strcmp(HIDDEN_FILE_NAME, dirEntry->name)) {
michael@0 789 printf("testfile found hidden file %s\n", pathname);
michael@0 790 return -1;
michael@0 791 }
michael@0 792
michael@0 793 }
michael@0 794 /*
michael@0 795 * Delete hidden file
michael@0 796 */
michael@0 797 strcpy(pathname, TEST_DIR);
michael@0 798 strcat(pathname, "/");
michael@0 799 strcat(pathname, HIDDEN_FILE_NAME);
michael@0 800 if (PR_FAILURE == PR_Delete(pathname)) {
michael@0 801 printf(
michael@0 802 "testfile failed to delete hidden file %s [%d, %d]\n",
michael@0 803 pathname, PR_GetError(), PR_GetOSError());
michael@0 804 return -1;
michael@0 805 }
michael@0 806
michael@0 807 PR_CloseDir(fd_dir);
michael@0 808 #endif /* XP_UNIX || (XP_PC && WIN32) */
michael@0 809
michael@0 810 strcpy(renamename, TEST_DIR);
michael@0 811 strcat(renamename, ".RENAMED");
michael@0 812 if (PR_FAILURE == PR_Rename(TEST_DIR, renamename)) {
michael@0 813 printf(
michael@0 814 "testfile failed to rename directory %s [%d, %d]\n",
michael@0 815 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 816 return -1;
michael@0 817 }
michael@0 818
michael@0 819 if (PR_FAILURE == PR_MkDir(TEST_DIR, 0777)) {
michael@0 820 printf(
michael@0 821 "testfile failed to recreate dir %s [%d, %d]\n",
michael@0 822 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 823 return -1;
michael@0 824 }
michael@0 825 if (PR_SUCCESS == PR_Rename(renamename, TEST_DIR)) {
michael@0 826 printf(
michael@0 827 "testfile renamed directory to existing name %s\n",
michael@0 828 renamename);
michael@0 829 return -1;
michael@0 830 }
michael@0 831
michael@0 832 if (PR_FAILURE == PR_RmDir(TEST_DIR)) {
michael@0 833 printf(
michael@0 834 "testfile failed to rmdir %s [%d, %d]\n",
michael@0 835 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 836 return -1;
michael@0 837 }
michael@0 838
michael@0 839 if (PR_FAILURE == PR_Rename(renamename, TEST_DIR)) {
michael@0 840 printf(
michael@0 841 "testfile failed to rename directory %s [%d, %d]\n",
michael@0 842 renamename, PR_GetError(), PR_GetOSError());
michael@0 843 return -1;
michael@0 844 }
michael@0 845 fd_dir = PR_OpenDir(TEST_DIR);
michael@0 846 if (fd_dir == NULL) {
michael@0 847 printf(
michael@0 848 "testfile failed to reopen directory %s [%d, %d]\n",
michael@0 849 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 850 return -1;
michael@0 851 }
michael@0 852
michael@0 853 strcpy(pathname, TEST_DIR);
michael@0 854 strcat(pathname, "/");
michael@0 855 strcat(pathname, FILE_NAME);
michael@0 856 path_len = strlen(pathname);
michael@0 857
michael@0 858 for (i = 0; i < FILES_IN_DIR; i++) {
michael@0 859
michael@0 860 sprintf(pathname + path_len,"%d%s",i,"");
michael@0 861
michael@0 862 if (PR_FAILURE == PR_Delete(pathname)) {
michael@0 863 printf(
michael@0 864 "testfile failed to delete file %s [%d, %d]\n",
michael@0 865 pathname, PR_GetError(), PR_GetOSError());
michael@0 866 return -1;
michael@0 867 }
michael@0 868 }
michael@0 869
michael@0 870 PR_CloseDir(fd_dir);
michael@0 871
michael@0 872 if (PR_FAILURE == PR_RmDir(TEST_DIR)) {
michael@0 873 printf(
michael@0 874 "testfile failed to rmdir %s [%d, %d]\n",
michael@0 875 TEST_DIR, PR_GetError(), PR_GetOSError());
michael@0 876 return -1;
michael@0 877 }
michael@0 878 PR_EnterMonitor(tinfo->mon);
michael@0 879 tinfo->done = 1;
michael@0 880 PR_Notify(tinfo->mon);
michael@0 881 PR_ExitMonitor(tinfo->mon);
michael@0 882
michael@0 883 return 0;
michael@0 884 }
michael@0 885 /************************************************************************/
michael@0 886
michael@0 887 /*
michael@0 888 * Test file and directory NSPR APIs
michael@0 889 */
michael@0 890
michael@0 891 int main(int argc, char **argv)
michael@0 892 {
michael@0 893 #ifdef WIN32
michael@0 894 PRUint32 len;
michael@0 895 #endif
michael@0 896 #if defined(XP_UNIX) || defined(XP_OS2)
michael@0 897 int opt;
michael@0 898 extern char *optarg;
michael@0 899 extern int optind;
michael@0 900 #endif
michael@0 901 #if defined(XP_UNIX) || defined(XP_OS2)
michael@0 902 while ((opt = getopt(argc, argv, "d")) != EOF) {
michael@0 903 switch(opt) {
michael@0 904 case 'd':
michael@0 905 _debug_on = 1;
michael@0 906 break;
michael@0 907 default:
michael@0 908 break;
michael@0 909 }
michael@0 910 }
michael@0 911 #endif
michael@0 912 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
michael@0 913 PR_STDIO_INIT();
michael@0 914
michael@0 915 mon = PR_NewMonitor();
michael@0 916 if (mon == NULL) {
michael@0 917 printf("testfile: PR_NewMonitor failed\n");
michael@0 918 exit(2);
michael@0 919 }
michael@0 920 #ifdef WIN32
michael@0 921
michael@0 922 #ifdef WINCE
michael@0 923 {
michael@0 924 WCHAR tdir[TMPDIR_LEN];
michael@0 925 len = GetTempPath(TMPDIR_LEN, tdir);
michael@0 926 if ((len > 0) && (len < (TMPDIR_LEN - 6))) {
michael@0 927 /*
michael@0 928 * enough space for prdir
michael@0 929 */
michael@0 930 WideCharToMultiByte(CP_ACP, 0, tdir, -1, testdir, TMPDIR_LEN, 0, 0);
michael@0 931 }
michael@0 932 }
michael@0 933 #else
michael@0 934 len = GetTempPath(TMPDIR_LEN, testdir);
michael@0 935 #endif /* WINCE */
michael@0 936
michael@0 937 if ((len > 0) && (len < (TMPDIR_LEN - 6))) {
michael@0 938 /*
michael@0 939 * enough space for prdir
michael@0 940 */
michael@0 941 strcpy((testdir + len),"prdir");
michael@0 942 TEST_DIR = testdir;
michael@0 943 printf("TEST_DIR = %s\n",TEST_DIR);
michael@0 944 }
michael@0 945 #endif /* WIN32 */
michael@0 946
michael@0 947 if (FileTest() < 0) {
michael@0 948 printf("File Test failed\n");
michael@0 949 exit(2);
michael@0 950 }
michael@0 951 printf("File Test passed\n");
michael@0 952 if ((RunDirTest() < 0) || dirtest_failed) {
michael@0 953 printf("Dir Test failed\n");
michael@0 954 exit(2);
michael@0 955 }
michael@0 956 printf("Dir Test passed\n");
michael@0 957
michael@0 958 PR_DestroyMonitor(mon);
michael@0 959 PR_Cleanup();
michael@0 960 return 0;
michael@0 961 }

mercurial