os/kernelhwsrv/kerneltest/f32test/filesystem/fat/t_raw.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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_raw.cpp
    15 //
    16 //
    17 
    18 #define __E32TEST_EXTENSION__
    19 
    20 #include <f32file.h>
    21 #include <e32test.h>
    22 #include "t_server.h"
    23 
    24 #include "fs_utils.h"
    25 #include "fat_utils.h"
    26 using namespace Fat_Test_Utils;
    27 
    28 
    29 GLDEF_D RTest test(_L("T_RAW"));
    30 
    31 LOCAL_D TInt gDrive;
    32 
    33 LOCAL_C void Test1()
    34 //
    35 // Test all methods
    36 //
    37 	{
    38 
    39 	test.Start(_L("Test all methods"));
    40 	RRawDisk rd;
    41 	TInt r=rd.Open(TheFs,gDrive);
    42 	test(r==KErrNone);
    43 	TBuf8<16> buf;
    44 	r=rd.Read(0,buf);
    45 	test(r==KErrNone);
    46 	r=rd.Write(0,buf);
    47 	test(r==KErrNone);
    48 	rd.Close();
    49 	test.End();
    50 	}
    51 
    52 LOCAL_C void Test2()
    53 //
    54 // Test cannot open a RRawDisk while other resources are open on it.
    55 //
    56 	{
    57 
    58 	MakeFile(_L("TRAW.TST"));
    59 	MakeDir(_L("\\F32-TST\\TRAW\\TRAWTEST\\"));
    60 
    61 	RFile f;
    62 	TInt r=f.Open(TheFs,_L("TRAW.TST"),EFileWrite);
    63 	test(r==KErrNone);
    64 	RRawDisk rd;
    65 	r=rd.Open(TheFs,gDrive);
    66 	test(r==KErrInUse);
    67 	f.Close();
    68 	r=rd.Open(TheFs,gDrive);
    69 	test(r==KErrNone);
    70 	rd.Close();
    71 
    72 	TFileName fileName;
    73 	r=f.Temp(TheFs,_L(""),fileName,EFileWrite);
    74 	test(r==KErrNone);
    75 	r=rd.Open(TheFs,gDrive);
    76 	test(r==KErrInUse);
    77 	f.Close();
    78 	r=rd.Open(TheFs,gDrive);
    79 	test(r==KErrNone);
    80 	rd.Close();
    81 
    82 	RDir d;
    83 	r=d.Open(TheFs,_L("TRAWTEST"),KEntryAttNormal);
    84 	test(r==KErrNone);
    85 	r=rd.Open(TheFs,gDrive);
    86 	test(r==KErrInUse);
    87 	d.Close();
    88 	r=rd.Open(TheFs,gDrive);
    89 	test(r==KErrNone);
    90 	rd.Close();
    91 
    92     RFormat fmt;
    93     TInt count;
    94     r=fmt.Open(TheFs,gSessionPath,EQuickFormat,count);
    95     test(r==KErrNone);
    96     r=rd.Open(TheFs,gDrive); // Raw disk access ok during formatting
    97     test.Printf(_L("open rd when fmt opn r=%d"),r);
    98 	test(r==KErrInUse);
    99     fmt.Close();
   100     r=rd.Open(TheFs,gDrive);
   101 	test.Printf(_L("open rd when fmt closed r=%d"),r);
   102     test(r==KErrNone);
   103 	RRawDisk rd2;
   104 	r=rd2.Open(TheFs,gDrive);	//should only have one Raw object open
   105 	test(r==KErrInUse);
   106 	rd2.Close();
   107     rd.Close();
   108 	}
   109 
   110 LOCAL_C void Test3()
   111 //
   112 // Test cannot open resources on a disk while raw access is taking place
   113 //
   114 	{
   115 
   116 	MakeFile(_L("TRAW.TST"));
   117 	MakeDir(_L("TRAW"));
   118 
   119 	RRawDisk rd;
   120 	TInt r=rd.Open(TheFs,gDrive);
   121 	test(r==KErrNone);
   122 
   123 	RFile f;
   124 	TFileName fileName;
   125 	r=f.Open(TheFs,_L("TRAW.TST"),EFileWrite);
   126 	test(r==KErrInUse);
   127 	r=f.Temp(TheFs,_L(""),fileName,EFileWrite);
   128 	test(r==KErrInUse);
   129 
   130 	RDir d;
   131 	r=d.Open(TheFs,_L("TRAW"),KEntryAttNormal);
   132 	test(r==KErrInUse);
   133 
   134 	RFormat fmt;
   135 	TInt count;
   136 	r=fmt.Open(TheFs,gSessionPath,EQuickFormat,count);
   137     if (r != KErrInUse)
   138         test.Printf(_L("Error %d. Sess = %S"), r, &gSessionPath);
   139 	test(r==KErrInUse);
   140 
   141 	CDir* dir=(CDir*)0x05;
   142 	r=TheFs.GetDir(_L("\\F32-TST\\*.*"),KEntryAttNormal,ESortNone,dir);
   143 	test(r==KErrInUse);
   144 	test(dir==NULL);
   145 
   146 	RRawDisk rd2;
   147 	r=rd2.Open(TheFs,gDrive);
   148 	test(r==KErrInUse);
   149 
   150 //	fmt.Close();
   151 	rd.Close();
   152 //	rd2.Close();
   153 	}
   154 
   155 LOCAL_C void Test4()
   156 //
   157 // Test Read and Write
   158 //
   159 	{
   160 
   161 	TBuf8<32> contents=_L8("This File says BOO");
   162 	MakeFile(_L("TRAW.TST"),contents);
   163 
   164 	RRawDisk rd;
   165 	TInt r=rd.Open(TheFs,gDrive);
   166 	test(r==KErrNone);
   167 
   168 	TBuf8<32> textBuf;
   169 	TInt64 pos=0;
   170 
   171 	test.Printf(_L("doing rawread"));
   172 	FOREVER
   173 		{
   174 		r=rd.Read(pos,textBuf);
   175 		if (r!=KErrNone)
   176 			{
   177 			test.Printf(_L("ERROR: RawDisk read returned %d at pos %ld"),r, pos);
   178 			test(0);
   179 			//test.Getch();
   180 			break;
   181 			}
   182 		if ((pos % (1024*1024)) == 0)
   183 			test.Printf(_L("Read position 0x%x            \r"), pos);
   184 		textBuf.SetLength(contents.Length());
   185 		if (textBuf==contents)
   186 			break;
   187 		pos+=512;
   188 		}
   189 	test.Printf(_L("\n"));
   190 
   191 	TBuf8<32> contents2=_L8("This File says MOO");
   192 	r=rd.Write(pos,contents2);
   193 	test(r==KErrNone);
   194 	rd.Close();
   195 
   196 	RFile f;
   197 	r=f.Open(TheFs,_L("TRAW.TST"),EFileRead);
   198 	test(r==KErrNone);
   199 	r=f.Read(textBuf);
   200 	test(r==KErrNone);
   201 	test(textBuf==contents2);
   202 	f.Close();
   203 	}
   204 
   205 enum TTestCommands
   206 	{
   207 	EThreadForgetToCloseSession,
   208 	EThreadForgetToCloseRaw,
   209 	EThreadHang
   210 	};
   211 
   212 RSemaphore gSemaphore;
   213 
   214 TInt MyThreadFunction(TAny* aThreadCommand)
   215 //
   216 // Do nasty things
   217 //
   218 	{
   219 
   220 	RRawDisk rd;
   221 	RFs fs;
   222 	TInt r=fs.Connect();
   223 	if (r!=KErrNone)
   224 		goto Error;
   225 	r=rd.Open(fs,gDrive);
   226 	if (r!=KErrNone)
   227 		goto Error;
   228 
   229 	switch((TTestCommands)(TInt)aThreadCommand)
   230 		{
   231 	case EThreadForgetToCloseSession:
   232 		return(KErrNone);
   233 
   234 	case EThreadForgetToCloseRaw:
   235 		break;
   236 
   237 	case EThreadHang:
   238 		gSemaphore.Signal();
   239 		FOREVER{};
   240 
   241 	default:
   242 		goto Error;
   243 		}
   244 	fs.Close();
   245 	return(KErrNone);
   246 
   247 Error:
   248 	User::Panic(_L("Shouldn't be here!"),0);
   249 	return(KErrNone);
   250 	}
   251 
   252 LOCAL_C void Test5()
   253 //
   254 // Test thread panics
   255 //
   256 	{
   257 
   258 	test.Next(_L("Test thread panics"));
   259 	MakeFile(_L("TEST.FILE"));
   260 
   261 	RThread thread;
   262 	TInt r=thread.Create(_L("MyThread"),MyThreadFunction,0x1000,0x1000,0x1000,(TAny*)EThreadForgetToCloseSession);
   263 	test(r==KErrNone);
   264 	TRequestStatus reqStat;
   265 	thread.Logon(reqStat);
   266 	thread.Resume();
   267 	User::WaitForRequest(reqStat);
   268 	CLOSE_AND_WAIT(thread);
   269 
   270 	// We know the disconnect has been sent to the file server by this point
   271 	// but we don't know it has been processed.
   272 	// Connect and disconnect a session here to make sure.
   273 	FsBarrier();
   274 
   275 	RFile f;
   276 	r=f.Open(TheFs,_L("TEST.FILE"),EFileWrite);
   277 	test(r==KErrNone);
   278 	f.Close();
   279 
   280 	r=thread.Create(_L("MyThread"),MyThreadFunction,0x1000,0x1000,0x1000,(TAny*)EThreadForgetToCloseRaw);
   281 	test(r==KErrNone);
   282 	thread.Logon(reqStat);
   283 	thread.Resume();
   284 	User::WaitForRequest(reqStat);
   285 	CLOSE_AND_WAIT(thread);
   286 
   287 	// We know the disconnect has been sent to the file server by this point
   288 	// but we don't know it has been processed.
   289 	// Connect and disconnect a session here to make sure.
   290 	FsBarrier();
   291 
   292 	r=f.Open(TheFs,_L("TEST.FILE"),EFileWrite);
   293 	test(r==KErrNone);
   294 	f.Close();
   295 
   296 	r=gSemaphore.CreateGlobal(_L("MySemaphore"),0);
   297 	test(r==KErrNone);
   298 	r=thread.Create(_L("MyThread"),MyThreadFunction,0x1000,0x1000,0x1000,(TAny*)EThreadHang);
   299 	test(r==KErrNone);
   300 	thread.Resume();
   301 	gSemaphore.Wait();
   302 	gSemaphore.Close();
   303 
   304 	r=f.Open(TheFs,_L("TEST.FILE"),EFileWrite);
   305 	test(r==KErrInUse);
   306 	TBool jit = User::JustInTime();
   307 	User::SetJustInTime(EFalse);
   308 	thread.Kill(KErrGeneral);
   309 	CLOSE_AND_WAIT(thread);
   310 	User::SetJustInTime(jit);
   311 
   312 	// We know the disconnect has been sent to the file server by this point
   313 	// but we don't know it has been processed.
   314 	// Connect and disconnect a session here to make sure.
   315 	FsBarrier();
   316 
   317 	r=f.Open(TheFs,_L("TEST.FILE"),EFileWrite);
   318 	test(r==KErrNone);
   319 	f.Close();
   320 	}
   321 
   322 
   323 GLDEF_C void CallTestsL()
   324 //
   325 // Do all tests
   326 //
   327 	{
   328 
   329     //-- set up console output
   330     Fat_Test_Utils::SetConsole(test.Console());
   331 
   332 	TInt r=TheFs.CharToDrive(gSessionPath[0],gDrive);
   333 	test(r==KErrNone);
   334 
   335     PrintDrvInfo(TheFs, gDrive);
   336 
   337     //-- check if this is FAT
   338     if(!Is_Fat(TheFs, gDrive))
   339     {
   340         test.Printf(_L("Skipping. This test requires FAT drive.\n"));
   341         return;
   342     }
   343 
   344     //-- format the drive.
   345     r = FormatFatDrive(TheFs, gDrive, ETrue);
   346     test_KErrNone(r);
   347 
   348 	CreateTestDirectory(_L("\\F32-TST\\TRAW\\"));
   349 
   350 	Test1();
   351 	Test2();
   352 	Test3();
   353 
   354 	Test4(); // (silly test) Not valid on LFFS
   355 
   356     Test5();
   357 
   358 	DeleteTestDirectory();
   359 	}