kerneltest/f32test/smassstorage/src/t_ms_clisvr.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2004-2009 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 the License "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32test\msfs\src\t_ms_clisvr.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #define __T_MS_CLISVR__
       
    19 
       
    20 #include <f32file.h>
       
    21 #include <e32test.h>
       
    22 #include <e32std.h>
       
    23 #include <e32std_private.h>
       
    24 #include <e32svr.h>
       
    25 #include <hal.h>
       
    26 #include <massstorage.h>
       
    27 #include "rusbmassstorage.h"
       
    28 #include "t_ms_main.h"
       
    29 
       
    30 #ifdef __USBMS_NO_PROCESSES__
       
    31 #include <e32math.h>
       
    32 #endif
       
    33 
       
    34 GLDEF_D RUsbMassStorage gUsbMs;
       
    35 
       
    36 // Unit test for MS client API and server API
       
    37 GLREF_C void t_ms_clisvr();
       
    38 
       
    39 // Unit test for MS file server
       
    40 GLREF_C void t_ms_fsunit();
       
    41 
       
    42 
       
    43 LOCAL_C TInt StartServer()
       
    44 //
       
    45 // Start the server process or thread
       
    46 //
       
    47 	{
       
    48 	const TUidType serverUid(KNullUid, KNullUid, KUsbMsSvrUid);
       
    49 
       
    50 #ifdef __USBMS_NO_PROCESSES__
       
    51 	//
       
    52 	// In EKA1 WINS the server is a DLL, the exported entrypoint returns a TInt
       
    53 	// which represents the real entry-point for the server thread
       
    54 	//
       
    55 	RLibrary lib;
       
    56 	TInt err = lib.Load(KUsbMsImg, serverUid);
       
    57 
       
    58 	if (err != KErrNone )
       
    59 		{
       
    60 		return err;
       
    61 		}
       
    62 	TLibraryFunction ordinal1 = lib.Lookup(1);
       
    63 	TThreadFunction serverFunc = reinterpret_cast<TThreadFunction>(ordinal1());
       
    64 
       
    65 	//
       
    66 	// To deal with the unique thread (+semaphore!) naming in EPOC, and that we may
       
    67 	// be trying to restart a server that has just exited we attempt to create a
       
    68 	// unique thread name for the server.
       
    69 	// This uses Math::Random() to generate a 32-bit random number for the name
       
    70 	//
       
    71 	TName name(KUsbMsServerName);
       
    72 	name.AppendNum(Math::Random(),EHex);
       
    73 	
       
    74 	RThread server;
       
    75 	err = server.Create (
       
    76 		name,
       
    77 		serverFunc,
       
    78 		KUsbMsStackSize,
       
    79 		NULL,
       
    80 		&lib,
       
    81 		NULL,
       
    82 		KUsbMsMinHeapSize,
       
    83 		KUsbMsMaxHeapSize,
       
    84 		EOwnerProcess
       
    85 	);
       
    86 
       
    87 	lib.Close();	// if successful, server thread has handle to library now
       
    88 #else
       
    89 	//
       
    90 	// EPOC and EKA2 is easy, we just create a new server process. Simultaneous
       
    91 	// launching of two such processes should be detected when the second one
       
    92 	// attempts to create the server object, failing with KErrAlreadyExists.
       
    93 	//
       
    94 	RProcess server;
       
    95 	TInt err = server.Create(KUsbMsImg1, KNullDesC, serverUid);
       
    96 	if (err == KErrNotFound)
       
    97 		{
       
    98 		err = server.Create(KUsbMsImg, KNullDesC, serverUid);
       
    99 		}
       
   100 #endif 
       
   101 	
       
   102 	if (err != KErrNone)
       
   103 		{
       
   104 		return err;
       
   105 		}
       
   106 
       
   107 	TRequestStatus stat;
       
   108 	server.Rendezvous(stat);
       
   109 	
       
   110 	if (stat!=KRequestPending)
       
   111 		server.Kill(0);		// abort startup
       
   112 	else
       
   113 		server.Resume();	// logon OK - start the server
       
   114 
       
   115 	User::WaitForRequest(stat);		// wait for start or death
       
   116 
       
   117 	// we can't use the 'exit reason' if the server panicked as this
       
   118 	// is the panic 'reason' and may be '0' which cannot be distinguished
       
   119 	// from KErrNone
       
   120 	err = (server.ExitType() == EExitPanic) ? KErrServerTerminated : stat.Int();
       
   121 
       
   122 	server.Close();
       
   123 
       
   124 	return err;
       
   125 	}
       
   126 
       
   127 LOCAL_C void DoTestConnect()
       
   128     // 
       
   129     // Connect to the file server
       
   130     // 
       
   131     {
       
   132     test.Printf(_L("DoTestConnect\n"));
       
   133     // Load MS file server
       
   134     TInt r = StartServer();
       
   135     test(KErrNone == r);
       
   136     // Connect to MS file server
       
   137     r = gUsbMs.Connect();
       
   138     test(KErrNone == r);
       
   139     
       
   140     test.Printf(_L("DoTestConnect ====> PASS\n"));
       
   141     }
       
   142     
       
   143 LOCAL_C void DoTestStart()
       
   144     //
       
   145     // Start mass storage device
       
   146     //
       
   147     {
       
   148 
       
   149     TMassStorageConfig msConfig;
       
   150     msConfig.iVendorId.Copy(t_vendorId);
       
   151     msConfig.iProductId.Copy(t_productId);
       
   152     msConfig.iProductRev.Copy(t_productRev);
       
   153 
       
   154     TInt r = gUsbMs.Start(msConfig);
       
   155     test(KErrNone == r);
       
   156     
       
   157     test.Printf(_L("DoTestStart ====> PASS\n"));
       
   158     }
       
   159 
       
   160 LOCAL_C void DoTestStop()
       
   161     //
       
   162     // Stop USB device
       
   163     //
       
   164     {
       
   165     test.Printf(_L("TestStop\n"));
       
   166     TInt r = gUsbMs.Stop();
       
   167     test(KErrNone == r); 
       
   168     
       
   169     test.Printf(_L("DoTestStop ====> PASS\n"));;
       
   170     }
       
   171 
       
   172 GLDEF_C void t_ms_clisvr()
       
   173 //
       
   174 // Do all tests
       
   175 //
       
   176 	{
       
   177 	test.Next( _L("TestConnect") );
       
   178     DoTestConnect();
       
   179    	test.Next(_L("TestStart"));
       
   180     DoTestStart();
       
   181     test.Next(_L("TestStop"));
       
   182     DoTestStop();
       
   183  	gUsbMs.Shutdown();
       
   184 	gUsbMs.Close();
       
   185     
       
   186     }
       
   187 
       
   188 GLDEF_C void CallTestsL()
       
   189 	{
       
   190 	test.Start(_L("ClientServer Tests"));
       
   191 	t_ms_clisvr();
       
   192 	test.End();
       
   193 	test.Start(_L("File System Unit Tests"));
       
   194 	t_ms_fsunit();
       
   195 	test.End();
       
   196 	}