1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/fileshare/t_handshare.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,886 @@
1.4 +// Copyright (c) 1996-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 the License "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 +// f32test\server\t_handshare.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <f32file.h>
1.22 +#include <e32test.h>
1.23 +#include "t_server.h"
1.24 +#include "handshare.h"
1.25 +
1.26 +GLDEF_D RTest test(_L("T_HANDSHARE"));
1.27 +
1.28 +LOCAL_D TInt drivenum;
1.29 +
1.30 +
1.31 +
1.32 +// test of (deprecated) RFile::Adopt()
1.33 +//
1.34 +// Request an open file (read only)
1.35 +//
1.36 +GLDEF_C void RequestFileDeprecatedAdopt()
1.37 + {
1.38 + test.Next(_L("RFile::Adopt()"));
1.39 +
1.40 + TInt r;
1.41 + RFileHandleSharer handsvr;
1.42 + do
1.43 + {
1.44 + r=handsvr.Connect();
1.45 + }
1.46 + while(r==KErrNotFound);
1.47 + test(r==KErrNone);
1.48 + r=handsvr.SetTestDrive(drivenum);
1.49 + test(r==KErrNone);
1.50 + TInt ssh;
1.51 + RFs fs1;
1.52 + TInt fsh = handsvr.GetFileHandle(ssh, EFileRead);
1.53 +
1.54 + r = fs1.SetReturnedHandle(fsh);
1.55 + test(r==KErrNone);
1.56 +
1.57 + RFile file;
1.58 + r=file.Adopt(fs1,ssh);
1.59 + test(r==KErrNone);
1.60 +
1.61 + TBuf8<100> rbuf;
1.62 + r=file.Read(0,rbuf);
1.63 + test(r==KErrNone);
1.64 +
1.65 + r=rbuf.CompareF(KTestData1());
1.66 + test(r==KErrNone);
1.67 +
1.68 + r=file.Write(KTestData());
1.69 + test(r==KErrAccessDenied);
1.70 +
1.71 + r=file.ChangeMode(EFileWrite);
1.72 + test(r==KErrArgument);
1.73 +
1.74 + r=file.Rename(_L("\\newname.txt"));
1.75 + test(r==KErrPermissionDenied || r==KErrAccessDenied);
1.76 +
1.77 + file.Close();
1.78 +
1.79 + fsh = handsvr.GetFileHandle(ssh, EFileRead);
1.80 + r = fs1.SetReturnedHandle(fsh);
1.81 + test(r==KErrNone);
1.82 +
1.83 + // Adopt a bad sub-session handle
1.84 +
1.85 + r=file.Adopt(fs1, KNullHandle);
1.86 + test(r==KErrBadHandle);
1.87 +
1.88 + r=file.Adopt(fs1, -1);
1.89 + test(r==KErrBadHandle);
1.90 +
1.91 +
1.92 + handsvr.Close();
1.93 + file.Close();
1.94 + RDebug::Print(_L("End Of Tests"));
1.95 + fs1.Close();
1.96 + }
1.97 +
1.98 +
1.99 +
1.100 +// Cloning tests
1.101 +GLDEF_C void Duplicate()
1.102 + {
1.103 + test.Next(_L("RFile::Duplicate()"));
1.104 +
1.105 + RFs fs;
1.106 + TInt r=fs.Connect();
1.107 + test(r==KErrNone);
1.108 +
1.109 + // Check the number of open file handles
1.110 + TInt resCount = fs.ResourceCount();
1.111 + test(resCount == 0);
1.112 +
1.113 + // create a file & fill it with data
1.114 + RFile file1;
1.115 + r=file1.Replace(fs,KCliFileName,EFileWrite);
1.116 + test(r==KErrNone);
1.117 + r=file1.Write(KTestData());
1.118 + test(r==KErrNone);
1.119 + file1.Close();
1.120 +
1.121 + // open the file for read
1.122 + r = file1.Open(fs,KCliFileName,EFileRead);
1.123 + test(r==KErrNone);
1.124 + TBuf8<100> rbuf;
1.125 + r = file1.Read(0,rbuf);
1.126 + test(r==KErrNone);
1.127 + r=rbuf.CompareF(KTestData);
1.128 + test(r==KErrNone);
1.129 +
1.130 +
1.131 + // clone the file
1.132 + RFile file2;
1.133 + r = file2.Duplicate(file1);
1.134 + test(r==0);
1.135 +
1.136 + // make sure file positions are the same
1.137 + TInt pos1 = 0;
1.138 + r = file1.Seek(ESeekCurrent, pos1);
1.139 + test(r==KErrNone);
1.140 + TInt pos2 = 0;
1.141 + r = file2.Seek(ESeekCurrent, pos2);
1.142 + test(r==KErrNone);
1.143 + test(pos1 == pos2);
1.144 +
1.145 + // change the position on the duplcated file handle &
1.146 + // verify that the original file handle's position is unchanged
1.147 + TInt oldPos1 = pos1;
1.148 +
1.149 + const TInt newPos2 = 5;
1.150 + pos2 = newPos2;
1.151 + r = file2.Seek(ESeekStart, pos2);
1.152 + test(r==KErrNone);
1.153 +
1.154 + pos1 = 0;
1.155 + r = file1.Seek(ESeekCurrent, pos1);
1.156 + test(r==KErrNone);
1.157 + test(pos1 == oldPos1);
1.158 +
1.159 + pos2 = 0;
1.160 + r = file2.Seek(ESeekCurrent, pos2);
1.161 + test(r==KErrNone);
1.162 + test(pos2 == newPos2);
1.163 + test(pos1 != pos2);
1.164 +
1.165 + // close the parent file and check we can still use the duplicated one.
1.166 + file1.Close();
1.167 +
1.168 + // Check the number of open file handles - should be 1 (the duplicated one)
1.169 + resCount = fs.ResourceCount();
1.170 + test(resCount == 1);
1.171 +
1.172 + fs.Close();
1.173 +
1.174 + rbuf.FillZ();
1.175 +
1.176 + // reset to pos 0
1.177 + pos2 = 0;
1.178 + r = file2.Seek(ESeekStart, pos2);
1.179 + test(r==KErrNone);
1.180 +
1.181 + r=file2.Read(0,rbuf);
1.182 + test(r==KErrNone);
1.183 + r=rbuf.CompareF(KTestData);
1.184 + test(r==KErrNone);
1.185 + file2.Close();
1.186 +
1.187 + // start again - this time we're going to close the duplicated file first
1.188 + // and check we can still use the parent file
1.189 +
1.190 + r = fs.Connect();
1.191 + test(r==KErrNone);
1.192 +
1.193 + // Make a note of the number of open resources
1.194 + fs.ResourceCountMarkStart();
1.195 +
1.196 + // open the file for read
1.197 + r = file1.Open(fs,KCliFileName,EFileRead);
1.198 + test(r==KErrNone);
1.199 +
1.200 +
1.201 + // clone the file & check we can read it
1.202 + r = file2.Duplicate(file1, EOwnerThread);
1.203 + test(r==0);
1.204 + rbuf.FillZ();
1.205 + r = file2.Read(0,rbuf);
1.206 + test(r==KErrNone);
1.207 + r=rbuf.CompareF(KTestData);
1.208 + test(r==KErrNone);
1.209 +
1.210 +
1.211 + // close the duplicated file and check we can still use the parent one.
1.212 + file2.Close();
1.213 +
1.214 + rbuf.FillZ();
1.215 +
1.216 + // check we can read the parent file
1.217 + r=file1.Read(0,rbuf);
1.218 + test(r==KErrNone);
1.219 + r=rbuf.CompareF(KTestData);
1.220 + test(r==KErrNone);
1.221 +
1.222 + // close the parent
1.223 + file1.Close();
1.224 +
1.225 + // Check the number of open file handles
1.226 + fs.ResourceCountMarkEnd();
1.227 + resCount = fs.ResourceCount();
1.228 + test(resCount == 0);
1.229 +
1.230 + fs.Close();
1.231 + }
1.232 +
1.233 +
1.234 +// Request an open file (write mode) from the server
1.235 +// using RFile::AdoptFromServer()
1.236 +GLDEF_C void RequestFileWrite()
1.237 + {
1.238 + TInt r;
1.239 + RFileHandleSharer handsvr;
1.240 + do
1.241 + {
1.242 + r=handsvr.Connect();
1.243 + }
1.244 + while(r==KErrNotFound);
1.245 + test(r==KErrNone);
1.246 +
1.247 + r=handsvr.SetTestDrive(drivenum);
1.248 + test(r==KErrNone);
1.249 +
1.250 + TInt ssh;
1.251 + TInt fsh = handsvr.GetFileHandle2(ssh, EFileWrite);
1.252 + test (fsh >= 0);
1.253 +
1.254 + // Closing the handle to the server ensures the server has closed it's
1.255 + // RFs and RFile handles - this provides a means of testing whether we
1.256 + // can still adopt the RFile even if the server has closed it's one.
1.257 +
1.258 + handsvr.Sync(); // make sure server has finished doing what it's doing
1.259 + handsvr.Close();
1.260 +
1.261 + // adopt the file handle from FHServer
1.262 + test.Next(_L("RFile::AdoptFromServer()"));
1.263 +
1.264 + RFile file;
1.265 + r=file.AdoptFromServer(fsh, ssh);
1.266 + test(r==KErrNone);
1.267 +
1.268 + TBuf8<100> rbuf;
1.269 + r=file.Read(0,rbuf);
1.270 + test(r==KErrNone);
1.271 +
1.272 + // server should write KTestData1 ("Server Write Server Write") to file
1.273 + r=rbuf.CompareF(KTestData1);
1.274 + test(r==KErrNone);
1.275 +
1.276 + // reset to pos 0
1.277 + TInt pos = 0;
1.278 + r = file.Seek(ESeekStart, pos);
1.279 + test(r==KErrNone);
1.280 +
1.281 + // overwrite with KTestData ("Client Write Client Write") to file
1.282 + r=file.Write(KTestData());
1.283 + test(r==KErrNone);
1.284 + rbuf.FillZ();
1.285 + r=file.Read(0,rbuf);
1.286 + test(r==KErrNone);
1.287 + r=rbuf.CompareF(KTestData);
1.288 + test(r==KErrNone);
1.289 +
1.290 +// !!! Disable platform security tests until we get the new APIs
1.291 +// r=file.Rename(_L("\\newname.txt"));
1.292 +// test(r==KErrPermissionDenied);
1.293 +
1.294 + test.Next(_L("RFile::Name()"));
1.295 +
1.296 + // retrieve the file name from the server
1.297 + TFileName name;
1.298 + r = file.Name(name);
1.299 + test(r==KErrNone);
1.300 + r = name.Compare(KSvrFileName());
1.301 + test(r==0);
1.302 +
1.303 +
1.304 + test.Next(_L("RFile::Duplicate()"));
1.305 + RFile file2;
1.306 + r = file2.Duplicate(file);
1.307 + test(r==0);
1.308 +
1.309 +
1.310 + TInt pos1 = 0;
1.311 + r = file.Seek(ESeekCurrent, pos1);
1.312 + test(r==KErrNone);
1.313 + TInt pos2 = 0;
1.314 + r = file2.Seek(ESeekCurrent, pos2);
1.315 + test(r==KErrNone);
1.316 + test(pos1 == pos2);
1.317 +
1.318 + // close the parent file and check we can still use the duplicated one.
1.319 + file.Close();
1.320 +
1.321 + rbuf.FillZ();
1.322 +
1.323 + // reset to pos 0
1.324 + pos2 = 0;
1.325 + r = file2.Seek(ESeekStart, pos2);
1.326 + test(r==KErrNone);
1.327 +
1.328 + r=file2.Read(0,rbuf);
1.329 + test(r==KErrNone);
1.330 + r=rbuf.CompareF(KTestData);
1.331 + test(r==KErrNone);
1.332 + file2.Close();
1.333 + }
1.334 +
1.335 +
1.336 +// Request a test file & test what we can do with it
1.337 +// i.e CFileMan::Copy, RFile::Rename() and RFs:Delete()
1.338 +//
1.339 +void RequestFileTest()
1.340 + {
1.341 + TInt r;
1.342 +
1.343 + RFs fs;
1.344 + r=fs.Connect();
1.345 + test(r==KErrNone);
1.346 + r=fs.ShareProtected();
1.347 + test(r==KErrNone);
1.348 +
1.349 +
1.350 + // define a filename in our private path
1.351 + TPath newPath;
1.352 + fs.PrivatePath(newPath);
1.353 + TFileName newFileName;
1.354 + newFileName = newPath;
1.355 + newFileName.Append(_L("newname.txt"));
1.356 +
1.357 + r=fs.CreatePrivatePath(drivenum);
1.358 + test(r==KErrNone);
1.359 + r=fs.SetSessionToPrivate(drivenum);
1.360 +
1.361 + RFileHandleSharer handsvr;
1.362 + do
1.363 + {
1.364 + r=handsvr.Connect();
1.365 + }
1.366 + while(r==KErrNotFound);
1.367 + test(r==KErrNone);
1.368 +
1.369 + r=handsvr.SetTestDrive(drivenum);
1.370 + test(r==KErrNone);
1.371 +
1.372 + // Next verify that we can copy a file retrieved from the server
1.373 + // using CFileMan::Copy()
1.374 +
1.375 + test.Next(_L("CFileMan::Copy()"));
1.376 +
1.377 + TInt ssh;
1.378 + TInt fsh = handsvr.GetFileHandle2(ssh, EFileRead);
1.379 + test (fsh >= 0);
1.380 +
1.381 + RFile file;
1.382 + r=file.AdoptFromServer(fsh, ssh);
1.383 + test(r==KErrNone);
1.384 +
1.385 +
1.386 + CFileMan* fileMan = NULL;
1.387 + TRAP(r, fileMan = CFileMan::NewL(fs));
1.388 + test(r == KErrNone && fileMan != NULL);
1.389 +
1.390 + // copy to file, overwrite
1.391 + r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite);
1.392 + test(r == KErrNone);
1.393 +
1.394 + // copy to file, don't overwrite
1.395 + r = fileMan->Copy(file, newFileName, 0);
1.396 + test(r == KErrAlreadyExists);
1.397 +
1.398 + // copy to file, overwrite
1.399 + r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite);
1.400 + test(r == KErrNone);
1.401 +
1.402 + // copy to path
1.403 + r = fileMan->Copy(file, newPath, CFileMan::EOverWrite);
1.404 + test(r == KErrNone);
1.405 +
1.406 + // copy to file, overwrite, asynchnonous
1.407 + TRequestStatus status(KRequestPending);
1.408 + r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite, status);
1.409 + test(r == KErrNone);
1.410 + User::WaitForRequest(status);
1.411 + test(status == KErrNone);
1.412 +
1.413 +
1.414 + // Negative tests...
1.415 + TPath newLongPath;
1.416 + TInt len;
1.417 +
1.418 + // copy to very long but valid path (no filename) which will overflow
1.419 + // when the filename is appended to it
1.420 + newLongPath = newPath;
1.421 + for (len=newLongPath.Length(); len< KMaxPath -4; len = newLongPath.Length())
1.422 + newLongPath.Append(_L("x\\"));
1.423 + r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
1.424 + test(r == KErrBadName);
1.425 +
1.426 + // copy to very long but valid path (no filename) which will overflow
1.427 + // when drive letter is pre-pended to it
1.428 + newLongPath = newPath;
1.429 + for (len=newLongPath.Length(); len< KMaxPath -2; len = newLongPath.Length())
1.430 + newLongPath.Append(_L("x\\"));
1.431 + r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
1.432 + test(r == KErrBadName);
1.433 +
1.434 + // copy to very long but valid path and filename which will overflow
1.435 + // when drive letter is pre-pended to it
1.436 + newLongPath.Append(_L("y"));
1.437 + r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
1.438 + test(r == KErrBadName);
1.439 +
1.440 + // copy to badly formed path
1.441 + newLongPath = newPath;
1.442 + newLongPath.Append(_L("\\y"));
1.443 + r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
1.444 + test(r == KErrBadName);
1.445 +
1.446 + // copy to correctly formed path which doesn't exist
1.447 + newLongPath = newPath;
1.448 + newLongPath.Append(_L("x\\y\\z"));
1.449 + r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
1.450 + test(r == KErrPathNotFound);
1.451 +
1.452 + delete fileMan; fileMan = NULL;
1.453 +
1.454 + file.Close();
1.455 +
1.456 +
1.457 + // First verify that we CANNOT rename a file retrieved from the server
1.458 + // that has not been opened in EFileShareExclusive mode
1.459 +
1.460 + test.Next(_L("negative test of RFile::Rename()"));
1.461 +
1.462 + fsh = handsvr.GetFileHandle2(ssh, TFileMode(EFileShareAny | EFileWrite));
1.463 + test (fsh >= 0);
1.464 +
1.465 + // adopt the file handle from FHServer
1.466 + r=file.AdoptFromServer(fsh, ssh);
1.467 + test(r==KErrNone);
1.468 +
1.469 + r=file.Rename(_L("newname.txt"));
1.470 + test(r==KErrPermissionDenied || r==KErrAccessDenied);
1.471 +
1.472 + // delete the file before we try to rename anything to it
1.473 + r = fs.Delete(newFileName);
1.474 + test(r == KErrNone || r == KErrNotFound);
1.475 +
1.476 + r=file.Rename(newFileName);
1.477 + test(r==KErrPermissionDenied || r==KErrAccessDenied);
1.478 +
1.479 + file.Close();
1.480 +
1.481 +
1.482 + // Next verify that we CAN rename a file retrieved from the server
1.483 + // that HAS been opened in EFileShareExclusive mode
1.484 +
1.485 + test.Next(_L("RFile::Rename()"));
1.486 +
1.487 + fsh = handsvr.GetFileHandle2(ssh, EFileWrite);
1.488 + test (fsh >= 0);
1.489 +
1.490 + r=file.AdoptFromServer(fsh, ssh);
1.491 + test(r==KErrNone);
1.492 +
1.493 + // delete the file before we try to rename anything to it
1.494 + r = fs.Delete(newFileName);
1.495 + test(r == KErrNone || r == KErrNotFound);
1.496 +
1.497 + r=file.Rename(newFileName);
1.498 + test(r==KErrNone);
1.499 +
1.500 + file.Close();
1.501 +
1.502 + // Next verify that we can delete the file (which should now
1.503 + // have been moved to our private directory)
1.504 + test.Next(_L("RFs::Delete()"));
1.505 + r = fs.Delete(newFileName);
1.506 + test(r == KErrNone);
1.507 +
1.508 + handsvr.Close();
1.509 +
1.510 + fs.Close();
1.511 + }
1.512 +
1.513 +//
1.514 +// Pass an open file to the server
1.515 +// The server will use RFile::AdoptFromClient()
1.516 +//
1.517 +GLDEF_C void PassFile()
1.518 + {
1.519 + RFs fs;
1.520 + TInt r=fs.Connect();
1.521 + test(r==KErrNone);
1.522 +
1.523 + // Check the number of open file handles
1.524 + TInt resCount = fs.ResourceCount();
1.525 + test(resCount == 0);
1.526 +
1.527 + r=fs.ShareProtected();
1.528 + test(r==KErrNone);
1.529 +
1.530 + r=fs.CreatePrivatePath(drivenum);
1.531 + test(r==KErrNone);
1.532 + r=fs.SetSessionToPrivate(drivenum);
1.533 +
1.534 +
1.535 + RFile file1;
1.536 + r=file1.Replace(fs,KCliFileName,EFileWrite);
1.537 + test(r==KErrNone);
1.538 + r=file1.Write(KTestData());
1.539 + test(r==KErrNone);
1.540 + file1.Close();
1.541 +
1.542 + RFileHandleSharer handsvr;
1.543 + do
1.544 + {
1.545 + r=handsvr.Connect();
1.546 + }
1.547 + while(r==KErrNotFound);
1.548 + test(r==KErrNone);
1.549 +
1.550 + r=handsvr.SetTestDrive(drivenum);
1.551 + test(r==KErrNone);
1.552 +
1.553 + r=fs.SetSessionToPrivate(drivenum);
1.554 + test(r==KErrNone);
1.555 +
1.556 + r=file1.Open(fs,KCliFileName,EFileRead);
1.557 + test(r==KErrNone);
1.558 +
1.559 + // pass the file handle to FHServer
1.560 + test.Next(_L("RFile::TransferToServer()"));
1.561 +
1.562 + TIpcArgs ipcArgs;
1.563 + file1.TransferToServer(ipcArgs, 0, 1);
1.564 +
1.565 + r = handsvr.PassFileHandle(ipcArgs);
1.566 +
1.567 +
1.568 + // verify that the original file handle's position is unchanged
1.569 + TInt pos = 0;
1.570 + r = file1.Seek(ESeekCurrent, pos);
1.571 + test(r==KErrNone);
1.572 + test(pos == 0);
1.573 +
1.574 + // make sure we can still use it
1.575 + TBuf8<100> rbuf;
1.576 + r=file1.Read(0,rbuf);
1.577 + test(r==KErrNone);
1.578 +
1.579 + // Close the file
1.580 + file1.Close();
1.581 +
1.582 + handsvr.Close();
1.583 +
1.584 + r=fs.MkDir(_L("C:\\mdir"));
1.585 + test(r==KErrNone || r==KErrAlreadyExists);
1.586 +
1.587 + // Check the number of open file handles
1.588 + resCount = fs.ResourceCount();
1.589 + test(resCount == 0);
1.590 +
1.591 + fs.Close();
1.592 + }
1.593 +
1.594 +
1.595 +//
1.596 +// Pass an invalid file handle to the server
1.597 +// The server will use RFile::AdoptFromClient()
1.598 +//
1.599 +GLDEF_C void PassInvalidFile()
1.600 + {
1.601 +
1.602 + RFs fs;
1.603 + TInt r=fs.Connect();
1.604 + test(r==KErrNone);
1.605 +
1.606 + // Check the number of open file handles
1.607 + TInt resCount = fs.ResourceCount();
1.608 + test(resCount == 0);
1.609 +
1.610 + r=fs.ShareProtected();
1.611 + test(r==KErrNone);
1.612 +
1.613 + r=fs.CreatePrivatePath(drivenum);
1.614 + test(r==KErrNone);
1.615 + r=fs.SetSessionToPrivate(drivenum);
1.616 +
1.617 +
1.618 + RFile file1;
1.619 + r=file1.Replace(fs,KCliFileName,EFileWrite);
1.620 + test(r==KErrNone);
1.621 + r=file1.Write(KTestData());
1.622 + test(r==KErrNone);
1.623 + file1.Close();
1.624 +
1.625 + RFileHandleSharer handsvr;
1.626 + do
1.627 + {
1.628 + r=handsvr.Connect();
1.629 + }
1.630 + while(r==KErrNotFound);
1.631 + test(r==KErrNone);
1.632 +
1.633 + r=handsvr.SetTestDrive(drivenum);
1.634 + test(r==KErrNone);
1.635 +
1.636 + r=fs.SetSessionToPrivate(drivenum);
1.637 + test(r==KErrNone);
1.638 +
1.639 + r=file1.Open(fs,KCliFileName,EFileRead);
1.640 + test(r==KErrNone);
1.641 +
1.642 + // check the resoure count - there should be 1 open file handle
1.643 + resCount = fs.ResourceCount();
1.644 + test(resCount == 1);
1.645 +
1.646 + // pass an invalid file handle to FHServer
1.647 + // by overwriting the IPC slots
1.648 + test.Next(_L("PassInvalidFileHandle - RFile::TransferToServer()"));
1.649 +
1.650 + TIpcArgs ipcArgs;
1.651 +
1.652 + // Pass a bad RFs handle
1.653 + file1.TransferToServer(ipcArgs, 0, 1);
1.654 +
1.655 + // check the resoure count - there should be 2 open file handles
1.656 + resCount = fs.ResourceCount();
1.657 + test(resCount == 2);
1.658 +
1.659 + ipcArgs.Set(0, 0); // invalidate the RFs handle
1.660 + r = handsvr.PassInvalidFileHandle(ipcArgs);
1.661 + test (r == KErrBadHandle);
1.662 +
1.663 + // Pass a bad RFile handle
1.664 + file1.TransferToServer(ipcArgs, 0, 1);
1.665 +
1.666 + // check the resoure count - there should be 3 open file handles
1.667 + resCount = fs.ResourceCount();
1.668 + test(resCount == 3);
1.669 +
1.670 + ipcArgs.Set(1, 0); // invalidate the RFile handle
1.671 + r = handsvr.PassInvalidFileHandle(ipcArgs);
1.672 + test (r == KErrBadHandle);
1.673 +
1.674 + // Pass bad RFs and RFile handles
1.675 + file1.TransferToServer(ipcArgs, 0, 1);
1.676 +
1.677 + // check the resoure count - there should be 4 open file handles
1.678 + resCount = fs.ResourceCount();
1.679 + test(resCount == 4);
1.680 +
1.681 +
1.682 + ipcArgs.Set(0, 0); // invalidate the RFs handle
1.683 + ipcArgs.Set(1, 0); // invalidate the RFile handle
1.684 + r = handsvr.PassInvalidFileHandle(ipcArgs);
1.685 + test (r == KErrBadHandle);
1.686 +
1.687 + // Close the file
1.688 + handsvr.Close();
1.689 + file1.Close();
1.690 +
1.691 + // Check the number of open file handles
1.692 + resCount = fs.ResourceCount();
1.693 + test(resCount == 3);
1.694 +
1.695 + fs.Close();
1.696 + }
1.697 +
1.698 +
1.699 +// Pass RFs/RFile handles to the server using
1.700 +// process parameter slots (RProcess::SetParameter()),
1.701 +// resume the process and wait for it to read the file...
1.702 +// The server will use RFile::AdoptFromCreator()
1.703 +GLDEF_C void PassFile(RProcess& aProcess)
1.704 + {
1.705 +
1.706 + RFs fs;
1.707 + TInt r=fs.Connect();
1.708 + test(r==KErrNone);
1.709 +
1.710 + // Check the number of open file handles
1.711 + TInt resCount = fs.ResourceCount();
1.712 + test(resCount == 0);
1.713 +
1.714 + r=fs.ShareProtected();
1.715 + test(r==KErrNone);
1.716 +
1.717 + r=fs.CreatePrivatePath(drivenum);
1.718 + test(r==KErrNone);
1.719 + r=fs.SetSessionToPrivate(drivenum);
1.720 +
1.721 +
1.722 + RFile file1;
1.723 + r=file1.Replace(fs,KCliFileName,EFileWrite);
1.724 + test(r==KErrNone);
1.725 + r=file1.Write(KTestData());
1.726 + test(r==KErrNone);
1.727 + file1.Close();
1.728 +
1.729 + r=file1.Open(fs, KCliFileName, EFileWrite);
1.730 +
1.731 + test(r==KErrNone);
1.732 +
1.733 + // NB slot 0 is reserved for the command line
1.734 +
1.735 + test.Next(_L("RFile::TransferToProcess()"));
1.736 +
1.737 + r = file1.TransferToProcess(aProcess, 1, 2);
1.738 +
1.739 + r = aProcess.SetParameter(3, drivenum);
1.740 + test(r==KErrNone);
1.741 +
1.742 + r=fs.SetSessionToPrivate(drivenum);
1.743 + test(r==KErrNone);
1.744 +
1.745 + // make sure we can still read from the file
1.746 + TBuf8<100> rbuf;
1.747 + r=file1.Read(0,rbuf);
1.748 + test(r==KErrNone);
1.749 + r=rbuf.CompareF(KTestData());
1.750 + test(r==KErrNone);
1.751 + file1.Close();
1.752 +
1.753 + r=fs.MkDir(_L("C:\\mdir"));
1.754 + test(r==KErrNone || r==KErrAlreadyExists);
1.755 +
1.756 + // Check the number of open file handles -
1.757 + // should be 1 (the one duplicated for the other process)
1.758 + resCount = fs.ResourceCount();
1.759 + test(resCount == 1);
1.760 +
1.761 + fs.Close();
1.762 +
1.763 + // Start the server thread
1.764 + aProcess.Resume();
1.765 +
1.766 +
1.767 +
1.768 + // connect to the server
1.769 + RFileHandleSharer handsvr;
1.770 + do
1.771 + {
1.772 + r=handsvr.Connect();
1.773 + }
1.774 + while(r==KErrNotFound);
1.775 + test(r==KErrNone);
1.776 +
1.777 +
1.778 + r=handsvr.SetTestDrive(drivenum);
1.779 + test(r==KErrNone);
1.780 +
1.781 + // wait for server to read the file
1.782 + r = handsvr.PassFileHandleProcess();
1.783 + test (r == KErrNone);
1.784 +
1.785 + handsvr.Close();
1.786 + }
1.787 +
1.788 +
1.789 +
1.790 +GLDEF_C void CallTestsL(void)
1.791 + {
1.792 + RFs::CharToDrive(gDriveToTest,drivenum);
1.793 +
1.794 + // make sure the session path exists
1.795 + RFs fs;
1.796 + TInt r = fs.Connect();
1.797 + test(r==KErrNone);
1.798 +
1.799 + TFileName sessionp;
1.800 + fs.SessionPath(sessionp);
1.801 + r = fs.MkDirAll(sessionp);
1.802 + test(r==KErrNone || r==KErrAlreadyExists);
1.803 + fs.Close();
1.804 +
1.805 + // Remember the number of open handles. Just for a sanity check ....
1.806 + TInt start_thc, start_phc;
1.807 + RThread().HandleCount(start_phc, start_thc);
1.808 + test.Printf(_L("Handles: start_phc %d, start_thc %d\n"), start_phc, start_thc);
1.809 +
1.810 + //create test server
1.811 + RProcess p;
1.812 + r = p.Create(_L("FHServer.exe"), KNullDesC);
1.813 + test(r==KErrNone);
1.814 +
1.815 + // RFile::Duplicate() tests
1.816 + Duplicate();
1.817 +
1.818 + // Pass RFs/RFile handles to the server using RFile::TransferToProcess()
1.819 + // process parameter slots (RProcess::SetParameter()),
1.820 + // resume the server process and wait for it to read the file...
1.821 + PassFile(p);
1.822 +
1.823 + // Transfer the file handle to FHServer using RFile::TransferToServer().
1.824 + // Get FHServer to transfer it to FHServer2
1.825 + PassFile();
1.826 +
1.827 +
1.828 + // Get an open writeable file from FHServer2 via FHServer
1.829 + // using RFile::AdoptFromServer()
1.830 + RequestFileWrite();
1.831 +
1.832 + // Deprecated RFile::Adopt() test
1.833 + RequestFileDeprecatedAdopt();
1.834 +
1.835 + // negative test
1.836 + // Pass an invalid file handle to the server
1.837 + PassInvalidFile();
1.838 +
1.839 +
1.840 + // Get an open file handle from FHServer2 via FHServer
1.841 + // and test whether we can copy/rename/delete it etc
1.842 + // be able to rename it.
1.843 + RequestFileTest();
1.844 +
1.845 + // stop the servers
1.846 + RFileHandleSharer handsvr;
1.847 + r=handsvr.Connect();
1.848 + test(r==KErrNone);
1.849 + r = handsvr.Exit();
1.850 + test(r == KErrNone);
1.851 + handsvr.Close();
1.852 +
1.853 + // delete the test file
1.854 + RFs cleanupfs;
1.855 + r=cleanupfs.Connect();
1.856 + test(r==KErrNone);
1.857 + r=cleanupfs.SetSessionToPrivate(drivenum);
1.858 + test(r==KErrNone);
1.859 + r=cleanupfs.Delete(KCliFileName);
1.860 + test(r==KErrNone || r==KErrNotFound);
1.861 + cleanupfs.Close();
1.862 +
1.863 +
1.864 + // wait for server process to end
1.865 + TRequestStatus status;
1.866 + p.Logon(status);
1.867 + User::WaitForRequest(status);
1.868 + test (status == KErrNone);
1.869 +
1.870 + // cleanup
1.871 + p.Close();
1.872 +
1.873 + // Sanity check for open handles
1.874 + TInt end_thc, end_phc;
1.875 + RThread().HandleCount(end_phc, end_thc);
1.876 + test.Printf(_L("Handles: end_phc %d, end_thc %d\n"), end_phc, end_thc);
1.877 +
1.878 + test(start_thc == end_thc);
1.879 + test(start_phc == end_phc);
1.880 +
1.881 + // and also for pending requests ...
1.882 + test(RThread().RequestCount() == 0);
1.883 +
1.884 +
1.885 + RDebug::Print(_L("End Of Tests"));
1.886 + }
1.887 +
1.888 +
1.889 +