Skip to content

Commit ea4887b

Browse files
committed
chore: add test for unhandled STUN messages
1 parent 10d5146 commit ea4887b

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ set(TESTS_SOURCES
7878
${CMAKE_CURRENT_SOURCE_DIR}/test/server.c
7979
${CMAKE_CURRENT_SOURCE_DIR}/test/conflict.c
8080
${CMAKE_CURRENT_SOURCE_DIR}/test/bind.c
81-
${CMAKE_CURRENT_SOURCE_DIR}/test/ufrag.c
81+
${CMAKE_CURRENT_SOURCE_DIR}/test/ufrag.c
82+
${CMAKE_CURRENT_SOURCE_DIR}/test/stun-unhandled.c
8283
)
8384
source_group("Test Files" FILES "${TESTS_SOURCES}")
8485

test/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int test_turn(void);
2222
int test_conflict(void);
2323
int test_bind(void);
2424
int test_ufrag(void);
25+
int test_stun_unhandled(void);
2526

2627
#ifndef NO_SERVER
2728
int test_server(void);
@@ -104,6 +105,12 @@ int main(int argc, char **argv) {
104105
return -1;
105106
}
106107

108+
printf("\nRunning unhandled stun message test...\n");
109+
if (test_stun_unhandled()) {
110+
fprintf(stderr, "Unhandled stun message test failed\n");
111+
return -1;
112+
}
113+
107114
#ifndef NO_SERVER
108115
printf("\nRunning server test...\n");
109116
if (test_server()) {

test/stun-unhandled.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright (c) 2022 Paul-Louis Ageneau
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#include "juice/juice.h"
10+
11+
#include <stdbool.h>
12+
#include <stdint.h>
13+
#include <stdio.h>
14+
#include <string.h>
15+
16+
#ifdef _WIN32
17+
#include <windows.h>
18+
static void sleep(unsigned int secs) { Sleep(secs * 1000); }
19+
#else
20+
#include <unistd.h> // for sleep
21+
#endif
22+
23+
#define BUFFER_SIZE 4096
24+
25+
static juice_agent_t *localAgent;
26+
static juice_agent_t *remoteAgent;
27+
static bool success;
28+
29+
void unhandled_stun_callback (const juice_mux_binding_request_t *info, void *user_ptr) {
30+
success = true;
31+
}
32+
33+
int test_stun_unhandled() {
34+
juice_set_log_level(JUICE_LOG_LEVEL_DEBUG);
35+
36+
uint16_t port = 60001;
37+
38+
// Set up callback
39+
juice_mux_listen("127.0.0.1", port, &unhandled_stun_callback, NULL);
40+
41+
// Create local agent
42+
juice_config_t localConfig;
43+
memset(&localConfig, 0, sizeof(localConfig));
44+
localConfig.concurrency_mode = JUICE_CONCURRENCY_MODE_MUX;
45+
localConfig.local_port_range_begin = port;
46+
localConfig.local_port_range_end = port;
47+
localAgent = juice_create(&localConfig);
48+
49+
// Gather local candidates
50+
juice_gather_candidates(localAgent);
51+
52+
// Generate local description
53+
char localSdp[JUICE_MAX_SDP_STRING_LEN];
54+
juice_get_local_description(localAgent, localSdp, JUICE_MAX_SDP_STRING_LEN);
55+
printf("Local description 1:\n%s\n", localSdp);
56+
57+
// Destroy local agent
58+
juice_destroy(localAgent);
59+
60+
// Create remote agent
61+
juice_config_t remoteConfig;
62+
memset(&remoteConfig, 0, sizeof(remoteConfig));
63+
localConfig.concurrency_mode = JUICE_CONCURRENCY_MODE_MUX;
64+
remoteAgent = juice_create(&remoteConfig);
65+
66+
// Remote agent: Receive description from local agent
67+
juice_set_remote_description(remoteAgent, localSdp);
68+
69+
// Remote agent: Gather candidates (and send them to local agent)
70+
juice_gather_candidates(remoteAgent);
71+
sleep(2);
72+
73+
// -- Should have received unhandled STUN packet(s) --
74+
75+
// Destroy remote agent
76+
juice_destroy(remoteAgent);
77+
78+
if (success) {
79+
printf("Success\n");
80+
return 0;
81+
} else {
82+
printf("Failure\n");
83+
return -1;
84+
}
85+
}

0 commit comments

Comments
 (0)