os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestCreateFileStep.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) 2004-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
/**
sl@0
    17
 @file
sl@0
    18
 @internalTechnology 
sl@0
    19
*/
sl@0
    20
sl@0
    21
// User Include
sl@0
    22
#include "TestCreateFileStep.h"
sl@0
    23
sl@0
    24
/**
sl@0
    25
Constructor. Sets the test step name
sl@0
    26
*/
sl@0
    27
CTestCreateFileStep::CTestCreateFileStep()
sl@0
    28
	{
sl@0
    29
	//Call base class method to set human readable name for test step
sl@0
    30
	SetTestStepName(KTestCreateFileStep);
sl@0
    31
	}
sl@0
    32
sl@0
    33
/**
sl@0
    34
Tries to create a file mentioned in the ini file.
sl@0
    35
@internalTechnology
sl@0
    36
@param		None
sl@0
    37
@return		EPass or EFail indicating the success or failure of file creation.
sl@0
    38
*/
sl@0
    39
TVerdict CTestCreateFileStep::doTestStepL()
sl@0
    40
	{
sl@0
    41
	// Get file path and name from ini file
sl@0
    42
	TPtrC fileName;
sl@0
    43
	TPtrC fileType;
sl@0
    44
	TPtrC drive;
sl@0
    45
	TInt err = KErrNone;
sl@0
    46
	RFs fs;
sl@0
    47
	RFile file;
sl@0
    48
		
sl@0
    49
	if (!GetStringFromConfig(ConfigSection(), KIniFileName, fileName))
sl@0
    50
		{
sl@0
    51
		ERR_PRINTF1(_L("Unable to read filename from ini file"));
sl@0
    52
		SetTestStepResult(EFail);
sl@0
    53
		}
sl@0
    54
	else
sl@0
    55
		{
sl@0
    56
		INFO_PRINTF2(_L("File name = %S"), &fileName);
sl@0
    57
		
sl@0
    58
		// check whether the filetype field exists in INI
sl@0
    59
		if(GetStringFromConfig(ConfigSection(), KIniFileType, fileType))
sl@0
    60
			{
sl@0
    61
			INFO_PRINTF2(_L("File type = %S"), &fileType);
sl@0
    62
			}
sl@0
    63
			
sl@0
    64
		if(fileType == KFileTypePrivate)
sl@0
    65
			{// The file is a private file. We require the drive too, as the path is relative
sl@0
    66
			if (!GetStringFromConfig(ConfigSection(), KIniDrive, drive))
sl@0
    67
				{
sl@0
    68
				ERR_PRINTF1(_L("If file type is private, drive should be provided. Unable to read drive"));
sl@0
    69
				SetTestStepResult(EFail);
sl@0
    70
				return TestStepResult();
sl@0
    71
				}
sl@0
    72
			TFileName fullyQualifiedName(fileName);
sl@0
    73
			// In the case of a secure vesrion of the OS
sl@0
    74
			// As the INI file contains relative file-name for private files
sl@0
    75
			// under the ExpectedFileName field, construct the fully-qualified
sl@0
    76
			// expected file-name
sl@0
    77
			if((err = CTestFileUriServer::CreateFullyQualifiedName(fileName, drive, fullyQualifiedName)) != KErrNone)
sl@0
    78
				{
sl@0
    79
				ERR_PRINTF2(_L("Error returned by CTestFileUriServer::CreateFullyQualifiedName: %D"), err);
sl@0
    80
				SetTestStepResult(EFail);
sl@0
    81
				return TestStepResult();
sl@0
    82
				}
sl@0
    83
			fileName.Set(fullyQualifiedName);
sl@0
    84
			INFO_PRINTF2(_L("Fully qualified name = %S"), &fileName);
sl@0
    85
			}
sl@0
    86
sl@0
    87
		// Connect to file server
sl@0
    88
		err = fs.Connect();
sl@0
    89
		if(err != KErrNone)
sl@0
    90
			{
sl@0
    91
			ERR_PRINTF2(_L("Error occured while connecting to file server: %D"), err);
sl@0
    92
			SetTestStepResult(EFail);
sl@0
    93
			}
sl@0
    94
sl@0
    95
		if(TestStepResult() == EPass)
sl@0
    96
			{
sl@0
    97
			// Try opening first to see whether file already exists.
sl@0
    98
			err = file.Open(fs, fileName, EFileRead);
sl@0
    99
			switch(err)
sl@0
   100
				{
sl@0
   101
				case KErrNone:
sl@0
   102
					// File already exists, this is NOT considered as fail
sl@0
   103
					INFO_PRINTF1(_L("File already exists"));
sl@0
   104
					break;
sl@0
   105
				case KErrNotReady:
sl@0
   106
					// Drive is not ready, this is NOT considered as fail
sl@0
   107
					INFO_PRINTF1(_L("Drive not ready"));
sl@0
   108
					break;	
sl@0
   109
				case KErrPathNotFound:
sl@0
   110
					// Create directories
sl@0
   111
					err = fs.MkDirAll(fileName);
sl@0
   112
					if (err != KErrNone)
sl@0
   113
						{
sl@0
   114
						ERR_PRINTF2(_L("Could not create directories. Error returned: %D"), err);
sl@0
   115
						SetTestStepResult(EFail);
sl@0
   116
						// Only if unable to create directories we break
sl@0
   117
						// else, next case should also be executed which creates the file
sl@0
   118
						break;
sl@0
   119
						}
sl@0
   120
					// No break
sl@0
   121
				case KErrNotFound:
sl@0
   122
					// Create the file
sl@0
   123
					err = file.Create(fs, fileName, EFileWrite);
sl@0
   124
					if(err != KErrNone)
sl@0
   125
						{
sl@0
   126
						ERR_PRINTF2(_L("Error occured while creating file: %D"), err);
sl@0
   127
						SetTestStepResult(EFail);
sl@0
   128
						}
sl@0
   129
					else
sl@0
   130
						{
sl@0
   131
						INFO_PRINTF1(_L("Successfully created file"));	
sl@0
   132
						}
sl@0
   133
					break;
sl@0
   134
				default:
sl@0
   135
					ERR_PRINTF2(_L("Error occured: %D"), err);
sl@0
   136
					SetTestStepResult(EFail);		
sl@0
   137
				}
sl@0
   138
				file.Close();
sl@0
   139
				fs.Close();
sl@0
   140
			}
sl@0
   141
		}
sl@0
   142
		return TestStepResult();
sl@0
   143
	}	// doTestStepL