]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/UnitTest/DecParserTest.py
BaseTools/UPT: Porting UPT Tool from Python2 to Python3
[mirror_edk2.git] / BaseTools / Source / Python / UPT / UnitTest / DecParserTest.py
CommitLineData
4234283c
LG
1## @file\r
2# This file contain unit test for DecParser\r
3#\r
f7496d71 4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 5#\r
f7496d71
LG
6# This program and the accompanying materials are licensed and made available\r
7# under the terms and conditions of the BSD License which accompanies this\r
8# distribution. The full text of the license may be found at\r
4234283c
LG
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14import os\r
15import unittest\r
16\r
17from Parser.DecParserMisc import \\r
18 IsValidCArray, \\r
19 IsValidPcdDatum\r
20\r
21from Parser.DecParser import Dec\r
22\r
23from Library.ParserValidate import IsValidCFormatGuid\r
24\r
25#\r
26# Test tool function\r
27#\r
28def TestToolFuncs():\r
29 assert IsValidCArray('{0x1, 0x23}')\r
f7496d71 30\r
4234283c
LG
31 # Empty after comma\r
32 assert not IsValidCArray('{0x1, 0x23, }')\r
f7496d71 33\r
4234283c
LG
34 # 0x2345 too long\r
35 assert not IsValidCArray('{0x1, 0x2345}')\r
f7496d71 36\r
4234283c
LG
37 # Must end with '}'\r
38 assert not IsValidCArray('{0x1, 0x23, ')\r
f7496d71 39\r
4234283c
LG
40 # Whitespace between numbers\r
41 assert not IsValidCArray('{0x1, 0x2 3, }')\r
f7496d71 42\r
4234283c
LG
43 assert IsValidPcdDatum('VOID*', '"test"')[0]\r
44 assert IsValidPcdDatum('VOID*', 'L"test"')[0]\r
45 assert IsValidPcdDatum('BOOLEAN', 'TRUE')[0]\r
46 assert IsValidPcdDatum('BOOLEAN', 'FALSE')[0]\r
47 assert IsValidPcdDatum('BOOLEAN', '0')[0]\r
48 assert IsValidPcdDatum('BOOLEAN', '1')[0]\r
49 assert IsValidPcdDatum('UINT8', '0xab')[0]\r
f7496d71 50\r
4234283c 51 assert not IsValidPcdDatum('UNKNOWNTYPE', '0xabc')[0]\r
f7496d71
LG
52 assert not IsValidPcdDatum('UINT8', 'not number')[0]\r
53\r
4234283c
LG
54 assert( IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }}'))\r
55 assert( not IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }} 0xaa'))\r
56\r
57def TestTemplate(TestString, TestFunc):\r
58 Path = os.path.join(os.getcwd(), 'test.dec')\r
59 Path = os.path.normpath(Path)\r
60 try:\r
61 f = open(Path, 'w')\r
f7496d71 62\r
4234283c
LG
63 # Write test string to file\r
64 f.write(TestString)\r
f7496d71 65\r
4234283c
LG
66 # Close file\r
67 f.close()\r
68 except:\r
72443dd2 69 print('Can not create temporary file [%s]!' % Path)\r
4234283c
LG
70 exit(-1)\r
71\r
72 # Call test function to test\r
73 Ret = TestFunc(Path, TestString)\r
f7496d71 74\r
4234283c
LG
75 # Test done, remove temporary file\r
76 os.remove(Path)\r
77 return Ret\r
78\r
79# To make test unit works OK, must set IsRaiseError to True\r
80# This function test right syntax DEC file\r
81# @retval: parser object\r
82#\r
83def TestOK(Path, TestString):\r
84 try:\r
85 Parser = Dec(Path)\r
86 except:\r
87 raise 'Bug!!! Correct syntax in DEC file, but exception raised!\n' + TestString\r
88 return Parser\r
89\r
90# This function test wrong syntax DEC file\r
91# if parser checked wrong syntax, exception thrown and it's expected result\r
92def TestError(Path, TestString):\r
93 try:\r
94 Dec(Path)\r
95 except:\r
96 # Raise error, get expected result\r
97 return True\r
98 raise 'Bug!!! Wrong syntax in DEC file, but passed by DEC parser!!\n' + TestString\r
99\r
100def TestDecDefine():\r
101 TestString = '''\r
102 [Defines]\r
103 DEC_SPECIFICATION = 0x00010005\r
104 PACKAGE_NAME = MdePkg\r
105 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766\r
106 PACKAGE_VERSION = 1.02\r
107 '''\r
108 Parser = TestTemplate(TestString, TestOK)\r
109 DefObj = Parser.GetDefineSectionObject()\r
110 assert DefObj.GetPackageSpecification() == '0x00010005'\r
111 assert DefObj.GetPackageName() == 'MdePkg'\r
112 assert DefObj.GetPackageGuid() == '1E73767F-8F52-4603-AEB4-F29B510B6766'\r
113 assert DefObj.GetPackageVersion() == '1.02'\r
f7496d71 114\r
4234283c
LG
115 TestString = '''\r
116 [Defines]\r
117 UNKNOW_KEY = 0x00010005 # A unknown key\r
118 '''\r
119 assert TestTemplate(TestString, TestError)\r
f7496d71 120\r
4234283c
LG
121 TestString = '''\r
122 [Defines]\r
123 PACKAGE_GUID = F-8F52-4603-AEB4-F29B510B6766 # Error GUID\r
124 '''\r
125 assert TestTemplate(TestString, TestError)\r
126\r
127def TestDecInclude():\r
128 TestString = '''\r
129 [Defines]\r
130 DEC_SPECIFICATION = 0x00010005\r
131 PACKAGE_NAME = MdePkg\r
132 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766\r
133 PACKAGE_VERSION = 1.02\r
134 [ \\\r
135 Includes]\r
136 Include\r
137 [Includes.IA32]\r
138 Include/Ia32\r
139 '''\r
f7496d71 140\r
4234283c
LG
141 # Create directory in current directory\r
142 try:\r
143 os.makedirs('Include/Ia32')\r
144 except:\r
145 pass\r
146 Parser = TestTemplate(TestString, TestOK)\r
f7496d71 147\r
4234283c
LG
148 IncObj = Parser.GetIncludeSectionObject()\r
149 Items = IncObj.GetIncludes()\r
150 assert len(Items) == 1\r
151 assert Items[0].File == 'Include'\r
f7496d71 152\r
4234283c
LG
153 Items = IncObj.GetIncludes('IA32')\r
154 assert len(Items) == 1\r
155 # normpath is called in DEC parser so '/' is converted to '\'\r
156 assert Items[0].File == 'Include\\Ia32'\r
f7496d71 157\r
4234283c
LG
158 TestString = '''\r
159 [Defines]\r
160 DEC_SPECIFICATION = 0x00010005\r
161 PACKAGE_NAME = MdePkg\r
162 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766\r
163 PACKAGE_VERSION = 1.02\r
164 [Includes]\r
165 Include_not_exist # directory does not exist\r
166 '''\r
167 assert TestTemplate(TestString, TestError)\r
f7496d71 168\r
4234283c
LG
169 os.removedirs('Include/Ia32')\r
170\r
171def TestDecGuidPpiProtocol():\r
172 TestString = '''\r
173 [Defines]\r
174 DEC_SPECIFICATION = 0x00010005\r
175 PACKAGE_NAME = MdePkg\r
176 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766\r
177 PACKAGE_VERSION = 1.02\r
178 [Guids]\r
179 #\r
180 # GUID defined in UEFI2.1/UEFI2.0/EFI1.1\r
181 #\r
182 ## Include/Guid/GlobalVariable.h\r
183 gEfiGlobalVariableGuid = { 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}\r
184 [Protocols]\r
185 ## Include/Protocol/Bds.h\r
186 gEfiBdsArchProtocolGuid = { 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}\r
187 [Ppis]\r
188 ## Include/Ppi/MasterBootMode.h\r
189 gEfiPeiMasterBootModePpiGuid = { 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }\r
190 '''\r
191 Parser = TestTemplate(TestString, TestOK)\r
192 Obj = Parser.GetGuidSectionObject()\r
193 Items = Obj.GetGuids()\r
194 assert Obj.GetSectionName() == 'Guids'.upper()\r
195 assert len(Items) == 1\r
196 assert Items[0].GuidCName == 'gEfiGlobalVariableGuid'\r
197 assert Items[0].GuidCValue == '{ 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}'\r
f7496d71 198\r
4234283c
LG
199 Obj = Parser.GetProtocolSectionObject()\r
200 Items = Obj.GetProtocols()\r
201 assert Obj.GetSectionName() == 'Protocols'.upper()\r
202 assert len(Items) == 1\r
203 assert Items[0].GuidCName == 'gEfiBdsArchProtocolGuid'\r
204 assert Items[0].GuidCValue == '{ 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}'\r
f7496d71 205\r
4234283c
LG
206 Obj = Parser.GetPpiSectionObject()\r
207 Items = Obj.GetPpis()\r
208 assert Obj.GetSectionName() == 'Ppis'.upper()\r
209 assert len(Items) == 1\r
210 assert Items[0].GuidCName == 'gEfiPeiMasterBootModePpiGuid'\r
211 assert Items[0].GuidCValue == '{ 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }'\r
212\r
213def TestDecPcd():\r
214 TestString = '''\r
215 [Defines]\r
216 DEC_SPECIFICATION = 0x00010005\r
217 PACKAGE_NAME = MdePkg\r
218 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766\r
219 PACKAGE_VERSION = 1.02\r
220 [PcdsFeatureFlag]\r
221 ## If TRUE, the component name protocol will not be installed.\r
222 gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d\r
f7496d71 223\r
4234283c
LG
224 [PcdsFixedAtBuild]\r
225 ## Indicates the maximum length of unicode string\r
226 gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength|1000000|UINT32|0x00000001\r
f7496d71 227\r
4234283c
LG
228 [PcdsFixedAtBuild.IPF]\r
229 ## The base address of IO port space for IA64 arch\r
230 gEfiMdePkgTokenSpaceGuid.PcdIoBlockBaseAddressForIpf|0x0ffffc000000|UINT64|0x0000000f\r
f7496d71 231\r
4234283c
LG
232 [PcdsFixedAtBuild,PcdsPatchableInModule]\r
233 ## This flag is used to control the printout of DebugLib\r
234 gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000000|UINT32|0x00000006\r
f7496d71 235\r
4234283c
LG
236 [PcdsFixedAtBuild,PcdsPatchableInModule,PcdsDynamic]\r
237 ## This value is used to set the base address of pci express hierarchy\r
238 gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress|0xE0000000|UINT64|0x0000000a\r
239 '''\r
240 Parser = TestTemplate(TestString, TestOK)\r
241 Obj = Parser.GetPcdSectionObject()\r
242 Items = Obj.GetPcds('PcdsFeatureFlag', 'COMMON')\r
243 assert len(Items) == 1\r
244 assert Items[0].TokenSpaceGuidCName == 'gEfiMdePkgTokenSpaceGuid'\r
245 assert Items[0].TokenCName == 'PcdComponentNameDisable'\r
246 assert Items[0].DefaultValue == 'FALSE'\r
247 assert Items[0].DatumType == 'BOOLEAN'\r
248 assert Items[0].TokenValue == '0x0000000d'\r
f7496d71 249\r
4234283c
LG
250 Items = Obj.GetPcdsByType('PcdsFixedAtBuild')\r
251 assert len(Items) == 4\r
252 assert len(Obj.GetPcdsByType('PcdsPatchableInModule')) == 2\r
253\r
254def TestDecUserExtension():\r
255 TestString = '''\r
256 [Defines]\r
257 DEC_SPECIFICATION = 0x00010005\r
258 PACKAGE_NAME = MdePkg\r
259 PACKAGE_GUID = 1E73767F-8F52-4603-AEB4-F29B510B6766\r
260 PACKAGE_VERSION = 1.02\r
261 [UserExtensions.MyID."TestString".IA32]\r
262 Some Strings...\r
263 '''\r
264 Parser = TestTemplate(TestString, TestOK)\r
265 Obj = Parser.GetUserExtensionSectionObject()\r
266 Items = Obj.GetAllUserExtensions()\r
267 assert len(Items) == 1\r
268 assert Items[0].UserString == 'Some Strings...'\r
269 assert len(Items[0].ArchAndModuleType) == 1\r
270 assert ['MyID', '"TestString"', 'IA32'] in Items[0].ArchAndModuleType\r
271\r
272if __name__ == '__main__':\r
273 import Logger.Logger\r
274 Logger.Logger.Initialize()\r
275 unittest.FunctionTestCase(TestToolFuncs).runTest()\r
276 unittest.FunctionTestCase(TestDecDefine).runTest()\r
277 unittest.FunctionTestCase(TestDecInclude).runTest()\r
278 unittest.FunctionTestCase(TestDecGuidPpiProtocol).runTest()\r
279 unittest.FunctionTestCase(TestDecPcd).runTest()\r
280 unittest.FunctionTestCase(TestDecUserExtension).runTest()\r
281\r
72443dd2 282 print('All tests passed...')\r
4234283c
LG
283\r
284\r