]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/UnitTest/DecParserTest.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / UPT / UnitTest / DecParserTest.py
1 ## @file
2 # This file contain unit test for DecParser
3 #
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 #
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 from __future__ import print_function
9 import os
10 import unittest
11
12 from Parser.DecParserMisc import \
13 IsValidCArray, \
14 IsValidPcdDatum
15
16 from Parser.DecParser import Dec
17
18 from Library.ParserValidate import IsValidCFormatGuid
19
20 #
21 # Test tool function
22 #
23 def TestToolFuncs():
24 assert IsValidCArray('{0x1, 0x23}')
25
26 # Empty after comma
27 assert not IsValidCArray('{0x1, 0x23, }')
28
29 # 0x2345 too long
30 assert not IsValidCArray('{0x1, 0x2345}')
31
32 # Must end with '}'
33 assert not IsValidCArray('{0x1, 0x23, ')
34
35 # Whitespace between numbers
36 assert not IsValidCArray('{0x1, 0x2 3, }')
37
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]
45
46 assert not IsValidPcdDatum('UNKNOWNTYPE', '0xabc')[0]
47 assert not IsValidPcdDatum('UINT8', 'not number')[0]
48
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'))
51
52 def TestTemplate(TestString, TestFunc):
53 Path = os.path.join(os.getcwd(), 'test.dec')
54 Path = os.path.normpath(Path)
55 try:
56 f = open(Path, 'w')
57
58 # Write test string to file
59 f.write(TestString)
60
61 # Close file
62 f.close()
63 except:
64 print('Can not create temporary file [%s]!' % Path)
65 exit(-1)
66
67 # Call test function to test
68 Ret = TestFunc(Path, TestString)
69
70 # Test done, remove temporary file
71 os.remove(Path)
72 return Ret
73
74 # To make test unit works OK, must set IsRaiseError to True
75 # This function test right syntax DEC file
76 # @retval: parser object
77 #
78 def TestOK(Path, TestString):
79 try:
80 Parser = Dec(Path)
81 except:
82 raise 'Bug!!! Correct syntax in DEC file, but exception raised!\n' + TestString
83 return Parser
84
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):
88 try:
89 Dec(Path)
90 except:
91 # Raise error, get expected result
92 return True
93 raise 'Bug!!! Wrong syntax in DEC file, but passed by DEC parser!!\n' + TestString
94
95 def TestDecDefine():
96 TestString = '''
97 [Defines]
98 DEC_SPECIFICATION = 0x00010005
99 PACKAGE_NAME = MdePkg
100 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
101 PACKAGE_VERSION = 1.02
102 '''
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'
109
110 TestString = '''
111 [Defines]
112 UNKNOW_KEY = 0x00010005 # A unknown key
113 '''
114 assert TestTemplate(TestString, TestError)
115
116 TestString = '''
117 [Defines]
118 PACKAGE_GUID = F-8F52-4603-AEB4-F29B510B6766 # Error GUID
119 '''
120 assert TestTemplate(TestString, TestError)
121
122 def TestDecInclude():
123 TestString = '''
124 [Defines]
125 DEC_SPECIFICATION = 0x00010005
126 PACKAGE_NAME = MdePkg
127 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
128 PACKAGE_VERSION = 1.02
129 [ \\
130 Includes]
131 Include
132 [Includes.IA32]
133 Include/Ia32
134 '''
135
136 # Create directory in current directory
137 try:
138 os.makedirs('Include/Ia32')
139 except:
140 pass
141 Parser = TestTemplate(TestString, TestOK)
142
143 IncObj = Parser.GetIncludeSectionObject()
144 Items = IncObj.GetIncludes()
145 assert len(Items) == 1
146 assert Items[0].File == 'Include'
147
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'
152
153 TestString = '''
154 [Defines]
155 DEC_SPECIFICATION = 0x00010005
156 PACKAGE_NAME = MdePkg
157 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
158 PACKAGE_VERSION = 1.02
159 [Includes]
160 Include_not_exist # directory does not exist
161 '''
162 assert TestTemplate(TestString, TestError)
163
164 os.removedirs('Include/Ia32')
165
166 def TestDecGuidPpiProtocol():
167 TestString = '''
168 [Defines]
169 DEC_SPECIFICATION = 0x00010005
170 PACKAGE_NAME = MdePkg
171 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
172 PACKAGE_VERSION = 1.02
173 [Guids]
174 #
175 # GUID defined in UEFI2.1/UEFI2.0/EFI1.1
176 #
177 ## Include/Guid/GlobalVariable.h
178 gEfiGlobalVariableGuid = { 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}
179 [Protocols]
180 ## Include/Protocol/Bds.h
181 gEfiBdsArchProtocolGuid = { 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}
182 [Ppis]
183 ## Include/Ppi/MasterBootMode.h
184 gEfiPeiMasterBootModePpiGuid = { 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }
185 '''
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 }}'
193
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 }}'
200
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 } }'
207
208 def TestDecPcd():
209 TestString = '''
210 [Defines]
211 DEC_SPECIFICATION = 0x00010005
212 PACKAGE_NAME = MdePkg
213 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766
214 PACKAGE_VERSION = 1.02
215 [PcdsFeatureFlag]
216 ## If TRUE, the component name protocol will not be installed.
217 gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d
218
219 [PcdsFixedAtBuild]
220 ## Indicates the maximum length of unicode string
221 gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength|1000000|UINT32|0x00000001
222
223 [PcdsFixedAtBuild.IPF]
224 ## The base address of IO port space for IA64 arch
225 gEfiMdePkgTokenSpaceGuid.PcdIoBlockBaseAddressForIpf|0x0ffffc000000|UINT64|0x0000000f
226
227 [PcdsFixedAtBuild,PcdsPatchableInModule]
228 ## This flag is used to control the printout of DebugLib
229 gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000000|UINT32|0x00000006
230
231 [PcdsFixedAtBuild,PcdsPatchableInModule,PcdsDynamic]
232 ## This value is used to set the base address of pci express hierarchy
233 gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress|0xE0000000|UINT64|0x0000000a
234 '''
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'
244
245 Items = Obj.GetPcdsByType('PcdsFixedAtBuild')
246 assert len(Items) == 4
247 assert len(Obj.GetPcdsByType('PcdsPatchableInModule')) == 2
248
249 def TestDecUserExtension():
250 TestString = '''
251 [Defines]
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]
257 Some Strings...
258 '''
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
266
267 if __name__ == '__main__':
268 import Logger.Logger
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()
276
277 print('All tests passed...')
278
279