os/security/contentmgmt/referencedrmagent/tcaf/source/OomStep.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
/*
sl@0
     2
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <test/testexecutelog.h>
sl@0
    20
#include <s32mem.h>
sl@0
    21
#include "cafserver.h"
sl@0
    22
#include "oomstep.h"
sl@0
    23
#include "content.h"
sl@0
    24
#include "data.h"
sl@0
    25
#include "supplier.h"
sl@0
    26
#include "cafmimeheader.h"
sl@0
    27
#include "importfile.h"
sl@0
    28
#include "supplieroutputfile.h"
sl@0
    29
#include "attribute.h"
sl@0
    30
#include "bitset.h"
sl@0
    31
sl@0
    32
using namespace ContentAccess;
sl@0
    33
sl@0
    34
const TInt KOomReadContent = 1;
sl@0
    35
const TInt KOomSupply = 2;
sl@0
    36
sl@0
    37
sl@0
    38
/* 
sl@0
    39
 * This step imports a DCF file into CAF 
sl@0
    40
 *
sl@0
    41
 */
sl@0
    42
CCafOomStep::~CCafOomStep()
sl@0
    43
	{
sl@0
    44
	}
sl@0
    45
sl@0
    46
CCafOomStep::CCafOomStep(CCAFServer& aParent) : iParent(aParent)
sl@0
    47
	{
sl@0
    48
	SetTestStepName(KCAFOomStep);
sl@0
    49
	}
sl@0
    50
sl@0
    51
sl@0
    52
TVerdict CCafOomStep::doTestStepL()
sl@0
    53
	{
sl@0
    54
	SetTestStepResult(EInconclusive);
sl@0
    55
sl@0
    56
	TInt failCount=1;
sl@0
    57
	TInt ret=-1;
sl@0
    58
	const TInt MaxAllocationFailures=450;
sl@0
    59
sl@0
    60
sl@0
    61
	TInt OomTest = KErrNotFound;
sl@0
    62
	GetIntFromConfig(ConfigSection(),_L("OomTest"),OomTest);
sl@0
    63
sl@0
    64
sl@0
    65
	if(OomTest == 2)
sl@0
    66
		{
sl@0
    67
		INFO_PRINTF2(_L("Starting Apparc..."),failCount);	
sl@0
    68
		StartApparcServerL();
sl@0
    69
		User::After(500000); // Allow async libraries to unload
sl@0
    70
		}
sl@0
    71
sl@0
    72
sl@0
    73
	while(failCount < MaxAllocationFailures && OomTest > 0)
sl@0
    74
		{
sl@0
    75
		// Mark heap and set allocation to fail
sl@0
    76
		__UHEAP_MARK;	
sl@0
    77
sl@0
    78
		switch(OomTest)
sl@0
    79
			{
sl@0
    80
		case KOomReadContent:
sl@0
    81
			INFO_PRINTF2(_L("ReadContent() __UHEAP_FAILNEXT: %d"),failCount);	
sl@0
    82
			__UHEAP_FAILNEXT(failCount);
sl@0
    83
			TRAP(ret,OomReadContentL());
sl@0
    84
			break;
sl@0
    85
		case KOomSupply:
sl@0
    86
			INFO_PRINTF2(_L("Supply() __UHEAP_FAILNEXT: %d"),failCount);	
sl@0
    87
			__UHEAP_FAILNEXT(failCount);
sl@0
    88
			TRAP(ret,OomSupplyL());
sl@0
    89
			break;
sl@0
    90
		default:
sl@0
    91
			INFO_PRINTF2(_L("Out of Memory test %d does not exist"),OomTest);
sl@0
    92
			SetTestStepResult(EFail);
sl@0
    93
			ret = KErrNone;
sl@0
    94
			break;
sl@0
    95
			};
sl@0
    96
sl@0
    97
		User::After(500000); // Allow async libraries to unload
sl@0
    98
sl@0
    99
sl@0
   100
		// Check all heap is free'd
sl@0
   101
		__UHEAP_MARKEND;
sl@0
   102
		
sl@0
   103
		// cancel heap failure
sl@0
   104
		__UHEAP_RESET;
sl@0
   105
		if(ret == KErrNone)
sl@0
   106
			{
sl@0
   107
			// Allocation failure has been moved all the way through the test
sl@0
   108
			// Succeeded after failCount allocations
sl@0
   109
			return TestStepResult();
sl@0
   110
			}
sl@0
   111
		else 
sl@0
   112
			{
sl@0
   113
			// Try failing a little further into the process
sl@0
   114
			failCount++;
sl@0
   115
			}
sl@0
   116
		}
sl@0
   117
sl@0
   118
	// We must have reached our maximum number of allocation failures
sl@0
   119
	// There must be some other problem 
sl@0
   120
	SetTestStepResult(EFail);
sl@0
   121
sl@0
   122
	return TestStepResult();
sl@0
   123
	}	
sl@0
   124
sl@0
   125
TVerdict CCafOomStep::OomReadContentL()
sl@0
   126
	{
sl@0
   127
	
sl@0
   128
	TBuf8<2> buffer;
sl@0
   129
	TRequestStatus status;
sl@0
   130
sl@0
   131
	TPtrC uri;
sl@0
   132
	GetStringFromConfig(ConfigSection(),_L("URI"),uri);
sl@0
   133
sl@0
   134
	CContent* content = CContent::NewLC(uri);
sl@0
   135
	CData* data = content->OpenContentL(EPeek);
sl@0
   136
	CleanupStack::PushL(data);
sl@0
   137
sl@0
   138
	TInt size = 0;
sl@0
   139
	data->DataSizeL(size);
sl@0
   140
	
sl@0
   141
	// Seek and read from start using both CAF and RFile
sl@0
   142
	TInt pos = size/4;
sl@0
   143
	data->Seek(ESeekStart, pos);
sl@0
   144
	data->Read(buffer);
sl@0
   145
sl@0
   146
	pos=0;
sl@0
   147
	data->Seek(ESeekCurrent, pos);
sl@0
   148
	
sl@0
   149
	data->Read(buffer,1,status);
sl@0
   150
	User::WaitForRequest(status);
sl@0
   151
sl@0
   152
	CleanupStack::PopAndDestroy(2, content);
sl@0
   153
sl@0
   154
	SetTestStepResult(EPass);
sl@0
   155
sl@0
   156
	return TestStepResult();
sl@0
   157
	}
sl@0
   158
sl@0
   159
sl@0
   160
TVerdict CCafOomStep::OomSupplyL()
sl@0
   161
	{
sl@0
   162
	SetTestStepResult(EInconclusive);
sl@0
   163
sl@0
   164
	RFs fs;
sl@0
   165
	RFile file;
sl@0
   166
	TBuf8 <128> readBuffer;
sl@0
   167
	
sl@0
   168
	TPtrC outputDirectory, sourceFileName, suggestedFileName, mimeType;
sl@0
   169
	TInt expectedLeave;
sl@0
   170
sl@0
   171
	// Get parameters from INI file
sl@0
   172
	GetStringFromConfig(ConfigSection(),_L("OutputPath"),outputDirectory);
sl@0
   173
	GetStringFromConfig(ConfigSection(),_L("SourceFileName"),sourceFileName);
sl@0
   174
	GetStringFromConfig(ConfigSection(),_L("SuggestedFileName"),suggestedFileName);
sl@0
   175
	GetStringFromConfig(ConfigSection(),_L("MimeType"),mimeType);
sl@0
   176
	GetIntFromConfig(ConfigSection(),_L("LeaveResult"),expectedLeave);
sl@0
   177
	
sl@0
   178
sl@0
   179
	// delete any file previously supplied, don't care if this 
sl@0
   180
	// has errors so trap.
sl@0
   181
	Delete(_L("C:\\supplied.drm"));
sl@0
   182
	Delete(_L("C:\\receipt.txt"));
sl@0
   183
sl@0
   184
sl@0
   185
	HBufC8 *mime = ConvertDes16toHBufC8LC(mimeType);
sl@0
   186
sl@0
   187
	CCafMimeHeader *header = CCafMimeHeader::NewL(mime->Des());
sl@0
   188
	CleanupStack::PushL(header);
sl@0
   189
sl@0
   190
	CSupplier *mySupplier = CSupplier::NewLC();
sl@0
   191
	mySupplier->SetOutputDirectoryL(outputDirectory);
sl@0
   192
	
sl@0
   193
	// ignore return value, just exercises code for CCover
sl@0
   194
	mySupplier->IsImportSupported(header->StandardMimeData(EContentType));
sl@0
   195
sl@0
   196
	CImportFile *import = NULL;
sl@0
   197
	import = mySupplier->ImportFileL(*header, suggestedFileName);
sl@0
   198
	CleanupStack::PushL(import);
sl@0
   199
sl@0
   200
	// read the input file and pass it to the CAF
sl@0
   201
	fs.Connect();
sl@0
   202
	CleanupClosePushL(fs);
sl@0
   203
	
sl@0
   204
	TInt result = file.Open(fs, sourceFileName, EFileRead | EFileStream | EFileShareAny);
sl@0
   205
	CleanupClosePushL(file);
sl@0
   206
	while(result == KErrNone)
sl@0
   207
		{
sl@0
   208
		result = file.Read(readBuffer);
sl@0
   209
		if(readBuffer.Length() == 0)
sl@0
   210
			break;
sl@0
   211
		User::LeaveIfError(import->WriteData(readBuffer));
sl@0
   212
		}
sl@0
   213
	CleanupStack::PopAndDestroy(&file); 
sl@0
   214
	User::LeaveIfError(import->WriteDataComplete());
sl@0
   215
sl@0
   216
	TInt n = import->OutputFileCountL();
sl@0
   217
	if(n > 0)
sl@0
   218
		{
sl@0
   219
		// get output file type (content or receipt)
sl@0
   220
		TOutputType outputType = import->OutputFileL(0).OutputType();
sl@0
   221
sl@0
   222
		// get output file name, but ignore it because it would cause a warning
sl@0
   223
		import->OutputFileL(0).FileName();
sl@0
   224
			
sl@0
   225
		// get output file mime type
sl@0
   226
		TPtrC8 OutputMimeType = import->OutputFileL(0).MimeTypeL();
sl@0
   227
		HBufC *outmime = ConvertDes8toHBufC16LC(OutputMimeType );
sl@0
   228
		CleanupStack::PopAndDestroy(outmime); 
sl@0
   229
sl@0
   230
		if(outputType != EContent)
sl@0
   231
			{
sl@0
   232
			SetTestStepResult(EFail);
sl@0
   233
			}
sl@0
   234
		else
sl@0
   235
			{
sl@0
   236
			// check an attribute, it's only a reference so no need to delete it
sl@0
   237
			import->OutputFileL(0).AttributesL(ETrue);
sl@0
   238
			}
sl@0
   239
		}
sl@0
   240
sl@0
   241
	CleanupStack::PopAndDestroy(&fs); 
sl@0
   242
	CleanupStack::PopAndDestroy(import); 
sl@0
   243
	CleanupStack::PopAndDestroy(mySupplier); 
sl@0
   244
	CleanupStack::PopAndDestroy(header); 
sl@0
   245
	CleanupStack::PopAndDestroy(mime); 
sl@0
   246
sl@0
   247
	if (TestStepResult() != EFail)
sl@0
   248
		{
sl@0
   249
		SetTestStepResult(EPass);
sl@0
   250
		}
sl@0
   251
sl@0
   252
	return TestStepResult();
sl@0
   253
	}
sl@0
   254