1## @file
2# This file contain unit test for Test [Binary] section part of InfParser
3#
4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5#
6# SPDX-License-Identifier: BSD-2-Clause-Patent
7
8from __future__ import print_function
9import os
10#import Object.Parser.InfObject as InfObject
11from Object.Parser.InfCommonObject import CurrentLine
12from Object.Parser.InfCommonObject import InfLineCommentObject
13from Object.Parser.InfBinaryObject import InfBinariesObject
14import Logger.Log as Logger
15import Library.GlobalData as Global
16##
17# Test Common binary item
18#
19
20#-------------start of common binary item test input--------------------------#
21
22#
23# Only has 1 element, binary item Type
24#
25SectionStringsCommonItem1 = \
26"""
27GUID
28"""
29#
30# Have 2 elements, binary item Type and FileName
31#
32SectionStringsCommonItem2 = \
33"""
34GUID | Test/Test.guid
35"""
36
37#
38# Have 3 elements, Type | FileName | Target | Family | TagName | FeatureFlagExp
39#
40SectionStringsCommonItem3 = \
41"""
42GUID | Test/Test.guid | DEBUG
43"""
44
45#
46# Have 3 elements, Type | FileName | Target
47# Target with MACRO defined in [Define] section
48#
49SectionStringsCommonItem4 = \
50"""
51GUID | Test/Test.guid | $(TARGET)
52"""
53
54#
55# Have 3 elements, Type | FileName | Target
56# FileName with MACRO defined in [Binary] section
57#
58SectionStringsCommonItem5 = \
59"""
60DEFINE BINARY_FILE_PATH = Test
61GUID | $(BINARY_FILE_PATH)/Test.guid | $(TARGET)
62"""
63
64#
65# Have 4 elements, Type | FileName | Target | Family
66#
67SectionStringsCommonItem6 = \
68"""
69GUID | Test/Test.guid | DEBUG | *
70"""
71
72#
73# Have 4 elements, Type | FileName | Target | Family
74#
75SectionStringsCommonItem7 = \
76"""
77GUID | Test/Test.guid | DEBUG | MSFT
78"""
79
80#
81# Have 5 elements, Type | FileName | Target | Family | TagName
82#
83SectionStringsCommonItem8 = \
84"""
85GUID | Test/Test.guid | DEBUG | MSFT | TEST
86"""
87
88#
89# Have 6 elements, Type | FileName | Target | Family | TagName | FFE
90#
91SectionStringsCommonItem9 = \
92"""
93GUID | Test/Test.guid | DEBUG | MSFT | TEST | TRUE
94"""
95
96#
97# Have 7 elements, Type | FileName | Target | Family | TagName | FFE | Overflow
98# Test wrong format
99#
100SectionStringsCommonItem10 = \
101"""
102GUID | Test/Test.guid | DEBUG | MSFT | TEST | TRUE | OVERFLOW
103"""
104
105#-------------end of common binary item test input----------------------------#
106
107
108
109#-------------start of VER type binary item test input------------------------#
110
111#
112# Has 1 element, error format
113#
114SectionStringsVerItem1 = \
115"""
116VER
117"""
118#
119# Have 5 elements, error format(Maximum elements amount is 4)
120#
121SectionStringsVerItem2 = \
122"""
123VER | Test/Test.ver | * | TRUE | OverFlow
124"""
125
126#
127# Have 2 elements, Type | FileName
128#
129SectionStringsVerItem3 = \
130"""
131VER | Test/Test.ver
132"""
133
134#
135# Have 3 elements, Type | FileName | Target
136#
137SectionStringsVerItem4 = \
138"""
139VER | Test/Test.ver | DEBUG
140"""
141
142#
143# Have 4 elements, Type | FileName | Target | FeatureFlagExp
144#
145SectionStringsVerItem5 = \
146"""
147VER | Test/Test.ver | DEBUG | TRUE
148"""
149
150#
151# Exist 2 VER items, both opened.
152#
153SectionStringsVerItem6 = \
154"""
155VER | Test/Test.ver | * | TRUE
156VER | Test/Test2.ver | * | TRUE
157"""
158
159
160#
161# Exist 2 VER items, only 1 opened.
162#
163SectionStringsVerItem7 = \
164"""
165VER | Test/Test.ver | * | TRUE
166VER | Test/Test2.ver | * | FALSE
167"""
168
169#-------------end of VER type binary item test input--------------------------#
170
171
172#-------------start of UI type binary item test input-------------------------#
173
174#
175# Test only one UI section can exist
176#
177SectionStringsUiItem1 = \
178"""
179UI | Test/Test.ui | * | TRUE
180UI | Test/Test2.ui | * | TRUE
181"""
182
183SectionStringsUiItem2 = \
184"""
185UI | Test/Test.ui | * | TRUE
186SEC_UI | Test/Test2.ui | * | TRUE
187"""
188
189SectionStringsUiItem3 = \
190"""
191UI | Test/Test.ui | * | TRUE
192UI | Test/Test2.ui | * | FALSE
193"""
194
195#
196# Has 1 element, error format
197#
198SectionStringsUiItem4 = \
199"""
200UI
201"""
202#
203# Have 5 elements, error format(Maximum elements amount is 4)
204#
205SectionStringsUiItem5 = \
206"""
207UI | Test/Test.ui | * | TRUE | OverFlow
208"""
209
210#
211# Have 2 elements, Type | FileName
212#
213SectionStringsUiItem6 = \
214"""
215UI | Test/Test.ui
216"""
217
218#
219# Have 3 elements, Type | FileName | Target
220#
221SectionStringsUiItem7 = \
222"""
223UI | Test/Test.ui | DEBUG
224"""
225
226#
227# Have 4 elements, Type | FileName | Target | FeatureFlagExp
228#
229SectionStringsUiItem8 = \
230"""
231UI | Test/Test.ui | DEBUG | TRUE
232"""
233#---------------end of UI type binary item test input-------------------------#
234
235
236gFileName = "BinarySectionTest.inf"
237
238##
239# Construct SectionString for call section parser usage.
240#
241def StringToSectionString(String):
242    Lines = String.split('\n')
243    LineNo = 0
244    SectionString = []
245    for Line in Lines:
246        if Line.strip() == '':
247            continue
248        SectionString.append((Line, LineNo, ''))
249        LineNo = LineNo + 1
250
251    return SectionString
252
253def PrepareTest(String):
254    SectionString = StringToSectionString(String)
255    ItemList = []
256    for Item in SectionString:
257        ValueList = Item[0].split('|')
258        for count in range(len(ValueList)):
259            ValueList[count] = ValueList[count].strip()
260        if len(ValueList) >= 2:
261            #
262            # Create a temp file for test.
263            #
264            FileName = os.path.normpath(os.path.realpath(ValueList[1].strip()))
265            try:
266                TempFile  = open (FileName, "w")
267                TempFile.close()
268            except:
269                print("File Create Error")
270        CurrentLine = CurrentLine()
271        CurrentLine.SetFileName("Test")
272        CurrentLine.SetLineString(Item[0])
273        CurrentLine.SetLineNo(Item[1])
274        InfLineCommentObject = InfLineCommentObject()
275
276        ItemList.append((ValueList, InfLineCommentObject, CurrentLine))
277
278    return ItemList
279
280if __name__ == '__main__':
281    Logger.Initialize()
282
283    InfBinariesInstance = InfBinariesObject()
284    ArchList = ['COMMON']
285    Global.gINF_MODULE_DIR = os.getcwd()
286
287    AllPassedFlag = True
288
289    #
290    # For All Ui test
291    #
292    UiStringList = [
293                    SectionStringsUiItem1,
294                    SectionStringsUiItem2,
295                    SectionStringsUiItem3,
296                    SectionStringsUiItem4,
297                    SectionStringsUiItem5,
298                    SectionStringsUiItem6,
299                    SectionStringsUiItem7,
300                    SectionStringsUiItem8
301                    ]
302
303    for Item in UiStringList:
304        Ui = PrepareTest(Item)
305        if Item == SectionStringsUiItem4 or Item == SectionStringsUiItem5:
306            try:
307                InfBinariesInstance.SetBinary(Ui = Ui, ArchList = ArchList)
308            except Logger.FatalError:
309                pass
310        else:
311            try:
312                InfBinariesInstance.SetBinary(Ui = Ui, ArchList = ArchList)
313            except:
314                AllPassedFlag = False
315
316    #
317    # For All Ver Test
318    #
319    VerStringList = [
320                     SectionStringsVerItem1,
321                     SectionStringsVerItem2,
322                     SectionStringsVerItem3,
323                     SectionStringsVerItem4,
324                     SectionStringsVerItem5,
325                     SectionStringsVerItem6,
326                     SectionStringsVerItem7
327                     ]
328    for Item in VerStringList:
329        Ver = PrepareTest(Item)
330        if Item == SectionStringsVerItem1 or \
331           Item == SectionStringsVerItem2:
332
333            try:
334                InfBinariesInstance.SetBinary(Ver = Ver, ArchList = ArchList)
335            except:
336                pass
337
338        else:
339            try:
340                InfBinariesInstance.SetBinary(Ver = Ver, ArchList = ArchList)
341            except:
342                AllPassedFlag = False
343
344    #
345    # For All Common Test
346    #
347    CommonStringList = [
348                     SectionStringsCommonItem1,
349                     SectionStringsCommonItem2,
350                     SectionStringsCommonItem3,
351                     SectionStringsCommonItem4,
352                     SectionStringsCommonItem5,
353                     SectionStringsCommonItem6,
354                     SectionStringsCommonItem7,
355                     SectionStringsCommonItem8,
356                     SectionStringsCommonItem9,
357                     SectionStringsCommonItem10
358                     ]
359
360    for Item in CommonStringList:
361        CommonBin = PrepareTest(Item)
362        if Item == SectionStringsCommonItem10 or \
363           Item == SectionStringsCommonItem1:
364
365            try:
366                InfBinariesInstance.SetBinary(CommonBinary = CommonBin, ArchList = ArchList)
367            except:
368                pass
369
370        else:
371            try:
372                InfBinariesInstance.SetBinary(Ver = Ver, ArchList = ArchList)
373            except:
374                print("Test Failed!")
375                AllPassedFlag = False
376
377    if AllPassedFlag :
378        print('All tests passed...')
379    else:
380        print('Some unit test failed!')
381
382