os/ossrv/genericopenlibs/cstdlib/TSTLIB/TWDIRS.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TWDIRS.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,967 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* Test code for directory and file handling
    1.19 +* 
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +
    1.25 +#include <stdlib.h>
    1.26 +#include <stdio.h>
    1.27 +#include <string.h>
    1.28 +#include <unistd.h>
    1.29 +#include <sys/stat.h>
    1.30 +#include <sys/fcntl.h>
    1.31 +#include <dirent.h>
    1.32 +#include <errno.h>
    1.33 +
    1.34 +#include "CTEST.H"	/* includes C interface to EPOC32 threads, and SpawnPosixServer */
    1.35 +
    1.36 +test_Data;
    1.37 +wchar_t rootpath[MAXPATHLEN];
    1.38 +
    1.39 +#define WIDENAME {L'/', L'T', L'o', L'p', L'/', L'e', L'u', L'r', L'o', 0x20ac, 0}
    1.40 +
    1.41 +/* construct a directory tree in various ways */
    1.42 +
    1.43 +/**
    1.44 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1082
    1.45 +@SYMTestCaseDesc	    Tests for operations on directory 
    1.46 +@SYMTestPriority 	    High
    1.47 +@SYMTestActions  	    Tests by creating directory with invalid name,existing directory name,wide characters
    1.48 +                        Tests for the error code returned while creating directories.
    1.49 +@SYMTestExpectedResults Test must not fail
    1.50 +@SYMREQ                 REQ0000
    1.51 +*/		
    1.52 +void make_tree()
    1.53 +	{
    1.54 +	int err,x;
    1.55 +	wchar_t namebuf[MAXPATHLEN], namebuf2[MAXPATHLEN];
    1.56 +	wchar_t toobig[MAXPATHLEN+MAXPATHLEN+1];
    1.57 +	wchar_t *p;
    1.58 +
    1.59 +	wchar_t widename[] = WIDENAME;
    1.60 +	
    1.61 +	test_Next("Create Directory Tree - relative paths");
    1.62 +
    1.63 +	err=wmkdir((wchar_t*)L"***", 0777);
    1.64 +	test_errno(err==-1,EINVAL);	/* bad directory name */
    1.65 +
    1.66 +
    1.67 +	err=wmkdir((wchar_t*)L"top", 0777);
    1.68 +	test(err==0);
    1.69 +
    1.70 +	err=wmkdir((wchar_t*)L"top", 0777);
    1.71 +	test_errno(err==-1,EEXIST);	/* directory already exists */
    1.72 +
    1.73 +	err=wmkdir(widename, 0777);
    1.74 +	test(err==0);
    1.75 +
    1.76 +	err=wmkdir((wchar_t*)L"top/middle1/bottom1", 0777);
    1.77 +	test_errno(err==-1,ENOENT);	/* missing middle bit of path */
    1.78 +
    1.79 +	err=wmkdir((wchar_t*)L"top/middle1", 0777);
    1.80 +	test(err==0);
    1.81 +
    1.82 +	err=wchdir((wchar_t*)L"./top//\\/.\\.\\../top/.");
    1.83 +	test(err==0);
    1.84 +
    1.85 +	p = wgetcwd(rootpath,sizeof(rootpath)/2);	/* save name of toplevel directory */
    1.86 +	test(p==rootpath);
    1.87 +
    1.88 +	err=wchdir((wchar_t*)L"middle1");
    1.89 +	test(err==0);
    1.90 +
    1.91 +	err=wchdir((wchar_t*)L"bottom2");
    1.92 +	test_errno(err==-1,ENOENT);	/* directory doesn't exist yet */
    1.93 +
    1.94 +	p = wgetcwd(namebuf,sizeof(namebuf));	/* prepare name for tests later */
    1.95 +	test(p==namebuf);
    1.96 +
    1.97 +	err=wmkdir((wchar_t*)L"bottom1",0777);
    1.98 +	test(err==0);
    1.99 +
   1.100 +	err=wmkdir((wchar_t*)L"read-only",0444);
   1.101 +	test(err==0);
   1.102 +
   1.103 +	err=wmkdir((wchar_t*)L"read-only/sub-read-only",0444);
   1.104 +	/* test_errno(err==-1,EACCES); */
   1.105 +	test(err==0);	/* Omission - EPOC32 has Win32 semantics for read-only directories */
   1.106 +
   1.107 +	err=wchdir((wchar_t*)L"../../top/middle1/bottom1");
   1.108 +	test(err==0);
   1.109 +
   1.110 +	test_Next("Create Directory Tree - absolute paths");
   1.111 +
   1.112 +	p = wcscat(namebuf,(wchar_t*)L"/bottom2");
   1.113 +	test(p==namebuf);	/* .../top/middle1/bottom2 */
   1.114 +
   1.115 +	err=wchdir(namebuf);
   1.116 +	test_errno(err==-1,ENOENT);	/* directory doesn't exist yet */
   1.117 +	
   1.118 +	err=wmkdir(namebuf, 0777);
   1.119 +	test(err==0);
   1.120 +
   1.121 +	err=wchdir(namebuf);
   1.122 +	test(err==0);
   1.123 +
   1.124 +	p = wgetcwd(namebuf,sizeof(namebuf));
   1.125 +	test(p==namebuf);
   1.126 +
   1.127 +	err=wmkdir((wchar_t*)L"../../middle2", 0777);
   1.128 +	test(err==0);
   1.129 +
   1.130 +	p = wgetcwd(namebuf2,sizeof(namebuf2));
   1.131 +	test(p==namebuf2);
   1.132 +	test(wcscmp(namebuf,namebuf2)==0);	/* mkdir shouldn't change cwd */
   1.133 +
   1.134 +//	memset(toobig,L'a', sizeof(toobig));
   1.135 +//	toobig[sizeof(toobig)-1]='\0';
   1.136 +	for (x = 0; x < (sizeof(toobig)/2)-1; x++)
   1.137 +		toobig[x] = L'a';
   1.138 +	toobig[x] = L'\0';
   1.139 +
   1.140 +
   1.141 +	err=wmkdir(toobig,0777);
   1.142 +	test_errno(err<0,ENAMETOOLONG);
   1.143 +
   1.144 +
   1.145 +	test_Next("Test wgetcwd");
   1.146 +	
   1.147 +	//size too small
   1.148 +	p = wgetcwd(namebuf, 4);
   1.149 +	test_errno(0==p, ERANGE);
   1.150 +
   1.151 +	//make it alloc a buffer
   1.152 +	p = wgetcwd(NULL, 300);
   1.153 +	test (NULL != p);
   1.154 +	free(p);
   1.155 +
   1.156 +	//alloc a buffer then fail with a too small size
   1.157 +	p = wgetcwd(NULL, 10);
   1.158 +	test_errno(0==p, ERANGE);
   1.159 +
   1.160 +	p = wgetcwd(namebuf2, MAXPATHLEN-1);
   1.161 +	test (NULL != p);
   1.162 +
   1.163 +	
   1.164 +	test_Next("Test wrealpath");
   1.165 +	p = wrealpath((wchar_t*)L"/top/../top/../top/./",namebuf);
   1.166 +	test((0==wcscmp((wchar_t*)L"C:\\top\\", p)) || (0==wcscmp((wchar_t*)L"D:\\top\\", p)));
   1.167 +	test((0==wcscmp((wchar_t*)L"C:\\top\\", namebuf)) || (0==wcscmp((wchar_t*)L"D:\\top\\", namebuf)));
   1.168 +	p = wrealpath((wchar_t*)L"/top/../top/././top/./",namebuf);
   1.169 +
   1.170 +}
   1.171 +
   1.172 +/**
   1.173 +   Directory tree is now
   1.174 + 
   1.175 +     top / middle2   
   1.176 +           middle1 / bottom1
   1.177 +                   / bottom2
   1.178 + 		   / read-only / sub-read-only
   1.179 +
   1.180 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1083
   1.181 +@SYMTestCaseDesc	    Tests for operations on creating files
   1.182 +@SYMTestPriority 	    High
   1.183 +@SYMTestActions  	    Tests by opening files which does not exists,check for closing a file twice
   1.184 +                        Tests for the error code returned while creating files
   1.185 +@SYMTestExpectedResults Test must not fail
   1.186 +@SYMREQ                 REQ0000
   1.187 +*/		
   1.188 +void create_files()
   1.189 +	{
   1.190 +	int err;
   1.191 +	int fd;
   1.192 +	wchar_t namebuf[MAXPATHLEN],*p;
   1.193 +
   1.194 +	test_Next("Creating Files - relative paths");
   1.195 +
   1.196 +	err=wchdir(rootpath);
   1.197 +	test(err==0);
   1.198 +
   1.199 +	fd = wopen((wchar_t*)L"topfile",O_RDWR+O_APPEND,0777);
   1.200 +	test_errno(fd<0,ENOENT);	/* doesn't exist */
   1.201 +
   1.202 +	fd = wopen((wchar_t*)L"topfile",O_RDWR+O_CREAT,0777);
   1.203 +	test(fd>=0);
   1.204 +
   1.205 +	err=close(fd);
   1.206 +	test(err==0);
   1.207 +
   1.208 +	err=close(fd);
   1.209 +	test_errno(err<0,EBADF);	/* can't close it twice */
   1.210 +
   1.211 +	fd = wopen((wchar_t*)L"topfile",O_RDWR+O_APPEND,0777);
   1.212 +	test(fd>=0);
   1.213 +
   1.214 +	err=close(fd);
   1.215 +	test(err==0);
   1.216 +
   1.217 +	fd = wopen((wchar_t*)L"topfile",O_RDWR+O_CREAT+O_EXCL,0777);
   1.218 +	test_errno(fd<0,EEXIST);	/* already exists */
   1.219 +
   1.220 +	fd = wopen((wchar_t*)L"middle1/bottom2/file",O_RDONLY+O_CREAT,0444);
   1.221 +	test(fd>=0);
   1.222 +
   1.223 +	err=close(fd);
   1.224 +	test(err==0);
   1.225 +
   1.226 +	fd = wopen((wchar_t*)L"middle1/bottom2/file",O_RDWR+O_APPEND,0777);
   1.227 +	/* test_errno(fd<0,EACCES); */
   1.228 +	test(fd>=0);	/* Omission - the original O_CREAT ignores the 0444 permissions */
   1.229 +	if (fd>=0)
   1.230 +		{
   1.231 +		err=close(fd);
   1.232 +		test(err==0);
   1.233 +		}
   1.234 +
   1.235 +	err=wchmod((wchar_t*)L"middle1/bottom2/file",0444);
   1.236 +	test(err==0);
   1.237 +
   1.238 +	fd = wopen((wchar_t*)L"middle1/bottom2/file",O_RDWR+O_APPEND,0777);
   1.239 +	test_errno(fd<0,EACCES);	/* not writeable */
   1.240 +
   1.241 +	fd = wopen((wchar_t*)L"middle2",O_RDWR+O_CREAT,0777);
   1.242 +	/* test_errno(fd<0,EISDIR); */
   1.243 +	test_errno(fd<0,EACCES);	/* Omission - we don't do EISDIR */
   1.244 +
   1.245 +	test_Next("Creating Files - absolute paths");
   1.246 +
   1.247 +	err=wchdir((wchar_t*)L"middle1/bottom1");
   1.248 +	test(err==0);
   1.249 +
   1.250 +	p = wgetcwd(namebuf,sizeof(namebuf)/2);	/* prepare name for tests later */
   1.251 +	test(p==namebuf);
   1.252 +
   1.253 +	p = wcscat(namebuf,(wchar_t*)L"absfile");
   1.254 +	test(p==namebuf);
   1.255 +
   1.256 +	fd = wopen(namebuf,O_RDWR+O_CREAT,0777);
   1.257 +	test(fd>=0);
   1.258 +
   1.259 +	err=close(fd);
   1.260 +	test(err==0);
   1.261 +
   1.262 +	fd = wopen((wchar_t*)L"../read-only/file",O_RDWR+O_CREAT,0444);
   1.263 +	/* test_errno(fd<0,EACCES); */
   1.264 +	test(fd>=0);	/* Omission - EPOC32 has Win32 semantics for read-only directories */
   1.265 +	if (fd>=0)
   1.266 +		{
   1.267 +		err=close(fd);
   1.268 +		test(err==0);
   1.269 +		}
   1.270 +	
   1.271 +	}
   1.272 +
   1.273 +/**
   1.274 + Directory tree is now
   1.275 + 
   1.276 +     top / topfile
   1.277 +           middle2 /   
   1.278 +           middle1 / bottom1 / absfile
   1.279 +                   / bottom2 / file                -- read-only
   1.280 +  		   / read-only / sub-read-only /
   1.281 +                                file
   1.282 + 
   1.283 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1084
   1.284 +@SYMTestCaseDesc	    Tests for renaming operations 
   1.285 +@SYMTestPriority 	    High
   1.286 +@SYMTestActions  	    Tests by renaming files.Tests for the error code returned while renaming files
   1.287 +@SYMTestExpectedResults Test must not fail
   1.288 +@SYMREQ                 REQ0000
   1.289 +*/		
   1.290 +void renaming()
   1.291 +	{
   1.292 +	int err;
   1.293 +
   1.294 +	test_Next("Renaming");
   1.295 +
   1.296 +	err=wchdir(rootpath);
   1.297 +	test(err==0);
   1.298 +
   1.299 +	err=wrename((wchar_t*)L"middle1",(wchar_t*)L"middle2");
   1.300 +	test_errno(err<0,EEXIST);
   1.301 +
   1.302 +	err=wrename((wchar_t*)L"middle1/bottom1/absfile",(wchar_t*)L"middle2/absfile");
   1.303 +	test(err==0);
   1.304 +
   1.305 +	err=wrename((wchar_t*)L"middle2/absfile",(wchar_t*)L"middle1/bottom1/absfile");
   1.306 +	test(err==0);
   1.307 +
   1.308 +	err=wrename((wchar_t*)L"middle1/bottom1/absfile",(wchar_t*)L"middle2/nonsuch/newname");
   1.309 +	test_errno(err<0,ENOENT);
   1.310 +
   1.311 +	err=wrename((wchar_t*)L"middle1",(wchar_t*)L"middle1/bottom1/subdirectory_of_self");
   1.312 +	test_errno(err<0,EACCES);
   1.313 +
   1.314 +	err=wrename((wchar_t*)L"middle1",(wchar_t*)L"newname");
   1.315 +	test(err==0);
   1.316 +
   1.317 +	err=wrename((wchar_t*)L"newname/bottom2/file",(wchar_t*)L"middle2/file");
   1.318 +	test(err==0);
   1.319 +
   1.320 +	err=wrename((wchar_t*)L"newname",(wchar_t*)L"middle1");
   1.321 +	test(err==0);
   1.322 +
   1.323 +	err=wrename((wchar_t*)L"middle2/file",(wchar_t*)L"middle1/bottom2/file");
   1.324 +	test(err==0);
   1.325 +
   1.326 +	err=wrename((wchar_t*)L"no such file",(wchar_t*)L"valid new name");
   1.327 +	test_errno(err<0,ENOENT);
   1.328 +
   1.329 +	err=wrename((wchar_t*)L"no such file",(wchar_t*)L"topfile");
   1.330 +	test_errno(err<0,ENOENT);
   1.331 +
   1.332 +	err=wrename((wchar_t*)L".",(wchar_t*)L"../different top");
   1.333 +	/* test_errno(err<0,EACCES);	-- can't change "." */
   1.334 +	test(err==0);	/* STDLIB resolves "." to full path, so this works */
   1.335 +
   1.336 +	err=wrename((wchar_t*)L"../different top",rootpath);
   1.337 +	test(err==0);
   1.338 +	}
   1.339 +
   1.340 +/** 
   1.341 +  Directory tree is now
   1.342 + 
   1.343 +     top / topfile
   1.344 +           middle2 /   
   1.345 +           middle1 / bottom1 / absfile
   1.346 +                   / bottom2 / file                -- read-only
   1.347 + 		   / read-only / sub-read-only /
   1.348 +                                 file
   1.349 +
   1.350 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1085
   1.351 +@SYMTestCaseDesc	    Tests for enumeration on directories 
   1.352 +@SYMTestPriority 	    High
   1.353 +@SYMTestActions  	    Tests for using WDIR inplace of DIR
   1.354 +@SYMTestExpectedResults Test must not fail
   1.355 +@SYMREQ                 REQ0000
   1.356 +*/		
   1.357 +void directory()
   1.358 +	{
   1.359 +	int err, count, i, j, fd;
   1.360 +	WDIR *dp;
   1.361 +	struct wdirent *ep;
   1.362 +	wchar_t name[MAXPATHLEN+1];
   1.363 +	off_t pos;
   1.364 +
   1.365 +	test_Next("Enumerating Directories");
   1.366 +
   1.367 +	err=wchdir(rootpath);
   1.368 +	test(err==0);
   1.369 +
   1.370 +	dp=wopendir((wchar_t*)L"topfile");
   1.371 +	/* test_errno(dp==0,ENOTDIR); -- not convinced about this anyway */
   1.372 +	test_errno(dp==0,ENOENT);
   1.373 +
   1.374 +	dp=wopendir((wchar_t*)L"no such file");
   1.375 +	test_errno(dp==0,ENOENT);
   1.376 +
   1.377 +	//test something sensible happens if someone uses a WDIR inplace of a DIR
   1.378 +	{
   1.379 +		DIR *p = opendir(".");
   1.380 +		test(p!=0);
   1.381 +
   1.382 +		// Test wants a DIR passed but won't compile under CW.
   1.383 +		// Force the compile by casting. The function will still get a DIR.
   1.384 +		// DIR inherits from WDIR.
   1.385 +		ep=wreaddir((WDIR*)p);  //expect a warning from this line.  p *IS* the wrong type
   1.386 +		test(ep != 0);
   1.387 +	}
   1.388 +
   1.389 +	dp=wopendir((wchar_t*)L".");
   1.390 +	test(dp!=0);
   1.391 +
   1.392 +	count=0;
   1.393 +	do
   1.394 +		{
   1.395 +		ep=wreaddir(dp);
   1.396 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.397 +			count++;
   1.398 +		}
   1.399 +	while (ep!=0);
   1.400 +	test(count==4);
   1.401 +
   1.402 +	for (i=0; i<4; i++)
   1.403 +		{
   1.404 +		wrewinddir(dp);
   1.405 +		for (j=0; j<=i; j++)
   1.406 +			{
   1.407 +			ep=wreaddir(dp);
   1.408 +			test(ep!=0);
   1.409 +			}
   1.410 +		wcscpy(name,ep->d_name);
   1.411 +		wrewinddir(dp);
   1.412 +		for (j=0; j<=i; j++)
   1.413 +			{
   1.414 +			ep=wreaddir(dp);
   1.415 +			test(ep!=0);
   1.416 +			}
   1.417 +		test(wcscmp(name,ep->d_name)==0);
   1.418 +		}
   1.419 +
   1.420 +	for (i=0; i<4; i++)
   1.421 +		{
   1.422 +		wrewinddir(dp);
   1.423 +		pos=wtelldir(dp);
   1.424 +		for (j=0; j<=i; j++)
   1.425 +			{
   1.426 +			pos=wtelldir(dp);
   1.427 +			ep=wreaddir(dp);
   1.428 +			test(ep!=0);
   1.429 +			}
   1.430 +		wcscpy(name,ep->d_name);
   1.431 +		wrewinddir(dp);
   1.432 +		wseekdir(dp, pos);
   1.433 +		ep=wreaddir(dp);
   1.434 +		test(ep!=0);
   1.435 +		test(wcscmp(name,ep->d_name)==0);
   1.436 +		}
   1.437 +
   1.438 +	err=wclosedir(dp);
   1.439 +	test(err==0);
   1.440 +
   1.441 +	dp=wopendir((wchar_t*)L"middle2\\");
   1.442 +	test(dp!=0);
   1.443 +
   1.444 +	count=0;
   1.445 +	do
   1.446 +		{
   1.447 +		ep=wreaddir(dp);
   1.448 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.449 +			count++;
   1.450 +		}
   1.451 +	while (ep!=0);
   1.452 +	test(count==0);	/* empty directory */
   1.453 +
   1.454 +	wrewinddir(dp);
   1.455 +
   1.456 +	fd = wopen((wchar_t*)L"middle2/extrafile",O_RDWR+O_CREAT,0777);
   1.457 +	test(fd>=0);
   1.458 +
   1.459 +	err=close(fd);
   1.460 +	test(err==0);
   1.461 +
   1.462 +	count=0;
   1.463 +	do
   1.464 +		{
   1.465 +		ep=wreaddir(dp);
   1.466 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.467 +			count++;
   1.468 +		}
   1.469 +	while (ep!=0);
   1.470 +	test(count==0);	/* shouldn't have noticed the change */
   1.471 +
   1.472 +	wrewinddir(dp);	/* and spot the new file */
   1.473 +	count=0;
   1.474 +	do
   1.475 +		{
   1.476 +		ep=wreaddir(dp);
   1.477 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.478 +			count++;
   1.479 +		}
   1.480 +	while (ep!=0);
   1.481 +	test(count==1);
   1.482 +
   1.483 +	wclosedir(dp);
   1.484 +
   1.485 +	dp=wopendir((wchar_t*)L"/");
   1.486 +	test(dp!=0);
   1.487 +
   1.488 +	count=0;
   1.489 +	do
   1.490 +		{
   1.491 +		ep=wreaddir(dp);
   1.492 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.493 +			count++;
   1.494 +		}
   1.495 +	while (ep!=0);
   1.496 +	test(count>0);
   1.497 +
   1.498 +	wclosedir(dp);
   1.499 +	}
   1.500 +
   1.501 +/**
   1.502 + Directory tree is now
   1.503 + 
   1.504 +     top / topfile
   1.505 +           middle2 / extrafile  
   1.506 +           middle1 / bottom1 / absfile
   1.507 +                   / bottom2 / file                -- read-only
   1.508 + 		   / read-only / sub-read-only /
   1.509 +                                 file
   1.510 +
   1.511 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1086
   1.512 +@SYMTestCaseDesc	    Tests for file attributes
   1.513 +@SYMTestPriority 	    High
   1.514 +@SYMTestActions  	    Tests the attributes on files and directories
   1.515 +@SYMTestExpectedResults Test must not fail
   1.516 +@SYMREQ                 REQ0000
   1.517 +*/		
   1.518 +void attributes()
   1.519 +	{
   1.520 +	int err;
   1.521 +	struct stat s1,s2;
   1.522 +	int fd;
   1.523 +	double diff;
   1.524 +
   1.525 +	test_Next("File Attributes");
   1.526 +
   1.527 +	err=wchdir(rootpath);
   1.528 +	test(err==0);
   1.529 +
   1.530 +	err=wstat((wchar_t*)L"middle earth/bag end/hobbit",&s1);
   1.531 +	test_errno(err<0,ENOENT);
   1.532 +
   1.533 +	err=wstat((wchar_t*)L"middle1/bottom2/file",&s1);
   1.534 +	test(err==0);
   1.535 +	test(S_ISREG(s1.st_mode)!=0);
   1.536 +	test(S_ISDIR(s1.st_mode)==0);
   1.537 +	test((s1.st_mode&S_IWUSR)==0);
   1.538 +	test(s1.st_size==0);
   1.539 +
   1.540 +	err=wstat((wchar_t*)L"topfile",&s1);
   1.541 +	test(err==0);
   1.542 +	test(S_ISREG(s1.st_mode)!=0);
   1.543 +	test(S_ISDIR(s1.st_mode)==0);
   1.544 +	test((s1.st_mode&S_IWUSR)!=0);
   1.545 +	test(s1.st_size==0);
   1.546 +
   1.547 +	err=wstat((wchar_t*)L"topfile",&s2);
   1.548 +	test(err==0);
   1.549 +	test(s1.st_mode==s2.st_mode);
   1.550 +	test(s1.st_size==s2.st_size);
   1.551 +	diff=difftime(s1.st_mtime,s2.st_mtime);
   1.552 +	test(diff==(double)0.0);
   1.553 +
   1.554 +	fd=wopen((wchar_t*)L"topfile", O_RDONLY, 0);
   1.555 +	test(fd>=0);
   1.556 +
   1.557 +	err=fstat(fd,&s2);
   1.558 +	test(err==0);
   1.559 +	test(s1.st_mode==s2.st_mode);
   1.560 +	test(s1.st_size==s2.st_size);
   1.561 +	diff=difftime(s1.st_mtime,s2.st_mtime);
   1.562 +	test(diff==(double)0.0);
   1.563 +
   1.564 +	err=wstat((wchar_t*)L"topfile",&s2);
   1.565 +	test(err==0);
   1.566 +	test(s1.st_mode==s2.st_mode);
   1.567 +	test(s1.st_size==s2.st_size);
   1.568 +	diff=difftime(s1.st_mtime,s2.st_mtime);
   1.569 +	test(diff==(double)0.0);
   1.570 +
   1.571 +	err=close(fd);
   1.572 +	test(err==0);
   1.573 +
   1.574 +	sleep(1);	/* to ensure that the modify time changes */
   1.575 +
   1.576 +	fd=wopen((wchar_t*)L"topfile", O_RDWR+O_APPEND, 0);
   1.577 +	test(fd>=0);
   1.578 +
   1.579 +	err=wstat((wchar_t*)L"topfile",&s2);
   1.580 +	test(err==0);
   1.581 +	test(s1.st_mode==s2.st_mode);
   1.582 +	test(s1.st_size==s2.st_size);
   1.583 +	/* probably not guaranteeed to have changed the modtime at this point */
   1.584 +
   1.585 +	
   1.586 +		{
   1.587 +		char temp[MAXPATHLEN+1];
   1.588 +		wcstombs(temp,rootpath,MAXPATHLEN);
   1.589 +		err=write(fd,temp,3);
   1.590 +		test(err==3);
   1.591 +		}
   1.592 +
   1.593 +	err=fsync(fd);
   1.594 +	test(err==0);
   1.595 +
   1.596 +	err=close(fd);
   1.597 +	test(err==0);
   1.598 +	
   1.599 +	// this needs a delay here with EKA2
   1.600 +	// this is a slightly dodgy way of getting one
   1.601 +	err=wstat((wchar_t*)L"topfile",&s1);
   1.602 +	err=wstat((wchar_t*)L"topfile",&s2);
   1.603 +	err=wstat((wchar_t*)L"topfile",&s1);
   1.604 +
   1.605 +	err=wstat((wchar_t*)L"topfile",&s2);
   1.606 +	test(err==0);
   1.607 +	test(s1.st_mode==s2.st_mode);
   1.608 +	test(s2.st_size==3);
   1.609 +	diff=difftime(s2.st_mtime,s1.st_mtime);
   1.610 +	test(diff>=(double)0.0);
   1.611 +
   1.612 +	test_Next("Directory Attributes");
   1.613 +
   1.614 +	err=wstat((wchar_t*)L"middle1",&s1);
   1.615 +	test(err==0);
   1.616 +	test(S_ISREG(s1.st_mode)==0);
   1.617 +	test(S_ISDIR(s1.st_mode)==1);
   1.618 +	test((s1.st_mode&S_IWUSR)!=0);
   1.619 +
   1.620 +	err=wstat((wchar_t*)L"middle1/read-only",&s1);
   1.621 +	test(err==0);
   1.622 +	test(S_ISREG(s1.st_mode)==0);
   1.623 +	test(S_ISDIR(s1.st_mode)==1);
   1.624 +	test((s1.st_mode&S_IWUSR)==0);
   1.625 +
   1.626 +	err=wstat((wchar_t*)L"/",&s1);
   1.627 +	test(err==0);
   1.628 +	test(S_ISREG(s1.st_mode)==0);
   1.629 +	test(S_ISDIR(s1.st_mode)==1);
   1.630 +
   1.631 +	err=waccess((wchar_t*)L"middle1/bottom1/absfile",W_OK);
   1.632 +	test(err==0);
   1.633 +
   1.634 +	err=waccess((wchar_t*)L"middle1/bottom1/absfile",R_OK);
   1.635 +	test(err==0);
   1.636 +
   1.637 +	err=waccess((wchar_t*)L"middle1/bottom2/file",W_OK);
   1.638 +	test(err!=0);
   1.639 +
   1.640 +	err=waccess((wchar_t*)L"middle1/bottom2/file",R_OK);
   1.641 +	test(err==0);
   1.642 +
   1.643 +	err=waccess((wchar_t*)L"middle1/read-only",W_OK);
   1.644 +	test(err==0);
   1.645 +
   1.646 +	err=waccess((wchar_t*)L"middle1/read-only",R_OK);
   1.647 +	test(err==0);
   1.648 +
   1.649 +	err=waccess((wchar_t*)L"middle1/no such directory",R_OK);
   1.650 +	test(err!=0);
   1.651 +}
   1.652 +
   1.653 +/**
   1.654 +    Directory tree is now
   1.655 +  
   1.656 +     top / topfile
   1.657 +           middle2 / extrafile  
   1.658 +           middle1 / bottom1 / absfile
   1.659 +                   / bottom2 / file                -- read-only
   1.660 + 		   / read-only / sub-read-only /
   1.661 +                                 file
   1.662 +
   1.663 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1087
   1.664 +@SYMTestCaseDesc	    Tests for searching on different drives
   1.665 +@SYMTestPriority 	    High
   1.666 +@SYMTestActions  	    Tests by searching on z drive,test for the error codes 
   1.667 +@SYMTestExpectedResults Test must not fail
   1.668 +@SYMREQ                 REQ0000
   1.669 +*/		
   1.670 + void searching()
   1.671 +	{
   1.672 +	int err,fd;
   1.673 +	wchar_t wname[MAXPATHLEN+1];
   1.674 +	char name[MAXPATHLEN+1];
   1.675 +	char narrowroot[MAXPATHLEN+1];
   1.676 +
   1.677 +	test_Next("Searching across drives");
   1.678 +
   1.679 +	//ho hum, no wsprintf yet
   1.680 +	wcstombs(narrowroot, rootpath, MAXPATHLEN);
   1.681 +	sprintf(name,"%s/middle2/extrafile",narrowroot);
   1.682 +	test(-1!=mbstowcs(wname, name, MAXPATHLEN));
   1.683 +
   1.684 +	err=wchdir((wchar_t*)L"z:/");
   1.685 +	test(err==0);
   1.686 +
   1.687 +	fd=wopen(wname+2, O_RDONLY, 0);
   1.688 +	test_errno(fd<0,ENOENT);	// doesn't exist on z:
   1.689 +
   1.690 +	wname[0]=L'?';
   1.691 +	fd=wopen(wname, O_RDWR, 0);
   1.692 +	test(fd>=0);			// found it on the original drive
   1.693 +
   1.694 +	err=close(fd);
   1.695 +	test(err==0);
   1.696 +	}
   1.697 +
   1.698 +/**
   1.699 +   Directory tree is now
   1.700 + 
   1.701 +      top / topfile
   1.702 +           middle2 / extrafile  
   1.703 +           middle1 / bottom1 / absfile
   1.704 +                   / bottom2 / file                -- read-only
   1.705 + 		   / read-only / sub-read-only /
   1.706 +                                 file
   1.707 + 
   1.708 +
   1.709 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1088
   1.710 +@SYMTestCaseDesc	    Tests for deleting files
   1.711 +@SYMTestPriority 	    High
   1.712 +@SYMTestActions  	    Tests by deleting files and directories.Tests for error codes
   1.713 +@SYMTestExpectedResults Test must not fail
   1.714 +@SYMREQ                 REQ0000
   1.715 +*/		
   1.716 +void deletion()
   1.717 +	{
   1.718 +	int err;
   1.719 +	wchar_t widename[] = WIDENAME;
   1.720 +
   1.721 +	test_Next("Deleting - files");
   1.722 +
   1.723 +	err=wchdir(rootpath);
   1.724 +	test(err==0);
   1.725 +
   1.726 +	err=wunlink((wchar_t*)L"middle1/bottom2/file");
   1.727 +	test_errno(err<0,EACCES);	/* file is read-only */
   1.728 +
   1.729 +	err=wchmod((wchar_t*)L"middle1/bottom2/file",0777);
   1.730 +	test(err==0);
   1.731 +
   1.732 +	err=wunlink((wchar_t*)L"middle1/bottom2/file");
   1.733 +	test(err==0);
   1.734 +
   1.735 +	err=wunlink((wchar_t*)L"middle2/extrafile");
   1.736 +	test(err==0);
   1.737 +
   1.738 +	err=wunlink((wchar_t*)L"middle1/read-only/file");
   1.739 +	/* test_errno(err<0,EPERM);	parent directory is read-only */
   1.740 +	test(err==0);	/* Omission - EPOC32 uses Win32 semantics for read-only directories */
   1.741 +
   1.742 +	test_Next("Deleting - directories");
   1.743 +
   1.744 +	err=wchdir(rootpath);
   1.745 +	test(err==0);
   1.746 +
   1.747 +	err=wrmdir(widename);
   1.748 +	test(err==0);
   1.749 +
   1.750 +	err=wrmdir((wchar_t*)L"middle1");
   1.751 +	test_errno(err<0,EEXIST);	/* not empty */
   1.752 +
   1.753 +	err=wrmdir((wchar_t*)L"middle1/bottom1");
   1.754 +	test_errno(err<0,EEXIST);	/* not empty */
   1.755 +
   1.756 +	err=wunlink((wchar_t*)L"middle1/bottom1/absfile");
   1.757 +	test(err==0);
   1.758 +
   1.759 +	err=wrmdir((wchar_t*)L"middle1/bottom1");
   1.760 +	test(err==0);
   1.761 +
   1.762 +	err=wrmdir((wchar_t*)L"middle1/bottom1");
   1.763 +	test_errno(err<0,ENOENT);	/* already deleted */
   1.764 +
   1.765 +	err=wrmdir((wchar_t*)L"middle1");
   1.766 +	test_errno(err<0,EEXIST);
   1.767 +
   1.768 +	err=wrmdir((wchar_t*)L"middle1/bottom2");
   1.769 +	test(err==0);
   1.770 +
   1.771 +	test_Next("Deleting - read-only directories");
   1.772 +
   1.773 +	err=wrmdir((wchar_t*)L"middle1/read-only/sub-read-only");
   1.774 +	/* test_errno(err!=0,EACCES);	-- permission denied - read-only parent */
   1.775 +	test_errno(err<0,EACCES);	/* Omission - EPOC32 uses Win32 semantics */
   1.776 +
   1.777 +	err=wchmod((wchar_t*)L"middle1/read-only",0777);
   1.778 +	test(err==0);
   1.779 +
   1.780 +	err=wrmdir((wchar_t*)L"middle1/read-only/sub-read-only");
   1.781 +	/* test(err==0); */
   1.782 +	/* EPOC32 doesn't use the writeability of the parent directory, but instead looks 
   1.783 +	 * at the attributes of the directory itself.
   1.784 +	 */
   1.785 +	test_errno(err!=0,EACCES);
   1.786 +
   1.787 +	err=wchmod((wchar_t*)L"middle1/read-only/sub-read-only",0777);
   1.788 +	test(err==0);
   1.789 +
   1.790 +	err=wrmdir((wchar_t*)L"middle1/read-only/sub-read-only");
   1.791 +	test(err==0);
   1.792 +
   1.793 +	err=wrmdir((wchar_t*)L"middle1/read-only");
   1.794 +	test(err==0);
   1.795 +
   1.796 +	err=wrmdir((wchar_t*)L"middle?");
   1.797 +	test_errno(err<0,EINVAL);	/* no wild cards please */
   1.798 +
   1.799 +	err=wrmdir((wchar_t*)L"middle1");
   1.800 +	test(err==0);
   1.801 +
   1.802 +	err=wrmdir((wchar_t*)L"../top/middle2");
   1.803 +	test(err==0);
   1.804 +
   1.805 +	err=wrmdir((wchar_t*)L".");
   1.806 +	test_errno(err<0,EEXIST);	/* not empty */
   1.807 +
   1.808 +	err=wunlink((wchar_t*)L"topfile");
   1.809 +	test(err==0);
   1.810 +
   1.811 +	err=wrmdir((wchar_t*)L".");
   1.812 +	test(err==0);
   1.813 +	}
   1.814 +
   1.815 +/**
   1.816 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1089
   1.817 +@SYMTestCaseDesc	    Tests for creation of temporary directory and files in it.
   1.818 +@SYMTestPriority 	    High
   1.819 +@SYMTestActions  	    Tests by creating a temporary director,and files and writing to the files.
   1.820 +                        Check for error codes.
   1.821 +@SYMTestExpectedResults Test must not fail
   1.822 +@SYMREQ                 REQ0000
   1.823 +*/
   1.824 +void temporary_files()
   1.825 +	{
   1.826 +	int err, count1, count2;
   1.827 +	WDIR *dp;
   1.828 +	struct wdirent *ep;
   1.829 +	FILE *fp;
   1.830 +	wchar_t name[L_tmpnam];
   1.831 +	wchar_t name2[L_tmpnam];
   1.832 +	char nname[L_tmpnam];
   1.833 +	char nname2[L_tmpnam];
   1.834 +	wchar_t *p;
   1.835 +
   1.836 +	test_Next("Temporary files");
   1.837 +
   1.838 +#define W_tmpdir   L"C:/system/temp/"
   1.839 +#define W_tmpfirstdir   L"C:/system/"
   1.840 +
   1.841 +	dp=wopendir((wchar_t*)W_tmpdir);
   1.842 +	if (dp==0)
   1.843 +		{
   1.844 +		printf("  Creating the directory %S ...\n", W_tmpdir);
   1.845 +		err=wmkdir((wchar_t*)W_tmpfirstdir, 0777);
   1.846 +		err=wmkdir((wchar_t*)W_tmpdir, 0777);
   1.847 + 		test(err==0);
   1.848 +		dp=wopendir((wchar_t*)W_tmpdir);
   1.849 +		}
   1.850 +	test(dp!=0);
   1.851 +
   1.852 +	count1=0;
   1.853 +	do
   1.854 +		{
   1.855 +		ep=wreaddir(dp);
   1.856 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.857 +			count1++;
   1.858 +		}
   1.859 +	while (ep!=0);
   1.860 +
   1.861 +	fp=tmpfile();
   1.862 +	test(fp!=0);
   1.863 +
   1.864 +	err=fprintf(fp,"hello");
   1.865 +	test(err==5);
   1.866 +
   1.867 +	wrewinddir(dp);
   1.868 +	count2=0;
   1.869 +	do
   1.870 +		{
   1.871 +		ep=wreaddir(dp);
   1.872 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.873 +			count2++;
   1.874 +		}
   1.875 +	while (ep!=0);
   1.876 +	test(count2==count1+1);	/* EPOC32 temporary files are visible in file system */
   1.877 +	err=fclose(fp);
   1.878 +	test(err==0);
   1.879 +
   1.880 +	wrewinddir(dp);
   1.881 +	count2=0;
   1.882 +	do
   1.883 +		{
   1.884 +		ep=wreaddir(dp);
   1.885 +		if (ep && wcscmp(ep->d_name,(wchar_t*)L".")!=0 && wcscmp(ep->d_name,(wchar_t*)L"..")!=0)
   1.886 +			count2++;
   1.887 +		}
   1.888 +	while (ep!=0);
   1.889 +	test(count2==count1);		/* should be automatically deleted */
   1.890 +
   1.891 +	wclosedir(dp);
   1.892 +
   1.893 +	p=wtmpnam(NULL);
   1.894 +	test(p!=0);
   1.895 +
   1.896 +	count1=wcslen(p);
   1.897 +	test(count1<L_tmpnam);
   1.898 +
   1.899 +	p=wtmpnam(name);
   1.900 +	test(p==name);
   1.901 +
   1.902 +	fp=wfopen(name,(wchar_t*)L"wb+");
   1.903 +	test(fp!=0);
   1.904 +
   1.905 +	p=wtmpnam(name2);
   1.906 +	test(p==name2);
   1.907 +
   1.908 +	err=wcscmp(name,name2);
   1.909 +	test(err!=0);
   1.910 +
   1.911 +	err=fclose(fp);
   1.912 +	test(err==0);
   1.913 +
   1.914 +	err=wunlink(name);
   1.915 +	test(err==0);
   1.916 +
   1.917 +	err=wcstombs(nname,name,50);
   1.918 +	test (err != -1);
   1.919 +	err=wcstombs(nname2,name2,50);
   1.920 +	test (err != -1);
   1.921 +	printf("  wtmpnam suggested %s and %s\n", nname, nname2);
   1.922 +	}
   1.923 +
   1.924 +int close_console=0;
   1.925 +void allTests()
   1.926 +	{
   1.927 +	int err=chdir("C:\\");
   1.928 +	test(err==0);
   1.929 +
   1.930 +	make_tree();
   1.931 +	create_files();
   1.932 +	renaming();
   1.933 +	directory();
   1.934 +	attributes();
   1.935 +	searching();
   1.936 +	deletion();
   1.937 +	temporary_files();
   1.938 +
   1.939 +	if (close_console)
   1.940 +		{
   1.941 +		test_Close();
   1.942 +		close(0);
   1.943 +		close(1);
   1.944 +		close(2);
   1.945 +		}
   1.946 +	}
   1.947 +
   1.948 +int main()
   1.949 +	{
   1.950 +	void* client;
   1.951 +	int err;
   1.952 +
   1.953 +	test_Title("Directory Handling");
   1.954 +
   1.955 +	allTests();
   1.956 +
   1.957 +	test_Next("Do it again using the CPosixServer (for them, not me)");
   1.958 +	close_console=1;
   1.959 +
   1.960 +	start_posix_server();	/* calls SpawnPosixServer from C++ code */
   1.961 +
   1.962 +	client=create_thread(allTests, "TDIRS tests");
   1.963 +	test(client!=0);
   1.964 +	start_thread(client);
   1.965 +	err=wait_for_thread(client);
   1.966 +	test(err==0);
   1.967 +
   1.968 +	test_Close();
   1.969 +	return 0;
   1.970 +	}