You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(examples): enhance example with docs, pytest setup, and standalone test server
- Add comprehensive README with TOC and quick start
- Add pytest setup and certificate generation scripts
- Add standalone WebSocket test server with TLS support
- Add troubleshooting and multiple testing approaches
idf.py menuconfig # Configure WiFi/Ethernet and WebSocket URI
28
+
idf.py build
29
+
```
30
+
31
+
3.**Flash and monitor:**
32
+
```bash
33
+
idf.py -p PORT flash monitor
34
+
```
35
+
36
+
4.**Run tests:**
37
+
```bash
38
+
pytest .
39
+
```
4
40
5
41
## How to Use Example
6
42
@@ -15,6 +51,27 @@ This example can be executed on any ESP32 board, the only required interface is
15
51
* Configure the websocket endpoint URI under "Example Configuration", if "WEBSOCKET_URI_FROM_STDIN" is selected then the example application will connect to the URI it reads from stdin (used for testing)
16
52
* To test a WebSocket client example over TLS, please enable one of the following configurations: `CONFIG_WS_OVER_TLS_MUTUAL_AUTH` or `CONFIG_WS_OVER_TLS_SERVER_AUTH`. See the sections below for more details.
17
53
54
+
### Pre-configured SDK Configurations
55
+
56
+
This example includes several pre-configured `sdkconfig.ci.*` files for different testing scenarios:
57
+
58
+
***sdkconfig.ci** - Default configuration with WebSocket over Ethernet (IP101 PHY, ESP32, IPv6) and hardcoded URI.
59
+
***sdkconfig.ci.plain_tcp** - WebSocket over plain TCP (no TLS, URI from stdin) using Ethernet (IP101 PHY, ESP32, IPv6).
60
+
***sdkconfig.ci.mutual_auth** - WebSocket with mutual TLS authentication (client/server certificate verification, skips CN check) and URI from stdin.
61
+
***sdkconfig.ci.dynamic_buffer** - WebSocket with dynamic buffer allocation, Ethernet (IP101 PHY, ESP32, IPv6), and hardcoded URI.
* Mutual Authentication: When `CONFIG_WS_OVER_TLS_MUTUAL_AUTH=y` is enabled, it's essential to provide valid certificates for both the server and client.
@@ -26,7 +83,7 @@ This example can be executed on any ESP32 board, the only required interface is
26
83
Please note: This example represents an extremely simplified approach to generating self-signed certificates/keys with a single common CA, devoid of CN checks, lacking password protection, and featuring hardcoded key sizes and types. It is intended solely for testing purposes.
27
84
In the outlined steps, we are omitting the configuration of the CN (Common Name) field due to the context of a testing environment. However, it's important to recognize that the CN field is a critical element of SSL/TLS certificates, significantly influencing the security and efficacy of HTTPS communications. This field facilitates the verification of a website's identity, enhancing trust and security in web interactions. In practical deployments beyond testing scenarios, ensuring the CN field is accurately set is paramount for maintaining the integrity and reliability of secure communications
28
85
29
-
### Generating a self signed Certificates with OpenSSL
86
+
### Generating a self signed Certificates with OpenSSL manually
30
87
* The example below outlines the process for creating new certificates for both the server and client using OpenSSL, a widely-used command line tool for implementing TLS protocol:
31
88
32
89
```
@@ -63,6 +120,26 @@ Please see the openssl man pages (man openssl) for more details.
63
120
It is **strongly recommended** to not reuse the example certificate in your application;
64
121
it is included only for demonstration.
65
122
123
+
### Certificate Generation Options
124
+
125
+
#### Option 1: Manual OpenSSL Commands
126
+
Follow the step-by-step process in the section above to understand certificate generation.
127
+
128
+
#### Option 2: Automated Script
129
+
**Note:** Test certificates are already available in the example. If you want to regenerate them or create new ones, use the provided `generate_certs.sh` script:
130
+
131
+
```bash
132
+
chmod +x generate_certs.sh
133
+
./generate_certs.sh
134
+
```
135
+
136
+
This script automatically generates all required certificates in the correct directories and cleans up temporary files.
137
+
138
+
#### Option 3: Online Certificate Generators
139
+
-**mkcert**: `install mkcert` then `mkcert -install` and `mkcert localhost`
140
+
-**Let's Encrypt**: For production certificates (free, automated renewal)
141
+
-**Online generators**: Search for "self-signed certificate generator" online
142
+
66
143
### Build and Flash
67
144
68
145
Build the project and flash it to the board, then run monitor tool to view serial output:
@@ -73,7 +150,36 @@ idf.py -p PORT flash monitor
73
150
74
151
(To exit the serial monitor, type ``Ctrl-]``.)
75
152
76
-
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
153
+
See the [ESP-IDF Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
154
+
155
+
## Testing with pytest
156
+
157
+
### Install Dependencies
158
+
159
+
Before running the pytest tests, you need to install the required Python packages:
160
+
161
+
```
162
+
pip install -r esp-protocols/ci/requirements.txt
163
+
```
164
+
165
+
### Run pytest
166
+
167
+
After installing the dependencies, you can run the pytest tests:
168
+
169
+
Run all tests in current directory:
170
+
```
171
+
pytest .
172
+
```
173
+
174
+
Run specific test file:
175
+
```
176
+
pytest pytest_websocket.py
177
+
```
178
+
179
+
To specify the target device or serial port, use:
180
+
```
181
+
pytest --target esp32 --port /dev/ttyUSB0
182
+
```
77
183
78
184
## Example Output
79
185
@@ -105,9 +211,101 @@ W (9162) WEBSOCKET: Received=hello 0003
105
211
```
106
212
107
213
108
-
## Python Flask echo server
214
+
## WebSocket Test Server
215
+
216
+
### Standalone Test Server
217
+
218
+
This example includes a standalone WebSocket test server (`websocket_server.py`) that can be used for testing your ESP32 WebSocket client:
219
+
220
+
#### Quick Start
221
+
222
+
**Plain WebSocket (No TLS):**
223
+
```bash
224
+
# Plain WebSocket server (no encryption)
225
+
python websocket_server.py
226
+
227
+
# Custom port
228
+
python websocket_server.py --port 9000
229
+
```
230
+
231
+
**Server-Only Authentication:**
232
+
```bash
233
+
# TLS WebSocket server (ESP32 verifies server)
234
+
python websocket_server.py --tls
235
+
236
+
# Custom port with TLS
237
+
python websocket_server.py --port 9000 --tls
238
+
```
239
+
240
+
**Mutual Authentication:**
241
+
```bash
242
+
# TLS with client certificate verification (both verify each other)
**Plain WebSocket (No TLS or Client Verification):**
278
+
```bash
279
+
# Basic server (port 8080)
280
+
python websocket_server.py
281
+
282
+
# Custom port
283
+
python websocket_server.py --port 9000
284
+
```
109
285
110
-
By default, the `wss://echo.websocket.org` endpoint is used. You can setup a Python websocket echo server locally and try the `ws://<your-ip>:5000` endpoint. To do this, install Flask-sock Python package
286
+
**Server-Only Authentication (ESP32 verifies server, server doesn't verify ESP32):**
287
+
```bash
288
+
# TLS server without client verification
289
+
python websocket_server.py --tls
290
+
291
+
# Custom port with TLS
292
+
python websocket_server.py --port 9000 --tls
293
+
```
294
+
295
+
**Mutual Authentication (Both ESP32 and server verify each other's certificates):**
The server will display the connection URL (e.g., `wss://192.168.1.100:8080`) that you can use in your ESP32 configuration.
305
+
306
+
### Alternative: Python Flask Echo Server
307
+
308
+
By default, the `wss://echo.websocket.org` endpoint is used. You can also setup a Python Flask websocket echo server locally and try the `ws://<your-ip>:5000` endpoint. To do this, install Flask-sock Python package
0 commit comments