1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/dbms/tdbms/t_dbfail.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,974 @@
1.4 +// Copyright (c) 1998-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 <d32dbms.h>
1.20 +#include <e32math.h>
1.21 +#include <s32file.h>
1.22 +#include <e32test.h>
1.23 +#include "t_dbfail.h"
1.24 +
1.25 +_LIT(KTestTitle,"t_dbfail: DBMS Failure mode test");
1.26 +GLDEF_D RTest test(KTestTitle);
1.27 +GLDEF_D RDbs TheDbs;
1.28 +GLDEF_D RDbNamedDatabase TheDatabase;
1.29 +GLDEF_D TClientHeap KClientHeap;
1.30 +GLDEF_D TServerHeap KServerHeap;
1.31 +
1.32 +
1.33 +LOCAL_D CTrapCleanup* TheTrapCleanup;
1.34 +LOCAL_D RFs TheFs;
1.35 +LOCAL_D RDbView TheView;
1.36 +LOCAL_D RDbTable TheTable;
1.37 +LOCAL_D RDbRowSet::TAccess Access;
1.38 +LOCAL_D CDbColSet* TheColSet;
1.39 +LOCAL_D CDbKey* TheKey;
1.40 +LOCAL_D const TDesC* TheSql;
1.41 +LOCAL_D TBuf<64> TheFormat;
1.42 +
1.43 +const TInt KTestCleanupStack=0x20;
1.44 +_LIT(KTestDatabase,"C:\\DBMS-TST\\t_fail.db");
1.45 +_LIT(TableName,"Table1");
1.46 +_LIT(TableName2,"Table_two");
1.47 +_LIT(TableNameX,"Bad Table Name");
1.48 +_LIT(IndexName,"Index1");
1.49 +_LIT(IndexName2,"Index2");
1.50 +_LIT(Column1,"column_one");
1.51 +_LIT(Column1Fold,"COLUMN_ONE");
1.52 +_LIT(Column2,"column_2");
1.53 +_LIT(Column2X,"column_2%");
1.54 +_LIT(SimpleSql,"select * from Table1");
1.55 +_LIT(UpdateSql,"update Table1 SET column_2='hello'");
1.56 +
1.57 +//const TPtrC ComplexSql(_S("select * from Table1 where column_one<0 and not column_one is null or column_2 not like '*fred*' and column_2>'m' order by column_one desc"));
1.58 +const TPtrC ComplexSql[]=
1.59 + {
1.60 + _S("select * from Table1 where column_one<0 and column_one is null"),
1.61 + _S("select * from Table1 where column_one<0 and (column_one is null and column_2 like '')"),
1.62 + _S("select * from Table1 where (column_one<0 and column_one is null) and column_2 like ''"),
1.63 + _S("select * from Table1 where (column_one<0 and column_one is null) and (column_2 like '' and column_one>0)"),
1.64 + _S("select * from Table1 where (column_one<0 and column_one is null) and (column_2 like '' and column_one>0 and column_one is null)"),
1.65 + _S("select * from Table1 where (column_one<0 and column_one is null and column_one = 10) and (column_2 like '' and column_one>0 and column_one is null)"),
1.66 + _S("select * from Table1 where column_one<0 and column_one is null and column_one = 10 and column_2 like '' and column_one>0 and column_one is null")
1.67 + };
1.68 +
1.69 +struct SSqlErrors
1.70 + {
1.71 + const TText* iSql;
1.72 + TInt iError;
1.73 + };
1.74 +
1.75 +LOCAL_D SSqlErrors const BadSql[]=
1.76 + {
1.77 + {_S("sponge"),KErrArgument},
1.78 + {_S("select * from widget"),KErrNotFound},
1.79 + {_S("select * from Table1 where x = 0"),KErrNotFound},
1.80 + {_S("select * from Table1 where x 0 like"),KErrArgument},
1.81 + {_S("select * from Table1 where column_2>'a' and column_one<'z'"),KErrGeneral},
1.82 + {_S("select from Table1"),KErrArgument},
1.83 + {_S("select x, from Table1"),KErrArgument},
1.84 + {_S("select x from Table1"),KErrNotFound},
1.85 + {_S("select column_2 column_one from Table1"),KErrArgument},
1.86 + {_S("select * from Table1 order by x"),KErrNotFound},
1.87 + {_S("select * from Table1 order column_one"),KErrArgument},
1.88 + {_S("select * from Table1 order by column_one down"),KErrArgument}
1.89 + };
1.90 +
1.91 +GLDEF_C void Connect()
1.92 + {
1.93 + TInt r=TheDbs.Connect();
1.94 + test (r==KErrNone);
1.95 + TheDbs.ResourceMark();
1.96 + }
1.97 +
1.98 +GLDEF_C void Disconnect()
1.99 + {
1.100 + TheDbs.ResourceCheck();
1.101 + TheDbs.Close();
1.102 + }
1.103 +
1.104 +
1.105 +//SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version
1.106 +LOCAL_C void DbCreateL()
1.107 + {
1.108 + User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase,TheFormat));
1.109 + }
1.110 +
1.111 +//SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version
1.112 +LOCAL_C void DbOpenL()
1.113 + {
1.114 + User::LeaveIfError(TheDatabase.Open(TheFs,KTestDatabase,TheFormat));
1.115 + CleanupClosePushL(TheDatabase);
1.116 + delete TheDatabase.TableNamesL(); // force a schema load
1.117 + CleanupStack::Pop();
1.118 + }
1.119 +
1.120 +//SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version
1.121 +LOCAL_C void DbShareL()
1.122 + {
1.123 + User::LeaveIfError(TheDatabase.Open(TheDbs,KTestDatabase,TheFormat));
1.124 + CleanupClosePushL(TheDatabase);
1.125 + delete TheDatabase.TableNamesL(); // force a schema load
1.126 + CleanupStack::Pop();
1.127 + }
1.128 +
1.129 +
1.130 +/**
1.131 +@SYMTestCaseID SYSLIB-DBMS-CT-0612
1.132 +@SYMTestCaseDesc Database validity test
1.133 +@SYMTestPriority Medium
1.134 +@SYMTestActions Tests for opening and closing of database
1.135 +@SYMTestExpectedResults Test must not fail
1.136 +@SYMREQ REQ0000
1.137 +*/
1.138 +LOCAL_C void TestOpenL()
1.139 + {
1.140 + _LIT(KFileNotFound,"not a database");
1.141 + TInt r=TheDatabase.Open(TheFs,KFileNotFound);
1.142 + test (r==KErrNotFound || r==KErrPathNotFound);
1.143 +//
1.144 + _LIT(KPathNotFound,"C:\\not a path\\database.db");
1.145 + r=TheDatabase.Open(TheFs,KPathNotFound);
1.146 + test (r==KErrPathNotFound);
1.147 +//
1.148 + _LIT(KNotFormat,"not.a.dbx");
1.149 + r=TheDatabase.Open(TheFs,KFileNotFound,KNotFormat);
1.150 + test (r==KErrNotFound || r==KErrPathNotFound);
1.151 +//
1.152 + DbCreateL();
1.153 + TheDatabase.Close();
1.154 + r=TheDatabase.Open(TheFs,KTestDatabase,TUid::Uid(0x01234567).Name());
1.155 + test (r==KErrNone); // New code has no loadable drivers, it is irrelevant to expect error here
1.156 + TheDatabase.Close(); // We have added it here because previous statement does not return error anymore
1.157 +//
1.158 + RFile f;
1.159 + r=f.Replace(TheFs,KTestDatabase,EFileWrite);
1.160 + test (r==KErrNone);
1.161 + TCheckedUid type(KDirectFileStoreLayoutUid);
1.162 + r=f.Write(type.Des());
1.163 + test (r==KErrNone);
1.164 + f.Close();
1.165 + r=TheDatabase.Open(TheFs,KTestDatabase);
1.166 + test (r==KErrNotSupported);
1.167 +//
1.168 + _LIT(KDefaultFormat,"epoc");
1.169 + r=TheDatabase.Open(TheFs,KTestDatabase,KDefaultFormat);
1.170 + test (r==KErrNotSupported); // We expect not supported db here
1.171 + }
1.172 +
1.173 +class TClient : public TContext
1.174 + {
1.175 +public:
1.176 + TClient() {}
1.177 +private:
1.178 + void OpenDbL() const
1.179 + {DbOpenL();}
1.180 + };
1.181 +class TServer : public TContext
1.182 + {
1.183 +public:
1.184 + TServer() {}
1.185 +private:
1.186 + void OpenDbL() const
1.187 + {DbShareL();}
1.188 + };
1.189 +
1.190 +const TClient KClient;
1.191 +const TServer KServer;
1.192 +
1.193 +void TFail::Test(const THeapFail& aHeap,const TContext* aContext)
1.194 + {
1.195 + TInt ii;
1.196 + TInt errCode;
1.197 + for (ii=1;;++ii)
1.198 + {
1.199 + if (aContext)
1.200 + {
1.201 + TRAP(errCode, aContext->OpenDbL());
1.202 + if(errCode != KErrNone)
1.203 + return;
1.204 + }
1.205 + aHeap.Fail(ii);
1.206 + aHeap.Mark();
1.207 + TRAPD(r,RunL());
1.208 + aHeap.Reset();
1.209 + if (r==KErrNone)
1.210 + break;
1.211 + test(r==KErrNoMemory);
1.212 + if (aContext)
1.213 + TheDatabase.Close();
1.214 + aHeap.Check();
1.215 + }
1.216 + End();
1.217 + if (aContext)
1.218 + TheDatabase.Close();
1.219 + aHeap.Check();
1.220 + }
1.221 +
1.222 +class TFailCreateDatabase : public TFail
1.223 + {
1.224 + void RunL()
1.225 + {DbCreateL();}
1.226 + void End()
1.227 + {TheDatabase.Close();}
1.228 + };
1.229 +
1.230 +class TFailOpenDatabase : public TFail
1.231 + {
1.232 + void RunL()
1.233 + {DbOpenL();}
1.234 + void End()
1.235 + {TheDatabase.Close();}
1.236 + };
1.237 +
1.238 +class TFailShareDatabase : public TFail
1.239 + {
1.240 + void RunL()
1.241 + {DbShareL();}
1.242 + void End()
1.243 + {TheDatabase.Close();}
1.244 + };
1.245 +
1.246 +/**
1.247 +@SYMTestCaseID SYSLIB-DBMS-CT-0613
1.248 +@SYMTestCaseDesc Tests for allocation failures on creating a database
1.249 +@SYMTestPriority Medium
1.250 +@SYMTestActions Tests for allocation failure for differently sourced databases
1.251 +@SYMTestExpectedResults Test must not fail
1.252 +@SYMREQ REQ0000
1.253 +*/
1.254 +LOCAL_C void OriginsL()
1.255 + {
1.256 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0613 Allocation failures on Creating a database "));
1.257 + TFailCreateDatabase t1;
1.258 + t1.Test(KClientHeap);
1.259 +//
1.260 + test.Next(_L("Fail to create existing database"));
1.261 + TUint att;
1.262 + TInt r=TheFs.Att(KTestDatabase,att);
1.263 + test (r==KErrNone);
1.264 + r=TheDatabase.Create(TheFs,KTestDatabase,TheFormat);
1.265 + test (r==KErrAlreadyExists);
1.266 + r=TheFs.Att(KTestDatabase,att);
1.267 + test (r==KErrNone);
1.268 +//
1.269 + test.Next(_L("Allocation failures on Open"));
1.270 + TFailOpenDatabase t2;
1.271 + t2.Test(KClientHeap);
1.272 +//
1.273 + test.Next(_L("Allocation failures on 1st Share"));
1.274 + Connect();
1.275 + TFailShareDatabase t3;
1.276 + t3.Test(KClientHeap);
1.277 + t3.Test(KServerHeap);
1.278 +//
1.279 + test.Next(_L("Allocation failures on 2nd Share"));
1.280 + DbShareL();
1.281 + RDbNamedDatabase temp=TheDatabase;TheDatabase=RDbNamedDatabase();
1.282 + t3.Test(KClientHeap);
1.283 + t3.Test(KServerHeap);
1.284 + temp.Close();
1.285 + Disconnect();
1.286 + test.End();
1.287 + }
1.288 +
1.289 +
1.290 +class TFailCreateTable : public TFail
1.291 + {
1.292 + void RunL()
1.293 + {User::LeaveIfError(TheDatabase.CreateTable(TableName,*TheColSet));}
1.294 + };
1.295 +
1.296 +class TFailAlterTable : public TFail
1.297 + {
1.298 + void RunL()
1.299 + {User::LeaveIfError(TheDatabase.AlterTable(TableName,*TheColSet));}
1.300 + };
1.301 +
1.302 +class TFailDropTable : public TFail
1.303 + {
1.304 + void RunL()
1.305 + {User::LeaveIfError(TheDatabase.DropTable(TableName));}
1.306 + };
1.307 +
1.308 +class TFailCreateIndex : public TFail
1.309 + {
1.310 + void RunL()
1.311 + {User::LeaveIfError(TheDatabase.CreateIndex(IndexName,TableName,*TheKey));}
1.312 + };
1.313 +
1.314 +class TFailDropIndex : public TFail
1.315 + {
1.316 + void RunL()
1.317 + {User::LeaveIfError(TheDatabase.DropIndex(IndexName,TableName));}
1.318 + };
1.319 +
1.320 +class TFailGetObject : public TFail
1.321 + {
1.322 +protected:
1.323 + void End()
1.324 + {delete iObject;}
1.325 +protected:
1.326 + CBase* iObject;
1.327 + };
1.328 +
1.329 +class TFailDatabaseTables : public TFailGetObject
1.330 + {
1.331 + void RunL()
1.332 + {iObject=TheDatabase.TableNamesL();}
1.333 + };
1.334 +
1.335 +class TFailDatabaseColSet : public TFailGetObject
1.336 + {
1.337 + void RunL()
1.338 + {iObject=TheDatabase.ColSetL(TableName);}
1.339 + };
1.340 +
1.341 +class TFailDatabaseIndexes : public TFailGetObject
1.342 + {
1.343 + void RunL()
1.344 + {iObject=TheDatabase.IndexNamesL(TableName);}
1.345 + };
1.346 +
1.347 +class TFailDatabaseKeys : public TFailGetObject
1.348 + {
1.349 + void RunL()
1.350 + {iObject=TheDatabase.KeyL(IndexName,TableName);}
1.351 + };
1.352 +
1.353 +const TInt KRowCount=60;
1.354 +
1.355 +LOCAL_C void WriteTableL()
1.356 + {
1.357 + DbOpenL();
1.358 + TInt r=TheTable.Open(TheDatabase,TableName);
1.359 + test (r==KErrNone);
1.360 + TheDatabase.Begin();
1.361 + for (TInt ii=0;ii<KRowCount;++ii)
1.362 + {
1.363 + TheTable.InsertL();
1.364 + TheTable.SetColL(1,TUint((ii*17)%KRowCount));
1.365 + TheTable.PutL();
1.366 + }
1.367 + r=TheDatabase.Commit();
1.368 + test (r==KErrNone);
1.369 + TheTable.Close();
1.370 + TheDatabase.Close();
1.371 + }
1.372 +
1.373 +LOCAL_C void DatabaseL()
1.374 + {
1.375 + test.Start(_L("Adding and dropping tables"));
1.376 + DbCreateL();
1.377 +// ensure the database locking list has been allocated
1.378 + TheDatabase.Begin();
1.379 + TheDatabase.Commit();
1.380 +//
1.381 + CDbColSet *col=CDbColSet::NewLC();
1.382 +//
1.383 + test.Next(_L("Empty Column Set"));
1.384 + __UHEAP_MARK;
1.385 + test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
1.386 + __UHEAP_MARKEND;
1.387 +//
1.388 + test.Next(_L("Invalid table name"));
1.389 + col->AddL(TDbCol(Column1,EDbColInt32));
1.390 + __UHEAP_MARK;
1.391 + test(TheDatabase.CreateTable(TableNameX,*col)!=KErrNone);
1.392 + __UHEAP_MARKEND;
1.393 +//
1.394 + test.Next(_L("Invalid column name"));
1.395 + col->AddL(TDbCol(Column2X,EDbColBit));
1.396 + __UHEAP_MARK;
1.397 + test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
1.398 + __UHEAP_MARKEND;
1.399 +//
1.400 + test.Next(_L("Duplicate column name"));
1.401 + col->Remove(Column2X);
1.402 + col->AddL(TDbCol(Column1Fold,EDbColBit));
1.403 + __UHEAP_MARK;
1.404 + test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
1.405 + __UHEAP_MARKEND;
1.406 +//
1.407 + test.Next(_L("Invalid column type"));
1.408 + col->Remove(Column1);
1.409 + col->AddL(TDbCol(Column2,TDbColType(-1)));
1.410 + __UHEAP_MARK;
1.411 + test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
1.412 + __UHEAP_MARKEND;
1.413 +//
1.414 + test.Next(_L("Invalid maximum length"));
1.415 + col->Remove(Column2);
1.416 + col->AddL(TDbCol(Column2,EDbColInt32,0));
1.417 + __UHEAP_MARK;
1.418 + test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
1.419 + __UHEAP_MARKEND;
1.420 +//
1.421 + test.Next(_L("Invalid attributes"));
1.422 + col->Remove(Column2);
1.423 + TDbCol cc(Column2,EDbColInt32);
1.424 + cc.iAttributes=13;
1.425 + col->AddL(cc);
1.426 + __UHEAP_MARK;
1.427 + test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
1.428 + __UHEAP_MARKEND;
1.429 +//
1.430 + test.Next(_L("Adding/dropping a table name twice"));
1.431 + col->Remove(Column2);
1.432 + col->AddL(TDbCol(Column2,EDbColText8));
1.433 + __UHEAP_MARK;
1.434 + test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
1.435 + test(TheDatabase.CreateTable(TableName,*col)==KErrAlreadyExists);
1.436 + test(TheDatabase.DropTable(TableNameX)!=KErrNone);
1.437 + test(TheDatabase.DropTable(TableName)==KErrNone);
1.438 + test(TheDatabase.DropTable(TableName)==KErrNotFound);
1.439 + __UHEAP_MARKEND;
1.440 +//
1.441 + test.Next(_L("Adding and dropping indexes"));
1.442 + test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
1.443 + TheDatabase.Close();
1.444 + CDbKey *key=CDbKey::NewLC();
1.445 + __UHEAP_MARK;
1.446 + DbOpenL();
1.447 + test(TheDatabase.CreateIndex(IndexName,TableName,*key)!=KErrNone);
1.448 + TheDatabase.Close();
1.449 + __UHEAP_MARKEND;
1.450 + key->AddL(Column2X());
1.451 + __UHEAP_MARK;
1.452 + DbOpenL();
1.453 + test(TheDatabase.CreateIndex(IndexName,TableName,*key)!=KErrNone);
1.454 + TheDatabase.Close();
1.455 + __UHEAP_MARKEND;
1.456 + key->Clear();
1.457 + key->AddL(Column1());
1.458 + __UHEAP_MARK;
1.459 + DbOpenL();
1.460 + test(TheDatabase.CreateIndex(TableNameX,TableName,*key)!=KErrNone);
1.461 + TheDatabase.Close();
1.462 + __UHEAP_CHECK(0);
1.463 + DbOpenL();
1.464 + test(TheDatabase.CreateIndex(IndexName,TableNameX,*key)!=KErrNone);
1.465 + TheDatabase.Close();
1.466 + __UHEAP_MARKEND;
1.467 + __UHEAP_MARK;
1.468 + DbOpenL();
1.469 + test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrNone);
1.470 + test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrAlreadyExists);
1.471 + test(TheDatabase.DropIndex(TableNameX,TableName)!=KErrNone);
1.472 + test(TheDatabase.DropIndex(IndexName,TableNameX)!=KErrNone);
1.473 + test(TheDatabase.DropIndex(IndexName,TableName)==KErrNone);
1.474 + test(TheDatabase.DropIndex(IndexName,TableName)==KErrNotFound);
1.475 + test(TheDatabase.DropTable(TableName)==KErrNone);
1.476 + test(TheDatabase.DropIndex(IndexName,TableName)==KErrNotFound);
1.477 + TheDatabase.Close();
1.478 + __UHEAP_MARKEND;
1.479 +//
1.480 + test.Next(_L("Allocation failure during DDL"));
1.481 + TFailCreateTable fct;
1.482 + TFailAlterTable fat;
1.483 + TFailDropTable fdt;
1.484 + TFailCreateIndex fci;
1.485 + TFailDropIndex fdi;
1.486 + TheColSet=CDbColSet::NewL();
1.487 + TheColSet->AddL(TDbCol(Column1,EDbColUint16));
1.488 + TheKey=CDbKey::NewL();
1.489 + TheKey->AddL(Column1());
1.490 + fct.Test(KClientHeap,KClient);
1.491 + WriteTableL();
1.492 + TheColSet->AddL(TDbCol(Column2,EDbColText));
1.493 + fat.Test(KClientHeap,KClient);
1.494 + fci.Test(KClientHeap,KClient);
1.495 + fdi.Test(KClientHeap,KClient);
1.496 + fdt.Test(KClientHeap,KClient);
1.497 +//
1.498 + test.Next(_L("Allocation failure during server DDL"));
1.499 + Connect();
1.500 + TheColSet->Remove(Column2);
1.501 + fct.Test(KClientHeap,KServer);
1.502 + WriteTableL();
1.503 + TheColSet->AddL(TDbCol(Column2,EDbColText));
1.504 + fat.Test(KClientHeap,KServer);
1.505 + fci.Test(KClientHeap,KServer);
1.506 + fdi.Test(KClientHeap,KServer);
1.507 + fdt.Test(KClientHeap,KServer);
1.508 +//
1.509 + TheColSet->Remove(Column2);
1.510 + fct.Test(KServerHeap,KServer);
1.511 + WriteTableL();
1.512 + TheColSet->AddL(TDbCol(Column2,EDbColText));
1.513 + fat.Test(KServerHeap,KServer);
1.514 + fci.Test(KServerHeap,KServer);
1.515 + fdi.Test(KServerHeap,KServer);
1.516 + fdt.Test(KServerHeap,KServer);
1.517 + Disconnect();
1.518 +//
1.519 + delete TheColSet;
1.520 + delete TheKey;
1.521 +
1.522 +//
1.523 + test.Next(_L("Allocation failure on schema enquiry"));
1.524 + DbCreateL();
1.525 + test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
1.526 + test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrNone);
1.527 + CleanupStack::PopAndDestroy(2); // columns set and key
1.528 + TheDatabase.Close();
1.529 + TFailDatabaseTables t4;
1.530 + TFailDatabaseColSet t5;
1.531 + TFailDatabaseIndexes t6;
1.532 + TFailDatabaseKeys t7;
1.533 + t4.Test(KClientHeap,KClient);
1.534 + t5.Test(KClientHeap,KClient);
1.535 + t6.Test(KClientHeap,KClient);
1.536 + t7.Test(KClientHeap,KClient);
1.537 +//
1.538 + test.Next(_L("Allocation failure on server schema enquiry"));
1.539 + Connect();
1.540 + t4.Test(KClientHeap,KServer);
1.541 + t4.Test(KServerHeap,KServer);
1.542 + t5.Test(KClientHeap,KServer);
1.543 + t5.Test(KServerHeap,KServer);
1.544 + t6.Test(KClientHeap,KServer);
1.545 + t6.Test(KServerHeap,KServer);
1.546 + t7.Test(KClientHeap,KServer);
1.547 + t7.Test(KServerHeap,KServer);
1.548 + Disconnect();
1.549 + test.End();
1.550 + }
1.551 +
1.552 +class TFailOpenTable : public TFail
1.553 + {
1.554 + void RunL()
1.555 + {User::LeaveIfError(TheTable.Open(TheDatabase,TableName,Access));}
1.556 + void End()
1.557 + {TheTable.Close();}
1.558 + };
1.559 +
1.560 +/**
1.561 +@SYMTestCaseID SYSLIB-DBMS-CT-0614
1.562 +@SYMTestCaseDesc Tests for allocation failure on opening and closing of database
1.563 +@SYMTestPriority Medium
1.564 +@SYMTestActions Tests for opening and closing of database
1.565 +@SYMTestExpectedResults Test must not fail
1.566 +@SYMREQ REQ0000
1.567 +*/
1.568 +LOCAL_C void TestTableL(const THeapFail& aHeap,const TContext& aContext)
1.569 + {
1.570 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0614 Allocation failure on Open "));
1.571 + TFailOpenTable fot;
1.572 + Access=RDbRowSet::EUpdatable;
1.573 + fot.Test(aHeap,aContext);
1.574 + Access=RDbRowSet::EReadOnly;
1.575 + fot.Test(aHeap,aContext);
1.576 + Access=RDbRowSet::EInsertOnly;
1.577 + fot.Test(aHeap,aContext);
1.578 +//
1.579 + test.Next(_L("Open invalid table"));
1.580 + aContext.OpenDbL();
1.581 + __UHEAP_MARK;
1.582 + TInt r=TheTable.Open(TheDatabase,TableNameX);
1.583 + test (r!=KErrNone);
1.584 + __UHEAP_MARKEND;
1.585 +//
1.586 + test.Next(_L("Set invalid index"));
1.587 + r=TheTable.Open(TheDatabase,TableName);
1.588 + test (r==KErrNone);
1.589 + __UHEAP_MARK;
1.590 + r=TheTable.SetIndex(IndexName2);
1.591 + test (r!=KErrNone);
1.592 + __UHEAP_MARKEND;
1.593 +//
1.594 + test.Next(_L("Allocation failure on 2nd Open"));
1.595 + RDbTable table(TheTable);
1.596 + Access=RDbRowSet::EUpdatable;
1.597 + fot.Test(aHeap);
1.598 + Access=RDbRowSet::EReadOnly;
1.599 + fot.Test(aHeap);
1.600 + Access=RDbRowSet::EInsertOnly;
1.601 + fot.Test(aHeap);
1.602 + table.Close();
1.603 + TheDatabase.Close();
1.604 + test.End();
1.605 + }
1.606 +
1.607 +LOCAL_C void TestTableDDL(const TContext& aContext)
1.608 + {
1.609 + test.Start(_L("DDL while open"));
1.610 + aContext.OpenDbL();
1.611 + TInt r=TheTable.Open(TheDatabase,TableName);
1.612 + test (r==KErrNone);
1.613 + CDbColSet* set=CDbColSet::NewLC();
1.614 + set->AddL(TDbCol(Column1,EDbColText));
1.615 + r=TheDatabase.CreateTable(TableName2,*set);
1.616 + test (r==KErrNone);
1.617 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.618 + test (r==KErrNone);
1.619 + TheTable.Close();
1.620 + r=TheTable.Open(TheDatabase,TableName2);
1.621 + test (r==KErrNone);
1.622 +//
1.623 + set->AddL(TDbCol(Column2,EDbColUint32));
1.624 + r=TheDatabase.AlterTable(TableName2,*set);
1.625 + test (r==KErrNone);
1.626 + CleanupStack::PopAndDestroy(); // set
1.627 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.628 + test (r==KErrDisconnected);
1.629 + TheTable.Reset();
1.630 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.631 + test (r==KErrDisconnected);
1.632 + TheTable.Close();
1.633 + r=TheTable.Open(TheDatabase,TableName2);
1.634 + test (r==KErrNone);
1.635 +//
1.636 + CDbKey* key=CDbKey::NewLC();
1.637 + key->AddL(Column2());
1.638 + r=TheDatabase.CreateIndex(IndexName2,TableName,*key);
1.639 + test (r==KErrNone);
1.640 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.641 + test (r==KErrNone);
1.642 + r=TheDatabase.DropIndex(IndexName2,TableName);
1.643 + test (r==KErrNone);
1.644 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.645 + test (r==KErrNone);
1.646 +//
1.647 + r=TheDatabase.CreateIndex(IndexName,TableName2,*key);
1.648 + test (r==KErrNone);
1.649 + CleanupStack::PopAndDestroy(); // key
1.650 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.651 + test (r==KErrDisconnected);
1.652 + TheTable.Close();
1.653 + r=TheTable.Open(TheDatabase,TableName2);
1.654 + test (r==KErrNone);
1.655 +//
1.656 + r=TheDatabase.DropIndex(IndexName,TableName2);
1.657 + test (r==KErrNone);
1.658 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.659 + test (r==KErrDisconnected);
1.660 + TheTable.Close();
1.661 + r=TheTable.Open(TheDatabase,TableName2);
1.662 + test (r==KErrNone);
1.663 +//
1.664 + r=TheDatabase.DropTable(TableName2);
1.665 + test (r==KErrNone);
1.666 + TRAP(r,TheTable.CountL(TheTable.EQuick));
1.667 + test (r==KErrDisconnected);
1.668 + TheTable.Close();
1.669 + TheDatabase.Close();
1.670 + test.End();
1.671 + }
1.672 +
1.673 +LOCAL_C void TableL()
1.674 + {
1.675 + test.Start(_L("Testing Client-side"));
1.676 + TestTableL(KClientHeap,KClient);
1.677 + TestTableDDL(KClient);
1.678 + test.Next(_L("Testing Client-Server"));
1.679 + Connect();
1.680 + TestTableL(KClientHeap,KServer);
1.681 + TestTableL(KServerHeap,KServer);
1.682 + TestTableDDL(KServer);
1.683 + Disconnect();
1.684 + test.End();
1.685 + }
1.686 +
1.687 +class TFailExecuteSQL : public TFail
1.688 + {
1.689 + void RunL()
1.690 + {User::LeaveIfError(TheDatabase.Execute(*TheSql));}
1.691 + void End()
1.692 + {}
1.693 + };
1.694 +
1.695 +class TFailPrepareView : public TFail
1.696 + {
1.697 + void RunL()
1.698 + {User::LeaveIfError(TheView.Prepare(TheDatabase,*TheSql,Access));}
1.699 + void End()
1.700 + {TheView.Close();}
1.701 + };
1.702 +
1.703 +/**
1.704 +@SYMTestCaseID SYSLIB-DBMS-CT-0615
1.705 +@SYMTestCaseDesc Tests for allocation failure on prepare
1.706 +@SYMTestPriority Medium
1.707 +@SYMTestActions Tests for error on updating a row set data
1.708 +@SYMTestExpectedResults Test must not fail
1.709 +@SYMREQ REQ0000
1.710 +*/
1.711 +LOCAL_C void TestView(const THeapFail& aHeap,const TContext& aContext)
1.712 + {
1.713 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0615 Allocation failure on Prepare "));
1.714 + TFailPrepareView fpv;
1.715 + TheSql=&SimpleSql;
1.716 + Access=RDbRowSet::EUpdatable;
1.717 + fpv.Test(aHeap,aContext);
1.718 + Access=RDbRowSet::EReadOnly;
1.719 + fpv.Test(aHeap,aContext);
1.720 + Access=RDbRowSet::EInsertOnly;
1.721 + fpv.Test(aHeap,aContext);
1.722 +//
1.723 + test.Next(_L("Allocation failure on Prepare (complex SQL)"));
1.724 + for (TUint ii=0;ii<sizeof(ComplexSql)/sizeof(ComplexSql[0]);++ii)
1.725 + {
1.726 + TheSql=&ComplexSql[ii];
1.727 + Access=RDbRowSet::EUpdatable;
1.728 + fpv.Test(aHeap,aContext);
1.729 + }
1.730 + test.End();
1.731 + }
1.732 +
1.733 +/**
1.734 +@SYMTestCaseID SYSLIB-DBMS-CT-0616
1.735 +@SYMTestCaseDesc Bad SQL query test
1.736 +@SYMTestPriority Medium
1.737 +@SYMTestActions Test for bad query
1.738 +@SYMTestExpectedResults Test must not fail
1.739 +@SYMREQ REQ0000
1.740 +*/
1.741 +LOCAL_C void TestSQLL(const TContext& aContext)
1.742 + {
1.743 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0616 Bad SQL "));
1.744 + aContext.OpenDbL();
1.745 + for (TUint ii=0;ii<sizeof(BadSql)/sizeof(BadSql[0]);++ii)
1.746 + test(TheView.Prepare(TheDatabase,TPtrC(BadSql[ii].iSql))==BadSql[ii].iError);
1.747 + TheDatabase.Close();
1.748 + test.End();
1.749 + }
1.750 +
1.751 +/**
1.752 +@SYMTestCaseID SYSLIB-DBMS-CT-0617
1.753 +@SYMTestCaseDesc Tests for updation of an SQL statement
1.754 +@SYMTestPriority Medium
1.755 +@SYMTestActions Tests for update SQL statement
1.756 +@SYMTestExpectedResults Test must not fail
1.757 +@SYMREQ REQ0000
1.758 +*/
1.759 +LOCAL_C void TestUpdateSQL(const THeapFail& aHeap,const TContext& aContext)
1.760 + {
1.761 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0617 Test for UPDATE SQL statement "));
1.762 + TFailExecuteSQL fsql;
1.763 + TheSql=&UpdateSql;
1.764 + fsql.Test(aHeap,aContext);
1.765 + test.End();
1.766 + }
1.767 +
1.768 +LOCAL_C void ViewL()
1.769 + {
1.770 + test.Start(_L("Client side"));
1.771 + TestView(KClientHeap,KClient);
1.772 + TestSQLL(KClient);
1.773 + test.Next(_L("Client-Server"));
1.774 + Connect();
1.775 + TestView(KClientHeap,KServer);
1.776 + TestView(KServerHeap,KServer);
1.777 + TestSQLL(KServer);
1.778 + TestUpdateSQL(KClientHeap,KClient);
1.779 + TestUpdateSQL(KServerHeap,KClient);
1.780 + Disconnect();
1.781 + test.End();
1.782 + }
1.783 +
1.784 +//TFailIncrementalUpdate implements the base class' virtual methods - RunL() and End().
1.785 +//TFailIncrementalUpdate::RunL() is called by the base class' Test() method, which simulates
1.786 +//OOM failures and checks the behaviour of the "incremental update" statement used by RunL().
1.787 +class TFailIncrementalUpdate : public TFail
1.788 + {
1.789 + virtual void RunL()
1.790 + {
1.791 + RDbUpdate dbUpdate;
1.792 + CleanupClosePushL(dbUpdate);
1.793 + User::LeaveIfError(dbUpdate.Execute(TheDatabase, _L("UPDATE A SET Name = 'ModifiedNameString' WHERE Id2 > 10")));
1.794 + TInt step = 0;
1.795 + for(TInt err=1;err>0;++step)
1.796 + {
1.797 + err = dbUpdate.Next();
1.798 + User::LeaveIfError(err);
1.799 + }
1.800 + test(step > 1);//just to be sure that the test executes dbUpdate.Next() more than once
1.801 + CleanupStack::PopAndDestroy(&dbUpdate);
1.802 + }
1.803 + virtual void End()
1.804 + {
1.805 + TheDatabase.Close();
1.806 + }
1.807 + };
1.808 +
1.809 +/**
1.810 +@SYMTestCaseID SYSLIB-DBMS-UT-3414
1.811 +@SYMTestCaseDesc "Incremental update" operations - OOM test.
1.812 +@SYMTestPriority High
1.813 +@SYMTestActions Create a test database with one table and insert some records there (> 100).
1.814 + Run an "incremental update" operation in OOM loop.
1.815 +@SYMTestExpectedResults The test should not fail or panic.
1.816 +@SYMDEF INC101720
1.817 +*/
1.818 +LOCAL_C void IncrementalUpdateTest(const THeapFail& aHeap)
1.819 + {
1.820 + //Create a test shared database with a table
1.821 + TheDatabase.Close();
1.822 + TheDbs.Close();
1.823 + TInt err = TheDbs.Connect();
1.824 + test(err == KErrNone);
1.825 + err = TheDatabase.Replace(TheFs, KTestDatabase);
1.826 + test(err == KErrNone);
1.827 + TheDatabase.Close();
1.828 + err = TheDatabase.Open(TheDbs, KTestDatabase);
1.829 + test(err == KErrNone);
1.830 + //Create a test table and fill the table with enough test records (> 100)
1.831 + err = TheDatabase.Execute(_L("CREATE TABLE A(Id COUNTER, Id2 INTEGER, Name LONG VARCHAR)"));
1.832 + test(err == KErrNone);
1.833 + const TInt KTestRecCount = 110;
1.834 + err = TheDatabase.Begin();
1.835 + test(err == KErrNone);
1.836 + for(TInt i=0;i<KTestRecCount;++i)
1.837 + {
1.838 + _LIT(KSqlFmtStr, "INSERT INTO A(Id2, Name) VALUES(%d, 'TestNameString')");
1.839 + TBuf<100> sql;
1.840 + TUint32 id = Math::Random() % KTestRecCount;
1.841 + sql.Format(KSqlFmtStr, id + 1);
1.842 + err = TheDatabase.Execute(sql);
1.843 + test(err == 1);
1.844 + }
1.845 + err = TheDatabase.Commit();
1.846 + test(err == KErrNone);
1.847 + //The OOM test
1.848 + TFailIncrementalUpdate testObj;
1.849 + testObj.Test(aHeap);
1.850 + //Cleanup
1.851 + TheDatabase.Close();
1.852 + TheDbs.Close();
1.853 + }
1.854 +
1.855 +//
1.856 +// Testing the DBMS for failure modes
1.857 +//
1.858 +LOCAL_C void doMainL()
1.859 + {
1.860 + test.Start(_L("Class RDbNamedDatabase"));
1.861 + __UHEAP_MARK;
1.862 + OriginsL();
1.863 + __UHEAP_CHECK(0);
1.864 + __UHEAP_MARK;
1.865 + Origins2();
1.866 + __UHEAP_CHECK(0);
1.867 + test.Next(_L("Class RDbDatabase"));
1.868 + DatabaseL();
1.869 + __UHEAP_CHECK(0);
1.870 + test.Next(_L("Class RDbTable"));
1.871 + TableL();
1.872 + __UHEAP_CHECK(0);
1.873 + test.Next(_L("Class RDbView"));
1.874 + ViewL();
1.875 + __UHEAP_MARKEND;
1.876 + test.End();
1.877 + }
1.878 +
1.879 +//
1.880 +// Prepare the test directory.
1.881 +//
1.882 +LOCAL_C void setupTestDirectory()
1.883 + {
1.884 + TInt r=TheFs.Connect();
1.885 + test(r==KErrNone);
1.886 +//
1.887 + r=TheFs.MkDir(KTestDatabase);
1.888 + test(r==KErrNone || r==KErrAlreadyExists);
1.889 + }
1.890 +
1.891 +//
1.892 +// Initialise the cleanup stack.
1.893 +//
1.894 +LOCAL_C void setupCleanup()
1.895 + {
1.896 + TheTrapCleanup=CTrapCleanup::New();
1.897 + test(TheTrapCleanup!=NULL);
1.898 + TRAPD(r,\
1.899 + {\
1.900 + for (TInt i=KTestCleanupStack;i>0;i--)\
1.901 + CleanupStack::PushL((TAny*)0);\
1.902 + CleanupStack::Pop(KTestCleanupStack);\
1.903 + });
1.904 + test(r==KErrNone);
1.905 + }
1.906 +
1.907 +LOCAL_C void DeleteDataFile(const TDesC& aFullName)
1.908 + {
1.909 + RFs fsSession;
1.910 + TInt err = fsSession.Connect();
1.911 + if(err == KErrNone)
1.912 + {
1.913 + TEntry entry;
1.914 + if(fsSession.Entry(aFullName, entry) == KErrNone)
1.915 + {
1.916 + RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
1.917 + err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
1.918 + if(err != KErrNone)
1.919 + {
1.920 + RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
1.921 + }
1.922 + err = fsSession.Delete(aFullName);
1.923 + if(err != KErrNone)
1.924 + {
1.925 + RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
1.926 + }
1.927 + }
1.928 + fsSession.Close();
1.929 + }
1.930 + else
1.931 + {
1.932 + RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
1.933 + }
1.934 + }
1.935 +
1.936 +GLDEF_C TInt E32Main()
1.937 + {
1.938 + test.Title();
1.939 + setupTestDirectory();
1.940 + setupCleanup();
1.941 + __UHEAP_MARK;
1.942 +//
1.943 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0612 Locating a database "));
1.944 + TRAPD(r,TestOpenL());
1.945 + test(r==KErrNone);
1.946 + PrepareDbFmtString();
1.947 + TRAP(r,TestOpen2());
1.948 + test(r==KErrNone);
1.949 + test.Next(_L("Standard database"));
1.950 + TRAP(r,doMainL());
1.951 + test(r==KErrNone);
1.952 + test.Next(_L("Secure database"));
1.953 + TRAP(r,doMainL());
1.954 + test(r==KErrNone);
1.955 + test.Next(_L("ISAM database"));
1.956 + TheFormat=_S("epoc[12345678]");
1.957 + TRAP(r,OriginsL());
1.958 + test(r==KErrNone);
1.959 + TRAP(r,Origins2());
1.960 + test(r==KErrNone);
1.961 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3414 \"Incremental update\" - client test "));
1.962 + IncrementalUpdateTest(KClientHeap);
1.963 + test.End();
1.964 + test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3414 \"Incremental update\" - client-server test "));
1.965 + IncrementalUpdateTest(KServerHeap);
1.966 + test.End();
1.967 +
1.968 + ::DeleteDataFile(KTestDatabase); // clean up data file used by this test - must be done before call to End() - DEF047652
1.969 + test.End();
1.970 +//
1.971 + __UHEAP_MARKEND;
1.972 +
1.973 + delete TheTrapCleanup;
1.974 + TheFs.Close();
1.975 + test.Close();
1.976 + return 0;
1.977 + }