kerneltest/f32test/server/t_rcount.cpp
changeset 0 a41df078684a
child 43 c1f20ce4abcf
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1996-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_rcount.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <e32test.h>
       
    20 
       
    21 #include "../server/t_server.h"
       
    22 
       
    23 GLDEF_D RTest test(_L("T_RCOUNT"));
       
    24 const TInt KHeapSize=0x2000;
       
    25 
       
    26 LOCAL_C void Test1()
       
    27 //
       
    28 // Test resource counting success
       
    29 //
       
    30 	{
       
    31 
       
    32 	test.Start(_L("Test resource count success"));
       
    33 	RFs TheFs;
       
    34 	TheFs.Connect();
       
    35 	RFile file;
       
    36 	TheFs.ResourceCountMarkStart();
       
    37 	TInt count=TheFs.ResourceCount();
       
    38 	test(count==0);
       
    39 	TFileName name=_L("Z:\\Test\\T_FILE.CPP");
       
    40 	name[0] = gExeFileName[0];
       
    41 	test.Printf(_L("%S\n"),&name);
       
    42 	TInt r=file.Open(TheFs,name,EFileRead);
       
    43 	test(r==KErrNone);
       
    44 	count=TheFs.ResourceCount();
       
    45 	test(count==1);
       
    46 	file.Close();
       
    47 	count=TheFs.ResourceCount();
       
    48 	test(count==0);
       
    49 	TheFs.ResourceCountMarkEnd();
       
    50 	TheFs.Close();
       
    51 	}
       
    52 
       
    53 LOCAL_C TInt TestPanic(TAny*)
       
    54 //
       
    55 // The Test thread
       
    56 //
       
    57 	{
       
    58 
       
    59 	User::SetJustInTime(EFalse);
       
    60 	RFs fs;
       
    61 	TInt r=fs.Connect();
       
    62 	test(r==KErrNone);
       
    63 	RFile file;
       
    64 	fs.ResourceCountMarkStart();
       
    65 	TFileName name=_L("Z:\\Test\\T_FILE.CPP");
       
    66 	name[0] = gExeFileName[0];
       
    67 
       
    68 	r=file.Open(fs,name,EFileRead);
       
    69 	test(r==KErrNone);
       
    70 	TInt count=fs.ResourceCount();
       
    71 	test(count==1);
       
    72 	fs.ResourceCountMarkEnd(); // MarkEnd without close
       
    73 	fs.Close();
       
    74 	return KErrNone;
       
    75 	}
       
    76 
       
    77 LOCAL_C void Test2()
       
    78 //
       
    79 // Test resource count failure
       
    80 //
       
    81 	{
       
    82 
       
    83 	test.Next(_L("Test resource count failure"));
       
    84 	TRequestStatus stat;	
       
    85 	RThread t;
       
    86 	TInt r=t.Create(_L("TestPanicThread"),TestPanic,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
       
    87 	test(r==KErrNone);
       
    88 	t.Logon(stat);
       
    89 	t.Resume();
       
    90 	User::WaitForRequest(stat);	
       
    91 	test(t.ExitReason()==CSession2::ESesFoundResCountHeaven);
       
    92 	test(t.ExitType()==EExitPanic);
       
    93 	CLOSE_AND_WAIT(t);
       
    94 	}
       
    95 
       
    96 
       
    97 _LIT(KDirName1, "c:\\f32-tst");
       
    98 _LIT(KFileName1, "c:\\crash001.txt");
       
    99 _LIT(KFileName2, "c:\\crash002.txt");
       
   100 
       
   101 LOCAL_C TInt SendBadHandle1(TAny *)
       
   102 //
       
   103 // Thread is panicked when server leaves when DoFsSubCloseL() calls CObjectIx::AtL().
       
   104 // Avoid case where thread is panicked by CObjectIx::Remove() because
       
   105 // ! (i<iHighWaterMark) closing the first file removes all of the CObjects.
       
   106 //
       
   107 	{
       
   108 	RFs fs;
       
   109 	test(fs.Connect() == KErrNone);
       
   110 
       
   111 	RFile f1;
       
   112 	test(f1.Replace(fs, KFileName1, EFileWrite) == KErrNone);
       
   113 	RFile fc(f1);
       
   114 
       
   115 	f1.Close();
       
   116 	fc.Close();									// file server panics thread here
       
   117 
       
   118 	test(EFalse);
       
   119 	fs.Close();
       
   120 	return KErrNone;							// shouldn't reach here
       
   121 	}
       
   122 
       
   123 
       
   124 LOCAL_C TInt SendBadHandle2(TAny *)
       
   125 //
       
   126 // Thread is panicked when server leaves when DoFsSubClose() calls CObjectIx::AtL().
       
   127 // Avoid case where thread is panicked by CObjectIx() because ! pR->obj, with handle
       
   128 // having been already removed.
       
   129 //
       
   130 	{
       
   131 	RFs fs;
       
   132 	test(fs.Connect() == KErrNone);
       
   133 
       
   134 	RFile f1;
       
   135 	test(f1.Replace(fs, KFileName1, EFileWrite) == KErrNone);
       
   136 	RFile f2;
       
   137 	test(f2.Replace(fs, KFileName2, EFileWrite) == KErrNone);
       
   138 
       
   139 	RFile fc(f1);
       
   140 	f1.Close();
       
   141 	fc.Close();									// file server panics thread here
       
   142 
       
   143 	test(EFalse);
       
   144 	fs.Close();
       
   145 	return KErrNone;							// shouldn't reach here
       
   146 	}
       
   147 
       
   148 
       
   149 LOCAL_C TInt SendBadHandle3(TAny*)
       
   150 //
       
   151 // Setup a bad RFile handle then try to write to the file, 
       
   152 // the thread should get paniced by the file server
       
   153 //
       
   154 	{
       
   155 	RFs f;
       
   156 	TInt r = f.Connect();
       
   157 	test(r==KErrNone);
       
   158 
       
   159 	RFile a;
       
   160 	r=a.Replace(f,KFileName1,EFileWrite);
       
   161 	test(r==KErrNone);
       
   162 	RFile b(a);
       
   163 	a.Close();
       
   164 	
       
   165 	r=b.Write(_L8("testing testing"));
       
   166 	
       
   167 	//thread should get paniced here so should not reach this
       
   168 	
       
   169 	test(EFalse);
       
   170 	f.Close();	
       
   171 
       
   172 	return KErrNone;
       
   173 	}
       
   174 
       
   175 
       
   176 LOCAL_C TInt SendBadHandle4(TAny*)
       
   177 //
       
   178 // Setup a bad RDir handle then try to read the Directory, 
       
   179 // the thread should get panniced by the server
       
   180 //
       
   181 	{
       
   182 	RFs f;
       
   183 	TInt r = f.Connect();
       
   184 	test(r==KErrNone);
       
   185 
       
   186 	RDir a;
       
   187 	r=a.Open(f,KDirName1,KEntryAttNormal);
       
   188 	test(r==KErrNone);
       
   189 	RDir b(a);
       
   190 	a.Close();
       
   191 	
       
   192 	TEntryArray dummyarray;
       
   193 	r=b.Read(dummyarray);
       
   194 	
       
   195 	//thread should get paniced here so should not reach this
       
   196 	
       
   197 	test(EFalse);
       
   198 	f.Close();	
       
   199 
       
   200 	return KErrNone;
       
   201 	}
       
   202 
       
   203 LOCAL_C TInt SendBadHandle5(TAny*)
       
   204 //
       
   205 // Setup a bad RFormat handle then try to call next on the Format object, 
       
   206 // the thread should get panniced by the server
       
   207 //
       
   208 	{
       
   209 
       
   210 #if defined(__EPOC32__)
       
   211 	_LIT(KDrive1, "C:\\");
       
   212 #else
       
   213 	_LIT(KDrive1, "X:\\");
       
   214 #endif
       
   215 
       
   216 	RFs f;
       
   217 	TInt r = f.Connect();
       
   218 	test(r==KErrNone);
       
   219 
       
   220 	RFormat a;
       
   221 	TInt count;
       
   222 	r=a.Open(f,KDrive1,EQuickFormat,count);
       
   223 	test(r==KErrNone);
       
   224 	RFormat b(a);
       
   225 	a.Close();
       
   226 	
       
   227 	r=b.Next(count);
       
   228 	
       
   229 	//thread should get paniced here so should not reach this
       
   230 	
       
   231 	test(EFalse);
       
   232 	f.Close();	
       
   233 
       
   234 	return KErrNone;
       
   235 	}
       
   236 
       
   237 
       
   238 LOCAL_C TInt SendBadHandle6(TAny*)
       
   239 //
       
   240 // Setup a bad RRawDisk handle then try to call read on the raw disk object, 
       
   241 // the thread should get panniced by the server
       
   242 //
       
   243 	{
       
   244 
       
   245 #if defined(__EPOC32__)
       
   246 	const TInt KDrive1 = EDriveC;
       
   247 #else
       
   248 	const TInt KDrive1 = EDriveX;
       
   249 #endif
       
   250 	
       
   251 	RFs f;
       
   252 	TInt r = f.Connect();
       
   253 	test(r==KErrNone);
       
   254 
       
   255 	RRawDisk a;
       
   256     r=a.Open(f,KDrive1);
       
   257 
       
   258 	test(r==KErrNone);
       
   259 	RRawDisk b(a);
       
   260 	a.Close();
       
   261 	TBuf8<19> buffer;
       
   262 	r=b.Read(0,buffer);
       
   263 	
       
   264 	//thread should get paniced here so should not reach this
       
   265 	
       
   266 	test(EFalse);
       
   267 	f.Close();	
       
   268 
       
   269 	return KErrNone;
       
   270 	}
       
   271 
       
   272 
       
   273 
       
   274 
       
   275 
       
   276 LOCAL_C void Test3()
       
   277 //
       
   278 //	Test how the file server copes with bad handles
       
   279 //
       
   280 	{
       
   281 	test.Start(_L("RunTests()"));
       
   282 
       
   283 	TRequestStatus rs;
       
   284 
       
   285 	RThread t1;
       
   286 	User::SetJustInTime(EFalse);
       
   287 	test(t1.Create(_L("Handle1Test"), SendBadHandle1, KDefaultStackSize, KHeapSize, KHeapSize, NULL) == KErrNone);
       
   288 	t1.Logon(rs);
       
   289 	t1.Resume();
       
   290 	User::WaitForRequest(rs);
       
   291 	User::SetJustInTime(ETrue);
       
   292 	test(t1.ExitType() == EExitPanic);
       
   293 	test(t1.ExitReason() == KErrBadHandle);
       
   294 	CLOSE_AND_WAIT(t1);
       
   295 
       
   296 	RThread t2;
       
   297 	User::SetJustInTime(EFalse);
       
   298 	test(t2.Create(_L("Handle2Test"), SendBadHandle2, KDefaultStackSize, KHeapSize, KHeapSize, NULL) == KErrNone);
       
   299 	t2.Logon(rs);
       
   300 	t2.Resume();
       
   301 	User::WaitForRequest(rs);
       
   302 	User::SetJustInTime(ETrue);
       
   303 	test(t2.ExitType() == EExitPanic);
       
   304 	test(t2.ExitReason() == KErrBadHandle);
       
   305 	CLOSE_AND_WAIT(t2);
       
   306 
       
   307 	RThread t3;
       
   308 	User::SetJustInTime(EFalse);
       
   309 	test(t3.Create(_L("Handle2Test"), SendBadHandle3, KDefaultStackSize, KHeapSize, KHeapSize, NULL) == KErrNone);
       
   310 	t3.Logon(rs);
       
   311 	t3.Resume();
       
   312 	User::WaitForRequest(rs);
       
   313 	User::SetJustInTime(ETrue);
       
   314 	test(t3.ExitType() == EExitPanic);
       
   315 	test(t3.ExitReason() == KErrBadHandle);
       
   316 	CLOSE_AND_WAIT(t3);
       
   317 
       
   318 	RThread t4;
       
   319 	User::SetJustInTime(EFalse);
       
   320 	test(t4.Create(_L("Handle2Test"), SendBadHandle4, KDefaultStackSize, KHeapSize, KHeapSize, NULL) == KErrNone);
       
   321 	t4.Logon(rs);
       
   322 	t4.Resume();
       
   323 	User::WaitForRequest(rs);
       
   324 	User::SetJustInTime(ETrue);
       
   325 	test(t4.ExitType() == EExitPanic);
       
   326 	test(t4.ExitReason() == KErrBadHandle);
       
   327 	CLOSE_AND_WAIT(t4);
       
   328 
       
   329 	RThread t5;
       
   330 	User::SetJustInTime(EFalse);
       
   331 	test(t5.Create(_L("Handle2Test"), SendBadHandle5, KDefaultStackSize, KHeapSize, KHeapSize, NULL) == KErrNone);
       
   332 	t5.Logon(rs);
       
   333 	t5.Resume();
       
   334 	User::WaitForRequest(rs);
       
   335 	User::SetJustInTime(ETrue);
       
   336 	test(t5.ExitType() == EExitPanic);
       
   337 	test(t5.ExitReason() == KErrBadHandle);
       
   338 	CLOSE_AND_WAIT(t5);
       
   339 
       
   340 	RThread t6;
       
   341 	User::SetJustInTime(EFalse);
       
   342 	test(t6.Create(_L("Handle2Test"), SendBadHandle6, KDefaultStackSize, KHeapSize, KHeapSize, NULL) == KErrNone);
       
   343 	t6.Logon(rs);
       
   344 	t6.Resume();
       
   345 	User::WaitForRequest(rs);
       
   346 	User::SetJustInTime(ETrue);
       
   347 	test(t6.ExitType() == EExitPanic);
       
   348 	test(t6.ExitReason() == KErrBadHandle);
       
   349 	CLOSE_AND_WAIT(t6);
       
   350 
       
   351 	User::SetJustInTime(ETrue);
       
   352 	test.End();
       
   353 	
       
   354     }
       
   355 
       
   356 
       
   357 LOCAL_C void DoTestsL()
       
   358 //
       
   359 // Call all tests
       
   360 //
       
   361 	{
       
   362 	Test1();
       
   363 	Test2();
       
   364 	Test3();
       
   365 	}
       
   366 
       
   367 GLDEF_C void CallTestsL(void)
       
   368 //
       
   369 // Test resource counting
       
   370 //
       
   371     {
       
   372 
       
   373 	test.Title();
       
   374 	test.Start(_L("Starting T_RCOUNT test"));
       
   375 
       
   376 	DoTestsL();
       
   377 
       
   378 	test.End();
       
   379 	test.End();
       
   380 	test.Close();
       
   381 
       
   382 	return;
       
   383     }
       
   384