kerneltest/e32test/dll/t_xxver2w.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2003-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\dll\t_xxver2w.cpp
       
    15 // Overview:
       
    16 // Check of DLL & EXE module info and DDL function ordinals
       
    17 // API Information:
       
    18 // RLibrary
       
    19 // Details:
       
    20 // - Use RLibrary::GetInfoFromHeader() and RLibrary::GetInfo() to get DLL 
       
    21 // information. Verify results are as expected.
       
    22 // - Use RLibrary::GetInfoFromHeader() and RLibrary::GetInfo() to get EXE 
       
    23 // information. Verify results are as expected.
       
    24 // - Load a DLL, lookup and verify the function at the specified ordinal 
       
    25 // within the DLL.
       
    26 // Platforms/Drives/Compatibility:
       
    27 // All.
       
    28 // Assumptions/Requirement/Pre-requisites:
       
    29 // Failures and causes:
       
    30 // Base Port information:
       
    31 // 
       
    32 //
       
    33 
       
    34 #include <e32uid.h>
       
    35 #include <e32test.h>
       
    36 #include <f32file.h>
       
    37 
       
    38 #include <e32wins.h>
       
    39 #include <emulator.h>
       
    40 #include <d_ldrtst.h>
       
    41 
       
    42 RTest test(_L("T_XXVER2W"));
       
    43 RLdrTest LdrTest;
       
    44 
       
    45 TFileName CopyBinaryFromZ(const TFileName& aFileName)
       
    46 	{
       
    47 	TParse parse;
       
    48 	test(parse.Set(aFileName,0,0)==KErrNone);
       
    49 	TFileName source = _L("z:");
       
    50 	source.Append(parse.NameAndExt());
       
    51 
       
    52 	TBuf<MAX_PATH> sName;
       
    53 	TInt r = MapEmulatedFileName(sName, parse.NameAndExt());
       
    54 	test(r==KErrNone);
       
    55 
       
    56 	TFileName destination = _S16("C:\\t_zzver2w.tmp");
       
    57 	TBuf<MAX_PATH> dName;
       
    58 	r = MapEmulatedFileName(dName, destination);
       
    59 	test(r==KErrNone);
       
    60 
       
    61 	r=Emulator::CopyFile((LPCTSTR)sName.PtrZ(),(LPCTSTR)dName.PtrZ(),FALSE);
       
    62 	test(r);
       
    63 
       
    64 	return destination;
       
    65 	}
       
    66 
       
    67 
       
    68 void TestDllInfo()
       
    69 	{
       
    70 	TFileName fn;
       
    71 	if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin))
       
    72 		fn = _S16("Z:\\sys\\bin\\t_ver2.dll");
       
    73 	else
       
    74 		fn = _S16("Z:\\system\\bin\\t_ver2.dll");
       
    75 	test.Printf(_L("Getting info for %S\n"), &fn);
       
    76 	TBool formHeader=EFalse;
       
    77 	for(;;)
       
    78 		{
       
    79 		RLibrary::TInfo info;
       
    80 		TPckg<RLibrary::TInfo> infoBuf(info);
       
    81 		TInt r;
       
    82 		if(formHeader)
       
    83 			{
       
    84 			TUint8* buf;
       
    85 
       
    86 			RFs fs;
       
    87 			test(fs.Connect()==KErrNone);
       
    88 			RFile file;
       
    89 			test((r=file.Open(fs,CopyBinaryFromZ(fn),0))==KErrNone);
       
    90 			TInt size;
       
    91 			test((r=file.Size(size))==KErrNone);
       
    92 			buf=new TUint8[size];
       
    93 			test(buf!=0);
       
    94 			TPtr8 header(buf,size);
       
    95 			test((r=file.Read(header))==KErrNone);
       
    96 			file.Close();
       
    97 			fs.Close();
       
    98 
       
    99 			r = RLibrary::GetInfoFromHeader(header, infoBuf);
       
   100 			test.Printf(_L("GetInfoFromHeader returns %d\n"), r);
       
   101 
       
   102 			delete buf;
       
   103 			}
       
   104 		else
       
   105 			{
       
   106 			r = RLibrary::GetInfo(fn, infoBuf);
       
   107 			test.Printf(_L("GetInfo returns %d\n"), r);
       
   108 			}
       
   109 
       
   110 		test(r==KErrNone);
       
   111 		const TUint32* uid = (const TUint32*)&info.iUids;
       
   112 		test.Printf(_L("VER  %08x\n"), info.iModuleVersion);
       
   113 		test.Printf(_L("UID1 %08x\n"), uid[0]);
       
   114 		test.Printf(_L("UID2 %08x\n"), uid[1]);
       
   115 		test.Printf(_L("UID3 %08x\n"), uid[2]);
       
   116 		test.Printf(_L("SID  %08x\n"), info.iSecurityInfo.iSecureId);
       
   117 		test.Printf(_L("VID  %08x\n"), info.iSecurityInfo.iVendorId);
       
   118 		test.Printf(_L("CAP0 %08x\n"), ((SSecurityInfo&)info.iSecurityInfo).iCaps[0]);
       
   119 		test.Printf(_L("CAP1 %08x\n"), ((SSecurityInfo&)info.iSecurityInfo).iCaps[1]);
       
   120 		TUint32 v = 0x00030000u;
       
   121 		test(info.iModuleVersion == v);
       
   122 		test(uid[0] == (TUint32)KDynamicLibraryUidValue);
       
   123 		test(uid[2] == (TUint32)0x40abcdef);
       
   124 		TUint32 xsid = ((v>>16)<<4)|(v&0x0f)|0x89abcd00u;
       
   125 		test(info.iSecurityInfo.iSecureId == xsid);
       
   126 		test(info.iSecurityInfo.iVendorId == 0x01234500+(xsid&0xff));
       
   127 		test(((SSecurityInfo&)info.iSecurityInfo).iCaps[0]==0x0002aaab);
       
   128 		test(((SSecurityInfo&)info.iSecurityInfo).iCaps[1]==0);
       
   129 
       
   130 		if(formHeader)
       
   131 			break;
       
   132 		formHeader = ETrue;
       
   133 		}
       
   134 	}
       
   135 
       
   136 void TestExeInfo()
       
   137 	{
       
   138 	TFileName fn;
       
   139 	if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin))
       
   140 		fn = _S16("Z:\\sys\\bin\\t_xxver2w.exe");
       
   141 	else
       
   142 		fn = _S16("Z:\\system\\bin\\t_xxver2w.exe");
       
   143 	test.Printf(_L("Getting info for %S\n"), &fn);
       
   144 	TBool formHeader=EFalse;
       
   145 	for(;;)
       
   146 		{
       
   147 		RLibrary::TInfo info;
       
   148 		TPckg<RLibrary::TInfo> infoBuf(info);
       
   149 		TInt r;
       
   150 		if(formHeader)
       
   151 			{
       
   152 			TUint8* buf;
       
   153 
       
   154 			RFs fs;
       
   155 			test(fs.Connect()==KErrNone);
       
   156 			RFile file;
       
   157 			test((r=file.Open(fs,CopyBinaryFromZ(fn),0))==KErrNone);
       
   158 			TInt size;
       
   159 			test((r=file.Size(size))==KErrNone);
       
   160 			buf=new TUint8[size];
       
   161 			test(buf!=0);
       
   162 			TPtr8 header(buf,size);
       
   163 			test((r=file.Read(header))==KErrNone);
       
   164 			file.Close();
       
   165 			fs.Close();
       
   166 
       
   167 			r = RLibrary::GetInfoFromHeader(header, infoBuf);
       
   168 			test.Printf(_L("GetInfoFromHeader returns %d\n"), r);
       
   169 
       
   170 			delete buf;
       
   171 			}
       
   172 		else
       
   173 			{
       
   174 			r = RLibrary::GetInfo(fn, infoBuf);
       
   175 			test.Printf(_L("GetInfo returns %d\n"), r);
       
   176 			}
       
   177 
       
   178 		test(r==KErrNone);
       
   179 		const TUint32* uid = (const TUint32*)&info.iUids;
       
   180 		test.Printf(_L("VER  %08x\n"), info.iModuleVersion);
       
   181 		test.Printf(_L("UID1 %08x\n"), uid[0]);
       
   182 		test.Printf(_L("UID2 %08x\n"), uid[1]);
       
   183 		test.Printf(_L("UID3 %08x\n"), uid[2]);
       
   184 		test.Printf(_L("SID  %08x\n"), info.iSecurityInfo.iSecureId);
       
   185 		test.Printf(_L("VID  %08x\n"), info.iSecurityInfo.iVendorId);
       
   186 		test.Printf(_L("CAP0 %08x\n"), ((SSecurityInfo&)info.iSecurityInfo).iCaps[0]);
       
   187 		test.Printf(_L("CAP1 %08x\n"), ((SSecurityInfo&)info.iSecurityInfo).iCaps[1]);
       
   188 		test(info.iModuleVersion == 0x00010000);
       
   189 		test(uid[0] == (TUint32)KExecutableImageUidValue);
       
   190 		TUint32 xuid3 = 0x40abcd77u;
       
   191 		test(uid[2] == xuid3);
       
   192 		test(info.iSecurityInfo.iSecureId == xuid3);
       
   193 		test(info.iSecurityInfo.iVendorId == 0x01234577);
       
   194 		test(((SSecurityInfo&)info.iSecurityInfo).iCaps[0]==0x0002aaab);
       
   195 		test(((SSecurityInfo&)info.iSecurityInfo).iCaps[1]==0);
       
   196 
       
   197 		if(formHeader)
       
   198 			break;
       
   199 		formHeader = ETrue;
       
   200 		}
       
   201 	}
       
   202 
       
   203 
       
   204 struct SExportInfo
       
   205 	{
       
   206 	TInt	iTotal;
       
   207 	TInt	iHoles;
       
   208 	TInt	iHole[1];
       
   209 	};
       
   210 
       
   211 const TInt DllExportInfo[] = {59,6,2,3,4,23,24,39};
       
   212 void CheckExports(RLibrary aLib)
       
   213 	{
       
   214 	const TFileName& fn = aLib.FileName();
       
   215 	test.Printf(_L("Filename %S\n"), &fn);
       
   216 	const SExportInfo* e = (const SExportInfo*)DllExportInfo;
       
   217 	TAny* libcs = LdrTest.LibraryCodeSeg(aLib.Handle());
       
   218 	test.Printf(_L("Code seg @%08x\n"), libcs);
       
   219 	test(libcs != NULL);
       
   220 	TInt n = e->iTotal;
       
   221 	TInt nh = e->iHoles;
       
   222 	TInt ord;
       
   223 	for (ord=1; ord<=n+1; ++ord)
       
   224 		{
       
   225 		TLibraryFunction f = aLib.Lookup(ord);
       
   226 		test.Printf(_L("Ord %3d->%08x\n"), ord, f);
       
   227 		if (ord>n)
       
   228 			{
       
   229 			test(!f);
       
   230 			continue;
       
   231 			}
       
   232 		TInt i;
       
   233 		for (i=0; i<nh && e->iHole[i]!=ord; ++i) {}
       
   234 		if (i<nh)
       
   235 			test(!f);	// hole
       
   236 		else
       
   237 			test(f!=NULL);
       
   238 		TAny* cs = LdrTest.CodeSegFromAddr((TLinAddr)f);
       
   239 		test(f ? (cs==libcs) : !cs);
       
   240 		}
       
   241 	}
       
   242 
       
   243 void TestMissingOrdinals()
       
   244 	{
       
   245 	RLibrary l;
       
   246 	TFileName fn;
       
   247 	if(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin))
       
   248 		fn = _S("Z:\\sys\\bin\\t_ver2.dll");
       
   249 	else
       
   250 		fn = _S("Z:\\system\\bin\\t_ver2.dll");
       
   251 	TInt r = l.Load(fn);
       
   252 	test.Printf(_L("Load %S returns %d\n"), &fn, r);
       
   253 	CheckExports(l);
       
   254 	l.Close();
       
   255 	}
       
   256 
       
   257 TInt E32Main()
       
   258 	{
       
   259 	test.Title();
       
   260 
       
   261 	test.Start(_L("Connect to test driver"));
       
   262 	TInt r = User::LoadLogicalDevice(_L("d_ldrtst"));
       
   263 	test(r==KErrNone || r==KErrAlreadyExists);
       
   264 	r = LdrTest.Open();
       
   265 	test(r==KErrNone);
       
   266 
       
   267 	test.Next(_L("Test DLL Info"));
       
   268 	TestDllInfo();
       
   269 
       
   270 	test.Next(_L("Test EXE Info"));
       
   271 	TestExeInfo();
       
   272 
       
   273 	test.Next(_L("Test Missing Ordinals"));
       
   274 	TestMissingOrdinals();
       
   275 
       
   276 	LdrTest.Close();
       
   277 	test.End();
       
   278 	return KErrNone;
       
   279 	}
       
   280