os/kernelhwsrv/kerneltest/f32test/server/t_chkuid.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // f32test\server\t_chkuid.cpp
    15 //
    16 //
    17 
    18 #include <f32file.h>
    19 #include <e32test.h>
    20 #include "t_server.h"
    21 #include "t_chlffs.h"
    22 
    23 #if defined(__WINS__)
    24 #define WIN32_LEAN_AND_MEAN
    25 #pragma warning (disable:4201) // warning C4201: nonstandard extension used : nameless struct/union
    26 #pragma warning (default:4201) // warning C4201: nonstandard extension used : nameless struct/union
    27 #endif
    28 
    29 GLDEF_D RTest test(_L("T_CHKUID"));
    30 
    31 
    32 LOCAL_C void CreateUidTestFiles()
    33 //
    34 // Create files with uids for testing
    35 //
    36 	{
    37     // Create \\gSessionPath\\UIDCHKNO.SHT - no uid, zero length
    38 	RFile file;
    39 	TInt r=file.Replace(TheFs,_L("UIDCHKNO.SHT"),EFileRead|EFileWrite);
    40 	test(r==KErrNone);
    41 	file.Close();
    42 
    43     // Create \\gSessionPath\\UIDCHKNO.LNG - no uid, long length
    44 	r=file.Replace(TheFs,_L("UIDCHKNO.LNG"),EFileRead|EFileWrite);
    45 	test(r==KErrNone);
    46 	r=file.Write(_L8("Hello World needs to be over 16 bytes"));
    47 	file.Close();
    48 
    49     // Create \\gSessionPath\\UIDCHK.BLG - with uid no data
    50 	r=file.Replace(TheFs,_L("UIDCHK.BLG"),EFileRead|EFileWrite);
    51 	test(r==KErrNone);
    52 	TUidType uidType(TUid::Uid('U'),TUid::Uid('I'),TUid::Uid('D'));
    53 	TCheckedUid checkedUid(uidType);
    54 	TPtrC8 buf((TUint8*)&checkedUid,sizeof(TCheckedUid));
    55 	r=file.Write(buf);
    56 	test(r==KErrNone);
    57 	file.Close();
    58 
    59     // Create \\gSessionPath\\UIDCHK.MSG - with uid and data
    60 	r=file.Replace(TheFs,_L("UIDCHK.MSG"),EFileRead|EFileWrite);
    61 	test(r==KErrNone);
    62 	TUidType uidType2(TUid::Uid('X'),TUid::Uid('Y'),TUid::Uid('Z'));
    63 	checkedUid.Set(uidType2);
    64 	buf.Set((TUint8*)&checkedUid,sizeof(TCheckedUid));
    65 	r=file.Write(buf);
    66 	test(r==KErrNone);
    67 	r=file.Write(_L8("More file data"));
    68 	test(r==KErrNone);
    69 	file.Close();
    70 
    71     // Create \\gSessionPath\\UIDCHK.DAT - uid stored only in the file
    72 	r=file.Replace(TheFs,_L("UIDCHK.DAT"),EFileRead|EFileWrite);
    73 	test(r==KErrNone);
    74 	TUidType uidType3(TUid::Uid('D'),TUid::Uid('A'),TUid::Uid('T'));
    75 	checkedUid.Set(uidType3);
    76 	buf.Set((TUint8*)&checkedUid,sizeof(TCheckedUid));
    77 	r=file.Write(buf);
    78 	test(r==KErrNone);
    79 	r=file.Write(_L8("More file data"));
    80 	test(r==KErrNone);
    81 	file.Close();
    82 
    83     // Create \\gSessionPath\\UIDCHK.PE - uid stored in WINS PE file header
    84 	r=file.Replace(TheFs,_L("UIDWINS.PE"),EFileRead|EFileWrite);
    85 	test(r==KErrNone);
    86 
    87 #if defined(__WINS__)
    88     if (!IsTestingLFFS())
    89         {
    90 	    RFile fileSource;
    91 	    r=fileSource.Open(TheFs,_L("Z:\\TEST\\T_CHKUID.EXE"),EFileShareReadersOnly|EFileRead);
    92 	    test(r==KErrNone);
    93 
    94 	    TBuf8<0x100> buffer;
    95 	    do
    96 		    {
    97 		    r=fileSource.Read(buffer);
    98 		    test(r==KErrNone);
    99 		    r=file.Write(buffer);
   100 		    test(r==KErrNone);
   101 		    }
   102 	    while (buffer.Length()==buffer.MaxLength());
   103 
   104 	    fileSource.Close();
   105         }
   106     else
   107         {
   108 	    r=file.Write(_L8("Some zany stuff here!"));
   109 	    test(r==KErrNone);
   110         }
   111 #else
   112 	r=file.Write(_L8("Some zany stuff here!"));
   113 	test(r==KErrNone);
   114 #endif
   115 	file.Close();
   116 	}
   117 
   118 LOCAL_C void Test1()
   119 //
   120 // Test GetDir
   121 //
   122 	{
   123 
   124 	test.Next(_L("Use GetDir to check files"));
   125 	CDir* dum=NULL;
   126 	TInt r=TheFs.GetDir(_L("UID*"),KEntryAttAllowUid,ESortByName,dum);
   127 	CDir& dir=*dum;
   128 	test(r==KErrNone);
   129 	TInt count=dir.Count();
   130 	test(count==6);
   131 
   132 	TEntry entry=dir[0];
   133 	test(entry.iName==_L("UIDCHK.BLG"));
   134 	test(entry.IsTypeValid());
   135 	test(entry.iType[0]==TUid::Uid('U') && entry.iType[1]==TUid::Uid('I') && entry.iType[2]==TUid::Uid('D'));
   136 
   137 	entry=dir[1];
   138 	test(entry.iName==_L("UIDCHK.DAT"));
   139 	test(entry.IsTypeValid());
   140 	test(entry.iType[0]==TUid::Uid('D') && entry.iType[1]==TUid::Uid('A') && entry.iType[2]==TUid::Uid('T'));
   141 
   142 	entry=dir[2];
   143 	test(entry.iName==_L("UIDCHK.MSG"));
   144 	test(entry.IsTypeValid());
   145 	test(entry.iType[0]==TUid::Uid('X') && entry.iType[1]==TUid::Uid('Y') && entry.iType[2]==TUid::Uid('Z'));
   146 
   147 	entry=dir[3];
   148 	test(entry.iName==_L("UIDCHKNO.LNG"));
   149 	test(entry.IsTypeValid()==EFalse);
   150 
   151 	entry=dir[4];
   152 	test(entry.iName==_L("UIDCHKNO.SHT"));
   153 	test(entry.IsTypeValid()==EFalse);
   154 
   155 	entry=dir[5];
   156 	test(entry.iName==_L("UIDWINS.PE"));
   157 #if defined(__WINS__)
   158 	TFileName sessionPath;
   159 	TheFs.SessionPath(sessionPath);
   160 	if (sessionPath[0]!='C')
   161 		test(entry.IsTypeValid()==EFalse);
   162 	else
   163 		{
   164 		test(entry.IsTypeValid());
   165 		test(entry.iType[0]==TUid::Uid(0x1000007a) && entry.iType[1]==TUid::Uid(2) && entry.iType[2]==TUid::Uid(3));
   166 		}
   167 #else
   168 	test(entry.IsTypeValid()==EFalse);
   169 #endif
   170 	delete dum;
   171 	}
   172 
   173 LOCAL_C void Test2()
   174 //
   175 // Test GetDir
   176 //
   177 	{
   178 
   179 	test.Next(_L("Test KEntryAttAllowUid allows uids"));
   180 	CDir* dum=NULL;
   181 	TInt r=TheFs.GetDir(_L("UID*"),0,ESortByName,dum);
   182 	CDir& dir=*dum;
   183 	test(r==KErrNone);
   184 	TInt count=dir.Count();
   185 	test(count==6);
   186 
   187 	TEntry entry=dir[0];
   188 	test(entry.iName==_L("UIDCHK.BLG"));
   189 	test(!entry.IsTypeValid());
   190 
   191 	entry=dir[1];
   192 	test(entry.iName==_L("UIDCHK.DAT"));
   193 	test(!entry.IsTypeValid());
   194 
   195 	entry=dir[2];
   196 	test(entry.iName==_L("UIDCHK.MSG"));
   197 	test(!entry.IsTypeValid());
   198 
   199 	entry=dir[3];
   200 	test(entry.iName==_L("UIDCHKNO.LNG"));
   201 	test(entry.IsTypeValid()==EFalse);
   202 
   203 	entry=dir[4];
   204 	test(entry.iName==_L("UIDCHKNO.SHT"));
   205 	test(entry.IsTypeValid()==EFalse);
   206 
   207 	entry=dir[5];
   208 	test(entry.iName==_L("UIDWINS.PE"));
   209 	test(entry.IsTypeValid()==EFalse);
   210 	delete dum;
   211 	}
   212 
   213 LOCAL_C void Test3()
   214 //
   215 // Test RFs::Entry()
   216 //
   217 	{
   218 
   219 	test.Next(_L("Use RFs::EntryL() to check files"));
   220 	TEntry entry;
   221 	TInt r=TheFs.Entry(_L("UIDCHKNO.SHT"),entry);
   222 	test(r==KErrNone);
   223 	test(entry.iName==_L("UIDCHKNO.SHT"));
   224 	test(entry.IsTypeValid()==EFalse);
   225 
   226 	r=TheFs.Entry(_L("UIDCHKNO.LNG"),entry);
   227 	test(r==KErrNone);
   228 	test(entry.iName==_L("UIDCHKNO.LNG"));
   229 	test(entry.IsTypeValid()==EFalse);
   230 
   231 	r=TheFs.Entry(_L("UIDCHK.MSG"),entry);
   232 	test(r==KErrNone);
   233 	test(entry.iName==_L("UIDCHK.MSG"));
   234 	test(entry.IsTypeValid());
   235 	test(entry.iType[0]==TUid::Uid('X') && entry.iType[1]==TUid::Uid('Y') && entry.iType[2]==TUid::Uid('Z'));
   236 
   237 	r=TheFs.Entry(_L("UIDCHK.BLG"),entry);
   238 	test(r==KErrNone);
   239 	test(entry.iName==_L("UIDCHK.BLG"));
   240 	test(entry.IsTypeValid());
   241 	test(entry.iType[0]==TUid::Uid('U') && entry.iType[1]==TUid::Uid('I') && entry.iType[2]==TUid::Uid('D'));
   242 
   243 	r=TheFs.Entry(_L("UIDCHK.DAT"),entry);
   244 	test(r==KErrNone);
   245 	test(entry.iName==_L("UIDCHK.DAT"));
   246 	test(entry.IsTypeValid());
   247 	test(entry.iType[0]==TUid::Uid('D') && entry.iType[1]==TUid::Uid('A') && entry.iType[2]==TUid::Uid('T'));
   248 
   249 	r=TheFs.Entry(_L("UIDWINS.PE"),entry);
   250 	test(r==KErrNone);
   251 	test(entry.iName==_L("UIDWINS.PE"));
   252 #if defined(__WINS__)
   253 	TFileName sessionPath;
   254 	TheFs.SessionPath(sessionPath);
   255 	if (sessionPath[0]!='C')
   256 		test(entry.IsTypeValid()==EFalse);
   257 	else
   258 		{
   259 		test(entry.IsTypeValid());
   260 		test(entry.iType[0]==TUid::Uid(0x1000007a) && entry.iType[1]==TUid::Uid(2) && entry.iType[2]==TUid::Uid(3));
   261 		}
   262 #else
   263 	test(entry.IsTypeValid()==EFalse);
   264 #endif
   265 	}
   266 
   267 LOCAL_C void Test4()
   268 //
   269 // Test uid's can be read when the file is open
   270 //
   271 //	EFileShareExclusive,EFileShareReadersOnly,EFileShareAny,
   272 //	EFileStream=0,EFileStreamText=0x100,
   273 //	EFileRead=0,EFileWrite=0x200
   274 //
   275 	{
   276 
   277 	test.Next(_L("Uids can be read if the file is open"));
   278 	RFile f;
   279 	TEntry entry;
   280 	TInt r=f.Open(TheFs,_L("UIDCHK.DAT"),EFileShareExclusive|EFileRead);
   281 	test(r==KErrNone);
   282 	r=TheFs.Entry(_L("UIDCHK.DAT"),entry);
   283 	test(r==KErrNone);
   284 	test(entry.iName==_L("UIDCHK.DAT"));
   285 	test(entry.IsTypeValid());
   286 	test(entry.iType[0]==TUid::Uid('D') && entry.iType[1]==TUid::Uid('A') && entry.iType[2]==TUid::Uid('T'));
   287 	f.Close();
   288 
   289 	r=f.Open(TheFs,_L("UIDCHK.DAT"),EFileShareExclusive|EFileWrite);
   290 	test(r==KErrNone);
   291 	r=TheFs.Entry(_L("UIDCHK.DAT"),entry);
   292 	test(r==KErrNone);
   293 	test(entry.iName==_L("UIDCHK.DAT"));
   294 	test(entry.IsTypeValid());
   295 	test(entry.iType[0]==TUid::Uid('D') && entry.iType[1]==TUid::Uid('A') && entry.iType[2]==TUid::Uid('T'));
   296 
   297 	r=f.SetSize(256);
   298 	test(r==KErrNone);
   299 	TBuf8<16> des;
   300 	r=TheFs.ReadFileSection(_L("UIDCHK.DAT"),0,des,16);
   301 	test(r==KErrNone);
   302 
   303 	f.Close();
   304 
   305 	r=f.Open(TheFs,_L("UIDCHK.DAT"),EFileShareReadersOnly|EFileRead);
   306 	test(r==KErrNone);
   307 	r=TheFs.Entry(_L("UIDCHK.DAT"),entry);
   308 	test(r==KErrNone);
   309 	test(entry.iName==_L("UIDCHK.DAT"));
   310 	test(entry.IsTypeValid());
   311 	test(entry.iType[0]==TUid::Uid('D') && entry.iType[1]==TUid::Uid('A') && entry.iType[2]==TUid::Uid('T'));
   312 	f.Close();
   313 
   314 //	EFileShareReadersOnly|EFileWrite is illegal
   315 
   316 	r=f.Open(TheFs,_L("UIDCHK.DAT"),EFileShareAny|EFileRead);
   317 	test(r==KErrNone);
   318 	r=TheFs.Entry(_L("UIDCHK.DAT"),entry);
   319 	test(r==KErrNone);
   320 	test(entry.iName==_L("UIDCHK.DAT"));
   321 	test(entry.IsTypeValid());
   322 	test(entry.iType[0]==TUid::Uid('D') && entry.iType[1]==TUid::Uid('A') && entry.iType[2]==TUid::Uid('T'));
   323 	f.Close();
   324 
   325 	r=f.Open(TheFs,_L("UIDCHK.DAT"),EFileShareAny|EFileWrite);
   326 	test(r==KErrNone);
   327 
   328 	RFile secondFile;
   329 	r=secondFile.Open(TheFs,_L("UIDCHK.DAT"),EFileShareAny|EFileWrite);
   330 	test(r==KErrNone);
   331 
   332 	RFile thirdFile;
   333 	r=thirdFile.Open(TheFs,_L("UIDCHK.DAT"),EFileShareAny|EFileRead);
   334 	test(r==KErrNone);
   335 
   336 	r=TheFs.Entry(_L("UIDCHK.DAT"),entry);
   337 	test(r==KErrNone);
   338 	test(entry.iName==_L("UIDCHK.DAT"));
   339 	test(entry.IsTypeValid());
   340 	test(entry.iType[0]==TUid::Uid('D') && entry.iType[1]==TUid::Uid('A') && entry.iType[2]==TUid::Uid('T'));
   341 	f.Close();
   342 	secondFile.Close();
   343 	thirdFile.Close();
   344 
   345 	r=f.Open(TheFs,_L("UIDWINS.PE"),EFileShareAny|EFileWrite);
   346 	test(r==KErrNone);
   347 
   348 	r=TheFs.Entry(_L("UIDWINS.PE"),entry);
   349 	test(r==KErrNone);
   350 	test(entry.iName==_L("UIDWINS.PE"));
   351 #if defined(__WINS__)
   352 	TFileName sessionPath;
   353 	TheFs.SessionPath(sessionPath);
   354 	if (sessionPath[0]!='C')
   355 		test(entry.IsTypeValid()==EFalse);
   356 	else
   357 		{
   358 		test(entry.IsTypeValid());
   359 		test(entry.iType[0]==TUid::Uid(0x1000007a) && entry.iType[1]==TUid::Uid(2) && entry.iType[2]==TUid::Uid(3));
   360 		}
   361 #else
   362 	test(entry.IsTypeValid()==EFalse);
   363 #endif
   364 	f.Close();
   365 	}
   366 
   367 LOCAL_C void TestZ()
   368 //
   369 // Test Rom filesystem
   370 //
   371 	{
   372 
   373 	CDir* dum=NULL;
   374 
   375 	TInt r=TheFs.GetDir(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin)?_L("Z:\\Sys\\Bin\\*"):_L("Z:\\System\\Bin\\*"),KEntryAttAllowUid,0,dum);
   376 
   377 	if (r==KErrNotReady)
   378 		{
   379 		test.Printf(_L("Error: Unable to open Z:\n"));
   380 		return;
   381 		}
   382 	test(r==KErrNone);
   383 	CDir& dir=*dum;
   384 	TInt count=dir.Count();
   385 	if (count==0)
   386 		test.Printf(_L("No files present on Z:\\*\n"));
   387 	while (count--)
   388 		{
   389 		TBuf<32> UID;
   390 		if (dir[count].IsTypeValid()==EFalse)
   391 			UID=_L("INVALID");
   392 		else
   393 			{
   394 			UID=dir[count].iType[0].Name();
   395 			UID.Append(dir[count].iType[1].Name());
   396 			UID.Append(dir[count].iType[2].Name());
   397 			}
   398 		test.Printf(_L("FILE: %S UID %S\n"),&dir[count].iName,&UID);
   399 		}
   400 	delete &dir;
   401 	TEntry entry;
   402 	r=TheFs.Entry(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin)?_L("Z:\\Sys\\Bin\\ELOCAL.FSY"):_L("Z:\\System\\Bin\\ELOCAL.FSY"),entry);
   403 	}
   404 
   405 GLDEF_C void CallTestsL(void)
   406 //
   407 // Do all tests
   408 //
   409 	{
   410 
   411 	TBuf<64> b;
   412 
   413 	TFileName sessionPath;
   414 
   415 	TInt r=TheFs.SessionPath(sessionPath);
   416 	test(r==KErrNone);
   417 	TChar driveLetter=sessionPath[0];
   418 	b.Format(_L("Testing filesystem on %c:"),(TText)driveLetter);
   419 	test.Next(b);
   420 
   421 	CreateUidTestFiles();
   422 	Test1();
   423 	Test2();
   424 	Test3();
   425 	Test4();
   426 
   427 	test.Next(_L("Testing filesystem on Z:"));
   428 	TRAP(r,TestZ());
   429 	if (r!=KErrNone)
   430 		test.Printf(_L("Error: %d\n"),r);
   431 	}