summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_dbnametest.cpp
blob: eeac6cb87551d28fbb42754473713c306a584e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
* Contact: http://www.qt-project.org/legal
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


/**
@SYMTestCaseID      PIM-APPENG-CNTM-CNTDB-106281-0001
@SYMTestCaseDesc    The purpose of this test is to verify the expected behaviour
                    of the CContactDatabase public APIs involving invalid database
                    file paths (eg. string length exceeds 190 characters,
                    and string containing path characters)
@SYMTestActions     Passing an empty string, a string with more than 190 characters,
                    and a string containing path into CContactDatabase's public APIs and
                    verify if the expected error code is returned
@SYMTestExpectedResults     Expected all test cases to pass.
@SYMDEF                     DEF106281
@SYMTestType                UT
@SYMTestPriority            Low
*/

#include <cntdb.h>
#include "ccntipccodes.h"
#include "t_dbnametest.h"

//===========================================================
// CDbNameTest Class
//===========================================================
CDbNameTest::~CDbNameTest()
    {
    iLongDbName.Close();
    iInvalidDbName.Close();

    if (iActiveSchedulerWait)
        {
        if (iActiveSchedulerWait->IsStarted())
            {
            iActiveSchedulerWait->AsyncStop();
            }
        delete iActiveSchedulerWait;
        }

    iTest->Close();
    delete iTest;
    }

CDbNameTest::CDbNameTest()
    : CActive(EPriorityStandard)
    {
    CActiveScheduler::Add(this);
    }

CDbNameTest* CDbNameTest::NewLC()
    {
    CDbNameTest* self = new (ELeave) CDbNameTest();
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

void CDbNameTest::ConstructL()
    {
    iTest = new(ELeave) RTest(_L("T_DBNameTest"));

    // Create invalid DB Name (with wildcard & path characters)
    iInvalidDbName.Create(_L("c:**"));
    }

void CDbNameTest::RunTestsL()
    {
    iTest->Start(_L(" ----------- T_DBNameTest ---------- "));
	
	RArray<TInt> errArray;
	CleanupClosePushL(errArray);
	
	//MaxLength	
	errArray.AppendL(KErrNone);  //Create
	errArray.AppendL(KErrNone);     //Open
	errArray.AppendL(KErrNone); //Replace
	errArray.AppendL(KErrNone);    //Delete
	errArray.AppendL(KErrNone);    //Exists
	SetAndTestFileNameL(EMaxLength, errArray);
	
	//ELessThanMaxLength
	errArray.Reset();	
	errArray.AppendL(KErrNone);  //Create
	errArray.AppendL(KErrNone); //Open
	errArray.AppendL(KErrNone); //Replace
	errArray.AppendL(KErrNone); //Delete
	errArray.AppendL(KErrNone); //Exists
	SetAndTestFileNameL(ELessThanMaxLength, errArray);

	//ENoDriveName
	errArray.Reset();	
	errArray.AppendL(KErrBadName); //Create
	errArray.AppendL(KErrBadName); //Open
	errArray.AppendL(KErrBadName); //Replace
	errArray.AppendL(KErrBadName); //Delete
	errArray.AppendL(KErrBadName); //Exists
	SetAndTestFileNameL(ENoDriveName, errArray);
		
	//EInvalidFileName
	errArray.Reset();
	errArray.AppendL(KErrBadName); //Create
	errArray.AppendL(KErrBadName); //Open
	errArray.AppendL(KErrBadName); //Replace
	errArray.AppendL(KErrBadName); //Delete
	errArray.AppendL(KErrBadName); //Exists
	SetAndTestFileNameL(EInvalidFileName, errArray);
	
	//ENullFileName	
	errArray.Reset();
	errArray.AppendL(KErrBadName);  //Create
	errArray.AppendL(KErrBadName);  //Open
	errArray.AppendL(KErrBadName);  //Replace
	errArray.AppendL(KErrNotFound); //Delete (expects no default database in the private directory) 
	errArray.AppendL(KErrNone);     //Exists
	//deleting default database if exists.
	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KNullDesC));
	SetAndTestFileNameL(ENullFileName, errArray);
	
	//EMoreThanMaxLength
	errArray.Reset();
	errArray.AppendL(KErrArgument); //Create
	errArray.AppendL(KErrArgument); //Open
	errArray.AppendL(KErrArgument); //Replace
	errArray.AppendL(KErrArgument); //Delete
	errArray.AppendL(KErrArgument); //Exists
	SetAndTestFileNameL(EMoreThanMaxLength, errArray);
   
    CleanupStack::PopAndDestroy(); //Releasing memory used by errArray.
    
    //EMoreThanMaxLength above creates a filename with length more than allowed length, Test Asyncopendatabasetest
    //requires such a file name, so it should be run after above test.
  
    AsyncOpenDatabaseTest();

    (*iTest) (iFailedTests == 0);

    iTest->End();
    }


void CDbNameTest::AsyncOpenDatabaseTest()
    {
    // open database with long path name
    iTest->Next(_L("TEST 1: CContactDatabase::Open() with long file name"));

    iCurrName.Set(iLongDbName);

    SetActive();
    TRequestStatus* status = &iStatus;
    iOpenOp = CContactDatabase::Open(iLongDbName, *status);

    iActiveSchedulerWait = new (ELeave) CActiveSchedulerWait();
    iActiveSchedulerWait->Start();
    }

void CDbNameTest::AsyncOpenDatabaseNullTestL()
    {
    // open database with NULL path name
    iTest->Next(_L("TEST 2: CContactDatabase::Open() with NULL file name."));

    iCurrName.Set(KNullDesC);

    SetActive();
    TRequestStatus* status = &iStatus;
    iOpenOp = CContactDatabase::Open(KNullDesC, *status);
    }

void CDbNameTest::AsyncOpenDatabaseInvalidTest()
    {
    // open database with invalid path name
    iTest->Next(_L("TEST 3: CContactDatabase::Open() with file name containing path or wildcard characters."));

    iCurrName.Set(iInvalidDbName);

    SetActive();
    TRequestStatus* status = &iStatus;
    iOpenOp = CContactDatabase::Open(iInvalidDbName, *status);
    }

void CDbNameTest::RunL()
    {
    TInt err = iStatus.Int();

    if (iOpenOp)
        {
        if (err == KErrNone)
            {
            // if the database has been successfully opened, close it
            CContactDatabase* database = iOpenOp->TakeDatabase();
            if (database)
                {
                delete database;
                }
            }
        delete iOpenOp;
        iOpenOp = NULL;
        }

    if (iLongDbName == iCurrName)
        {
        if (err != KErrArgument)
            {
            iFailedTests++;
            iTest->Printf(_L("-> FAILED - invalid error code returned-- %d.\n"), err);
            }
        
        // Proceed to test 2
        AsyncOpenDatabaseNullTestL();
        }
    else if (iCurrName.Length() == 0)
        {
        // Asyc Open treats a NULL database path as the default database.
        // Therefore, both KErrNone, (meaning a default database exists), and 
        // KErrNotFound, (meaning the default database not yet exist), are acceptable
        // error codes.
        if (err != KErrNone && err != KErrNotFound)
            {
            iFailedTests++;
            iTest->Printf(_L("-> FAILED - invalid error code returned-- %d.\n"), err);
            }

        // Process to last test
        AsyncOpenDatabaseInvalidTest();
        }
    else
        {
        if (err != KErrBadName)
            {
            iFailedTests++;
            iTest->Printf(_L("-> FAILED - invalid error code returned-- %d.\n"), err);
            }

        iActiveSchedulerWait->AsyncStop();
        }
    }


/**
@SYMTestCaseID      		PIM-DBNAMETEST-0001
@SYMTestCaseDesc    		Attempts to test the behaviour of Contactdatabse API's
							while Opening, Deleting, Creating, Replacing a filename of different types. 
@SYMTestActions     		1)Create a filename, it can be of different types.
							2)Try to perform Create,Open,Delete,Replace on that filename.
							3)Check if it returns a correct errorcode.						
@SYMTestExpectedResults     Expected all test cases to pass.
@SYMDEF                     DEF114180
@SYMTestType                UT
@SYMTestPriority            Low
*/
    
void CDbNameTest::SetAndTestFileNameL(TFileNameType aType, RArray<TInt>& aErrArray)
	{
	_LIT16(KDrive,"c:");
	
	TBool driveName = ETrue;
	TBool invalidFileName = EFalse;
	TBool createFilename = ETrue;
	TBool nullFilename = EFalse;
	TInt nameLength = 0;
	
	
	switch (aType)
		{
		case ENoDriveName:
			{
			iTest->Next(_L("\n*****Operations without Drive name.*****"));
			nameLength = 50;
			driveName = EFalse;	
			}
		break;
		
		case EMoreThanMaxLength:
			{
			iTest->Next(_L("\n*****Operations with Filename more than maximum allowed length.*****"));
			nameLength = 200;	
			}
		break;
		
		case EMaxLength:
			{
			iTest->Next(_L("\n*****Operations with Filename having maximum allowed length.*****"));
			nameLength = 190;	
			}
		break;
		
		case ELessThanMaxLength:
			{
			iTest->Next(_L("\n*****Operations with Filename length less than maximum allowed length.*****"));
			nameLength = 100;	
			}
		break;
		
		case  EInvalidFileName:
			{
			iTest->Next(_L("\n*****Operations with Invalid Filename.*****"));
			invalidFileName = ETrue;
			createFilename = EFalse;
			driveName = EFalse;
			nameLength = 5;	
			}
		break;
		
		case ENullFileName:
			{
			iTest->Next(_L("\n*****Operations with NULL Filename.*****"));			
			nullFilename = ETrue;
			createFilename = EFalse;
			driveName = EFalse;	
			}
		break;	
		
		default:
		User::Leave(KErrArgument);
		
		}
	
	iLongDbName.Close();
	iLongDbName.CreateL(nameLength);
	
	if(driveName)
		{
		iLongDbName.Append(KDrive);	
		}
	
	if(invalidFileName)
		{
		iLongDbName = iInvalidDbName;	
		}
		
	if(nullFilename)
		{
		iLongDbName.Append(KNullDesC);	
		}
		
	if(createFilename)
		{
	    for (TInt i = 1; i < iLongDbName.MaxLength()-2; i++)
	        {
	        TChar ch('a');
	        ch += i % 26;
	        iLongDbName.Append(ch);
	        }		
		}

	TInt typeOfTests = 5;
 	for(TInt loop = 0;loop < typeOfTests ; ++loop)
	 	{
	 	
	 	CContactDatabase* db = NULL;
	 	
	 	TInt err(0);
	 	
	 	TInt typeOfError(0);
	 	
	 	switch(loop)
	 	{
	 		case 0:   // CREATE DATABASE TEST
	 	 		{
		 		iTest->Printf(_L("TEST : CContactDatabase::CreateL() ."));
		    	
		    	TRAP(err, db = CContactDatabase::CreateL(iLongDbName));
		    	
		    	if (err == aErrArray[0])
			    	{
			    	typeOfError = 1;					
			    	}
		    	else if(err == KErrAlreadyExists || err == KErrBadName || err == KErrDiskFull || err == KErrArgument)
			    	{
			     	typeOfError = 2;	
			    	}
				} 
	     	break;
	 
	 		case 1:	  // OPEN DATABASE TEST
		 		{
		 		iTest->Printf(_L("TEST : CContactDatabase::OpenL()."));
		 		
		    	TRAP(err, db = CContactDatabase::OpenL(iLongDbName));
		    	
		    	if (err == aErrArray[1])
			    	{
			    	typeOfError = 1;					
			    	}
		    	else if(err == KErrNotFound || err == KErrLocked || err == KErrBadName || err == KErrDiskFull || err == KErrArgument)
			    	{
			     	typeOfError = 2;	
			    	}		    	
				}    	
	 		break;
	 		
	 		case 2:  // REPLACE DATABASE TEST
		 		{
		 		iTest->Printf(_L("TEST : CContactDatabase::ReplaceL()()."));
		 		
		    	TRAP(err, db = CContactDatabase::ReplaceL(iLongDbName));
		    	
		    	if (err == aErrArray[2])
			    	{
			    	typeOfError = 1;					
			    	}
		    	else if(err == KErrInUse || err == KErrBadName || err == KErrDiskFull || err == KErrArgument)
			    	{
			     	typeOfError = 2;	
			    	}		    	
				}  		
	 		break;
	 		
	 		case 3:  // DELETE DATABASE TEST
	 	 		{
		 		iTest->Printf(_L("TEST : CContactDatabase::DeleteDatabaseL()."));
		 		
		    	TRAP(err, CContactDatabase::DeleteDatabaseL(iLongDbName));
		    	
		    	if (err == aErrArray[3])
			    	{
			    	typeOfError = 1;					
			    	}
		    	else if(err == KErrNotFound || err == KErrBadName || err == KErrInUse || err == KErrArgument)
			    	{
			     	typeOfError = 2;	
			    	}		    	
				} 		
	 		break;
	 		
	 		case 4:  // DATABASE EXISTS TEST
	 	 		{
		 		iTest->Printf(_L("TEST : CContactDatabase::ContactDatabaseExistsL()."));
		 		
		    	TRAP(err, CContactDatabase::ContactDatabaseExistsL(iLongDbName));
		    	
		    	if (err == aErrArray[4])
			    	{
			    	typeOfError = 1;					
			    	}
		    	else if(err == KErrNotReady || err == KErrBadName || err == KErrNotFound || err == KErrArgument || err == KErrCorrupt)
			    	{
			     	typeOfError = 2;	
			    	}		    	
				} 		
	 		break;
	 		
	 		
	 		default:
	 			User::Leave(KErrArgument);
	 		break;	 		
	 		
	 	}
	 	
	 	if(typeOfError == 1)
		 	{
			iTest->Printf(_L("Correct Error Code Returned: %d"), err);
		 	}
	 	else if(typeOfError == 2)
		 	{
			iTest->Printf(_L("Incorrect, but one of the possible error codes returned: %d"), err);
		 	}
	 	else
		 	{
			iTest->Printf(_L("ERROR:- Test Failed, Unexpected Error Code returned: %d"), err);
		 	}
 	 
	    if (db)
	        {
	        delete db;
	        }
	  	}  	
				
	}


LOCAL_C void DoTestsL()
    {
    CDbNameTest* test = CDbNameTest::NewLC();
    test->RunTestsL();
    CleanupStack::PopAndDestroy(test);
    }

GLDEF_C TInt E32Main()
    {
    // Init
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    if (!cleanupStack)
        {
        return KErrNoMemory;
        }

    CActiveScheduler* activeScheduler = new CActiveScheduler;
    if (!activeScheduler)
        {
        return KErrNoMemory;
        }
    CActiveScheduler::Install(activeScheduler);

    // Run the tests
    __UHEAP_MARK;
    TRAPD(err, DoTestsL());
    __UHEAP_MARKEND;

    // Cleanup
    delete activeScheduler;
    delete cleanupStack;
    return err;
    }