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