os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestCreateFileStep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestCreateFileStep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,143 @@
     1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +/**
    1.20 + @file
    1.21 + @internalTechnology 
    1.22 +*/
    1.23 +
    1.24 +// User Include
    1.25 +#include "TestCreateFileStep.h"
    1.26 +
    1.27 +/**
    1.28 +Constructor. Sets the test step name
    1.29 +*/
    1.30 +CTestCreateFileStep::CTestCreateFileStep()
    1.31 +	{
    1.32 +	//Call base class method to set human readable name for test step
    1.33 +	SetTestStepName(KTestCreateFileStep);
    1.34 +	}
    1.35 +
    1.36 +/**
    1.37 +Tries to create a file mentioned in the ini file.
    1.38 +@internalTechnology
    1.39 +@param		None
    1.40 +@return		EPass or EFail indicating the success or failure of file creation.
    1.41 +*/
    1.42 +TVerdict CTestCreateFileStep::doTestStepL()
    1.43 +	{
    1.44 +	// Get file path and name from ini file
    1.45 +	TPtrC fileName;
    1.46 +	TPtrC fileType;
    1.47 +	TPtrC drive;
    1.48 +	TInt err = KErrNone;
    1.49 +	RFs fs;
    1.50 +	RFile file;
    1.51 +		
    1.52 +	if (!GetStringFromConfig(ConfigSection(), KIniFileName, fileName))
    1.53 +		{
    1.54 +		ERR_PRINTF1(_L("Unable to read filename from ini file"));
    1.55 +		SetTestStepResult(EFail);
    1.56 +		}
    1.57 +	else
    1.58 +		{
    1.59 +		INFO_PRINTF2(_L("File name = %S"), &fileName);
    1.60 +		
    1.61 +		// check whether the filetype field exists in INI
    1.62 +		if(GetStringFromConfig(ConfigSection(), KIniFileType, fileType))
    1.63 +			{
    1.64 +			INFO_PRINTF2(_L("File type = %S"), &fileType);
    1.65 +			}
    1.66 +			
    1.67 +		if(fileType == KFileTypePrivate)
    1.68 +			{// The file is a private file. We require the drive too, as the path is relative
    1.69 +			if (!GetStringFromConfig(ConfigSection(), KIniDrive, drive))
    1.70 +				{
    1.71 +				ERR_PRINTF1(_L("If file type is private, drive should be provided. Unable to read drive"));
    1.72 +				SetTestStepResult(EFail);
    1.73 +				return TestStepResult();
    1.74 +				}
    1.75 +			TFileName fullyQualifiedName(fileName);
    1.76 +			// In the case of a secure vesrion of the OS
    1.77 +			// As the INI file contains relative file-name for private files
    1.78 +			// under the ExpectedFileName field, construct the fully-qualified
    1.79 +			// expected file-name
    1.80 +			if((err = CTestFileUriServer::CreateFullyQualifiedName(fileName, drive, fullyQualifiedName)) != KErrNone)
    1.81 +				{
    1.82 +				ERR_PRINTF2(_L("Error returned by CTestFileUriServer::CreateFullyQualifiedName: %D"), err);
    1.83 +				SetTestStepResult(EFail);
    1.84 +				return TestStepResult();
    1.85 +				}
    1.86 +			fileName.Set(fullyQualifiedName);
    1.87 +			INFO_PRINTF2(_L("Fully qualified name = %S"), &fileName);
    1.88 +			}
    1.89 +
    1.90 +		// Connect to file server
    1.91 +		err = fs.Connect();
    1.92 +		if(err != KErrNone)
    1.93 +			{
    1.94 +			ERR_PRINTF2(_L("Error occured while connecting to file server: %D"), err);
    1.95 +			SetTestStepResult(EFail);
    1.96 +			}
    1.97 +
    1.98 +		if(TestStepResult() == EPass)
    1.99 +			{
   1.100 +			// Try opening first to see whether file already exists.
   1.101 +			err = file.Open(fs, fileName, EFileRead);
   1.102 +			switch(err)
   1.103 +				{
   1.104 +				case KErrNone:
   1.105 +					// File already exists, this is NOT considered as fail
   1.106 +					INFO_PRINTF1(_L("File already exists"));
   1.107 +					break;
   1.108 +				case KErrNotReady:
   1.109 +					// Drive is not ready, this is NOT considered as fail
   1.110 +					INFO_PRINTF1(_L("Drive not ready"));
   1.111 +					break;	
   1.112 +				case KErrPathNotFound:
   1.113 +					// Create directories
   1.114 +					err = fs.MkDirAll(fileName);
   1.115 +					if (err != KErrNone)
   1.116 +						{
   1.117 +						ERR_PRINTF2(_L("Could not create directories. Error returned: %D"), err);
   1.118 +						SetTestStepResult(EFail);
   1.119 +						// Only if unable to create directories we break
   1.120 +						// else, next case should also be executed which creates the file
   1.121 +						break;
   1.122 +						}
   1.123 +					// No break
   1.124 +				case KErrNotFound:
   1.125 +					// Create the file
   1.126 +					err = file.Create(fs, fileName, EFileWrite);
   1.127 +					if(err != KErrNone)
   1.128 +						{
   1.129 +						ERR_PRINTF2(_L("Error occured while creating file: %D"), err);
   1.130 +						SetTestStepResult(EFail);
   1.131 +						}
   1.132 +					else
   1.133 +						{
   1.134 +						INFO_PRINTF1(_L("Successfully created file"));	
   1.135 +						}
   1.136 +					break;
   1.137 +				default:
   1.138 +					ERR_PRINTF2(_L("Error occured: %D"), err);
   1.139 +					SetTestStepResult(EFail);		
   1.140 +				}
   1.141 +				file.Close();
   1.142 +				fs.Close();
   1.143 +			}
   1.144 +		}
   1.145 +		return TestStepResult();
   1.146 +	}	// doTestStepL