usbmgmt/usbmgrtest/csy/t_ecacm/src/t_csyaccess.cpp
changeset 0 c9bc50fca66e
child 15 f92a4f87e424
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * Contains sanity tests for various things.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <c32comm.h>
       
    21 #include <usbman.h>
       
    22 #include <acminterface.h>
       
    23 #include <e32test.h>
       
    24 
       
    25 #ifndef __WINS__
       
    26 _LIT(KCommDriverName, "EUSBC");
       
    27 //_LIT(KCommDeviceName, "USBC");
       
    28 #else
       
    29 _LIT(KCommPhysDriverName, "ECDRV");
       
    30 _LIT(KCommPhysDeviceName, "Comm.Wins");
       
    31 _LIT(KCommDriverName, "ECOMM");
       
    32 //_LIT(KCommDeviceName, "Comm");
       
    33 #endif
       
    34 
       
    35 LOCAL_D RTest gTest(_L("T_CSYACCESS"));
       
    36   
       
    37 enum TInitReturnValue 
       
    38 	{
       
    39 	ENotFinished,
       
    40 	EFinished
       
    41 	};
       
    42 
       
    43 TInitReturnValue C32_FailAcmOpen(TUint aCount);
       
    44 TInitReturnValue C32_SucceedAcmOpen(TUint aCount);
       
    45 TInitReturnValue USBMAN_SucceedAcmOpen(TUint aCount);
       
    46 
       
    47 TUint gInitSafetyTimeout = 100000;
       
    48 TUint gBetweenTestDelay = 1000000;
       
    49 
       
    50 TInt _1();
       
    51 TInt _2();
       
    52 TInt _3();
       
    53 TInt _4();
       
    54 TInt _5();
       
    55 TInt Oom(TInitReturnValue fn(TUint n));
       
    56 TInt Regression_DEF23333();
       
    57 TInt RunOneTest(TInt aKey);
       
    58 void RunAll();
       
    59 
       
    60 #define TEST(AAA)	{ if ( !(AAA) ) { return __LINE__; } }
       
    61 
       
    62 TInt _1()
       
    63 /**
       
    64  * Checks: 
       
    65  *	1/ can't open ACM port when USBMAN isn't started.
       
    66  *	2/ can open ACM port when USBMAN is started.
       
    67  *	3/ then stop USBMAN and check Read and Write complete with error.
       
    68  *	4/ then close the port and check that opening ACM port returns error.
       
    69  *	5/ then restarts USBMAN and checks the ACM port can be opened then shuts 
       
    70  *		it all down.
       
    71  *	6/ then start USBMAN, open ACM port, issue a Read request, close USBMAN, 
       
    72  *		and check the Read is completed with an error.
       
    73  */
       
    74 	{
       
    75 	gTest.Printf(_L("\nRunning test 1"));
       
    76 
       
    77 	CActiveScheduler* sch = new(ELeave) CActiveScheduler;
       
    78 	CActiveScheduler::Install(sch);
       
    79 
       
    80 	TName portName(KAcmSerialName);
       
    81 	portName.AppendFormat(_L("::%d"), KAcmLowUnit);
       
    82 
       
    83 	// Try to access serial/USB when the registration hasn't been done
       
    84 	RCommServ sess;
       
    85 	TEST(sess.Connect() == KErrNone);
       
    86 	RComm port;
       
    87 	TEST(sess.LoadCommModule(KAcmCsyName) == KErrNone);
       
    88 	TInt err = port.Open(sess, portName, ECommExclusive); // should fail
       
    89 	TEST(err == KErrAccessDenied);
       
    90 
       
    91 	// Start USB services
       
    92 	RUsb usb;
       
    93 	TEST(usb.Connect() == KErrNone);
       
    94 	TRequestStatus stat;
       
    95 	usb.Start(stat);
       
    96 	User::WaitForRequest(stat);
       
    97 	TEST(stat.Int() == KErrNone);
       
    98 
       
    99 	// Try to access serial/USB when the registration HAS been done
       
   100 	TEST(port.Open(sess, portName, ECommExclusive) == KErrNone); // should work
       
   101 
       
   102 	// Deregister and check further RComm calls
       
   103 	usb.Stop();
       
   104 	usb.Close();
       
   105 	TBuf8<10> buf;
       
   106 	port.Read(stat, buf);
       
   107 	User::WaitForRequest(stat);
       
   108 	TEST(stat.Int() == KErrAccessDenied);
       
   109 	port.Write(stat, _L8("stuff"));
       
   110 	User::WaitForRequest(stat);
       
   111 	TEST(stat.Int() == KErrAccessDenied);
       
   112 	port.Close();
       
   113 	err = port.Open(sess, portName, ECommExclusive); // should fail
       
   114 	TEST(err == KErrAccessDenied);
       
   115 
       
   116 	// Check can open again after restarting service
       
   117 	TEST(usb.Connect() == KErrNone);
       
   118 	usb.Start(stat);
       
   119 	User::WaitForRequest(stat);
       
   120 	TEST(stat.Int() == KErrNone);
       
   121 	err = port.Open(sess, portName, ECommExclusive); // should work
       
   122 	TEST(err == KErrNone);
       
   123 	port.Close();
       
   124 	usb.Stop();
       
   125 	usb.Close();
       
   126   
       
   127 	// Check completion of an outstanding RComm request when usbman pulls the 
       
   128 	// rug out.
       
   129 	TEST(usb.Connect() == KErrNone);
       
   130 	usb.Start(stat);
       
   131 	User::WaitForRequest(stat);
       
   132 	TEST(stat.Int() == KErrNone);
       
   133 	err = port.Open(sess, portName, ECommExclusive); // should work
       
   134 	TEST(err == KErrNone);
       
   135 
       
   136 	// Hacky pause to wait for device to enumerate.
       
   137 	User::After(1000000);
       
   138 
       
   139 	port.Read(stat, buf);
       
   140 	usb.Stop();
       
   141 	User::WaitForRequest(stat);
       
   142 	TEST(stat.Int() == KErrAccessDenied);
       
   143   
       
   144 	// Clean up
       
   145 	port.Close(); 
       
   146 	TEST(sess.UnloadCommModule(KAcmSerialName) == KErrNone);
       
   147 	sess.Close();
       
   148 
       
   149 	usb.Close();
       
   150 
       
   151 	delete sch;
       
   152 
       
   153 	return KErrNone;
       
   154 	}
       
   155 
       
   156 TInt _2()
       
   157 /**
       
   158  * Checks: 
       
   159  *	simple open/close of registration port
       
   160  */
       
   161 	{
       
   162 	gTest.Printf(_L("\nRunning test 2"));
       
   163 
       
   164 	CActiveScheduler* sch = new(ELeave) CActiveScheduler;
       
   165 	CActiveScheduler::Install(sch);
       
   166 
       
   167 	TName portName(KAcmSerialName);
       
   168 	portName.AppendFormat(_L("::%d"), 666);
       
   169 
       
   170 	RCommServ sess;
       
   171 	TEST(sess.Connect() == KErrNone);
       
   172 	RComm port;
       
   173 	TEST(sess.LoadCommModule(KAcmCsyName) == KErrNone);
       
   174 	TEST(port.Open(sess, portName, ECommExclusive) == KErrNone); 
       
   175 	port.Close();
       
   176 	TEST(sess.UnloadCommModule(KAcmSerialName) == KErrNone);
       
   177 	sess.Close();
       
   178 
       
   179 	delete sch;
       
   180 
       
   181 	return KErrNone;
       
   182 	}
       
   183 
       
   184 TInt _3()
       
   185 /**
       
   186  * Checks: 
       
   187  *	simple open/close of registration port with open/close of ACM port inside 
       
   188  *	that.
       
   189  */
       
   190 	{
       
   191 	gTest.Printf(_L("\nRunning test 3"));
       
   192 
       
   193 	CActiveScheduler* sch = new(ELeave) CActiveScheduler;
       
   194 	CActiveScheduler::Install(sch);
       
   195 
       
   196 	TName portName(KAcmSerialName);
       
   197 	portName.AppendFormat(_L("::%d"), 666);
       
   198 
       
   199 	RCommServ sess;
       
   200 	TEST(sess.Connect() == KErrNone);
       
   201 	RComm port;
       
   202 	TEST(sess.LoadCommModule(KAcmCsyName) == KErrNone);
       
   203 	TEST(port.Open(sess, portName, ECommExclusive) == KErrNone); 
       
   204 
       
   205 	TName acmPortName(KAcmSerialName);
       
   206 	acmPortName.AppendFormat(_L("::%d"), KAcmLowUnit);
       
   207 	RComm acmPort;
       
   208 	TEST(acmPort.Open(sess, acmPortName, ECommExclusive) == KErrNone); 
       
   209 	acmPort.Close();
       
   210 
       
   211 	port.Close();
       
   212 	TEST(sess.UnloadCommModule(KAcmSerialName) == KErrNone);
       
   213 	sess.Close();
       
   214 
       
   215 	delete sch;
       
   216 
       
   217 	return KErrNone;
       
   218 	}
       
   219 
       
   220 TInt _4()
       
   221 /**
       
   222  * Checks: 
       
   223  *	opens registration port (NB actually starts USB- just opening reg port 
       
   224  *		doesn't instantiate the LDD, which we need to get the Read test to 
       
   225  *		work. Otherwise the read comes back immediately with KErrGeneral.)
       
   226  *	opens ACM port
       
   227  *	check ACM APIs active
       
   228  *	closes registration port (NB stop & close USB)
       
   229  *	checks ACM APIs inactive
       
   230  *	closes ACM port.
       
   231  */
       
   232 	{
       
   233 	gTest.Printf(_L("\nRunning test 4"));
       
   234 
       
   235 	CActiveScheduler* sch = new(ELeave) CActiveScheduler;
       
   236 	CActiveScheduler::Install(sch);
       
   237 
       
   238 	// Start USB services
       
   239 	RUsb usb;
       
   240 	TEST(usb.Connect() == KErrNone);
       
   241 	TRequestStatus stat;
       
   242 	usb.Start(stat);
       
   243 	User::WaitForRequest(stat);
       
   244 	TEST(stat.Int() == KErrNone);	  
       
   245 
       
   246 	RCommServ sess;
       
   247 	TEST(sess.Connect() == KErrNone);
       
   248 
       
   249 	TName acmPortName(KAcmSerialName);
       
   250 	acmPortName.AppendFormat(_L("::%d"), KAcmLowUnit); 
       
   251 	RComm acmPort;
       
   252 	TEST(acmPort.Open(sess, acmPortName, ECommExclusive) == KErrNone); 
       
   253 
       
   254 	// Hacky pause to wait for device to enumerate.
       
   255 	User::After(1000000);
       
   256 
       
   257 	TRequestStatus readStat;
       
   258 	TRequestStatus timerStat;
       
   259 	TBuf8<10> buf;
       
   260 	acmPort.Read(readStat, buf);
       
   261 	RTimer timer;
       
   262 	TEST(timer.CreateLocal() == KErrNone);
       
   263 	timer.After(timerStat, 1000000); 
       
   264 	User::WaitForRequest(readStat, timerStat);
       
   265 	TEST(readStat == KRequestPending);
       
   266 	TEST(timerStat == KErrNone);
       
   267 	acmPort.ReadCancel();
       
   268 	User::WaitForRequest(readStat);
       
   269 	TEST(readStat == KErrCancel);
       
   270 	timer.Close();
       
   271 
       
   272 	usb.Stop();
       
   273 	usb.Close();
       
   274 
       
   275 	acmPort.Read(readStat, buf);
       
   276 	User::WaitForRequest(readStat);
       
   277 	TEST(readStat == KErrAccessDenied);
       
   278 
       
   279 	acmPort.Close();
       
   280 
       
   281 	sess.Close();
       
   282 
       
   283 	delete sch;
       
   284 
       
   285 	return KErrNone;
       
   286 	}
       
   287 
       
   288 TInt _5()
       
   289 /**
       
   290  * Checks: 
       
   291  *	opens registration port
       
   292  *	opens ACM port
       
   293  *	check ACM APIs active
       
   294  *	closes registration port
       
   295  *	checks ACM APIs inactive
       
   296  *	opens registration port
       
   297  *	checks ACM APIs active
       
   298  *	closes ACM port
       
   299  *	closes registration port
       
   300  */
       
   301 	{
       
   302 	gTest.Printf(_L("\nRunning test 5"));
       
   303 
       
   304 	CActiveScheduler* sch = new(ELeave) CActiveScheduler;
       
   305 	CActiveScheduler::Install(sch);
       
   306 
       
   307 	// Start USB services
       
   308 	RUsb usb;
       
   309 	TEST(usb.Connect() == KErrNone);
       
   310 	TRequestStatus stat;
       
   311 	usb.Start(stat);
       
   312 	User::WaitForRequest(stat);
       
   313 	TEST(stat.Int() == KErrNone);	  
       
   314 
       
   315 	RCommServ sess;
       
   316 	TEST(sess.Connect() == KErrNone);
       
   317 
       
   318 	TName acmPortName(KAcmSerialName);
       
   319 	acmPortName.AppendFormat(_L("::%d"), KAcmLowUnit);
       
   320 	RComm acmPort;
       
   321 	TEST(acmPort.Open(sess, acmPortName, ECommExclusive) == KErrNone); 
       
   322 
       
   323 	// Hacky pause to wait for device to enumerate.
       
   324 	User::After(1000000);  
       
   325 
       
   326 	TRequestStatus readStat;
       
   327 	TRequestStatus timerStat;
       
   328 	TBuf8<10> buf;
       
   329 	acmPort.Read(readStat, buf);
       
   330 	RTimer timer;
       
   331 	TEST(timer.CreateLocal() == KErrNone);
       
   332 	timer.After(timerStat, 1000000); 
       
   333 	User::WaitForRequest(readStat, timerStat);
       
   334 	TEST(readStat == KRequestPending);
       
   335 	TEST(timerStat == KErrNone);
       
   336 	acmPort.ReadCancel();
       
   337 	User::WaitForRequest(readStat);
       
   338 	TEST(readStat == KErrCancel);
       
   339 	timer.Close();
       
   340 
       
   341 	usb.Stop();
       
   342 
       
   343 	acmPort.Read(readStat, buf);
       
   344 	User::WaitForRequest(readStat);
       
   345 	TEST(readStat == KErrAccessDenied);
       
   346 
       
   347 	usb.Start(stat);
       
   348 	User::WaitForRequest(stat);
       
   349 	TEST(stat.Int() == KErrNone);	  
       
   350 
       
   351 	// Hacky pause to wait for device to enumerate.
       
   352 	User::After(1000000);
       
   353 
       
   354 	acmPort.Read(readStat, buf);
       
   355 	TEST(timer.CreateLocal() == KErrNone);
       
   356 	timer.After(timerStat, 1000000);
       
   357 	User::WaitForRequest(readStat, timerStat);
       
   358 	TEST(readStat == KRequestPending);
       
   359 	TEST(timerStat == KErrNone);
       
   360 	acmPort.ReadCancel();
       
   361 	User::WaitForRequest(readStat);
       
   362 	TEST(readStat == KErrCancel);
       
   363 	timer.Close();
       
   364 		
       
   365 	acmPort.Close();
       
   366 
       
   367 	usb.Stop();
       
   368 	usb.Close();
       
   369 	sess.Close();
       
   370 
       
   371 	delete sch;
       
   372 
       
   373 	return KErrNone;
       
   374 	}
       
   375 
       
   376 TInitReturnValue C32_FailAcmOpen(TUint aCount)
       
   377 /**
       
   378  * One iteration of loading ECACM.CSY and trying to open the ACM port. Will 
       
   379  * fail due to the access control (USB isn't started). Heap failures in C32's 
       
   380  * heap.
       
   381  */
       
   382 	{
       
   383 	RCommServ serv;
       
   384 	TInt err = serv.Connect();
       
   385 	if ( !err )
       
   386 		{
       
   387 		gTest.Printf(_L("\tconnected to server\n"));
       
   388 		serv.__DbgMarkHeap();
       
   389 		serv.__DbgFailNext(aCount);
       
   390 
       
   391 		err = serv.LoadCommModule(KAcmCsyName);
       
   392 		if ( !err )
       
   393 			{
       
   394 			gTest.Printf(_L("\tloaded comm module\n"));
       
   395 			RComm comm;
       
   396 			TName name(KAcmSerialName);
       
   397 			name.AppendFormat(_L("::%d"), KAcmLowUnit); 
       
   398 			err = comm.Open(serv, name, ECommExclusive);
       
   399 			if ( err == KErrAccessDenied ) 
       
   400 				{
       
   401 				gTest.Printf(_L("\tsuccessful result from RComm::Open\n"));
       
   402 				err = KErrNone;
       
   403 				}
       
   404 			}
       
   405 		serv.UnloadCommModule(KAcmSerialName);
       
   406 		User::After(gInitSafetyTimeout);
       
   407 		serv.__DbgMarkEnd(0);
       
   408 		serv.Close();
       
   409 		User::After(gInitSafetyTimeout);
       
   410 
       
   411 		if ( !err )
       
   412 			{
       
   413 			// Test for if the test has finished. If aCount has become so big 
       
   414 			// as to skip over all the allocs in the above use case, the next 
       
   415 			// API call which causes alloc will fail with KErrNoMemory. 
       
   416 			if ( serv.Connect() == KErrNoMemory )
       
   417 				{
       
   418 				serv.Close();
       
   419 				User::After(gInitSafetyTimeout);
       
   420 				return EFinished;
       
   421 				}
       
   422 			}
       
   423 		}
       
   424 
       
   425 	serv.Close();
       
   426 	User::After(gInitSafetyTimeout);
       
   427 
       
   428 	return ENotFinished;
       
   429 	}
       
   430 
       
   431 TInitReturnValue C32_SucceedAcmOpen(TUint aCount)
       
   432 /**
       
   433  * One iteration of starting USB, loading ECACM.CSY, trying to open the ACM 
       
   434  * port and changing the ACM port's buffer sizes. Heap failures in C32's heap.
       
   435  */
       
   436 	{
       
   437 	RUsb usb;
       
   438 	TInt err = usb.Connect();
       
   439 	gTest.Printf(_L("\tRUsb::Connect = %d\n"), err);
       
   440 	if ( !err )
       
   441 		{
       
   442 		RCommServ serv;
       
   443 		err = serv.Connect();
       
   444 		gTest.Printf(_L("\tRCommServ::Connect = %d\n"), err);
       
   445 
       
   446 		if ( !err )
       
   447 			{
       
   448 			serv.__DbgMarkHeap();
       
   449 			serv.__DbgFailNext(aCount);
       
   450 
       
   451 			TRequestStatus stat;
       
   452 			usb.Start(stat);
       
   453 			User::WaitForRequest(stat);
       
   454 			err = stat.Int();
       
   455 			gTest.Printf(_L("\tRUsb::Start = %d\n"), err);
       
   456 			if ( !err )
       
   457 				{
       
   458 				err = serv.LoadCommModule(KAcmCsyName);
       
   459 				gTest.Printf(_L("\tRCommServ::LoadCommModule = %d\n"), err);
       
   460 				if ( !err )
       
   461 					{
       
   462 					RComm comm;
       
   463 					TName name(KAcmSerialName);
       
   464 					name.AppendFormat(_L("::%d"), KAcmLowUnit);
       
   465 					err = comm.Open(serv, name, ECommExclusive);
       
   466 					gTest.Printf(_L("\tRComm::Open = %d\n"), err);
       
   467 					if ( !err ) 
       
   468 						{
       
   469 						gTest.Printf(_L("\tsuccessful result from RComm::Open\n"));
       
   470 						
       
   471 	/*					TCommServerConfigV01 serverConfig;
       
   472 						TCommServerConfig serverConfigBuf(serverConfig);
       
   473 						comm.Mode(serverConfigBuf);
       
   474 
       
   475 						const TUint KBufSize = 0x1000;
       
   476 						serverConfig.iBufSize = KBufSize;
       
   477 
       
   478 						err = comm.SetMode(serverConfig);
       
   479 						if ( !err )
       
   480 							{
       
   481 							gTest.Printf(_L("\tsuccessful result from RComm::SetMode\n"));
       
   482 							// End of use case.
       
   483 							}
       
   484   */
       
   485 						comm.Close();			
       
   486 						}
       
   487 					}
       
   488 				serv.UnloadCommModule(KAcmSerialName);
       
   489 				
       
   490 				usb.Stop();
       
   491 				User::After(gInitSafetyTimeout);
       
   492 
       
   493 				serv.__DbgMarkEnd(0);
       
   494 				}
       
   495 			serv.Close();
       
   496 			User::After(gInitSafetyTimeout);
       
   497 			}
       
   498 		
       
   499 		usb.Close();
       
   500 		User::After(gInitSafetyTimeout);
       
   501 
       
   502 		if ( !err )
       
   503 			{
       
   504 			// Test for if the test has finished. If aCount has become so big 
       
   505 			// as to skip over all the allocs in the above use case, the next 
       
   506 			// API call which causes alloc will fail with KErrNoMemory. 
       
   507 			if ( serv.Connect() == KErrNoMemory )
       
   508 				{
       
   509 				serv.Close();
       
   510 				User::After(gInitSafetyTimeout);
       
   511 				return EFinished;
       
   512 				}
       
   513 			}
       
   514 		}
       
   515 
       
   516 	return ENotFinished;
       
   517 	}
       
   518 
       
   519 TInitReturnValue USBMAN_SucceedAcmOpen(TUint aCount)
       
   520 /**
       
   521  * One iteration of doing RUsb::Start/Stop. Heap failures in USBMAN's heap.
       
   522  */
       
   523 	{
       
   524 	RUsb usb;
       
   525 	TInt err = usb.Connect();
       
   526 	if ( !err )
       
   527 		{
       
   528 		gTest.Printf(_L("\tconnected to USBMAN\n"));
       
   529 
       
   530 		err = usb.__DbgMarkHeap();
       
   531 		if ( !err )
       
   532 			{
       
   533 			err = usb.__DbgFailNext(aCount);
       
   534 			if ( !err )
       
   535 				{
       
   536 				TRequestStatus stat;
       
   537 				usb.Start(stat);
       
   538 				User::WaitForRequest(stat);
       
   539 				err = stat.Int();
       
   540 				gTest.Printf(_L("\tRUsb::Start completed with %d\n"), err);
       
   541 				if ( !err )
       
   542 					{
       
   543 					usb.Stop(stat);
       
   544 					User::WaitForRequest(stat);
       
   545 					err = stat.Int();
       
   546 					gTest.Printf(_L("\tRUsb::Stop completed with %d\n"), err);
       
   547 					}
       
   548 				}
       
   549 			else
       
   550 				{
       
   551 				gTest.Printf(_L("\tRUsb::__DbgFailNext completed with %d\n"), err);
       
   552 				User::Panic(_L("t_csyaccess"), 1);
       
   553 				}
       
   554 			}
       
   555 		else
       
   556 			{
       
   557 			gTest.Printf(_L("\tRUsb::__DbgMarkHeap completed with %d\n"), err);
       
   558 			User::Panic(_L("t_csyaccess"), 1);
       
   559 			}
       
   560 		}
       
   561 	else
       
   562 		{
       
   563 		gTest.Printf(_L("\tRUsb::Connect completed with %d\n"), err);
       
   564 		User::Panic(_L("t_csyaccess"), 1);
       
   565 		}
       
   566 
       
   567 	usb.__DbgMarkEnd(0);
       
   568 	usb.Close();
       
   569 	
       
   570 	if ( !err )
       
   571 		{
       
   572 		// Test for if the test has finished. If aCount has become so big 
       
   573 		// as to skip over all the allocs in the above use case, the next 
       
   574 		// API call which causes alloc will fail with KErrNoMemory. 
       
   575 		if ( usb.Connect() == KErrNoMemory )
       
   576 			{
       
   577 			usb.Close();
       
   578 			User::After(gInitSafetyTimeout);
       
   579 			return EFinished;
       
   580 			}
       
   581 		}
       
   582   
       
   583 	User::After(gInitSafetyTimeout);
       
   584 
       
   585 	return ENotFinished;
       
   586 	}
       
   587 
       
   588 TInt Oom(TInitReturnValue fn(TUint n))
       
   589 /**
       
   590  * Wrapper for OOM iterations.
       
   591  *
       
   592  * @param fn Function pointer taking a TUint (the iteration) and returning a 
       
   593  * TInitReturnValue indicating whether the test has completed or not.
       
   594  * @return KErrNone.
       
   595  */
       
   596 	{
       
   597 	gTest.Printf(_L("\n"));
       
   598 
       
   599 	for ( TInt n = 1 ; ; n++ )
       
   600 		{
       
   601 		gTest.Printf(_L("Failing alloc %d\n"),n);
       
   602 	
       
   603 		if ( fn(n) == EFinished )
       
   604 			return KErrNone;
       
   605 		}
       
   606 	}
       
   607 
       
   608 TInt Regression_DEF23333()
       
   609 /**
       
   610  * Regression test for defect DEF23333.
       
   611  * Buffer size member of Tx class (CAcmWriter) isn't changed when SetBufSize 
       
   612  * is used.
       
   613  */
       
   614 	{
       
   615 	gTest.Printf(_L("\nRunning test Regression_DEF23333"));
       
   616 
       
   617 	CActiveScheduler* sch = new(ELeave) CActiveScheduler;
       
   618 	CActiveScheduler::Install(sch);
       
   619 
       
   620 	RUsb usb;
       
   621 	TEST(usb.Connect() == KErrNone);
       
   622 	TRequestStatus stat;
       
   623 	usb.Start(stat);
       
   624 	User::WaitForRequest(stat);
       
   625 	TEST(stat == KErrNone);
       
   626 
       
   627 	TName portName(KAcmSerialName);
       
   628 	portName.AppendFormat(_L("::%d"), KAcmLowUnit);
       
   629 
       
   630 	RCommServ sess;
       
   631 	TEST(sess.Connect() == KErrNone);
       
   632 	RComm acmPort;
       
   633 	TEST(sess.LoadCommModule(KAcmCsyName) == KErrNone);
       
   634 	TEST(acmPort.Open(sess, portName, ECommExclusive) == KErrNone);
       
   635 
       
   636 	// Hacky pause to wait for device to enumerate.
       
   637 	User::After(1000000);
       
   638 	
       
   639 	// This seems to be needed to keep the LDD happy.
       
   640 	gTest.Printf(_L("\nAttach a HyperTerminal session (emulated serial over USB) and hit a key"));
       
   641 	gTest.Getch();
       
   642 	
       
   643 	TInt len = acmPort.ReceiveBufferLength();
       
   644 	TEST(len > 10); 
       
   645 	// Check we can Write amounts of data less than the default starting 
       
   646 	// buffer size.
       
   647 	HBufC8* data = HBufC8::NewL(len + 20);
       
   648 	TPtr8 ptr = data->Des();
       
   649 	ptr.SetLength(len - 1);
       
   650 	acmPort.Write(stat, *data);
       
   651 	User::WaitForRequest(stat);
       
   652 	TEST(stat == KErrNone);
       
   653 	// Check writing an amount bigger than this fails.
       
   654 	ptr.SetLength(len + 1);
       
   655 	acmPort.Write(stat, *data);
       
   656 	User::WaitForRequest(stat);
       
   657 	TEST(stat == KErrNoMemory);	
       
   658 	// Resize the buffer.
       
   659 	const TInt newLength = 10;
       
   660 	acmPort.SetReceiveBufferLength(newLength);
       
   661 	TEST(acmPort.ReceiveBufferLength() == newLength);
       
   662 	// Check writing an amount smaller than the new buffer.
       
   663 	ptr.SetLength(newLength - 1);
       
   664 	acmPort.Write(stat, *data);
       
   665 	User::WaitForRequest(stat);
       
   666 	TEST(stat == KErrNone);
       
   667 	// Check writing an amount larger than the new size fails.
       
   668 	ptr.SetLength(newLength + 1);
       
   669 	acmPort.Write(stat, *data);
       
   670 	User::WaitForRequest(stat);
       
   671 	TEST(stat == KErrNoMemory);	
       
   672 	
       
   673 	delete data;
       
   674 	acmPort.Close();
       
   675 	TEST(sess.UnloadCommModule(KAcmSerialName) == KErrNone);
       
   676 	sess.Close();
       
   677 	usb.Stop(stat);
       
   678 	User::WaitForRequest(stat);
       
   679 	TEST(stat == KErrNone);
       
   680 	usb.Close();
       
   681 	
       
   682 	delete sch;
       
   683 
       
   684 	// This seems to be needed to keep the LDD happy.
       
   685 	gTest.Printf(_L("\nDisconnect the HyperTerminal session and hit a key"));
       
   686 	gTest.Getch();
       
   687 
       
   688 	return KErrNone;
       
   689 	}
       
   690 
       
   691 void PrintTestInstructions()
       
   692 	{
       
   693 	gTest.Printf(_L("\nThese tests should be run with USB OFF,"));
       
   694 	gTest.Printf(_L("\ni.e. RUsb has not been Started, or, if"));
       
   695 	gTest.Printf(_L("\nit has, it should be Stopped again."));
       
   696 	gTest.Printf(_L("\nAlso, there should be no open subsessions"));
       
   697 	gTest.Printf(_L("\non the ECACM.CSY."));
       
   698 	gTest.Printf(_L("\nIn a Techview ROM, it will be necessary"));
       
   699 	gTest.Printf(_L("\nto remove the watchers so that nothing"));
       
   700 	gTest.Printf(_L("\ninterferes with C32's heap (for the OOM"));
       
   701 	gTest.Printf(_L("\ntests in C32's heap)."));
       
   702 	gTest.Printf(_L("\nNothing else should be using USBMAN or"));
       
   703 	gTest.Printf(_L("\nECACM during the test."));
       
   704 	}
       
   705 
       
   706 void PrintTestIds()
       
   707 	{
       
   708 	gTest.Printf(_L("\n1/"));
       
   709 	gTest.Printf(_L("\n2/"));
       
   710 	gTest.Printf(_L("\n3/"));
       
   711 	gTest.Printf(_L("\n4/"));
       
   712 	gTest.Printf(_L("\n5/"));
       
   713 	gTest.Printf(_L("\n6/ OOM1 (in C32)- failure opening ACM port"));
       
   714 	gTest.Printf(_L("\n7/ OOM2 (in C32)- success opening ACM port"));
       
   715 	gTest.Printf(_L("\n8/ OOM3 (in USBMAN)- RUsb::Start"));
       
   716 	gTest.Printf(_L("\n9/ Regression_DEF23333"));
       
   717 	gTest.Printf(_L("\n! to run all"));
       
   718 	gTest.Printf(_L("\nEsc to exit"));
       
   719 	}
       
   720 
       
   721 GLDEF_C TInt E32Main()
       
   722 	{
       
   723 	__UHEAP_MARK;
       
   724 	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
       
   725 												  	
       
   726 	gTest.Title();
       
   727 	gTest.Start(_L("Starting E32Main"));
       
   728 
       
   729 	gTest.Next(_L("loading LDD"));
       
   730 	TInt r = User::LoadLogicalDevice(KCommDriverName);
       
   731 	gTest(r == KErrNone || r == KErrAlreadyExists);
       
   732 
       
   733 #ifdef __WINS__
       
   734 	gTest.Next(_L("loading PDD"));
       
   735 	r = User::LoadPhysicalDevice(KCommPhysDriverName);
       
   736 	gTest(r == KErrNone || r == KErrAlreadyExists);
       
   737 #endif
       
   738 
       
   739 	gTest.Next(_L("starting C32"));
       
   740 	r = StartC32();
       
   741 	gTest(r == KErrNone || r == KErrAlreadyExists);
       
   742 
       
   743 	PrintTestInstructions();
       
   744 
       
   745 	PrintTestIds();
       
   746 
       
   747 	TInt key = gTest.Getch();
       
   748 
       
   749 	while ( key != EKeyEscape )
       
   750 		{
       
   751 		TInt error = KErrNone;
       
   752 
       
   753 		RCommServ sess;
       
   754 		sess.Connect();
       
   755 		sess.__DbgMarkHeap();
       
   756 
       
   757 		switch (key )
       
   758 			{
       
   759 		case '!':
       
   760 			RunAll();
       
   761 			break;
       
   762 		default:
       
   763 			error = RunOneTest(key);
       
   764 			break;
       
   765 			}
       
   766 
       
   767 		User::After(gInitSafetyTimeout);
       
   768 
       
   769 		sess.__DbgCheckHeap(0);
       
   770 		sess.Close();
       
   771 
       
   772 		User::After(gBetweenTestDelay);
       
   773 
       
   774 		gTest.Printf(_L("\nTest completed with error %d"), error);
       
   775 
       
   776 		PrintTestIds();
       
   777 
       
   778 		key = gTest.Getch();
       
   779 		}
       
   780 
       
   781 	gTest.End();
       
   782 	gTest.Close();
       
   783 
       
   784 	delete cleanup; // destroy clean-up stack
       
   785 	__UHEAP_MARKEND;
       
   786 	return KErrNone;
       
   787 	}
       
   788 
       
   789 TInt RunOneTest(TInt aKey)
       
   790 	{
       
   791 	TInt error = KErrNone;
       
   792 
       
   793 	switch ( aKey )
       
   794 		{
       
   795 	case '1':
       
   796 		error = _1();
       
   797 		break;
       
   798 	case '2':
       
   799 		error = _2();
       
   800 		break;
       
   801 	case '3':
       
   802 		error = _3();
       
   803 		break;
       
   804 	case '4':
       
   805 		error = _4();
       
   806 		break;
       
   807 	case '5':
       
   808 		error = _5();
       
   809 		break;
       
   810 	case '6':
       
   811 		error = Oom(C32_FailAcmOpen);
       
   812 		break;
       
   813 	case '7':
       
   814 		error = Oom(C32_SucceedAcmOpen);
       
   815 		break;
       
   816 	case '8':
       
   817 		error = Oom(USBMAN_SucceedAcmOpen);
       
   818 		break;
       
   819 	case '9':
       
   820 		error = Regression_DEF23333();
       
   821 		break;
       
   822 	default:
       
   823 		gTest.Printf(_L("\nKey not recognised"));
       
   824 		break;
       
   825 		}
       
   826 
       
   827 	return error;
       
   828 	}
       
   829 		
       
   830 void RunAll()
       
   831 	{
       
   832 	RCommServ sess;
       
   833 	sess.Connect();
       
   834 
       
   835 	for ( TInt ii = 0 ; ii < 9 ; ii++ ) // TODO: keep 9 up-to-date with number of tests.
       
   836 		{
       
   837 		sess.__DbgMarkHeap();
       
   838 		gTest.Printf(_L("\nTest completed with error %d"), RunOneTest('1'+ii));
       
   839 		sess.__DbgCheckHeap(0);
       
   840 		User::After(gBetweenTestDelay);
       
   841 		}
       
   842 
       
   843 	sess.Close();
       
   844 
       
   845 	User::After(gBetweenTestDelay);
       
   846 	}
       
   847 
       
   848 //
       
   849 // End of file