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