os/ossrv/genericservices/httputils/Test/t_uriparser/CUriUtilsTest.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/httputils/Test/t_uriparser/CUriUtilsTest.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,273 @@
     1.4 +// Copyright (c) 2001-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 +#include "CUriUtilsTest.h"
    1.20 +
    1.21 +#include <e32base.h>
    1.22 +#include <uriutils.h>
    1.23 +
    1.24 +#include "UriUtilsTests.h"
    1.25 +
    1.26 +CUriUtilsTest* CUriUtilsTest::NewLC(CIpuTestHarness* aTestHarness)
    1.27 +	{
    1.28 +	CUriUtilsTest* self = new (ELeave) CUriUtilsTest(aTestHarness);
    1.29 +	CleanupStack::PushL(self);
    1.30 +	self->ConstructL();
    1.31 +	return self;
    1.32 +	}
    1.33 +
    1.34 +CUriUtilsTest* CUriUtilsTest::NewL(CIpuTestHarness* aTestHarness)
    1.35 +	{
    1.36 +	CUriUtilsTest* self = CUriUtilsTest::NewLC(aTestHarness);
    1.37 +	CleanupStack::Pop(self);
    1.38 +	return self;
    1.39 +	}
    1.40 +
    1.41 +CUriUtilsTest::CUriUtilsTest(CIpuTestHarness* aTestHarness)
    1.42 +: iTestHarness(aTestHarness)
    1.43 +	{
    1.44 +	}
    1.45 +
    1.46 +void CUriUtilsTest::ConstructL()
    1.47 +	{
    1.48 +	}
    1.49 +
    1.50 +CUriUtilsTest::~CUriUtilsTest()
    1.51 +	{
    1.52 +	}
    1.53 +
    1.54 +void CUriUtilsTest::DoTestsL()
    1.55 +	{
    1.56 +	TestUriUtilsConvertToInternetFormL(KDisplayForm0, KInternetForm0);
    1.57 +	TestUriUtilsConvertToDisplayFormDeprecatedL(KInternetForm0, KDisplayForm0);
    1.58 +	TestUriUtilsConvertToDisplayFormL(KInternetForm0, KValidDisplayForm0);
    1.59 +
    1.60 +	TestUriUtilsConvertToInternetFormL(KDisplayForm1, KInternetForm1);
    1.61 +	TestUriUtilsConvertToDisplayFormDeprecatedL(KInternetForm1, KDisplayForm1);
    1.62 +	TestUriUtilsConvertToDisplayFormL(KInternetForm1, KValidDisplayForm1);
    1.63 +
    1.64 +	TestUriUtilsHasInvalidCharsL(KInvalidChars0, KInvalid0);
    1.65 +	TestUriUtilsHasInvalidCharsL(KInvalidChars1, KInvalid0);
    1.66 +	TestUriUtilsHasInvalidCharsL(KInvalidChars2, KInvalid0);
    1.67 +	TestUriUtilsHasInvalidCharsL(KInvalidChars3, KInvalid0);
    1.68 +	TestUriUtilsHasInvalidCharsL(KInvalidChars4, KInvalid0);
    1.69 +	TestUriUtilsHasInvalidCharsL(KInvalidChars5, KInvalid0);
    1.70 +	TestUriUtilsHasInvalidCharsL(KInvalidChars6, KInvalid0);
    1.71 +	TestUriUtilsHasInvalidCharsL(KInvalidChars7, KInvalid0);
    1.72 +	TestUriUtilsHasInvalidCharsL(KInvalidChars8, KInvalid0);
    1.73 +	TestUriUtilsHasInvalidCharsL(KInvalidChars9, KInvalid0);
    1.74 +	TestUriUtilsHasInvalidCharsL(KInvalidChars10, KInvalid0);
    1.75 +	TestUriUtilsHasInvalidCharsL(KInvalidChars11, KInvalid0);
    1.76 + 
    1.77 +	TestUriUtilsHasInvalidCharsL(KInvalidChars12, KInvalid1);
    1.78 +	TestUriUtilsHasInvalidCharsL(KInvalidChars13, KInvalid1);
    1.79 +	TestUriUtilsHasInvalidCharsL(KInvalidChars14, KInvalid1);
    1.80 +	TestUriUtilsHasInvalidCharsL(KInvalidChars15, KInvalid1);
    1.81 +	TestUriUtilsHasInvalidCharsL(KInvalidChars16, KInvalid1);
    1.82 +	TestUriUtilsHasInvalidCharsL(KInvalidChars17, KInvalid1);
    1.83 +	TestUriUtilsHasInvalidCharsL(KInvalidChars18, KInvalid1);
    1.84 +	TestUriUtilsHasInvalidCharsL(KInvalidChars19, KInvalid1);
    1.85 +	TestUriUtilsHasInvalidCharsL(KInvalidChars20, KInvalid1);
    1.86 +	TestUriUtilsHasInvalidCharsL(KInvalidChars21, KInvalid1);
    1.87 +	TestUriUtilsHasInvalidCharsL(KInvalidChars22, KInvalid1);
    1.88 +	TestUriUtilsHasInvalidCharsL(KInvalidChars23, KInvalid1);
    1.89 +	TestUriUtilsHasInvalidCharsL(KInvalidChars24, KInvalid1);
    1.90 +	TestUriUtilsHasInvalidCharsL(KInvalidChars25, KInvalid1);
    1.91 +	TestUriUtilsHasInvalidCharsL(KInvalidChars26, KInvalid1);
    1.92 +	TestUriUtilsHasInvalidCharsL(KInvalidChars27, KInvalid1);
    1.93 +	TestUriUtilsHasInvalidCharsL(KInvalidChars28, KInvalid1);
    1.94 +
    1.95 +
    1.96 +	//
    1.97 +	// Tests for 2.9.1.3 Host-Type checking
    1.98 +
    1.99 +	// All these are IPv6 tests
   1.100 +	TestUriUtilsHostTypeL(KHostTypeIPv6Format1, UriUtils::EIPv6Host);			   
   1.101 +	TestUriUtilsHostTypeL(KHostTypeIPv6Format2, UriUtils::EIPv6Host);
   1.102 +	TestUriUtilsHostTypeL(KHostTypeIPv6Format3, UriUtils::EIPv6Host);
   1.103 +	TestUriUtilsHostTypeL(KHostTypeIPv6Format4, UriUtils::EIPv6Host);
   1.104 +	TestUriUtilsHostTypeL(KHostTypeIPv6Format5, UriUtils::EIPv6Host);
   1.105 +	TestUriUtilsHostTypeL(KHostTypeIPv6Format6, UriUtils::EIPv6Host);
   1.106 +	TestUriUtilsHostTypeL(KHostTypeIPv6Format7, UriUtils::EIPv6Host);
   1.107 +
   1.108 +	TestUriUtilsHostTypeL(KHostTypeIPv4Format, UriUtils::EIPv4Host);
   1.109 +
   1.110 +	// Invalid format IPv4 hosts should be considered Text hosts
   1.111 +	TestUriUtilsHostTypeL(KHostTypeIPv4InvalidFormat1, UriUtils::ETextHost);
   1.112 +	TestUriUtilsHostTypeL(KHostTypeIPv4InvalidFormat2, UriUtils::ETextHost);
   1.113 +
   1.114 +	TestUriUtilsHostTypeL(KHostTypeTextFormat1, UriUtils::ETextHost);
   1.115 +	TestUriUtilsHostTypeL(KHostTypeTextFormat2, UriUtils::ETextHost);
   1.116 +	//
   1.117 +	//
   1.118 +	}
   1.119 +
   1.120 +void CUriUtilsTest::TestUriUtilsConvertToInternetFormDeprecatedL(const TDesC& aData, const TDesC& aExpected)
   1.121 +	{
   1.122 +	// Make 8-bit copy
   1.123 +	HBufC8* expectedBuf = HBufC8::NewLC(aExpected.Length());
   1.124 +	TPtr8 expected8Bit = expectedBuf->Des();
   1.125 +	expected8Bit.Copy(aExpected);
   1.126 +
   1.127 +	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm (deprecated)"));
   1.128 +
   1.129 +	TUriParser16 parser;
   1.130 +	parser.Parse(aData);
   1.131 +	CUri8* uri = NULL;
   1.132 +	TRAPD(error, uri = UriUtils::ConvertToInternetFormL(parser));
   1.133 +	if( error == KErrNone )
   1.134 +		{
   1.135 +		if( uri->Uri().UriDes().Compare(expected8Bit) != 0 )
   1.136 +			{
   1.137 +			error = KErrNotFound;
   1.138 +			}
   1.139 +		delete uri;
   1.140 +		}
   1.141 +	iTestHarness->EndTest(error);
   1.142 +
   1.143 +	CleanupStack::PopAndDestroy(expectedBuf);	// expectedBuf
   1.144 +	}
   1.145 +void CUriUtilsTest::TestUriUtilsConvertToInternetFormL(const TDesC& aData, const TDesC& aExpected)
   1.146 +	{
   1.147 +	// Make 8-bit copy
   1.148 +	HBufC8* expectedBuf = HBufC8::NewLC(aExpected.Length());
   1.149 +	TPtr8 expected8Bit = expectedBuf->Des();
   1.150 +	expected8Bit.Copy(aExpected);
   1.151 +
   1.152 +	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm"));
   1.153 +
   1.154 +	CUri8* uri = NULL;
   1.155 +	TRAPD(error, uri = UriUtils::CreateUriL(aData));
   1.156 +	if( error == KErrNone )
   1.157 +		{
   1.158 +		if( uri->Uri().UriDes().Compare(expected8Bit) != 0 )
   1.159 +			{
   1.160 +			error = KErrNotFound;
   1.161 +			}
   1.162 +		delete uri;
   1.163 +		}
   1.164 +	iTestHarness->EndTest(error);
   1.165 +
   1.166 +	CleanupStack::PopAndDestroy(expectedBuf);	// expectedBuf	
   1.167 +	TestUriUtilsConvertToInternetFormDeprecatedL(aData, aExpected );
   1.168 +	}
   1.169 +
   1.170 +void CUriUtilsTest::TestUriUtilsConvertToDisplayFormDeprecatedL(const TDesC& aData, const TDesC& aExpected)
   1.171 +	{
   1.172 +	// Make 8-bit copy
   1.173 +	HBufC8* dataBuf = HBufC8::NewLC(aData.Length());
   1.174 +	TPtr8 data8Bit = dataBuf->Des();
   1.175 +	data8Bit.Copy(aData);
   1.176 +
   1.177 +	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm (deprecated)"));
   1.178 +
   1.179 +	TUriParser8 parser;
   1.180 +	parser.Parse(data8Bit);
   1.181 +	CUri16* uri = NULL;
   1.182 +	TRAPD(error, uri = UriUtils::ConvertToDisplayFormL(parser));
   1.183 +	if( error == KErrNone )
   1.184 +		{
   1.185 +		if( uri->Uri().UriDes().Compare(aExpected) != 0 )
   1.186 +			{
   1.187 +			error = KErrNotFound;
   1.188 +			}
   1.189 +		delete uri;
   1.190 +		}
   1.191 +	iTestHarness->EndTest(error);
   1.192 +
   1.193 +	CleanupStack::PopAndDestroy(dataBuf);	// dataBuf
   1.194 +	}
   1.195 +	
   1.196 +void CUriUtilsTest::TestUriUtilsConvertToDisplayFormL(const TDesC& aData, const TDesC& aExpected)
   1.197 +	{
   1.198 +	// Make 8-bit copy
   1.199 +	HBufC8* dataBuf = HBufC8::NewLC(aData.Length());
   1.200 +	TPtr8 data8Bit = dataBuf->Des();
   1.201 +	data8Bit.Copy(aData);
   1.202 +
   1.203 +	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm"));
   1.204 +
   1.205 +	TUriParser8 parser;
   1.206 +	parser.Parse(data8Bit);
   1.207 +	HBufC* uri = NULL;
   1.208 +	TRAPD(error, uri = parser.DisplayFormL());
   1.209 +	if( error == KErrNone )
   1.210 +		{
   1.211 +		if( uri->Compare(aExpected) != 0 )
   1.212 +			{
   1.213 +			error = KErrNotFound;
   1.214 +			}
   1.215 +		delete uri;
   1.216 +		}
   1.217 +	iTestHarness->EndTest(error);
   1.218 +
   1.219 +	CleanupStack::PopAndDestroy(dataBuf);	// dataBuf
   1.220 +	}
   1.221 +
   1.222 +void CUriUtilsTest::TestUriUtilsHasInvalidCharsL(const TDesC16& aData, TBool aHasInvalidChars)
   1.223 +	{
   1.224 +	iTestHarness->StartTestL(_L("Test UriUtils HasInvalidChars (16-bit)"));
   1.225 +	
   1.226 +	TBool invalid = UriUtils::HasInvalidChars(aData);
   1.227 +
   1.228 +	TInt error = KErrNone;
   1.229 +	if( (!invalid && aHasInvalidChars) | (invalid && !aHasInvalidChars) )
   1.230 +		{
   1.231 +		error = KErrNotFound;
   1.232 +		}
   1.233 +	iTestHarness->EndTest(error);
   1.234 +	}
   1.235 +
   1.236 +void CUriUtilsTest::TestUriUtilsHasInvalidCharsL(const TDesC8& aData, TBool aHasInvalidChars)
   1.237 +	{
   1.238 +	iTestHarness->StartTestL(_L("Test UriUtils HasInvalidChars (8-bit)"));
   1.239 +	
   1.240 +	TBool invalid = UriUtils::HasInvalidChars(aData);
   1.241 +
   1.242 +	TInt error = KErrNone;
   1.243 +	if( (!invalid && aHasInvalidChars) | (invalid && !aHasInvalidChars) )
   1.244 +		{
   1.245 +		error = KErrNotFound;
   1.246 +		}
   1.247 +	iTestHarness->EndTest(error);
   1.248 +	}
   1.249 +
   1.250 +void CUriUtilsTest::TestUriUtilsHostTypeL(const TDesC8& aData, UriUtils::TUriHostType aHostType)
   1.251 +	{
   1.252 +	iTestHarness->StartTestL(_L("Test UriUtils HostType (8-bit)"));
   1.253 +	
   1.254 +	UriUtils::TUriHostType hostType = UriUtils::HostType(aData);
   1.255 +
   1.256 +	TInt error = KErrNone;
   1.257 +	if ( hostType != aHostType )
   1.258 +		{
   1.259 +		error = KErrNotFound;	
   1.260 +		}	
   1.261 +	iTestHarness->EndTest(error);
   1.262 +	}
   1.263 +	
   1.264 +void CUriUtilsTest::TestUriUtilsHostTypeL(const TDesC16& aData, UriUtils::TUriHostType aHostType)
   1.265 +	{
   1.266 +	iTestHarness->StartTestL(_L("Test UriUtils HostType (16-bit)"));
   1.267 +
   1.268 +	UriUtils::TUriHostType hostType = UriUtils::HostType(aData);
   1.269 +
   1.270 +	TInt error = KErrNone;
   1.271 +	if ( hostType != aHostType )
   1.272 +		{
   1.273 +		error = KErrNotFound;
   1.274 +		}	
   1.275 +	iTestHarness->EndTest(error);
   1.276 +	}