os/kernelhwsrv/kerneltest/f32test/server/b_file.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1995-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\b_file.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <f32file.h>
sl@0
    19
#include <e32math.h>
sl@0
    20
#include <e32test.h>
sl@0
    21
#include "t_server.h"
sl@0
    22
sl@0
    23
GLDEF_D RTest test(_L("B_FILE"));
sl@0
    24
sl@0
    25
LOCAL_D RFile TheFile;
sl@0
    26
LOCAL_D TInt bret; // Expected error return
sl@0
    27
LOCAL_D TInt aret; // Actual error return
sl@0
    28
LOCAL_D TBuf8<10000> tbuf; // Test buffer
sl@0
    29
LOCAL_D TPtrC tbin(_S("\\F32-TST\\TEST.BIN"));
sl@0
    30
LOCAL_D TPtrC rndm(_S("\\F32-TST\\RANDOM.TST"));
sl@0
    31
LOCAL_D TPtrC tzzz(_S("\\F32-TST\\ZZZZZZ.ZZZ"));
sl@0
    32
LOCAL_D const TInt KRandomNumbers=1024;
sl@0
    33
sl@0
    34
LOCAL_C void bopen(TUint aMode)
sl@0
    35
//
sl@0
    36
// Open the binary file.
sl@0
    37
//
sl@0
    38
    {
sl@0
    39
sl@0
    40
    aret=TheFile.Open(TheFs,tbin,aMode);
sl@0
    41
    test(aret==bret);
sl@0
    42
    }
sl@0
    43
sl@0
    44
LOCAL_C void bcreate(TUint aMode)
sl@0
    45
//
sl@0
    46
// Open the binary file.
sl@0
    47
//
sl@0
    48
    {
sl@0
    49
sl@0
    50
    aret=TheFile.Create(TheFs,tbin,aMode);
sl@0
    51
    test(aret==bret);
sl@0
    52
    }
sl@0
    53
sl@0
    54
LOCAL_C void breplace(TUint aMode)
sl@0
    55
//
sl@0
    56
// Open the binary file.
sl@0
    57
//
sl@0
    58
    {
sl@0
    59
sl@0
    60
    aret=TheFile.Replace(TheFs,tbin,aMode);
sl@0
    61
    test(aret==bret);
sl@0
    62
    }
sl@0
    63
sl@0
    64
LOCAL_C void bwrite(TInt aLength)
sl@0
    65
//
sl@0
    66
// Write aLength bytes of test data at current position.
sl@0
    67
//
sl@0
    68
    {
sl@0
    69
sl@0
    70
	CheckDisk();
sl@0
    71
    test.Printf(_L("bwrite1,len=%u\n"),aLength);
sl@0
    72
    TInt pos=0; // Relative position zero
sl@0
    73
    aret=TheFile.Seek(ESeekCurrent,pos);
sl@0
    74
    test.Printf(_L("bwrite2,pos=%u\n"),pos);
sl@0
    75
    test(aret==KErrNone);
sl@0
    76
    TInt count=pos&0xff;
sl@0
    77
	tbuf.SetLength(aLength);
sl@0
    78
	TText8* p=(TText8*)tbuf.Ptr();
sl@0
    79
	TText8* pE=p+aLength;
sl@0
    80
    while (p<pE)
sl@0
    81
        {
sl@0
    82
        *p++=(TText8)count++;
sl@0
    83
        count&=0xff;
sl@0
    84
        }
sl@0
    85
    test.Printf(_L("bwrite3\n"));
sl@0
    86
    aret=TheFile.Write(tbuf);
sl@0
    87
    test.Printf(_L("bwrite4\n"));
sl@0
    88
    test(aret==bret);
sl@0
    89
	CheckDisk();
sl@0
    90
    }
sl@0
    91
sl@0
    92
LOCAL_C void bread(TInt aLength)
sl@0
    93
//
sl@0
    94
// Read and check aLength bytes of test data at current position.
sl@0
    95
//
sl@0
    96
    {
sl@0
    97
sl@0
    98
	CheckDisk();
sl@0
    99
    TInt pos=0; // Relative position zero
sl@0
   100
    aret=TheFile.Seek(ESeekCurrent,pos);
sl@0
   101
    test(aret==KErrNone);
sl@0
   102
    TInt count=pos&0xff;
sl@0
   103
    aret=TheFile.Read(tbuf,aLength);
sl@0
   104
    if (bret<KErrNone)
sl@0
   105
		{
sl@0
   106
        test(bret==aret);
sl@0
   107
		return;
sl@0
   108
		}
sl@0
   109
    test(((TInt)tbuf.Length())==bret);
sl@0
   110
	const TText8* p=tbuf.Ptr();
sl@0
   111
	const TText8* pE=p+bret;
sl@0
   112
    while (p<pE)
sl@0
   113
        {
sl@0
   114
        if (*p++!=count++)
sl@0
   115
			test.Panic(_L("bread data different"));
sl@0
   116
        count&=0xff;
sl@0
   117
        }
sl@0
   118
	CheckDisk();
sl@0
   119
    }
sl@0
   120
sl@0
   121
LOCAL_C void bposa(TInt aPos)
sl@0
   122
//
sl@0
   123
// Position absolute.
sl@0
   124
//
sl@0
   125
    {
sl@0
   126
sl@0
   127
	CheckDisk();
sl@0
   128
    TInt newpos=aPos;
sl@0
   129
    aret=TheFile.Seek(ESeekStart,newpos);
sl@0
   130
    test(aret==KErrNone);
sl@0
   131
    test(newpos==aPos);
sl@0
   132
	CheckDisk();
sl@0
   133
    }
sl@0
   134
sl@0
   135
LOCAL_C void bclose()
sl@0
   136
//
sl@0
   137
// Close the file.
sl@0
   138
//
sl@0
   139
    {
sl@0
   140
sl@0
   141
	CheckDisk();
sl@0
   142
    TheFile.Close();
sl@0
   143
	CheckDisk();
sl@0
   144
    }
sl@0
   145
sl@0
   146
LOCAL_C void btest1(TUint aMode)
sl@0
   147
//
sl@0
   148
// Binary file tests.
sl@0
   149
//
sl@0
   150
    {
sl@0
   151
sl@0
   152
    test.Start(_L("BTEST1..."));
sl@0
   153
    bret=0;
sl@0
   154
    breplace(aMode|EFileWrite);
sl@0
   155
    bret=0; bread(1);
sl@0
   156
    bret=0; bwrite(1); bposa(0l);
sl@0
   157
    bret=1; bread(2);
sl@0
   158
    bret=0; bread(1);
sl@0
   159
    bret=0; bposa(0l);
sl@0
   160
    bret=1; bread(1);
sl@0
   161
    bret=0; bread(1); bret=0;
sl@0
   162
    bclose();
sl@0
   163
    bret=KErrAlreadyExists;bcreate(aMode|EFileWrite);bret=0;
sl@0
   164
    bopen(aMode|EFileRead);
sl@0
   165
    bret=KErrAccessDenied; bwrite(1); bret=0;
sl@0
   166
    bclose();
sl@0
   167
	aret=TheFile.Open(TheFs,tzzz,EFileRead);
sl@0
   168
	test(aret==KErrNotFound);
sl@0
   169
	test.End();
sl@0
   170
    }
sl@0
   171
sl@0
   172
LOCAL_C void btest2(TUint aMode)
sl@0
   173
//
sl@0
   174
// Binary file tests.
sl@0
   175
//
sl@0
   176
    {
sl@0
   177
sl@0
   178
    test.Start(_L("BTEST2..."));
sl@0
   179
    bret=0;
sl@0
   180
    breplace(aMode|EFileWrite);
sl@0
   181
    bwrite(11);
sl@0
   182
    bposa(0);
sl@0
   183
    bret=5; bread(5); bret=0;
sl@0
   184
    bwrite(45);
sl@0
   185
    bposa(5);
sl@0
   186
    bret=45; bread(45); bret=0;
sl@0
   187
    bwrite(1000);
sl@0
   188
    bposa(600);
sl@0
   189
    bret=300; bread(300); bret=0;
sl@0
   190
    bclose();
sl@0
   191
    bopen(aMode|EFileWrite);
sl@0
   192
    bposa(5);
sl@0
   193
    bret=5; bread(5); 
sl@0
   194
    bret=1000; bread(1000); bret=0;
sl@0
   195
    bclose();
sl@0
   196
    bopen(aMode|EFileWrite);
sl@0
   197
    bposa(KMaxTInt);
sl@0
   198
    bwrite(50);
sl@0
   199
    bposa(0);
sl@0
   200
    bret=1100; bread(1100); bret=0;
sl@0
   201
    aret=TheFile.Flush();
sl@0
   202
    test(aret==KErrNone);
sl@0
   203
    aret=TheFile.SetSize(2000);
sl@0
   204
    test(aret==KErrNone);
sl@0
   205
    TInt pos=0;
sl@0
   206
    aret=TheFile.Seek(ESeekCurrent,pos);
sl@0
   207
    test(aret==KErrNone && pos==1100);
sl@0
   208
    pos=0;
sl@0
   209
    aret=TheFile.Seek(ESeekEnd,pos);
sl@0
   210
    test(aret==KErrNone && pos==2000);
sl@0
   211
    bclose();
sl@0
   212
	test.End();
sl@0
   213
    }
sl@0
   214
sl@0
   215
LOCAL_C void rndtest(TUint aMode)
sl@0
   216
//
sl@0
   217
// Tests the file handling by writing a file of random numbers, 
sl@0
   218
// closing the file, reseeding the random number generator with
sl@0
   219
// the same number, then reading the file back again, checking
sl@0
   220
// it against the generator.
sl@0
   221
//
sl@0
   222
    {
sl@0
   223
sl@0
   224
    TInt cnt;
sl@0
   225
    test.Start(_L("RNDTEST..."));
sl@0
   226
    TInt64 seed(0),zero(0);
sl@0
   227
	aret=TheFile.Replace(TheFs,rndm,EFileWrite|aMode);
sl@0
   228
	test(aret==KErrNone);
sl@0
   229
    for (cnt=0;cnt<KRandomNumbers;cnt++)
sl@0
   230
        {
sl@0
   231
		TBuf8<0x10> b;
sl@0
   232
		b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
sl@0
   233
		aret=TheFile.Write(b);
sl@0
   234
		test(aret==KErrNone);
sl@0
   235
        }
sl@0
   236
	TheFile.Close();
sl@0
   237
//
sl@0
   238
    test.Next(_L("Reading back"));
sl@0
   239
    seed=zero;
sl@0
   240
	aret=TheFile.Open(TheFs,rndm,aMode);
sl@0
   241
	test(aret==KErrNone);
sl@0
   242
    for (cnt=0;cnt<KRandomNumbers;cnt++)
sl@0
   243
        {
sl@0
   244
		TBuf8<8> b;
sl@0
   245
		b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
sl@0
   246
		TBuf8<8> r;
sl@0
   247
		aret=TheFile.Read(r);
sl@0
   248
		test(aret==KErrNone);
sl@0
   249
		test(b==r);
sl@0
   250
        }
sl@0
   251
	TheFile.Close();
sl@0
   252
    aret=TheFs.Delete(rndm);
sl@0
   253
	test(aret==KErrNone);
sl@0
   254
//
sl@0
   255
	test.End();
sl@0
   256
    }
sl@0
   257
sl@0
   258
LOCAL_C void testAutoClose()
sl@0
   259
//
sl@0
   260
// Tests TAutoClose template class
sl@0
   261
//
sl@0
   262
    {
sl@0
   263
sl@0
   264
    test.Start(_L("TAutoClose..."));
sl@0
   265
	TAutoClose<RFile> f;
sl@0
   266
	aret=f.iObj.Replace(TheFs,rndm,EFileWrite);
sl@0
   267
	test(aret==KErrNone);
sl@0
   268
    TInt64 seed;
sl@0
   269
    for (TInt cnt=0;cnt<KRandomNumbers;cnt++)
sl@0
   270
        {
sl@0
   271
		TBuf8<0x10> b;
sl@0
   272
		b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
sl@0
   273
		aret=f.iObj.Write(b);
sl@0
   274
		test(aret==KErrNone);
sl@0
   275
        }
sl@0
   276
	test.End();
sl@0
   277
    }
sl@0
   278
sl@0
   279
LOCAL_C void readWithNegativeLengthTest()
sl@0
   280
{
sl@0
   281
    test.Start(_L("Read with Negative Length Test..."));
sl@0
   282
    TInt	ret;
sl@0
   283
	TRequestStatus status = KRequestPending;
sl@0
   284
	TheFile.Open(TheFs,tbin,EFileRead);
sl@0
   285
 	ret = TheFile.Read(0,tbuf,-1);			// sync
sl@0
   286
 	test ( ret == KErrArgument);
sl@0
   287
 	TheFile.Read(0,tbuf,-1,status);		// async
sl@0
   288
 	User::WaitForRequest(status);
sl@0
   289
 	test(status.Int() == KErrArgument);
sl@0
   290
	TheFile.Close();
sl@0
   291
	test.End();
sl@0
   292
}
sl@0
   293
sl@0
   294
LOCAL_C void readWithNegativeLengthTestForEmptyFile() 
sl@0
   295
	
sl@0
   296
	{
sl@0
   297
sl@0
   298
	test.Start(_L("Read with Negative Length Test(For EmptyFile)..."));
sl@0
   299
	RFile f;             
sl@0
   300
	MakeFile(_L("C:\\F32-TST\\TFILE\\hello2.txt"));
sl@0
   301
	TInt r=f.Open(TheFs,_L("C:\\F32-TST\\TFILE\\hello2.txt"),EFileRead); 
sl@0
   302
	test(r==KErrNone);
sl@0
   303
sl@0
   304
	TBuf8<0x100> a;
sl@0
   305
	test.Next(_L("Check Negative length when file is empty"));
sl@0
   306
	r=f.Read(a, -10);
sl@0
   307
	test(r==KErrArgument);
sl@0
   308
	r=f.Read(0,a, -1);
sl@0
   309
	test(r==KErrArgument);
sl@0
   310
	r=f.Read(0,a, -10);
sl@0
   311
	test(r==KErrArgument);
sl@0
   312
	TRequestStatus	stat1;
sl@0
   313
	f.Read(0,a,-5,stat1);
sl@0
   314
	User::WaitForRequest(stat1);
sl@0
   315
	test(stat1.Int() == KErrArgument);
sl@0
   316
	f.Read(a,-5,stat1);
sl@0
   317
	User::WaitForRequest(stat1);
sl@0
   318
	test(stat1.Int() == KErrArgument);
sl@0
   319
	f.Close();	
sl@0
   320
	test.End();
sl@0
   321
	
sl@0
   322
	}
sl@0
   323
sl@0
   324
GLDEF_C void CallTestsL()
sl@0
   325
//
sl@0
   326
// Call tests that may leave
sl@0
   327
//
sl@0
   328
	{
sl@0
   329
	
sl@0
   330
	testAutoClose();
sl@0
   331
    btest1(EFileStream);
sl@0
   332
    btest2(EFileStream);
sl@0
   333
    btest1(EFileStreamText);
sl@0
   334
    btest2(EFileStreamText);
sl@0
   335
    rndtest(EFileStream);
sl@0
   336
	readWithNegativeLengthTest();
sl@0
   337
	readWithNegativeLengthTestForEmptyFile();
sl@0
   338
	
sl@0
   339
	}