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