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