diff --git a/monasca_common/tests/validation/test_metric_validation.py b/monasca_common/tests/validation/test_metric_validation.py index 29595cc4..ff24f876 100644 --- a/monasca_common/tests/validation/test_metric_validation.py +++ b/monasca_common/tests/validation/test_metric_validation.py @@ -1,3 +1,4 @@ +# coding=utf-8 # (C) Copyright 2016-2017 Hewlett Packard Enterprise Development LP # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +15,7 @@ # limitations under the License. from oslotest import base +import codecs import six from monasca_common.validation import metrics as metric_validator @@ -27,6 +29,47 @@ valid_dimension_chars = " .'_-" invalid_dimension_chars = "<>={},\"\\\\;&" +def _hex_to_unicode(hex_raw): + hex_raw = six.b(hex_raw.replace(' ', '')) + hex_str_raw = codecs.getdecoder('hex')(hex_raw)[0] + hex_str = hex_str_raw.decode('utf-8', 'replace') + return hex_str + +# NOTE(trebskit) => http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt +UNICODE_MESSAGES = [ + # 1 correct UTF-8 text + {'case': 'greek', 'input': 'κόσμε'}, + # 2.3 Other boundary conditions + {'case': 'stress_2_3_1', 'input': _hex_to_unicode('ed 9f bf')}, + {'case': 'stress_2_3_2', 'input': _hex_to_unicode('ee 80 80')}, + {'case': 'stress_2_3_3', 'input': _hex_to_unicode('ef bf bd')}, + {'case': 'stress_2_3_4', 'input': _hex_to_unicode('f4 8f bf bf')}, + {'case': 'stress_2_3_5', 'input': _hex_to_unicode('f4 90 80 80')}, + # 3.5 Impossible byes + {'case': 'stress_3_5_1', 'input': _hex_to_unicode('fe')}, + {'case': 'stress_3_5_2', 'input': _hex_to_unicode('ff')}, + {'case': 'stress_3_5_3', 'input': _hex_to_unicode('fe fe ff ff')}, + # 4.1 Examples of an overlong ASCII character + {'case': 'stress_4_1_1', 'input': _hex_to_unicode('c0 af')}, + {'case': 'stress_4_1_2', 'input': _hex_to_unicode('e0 80 af')}, + {'case': 'stress_4_1_3', 'input': _hex_to_unicode('f0 80 80 af')}, + {'case': 'stress_4_1_4', 'input': _hex_to_unicode('f8 80 80 80 af')}, + {'case': 'stress_4_1_5', 'input': _hex_to_unicode('fc 80 80 80 80 af')}, + # 4.2 Maximum overlong sequences + {'case': 'stress_4_2_1', 'input': _hex_to_unicode('c1 bf')}, + {'case': 'stress_4_2_2', 'input': _hex_to_unicode('e0 9f bf')}, + {'case': 'stress_4_2_3', 'input': _hex_to_unicode('f0 8f bf bf')}, + {'case': 'stress_4_2_4', 'input': _hex_to_unicode('f8 87 bf bf bf')}, + {'case': 'stress_4_2_5', 'input': _hex_to_unicode('fc 83 bf bf bf bf')}, + # 4.3 Overlong representation of the NUL character + {'case': 'stress_4_3_1', 'input': _hex_to_unicode('c0 80')}, + {'case': 'stress_4_3_2', 'input': _hex_to_unicode('e0 80 80')}, + {'case': 'stress_4_3_3', 'input': _hex_to_unicode('f0 80 80 80')}, + {'case': 'stress_4_3_4', 'input': _hex_to_unicode('f8 80 80 80 80')}, + {'case': 'stress_4_3_5', 'input': _hex_to_unicode('fc 80 80 80 80 80')} +] + + class TestMetricValidation(base.BaseTestCase): def test_valid_single_metric(self): metric = {"name": "test_metric_name", @@ -56,19 +99,21 @@ class TestMetricValidation(base.BaseTestCase): def test_valid_metric_unicode_dimension_value(self): metric = {"name": "test_metric_name", "timestamp": 1405630174123, - "dimensions": {six.unichr(2440): 'B', 'B': 'C', 'D': 'E'}, + "dimensions": {UNICODE_MESSAGES[0]['input']: 'B', 'B': 'C', + 'D': 'E'}, "value": 5} metric_validator.validate(metric) def test_valid_metric_unicode_dimension_key(self): metric = {"name": 'test_metric_name', - "dimensions": {'A': 'B', 'B': six.unichr(920), 'D': 'E'}, + "dimensions": {'A': 'B', 'B': UNICODE_MESSAGES[0]['input'], + 'D': 'E'}, "timestamp": 1405630174123, "value": 5} metric_validator.validate(metric) def test_valid_metric_unicode_metric_name(self): - metric = {"name": six.unichr(6021), + metric = {"name": UNICODE_MESSAGES[0]['input'], "dimensions": {"key1": "value1", "key2": "value2"}, "timestamp": 1405630174123,