os/ossrv/genericopenlibs/cstdlib/TSTLIB/TDIRS.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
* Test code for directory and file handling
sl@0
    16
* 
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
sl@0
    22
#include <stdlib.h>
sl@0
    23
#include <stdio.h>
sl@0
    24
#include <string.h>
sl@0
    25
#include <unistd.h>
sl@0
    26
#include <sys/stat.h>
sl@0
    27
#include <sys/fcntl.h>
sl@0
    28
#include <dirent.h>
sl@0
    29
#include <errno.h>
sl@0
    30
sl@0
    31
#include "CTEST.H"	/* includes C interface to EPOC32 threads, and SpawnPosixServer */
sl@0
    32
sl@0
    33
test_Data;
sl@0
    34
char rootpath[MAXPATHLEN];
sl@0
    35
sl@0
    36
/* construct a directory tree in various ways */
sl@0
    37
#define WIDENAME {L'/', L'T', L'o', L'p', L'/', L'e', L'u', L'r', L'o', 0x20ac, 0}
sl@0
    38
sl@0
    39
/**
sl@0
    40
@SYMTestCaseID          SYSLIB-STDLIB-CT-1047
sl@0
    41
@SYMTestCaseDesc	    Tests for operations on directory 
sl@0
    42
@SYMTestPriority 	    High
sl@0
    43
@SYMTestActions  	    Tests by creating directory with invalid name,existing directory name,wide characters
sl@0
    44
                        Tests for the error code returned while creating directories.
sl@0
    45
@SYMTestExpectedResults Test must not fail
sl@0
    46
@SYMREQ                 REQ0000
sl@0
    47
*/		
sl@0
    48
void make_tree()
sl@0
    49
	{
sl@0
    50
	int err;
sl@0
    51
	char namebuf[MAXPATHLEN], namebuf2[MAXPATHLEN];
sl@0
    52
	char toobig[MAXPATHLEN+MAXPATHLEN+1];
sl@0
    53
	char *p;
sl@0
    54
	wchar_t *wp;
sl@0
    55
sl@0
    56
	
sl@0
    57
	wchar_t widenamebuf[MAXPATHLEN+1];
sl@0
    58
	wchar_t widename[] = WIDENAME;
sl@0
    59
sl@0
    60
sl@0
    61
sl@0
    62
	test_Next("Create Directory Tree - relative paths");
sl@0
    63
sl@0
    64
	err=mkdir("***", 0777);
sl@0
    65
	test_errno(err==-1,EINVAL);	/* bad directory name */
sl@0
    66
sl@0
    67
sl@0
    68
	err=mkdir("top", 0777);
sl@0
    69
	test(err==0);
sl@0
    70
sl@0
    71
	err=mkdir("top", 0777);
sl@0
    72
	test_errno(err==-1,EEXIST);	/* directory already exists */
sl@0
    73
sl@0
    74
	
sl@0
    75
	//make a dir with a wide character in the name
sl@0
    76
	err = wcstombs(namebuf, widename, MAXPATHLEN);
sl@0
    77
	test(err!=-1);
sl@0
    78
	
sl@0
    79
	err=mkdir(namebuf, 0777);
sl@0
    80
	test(err==0);
sl@0
    81
	
sl@0
    82
sl@0
    83
	err=mkdir("top/middle1/bottom1", 0777);
sl@0
    84
	test_errno(err==-1,ENOENT);	/* missing middle bit of path */
sl@0
    85
sl@0
    86
	err=mkdir("top/middle1", 0777);
sl@0
    87
	test(err==0);
sl@0
    88
sl@0
    89
	err=chdir("./top//\\/.\\.\\../top/.");
sl@0
    90
	test(err==0);
sl@0
    91
sl@0
    92
	p = getcwd(rootpath,sizeof(rootpath));	/* save name of toplevel directory */
sl@0
    93
	test(p==rootpath);
sl@0
    94
sl@0
    95
	err=chdir("middle1");
sl@0
    96
	test(err==0);
sl@0
    97
sl@0
    98
	err=chdir("bottom2");
sl@0
    99
	test_errno(err==-1,ENOENT);	/* directory doesn't exist yet */
sl@0
   100
sl@0
   101
	p = getcwd(namebuf,sizeof(namebuf));	/* prepare name for tests later */
sl@0
   102
	test(p==namebuf);
sl@0
   103
sl@0
   104
	err=mkdir("bottom1",0777);
sl@0
   105
	test(err==0);
sl@0
   106
sl@0
   107
	err=mkdir("read-only",0444);
sl@0
   108
	test(err==0);
sl@0
   109
sl@0
   110
	err=mkdir("read-only/sub-read-only",0444);
sl@0
   111
	/* test_errno(err==-1,EACCES); */
sl@0
   112
	test(err==0);	/* Omission - EPOC32 has Win32 semantics for read-only directories */
sl@0
   113
sl@0
   114
	err=chdir("../../top/middle1/bottom1");
sl@0
   115
	test(err==0);
sl@0
   116
sl@0
   117
	test_Next("Create Directory Tree - absolute paths");
sl@0
   118
sl@0
   119
	p = strcat(namebuf,"/bottom2");
sl@0
   120
	test(p==namebuf);	/* .../top/middle1/bottom2 */
sl@0
   121
sl@0
   122
	err=chdir(namebuf);
sl@0
   123
	test_errno(err==-1,ENOENT);	/* directory doesn't exist yet */
sl@0
   124
	
sl@0
   125
	err=mkdir(namebuf, 0777);
sl@0
   126
	test(err==0);
sl@0
   127
sl@0
   128
	err=chdir(namebuf);
sl@0
   129
	test(err==0);
sl@0
   130
sl@0
   131
	p = getcwd(namebuf,sizeof(namebuf));
sl@0
   132
	test(p==namebuf);
sl@0
   133
sl@0
   134
	err=mkdir("../../middle2", 0777);
sl@0
   135
	test(err==0);
sl@0
   136
sl@0
   137
	p = getcwd(namebuf2,sizeof(namebuf2));
sl@0
   138
	test(p==namebuf2);
sl@0
   139
	test(strcmp(namebuf,namebuf2)==0);	/* mkdir shouldn't change cwd */
sl@0
   140
sl@0
   141
	memset(toobig,'a', sizeof(toobig));
sl@0
   142
	toobig[sizeof(toobig)-1]='\0';
sl@0
   143
sl@0
   144
	err=mkdir(toobig,0777);
sl@0
   145
	test_errno(err<0,ENAMETOOLONG);
sl@0
   146
sl@0
   147
sl@0
   148
	test_Next("Test getcwd");
sl@0
   149
	
sl@0
   150
	//size too small
sl@0
   151
	p = getcwd(namebuf, 4);
sl@0
   152
	test_errno(0==p, ERANGE);
sl@0
   153
sl@0
   154
	//make it alloc a buffer
sl@0
   155
	p = getcwd(NULL, 300);
sl@0
   156
	test (NULL != p);
sl@0
   157
	free(p);
sl@0
   158
sl@0
   159
	//alloc a buffer then fail with a too small size
sl@0
   160
	p = getcwd(NULL, 10);
sl@0
   161
	test_errno(0==p, ERANGE);
sl@0
   162
sl@0
   163
	wp = wgetcwd(widenamebuf, MAXPATHLEN-1);
sl@0
   164
	test (NULL != wp);
sl@0
   165
sl@0
   166
	p = getcwd(namebuf2, MAXPATHLEN-1);
sl@0
   167
	test (NULL != p);
sl@0
   168
sl@0
   169
	wcstombs(namebuf, widenamebuf, MAXPATHLEN-1);
sl@0
   170
	test(strcmp(namebuf, namebuf2) == 0);
sl@0
   171
	
sl@0
   172
	
sl@0
   173
	//test realpath
sl@0
   174
	strcpy(namebuf,"bobby.dog");
sl@0
   175
	test( (0==strcmp("C:\\top\\", realpath("/top/../top/../top/./",namebuf))) || 
sl@0
   176
		  (0==strcmp("D:\\top\\", realpath("/top/../top/../top/./",namebuf))));
sl@0
   177
sl@0
   178
	
sl@0
   179
sl@0
   180
}
sl@0
   181
sl@0
   182
/**
sl@0
   183
   Directory tree is now
sl@0
   184
 
sl@0
   185
     top / middle2   
sl@0
   186
           middle1 / bottom1
sl@0
   187
                   / bottom2
sl@0
   188
 		   / read-only / sub-read-only
sl@0
   189
 
sl@0
   190
sl@0
   191
@SYMTestCaseID          SYSLIB-STDLIB-CT-1048
sl@0
   192
@SYMTestCaseDesc	    Tests for operations on creating files
sl@0
   193
@SYMTestPriority 	    High
sl@0
   194
@SYMTestActions  	    Tests by opening files which does not exists,check for closing a file twice
sl@0
   195
                        Tests for the error code returned while creating files
sl@0
   196
@SYMTestExpectedResults Test must not fail
sl@0
   197
@SYMREQ                 REQ0000
sl@0
   198
*/		
sl@0
   199
void create_files()
sl@0
   200
	{
sl@0
   201
	int err;
sl@0
   202
	int fd;
sl@0
   203
	char namebuf[MAXPATHLEN],*p;
sl@0
   204
sl@0
   205
	test_Next("Creating Files - relative paths");
sl@0
   206
sl@0
   207
	err=chdir(rootpath);
sl@0
   208
	test(err==0);
sl@0
   209
sl@0
   210
	fd = open("topfile",O_RDWR+O_APPEND,0777);
sl@0
   211
	test_errno(fd<0,ENOENT);	/* doesn't exist */
sl@0
   212
sl@0
   213
	fd = open("topfile",O_RDWR+O_CREAT,0777);
sl@0
   214
	test(fd>=0);
sl@0
   215
sl@0
   216
	err=close(fd);
sl@0
   217
	test(err==0);
sl@0
   218
sl@0
   219
	err=close(fd);
sl@0
   220
	test_errno(err<0,EBADF);	/* can't close it twice */
sl@0
   221
sl@0
   222
	fd = open("topfile",O_RDWR+O_APPEND,0777);
sl@0
   223
	test(fd>=0);
sl@0
   224
sl@0
   225
	err=close(fd);
sl@0
   226
	test(err==0);
sl@0
   227
sl@0
   228
	fd = open("topfile",O_RDWR+O_CREAT+O_EXCL,0777);
sl@0
   229
	test_errno(fd<0,EEXIST);	/* already exists */
sl@0
   230
sl@0
   231
	fd = open("middle1/bottom2/file",O_RDONLY+O_CREAT,0444);
sl@0
   232
	test(fd>=0);
sl@0
   233
sl@0
   234
	err=close(fd);
sl@0
   235
	test(err==0);
sl@0
   236
sl@0
   237
	fd = open("middle1/bottom2/file",O_RDWR+O_APPEND,0777);
sl@0
   238
	/* test_errno(fd<0,EACCES); */
sl@0
   239
	test(fd>=0);	/* Omission - the original O_CREAT ignores the 0444 permissions */
sl@0
   240
	if (fd>=0)
sl@0
   241
		{
sl@0
   242
		err=close(fd);
sl@0
   243
		test(err==0);
sl@0
   244
		}
sl@0
   245
sl@0
   246
	err=chmod("middle1/bottom2/file",0444);
sl@0
   247
	test(err==0);
sl@0
   248
sl@0
   249
	fd = open("middle1/bottom2/file",O_RDWR+O_APPEND,0777);
sl@0
   250
	test_errno(fd<0,EACCES);	/* not writeable */
sl@0
   251
sl@0
   252
	fd = open("middle2",O_RDWR+O_CREAT,0777);
sl@0
   253
	/* test_errno(fd<0,EISDIR); */
sl@0
   254
	test_errno(fd<0,EACCES);	/* Omission - we don't do EISDIR */
sl@0
   255
sl@0
   256
	test_Next("Creating Files - absolute paths");
sl@0
   257
sl@0
   258
	err=chdir("middle1/bottom1");
sl@0
   259
	test(err==0);
sl@0
   260
sl@0
   261
	p = getcwd(namebuf,sizeof(namebuf));	/* prepare name for tests later */
sl@0
   262
	test(p==namebuf);
sl@0
   263
sl@0
   264
	p = strcat(namebuf,"absfile");
sl@0
   265
	test(p==namebuf);
sl@0
   266
sl@0
   267
	fd = open(namebuf,O_RDWR+O_CREAT,0777);
sl@0
   268
	test(fd>=0);
sl@0
   269
sl@0
   270
	err=close(fd);
sl@0
   271
	test(err==0);
sl@0
   272
sl@0
   273
	fd = open("../read-only/file",O_RDWR+O_CREAT,0444);
sl@0
   274
	/* test_errno(fd<0,EACCES); */
sl@0
   275
	test(fd>=0);	/* Omission - EPOC32 has Win32 semantics for read-only directories */
sl@0
   276
	if (fd>=0)
sl@0
   277
		{
sl@0
   278
		err=close(fd);
sl@0
   279
		test(err==0);
sl@0
   280
		}
sl@0
   281
	
sl@0
   282
	}
sl@0
   283
sl@0
   284
/**
sl@0
   285
   Directory tree is now
sl@0
   286
 
sl@0
   287
    top / topfile
sl@0
   288
           middle2 /   
sl@0
   289
           middle1 / bottom1 / absfile
sl@0
   290
                   / bottom2 / file                -- read-only
sl@0
   291
 		   / read-only / sub-read-only /
sl@0
   292
                                 file
sl@0
   293
 
sl@0
   294
sl@0
   295
@SYMTestCaseID          SYSLIB-STDLIB-CT-1049
sl@0
   296
@SYMTestCaseDesc	    Tests for renaming operations 
sl@0
   297
@SYMTestPriority 	    High
sl@0
   298
@SYMTestActions  	    Tests by renaming files.Tests for the error code returned while renaming files
sl@0
   299
@SYMTestExpectedResults Test must not fail
sl@0
   300
@SYMREQ                 REQ0000
sl@0
   301
*/		
sl@0
   302
void renaming()
sl@0
   303
	{
sl@0
   304
	int err;
sl@0
   305
sl@0
   306
	test_Next("Renaming");
sl@0
   307
sl@0
   308
	err=chdir(rootpath);
sl@0
   309
	test(err==0);
sl@0
   310
sl@0
   311
	err=rename("middle1","middle2");
sl@0
   312
	test_errno(err<0,EEXIST);
sl@0
   313
sl@0
   314
	err=rename("middle1/bottom1/absfile","middle2/absfile");
sl@0
   315
	test(err==0);
sl@0
   316
sl@0
   317
	err=rename("middle2/absfile","middle1/bottom1/absfile");
sl@0
   318
	test(err==0);
sl@0
   319
sl@0
   320
	err=rename("middle1/bottom1/absfile","middle2/nonsuch/newname");
sl@0
   321
	test_errno(err<0,ENOENT);
sl@0
   322
sl@0
   323
	err=rename("middle1","middle1/bottom1/subdirectory_of_self");
sl@0
   324
	test_errno(err<0,EACCES);
sl@0
   325
sl@0
   326
	err=rename("middle1","newname");
sl@0
   327
	test(err==0);
sl@0
   328
sl@0
   329
	err=rename("newname/bottom2/file","middle2/file");
sl@0
   330
	test(err==0);
sl@0
   331
sl@0
   332
	err=rename("newname","middle1");
sl@0
   333
	test(err==0);
sl@0
   334
sl@0
   335
	err=rename("middle2/file","middle1/bottom2/file");
sl@0
   336
	test(err==0);
sl@0
   337
sl@0
   338
	err=rename("no such file","valid new name");
sl@0
   339
	test_errno(err<0,ENOENT);
sl@0
   340
sl@0
   341
	err=rename("no such file","topfile");
sl@0
   342
	test_errno(err<0,ENOENT);
sl@0
   343
sl@0
   344
	err=rename(".","../different top");
sl@0
   345
	/* test_errno(err<0,EACCES);	-- can't change "." */
sl@0
   346
	test(err==0);	/* STDLIB resolves "." to full path, so this works */
sl@0
   347
sl@0
   348
	err=rename("../different top",rootpath);
sl@0
   349
	test(err==0);
sl@0
   350
	}
sl@0
   351
sl@0
   352
/**
sl@0
   353
   Directory tree is now
sl@0
   354
 
sl@0
   355
     top / topfile
sl@0
   356
           middle2 /   
sl@0
   357
           middle1 / bottom1 / absfile
sl@0
   358
                   / bottom2 / file                -- read-only
sl@0
   359
 		   / read-only / sub-read-only /
sl@0
   360
                                 file
sl@0
   361
 
sl@0
   362
sl@0
   363
@SYMTestCaseID          SYSLIB-STDLIB-CT-1050
sl@0
   364
@SYMTestCaseDesc	    Tests for enumeration on directories 
sl@0
   365
@SYMTestPriority 	    High
sl@0
   366
@SYMTestActions  	    Tests by opening directories
sl@0
   367
@SYMTestExpectedResults Test must not fail
sl@0
   368
@SYMREQ                 REQ0000
sl@0
   369
*/		
sl@0
   370
void directory()
sl@0
   371
	{
sl@0
   372
	int err, count, i, j, fd;
sl@0
   373
	DIR *dp;
sl@0
   374
	struct dirent *ep;
sl@0
   375
	char name[MAXPATHLEN+1];
sl@0
   376
	off_t pos;
sl@0
   377
sl@0
   378
	test_Next("Enumerating Directories");
sl@0
   379
sl@0
   380
	err=chdir(rootpath);
sl@0
   381
	test(err==0);
sl@0
   382
sl@0
   383
	dp=opendir("topfile");
sl@0
   384
	/* test_errno(dp==0,ENOTDIR); -- not convinced about this anyway */
sl@0
   385
	test_errno(dp==0,ENOENT);
sl@0
   386
sl@0
   387
	dp=opendir("no such file");
sl@0
   388
	test_errno(dp==0,ENOENT);
sl@0
   389
sl@0
   390
sl@0
   391
	//test something sensible happens if someone uses a WDIR inplace of a DIR
sl@0
   392
	{
sl@0
   393
		WDIR *wp = wopendir((wchar_t*)L".");
sl@0
   394
		test(wp!=0);
sl@0
   395
sl@0
   396
		// Test wants a WDIR passed but won't compile under CW.
sl@0
   397
		// Force the compile by casting. The function will still get a WDIR.
sl@0
   398
		// DIR inherits from WDIR.
sl@0
   399
		ep=readdir((DIR*)wp);
sl@0
   400
		test_errno(ep==0,EINVAL);
sl@0
   401
sl@0
   402
		wclosedir(wp);
sl@0
   403
	}
sl@0
   404
sl@0
   405
sl@0
   406
sl@0
   407
sl@0
   408
	dp=opendir(".");
sl@0
   409
	test(dp!=0);
sl@0
   410
sl@0
   411
	count=0;
sl@0
   412
	do
sl@0
   413
		{
sl@0
   414
		ep=readdir(dp);
sl@0
   415
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   416
			count++;
sl@0
   417
		}
sl@0
   418
	while (ep!=0);
sl@0
   419
	test(count==4);
sl@0
   420
sl@0
   421
	for (i=0; i<4; i++)
sl@0
   422
		{
sl@0
   423
		rewinddir(dp);
sl@0
   424
		for (j=0; j<=i; j++)
sl@0
   425
			{
sl@0
   426
			ep=readdir(dp);
sl@0
   427
			test(ep!=0);
sl@0
   428
			}
sl@0
   429
		strcpy(name,ep->d_name);
sl@0
   430
		rewinddir(dp);
sl@0
   431
		for (j=0; j<=i; j++)
sl@0
   432
			{
sl@0
   433
			ep=readdir(dp);
sl@0
   434
			test(ep!=0);
sl@0
   435
			}
sl@0
   436
		test(strcmp(name,ep->d_name)==0);
sl@0
   437
		}
sl@0
   438
sl@0
   439
	for (i=0; i<4; i++)
sl@0
   440
		{
sl@0
   441
		rewinddir(dp);
sl@0
   442
		pos=telldir(dp);
sl@0
   443
		for (j=0; j<=i; j++)
sl@0
   444
			{
sl@0
   445
			pos=telldir(dp);
sl@0
   446
			ep=readdir(dp);
sl@0
   447
			test(ep!=0);
sl@0
   448
			}
sl@0
   449
		strcpy(name,ep->d_name);
sl@0
   450
		rewinddir(dp);
sl@0
   451
		seekdir(dp, pos);
sl@0
   452
		ep=readdir(dp);
sl@0
   453
		test(ep!=0);
sl@0
   454
		test(strcmp(name,ep->d_name)==0);
sl@0
   455
		}
sl@0
   456
sl@0
   457
	err=closedir(dp);
sl@0
   458
	test(err==0);
sl@0
   459
sl@0
   460
	dp=opendir("middle2");
sl@0
   461
	test(dp!=0);
sl@0
   462
sl@0
   463
	count=0;
sl@0
   464
	do
sl@0
   465
		{
sl@0
   466
		ep=readdir(dp);
sl@0
   467
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   468
			count++;
sl@0
   469
		}
sl@0
   470
	while (ep!=0);
sl@0
   471
	test(count==0);	/* empty directory */
sl@0
   472
sl@0
   473
	rewinddir(dp);
sl@0
   474
sl@0
   475
	fd = open("middle2/extrafile",O_RDWR+O_CREAT,0777);
sl@0
   476
	test(fd>=0);
sl@0
   477
sl@0
   478
	err=close(fd);
sl@0
   479
	test(err==0);
sl@0
   480
sl@0
   481
	count=0;
sl@0
   482
	do
sl@0
   483
		{
sl@0
   484
		ep=readdir(dp);
sl@0
   485
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   486
			count++;
sl@0
   487
		}
sl@0
   488
	while (ep!=0);
sl@0
   489
	test(count==0);	/* shouldn't have noticed the change */
sl@0
   490
sl@0
   491
	rewinddir(dp);	/* and spot the new file */
sl@0
   492
	count=0;
sl@0
   493
	do
sl@0
   494
		{
sl@0
   495
		ep=readdir(dp);
sl@0
   496
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   497
			count++;
sl@0
   498
		}
sl@0
   499
	while (ep!=0);
sl@0
   500
	test(count==1);
sl@0
   501
sl@0
   502
	closedir(dp);
sl@0
   503
sl@0
   504
	dp=opendir("/");
sl@0
   505
	test(dp!=0);
sl@0
   506
sl@0
   507
	count=0;
sl@0
   508
	do
sl@0
   509
		{
sl@0
   510
		ep=readdir(dp);
sl@0
   511
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   512
			count++;
sl@0
   513
		}
sl@0
   514
	while (ep!=0);
sl@0
   515
	test(count>0);
sl@0
   516
sl@0
   517
	closedir(dp);
sl@0
   518
	}
sl@0
   519
sl@0
   520
/**
sl@0
   521
   Directory tree is now
sl@0
   522
 
sl@0
   523
     top / topfile
sl@0
   524
            middle2 / extrafile  
sl@0
   525
           middle1 / bottom1 / absfile
sl@0
   526
                   / bottom2 / file                -- read-only
sl@0
   527
 		   / read-only / sub-read-only /
sl@0
   528
                                 file
sl@0
   529
 
sl@0
   530
@SYMTestCaseID          SYSLIB-STDLIB-CT-1051
sl@0
   531
@SYMTestCaseDesc	    Tests for file attributes
sl@0
   532
@SYMTestPriority 	    High
sl@0
   533
@SYMTestActions  	    Tests the attributes on files and directories
sl@0
   534
@SYMTestExpectedResults Test must not fail
sl@0
   535
@SYMREQ                 REQ0000
sl@0
   536
*/		
sl@0
   537
void attributes()
sl@0
   538
	{
sl@0
   539
	int err;
sl@0
   540
	struct stat s1,s2;
sl@0
   541
	int fd;
sl@0
   542
	double diff;
sl@0
   543
sl@0
   544
	test_Next("File Attributes");
sl@0
   545
sl@0
   546
	err=chdir(rootpath);
sl@0
   547
	test(err==0);
sl@0
   548
sl@0
   549
	err=stat("middle earth/bag end/hobbit",&s1);
sl@0
   550
	test_errno(err<0,ENOENT);
sl@0
   551
sl@0
   552
	err=stat("middle1/bottom2/file",&s1);
sl@0
   553
	test(err==0);
sl@0
   554
	test(S_ISREG(s1.st_mode)!=0);
sl@0
   555
	test(S_ISDIR(s1.st_mode)==0);
sl@0
   556
	test((s1.st_mode&S_IWUSR)==0);
sl@0
   557
	test(s1.st_size==0);
sl@0
   558
sl@0
   559
	err=stat("topfile",&s1);
sl@0
   560
	test(err==0);
sl@0
   561
	test(S_ISREG(s1.st_mode)!=0);
sl@0
   562
	test(S_ISDIR(s1.st_mode)==0);
sl@0
   563
	test((s1.st_mode&S_IWUSR)!=0);
sl@0
   564
	test(s1.st_size==0);
sl@0
   565
sl@0
   566
	err=stat("topfile",&s2);
sl@0
   567
	test(err==0);
sl@0
   568
	test(s1.st_mode==s2.st_mode);
sl@0
   569
	test(s1.st_size==s2.st_size);
sl@0
   570
	diff=difftime(s1.st_mtime,s2.st_mtime);
sl@0
   571
	test(diff==(double)0.0);
sl@0
   572
sl@0
   573
	fd=open("topfile", O_RDONLY, 0);
sl@0
   574
	test(fd>=0);
sl@0
   575
sl@0
   576
	err=fstat(fd,&s2);
sl@0
   577
	test(err==0);
sl@0
   578
	test(s1.st_mode==s2.st_mode);
sl@0
   579
	test(s1.st_size==s2.st_size);
sl@0
   580
	diff=difftime(s1.st_mtime,s2.st_mtime);
sl@0
   581
	test(diff==(double)0.0);
sl@0
   582
sl@0
   583
	err=stat("topfile",&s2);
sl@0
   584
	test(err==0);
sl@0
   585
	test(s1.st_mode==s2.st_mode);
sl@0
   586
	test(s1.st_size==s2.st_size);
sl@0
   587
	diff=difftime(s1.st_mtime,s2.st_mtime);
sl@0
   588
	test(diff==(double)0.0);
sl@0
   589
sl@0
   590
	err=close(fd);
sl@0
   591
	test(err==0);
sl@0
   592
sl@0
   593
	sleep(1);	/* to ensure that the modify time changes */
sl@0
   594
sl@0
   595
	fd=open("topfile", O_RDWR+O_APPEND, 0);
sl@0
   596
	test(fd>=0);
sl@0
   597
sl@0
   598
	err=stat("topfile",&s2);
sl@0
   599
	test(err==0);
sl@0
   600
	test(s1.st_mode==s2.st_mode);
sl@0
   601
	test(s1.st_size==s2.st_size);
sl@0
   602
	/* probably not guaranteeed to have changed the modtime at this point */
sl@0
   603
sl@0
   604
	err=write(fd,rootpath,3);
sl@0
   605
	test(err==3);
sl@0
   606
sl@0
   607
	err=fsync(fd);
sl@0
   608
	test(err==0);
sl@0
   609
sl@0
   610
	err=close(fd);
sl@0
   611
	test(err==0);
sl@0
   612
sl@0
   613
	err=stat("topfile",&s2);
sl@0
   614
	test(err==0);
sl@0
   615
	test(s1.st_mode==s2.st_mode);
sl@0
   616
	test(s2.st_size==3);
sl@0
   617
	diff=difftime(s2.st_mtime,s1.st_mtime);
sl@0
   618
	test(diff>(double)0.0);
sl@0
   619
sl@0
   620
	test_Next("Directory Attributes");
sl@0
   621
sl@0
   622
	err=stat("middle1",&s1);
sl@0
   623
	test(err==0);
sl@0
   624
	test(S_ISREG(s1.st_mode)==0);
sl@0
   625
	test(S_ISDIR(s1.st_mode)==1);
sl@0
   626
	test((s1.st_mode&S_IWUSR)!=0);
sl@0
   627
sl@0
   628
	err=stat("middle1/read-only",&s1);
sl@0
   629
	test(err==0);
sl@0
   630
	test(S_ISREG(s1.st_mode)==0);
sl@0
   631
	test(S_ISDIR(s1.st_mode)==1);
sl@0
   632
	test((s1.st_mode&S_IWUSR)==0);
sl@0
   633
sl@0
   634
	err=stat("/",&s1);
sl@0
   635
	test(err==0);
sl@0
   636
	test(S_ISREG(s1.st_mode)==0);
sl@0
   637
	test(S_ISDIR(s1.st_mode)==1);
sl@0
   638
sl@0
   639
	err=access("middle1/bottom1/absfile",W_OK);
sl@0
   640
	test(err==0);
sl@0
   641
sl@0
   642
	err=access("middle1/bottom1/absfile",R_OK);
sl@0
   643
	test(err==0);
sl@0
   644
sl@0
   645
	err=access("middle1/bottom2/file",W_OK);
sl@0
   646
	test(err!=0);
sl@0
   647
sl@0
   648
	err=access("middle1/bottom2/file",R_OK);
sl@0
   649
	test(err==0);
sl@0
   650
sl@0
   651
	err=access("middle1/read-only",W_OK);
sl@0
   652
	test(err==0);
sl@0
   653
sl@0
   654
	err=access("middle1/read-only",R_OK);
sl@0
   655
	test(err==0);
sl@0
   656
sl@0
   657
	err=access("middle1/no such directory",R_OK);
sl@0
   658
	test(err!=0);
sl@0
   659
}
sl@0
   660
sl@0
   661
/**
sl@0
   662
   Directory tree is now
sl@0
   663
 
sl@0
   664
     top / topfile
sl@0
   665
           middle2 / extrafile  
sl@0
   666
           middle1 / bottom1 / absfile
sl@0
   667
                   / bottom2 / file                -- read-only
sl@0
   668
 		   / read-only / sub-read-only /
sl@0
   669
                                 file
sl@0
   670
 
sl@0
   671
sl@0
   672
@SYMTestCaseID          SYSLIB-STDLIB-CT-1052
sl@0
   673
@SYMTestCaseDesc	    Tests for searching on different drives
sl@0
   674
@SYMTestPriority 	    High
sl@0
   675
@SYMTestActions  	    Tests by searching on z drive,test for the error codes 
sl@0
   676
@SYMTestExpectedResults Test must not fail
sl@0
   677
@SYMREQ                 REQ0000
sl@0
   678
*/		
sl@0
   679
 void searching()
sl@0
   680
	{
sl@0
   681
	int err,fd;
sl@0
   682
	char name[MAXPATHLEN+1];
sl@0
   683
sl@0
   684
	test_Next("Searching across drives");
sl@0
   685
sl@0
   686
	sprintf(name,"%s/middle2/extrafile",rootpath);
sl@0
   687
sl@0
   688
	err=chdir("z:/");
sl@0
   689
	test(err==0);
sl@0
   690
sl@0
   691
	fd=open(name+2, O_RDONLY, 0);
sl@0
   692
	test_errno(fd<0,ENOENT);	// doesn't exist on z:
sl@0
   693
sl@0
   694
	name[0]='?';
sl@0
   695
	fd=open(name, O_RDWR, 0);
sl@0
   696
	test(fd>=0);			// found it on the original drive
sl@0
   697
sl@0
   698
	err=close(fd);
sl@0
   699
	test(err==0);
sl@0
   700
	}
sl@0
   701
sl@0
   702
/**
sl@0
   703
   Directory tree is now
sl@0
   704
 
sl@0
   705
     top / topfile
sl@0
   706
           middle2 / extrafile  
sl@0
   707
           middle1 / bottom1 / absfile
sl@0
   708
                   / bottom2 / file                -- read-only
sl@0
   709
 		   / read-only / sub-read-only /
sl@0
   710
                                 file
sl@0
   711
 
sl@0
   712
sl@0
   713
@SYMTestCaseID          SYSLIB-STDLIB-CT-1053
sl@0
   714
@SYMTestCaseDesc	    Tests for deleting files
sl@0
   715
@SYMTestPriority 	    High
sl@0
   716
@SYMTestActions  	    Tests by deleting files and directories.Test for error codes
sl@0
   717
@SYMTestExpectedResults Test must not fail
sl@0
   718
@SYMREQ                 REQ0000
sl@0
   719
*/		
sl@0
   720
void deletion()
sl@0
   721
	{
sl@0
   722
	int err;
sl@0
   723
	char namebuf[MAXPATHLEN];
sl@0
   724
	wchar_t widename[] = WIDENAME;
sl@0
   725
sl@0
   726
sl@0
   727
	test_Next("Deleting - files");
sl@0
   728
sl@0
   729
	err=chdir(rootpath);
sl@0
   730
	test(err==0);
sl@0
   731
sl@0
   732
sl@0
   733
sl@0
   734
	err=unlink("middle1/bottom2/file");
sl@0
   735
	test_errno(err<0,EACCES);	/* file is read-only */
sl@0
   736
sl@0
   737
	err=chmod("middle1/bottom2/file",0777);
sl@0
   738
	test(err==0);
sl@0
   739
sl@0
   740
	err=unlink("middle1/bottom2/file");
sl@0
   741
	test(err==0);
sl@0
   742
sl@0
   743
	err=unlink("middle2/extrafile");
sl@0
   744
	test(err==0);
sl@0
   745
sl@0
   746
	err=unlink("middle1/read-only/file");
sl@0
   747
	/* test_errno(err<0,EPERM);	parent directory is read-only */
sl@0
   748
	test(err==0);	/* Omission - EPOC32 uses Win32 semantics for read-only directories */
sl@0
   749
sl@0
   750
	test_Next("Deleting - directories");
sl@0
   751
sl@0
   752
	err=chdir(rootpath);
sl@0
   753
	test(err==0);
sl@0
   754
sl@0
   755
	//delete the dir with a wide character in the name
sl@0
   756
	err = wcstombs(namebuf, widename, MAXPATHLEN);
sl@0
   757
	test(err!=-1);
sl@0
   758
	
sl@0
   759
	err=rmdir(namebuf);
sl@0
   760
	test(err==0);
sl@0
   761
sl@0
   762
	err=rmdir("middle1");
sl@0
   763
	test_errno(err<0,EEXIST);	/* not empty */
sl@0
   764
sl@0
   765
	err=rmdir("middle1/bottom1");
sl@0
   766
	test_errno(err<0,EEXIST);	/* not empty */
sl@0
   767
sl@0
   768
	err=unlink("middle1/bottom1/absfile");
sl@0
   769
	test(err==0);
sl@0
   770
sl@0
   771
	err=rmdir("middle1/bottom1");
sl@0
   772
	test(err==0);
sl@0
   773
sl@0
   774
	err=rmdir("middle1/bottom1");
sl@0
   775
	test_errno(err<0,ENOENT);	/* already deleted */
sl@0
   776
sl@0
   777
	err=rmdir("middle1");
sl@0
   778
	test_errno(err<0,EEXIST);
sl@0
   779
sl@0
   780
	err=rmdir("middle1/bottom2");
sl@0
   781
	test(err==0);
sl@0
   782
sl@0
   783
	test_Next("Deleting - read-only directories");
sl@0
   784
sl@0
   785
	err=rmdir("middle1/read-only/sub-read-only");
sl@0
   786
	/* test_errno(err!=0,EACCES);	-- permission denied - read-only parent */
sl@0
   787
	test_errno(err<0,EACCES);	/* Omission - EPOC32 uses Win32 semantics */
sl@0
   788
sl@0
   789
	err=chmod("middle1/read-only",0777);
sl@0
   790
	test(err==0);
sl@0
   791
sl@0
   792
	err=rmdir("middle1/read-only/sub-read-only");
sl@0
   793
	/* test(err==0); */
sl@0
   794
	/* EPOC32 doesn't use the writeability of the parent directory, but instead looks 
sl@0
   795
	 * at the attributes of the directory itself.
sl@0
   796
	 */
sl@0
   797
	test_errno(err!=0,EACCES);
sl@0
   798
sl@0
   799
	err=chmod("middle1/read-only/sub-read-only",0777);
sl@0
   800
	test(err==0);
sl@0
   801
sl@0
   802
	err=rmdir("middle1/read-only/sub-read-only");
sl@0
   803
	test(err==0);
sl@0
   804
sl@0
   805
	err=rmdir("middle1/read-only");
sl@0
   806
	test(err==0);
sl@0
   807
sl@0
   808
	err=rmdir("middle?");
sl@0
   809
	test_errno(err<0,EINVAL);	/* no wild cards please */
sl@0
   810
sl@0
   811
	err=rmdir("middle1");
sl@0
   812
	test(err==0);
sl@0
   813
sl@0
   814
	err=rmdir("../top/middle2");
sl@0
   815
	test(err==0);
sl@0
   816
sl@0
   817
	err=rmdir(".");
sl@0
   818
	test_errno(err<0,EEXIST);	/* not empty */
sl@0
   819
sl@0
   820
	err=unlink("topfile");
sl@0
   821
	test(err==0);
sl@0
   822
sl@0
   823
	err=rmdir(".");
sl@0
   824
	test(err==0);
sl@0
   825
	}
sl@0
   826
sl@0
   827
/**
sl@0
   828
@SYMTestCaseID          SYSLIB-STDLIB-CT-1054
sl@0
   829
@SYMTestCaseDesc	    Tests for creation of temporary directory and files in it.
sl@0
   830
@SYMTestPriority 	    High
sl@0
   831
@SYMTestActions  	    Tests by creating a temporary directory,files and writing to the files.
sl@0
   832
                        Check for error codes.
sl@0
   833
@SYMTestExpectedResults Test must not fail
sl@0
   834
@SYMREQ                 REQ0000
sl@0
   835
*/		
sl@0
   836
void temporary_files()
sl@0
   837
	{
sl@0
   838
#define tmpdir        "c:/system/temp"
sl@0
   839
sl@0
   840
	int err, count1, count2;
sl@0
   841
	DIR *dp;
sl@0
   842
	struct dirent *ep;
sl@0
   843
	FILE *fp;
sl@0
   844
	char name[L_tmpnam];
sl@0
   845
	char name2[L_tmpnam];
sl@0
   846
	char *p;
sl@0
   847
sl@0
   848
	test_Next("Temporary files");
sl@0
   849
sl@0
   850
	dp=opendir(tmpdir);
sl@0
   851
	if (dp==0)
sl@0
   852
		{
sl@0
   853
		printf("  Creating the directory %s ...\n", tmpdir);
sl@0
   854
		err=mkdir("c:/system", 0777);
sl@0
   855
		test(err==0);
sl@0
   856
		err=mkdir(tmpdir, 0777);
sl@0
   857
		test(err==0);
sl@0
   858
		dp=opendir(tmpdir);
sl@0
   859
		}
sl@0
   860
	test(dp!=0);
sl@0
   861
sl@0
   862
	count1=0;
sl@0
   863
	do
sl@0
   864
		{
sl@0
   865
		ep=readdir(dp);
sl@0
   866
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   867
			count1++;
sl@0
   868
		}
sl@0
   869
	while (ep!=0);
sl@0
   870
sl@0
   871
	fp=tmpfile();
sl@0
   872
	test(fp!=0);
sl@0
   873
sl@0
   874
	err=fprintf(fp,"hello");
sl@0
   875
	test(err==5);
sl@0
   876
sl@0
   877
	rewinddir(dp);
sl@0
   878
	count2=0;
sl@0
   879
	do
sl@0
   880
		{
sl@0
   881
		ep=readdir(dp);
sl@0
   882
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   883
			count2++;
sl@0
   884
		}
sl@0
   885
	while (ep!=0);
sl@0
   886
	test(count2==count1+1);	/* EPOC32 temporary files are visible in file system */
sl@0
   887
	err=fclose(fp);
sl@0
   888
	test(err==0);
sl@0
   889
sl@0
   890
	rewinddir(dp);
sl@0
   891
	count2=0;
sl@0
   892
	do
sl@0
   893
		{
sl@0
   894
		ep=readdir(dp);
sl@0
   895
		if (ep && strcmp(ep->d_name,".")!=0 && strcmp(ep->d_name,"..")!=0)
sl@0
   896
			count2++;
sl@0
   897
		}
sl@0
   898
	while (ep!=0);
sl@0
   899
	test(count2==count1);		/* should be automatically deleted */
sl@0
   900
sl@0
   901
	closedir(dp);
sl@0
   902
sl@0
   903
	p=tmpnam(NULL);
sl@0
   904
	test(p!=0);
sl@0
   905
sl@0
   906
	count1=strlen(p);
sl@0
   907
	test(count1<L_tmpnam);
sl@0
   908
sl@0
   909
	p=tmpnam(name);
sl@0
   910
	test(p==name);
sl@0
   911
sl@0
   912
	fp=fopen(name,"wb+");
sl@0
   913
	test(fp!=0);
sl@0
   914
sl@0
   915
	p=tmpnam(name2);
sl@0
   916
	test(p==name2);
sl@0
   917
sl@0
   918
	err=strcmp(name,name2);
sl@0
   919
	test(err!=0);
sl@0
   920
sl@0
   921
	err=fclose(fp);
sl@0
   922
	test(err==0);
sl@0
   923
sl@0
   924
	err=unlink(name);
sl@0
   925
	test(err==0);
sl@0
   926
sl@0
   927
	printf("  Tmpnam suggested %s and %s\n", name, name2);
sl@0
   928
	}
sl@0
   929
sl@0
   930
int close_console=0;
sl@0
   931
void allTests()
sl@0
   932
	{
sl@0
   933
	int err=chdir("C:\\");
sl@0
   934
	test(err==0);
sl@0
   935
sl@0
   936
	make_tree();
sl@0
   937
	create_files();
sl@0
   938
	renaming();
sl@0
   939
	directory();
sl@0
   940
	attributes();
sl@0
   941
	searching();
sl@0
   942
	deletion();
sl@0
   943
	temporary_files();
sl@0
   944
sl@0
   945
	if (close_console)
sl@0
   946
		{
sl@0
   947
		test_Close();
sl@0
   948
		close(0);
sl@0
   949
		close(1);
sl@0
   950
		close(2);
sl@0
   951
sl@0
   952
		CloseSTDLIB();
sl@0
   953
		}
sl@0
   954
	}
sl@0
   955
sl@0
   956
int main()
sl@0
   957
	{
sl@0
   958
	void* client;
sl@0
   959
	int err;
sl@0
   960
sl@0
   961
	test_Title("Directory Handling");
sl@0
   962
sl@0
   963
	allTests();
sl@0
   964
sl@0
   965
	test_Next("Do it again using the CPosixServer (for them, not me)");
sl@0
   966
	close_console=1;
sl@0
   967
sl@0
   968
	start_posix_server();	/* calls SpawnPosixServer from C++ code */
sl@0
   969
sl@0
   970
	client=create_thread(allTests, "TDIRS tests");
sl@0
   971
	test(client!=0);
sl@0
   972
	start_thread(client);
sl@0
   973
	err=wait_for_thread(client);
sl@0
   974
	test(err==0);
sl@0
   975
sl@0
   976
	CloseSTDLIB();
sl@0
   977
	test_Close();
sl@0
   978
	return 0;
sl@0
   979
	}