Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Test code for directory and file handling
27 #include <sys/fcntl.h>
31 #include "CTEST.H" /* includes C interface to EPOC32 threads, and SpawnPosixServer */
34 wchar_t rootpath[MAXPATHLEN];
36 #define WIDENAME {L'/', L'T', L'o', L'p', L'/', L'e', L'u', L'r', L'o', 0x20ac, 0}
38 /* construct a directory tree in various ways */
41 @SYMTestCaseID SYSLIB-STDLIB-CT-1082
42 @SYMTestCaseDesc Tests for operations on directory
44 @SYMTestActions Tests by creating directory with invalid name,existing directory name,wide characters
45 Tests for the error code returned while creating directories.
46 @SYMTestExpectedResults Test must not fail
52 wchar_t namebuf[MAXPATHLEN], namebuf2[MAXPATHLEN];
53 wchar_t toobig[MAXPATHLEN+MAXPATHLEN+1];
56 wchar_t widename[] = WIDENAME;
58 test_Next("Create Directory Tree - relative paths");
60 err=wmkdir((wchar_t*)L"***", 0777);
61 test_errno(err==-1,EINVAL); /* bad directory name */
64 err=wmkdir((wchar_t*)L"top", 0777);
67 err=wmkdir((wchar_t*)L"top", 0777);
68 test_errno(err==-1,EEXIST); /* directory already exists */
70 err=wmkdir(widename, 0777);
73 err=wmkdir((wchar_t*)L"top/middle1/bottom1", 0777);
74 test_errno(err==-1,ENOENT); /* missing middle bit of path */
76 err=wmkdir((wchar_t*)L"top/middle1", 0777);
79 err=wchdir((wchar_t*)L"./top//\\/.\\.\\../top/.");
82 p = wgetcwd(rootpath,sizeof(rootpath)/2); /* save name of toplevel directory */
85 err=wchdir((wchar_t*)L"middle1");
88 err=wchdir((wchar_t*)L"bottom2");
89 test_errno(err==-1,ENOENT); /* directory doesn't exist yet */
91 p = wgetcwd(namebuf,sizeof(namebuf)); /* prepare name for tests later */
94 err=wmkdir((wchar_t*)L"bottom1",0777);
97 err=wmkdir((wchar_t*)L"read-only",0444);
100 err=wmkdir((wchar_t*)L"read-only/sub-read-only",0444);
101 /* test_errno(err==-1,EACCES); */
102 test(err==0); /* Omission - EPOC32 has Win32 semantics for read-only directories */
104 err=wchdir((wchar_t*)L"../../top/middle1/bottom1");
107 test_Next("Create Directory Tree - absolute paths");
109 p = wcscat(namebuf,(wchar_t*)L"/bottom2");
110 test(p==namebuf); /* .../top/middle1/bottom2 */
113 test_errno(err==-1,ENOENT); /* directory doesn't exist yet */
115 err=wmkdir(namebuf, 0777);
121 p = wgetcwd(namebuf,sizeof(namebuf));
124 err=wmkdir((wchar_t*)L"../../middle2", 0777);
127 p = wgetcwd(namebuf2,sizeof(namebuf2));
129 test(wcscmp(namebuf,namebuf2)==0); /* mkdir shouldn't change cwd */
131 // memset(toobig,L'a', sizeof(toobig));
132 // toobig[sizeof(toobig)-1]='\0';
133 for (x = 0; x < (sizeof(toobig)/2)-1; x++)
138 err=wmkdir(toobig,0777);
139 test_errno(err<0,ENAMETOOLONG);
142 test_Next("Test wgetcwd");
145 p = wgetcwd(namebuf, 4);
146 test_errno(0==p, ERANGE);
148 //make it alloc a buffer
149 p = wgetcwd(NULL, 300);
153 //alloc a buffer then fail with a too small size
154 p = wgetcwd(NULL, 10);
155 test_errno(0==p, ERANGE);
157 p = wgetcwd(namebuf2, MAXPATHLEN-1);
161 test_Next("Test wrealpath");
162 p = wrealpath((wchar_t*)L"/top/../top/../top/./",namebuf);
163 test((0==wcscmp((wchar_t*)L"C:\\top\\", p)) || (0==wcscmp((wchar_t*)L"D:\\top\\", p)));
164 test((0==wcscmp((wchar_t*)L"C:\\top\\", namebuf)) || (0==wcscmp((wchar_t*)L"D:\\top\\", namebuf)));
165 p = wrealpath((wchar_t*)L"/top/../top/././top/./",namebuf);
170 Directory tree is now
175 / read-only / sub-read-only
177 @SYMTestCaseID SYSLIB-STDLIB-CT-1083
178 @SYMTestCaseDesc Tests for operations on creating files
179 @SYMTestPriority High
180 @SYMTestActions Tests by opening files which does not exists,check for closing a file twice
181 Tests for the error code returned while creating files
182 @SYMTestExpectedResults Test must not fail
189 wchar_t namebuf[MAXPATHLEN],*p;
191 test_Next("Creating Files - relative paths");
193 err=wchdir(rootpath);
196 fd = wopen((wchar_t*)L"topfile",O_RDWR+O_APPEND,0777);
197 test_errno(fd<0,ENOENT); /* doesn't exist */
199 fd = wopen((wchar_t*)L"topfile",O_RDWR+O_CREAT,0777);
206 test_errno(err<0,EBADF); /* can't close it twice */
208 fd = wopen((wchar_t*)L"topfile",O_RDWR+O_APPEND,0777);
214 fd = wopen((wchar_t*)L"topfile",O_RDWR+O_CREAT+O_EXCL,0777);
215 test_errno(fd<0,EEXIST); /* already exists */
217 fd = wopen((wchar_t*)L"middle1/bottom2/file",O_RDONLY+O_CREAT,0444);
223 fd = wopen((wchar_t*)L"middle1/bottom2/file",O_RDWR+O_APPEND,0777);
224 /* test_errno(fd<0,EACCES); */
225 test(fd>=0); /* Omission - the original O_CREAT ignores the 0444 permissions */
232 err=wchmod((wchar_t*)L"middle1/bottom2/file",0444);
235 fd = wopen((wchar_t*)L"middle1/bottom2/file",O_RDWR+O_APPEND,0777);
236 test_errno(fd<0,EACCES); /* not writeable */
238 fd = wopen((wchar_t*)L"middle2",O_RDWR+O_CREAT,0777);
239 /* test_errno(fd<0,EISDIR); */
240 test_errno(fd<0,EACCES); /* Omission - we don't do EISDIR */
242 test_Next("Creating Files - absolute paths");
244 err=wchdir((wchar_t*)L"middle1/bottom1");
247 p = wgetcwd(namebuf,sizeof(namebuf)/2); /* prepare name for tests later */
250 p = wcscat(namebuf,(wchar_t*)L"absfile");
253 fd = wopen(namebuf,O_RDWR+O_CREAT,0777);
259 fd = wopen((wchar_t*)L"../read-only/file",O_RDWR+O_CREAT,0444);
260 /* test_errno(fd<0,EACCES); */
261 test(fd>=0); /* Omission - EPOC32 has Win32 semantics for read-only directories */
271 Directory tree is now
275 middle1 / bottom1 / absfile
276 / bottom2 / file -- read-only
277 / read-only / sub-read-only /
280 @SYMTestCaseID SYSLIB-STDLIB-CT-1084
281 @SYMTestCaseDesc Tests for renaming operations
282 @SYMTestPriority High
283 @SYMTestActions Tests by renaming files.Tests for the error code returned while renaming files
284 @SYMTestExpectedResults Test must not fail
291 test_Next("Renaming");
293 err=wchdir(rootpath);
296 err=wrename((wchar_t*)L"middle1",(wchar_t*)L"middle2");
297 test_errno(err<0,EEXIST);
299 err=wrename((wchar_t*)L"middle1/bottom1/absfile",(wchar_t*)L"middle2/absfile");
302 err=wrename((wchar_t*)L"middle2/absfile",(wchar_t*)L"middle1/bottom1/absfile");
305 err=wrename((wchar_t*)L"middle1/bottom1/absfile",(wchar_t*)L"middle2/nonsuch/newname");
306 test_errno(err<0,ENOENT);
308 err=wrename((wchar_t*)L"middle1",(wchar_t*)L"middle1/bottom1/subdirectory_of_self");
309 test_errno(err<0,EACCES);
311 err=wrename((wchar_t*)L"middle1",(wchar_t*)L"newname");
314 err=wrename((wchar_t*)L"newname/bottom2/file",(wchar_t*)L"middle2/file");
317 err=wrename((wchar_t*)L"newname",(wchar_t*)L"middle1");
320 err=wrename((wchar_t*)L"middle2/file",(wchar_t*)L"middle1/bottom2/file");
323 err=wrename((wchar_t*)L"no such file",(wchar_t*)L"valid new name");
324 test_errno(err<0,ENOENT);
326 err=wrename((wchar_t*)L"no such file",(wchar_t*)L"topfile");
327 test_errno(err<0,ENOENT);
329 err=wrename((wchar_t*)L".",(wchar_t*)L"../different top");
330 /* test_errno(err<0,EACCES); -- can't change "." */
331 test(err==0); /* STDLIB resolves "." to full path, so this works */
333 err=wrename((wchar_t*)L"../different top",rootpath);
338 Directory tree is now
342 middle1 / bottom1 / absfile
343 / bottom2 / file -- read-only
344 / read-only / sub-read-only /
347 @SYMTestCaseID SYSLIB-STDLIB-CT-1085
348 @SYMTestCaseDesc Tests for enumeration on directories
349 @SYMTestPriority High
350 @SYMTestActions Tests for using WDIR inplace of DIR
351 @SYMTestExpectedResults Test must not fail
356 int err, count, i, j, fd;
359 wchar_t name[MAXPATHLEN+1];
362 test_Next("Enumerating Directories");
364 err=wchdir(rootpath);
367 dp=wopendir((wchar_t*)L"topfile");
368 /* test_errno(dp==0,ENOTDIR); -- not convinced about this anyway */
369 test_errno(dp==0,ENOENT);
371 dp=wopendir((wchar_t*)L"no such file");
372 test_errno(dp==0,ENOENT);
374 //test something sensible happens if someone uses a WDIR inplace of a DIR
376 DIR *p = opendir(".");
379 // Test wants a DIR passed but won't compile under CW.
380 // Force the compile by casting. The function will still get a DIR.
381 // DIR inherits from WDIR.
382 ep=wreaddir((WDIR*)p); //expect a warning from this line. p *IS* the wrong type
386 dp=wopendir((wchar_t*)L".");
393 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
407 wcscpy(name,ep->d_name);
414 test(wcscmp(name,ep->d_name)==0);
427 wcscpy(name,ep->d_name);
432 test(wcscmp(name,ep->d_name)==0);
438 dp=wopendir((wchar_t*)L"middle2\\");
445 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
449 test(count==0); /* empty directory */
453 fd = wopen((wchar_t*)L"middle2/extrafile",O_RDWR+O_CREAT,0777);
463 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
467 test(count==0); /* shouldn't have noticed the change */
469 wrewinddir(dp); /* and spot the new file */
474 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
482 dp=wopendir((wchar_t*)L"/");
489 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
499 Directory tree is now
503 middle1 / bottom1 / absfile
504 / bottom2 / file -- read-only
505 / read-only / sub-read-only /
508 @SYMTestCaseID SYSLIB-STDLIB-CT-1086
509 @SYMTestCaseDesc Tests for file attributes
510 @SYMTestPriority High
511 @SYMTestActions Tests the attributes on files and directories
512 @SYMTestExpectedResults Test must not fail
522 test_Next("File Attributes");
524 err=wchdir(rootpath);
527 err=wstat((wchar_t*)L"middle earth/bag end/hobbit",&s1);
528 test_errno(err<0,ENOENT);
530 err=wstat((wchar_t*)L"middle1/bottom2/file",&s1);
532 test(S_ISREG(s1.st_mode)!=0);
533 test(S_ISDIR(s1.st_mode)==0);
534 test((s1.st_mode&S_IWUSR)==0);
537 err=wstat((wchar_t*)L"topfile",&s1);
539 test(S_ISREG(s1.st_mode)!=0);
540 test(S_ISDIR(s1.st_mode)==0);
541 test((s1.st_mode&S_IWUSR)!=0);
544 err=wstat((wchar_t*)L"topfile",&s2);
546 test(s1.st_mode==s2.st_mode);
547 test(s1.st_size==s2.st_size);
548 diff=difftime(s1.st_mtime,s2.st_mtime);
549 test(diff==(double)0.0);
551 fd=wopen((wchar_t*)L"topfile", O_RDONLY, 0);
556 test(s1.st_mode==s2.st_mode);
557 test(s1.st_size==s2.st_size);
558 diff=difftime(s1.st_mtime,s2.st_mtime);
559 test(diff==(double)0.0);
561 err=wstat((wchar_t*)L"topfile",&s2);
563 test(s1.st_mode==s2.st_mode);
564 test(s1.st_size==s2.st_size);
565 diff=difftime(s1.st_mtime,s2.st_mtime);
566 test(diff==(double)0.0);
571 sleep(1); /* to ensure that the modify time changes */
573 fd=wopen((wchar_t*)L"topfile", O_RDWR+O_APPEND, 0);
576 err=wstat((wchar_t*)L"topfile",&s2);
578 test(s1.st_mode==s2.st_mode);
579 test(s1.st_size==s2.st_size);
580 /* probably not guaranteeed to have changed the modtime at this point */
584 char temp[MAXPATHLEN+1];
585 wcstombs(temp,rootpath,MAXPATHLEN);
586 err=write(fd,temp,3);
596 // this needs a delay here with EKA2
597 // this is a slightly dodgy way of getting one
598 err=wstat((wchar_t*)L"topfile",&s1);
599 err=wstat((wchar_t*)L"topfile",&s2);
600 err=wstat((wchar_t*)L"topfile",&s1);
602 err=wstat((wchar_t*)L"topfile",&s2);
604 test(s1.st_mode==s2.st_mode);
606 diff=difftime(s2.st_mtime,s1.st_mtime);
607 test(diff>=(double)0.0);
609 test_Next("Directory Attributes");
611 err=wstat((wchar_t*)L"middle1",&s1);
613 test(S_ISREG(s1.st_mode)==0);
614 test(S_ISDIR(s1.st_mode)==1);
615 test((s1.st_mode&S_IWUSR)!=0);
617 err=wstat((wchar_t*)L"middle1/read-only",&s1);
619 test(S_ISREG(s1.st_mode)==0);
620 test(S_ISDIR(s1.st_mode)==1);
621 test((s1.st_mode&S_IWUSR)==0);
623 err=wstat((wchar_t*)L"/",&s1);
625 test(S_ISREG(s1.st_mode)==0);
626 test(S_ISDIR(s1.st_mode)==1);
628 err=waccess((wchar_t*)L"middle1/bottom1/absfile",W_OK);
631 err=waccess((wchar_t*)L"middle1/bottom1/absfile",R_OK);
634 err=waccess((wchar_t*)L"middle1/bottom2/file",W_OK);
637 err=waccess((wchar_t*)L"middle1/bottom2/file",R_OK);
640 err=waccess((wchar_t*)L"middle1/read-only",W_OK);
643 err=waccess((wchar_t*)L"middle1/read-only",R_OK);
646 err=waccess((wchar_t*)L"middle1/no such directory",R_OK);
651 Directory tree is now
655 middle1 / bottom1 / absfile
656 / bottom2 / file -- read-only
657 / read-only / sub-read-only /
660 @SYMTestCaseID SYSLIB-STDLIB-CT-1087
661 @SYMTestCaseDesc Tests for searching on different drives
662 @SYMTestPriority High
663 @SYMTestActions Tests by searching on z drive,test for the error codes
664 @SYMTestExpectedResults Test must not fail
670 wchar_t wname[MAXPATHLEN+1];
671 char name[MAXPATHLEN+1];
672 char narrowroot[MAXPATHLEN+1];
674 test_Next("Searching across drives");
676 //ho hum, no wsprintf yet
677 wcstombs(narrowroot, rootpath, MAXPATHLEN);
678 sprintf(name,"%s/middle2/extrafile",narrowroot);
679 test(-1!=mbstowcs(wname, name, MAXPATHLEN));
681 err=wchdir((wchar_t*)L"z:/");
684 fd=wopen(wname+2, O_RDONLY, 0);
685 test_errno(fd<0,ENOENT); // doesn't exist on z:
688 fd=wopen(wname, O_RDWR, 0);
689 test(fd>=0); // found it on the original drive
696 Directory tree is now
700 middle1 / bottom1 / absfile
701 / bottom2 / file -- read-only
702 / read-only / sub-read-only /
706 @SYMTestCaseID SYSLIB-STDLIB-CT-1088
707 @SYMTestCaseDesc Tests for deleting files
708 @SYMTestPriority High
709 @SYMTestActions Tests by deleting files and directories.Tests for error codes
710 @SYMTestExpectedResults Test must not fail
716 wchar_t widename[] = WIDENAME;
718 test_Next("Deleting - files");
720 err=wchdir(rootpath);
723 err=wunlink((wchar_t*)L"middle1/bottom2/file");
724 test_errno(err<0,EACCES); /* file is read-only */
726 err=wchmod((wchar_t*)L"middle1/bottom2/file",0777);
729 err=wunlink((wchar_t*)L"middle1/bottom2/file");
732 err=wunlink((wchar_t*)L"middle2/extrafile");
735 err=wunlink((wchar_t*)L"middle1/read-only/file");
736 /* test_errno(err<0,EPERM); parent directory is read-only */
737 test(err==0); /* Omission - EPOC32 uses Win32 semantics for read-only directories */
739 test_Next("Deleting - directories");
741 err=wchdir(rootpath);
744 err=wrmdir(widename);
747 err=wrmdir((wchar_t*)L"middle1");
748 test_errno(err<0,EEXIST); /* not empty */
750 err=wrmdir((wchar_t*)L"middle1/bottom1");
751 test_errno(err<0,EEXIST); /* not empty */
753 err=wunlink((wchar_t*)L"middle1/bottom1/absfile");
756 err=wrmdir((wchar_t*)L"middle1/bottom1");
759 err=wrmdir((wchar_t*)L"middle1/bottom1");
760 test_errno(err<0,ENOENT); /* already deleted */
762 err=wrmdir((wchar_t*)L"middle1");
763 test_errno(err<0,EEXIST);
765 err=wrmdir((wchar_t*)L"middle1/bottom2");
768 test_Next("Deleting - read-only directories");
770 err=wrmdir((wchar_t*)L"middle1/read-only/sub-read-only");
771 /* test_errno(err!=0,EACCES); -- permission denied - read-only parent */
772 test_errno(err<0,EACCES); /* Omission - EPOC32 uses Win32 semantics */
774 err=wchmod((wchar_t*)L"middle1/read-only",0777);
777 err=wrmdir((wchar_t*)L"middle1/read-only/sub-read-only");
779 /* EPOC32 doesn't use the writeability of the parent directory, but instead looks
780 * at the attributes of the directory itself.
782 test_errno(err!=0,EACCES);
784 err=wchmod((wchar_t*)L"middle1/read-only/sub-read-only",0777);
787 err=wrmdir((wchar_t*)L"middle1/read-only/sub-read-only");
790 err=wrmdir((wchar_t*)L"middle1/read-only");
793 err=wrmdir((wchar_t*)L"middle?");
794 test_errno(err<0,EINVAL); /* no wild cards please */
796 err=wrmdir((wchar_t*)L"middle1");
799 err=wrmdir((wchar_t*)L"../top/middle2");
802 err=wrmdir((wchar_t*)L".");
803 test_errno(err<0,EEXIST); /* not empty */
805 err=wunlink((wchar_t*)L"topfile");
808 err=wrmdir((wchar_t*)L".");
813 @SYMTestCaseID SYSLIB-STDLIB-CT-1089
814 @SYMTestCaseDesc Tests for creation of temporary directory and files in it.
815 @SYMTestPriority High
816 @SYMTestActions Tests by creating a temporary director,and files and writing to the files.
817 Check for error codes.
818 @SYMTestExpectedResults Test must not fail
821 void temporary_files()
823 int err, count1, count2;
827 wchar_t name[L_tmpnam];
828 wchar_t name2[L_tmpnam];
829 char nname[L_tmpnam];
830 char nname2[L_tmpnam];
833 test_Next("Temporary files");
835 #define W_tmpdir L"C:/system/temp/"
836 #define W_tmpfirstdir L"C:/system/"
838 dp=wopendir((wchar_t*)W_tmpdir);
841 printf(" Creating the directory %S ...\n", W_tmpdir);
842 err=wmkdir((wchar_t*)W_tmpfirstdir, 0777);
843 err=wmkdir((wchar_t*)W_tmpdir, 0777);
845 dp=wopendir((wchar_t*)W_tmpdir);
853 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
861 err=fprintf(fp,"hello");
869 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
873 test(count2==count1+1); /* EPOC32 temporary files are visible in file system */
882 if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
886 test(count2==count1); /* should be automatically deleted */
894 test(count1<L_tmpnam);
899 fp=wfopen(name,(wchar_t*)L"wb+");
905 err=wcscmp(name,name2);
914 err=wcstombs(nname,name,50);
916 err=wcstombs(nname2,name2,50);
918 printf(" wtmpnam suggested %s and %s\n", nname, nname2);
924 int err=chdir("C:\\");
950 test_Title("Directory Handling");
954 test_Next("Do it again using the CPosixServer (for them, not me)");
957 start_posix_server(); /* calls SpawnPosixServer from C++ code */
959 client=create_thread(allTests, "TDIRS tests");
961 start_thread(client);
962 err=wait_for_thread(client);