os/ossrv/genericservices/httputils/Test/t_wspcodec/cwspencodetest.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) 2002-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
// Implementation of test CWSPEncodeTest.
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
sl@0
    19
#include "cwspencodetest.h"
sl@0
    20
#include <wspencoder.h>
sl@0
    21
sl@0
    22
sl@0
    23
CWSPEncodeTest* CWSPEncodeTest::NewLC(CIpuTestHarness* aTestHarness)
sl@0
    24
	{
sl@0
    25
	CWSPEncodeTest* self = new(ELeave)CWSPEncodeTest(aTestHarness);
sl@0
    26
	CleanupStack::PushL(self);
sl@0
    27
	return self;
sl@0
    28
	}
sl@0
    29
sl@0
    30
sl@0
    31
CWSPEncodeTest::CWSPEncodeTest(CIpuTestHarness* aTestHarness):
sl@0
    32
	iTestHarness(aTestHarness)
sl@0
    33
	{}
sl@0
    34
sl@0
    35
CWSPEncodeTest::~CWSPEncodeTest()
sl@0
    36
	{}
sl@0
    37
sl@0
    38
void CWSPEncodeTest::DoTestsL()
sl@0
    39
	{
sl@0
    40
	HeaderEncoderTestL();
sl@0
    41
	ShortIntTestL();
sl@0
    42
	LongIntTestL();
sl@0
    43
	UintVarTestL();
sl@0
    44
	StringTestL();
sl@0
    45
	DateTestL();
sl@0
    46
	}
sl@0
    47
	
sl@0
    48
sl@0
    49
void CWSPEncodeTest::HeaderEncoderTestL()
sl@0
    50
	{
sl@0
    51
	_LIT(KTestText, "WSP Encode: Header");
sl@0
    52
	iTestHarness->StartTestL(KTestText);
sl@0
    53
	TRAPD(error, TestHeaderEncoderL());
sl@0
    54
	iTestHarness->EndTest(error);
sl@0
    55
	User::LeaveIfError(error);
sl@0
    56
	}
sl@0
    57
sl@0
    58
void CWSPEncodeTest::TestHeaderEncoderL()
sl@0
    59
	{
sl@0
    60
	TInt err=0;
sl@0
    61
sl@0
    62
	CWspHeaderEncoder* primEncoder = CWspHeaderEncoder::NewLC();
sl@0
    63
sl@0
    64
	primEncoder->StartHeaderL(0x27);
sl@0
    65
	primEncoder->StartValueLengthL();
sl@0
    66
	primEncoder->AddUintVarL(0xff);
sl@0
    67
	primEncoder->AddLongIntL(999999);
sl@0
    68
	primEncoder->AddIntegerL(0x7F);
sl@0
    69
	primEncoder->EndValueLengthL();
sl@0
    70
	HBufC8* buf = primEncoder->EndHeaderL();
sl@0
    71
	CleanupStack::PushL(buf);
sl@0
    72
	CleanupStack::PopAndDestroy(2); //primEncoder, buf
sl@0
    73
sl@0
    74
	User::LeaveIfError(err);
sl@0
    75
	}
sl@0
    76
sl@0
    77
sl@0
    78
void CWSPEncodeTest::ShortIntTestL()
sl@0
    79
	{
sl@0
    80
	_LIT(KTestText, "WSP Encode: ShortInt");
sl@0
    81
	iTestHarness->StartTestL(KTestText);
sl@0
    82
	TInt error = TestShortInt();
sl@0
    83
	iTestHarness->EndTest(error);
sl@0
    84
	User::LeaveIfError(error);
sl@0
    85
	}
sl@0
    86
sl@0
    87
sl@0
    88
TInt CWSPEncodeTest::TestShortInt()
sl@0
    89
	{
sl@0
    90
	TUint8 input;
sl@0
    91
	TUint8 output;
sl@0
    92
sl@0
    93
	// TWspPrimitiveEncoder::ShortInt
sl@0
    94
	// Check 7 and 8 bit values. If 8bit notinh happens
sl@0
    95
	input=0; // MIN input value
sl@0
    96
	output = TWspPrimitiveEncoder::ShortInt(input);
sl@0
    97
	if (output !=128)
sl@0
    98
		return KErrGeneral;
sl@0
    99
		
sl@0
   100
	input=35; // MAX input value
sl@0
   101
	output = TWspPrimitiveEncoder::ShortInt(input);
sl@0
   102
	if (output !=163)
sl@0
   103
		return KErrGeneral;
sl@0
   104
sl@0
   105
	input=0x7F; // MAX input value
sl@0
   106
	output = TWspPrimitiveEncoder::ShortInt(input);
sl@0
   107
	if (output !=255)
sl@0
   108
		return KErrGeneral;
sl@0
   109
sl@0
   110
	input=0x80; // invalid value
sl@0
   111
	output = TWspPrimitiveEncoder::ShortInt(input);
sl@0
   112
	if (output !=0)
sl@0
   113
		return KErrGeneral;
sl@0
   114
sl@0
   115
	return KErrNone;
sl@0
   116
	}
sl@0
   117
sl@0
   118
sl@0
   119
void CWSPEncodeTest::LongIntTestL()
sl@0
   120
	{
sl@0
   121
	_LIT(KTestText, "WSP Encode: LongInt");
sl@0
   122
	iTestHarness->StartTestL(KTestText);
sl@0
   123
	TRAPD(error,TestLongIntL());
sl@0
   124
	iTestHarness->EndTest(error);
sl@0
   125
	User::LeaveIfError(error);
sl@0
   126
	}
sl@0
   127
sl@0
   128
void CWSPEncodeTest::TestLongIntL()
sl@0
   129
	{
sl@0
   130
	HBufC8* output=NULL;
sl@0
   131
	TBuf8<255> buf;
sl@0
   132
	TUint32 input;
sl@0
   133
sl@0
   134
	const TUint8 KZeroValueLongInt[] = {0x01, 0x00};
sl@0
   135
	const TUint8 K127LongInt[] = {0x01, 0x7F};
sl@0
   136
	const TUint8 K255LongInt[] = {0x01, 0xFF};
sl@0
   137
	const TUint8 K256LongInt[] = {0x02, 0x01, 0x00};
sl@0
   138
	const TUint8 KFFFFLongInt[] = {0x02, 0xFF, 0xFF};
sl@0
   139
	const TUint8 K10000LongInt[] = {0x03, 0x01, 0x00, 0x00};
sl@0
   140
	const TUint8 KFFFFFFLongInt[] = {0x03, 0xFF, 0xFF, 0xFF};
sl@0
   141
	const TUint8 K1000000LongInt[] = {0x04, 0x01, 0x00, 0x00, 0x00};
sl@0
   142
	const TUint8 KFFFFFFFFLongInt[] = {0x04, 0xFF, 0xFF, 0xFF, 0xFF};
sl@0
   143
sl@0
   144
	input=0;
sl@0
   145
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   146
	CleanupStack::PushL(output);
sl@0
   147
	buf.Copy(KZeroValueLongInt, sizeof(KZeroValueLongInt));
sl@0
   148
	if ((*output).CompareF(buf)!=0)
sl@0
   149
		User::Leave(KErrGeneral);
sl@0
   150
	CleanupStack::PopAndDestroy(output);
sl@0
   151
sl@0
   152
	input=127;
sl@0
   153
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   154
	CleanupStack::PushL(output);
sl@0
   155
	buf.Copy(K127LongInt, sizeof(K127LongInt));
sl@0
   156
	if ((*output).CompareF(buf)!=0)
sl@0
   157
		User::Leave(KErrGeneral);
sl@0
   158
	CleanupStack::PopAndDestroy(output);
sl@0
   159
sl@0
   160
	input=255;
sl@0
   161
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   162
	CleanupStack::PushL(output);
sl@0
   163
	buf.Copy(K255LongInt, sizeof(K255LongInt));
sl@0
   164
	if ((*output).CompareF(buf)!=0)
sl@0
   165
		User::Leave(KErrGeneral);
sl@0
   166
	CleanupStack::PopAndDestroy(output);
sl@0
   167
sl@0
   168
	input=256;
sl@0
   169
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   170
	CleanupStack::PushL(output);
sl@0
   171
	buf.Copy(K256LongInt, sizeof(K256LongInt));
sl@0
   172
	if ((*output).CompareF(buf)!=0)
sl@0
   173
		User::Leave(KErrGeneral);
sl@0
   174
	CleanupStack::PopAndDestroy(output);
sl@0
   175
sl@0
   176
	input=0xFFFF;
sl@0
   177
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   178
	CleanupStack::PushL(output);
sl@0
   179
	buf.Copy(KFFFFLongInt, sizeof(KFFFFLongInt));
sl@0
   180
	if ((*output).CompareF(buf)!=0)
sl@0
   181
		User::Leave(KErrGeneral);
sl@0
   182
	CleanupStack::PopAndDestroy(output);
sl@0
   183
sl@0
   184
	input=0x10000;
sl@0
   185
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   186
	CleanupStack::PushL(output);
sl@0
   187
	buf.Copy(K10000LongInt, sizeof(K10000LongInt));
sl@0
   188
	if ((*output).CompareF(buf)!=0)
sl@0
   189
		User::Leave(KErrGeneral);
sl@0
   190
	CleanupStack::PopAndDestroy(output);
sl@0
   191
sl@0
   192
	input=0xFFFFFF;
sl@0
   193
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   194
	CleanupStack::PushL(output);
sl@0
   195
	buf.Copy(KFFFFFFLongInt, sizeof(KFFFFFFLongInt));
sl@0
   196
	if ((*output).CompareF(buf)!=0)
sl@0
   197
		User::Leave(KErrGeneral);
sl@0
   198
	CleanupStack::PopAndDestroy(output);
sl@0
   199
sl@0
   200
	input=0x1000000;
sl@0
   201
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   202
	CleanupStack::PushL(output);
sl@0
   203
	buf.Copy(K1000000LongInt, sizeof(K1000000LongInt));
sl@0
   204
	if ((*output).CompareF(buf)!=0)
sl@0
   205
		User::Leave(KErrGeneral);
sl@0
   206
	CleanupStack::PopAndDestroy(output);
sl@0
   207
sl@0
   208
	input=0xFFFFFFFF;
sl@0
   209
	output = TWspPrimitiveEncoder::LongIntL(input);
sl@0
   210
	CleanupStack::PushL(output);
sl@0
   211
	buf.Copy(KFFFFFFFFLongInt, sizeof(KFFFFFFFFLongInt));
sl@0
   212
	if ((*output).CompareF(buf)!=0)
sl@0
   213
		User::Leave(KErrGeneral);
sl@0
   214
	CleanupStack::PopAndDestroy(output);
sl@0
   215
	}
sl@0
   216
sl@0
   217
sl@0
   218
void CWSPEncodeTest::UintVarTestL()
sl@0
   219
	{
sl@0
   220
	_LIT(KTestText, "WSP Encode: UintVar");
sl@0
   221
	iTestHarness->StartTestL(KTestText);
sl@0
   222
	TRAPD(error,TestUintVarL());
sl@0
   223
	iTestHarness->EndTest(error);
sl@0
   224
	User::LeaveIfError(error);
sl@0
   225
	}
sl@0
   226
sl@0
   227
void CWSPEncodeTest::TestUintVarL()
sl@0
   228
	{
sl@0
   229
sl@0
   230
	HBufC8* output=NULL;
sl@0
   231
	TBuf8<255> buf;
sl@0
   232
	TUint32 input;
sl@0
   233
sl@0
   234
	const TUint8 KMinValue[] = {0x00};
sl@0
   235
	const TUint8 KMaxValue[] = {0x8F, 0xFF, 0xFF, 0xFF, 0x7F};
sl@0
   236
sl@0
   237
	const TUint8 K7bitBoundaryLower[] = {0x7F};
sl@0
   238
	const TUint8 K7bitBoundaryUpper[] = {0x81, 0x00};
sl@0
   239
sl@0
   240
	const TUint8 K14bitBoundaryLower[] = {0xFF, 0x7F};
sl@0
   241
	const TUint8 K14bitBoundaryUpper[] = {0x81, 0x80, 0x00};
sl@0
   242
sl@0
   243
	const TUint8 K21bitBoundaryLower[] = {0xFF, 0xFF, 0x7F};
sl@0
   244
	const TUint8 K21bitBoundaryUpper[] = {0x81, 0x80, 0x80, 0x00};
sl@0
   245
	const TUint8 K28bitBoundaryLower[] = {0xFF, 0xFF, 0xFF, 0x7F};
sl@0
   246
	const TUint8 K28bitBoundaryUpper[] = {0x81, 0x80, 0x80, 0x80, 0x00};
sl@0
   247
sl@0
   248
	input=0;
sl@0
   249
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   250
	CleanupStack::PushL(output);
sl@0
   251
	buf.Copy(KMinValue, sizeof(KMinValue));
sl@0
   252
	if ((*output).CompareF(buf)!=0)
sl@0
   253
		User::Leave(KErrGeneral);
sl@0
   254
	CleanupStack::PopAndDestroy(output);
sl@0
   255
sl@0
   256
	input=0xFFFFFFFF;
sl@0
   257
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   258
	CleanupStack::PushL(output);
sl@0
   259
	buf.Copy(KMaxValue, sizeof(KMaxValue));
sl@0
   260
	if ((*output).CompareF(buf)!=0)
sl@0
   261
		User::Leave(KErrGeneral);
sl@0
   262
	CleanupStack::PopAndDestroy(output);
sl@0
   263
sl@0
   264
	input=0x7F;
sl@0
   265
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   266
	CleanupStack::PushL(output);
sl@0
   267
	buf.Copy(K7bitBoundaryLower, sizeof(K7bitBoundaryLower));
sl@0
   268
	if ((*output).CompareF(buf)!=0)
sl@0
   269
		User::Leave(KErrGeneral);
sl@0
   270
	CleanupStack::PopAndDestroy(output);
sl@0
   271
sl@0
   272
	input=0x80;
sl@0
   273
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   274
	CleanupStack::PushL(output);
sl@0
   275
	buf.Copy(K7bitBoundaryUpper, sizeof(K7bitBoundaryUpper));
sl@0
   276
	if ((*output).CompareF(buf)!=0)
sl@0
   277
		User::Leave(KErrGeneral);
sl@0
   278
	CleanupStack::PopAndDestroy(output);
sl@0
   279
sl@0
   280
	input=0x3FFF;
sl@0
   281
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   282
	CleanupStack::PushL(output);
sl@0
   283
	buf.Copy(K14bitBoundaryLower, sizeof(K14bitBoundaryLower));
sl@0
   284
	if ((*output).CompareF(buf)!=0)
sl@0
   285
		User::Leave(KErrGeneral);
sl@0
   286
	CleanupStack::PopAndDestroy(output);
sl@0
   287
sl@0
   288
	input=0x4000;
sl@0
   289
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   290
	CleanupStack::PushL(output);
sl@0
   291
	buf.Copy(K14bitBoundaryUpper, sizeof(K14bitBoundaryUpper));
sl@0
   292
	if ((*output).CompareF(buf)!=0)
sl@0
   293
		User::Leave(KErrGeneral);
sl@0
   294
	CleanupStack::PopAndDestroy(output);
sl@0
   295
sl@0
   296
	input=0x1FFFFF;
sl@0
   297
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   298
	CleanupStack::PushL(output);
sl@0
   299
	buf.Copy(K21bitBoundaryLower, sizeof(K21bitBoundaryLower));
sl@0
   300
	if ((*output).CompareF(buf)!=0)
sl@0
   301
		User::Leave(KErrGeneral);
sl@0
   302
	CleanupStack::PopAndDestroy(output);
sl@0
   303
sl@0
   304
	input=0x200000;
sl@0
   305
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   306
	CleanupStack::PushL(output);
sl@0
   307
	buf.Copy(K21bitBoundaryUpper, sizeof(K21bitBoundaryUpper));
sl@0
   308
	if ((*output).CompareF(buf)!=0)
sl@0
   309
		User::Leave(KErrGeneral);
sl@0
   310
	CleanupStack::PopAndDestroy(output);
sl@0
   311
sl@0
   312
	input=0xFFFFFFF;
sl@0
   313
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   314
	CleanupStack::PushL(output);
sl@0
   315
	buf.Copy(K28bitBoundaryLower, sizeof(K28bitBoundaryLower));
sl@0
   316
	if ((*output).CompareF(buf)!=0)
sl@0
   317
		User::Leave(KErrGeneral);
sl@0
   318
	CleanupStack::PopAndDestroy(output);
sl@0
   319
sl@0
   320
	input=0x10000000;
sl@0
   321
	output = TWspPrimitiveEncoder::UintVarL(input);
sl@0
   322
	CleanupStack::PushL(output);
sl@0
   323
	buf.Copy(K28bitBoundaryUpper, sizeof(K28bitBoundaryUpper));
sl@0
   324
	if ((*output).CompareF(buf)!=0)
sl@0
   325
		User::Leave(KErrGeneral);
sl@0
   326
	CleanupStack::PopAndDestroy(output);
sl@0
   327
sl@0
   328
	}
sl@0
   329
sl@0
   330
sl@0
   331
void CWSPEncodeTest::StringTestL()
sl@0
   332
	{
sl@0
   333
	_LIT(KTestText, "WSP Encode: String");
sl@0
   334
	iTestHarness->StartTestL(KTestText);
sl@0
   335
	TRAPD(error,TestStringL());
sl@0
   336
	iTestHarness->EndTest(error);
sl@0
   337
	User::LeaveIfError(error);
sl@0
   338
	}
sl@0
   339
sl@0
   340
void CWSPEncodeTest::TestStringL()
sl@0
   341
	{
sl@0
   342
	TBuf8<255> buf;
sl@0
   343
sl@0
   344
	// Test 1. A regular string
sl@0
   345
	const TUint8 KTestStringResult1[] = {'X','-','n','e','w','-','H','e','a','d','e','r','F','o','o',0x00};
sl@0
   346
	_LIT8(KTestString1,"X-new-HeaderFoo");
sl@0
   347
sl@0
   348
	// Test 2. A quoted string
sl@0
   349
	const TUint8 KTestStringResult2[] = {'\"','X','-','n','e','w','-','H','e','a','d','e','r','F','o','o',0x00};
sl@0
   350
	_LIT8(KTestString2,"\"X-new-HeaderFoo\"");
sl@0
   351
sl@0
   352
	// Test 3. A string beginning with an Upper-Ascii character
sl@0
   353
	const TUint8 KTestStringResult3[] = {0x7F,0x80,'X','-','n','e','w','-','H','e','a','d','e','r','F','o','o',0x00};
sl@0
   354
	const TUint8 KTestString3[] = {0x80,'X','-','n','e','w','-','H','e','a','d','e','r','F','o','o'};
sl@0
   355
sl@0
   356
	HBufC8* output = TWspPrimitiveEncoder::TextStringL(KTestString1);
sl@0
   357
	CleanupStack::PushL(output);
sl@0
   358
	buf.Copy(KTestStringResult1, sizeof(KTestStringResult1));
sl@0
   359
	if ((*output).CompareF(buf)!=0)
sl@0
   360
		User::Leave(KErrGeneral);
sl@0
   361
	CleanupStack::PopAndDestroy(output);
sl@0
   362
sl@0
   363
sl@0
   364
	output = TWspPrimitiveEncoder::TextStringL(KTestString2);
sl@0
   365
	CleanupStack::PushL(output);
sl@0
   366
	buf.Copy(KTestStringResult2, sizeof(KTestStringResult2));
sl@0
   367
	if ((*output).CompareF(buf)!=0)
sl@0
   368
		User::Leave(KErrGeneral);
sl@0
   369
	CleanupStack::PopAndDestroy(output);
sl@0
   370
sl@0
   371
	buf.Copy(KTestString3, sizeof(KTestString3));
sl@0
   372
	output = TWspPrimitiveEncoder::TextStringL(buf);
sl@0
   373
	CleanupStack::PushL(output);
sl@0
   374
	buf.Copy(KTestStringResult3, sizeof(KTestStringResult3));
sl@0
   375
	if ((*output).CompareF(buf)!=0)
sl@0
   376
		User::Leave(KErrGeneral);
sl@0
   377
	CleanupStack::PopAndDestroy(output);
sl@0
   378
	}
sl@0
   379
sl@0
   380
sl@0
   381
void CWSPEncodeTest::DateTestL()
sl@0
   382
	{
sl@0
   383
	_LIT(KTestText, "WSP Encode: Date");
sl@0
   384
	iTestHarness->StartTestL(KTestText);
sl@0
   385
	TRAPD(error,TestDateL());
sl@0
   386
	iTestHarness->EndTest(error);
sl@0
   387
	User::LeaveIfError(error);
sl@0
   388
	}
sl@0
   389
sl@0
   390
sl@0
   391
void CWSPEncodeTest::TestDateL()
sl@0
   392
	{
sl@0
   393
	TBuf8<255> buf;
sl@0
   394
sl@0
   395
	const TUint8 KDate0[] = {0x01, 0x00};
sl@0
   396
sl@0
   397
	TDateTime date(1970,EJanuary,0,0,0,0,0);
sl@0
   398
	
sl@0
   399
	HBufC8* output = TWspPrimitiveEncoder::DateL(date);
sl@0
   400
	CleanupStack::PushL(output);
sl@0
   401
	buf.Copy(KDate0, sizeof(KDate0));
sl@0
   402
	if((*output).CompareF(buf)!=0)
sl@0
   403
		User::Leave(KErrGeneral);
sl@0
   404
	CleanupStack::PopAndDestroy(output);
sl@0
   405
	}
sl@0
   406
sl@0
   407