]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/UnitTest/DecParserTest.py
2 # This file contain unit test for DecParser
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials are licensed and made available
7 # under the terms and conditions of the BSD License which accompanies this
8 # distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 from __future__
import print_function
18 from Parser
.DecParserMisc
import \
22 from Parser
.DecParser
import Dec
24 from Library
.ParserValidate
import IsValidCFormatGuid
30 assert IsValidCArray('{0x1, 0x23}')
33 assert not IsValidCArray('{0x1, 0x23, }')
36 assert not IsValidCArray('{0x1, 0x2345}')
39 assert not IsValidCArray('{0x1, 0x23, ')
41 # Whitespace between numbers
42 assert not IsValidCArray('{0x1, 0x2 3, }')
44 assert IsValidPcdDatum('VOID*', '"test"')[0]
45 assert IsValidPcdDatum('VOID*', 'L"test"')[0]
46 assert IsValidPcdDatum('BOOLEAN', 'TRUE')[0]
47 assert IsValidPcdDatum('BOOLEAN', 'FALSE')[0]
48 assert IsValidPcdDatum('BOOLEAN', '0')[0]
49 assert IsValidPcdDatum('BOOLEAN', '1')[0]
50 assert IsValidPcdDatum('UINT8', '0xab')[0]
52 assert not IsValidPcdDatum('UNKNOWNTYPE', '0xabc')[0]
53 assert not IsValidPcdDatum('UINT8', 'not number')[0]
55 assert( IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }}'))
56 assert( not IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }} 0xaa'))
58 def TestTemplate(TestString
, TestFunc
):
59 Path
= os
.path
.join(os
.getcwd(), 'test.dec')
60 Path
= os
.path
.normpath(Path
)
64 # Write test string to file
70 print('Can not create temporary file [%s]!' % Path
)
73 # Call test function to test
74 Ret
= TestFunc(Path
, TestString
)
76 # Test done, remove temporary file
80 # To make test unit works OK, must set IsRaiseError to True
81 # This function test right syntax DEC file
82 # @retval: parser object
84 def TestOK(Path
, TestString
):
88 raise 'Bug!!! Correct syntax in DEC file, but exception raised!\n' + TestString
91 # This function test wrong syntax DEC file
92 # if parser checked wrong syntax, exception thrown and it's expected result
93 def TestError(Path
, TestString
):
97 # Raise error, get expected result
99 raise 'Bug!!! Wrong syntax in DEC file, but passed by DEC parser!!\n' + TestString
104 DEC_SPECIFICATION = 0x00010005
105 PACKAGE_NAME = MdePkg
106 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
107 PACKAGE_VERSION = 1.02
109 Parser
= TestTemplate(TestString
, TestOK
)
110 DefObj
= Parser
.GetDefineSectionObject()
111 assert DefObj
.GetPackageSpecification() == '0x00010005'
112 assert DefObj
.GetPackageName() == 'MdePkg'
113 assert DefObj
.GetPackageGuid() == '1E73767F-8F52-4603-AEB4-F29B510B6766'
114 assert DefObj
.GetPackageVersion() == '1.02'
118 UNKNOW_KEY = 0x00010005 # A unknown key
120 assert TestTemplate(TestString
, TestError
)
124 PACKAGE_GUID = F-8F52-4603-AEB4-F29B510B6766 # Error GUID
126 assert TestTemplate(TestString
, TestError
)
128 def TestDecInclude():
131 DEC_SPECIFICATION = 0x00010005
132 PACKAGE_NAME = MdePkg
133 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
134 PACKAGE_VERSION = 1.02
142 # Create directory in current directory
144 os
.makedirs('Include/Ia32')
147 Parser
= TestTemplate(TestString
, TestOK
)
149 IncObj
= Parser
.GetIncludeSectionObject()
150 Items
= IncObj
.GetIncludes()
151 assert len(Items
) == 1
152 assert Items
[0].File
== 'Include'
154 Items
= IncObj
.GetIncludes('IA32')
155 assert len(Items
) == 1
156 # normpath is called in DEC parser so '/' is converted to '\'
157 assert Items
[0].File
== 'Include\\Ia32'
161 DEC_SPECIFICATION = 0x00010005
162 PACKAGE_NAME = MdePkg
163 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
164 PACKAGE_VERSION = 1.02
166 Include_not_exist # directory does not exist
168 assert TestTemplate(TestString
, TestError
)
170 os
.removedirs('Include/Ia32')
172 def TestDecGuidPpiProtocol():
175 DEC_SPECIFICATION = 0x00010005
176 PACKAGE_NAME = MdePkg
177 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
178 PACKAGE_VERSION = 1.02
181 # GUID defined in UEFI2.1/UEFI2.0/EFI1.1
183 ## Include/Guid/GlobalVariable.h
184 gEfiGlobalVariableGuid = { 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}
186 ## Include/Protocol/Bds.h
187 gEfiBdsArchProtocolGuid = { 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}
189 ## Include/Ppi/MasterBootMode.h
190 gEfiPeiMasterBootModePpiGuid = { 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }
192 Parser
= TestTemplate(TestString
, TestOK
)
193 Obj
= Parser
.GetGuidSectionObject()
194 Items
= Obj
.GetGuids()
195 assert Obj
.GetSectionName() == 'Guids'.upper()
196 assert len(Items
) == 1
197 assert Items
[0].GuidCName
== 'gEfiGlobalVariableGuid'
198 assert Items
[0].GuidCValue
== '{ 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}'
200 Obj
= Parser
.GetProtocolSectionObject()
201 Items
= Obj
.GetProtocols()
202 assert Obj
.GetSectionName() == 'Protocols'.upper()
203 assert len(Items
) == 1
204 assert Items
[0].GuidCName
== 'gEfiBdsArchProtocolGuid'
205 assert Items
[0].GuidCValue
== '{ 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}'
207 Obj
= Parser
.GetPpiSectionObject()
208 Items
= Obj
.GetPpis()
209 assert Obj
.GetSectionName() == 'Ppis'.upper()
210 assert len(Items
) == 1
211 assert Items
[0].GuidCName
== 'gEfiPeiMasterBootModePpiGuid'
212 assert Items
[0].GuidCValue
== '{ 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }'
217 DEC_SPECIFICATION = 0x00010005
218 PACKAGE_NAME = MdePkg
219 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
220 PACKAGE_VERSION = 1.02
222 ## If TRUE, the component name protocol will not be installed.
223 gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d
226 ## Indicates the maximum length of unicode string
227 gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength|1000000|UINT32|0x00000001
229 [PcdsFixedAtBuild.IPF]
230 ## The base address of IO port space for IA64 arch
231 gEfiMdePkgTokenSpaceGuid.PcdIoBlockBaseAddressForIpf|0x0ffffc000000|UINT64|0x0000000f
233 [PcdsFixedAtBuild,PcdsPatchableInModule]
234 ## This flag is used to control the printout of DebugLib
235 gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000000|UINT32|0x00000006
237 [PcdsFixedAtBuild,PcdsPatchableInModule,PcdsDynamic]
238 ## This value is used to set the base address of pci express hierarchy
239 gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress|0xE0000000|UINT64|0x0000000a
241 Parser
= TestTemplate(TestString
, TestOK
)
242 Obj
= Parser
.GetPcdSectionObject()
243 Items
= Obj
.GetPcds('PcdsFeatureFlag', 'COMMON')
244 assert len(Items
) == 1
245 assert Items
[0].TokenSpaceGuidCName
== 'gEfiMdePkgTokenSpaceGuid'
246 assert Items
[0].TokenCName
== 'PcdComponentNameDisable'
247 assert Items
[0].DefaultValue
== 'FALSE'
248 assert Items
[0].DatumType
== 'BOOLEAN'
249 assert Items
[0].TokenValue
== '0x0000000d'
251 Items
= Obj
.GetPcdsByType('PcdsFixedAtBuild')
252 assert len(Items
) == 4
253 assert len(Obj
.GetPcdsByType('PcdsPatchableInModule')) == 2
255 def TestDecUserExtension():
258 DEC_SPECIFICATION = 0x00010005
259 PACKAGE_NAME = MdePkg
260 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
261 PACKAGE_VERSION = 1.02
262 [UserExtensions.MyID."TestString".IA32]
265 Parser
= TestTemplate(TestString
, TestOK
)
266 Obj
= Parser
.GetUserExtensionSectionObject()
267 Items
= Obj
.GetAllUserExtensions()
268 assert len(Items
) == 1
269 assert Items
[0].UserString
== 'Some Strings...'
270 assert len(Items
[0].ArchAndModuleType
) == 1
271 assert ['MyID', '"TestString"', 'IA32'] in Items
[0].ArchAndModuleType
273 if __name__
== '__main__':
275 Logger
.Logger
.Initialize()
276 unittest
.FunctionTestCase(TestToolFuncs
).runTest()
277 unittest
.FunctionTestCase(TestDecDefine
).runTest()
278 unittest
.FunctionTestCase(TestDecInclude
).runTest()
279 unittest
.FunctionTestCase(TestDecGuidPpiProtocol
).runTest()
280 unittest
.FunctionTestCase(TestDecPcd
).runTest()
281 unittest
.FunctionTestCase(TestDecUserExtension
).runTest()
283 print('All tests passed...')