1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/contentmgmt/referencedrmagent/tcaf/source/OomStep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,254 @@
1.4 +/*
1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <test/testexecutelog.h>
1.23 +#include <s32mem.h>
1.24 +#include "cafserver.h"
1.25 +#include "oomstep.h"
1.26 +#include "content.h"
1.27 +#include "data.h"
1.28 +#include "supplier.h"
1.29 +#include "cafmimeheader.h"
1.30 +#include "importfile.h"
1.31 +#include "supplieroutputfile.h"
1.32 +#include "attribute.h"
1.33 +#include "bitset.h"
1.34 +
1.35 +using namespace ContentAccess;
1.36 +
1.37 +const TInt KOomReadContent = 1;
1.38 +const TInt KOomSupply = 2;
1.39 +
1.40 +
1.41 +/*
1.42 + * This step imports a DCF file into CAF
1.43 + *
1.44 + */
1.45 +CCafOomStep::~CCafOomStep()
1.46 + {
1.47 + }
1.48 +
1.49 +CCafOomStep::CCafOomStep(CCAFServer& aParent) : iParent(aParent)
1.50 + {
1.51 + SetTestStepName(KCAFOomStep);
1.52 + }
1.53 +
1.54 +
1.55 +TVerdict CCafOomStep::doTestStepL()
1.56 + {
1.57 + SetTestStepResult(EInconclusive);
1.58 +
1.59 + TInt failCount=1;
1.60 + TInt ret=-1;
1.61 + const TInt MaxAllocationFailures=450;
1.62 +
1.63 +
1.64 + TInt OomTest = KErrNotFound;
1.65 + GetIntFromConfig(ConfigSection(),_L("OomTest"),OomTest);
1.66 +
1.67 +
1.68 + if(OomTest == 2)
1.69 + {
1.70 + INFO_PRINTF2(_L("Starting Apparc..."),failCount);
1.71 + StartApparcServerL();
1.72 + User::After(500000); // Allow async libraries to unload
1.73 + }
1.74 +
1.75 +
1.76 + while(failCount < MaxAllocationFailures && OomTest > 0)
1.77 + {
1.78 + // Mark heap and set allocation to fail
1.79 + __UHEAP_MARK;
1.80 +
1.81 + switch(OomTest)
1.82 + {
1.83 + case KOomReadContent:
1.84 + INFO_PRINTF2(_L("ReadContent() __UHEAP_FAILNEXT: %d"),failCount);
1.85 + __UHEAP_FAILNEXT(failCount);
1.86 + TRAP(ret,OomReadContentL());
1.87 + break;
1.88 + case KOomSupply:
1.89 + INFO_PRINTF2(_L("Supply() __UHEAP_FAILNEXT: %d"),failCount);
1.90 + __UHEAP_FAILNEXT(failCount);
1.91 + TRAP(ret,OomSupplyL());
1.92 + break;
1.93 + default:
1.94 + INFO_PRINTF2(_L("Out of Memory test %d does not exist"),OomTest);
1.95 + SetTestStepResult(EFail);
1.96 + ret = KErrNone;
1.97 + break;
1.98 + };
1.99 +
1.100 + User::After(500000); // Allow async libraries to unload
1.101 +
1.102 +
1.103 + // Check all heap is free'd
1.104 + __UHEAP_MARKEND;
1.105 +
1.106 + // cancel heap failure
1.107 + __UHEAP_RESET;
1.108 + if(ret == KErrNone)
1.109 + {
1.110 + // Allocation failure has been moved all the way through the test
1.111 + // Succeeded after failCount allocations
1.112 + return TestStepResult();
1.113 + }
1.114 + else
1.115 + {
1.116 + // Try failing a little further into the process
1.117 + failCount++;
1.118 + }
1.119 + }
1.120 +
1.121 + // We must have reached our maximum number of allocation failures
1.122 + // There must be some other problem
1.123 + SetTestStepResult(EFail);
1.124 +
1.125 + return TestStepResult();
1.126 + }
1.127 +
1.128 +TVerdict CCafOomStep::OomReadContentL()
1.129 + {
1.130 +
1.131 + TBuf8<2> buffer;
1.132 + TRequestStatus status;
1.133 +
1.134 + TPtrC uri;
1.135 + GetStringFromConfig(ConfigSection(),_L("URI"),uri);
1.136 +
1.137 + CContent* content = CContent::NewLC(uri);
1.138 + CData* data = content->OpenContentL(EPeek);
1.139 + CleanupStack::PushL(data);
1.140 +
1.141 + TInt size = 0;
1.142 + data->DataSizeL(size);
1.143 +
1.144 + // Seek and read from start using both CAF and RFile
1.145 + TInt pos = size/4;
1.146 + data->Seek(ESeekStart, pos);
1.147 + data->Read(buffer);
1.148 +
1.149 + pos=0;
1.150 + data->Seek(ESeekCurrent, pos);
1.151 +
1.152 + data->Read(buffer,1,status);
1.153 + User::WaitForRequest(status);
1.154 +
1.155 + CleanupStack::PopAndDestroy(2, content);
1.156 +
1.157 + SetTestStepResult(EPass);
1.158 +
1.159 + return TestStepResult();
1.160 + }
1.161 +
1.162 +
1.163 +TVerdict CCafOomStep::OomSupplyL()
1.164 + {
1.165 + SetTestStepResult(EInconclusive);
1.166 +
1.167 + RFs fs;
1.168 + RFile file;
1.169 + TBuf8 <128> readBuffer;
1.170 +
1.171 + TPtrC outputDirectory, sourceFileName, suggestedFileName, mimeType;
1.172 + TInt expectedLeave;
1.173 +
1.174 + // Get parameters from INI file
1.175 + GetStringFromConfig(ConfigSection(),_L("OutputPath"),outputDirectory);
1.176 + GetStringFromConfig(ConfigSection(),_L("SourceFileName"),sourceFileName);
1.177 + GetStringFromConfig(ConfigSection(),_L("SuggestedFileName"),suggestedFileName);
1.178 + GetStringFromConfig(ConfigSection(),_L("MimeType"),mimeType);
1.179 + GetIntFromConfig(ConfigSection(),_L("LeaveResult"),expectedLeave);
1.180 +
1.181 +
1.182 + // delete any file previously supplied, don't care if this
1.183 + // has errors so trap.
1.184 + Delete(_L("C:\\supplied.drm"));
1.185 + Delete(_L("C:\\receipt.txt"));
1.186 +
1.187 +
1.188 + HBufC8 *mime = ConvertDes16toHBufC8LC(mimeType);
1.189 +
1.190 + CCafMimeHeader *header = CCafMimeHeader::NewL(mime->Des());
1.191 + CleanupStack::PushL(header);
1.192 +
1.193 + CSupplier *mySupplier = CSupplier::NewLC();
1.194 + mySupplier->SetOutputDirectoryL(outputDirectory);
1.195 +
1.196 + // ignore return value, just exercises code for CCover
1.197 + mySupplier->IsImportSupported(header->StandardMimeData(EContentType));
1.198 +
1.199 + CImportFile *import = NULL;
1.200 + import = mySupplier->ImportFileL(*header, suggestedFileName);
1.201 + CleanupStack::PushL(import);
1.202 +
1.203 + // read the input file and pass it to the CAF
1.204 + fs.Connect();
1.205 + CleanupClosePushL(fs);
1.206 +
1.207 + TInt result = file.Open(fs, sourceFileName, EFileRead | EFileStream | EFileShareAny);
1.208 + CleanupClosePushL(file);
1.209 + while(result == KErrNone)
1.210 + {
1.211 + result = file.Read(readBuffer);
1.212 + if(readBuffer.Length() == 0)
1.213 + break;
1.214 + User::LeaveIfError(import->WriteData(readBuffer));
1.215 + }
1.216 + CleanupStack::PopAndDestroy(&file);
1.217 + User::LeaveIfError(import->WriteDataComplete());
1.218 +
1.219 + TInt n = import->OutputFileCountL();
1.220 + if(n > 0)
1.221 + {
1.222 + // get output file type (content or receipt)
1.223 + TOutputType outputType = import->OutputFileL(0).OutputType();
1.224 +
1.225 + // get output file name, but ignore it because it would cause a warning
1.226 + import->OutputFileL(0).FileName();
1.227 +
1.228 + // get output file mime type
1.229 + TPtrC8 OutputMimeType = import->OutputFileL(0).MimeTypeL();
1.230 + HBufC *outmime = ConvertDes8toHBufC16LC(OutputMimeType );
1.231 + CleanupStack::PopAndDestroy(outmime);
1.232 +
1.233 + if(outputType != EContent)
1.234 + {
1.235 + SetTestStepResult(EFail);
1.236 + }
1.237 + else
1.238 + {
1.239 + // check an attribute, it's only a reference so no need to delete it
1.240 + import->OutputFileL(0).AttributesL(ETrue);
1.241 + }
1.242 + }
1.243 +
1.244 + CleanupStack::PopAndDestroy(&fs);
1.245 + CleanupStack::PopAndDestroy(import);
1.246 + CleanupStack::PopAndDestroy(mySupplier);
1.247 + CleanupStack::PopAndDestroy(header);
1.248 + CleanupStack::PopAndDestroy(mime);
1.249 +
1.250 + if (TestStepResult() != EFail)
1.251 + {
1.252 + SetTestStepResult(EPass);
1.253 + }
1.254 +
1.255 + return TestStepResult();
1.256 + }
1.257 +