]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Tests/CheckUnicodeSourceFiles.py
BaseTools: Remove types.TypeType
[mirror_edk2.git] / BaseTools / Tests / CheckUnicodeSourceFiles.py
index 0083ad8589ba8caa098f7f72898dd8d541ff5cc3..6ae62f180a840090141d4fdf23aafe0e3a7e99c8 100644 (file)
@@ -38,7 +38,10 @@ class Tests(TestTools.BaseToolsTest):
     def EncodeToFile(self, encoding, string=None):\r
         if string is None:\r
             string = self.SampleData\r
-        data = codecs.encode(string, encoding)\r
+        if encoding is not None:\r
+            data = codecs.encode(string, encoding)\r
+        else:\r
+            data = string\r
         path = 'input.uni'\r
         self.WriteTmpFile(path, data)\r
         return PathClass(self.GetTmpFilePath(path))\r
@@ -81,6 +84,96 @@ class Tests(TestTools.BaseToolsTest):
     def testUtf16InUniFile(self):\r
         self.CheckFile('utf_16', shouldPass=True)\r
 \r
+    def testSupplementaryPlaneUnicodeCharInUtf16File(self):\r
+        #\r
+        # Supplementary Plane characters can exist in UTF-16 files,\r
+        # but they are not valid UCS-2 characters.\r
+        #\r
+        # This test makes sure that BaseTools rejects these characters\r
+        # if seen in a .uni file.\r
+        #\r
+        data = u'''\r
+            #langdef en-US "English"\r
+            #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"\r
+        '''\r
+\r
+        self.CheckFile('utf_16', shouldPass=False, string=data)\r
+\r
+    def testSurrogatePairUnicodeCharInUtf16File(self):\r
+        #\r
+        # Surrogate Pair code points are used in UTF-16 files to\r
+        # encode the Supplementary Plane characters. But, a Surrogate\r
+        # Pair code point which is not followed by another Surrogate\r
+        # Pair code point might be interpreted as a single code point\r
+        # with the Surrogate Pair code point.\r
+        #\r
+        # This test makes sure that BaseTools rejects these characters\r
+        # if seen in a .uni file.\r
+        #\r
+        data = codecs.BOM_UTF16_LE + '//\x01\xd8 '\r
+\r
+        self.CheckFile(encoding=None, shouldPass=False, string=data)\r
+\r
+    def testValidUtf8File(self):\r
+        self.CheckFile(encoding='utf_8', shouldPass=True)\r
+\r
+    def testValidUtf8FileWithBom(self):\r
+        #\r
+        # Same test as testValidUtf8File, but add the UTF-8 BOM\r
+        #\r
+        data = codecs.BOM_UTF8 + codecs.encode(self.SampleData, 'utf_8')\r
+\r
+        self.CheckFile(encoding=None, shouldPass=True, string=data)\r
+\r
+    def test32bitUnicodeCharInUtf8File(self):\r
+        data = u'''\r
+            #langdef en-US "English"\r
+            #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"\r
+        '''\r
+\r
+        self.CheckFile('utf_16', shouldPass=False, string=data)\r
+\r
+    def test32bitUnicodeCharInUtf8File(self):\r
+        data = u'''\r
+            #langdef en-US "English"\r
+            #string STR_A #language en-US "CodePoint (\U00010300) > 0xFFFF"\r
+        '''\r
+\r
+        self.CheckFile('utf_8', shouldPass=False, string=data)\r
+\r
+    def test32bitUnicodeCharInUtf8Comment(self):\r
+        data = u'''\r
+            // Even in comments, we reject non-UCS-2 chars: \U00010300\r
+            #langdef en-US "English"\r
+            #string STR_A #language en-US "A"\r
+        '''\r
+\r
+        self.CheckFile('utf_8', shouldPass=False, string=data)\r
+\r
+    def testSurrogatePairUnicodeCharInUtf8File(self):\r
+        #\r
+        # Surrogate Pair code points are used in UTF-16 files to\r
+        # encode the Supplementary Plane characters. In UTF-8, it is\r
+        # trivial to encode these code points, but they are not valid\r
+        # code points for characters, since they are reserved for the\r
+        # UTF-16 Surrogate Pairs.\r
+        #\r
+        # This test makes sure that BaseTools rejects these characters\r
+        # if seen in a .uni file.\r
+        #\r
+        data = '\xed\xa0\x81'\r
+\r
+        self.CheckFile(encoding=None, shouldPass=False, string=data)\r
+\r
+    def testSurrogatePairUnicodeCharInUtf8FileWithBom(self):\r
+        #\r
+        # Same test as testSurrogatePairUnicodeCharInUtf8File, but add\r
+        # the UTF-8 BOM\r
+        #\r
+        data = codecs.BOM_UTF8 + '\xed\xa0\x81'\r
+\r
+        self.CheckFile(encoding=None, shouldPass=False, string=data)\r
+\r
 TheTestSuite = TestTools.MakeTheTestSuite(locals())\r
 \r
 if __name__ == '__main__':\r