FreeRTOS: HTTPS Client
HTTPS Client v1.0.0 library
Return to main page ↑
iot_tests_https_common.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS HTTPS Client V1.1.3
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25 
30 #ifndef IOT_TESTS_HTTPS_COMMON_H_
31 #define IOT_TESTS_HTTPS_COMMON_H_
32 
33 /* The config header is always included first. */
34 #include "iot_config.h"
35 
36 /* C standard includes. */
37 #include <string.h>
38 
39 /* SDK initialization include. */
40 #include "iot_init.h"
41 
42 /* HTTPS Client internal include. */
43 #include "private/iot_https_internal.h"
44 
45 /* Test access include. */
46 #include "iot_test_access_https.h"
47 
48 /* Credential includes. */
49 #include "aws_clientcredential.h"
50 #include "aws_clientcredential_keys.h"
51 
52 /* Test framework includes. */
53 #include "unity_fixture.h"
54 
55 /*-----------------------------------------------------------*/
56 
60 #define HTTPS_TEST_PORT ( ( uint16_t ) 443 )
61 
65 #define HTTPS_TEST_ADDRESS "www.amazon.com "
66 
70 #define HTTPS_TEST_PATH "/path.txt"
71 
75 #define HTTPS_TEST_ALPN_PROTOCOL "http/1.1"
76 
80 #define HTTPS_TEST_ROOT_CA \
81  "-----BEGIN CERTIFICATE-----\n" \
82  "MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\n" \
83  "RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\n" \
84  "VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\n" \
85  "DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\n" \
86  "ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\n" \
87  "VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\n" \
88  "mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\n" \
89  "IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\n" \
90  "mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\n" \
91  "XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\n" \
92  "dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\n" \
93  "jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\n" \
94  "BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\n" \
95  "DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\n" \
96  "9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\n" \
97  "jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\n" \
98  "Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\n" \
99  "ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\n" \
100  "R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\n" \
101  "-----END CERTIFICATE-----\n"
102 
103 
107 #define HTTPS_TEST_CONN_USER_BUFFER_SIZE ( sizeof( _httpsConnection_t ) )
108 
112 #define HTTPS_TEST_REQ_USER_BUFFER_SIZE ( 512 )
113 
119 #define HTTPS_TEST_RESP_USER_BUFFER_SIZE ( sizeof( _httpsResponse_t ) + 128 )
120 
124 #define HTTPS_TEST_RESP_BODY_BUFFER_SIZE ( 256 )
125 
132 #define HTTPS_TEST_RESPONSE_MESSAGE_LENGTH ( 1024 )
133 
137 #define HTTPS_TEST_RESP_HEADER_BUFFER_LENGTH ( HTTPS_TEST_RESP_USER_BUFFER_SIZE - sizeof( _httpsResponse_t ) )
138 
142 #define HTTPS_TEST_REQUEST_BODY \
143  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore " \
144  "magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo" \
145  " consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla paria" \
146  "tur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est lab" \
147  "orum."
148 #define HTTPS_TEST_REQUEST_BODY_LENGTH ( sizeof( HTTPS_TEST_REQUEST_BODY ) - 1 )
153 #define HTTPS_TEST_SMALL_RESPONSE \
154  "HTTP/1.1 200 OK\r\nheader0: value0\r\nheader1: value1\r\n" \
155  "header2: value2 value2a\r\nContent-Length: 26\r\n\r\nabcdefghijklmnopqrstuvwxyz"
156 #define HTTPS_TEST_SMALL_RESPONSE_LENGTH ( sizeof( HTTPS_TEST_SMALL_RESPONSE ) - 1 )
161 #define HTTPS_TEST_CHUNKED_RESPONSE \
162  "HTTP/1.1 403 Forbidden\r\n" \
163  "header0: value0\r\n" \
164  "header1: value1\r\n" \
165  "Transfer-Encoding: chunked\r\n" \
166  "\r\n" \
167  "b\r\n" \
168  "abcdefghijk\r\n" \
169  "c\r\n" \
170  "lmnopqrstuvw\r\n" \
171  "3\r\n" \
172  "xyz\r\n" \
173  "0\r\n" \
174  "\r\n"
175 #define HTTPS_TEST_CHUNKED_RESPONSE_BODY_LENGTH ( 0xB + 0xC + 3 )
177 /*-----------------------------------------------------------*/
178 
187 
196 
205 
214 
223 
230 
236 
242  .addressLen = sizeof( HTTPS_TEST_ADDRESS ) - 1,
243  .port = HTTPS_TEST_PORT,
244  .flags = 0,
245  .pCaCert = HTTPS_TEST_ROOT_CA,
246  .caCertLen = sizeof( HTTPS_TEST_ROOT_CA ),
247  .userBuffer.pBuffer = _pConnUserBuffer,
248  .userBuffer.bufferLen = sizeof( _pConnUserBuffer ),
249  .pClientCert = keyCLIENT_CERTIFICATE_PEM,
250  .clientCertLen = sizeof( keyCLIENT_CERTIFICATE_PEM ),
251  .pPrivateKey = keyCLIENT_PRIVATE_KEY_PEM,
252  .privateKeyLen = sizeof( keyCLIENT_PRIVATE_KEY_PEM ),
253  .pAlpnProtocols = HTTPS_TEST_ALPN_PROTOCOL,
254  .alpnProtocolsLen = sizeof( HTTPS_TEST_ALPN_PROTOCOL ),
255  .pNetworkInterface = &_networkInterface
256 };
257 
258 /*-----------------------------------------------------------*/
259 
263 static inline IotNetworkError_t _networkCreateSuccess( void * pConnectionInfo,
264  void * pCredentialInfo,
265  void ** pConnection )
266 {
267  ( void ) pConnectionInfo;
268  ( void ) pCredentialInfo;
269  ( void ) pConnection;
270  return IOT_NETWORK_SUCCESS;
271 }
272 
273 /*-----------------------------------------------------------*/
274 
278 static inline IotNetworkError_t _setReceiveCallbackSuccess( void * pConnection,
279  IotNetworkReceiveCallback_t receiveCallback,
280  void * pContext )
281 {
282  ( void ) pConnection;
283  ( void ) receiveCallback;
284  ( void ) pContext;
285  return IOT_NETWORK_SUCCESS;
286 }
287 
288 /*-----------------------------------------------------------*/
289 
294 {
296 
299  IotHttpsClient_Connect( &connHandle, &_connInfo );
300  return connHandle;
301 }
302 
303 /*-----------------------------------------------------------*/
304 
309 {
311 
312  IotHttpsClient_InitializeRequest( &reqHandle, pReqInfo );
313  return reqHandle;
314 }
315 
316 /*-----------------------------------------------------------*/
317 
322  IotHttpsRequestHandle_t reqHandle )
323 {
325 
326  IotTestHttps_initializeResponse( &respHandle, pRespInfo, reqHandle );
327  return respHandle;
328 }
329 
330 /*-----------------------------------------------------------*/
331 
335 static inline IotNetworkError_t _networkCloseSuccess( void * pConnection )
336 {
337  ( void ) pConnection;
338  /* When the network closes there should be no data on the socket. */
340  ( void ) memset( _pRespMessageBuffer, 0x00, sizeof( _pRespMessageBuffer ) );
341  return IOT_NETWORK_SUCCESS;
342 }
343 
344 /*-----------------------------------------------------------*/
345 
349 static inline IotNetworkError_t _networkDestroySuccess( void * pConnection )
350 {
351  ( void ) pConnection;
352  return IOT_NETWORK_SUCCESS;
353 }
354 
355 
356 
357 /*-----------------------------------------------------------*/
358 
362 static inline size_t _networkReceiveSuccess( void * pConnection,
363  uint8_t * pBuffer,
364  size_t bytesRequested )
365 {
366  size_t responseMessageLengthLeft = strlen( ( char * ) _pRespMessageBuffer ) - _nextRespMessageBufferByteToReceive;
367  size_t copyLen = 0;
368 
369  ( void ) pConnection;
370  ( void ) pBuffer;
371  ( void ) bytesRequested;
372 
373  if( responseMessageLengthLeft < bytesRequested )
374  {
375  copyLen = responseMessageLengthLeft;
376  }
377  else
378  {
379  copyLen = bytesRequested;
380  }
381 
382  memcpy( pBuffer, &( _pRespMessageBuffer[ _nextRespMessageBufferByteToReceive ] ), copyLen );
384 
385  return copyLen;
386 }
387 
388 /*-----------------------------------------------------------*/
389 
401 void _generateHttpResponseMessage( int headerLength,
402  int bodyLength );
403 
409 void _verifyHttpResponseBody( int bodyLength,
410  uint8_t * pBody,
411  int startIndex );
412 
413 #endif /* IOT_TESTS_HTTPS_COMMON_H_ */
_pReqUserBuffer
uint8_t _pReqUserBuffer[HTTPS_TEST_REQ_USER_BUFFER_SIZE]
HTTPS Request user buffer to share among the tests.
Definition: iot_tests_https_common.c:93
IOT_HTTPS_RESPONSE_HANDLE_INITIALIZER
#define IOT_HTTPS_RESPONSE_HANDLE_INITIALIZER
Initializer for IotHttpsResponseHandle_t.
Definition: iot_https_types.h:194
IotNetworkInterface_t::setReceiveCallback
IotNetworkError_t(* setReceiveCallback)(void *pConnection, IotNetworkReceiveCallback_t receiveCallback, void *pContext)
IotNetworkInterface_t::create
IotNetworkError_t(* create)(void *pConnectionInfo, void *pCredentialInfo, void **pConnection)
_networkCloseSuccess
static IotNetworkError_t _networkCloseSuccess(void *pConnection)
Network Abstraction close function that succeeds.
Definition: iot_tests_https_common.h:335
IotHttpsResponseHandle_t
struct _httpsResponse * IotHttpsResponseHandle_t
Opaque handle of an HTTP response.
Definition: iot_https_types.h:280
IotHttpsRequestInfo_t
HTTP request configuration.
Definition: iot_https_types.h:796
IotHttpsResponseInfo_t
HTTP request configuration.
Definition: iot_https_types.h:884
IotHttpsRequestHandle_t
struct _httpsRequest * IotHttpsRequestHandle_t
Opaque handle of an HTTP request.
Definition: iot_https_types.h:263
IOT_HTTPS_CONNECTION_HANDLE_INITIALIZER
#define IOT_HTTPS_CONNECTION_HANDLE_INITIALIZER
Initializer for IotHttpsConnectionHandle_t.
Definition: iot_https_types.h:190
IotHttpsConnectionHandle_t
struct _httpsConnection * IotHttpsConnectionHandle_t
Opaque handle of an HTTP connection.
Definition: iot_https_types.h:248
IOT_NETWORK_SUCCESS
IOT_NETWORK_SUCCESS
_pRespMessageBuffer
uint8_t _pRespMessageBuffer[HTTPS_TEST_RESPONSE_MESSAGE_LENGTH]
An HTTP response message to share among the tests.
Definition: iot_tests_https_common.c:120
IotNetworkInterface_t
IotHttpsClient_InitializeRequest
IotHttpsReturnCode_t IotHttpsClient_InitializeRequest(IotHttpsRequestHandle_t *pReqHandle, IotHttpsRequestInfo_t *pReqInfo)
Initializes the request by adding a formatted Request-Line to the start of HTTPS request header buffe...
Definition: iot_https_client.c:2731
_networkReceiveSuccess
static size_t _networkReceiveSuccess(void *pConnection, uint8_t *pBuffer, size_t bytesRequested)
Network abstraction receive function that succeeds.
Definition: iot_tests_https_common.h:362
_getRespHandle
static IotHttpsResponseHandle_t _getRespHandle(IotHttpsResponseInfo_t *pRespInfo, IotHttpsRequestHandle_t reqHandle)
Get a valid response handle using _pRespUserBuffer, and respInfoGET.
Definition: iot_tests_https_common.h:321
HTTPS_TEST_RESP_BODY_BUFFER_SIZE
#define HTTPS_TEST_RESP_BODY_BUFFER_SIZE
The size of the response body buffer to use among the tests.
Definition: iot_tests_https_common.h:124
IotHttpsConnectionInfo_t::pAddress
const char * pAddress
Remote server address that is DNS discoverable.
Definition: iot_https_types.h:727
_setReceiveCallbackSuccess
static IotNetworkError_t _setReceiveCallbackSuccess(void *pConnection, IotNetworkReceiveCallback_t receiveCallback, void *pContext)
Network Abstraction setReceiveCallback that succeeds.
Definition: iot_tests_https_common.h:278
_networkCreateSuccess
static IotNetworkError_t _networkCreateSuccess(void *pConnectionInfo, void *pCredentialInfo, void **pConnection)
Network Abstraction create function that succeeds.
Definition: iot_tests_https_common.h:263
HTTPS_TEST_REQ_USER_BUFFER_SIZE
#define HTTPS_TEST_REQ_USER_BUFFER_SIZE
The size of the request user buffer to use among the tests.
Definition: iot_tests_https_common.h:112
IotNetworkError_t
IotNetworkError_t
HTTPS_TEST_ALPN_PROTOCOL
#define HTTPS_TEST_ALPN_PROTOCOL
Test HTTP/1.1 protocol to share among the tests.
Definition: iot_tests_https_common.h:75
HTTPS_TEST_PORT
#define HTTPS_TEST_PORT
Test TLS TCP port.
Definition: iot_tests_https_common.h:60
HTTPS_TEST_RESP_USER_BUFFER_SIZE
#define HTTPS_TEST_RESP_USER_BUFFER_SIZE
The size of the response user buffer to use among the tests.
Definition: iot_tests_https_common.h:119
_nextRespMessageBufferByteToReceive
static uint32_t _nextRespMessageBufferByteToReceive
The current place in _pRespMessageBuffer to receive the next byte.
Definition: iot_tests_https_common.h:229
_generateHttpResponseMessage
void _generateHttpResponseMessage(int headerLength, int bodyLength)
Generate an test HTTP response message with the specified header length and the specified body length...
Definition: iot_tests_https_common.c:125
IotHttpsClient_Connect
IotHttpsReturnCode_t IotHttpsClient_Connect(IotHttpsConnectionHandle_t *pConnHandle, IotHttpsConnectionInfo_t *pConnInfo)
Explicitly connect to the HTTPS server given the connection configuration pConnConfig.
Definition: iot_https_client.c:2589
iot_test_access_https.h
Declares the functions that provide access to the internal functions and variables of the HTTPS Clien...
_getReqHandle
static IotHttpsRequestHandle_t _getReqHandle(IotHttpsRequestInfo_t *pReqInfo)
Get a valid request handle with the input pReqInfo.
Definition: iot_tests_https_common.h:308
_networkInterface
static IotNetworkInterface_t _networkInterface
An IotNetworkInterface_t to share among the tests.
Definition: iot_tests_https_common.h:235
HTTPS_TEST_CONN_USER_BUFFER_SIZE
#define HTTPS_TEST_CONN_USER_BUFFER_SIZE
The size of the connection user buffer to use among the tests.
Definition: iot_tests_https_common.h:107
HTTPS_TEST_RESPONSE_MESSAGE_LENGTH
#define HTTPS_TEST_RESPONSE_MESSAGE_LENGTH
The maximum length of the HTTP response message buffer shared among the test.
Definition: iot_tests_https_common.h:132
IOT_HTTPS_REQUEST_HANDLE_INITIALIZER
#define IOT_HTTPS_REQUEST_HANDLE_INITIALIZER
Initializer for IotHttpsRequestHandle_t.
Definition: iot_https_types.h:192
_getConnHandle
static IotHttpsConnectionHandle_t _getConnHandle(void)
Get a valid connected state intialized connection handle using _pConnUserBuffer and _connInfo.
Definition: iot_tests_https_common.h:293
HTTPS_TEST_ROOT_CA
#define HTTPS_TEST_ROOT_CA
Baltimore Cybertrust root CA to share among the tests.
Definition: iot_tests_https_common.h:80
_pRespBodyBuffer
uint8_t _pRespBodyBuffer[HTTPS_TEST_RESP_BODY_BUFFER_SIZE]
HTTPS Response body buffer to share among the tests.
Definition: iot_tests_https_common.c:111
_networkDestroySuccess
static IotNetworkError_t _networkDestroySuccess(void *pConnection)
Network abstraction destroy function that succeeds.
Definition: iot_tests_https_common.h:349
IotTestHttps_initializeResponse
IotHttpsReturnCode_t IotTestHttps_initializeResponse(IotHttpsResponseHandle_t *pRespHandle, IotHttpsResponseInfo_t *pRespInfo, IotHttpsRequestHandle_t reqHandle)
Test access function for _initializeResponse.
Definition: iot_test_access_https_client.c:38
_pConnUserBuffer
uint8_t _pConnUserBuffer[HTTPS_TEST_CONN_USER_BUFFER_SIZE]
HTTPS Client connection user buffer to share among the tests.
Definition: iot_tests_https_common.c:84
_connInfo
static IotHttpsConnectionInfo_t _connInfo
A IotHttpsConnectionInfo_t to share among the tests.
Definition: iot_tests_https_common.h:240
_verifyHttpResponseBody
void _verifyHttpResponseBody(int bodyLength, uint8_t *pBody, int startIndex)
Test verify the response body in pBody up to bodyLength.
Definition: iot_tests_https_common.c:235
HTTPS_TEST_ADDRESS
#define HTTPS_TEST_ADDRESS
Test address to share among the tests.
Definition: iot_tests_https_common.h:65
IotHttpsConnectionInfo_t
HTTP connection configuration.
Definition: iot_https_types.h:721
_pRespUserBuffer
uint8_t _pRespUserBuffer[HTTPS_TEST_RESP_USER_BUFFER_SIZE]
HTTPS Response user buffer to share among the tests.
Definition: iot_tests_https_common.c:102