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 char rootpath[MAXPATHLEN];
36 /* construct a directory tree in various ways */
37 #define WIDENAME {L'/', L'T', L'o', L'p', L'/', L'e', L'u', L'r', L'o', 0x20ac, 0}
40 @SYMTestCaseID SYSLIB-STDLIB-CT-1047
41 @SYMTestCaseDesc Tests for operations on directory
43 @SYMTestActions Tests by creating directory with invalid name,existing directory name,wide characters
44 Tests for the error code returned while creating directories.
45 @SYMTestExpectedResults Test must not fail
51 char namebuf[MAXPATHLEN], namebuf2[MAXPATHLEN];
52 char toobig[MAXPATHLEN+MAXPATHLEN+1];
57 wchar_t widenamebuf[MAXPATHLEN+1];
58 wchar_t widename[] = WIDENAME;
62 test_Next("Create Directory Tree - relative paths");
64 err=mkdir("***", 0777);
65 test_errno(err==-1,EINVAL); /* bad directory name */
68 err=mkdir("top", 0777);
71 err=mkdir("top", 0777);
72 test_errno(err==-1,EEXIST); /* directory already exists */
75 //make a dir with a wide character in the name
76 err = wcstombs(namebuf, widename, MAXPATHLEN);
79 err=mkdir(namebuf, 0777);
83 err=mkdir("top/middle1/bottom1", 0777);
84 test_errno(err==-1,ENOENT); /* missing middle bit of path */
86 err=mkdir("top/middle1", 0777);
89 err=chdir("./top//\\/.\\.\\../top/.");
92 p = getcwd(rootpath,sizeof(rootpath)); /* save name of toplevel directory */
99 test_errno(err==-1,ENOENT); /* directory doesn't exist yet */
101 p = getcwd(namebuf,sizeof(namebuf)); /* prepare name for tests later */
104 err=mkdir("bottom1",0777);
107 err=mkdir("read-only",0444);
110 err=mkdir("read-only/sub-read-only",0444);
111 /* test_errno(err==-1,EACCES); */
112 test(err==0); /* Omission - EPOC32 has Win32 semantics for read-only directories */
114 err=chdir("../../top/middle1/bottom1");
117 test_Next("Create Directory Tree - absolute paths");
119 p = strcat(namebuf,"/bottom2");
120 test(p==namebuf); /* .../top/middle1/bottom2 */
123 test_errno(err==-1,ENOENT); /* directory doesn't exist yet */
125 err=mkdir(namebuf, 0777);
131 p = getcwd(namebuf,sizeof(namebuf));
134 err=mkdir("../../middle2", 0777);
137 p = getcwd(namebuf2,sizeof(namebuf2));
139 test(strcmp(namebuf,namebuf2)==0); /* mkdir shouldn't change cwd */
141 memset(toobig,'a', sizeof(toobig));
142 toobig[sizeof(toobig)-1]='\0';
144 err=mkdir(toobig,0777);
145 test_errno(err<0,ENAMETOOLONG);
148 test_Next("Test getcwd");
151 p = getcwd(namebuf, 4);
152 test_errno(0==p, ERANGE);
154 //make it alloc a buffer
155 p = getcwd(NULL, 300);
159 //alloc a buffer then fail with a too small size
160 p = getcwd(NULL, 10);
161 test_errno(0==p, ERANGE);
163 wp = wgetcwd(widenamebuf, MAXPATHLEN-1);
166 p = getcwd(namebuf2, MAXPATHLEN-1);
169 wcstombs(namebuf, widenamebuf, MAXPATHLEN-1);
170 test(strcmp(namebuf, namebuf2) == 0);
174 strcpy(namebuf,"bobby.dog");
175 test( (0==strcmp("C:\\top\\", realpath("/top/../top/../top/./",namebuf))) ||
176 (0==strcmp("D:\\top\\", realpath("/top/../top/../top/./",namebuf))));
183 Directory tree is now
188 / read-only / sub-read-only
191 @SYMTestCaseID SYSLIB-STDLIB-CT-1048
192 @SYMTestCaseDesc Tests for operations on creating files
193 @SYMTestPriority High
194 @SYMTestActions Tests by opening files which does not exists,check for closing a file twice
195 Tests for the error code returned while creating files
196 @SYMTestExpectedResults Test must not fail
203 char namebuf[MAXPATHLEN],*p;
205 test_Next("Creating Files - relative paths");
210 fd = open("topfile",O_RDWR+O_APPEND,0777);
211 test_errno(fd<0,ENOENT); /* doesn't exist */
213 fd = open("topfile",O_RDWR+O_CREAT,0777);
220 test_errno(err<0,EBADF); /* can't close it twice */
222 fd = open("topfile",O_RDWR+O_APPEND,0777);
228 fd = open("topfile",O_RDWR+O_CREAT+O_EXCL,0777);
229 test_errno(fd<0,EEXIST); /* already exists */
231 fd = open("middle1/bottom2/file",O_RDONLY+O_CREAT,0444);
237 fd = open("middle1/bottom2/file",O_RDWR+O_APPEND,0777);
238 /* test_errno(fd<0,EACCES); */
239 test(fd>=0); /* Omission - the original O_CREAT ignores the 0444 permissions */
246 err=chmod("middle1/bottom2/file",0444);
249 fd = open("middle1/bottom2/file",O_RDWR+O_APPEND,0777);
250 test_errno(fd<0,EACCES); /* not writeable */
252 fd = open("middle2",O_RDWR+O_CREAT,0777);
253 /* test_errno(fd<0,EISDIR); */
254 test_errno(fd<0,EACCES); /* Omission - we don't do EISDIR */
256 test_Next("Creating Files - absolute paths");
258 err=chdir("middle1/bottom1");
261 p = getcwd(namebuf,sizeof(namebuf)); /* prepare name for tests later */
264 p = strcat(namebuf,"absfile");
267 fd = open(namebuf,O_RDWR+O_CREAT,0777);
273 fd = open("../read-only/file",O_RDWR+O_CREAT,0444);
274 /* test_errno(fd<0,EACCES); */
275 test(fd>=0); /* Omission - EPOC32 has Win32 semantics for read-only directories */
285 Directory tree is now
289 middle1 / bottom1 / absfile
290 / bottom2 / file -- read-only
291 / read-only / sub-read-only /
295 @SYMTestCaseID SYSLIB-STDLIB-CT-1049
296 @SYMTestCaseDesc Tests for renaming operations
297 @SYMTestPriority High
298 @SYMTestActions Tests by renaming files.Tests for the error code returned while renaming files
299 @SYMTestExpectedResults Test must not fail
306 test_Next("Renaming");
311 err=rename("middle1","middle2");
312 test_errno(err<0,EEXIST);
314 err=rename("middle1/bottom1/absfile","middle2/absfile");
317 err=rename("middle2/absfile","middle1/bottom1/absfile");
320 err=rename("middle1/bottom1/absfile","middle2/nonsuch/newname");
321 test_errno(err<0,ENOENT);
323 err=rename("middle1","middle1/bottom1/subdirectory_of_self");
324 test_errno(err<0,EACCES);
326 err=rename("middle1","newname");
329 err=rename("newname/bottom2/file","middle2/file");
332 err=rename("newname","middle1");
335 err=rename("middle2/file","middle1/bottom2/file");
338 err=rename("no such file","valid new name");
339 test_errno(err<0,ENOENT);
341 err=rename("no such file","topfile");
342 test_errno(err<0,ENOENT);
344 err=rename(".","../different top");
345 /* test_errno(err<0,EACCES); -- can't change "." */
346 test(err==0); /* STDLIB resolves "." to full path, so this works */
348 err=rename("../different top",rootpath);
353 Directory tree is now
357 middle1 / bottom1 / absfile
358 / bottom2 / file -- read-only
359 / read-only / sub-read-only /
363 @SYMTestCaseID SYSLIB-STDLIB-CT-1050
364 @SYMTestCaseDesc Tests for enumeration on directories
365 @SYMTestPriority High
366 @SYMTestActions Tests by opening directories
367 @SYMTestExpectedResults Test must not fail
372 int err, count, i, j, fd;
375 char name[MAXPATHLEN+1];
378 test_Next("Enumerating Directories");
383 dp=opendir("topfile");
384 /* test_errno(dp==0,ENOTDIR); -- not convinced about this anyway */
385 test_errno(dp==0,ENOENT);
387 dp=opendir("no such file");
388 test_errno(dp==0,ENOENT);
391 //test something sensible happens if someone uses a WDIR inplace of a DIR
393 WDIR *wp = wopendir((wchar_t*)L".");
396 // Test wants a WDIR passed but won't compile under CW.
397 // Force the compile by casting. The function will still get a WDIR.
398 // DIR inherits from WDIR.
399 ep=readdir((DIR*)wp);
400 test_errno(ep==0,EINVAL);
415 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
429 strcpy(name,ep->d_name);
436 test(strcmp(name,ep->d_name)==0);
449 strcpy(name,ep->d_name);
454 test(strcmp(name,ep->d_name)==0);
460 dp=opendir("middle2");
467 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
471 test(count==0); /* empty directory */
475 fd = open("middle2/extrafile",O_RDWR+O_CREAT,0777);
485 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
489 test(count==0); /* shouldn't have noticed the change */
491 rewinddir(dp); /* and spot the new file */
496 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
511 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
521 Directory tree is now
525 middle1 / bottom1 / absfile
526 / bottom2 / file -- read-only
527 / read-only / sub-read-only /
530 @SYMTestCaseID SYSLIB-STDLIB-CT-1051
531 @SYMTestCaseDesc Tests for file attributes
532 @SYMTestPriority High
533 @SYMTestActions Tests the attributes on files and directories
534 @SYMTestExpectedResults Test must not fail
544 test_Next("File Attributes");
549 err=stat("middle earth/bag end/hobbit",&s1);
550 test_errno(err<0,ENOENT);
552 err=stat("middle1/bottom2/file",&s1);
554 test(S_ISREG(s1.st_mode)!=0);
555 test(S_ISDIR(s1.st_mode)==0);
556 test((s1.st_mode&S_IWUSR)==0);
559 err=stat("topfile",&s1);
561 test(S_ISREG(s1.st_mode)!=0);
562 test(S_ISDIR(s1.st_mode)==0);
563 test((s1.st_mode&S_IWUSR)!=0);
566 err=stat("topfile",&s2);
568 test(s1.st_mode==s2.st_mode);
569 test(s1.st_size==s2.st_size);
570 diff=difftime(s1.st_mtime,s2.st_mtime);
571 test(diff==(double)0.0);
573 fd=open("topfile", O_RDONLY, 0);
578 test(s1.st_mode==s2.st_mode);
579 test(s1.st_size==s2.st_size);
580 diff=difftime(s1.st_mtime,s2.st_mtime);
581 test(diff==(double)0.0);
583 err=stat("topfile",&s2);
585 test(s1.st_mode==s2.st_mode);
586 test(s1.st_size==s2.st_size);
587 diff=difftime(s1.st_mtime,s2.st_mtime);
588 test(diff==(double)0.0);
593 sleep(1); /* to ensure that the modify time changes */
595 fd=open("topfile", O_RDWR+O_APPEND, 0);
598 err=stat("topfile",&s2);
600 test(s1.st_mode==s2.st_mode);
601 test(s1.st_size==s2.st_size);
602 /* probably not guaranteeed to have changed the modtime at this point */
604 err=write(fd,rootpath,3);
613 err=stat("topfile",&s2);
615 test(s1.st_mode==s2.st_mode);
617 diff=difftime(s2.st_mtime,s1.st_mtime);
618 test(diff>(double)0.0);
620 test_Next("Directory Attributes");
622 err=stat("middle1",&s1);
624 test(S_ISREG(s1.st_mode)==0);
625 test(S_ISDIR(s1.st_mode)==1);
626 test((s1.st_mode&S_IWUSR)!=0);
628 err=stat("middle1/read-only",&s1);
630 test(S_ISREG(s1.st_mode)==0);
631 test(S_ISDIR(s1.st_mode)==1);
632 test((s1.st_mode&S_IWUSR)==0);
636 test(S_ISREG(s1.st_mode)==0);
637 test(S_ISDIR(s1.st_mode)==1);
639 err=access("middle1/bottom1/absfile",W_OK);
642 err=access("middle1/bottom1/absfile",R_OK);
645 err=access("middle1/bottom2/file",W_OK);
648 err=access("middle1/bottom2/file",R_OK);
651 err=access("middle1/read-only",W_OK);
654 err=access("middle1/read-only",R_OK);
657 err=access("middle1/no such directory",R_OK);
662 Directory tree is now
666 middle1 / bottom1 / absfile
667 / bottom2 / file -- read-only
668 / read-only / sub-read-only /
672 @SYMTestCaseID SYSLIB-STDLIB-CT-1052
673 @SYMTestCaseDesc Tests for searching on different drives
674 @SYMTestPriority High
675 @SYMTestActions Tests by searching on z drive,test for the error codes
676 @SYMTestExpectedResults Test must not fail
682 char name[MAXPATHLEN+1];
684 test_Next("Searching across drives");
686 sprintf(name,"%s/middle2/extrafile",rootpath);
691 fd=open(name+2, O_RDONLY, 0);
692 test_errno(fd<0,ENOENT); // doesn't exist on z:
695 fd=open(name, O_RDWR, 0);
696 test(fd>=0); // found it on the original drive
703 Directory tree is now
707 middle1 / bottom1 / absfile
708 / bottom2 / file -- read-only
709 / read-only / sub-read-only /
713 @SYMTestCaseID SYSLIB-STDLIB-CT-1053
714 @SYMTestCaseDesc Tests for deleting files
715 @SYMTestPriority High
716 @SYMTestActions Tests by deleting files and directories.Test for error codes
717 @SYMTestExpectedResults Test must not fail
723 char namebuf[MAXPATHLEN];
724 wchar_t widename[] = WIDENAME;
727 test_Next("Deleting - files");
734 err=unlink("middle1/bottom2/file");
735 test_errno(err<0,EACCES); /* file is read-only */
737 err=chmod("middle1/bottom2/file",0777);
740 err=unlink("middle1/bottom2/file");
743 err=unlink("middle2/extrafile");
746 err=unlink("middle1/read-only/file");
747 /* test_errno(err<0,EPERM); parent directory is read-only */
748 test(err==0); /* Omission - EPOC32 uses Win32 semantics for read-only directories */
750 test_Next("Deleting - directories");
755 //delete the dir with a wide character in the name
756 err = wcstombs(namebuf, widename, MAXPATHLEN);
762 err=rmdir("middle1");
763 test_errno(err<0,EEXIST); /* not empty */
765 err=rmdir("middle1/bottom1");
766 test_errno(err<0,EEXIST); /* not empty */
768 err=unlink("middle1/bottom1/absfile");
771 err=rmdir("middle1/bottom1");
774 err=rmdir("middle1/bottom1");
775 test_errno(err<0,ENOENT); /* already deleted */
777 err=rmdir("middle1");
778 test_errno(err<0,EEXIST);
780 err=rmdir("middle1/bottom2");
783 test_Next("Deleting - read-only directories");
785 err=rmdir("middle1/read-only/sub-read-only");
786 /* test_errno(err!=0,EACCES); -- permission denied - read-only parent */
787 test_errno(err<0,EACCES); /* Omission - EPOC32 uses Win32 semantics */
789 err=chmod("middle1/read-only",0777);
792 err=rmdir("middle1/read-only/sub-read-only");
794 /* EPOC32 doesn't use the writeability of the parent directory, but instead looks
795 * at the attributes of the directory itself.
797 test_errno(err!=0,EACCES);
799 err=chmod("middle1/read-only/sub-read-only",0777);
802 err=rmdir("middle1/read-only/sub-read-only");
805 err=rmdir("middle1/read-only");
808 err=rmdir("middle?");
809 test_errno(err<0,EINVAL); /* no wild cards please */
811 err=rmdir("middle1");
814 err=rmdir("../top/middle2");
818 test_errno(err<0,EEXIST); /* not empty */
820 err=unlink("topfile");
828 @SYMTestCaseID SYSLIB-STDLIB-CT-1054
829 @SYMTestCaseDesc Tests for creation of temporary directory and files in it.
830 @SYMTestPriority High
831 @SYMTestActions Tests by creating a temporary directory,files and writing to the files.
832 Check for error codes.
833 @SYMTestExpectedResults Test must not fail
836 void temporary_files()
838 #define tmpdir "c:/system/temp"
840 int err, count1, count2;
845 char name2[L_tmpnam];
848 test_Next("Temporary files");
853 printf(" Creating the directory %s ...\n", tmpdir);
854 err=mkdir("c:/system", 0777);
856 err=mkdir(tmpdir, 0777);
866 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
874 err=fprintf(fp,"hello");
882 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
886 test(count2==count1+1); /* EPOC32 temporary files are visible in file system */
895 if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
899 test(count2==count1); /* should be automatically deleted */
907 test(count1<L_tmpnam);
912 fp=fopen(name,"wb+");
918 err=strcmp(name,name2);
927 printf(" Tmpnam suggested %s and %s\n", name, name2);
933 int err=chdir("C:\\");
961 test_Title("Directory Handling");
965 test_Next("Do it again using the CPosixServer (for them, not me)");
968 start_posix_server(); /* calls SpawnPosixServer from C++ code */
970 client=create_thread(allTests, "TDIRS tests");
972 start_thread(client);
973 err=wait_for_thread(client);