test_signatures.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from openleadr.utils import generate_id, certificate_fingerprint, ensure_bytes
  13. from openleadr.messaging import create_message, parse_message, validate_xml_signature, validate_xml_schema
  14. from hashlib import sha256
  15. from base64 import b64encode
  16. from datetime import datetime, timedelta, timezone
  17. from lxml import etree
  18. import os
  19. with open(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))), 'certificates', 'dummy_ven.crt'), 'rb') as file:
  20. TEST_CERT = file.read()
  21. with open(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))), 'certificates', 'dummy_ven.key'), 'rb') as file:
  22. TEST_KEY = file.read()
  23. TEST_KEY_PASSWORD = 'openadr'
  24. def test_message_validation():
  25. msg = create_message('oadrPoll', ven_id='123', cert=TEST_CERT, key=TEST_KEY)
  26. tree = etree.fromstring(msg.encode('utf-8'))
  27. validate_xml_signature(tree)
  28. parsed_type, parsed_message = parse_message(msg)
  29. assert parsed_type == 'oadrPoll'
  30. def test_message_validation_complex():
  31. now = datetime.now(timezone.utc)
  32. event_id = generate_id()
  33. active_period = {"dtstart": now + timedelta(minutes=1),
  34. "duration": timedelta(minutes=9)}
  35. event_descriptor = {"event_id": event_id,
  36. "modification_number": 1,
  37. "modification_date_time": now,
  38. "priority": 1,
  39. "market_context": "http://MarketContext1",
  40. "created_date_time": now,
  41. "event_status": "near",
  42. "test_event": "false",
  43. "vtn_comment": "This is an event"}
  44. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 8},
  45. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 10},
  46. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 12},
  47. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 14},
  48. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 16},
  49. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 18},
  50. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 20},
  51. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 10},
  52. {"duration": timedelta(minutes=1), "uid": 9, "signal_payload": 20}],
  53. "signal_name": "LOAD_CONTROL",
  54. #"signal_name": "simple",
  55. #"signal_type": "level",
  56. "signal_type": "x-loadControlCapacity",
  57. "signal_id": generate_id(),
  58. "current_value": 9.99}]
  59. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  60. event = {'active_period': active_period,
  61. 'event_descriptor': event_descriptor,
  62. 'event_signals': event_signals,
  63. 'targets': event_targets,
  64. 'response_required': 'always'}
  65. msg = create_message('oadrDistributeEvent',
  66. request_id=generate_id(),
  67. response={'request_id': 123, 'response_code': 200, 'response_description': 'OK'},
  68. events=[event],
  69. cert=TEST_CERT,
  70. key=TEST_KEY)
  71. tree = etree.fromstring(msg.encode('utf-8'))
  72. validate_xml_signature(tree)
  73. parsed_type, parsed_msg = parse_message(msg)