example_test.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2021 Espressif Systems (Shanghai) CO LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import http.client
  17. import os
  18. import re
  19. import ssl
  20. import tiny_test_fw
  21. import ttfw_idf
  22. from tiny_test_fw import Utility
  23. server_cert_pem = '-----BEGIN CERTIFICATE-----\n'\
  24. 'MIIDKzCCAhOgAwIBAgIUBxM3WJf2bP12kAfqhmhhjZWv0ukwDQYJKoZIhvcNAQEL\n'\
  25. 'BQAwJTEjMCEGA1UEAwwaRVNQMzIgSFRUUFMgc2VydmVyIGV4YW1wbGUwHhcNMTgx\n'\
  26. 'MDE3MTEzMjU3WhcNMjgxMDE0MTEzMjU3WjAlMSMwIQYDVQQDDBpFU1AzMiBIVFRQ\n'\
  27. 'UyBzZXJ2ZXIgZXhhbXBsZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n'\
  28. 'ALBint6nP77RCQcmKgwPtTsGK0uClxg+LwKJ3WXuye3oqnnjqJCwMEneXzGdG09T\n'\
  29. 'sA0SyNPwrEgebLCH80an3gWU4pHDdqGHfJQa2jBL290e/5L5MB+6PTs2NKcojK/k\n'\
  30. 'qcZkn58MWXhDW1NpAnJtjVniK2Ksvr/YIYSbyD+JiEs0MGxEx+kOl9d7hRHJaIzd\n'\
  31. 'GF/vO2pl295v1qXekAlkgNMtYIVAjUy9CMpqaQBCQRL+BmPSJRkXBsYk8GPnieS4\n'\
  32. 'sUsp53DsNvCCtWDT6fd9D1v+BB6nDk/FCPKhtjYOwOAZlX4wWNSZpRNr5dfrxKsb\n'\
  33. 'jAn4PCuR2akdF4G8WLUeDWECAwEAAaNTMFEwHQYDVR0OBBYEFMnmdJKOEepXrHI/\n'\
  34. 'ivM6mVqJgAX8MB8GA1UdIwQYMBaAFMnmdJKOEepXrHI/ivM6mVqJgAX8MA8GA1Ud\n'\
  35. 'EwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBADiXIGEkSsN0SLSfCF1VNWO3\n'\
  36. 'emBurfOcDq4EGEaxRKAU0814VEmU87btIDx80+z5Dbf+GGHCPrY7odIkxGNn0DJY\n'\
  37. 'W1WcF+DOcbiWoUN6DTkAML0SMnp8aGj9ffx3x+qoggT+vGdWVVA4pgwqZT7Ybntx\n'\
  38. 'bkzcNFW0sqmCv4IN1t4w6L0A87ZwsNwVpre/j6uyBw7s8YoJHDLRFT6g7qgn0tcN\n'\
  39. 'ZufhNISvgWCVJQy/SZjNBHSpnIdCUSJAeTY2mkM4sGxY0Widk8LnjydxZUSxC3Nl\n'\
  40. 'hb6pnMh3jRq4h0+5CZielA4/a+TdrNPv/qok67ot/XJdY3qHCCd8O2b14OVq9jo=\n'\
  41. '-----END CERTIFICATE-----\n'
  42. success_response = '<h1>Hello Secure World!</h1>'
  43. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols')
  44. def test_examples_protocol_https_server_simple(env, extra_data): # type: (tiny_test_fw.Env.Env, None) -> None # pylint: disable=unused-argument
  45. """
  46. steps: |
  47. 1. join AP
  48. 2. connect to www.howsmyssl.com:443
  49. 3. send http request
  50. """
  51. dut1 = env.get_dut('https_server_simple', 'examples/protocols/https_server/simple', dut_class=ttfw_idf.ESP32DUT)
  52. # check and log bin size
  53. binary_file = os.path.join(dut1.app.binary_path, 'https_server.bin')
  54. bin_size = os.path.getsize(binary_file)
  55. ttfw_idf.log_performance('https_server_simple_bin_size', '{}KB'.format(bin_size // 1024))
  56. # start test
  57. dut1.start_app()
  58. # Parse IP address and port of the server
  59. dut1.expect(re.compile(r'Starting server'))
  60. got_port = dut1.expect(re.compile(r'Server listening on port (\d+)'), timeout=30)[0]
  61. Utility.console_log('Waiting to connect with AP')
  62. got_ip = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0]
  63. # Expected logs
  64. Utility.console_log('Got IP : ' + got_ip)
  65. Utility.console_log('Got Port : ' + got_port)
  66. Utility.console_log('Performing GET request over an SSL connection with the server')
  67. ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
  68. ssl_context.verify_mode = ssl.CERT_REQUIRED
  69. ssl_context.check_hostname = False
  70. ssl_context.load_verify_locations(cadata=server_cert_pem)
  71. conn = http.client.HTTPSConnection(got_ip, got_port, context=ssl_context)
  72. Utility.console_log('Performing SSL handshake with the server')
  73. conn.request('GET','/')
  74. resp = conn.getresponse()
  75. dut1.expect('performing session handshake')
  76. got_resp = resp.read().decode('utf-8')
  77. # Close the connection
  78. if got_resp != success_response:
  79. Utility.console_log('Response obtained does not match with correct response')
  80. raise RuntimeError('Failed to test SSL connection')
  81. Utility.console_log('Correct response obtained')
  82. Utility.console_log('SSL connection test successful\nClosing the connection')
  83. conn.close()
  84. if __name__ == '__main__':
  85. test_examples_protocol_https_server_simple() # pylint: disable=no-value-for-parameter