test_signatures.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(__file__)), 'cert.pem'), 'rb') as file:
  20. TEST_CERT = file.read()
  21. with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'key.pem'), '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, passphrase='openadr')
  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. passphrase='openadr')
  72. tree = etree.fromstring(msg.encode('utf-8'))
  73. validate_xml_signature(tree)
  74. parsed_type, parsed_msg = parse_message(msg)