home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / testfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  19.1 KB  |  800 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "nspr.h"
  20. #include "prpriv.h"
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. static int _debug_on = 0;
  27.  
  28. #ifdef XP_MAC
  29. #include "prlog.h"
  30. #include "primpl.h"
  31. #define printf PR_LogPrint
  32. #define setbuf(x,y)
  33. extern void SetupMacPrintfLog(char *logFile);
  34. #endif
  35.  
  36. #ifdef XP_PC
  37. #define mode_t int
  38. #endif
  39.  
  40. #define DPRINTF(arg) if (_debug_on) printf arg
  41.  
  42. PRLock *lock;
  43. PRMonitor *mon;
  44. PRMonitor *mon2;
  45. PRInt32 count;
  46. int thread_count;
  47.  
  48. #ifdef WIN16
  49. #define    BUF_DATA_SIZE    256 * 120
  50. #else
  51. #define    BUF_DATA_SIZE    256 * 1024
  52. #endif
  53.  
  54. #define NUM_RDWR_THREADS 10
  55. #define CHUNK_SIZE 512
  56.  
  57. typedef struct buffer {
  58.     char    data[BUF_DATA_SIZE];
  59. } buffer;
  60.  
  61. typedef struct File_Rdwr_Param {
  62.     char    *pathname;
  63.     char    *buf;
  64.     int    offset;
  65.     int    len;
  66. } File_Rdwr_Param;
  67.  
  68. #ifdef XP_PC
  69. char *TEST_DIR = "C:\\temp\\prdir";
  70. char *FILE_NAME = "pr_testfile";
  71. char *HIDDEN_FILE_NAME = "hidden_pr_testfile";
  72. #else
  73. char *TEST_DIR = "/tmp/testfile_dir";
  74. char *FILE_NAME = "pr_testfile";
  75. char *HIDDEN_FILE_NAME = ".hidden_pr_testfile";
  76. #endif
  77. buffer *in_buf, *out_buf;
  78. char pathname[256], renamename[256];
  79.  
  80. static void PR_CALLBACK File_Write(void *arg)
  81. {
  82. PRFileDesc *fd_file;
  83. File_Rdwr_Param *fp = (File_Rdwr_Param *) arg;
  84. char *name, *buf;
  85. int offset, len;
  86.  
  87.     setbuf(stdout, NULL);
  88.     name = fp->pathname;
  89.     buf = fp->buf;
  90.     offset = fp->offset;
  91.     len = fp->len;
  92.     
  93.     fd_file = PR_Open(name, PR_RDWR | PR_CREATE_FILE, 0777);
  94.     if (fd_file == NULL) {
  95.         printf("testfile failed to create/open file %s\n",name);
  96.         return;
  97.     }
  98.     if (PR_Seek(fd_file, offset, PR_SEEK_SET) < 0) {
  99.         printf("testfile failed to seek in file %s\n",name);
  100.         return;
  101.     }    
  102.     if ((PR_Write(fd_file, buf, len)) < 0) {
  103.         printf("testfile failed to write to file %s\n",name);
  104.         return;
  105.     }    
  106.     DPRINTF(("Write out_buf[0] = 0x%x\n",(*((int *) buf))));
  107.     PR_Close(fd_file);
  108.  
  109.     PR_EnterMonitor(mon2);
  110.     --thread_count;
  111.     PR_Notify(mon2);
  112.     PR_ExitMonitor(mon2);
  113. }
  114.  
  115. static void PR_CALLBACK File_Read(void *arg)
  116. {
  117. PRFileDesc *fd_file;
  118. File_Rdwr_Param *fp = (File_Rdwr_Param *) arg;
  119. char *name, *buf;
  120. int offset, len;
  121.  
  122.     setbuf(stdout, NULL);
  123.     name = fp->pathname;
  124.     buf = fp->buf;
  125.     offset = fp->offset;
  126.     len = fp->len;
  127.     
  128.     fd_file = PR_Open(name, PR_RDONLY, 0);
  129.     if (fd_file == NULL) {
  130.         printf("testfile failed to open file %s\n",name);
  131.         return;
  132.     }
  133.     if (PR_Seek(fd_file, offset, PR_SEEK_SET) < 0) {
  134.         printf("testfile failed to seek in file %s\n",name);
  135.         return;
  136.     }    
  137.     if ((PR_Read(fd_file, buf, len)) < 0) {
  138.         printf("testfile failed to read to file %s\n",name);
  139.         return;
  140.     }    
  141.     DPRINTF(("Read in_buf[0] = 0x%x\n",(*((int *) buf))));
  142.     PR_Close(fd_file);
  143.  
  144.     PR_EnterMonitor(mon2);
  145.     --thread_count;
  146.     PR_Notify(mon2);
  147.     PR_ExitMonitor(mon2);
  148. }
  149.  
  150.  
  151. static void Misc_File_Tests(char *pathname)
  152. {
  153. PRFileDesc *fd_file;
  154. int len;
  155. PRFileInfo file_info, file_info1;
  156. char tmpname[1024];
  157.  
  158.     setbuf(stdout, NULL);
  159.     /*
  160.      * Test PR_Available, PR_Seek, PR_GetFileInfo, PR_Rename, PR_Access
  161.      */
  162.  
  163.     fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
  164.  
  165.     if (fd_file == NULL) {
  166.         printf("testfile failed to create/open file %s\n",pathname);
  167.         return;
  168.     }
  169.     if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) {
  170.         printf("testfile PR_GetFileInfo failed on file %s\n",pathname);
  171.         goto cleanup;
  172.     }
  173.     if (PR_Access(pathname, PR_ACCESS_EXISTS) != 0) {
  174.         printf("testfile PR_Access failed on file %s\n",pathname);
  175.         goto cleanup;
  176.     }
  177.     if (PR_Access(pathname, PR_ACCESS_WRITE_OK) != 0) {
  178.         printf("testfile PR_Access failed on file %s\n",pathname);
  179.         goto cleanup;
  180.     }
  181.     if (PR_Access(pathname, PR_ACCESS_READ_OK) != 0) {
  182.         printf("testfile PR_Access failed on file %s\n",pathname);
  183.         goto cleanup;
  184.     }
  185.  
  186.  
  187.     if (PR_GetFileInfo(pathname, &file_info) < 0) {
  188.         printf("testfile PR_GetFileInfo failed on file %s\n",pathname);
  189.         return;
  190.     }
  191.     if (file_info.type != PR_FILE_FILE) {
  192.         printf(
  193.         "testfile PR_GetFileInfo returned incorrect type for file %s\n",
  194.         pathname);
  195.         goto cleanup;
  196.     }
  197.     if (file_info.size != 0) {
  198.         printf(
  199.         "testfile PR_GetFileInfo returned incorrect size (%d should be 0) for file %s\n",
  200.         file_info.size, pathname);
  201.         goto cleanup;
  202.     }
  203.     file_info1 = file_info;
  204.  
  205.     len = PR_Available(fd_file);
  206.     if (len < 0) {
  207.         printf("testfile PR_Available failed on file %s\n",pathname);
  208.         goto cleanup;
  209.     } else if (len != 0) {
  210.         printf(
  211.         "testfile PR_Available failed: expected/returned = %d/%d bytes\n",
  212.             0, len);
  213.         goto cleanup;
  214.     }
  215.     len = PR_Write(fd_file, out_buf->data, CHUNK_SIZE);
  216.     if (len < 0) {
  217.         printf("testfile failed to write to file %s\n",pathname);
  218.         goto cleanup;
  219.     }
  220.     if (PR_GetOpenFileInfo(fd_file, &file_info) < 0) {
  221.         printf("testfile PR_GetFileInfo failed on file %s\n",pathname);
  222.         goto cleanup;
  223.     }
  224.     if (file_info.size != CHUNK_SIZE) {
  225.         printf(
  226.         "testfile PR_GetFileInfo returned incorrect size (%d should be %d) for file %s\n",
  227.         file_info.size, CHUNK_SIZE, pathname);
  228.         goto cleanup;
  229.     }
  230.     if (LL_NE(file_info.creationTime , file_info1.creationTime)) {
  231.         printf(
  232.         "testfile PR_GetFileInfo returned incorrect creation time: %s\n",
  233.         pathname);
  234.         goto cleanup;
  235.     }
  236.     if (LL_CMP(file_info.modifyTime, > , file_info1.modifyTime)) {
  237.         printf(
  238.         "testfile PR_GetFileInfo returned incorrect modify time: %s\n",
  239.         pathname);
  240.         goto cleanup;
  241.     }
  242.  
  243.     len = PR_Available(fd_file);
  244.     if (len < 0) {
  245.         printf("testfile PR_Available failed on file %s\n",pathname);
  246.         goto cleanup;
  247.     } else if (len != 0) {
  248.         printf(
  249.         "testfile PR_Available failed: expected/returned = %d/%d bytes\n",
  250.             0, len);
  251.         goto cleanup;
  252.     }
  253.     
  254.     PR_Seek(fd_file, 0, PR_SEEK_SET);
  255.     len = PR_Available(fd_file);
  256.     if (len < 0) {
  257.         printf("testfile PR_Available failed on file %s\n",pathname);
  258.         return;
  259.     } else if (len != CHUNK_SIZE) {
  260.         printf(
  261.         "testfile PR_Available failed: expected/returned = %d/%d bytes\n",
  262.             CHUNK_SIZE, len);
  263.         goto cleanup;
  264.     }
  265.     PR_Close(fd_file);
  266.  
  267.     strcpy(tmpname,pathname);
  268.     strcat(tmpname,".RENAMED");
  269.     if (PR_FAILURE == PR_Rename(pathname, tmpname)) {
  270.         printf("testfile failed to rename file %s\n",pathname);
  271.         goto cleanup;
  272.     }
  273.  
  274.     fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
  275.     len = PR_Write(fd_file, out_buf->data, CHUNK_SIZE);
  276.     PR_Close(fd_file);
  277.     if (PR_SUCCESS == PR_Rename(pathname, tmpname)) {
  278.         printf("testfile renamed to existing file %s\n",pathname);
  279.     }
  280.  
  281.     if ((PR_Delete(tmpname)) < 0)
  282.         printf("testfile failed to unlink file %s\n",tmpname);
  283.  
  284. cleanup:
  285.     if ((PR_Delete(pathname)) < 0)
  286.         printf("testfile failed to unlink file %s\n",pathname);
  287. }
  288.  
  289.  
  290. static PRInt32 PR_CALLBACK FileTest(void)
  291. {
  292. PRDir *fd_dir;
  293. int i, offset, len;
  294. PRThread *t;
  295. File_Rdwr_Param *fparamp;
  296.  
  297.     /*
  298.      * Create Test dir
  299.      */
  300.     if ((PR_MkDir(TEST_DIR, 0777)) < 0) {
  301.         printf("testfile failed to create dir %s\n",TEST_DIR);
  302.         return -1;
  303.     }
  304.     fd_dir = PR_OpenDir(TEST_DIR);
  305.     if (fd_dir == NULL) {
  306.         printf("testfile failed to open dir %s\n",TEST_DIR);
  307.         return -1;
  308.     }
  309.  
  310.     PR_CloseDir(fd_dir);
  311.  
  312.     strcat(pathname, TEST_DIR);
  313.     strcat(pathname, "/");
  314.     strcat(pathname, FILE_NAME);
  315.  
  316.     in_buf = PR_NEW(buffer);
  317.     if (in_buf == NULL) {
  318.         printf(
  319.         "testfile failed to alloc buffer struct\n");
  320.         return -1;
  321.     }
  322.     out_buf = PR_NEW(buffer);
  323.     if (out_buf == NULL) {
  324.         printf(
  325.         "testfile failed to alloc buffer struct\n");
  326.         return -1;
  327.     }
  328.  
  329.     /*
  330.      * Start a bunch of writer threads
  331.      */
  332.     offset = 0;
  333.     len = CHUNK_SIZE;
  334.     mon2 = PR_NewMonitor();
  335.     if (mon2 == NULL) {
  336.         printf("testfile: PR_NewMonitor failed\n");
  337.         return -1;
  338.     }
  339.     PR_EnterMonitor(mon2);
  340.     for (i = 0; i < NUM_RDWR_THREADS; i++) {
  341.         fparamp = PR_NEW(File_Rdwr_Param);
  342.         if (fparamp == NULL) {
  343.             printf(
  344.             "testfile failed to alloc File_Rdwr_Param struct\n");
  345.             return -1;
  346.         }
  347.         fparamp->pathname = pathname;
  348.         fparamp->buf = out_buf->data + offset;
  349.         fparamp->offset = offset;
  350.         fparamp->len = len;
  351.         memset(fparamp->buf, i, len);
  352.         /*
  353.          * Create LOCAL and GLOBAL Threads, alternately
  354.          */
  355.         if (i % 1)
  356.             t = PR_CreateThread(PR_USER_THREAD,
  357.                       File_Write, (void *)fparamp, 
  358.                       PR_PRIORITY_NORMAL,
  359.                       PR_GLOBAL_THREAD,
  360.                       PR_UNJOINABLE_THREAD,
  361.                       0);
  362.         else
  363.             t = PR_CreateThread(PR_USER_THREAD,
  364.                       File_Write, (void *)fparamp, 
  365.                       PR_PRIORITY_NORMAL,
  366.                       PR_LOCAL_THREAD,
  367.                       PR_UNJOINABLE_THREAD,
  368.                       0);
  369.         offset += len;
  370.     }
  371.     thread_count = i;
  372.     /* Wait for writer threads to exit */
  373.     while (thread_count) {
  374.         PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
  375.     }
  376.     PR_ExitMonitor(mon2);
  377.  
  378.  
  379.     /*
  380.      * Start a bunch of reader threads
  381.      */
  382.     offset = 0;
  383.     len = CHUNK_SIZE;
  384.     PR_EnterMonitor(mon2);
  385.     for (i = 0; i < NUM_RDWR_THREADS; i++) {
  386.         fparamp = PR_NEW(File_Rdwr_Param);
  387.         if (fparamp == NULL) {
  388.             printf(
  389.             "testfile failed to alloc File_Rdwr_Param struct\n");
  390.             return -1;
  391.         }
  392.         fparamp->pathname = pathname;
  393.         fparamp->buf = in_buf->data + offset;
  394.         fparamp->offset = offset;
  395.         fparamp->len = len;
  396.         /*
  397.          * Create LOCAL and GLOBAL Threads, alternately
  398.          */
  399.         if (i % 1)
  400.             t = PR_CreateThread(PR_USER_THREAD,
  401.                       File_Read, (void *)fparamp, 
  402.                       PR_PRIORITY_NORMAL,
  403.                       PR_LOCAL_THREAD,
  404.                       PR_UNJOINABLE_THREAD,
  405.                       0);
  406.         else
  407.             t = PR_CreateThread(PR_USER_THREAD,
  408.                       File_Read, (void *)fparamp, 
  409.                       PR_PRIORITY_NORMAL,
  410.                       PR_GLOBAL_THREAD,
  411.                       PR_UNJOINABLE_THREAD,
  412.                       0);
  413.         offset += len;
  414.         if ((offset + len) > BUF_DATA_SIZE)
  415.             break;
  416.     }
  417.     thread_count = i;
  418.  
  419.     /* Wait for reader threads to exit */
  420.     while (thread_count) {
  421.         PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
  422.     }
  423.     PR_ExitMonitor(mon2);
  424.  
  425.     if (memcmp(in_buf->data, out_buf->data, offset) != 0) {
  426.         printf("File Test failed: file data corrupted\n");
  427.     }
  428.  
  429.     if ((PR_Delete(pathname)) < 0) {
  430.         printf("testfile failed to unlink file %s\n",pathname);
  431.         return -1;
  432.     }
  433.  
  434.     /*
  435.      * Test PR_Available, PR_Seek, PR_GetFileInfo, PR_Rename, PR_Access
  436.      */
  437.     Misc_File_Tests(pathname);
  438.  
  439.     if ((PR_RmDir(TEST_DIR)) < 0) {
  440.         printf("testfile failed to rmdir %s\n", TEST_DIR);
  441.         return -1;
  442.     }
  443.     return 0;
  444. }
  445.  
  446. static PRInt32 PR_CALLBACK DirTest(void)
  447. {
  448. PRFileDesc *fd_file;
  449. PRDir *fd_dir;
  450. int i;
  451. int path_len;
  452. PRDirEntry *dirEntry;
  453. PRFileInfo info;
  454. PRInt32 num_files = 0;
  455. #if defined(XP_PC) && defined(WIN32)
  456. HANDLE hfile;
  457. #endif
  458.  
  459. #define  FILES_IN_DIR 20
  460.  
  461.     /*
  462.      * Create Test dir
  463.      */
  464.     DPRINTF(("Creating test dir %s\n",TEST_DIR));
  465.     if ((PR_MkDir(TEST_DIR, 0777)) < 0) {
  466.         printf(
  467.             "testfile failed to create dir %s [%d, %d]\n",
  468.             TEST_DIR, PR_GetError(), PR_GetOSError());
  469.         return -1;
  470.     }
  471.     fd_dir = PR_OpenDir(TEST_DIR);
  472.     if (fd_dir == NULL) {
  473.         printf(
  474.             "testfile failed to open dirctory %s [%d, %d]\n",
  475.             TEST_DIR, PR_GetError(), PR_GetOSError());
  476.         return -1;
  477.     }
  478.  
  479.     strcpy(pathname, TEST_DIR);
  480.     strcat(pathname, "/");
  481.     strcat(pathname, FILE_NAME);
  482.     path_len = strlen(pathname);
  483.     
  484.     for (i = 0; i < FILES_IN_DIR; i++) {
  485.  
  486.         sprintf(pathname + path_len,"%d%s",i,"");
  487.  
  488.         DPRINTF(("Creating test file %s\n",pathname));
  489.  
  490.         fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
  491.  
  492.         if (fd_file == NULL) {
  493.             printf(
  494.                     "testfile failed to create/open file %s [%d, %d]\n",
  495.                     pathname, PR_GetError(), PR_GetOSError());
  496.             return -1;
  497.         }
  498.         PR_Close(fd_file);
  499.     }
  500. #if defined(XP_UNIX) || defined(XP_MAC) || (defined(XP_PC) && defined(WIN32))
  501.     /*
  502.      * Create a hidden file - a platform-dependent operation
  503.      */
  504.     strcpy(pathname, TEST_DIR);
  505.     strcat(pathname, "/");
  506.     strcat(pathname, HIDDEN_FILE_NAME);
  507. #if defined(XP_UNIX) || defined(XP_MAC)
  508.     DPRINTF(("Creating hidden test file %s\n",pathname));
  509.     fd_file = PR_Open(pathname, PR_RDWR | PR_CREATE_FILE, 0777);
  510.  
  511.     if (fd_file == NULL) {
  512.         printf(
  513.                 "testfile failed to create/open hidden file %s [%d, %d]\n",
  514.                 pathname, PR_GetError(), PR_GetOSError());
  515.         return -1;
  516.     }
  517.  
  518. #if defined(XP_MAC)
  519.     {
  520. #include <files.h>
  521.  
  522.     OSErr            err;
  523.     FCBPBRec        fcbpb;
  524.     CInfoPBRec        pb;
  525.     Str255            pascalMacPath;
  526.  
  527.     fcbpb.ioNamePtr = pascalMacPath;
  528.     fcbpb.ioVRefNum = 0;
  529.     fcbpb.ioRefNum = fd_file->secret->md.osfd;
  530.     fcbpb.ioFCBIndx = 0;
  531.     
  532.     err = PBGetFCBInfoSync(&fcbpb);
  533.     if (err != noErr) {
  534.         PR_Close(fd_file);
  535.         return -1;
  536.     }
  537.     
  538.     pb.hFileInfo.ioNamePtr = pascalMacPath;
  539.     pb.hFileInfo.ioVRefNum = fcbpb.ioFCBVRefNum;
  540.     pb.hFileInfo.ioDirID = fcbpb.ioFCBParID;
  541.     pb.hFileInfo.ioFDirIndex = 0;
  542.     
  543.     err = PBGetCatInfoSync(&pb);
  544.     if (err != noErr) {
  545.         PR_Close(fd_file);
  546.         return -1;
  547.     }
  548.  
  549.     pb.hFileInfo.ioNamePtr = pascalMacPath;
  550.     pb.hFileInfo.ioVRefNum = fcbpb.ioFCBVRefNum;
  551.     pb.hFileInfo.ioDirID = fcbpb.ioFCBParID;
  552.     pb.hFileInfo.ioFDirIndex = 0;
  553.     
  554.     pb.hFileInfo.ioFlFndrInfo.fdFlags |= fInvisible;
  555.  
  556.     err = PBSetCatInfoSync(&pb);
  557.     if (err != noErr) {
  558.         PR_Close(fd_file);
  559.         return -1;
  560.     }
  561.  
  562.     }
  563. #endif
  564.  
  565.     PR_Close(fd_file);
  566.  
  567.     
  568. #elif defined(XP_PC) && defined(WIN32)
  569.     DPRINTF(("Creating hidden test file %s\n",pathname));
  570.     hfile = CreateFile(pathname, GENERIC_READ,
  571.                         FILE_SHARE_READ|FILE_SHARE_WRITE,
  572.                         NULL,
  573.                         CREATE_NEW,
  574.                         FILE_ATTRIBUTE_HIDDEN,
  575.                         NULL);
  576.     if (hfile == INVALID_HANDLE_VALUE) {
  577.         printf("testfile failed to create/open hidden file %s [0, %d]\n",
  578.                 pathname, GetLastError());
  579.         return -1;
  580.     }
  581.     CloseHandle(hfile);
  582.                         
  583. #endif    /* XP _UNIX || XP_MAC*/
  584.  
  585. #endif    /* XP_UNIX || XP_MAC ||(XP_PC && WIN32) */
  586.  
  587.  
  588.     if (PR_FAILURE == PR_CloseDir(fd_dir))
  589.     {
  590.         printf(
  591.             "testfile failed to close dirctory %s [%d, %d]\n",
  592.             TEST_DIR, PR_GetError(), PR_GetOSError());
  593.         return -1;
  594.     }
  595.     fd_dir = PR_OpenDir(TEST_DIR);
  596.     if (fd_dir == NULL) {
  597.         printf(
  598.             "testfile failed to reopen dirctory %s [%d, %d]\n",
  599.             TEST_DIR, PR_GetError(), PR_GetOSError());
  600.         return -1;
  601.     }
  602.   
  603.     /*
  604.      * List all files, including hidden files
  605.      */
  606.     DPRINTF(("Listing all files in directory %s\n",TEST_DIR));
  607. #if defined(XP_UNIX) || defined(XP_MAC) || (defined(XP_PC) && defined(WIN32))
  608.     num_files = FILES_IN_DIR + 1;
  609. #else
  610.     num_files = FILES_IN_DIR;
  611. #endif
  612.     while ((dirEntry = PR_ReadDir(fd_dir, PR_SKIP_BOTH)) != NULL) {
  613.         num_files--;
  614.         strcpy(pathname, TEST_DIR);
  615.         strcat(pathname, "/");
  616.         strcat(pathname, dirEntry->name);
  617.         DPRINTF(("\t%s\n",dirEntry->name));
  618.  
  619.         if ((PR_GetFileInfo(pathname, &info)) < 0) {
  620.             printf(
  621.                 "testfile failed to GetFileInfo file %s [%d, %d]\n",
  622.                 pathname, PR_GetError(), PR_GetOSError());
  623.             return -1;
  624.         }
  625.         
  626.         if (info.type != PR_FILE_FILE) {
  627.             printf(
  628.                 "testfile incorrect fileinfo for file %s [%d, %d]\n",
  629.                 pathname, PR_GetError(), PR_GetOSError());
  630.             return -1;
  631.         }
  632.     }
  633.     if (num_files != 0)
  634.     {
  635.         printf(
  636.             "testfile failed to find all files in directory %s [%d, %d]\n",
  637.             TEST_DIR, PR_GetError(), PR_GetOSError());
  638.         return -1;
  639.     }
  640.  
  641.     PR_CloseDir(fd_dir);
  642.  
  643. #if defined(XP_UNIX) || defined(XP_MAC) || (defined(XP_PC) && defined(WIN32))
  644.  
  645.     /*
  646.      * List all files, except hidden files
  647.      */
  648.  
  649.     fd_dir = PR_OpenDir(TEST_DIR);
  650.     if (fd_dir == NULL) {
  651.         printf(
  652.             "testfile failed to reopen dirctory %s [%d, %d]\n",
  653.             TEST_DIR, PR_GetError(), PR_GetOSError());
  654.         return -1;
  655.     }
  656.   
  657.     DPRINTF(("Listing non-hidden files in directory %s\n",TEST_DIR));
  658.     while ((dirEntry = PR_ReadDir(fd_dir, PR_SKIP_HIDDEN)) != NULL) {
  659.         DPRINTF(("\t%s\n",dirEntry->name));
  660.         if (!strcmp(HIDDEN_FILE_NAME, dirEntry->name)) {
  661.             printf("testfile found hidden file %s\n", pathname);
  662.             return -1;
  663.         }
  664.  
  665.     }
  666.     /*
  667.      * Delete hidden file
  668.      */
  669.     strcpy(pathname, TEST_DIR);
  670.     strcat(pathname, "/");
  671.     strcat(pathname, HIDDEN_FILE_NAME);
  672.     if (PR_FAILURE == PR_Delete(pathname)) {
  673.         printf(
  674.             "testfile failed to delete hidden file %s [%d, %d]\n",
  675.             pathname, PR_GetError(), PR_GetOSError());
  676.         return -1;
  677.     }
  678.  
  679.     PR_CloseDir(fd_dir);
  680. #endif    /* XP_UNIX || XP_MAC || (XP_PC && WIN32) */
  681.  
  682.     strcpy(renamename, TEST_DIR);
  683.     strcat(renamename, ".RENAMED");
  684.     if (PR_FAILURE == PR_Rename(TEST_DIR, renamename)) {
  685.         printf(
  686.             "testfile failed to rename directory %s [%d, %d]\n",
  687.             TEST_DIR, PR_GetError(), PR_GetOSError());
  688.         return -1;
  689.     }
  690.     
  691.     if (PR_FAILURE == PR_MkDir(TEST_DIR, 0777)) {
  692.         printf(
  693.             "testfile failed to recreate dir %s [%d, %d]\n",
  694.             TEST_DIR, PR_GetError(), PR_GetOSError());
  695.         return -1;
  696.     }
  697.     if (PR_SUCCESS == PR_Rename(renamename, TEST_DIR)) {
  698.         printf(
  699.             "testfile renamed directory to existing name %s\n",
  700.             renamename);
  701.         return -1;
  702.     }
  703.  
  704.     if (PR_FAILURE == PR_RmDir(TEST_DIR)) {
  705.         printf(
  706.             "testfile failed to rmdir %s [%d, %d]\n",
  707.             TEST_DIR, PR_GetError(), PR_GetOSError());
  708.         return -1;
  709.     }
  710.  
  711.     if (PR_FAILURE == PR_Rename(renamename, TEST_DIR)) {
  712.         printf(
  713.             "testfile failed to rename directory %s [%d, %d]\n",
  714.             renamename, PR_GetError(), PR_GetOSError());
  715.         return -1;
  716.     }
  717.     fd_dir = PR_OpenDir(TEST_DIR);
  718.     if (fd_dir == NULL) {
  719.         printf(
  720.             "testfile failed to reopen directory %s [%d, %d]\n",
  721.             TEST_DIR, PR_GetError(), PR_GetOSError());
  722.         return -1;
  723.     }
  724.  
  725.     strcpy(pathname, TEST_DIR);
  726.     strcat(pathname, "/");
  727.     strcat(pathname, FILE_NAME);
  728.     path_len = strlen(pathname);
  729.     
  730.     for (i = 0; i < FILES_IN_DIR; i++) {
  731.  
  732.         sprintf(pathname + path_len,"%d%s",i,"");
  733.  
  734.         if (PR_FAILURE == PR_Delete(pathname)) {
  735.             printf(
  736.                 "testfile failed to delete file %s [%d, %d]\n",
  737.                 pathname, PR_GetError(), PR_GetOSError());
  738.             return -1;
  739.         }
  740.     }
  741.  
  742.     PR_CloseDir(fd_dir);
  743.  
  744.     if (PR_FAILURE == PR_RmDir(TEST_DIR)) {
  745.         printf(
  746.             "testfile failed to rmdir %s [%d, %d]\n",
  747.             TEST_DIR, PR_GetError(), PR_GetOSError());
  748.         return -1;
  749.     }
  750.  
  751.     return 0;
  752. }
  753. /************************************************************************/
  754.  
  755. /*
  756.  * Test file and directory NSPR APIs
  757.  */
  758.  
  759. void main(int argc, char **argv)
  760. {
  761. #ifdef XP_UNIX
  762.         int opt;
  763.         extern char *optarg;
  764.     extern int optind;
  765. #endif
  766. #ifdef XP_UNIX
  767.         while ((opt = getopt(argc, argv, "d")) != EOF) {
  768.                 switch(opt) {
  769.                         case 'd':
  770.                                 _debug_on = 1;
  771.                                 break;
  772.                         default:
  773.                                 break;
  774.                 }
  775.         }
  776. #endif
  777.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  778.     PR_STDIO_INIT();
  779.  
  780. #ifdef XP_MAC
  781.     SetupMacPrintfLog("testfile.log");
  782. #endif
  783.  
  784.     mon2 = PR_NewMonitor();
  785.  
  786.     if (FileTest() < 0) {
  787.         printf("File Test failed\n");
  788.         exit(2);
  789.     }
  790.     printf("File Test passed\n");
  791.  
  792.     if (DirTest() < 0) {
  793.         printf("Dir Test failed\n");
  794.         exit(2);
  795.     }
  796.     printf("Dir Test passed\n");
  797.  
  798.     PR_Cleanup();
  799. }
  800.