summaryrefslogtreecommitdiff
path: root/BaseTools/Source/Python/UPT/UnitTest/DecParserTest.py
blob: 8b4ece2617a1a7dbbdcc5a1eee0bd9d31f3c0da3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
## @file
# This file contain unit test for DecParser
#
# Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
#
# This program and the accompanying materials are licensed and made available 
# under the terms and conditions of the BSD License which accompanies this 
# distribution. The full text of the license may be found at 
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

import os
import unittest

from Parser.DecParserMisc import \
    IsValidCArray, \
    IsValidPcdDatum

from Parser.DecParser import Dec

from Library.ParserValidate import IsValidCFormatGuid

#
# Test tool function
#
def TestToolFuncs():
    assert IsValidCArray('{0x1, 0x23}')
    
    # Empty after comma
    assert not IsValidCArray('{0x1, 0x23, }')
    
    # 0x2345 too long
    assert not IsValidCArray('{0x1, 0x2345}')
    
    # Must end with '}'
    assert not IsValidCArray('{0x1, 0x23, ')
    
    # Whitespace between numbers
    assert not IsValidCArray('{0x1, 0x2 3, }')
    
    assert IsValidPcdDatum('VOID*', '"test"')[0]
    assert IsValidPcdDatum('VOID*', 'L"test"')[0]
    assert IsValidPcdDatum('BOOLEAN', 'TRUE')[0]
    assert IsValidPcdDatum('BOOLEAN', 'FALSE')[0]
    assert IsValidPcdDatum('BOOLEAN', '0')[0]
    assert IsValidPcdDatum('BOOLEAN', '1')[0]
    assert IsValidPcdDatum('UINT8', '0xab')[0]
    
    assert not IsValidPcdDatum('UNKNOWNTYPE', '0xabc')[0]
    assert not IsValidPcdDatum('UINT8', 'not number')[0]   
    
    assert( IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }}'))
    assert( not IsValidCFormatGuid('{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }} 0xaa'))

def TestTemplate(TestString, TestFunc):
    Path = os.path.join(os.getcwd(), 'test.dec')
    Path = os.path.normpath(Path)
    try:
        f = open(Path, 'w')
        
        # Write test string to file
        f.write(TestString)
        
        # Close file
        f.close()
    except:
        print 'Can not create temporary file [%s]!' % Path
        exit(-1)

    # Call test function to test
    Ret = TestFunc(Path, TestString)
    
    # Test done, remove temporary file
    os.remove(Path)
    return Ret

# To make test unit works OK, must set IsRaiseError to True
# This function test right syntax DEC file
# @retval: parser object
#
def TestOK(Path, TestString):
    try:
        Parser = Dec(Path)
    except:
        raise 'Bug!!! Correct syntax in DEC file, but exception raised!\n' + TestString
    return Parser

# This function test wrong syntax DEC file
# if parser checked wrong syntax, exception thrown and it's expected result
def TestError(Path, TestString):
    try:
        Dec(Path)
    except:
        # Raise error, get expected result
        return True
    raise 'Bug!!! Wrong syntax in DEC file, but passed by DEC parser!!\n' + TestString

def TestDecDefine():
    TestString = '''
    [Defines]
      DEC_SPECIFICATION              = 0x00010005
      PACKAGE_NAME                   = MdePkg
      PACKAGE_GUID                   = 1E73767F-8F52-4603-AEB4-F29B510B6766
      PACKAGE_VERSION                = 1.02
    '''
    Parser = TestTemplate(TestString, TestOK)
    DefObj = Parser.GetDefineSectionObject()
    assert DefObj.GetPackageSpecification() == '0x00010005'
    assert DefObj.GetPackageName() == 'MdePkg'
    assert DefObj.GetPackageGuid() == '1E73767F-8F52-4603-AEB4-F29B510B6766'
    assert DefObj.GetPackageVersion() == '1.02'
    
    TestString = '''
    [Defines]
      UNKNOW_KEY              = 0x00010005 # A unknown key
    '''
    assert TestTemplate(TestString, TestError)
    
    TestString = '''
    [Defines]
      PACKAGE_GUID                   = F-8F52-4603-AEB4-F29B510B6766 # Error GUID
    '''
    assert TestTemplate(TestString, TestError)

def TestDecInclude():
    TestString = '''
    [Defines]
      DEC_SPECIFICATION              = 0x00010005
      PACKAGE_NAME                   = MdePkg
      PACKAGE_GUID                   = 1E73767F-8F52-4603-AEB4-F29B510B6766
      PACKAGE_VERSION                = 1.02
    [ \\
    Includes]
      Include
    [Includes.IA32]
      Include/Ia32
    '''
    
    # Create directory in current directory
    try:
        os.makedirs('Include/Ia32')
    except:
        pass
    Parser = TestTemplate(TestString, TestOK)
    
    IncObj = Parser.GetIncludeSectionObject()
    Items = IncObj.GetIncludes()
    assert len(Items) == 1
    assert Items[0].File == 'Include'
    
    Items = IncObj.GetIncludes('IA32')
    assert len(Items) == 1
    # normpath is called in DEC parser so '/' is converted to '\'
    assert Items[0].File == 'Include\\Ia32'
    
    TestString = '''
    [Defines]
      DEC_SPECIFICATION              = 0x00010005
      PACKAGE_NAME                   = MdePkg
      PACKAGE_GUID                   = 1E73767F-8F52-4603-AEB4-F29B510B6766
      PACKAGE_VERSION                = 1.02
    [Includes]
      Include_not_exist  # directory does not exist
    '''
    assert TestTemplate(TestString, TestError)
    
    os.removedirs('Include/Ia32')

def TestDecGuidPpiProtocol():
    TestString = '''
    [Defines]
      DEC_SPECIFICATION              = 0x00010005
      PACKAGE_NAME                   = MdePkg
      PACKAGE_GUID                   = 1E73767F-8F52-4603-AEB4-F29B510B6766
      PACKAGE_VERSION                = 1.02
    [Guids]
      #
      # GUID defined in UEFI2.1/UEFI2.0/EFI1.1
      #
      ## Include/Guid/GlobalVariable.h
      gEfiGlobalVariableGuid         = { 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}
    [Protocols]
      ## Include/Protocol/Bds.h
      gEfiBdsArchProtocolGuid        = { 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}
    [Ppis]
      ## Include/Ppi/MasterBootMode.h
      gEfiPeiMasterBootModePpiGuid = { 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }
    '''
    Parser = TestTemplate(TestString, TestOK)
    Obj = Parser.GetGuidSectionObject()
    Items = Obj.GetGuids()
    assert Obj.GetSectionName() == 'Guids'.upper()
    assert len(Items) == 1
    assert Items[0].GuidCName == 'gEfiGlobalVariableGuid'
    assert Items[0].GuidCValue == '{ 0x8BE4DF61, 0x93CA, 0x11D2, { 0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C }}'
    
    Obj = Parser.GetProtocolSectionObject()
    Items = Obj.GetProtocols()
    assert Obj.GetSectionName() == 'Protocols'.upper()
    assert len(Items) == 1
    assert Items[0].GuidCName == 'gEfiBdsArchProtocolGuid'
    assert Items[0].GuidCValue == '{ 0x665E3FF6, 0x46CC, 0x11D4, { 0x9A, 0x38, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }}'
    
    Obj = Parser.GetPpiSectionObject()
    Items = Obj.GetPpis()
    assert Obj.GetSectionName() == 'Ppis'.upper()
    assert len(Items) == 1
    assert Items[0].GuidCName == 'gEfiPeiMasterBootModePpiGuid'
    assert Items[0].GuidCValue == '{ 0x7408d748, 0xfc8c, 0x4ee6, {0x92, 0x88, 0xc4, 0xbe, 0xc0, 0x92, 0xa4, 0x10 } }'

def TestDecPcd():
    TestString = '''
    [Defines]
      DEC_SPECIFICATION              = 0x00010005
      PACKAGE_NAME                   = MdePkg
      PACKAGE_GUID                   = 1E73767F-8F52-4603-AEB4-F29B510B6766
      PACKAGE_VERSION                = 1.02
    [PcdsFeatureFlag]
      ## If TRUE, the component name protocol will not be installed.
      gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable|FALSE|BOOLEAN|0x0000000d
      
    [PcdsFixedAtBuild]
      ## Indicates the maximum length of unicode string
      gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength|1000000|UINT32|0x00000001
      
    [PcdsFixedAtBuild.IPF]
      ## The base address of IO port space for IA64 arch
      gEfiMdePkgTokenSpaceGuid.PcdIoBlockBaseAddressForIpf|0x0ffffc000000|UINT64|0x0000000f
      
    [PcdsFixedAtBuild,PcdsPatchableInModule]
      ## This flag is used to control the printout of DebugLib
      gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000000|UINT32|0x00000006
      
    [PcdsFixedAtBuild,PcdsPatchableInModule,PcdsDynamic]
      ## This value is used to set the base address of pci express hierarchy
      gEfiMdePkgTokenSpaceGuid.PcdPciExpressBaseAddress|0xE0000000|UINT64|0x0000000a
    '''
    Parser = TestTemplate(TestString, TestOK)
    Obj = Parser.GetPcdSectionObject()
    Items = Obj.GetPcds('PcdsFeatureFlag', 'COMMON')
    assert len(Items) == 1
    assert Items[0].TokenSpaceGuidCName == 'gEfiMdePkgTokenSpaceGuid'
    assert Items[0].TokenCName == 'PcdComponentNameDisable'
    assert Items[0].DefaultValue == 'FALSE'
    assert Items[0].DatumType == 'BOOLEAN'
    assert Items[0].TokenValue == '0x0000000d'
    
    Items = Obj.GetPcdsByType('PcdsFixedAtBuild')
    assert len(Items) == 4
    assert len(Obj.GetPcdsByType('PcdsPatchableInModule')) == 2

def TestDecUserExtension():
    TestString = '''
    [Defines]
      DEC_SPECIFICATION              = 0x00010005
      PACKAGE_NAME                   = MdePkg
      PACKAGE_GUID                   = 1E73767F-8F52-4603-AEB4-F29B510B6766
      PACKAGE_VERSION                = 1.02
    [UserExtensions.MyID."TestString".IA32]
      Some Strings...
    '''
    Parser = TestTemplate(TestString, TestOK)
    Obj = Parser.GetUserExtensionSectionObject()
    Items = Obj.GetAllUserExtensions()
    assert len(Items) == 1
    assert Items[0].UserString == 'Some Strings...'
    assert len(Items[0].ArchAndModuleType) == 1
    assert ['MyID', '"TestString"', 'IA32'] in Items[0].ArchAndModuleType

if __name__ == '__main__':
    import Logger.Logger
    Logger.Logger.Initialize()
    unittest.FunctionTestCase(TestToolFuncs).runTest()
    unittest.FunctionTestCase(TestDecDefine).runTest()
    unittest.FunctionTestCase(TestDecInclude).runTest()
    unittest.FunctionTestCase(TestDecGuidPpiProtocol).runTest()
    unittest.FunctionTestCase(TestDecPcd).runTest()
    unittest.FunctionTestCase(TestDecUserExtension).runTest()

    print 'All tests passed...'