os/kernelhwsrv/kerneltest/e32test/device/t_tldd.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/device/t_tldd.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,329 @@
     1.4 +// Copyright (c) 1998-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 +// e32test\device\t_tldd.cpp
    1.18 +// Overview:
    1.19 +// Test device driver loading and unloading and
    1.20 +// the RBusLogicalChannel hook functionality.
    1.21 +// API Information:
    1.22 +// RBusLogicalChannel, DLogicalChannel, Kern::InstallLogicalDevice.
    1.23 +// Details:
    1.24 +// - Test loading the test logical device driver "D_LDD" and creating a logical
    1.25 +// channel.
    1.26 +// - Test initialisation of LDD data and bss sections
    1.27 +// - Test passing data to driver and back, and storing it in data/bss/heap
    1.28 +// areas
    1.29 +// - Test contructors were called for driver global data
    1.30 +// - Test functionality of Kern::InstallLogicalDevice by asking the test LDD
    1.31 +// to create a second device. Check this device works, and check it goes
    1.32 +// away when freed.
    1.33 +// - Check the return value as KErrInUse when the LDD close method is called 
    1.34 +// before closing the handle.
    1.35 +// - Close the handle and unload the device, verify success.
    1.36 +// - Check that no memory is leaked on the kernel heap
    1.37 +// - Load the logical device driver again and verify that the global variables
    1.38 +// and bss have been reinitialized.
    1.39 +// - Close the handle and unload the device, verify success.
    1.40 +// - Test User::LoadLogicalDevice and User::FreeLogicalDevice APIs, including
    1.41 +// free whilst in use and load whilst already loaded
    1.42 +// - On hardware, repeat all tests with the "D_LDD_RAM" device driver.
    1.43 +// - Check the return value is KErrNotSupported when trying to load invalid 
    1.44 +// logical device driver.
    1.45 +// Platforms/Drives/Compatibility:
    1.46 +// All.
    1.47 +// Assumptions/Requirement/Pre-requisites:
    1.48 +// Failures and causes:
    1.49 +// Base Port information:
    1.50 +// 
    1.51 +//
    1.52 +
    1.53 +#include <e32test.h>
    1.54 +#include <e32hal.h>
    1.55 +#include <e32def.h>
    1.56 +#include <e32def_private.h>
    1.57 +#include "d_ldd.h"
    1.58 +
    1.59 +_LIT(KLddFileName, "D_LDD.LDD");
    1.60 +_LIT(KLddFileNameBadUid, "D_LDDNS.LDD");
    1.61 +
    1.62 +#ifdef __EPOC32__
    1.63 +_LIT(KLddFileNameRam, "D_LDD_RAM.LDD");
    1.64 +#endif
    1.65 +
    1.66 +GLDEF_D RLddTest ldd;
    1.67 +GLDEF_D RTest test(_L("LDD tests"));
    1.68 +
    1.69 +void DoTest(const TDesC& aLddFileName)
    1.70 +	{
    1.71 +	TInt r;
    1.72 +    test.Start(_L("Loading LDD"));
    1.73 +#ifdef __EPOC32__
    1.74 +	// Need to load/unload before doing __KHEAP_MARK to ensure kernel's CodeChunk has been created
    1.75 +	r=User::LoadLogicalDevice(aLddFileName);
    1.76 +	test.Printf(_L("Returned %d\n"), r);
    1.77 +    test(r==KErrNone);
    1.78 +    test.Next(_L("Unloading LDD"));
    1.79 +	r = RLddTest::UnloadAndWait();
    1.80 +	test.Printf(_L("Returned %d\n"), r);
    1.81 +	test(r==KErrNone);
    1.82 +    test.Next(_L("Loading LDD"));
    1.83 +#endif
    1.84 +	__KHEAP_MARK;
    1.85 +	r=User::LoadLogicalDevice(aLddFileName);
    1.86 +	test.Printf(_L("Returned %d\n"), r);
    1.87 +    test(r==KErrNone);
    1.88 +    test.Next(_L("Opening device driver"));
    1.89 +	test(ldd.Open()==KErrNone);
    1.90 +	test.Next(_L("Test LDD global static function pointer"));
    1.91 +	r=ldd.Test1();
    1.92 +	test.Printf(_L("%x\n"), r);
    1.93 +	test.Next(_L("Test LDD global static data"));
    1.94 +	r=ldd.Test2();
    1.95 +	test.Printf(_L("%x\n"), r);
    1.96 +	test(r==0x100);
    1.97 +	r=ldd.Test2();
    1.98 +	test.Printf(_L("%x\n"), r);
    1.99 +	test(r==0x101);
   1.100 +	r=ldd.Test2();
   1.101 +	test.Printf(_L("%x\n"), r);
   1.102 +	test(r==0x102);
   1.103 +	r=ldd.Test3();
   1.104 +	test.Printf(_L("%x\n"), r);
   1.105 +	test(r==0x103);
   1.106 +	r=ldd.Test3();
   1.107 +	test.Printf(_L("%x\n"), r);
   1.108 +	test(r==0x102);
   1.109 +	test.Next(_L("Test LDD .bss"));
   1.110 +	r=ldd.Test5();
   1.111 +	test.Printf(_L("%x\n"), r);
   1.112 +	test(r==0);
   1.113 +	r=ldd.Test6(299792458);
   1.114 +	test.Printf(_L("%x\n"), r);
   1.115 +	test(r==KErrNone);
   1.116 +	r=ldd.Test5();
   1.117 +	test.Printf(_L("%x\n"), r);
   1.118 +	test(r==299792458);
   1.119 +	test.Next(_L("Test global constructors"));
   1.120 +	TUint32 v = ldd.Test7();
   1.121 +	test.Printf(_L("iInt = %u\n"),v);
   1.122 +	test(v==487);
   1.123 +	r = ldd.Test9();
   1.124 +	test.Printf(_L("Verify returned %d\n"), r);
   1.125 +	test(r==KErrNone);
   1.126 +	ldd.Test8(488);
   1.127 +	v = ldd.Test7();
   1.128 +	test.Printf(_L("iInt = %u\n"),v);
   1.129 +	test(v==488);
   1.130 +	r = ldd.Test9();
   1.131 +	test.Printf(_L("Verify returned %d\n"), r);
   1.132 +	test(r==KErrNone);
   1.133 +
   1.134 +	test.Next(_L("Test linked global static function pointer"));
   1.135 +	r=ldd.LinkedTest1();
   1.136 +	test.Printf(_L("%x\n"), r);
   1.137 +	test.Next(_L("Test linked global static data"));
   1.138 +	r=ldd.LinkedTest2();
   1.139 +	test.Printf(_L("%x\n"), r);
   1.140 +	test(r==0x100);
   1.141 +	r=ldd.LinkedTest2();
   1.142 +	test.Printf(_L("%x\n"), r);
   1.143 +	test(r==0x101);
   1.144 +	r=ldd.LinkedTest2();
   1.145 +	test.Printf(_L("%x\n"), r);
   1.146 +	test(r==0x102);
   1.147 +	r=ldd.LinkedTest3();
   1.148 +	test.Printf(_L("%x\n"), r);
   1.149 +	test(r==0x103);
   1.150 +	r=ldd.LinkedTest3();
   1.151 +	test.Printf(_L("%x\n"), r);
   1.152 +	test(r==0x102);
   1.153 +	test.Next(_L("Test linked .bss"));
   1.154 +	r=ldd.LinkedTest5();
   1.155 +	test.Printf(_L("%x\n"), r);
   1.156 +	test(r==0);
   1.157 +	r=ldd.LinkedTest6(299792458);
   1.158 +	test.Printf(_L("%x\n"), r);
   1.159 +	test(r==KErrNone);
   1.160 +	r=ldd.LinkedTest5();
   1.161 +	test.Printf(_L("%x\n"), r);
   1.162 +	test(r==299792458);
   1.163 +	test.Next(_L("Test linked global constructors"));
   1.164 +	v = ldd.LinkedTest7();
   1.165 +	test.Printf(_L("iInt = %u\n"),v);
   1.166 +	test(v==487);
   1.167 +	r = ldd.LinkedTest9();
   1.168 +	test.Printf(_L("Verify returned %d\n"), r);
   1.169 +	test(r==KErrNone);
   1.170 +	ldd.LinkedTest8(488);
   1.171 +	v = ldd.LinkedTest7();
   1.172 +	test.Printf(_L("iInt = %u\n"),v);
   1.173 +	test(v==488);
   1.174 +	r = ldd.LinkedTest9();
   1.175 +	test.Printf(_L("Verify returned %d\n"), r);
   1.176 +	test(r==KErrNone);
   1.177 +
   1.178 +
   1.179 +	test.Next(_L("Test behaviour of Kern::InstallLogicalDevice"));
   1.180 +	RKInstallTest ki;
   1.181 +	r = ki.Open();
   1.182 +	test(r==KErrNotFound);
   1.183 +	r = ldd.TestKInstall();
   1.184 +	test(r==KErrNone);
   1.185 +	r = ki.Open();
   1.186 +	test(r==KErrNone);
   1.187 +	ki.Close();
   1.188 +	r = User::FreeLogicalDevice(KKInstallLddName);
   1.189 +	test(r==KErrNone);
   1.190 +	r = ki.Open();
   1.191 +	test(r==KErrNotFound);
   1.192 +	
   1.193 +	test.Next(_L("Try to unload with open channel"));
   1.194 +	TInt i;
   1.195 +	for (i=0; i<10; ++i)
   1.196 +		{
   1.197 +		r = RLddTest::UnloadAndWait();
   1.198 +		test.Printf(_L("Returned %d\n"), r);
   1.199 +		test(r==KErrInUse);
   1.200 +		}
   1.201 +	test.Next(_L("Close"));
   1.202 +	ldd.Close();
   1.203 +	test.Next(_L("Unload"));
   1.204 +	r = RLddTest::UnloadAndWait();
   1.205 +	test.Printf(_L("Returned %d\n"), r);
   1.206 +	test(r==KErrNone);
   1.207 +	__KHEAP_MARKEND;
   1.208 +	test.Next(_L("Reload"));
   1.209 +	r=User::LoadLogicalDevice(aLddFileName);
   1.210 +	test.Printf(_L("Returned %d\n"), r);
   1.211 +    test(r==KErrNone);
   1.212 +	test.Next(_L("Open again"));
   1.213 +	test(ldd.Open()==KErrNone);
   1.214 +
   1.215 +	test.Next(_L("Test data re-initialised"));
   1.216 +	r=ldd.Test4();
   1.217 +	test.Printf(_L("%x\n"), r);
   1.218 +	test(r==0x100);
   1.219 +	test.Next(_L("Test .bss reinitialised"));
   1.220 +	r=ldd.Test5();
   1.221 +	test.Printf(_L("%x\n"), r);
   1.222 +	test(r==0);
   1.223 +	v = ldd.Test7();
   1.224 +	test.Printf(_L("iInt = %u\n"),v);
   1.225 +	test(v==487);
   1.226 +	r = ldd.Test9();
   1.227 +	test.Printf(_L("Verify returned %d\n"), r);
   1.228 +	test(r==KErrNone);
   1.229 +	
   1.230 +	test.Next(_L("Test linked data re-initialised"));
   1.231 +	r=ldd.LinkedTest4();
   1.232 +	test.Printf(_L("%x\n"), r);
   1.233 +	test(r==0x100);
   1.234 +	test.Next(_L("Test linked .bss reinitialised"));
   1.235 +	r=ldd.LinkedTest5();
   1.236 +	test.Printf(_L("%x\n"), r);
   1.237 +	test(r==0);
   1.238 +	v = ldd.LinkedTest7();
   1.239 +	test.Printf(_L("iInt = %u\n"),v);
   1.240 +	test(v==487);
   1.241 +	r = ldd.LinkedTest9();
   1.242 +	test.Printf(_L("Verify returned %d\n"), r);
   1.243 +	test(r==KErrNone);
   1.244 +
   1.245 +	ldd.Close();
   1.246 +
   1.247 +	test.Next(_L("Unload"));
   1.248 +	r = RLddTest::Unload();
   1.249 +	test(r==KErrNone);
   1.250 +	r=User::LoadLogicalDevice(aLddFileName);
   1.251 +    test(r==KErrNone);
   1.252 +	r = RLddTest::UnloadAndWait();
   1.253 +	test.Printf(_L("Returned %d\n"), r);
   1.254 +	test(r==KErrNone);
   1.255 +
   1.256 +	// Tests for User::LoadLogicalDevice and User::FreeLogicalDevice
   1.257 +	RDevice device;
   1.258 +	test.Next(_L("Load Device"));
   1.259 +	r=User::LoadLogicalDevice(aLddFileName);
   1.260 +    test(r==KErrNone);
   1.261 +	test.Next(_L("Open Device"));
   1.262 +	r=device.Open(KLddName);
   1.263 +    test(r==KErrNone);
   1.264 +	test.Next(_L("Unload whilst in use"));
   1.265 +	r = User::FreeLogicalDevice(KLddName);;
   1.266 +	test(r==KErrInUse);
   1.267 +	test.Next(_L("Close Device"));
   1.268 +	device.Close();
   1.269 +	test.Next(_L("Unload"));
   1.270 +	r = User::FreeLogicalDevice(KLddName);;
   1.271 +	test(r==KErrNone);
   1.272 +
   1.273 +	test.Next(_L("Load Device"));
   1.274 +	r=User::LoadLogicalDevice(aLddFileName);
   1.275 +    test(r==KErrNone);
   1.276 +	test.Next(_L("Load Device again"));
   1.277 +	r=User::LoadLogicalDevice(aLddFileName);
   1.278 +    test(r==KErrAlreadyExists);
   1.279 +	test.Next(_L("Unload"));
   1.280 +	r = User::FreeLogicalDevice(KLddName);;
   1.281 +	test(r==KErrNone);
   1.282 +	
   1.283 +	test.End();
   1.284 +	}
   1.285 +
   1.286 +
   1.287 +void DoTest2(const TDesC& aLddFileName)
   1.288 +	{
   1.289 +    test.Start(_L("Test repeated loading/unloading of RAM-based LDD"));
   1.290 +
   1.291 +	// This tests repeated loading and unloading works, and that it always
   1.292 +	// re-loads the LDD code to the same address
   1.293 +	
   1.294 +	TInt func = 0;
   1.295 +	for (TInt i = 0 ; i < 100 ; ++i)
   1.296 +		{
   1.297 +		test(User::LoadLogicalDevice(aLddFileName)==KErrNone);
   1.298 +		test(ldd.Open()==KErrNone);
   1.299 +
   1.300 +		if (i == 0)
   1.301 +			func=ldd.Test1();
   1.302 +		else	
   1.303 +			test(func==ldd.Test1());
   1.304 +		
   1.305 +		ldd.Close();
   1.306 +		test(User::FreeLogicalDevice(KLddName)==KErrNone);
   1.307 +		}
   1.308 +	test.End();
   1.309 +	}
   1.310 +
   1.311 +GLDEF_C TInt E32Main()
   1.312 +//
   1.313 +// Test LDD static data
   1.314 +//
   1.315 +    {
   1.316 +	TInt r;
   1.317 +	test.Title();
   1.318 +	test.Start(_L("Test device driver loading and unloading"));
   1.319 +
   1.320 +	DoTest(KLddFileName);
   1.321 +#ifdef __EPOC32__
   1.322 +	DoTest(KLddFileNameRam);
   1.323 +	DoTest2(KLddFileNameRam);
   1.324 +#endif
   1.325 +
   1.326 +	r=User::LoadLogicalDevice(KLddFileNameBadUid);
   1.327 +	test(r==KErrNotSupported);
   1.328 +
   1.329 +    test.End();
   1.330 +	return(KErrNone);
   1.331 +    }
   1.332 +