]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Tests/CheckUnicodeSourceFiles.py
2 # Unit tests for AutoGen.UniClassObject
4 # Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
19 from Common
.Misc
import PathClass
20 import AutoGen
.UniClassObject
as BtUni
22 from Common
import EdkLogger
23 EdkLogger
.InitializeForUnitTest()
25 class Tests(TestTools
.BaseToolsTest
):
28 #langdef en-US "English"
29 #string STR_A #language en-US "STR_A for en-US"
32 def EncodeToFile(self
, encoding
, string
=None):
34 string
= self
.SampleData
35 if encoding
is not None:
36 data
= codecs
.encode(string
, encoding
)
40 self
.WriteTmpFile(path
, data
)
41 return PathClass(self
.GetTmpFilePath(path
))
43 def ErrorFailure(self
, error
, encoding
, shouldPass
):
44 msg
= error
+ ' should '
47 msg
+= 'be generated for '
48 msg
+= '%s data in a .uni file' % encoding
51 def UnicodeErrorFailure(self
, encoding
, shouldPass
):
52 self
.ErrorFailure('UnicodeError', encoding
, shouldPass
)
54 def EdkErrorFailure(self
, encoding
, shouldPass
):
55 self
.ErrorFailure('EdkLogger.FatalError', encoding
, shouldPass
)
57 def CheckFile(self
, encoding
, shouldPass
, string
=None):
58 path
= self
.EncodeToFile(encoding
, string
)
60 BtUni
.UniFileClassObject([path
])
67 self
.UnicodeErrorFailure(encoding
, shouldPass
)
68 except EdkLogger
.FatalError
:
72 self
.EdkErrorFailure(encoding
, shouldPass
)
76 self
.EdkErrorFailure(encoding
, shouldPass
)
78 def testUtf16InUniFile(self
):
79 self
.CheckFile('utf_16', shouldPass
=True)
81 def testSupplementaryPlaneUnicodeCharInUtf16File(self
):
83 # Supplementary Plane characters can exist in UTF-16 files,
84 # but they are not valid UCS-2 characters.
86 # This test makes sure that BaseTools rejects these characters
87 # if seen in a .uni file.
90 #langdef en-US "English"
91 #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"
94 self
.CheckFile('utf_16', shouldPass
=False, string
=data
)
96 def testSurrogatePairUnicodeCharInUtf16File(self
):
98 # Surrogate Pair code points are used in UTF-16 files to
99 # encode the Supplementary Plane characters. But, a Surrogate
100 # Pair code point which is not followed by another Surrogate
101 # Pair code point might be interpreted as a single code point
102 # with the Surrogate Pair code point.
104 # This test makes sure that BaseTools rejects these characters
105 # if seen in a .uni file.
107 data
= codecs
.BOM_UTF16_LE
+ b
'//\x01\xd8 '
109 self
.CheckFile(encoding
=None, shouldPass
=False, string
=data
)
111 def testValidUtf8File(self
):
112 self
.CheckFile(encoding
='utf_8', shouldPass
=True)
114 def testValidUtf8FileWithBom(self
):
116 # Same test as testValidUtf8File, but add the UTF-8 BOM
118 data
= codecs
.BOM_UTF8
+ codecs
.encode(self
.SampleData
, 'utf_8')
120 self
.CheckFile(encoding
=None, shouldPass
=True, string
=data
)
122 def test32bitUnicodeCharInUtf8File(self
):
124 #langdef en-US "English"
125 #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"
128 self
.CheckFile('utf_16', shouldPass
=False, string
=data
)
130 def test32bitUnicodeCharInUtf8File(self
):
132 #langdef en-US "English"
133 #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"
136 self
.CheckFile('utf_8', shouldPass
=False, string
=data
)
138 def test32bitUnicodeCharInUtf8Comment(self
):
140 // Even in comments, we reject non-UCS-2 chars: \U00010300
141 #langdef en-US "English"
142 #string STR_A #language en-US "A"
145 self
.CheckFile('utf_8', shouldPass
=False, string
=data
)
147 def testSurrogatePairUnicodeCharInUtf8File(self
):
149 # Surrogate Pair code points are used in UTF-16 files to
150 # encode the Supplementary Plane characters. In UTF-8, it is
151 # trivial to encode these code points, but they are not valid
152 # code points for characters, since they are reserved for the
153 # UTF-16 Surrogate Pairs.
155 # This test makes sure that BaseTools rejects these characters
156 # if seen in a .uni file.
158 data
= b
'\xed\xa0\x81'
160 self
.CheckFile(encoding
=None, shouldPass
=False, string
=data
)
162 def testSurrogatePairUnicodeCharInUtf8FileWithBom(self
):
164 # Same test as testSurrogatePairUnicodeCharInUtf8File, but add
167 data
= codecs
.BOM_UTF8
+ b
'\xed\xa0\x81'
169 self
.CheckFile(encoding
=None, shouldPass
=False, string
=data
)
171 TheTestSuite
= TestTools
.MakeTheTestSuite(locals())
173 if __name__
== '__main__':
174 allTests
= TheTestSuite()
175 unittest
.TextTestRunner().run(allTests
)