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