]>
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 # SPDX-License-Identifier: BSD-2-Clause-Patent
8 from __future__
import print_function
12 from Parser
.DecParserMisc
import \
16 from Parser
.DecParser
import Dec
18 from Library
.ParserValidate
import IsValidCFormatGuid
24 assert IsValidCArray('{0x1, 0x23}')
27 assert not IsValidCArray('{0x1, 0x23, }')
30 assert not IsValidCArray('{0x1, 0x2345}')
33 assert not IsValidCArray('{0x1, 0x23, ')
35 # Whitespace between numbers
36 assert not IsValidCArray('{0x1, 0x2 3, }')
38 assert IsValidPcdDatum('VOID*', '"test"')[0]
39 assert IsValidPcdDatum('VOID*', 'L"test"')[0]
40 assert IsValidPcdDatum('BOOLEAN', 'TRUE')[0]
41 assert IsValidPcdDatum('BOOLEAN', 'FALSE')[0]
42 assert IsValidPcdDatum('BOOLEAN', '0')[0]
43 assert IsValidPcdDatum('BOOLEAN', '1')[0]
44 assert IsValidPcdDatum('UINT8', '0xab')[0]
46 assert not IsValidPcdDatum('UNKNOWNTYPE', '0xabc')[0]
47 assert not IsValidPcdDatum('UINT8', 'not number')[0]
49 assert( IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }}'))
50 assert( not IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }} 0xaa'))
52 def TestTemplate(TestString
, TestFunc
):
53 Path
= os
.path
.join(os
.getcwd(), 'test.dec')
54 Path
= os
.path
.normpath(Path
)
58 # Write test string to file
64 print('Can not create temporary file [%s]!' % Path
)
67 # Call test function to test
68 Ret
= TestFunc(Path
, TestString
)
70 # Test done, remove temporary file
74 # To make test unit works OK, must set IsRaiseError to True
75 # This function test right syntax DEC file
76 # @retval: parser object
78 def TestOK(Path
, TestString
):
82 raise 'Bug!!! Correct syntax in DEC file, but exception raised!\n' + TestString
85 # This function test wrong syntax DEC file
86 # if parser checked wrong syntax, exception thrown and it's expected result
87 def TestError(Path
, TestString
):
91 # Raise error, get expected result
93 raise 'Bug!!! Wrong syntax in DEC file, but passed by DEC parser!!\n' + TestString
98 DEC_SPECIFICATION = 0x00010005
100 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
101 PACKAGE_VERSION = 1.02
103 Parser
= TestTemplate(TestString
, TestOK
)
104 DefObj
= Parser
.GetDefineSectionObject()
105 assert DefObj
.GetPackageSpecification() == '0x00010005'
106 assert DefObj
.GetPackageName() == 'MdePkg'
107 assert DefObj
.GetPackageGuid() == '1E73767F-8F52-4603-AEB4-F29B510B6766'
108 assert DefObj
.GetPackageVersion() == '1.02'
112 UNKNOW_KEY = 0x00010005 # A unknown key
114 assert TestTemplate(TestString
, TestError
)
118 PACKAGE_GUID = F-8F52-4603-AEB4-F29B510B6766 # Error GUID
120 assert TestTemplate(TestString
, TestError
)
122 def TestDecInclude():
125 DEC_SPECIFICATION = 0x00010005
126 PACKAGE_NAME = MdePkg
127 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
128 PACKAGE_VERSION = 1.02
136 # Create directory in current directory
138 os
.makedirs('Include/Ia32')
141 Parser
= TestTemplate(TestString
, TestOK
)
143 IncObj
= Parser
.GetIncludeSectionObject()
144 Items
= IncObj
.GetIncludes()
145 assert len(Items
) == 1
146 assert Items
[0].File
== 'Include'
148 Items
= IncObj
.GetIncludes('IA32')
149 assert len(Items
) == 1
150 # normpath is called in DEC parser so '/' is converted to '\'
151 assert Items
[0].File
== 'Include\\Ia32'
155 DEC_SPECIFICATION = 0x00010005
156 PACKAGE_NAME = MdePkg
157 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
158 PACKAGE_VERSION = 1.02
160 Include_not_exist # directory does not exist
162 assert TestTemplate(TestString
, TestError
)
164 os
.removedirs('Include/Ia32')
166 def TestDecGuidPpiProtocol():
169 DEC_SPECIFICATION = 0x00010005
170 PACKAGE_NAME = MdePkg
171 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
172 PACKAGE_VERSION = 1.02
175 # GUID defined in UEFI2.1/UEFI2.0/EFI1.1
177 ## Include/Guid/GlobalVariable.h
178 gEfiGlobalVariableGuid = { 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}
180 ## Include/Protocol/Bds.h
181 gEfiBdsArchProtocolGuid = { 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}
183 ## Include/Ppi/MasterBootMode.h
184 gEfiPeiMasterBootModePpiGuid = { 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }
186 Parser
= TestTemplate(TestString
, TestOK
)
187 Obj
= Parser
.GetGuidSectionObject()
188 Items
= Obj
.GetGuids()
189 assert Obj
.GetSectionName() == 'Guids'.upper()
190 assert len(Items
) == 1
191 assert Items
[0].GuidCName
== 'gEfiGlobalVariableGuid'
192 assert Items
[0].GuidCValue
== '{ 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}'
194 Obj
= Parser
.GetProtocolSectionObject()
195 Items
= Obj
.GetProtocols()
196 assert Obj
.GetSectionName() == 'Protocols'.upper()
197 assert len(Items
) == 1
198 assert Items
[0].GuidCName
== 'gEfiBdsArchProtocolGuid'
199 assert Items
[0].GuidCValue
== '{ 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}'
201 Obj
= Parser
.GetPpiSectionObject()
202 Items
= Obj
.GetPpis()
203 assert Obj
.GetSectionName() == 'Ppis'.upper()
204 assert len(Items
) == 1
205 assert Items
[0].GuidCName
== 'gEfiPeiMasterBootModePpiGuid'
206 assert Items
[0].GuidCValue
== '{ 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }'
211 DEC_SPECIFICATION = 0x00010005
212 PACKAGE_NAME = MdePkg
213 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
214 PACKAGE_VERSION = 1.02
216 ## If TRUE, the component name protocol will not be installed.
217 gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d
220 ## Indicates the maximum length of unicode string
221 gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength|1000000|UINT32|0x00000001
223 [PcdsFixedAtBuild.IPF]
224 ## The base address of IO port space for IA64 arch
225 gEfiMdePkgTokenSpaceGuid.PcdIoBlockBaseAddressForIpf|0x0ffffc000000|UINT64|0x0000000f
227 [PcdsFixedAtBuild,PcdsPatchableInModule]
228 ## This flag is used to control the printout of DebugLib
229 gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000000|UINT32|0x00000006
231 [PcdsFixedAtBuild,PcdsPatchableInModule,PcdsDynamic]
232 ## This value is used to set the base address of pci express hierarchy
233 gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress|0xE0000000|UINT64|0x0000000a
235 Parser
= TestTemplate(TestString
, TestOK
)
236 Obj
= Parser
.GetPcdSectionObject()
237 Items
= Obj
.GetPcds('PcdsFeatureFlag', 'COMMON')
238 assert len(Items
) == 1
239 assert Items
[0].TokenSpaceGuidCName
== 'gEfiMdePkgTokenSpaceGuid'
240 assert Items
[0].TokenCName
== 'PcdComponentNameDisable'
241 assert Items
[0].DefaultValue
== 'FALSE'
242 assert Items
[0].DatumType
== 'BOOLEAN'
243 assert Items
[0].TokenValue
== '0x0000000d'
245 Items
= Obj
.GetPcdsByType('PcdsFixedAtBuild')
246 assert len(Items
) == 4
247 assert len(Obj
.GetPcdsByType('PcdsPatchableInModule')) == 2
249 def TestDecUserExtension():
252 DEC_SPECIFICATION = 0x00010005
253 PACKAGE_NAME = MdePkg
254 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
255 PACKAGE_VERSION = 1.02
256 [UserExtensions.MyID."TestString".IA32]
259 Parser
= TestTemplate(TestString
, TestOK
)
260 Obj
= Parser
.GetUserExtensionSectionObject()
261 Items
= Obj
.GetAllUserExtensions()
262 assert len(Items
) == 1
263 assert Items
[0].UserString
== 'Some Strings...'
264 assert len(Items
[0].ArchAndModuleType
) == 1
265 assert ['MyID', '"TestString"', 'IA32'] in Items
[0].ArchAndModuleType
267 if __name__
== '__main__':
269 Logger
.Logger
.Initialize()
270 unittest
.FunctionTestCase(TestToolFuncs
).runTest()
271 unittest
.FunctionTestCase(TestDecDefine
).runTest()
272 unittest
.FunctionTestCase(TestDecInclude
).runTest()
273 unittest
.FunctionTestCase(TestDecGuidPpiProtocol
).runTest()
274 unittest
.FunctionTestCase(TestDecPcd
).runTest()
275 unittest
.FunctionTestCase(TestDecUserExtension
).runTest()
277 print('All tests passed...')