-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix octet string parsing in yaml test parser
Old parsing had issue since str.encode for utf-8 character would be improperly convert. For example `\xff` would become `b'\xc3\xbf'` when we wanted it to be `b'\xff`. Co-authored-by: Tennessee Carmel-Veilleux <[email protected]>
- Loading branch information
1 parent
b87a86b
commit f707749
Showing
3 changed files
with
88 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/controller/python/test/unit_tests/test_yaml_format_converter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# | ||
# Copyright (c) 2022 Project CHIP Authors | ||
# All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from chip.yaml.format_converter import convert_yaml_octet_string_to_bytes | ||
from binascii import unhexlify | ||
import unittest | ||
|
||
|
||
class TestOctetStringYamlDecode(unittest.TestCase): | ||
def test_common_cases(self): | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("hex:aa55"), unhexlify("aa55")) | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("hex:"), unhexlify("")) | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("hex:AA55"), unhexlify("aa55")) | ||
|
||
self.assertEqual(convert_yaml_octet_string_to_bytes("0\xaa\x55"), unhexlify("30aa55")) | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("0\xAA\x55"), unhexlify("30aa55")) | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("0\xAa\x55"), unhexlify("30aa55")) | ||
|
||
self.assertEqual(convert_yaml_octet_string_to_bytes("0hex:"), b"0hex:") | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("0hex:A"), b"0hex:A") | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("0hex:AA55"), b"0hex:AA55") | ||
|
||
self.assertEqual(convert_yaml_octet_string_to_bytes("AA55"), b"AA55") | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("AA\n\r\t55"), unhexlify("41410a0d093535")) | ||
# TODO(#23669): After utf8 is properly supported expected result is unhexlify("c3a9c3a90a0a") | ||
self.assertEqual(convert_yaml_octet_string_to_bytes("\xC3\xA9é\n\n"), unhexlify("c3a9e90a0a")) | ||
|
||
# Partial hex nibble | ||
with self.assertRaises(ValueError): | ||
convert_yaml_octet_string_to_bytes("hex:aa5") | ||
|
||
|
||
def main(): | ||
unittest.main() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |