kerneltest/e32test/device/t_tldd.cpp
changeset 0 a41df078684a
child 293 0659d0e1a03c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-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 // e32test\device\t_tldd.cpp
       
    15 // Overview:
       
    16 // Test device driver loading and unloading and
       
    17 // the RBusLogicalChannel hook functionality.
       
    18 // API Information:
       
    19 // RBusLogicalChannel, DLogicalChannel, Kern::InstallLogicalDevice.
       
    20 // Details:
       
    21 // - Test loading the test logical device driver "D_LDD" and creating a logical
       
    22 // channel.
       
    23 // - Test initialisation of LDD data and bss sections
       
    24 // - Test passing data to driver and back, and storing it in data/bss/heap
       
    25 // areas
       
    26 // - Test contructors were called for driver global data
       
    27 // - Test functionality of Kern::InstallLogicalDevice by asking the test LDD
       
    28 // to create a second device. Check this device works, and check it goes
       
    29 // away when freed.
       
    30 // - Check the return value as KErrInUse when the LDD close method is called 
       
    31 // before closing the handle.
       
    32 // - Close the handle and unload the device, verify success.
       
    33 // - Check that no memory is leaked on the kernel heap
       
    34 // - Load the logical device driver again and verify that the global variables
       
    35 // and bss have been reinitialized.
       
    36 // - Close the handle and unload the device, verify success.
       
    37 // - Test User::LoadLogicalDevice and User::FreeLogicalDevice APIs, including
       
    38 // free whilst in use and load whilst already loaded
       
    39 // - On hardware, repeat all tests with the "D_LDD_RAM" device driver.
       
    40 // - Check the return value is KErrNotSupported when trying to load invalid 
       
    41 // logical device driver.
       
    42 // Platforms/Drives/Compatibility:
       
    43 // All.
       
    44 // Assumptions/Requirement/Pre-requisites:
       
    45 // Failures and causes:
       
    46 // Base Port information:
       
    47 // 
       
    48 //
       
    49 
       
    50 #include <e32test.h>
       
    51 #include <e32hal.h>
       
    52 #include <e32def.h>
       
    53 #include <e32def_private.h>
       
    54 #include "d_ldd.h"
       
    55 
       
    56 _LIT(KLddFileName, "D_LDD.LDD");
       
    57 _LIT(KLddFileNameBadUid, "D_LDDNS.LDD");
       
    58 
       
    59 #ifdef __EPOC32__
       
    60 _LIT(KLddFileNameRam, "D_LDD_RAM.LDD");
       
    61 #endif
       
    62 
       
    63 GLDEF_D RLddTest ldd;
       
    64 GLDEF_D RTest test(_L("LDD tests"));
       
    65 
       
    66 void DoTest(const TDesC& aLddFileName)
       
    67 	{
       
    68 	TInt r;
       
    69     test.Start(_L("Loading LDD"));
       
    70 #ifdef __EPOC32__
       
    71 	// Need to load/unload before doing __KHEAP_MARK to ensure kernel's CodeChunk has been created
       
    72 	r=User::LoadLogicalDevice(aLddFileName);
       
    73 	test.Printf(_L("Returned %d\n"), r);
       
    74     test(r==KErrNone);
       
    75     test.Next(_L("Unloading LDD"));
       
    76 	r = RLddTest::UnloadAndWait();
       
    77 	test.Printf(_L("Returned %d\n"), r);
       
    78 	test(r==KErrNone);
       
    79     test.Next(_L("Loading LDD"));
       
    80 #endif
       
    81 	__KHEAP_MARK;
       
    82 	r=User::LoadLogicalDevice(aLddFileName);
       
    83 	test.Printf(_L("Returned %d\n"), r);
       
    84     test(r==KErrNone);
       
    85     test.Next(_L("Opening device driver"));
       
    86 	test(ldd.Open()==KErrNone);
       
    87 	test.Next(_L("Test LDD global static function pointer"));
       
    88 	r=ldd.Test1();
       
    89 	test.Printf(_L("%x\n"), r);
       
    90 	test.Next(_L("Test LDD global static data"));
       
    91 	r=ldd.Test2();
       
    92 	test.Printf(_L("%x\n"), r);
       
    93 	test(r==0x100);
       
    94 	r=ldd.Test2();
       
    95 	test.Printf(_L("%x\n"), r);
       
    96 	test(r==0x101);
       
    97 	r=ldd.Test2();
       
    98 	test.Printf(_L("%x\n"), r);
       
    99 	test(r==0x102);
       
   100 	r=ldd.Test3();
       
   101 	test.Printf(_L("%x\n"), r);
       
   102 	test(r==0x103);
       
   103 	r=ldd.Test3();
       
   104 	test.Printf(_L("%x\n"), r);
       
   105 	test(r==0x102);
       
   106 	test.Next(_L("Test LDD .bss"));
       
   107 	r=ldd.Test5();
       
   108 	test.Printf(_L("%x\n"), r);
       
   109 	test(r==0);
       
   110 	r=ldd.Test6(299792458);
       
   111 	test.Printf(_L("%x\n"), r);
       
   112 	test(r==KErrNone);
       
   113 	r=ldd.Test5();
       
   114 	test.Printf(_L("%x\n"), r);
       
   115 	test(r==299792458);
       
   116 	test.Next(_L("Test global constructors"));
       
   117 	TUint32 v = ldd.Test7();
       
   118 	test.Printf(_L("iInt = %u\n"),v);
       
   119 	test(v==487);
       
   120 	r = ldd.Test9();
       
   121 	test.Printf(_L("Verify returned %d\n"), r);
       
   122 	test(r==KErrNone);
       
   123 	ldd.Test8(488);
       
   124 	v = ldd.Test7();
       
   125 	test.Printf(_L("iInt = %u\n"),v);
       
   126 	test(v==488);
       
   127 	r = ldd.Test9();
       
   128 	test.Printf(_L("Verify returned %d\n"), r);
       
   129 	test(r==KErrNone);
       
   130 
       
   131 	test.Next(_L("Test linked global static function pointer"));
       
   132 	r=ldd.LinkedTest1();
       
   133 	test.Printf(_L("%x\n"), r);
       
   134 	test.Next(_L("Test linked global static data"));
       
   135 	r=ldd.LinkedTest2();
       
   136 	test.Printf(_L("%x\n"), r);
       
   137 	test(r==0x100);
       
   138 	r=ldd.LinkedTest2();
       
   139 	test.Printf(_L("%x\n"), r);
       
   140 	test(r==0x101);
       
   141 	r=ldd.LinkedTest2();
       
   142 	test.Printf(_L("%x\n"), r);
       
   143 	test(r==0x102);
       
   144 	r=ldd.LinkedTest3();
       
   145 	test.Printf(_L("%x\n"), r);
       
   146 	test(r==0x103);
       
   147 	r=ldd.LinkedTest3();
       
   148 	test.Printf(_L("%x\n"), r);
       
   149 	test(r==0x102);
       
   150 	test.Next(_L("Test linked .bss"));
       
   151 	r=ldd.LinkedTest5();
       
   152 	test.Printf(_L("%x\n"), r);
       
   153 	test(r==0);
       
   154 	r=ldd.LinkedTest6(299792458);
       
   155 	test.Printf(_L("%x\n"), r);
       
   156 	test(r==KErrNone);
       
   157 	r=ldd.LinkedTest5();
       
   158 	test.Printf(_L("%x\n"), r);
       
   159 	test(r==299792458);
       
   160 	test.Next(_L("Test linked global constructors"));
       
   161 	v = ldd.LinkedTest7();
       
   162 	test.Printf(_L("iInt = %u\n"),v);
       
   163 	test(v==487);
       
   164 	r = ldd.LinkedTest9();
       
   165 	test.Printf(_L("Verify returned %d\n"), r);
       
   166 	test(r==KErrNone);
       
   167 	ldd.LinkedTest8(488);
       
   168 	v = ldd.LinkedTest7();
       
   169 	test.Printf(_L("iInt = %u\n"),v);
       
   170 	test(v==488);
       
   171 	r = ldd.LinkedTest9();
       
   172 	test.Printf(_L("Verify returned %d\n"), r);
       
   173 	test(r==KErrNone);
       
   174 
       
   175 
       
   176 	test.Next(_L("Test behaviour of Kern::InstallLogicalDevice"));
       
   177 	RKInstallTest ki;
       
   178 	r = ki.Open();
       
   179 	test(r==KErrNotFound);
       
   180 	r = ldd.TestKInstall();
       
   181 	test(r==KErrNone);
       
   182 	r = ki.Open();
       
   183 	test(r==KErrNone);
       
   184 	ki.Close();
       
   185 	r = User::FreeLogicalDevice(KKInstallLddName);
       
   186 	test(r==KErrNone);
       
   187 	r = ki.Open();
       
   188 	test(r==KErrNotFound);
       
   189 	
       
   190 	test.Next(_L("Try to unload with open channel"));
       
   191 	TInt i;
       
   192 	for (i=0; i<10; ++i)
       
   193 		{
       
   194 		r = RLddTest::UnloadAndWait();
       
   195 		test.Printf(_L("Returned %d\n"), r);
       
   196 		test(r==KErrInUse);
       
   197 		}
       
   198 	test.Next(_L("Close"));
       
   199 	ldd.Close();
       
   200 	test.Next(_L("Unload"));
       
   201 	r = RLddTest::UnloadAndWait();
       
   202 	test.Printf(_L("Returned %d\n"), r);
       
   203 	test(r==KErrNone);
       
   204 	__KHEAP_MARKEND;
       
   205 	test.Next(_L("Reload"));
       
   206 	r=User::LoadLogicalDevice(aLddFileName);
       
   207 	test.Printf(_L("Returned %d\n"), r);
       
   208     test(r==KErrNone);
       
   209 	test.Next(_L("Open again"));
       
   210 	test(ldd.Open()==KErrNone);
       
   211 
       
   212 	test.Next(_L("Test data re-initialised"));
       
   213 	r=ldd.Test4();
       
   214 	test.Printf(_L("%x\n"), r);
       
   215 	test(r==0x100);
       
   216 	test.Next(_L("Test .bss reinitialised"));
       
   217 	r=ldd.Test5();
       
   218 	test.Printf(_L("%x\n"), r);
       
   219 	test(r==0);
       
   220 	v = ldd.Test7();
       
   221 	test.Printf(_L("iInt = %u\n"),v);
       
   222 	test(v==487);
       
   223 	r = ldd.Test9();
       
   224 	test.Printf(_L("Verify returned %d\n"), r);
       
   225 	test(r==KErrNone);
       
   226 	
       
   227 	test.Next(_L("Test linked data re-initialised"));
       
   228 	r=ldd.LinkedTest4();
       
   229 	test.Printf(_L("%x\n"), r);
       
   230 	test(r==0x100);
       
   231 	test.Next(_L("Test linked .bss reinitialised"));
       
   232 	r=ldd.LinkedTest5();
       
   233 	test.Printf(_L("%x\n"), r);
       
   234 	test(r==0);
       
   235 	v = ldd.LinkedTest7();
       
   236 	test.Printf(_L("iInt = %u\n"),v);
       
   237 	test(v==487);
       
   238 	r = ldd.LinkedTest9();
       
   239 	test.Printf(_L("Verify returned %d\n"), r);
       
   240 	test(r==KErrNone);
       
   241 
       
   242 	ldd.Close();
       
   243 
       
   244 	test.Next(_L("Unload"));
       
   245 	r = RLddTest::Unload();
       
   246 	test(r==KErrNone);
       
   247 	r=User::LoadLogicalDevice(aLddFileName);
       
   248     test(r==KErrNone);
       
   249 	r = RLddTest::UnloadAndWait();
       
   250 	test.Printf(_L("Returned %d\n"), r);
       
   251 	test(r==KErrNone);
       
   252 
       
   253 	// Tests for User::LoadLogicalDevice and User::FreeLogicalDevice
       
   254 	RDevice device;
       
   255 	test.Next(_L("Load Device"));
       
   256 	r=User::LoadLogicalDevice(aLddFileName);
       
   257     test(r==KErrNone);
       
   258 	test.Next(_L("Open Device"));
       
   259 	r=device.Open(KLddName);
       
   260     test(r==KErrNone);
       
   261 	test.Next(_L("Unload whilst in use"));
       
   262 	r = User::FreeLogicalDevice(KLddName);;
       
   263 	test(r==KErrInUse);
       
   264 	test.Next(_L("Close Device"));
       
   265 	device.Close();
       
   266 	test.Next(_L("Unload"));
       
   267 	r = User::FreeLogicalDevice(KLddName);;
       
   268 	test(r==KErrNone);
       
   269 
       
   270 	test.Next(_L("Load Device"));
       
   271 	r=User::LoadLogicalDevice(aLddFileName);
       
   272     test(r==KErrNone);
       
   273 	test.Next(_L("Load Device again"));
       
   274 	r=User::LoadLogicalDevice(aLddFileName);
       
   275     test(r==KErrAlreadyExists);
       
   276 	test.Next(_L("Unload"));
       
   277 	r = User::FreeLogicalDevice(KLddName);;
       
   278 	test(r==KErrNone);
       
   279 	
       
   280 	test.End();
       
   281 	}
       
   282 
       
   283 
       
   284 void DoTest2(const TDesC& aLddFileName)
       
   285 	{
       
   286     test.Start(_L("Test repeated loading/unloading of RAM-based LDD"));
       
   287 
       
   288 	// This tests repeated loading and unloading works, and that it always
       
   289 	// re-loads the LDD code to the same address
       
   290 	
       
   291 	TInt func = 0;
       
   292 	for (TInt i = 0 ; i < 100 ; ++i)
       
   293 		{
       
   294 		test(User::LoadLogicalDevice(aLddFileName)==KErrNone);
       
   295 		test(ldd.Open()==KErrNone);
       
   296 
       
   297 		if (i == 0)
       
   298 			func=ldd.Test1();
       
   299 		else	
       
   300 			test(func==ldd.Test1());
       
   301 		
       
   302 		ldd.Close();
       
   303 		test(User::FreeLogicalDevice(KLddName)==KErrNone);
       
   304 		}
       
   305 	test.End();
       
   306 	}
       
   307 
       
   308 GLDEF_C TInt E32Main()
       
   309 //
       
   310 // Test LDD static data
       
   311 //
       
   312     {
       
   313 	TInt r;
       
   314 	test.Title();
       
   315 	test.Start(_L("Test device driver loading and unloading"));
       
   316 
       
   317 	DoTest(KLddFileName);
       
   318 #ifdef __EPOC32__
       
   319 	DoTest(KLddFileNameRam);
       
   320 	DoTest2(KLddFileNameRam);
       
   321 #endif
       
   322 
       
   323 	r=User::LoadLogicalDevice(KLddFileNameBadUid);
       
   324 	test(r==KErrNotSupported);
       
   325 
       
   326     test.End();
       
   327 	return(KErrNone);
       
   328     }
       
   329