Update contrib.
1 // Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 const TInt KTestCleanupStack=0x20;
21 // This is a path specification and should not be used as is
22 _LIT(KFileLocationSpec, "Z:\\STOR-TST\\T_FDIR.DAT");
23 const TUint8* KTestData=_S8("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
24 const TInt KTestLength=36;
25 const TInt KTestTotal=KTestLength*(KTestLength+1);
26 const TPtrC8 KTestDes(KTestData,KTestLength);
28 LOCAL_D CTrapCleanup* TheTrapCleanup;
29 LOCAL_D RTest test(_L("t_storfdir"));
31 LOCAL_D TFileName TheTempFile;
32 LOCAL_D TBuf8<KTestLength+1> TheBuf;
35 @SYMTestCaseID SYSLIB-STORE-CT-1147
36 @SYMTestCaseDesc Writing to a store test
38 @SYMTestActions Tests for writing to a store
39 @SYMTestExpectedResults Test must not fail
42 LOCAL_C void testWriteL(CPersistentStore& aStore)
44 test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1147 Writing... "));
45 RStoreWriteStream out;
46 TStreamId id=out.CreateLC(aStore);
47 for (TInt i=0;i<=KTestLength;++i)
49 out.WriteL(KTestDes,i);
50 out.WriteL(&KTestData[i],KTestLength-i);
54 aStore.SetRootL(out.CreateL(aStore));
58 CleanupStack::PopAndDestroy();
61 @SYMTestCaseID SYSLIB-STORE-CT-1148
62 @SYMTestCaseDesc Reading from a stream test
64 @SYMTestActions Attempt for reading data from a stream
65 @SYMTestExpectedResults Test must not fail
68 LOCAL_C void testReadL(RReadStream& aStream)
70 test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1148 "));
71 for (TInt i=KTestLength;i>=0;--i)
73 aStream.ReadL(TheBuf,i);
74 test(TheBuf.Length()==i);
76 aStream.ReadL(&TheBuf[i],KTestLength-i);
77 TheBuf.SetLength(KTestLength);
78 test(TheBuf==KTestDes);
83 // Test reading from a store
85 LOCAL_C void testReadL(const CPersistentStore& aStore)
87 test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1148 Reading... "));
89 in.OpenLC(aStore,aStore.Root());
96 CleanupStack::PopAndDestroy();
100 // Test copying from one stream to another.
102 LOCAL_C void testCopyL(RWriteStream& aWriteStream,RReadStream& aReadStream)
104 test.Next(_L("Copying"));
105 for (TInt i=KTestLength;i>=0;--i)
107 aWriteStream.WriteL(aReadStream,i);
108 aReadStream.ReadL(aWriteStream,KTestLength-i);
112 @SYMTestCaseID SYSLIB-STORE-CT-1149
113 @SYMTestCaseDesc Tests writing using a direct file store
114 @SYMTestPriority High
115 @SYMTestActions Tests for memory and end of file error while creating the store.
116 Tests for writing to replaced,temporary,opened,created file.
117 Tests for creating an already existing file.
118 Tests for panic while deleting a file.
119 @SYMTestExpectedResults Test must not fail
122 LOCAL_C void testWriteL()
124 test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1149 Creating and failing to open 'ghost' file "));
126 TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
128 parse.Set(drive.Name(), &KFileLocationSpec, NULL);
130 TheFs.Delete(parse.NameAndExt());
131 CFileStore* store=CDirectFileStore::CreateLC(TheFs,parse.NameAndExt(),EFileWrite);
132 CleanupStack::PopAndDestroy();
134 TRAPD(r,store=CDirectFileStore::OpenL(TheFs,parse.NameAndExt(),EFileRead|EFileWrite));
135 test(store==NULL&&r==KErrEof);
137 test.Next(_L("Writing to replaced file"));
138 store=CDirectFileStore::ReplaceLC(TheFs,parse.NameAndExt(),EFileWrite);
139 store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KDirectFileStoreLayoutUid));
141 CleanupStack::PopAndDestroy();
143 test.Next(_L("Writing to replaced file - 2"));
144 store=CDirectFileStore::ReplaceL(TheFs,parse.NameAndExt(),EFileWrite);
145 CleanupStack::PushL(store);
146 store->SetTypeL(TUidType(KDirectFileStoreLayoutUid,KDirectFileStoreLayoutUid));
148 CleanupStack::PopAndDestroy();
150 test.Next(_L("Writing to temp file"));
151 store=CDirectFileStore::TempLC(TheFs,parse.DriveAndPath(),TheTempFile,EFileWrite);
152 store->SetTypeL(TUidType(store->Layout(),KNullUid,KDirectFileStoreLayoutUid));
155 CleanupStack::PopAndDestroy();
156 (void)TheFs.Delete(TheTempFile);
158 test.Next(_L("Writing to temp file - 2"));
159 store=CDirectFileStore::TempL(TheFs,parse.DriveAndPath(),TheTempFile,EFileWrite);
160 CleanupStack::PushL(store);
161 store->SetTypeL(TUidType(store->Layout(),KNullUid,KDirectFileStoreLayoutUid));
164 CleanupStack::PopAndDestroy();
166 test.Next(_L("Writing to opened file"));
167 store=CDirectFileStore::OpenLC(TheFs,parse.NameAndExt(),EFileRead|EFileWrite);
170 CleanupStack::PopAndDestroy();
172 test.Next(_L("Failing to create existing file"));
174 TRAP(r,store=CDirectFileStore::CreateL(TheFs,TheTempFile,EFileWrite));
175 test(store==NULL&&r==KErrAlreadyExists);
176 if (TheFs.Delete(parse.NameAndExt())!=KErrNone)
177 test.Panic(_L("Deleting file"));
179 test.Next(_L("Writing to created file"));
181 test(file.Create(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
182 store=CDirectFileStore::NewLC(file);
183 CleanupStack::PopAndDestroy();
184 test(file.Open(TheFs,parse.NameAndExt(),EFileWrite)==KErrNone);
185 store=CDirectFileStore::NewL(file);
186 CleanupStack::PushL(store);
187 store->SetTypeL(KDirectFileStoreLayoutUid);
190 CleanupStack::PopAndDestroy();
193 @SYMTestCaseID SYSLIB-STORE-CT-1150
194 @SYMTestCaseDesc Tests reading from an opened file
195 @SYMTestPriority High
196 @SYMTestActions Tests for reading using a direct file store
197 @SYMTestExpectedResults Test must not fail
200 LOCAL_C void testReadL()
202 test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1150 Reading from opened file "));
203 TParsePtrC parse(KFileLocationSpec);
206 test(file.Open(TheFs,parse.NameAndExt(),EFileRead)==KErrNone);
207 CFileStore* store=CFileStore::FromL(file);
208 CleanupStack::PushL(store);
211 TRAPD(r, store->RevertL());
212 test(r== KErrNotSupported);
213 CleanupStack::PopAndDestroy();
215 test.Next(_L("Reading from temp file"));
216 test(file.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
217 store=CDirectFileStore::FromLC(file);
219 CleanupStack::PopAndDestroy();
221 test.Next(_L("Reading from temp file - 2"));
222 test(file.Open(TheFs,TheTempFile,EFileRead)==KErrNone);
223 store=CDirectFileStore::FromL(file);
224 CleanupStack::PushL(store);
226 CleanupStack::PopAndDestroy();
229 @SYMTestCaseID SYSLIB-STORE-CT-1151
230 @SYMTestCaseDesc Copying to a single file store test.
231 @SYMTestPriority High
232 @SYMTestActions Test for copying using different buffer sizes
233 @SYMTestExpectedResults Test must not fail
236 LOCAL_C void testCopyL()
238 test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1151 Copying using small transfers "));
239 TParsePtrC parse(KFileLocationSpec);
241 CFileStore* store=CFileStore::OpenLC(TheFs,parse.NameAndExt(),EFileRead|EFileWrite);
243 in.OpenLC(*store,store->Root());
248 in.OpenL(*store,copyId);
249 RStoreWriteStream out;
250 TStreamId id=out.CreateLC(*store);
259 test.Next(_L("Copying using a single big transfer"));
260 in.OpenL(*store,copyId);
261 id=out.CreateL(*store);
262 in.ReadL(out,KTestTotal);
269 in.OpenL(*store,copyId);
270 id=out.CreateL(*store);
271 out.WriteL(in,KTestTotal);
278 CleanupStack::PopAndDestroy(3);
282 // Prepare the test directory.
284 LOCAL_C void setupTestDirectory()
286 TInt r=TheFs.Connect();
289 TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
291 parse.Set(drive.Name(), &KFileLocationSpec, NULL);
293 r=TheFs.MkDir(parse.DriveAndPath());
294 test(r==KErrNone||r==KErrAlreadyExists);
295 r=TheFs.SetSessionPath(parse.DriveAndPath());
300 // Initialise the cleanup stack.
302 LOCAL_C void setupCleanup()
304 TheTrapCleanup=CTrapCleanup::New();
305 test(TheTrapCleanup!=NULL);
308 for (TInt i=KTestCleanupStack;i>0;i--)\
309 CleanupStack::PushL((TAny*)0);\
310 CleanupStack::Pop(KTestCleanupStack);\
315 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
318 TInt err = fsSession.Connect();
322 if(fsSession.Entry(aFullName, entry) == KErrNone)
324 RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
325 err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
328 RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
330 err = fsSession.Delete(aFullName);
333 RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
340 RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
344 GLDEF_C TInt E32Main()
346 // Test direct file store.
350 setupTestDirectory();
354 test.Start(_L("Test direct file store"));
355 TRAPD(r,testWriteL());
362 //deletion of data files must be before call to .End() - DEF047652
363 TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
365 parse.Set(drive.Name(), &KFileLocationSpec, NULL);
366 ::DeleteDataFile(parse.FullName());
372 delete TheTrapCleanup;
373 if (TheFs.Delete(TheTempFile)!=KErrNone)
374 test.Panic(_L("Deleting temp file"));