VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/testfile.c@ 25803

Last change on this file since 25803 was 1, checked in by vboxsync, 55 years ago

import

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette