os/security/contentmgmt/cafstreamingsupport/test/tscaf/source/tpostacquisition.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Implements the post-acquisition content protection test.
    15 // 
    16 //
    17 
    18 #include "tpostacquisition.h"
    19 #include "testprotstrdesc.h"
    20 #include "testagentkeystreamdecoder.h"
    21 #include <caf/data.h>
    22 #include <caf/content.h>
    23 #include <caf/supplier.h>
    24 #include <caf/metadataarray.h>
    25 #include <caf/supplieroutputfile.h>
    26 #include <caf/streaming/keystreamdecoder.h>
    27 
    28 using namespace StreamAccess;
    29 using namespace ContentAccess;
    30 
    31 CScafPostAcquisition::CScafPostAcquisition(CScafServer& aParent)
    32 /**
    33  	Constructor for the postacquisition test object.
    34  */
    35 	:	CScafStep(aParent)
    36 	{
    37 	SetTestStepName(KScafPostAcquisitionStep);
    38 	}
    39 
    40 CScafPostAcquisition::~CScafPostAcquisition()
    41 /**
    42  	Destructor for the postacquisition test object.
    43  */
    44 	{
    45 	//empty
    46 	}
    47 
    48 void DeleteFileL(RFs& aFs, const TDesC& aPath)
    49 	{
    50 	TInt err = 0;
    51 	err = aFs.Delete(aPath);
    52 	if(err != KErrNone && err != KErrNotFound && err != KErrPathNotFound)
    53 		{
    54 		User::Leave(err);
    55 		}
    56 	}
    57 
    58 TVerdict CScafPostAcquisition::doTestL()
    59 	{
    60 	TBool createOutputFile = EFalse;
    61 	
    62 	if(!GetBoolFromConfig(ConfigSection(), KCreateOutputFiles, createOutputFile))
    63 		{
    64 		ERR_PRINTF2(_L("Failed to read '%S' key of configuration"), &KCreateOutputFiles);
    65 		SetTestStepResult(EFail);
    66 		return TestStepResult();
    67 		}
    68 	
    69 	//Ensure pre-conditions
    70 	CleanAgentsPrivateFolderL(KRtaPrivateFolder());
    71 	CleanAgentsPrivateFolderL(KStaPrivateFolder());
    72 	CleanAgentsPrivateFolderL(KTestServerPrivateFolder());
    73 	CleanAgentsPrivateFolderL(KRtaTestFolder());
    74 	DeleteFileL(iParent.Fs(), _L("c:\\PostAcquisitionTest.dr"));
    75 	DeleteFileL(iParent.Fs(), _L("c:\\PostAcquisitionTest.TXT"));
    76 
    77 	//First copy the RO, which allows to record the protected content, into the test agent's private directory
    78 	CopyFile2AgentsPrivateFolderL(iParent.Fs(), KRecordableRoFileName(), KStaPrivateFolder());
    79 	
    80 	//Secondly copy the program protected RO into the test agent's private directory
    81 	CopyFile2AgentsPrivateFolderL(iParent.Fs(), KPostAcquisitionRoFileName(), KStaPrivateFolder());
    82 	
    83 	//Create an SDP document object and set the created key stream field object
    84 	CSdpDocument* sdpDoc = CreateSdpDocumentLC();
    85 	//Create a SDP object for RO-Recordable
    86 	CSdpMediaField* sdp = CreateSdpLC(0);
    87 	AddMediaFieldL(*sdpDoc, sdp);
    88 	CleanupStack::Pop(sdp);
    89 	
    90 	// Generate a protected key stream description. 
    91 	HBufC* filePath = GetFullPathLC(KStaPrivateFolder, KNullDesC);
    92 	CTestProtectedStreamDesc* protectStreamDesc = CTestProtectedStreamDesc::NewL(*filePath);
    93 	CleanupStack::PopAndDestroy(filePath);
    94 	CleanupStack::PushL(protectStreamDesc);
    95 	
    96 	//Create a key stream decoder
    97 	CKeyStreamDecoder* decoder = CKeyStreamDecoder::NewL(*protectStreamDesc, *sdp, *sdpDoc); 
    98 	CleanupStack::PushL(decoder);
    99 	
   100 	//Check whether the content is recordable
   101 	TBool value = EFalse;
   102 	decoder->GetAttributeL(ECanExport, value);
   103 	if(!value)
   104 		{
   105 		ERR_PRINTF1(_L("The content can not be exported. Unexpected result!"));
   106 		SetTestStepResult(EFail);
   107 		User::Leave(KErrCANotSupported);
   108 		}
   109 	
   110 	CImportFile* import(0);
   111 	_LIT(KPostAcqContentFileName, "postacquisition.content");
   112 	if(!createOutputFile)
   113 		{
   114 		HBufC* outputDir = GetFullPathLC(KRtaPrivateFolder(), KNullDesC());
   115 		import = decoder->CreateImportSessionLC(_L8("text/plain"), KPostAcqContentFileName(), *outputDir);
   116 		CleanupStack::Pop(import);
   117 		CleanupStack::PopAndDestroy(outputDir);
   118 		CleanupStack::PushL(import);
   119 		}
   120 	else
   121 		{
   122 		import = decoder->CreateImportSessionLC(_L8("text/plain")); 
   123 		//To get a better coverage, delete this import object and create another one with zero-length-output-directory.
   124 		CleanupStack::PopAndDestroy(import);
   125 		DeleteFileL(iParent.Fs(), _L("c:\\PostAcquisitionTest.dr"));
   126 		DeleteFileL(iParent.Fs(), _L("c:\\PostAcquisitionTest.TXT"));
   127 		import = decoder->CreateImportSessionLC(_L8("text/plain"), _L("NULL"), KNullDesC()); 
   128 		}
   129 	
   130 	_LIT8(KStreamingData1,"SampleStreamingDataBegan");
   131 	_LIT8(KStreamingData2,"TheEndOfStreamingData");
   132 	_LIT8(KStreamingDataFull,"SampleStreamingDataBeganTheEndOfStreamingData");
   133 	
   134 	User::LeaveIfError(import->WriteData(KStreamingData1()));
   135 	User::LeaveIfError(import->WriteData(KStreamingData2()));
   136 	//Import has been completed
   137 	TFileName fileName;
   138 	TInt err = 0;
   139 	err = import->WriteDataComplete();
   140 	while (err == KErrCANewFileHandleRequired)              
   141         {
   142         err = ProvideNewOutputFileL(*import, fileName);
   143         if(err == KErrNone)
   144         	{
   145         	err = import->WriteDataComplete();
   146         	}
   147         }
   148 	User::LeaveIfError(err);
   149 	
   150 	CleanupStack::PopAndDestroy(3, protectStreamDesc);
   151 	CleanupStack::PopAndDestroy(); //sdpDoc
   152 	
   153 	//Try to read what we have imported
   154 	CContent* content(0);
   155 	if(!createOutputFile)
   156 		{
   157 		HBufC* filePath = GetFullPathLC(KRtaPrivateFolder(), KPostAcqContentFileName());
   158 		content = CContent::NewL(*filePath);
   159 		CleanupStack::PopAndDestroy(filePath);
   160 		CleanupStack::PushL(content);
   161 		}
   162 	else
   163 		{
   164 		content = CContent::NewLC(fileName);
   165 		}
   166 	CData* mydata = content->OpenContentL(EView);
   167 	CleanupStack::PushL(mydata);
   168 	
   169 	RBuf8 rdata;
   170 	rdata.CreateL(KStreamingDataFull().Length());
   171 	rdata.CleanupClosePushL();
   172 	User::LeaveIfError(mydata->Read(rdata));
   173 	if(rdata.Compare(KStreamingDataFull()))
   174 		{
   175 		SetTestStepResult(EFail);		
   176 		}
   177 	CleanupStack::PopAndDestroy(3, content);
   178 	return TestStepResult();
   179 	}
   180 
   181 TInt CScafPostAcquisition::ProvideNewOutputFileL(CImportFile& aImport, TDes& aName)
   182 	{
   183 	TInt err = 0;
   184 	// supply new file in order to continue writing
   185 	aImport.GetSuggestedOutputFileName(aName);
   186     HBufC* newFullName = GetFullPathLC(KRtaTestFolder(), aName);
   187     RFile newFile;
   188     User::LeaveIfError(newFile.Replace(iParent.Fs(), *newFullName, EFileWrite));
   189     err = aImport.ContinueWithNewOutputFile(newFile, *newFullName);
   190     newFile.Close();
   191     aName.Copy(*newFullName);
   192     CleanupStack::PopAndDestroy(newFullName);
   193     return err;
   194 	}