os/persistentdata/persistentstorage/store/TSTOR/t_storverify.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) 2008-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 "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
//
sl@0
    15
sl@0
    16
#include <s32mem.h>
sl@0
    17
#include <s32file.h>
sl@0
    18
#include <e32test.h>
sl@0
    19
sl@0
    20
const TInt KTestCleanupStack=0x20;
sl@0
    21
sl@0
    22
// This is a path specification and should not be used as is
sl@0
    23
_LIT(KTestDirectFileName, "Z:\\STOR-TST\\T_STRDV.DAT");	//deletion of data files must be before call to .End() - DEF047652
sl@0
    24
_LIT(KTestPermanentFileName, "Z:\\STOR-TST\\T_STRPV.DAT");	//deletion of data files must be before call to .End() - DEF047652
sl@0
    25
sl@0
    26
// The following data will be written to the beginning of the store file in the length of
sl@0
    27
// sizeof(TCheckedUid) to corrupt it. Its length should be more than that number.
sl@0
    28
_LIT8(KTestJunkData, "Junk data for corrupting the store file");
sl@0
    29
sl@0
    30
LOCAL_D RTest Test(_L("t_storverify"));
sl@0
    31
LOCAL_D CTrapCleanup* TheTrapCleanup;
sl@0
    32
LOCAL_D RFs TheFs;
sl@0
    33
LOCAL_D CFileStore* TheStore;
sl@0
    34
LOCAL_D RStoreWriteStream TheSink;
sl@0
    35
sl@0
    36
LOCAL_C void DeleteDataFile(const TDesC& aFullName);
sl@0
    37
sl@0
    38
/**
sl@0
    39
@SYMTestCaseID          SYSLIB-STORE-CT-3350
sl@0
    40
@SYMTestCaseDesc	    Direct file store verification test
sl@0
    41
@SYMTestPriority 	    High
sl@0
    42
@SYMTestActions  	    Open a corrupted direct store file
sl@0
    43
@SYMTestExpectedResults the function called leaves with KErrNotSupported
sl@0
    44
@SYMDEF                 DEF100757
sl@0
    45
*/
sl@0
    46
LOCAL_C void DirectFileL()
sl@0
    47
	{
sl@0
    48
	Test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-3350 Creating Direct File Store "));
sl@0
    49
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
    50
	TParse parse;
sl@0
    51
	parse.Set(drive.Name(), &KTestDirectFileName, NULL);
sl@0
    52
	
sl@0
    53
	// create a direct file store
sl@0
    54
	TheStore=CDirectFileStore::ReplaceLC(TheFs,parse.FullName(),EFileWrite);
sl@0
    55
	TheStore->SetTypeL(TheStore->Layout());
sl@0
    56
	TInt8 val=100;
sl@0
    57
	TStreamId id=TheSink.CreateL(*TheStore);
sl@0
    58
	TheSink<<val;
sl@0
    59
	TheSink.Close();
sl@0
    60
	TheStore->SetRootL(id);
sl@0
    61
	TheStore->CommitL();
sl@0
    62
	CleanupStack::PopAndDestroy(TheStore);
sl@0
    63
sl@0
    64
	// corrupt the store file
sl@0
    65
	RFile file;
sl@0
    66
sl@0
    67
	User::LeaveIfError(file.Open(TheFs,parse.FullName(),EFileWrite));
sl@0
    68
	CleanupClosePushL(file);
sl@0
    69
	User::LeaveIfError(file.Write(KTestJunkData,sizeof(TCheckedUid)));
sl@0
    70
	CleanupStack::PopAndDestroy(&file);
sl@0
    71
sl@0
    72
	// the CDirectFileStore::OpenLC should leave if passed the corrupted store file name
sl@0
    73
	TheStore = NULL;
sl@0
    74
	TRAPD(r, \
sl@0
    75
			{\
sl@0
    76
			TheStore=CDirectFileStore::OpenLC(TheFs,parse.FullName(),EFileRead);\
sl@0
    77
			CleanupStack::PopAndDestroy(TheStore); \
sl@0
    78
			}\
sl@0
    79
		);
sl@0
    80
	Test(r==KErrNotSupported);
sl@0
    81
	}
sl@0
    82
sl@0
    83
/**
sl@0
    84
@SYMTestCaseID          SYSLIB-STORE-CT-3351
sl@0
    85
@SYMTestCaseDesc	    Permanent file store verification test
sl@0
    86
@SYMTestPriority 	    High
sl@0
    87
@SYMTestActions  	    Open a corrupted permanent store file
sl@0
    88
@SYMTestExpectedResults the function called leaves with KErrNotSupported
sl@0
    89
@SYMDEF                 DEF100757
sl@0
    90
*/
sl@0
    91
LOCAL_C void PermFileL()
sl@0
    92
	{
sl@0
    93
	Test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-3351 Creating Permanent File Store "));
sl@0
    94
sl@0
    95
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
    96
	TParse parse;
sl@0
    97
	parse.Set(drive.Name(), &KTestPermanentFileName, NULL);
sl@0
    98
	
sl@0
    99
	// create a permanent file store
sl@0
   100
	TheStore=CPermanentFileStore::ReplaceLC(TheFs,parse.FullName(),EFileWrite);
sl@0
   101
	TheStore->SetTypeL(TheStore->Layout());
sl@0
   102
	TInt8 val=100;
sl@0
   103
	TStreamId id=TheSink.CreateL(*TheStore);
sl@0
   104
	TheSink<<val;
sl@0
   105
	TheSink.Close();
sl@0
   106
	TheStore->SetRootL(id);
sl@0
   107
	TheStore->CommitL();
sl@0
   108
	CleanupStack::PopAndDestroy(TheStore);
sl@0
   109
sl@0
   110
	// corrupt the new data file
sl@0
   111
	RFile file;
sl@0
   112
	User::LeaveIfError(file.Open(TheFs,parse.FullName(),EFileWrite));
sl@0
   113
	CleanupClosePushL(file);
sl@0
   114
	User::LeaveIfError(file.Write(KTestJunkData,sizeof(TCheckedUid)));
sl@0
   115
	CleanupStack::PopAndDestroy(&file);
sl@0
   116
sl@0
   117
	// the CPermanentFileStore::OpenLC should leave if passed the corrupted store file name
sl@0
   118
	TheStore = NULL;
sl@0
   119
	TRAPD(r, \
sl@0
   120
			{\
sl@0
   121
			TheStore=CPermanentFileStore::OpenLC(TheFs,parse.FullName(),EFileRead);\
sl@0
   122
			CleanupStack::PopAndDestroy(TheStore); \
sl@0
   123
			}\
sl@0
   124
		);
sl@0
   125
	Test(r==KErrNotSupported);
sl@0
   126
	}
sl@0
   127
sl@0
   128
LOCAL_C void doMainL()
sl@0
   129
	{
sl@0
   130
	DirectFileL();
sl@0
   131
	PermFileL();
sl@0
   132
	}
sl@0
   133
sl@0
   134
//
sl@0
   135
// Prepare the test directory.
sl@0
   136
//
sl@0
   137
LOCAL_C void setupTestDirectory()
sl@0
   138
    {
sl@0
   139
	TInt r=TheFs.Connect();
sl@0
   140
	Test(r==KErrNone);
sl@0
   141
	TPtrC testDir=_L("\\STOR-TST\\");
sl@0
   142
	r=TheFs.MkDir(testDir);
sl@0
   143
	Test(r==KErrNone||r==KErrAlreadyExists);
sl@0
   144
	r=TheFs.SetSessionPath(testDir);
sl@0
   145
	Test(r==KErrNone);
sl@0
   146
	}
sl@0
   147
sl@0
   148
//
sl@0
   149
// Initialise the cleanup stack.
sl@0
   150
//
sl@0
   151
LOCAL_C void setupCleanup()
sl@0
   152
    {
sl@0
   153
	TheTrapCleanup=CTrapCleanup::New();
sl@0
   154
	Test(TheTrapCleanup!=NULL);
sl@0
   155
	TRAPD(r,\
sl@0
   156
		{\
sl@0
   157
		for (TInt i=KTestCleanupStack;i>0;i--)\
sl@0
   158
			CleanupStack::PushL((TAny*)1);\
sl@0
   159
		Test(r==KErrNone);\
sl@0
   160
		CleanupStack::Pop(KTestCleanupStack);\
sl@0
   161
		});
sl@0
   162
	Test(r==KErrNone);
sl@0
   163
	}
sl@0
   164
sl@0
   165
//
sl@0
   166
// Delete a specified file
sl@0
   167
//
sl@0
   168
LOCAL_C void DeleteDataFile(const TDesC& aFullName)
sl@0
   169
	{
sl@0
   170
	RFs fsSession;
sl@0
   171
	TInt err = fsSession.Connect();
sl@0
   172
	if(err == KErrNone)
sl@0
   173
		{
sl@0
   174
		TEntry entry;
sl@0
   175
		if(fsSession.Entry(aFullName, entry) == KErrNone)
sl@0
   176
			{
sl@0
   177
			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
sl@0
   178
			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
sl@0
   179
			if(err != KErrNone)
sl@0
   180
				{
sl@0
   181
				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
sl@0
   182
				}
sl@0
   183
			err = fsSession.Delete(aFullName);
sl@0
   184
			if(err != KErrNone)
sl@0
   185
				{
sl@0
   186
				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
sl@0
   187
				}
sl@0
   188
			}
sl@0
   189
		fsSession.Close();
sl@0
   190
		}
sl@0
   191
	else
sl@0
   192
		{
sl@0
   193
		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
sl@0
   194
		}
sl@0
   195
	}
sl@0
   196
sl@0
   197
//
sl@0
   198
// Test the store verification.
sl@0
   199
//
sl@0
   200
GLDEF_C TInt E32Main()
sl@0
   201
    {
sl@0
   202
	Test.Title();
sl@0
   203
	setupTestDirectory();
sl@0
   204
	setupCleanup();
sl@0
   205
	__UHEAP_MARK;
sl@0
   206
	Test.Start(_L("Store Verification Test"));
sl@0
   207
sl@0
   208
	TRAPD(r,doMainL());
sl@0
   209
sl@0
   210
	TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));	
sl@0
   211
	TParse parse;
sl@0
   212
	parse.Set(drive.Name(), &KTestDirectFileName, NULL);
sl@0
   213
	::DeleteDataFile(parse.FullName());
sl@0
   214
	
sl@0
   215
	parse.Set(drive.Name(), &KTestPermanentFileName, NULL);
sl@0
   216
	::DeleteDataFile(parse.FullName());
sl@0
   217
sl@0
   218
	Test(r==KErrNone);
sl@0
   219
sl@0
   220
	Test.End();
sl@0
   221
sl@0
   222
	__UHEAP_MARKEND;
sl@0
   223
sl@0
   224
	delete TheTrapCleanup;
sl@0
   225
	TheFs.Close();
sl@0
   226
	Test.Close();
sl@0
   227
	return 0;
sl@0
   228
    }
sl@0
   229